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