uhid.c revision 1.26.2.2 1 1.26.2.2 bouyer /* $NetBSD: uhid.c,v 1.26.2.2 2001/01/05 17:36:32 bouyer Exp $ */
2 1.26.2.1 bouyer /* $FreeBSD: src/sys/dev/usb/uhid.c,v 1.22 1999/11/17 22:33:43 n_hibma Exp $ */
3 1.1 augustss
4 1.1 augustss /*
5 1.1 augustss * Copyright (c) 1998 The NetBSD Foundation, Inc.
6 1.1 augustss * All rights reserved.
7 1.1 augustss *
8 1.6 augustss * This code is derived from software contributed to The NetBSD Foundation
9 1.26.2.1 bouyer * by Lennart Augustsson (lennart (at) augustsson.net) at
10 1.6 augustss * Carlstedt Research & Technology.
11 1.1 augustss *
12 1.1 augustss * Redistribution and use in source and binary forms, with or without
13 1.1 augustss * modification, are permitted provided that the following conditions
14 1.1 augustss * are met:
15 1.1 augustss * 1. Redistributions of source code must retain the above copyright
16 1.1 augustss * notice, this list of conditions and the following disclaimer.
17 1.1 augustss * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 augustss * notice, this list of conditions and the following disclaimer in the
19 1.1 augustss * documentation and/or other materials provided with the distribution.
20 1.1 augustss * 3. All advertising materials mentioning features or use of this software
21 1.1 augustss * must display the following acknowledgement:
22 1.1 augustss * This product includes software developed by the NetBSD
23 1.1 augustss * Foundation, Inc. and its contributors.
24 1.1 augustss * 4. Neither the name of The NetBSD Foundation nor the names of its
25 1.1 augustss * contributors may be used to endorse or promote products derived
26 1.1 augustss * from this software without specific prior written permission.
27 1.1 augustss *
28 1.1 augustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 1.1 augustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 1.1 augustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 1.1 augustss * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 1.1 augustss * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 1.1 augustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 1.1 augustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 1.1 augustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 1.1 augustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 1.1 augustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 1.1 augustss * POSSIBILITY OF SUCH DAMAGE.
39 1.1 augustss */
40 1.1 augustss
41 1.15 augustss /*
42 1.26.2.2 bouyer * HID spec: http://www.usb.org/developers/data/devclass/hid1_1.pdf
43 1.15 augustss */
44 1.1 augustss
45 1.1 augustss #include <sys/param.h>
46 1.1 augustss #include <sys/systm.h>
47 1.1 augustss #include <sys/kernel.h>
48 1.1 augustss #include <sys/malloc.h>
49 1.26.2.1 bouyer #include <sys/signalvar.h>
50 1.20 augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
51 1.14 augustss #include <sys/device.h>
52 1.12 augustss #include <sys/ioctl.h>
53 1.12 augustss #elif defined(__FreeBSD__)
54 1.12 augustss #include <sys/ioccom.h>
55 1.12 augustss #include <sys/filio.h>
56 1.12 augustss #include <sys/module.h>
57 1.12 augustss #include <sys/bus.h>
58 1.14 augustss #include <sys/ioccom.h>
59 1.12 augustss #endif
60 1.18 augustss #include <sys/conf.h>
61 1.1 augustss #include <sys/tty.h>
62 1.1 augustss #include <sys/file.h>
63 1.1 augustss #include <sys/select.h>
64 1.1 augustss #include <sys/proc.h>
65 1.1 augustss #include <sys/vnode.h>
66 1.1 augustss #include <sys/poll.h>
67 1.1 augustss
68 1.1 augustss #include <dev/usb/usb.h>
69 1.1 augustss #include <dev/usb/usbhid.h>
70 1.1 augustss
71 1.26.2.2 bouyer #include <dev/usb/usbdevs.h>
72 1.1 augustss #include <dev/usb/usbdi.h>
73 1.1 augustss #include <dev/usb/usbdi_util.h>
74 1.1 augustss #include <dev/usb/hid.h>
75 1.1 augustss #include <dev/usb/usb_quirks.h>
76 1.1 augustss
77 1.26.2.2 bouyer /* Report descriptor for broken Wacom Graphire */
78 1.26.2.2 bouyer #include <dev/usb/ugraphire_rdesc.h>
79 1.26.2.2 bouyer
80 1.26 augustss #ifdef UHID_DEBUG
81 1.19 augustss #define DPRINTF(x) if (uhiddebug) logprintf x
82 1.19 augustss #define DPRINTFN(n,x) if (uhiddebug>(n)) logprintf x
83 1.1 augustss int uhiddebug = 0;
84 1.1 augustss #else
85 1.1 augustss #define DPRINTF(x)
86 1.1 augustss #define DPRINTFN(n,x)
87 1.1 augustss #endif
88 1.1 augustss
89 1.1 augustss struct uhid_softc {
90 1.24 augustss USBBASEDEVICE sc_dev; /* base device */
91 1.26.2.1 bouyer usbd_device_handle sc_udev;
92 1.1 augustss usbd_interface_handle sc_iface; /* interface */
93 1.1 augustss usbd_pipe_handle sc_intrpipe; /* interrupt pipe */
94 1.1 augustss int sc_ep_addr;
95 1.1 augustss
96 1.1 augustss int sc_isize;
97 1.1 augustss int sc_osize;
98 1.2 augustss int sc_fsize;
99 1.1 augustss u_int8_t sc_iid;
100 1.1 augustss u_int8_t sc_oid;
101 1.2 augustss u_int8_t sc_fid;
102 1.1 augustss
103 1.26.2.1 bouyer u_char *sc_ibuf;
104 1.26.2.1 bouyer u_char *sc_obuf;
105 1.1 augustss
106 1.1 augustss void *sc_repdesc;
107 1.1 augustss int sc_repdesc_size;
108 1.1 augustss
109 1.1 augustss struct clist sc_q;
110 1.1 augustss struct selinfo sc_rsel;
111 1.26.2.1 bouyer struct proc *sc_async; /* process that wants SIGIO */
112 1.1 augustss u_char sc_state; /* driver state */
113 1.1 augustss #define UHID_OPEN 0x01 /* device is open */
114 1.5 augustss #define UHID_ASLP 0x02 /* waiting for device data */
115 1.1 augustss #define UHID_NEEDCLEAR 0x04 /* needs clearing endpoint stall */
116 1.2 augustss #define UHID_IMMED 0x08 /* return read data immediately */
117 1.18 augustss
118 1.18 augustss int sc_refcnt;
119 1.18 augustss u_char sc_dying;
120 1.1 augustss };
121 1.1 augustss
122 1.1 augustss #define UHIDUNIT(dev) (minor(dev))
123 1.1 augustss #define UHID_CHUNK 128 /* chunk size for read */
124 1.1 augustss #define UHID_BSIZE 1020 /* buffer size */
125 1.1 augustss
126 1.20 augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
127 1.19 augustss cdev_decl(uhid);
128 1.19 augustss #elif defined(__FreeBSD__)
129 1.19 augustss d_open_t uhidopen;
130 1.19 augustss d_close_t uhidclose;
131 1.19 augustss d_read_t uhidread;
132 1.19 augustss d_write_t uhidwrite;
133 1.19 augustss d_ioctl_t uhidioctl;
134 1.19 augustss d_poll_t uhidpoll;
135 1.19 augustss
136 1.19 augustss #define UHID_CDEV_MAJOR 122
137 1.19 augustss
138 1.26.2.1 bouyer Static struct cdevsw uhid_cdevsw = {
139 1.19 augustss /* open */ uhidopen,
140 1.19 augustss /* close */ uhidclose,
141 1.19 augustss /* read */ uhidread,
142 1.19 augustss /* write */ uhidwrite,
143 1.19 augustss /* ioctl */ uhidioctl,
144 1.19 augustss /* poll */ uhidpoll,
145 1.19 augustss /* mmap */ nommap,
146 1.19 augustss /* strategy */ nostrategy,
147 1.19 augustss /* name */ "uhid",
148 1.19 augustss /* maj */ UHID_CDEV_MAJOR,
149 1.19 augustss /* dump */ nodump,
150 1.19 augustss /* psize */ nopsize,
151 1.19 augustss /* flags */ 0,
152 1.19 augustss /* bmaj */ -1
153 1.19 augustss };
154 1.19 augustss #endif
155 1.19 augustss
156 1.26.2.1 bouyer Static void uhid_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
157 1.18 augustss
158 1.26.2.1 bouyer Static int uhid_do_read(struct uhid_softc *, struct uio *uio, int);
159 1.26.2.1 bouyer Static int uhid_do_write(struct uhid_softc *, struct uio *uio, int);
160 1.26.2.1 bouyer Static int uhid_do_ioctl(struct uhid_softc*, u_long, caddr_t, int,struct proc*);
161 1.1 augustss
162 1.12 augustss USB_DECLARE_DRIVER(uhid);
163 1.1 augustss
164 1.12 augustss USB_MATCH(uhid)
165 1.1 augustss {
166 1.12 augustss USB_MATCH_START(uhid, uaa);
167 1.1 augustss usb_interface_descriptor_t *id;
168 1.1 augustss
169 1.26.2.1 bouyer if (uaa->iface == NULL)
170 1.1 augustss return (UMATCH_NONE);
171 1.1 augustss id = usbd_get_interface_descriptor(uaa->iface);
172 1.26.2.1 bouyer if (id == NULL || id->bInterfaceClass != UICLASS_HID)
173 1.1 augustss return (UMATCH_NONE);
174 1.1 augustss return (UMATCH_IFACECLASS_GENERIC);
175 1.1 augustss }
176 1.1 augustss
177 1.12 augustss USB_ATTACH(uhid)
178 1.1 augustss {
179 1.12 augustss USB_ATTACH_START(uhid, sc, uaa);
180 1.1 augustss usbd_interface_handle iface = uaa->iface;
181 1.1 augustss usb_interface_descriptor_t *id;
182 1.1 augustss usb_endpoint_descriptor_t *ed;
183 1.1 augustss int size;
184 1.1 augustss void *desc;
185 1.26.2.1 bouyer usbd_status err;
186 1.1 augustss char devinfo[1024];
187 1.1 augustss
188 1.26.2.1 bouyer sc->sc_udev = uaa->device;
189 1.1 augustss sc->sc_iface = iface;
190 1.1 augustss id = usbd_get_interface_descriptor(iface);
191 1.1 augustss usbd_devinfo(uaa->device, 0, devinfo);
192 1.12 augustss USB_ATTACH_SETUP;
193 1.12 augustss printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
194 1.7 augustss devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
195 1.12 augustss
196 1.1 augustss ed = usbd_interface2endpoint_descriptor(iface, 0);
197 1.26.2.1 bouyer if (ed == NULL) {
198 1.1 augustss printf("%s: could not read endpoint descriptor\n",
199 1.12 augustss USBDEVNAME(sc->sc_dev));
200 1.18 augustss sc->sc_dying = 1;
201 1.12 augustss USB_ATTACH_ERROR_RETURN;
202 1.1 augustss }
203 1.1 augustss
204 1.11 augustss DPRINTFN(10,("uhid_attach: bLength=%d bDescriptorType=%d "
205 1.11 augustss "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
206 1.11 augustss " bInterval=%d\n",
207 1.11 augustss ed->bLength, ed->bDescriptorType,
208 1.11 augustss ed->bEndpointAddress & UE_ADDR,
209 1.23 augustss UE_GET_DIR(ed->bEndpointAddress)==UE_DIR_IN? "in" : "out",
210 1.11 augustss ed->bmAttributes & UE_XFERTYPE,
211 1.11 augustss UGETW(ed->wMaxPacketSize), ed->bInterval));
212 1.1 augustss
213 1.23 augustss if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN ||
214 1.1 augustss (ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
215 1.12 augustss printf("%s: unexpected endpoint\n", USBDEVNAME(sc->sc_dev));
216 1.18 augustss sc->sc_dying = 1;
217 1.12 augustss USB_ATTACH_ERROR_RETURN;
218 1.1 augustss }
219 1.1 augustss
220 1.1 augustss sc->sc_ep_addr = ed->bEndpointAddress;
221 1.1 augustss
222 1.26.2.2 bouyer if (uaa->vendor == USB_VENDOR_WACOM &&
223 1.26.2.2 bouyer uaa->product == USB_PRODUCT_WACOM_GRAPHIRE /* &&
224 1.26.2.2 bouyer uaa->revision == 0x???? */) { /* XXX should use revision */
225 1.26.2.2 bouyer /* The report descriptor for the Wacom Graphire is broken. */
226 1.26.2.2 bouyer size = sizeof uhid_graphire_report_descr;
227 1.26.2.2 bouyer desc = malloc(size, M_USBDEV, M_NOWAIT);
228 1.26.2.2 bouyer if (desc == NULL)
229 1.26.2.2 bouyer err = USBD_NOMEM;
230 1.26.2.2 bouyer else {
231 1.26.2.2 bouyer err = USBD_NORMAL_COMPLETION;
232 1.26.2.2 bouyer memcpy(desc, uhid_graphire_report_descr, size);
233 1.26.2.2 bouyer }
234 1.26.2.2 bouyer } else {
235 1.26.2.2 bouyer desc = NULL;
236 1.26.2.2 bouyer err = usbd_alloc_report_desc(uaa->iface, &desc, &size,M_USBDEV);
237 1.26.2.2 bouyer }
238 1.26.2.1 bouyer if (err) {
239 1.12 augustss printf("%s: no report descriptor\n", USBDEVNAME(sc->sc_dev));
240 1.18 augustss sc->sc_dying = 1;
241 1.12 augustss USB_ATTACH_ERROR_RETURN;
242 1.1 augustss }
243 1.1 augustss
244 1.1 augustss (void)usbd_set_idle(iface, 0, 0);
245 1.1 augustss
246 1.2 augustss sc->sc_isize = hid_report_size(desc, size, hid_input, &sc->sc_iid);
247 1.2 augustss sc->sc_osize = hid_report_size(desc, size, hid_output, &sc->sc_oid);
248 1.2 augustss sc->sc_fsize = hid_report_size(desc, size, hid_feature, &sc->sc_fid);
249 1.1 augustss
250 1.1 augustss sc->sc_repdesc = desc;
251 1.1 augustss sc->sc_repdesc_size = size;
252 1.12 augustss
253 1.26.2.1 bouyer #ifdef __FreeBSD__
254 1.26.2.1 bouyer {
255 1.26.2.1 bouyer static int global_init_done = 0;
256 1.26.2.1 bouyer
257 1.26.2.1 bouyer if (!global_init_done) {
258 1.26.2.1 bouyer cdevsw_add(&uhid_cdevsw);
259 1.26.2.1 bouyer global_init_done = 1;
260 1.26.2.1 bouyer }
261 1.26.2.1 bouyer }
262 1.26.2.1 bouyer #endif
263 1.26.2.1 bouyer
264 1.26.2.1 bouyer usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
265 1.26.2.1 bouyer USBDEV(sc->sc_dev));
266 1.26.2.1 bouyer
267 1.12 augustss USB_ATTACH_SUCCESS_RETURN;
268 1.1 augustss }
269 1.1 augustss
270 1.26 augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
271 1.18 augustss int
272 1.26.2.1 bouyer uhid_activate(device_ptr_t self, enum devact act)
273 1.18 augustss {
274 1.21 augustss struct uhid_softc *sc = (struct uhid_softc *)self;
275 1.21 augustss
276 1.21 augustss switch (act) {
277 1.21 augustss case DVACT_ACTIVATE:
278 1.21 augustss return (EOPNOTSUPP);
279 1.21 augustss break;
280 1.21 augustss
281 1.21 augustss case DVACT_DEACTIVATE:
282 1.21 augustss sc->sc_dying = 1;
283 1.21 augustss break;
284 1.21 augustss }
285 1.18 augustss return (0);
286 1.12 augustss }
287 1.26 augustss #endif
288 1.12 augustss
289 1.26 augustss USB_DETACH(uhid)
290 1.1 augustss {
291 1.26 augustss USB_DETACH_START(uhid, sc);
292 1.26 augustss int s;
293 1.26 augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
294 1.18 augustss int maj, mn;
295 1.18 augustss
296 1.18 augustss DPRINTF(("uhid_detach: sc=%p flags=%d\n", sc, flags));
297 1.26 augustss #else
298 1.26 augustss DPRINTF(("uhid_detach: sc=%p\n", sc));
299 1.26 augustss #endif
300 1.3 augustss
301 1.18 augustss sc->sc_dying = 1;
302 1.26.2.1 bouyer if (sc->sc_intrpipe != NULL)
303 1.18 augustss usbd_abort_pipe(sc->sc_intrpipe);
304 1.18 augustss
305 1.18 augustss if (sc->sc_state & UHID_OPEN) {
306 1.18 augustss s = splusb();
307 1.18 augustss if (--sc->sc_refcnt >= 0) {
308 1.18 augustss /* Wake everyone */
309 1.18 augustss wakeup(&sc->sc_q);
310 1.18 augustss /* Wait for processes to go away. */
311 1.24 augustss usb_detach_wait(USBDEV(sc->sc_dev));
312 1.18 augustss }
313 1.18 augustss splx(s);
314 1.18 augustss }
315 1.18 augustss
316 1.26 augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
317 1.18 augustss /* locate the major number */
318 1.18 augustss for (maj = 0; maj < nchrdev; maj++)
319 1.18 augustss if (cdevsw[maj].d_open == uhidopen)
320 1.18 augustss break;
321 1.18 augustss
322 1.18 augustss /* Nuke the vnodes for any open instances (calls close). */
323 1.18 augustss mn = self->dv_unit;
324 1.18 augustss vdevgone(maj, mn, mn, VCHR);
325 1.26 augustss #elif defined(__FreeBSD__)
326 1.26 augustss /* XXX not implemented yet */
327 1.26 augustss #endif
328 1.18 augustss
329 1.26.2.1 bouyer if (sc->sc_repdesc)
330 1.26.2.1 bouyer free(sc->sc_repdesc, M_USBDEV);
331 1.26.2.1 bouyer
332 1.26.2.1 bouyer usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
333 1.26.2.1 bouyer USBDEV(sc->sc_dev));
334 1.18 augustss
335 1.18 augustss return (0);
336 1.1 augustss }
337 1.1 augustss
338 1.1 augustss void
339 1.26.2.1 bouyer uhid_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
340 1.1 augustss {
341 1.1 augustss struct uhid_softc *sc = addr;
342 1.1 augustss
343 1.26.2.1 bouyer #ifdef UHID_DEBUG
344 1.26.2.1 bouyer if (uhiddebug > 5) {
345 1.26.2.1 bouyer u_int32_t cc, i;
346 1.26.2.1 bouyer
347 1.26.2.1 bouyer usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
348 1.26.2.1 bouyer DPRINTF(("uhid_intr: status=%d cc=%d\n", status, cc));
349 1.26.2.1 bouyer DPRINTF(("uhid_intr: data ="));
350 1.26.2.1 bouyer for (i = 0; i < cc; i++)
351 1.26.2.1 bouyer DPRINTF((" %02x", sc->sc_ibuf[i]));
352 1.26.2.1 bouyer DPRINTF(("\n"));
353 1.26.2.1 bouyer }
354 1.26.2.1 bouyer #endif
355 1.1 augustss
356 1.1 augustss if (status == USBD_CANCELLED)
357 1.1 augustss return;
358 1.1 augustss
359 1.1 augustss if (status != USBD_NORMAL_COMPLETION) {
360 1.1 augustss DPRINTF(("uhid_intr: status=%d\n", status));
361 1.1 augustss sc->sc_state |= UHID_NEEDCLEAR;
362 1.1 augustss return;
363 1.1 augustss }
364 1.1 augustss
365 1.1 augustss (void) b_to_q(sc->sc_ibuf, sc->sc_isize, &sc->sc_q);
366 1.1 augustss
367 1.1 augustss if (sc->sc_state & UHID_ASLP) {
368 1.1 augustss sc->sc_state &= ~UHID_ASLP;
369 1.1 augustss DPRINTFN(5, ("uhid_intr: waking %p\n", sc));
370 1.18 augustss wakeup(&sc->sc_q);
371 1.1 augustss }
372 1.1 augustss selwakeup(&sc->sc_rsel);
373 1.26.2.1 bouyer if (sc->sc_async != NULL) {
374 1.26.2.1 bouyer DPRINTFN(3, ("uhid_intr: sending SIGIO %p\n", sc->sc_async));
375 1.26.2.1 bouyer psignal(sc->sc_async, SIGIO);
376 1.26.2.1 bouyer }
377 1.1 augustss }
378 1.1 augustss
379 1.1 augustss int
380 1.26.2.1 bouyer uhidopen(dev_t dev, int flag, int mode, struct proc *p)
381 1.1 augustss {
382 1.25 augustss struct uhid_softc *sc;
383 1.26.2.1 bouyer usbd_status err;
384 1.25 augustss
385 1.12 augustss USB_GET_SC_OPEN(uhid, UHIDUNIT(dev), sc);
386 1.1 augustss
387 1.18 augustss DPRINTF(("uhidopen: sc=%p\n", sc));
388 1.1 augustss
389 1.18 augustss if (sc->sc_dying)
390 1.18 augustss return (ENXIO);
391 1.1 augustss
392 1.1 augustss if (sc->sc_state & UHID_OPEN)
393 1.12 augustss return (EBUSY);
394 1.17 augustss sc->sc_state |= UHID_OPEN;
395 1.1 augustss
396 1.17 augustss if (clalloc(&sc->sc_q, UHID_BSIZE, 0) == -1) {
397 1.17 augustss sc->sc_state &= ~UHID_OPEN;
398 1.12 augustss return (ENOMEM);
399 1.17 augustss }
400 1.1 augustss
401 1.18 augustss sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
402 1.18 augustss sc->sc_obuf = malloc(sc->sc_osize, M_USBDEV, M_WAITOK);
403 1.1 augustss
404 1.1 augustss /* Set up interrupt pipe. */
405 1.26.2.1 bouyer err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
406 1.26.2.1 bouyer USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc, sc->sc_ibuf,
407 1.26.2.1 bouyer sc->sc_isize, uhid_intr, USBD_DEFAULT_INTERVAL);
408 1.26.2.1 bouyer if (err) {
409 1.11 augustss DPRINTF(("uhidopen: usbd_open_pipe_intr failed, "
410 1.26.2.1 bouyer "error=%d\n",err));
411 1.18 augustss free(sc->sc_ibuf, M_USBDEV);
412 1.18 augustss free(sc->sc_obuf, M_USBDEV);
413 1.1 augustss sc->sc_state &= ~UHID_OPEN;
414 1.1 augustss return (EIO);
415 1.1 augustss }
416 1.17 augustss
417 1.17 augustss sc->sc_state &= ~UHID_IMMED;
418 1.17 augustss
419 1.26.2.1 bouyer sc->sc_async = 0;
420 1.26.2.1 bouyer
421 1.12 augustss return (0);
422 1.1 augustss }
423 1.1 augustss
424 1.1 augustss int
425 1.26.2.1 bouyer uhidclose(dev_t dev, int flag, int mode, struct proc *p)
426 1.1 augustss {
427 1.25 augustss struct uhid_softc *sc;
428 1.25 augustss
429 1.12 augustss USB_GET_SC(uhid, UHIDUNIT(dev), sc);
430 1.1 augustss
431 1.1 augustss DPRINTF(("uhidclose: sc=%p\n", sc));
432 1.1 augustss
433 1.1 augustss /* Disable interrupts. */
434 1.1 augustss usbd_abort_pipe(sc->sc_intrpipe);
435 1.1 augustss usbd_close_pipe(sc->sc_intrpipe);
436 1.18 augustss sc->sc_intrpipe = 0;
437 1.1 augustss
438 1.1 augustss clfree(&sc->sc_q);
439 1.1 augustss
440 1.18 augustss free(sc->sc_ibuf, M_USBDEV);
441 1.18 augustss free(sc->sc_obuf, M_USBDEV);
442 1.17 augustss
443 1.17 augustss sc->sc_state &= ~UHID_OPEN;
444 1.1 augustss
445 1.26.2.1 bouyer sc->sc_async = 0;
446 1.26.2.1 bouyer
447 1.12 augustss return (0);
448 1.1 augustss }
449 1.1 augustss
450 1.1 augustss int
451 1.26.2.1 bouyer uhid_do_read(struct uhid_softc *sc, struct uio *uio, int flag)
452 1.1 augustss {
453 1.1 augustss int s;
454 1.1 augustss int error = 0;
455 1.1 augustss size_t length;
456 1.1 augustss u_char buffer[UHID_CHUNK];
457 1.26.2.1 bouyer usbd_status err;
458 1.1 augustss
459 1.1 augustss DPRINTFN(1, ("uhidread\n"));
460 1.2 augustss if (sc->sc_state & UHID_IMMED) {
461 1.2 augustss DPRINTFN(1, ("uhidread immed\n"));
462 1.2 augustss
463 1.26.2.1 bouyer err = usbd_get_report(sc->sc_iface, UHID_INPUT_REPORT,
464 1.16 augustss sc->sc_iid, buffer, sc->sc_isize);
465 1.26.2.1 bouyer if (err)
466 1.2 augustss return (EIO);
467 1.2 augustss return (uiomove(buffer, sc->sc_isize, uio));
468 1.2 augustss }
469 1.2 augustss
470 1.10 augustss s = splusb();
471 1.1 augustss while (sc->sc_q.c_cc == 0) {
472 1.1 augustss if (flag & IO_NDELAY) {
473 1.1 augustss splx(s);
474 1.18 augustss return (EWOULDBLOCK);
475 1.1 augustss }
476 1.1 augustss sc->sc_state |= UHID_ASLP;
477 1.1 augustss DPRINTFN(5, ("uhidread: sleep on %p\n", sc));
478 1.18 augustss error = tsleep(&sc->sc_q, PZERO | PCATCH, "uhidrea", 0);
479 1.1 augustss DPRINTFN(5, ("uhidread: woke, error=%d\n", error));
480 1.18 augustss if (sc->sc_dying)
481 1.18 augustss error = EIO;
482 1.1 augustss if (error) {
483 1.1 augustss sc->sc_state &= ~UHID_ASLP;
484 1.18 augustss break;
485 1.1 augustss }
486 1.1 augustss if (sc->sc_state & UHID_NEEDCLEAR) {
487 1.1 augustss DPRINTFN(-1,("uhidread: clearing stall\n"));
488 1.1 augustss sc->sc_state &= ~UHID_NEEDCLEAR;
489 1.1 augustss usbd_clear_endpoint_stall(sc->sc_intrpipe);
490 1.1 augustss }
491 1.1 augustss }
492 1.1 augustss splx(s);
493 1.1 augustss
494 1.1 augustss /* Transfer as many chunks as possible. */
495 1.18 augustss while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0 && !error) {
496 1.1 augustss length = min(sc->sc_q.c_cc, uio->uio_resid);
497 1.1 augustss if (length > sizeof(buffer))
498 1.1 augustss length = sizeof(buffer);
499 1.1 augustss
500 1.1 augustss /* Remove a small chunk from the input queue. */
501 1.1 augustss (void) q_to_b(&sc->sc_q, buffer, length);
502 1.26.2.1 bouyer DPRINTFN(5, ("uhidread: got %lu chars\n", (u_long)length));
503 1.1 augustss
504 1.1 augustss /* Copy the data to the user process. */
505 1.1 augustss if ((error = uiomove(buffer, length, uio)) != 0)
506 1.1 augustss break;
507 1.1 augustss }
508 1.1 augustss
509 1.1 augustss return (error);
510 1.1 augustss }
511 1.1 augustss
512 1.1 augustss int
513 1.26.2.1 bouyer uhidread(dev_t dev, struct uio *uio, int flag)
514 1.1 augustss {
515 1.25 augustss struct uhid_softc *sc;
516 1.25 augustss int error;
517 1.25 augustss
518 1.18 augustss USB_GET_SC(uhid, UHIDUNIT(dev), sc);
519 1.18 augustss
520 1.18 augustss sc->sc_refcnt++;
521 1.18 augustss error = uhid_do_read(sc, uio, flag);
522 1.18 augustss if (--sc->sc_refcnt < 0)
523 1.24 augustss usb_detach_wakeup(USBDEV(sc->sc_dev));
524 1.18 augustss return (error);
525 1.18 augustss }
526 1.18 augustss
527 1.18 augustss int
528 1.26.2.1 bouyer uhid_do_write(struct uhid_softc *sc, struct uio *uio, int flag)
529 1.18 augustss {
530 1.1 augustss int error;
531 1.1 augustss int size;
532 1.26.2.1 bouyer usbd_status err;
533 1.1 augustss
534 1.18 augustss DPRINTFN(1, ("uhidwrite\n"));
535 1.18 augustss
536 1.18 augustss if (sc->sc_dying)
537 1.1 augustss return (EIO);
538 1.1 augustss
539 1.1 augustss size = sc->sc_osize;
540 1.1 augustss error = 0;
541 1.18 augustss if (uio->uio_resid != size)
542 1.18 augustss return (EINVAL);
543 1.18 augustss error = uiomove(sc->sc_obuf, size, uio);
544 1.18 augustss if (!error) {
545 1.1 augustss if (sc->sc_oid)
546 1.26.2.1 bouyer err = usbd_set_report(sc->sc_iface, UHID_OUTPUT_REPORT,
547 1.26.2.1 bouyer sc->sc_obuf[0], sc->sc_obuf+1, size-1);
548 1.1 augustss else
549 1.26.2.1 bouyer err = usbd_set_report(sc->sc_iface, UHID_OUTPUT_REPORT,
550 1.26.2.1 bouyer 0, sc->sc_obuf, size);
551 1.26.2.1 bouyer if (err)
552 1.1 augustss error = EIO;
553 1.1 augustss }
554 1.18 augustss
555 1.1 augustss return (error);
556 1.1 augustss }
557 1.1 augustss
558 1.1 augustss int
559 1.26.2.1 bouyer uhidwrite(dev_t dev, struct uio *uio, int flag)
560 1.18 augustss {
561 1.25 augustss struct uhid_softc *sc;
562 1.25 augustss int error;
563 1.25 augustss
564 1.18 augustss USB_GET_SC(uhid, UHIDUNIT(dev), sc);
565 1.18 augustss
566 1.18 augustss sc->sc_refcnt++;
567 1.18 augustss error = uhid_do_write(sc, uio, flag);
568 1.18 augustss if (--sc->sc_refcnt < 0)
569 1.24 augustss usb_detach_wakeup(USBDEV(sc->sc_dev));
570 1.18 augustss return (error);
571 1.18 augustss }
572 1.18 augustss
573 1.18 augustss int
574 1.26.2.1 bouyer uhid_do_ioctl(struct uhid_softc *sc, u_long cmd, caddr_t addr,
575 1.26.2.1 bouyer int flag, struct proc *p)
576 1.1 augustss {
577 1.1 augustss struct usb_ctl_report_desc *rd;
578 1.2 augustss struct usb_ctl_report *re;
579 1.2 augustss int size, id;
580 1.26.2.1 bouyer usbd_status err;
581 1.1 augustss
582 1.18 augustss DPRINTFN(2, ("uhidioctl: cmd=%lx\n", cmd));
583 1.18 augustss
584 1.18 augustss if (sc->sc_dying)
585 1.1 augustss return (EIO);
586 1.1 augustss
587 1.1 augustss switch (cmd) {
588 1.2 augustss case FIONBIO:
589 1.2 augustss /* All handled in the upper FS layer. */
590 1.2 augustss break;
591 1.2 augustss
592 1.26.2.1 bouyer case FIOASYNC:
593 1.26.2.1 bouyer if (*(int *)addr) {
594 1.26.2.1 bouyer if (sc->sc_async != NULL)
595 1.26.2.1 bouyer return (EBUSY);
596 1.26.2.1 bouyer sc->sc_async = p;
597 1.26.2.1 bouyer DPRINTF(("uhid_do_ioctl: FIOASYNC %p\n", p));
598 1.26.2.1 bouyer } else
599 1.26.2.1 bouyer sc->sc_async = NULL;
600 1.26.2.1 bouyer break;
601 1.26.2.1 bouyer
602 1.26.2.1 bouyer /* XXX this is not the most general solution. */
603 1.26.2.1 bouyer case TIOCSPGRP:
604 1.26.2.1 bouyer if (sc->sc_async == NULL)
605 1.26.2.1 bouyer return (EINVAL);
606 1.26.2.1 bouyer if (*(int *)addr != sc->sc_async->p_pgid)
607 1.26.2.1 bouyer return (EPERM);
608 1.26.2.1 bouyer break;
609 1.26.2.1 bouyer
610 1.1 augustss case USB_GET_REPORT_DESC:
611 1.1 augustss rd = (struct usb_ctl_report_desc *)addr;
612 1.1 augustss size = min(sc->sc_repdesc_size, sizeof rd->data);
613 1.1 augustss rd->size = size;
614 1.1 augustss memcpy(rd->data, sc->sc_repdesc, size);
615 1.1 augustss break;
616 1.2 augustss
617 1.2 augustss case USB_SET_IMMED:
618 1.9 augustss if (*(int *)addr) {
619 1.26.2.1 bouyer /* XXX should read into ibuf, but does it matter? */
620 1.26.2.1 bouyer err = usbd_get_report(sc->sc_iface, UHID_INPUT_REPORT,
621 1.26.2.1 bouyer sc->sc_iid, sc->sc_ibuf, sc->sc_isize);
622 1.26.2.1 bouyer if (err)
623 1.9 augustss return (EOPNOTSUPP);
624 1.12 augustss
625 1.2 augustss sc->sc_state |= UHID_IMMED;
626 1.9 augustss } else
627 1.2 augustss sc->sc_state &= ~UHID_IMMED;
628 1.2 augustss break;
629 1.2 augustss
630 1.2 augustss case USB_GET_REPORT:
631 1.2 augustss re = (struct usb_ctl_report *)addr;
632 1.2 augustss switch (re->report) {
633 1.2 augustss case UHID_INPUT_REPORT:
634 1.2 augustss size = sc->sc_isize;
635 1.2 augustss id = sc->sc_iid;
636 1.2 augustss break;
637 1.2 augustss case UHID_OUTPUT_REPORT:
638 1.2 augustss size = sc->sc_osize;
639 1.2 augustss id = sc->sc_oid;
640 1.2 augustss break;
641 1.2 augustss case UHID_FEATURE_REPORT:
642 1.2 augustss size = sc->sc_fsize;
643 1.2 augustss id = sc->sc_fid;
644 1.2 augustss break;
645 1.2 augustss default:
646 1.2 augustss return (EINVAL);
647 1.2 augustss }
648 1.26.2.1 bouyer err = usbd_get_report(sc->sc_iface, re->report, id, re->data,
649 1.26.2.1 bouyer size);
650 1.26.2.1 bouyer if (err)
651 1.26.2.1 bouyer return (EIO);
652 1.26.2.1 bouyer break;
653 1.26.2.1 bouyer
654 1.26.2.1 bouyer case USB_SET_REPORT:
655 1.26.2.1 bouyer re = (struct usb_ctl_report *)addr;
656 1.26.2.1 bouyer switch (re->report) {
657 1.26.2.1 bouyer case UHID_INPUT_REPORT:
658 1.26.2.1 bouyer size = sc->sc_isize;
659 1.26.2.1 bouyer id = sc->sc_iid;
660 1.26.2.1 bouyer break;
661 1.26.2.1 bouyer case UHID_OUTPUT_REPORT:
662 1.26.2.1 bouyer size = sc->sc_osize;
663 1.26.2.1 bouyer id = sc->sc_oid;
664 1.26.2.1 bouyer break;
665 1.26.2.1 bouyer case UHID_FEATURE_REPORT:
666 1.26.2.1 bouyer size = sc->sc_fsize;
667 1.26.2.1 bouyer id = sc->sc_fid;
668 1.26.2.1 bouyer break;
669 1.26.2.1 bouyer default:
670 1.26.2.1 bouyer return (EINVAL);
671 1.26.2.1 bouyer }
672 1.26.2.1 bouyer err = usbd_set_report(sc->sc_iface, re->report, id, re->data,
673 1.26.2.1 bouyer size);
674 1.26.2.1 bouyer if (err)
675 1.2 augustss return (EIO);
676 1.2 augustss break;
677 1.2 augustss
678 1.1 augustss default:
679 1.1 augustss return (EINVAL);
680 1.1 augustss }
681 1.1 augustss return (0);
682 1.1 augustss }
683 1.1 augustss
684 1.1 augustss int
685 1.26.2.1 bouyer uhidioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
686 1.18 augustss {
687 1.25 augustss struct uhid_softc *sc;
688 1.25 augustss int error;
689 1.25 augustss
690 1.18 augustss USB_GET_SC(uhid, UHIDUNIT(dev), sc);
691 1.18 augustss
692 1.18 augustss sc->sc_refcnt++;
693 1.18 augustss error = uhid_do_ioctl(sc, cmd, addr, flag, p);
694 1.18 augustss if (--sc->sc_refcnt < 0)
695 1.24 augustss usb_detach_wakeup(USBDEV(sc->sc_dev));
696 1.18 augustss return (error);
697 1.18 augustss }
698 1.18 augustss
699 1.18 augustss int
700 1.26.2.1 bouyer uhidpoll(dev_t dev, int events, struct proc *p)
701 1.1 augustss {
702 1.25 augustss struct uhid_softc *sc;
703 1.1 augustss int revents = 0;
704 1.1 augustss int s;
705 1.25 augustss
706 1.12 augustss USB_GET_SC(uhid, UHIDUNIT(dev), sc);
707 1.1 augustss
708 1.18 augustss if (sc->sc_dying)
709 1.1 augustss return (EIO);
710 1.1 augustss
711 1.10 augustss s = splusb();
712 1.1 augustss if (events & (POLLOUT | POLLWRNORM))
713 1.1 augustss revents |= events & (POLLOUT | POLLWRNORM);
714 1.4 veego if (events & (POLLIN | POLLRDNORM)) {
715 1.1 augustss if (sc->sc_q.c_cc > 0)
716 1.1 augustss revents |= events & (POLLIN | POLLRDNORM);
717 1.1 augustss else
718 1.1 augustss selrecord(p, &sc->sc_rsel);
719 1.4 veego }
720 1.1 augustss
721 1.1 augustss splx(s);
722 1.1 augustss return (revents);
723 1.1 augustss }
724 1.12 augustss
725 1.12 augustss #if defined(__FreeBSD__)
726 1.26.2.1 bouyer DRIVER_MODULE(uhid, uhub, uhid_driver, uhid_devclass, usbd_driver_load, 0);
727 1.12 augustss #endif
728