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