ums.c revision 1.9 1 /* $NetBSD: ums.c,v 1.9 1998/10/01 19:10:26 matt 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 <dev/usb/usb.h>
55 #include <dev/usb/usbhid.h>
56
57 #include <dev/usb/usbdi.h>
58 #include <dev/usb/usbdi_util.h>
59 #include <dev/usb/usbdevs.h>
60 #include <dev/usb/usb_quirks.h>
61 #include <dev/usb/hid.h>
62
63 #include <dev/wscons/wsconsio.h>
64 #include <dev/wscons/wsmousevar.h>
65
66 #ifdef USB_DEBUG
67 #define DPRINTF(x) if (umsdebug) printf x
68 #define DPRINTFN(n,x) if (umsdebug>(n)) printf x
69 int umsdebug = 0;
70 #else
71 #define DPRINTF(x)
72 #define DPRINTFN(n,x)
73 #endif
74
75 #define PS2LBUTMASK 0x01
76 #define PS2RBUTMASK 0x02
77 #define PS2MBUTMASK 0x04
78 #define PS2BUTMASK 0x0f
79
80 struct ums_softc {
81 struct device sc_dev; /* base device */
82 usbd_interface_handle sc_iface; /* interface */
83 usbd_pipe_handle sc_intrpipe; /* interrupt pipe */
84 int sc_ep_addr;
85
86 u_char *sc_ibuf;
87 u_int8_t sc_iid;
88 int sc_isize;
89 struct hid_location sc_loc_x, sc_loc_y, sc_loc_z,
90 sc_loc_btn1, sc_loc_btn2, sc_loc_btn3;
91
92 u_char sc_buttons; /* mouse button status */
93 int sc_disconnected; /* device is gone */
94
95 int sc_enabled;
96 struct device *sc_wsmousedev;
97 };
98
99 #define MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
100 #define MOUSE_FLAGS (HIO_RELATIVE)
101
102 int ums_match __P((struct device *, struct cfdata *, void *));
103 void ums_attach __P((struct device *, struct device *, void *));
104
105 void ums_intr __P((usbd_request_handle, usbd_private_handle, usbd_status));
106 void ums_disco __P((void *));
107
108 int ums_enable __P((void *));
109 int ums_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
110 void ums_disable __P((void *));
111
112 const struct wsmouse_accessops ums_accessops = {
113 ums_enable,
114 ums_ioctl,
115 ums_disable,
116 };
117
118 extern struct cfdriver ums_cd;
119
120 struct cfattach ums_ca = {
121 sizeof(struct ums_softc), ums_match, ums_attach
122 };
123
124 int
125 ums_match(parent, match, aux)
126 struct device *parent;
127 struct cfdata *match;
128 void *aux;
129 {
130 struct usb_attach_arg *uaa = aux;
131 usb_interface_descriptor_t *id;
132 int size, ret;
133 void *desc;
134 usbd_status r;
135
136 if (!uaa->iface)
137 return (UMATCH_NONE);
138 id = usbd_get_interface_descriptor(uaa->iface);
139 if (id->bInterfaceClass != UCLASS_HID)
140 return (UMATCH_NONE);
141
142 r = usbd_alloc_report_desc(uaa->iface, &desc, &size, M_TEMP);
143 if (r != USBD_NORMAL_COMPLETION)
144 return (UMATCH_NONE);
145
146 if (hid_is_collection(desc, size,
147 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE)))
148 ret = UMATCH_IFACECLASS;
149 else
150 ret = UMATCH_NONE;
151
152 free(desc, M_TEMP);
153 return (ret);
154 }
155
156 void
157 ums_attach(parent, self, aux)
158 struct device *parent;
159 struct device *self;
160 void *aux;
161 {
162 struct ums_softc *sc = (struct ums_softc *)self;
163 struct usb_attach_arg *uaa = aux;
164 usbd_interface_handle iface = uaa->iface;
165 usb_interface_descriptor_t *id;
166 usb_endpoint_descriptor_t *ed;
167 struct wsmousedev_attach_args a;
168 int size;
169 void *desc;
170 usbd_status r;
171 char devinfo[1024];
172 u_int32_t flags;
173
174 sc->sc_disconnected = 1;
175 sc->sc_iface = iface;
176 id = usbd_get_interface_descriptor(iface);
177 usbd_devinfo(uaa->device, 0, devinfo);
178 printf(": %s (interface class %d/%d)\n", devinfo,
179 id->bInterfaceClass, id->bInterfaceSubClass);
180 ed = usbd_interface2endpoint_descriptor(iface, 0);
181 if (!ed) {
182 printf("%s: could not read endpoint descriptor\n",
183 sc->sc_dev.dv_xname);
184 return;
185 }
186
187 DPRINTFN(10,("ums_attach: \
188 bLength=%d bDescriptorType=%d bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d bInterval=%d\n",
189 ed->bLength, ed->bDescriptorType, 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 sc->sc_dev.dv_xname);
198 return;
199 }
200
201 r = usbd_alloc_report_desc(uaa->iface, &desc, &size, M_TEMP);
202 if (r != USBD_NORMAL_COMPLETION)
203 return;
204 if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
205 hid_input, &sc->sc_loc_x, &flags)) {
206 printf("%s: mouse has no X report\n", sc->sc_dev.dv_xname);
207 return;
208 }
209 if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS)
210 printf("%s: sorry, X report 0x%04x not supported yet\n",
211 sc->sc_dev.dv_xname, flags);
212 if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
213 hid_input, &sc->sc_loc_y, &flags)) {
214 printf("%s: mouse has no Y report\n", sc->sc_dev.dv_xname);
215 return;
216 }
217 if (hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Z),
218 hid_input, &sc->sc_loc_z, &flags) ||
219 hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_WHEEL),
220 hid_input, &sc->sc_loc_z, &flags)) {
221 if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS)
222 /* Bad Z coord, ignore it */
223 sc->sc_loc_z.size = 0;
224 }
225
226 if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS)
227 printf("%s: sorry, Y report 0x%04x not supported yet\n",
228 sc->sc_dev.dv_xname, flags);
229 hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, 1), /* left */
230 hid_input, &sc->sc_loc_btn1, 0);
231 hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, 2), /* right */
232 hid_input, &sc->sc_loc_btn2, 0);
233 hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, 3), /* middle */
234 hid_input, &sc->sc_loc_btn3, 0);
235 DPRINTF(("ums_attach: sc=%p\n", sc));
236 DPRINTF(("ums_attach: X %d/%d\n",
237 sc->sc_loc_x.pos, sc->sc_loc_x.size));
238 DPRINTF(("ums_attach: Y %d/%d\n",
239 sc->sc_loc_x.pos, sc->sc_loc_x.size));
240 DPRINTF(("ums_attach: Z %d/%d\n",
241 sc->sc_loc_z.pos, sc->sc_loc_z.size));
242 DPRINTF(("ums_attach: B1 %d/%d\n",
243 sc->sc_loc_btn1.pos,sc->sc_loc_btn1.size));
244 DPRINTF(("ums_attach: B2 %d/%d\n",
245 sc->sc_loc_btn2.pos,sc->sc_loc_btn2.size));
246 DPRINTF(("ums_attach: B3 %d/%d\n",
247 sc->sc_loc_btn3.pos,sc->sc_loc_btn3.size));
248
249 sc->sc_isize = hid_report_size(desc, size, hid_input, &sc->sc_iid);
250 DPRINTF(("ums_attach: size=%d, id=%d\n", sc->sc_isize, sc->sc_iid));
251 sc->sc_ibuf = malloc(sc->sc_isize, M_USB, M_NOWAIT);
252 if (!sc->sc_ibuf)
253 return;
254
255 sc->sc_ep_addr = ed->bEndpointAddress;
256 sc->sc_disconnected = 0;
257 free(desc, M_TEMP);
258
259 a.accessops = &ums_accessops;
260 a.accesscookie = sc;
261
262 sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
263 }
264
265 void
266 ums_disco(p)
267 void *p;
268 {
269 struct ums_softc *sc = p;
270
271 DPRINTF(("ums_disco: sc=%p\n", sc));
272 usbd_abort_pipe(sc->sc_intrpipe);
273 sc->sc_disconnected = 1;
274 }
275
276 void
277 ums_intr(reqh, addr, status)
278 usbd_request_handle reqh;
279 usbd_private_handle addr;
280 usbd_status status;
281 {
282 struct ums_softc *sc = addr;
283 u_char *ibuf;
284 int dx, dy, dz;
285 u_char buttons;
286
287 DPRINTFN(5, ("ums_intr: sc=%p status=%d\n", sc, status));
288 DPRINTFN(5, ("ums_intr: data = %02x %02x %02x\n",
289 sc->sc_ibuf[0], sc->sc_ibuf[1], sc->sc_ibuf[2]));
290
291 if (status == USBD_CANCELLED)
292 return;
293
294 if (status != USBD_NORMAL_COMPLETION) {
295 DPRINTF(("ums_intr: status=%d\n", status));
296 usbd_clear_endpoint_stall_async(sc->sc_intrpipe);
297 return;
298 }
299
300 ibuf = sc->sc_ibuf;
301 if (sc->sc_iid) {
302 if (*ibuf++ != sc->sc_iid)
303 return;
304 }
305 dx = hid_get_data(ibuf, &sc->sc_loc_x);
306 dy = -hid_get_data(ibuf, &sc->sc_loc_y);
307 dz = hid_get_data(ibuf, &sc->sc_loc_z);
308 buttons = 0;
309 if (hid_get_data(ibuf, &sc->sc_loc_btn1)) buttons |= 1 << 0;
310 if (hid_get_data(ibuf, &sc->sc_loc_btn2)) buttons |= 1 << 2;
311 if (hid_get_data(ibuf, &sc->sc_loc_btn3)) buttons |= 1 << 1;
312
313 if (dx || dy || buttons != sc->sc_buttons) {
314 sc->sc_buttons = buttons;
315 if (sc->sc_wsmousedev)
316 wsmouse_input(sc->sc_wsmousedev, buttons, dx, dy, dz);
317 }
318 }
319
320 /* wscons routines */
321 int
322 ums_enable(v)
323 void *v;
324 {
325 struct ums_softc *sc = v;
326 usbd_status r;
327
328 if (sc->sc_enabled)
329 return EBUSY;
330
331 sc->sc_enabled = 1;
332 sc->sc_buttons = 0;
333
334 /* Set up interrupt pipe. */
335 r = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
336 USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc,
337 sc->sc_ibuf, sc->sc_isize, ums_intr);
338 if (r != USBD_NORMAL_COMPLETION) {
339 DPRINTF(("ums_enable: usbd_open_pipe_intr failed, error=%d\n",
340 r));
341 sc->sc_enabled = 0;
342 return (EIO);
343 }
344 usbd_set_disco(sc->sc_intrpipe, ums_disco, sc);
345 return (0);
346 }
347
348 void
349 ums_disable(v)
350 void *v;
351 {
352 struct ums_softc *sc = v;
353
354 /* Disable interrupts. */
355 usbd_abort_pipe(sc->sc_intrpipe);
356 usbd_close_pipe(sc->sc_intrpipe);
357
358 sc->sc_enabled = 0;
359 }
360
361 int
362 ums_ioctl(v, cmd, data, flag, p)
363 void *v;
364 u_long cmd;
365 caddr_t data;
366 int flag;
367 struct proc *p;
368 {
369 #if 0
370 struct ums_softc *sc = v;
371 #endif
372
373 switch (cmd) {
374 case WSMOUSEIO_GTYPE:
375 *(u_int *)data = WSMOUSE_TYPE_PS2;
376 return (0);
377 }
378 return (-1);
379 }
380
381