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