ums.c revision 1.2 1 /* $NetBSD: ums.c,v 1.2 1998/07/24 20:59:57 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 #ifdef USB_DEBUG
66 #define DPRINTF(x) if (umsdebug) printf x
67 #define DPRINTFN(n,x) if (umsdebug>(n)) printf x
68 int umsdebug = 0;
69 #else
70 #define DPRINTF(x)
71 #define DPRINTFN(n,x)
72 #endif
73
74 #define PS2LBUTMASK 0x01
75 #define PS2RBUTMASK 0x02
76 #define PS2MBUTMASK 0x04
77 #define PS2BUTMASK 0x0f
78
79 struct ums_softc {
80 struct device sc_dev; /* base device */
81 usbd_interface_handle sc_iface; /* interface */
82 usbd_pipe_handle sc_intrpipe; /* interrupt pipe */
83 int sc_ep_addr;
84
85 u_char *sc_ibuf;
86 u_int8_t sc_iid;
87 int sc_isize;
88 struct hid_location sc_loc_x, sc_loc_y,
89 sc_loc_btn1, sc_loc_btn2, sc_loc_btn3;
90
91 struct clist sc_q;
92 struct selinfo sc_rsel;
93 u_char sc_state; /* mouse driver state */
94 #define UMS_OPEN 0x01 /* device is open */
95 #define UMS_ASLP 0x02 /* waiting for mouse data */
96 #define UMS_NEEDCLEAR 0x04 /* needs clearing endpoint stall */
97 u_char sc_status; /* mouse button status */
98 int sc_x, sc_y; /* accumulated motion in the X,Y axis */
99 int sc_disconnected; /* device is gone */
100 };
101
102 #define UMSUNIT(dev) (minor(dev))
103 #define UMS_CHUNK 128 /* chunk size for read */
104 #define UMS_BSIZE 1020 /* buffer size */
105
106 #define MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
107 #define MOUSE_FLAGS (HIO_RELATIVE)
108
109 int ums_match __P((struct device *, struct cfdata *, void *));
110 void ums_attach __P((struct device *, struct device *, void *));
111
112 int umsopen __P((dev_t, int, int, struct proc *));
113 int umsclose __P((dev_t, int, int, struct proc *p));
114 int umsread __P((dev_t, struct uio *uio, int));
115 int umsioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
116 int umspoll __P((dev_t, int, struct proc *));
117 void ums_intr __P((usbd_request_handle, usbd_private_handle, usbd_status));
118 void ums_disco __P((void *));
119
120 extern struct cfdriver ums_cd;
121
122 struct cfattach ums_ca = {
123 sizeof(struct ums_softc), ums_match, ums_attach
124 };
125
126 int
127 ums_match(parent, match, aux)
128 struct device *parent;
129 struct cfdata *match;
130 void *aux;
131 {
132 struct usb_attach_arg *uaa = aux;
133 usb_interface_descriptor_t *id;
134 int size, ret;
135 void *desc;
136 usbd_status r;
137
138 if (!uaa->iface)
139 return (UMATCH_NONE);
140 id = usbd_get_interface_descriptor(uaa->iface);
141 if (id->bInterfaceClass != UCLASS_HID)
142 return (UMATCH_NONE);
143
144 r = usbd_alloc_report_desc(uaa->iface, &desc, &size, M_TEMP);
145 if (r != USBD_NORMAL_COMPLETION)
146 return (UMATCH_NONE);
147
148 if (hid_is_collection(desc, size,
149 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE)))
150 ret = UMATCH_IFACECLASS;
151 else
152 ret = UMATCH_NONE;
153
154 free(desc, M_TEMP);
155 return (ret);
156 }
157
158 void
159 ums_attach(parent, self, aux)
160 struct device *parent;
161 struct device *self;
162 void *aux;
163 {
164 struct ums_softc *sc = (struct ums_softc *)self;
165 struct usb_attach_arg *uaa = aux;
166 usbd_interface_handle iface = uaa->iface;
167 usb_interface_descriptor_t *id;
168 usb_endpoint_descriptor_t *ed;
169 int size;
170 void *desc;
171 usbd_status r;
172 char devinfo[1024];
173 u_int32_t flags;
174
175 sc->sc_disconnected = 1;
176 sc->sc_iface = iface;
177 id = usbd_get_interface_descriptor(iface);
178 usbd_devinfo(uaa->device, 0, devinfo);
179 printf(": %s (interface class %d/%d)\n", devinfo,
180 id->bInterfaceClass, id->bInterfaceSubClass);
181 ed = usbd_interface2endpoint_descriptor(iface, 0);
182 if (!ed) {
183 printf("%s: could not read endpoint descriptor\n",
184 sc->sc_dev.dv_xname);
185 return;
186 }
187
188 DPRINTFN(10,("ums_attach: \
189 bLength=%d bDescriptorType=%d bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d bInterval=%d\n",
190 ed->bLength, ed->bDescriptorType, ed->bEndpointAddress & UE_ADDR,
191 ed->bEndpointAddress & UE_IN ? "in" : "out",
192 ed->bmAttributes & UE_XFERTYPE,
193 UGETW(ed->wMaxPacketSize), ed->bInterval));
194
195 if ((ed->bEndpointAddress & UE_IN) != UE_IN ||
196 (ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
197 printf("%s: unexpected endpoint\n",
198 sc->sc_dev.dv_xname);
199 return;
200 }
201
202 r = usbd_alloc_report_desc(uaa->iface, &desc, &size, M_TEMP);
203 if (r != USBD_NORMAL_COMPLETION)
204 return;
205 if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
206 hid_input, &sc->sc_loc_x, &flags)) {
207 printf("%s: mouse has no X report\n", sc->sc_dev.dv_xname);
208 return;
209 }
210 if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS)
211 printf("%s: sorry, X report 0x%04x not supported yet\n",
212 sc->sc_dev.dv_xname, flags);
213 if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
214 hid_input, &sc->sc_loc_y, &flags)) {
215 printf("%s: mouse has no Y report\n", sc->sc_dev.dv_xname);
216 return;
217 }
218 if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS)
219 printf("%s: sorry, Y report 0x%04x not supported yet\n",
220 sc->sc_dev.dv_xname, flags);
221 hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, 1),
222 hid_input, &sc->sc_loc_btn1, 0);
223 hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, 2),
224 hid_input, &sc->sc_loc_btn2, 0);
225 hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, 3),
226 hid_input, &sc->sc_loc_btn3, 0);
227 DPRINTF(("ums_attach: sc=%p\n", sc));
228 DPRINTF(("ums_attach: X %d/%d\n",
229 sc->sc_loc_x.pos, sc->sc_loc_x.size));
230 DPRINTF(("ums_attach: Y %d/%d\n",
231 sc->sc_loc_x.pos, sc->sc_loc_x.size));
232 DPRINTF(("ums_attach: B1 %d/%d\n",
233 sc->sc_loc_btn1.pos,sc->sc_loc_btn1.size));
234 DPRINTF(("ums_attach: B2 %d/%d\n",
235 sc->sc_loc_btn2.pos,sc->sc_loc_btn2.size));
236 DPRINTF(("ums_attach: B3 %d/%d\n",
237 sc->sc_loc_btn3.pos,sc->sc_loc_btn3.size));
238
239 sc->sc_isize = hid_report_size(desc, size, hid_input, &sc->sc_iid);
240 DPRINTF(("ums_attach: size=%d, id=%d\n", sc->sc_isize, sc->sc_iid));
241 sc->sc_ibuf = malloc(sc->sc_isize, M_USB, M_NOWAIT);
242 if (!sc->sc_ibuf)
243 return;
244
245 sc->sc_ep_addr = ed->bEndpointAddress;
246 sc->sc_disconnected = 0;
247 free(desc, M_TEMP);
248 }
249
250 void
251 ums_disco(p)
252 void *p;
253 {
254 struct ums_softc *sc = p;
255 sc->sc_disconnected = 1;
256 }
257
258 void
259 ums_intr(reqh, addr, status)
260 usbd_request_handle reqh;
261 usbd_private_handle addr;
262 usbd_status status;
263 {
264 struct ums_softc *sc = addr;
265 u_char *ibuf;
266 char dx, dy;
267 u_char buttons, changed;
268 u_char buffer[5];
269
270 DPRINTFN(5, ("ums_intr: sc=%p status=%d\n", sc, status));
271 DPRINTFN(5, ("ums_intr: data = %02x %02x %02x\n",
272 sc->sc_ibuf[0], sc->sc_ibuf[1], sc->sc_ibuf[2]));
273
274 if (status == USBD_CANCELLED)
275 return;
276
277 if (status != USBD_NORMAL_COMPLETION) {
278 DPRINTF(("ums_intr: status=%d\n", status));
279 sc->sc_state |= UMS_NEEDCLEAR;
280 return;
281 }
282
283 ibuf = sc->sc_ibuf;
284 if (sc->sc_iid) {
285 if (*ibuf++ != sc->sc_iid)
286 return;
287 }
288 DPRINTF(("ums_attach: X %d/%d\n",
289 sc->sc_loc_x.pos, sc->sc_loc_x.size));
290 dx = hid_get_data(ibuf, &sc->sc_loc_x);
291 dy = -hid_get_data(ibuf, &sc->sc_loc_y);
292 buttons = 0;
293 if (hid_get_data(ibuf, &sc->sc_loc_btn1)) buttons |= PS2LBUTMASK;
294 if (hid_get_data(ibuf, &sc->sc_loc_btn2)) buttons |= PS2RBUTMASK;
295 if (hid_get_data(ibuf, &sc->sc_loc_btn3)) buttons |= PS2MBUTMASK;
296
297 buttons = ((buttons & PS2LBUTMASK) << 2) |
298 ((buttons & (PS2RBUTMASK | PS2MBUTMASK)) >> 1);
299 changed = ((buttons ^ sc->sc_status) & BUTSTATMASK) << 3;
300 sc->sc_status = buttons | (sc->sc_status & ~BUTSTATMASK) | changed;
301
302 if (dx || dy || changed) {
303 /* Update accumulated movements. */
304 sc->sc_x += dx;
305 sc->sc_y += dy;
306
307 /* Add this event to the queue. */
308 buffer[0] = 0x80 | (buttons ^ BUTSTATMASK);
309 buffer[1] = dx;
310 buffer[2] = dy;
311 buffer[3] = buffer[4] = 0;
312 (void) b_to_q(buffer, sizeof buffer, &sc->sc_q);
313
314 if (sc->sc_state & UMS_ASLP) {
315 sc->sc_state &= ~UMS_ASLP;
316 DPRINTFN(5, ("ums_intr: waking %p\n", sc));
317 wakeup((caddr_t)sc);
318 }
319 selwakeup(&sc->sc_rsel);
320 }
321 }
322
323 int
324 umsopen(dev, flag, mode, p)
325 dev_t dev;
326 int flag;
327 int mode;
328 struct proc *p;
329 {
330 int unit = UMSUNIT(dev);
331 struct ums_softc *sc;
332 usbd_status r;
333
334 if (unit >= ums_cd.cd_ndevs)
335 return ENXIO;
336 sc = ums_cd.cd_devs[unit];
337 if (!sc)
338 return ENXIO;
339
340 DPRINTF(("umsopen: sc=%p, disco=%d\n", sc, sc->sc_disconnected));
341
342 if (sc->sc_disconnected)
343 return (EIO);
344
345 if (sc->sc_state & UMS_OPEN)
346 return EBUSY;
347
348 if (clalloc(&sc->sc_q, UMS_BSIZE, 0) == -1)
349 return ENOMEM;
350
351 sc->sc_state |= UMS_OPEN;
352 sc->sc_status = 0;
353
354 /* Set up interrupt pipe. */
355 r = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
356 USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc,
357 sc->sc_ibuf, sc->sc_isize, ums_intr);
358 if (r != USBD_NORMAL_COMPLETION) {
359 DPRINTF(("umsopen: usbd_open_pipe_intr failed, error=%d\n",r));
360 sc->sc_state &= ~UMS_OPEN;
361 clfree(&sc->sc_q);
362 return (EIO);
363 }
364 usbd_set_disco(sc->sc_intrpipe, ums_disco, sc);
365 return 0;
366 }
367
368 int
369 umsclose(dev, flag, mode, p)
370 dev_t dev;
371 int flag;
372 int mode;
373 struct proc *p;
374 {
375 struct ums_softc *sc = ums_cd.cd_devs[UMSUNIT(dev)];
376
377 if (sc->sc_disconnected)
378 return (EIO);
379
380 DPRINTF(("umsclose: sc=%p\n", sc));
381
382 /* Disable interrupts. */
383 usbd_abort_pipe(sc->sc_intrpipe);
384 usbd_close_pipe(sc->sc_intrpipe);
385
386 sc->sc_state &= ~UMS_OPEN;
387
388 clfree(&sc->sc_q);
389
390 return 0;
391 }
392
393 int
394 umsread(dev, uio, flag)
395 dev_t dev;
396 struct uio *uio;
397 int flag;
398 {
399 struct ums_softc *sc = ums_cd.cd_devs[UMSUNIT(dev)];
400 int s;
401 int error = 0;
402 size_t length;
403 u_char buffer[UMS_CHUNK];
404
405 /* Block until mouse activity occured. */
406
407 if (sc->sc_disconnected)
408 return (EIO);
409
410 s = spltty();
411 while (sc->sc_q.c_cc == 0) {
412 if (flag & IO_NDELAY) {
413 splx(s);
414 return EWOULDBLOCK;
415 }
416 sc->sc_state |= UMS_ASLP;
417 DPRINTFN(5, ("umsread: sleep on %p\n", sc));
418 error = tsleep((caddr_t)sc, PZERO | PCATCH, "umsrea", 0);
419 DPRINTFN(5, ("umsread: woke, error=%d\n", error));
420 if (error) {
421 sc->sc_state &= ~UMS_ASLP;
422 splx(s);
423 return error;
424 }
425 if (sc->sc_state & UMS_NEEDCLEAR) {
426 DPRINTFN(-1,("umsread: clearing stall\n"));
427 sc->sc_state &= ~UMS_NEEDCLEAR;
428 usbd_clear_endpoint_stall(sc->sc_intrpipe);
429 }
430 }
431 splx(s);
432
433 /* Transfer as many chunks as possible. */
434 while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0) {
435 length = min(sc->sc_q.c_cc, uio->uio_resid);
436 if (length > sizeof(buffer))
437 length = sizeof(buffer);
438
439 /* Remove a small chunk from the input queue. */
440 (void) q_to_b(&sc->sc_q, buffer, length);
441 DPRINTFN(5, ("umsread: got %d chars\n", length));
442
443 /* Copy the data to the user process. */
444 if ((error = uiomove(buffer, length, uio)) != 0)
445 break;
446 }
447
448 return error;
449 }
450
451 int
452 umsioctl(dev, cmd, addr, flag, p)
453 dev_t dev;
454 u_long cmd;
455 caddr_t addr;
456 int flag;
457 struct proc *p;
458 {
459 struct ums_softc *sc = ums_cd.cd_devs[UMSUNIT(dev)];
460
461 if (sc->sc_disconnected)
462 return (EIO);
463
464 return EINVAL;
465 }
466
467 int
468 umspoll(dev, events, p)
469 dev_t dev;
470 int events;
471 struct proc *p;
472 {
473 struct ums_softc *sc = ums_cd.cd_devs[UMSUNIT(dev)];
474 int revents = 0;
475 int s;
476
477 if (sc->sc_disconnected)
478 return (EIO);
479
480 s = spltty();
481 if (events & (POLLIN | POLLRDNORM))
482 if (sc->sc_q.c_cc > 0)
483 revents |= events & (POLLIN | POLLRDNORM);
484 else
485 selrecord(p, &sc->sc_rsel);
486
487 splx(s);
488 return (revents);
489 }
490