ums.c revision 1.11 1 1.11 augustss /* $NetBSD: ums.c,v 1.11 1998/11/25 22:32:05 augustss Exp $ */
2 1.1 augustss
3 1.1 augustss /*
4 1.1 augustss * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 1.1 augustss * All rights reserved.
6 1.1 augustss *
7 1.11 augustss * This code is derived from software contributed to The NetBSD Foundation
8 1.11 augustss * by Lennart Augustsson (augustss (at) carlstedt.se) at
9 1.11 augustss * Carlstedt Research & Technology.
10 1.1 augustss *
11 1.1 augustss * Redistribution and use in source and binary forms, with or without
12 1.1 augustss * modification, are permitted provided that the following conditions
13 1.1 augustss * are met:
14 1.1 augustss * 1. Redistributions of source code must retain the above copyright
15 1.1 augustss * notice, this list of conditions and the following disclaimer.
16 1.1 augustss * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 augustss * notice, this list of conditions and the following disclaimer in the
18 1.1 augustss * documentation and/or other materials provided with the distribution.
19 1.1 augustss * 3. All advertising materials mentioning features or use of this software
20 1.1 augustss * must display the following acknowledgement:
21 1.1 augustss * This product includes software developed by the NetBSD
22 1.1 augustss * Foundation, Inc. and its contributors.
23 1.1 augustss * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.1 augustss * contributors may be used to endorse or promote products derived
25 1.1 augustss * from this software without specific prior written permission.
26 1.1 augustss *
27 1.1 augustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.1 augustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.1 augustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.1 augustss * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.1 augustss * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.1 augustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.1 augustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.1 augustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.1 augustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.1 augustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.1 augustss * POSSIBILITY OF SUCH DAMAGE.
38 1.1 augustss */
39 1.1 augustss
40 1.1 augustss
41 1.1 augustss #include <sys/param.h>
42 1.1 augustss #include <sys/systm.h>
43 1.1 augustss #include <sys/kernel.h>
44 1.1 augustss #include <sys/malloc.h>
45 1.1 augustss #include <sys/device.h>
46 1.1 augustss #include <sys/ioctl.h>
47 1.1 augustss #include <sys/tty.h>
48 1.1 augustss #include <sys/file.h>
49 1.1 augustss #include <sys/select.h>
50 1.1 augustss #include <sys/proc.h>
51 1.1 augustss #include <sys/vnode.h>
52 1.1 augustss #include <sys/device.h>
53 1.1 augustss #include <sys/poll.h>
54 1.1 augustss
55 1.1 augustss #include <dev/usb/usb.h>
56 1.1 augustss #include <dev/usb/usbhid.h>
57 1.1 augustss
58 1.1 augustss #include <dev/usb/usbdi.h>
59 1.1 augustss #include <dev/usb/usbdi_util.h>
60 1.1 augustss #include <dev/usb/usbdevs.h>
61 1.1 augustss #include <dev/usb/usb_quirks.h>
62 1.1 augustss #include <dev/usb/hid.h>
63 1.1 augustss
64 1.3 augustss #include <dev/wscons/wsconsio.h>
65 1.3 augustss #include <dev/wscons/wsmousevar.h>
66 1.3 augustss
67 1.1 augustss #ifdef USB_DEBUG
68 1.1 augustss #define DPRINTF(x) if (umsdebug) printf x
69 1.1 augustss #define DPRINTFN(n,x) if (umsdebug>(n)) printf x
70 1.1 augustss int umsdebug = 0;
71 1.1 augustss #else
72 1.1 augustss #define DPRINTF(x)
73 1.1 augustss #define DPRINTFN(n,x)
74 1.1 augustss #endif
75 1.1 augustss
76 1.1 augustss #define PS2LBUTMASK 0x01
77 1.1 augustss #define PS2RBUTMASK 0x02
78 1.1 augustss #define PS2MBUTMASK 0x04
79 1.1 augustss #define PS2BUTMASK 0x0f
80 1.1 augustss
81 1.1 augustss struct ums_softc {
82 1.1 augustss struct device sc_dev; /* base device */
83 1.1 augustss usbd_interface_handle sc_iface; /* interface */
84 1.1 augustss usbd_pipe_handle sc_intrpipe; /* interrupt pipe */
85 1.1 augustss int sc_ep_addr;
86 1.1 augustss
87 1.1 augustss u_char *sc_ibuf;
88 1.1 augustss u_int8_t sc_iid;
89 1.1 augustss int sc_isize;
90 1.6 augustss struct hid_location sc_loc_x, sc_loc_y, sc_loc_z,
91 1.1 augustss sc_loc_btn1, sc_loc_btn2, sc_loc_btn3;
92 1.1 augustss
93 1.4 augustss u_char sc_buttons; /* mouse button status */
94 1.1 augustss int sc_disconnected; /* device is gone */
95 1.3 augustss
96 1.3 augustss int sc_enabled;
97 1.3 augustss struct device *sc_wsmousedev;
98 1.1 augustss };
99 1.1 augustss
100 1.2 augustss #define MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
101 1.2 augustss #define MOUSE_FLAGS (HIO_RELATIVE)
102 1.2 augustss
103 1.1 augustss int ums_match __P((struct device *, struct cfdata *, void *));
104 1.1 augustss void ums_attach __P((struct device *, struct device *, void *));
105 1.1 augustss
106 1.1 augustss void ums_intr __P((usbd_request_handle, usbd_private_handle, usbd_status));
107 1.1 augustss void ums_disco __P((void *));
108 1.1 augustss
109 1.3 augustss int ums_enable __P((void *));
110 1.3 augustss int ums_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
111 1.3 augustss void ums_disable __P((void *));
112 1.3 augustss
113 1.3 augustss const struct wsmouse_accessops ums_accessops = {
114 1.3 augustss ums_enable,
115 1.3 augustss ums_ioctl,
116 1.3 augustss ums_disable,
117 1.3 augustss };
118 1.3 augustss
119 1.1 augustss extern struct cfdriver ums_cd;
120 1.1 augustss
121 1.1 augustss struct cfattach ums_ca = {
122 1.1 augustss sizeof(struct ums_softc), ums_match, ums_attach
123 1.1 augustss };
124 1.1 augustss
125 1.1 augustss int
126 1.1 augustss ums_match(parent, match, aux)
127 1.1 augustss struct device *parent;
128 1.1 augustss struct cfdata *match;
129 1.1 augustss void *aux;
130 1.1 augustss {
131 1.1 augustss struct usb_attach_arg *uaa = aux;
132 1.1 augustss usb_interface_descriptor_t *id;
133 1.1 augustss int size, ret;
134 1.1 augustss void *desc;
135 1.1 augustss usbd_status r;
136 1.1 augustss
137 1.1 augustss if (!uaa->iface)
138 1.1 augustss return (UMATCH_NONE);
139 1.1 augustss id = usbd_get_interface_descriptor(uaa->iface);
140 1.1 augustss if (id->bInterfaceClass != UCLASS_HID)
141 1.1 augustss return (UMATCH_NONE);
142 1.1 augustss
143 1.1 augustss r = usbd_alloc_report_desc(uaa->iface, &desc, &size, M_TEMP);
144 1.1 augustss if (r != USBD_NORMAL_COMPLETION)
145 1.1 augustss return (UMATCH_NONE);
146 1.1 augustss
147 1.1 augustss if (hid_is_collection(desc, size,
148 1.1 augustss HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE)))
149 1.1 augustss ret = UMATCH_IFACECLASS;
150 1.1 augustss else
151 1.1 augustss ret = UMATCH_NONE;
152 1.1 augustss
153 1.1 augustss free(desc, M_TEMP);
154 1.1 augustss return (ret);
155 1.1 augustss }
156 1.1 augustss
157 1.1 augustss void
158 1.1 augustss ums_attach(parent, self, aux)
159 1.1 augustss struct device *parent;
160 1.1 augustss struct device *self;
161 1.1 augustss void *aux;
162 1.1 augustss {
163 1.1 augustss struct ums_softc *sc = (struct ums_softc *)self;
164 1.1 augustss struct usb_attach_arg *uaa = aux;
165 1.1 augustss usbd_interface_handle iface = uaa->iface;
166 1.1 augustss usb_interface_descriptor_t *id;
167 1.1 augustss usb_endpoint_descriptor_t *ed;
168 1.3 augustss struct wsmousedev_attach_args a;
169 1.1 augustss int size;
170 1.1 augustss void *desc;
171 1.1 augustss usbd_status r;
172 1.1 augustss char devinfo[1024];
173 1.2 augustss u_int32_t flags;
174 1.1 augustss
175 1.1 augustss sc->sc_disconnected = 1;
176 1.1 augustss sc->sc_iface = iface;
177 1.1 augustss id = usbd_get_interface_descriptor(iface);
178 1.1 augustss usbd_devinfo(uaa->device, 0, devinfo);
179 1.1 augustss printf(": %s (interface class %d/%d)\n", devinfo,
180 1.1 augustss id->bInterfaceClass, id->bInterfaceSubClass);
181 1.1 augustss ed = usbd_interface2endpoint_descriptor(iface, 0);
182 1.1 augustss if (!ed) {
183 1.1 augustss printf("%s: could not read endpoint descriptor\n",
184 1.1 augustss sc->sc_dev.dv_xname);
185 1.1 augustss return;
186 1.1 augustss }
187 1.1 augustss
188 1.1 augustss DPRINTFN(10,("ums_attach: \
189 1.1 augustss bLength=%d bDescriptorType=%d bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d bInterval=%d\n",
190 1.1 augustss ed->bLength, ed->bDescriptorType, ed->bEndpointAddress & UE_ADDR,
191 1.1 augustss ed->bEndpointAddress & UE_IN ? "in" : "out",
192 1.1 augustss ed->bmAttributes & UE_XFERTYPE,
193 1.1 augustss UGETW(ed->wMaxPacketSize), ed->bInterval));
194 1.1 augustss
195 1.1 augustss if ((ed->bEndpointAddress & UE_IN) != UE_IN ||
196 1.1 augustss (ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
197 1.1 augustss printf("%s: unexpected endpoint\n",
198 1.1 augustss sc->sc_dev.dv_xname);
199 1.1 augustss return;
200 1.1 augustss }
201 1.1 augustss
202 1.1 augustss r = usbd_alloc_report_desc(uaa->iface, &desc, &size, M_TEMP);
203 1.1 augustss if (r != USBD_NORMAL_COMPLETION)
204 1.1 augustss return;
205 1.2 augustss if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
206 1.2 augustss hid_input, &sc->sc_loc_x, &flags)) {
207 1.1 augustss printf("%s: mouse has no X report\n", sc->sc_dev.dv_xname);
208 1.1 augustss return;
209 1.1 augustss }
210 1.2 augustss if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS)
211 1.2 augustss printf("%s: sorry, X report 0x%04x not supported yet\n",
212 1.2 augustss sc->sc_dev.dv_xname, flags);
213 1.10 augustss
214 1.2 augustss if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
215 1.2 augustss hid_input, &sc->sc_loc_y, &flags)) {
216 1.1 augustss printf("%s: mouse has no Y report\n", sc->sc_dev.dv_xname);
217 1.1 augustss return;
218 1.1 augustss }
219 1.10 augustss if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS)
220 1.10 augustss printf("%s: sorry, Y report 0x%04x not supported yet\n",
221 1.10 augustss sc->sc_dev.dv_xname, flags);
222 1.10 augustss
223 1.6 augustss if (hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Z),
224 1.6 augustss hid_input, &sc->sc_loc_z, &flags) ||
225 1.6 augustss hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_WHEEL),
226 1.6 augustss hid_input, &sc->sc_loc_z, &flags)) {
227 1.6 augustss if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS)
228 1.6 augustss /* Bad Z coord, ignore it */
229 1.6 augustss sc->sc_loc_z.size = 0;
230 1.6 augustss }
231 1.6 augustss
232 1.4 augustss hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, 1), /* left */
233 1.2 augustss hid_input, &sc->sc_loc_btn1, 0);
234 1.4 augustss hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, 2), /* right */
235 1.2 augustss hid_input, &sc->sc_loc_btn2, 0);
236 1.4 augustss hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, 3), /* middle */
237 1.2 augustss hid_input, &sc->sc_loc_btn3, 0);
238 1.1 augustss DPRINTF(("ums_attach: sc=%p\n", sc));
239 1.1 augustss DPRINTF(("ums_attach: X %d/%d\n",
240 1.1 augustss sc->sc_loc_x.pos, sc->sc_loc_x.size));
241 1.1 augustss DPRINTF(("ums_attach: Y %d/%d\n",
242 1.1 augustss sc->sc_loc_x.pos, sc->sc_loc_x.size));
243 1.6 augustss DPRINTF(("ums_attach: Z %d/%d\n",
244 1.6 augustss sc->sc_loc_z.pos, sc->sc_loc_z.size));
245 1.1 augustss DPRINTF(("ums_attach: B1 %d/%d\n",
246 1.1 augustss sc->sc_loc_btn1.pos,sc->sc_loc_btn1.size));
247 1.1 augustss DPRINTF(("ums_attach: B2 %d/%d\n",
248 1.1 augustss sc->sc_loc_btn2.pos,sc->sc_loc_btn2.size));
249 1.1 augustss DPRINTF(("ums_attach: B3 %d/%d\n",
250 1.1 augustss sc->sc_loc_btn3.pos,sc->sc_loc_btn3.size));
251 1.1 augustss
252 1.1 augustss sc->sc_isize = hid_report_size(desc, size, hid_input, &sc->sc_iid);
253 1.1 augustss DPRINTF(("ums_attach: size=%d, id=%d\n", sc->sc_isize, sc->sc_iid));
254 1.1 augustss sc->sc_ibuf = malloc(sc->sc_isize, M_USB, M_NOWAIT);
255 1.1 augustss if (!sc->sc_ibuf)
256 1.1 augustss return;
257 1.1 augustss
258 1.1 augustss sc->sc_ep_addr = ed->bEndpointAddress;
259 1.1 augustss sc->sc_disconnected = 0;
260 1.1 augustss free(desc, M_TEMP);
261 1.3 augustss
262 1.3 augustss a.accessops = &ums_accessops;
263 1.3 augustss a.accesscookie = sc;
264 1.3 augustss
265 1.3 augustss sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
266 1.1 augustss }
267 1.1 augustss
268 1.1 augustss void
269 1.1 augustss ums_disco(p)
270 1.1 augustss void *p;
271 1.1 augustss {
272 1.1 augustss struct ums_softc *sc = p;
273 1.8 augustss
274 1.8 augustss DPRINTF(("ums_disco: sc=%p\n", sc));
275 1.8 augustss usbd_abort_pipe(sc->sc_intrpipe);
276 1.1 augustss sc->sc_disconnected = 1;
277 1.1 augustss }
278 1.1 augustss
279 1.1 augustss void
280 1.1 augustss ums_intr(reqh, addr, status)
281 1.1 augustss usbd_request_handle reqh;
282 1.1 augustss usbd_private_handle addr;
283 1.1 augustss usbd_status status;
284 1.1 augustss {
285 1.1 augustss struct ums_softc *sc = addr;
286 1.1 augustss u_char *ibuf;
287 1.6 augustss int dx, dy, dz;
288 1.4 augustss u_char buttons;
289 1.1 augustss
290 1.1 augustss DPRINTFN(5, ("ums_intr: sc=%p status=%d\n", sc, status));
291 1.1 augustss DPRINTFN(5, ("ums_intr: data = %02x %02x %02x\n",
292 1.1 augustss sc->sc_ibuf[0], sc->sc_ibuf[1], sc->sc_ibuf[2]));
293 1.1 augustss
294 1.1 augustss if (status == USBD_CANCELLED)
295 1.1 augustss return;
296 1.1 augustss
297 1.1 augustss if (status != USBD_NORMAL_COMPLETION) {
298 1.1 augustss DPRINTF(("ums_intr: status=%d\n", status));
299 1.7 augustss usbd_clear_endpoint_stall_async(sc->sc_intrpipe);
300 1.1 augustss return;
301 1.1 augustss }
302 1.1 augustss
303 1.1 augustss ibuf = sc->sc_ibuf;
304 1.1 augustss if (sc->sc_iid) {
305 1.1 augustss if (*ibuf++ != sc->sc_iid)
306 1.1 augustss return;
307 1.1 augustss }
308 1.1 augustss dx = hid_get_data(ibuf, &sc->sc_loc_x);
309 1.1 augustss dy = -hid_get_data(ibuf, &sc->sc_loc_y);
310 1.6 augustss dz = hid_get_data(ibuf, &sc->sc_loc_z);
311 1.1 augustss buttons = 0;
312 1.4 augustss if (hid_get_data(ibuf, &sc->sc_loc_btn1)) buttons |= 1 << 0;
313 1.4 augustss if (hid_get_data(ibuf, &sc->sc_loc_btn2)) buttons |= 1 << 2;
314 1.4 augustss if (hid_get_data(ibuf, &sc->sc_loc_btn3)) buttons |= 1 << 1;
315 1.4 augustss
316 1.4 augustss if (dx || dy || buttons != sc->sc_buttons) {
317 1.4 augustss sc->sc_buttons = buttons;
318 1.4 augustss if (sc->sc_wsmousedev)
319 1.6 augustss wsmouse_input(sc->sc_wsmousedev, buttons, dx, dy, dz);
320 1.1 augustss }
321 1.1 augustss }
322 1.3 augustss
323 1.3 augustss /* wscons routines */
324 1.3 augustss int
325 1.3 augustss ums_enable(v)
326 1.3 augustss void *v;
327 1.3 augustss {
328 1.3 augustss struct ums_softc *sc = v;
329 1.3 augustss usbd_status r;
330 1.3 augustss
331 1.3 augustss if (sc->sc_enabled)
332 1.3 augustss return EBUSY;
333 1.3 augustss
334 1.3 augustss sc->sc_enabled = 1;
335 1.4 augustss sc->sc_buttons = 0;
336 1.3 augustss
337 1.3 augustss /* Set up interrupt pipe. */
338 1.3 augustss r = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
339 1.3 augustss USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc,
340 1.3 augustss sc->sc_ibuf, sc->sc_isize, ums_intr);
341 1.3 augustss if (r != USBD_NORMAL_COMPLETION) {
342 1.3 augustss DPRINTF(("ums_enable: usbd_open_pipe_intr failed, error=%d\n",
343 1.3 augustss r));
344 1.3 augustss sc->sc_enabled = 0;
345 1.3 augustss return (EIO);
346 1.3 augustss }
347 1.3 augustss usbd_set_disco(sc->sc_intrpipe, ums_disco, sc);
348 1.3 augustss return (0);
349 1.3 augustss }
350 1.3 augustss
351 1.3 augustss void
352 1.3 augustss ums_disable(v)
353 1.3 augustss void *v;
354 1.3 augustss {
355 1.3 augustss struct ums_softc *sc = v;
356 1.3 augustss
357 1.3 augustss /* Disable interrupts. */
358 1.3 augustss usbd_abort_pipe(sc->sc_intrpipe);
359 1.3 augustss usbd_close_pipe(sc->sc_intrpipe);
360 1.3 augustss
361 1.3 augustss sc->sc_enabled = 0;
362 1.3 augustss }
363 1.3 augustss
364 1.3 augustss int
365 1.3 augustss ums_ioctl(v, cmd, data, flag, p)
366 1.3 augustss void *v;
367 1.3 augustss u_long cmd;
368 1.3 augustss caddr_t data;
369 1.3 augustss int flag;
370 1.3 augustss struct proc *p;
371 1.3 augustss {
372 1.3 augustss #if 0
373 1.3 augustss struct ums_softc *sc = v;
374 1.3 augustss #endif
375 1.3 augustss
376 1.3 augustss switch (cmd) {
377 1.3 augustss case WSMOUSEIO_GTYPE:
378 1.3 augustss *(u_int *)data = WSMOUSE_TYPE_PS2;
379 1.3 augustss return (0);
380 1.3 augustss }
381 1.3 augustss return (-1);
382 1.3 augustss }
383 1.3 augustss
384