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