uhid.c revision 1.15.4.2 1 1.15.4.2 thorpej /* $NetBSD: uhid.c,v 1.15.4.2 1999/07/01 23:40:22 thorpej 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.6 augustss * This code is derived from software contributed to The NetBSD Foundation
8 1.6 augustss * by Lennart Augustsson (augustss (at) carlstedt.se) at
9 1.6 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.1 augustss */
39 1.1 augustss
40 1.15 augustss /*
41 1.15 augustss * HID spec: http://www.usb.org/developers/data/usbhid10.pdf
42 1.15 augustss */
43 1.1 augustss
44 1.1 augustss #include <sys/param.h>
45 1.1 augustss #include <sys/systm.h>
46 1.1 augustss #include <sys/kernel.h>
47 1.1 augustss #include <sys/malloc.h>
48 1.12 augustss #if defined(__NetBSD__)
49 1.14 augustss #include <sys/device.h>
50 1.12 augustss #include <sys/ioctl.h>
51 1.12 augustss #elif defined(__FreeBSD__)
52 1.12 augustss #include <sys/ioccom.h>
53 1.12 augustss #include <sys/filio.h>
54 1.12 augustss #include <sys/module.h>
55 1.12 augustss #include <sys/bus.h>
56 1.14 augustss #include <sys/ioccom.h>
57 1.12 augustss #endif
58 1.15.4.2 thorpej #include <sys/conf.h>
59 1.1 augustss #include <sys/tty.h>
60 1.1 augustss #include <sys/file.h>
61 1.1 augustss #include <sys/select.h>
62 1.1 augustss #include <sys/proc.h>
63 1.1 augustss #include <sys/vnode.h>
64 1.1 augustss #include <sys/poll.h>
65 1.1 augustss
66 1.1 augustss #include <dev/usb/usb.h>
67 1.1 augustss #include <dev/usb/usbhid.h>
68 1.1 augustss
69 1.1 augustss #include <dev/usb/usbdi.h>
70 1.1 augustss #include <dev/usb/usbdi_util.h>
71 1.1 augustss #include <dev/usb/hid.h>
72 1.1 augustss #include <dev/usb/usb_quirks.h>
73 1.1 augustss
74 1.1 augustss #ifdef USB_DEBUG
75 1.1 augustss #define DPRINTF(x) if (uhiddebug) printf x
76 1.1 augustss #define DPRINTFN(n,x) if (uhiddebug>(n)) printf x
77 1.1 augustss int uhiddebug = 0;
78 1.1 augustss #else
79 1.1 augustss #define DPRINTF(x)
80 1.1 augustss #define DPRINTFN(n,x)
81 1.1 augustss #endif
82 1.1 augustss
83 1.1 augustss struct uhid_softc {
84 1.15.4.2 thorpej bdevice sc_dev; /* base device */
85 1.1 augustss usbd_interface_handle sc_iface; /* interface */
86 1.1 augustss usbd_pipe_handle sc_intrpipe; /* interrupt pipe */
87 1.1 augustss int sc_ep_addr;
88 1.1 augustss
89 1.1 augustss int sc_isize;
90 1.1 augustss int sc_osize;
91 1.2 augustss int sc_fsize;
92 1.1 augustss u_int8_t sc_iid;
93 1.1 augustss u_int8_t sc_oid;
94 1.2 augustss u_int8_t sc_fid;
95 1.1 augustss
96 1.1 augustss char *sc_ibuf;
97 1.1 augustss char *sc_obuf;
98 1.1 augustss
99 1.1 augustss void *sc_repdesc;
100 1.1 augustss int sc_repdesc_size;
101 1.1 augustss
102 1.1 augustss struct clist sc_q;
103 1.1 augustss struct selinfo sc_rsel;
104 1.1 augustss u_char sc_state; /* driver state */
105 1.1 augustss #define UHID_OPEN 0x01 /* device is open */
106 1.5 augustss #define UHID_ASLP 0x02 /* waiting for device data */
107 1.1 augustss #define UHID_NEEDCLEAR 0x04 /* needs clearing endpoint stall */
108 1.2 augustss #define UHID_IMMED 0x08 /* return read data immediately */
109 1.15.4.2 thorpej
110 1.15.4.2 thorpej int sc_refcnt;
111 1.15.4.2 thorpej u_char sc_dying;
112 1.1 augustss };
113 1.1 augustss
114 1.1 augustss #define UHIDUNIT(dev) (minor(dev))
115 1.1 augustss #define UHID_CHUNK 128 /* chunk size for read */
116 1.1 augustss #define UHID_BSIZE 1020 /* buffer size */
117 1.1 augustss
118 1.1 augustss int uhidopen __P((dev_t, int, int, struct proc *));
119 1.1 augustss int uhidclose __P((dev_t, int, int, struct proc *p));
120 1.1 augustss int uhidread __P((dev_t, struct uio *uio, int));
121 1.1 augustss int uhidwrite __P((dev_t, struct uio *uio, int));
122 1.1 augustss int uhidioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
123 1.1 augustss int uhidpoll __P((dev_t, int, struct proc *));
124 1.1 augustss void uhid_intr __P((usbd_request_handle, usbd_private_handle, usbd_status));
125 1.15.4.2 thorpej
126 1.15.4.2 thorpej int uhid_do_read __P((struct uhid_softc *, struct uio *uio, int));
127 1.15.4.2 thorpej int uhid_do_write __P((struct uhid_softc *, struct uio *uio, int));
128 1.15.4.2 thorpej int uhid_do_ioctl __P((struct uhid_softc *, u_long, caddr_t, int, struct proc *));
129 1.1 augustss
130 1.12 augustss USB_DECLARE_DRIVER(uhid);
131 1.1 augustss
132 1.12 augustss USB_MATCH(uhid)
133 1.1 augustss {
134 1.12 augustss USB_MATCH_START(uhid, uaa);
135 1.1 augustss usb_interface_descriptor_t *id;
136 1.1 augustss
137 1.1 augustss if (!uaa->iface)
138 1.1 augustss return (UMATCH_NONE);
139 1.1 augustss id = usbd_get_interface_descriptor(uaa->iface);
140 1.1 augustss if (!id || id->bInterfaceClass != UCLASS_HID)
141 1.1 augustss return (UMATCH_NONE);
142 1.1 augustss return (UMATCH_IFACECLASS_GENERIC);
143 1.1 augustss }
144 1.1 augustss
145 1.12 augustss USB_ATTACH(uhid)
146 1.1 augustss {
147 1.12 augustss USB_ATTACH_START(uhid, sc, uaa);
148 1.1 augustss usbd_interface_handle iface = uaa->iface;
149 1.1 augustss usb_interface_descriptor_t *id;
150 1.1 augustss usb_endpoint_descriptor_t *ed;
151 1.1 augustss int size;
152 1.1 augustss void *desc;
153 1.1 augustss usbd_status r;
154 1.1 augustss char devinfo[1024];
155 1.1 augustss
156 1.1 augustss sc->sc_iface = iface;
157 1.1 augustss id = usbd_get_interface_descriptor(iface);
158 1.1 augustss usbd_devinfo(uaa->device, 0, devinfo);
159 1.12 augustss USB_ATTACH_SETUP;
160 1.12 augustss printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
161 1.7 augustss devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
162 1.12 augustss
163 1.1 augustss ed = usbd_interface2endpoint_descriptor(iface, 0);
164 1.1 augustss if (!ed) {
165 1.1 augustss printf("%s: could not read endpoint descriptor\n",
166 1.12 augustss USBDEVNAME(sc->sc_dev));
167 1.15.4.2 thorpej sc->sc_dying = 1;
168 1.12 augustss USB_ATTACH_ERROR_RETURN;
169 1.1 augustss }
170 1.1 augustss
171 1.11 augustss DPRINTFN(10,("uhid_attach: bLength=%d bDescriptorType=%d "
172 1.11 augustss "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
173 1.11 augustss " bInterval=%d\n",
174 1.11 augustss ed->bLength, ed->bDescriptorType,
175 1.11 augustss ed->bEndpointAddress & UE_ADDR,
176 1.11 augustss ed->bEndpointAddress & UE_IN ? "in" : "out",
177 1.11 augustss ed->bmAttributes & UE_XFERTYPE,
178 1.11 augustss UGETW(ed->wMaxPacketSize), ed->bInterval));
179 1.1 augustss
180 1.1 augustss if ((ed->bEndpointAddress & UE_IN) != UE_IN ||
181 1.1 augustss (ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
182 1.12 augustss printf("%s: unexpected endpoint\n", USBDEVNAME(sc->sc_dev));
183 1.15.4.2 thorpej sc->sc_dying = 1;
184 1.12 augustss USB_ATTACH_ERROR_RETURN;
185 1.1 augustss }
186 1.1 augustss
187 1.1 augustss sc->sc_ep_addr = ed->bEndpointAddress;
188 1.1 augustss
189 1.15.4.2 thorpej desc = 0;
190 1.15.4.2 thorpej r = usbd_alloc_report_desc(uaa->iface, &desc, &size, M_USBDEV);
191 1.1 augustss if (r != USBD_NORMAL_COMPLETION) {
192 1.12 augustss printf("%s: no report descriptor\n", USBDEVNAME(sc->sc_dev));
193 1.15.4.2 thorpej sc->sc_dying = 1;
194 1.15.4.2 thorpej if (desc)
195 1.15.4.2 thorpej free(desc, M_USBDEV);
196 1.12 augustss USB_ATTACH_ERROR_RETURN;
197 1.1 augustss }
198 1.1 augustss
199 1.1 augustss (void)usbd_set_idle(iface, 0, 0);
200 1.1 augustss
201 1.2 augustss sc->sc_isize = hid_report_size(desc, size, hid_input, &sc->sc_iid);
202 1.2 augustss sc->sc_osize = hid_report_size(desc, size, hid_output, &sc->sc_oid);
203 1.2 augustss sc->sc_fsize = hid_report_size(desc, size, hid_feature, &sc->sc_fid);
204 1.1 augustss
205 1.1 augustss sc->sc_repdesc = desc;
206 1.1 augustss sc->sc_repdesc_size = size;
207 1.12 augustss
208 1.12 augustss USB_ATTACH_SUCCESS_RETURN;
209 1.1 augustss }
210 1.1 augustss
211 1.15.4.2 thorpej int
212 1.15.4.2 thorpej uhid_activate(self, act)
213 1.15.4.2 thorpej struct device *self;
214 1.15.4.2 thorpej enum devact act;
215 1.15.4.2 thorpej {
216 1.15.4.2 thorpej return (0);
217 1.12 augustss }
218 1.12 augustss
219 1.15.4.2 thorpej int
220 1.15.4.2 thorpej uhid_detach(self, flags)
221 1.15.4.2 thorpej struct device *self;
222 1.15.4.2 thorpej int flags;
223 1.1 augustss {
224 1.15.4.2 thorpej struct uhid_softc *sc = (struct uhid_softc *)self;
225 1.15.4.2 thorpej int maj, mn;
226 1.15.4.2 thorpej int s;
227 1.3 augustss
228 1.15.4.2 thorpej DPRINTF(("uhid_detach: sc=%p flags=%d\n", sc, flags));
229 1.15.4.2 thorpej
230 1.15.4.2 thorpej sc->sc_dying = 1;
231 1.15.4.2 thorpej if (sc->sc_intrpipe)
232 1.15.4.2 thorpej usbd_abort_pipe(sc->sc_intrpipe);
233 1.15.4.2 thorpej
234 1.15.4.2 thorpej if (sc->sc_state & UHID_OPEN) {
235 1.15.4.2 thorpej s = splusb();
236 1.15.4.2 thorpej if (--sc->sc_refcnt >= 0) {
237 1.15.4.2 thorpej /* Wake everyone */
238 1.15.4.2 thorpej wakeup(&sc->sc_q);
239 1.15.4.2 thorpej /* Wait for processes to go away. */
240 1.15.4.2 thorpej usb_detach_wait(&sc->sc_dev);
241 1.15.4.2 thorpej }
242 1.15.4.2 thorpej splx(s);
243 1.15.4.2 thorpej }
244 1.15.4.2 thorpej
245 1.15.4.2 thorpej /* locate the major number */
246 1.15.4.2 thorpej for (maj = 0; maj < nchrdev; maj++)
247 1.15.4.2 thorpej if (cdevsw[maj].d_open == uhidopen)
248 1.15.4.2 thorpej break;
249 1.15.4.2 thorpej
250 1.15.4.2 thorpej /* Nuke the vnodes for any open instances (calls close). */
251 1.15.4.2 thorpej mn = self->dv_unit;
252 1.15.4.2 thorpej vdevgone(maj, mn, mn, VCHR);
253 1.15.4.2 thorpej
254 1.15.4.2 thorpej free(sc->sc_repdesc, M_USBDEV);
255 1.15.4.2 thorpej
256 1.15.4.2 thorpej return (0);
257 1.1 augustss }
258 1.1 augustss
259 1.1 augustss void
260 1.1 augustss uhid_intr(reqh, addr, status)
261 1.1 augustss usbd_request_handle reqh;
262 1.1 augustss usbd_private_handle addr;
263 1.1 augustss usbd_status status;
264 1.1 augustss {
265 1.1 augustss struct uhid_softc *sc = addr;
266 1.1 augustss
267 1.1 augustss DPRINTFN(5, ("uhid_intr: status=%d\n", status));
268 1.1 augustss DPRINTFN(5, ("uhid_intr: data = %02x %02x %02x\n",
269 1.1 augustss sc->sc_ibuf[0], sc->sc_ibuf[1], sc->sc_ibuf[2]));
270 1.1 augustss
271 1.1 augustss if (status == USBD_CANCELLED)
272 1.1 augustss return;
273 1.1 augustss
274 1.1 augustss if (status != USBD_NORMAL_COMPLETION) {
275 1.1 augustss DPRINTF(("uhid_intr: status=%d\n", status));
276 1.1 augustss sc->sc_state |= UHID_NEEDCLEAR;
277 1.1 augustss return;
278 1.1 augustss }
279 1.1 augustss
280 1.1 augustss (void) b_to_q(sc->sc_ibuf, sc->sc_isize, &sc->sc_q);
281 1.1 augustss
282 1.1 augustss if (sc->sc_state & UHID_ASLP) {
283 1.1 augustss sc->sc_state &= ~UHID_ASLP;
284 1.1 augustss DPRINTFN(5, ("uhid_intr: waking %p\n", sc));
285 1.15.4.2 thorpej wakeup(&sc->sc_q);
286 1.1 augustss }
287 1.1 augustss selwakeup(&sc->sc_rsel);
288 1.1 augustss }
289 1.1 augustss
290 1.1 augustss int
291 1.1 augustss uhidopen(dev, flag, mode, p)
292 1.1 augustss dev_t dev;
293 1.1 augustss int flag;
294 1.1 augustss int mode;
295 1.1 augustss struct proc *p;
296 1.1 augustss {
297 1.1 augustss usbd_status r;
298 1.12 augustss USB_GET_SC_OPEN(uhid, UHIDUNIT(dev), sc);
299 1.1 augustss
300 1.15.4.2 thorpej DPRINTF(("uhidopen: sc=%p\n", sc));
301 1.1 augustss
302 1.15.4.2 thorpej if (sc->sc_dying)
303 1.15.4.2 thorpej return (ENXIO);
304 1.1 augustss
305 1.1 augustss if (sc->sc_state & UHID_OPEN)
306 1.12 augustss return (EBUSY);
307 1.15.4.1 thorpej sc->sc_state |= UHID_OPEN;
308 1.1 augustss
309 1.12 augustss #if defined(__NetBSD__)
310 1.15.4.1 thorpej if (clalloc(&sc->sc_q, UHID_BSIZE, 0) == -1) {
311 1.15.4.1 thorpej sc->sc_state &= ~UHID_OPEN;
312 1.12 augustss return (ENOMEM);
313 1.15.4.1 thorpej }
314 1.12 augustss #elif defined(__FreeBSD__)
315 1.12 augustss clist_alloc_cblocks(&sc->sc_q, UHID_BSIZE, 0);
316 1.12 augustss #endif
317 1.1 augustss
318 1.15.4.2 thorpej sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
319 1.15.4.2 thorpej sc->sc_obuf = malloc(sc->sc_osize, M_USBDEV, M_WAITOK);
320 1.1 augustss
321 1.1 augustss /* Set up interrupt pipe. */
322 1.1 augustss r = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
323 1.1 augustss USBD_SHORT_XFER_OK,
324 1.1 augustss &sc->sc_intrpipe, sc, sc->sc_ibuf,
325 1.1 augustss sc->sc_isize, uhid_intr);
326 1.1 augustss if (r != USBD_NORMAL_COMPLETION) {
327 1.11 augustss DPRINTF(("uhidopen: usbd_open_pipe_intr failed, "
328 1.11 augustss "error=%d\n",r));
329 1.15.4.2 thorpej free(sc->sc_ibuf, M_USBDEV);
330 1.15.4.2 thorpej free(sc->sc_obuf, M_USBDEV);
331 1.1 augustss sc->sc_state &= ~UHID_OPEN;
332 1.1 augustss return (EIO);
333 1.1 augustss }
334 1.15.4.1 thorpej
335 1.15.4.1 thorpej sc->sc_state &= ~UHID_IMMED;
336 1.15.4.1 thorpej
337 1.12 augustss return (0);
338 1.1 augustss }
339 1.1 augustss
340 1.1 augustss int
341 1.1 augustss uhidclose(dev, flag, mode, p)
342 1.1 augustss dev_t dev;
343 1.1 augustss int flag;
344 1.1 augustss int mode;
345 1.1 augustss struct proc *p;
346 1.1 augustss {
347 1.12 augustss USB_GET_SC(uhid, UHIDUNIT(dev), sc);
348 1.1 augustss
349 1.1 augustss DPRINTF(("uhidclose: sc=%p\n", sc));
350 1.1 augustss
351 1.1 augustss /* Disable interrupts. */
352 1.1 augustss usbd_abort_pipe(sc->sc_intrpipe);
353 1.1 augustss usbd_close_pipe(sc->sc_intrpipe);
354 1.15.4.2 thorpej sc->sc_intrpipe = 0;
355 1.1 augustss
356 1.12 augustss #if defined(__NetBSD__)
357 1.1 augustss clfree(&sc->sc_q);
358 1.12 augustss #elif defined(__FreeBSD__)
359 1.12 augustss clist_free_cblocks(&sc->sc_q);
360 1.12 augustss #endif
361 1.1 augustss
362 1.15.4.2 thorpej free(sc->sc_ibuf, M_USBDEV);
363 1.15.4.2 thorpej free(sc->sc_obuf, M_USBDEV);
364 1.1 augustss
365 1.15.4.1 thorpej sc->sc_state &= ~UHID_OPEN;
366 1.15.4.1 thorpej
367 1.12 augustss return (0);
368 1.1 augustss }
369 1.1 augustss
370 1.1 augustss int
371 1.15.4.2 thorpej uhid_do_read(sc, uio, flag)
372 1.15.4.2 thorpej struct uhid_softc *sc;
373 1.1 augustss struct uio *uio;
374 1.1 augustss int flag;
375 1.1 augustss {
376 1.1 augustss int s;
377 1.1 augustss int error = 0;
378 1.1 augustss size_t length;
379 1.1 augustss u_char buffer[UHID_CHUNK];
380 1.2 augustss usbd_status r;
381 1.1 augustss
382 1.1 augustss DPRINTFN(1, ("uhidread\n"));
383 1.2 augustss if (sc->sc_state & UHID_IMMED) {
384 1.2 augustss DPRINTFN(1, ("uhidread immed\n"));
385 1.2 augustss
386 1.2 augustss r = usbd_get_report(sc->sc_iface, UHID_INPUT_REPORT,
387 1.15.4.1 thorpej sc->sc_iid, buffer, sc->sc_isize);
388 1.2 augustss if (r != USBD_NORMAL_COMPLETION)
389 1.2 augustss return (EIO);
390 1.2 augustss return (uiomove(buffer, sc->sc_isize, uio));
391 1.2 augustss }
392 1.2 augustss
393 1.10 augustss s = splusb();
394 1.1 augustss while (sc->sc_q.c_cc == 0) {
395 1.1 augustss if (flag & IO_NDELAY) {
396 1.1 augustss splx(s);
397 1.15.4.2 thorpej return (EWOULDBLOCK);
398 1.1 augustss }
399 1.1 augustss sc->sc_state |= UHID_ASLP;
400 1.1 augustss DPRINTFN(5, ("uhidread: sleep on %p\n", sc));
401 1.15.4.2 thorpej error = tsleep(&sc->sc_q, PZERO | PCATCH, "uhidrea", 0);
402 1.1 augustss DPRINTFN(5, ("uhidread: woke, error=%d\n", error));
403 1.15.4.2 thorpej if (sc->sc_dying)
404 1.15.4.2 thorpej error = EIO;
405 1.1 augustss if (error) {
406 1.1 augustss sc->sc_state &= ~UHID_ASLP;
407 1.15.4.2 thorpej break;
408 1.1 augustss }
409 1.1 augustss if (sc->sc_state & UHID_NEEDCLEAR) {
410 1.1 augustss DPRINTFN(-1,("uhidread: clearing stall\n"));
411 1.1 augustss sc->sc_state &= ~UHID_NEEDCLEAR;
412 1.1 augustss usbd_clear_endpoint_stall(sc->sc_intrpipe);
413 1.1 augustss }
414 1.1 augustss }
415 1.1 augustss splx(s);
416 1.1 augustss
417 1.1 augustss /* Transfer as many chunks as possible. */
418 1.15.4.2 thorpej while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0 && !error) {
419 1.1 augustss length = min(sc->sc_q.c_cc, uio->uio_resid);
420 1.1 augustss if (length > sizeof(buffer))
421 1.1 augustss length = sizeof(buffer);
422 1.1 augustss
423 1.1 augustss /* Remove a small chunk from the input queue. */
424 1.1 augustss (void) q_to_b(&sc->sc_q, buffer, length);
425 1.1 augustss DPRINTFN(5, ("uhidread: got %d chars\n", length));
426 1.1 augustss
427 1.1 augustss /* Copy the data to the user process. */
428 1.1 augustss if ((error = uiomove(buffer, length, uio)) != 0)
429 1.1 augustss break;
430 1.1 augustss }
431 1.1 augustss
432 1.1 augustss return (error);
433 1.1 augustss }
434 1.1 augustss
435 1.1 augustss int
436 1.15.4.2 thorpej uhidread(dev, uio, flag)
437 1.1 augustss dev_t dev;
438 1.1 augustss struct uio *uio;
439 1.1 augustss int flag;
440 1.1 augustss {
441 1.15.4.2 thorpej USB_GET_SC(uhid, UHIDUNIT(dev), sc);
442 1.15.4.2 thorpej int error;
443 1.15.4.2 thorpej
444 1.15.4.2 thorpej sc->sc_refcnt++;
445 1.15.4.2 thorpej error = uhid_do_read(sc, uio, flag);
446 1.15.4.2 thorpej if (--sc->sc_refcnt < 0)
447 1.15.4.2 thorpej usb_detach_wakeup(&sc->sc_dev);
448 1.15.4.2 thorpej return (error);
449 1.15.4.2 thorpej }
450 1.15.4.2 thorpej
451 1.15.4.2 thorpej int
452 1.15.4.2 thorpej uhid_do_write(sc, uio, flag)
453 1.15.4.2 thorpej struct uhid_softc *sc;
454 1.15.4.2 thorpej struct uio *uio;
455 1.15.4.2 thorpej int flag;
456 1.15.4.2 thorpej {
457 1.1 augustss int error;
458 1.1 augustss int size;
459 1.1 augustss usbd_status r;
460 1.1 augustss
461 1.1 augustss DPRINTFN(1, ("uhidwrite\n"));
462 1.1 augustss
463 1.15.4.2 thorpej if (sc->sc_dying)
464 1.15.4.2 thorpej return (EIO);
465 1.15.4.2 thorpej
466 1.1 augustss size = sc->sc_osize;
467 1.1 augustss error = 0;
468 1.15.4.2 thorpej if (uio->uio_resid != size)
469 1.15.4.2 thorpej return (EINVAL);
470 1.15.4.2 thorpej error = uiomove(sc->sc_obuf, size, uio);
471 1.15.4.2 thorpej if (!error) {
472 1.1 augustss if (sc->sc_oid)
473 1.1 augustss r = usbd_set_report(sc->sc_iface, UHID_OUTPUT_REPORT,
474 1.1 augustss sc->sc_obuf[0],
475 1.1 augustss sc->sc_obuf+1, size-1);
476 1.1 augustss else
477 1.1 augustss r = usbd_set_report(sc->sc_iface, UHID_OUTPUT_REPORT,
478 1.1 augustss 0, sc->sc_obuf, size);
479 1.1 augustss if (r != USBD_NORMAL_COMPLETION) {
480 1.1 augustss error = EIO;
481 1.1 augustss }
482 1.1 augustss }
483 1.15.4.2 thorpej
484 1.1 augustss return (error);
485 1.1 augustss }
486 1.1 augustss
487 1.1 augustss int
488 1.15.4.2 thorpej uhidwrite(dev, uio, flag)
489 1.1 augustss dev_t dev;
490 1.15.4.2 thorpej struct uio *uio;
491 1.15.4.2 thorpej int flag;
492 1.15.4.2 thorpej {
493 1.15.4.2 thorpej USB_GET_SC(uhid, UHIDUNIT(dev), sc);
494 1.15.4.2 thorpej int error;
495 1.15.4.2 thorpej
496 1.15.4.2 thorpej sc->sc_refcnt++;
497 1.15.4.2 thorpej error = uhid_do_write(sc, uio, flag);
498 1.15.4.2 thorpej if (--sc->sc_refcnt < 0)
499 1.15.4.2 thorpej usb_detach_wakeup(&sc->sc_dev);
500 1.15.4.2 thorpej return (error);
501 1.15.4.2 thorpej }
502 1.15.4.2 thorpej
503 1.15.4.2 thorpej int
504 1.15.4.2 thorpej uhid_do_ioctl(sc, cmd, addr, flag, p)
505 1.15.4.2 thorpej struct uhid_softc *sc;
506 1.1 augustss u_long cmd;
507 1.1 augustss caddr_t addr;
508 1.1 augustss int flag;
509 1.1 augustss struct proc *p;
510 1.1 augustss {
511 1.1 augustss struct usb_ctl_report_desc *rd;
512 1.2 augustss struct usb_ctl_report *re;
513 1.2 augustss int size, id;
514 1.2 augustss usbd_status r;
515 1.1 augustss
516 1.15.4.2 thorpej DPRINTFN(2, ("uhidioctl: cmd=%lx\n", cmd));
517 1.15.4.2 thorpej
518 1.15.4.2 thorpej if (sc->sc_dying)
519 1.1 augustss return (EIO);
520 1.1 augustss
521 1.1 augustss switch (cmd) {
522 1.2 augustss case FIONBIO:
523 1.2 augustss /* All handled in the upper FS layer. */
524 1.2 augustss break;
525 1.2 augustss
526 1.1 augustss case USB_GET_REPORT_DESC:
527 1.1 augustss rd = (struct usb_ctl_report_desc *)addr;
528 1.1 augustss size = min(sc->sc_repdesc_size, sizeof rd->data);
529 1.1 augustss rd->size = size;
530 1.1 augustss memcpy(rd->data, sc->sc_repdesc, size);
531 1.1 augustss break;
532 1.2 augustss
533 1.2 augustss case USB_SET_IMMED:
534 1.9 augustss if (*(int *)addr) {
535 1.9 augustss /* XXX should read into ibuf, but does it matter */
536 1.9 augustss r = usbd_get_report(sc->sc_iface, UHID_INPUT_REPORT,
537 1.9 augustss sc->sc_iid, sc->sc_ibuf,
538 1.9 augustss sc->sc_isize);
539 1.9 augustss if (r != USBD_NORMAL_COMPLETION)
540 1.9 augustss return (EOPNOTSUPP);
541 1.12 augustss
542 1.2 augustss sc->sc_state |= UHID_IMMED;
543 1.9 augustss } else
544 1.2 augustss sc->sc_state &= ~UHID_IMMED;
545 1.2 augustss break;
546 1.2 augustss
547 1.2 augustss case USB_GET_REPORT:
548 1.2 augustss re = (struct usb_ctl_report *)addr;
549 1.2 augustss switch (re->report) {
550 1.2 augustss case UHID_INPUT_REPORT:
551 1.2 augustss size = sc->sc_isize;
552 1.2 augustss id = sc->sc_iid;
553 1.2 augustss break;
554 1.2 augustss case UHID_OUTPUT_REPORT:
555 1.2 augustss size = sc->sc_osize;
556 1.2 augustss id = sc->sc_oid;
557 1.2 augustss break;
558 1.2 augustss case UHID_FEATURE_REPORT:
559 1.2 augustss size = sc->sc_fsize;
560 1.2 augustss id = sc->sc_fid;
561 1.2 augustss break;
562 1.2 augustss default:
563 1.2 augustss return (EINVAL);
564 1.2 augustss }
565 1.2 augustss r = usbd_get_report(sc->sc_iface, re->report, id,
566 1.2 augustss re->data, size);
567 1.2 augustss if (r != USBD_NORMAL_COMPLETION)
568 1.2 augustss return (EIO);
569 1.2 augustss break;
570 1.2 augustss
571 1.1 augustss default:
572 1.1 augustss return (EINVAL);
573 1.1 augustss }
574 1.1 augustss return (0);
575 1.1 augustss }
576 1.1 augustss
577 1.1 augustss int
578 1.15.4.2 thorpej uhidioctl(dev, cmd, addr, flag, p)
579 1.15.4.2 thorpej dev_t dev;
580 1.15.4.2 thorpej u_long cmd;
581 1.15.4.2 thorpej caddr_t addr;
582 1.15.4.2 thorpej int flag;
583 1.15.4.2 thorpej struct proc *p;
584 1.15.4.2 thorpej {
585 1.15.4.2 thorpej USB_GET_SC(uhid, UHIDUNIT(dev), sc);
586 1.15.4.2 thorpej int error;
587 1.15.4.2 thorpej
588 1.15.4.2 thorpej sc->sc_refcnt++;
589 1.15.4.2 thorpej error = uhid_do_ioctl(sc, cmd, addr, flag, p);
590 1.15.4.2 thorpej if (--sc->sc_refcnt < 0)
591 1.15.4.2 thorpej usb_detach_wakeup(&sc->sc_dev);
592 1.15.4.2 thorpej return (error);
593 1.15.4.2 thorpej }
594 1.15.4.2 thorpej
595 1.15.4.2 thorpej int
596 1.1 augustss uhidpoll(dev, events, p)
597 1.1 augustss dev_t dev;
598 1.1 augustss int events;
599 1.1 augustss struct proc *p;
600 1.1 augustss {
601 1.1 augustss int revents = 0;
602 1.1 augustss int s;
603 1.12 augustss USB_GET_SC(uhid, UHIDUNIT(dev), sc);
604 1.1 augustss
605 1.15.4.2 thorpej if (sc->sc_dying)
606 1.1 augustss return (EIO);
607 1.1 augustss
608 1.10 augustss s = splusb();
609 1.1 augustss if (events & (POLLOUT | POLLWRNORM))
610 1.1 augustss revents |= events & (POLLOUT | POLLWRNORM);
611 1.4 veego if (events & (POLLIN | POLLRDNORM)) {
612 1.1 augustss if (sc->sc_q.c_cc > 0)
613 1.1 augustss revents |= events & (POLLIN | POLLRDNORM);
614 1.1 augustss else
615 1.1 augustss selrecord(p, &sc->sc_rsel);
616 1.4 veego }
617 1.1 augustss
618 1.1 augustss splx(s);
619 1.1 augustss return (revents);
620 1.1 augustss }
621 1.12 augustss
622 1.12 augustss #if defined(__FreeBSD__)
623 1.14 augustss DRIVER_MODULE(uhid, usb, uhid_driver, uhid_devclass, usbd_driver_load, 0);
624 1.12 augustss #endif
625