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