joy.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* $OpenBSD: joy.c,v 1.15 2015/02/10 21:58:16 miod Exp $ */
  2. /* $NetBSD: joy.c,v 1.3 1996/05/05 19:46:15 christos Exp $ */
  3. /*-
  4. * Copyright (c) 1995 Jean-Marc Zucconi
  5. * All rights reserved.
  6. *
  7. * Ported to NetBSD by Matthieu Herrb <matthieu@laas.fr>
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer
  14. * in this position and unchanged.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. The name of the author may not be used to endorse or promote products
  19. * derived from this software without specific prior written permission
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  22. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  23. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  30. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include <sys/param.h>
  34. #include <sys/systm.h>
  35. #include <sys/kernel.h>
  36. #include <sys/device.h>
  37. #include <sys/errno.h>
  38. #include <machine/cpu.h>
  39. #include <machine/pio.h>
  40. #include <machine/cpufunc.h>
  41. #include <machine/joystick.h>
  42. #include <machine/conf.h>
  43. #include <dev/isa/isavar.h>
  44. #include <dev/isa/isareg.h>
  45. #include <dev/ic/i8253reg.h>
  46. #include <i386/isa/joyreg.h>
  47. static int joy_get_tick(void);
  48. struct cfdriver joy_cd = {
  49. NULL, "joy", DV_DULL
  50. };
  51. int
  52. joyopen(dev_t dev, int flag, int mode, struct proc *p)
  53. {
  54. int unit = JOYUNIT(dev);
  55. int i = JOYPART(dev);
  56. struct joy_softc *sc;
  57. if (unit >= joy_cd.cd_ndevs)
  58. return (ENXIO);
  59. sc = joy_cd.cd_devs[unit];
  60. if (sc == NULL)
  61. return (ENXIO);
  62. if (sc->timeout[i])
  63. return EBUSY;
  64. sc->x_off[i] = sc->y_off[i] = 0;
  65. sc->timeout[i] = JOY_TIMEOUT;
  66. return 0;
  67. }
  68. int
  69. joyclose(dev_t dev, int flag, int mode, struct proc *p)
  70. {
  71. int unit = JOYUNIT(dev);
  72. int i = JOYPART(dev);
  73. struct joy_softc *sc = joy_cd.cd_devs[unit];
  74. sc->timeout[i] = 0;
  75. return 0;
  76. }
  77. int
  78. joyread(dev_t dev, struct uio *uio, int flag)
  79. {
  80. int unit = JOYUNIT(dev);
  81. struct joy_softc *sc = joy_cd.cd_devs[unit];
  82. struct joystick c;
  83. int port = sc->port;
  84. int i, t0, t1;
  85. int state = 0, x = 0, y = 0;
  86. disable_intr();
  87. outb(port, 0xff);
  88. t0 = joy_get_tick();
  89. t1 = t0;
  90. i = USEC2TICKS(sc->timeout[JOYPART(dev)]);
  91. while (t0 - t1 < i) {
  92. state = inb(port);
  93. if (JOYPART(dev) == 1)
  94. state >>= 2;
  95. t1 = joy_get_tick();
  96. if (t1 > t0)
  97. t1 -= TIMER_FREQ / hz;
  98. if (!x && !(state & 0x01))
  99. x = t1;
  100. if (!y && !(state & 0x02))
  101. y = t1;
  102. if (x && y)
  103. break;
  104. }
  105. enable_intr();
  106. c.x = x ? sc->x_off[JOYPART(dev)] + TICKS2USEC(t0 - x) : 0x80000000;
  107. c.y = y ? sc->y_off[JOYPART(dev)] + TICKS2USEC(t0 - y) : 0x80000000;
  108. state >>= 4;
  109. c.b1 = ~state & 1;
  110. c.b2 = ~(state >> 1) & 1;
  111. return uiomove((caddr_t) & c, sizeof(struct joystick), uio);
  112. }
  113. int
  114. joyioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
  115. {
  116. int unit = JOYUNIT(dev);
  117. struct joy_softc *sc = joy_cd.cd_devs[unit];
  118. int i = JOYPART(dev);
  119. int x;
  120. switch (cmd) {
  121. case JOY_SETTIMEOUT:
  122. x = *(int *) data;
  123. if (x < 1 || x > 10000) /* 10ms maximum! */
  124. return EINVAL;
  125. sc->timeout[i] = x;
  126. break;
  127. case JOY_GETTIMEOUT:
  128. *(int *) data = sc->timeout[i];
  129. break;
  130. case JOY_SET_X_OFFSET:
  131. sc->x_off[i] = *(int *) data;
  132. break;
  133. case JOY_SET_Y_OFFSET:
  134. sc->y_off[i] = *(int *) data;
  135. break;
  136. case JOY_GET_X_OFFSET:
  137. *(int *) data = sc->x_off[i];
  138. break;
  139. case JOY_GET_Y_OFFSET:
  140. *(int *) data = sc->y_off[i];
  141. break;
  142. default:
  143. return ENXIO;
  144. }
  145. return 0;
  146. }
  147. static int
  148. joy_get_tick(void)
  149. {
  150. int low, high;
  151. outb(IO_TIMER1 + TIMER_MODE, TIMER_SEL0);
  152. low = inb(IO_TIMER1 + TIMER_CNTR0);
  153. high = inb(IO_TIMER1 + TIMER_CNTR0);
  154. return (high << 8) | low;
  155. }