lms.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* $OpenBSD: lms.c,v 1.20 2007/04/10 22:37:17 miod Exp $ */
  2. /* $NetBSD: lms.c,v 1.38 2000/01/08 02:57:25 takemura Exp $ */
  3. /*-
  4. * Copyright (c) 1993, 1994 Charles M. Hannum.
  5. * Copyright (c) 1992, 1993 Erik Forsberg.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY EXPRESS OR IMPLIED
  15. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  16. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  17. * NO EVENT SHALL I BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  21. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  22. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include <sys/param.h>
  26. #include <sys/systm.h>
  27. #include <sys/ioctl.h>
  28. #include <sys/device.h>
  29. #include <machine/bus.h>
  30. #include <machine/intr.h>
  31. #include <dev/isa/isavar.h>
  32. #include <dev/wscons/wsconsio.h>
  33. #include <dev/wscons/wsmousevar.h>
  34. #define LMS_DATA 0 /* offset for data port, read-only */
  35. #define LMS_SIGN 1 /* offset for signature port, read-write */
  36. #define LMS_INTR 2 /* offset for interrupt port, read-only */
  37. #define LMS_CNTRL 2 /* offset for control port, write-only */
  38. #define LMS_CONFIG 3 /* for configuration port, read-write */
  39. #define LMS_NPORTS 4
  40. struct lms_softc { /* driver status information */
  41. struct device sc_dev;
  42. void *sc_ih;
  43. bus_space_tag_t sc_iot; /* bus i/o space identifier */
  44. bus_space_handle_t sc_ioh; /* bus i/o handle */
  45. int sc_enabled; /* device is open */
  46. int oldbuttons; /* mouse button status */
  47. struct device *sc_wsmousedev;
  48. };
  49. int lmsprobe(struct device *, void *, void *);
  50. void lmsattach(struct device *, struct device *, void *);
  51. int lmsintr(void *);
  52. struct cfattach lms_ca = {
  53. sizeof(struct lms_softc), lmsprobe, lmsattach
  54. };
  55. int lms_enable(void *);
  56. int lms_ioctl(void *, u_long, caddr_t, int, struct proc *);
  57. void lms_disable(void *);
  58. const struct wsmouse_accessops lms_accessops = {
  59. lms_enable,
  60. lms_ioctl,
  61. lms_disable,
  62. };
  63. int
  64. lmsprobe(struct device *parent, void *match, void *aux)
  65. {
  66. struct isa_attach_args *ia = aux;
  67. bus_space_tag_t iot = ia->ia_iot;
  68. bus_space_handle_t ioh;
  69. int rv;
  70. /* Disallow wildcarded i/o base. */
  71. if (ia->ia_iobase == IOBASEUNK)
  72. return 0;
  73. /* Map the i/o space. */
  74. if (bus_space_map(iot, ia->ia_iobase, LMS_NPORTS, 0, &ioh))
  75. return 0;
  76. rv = 0;
  77. /* Configure and check for port present. */
  78. bus_space_write_1(iot, ioh, LMS_CONFIG, 0x91);
  79. delay(10);
  80. bus_space_write_1(iot, ioh, LMS_SIGN, 0x0c);
  81. delay(10);
  82. if (bus_space_read_1(iot, ioh, LMS_SIGN) != 0x0c)
  83. goto out;
  84. bus_space_write_1(iot, ioh, LMS_SIGN, 0x50);
  85. delay(10);
  86. if (bus_space_read_1(iot, ioh, LMS_SIGN) != 0x50)
  87. goto out;
  88. /* Disable interrupts. */
  89. bus_space_write_1(iot, ioh, LMS_CNTRL, 0x10);
  90. rv = 1;
  91. ia->ia_iosize = LMS_NPORTS;
  92. ia->ia_msize = 0;
  93. out:
  94. bus_space_unmap(iot, ioh, LMS_NPORTS);
  95. return rv;
  96. }
  97. void
  98. lmsattach(struct device *parent, struct device *self, void *aux)
  99. {
  100. struct lms_softc *sc = (void *)self;
  101. struct isa_attach_args *ia = aux;
  102. bus_space_tag_t iot = ia->ia_iot;
  103. bus_space_handle_t ioh;
  104. struct wsmousedev_attach_args a;
  105. printf("\n");
  106. if (bus_space_map(iot, ia->ia_iobase, LMS_NPORTS, 0, &ioh)) {
  107. printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
  108. return;
  109. }
  110. /* Other initialization was done by lmsprobe. */
  111. sc->sc_iot = iot;
  112. sc->sc_ioh = ioh;
  113. sc->sc_enabled = 0;
  114. sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_PULSE,
  115. IPL_TTY, lmsintr, sc, sc->sc_dev.dv_xname);
  116. a.accessops = &lms_accessops;
  117. a.accesscookie = sc;
  118. /*
  119. * Attach the wsmouse, saving a handle to it.
  120. * Note that we don't need to check this pointer against NULL
  121. * here or in psmintr, because if this fails lms_enable() will
  122. * never be called, so lmsintr() will never be called.
  123. */
  124. sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
  125. }
  126. int
  127. lms_enable(void *v)
  128. {
  129. struct lms_softc *sc = v;
  130. if (sc->sc_enabled)
  131. return EBUSY;
  132. sc->sc_enabled = 1;
  133. sc->oldbuttons = 0;
  134. /* Enable interrupts. */
  135. bus_space_write_1(sc->sc_iot, sc->sc_ioh, LMS_CNTRL, 0);
  136. return 0;
  137. }
  138. void
  139. lms_disable(void *v)
  140. {
  141. struct lms_softc *sc = v;
  142. /* Disable interrupts. */
  143. bus_space_write_1(sc->sc_iot, sc->sc_ioh, LMS_CNTRL, 0x10);
  144. sc->sc_enabled = 0;
  145. }
  146. int
  147. lms_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
  148. {
  149. #if 0
  150. struct lms_softc *sc = v;
  151. #endif
  152. switch (cmd) {
  153. case WSMOUSEIO_GTYPE:
  154. *(u_int *)data = WSMOUSE_TYPE_LMS;
  155. return (0);
  156. }
  157. return (-1);
  158. }
  159. int
  160. lmsintr(void *arg)
  161. {
  162. struct lms_softc *sc = arg;
  163. bus_space_tag_t iot = sc->sc_iot;
  164. bus_space_handle_t ioh = sc->sc_ioh;
  165. u_char hi, lo;
  166. signed char dx, dy;
  167. u_int buttons;
  168. int changed;
  169. if (!sc->sc_enabled)
  170. /* Interrupts are not expected. */
  171. return 0;
  172. bus_space_write_1(iot, ioh, LMS_CNTRL, 0xab);
  173. hi = bus_space_read_1(iot, ioh, LMS_DATA);
  174. bus_space_write_1(iot, ioh, LMS_CNTRL, 0x90);
  175. lo = bus_space_read_1(iot, ioh, LMS_DATA);
  176. dx = ((hi & 0x0f) << 4) | (lo & 0x0f);
  177. /* Bounding at -127 avoids a bug in XFree86. */
  178. dx = (dx == -128) ? -127 : dx;
  179. bus_space_write_1(iot, ioh, LMS_CNTRL, 0xf0);
  180. hi = bus_space_read_1(iot, ioh, LMS_DATA);
  181. bus_space_write_1(iot, ioh, LMS_CNTRL, 0xd0);
  182. lo = bus_space_read_1(iot, ioh, LMS_DATA);
  183. dy = ((hi & 0x0f) << 4) | (lo & 0x0f);
  184. dy = (dy == -128) ? 127 : -dy;
  185. bus_space_write_1(iot, ioh, LMS_CNTRL, 0);
  186. buttons = ((hi & 0x80) ? 0 : 0x1) |
  187. ((hi & 0x40) ? 0 : 0x2) |
  188. ((hi & 0x20) ? 0 : 0x4);
  189. changed = (buttons ^ sc->oldbuttons);
  190. sc->oldbuttons = buttons;
  191. if (dx || dy || changed)
  192. wsmouse_input(sc->sc_wsmousedev,
  193. buttons, dx, dy, 0, 0, WSMOUSE_INPUT_DELTA);
  194. return -1;
  195. }
  196. struct cfdriver lms_cd = {
  197. NULL, "lms", DV_DULL
  198. };