ums.c revision 1.11 1 /* $NetBSD: ums.c,v 1.11 1998/11/25 22:32:05 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->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(": %s (interface class %d/%d)\n", devinfo,
180 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: \
189 bLength=%d bDescriptorType=%d bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d bInterval=%d\n",
190 ed->bLength, ed->bDescriptorType, ed->bEndpointAddress & UE_ADDR,
191 ed->bEndpointAddress & UE_IN ? "in" : "out",
192 ed->bmAttributes & UE_XFERTYPE,
193 UGETW(ed->wMaxPacketSize), ed->bInterval));
194
195 if ((ed->bEndpointAddress & UE_IN) != UE_IN ||
196 (ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
197 printf("%s: unexpected endpoint\n",
198 sc->sc_dev.dv_xname);
199 return;
200 }
201
202 r = usbd_alloc_report_desc(uaa->iface, &desc, &size, M_TEMP);
203 if (r != USBD_NORMAL_COMPLETION)
204 return;
205 if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
206 hid_input, &sc->sc_loc_x, &flags)) {
207 printf("%s: mouse has no X report\n", sc->sc_dev.dv_xname);
208 return;
209 }
210 if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS)
211 printf("%s: sorry, X report 0x%04x not supported yet\n",
212 sc->sc_dev.dv_xname, flags);
213
214 if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
215 hid_input, &sc->sc_loc_y, &flags)) {
216 printf("%s: mouse has no Y report\n", sc->sc_dev.dv_xname);
217 return;
218 }
219 if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS)
220 printf("%s: sorry, Y report 0x%04x not supported yet\n",
221 sc->sc_dev.dv_xname, flags);
222
223 if (hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Z),
224 hid_input, &sc->sc_loc_z, &flags) ||
225 hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_WHEEL),
226 hid_input, &sc->sc_loc_z, &flags)) {
227 if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS)
228 /* Bad Z coord, ignore it */
229 sc->sc_loc_z.size = 0;
230 }
231
232 hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, 1), /* left */
233 hid_input, &sc->sc_loc_btn1, 0);
234 hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, 2), /* right */
235 hid_input, &sc->sc_loc_btn2, 0);
236 hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, 3), /* middle */
237 hid_input, &sc->sc_loc_btn3, 0);
238 DPRINTF(("ums_attach: sc=%p\n", sc));
239 DPRINTF(("ums_attach: X %d/%d\n",
240 sc->sc_loc_x.pos, sc->sc_loc_x.size));
241 DPRINTF(("ums_attach: Y %d/%d\n",
242 sc->sc_loc_x.pos, sc->sc_loc_x.size));
243 DPRINTF(("ums_attach: Z %d/%d\n",
244 sc->sc_loc_z.pos, sc->sc_loc_z.size));
245 DPRINTF(("ums_attach: B1 %d/%d\n",
246 sc->sc_loc_btn1.pos,sc->sc_loc_btn1.size));
247 DPRINTF(("ums_attach: B2 %d/%d\n",
248 sc->sc_loc_btn2.pos,sc->sc_loc_btn2.size));
249 DPRINTF(("ums_attach: B3 %d/%d\n",
250 sc->sc_loc_btn3.pos,sc->sc_loc_btn3.size));
251
252 sc->sc_isize = hid_report_size(desc, size, hid_input, &sc->sc_iid);
253 DPRINTF(("ums_attach: size=%d, id=%d\n", sc->sc_isize, sc->sc_iid));
254 sc->sc_ibuf = malloc(sc->sc_isize, M_USB, M_NOWAIT);
255 if (!sc->sc_ibuf)
256 return;
257
258 sc->sc_ep_addr = ed->bEndpointAddress;
259 sc->sc_disconnected = 0;
260 free(desc, M_TEMP);
261
262 a.accessops = &ums_accessops;
263 a.accesscookie = sc;
264
265 sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
266 }
267
268 void
269 ums_disco(p)
270 void *p;
271 {
272 struct ums_softc *sc = p;
273
274 DPRINTF(("ums_disco: sc=%p\n", sc));
275 usbd_abort_pipe(sc->sc_intrpipe);
276 sc->sc_disconnected = 1;
277 }
278
279 void
280 ums_intr(reqh, addr, status)
281 usbd_request_handle reqh;
282 usbd_private_handle addr;
283 usbd_status status;
284 {
285 struct ums_softc *sc = addr;
286 u_char *ibuf;
287 int dx, dy, dz;
288 u_char buttons;
289
290 DPRINTFN(5, ("ums_intr: sc=%p status=%d\n", sc, status));
291 DPRINTFN(5, ("ums_intr: data = %02x %02x %02x\n",
292 sc->sc_ibuf[0], sc->sc_ibuf[1], sc->sc_ibuf[2]));
293
294 if (status == USBD_CANCELLED)
295 return;
296
297 if (status != USBD_NORMAL_COMPLETION) {
298 DPRINTF(("ums_intr: status=%d\n", status));
299 usbd_clear_endpoint_stall_async(sc->sc_intrpipe);
300 return;
301 }
302
303 ibuf = sc->sc_ibuf;
304 if (sc->sc_iid) {
305 if (*ibuf++ != sc->sc_iid)
306 return;
307 }
308 dx = hid_get_data(ibuf, &sc->sc_loc_x);
309 dy = -hid_get_data(ibuf, &sc->sc_loc_y);
310 dz = hid_get_data(ibuf, &sc->sc_loc_z);
311 buttons = 0;
312 if (hid_get_data(ibuf, &sc->sc_loc_btn1)) buttons |= 1 << 0;
313 if (hid_get_data(ibuf, &sc->sc_loc_btn2)) buttons |= 1 << 2;
314 if (hid_get_data(ibuf, &sc->sc_loc_btn3)) buttons |= 1 << 1;
315
316 if (dx || dy || buttons != sc->sc_buttons) {
317 sc->sc_buttons = buttons;
318 if (sc->sc_wsmousedev)
319 wsmouse_input(sc->sc_wsmousedev, buttons, dx, dy, dz);
320 }
321 }
322
323 /* wscons routines */
324 int
325 ums_enable(v)
326 void *v;
327 {
328 struct ums_softc *sc = v;
329 usbd_status r;
330
331 if (sc->sc_enabled)
332 return EBUSY;
333
334 sc->sc_enabled = 1;
335 sc->sc_buttons = 0;
336
337 /* Set up interrupt pipe. */
338 r = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
339 USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc,
340 sc->sc_ibuf, sc->sc_isize, ums_intr);
341 if (r != USBD_NORMAL_COMPLETION) {
342 DPRINTF(("ums_enable: usbd_open_pipe_intr failed, error=%d\n",
343 r));
344 sc->sc_enabled = 0;
345 return (EIO);
346 }
347 usbd_set_disco(sc->sc_intrpipe, ums_disco, sc);
348 return (0);
349 }
350
351 void
352 ums_disable(v)
353 void *v;
354 {
355 struct ums_softc *sc = v;
356
357 /* Disable interrupts. */
358 usbd_abort_pipe(sc->sc_intrpipe);
359 usbd_close_pipe(sc->sc_intrpipe);
360
361 sc->sc_enabled = 0;
362 }
363
364 int
365 ums_ioctl(v, cmd, data, flag, p)
366 void *v;
367 u_long cmd;
368 caddr_t data;
369 int flag;
370 struct proc *p;
371 {
372 #if 0
373 struct ums_softc *sc = v;
374 #endif
375
376 switch (cmd) {
377 case WSMOUSEIO_GTYPE:
378 *(u_int *)data = WSMOUSE_TYPE_PS2;
379 return (0);
380 }
381 return (-1);
382 }
383
384