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