ums.c revision 1.35 1 /* $NetBSD: ums.c,v 1.35 1999/11/15 22:04:14 augustss Exp $ */
2
3 /*
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (augustss (at) carlstedt.se) at
9 * Carlstedt Research & Technology.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * HID spec: http://www.usb.org/developers/data/usbhid10.pdf
42 */
43
44 /* XXX complete SPUR_UP change */
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/device.h>
51 #include <sys/ioctl.h>
52 #include <sys/tty.h>
53 #include <sys/file.h>
54 #include <sys/select.h>
55 #include <sys/proc.h>
56 #include <sys/vnode.h>
57 #include <sys/poll.h>
58
59 #include <dev/usb/usb.h>
60 #include <dev/usb/usbhid.h>
61
62 #include <dev/usb/usbdi.h>
63 #include <dev/usb/usbdi_util.h>
64 #include <dev/usb/usbdevs.h>
65 #include <dev/usb/usb_quirks.h>
66 #include <dev/usb/hid.h>
67
68 #include <dev/wscons/wsconsio.h>
69 #include <dev/wscons/wsmousevar.h>
70
71 #ifdef USB_DEBUG
72 #define DPRINTF(x) if (umsdebug) logprintf x
73 #define DPRINTFN(n,x) if (umsdebug>(n)) logprintf x
74 int umsdebug = 0;
75 #else
76 #define DPRINTF(x)
77 #define DPRINTFN(n,x)
78 #endif
79
80 #define UMS_BUT(i) ((i) == 1 || (i) == 2 ? 3 - (i) : i)
81
82 #define UMSUNIT(s) (minor(s))
83
84 #define PS2LBUTMASK x01
85 #define PS2RBUTMASK x02
86 #define PS2MBUTMASK x04
87 #define PS2BUTMASK 0x0f
88
89 struct ums_softc {
90 USBBASEDEVICE sc_dev; /* base device */
91 usbd_interface_handle sc_iface; /* interface */
92 usbd_pipe_handle sc_intrpipe; /* interrupt pipe */
93 int sc_ep_addr;
94
95 u_char *sc_ibuf;
96 u_int8_t sc_iid;
97 int sc_isize;
98 struct hid_location sc_loc_x, sc_loc_y, sc_loc_z;
99 struct hid_location *sc_loc_btn;
100
101 int sc_enabled;
102
103 int flags; /* device configuration */
104 #define UMS_Z 0x01 /* z direction available */
105 #define UMS_SPUR_BUT_UP 0x02 /* spurious button up events */
106 #define UMS_REVZ 0x04 /* Z-axis is reversed */
107
108 int nbuttons;
109 #define MAX_BUTTONS 31 /* chosen because sc_buttons is u_int32_t */
110
111 u_int32_t sc_buttons; /* mouse button status */
112 struct device *sc_wsmousedev;
113
114 char sc_dying;
115 };
116
117 #define MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
118 #define MOUSE_FLAGS (HIO_RELATIVE)
119
120 static void ums_intr __P((usbd_xfer_handle, usbd_private_handle,
121 usbd_status));
122
123 static int ums_enable __P((void *));
124 static void ums_disable __P((void *));
125 static int ums_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
126
127 const struct wsmouse_accessops ums_accessops = {
128 ums_enable,
129 ums_ioctl,
130 ums_disable,
131 };
132
133 USB_DECLARE_DRIVER(ums);
134
135 USB_MATCH(ums)
136 {
137 USB_MATCH_START(ums, uaa);
138 usb_interface_descriptor_t *id;
139 int size, ret;
140 void *desc;
141 usbd_status err;
142
143 if (uaa->iface == NULL)
144 return (UMATCH_NONE);
145 id = usbd_get_interface_descriptor(uaa->iface);
146 if (id == NULL || id->bInterfaceClass != UCLASS_HID)
147 return (UMATCH_NONE);
148
149 err = usbd_alloc_report_desc(uaa->iface, &desc, &size, M_TEMP);
150 if (err)
151 return (UMATCH_NONE);
152
153 if (hid_is_collection(desc, size,
154 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE)))
155 ret = UMATCH_IFACECLASS;
156 else
157 ret = UMATCH_NONE;
158
159 free(desc, M_TEMP);
160 return (ret);
161 }
162
163 USB_ATTACH(ums)
164 {
165 USB_ATTACH_START(ums, sc, uaa);
166 usbd_interface_handle iface = uaa->iface;
167 usb_interface_descriptor_t *id;
168 usb_endpoint_descriptor_t *ed;
169 struct wsmousedev_attach_args a;
170 int size;
171 void *desc;
172 usbd_status err;
173 char devinfo[1024];
174 u_int32_t flags, quirks;
175 int i;
176 struct hid_location loc_btn;
177
178 sc->sc_iface = iface;
179 id = usbd_get_interface_descriptor(iface);
180 usbd_devinfo(uaa->device, 0, devinfo);
181 USB_ATTACH_SETUP;
182 printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
183 devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
184 ed = usbd_interface2endpoint_descriptor(iface, 0);
185 if (ed == NULL) {
186 printf("%s: could not read endpoint descriptor\n",
187 USBDEVNAME(sc->sc_dev));
188 USB_ATTACH_ERROR_RETURN;
189 }
190
191 DPRINTFN(10,("ums_attach: bLength=%d bDescriptorType=%d "
192 "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
193 " bInterval=%d\n",
194 ed->bLength, ed->bDescriptorType,
195 ed->bEndpointAddress & UE_ADDR,
196 UE_GET_DIR(ed->bEndpointAddress)==UE_DIR_IN? "in" : "out",
197 ed->bmAttributes & UE_XFERTYPE,
198 UGETW(ed->wMaxPacketSize), ed->bInterval));
199
200 if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN ||
201 (ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
202 printf("%s: unexpected endpoint\n",
203 USBDEVNAME(sc->sc_dev));
204 USB_ATTACH_ERROR_RETURN;
205 }
206
207 quirks = usbd_get_quirks(uaa->device)->uq_flags;
208 if (quirks & UQ_MS_REVZ)
209 sc->flags |= UMS_REVZ;
210 if (quirks & UQ_SPUR_BUT_UP)
211 sc->flags |= UMS_SPUR_BUT_UP;
212
213 err = usbd_alloc_report_desc(uaa->iface, &desc, &size, M_TEMP);
214 if (err)
215 USB_ATTACH_ERROR_RETURN;
216
217 if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
218 hid_input, &sc->sc_loc_x, &flags)) {
219 printf("%s: mouse has no X report\n", USBDEVNAME(sc->sc_dev));
220 USB_ATTACH_ERROR_RETURN;
221 }
222 if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
223 printf("%s: X report 0x%04x not supported\n",
224 USBDEVNAME(sc->sc_dev), flags);
225 USB_ATTACH_ERROR_RETURN;
226 }
227
228 if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
229 hid_input, &sc->sc_loc_y, &flags)) {
230 printf("%s: mouse has no Y report\n", USBDEVNAME(sc->sc_dev));
231 USB_ATTACH_ERROR_RETURN;
232 }
233 if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
234 printf("%s: Y report 0x%04x not supported\n",
235 USBDEVNAME(sc->sc_dev), flags);
236 USB_ATTACH_ERROR_RETURN;
237 }
238
239 /* Try to guess the Z activator: first check Z, then WHEEL. */
240 if (hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Z),
241 hid_input, &sc->sc_loc_z, &flags) ||
242 hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_WHEEL),
243 hid_input, &sc->sc_loc_z, &flags)) {
244 if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
245 sc->sc_loc_z.size = 0; /* Bad Z coord, ignore it */
246 } else {
247 sc->flags |= UMS_Z;
248 }
249 }
250
251 /* figure out the number of buttons */
252 for (i = 1; i <= MAX_BUTTONS; i++)
253 if (!hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),
254 hid_input, &loc_btn, 0))
255 break;
256 sc->nbuttons = i - 1;
257 sc->sc_loc_btn = malloc(sizeof(struct hid_location)*sc->nbuttons,
258 M_USBDEV, M_NOWAIT);
259 if (!sc->sc_loc_btn) {
260 printf("%s: no memory\n", USBDEVNAME(sc->sc_dev));
261 USB_ATTACH_ERROR_RETURN;
262 }
263
264 printf("%s: %d buttons%s\n", USBDEVNAME(sc->sc_dev),
265 sc->nbuttons, sc->flags & UMS_Z ? " and Z dir." : "");
266
267 for (i = 1; i <= sc->nbuttons; i++)
268 hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),
269 hid_input, &sc->sc_loc_btn[i-1], 0);
270
271 sc->sc_isize = hid_report_size(desc, size, hid_input, &sc->sc_iid);
272 sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_NOWAIT);
273 if (sc->sc_ibuf == NULL) {
274 printf("%s: no memory\n", USBDEVNAME(sc->sc_dev));
275 free(sc->sc_loc_btn, M_USBDEV);
276 USB_ATTACH_ERROR_RETURN;
277 }
278
279 sc->sc_ep_addr = ed->bEndpointAddress;
280 free(desc, M_TEMP);
281
282 #ifdef USB_DEBUG
283 DPRINTF(("ums_attach: sc=%p\n", sc));
284 DPRINTF(("ums_attach: X\t%d/%d\n",
285 sc->sc_loc_x.pos, sc->sc_loc_x.size));
286 DPRINTF(("ums_attach: Y\t%d/%d\n",
287 sc->sc_loc_x.pos, sc->sc_loc_x.size));
288 if (sc->flags & UMS_Z)
289 DPRINTF(("ums_attach: Z\t%d/%d\n",
290 sc->sc_loc_z.pos, sc->sc_loc_z.size));
291 for (i = 1; i <= sc->nbuttons; i++) {
292 DPRINTF(("ums_attach: B%d\t%d/%d\n",
293 i, sc->sc_loc_btn[i-1].pos,sc->sc_loc_btn[i-1].size));
294 }
295 DPRINTF(("ums_attach: size=%d, id=%d\n", sc->sc_isize, sc->sc_iid));
296 #endif
297
298 a.accessops = &ums_accessops;
299 a.accesscookie = sc;
300
301 sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
302
303 USB_ATTACH_SUCCESS_RETURN;
304 }
305
306 int
307 ums_activate(self, act)
308 device_ptr_t self;
309 enum devact act;
310 {
311 struct ums_softc *sc = (struct ums_softc *)self;
312 int rv = 0;
313
314 switch (act) {
315 case DVACT_ACTIVATE:
316 return (EOPNOTSUPP);
317 break;
318
319 case DVACT_DEACTIVATE:
320 if (sc->sc_wsmousedev != NULL)
321 rv = config_deactivate(sc->sc_wsmousedev);
322 sc->sc_dying = 1;
323 break;
324 }
325 return (rv);
326 }
327
328 USB_DETACH(ums)
329 {
330 USB_DETACH_START(ums, sc);
331 int rv = 0;
332
333 DPRINTF(("ums_detach: sc=%p flags=%d\n", sc, flags));
334
335 /* No need to do reference counting of ums, wsmouse has all the goo. */
336 if (sc->sc_wsmousedev != NULL)
337 rv = config_detach(sc->sc_wsmousedev, flags);
338 if (rv == 0) {
339 free(sc->sc_loc_btn, M_USBDEV);
340 free(sc->sc_ibuf, M_USBDEV);
341 }
342 return (rv);
343 }
344
345 void
346 ums_intr(xfer, addr, status)
347 usbd_xfer_handle xfer;
348 usbd_private_handle addr;
349 usbd_status status;
350 {
351 struct ums_softc *sc = addr;
352 u_char *ibuf;
353 int dx, dy, dz;
354 u_int32_t buttons = 0;
355 int i;
356 int s;
357
358 DPRINTFN(5, ("ums_intr: sc=%p status=%d\n", sc, status));
359 DPRINTFN(5, ("ums_intr: data = %02x %02x %02x\n",
360 sc->sc_ibuf[0], sc->sc_ibuf[1], sc->sc_ibuf[2]));
361
362 if (status == USBD_CANCELLED)
363 return;
364
365 if (status != USBD_NORMAL_COMPLETION) {
366 DPRINTF(("ums_intr: status=%d\n", status));
367 usbd_clear_endpoint_stall_async(sc->sc_intrpipe);
368 return;
369 }
370
371 ibuf = sc->sc_ibuf;
372 if (sc->sc_iid != 0) {
373 if (*ibuf++ != sc->sc_iid)
374 return;
375 }
376 dx = hid_get_data(ibuf, &sc->sc_loc_x);
377 dy = -hid_get_data(ibuf, &sc->sc_loc_y);
378 dz = hid_get_data(ibuf, &sc->sc_loc_z);
379 if (sc->flags & UMS_REVZ)
380 dz = -dz;
381 for (i = 0; i < sc->nbuttons; i++)
382 if (hid_get_data(ibuf, &sc->sc_loc_btn[i]))
383 buttons |= (1 << UMS_BUT(i));
384
385 if (dx != 0 || dy != 0 || dz != 0 || buttons != sc->sc_buttons) {
386 DPRINTFN(10, ("ums_intr: x:%d y:%d z:%d buttons:0x%x\n",
387 dx, dy, dz, buttons));
388 sc->sc_buttons = buttons;
389 if (sc->sc_wsmousedev != NULL) {
390 s = spltty();
391 wsmouse_input(sc->sc_wsmousedev, buttons, dx, dy, dz);
392 splx(s);
393 }
394 }
395 }
396
397 static int
398 ums_enable(v)
399 void *v;
400 {
401 struct ums_softc *sc = v;
402
403 usbd_status err;
404
405 DPRINTFN(1,("ums_enable: sc=%p\n", sc));
406
407 if (sc->sc_dying)
408 return (EIO);
409
410 if (sc->sc_enabled)
411 return (EBUSY);
412
413 sc->sc_enabled = 1;
414 sc->sc_buttons = 0;
415
416 /* Set up interrupt pipe. */
417 err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
418 USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc,
419 sc->sc_ibuf, sc->sc_isize, ums_intr);
420 if (err) {
421 DPRINTF(("ums_enable: usbd_open_pipe_intr failed, error=%d\n",
422 err));
423 sc->sc_enabled = 0;
424 return (EIO);
425 }
426 return (0);
427 }
428
429 static void
430 ums_disable(v)
431 void *v;
432 {
433 struct ums_softc *sc = v;
434
435 DPRINTFN(1,("ums_disable: sc=%p\n", sc));
436 #ifdef DIAGNOSTIC
437 if (!sc->sc_enabled) {
438 printf("ums_disable: not enabled\n");
439 return;
440 }
441 #endif
442
443 /* Disable interrupts. */
444 usbd_abort_pipe(sc->sc_intrpipe);
445 usbd_close_pipe(sc->sc_intrpipe);
446
447 sc->sc_enabled = 0;
448 }
449
450 static int
451 ums_ioctl(v, cmd, data, flag, p)
452 void *v;
453 u_long cmd;
454 caddr_t data;
455 int flag;
456 struct proc *p;
457
458 {
459 switch (cmd) {
460 case WSMOUSEIO_GTYPE:
461 *(u_int *)data = WSMOUSE_TYPE_USB;
462 return (0);
463 }
464
465 return (-1);
466 }
467