ums.c revision 1.56 1 1.56 atatat /* $NetBSD: ums.c,v 1.56 2002/03/17 19:41:05 atatat 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.43 augustss * by Lennart Augustsson (lennart (at) augustsson.net) 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.20 augustss */
39 1.20 augustss
40 1.20 augustss /*
41 1.46 augustss * HID spec: http://www.usb.org/developers/data/devclass/hid1_1.pdf
42 1.1 augustss */
43 1.52 lukem
44 1.52 lukem #include <sys/cdefs.h>
45 1.56 atatat __KERNEL_RCSID(0, "$NetBSD: ums.c,v 1.56 2002/03/17 19:41:05 atatat Exp $");
46 1.1 augustss
47 1.1 augustss #include <sys/param.h>
48 1.1 augustss #include <sys/systm.h>
49 1.1 augustss #include <sys/kernel.h>
50 1.1 augustss #include <sys/malloc.h>
51 1.1 augustss #include <sys/device.h>
52 1.1 augustss #include <sys/ioctl.h>
53 1.1 augustss #include <sys/tty.h>
54 1.1 augustss #include <sys/file.h>
55 1.1 augustss #include <sys/select.h>
56 1.1 augustss #include <sys/proc.h>
57 1.1 augustss #include <sys/vnode.h>
58 1.1 augustss #include <sys/poll.h>
59 1.1 augustss
60 1.1 augustss #include <dev/usb/usb.h>
61 1.1 augustss #include <dev/usb/usbhid.h>
62 1.1 augustss
63 1.1 augustss #include <dev/usb/usbdi.h>
64 1.1 augustss #include <dev/usb/usbdi_util.h>
65 1.1 augustss #include <dev/usb/usbdevs.h>
66 1.1 augustss #include <dev/usb/usb_quirks.h>
67 1.53 augustss #include <dev/usb/uhidev.h>
68 1.1 augustss #include <dev/usb/hid.h>
69 1.1 augustss
70 1.3 augustss #include <dev/wscons/wsconsio.h>
71 1.3 augustss #include <dev/wscons/wsmousevar.h>
72 1.3 augustss
73 1.1 augustss #ifdef USB_DEBUG
74 1.26 augustss #define DPRINTF(x) if (umsdebug) logprintf x
75 1.26 augustss #define DPRINTFN(n,x) if (umsdebug>(n)) logprintf x
76 1.1 augustss int umsdebug = 0;
77 1.1 augustss #else
78 1.1 augustss #define DPRINTF(x)
79 1.1 augustss #define DPRINTFN(n,x)
80 1.1 augustss #endif
81 1.1 augustss
82 1.27 augustss #define UMS_BUT(i) ((i) == 1 || (i) == 2 ? 3 - (i) : i)
83 1.27 augustss
84 1.27 augustss #define UMSUNIT(s) (minor(s))
85 1.16 augustss
86 1.16 augustss #define PS2LBUTMASK x01
87 1.16 augustss #define PS2RBUTMASK x02
88 1.16 augustss #define PS2MBUTMASK x04
89 1.1 augustss #define PS2BUTMASK 0x0f
90 1.1 augustss
91 1.53 augustss #define MAX_BUTTONS 7 /* must not exceed size of sc_buttons */
92 1.53 augustss
93 1.1 augustss struct ums_softc {
94 1.53 augustss struct uhidev sc_hdev;
95 1.53 augustss
96 1.16 augustss struct hid_location sc_loc_x, sc_loc_y, sc_loc_z;
97 1.53 augustss struct hid_location sc_loc_btn[MAX_BUTTONS];
98 1.1 augustss
99 1.16 augustss int sc_enabled;
100 1.3 augustss
101 1.16 augustss int flags; /* device configuration */
102 1.16 augustss #define UMS_Z 0x01 /* z direction available */
103 1.35 augustss #define UMS_SPUR_BUT_UP 0x02 /* spurious button up events */
104 1.35 augustss #define UMS_REVZ 0x04 /* Z-axis is reversed */
105 1.35 augustss
106 1.16 augustss int nbuttons;
107 1.16 augustss
108 1.27 augustss u_int32_t sc_buttons; /* mouse button status */
109 1.3 augustss struct device *sc_wsmousedev;
110 1.28 augustss
111 1.28 augustss char sc_dying;
112 1.1 augustss };
113 1.1 augustss
114 1.2 augustss #define MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
115 1.2 augustss #define MOUSE_FLAGS (HIO_RELATIVE)
116 1.2 augustss
117 1.53 augustss Static void ums_intr(struct uhidev *addr, void *ibuf, u_int len);
118 1.1 augustss
119 1.44 augustss Static int ums_enable(void *);
120 1.44 augustss Static void ums_disable(void *);
121 1.55 augustss Static int ums_ioctl(void *, u_long, caddr_t, int, usb_proc_ptr );
122 1.3 augustss
123 1.3 augustss const struct wsmouse_accessops ums_accessops = {
124 1.3 augustss ums_enable,
125 1.3 augustss ums_ioctl,
126 1.3 augustss ums_disable,
127 1.3 augustss };
128 1.3 augustss
129 1.16 augustss USB_DECLARE_DRIVER(ums);
130 1.1 augustss
131 1.53 augustss int
132 1.53 augustss ums_match(struct device *parent, struct cfdata *match, void *aux)
133 1.1 augustss {
134 1.53 augustss struct uhidev_attach_arg *uha = aux;
135 1.53 augustss int size;
136 1.1 augustss void *desc;
137 1.1 augustss
138 1.53 augustss uhidev_get_report_desc(uha->parent, &desc, &size);
139 1.53 augustss if (!hid_is_collection(desc, size, uha->reportid,
140 1.53 augustss HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE)))
141 1.1 augustss return (UMATCH_NONE);
142 1.1 augustss
143 1.53 augustss return (UMATCH_IFACECLASS);
144 1.1 augustss }
145 1.1 augustss
146 1.53 augustss void
147 1.53 augustss ums_attach(struct device *parent, struct device *self, void *aux)
148 1.1 augustss {
149 1.53 augustss struct ums_softc *sc = (struct ums_softc *)self;
150 1.53 augustss struct uhidev_attach_arg *uha = aux;
151 1.3 augustss struct wsmousedev_attach_args a;
152 1.1 augustss int size;
153 1.1 augustss void *desc;
154 1.35 augustss u_int32_t flags, quirks;
155 1.37 augustss int i, wheel;
156 1.16 augustss struct hid_location loc_btn;
157 1.1 augustss
158 1.53 augustss sc->sc_hdev.sc_intr = ums_intr;
159 1.53 augustss sc->sc_hdev.sc_parent = uha->parent;
160 1.53 augustss sc->sc_hdev.sc_report_id = uha->reportid;
161 1.1 augustss
162 1.53 augustss quirks = usbd_get_quirks(uha->parent->sc_udev)->uq_flags;
163 1.35 augustss if (quirks & UQ_MS_REVZ)
164 1.35 augustss sc->flags |= UMS_REVZ;
165 1.35 augustss if (quirks & UQ_SPUR_BUT_UP)
166 1.35 augustss sc->flags |= UMS_SPUR_BUT_UP;
167 1.24 augustss
168 1.53 augustss uhidev_get_report_desc(uha->parent, &desc, &size);
169 1.16 augustss
170 1.2 augustss if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
171 1.53 augustss uha->reportid, hid_input, &sc->sc_loc_x, &flags)) {
172 1.53 augustss printf("\n%s: mouse has no X report\n",
173 1.53 augustss USBDEVNAME(sc->sc_hdev.sc_dev));
174 1.16 augustss USB_ATTACH_ERROR_RETURN;
175 1.16 augustss }
176 1.16 augustss if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
177 1.53 augustss printf("\n%s: X report 0x%04x not supported\n",
178 1.53 augustss USBDEVNAME(sc->sc_hdev.sc_dev), flags);
179 1.16 augustss USB_ATTACH_ERROR_RETURN;
180 1.1 augustss }
181 1.10 augustss
182 1.2 augustss if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
183 1.53 augustss uha->reportid, hid_input, &sc->sc_loc_y, &flags)) {
184 1.53 augustss printf("\n%s: mouse has no Y report\n",
185 1.53 augustss USBDEVNAME(sc->sc_hdev.sc_dev));
186 1.16 augustss USB_ATTACH_ERROR_RETURN;
187 1.16 augustss }
188 1.16 augustss if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
189 1.53 augustss printf("\n%s: Y report 0x%04x not supported\n",
190 1.53 augustss USBDEVNAME(sc->sc_hdev.sc_dev), flags);
191 1.16 augustss USB_ATTACH_ERROR_RETURN;
192 1.1 augustss }
193 1.10 augustss
194 1.22 augustss /* Try to guess the Z activator: first check Z, then WHEEL. */
195 1.37 augustss wheel = 0;
196 1.6 augustss if (hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Z),
197 1.53 augustss uha->reportid, hid_input, &sc->sc_loc_z, &flags) ||
198 1.37 augustss (wheel = hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP,
199 1.37 augustss HUG_WHEEL),
200 1.53 augustss uha->reportid, hid_input, &sc->sc_loc_z, &flags))) {
201 1.16 augustss if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
202 1.16 augustss sc->sc_loc_z.size = 0; /* Bad Z coord, ignore it */
203 1.16 augustss } else {
204 1.16 augustss sc->flags |= UMS_Z;
205 1.37 augustss /* Wheels need the Z axis reversed. */
206 1.37 augustss if (wheel)
207 1.37 augustss sc->flags ^= UMS_REVZ;
208 1.16 augustss }
209 1.6 augustss }
210 1.6 augustss
211 1.21 augustss /* figure out the number of buttons */
212 1.21 augustss for (i = 1; i <= MAX_BUTTONS; i++)
213 1.16 augustss if (!hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),
214 1.53 augustss uha->reportid, hid_input, &loc_btn, 0))
215 1.16 augustss break;
216 1.16 augustss sc->nbuttons = i - 1;
217 1.16 augustss
218 1.53 augustss printf(": %d button%s%s\n",
219 1.45 augustss sc->nbuttons, sc->nbuttons == 1 ? "" : "s",
220 1.45 augustss sc->flags & UMS_Z ? " and Z dir." : "");
221 1.16 augustss
222 1.16 augustss for (i = 1; i <= sc->nbuttons; i++)
223 1.16 augustss hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),
224 1.53 augustss uha->reportid, hid_input,
225 1.53 augustss &sc->sc_loc_btn[i-1], 0);
226 1.3 augustss
227 1.16 augustss #ifdef USB_DEBUG
228 1.16 augustss DPRINTF(("ums_attach: sc=%p\n", sc));
229 1.16 augustss DPRINTF(("ums_attach: X\t%d/%d\n",
230 1.16 augustss sc->sc_loc_x.pos, sc->sc_loc_x.size));
231 1.16 augustss DPRINTF(("ums_attach: Y\t%d/%d\n",
232 1.48 augustss sc->sc_loc_y.pos, sc->sc_loc_y.size));
233 1.16 augustss if (sc->flags & UMS_Z)
234 1.16 augustss DPRINTF(("ums_attach: Z\t%d/%d\n",
235 1.16 augustss sc->sc_loc_z.pos, sc->sc_loc_z.size));
236 1.16 augustss for (i = 1; i <= sc->nbuttons; i++) {
237 1.16 augustss DPRINTF(("ums_attach: B%d\t%d/%d\n",
238 1.16 augustss i, sc->sc_loc_btn[i-1].pos,sc->sc_loc_btn[i-1].size));
239 1.16 augustss }
240 1.16 augustss #endif
241 1.16 augustss
242 1.3 augustss a.accessops = &ums_accessops;
243 1.3 augustss a.accesscookie = sc;
244 1.3 augustss
245 1.47 augustss sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
246 1.40 augustss
247 1.16 augustss USB_ATTACH_SUCCESS_RETURN;
248 1.16 augustss }
249 1.16 augustss
250 1.25 augustss int
251 1.44 augustss ums_activate(device_ptr_t self, enum devact act)
252 1.16 augustss {
253 1.28 augustss struct ums_softc *sc = (struct ums_softc *)self;
254 1.32 augustss int rv = 0;
255 1.28 augustss
256 1.28 augustss switch (act) {
257 1.28 augustss case DVACT_ACTIVATE:
258 1.28 augustss return (EOPNOTSUPP);
259 1.28 augustss break;
260 1.28 augustss
261 1.28 augustss case DVACT_DEACTIVATE:
262 1.34 augustss if (sc->sc_wsmousedev != NULL)
263 1.32 augustss rv = config_deactivate(sc->sc_wsmousedev);
264 1.28 augustss sc->sc_dying = 1;
265 1.28 augustss break;
266 1.28 augustss }
267 1.32 augustss return (rv);
268 1.1 augustss }
269 1.1 augustss
270 1.53 augustss int
271 1.53 augustss ums_detach(struct device *self, int flags)
272 1.1 augustss {
273 1.53 augustss struct ums_softc *sc = (struct ums_softc *)self;
274 1.25 augustss int rv = 0;
275 1.8 augustss
276 1.25 augustss DPRINTF(("ums_detach: sc=%p flags=%d\n", sc, flags));
277 1.33 augustss
278 1.51 augustss /* No need to do reference counting of ums, wsmouse has all the goo. */
279 1.51 augustss if (sc->sc_wsmousedev != NULL)
280 1.51 augustss rv = config_detach(sc->sc_wsmousedev, flags);
281 1.40 augustss
282 1.25 augustss return (rv);
283 1.1 augustss }
284 1.1 augustss
285 1.1 augustss void
286 1.53 augustss ums_intr(struct uhidev *addr, void *ibuf, u_int len)
287 1.1 augustss {
288 1.53 augustss struct ums_softc *sc = (struct ums_softc *)addr;
289 1.6 augustss int dx, dy, dz;
290 1.27 augustss u_int32_t buttons = 0;
291 1.16 augustss int i;
292 1.23 augustss int s;
293 1.54 augustss
294 1.54 augustss DPRINTFN(5,("ums_intr: len=%d\n", len));
295 1.21 augustss
296 1.1 augustss dx = hid_get_data(ibuf, &sc->sc_loc_x);
297 1.1 augustss dy = -hid_get_data(ibuf, &sc->sc_loc_y);
298 1.6 augustss dz = hid_get_data(ibuf, &sc->sc_loc_z);
299 1.35 augustss if (sc->flags & UMS_REVZ)
300 1.24 augustss dz = -dz;
301 1.16 augustss for (i = 0; i < sc->nbuttons; i++)
302 1.16 augustss if (hid_get_data(ibuf, &sc->sc_loc_btn[i]))
303 1.16 augustss buttons |= (1 << UMS_BUT(i));
304 1.4 augustss
305 1.35 augustss if (dx != 0 || dy != 0 || dz != 0 || buttons != sc->sc_buttons) {
306 1.16 augustss DPRINTFN(10, ("ums_intr: x:%d y:%d z:%d buttons:0x%x\n",
307 1.16 augustss dx, dy, dz, buttons));
308 1.4 augustss sc->sc_buttons = buttons;
309 1.35 augustss if (sc->sc_wsmousedev != NULL) {
310 1.23 augustss s = spltty();
311 1.38 takemura wsmouse_input(sc->sc_wsmousedev, buttons, dx, dy, dz,
312 1.38 takemura WSMOUSE_INPUT_DELTA);
313 1.23 augustss splx(s);
314 1.23 augustss }
315 1.1 augustss }
316 1.1 augustss }
317 1.3 augustss
318 1.42 augustss Static int
319 1.44 augustss ums_enable(void *v)
320 1.3 augustss {
321 1.3 augustss struct ums_softc *sc = v;
322 1.16 augustss
323 1.25 augustss DPRINTFN(1,("ums_enable: sc=%p\n", sc));
324 1.28 augustss
325 1.28 augustss if (sc->sc_dying)
326 1.28 augustss return (EIO);
327 1.28 augustss
328 1.3 augustss if (sc->sc_enabled)
329 1.28 augustss return (EBUSY);
330 1.3 augustss
331 1.3 augustss sc->sc_enabled = 1;
332 1.4 augustss sc->sc_buttons = 0;
333 1.3 augustss
334 1.53 augustss return (uhidev_open(&sc->sc_hdev));
335 1.3 augustss }
336 1.3 augustss
337 1.42 augustss Static void
338 1.44 augustss ums_disable(void *v)
339 1.3 augustss {
340 1.3 augustss struct ums_softc *sc = v;
341 1.25 augustss
342 1.25 augustss DPRINTFN(1,("ums_disable: sc=%p\n", sc));
343 1.25 augustss #ifdef DIAGNOSTIC
344 1.25 augustss if (!sc->sc_enabled) {
345 1.25 augustss printf("ums_disable: not enabled\n");
346 1.25 augustss return;
347 1.25 augustss }
348 1.25 augustss #endif
349 1.3 augustss
350 1.3 augustss sc->sc_enabled = 0;
351 1.53 augustss return (uhidev_close(&sc->sc_hdev));
352 1.3 augustss }
353 1.3 augustss
354 1.42 augustss Static int
355 1.55 augustss ums_ioctl(void *v, u_long cmd, caddr_t data, int flag, usb_proc_ptr p)
356 1.16 augustss
357 1.3 augustss {
358 1.3 augustss switch (cmd) {
359 1.3 augustss case WSMOUSEIO_GTYPE:
360 1.17 augustss *(u_int *)data = WSMOUSE_TYPE_USB;
361 1.3 augustss return (0);
362 1.3 augustss }
363 1.16 augustss
364 1.56 atatat return (EPASSTHROUGH);
365 1.3 augustss }
366