sleep.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* $Id:$ */
  2. /*
  3. * Copyright (c) 2008 Aaron Turner.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the names of the copyright owners nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  20. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  23. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  25. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  27. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  28. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  29. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include "config.h"
  32. #include "defines.h"
  33. #include "common.h"
  34. #include <sys/types.h>
  35. #include <sys/time.h>
  36. #include <unistd.h>
  37. #include <errno.h>
  38. #include <string.h>
  39. #ifdef HAVE_SYS_EVENT
  40. #include <sys/event.h>
  41. #endif
  42. /* necessary for ioport_sleep() functions */
  43. #ifdef HAVE_SYS_IO_H /* Linux */
  44. #include <sys/io.h>
  45. #elif defined HAVE_ARCHITECTURE_I386_PIO_H /* OS X */
  46. #include <architecture/i386/pio.h>
  47. #endif
  48. float gettimeofday_sleep_value;
  49. int ioport_sleep_value;
  50. void
  51. ioport_sleep_init(void)
  52. {
  53. #ifdef HAVE_IOPERM
  54. ioperm(0x80,1,1);
  55. ioport_sleep_value = inb(0x80);
  56. #else
  57. err(1, "Platform does not support IO Port for timing");
  58. #endif
  59. }
  60. void
  61. ioport_sleep(const struct timespec nap)
  62. {
  63. #ifdef HAVE_IOPERM
  64. struct timeval nap_for;
  65. u_int32_t usec;
  66. time_t i;
  67. TIMESPEC_TO_TIMEVAL(&nap_for, &nap);
  68. /*
  69. * process the seconds, we do this in a loop so we don't have to
  70. * use slower 64bit integers or worry about integer overflows.
  71. */
  72. for (i = 0; i < nap_for.tv_sec; i ++) {
  73. usec = SEC_TO_MICROSEC(nap_for.tv_sec);
  74. while (usec > 0) {
  75. usec --;
  76. outb(ioport_sleep_value, 0x80);
  77. }
  78. }
  79. /* process the usec */
  80. usec = nap.tv_nsec / 1000;
  81. usec --; /* fudge factor for all the above */
  82. while (usec > 0) {
  83. usec --;
  84. outb(ioport_sleep_value, 0x80);
  85. }
  86. #else
  87. err(1, "Platform does not support IO Port for timing");
  88. #endif
  89. }