ums.c revision 1.28 1 /* $NetBSD: ums.c,v 1.28 1999/08/23 22:55: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 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #include <sys/device.h>
49 #include <sys/ioctl.h>
50 #include <sys/tty.h>
51 #include <sys/file.h>
52 #include <sys/select.h>
53 #include <sys/proc.h>
54 #include <sys/vnode.h>
55 #include <sys/poll.h>
56
57 #include <dev/usb/usb.h>
58 #include <dev/usb/usbhid.h>
59
60 #include <dev/usb/usbdi.h>
61 #include <dev/usb/usbdi_util.h>
62 #include <dev/usb/usbdevs.h>
63 #include <dev/usb/usb_quirks.h>
64 #include <dev/usb/hid.h>
65
66 #include <dev/wscons/wsconsio.h>
67 #include <dev/wscons/wsmousevar.h>
68
69 #ifdef USB_DEBUG
70 #define DPRINTF(x) if (umsdebug) logprintf x
71 #define DPRINTFN(n,x) if (umsdebug>(n)) logprintf x
72 int umsdebug = 0;
73 #else
74 #define DPRINTF(x)
75 #define DPRINTFN(n,x)
76 #endif
77
78 #define UMS_BUT(i) ((i) == 1 || (i) == 2 ? 3 - (i) : i)
79
80 #define UMSUNIT(s) (minor(s))
81
82 #define PS2LBUTMASK x01
83 #define PS2RBUTMASK x02
84 #define PS2MBUTMASK x04
85 #define PS2BUTMASK 0x0f
86
87 struct ums_softc {
88 bdevice sc_dev; /* base device */
89 usbd_interface_handle sc_iface; /* interface */
90 usbd_pipe_handle sc_intrpipe; /* interrupt pipe */
91 int sc_ep_addr;
92
93 u_char *sc_ibuf;
94 u_int8_t sc_iid;
95 int sc_isize;
96 struct hid_location sc_loc_x, sc_loc_y, sc_loc_z;
97 struct hid_location *sc_loc_btn;
98
99 int sc_enabled;
100
101 int flags; /* device configuration */
102 #define UMS_Z 0x01 /* z direction available */
103 int nbuttons;
104 #define MAX_BUTTONS 31 /* chosen because sc_buttons is u_int32_t */
105
106 char revz; /* Z-axis is reversed */
107
108 u_int32_t sc_buttons; /* mouse button status */
109 struct device *sc_wsmousedev;
110
111 char sc_dying;
112 };
113
114 #define MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
115 #define MOUSE_FLAGS (HIO_RELATIVE)
116
117 void ums_intr __P((usbd_request_handle, usbd_private_handle, usbd_status));
118
119 static int ums_enable __P((void *));
120 static void ums_disable __P((void *));
121 static int ums_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
122
123 const struct wsmouse_accessops ums_accessops = {
124 ums_enable,
125 ums_ioctl,
126 ums_disable,
127 };
128
129 USB_DECLARE_DRIVER(ums);
130
131 USB_MATCH(ums)
132 {
133 USB_MATCH_START(ums, uaa);
134 usb_interface_descriptor_t *id;
135 int size, ret;
136 void *desc;
137 usbd_status r;
138
139 if (!uaa->iface)
140 return (UMATCH_NONE);
141 id = usbd_get_interface_descriptor(uaa->iface);
142 if (!id || id->bInterfaceClass != UCLASS_HID)
143 return (UMATCH_NONE);
144
145 r = usbd_alloc_report_desc(uaa->iface, &desc, &size, M_TEMP);
146 if (r != USBD_NORMAL_COMPLETION)
147 return (UMATCH_NONE);
148
149 if (hid_is_collection(desc, size,
150 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE)))
151 ret = UMATCH_IFACECLASS;
152 else
153 ret = UMATCH_NONE;
154
155 free(desc, M_TEMP);
156 return (ret);
157 }
158
159 USB_ATTACH(ums)
160 {
161 USB_ATTACH_START(ums, sc, uaa);
162 usbd_interface_handle iface = uaa->iface;
163 usb_interface_descriptor_t *id;
164 usb_endpoint_descriptor_t *ed;
165 struct wsmousedev_attach_args a;
166 int size;
167 void *desc;
168 usbd_status r;
169 char devinfo[1024];
170 u_int32_t flags;
171 int i;
172 struct hid_location loc_btn;
173
174 sc->sc_iface = iface;
175 id = usbd_get_interface_descriptor(iface);
176 usbd_devinfo(uaa->device, 0, devinfo);
177 USB_ATTACH_SETUP;
178 printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
179 devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
180 ed = usbd_interface2endpoint_descriptor(iface, 0);
181 if (!ed) {
182 printf("%s: could not read endpoint descriptor\n",
183 USBDEVNAME(sc->sc_dev));
184 USB_ATTACH_ERROR_RETURN;
185 }
186
187 DPRINTFN(10,("ums_attach: bLength=%d bDescriptorType=%d "
188 "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
189 " bInterval=%d\n",
190 ed->bLength, ed->bDescriptorType,
191 ed->bEndpointAddress & UE_ADDR,
192 ed->bEndpointAddress & UE_IN ? "in" : "out",
193 ed->bmAttributes & UE_XFERTYPE,
194 UGETW(ed->wMaxPacketSize), ed->bInterval));
195
196 if ((ed->bEndpointAddress & UE_IN) != UE_IN ||
197 (ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
198 printf("%s: unexpected endpoint\n",
199 USBDEVNAME(sc->sc_dev));
200 USB_ATTACH_ERROR_RETURN;
201 }
202
203 sc->revz = (usbd_get_quirks(uaa->device)->uq_flags & UQ_MS_REVZ) != 0;
204
205 r = usbd_alloc_report_desc(uaa->iface, &desc, &size, M_TEMP);
206 if (r != USBD_NORMAL_COMPLETION)
207 USB_ATTACH_ERROR_RETURN;
208
209 if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
210 hid_input, &sc->sc_loc_x, &flags)) {
211 printf("%s: mouse has no X report\n", USBDEVNAME(sc->sc_dev));
212 USB_ATTACH_ERROR_RETURN;
213 }
214 if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
215 printf("%s: X report 0x%04x not supported\n",
216 USBDEVNAME(sc->sc_dev), flags);
217 USB_ATTACH_ERROR_RETURN;
218 }
219
220 if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
221 hid_input, &sc->sc_loc_y, &flags)) {
222 printf("%s: mouse has no Y report\n", USBDEVNAME(sc->sc_dev));
223 USB_ATTACH_ERROR_RETURN;
224 }
225 if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
226 printf("%s: Y report 0x%04x not supported\n",
227 USBDEVNAME(sc->sc_dev), flags);
228 USB_ATTACH_ERROR_RETURN;
229 }
230
231 /* Try to guess the Z activator: first check Z, then WHEEL. */
232 if (hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Z),
233 hid_input, &sc->sc_loc_z, &flags) ||
234 hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_WHEEL),
235 hid_input, &sc->sc_loc_z, &flags)) {
236 if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
237 sc->sc_loc_z.size = 0; /* Bad Z coord, ignore it */
238 } else {
239 sc->flags |= UMS_Z;
240 }
241 }
242
243 /* figure out the number of buttons */
244 for (i = 1; i <= MAX_BUTTONS; i++)
245 if (!hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),
246 hid_input, &loc_btn, 0))
247 break;
248 sc->nbuttons = i - 1;
249 sc->sc_loc_btn = malloc(sizeof(struct hid_location)*sc->nbuttons,
250 M_USBDEV, M_NOWAIT);
251 if (!sc->sc_loc_btn) {
252 printf("%s: no memory\n", USBDEVNAME(sc->sc_dev));
253 USB_ATTACH_ERROR_RETURN;
254 }
255
256 printf("%s: %d buttons%s\n", USBDEVNAME(sc->sc_dev),
257 sc->nbuttons, sc->flags & UMS_Z ? " and Z dir." : "");
258
259 for (i = 1; i <= sc->nbuttons; i++)
260 hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),
261 hid_input, &sc->sc_loc_btn[i-1], 0);
262
263 sc->sc_isize = hid_report_size(desc, size, hid_input, &sc->sc_iid);
264 sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_NOWAIT);
265 if (!sc->sc_ibuf) {
266 printf("%s: no memory\n", USBDEVNAME(sc->sc_dev));
267 free(sc->sc_loc_btn, M_USBDEV);
268 USB_ATTACH_ERROR_RETURN;
269 }
270
271 sc->sc_ep_addr = ed->bEndpointAddress;
272 free(desc, M_TEMP);
273
274 #ifdef USB_DEBUG
275 DPRINTF(("ums_attach: sc=%p\n", sc));
276 DPRINTF(("ums_attach: X\t%d/%d\n",
277 sc->sc_loc_x.pos, sc->sc_loc_x.size));
278 DPRINTF(("ums_attach: Y\t%d/%d\n",
279 sc->sc_loc_x.pos, sc->sc_loc_x.size));
280 if (sc->flags & UMS_Z)
281 DPRINTF(("ums_attach: Z\t%d/%d\n",
282 sc->sc_loc_z.pos, sc->sc_loc_z.size));
283 for (i = 1; i <= sc->nbuttons; i++) {
284 DPRINTF(("ums_attach: B%d\t%d/%d\n",
285 i, sc->sc_loc_btn[i-1].pos,sc->sc_loc_btn[i-1].size));
286 }
287 DPRINTF(("ums_attach: size=%d, id=%d\n", sc->sc_isize, sc->sc_iid));
288 #endif
289
290 a.accessops = &ums_accessops;
291 a.accesscookie = sc;
292
293 sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
294
295 USB_ATTACH_SUCCESS_RETURN;
296 }
297
298 int
299 ums_activate(self, act)
300 struct device *self;
301 enum devact act;
302 {
303 struct ums_softc *sc = (struct ums_softc *)self;
304
305 switch (act) {
306 case DVACT_ACTIVATE:
307 return (EOPNOTSUPP);
308 break;
309
310 case DVACT_DEACTIVATE:
311 sc->sc_dying = 1;
312 break;
313 }
314 return (0);
315 }
316
317 int
318 ums_detach(self, flags)
319 struct device *self;
320 int flags;
321 {
322 struct ums_softc *sc = (struct ums_softc *)self;
323 int rv = 0;
324
325 DPRINTF(("ums_detach: sc=%p flags=%d\n", sc, flags));
326 /* No need to do reference counting of ums, wsmouse has all the goo. */
327 if (sc->sc_wsmousedev)
328 rv = config_detach(sc->sc_wsmousedev, flags);
329 if (rv == 0) {
330 free(sc->sc_loc_btn, M_USBDEV);
331 free(sc->sc_ibuf, M_USBDEV);
332 }
333 return (rv);
334 }
335
336 void
337 ums_intr(reqh, addr, status)
338 usbd_request_handle reqh;
339 usbd_private_handle addr;
340 usbd_status status;
341 {
342 struct ums_softc *sc = addr;
343 u_char *ibuf;
344 int dx, dy, dz;
345 u_int32_t buttons = 0;
346 int i;
347 int s;
348
349 DPRINTFN(5, ("ums_intr: sc=%p status=%d\n", sc, status));
350 DPRINTFN(5, ("ums_intr: data = %02x %02x %02x\n",
351 sc->sc_ibuf[0], sc->sc_ibuf[1], sc->sc_ibuf[2]));
352
353 if (status == USBD_CANCELLED)
354 return;
355
356 if (status != USBD_NORMAL_COMPLETION) {
357 DPRINTF(("ums_intr: status=%d\n", status));
358 usbd_clear_endpoint_stall_async(sc->sc_intrpipe);
359 return;
360 }
361
362 ibuf = sc->sc_ibuf;
363 if (sc->sc_iid) {
364 if (*ibuf++ != sc->sc_iid)
365 return;
366 }
367 dx = hid_get_data(ibuf, &sc->sc_loc_x);
368 dy = -hid_get_data(ibuf, &sc->sc_loc_y);
369 dz = hid_get_data(ibuf, &sc->sc_loc_z);
370 if (sc->revz)
371 dz = -dz;
372 for (i = 0; i < sc->nbuttons; i++)
373 if (hid_get_data(ibuf, &sc->sc_loc_btn[i]))
374 buttons |= (1 << UMS_BUT(i));
375
376 if (dx || dy || dz || buttons != sc->sc_buttons) {
377 DPRINTFN(10, ("ums_intr: x:%d y:%d z:%d buttons:0x%x\n",
378 dx, dy, dz, buttons));
379 sc->sc_buttons = buttons;
380 if (sc->sc_wsmousedev) {
381 s = spltty();
382 wsmouse_input(sc->sc_wsmousedev, buttons, dx, dy, dz);
383 splx(s);
384 }
385 }
386 }
387
388 static int
389 ums_enable(v)
390 void *v;
391 {
392 struct ums_softc *sc = v;
393
394 usbd_status r;
395
396 DPRINTFN(1,("ums_enable: sc=%p\n", sc));
397
398 if (sc->sc_dying)
399 return (EIO);
400
401 if (sc->sc_enabled)
402 return (EBUSY);
403
404 sc->sc_enabled = 1;
405 sc->sc_buttons = 0;
406
407 /* Set up interrupt pipe. */
408 r = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
409 USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc,
410 sc->sc_ibuf, sc->sc_isize, ums_intr);
411 if (r != USBD_NORMAL_COMPLETION) {
412 DPRINTF(("ums_enable: usbd_open_pipe_intr failed, error=%d\n",
413 r));
414 sc->sc_enabled = 0;
415 return (EIO);
416 }
417 return (0);
418 }
419
420 static void
421 ums_disable(v)
422 void *v;
423 {
424 struct ums_softc *sc = v;
425
426 DPRINTFN(1,("ums_disable: sc=%p\n", sc));
427 #ifdef DIAGNOSTIC
428 if (!sc->sc_enabled) {
429 printf("ums_disable: not enabled\n");
430 return;
431 }
432 #endif
433
434 /* Disable interrupts. */
435 usbd_abort_pipe(sc->sc_intrpipe);
436 usbd_close_pipe(sc->sc_intrpipe);
437
438 sc->sc_enabled = 0;
439 }
440
441 static int
442 ums_ioctl(v, cmd, data, flag, p)
443 void *v;
444 u_long cmd;
445 caddr_t data;
446 int flag;
447 struct proc *p;
448
449 {
450 switch (cmd) {
451 case WSMOUSEIO_GTYPE:
452 *(u_int *)data = WSMOUSE_TYPE_USB;
453 return (0);
454 }
455
456 return (-1);
457 }
458
459