ums.c revision 1.3 1 /* $NetBSD: ums.c,v 1.3 1998/07/25 01:46:38 augustss Exp $ */
2
3 /*
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Author: Lennart Augustsson <augustss (at) carlstedt.se>
8 * Carlstedt Research & Technology
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/device.h>
45 #include <sys/ioctl.h>
46 #include <sys/tty.h>
47 #include <sys/file.h>
48 #include <sys/select.h>
49 #include <sys/proc.h>
50 #include <sys/vnode.h>
51 #include <sys/device.h>
52 #include <sys/poll.h>
53
54 #include <machine/mouse.h>
55
56 #include <dev/usb/usb.h>
57 #include <dev/usb/usbhid.h>
58
59 #include <dev/usb/usbdi.h>
60 #include <dev/usb/usbdi_util.h>
61 #include <dev/usb/usbdevs.h>
62 #include <dev/usb/usb_quirks.h>
63 #include <dev/usb/hid.h>
64
65 #include <dev/wscons/wsconsio.h>
66 #include <dev/wscons/wsmousevar.h>
67
68 #ifdef USB_DEBUG
69 #define DPRINTF(x) if (umsdebug) printf x
70 #define DPRINTFN(n,x) if (umsdebug>(n)) printf x
71 int umsdebug = 0;
72 #else
73 #define DPRINTF(x)
74 #define DPRINTFN(n,x)
75 #endif
76
77 #define PS2LBUTMASK 0x01
78 #define PS2RBUTMASK 0x02
79 #define PS2MBUTMASK 0x04
80 #define PS2BUTMASK 0x0f
81
82 struct ums_softc {
83 struct device sc_dev; /* base device */
84 usbd_interface_handle sc_iface; /* interface */
85 usbd_pipe_handle sc_intrpipe; /* interrupt pipe */
86 int sc_ep_addr;
87
88 u_char *sc_ibuf;
89 u_int8_t sc_iid;
90 int sc_isize;
91 struct hid_location sc_loc_x, sc_loc_y,
92 sc_loc_btn1, sc_loc_btn2, sc_loc_btn3;
93
94 struct clist sc_q;
95 struct selinfo sc_rsel;
96 u_char sc_state; /* mouse driver state */
97 #define UMS_OPEN 0x01 /* device is open */
98 #define UMS_ASLP 0x02 /* waiting for mouse data */
99 #define UMS_NEEDCLEAR 0x04 /* needs clearing endpoint stall */
100 u_char sc_status; /* mouse button status */
101 int sc_x, sc_y; /* accumulated motion in the X,Y axis */
102 int sc_disconnected; /* device is gone */
103
104 int sc_enabled;
105 struct device *sc_wsmousedev;
106 };
107
108 #define UMSUNIT(dev) (minor(dev))
109 #define UMS_CHUNK 128 /* chunk size for read */
110 #define UMS_BSIZE 1020 /* buffer size */
111
112 #define MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
113 #define MOUSE_FLAGS (HIO_RELATIVE)
114
115 int ums_match __P((struct device *, struct cfdata *, void *));
116 void ums_attach __P((struct device *, struct device *, void *));
117
118 int umsopen __P((dev_t, int, int, struct proc *));
119 int umsclose __P((dev_t, int, int, struct proc *p));
120 int umsread __P((dev_t, struct uio *uio, int));
121 int umsioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
122 int umspoll __P((dev_t, int, struct proc *));
123 void ums_intr __P((usbd_request_handle, usbd_private_handle, usbd_status));
124 void ums_disco __P((void *));
125
126 int ums_enable __P((void *));
127 int ums_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
128 void ums_disable __P((void *));
129
130 const struct wsmouse_accessops ums_accessops = {
131 ums_enable,
132 ums_ioctl,
133 ums_disable,
134 };
135
136 extern struct cfdriver ums_cd;
137
138 struct cfattach ums_ca = {
139 sizeof(struct ums_softc), ums_match, ums_attach
140 };
141
142 int
143 ums_match(parent, match, aux)
144 struct device *parent;
145 struct cfdata *match;
146 void *aux;
147 {
148 struct usb_attach_arg *uaa = aux;
149 usb_interface_descriptor_t *id;
150 int size, ret;
151 void *desc;
152 usbd_status r;
153
154 if (!uaa->iface)
155 return (UMATCH_NONE);
156 id = usbd_get_interface_descriptor(uaa->iface);
157 if (id->bInterfaceClass != UCLASS_HID)
158 return (UMATCH_NONE);
159
160 r = usbd_alloc_report_desc(uaa->iface, &desc, &size, M_TEMP);
161 if (r != USBD_NORMAL_COMPLETION)
162 return (UMATCH_NONE);
163
164 if (hid_is_collection(desc, size,
165 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE)))
166 ret = UMATCH_IFACECLASS;
167 else
168 ret = UMATCH_NONE;
169
170 free(desc, M_TEMP);
171 return (ret);
172 }
173
174 void
175 ums_attach(parent, self, aux)
176 struct device *parent;
177 struct device *self;
178 void *aux;
179 {
180 struct ums_softc *sc = (struct ums_softc *)self;
181 struct usb_attach_arg *uaa = aux;
182 usbd_interface_handle iface = uaa->iface;
183 usb_interface_descriptor_t *id;
184 usb_endpoint_descriptor_t *ed;
185 struct wsmousedev_attach_args a;
186 int size;
187 void *desc;
188 usbd_status r;
189 char devinfo[1024];
190 u_int32_t flags;
191
192 sc->sc_disconnected = 1;
193 sc->sc_iface = iface;
194 id = usbd_get_interface_descriptor(iface);
195 usbd_devinfo(uaa->device, 0, devinfo);
196 printf(": %s (interface class %d/%d)\n", devinfo,
197 id->bInterfaceClass, id->bInterfaceSubClass);
198 ed = usbd_interface2endpoint_descriptor(iface, 0);
199 if (!ed) {
200 printf("%s: could not read endpoint descriptor\n",
201 sc->sc_dev.dv_xname);
202 return;
203 }
204
205 DPRINTFN(10,("ums_attach: \
206 bLength=%d bDescriptorType=%d bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d bInterval=%d\n",
207 ed->bLength, ed->bDescriptorType, ed->bEndpointAddress & UE_ADDR,
208 ed->bEndpointAddress & UE_IN ? "in" : "out",
209 ed->bmAttributes & UE_XFERTYPE,
210 UGETW(ed->wMaxPacketSize), ed->bInterval));
211
212 if ((ed->bEndpointAddress & UE_IN) != UE_IN ||
213 (ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
214 printf("%s: unexpected endpoint\n",
215 sc->sc_dev.dv_xname);
216 return;
217 }
218
219 r = usbd_alloc_report_desc(uaa->iface, &desc, &size, M_TEMP);
220 if (r != USBD_NORMAL_COMPLETION)
221 return;
222 if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
223 hid_input, &sc->sc_loc_x, &flags)) {
224 printf("%s: mouse has no X report\n", sc->sc_dev.dv_xname);
225 return;
226 }
227 if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS)
228 printf("%s: sorry, X report 0x%04x not supported yet\n",
229 sc->sc_dev.dv_xname, flags);
230 if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
231 hid_input, &sc->sc_loc_y, &flags)) {
232 printf("%s: mouse has no Y report\n", sc->sc_dev.dv_xname);
233 return;
234 }
235 if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS)
236 printf("%s: sorry, Y report 0x%04x not supported yet\n",
237 sc->sc_dev.dv_xname, flags);
238 hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, 1),
239 hid_input, &sc->sc_loc_btn1, 0);
240 hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, 2),
241 hid_input, &sc->sc_loc_btn2, 0);
242 hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, 3),
243 hid_input, &sc->sc_loc_btn3, 0);
244 DPRINTF(("ums_attach: sc=%p\n", sc));
245 DPRINTF(("ums_attach: X %d/%d\n",
246 sc->sc_loc_x.pos, sc->sc_loc_x.size));
247 DPRINTF(("ums_attach: Y %d/%d\n",
248 sc->sc_loc_x.pos, sc->sc_loc_x.size));
249 DPRINTF(("ums_attach: B1 %d/%d\n",
250 sc->sc_loc_btn1.pos,sc->sc_loc_btn1.size));
251 DPRINTF(("ums_attach: B2 %d/%d\n",
252 sc->sc_loc_btn2.pos,sc->sc_loc_btn2.size));
253 DPRINTF(("ums_attach: B3 %d/%d\n",
254 sc->sc_loc_btn3.pos,sc->sc_loc_btn3.size));
255
256 sc->sc_isize = hid_report_size(desc, size, hid_input, &sc->sc_iid);
257 DPRINTF(("ums_attach: size=%d, id=%d\n", sc->sc_isize, sc->sc_iid));
258 sc->sc_ibuf = malloc(sc->sc_isize, M_USB, M_NOWAIT);
259 if (!sc->sc_ibuf)
260 return;
261
262 sc->sc_ep_addr = ed->bEndpointAddress;
263 sc->sc_disconnected = 0;
264 free(desc, M_TEMP);
265
266 a.accessops = &ums_accessops;
267 a.accesscookie = sc;
268
269 sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
270 }
271
272 void
273 ums_disco(p)
274 void *p;
275 {
276 struct ums_softc *sc = p;
277 sc->sc_disconnected = 1;
278 }
279
280 void
281 ums_intr(reqh, addr, status)
282 usbd_request_handle reqh;
283 usbd_private_handle addr;
284 usbd_status status;
285 {
286 struct ums_softc *sc = addr;
287 u_char *ibuf;
288 char dx, dy;
289 u_char buttons, changed;
290 u_char buffer[5];
291
292 DPRINTFN(5, ("ums_intr: sc=%p status=%d\n", sc, status));
293 DPRINTFN(5, ("ums_intr: data = %02x %02x %02x\n",
294 sc->sc_ibuf[0], sc->sc_ibuf[1], sc->sc_ibuf[2]));
295
296 if (status == USBD_CANCELLED)
297 return;
298
299 if (status != USBD_NORMAL_COMPLETION) {
300 DPRINTF(("ums_intr: status=%d\n", status));
301 sc->sc_state |= UMS_NEEDCLEAR;
302 return;
303 }
304
305 ibuf = sc->sc_ibuf;
306 if (sc->sc_iid) {
307 if (*ibuf++ != sc->sc_iid)
308 return;
309 }
310 DPRINTF(("ums_attach: X %d/%d\n",
311 sc->sc_loc_x.pos, sc->sc_loc_x.size));
312 dx = hid_get_data(ibuf, &sc->sc_loc_x);
313 dy = -hid_get_data(ibuf, &sc->sc_loc_y);
314 buttons = 0;
315 if (hid_get_data(ibuf, &sc->sc_loc_btn1)) buttons |= PS2LBUTMASK;
316 if (hid_get_data(ibuf, &sc->sc_loc_btn2)) buttons |= PS2RBUTMASK;
317 if (hid_get_data(ibuf, &sc->sc_loc_btn3)) buttons |= PS2MBUTMASK;
318
319 buttons = ((buttons & PS2LBUTMASK) << 2) |
320 ((buttons & (PS2RBUTMASK | PS2MBUTMASK)) >> 1);
321 changed = ((buttons ^ sc->sc_status) & BUTSTATMASK) << 3;
322 sc->sc_status = buttons | (sc->sc_status & ~BUTSTATMASK) | changed;
323
324 if (dx || dy || changed) {
325 /* Update accumulated movements. */
326 sc->sc_x += dx;
327 sc->sc_y += dy;
328
329 if (sc->sc_wsmousedev) {
330 wsmouse_input(sc->sc_wsmousedev, buttons, dx, dy);
331 } else {
332 /* Add this event to the queue. */
333 buffer[0] = 0x80 | (buttons ^ BUTSTATMASK);
334 buffer[1] = dx;
335 buffer[2] = dy;
336 buffer[3] = buffer[4] = 0;
337 (void) b_to_q(buffer, sizeof buffer, &sc->sc_q);
338
339 if (sc->sc_state & UMS_ASLP) {
340 sc->sc_state &= ~UMS_ASLP;
341 DPRINTFN(5, ("ums_intr: waking %p\n", sc));
342 wakeup((caddr_t)sc);
343 }
344 selwakeup(&sc->sc_rsel);
345 }
346 }
347 }
348
349 int
350 umsopen(dev, flag, mode, p)
351 dev_t dev;
352 int flag;
353 int mode;
354 struct proc *p;
355 {
356 int unit = UMSUNIT(dev);
357 struct ums_softc *sc;
358 usbd_status r;
359
360 if (unit >= ums_cd.cd_ndevs)
361 return (ENXIO);
362 sc = ums_cd.cd_devs[unit];
363 if (!sc)
364 return (ENXIO);
365
366 DPRINTF(("umsopen: sc=%p, disco=%d\n", sc, sc->sc_disconnected));
367
368 if (sc->sc_wsmousedev)
369 return (ENXIO);
370
371 if (sc->sc_disconnected)
372 return (EIO);
373
374 if (sc->sc_state & UMS_OPEN)
375 return (EBUSY);
376
377 if (clalloc(&sc->sc_q, UMS_BSIZE, 0) == -1)
378 return (ENOMEM);
379
380 sc->sc_state |= UMS_OPEN;
381 sc->sc_status = 0;
382
383 /* Set up interrupt pipe. */
384 r = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
385 USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc,
386 sc->sc_ibuf, sc->sc_isize, ums_intr);
387 if (r != USBD_NORMAL_COMPLETION) {
388 DPRINTF(("umsopen: usbd_open_pipe_intr failed, error=%d\n",r));
389 sc->sc_state &= ~UMS_OPEN;
390 clfree(&sc->sc_q);
391 return (EIO);
392 }
393 usbd_set_disco(sc->sc_intrpipe, ums_disco, sc);
394 return 0;
395 }
396
397 int
398 umsclose(dev, flag, mode, p)
399 dev_t dev;
400 int flag;
401 int mode;
402 struct proc *p;
403 {
404 struct ums_softc *sc = ums_cd.cd_devs[UMSUNIT(dev)];
405
406 if (sc->sc_disconnected)
407 return (EIO);
408
409 DPRINTF(("umsclose: sc=%p\n", sc));
410
411 /* Disable interrupts. */
412 usbd_abort_pipe(sc->sc_intrpipe);
413 usbd_close_pipe(sc->sc_intrpipe);
414
415 sc->sc_state &= ~UMS_OPEN;
416
417 clfree(&sc->sc_q);
418
419 return 0;
420 }
421
422 int
423 umsread(dev, uio, flag)
424 dev_t dev;
425 struct uio *uio;
426 int flag;
427 {
428 struct ums_softc *sc = ums_cd.cd_devs[UMSUNIT(dev)];
429 int s;
430 int error = 0;
431 size_t length;
432 u_char buffer[UMS_CHUNK];
433
434 /* Block until mouse activity occured. */
435
436 if (sc->sc_disconnected)
437 return (EIO);
438
439 s = spltty();
440 while (sc->sc_q.c_cc == 0) {
441 if (flag & IO_NDELAY) {
442 splx(s);
443 return EWOULDBLOCK;
444 }
445 sc->sc_state |= UMS_ASLP;
446 DPRINTFN(5, ("umsread: sleep on %p\n", sc));
447 error = tsleep((caddr_t)sc, PZERO | PCATCH, "umsrea", 0);
448 DPRINTFN(5, ("umsread: woke, error=%d\n", error));
449 if (error) {
450 sc->sc_state &= ~UMS_ASLP;
451 splx(s);
452 return error;
453 }
454 if (sc->sc_state & UMS_NEEDCLEAR) {
455 DPRINTFN(-1,("umsread: clearing stall\n"));
456 sc->sc_state &= ~UMS_NEEDCLEAR;
457 usbd_clear_endpoint_stall(sc->sc_intrpipe);
458 }
459 }
460 splx(s);
461
462 /* Transfer as many chunks as possible. */
463 while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0) {
464 length = min(sc->sc_q.c_cc, uio->uio_resid);
465 if (length > sizeof(buffer))
466 length = sizeof(buffer);
467
468 /* Remove a small chunk from the input queue. */
469 (void) q_to_b(&sc->sc_q, buffer, length);
470 DPRINTFN(5, ("umsread: got %d chars\n", length));
471
472 /* Copy the data to the user process. */
473 if ((error = uiomove(buffer, length, uio)) != 0)
474 break;
475 }
476
477 return error;
478 }
479
480 int
481 umsioctl(dev, cmd, addr, flag, p)
482 dev_t dev;
483 u_long cmd;
484 caddr_t addr;
485 int flag;
486 struct proc *p;
487 {
488 struct ums_softc *sc = ums_cd.cd_devs[UMSUNIT(dev)];
489
490 if (sc->sc_disconnected)
491 return (EIO);
492
493 return EINVAL;
494 }
495
496 int
497 umspoll(dev, events, p)
498 dev_t dev;
499 int events;
500 struct proc *p;
501 {
502 struct ums_softc *sc = ums_cd.cd_devs[UMSUNIT(dev)];
503 int revents = 0;
504 int s;
505
506 if (sc->sc_disconnected)
507 return (EIO);
508
509 s = spltty();
510 if (events & (POLLIN | POLLRDNORM))
511 if (sc->sc_q.c_cc > 0)
512 revents |= events & (POLLIN | POLLRDNORM);
513 else
514 selrecord(p, &sc->sc_rsel);
515
516 splx(s);
517 return (revents);
518 }
519
520 /* wscons routines */
521 int
522 ums_enable(v)
523 void *v;
524 {
525 struct ums_softc *sc = v;
526 usbd_status r;
527
528 if (sc->sc_enabled)
529 return EBUSY;
530
531 sc->sc_enabled = 1;
532 sc->sc_status = 0;
533
534 /* Set up interrupt pipe. */
535 r = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
536 USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc,
537 sc->sc_ibuf, sc->sc_isize, ums_intr);
538 if (r != USBD_NORMAL_COMPLETION) {
539 DPRINTF(("ums_enable: usbd_open_pipe_intr failed, error=%d\n",
540 r));
541 sc->sc_enabled = 0;
542 return (EIO);
543 }
544 usbd_set_disco(sc->sc_intrpipe, ums_disco, sc);
545 return (0);
546 }
547
548 void
549 ums_disable(v)
550 void *v;
551 {
552 struct ums_softc *sc = v;
553
554 /* Disable interrupts. */
555 usbd_abort_pipe(sc->sc_intrpipe);
556 usbd_close_pipe(sc->sc_intrpipe);
557
558 sc->sc_enabled = 0;
559 }
560
561 int
562 ums_ioctl(v, cmd, data, flag, p)
563 void *v;
564 u_long cmd;
565 caddr_t data;
566 int flag;
567 struct proc *p;
568 {
569 #if 0
570 struct ums_softc *sc = v;
571 #endif
572
573 switch (cmd) {
574 case WSMOUSEIO_GTYPE:
575 *(u_int *)data = WSMOUSE_TYPE_PS2;
576 return (0);
577 }
578 return (-1);
579 }
580
581