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