ugen.c revision 1.91.4.1 1 /* $NetBSD: ugen.c,v 1.91.4.1 2007/05/22 14:57:41 itohy Exp $ */
2
3 /*
4 * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart (at) augustsson.net) at
9 * Carlstedt Research & Technology.
10 *
11 * Copyright (c) 2006 BBN Technologies Corp. All rights reserved.
12 * Effort sponsored in part by the Defense Advanced Research Projects
13 * Agency (DARPA) and the Department of the Interior National Business
14 * Center under agreement number NBCHC050166.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. All advertising materials mentioning features or use of this software
25 * must display the following acknowledgement:
26 * This product includes software developed by the NetBSD
27 * Foundation, Inc. and its contributors.
28 * 4. Neither the name of The NetBSD Foundation nor the names of its
29 * contributors may be used to endorse or promote products derived
30 * from this software without specific prior written permission.
31 *
32 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
33 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
34 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
35 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
36 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
37 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
39 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
40 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGE.
43 */
44
45
46 #include <sys/cdefs.h>
47 __KERNEL_RCSID(0, "$NetBSD: ugen.c,v 1.91.4.1 2007/05/22 14:57:41 itohy Exp $");
48
49 #include "opt_ugen_bulk_ra_wb.h"
50 #include "opt_compat_netbsd.h"
51
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
55 #include <sys/malloc.h>
56 #if defined(__NetBSD__) || defined(__OpenBSD__)
57 #include <sys/device.h>
58 #include <sys/ioctl.h>
59 #elif defined(__FreeBSD__)
60 #include <sys/module.h>
61 #include <sys/bus.h>
62 #include <sys/ioccom.h>
63 #include <sys/conf.h>
64 #include <sys/fcntl.h>
65 #include <sys/filio.h>
66 #endif
67 #include <sys/conf.h>
68 #include <sys/tty.h>
69 #include <sys/file.h>
70 #include <sys/select.h>
71 #include <sys/proc.h>
72 #include <sys/vnode.h>
73 #include <sys/poll.h>
74
75 #include <dev/usb/usb.h>
76 #include <dev/usb/usbdi.h>
77 #include <dev/usb/usbdi_util.h>
78
79 #ifdef UGEN_DEBUG
80 #define DPRINTF(x) if (ugendebug) logprintf x
81 #define DPRINTFN(n,x) if (ugendebug>(n)) logprintf x
82 int ugendebug = 0;
83 #else
84 #define DPRINTF(x)
85 #define DPRINTFN(n,x)
86 #endif
87
88 #define UGEN_CHUNK 128 /* chunk size for read */
89 #define UGEN_IBSIZE 1020 /* buffer size */
90 #define UGEN_BBSIZE 1024
91
92 #define UGEN_NISOFRAMES 500 /* 0.5 seconds worth */
93 #define UGEN_NISOREQS 6 /* number of outstanding xfer requests */
94 #define UGEN_NISORFRMS 4 /* number of frames (miliseconds) per req */
95
96 #define UGEN_BULK_RA_WB_BUFSIZE 16384 /* default buffer size */
97 #define UGEN_BULK_RA_WB_BUFMAX (1 << 20) /* maximum allowed buffer */
98
99 struct ugen_endpoint {
100 struct ugen_softc *sc;
101 usb_endpoint_descriptor_t *edesc;
102 usbd_interface_handle iface;
103 int state;
104 #define UGEN_ASLP 0x02 /* waiting for data */
105 #define UGEN_SHORT_OK 0x04 /* short xfers are OK */
106 #define UGEN_BULK_RA 0x08 /* in bulk read-ahead mode */
107 #define UGEN_BULK_WB 0x10 /* in bulk write-behind mode */
108 #define UGEN_RA_WB_STOP 0x20 /* RA/WB xfer is stopped (buffer full/empty) */
109 usbd_pipe_handle pipeh;
110 struct clist q;
111 struct selinfo rsel;
112 u_char *ibuf; /* start of buffer (circular for isoc) */
113 u_char *fill; /* location for input (isoc) */
114 u_char *limit; /* end of circular buffer (isoc) */
115 u_char *cur; /* current read location (isoc) */
116 u_int32_t timeout;
117 #ifdef UGEN_BULK_RA_WB
118 u_int32_t ra_wb_bufsize; /* requested size for RA/WB buffer */
119 u_int32_t ra_wb_reqsize; /* requested xfer length for RA/WB */
120 u_int32_t ra_wb_used; /* how much is in buffer */
121 u_int32_t ra_wb_xferlen; /* current xfer length for RA/WB */
122 usbd_xfer_handle ra_wb_xfer;
123 #endif
124 struct isoreq {
125 struct ugen_endpoint *sce;
126 usbd_xfer_handle xfer;
127 void *dmabuf;
128 u_int16_t sizes[UGEN_NISORFRMS];
129 } isoreqs[UGEN_NISOREQS];
130 };
131
132 struct ugen_softc {
133 USBBASEDEVICE sc_dev; /* base device */
134 usbd_device_handle sc_udev;
135
136 char sc_is_open[USB_MAX_ENDPOINTS];
137 struct ugen_endpoint sc_endpoints[USB_MAX_ENDPOINTS][2];
138 #define OUT 0
139 #define IN 1
140
141 int sc_refcnt;
142 char sc_buffer[UGEN_BBSIZE];
143 u_char sc_dying;
144 };
145
146 #if defined(__NetBSD__)
147 dev_type_open(ugenopen);
148 dev_type_close(ugenclose);
149 dev_type_read(ugenread);
150 dev_type_write(ugenwrite);
151 dev_type_ioctl(ugenioctl);
152 dev_type_poll(ugenpoll);
153 dev_type_kqfilter(ugenkqfilter);
154
155 const struct cdevsw ugen_cdevsw = {
156 ugenopen, ugenclose, ugenread, ugenwrite, ugenioctl,
157 nostop, notty, ugenpoll, nommap, ugenkqfilter, D_OTHER,
158 };
159 #elif defined(__OpenBSD__)
160 cdev_decl(ugen);
161 #elif defined(__FreeBSD__)
162 d_open_t ugenopen;
163 d_close_t ugenclose;
164 d_read_t ugenread;
165 d_write_t ugenwrite;
166 d_ioctl_t ugenioctl;
167 d_poll_t ugenpoll;
168
169 #define UGEN_CDEV_MAJOR 114
170
171 Static struct cdevsw ugen_cdevsw = {
172 /* open */ ugenopen,
173 /* close */ ugenclose,
174 /* read */ ugenread,
175 /* write */ ugenwrite,
176 /* ioctl */ ugenioctl,
177 /* poll */ ugenpoll,
178 /* mmap */ nommap,
179 /* strategy */ nostrategy,
180 /* name */ "ugen",
181 /* maj */ UGEN_CDEV_MAJOR,
182 /* dump */ nodump,
183 /* psize */ nopsize,
184 /* flags */ 0,
185 /* bmaj */ -1
186 };
187 #endif
188
189 Static void ugenintr(usbd_xfer_handle xfer, usbd_private_handle addr,
190 usbd_status status);
191 Static void ugen_isoc_rintr(usbd_xfer_handle xfer, usbd_private_handle addr,
192 usbd_status status);
193 #ifdef UGEN_BULK_RA_WB
194 Static void ugen_bulkra_intr(usbd_xfer_handle xfer, usbd_private_handle addr,
195 usbd_status status);
196 Static void ugen_bulkwb_intr(usbd_xfer_handle xfer, usbd_private_handle addr,
197 usbd_status status);
198 #endif
199 Static int ugen_do_read(struct ugen_softc *, int, struct uio *, int);
200 Static int ugen_do_write(struct ugen_softc *, int, struct uio *, int);
201 Static int ugen_do_ioctl(struct ugen_softc *, int, u_long,
202 caddr_t, int, struct lwp *);
203 Static int ugen_set_config(struct ugen_softc *sc, int configno);
204 Static usb_config_descriptor_t *ugen_get_cdesc(struct ugen_softc *sc,
205 int index, int *lenp);
206 Static usbd_status ugen_set_interface(struct ugen_softc *, int, int);
207 Static int ugen_get_alt_index(struct ugen_softc *sc, int ifaceidx);
208
209 #define UGENUNIT(n) ((minor(n) >> 4) & 0xf)
210 #define UGENENDPOINT(n) (minor(n) & 0xf)
211 #define UGENDEV(u, e) (makedev(0, ((u) << 4) | (e)))
212
213 USB_DECLARE_DRIVER(ugen);
214
215 USB_MATCH(ugen)
216 {
217 USB_MATCH_START(ugen, uaa);
218
219 if (match->cf_flags & 1)
220 return (UMATCH_HIGHEST);
221 else if (uaa->usegeneric)
222 return (UMATCH_GENERIC);
223 else
224 return (UMATCH_NONE);
225 }
226
227 USB_ATTACH(ugen)
228 {
229 USB_ATTACH_START(ugen, sc, uaa);
230 usbd_device_handle udev;
231 char *devinfop;
232 usbd_status err;
233 int conf;
234
235 devinfop = usbd_devinfo_alloc(uaa->device, 0);
236 USB_ATTACH_SETUP;
237 printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfop);
238 usbd_devinfo_free(devinfop);
239
240 sc->sc_udev = udev = uaa->device;
241
242 /* First set configuration index 0, the default one for ugen. */
243 err = usbd_set_config_index(udev, 0, 0);
244 if (err) {
245 printf("%s: setting configuration index 0 failed\n",
246 USBDEVNAME(sc->sc_dev));
247 sc->sc_dying = 1;
248 USB_ATTACH_ERROR_RETURN;
249 }
250 conf = usbd_get_config_descriptor(udev)->bConfigurationValue;
251
252 /* Set up all the local state for this configuration. */
253 err = ugen_set_config(sc, conf);
254 if (err) {
255 printf("%s: setting configuration %d failed\n",
256 USBDEVNAME(sc->sc_dev), conf);
257 sc->sc_dying = 1;
258 USB_ATTACH_ERROR_RETURN;
259 }
260
261 #ifdef __FreeBSD__
262 {
263 static int global_init_done = 0;
264 if (!global_init_done) {
265 cdevsw_add(&ugen_cdevsw);
266 global_init_done = 1;
267 }
268 }
269 #endif
270
271 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
272 USBDEV(sc->sc_dev));
273
274 USB_ATTACH_SUCCESS_RETURN;
275 }
276
277 Static int
278 ugen_set_config(struct ugen_softc *sc, int configno)
279 {
280 usbd_device_handle dev = sc->sc_udev;
281 usbd_interface_handle iface;
282 usb_endpoint_descriptor_t *ed;
283 struct ugen_endpoint *sce;
284 u_int8_t niface, nendpt;
285 int ifaceno, endptno, endpt;
286 usbd_status err;
287 int dir;
288
289 DPRINTFN(1,("ugen_set_config: %s to configno %d, sc=%p\n",
290 USBDEVNAME(sc->sc_dev), configno, sc));
291
292 /*
293 * We start at 1, not 0, because we don't care whether the
294 * control endpoint is open or not. It is always present.
295 */
296 for (endptno = 1; endptno < USB_MAX_ENDPOINTS; endptno++)
297 if (sc->sc_is_open[endptno]) {
298 DPRINTFN(1,
299 ("ugen_set_config: %s - endpoint %d is open\n",
300 USBDEVNAME(sc->sc_dev), endptno));
301 return (USBD_IN_USE);
302 }
303
304 /* Avoid setting the current value. */
305 if (usbd_get_config_descriptor(dev)->bConfigurationValue != configno) {
306 err = usbd_set_config_no(dev, configno, 1);
307 if (err)
308 return (err);
309 }
310
311 err = usbd_interface_count(dev, &niface);
312 if (err)
313 return (err);
314 memset(sc->sc_endpoints, 0, sizeof sc->sc_endpoints);
315 for (ifaceno = 0; ifaceno < niface; ifaceno++) {
316 DPRINTFN(1,("ugen_set_config: ifaceno %d\n", ifaceno));
317 err = usbd_device2interface_handle(dev, ifaceno, &iface);
318 if (err)
319 return (err);
320 err = usbd_endpoint_count(iface, &nendpt);
321 if (err)
322 return (err);
323 for (endptno = 0; endptno < nendpt; endptno++) {
324 ed = usbd_interface2endpoint_descriptor(iface,endptno);
325 KASSERT(ed != NULL);
326 endpt = ed->bEndpointAddress;
327 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
328 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
329 DPRINTFN(1,("ugen_set_config: endptno %d, endpt=0x%02x"
330 "(%d,%d), sce=%p\n",
331 endptno, endpt, UE_GET_ADDR(endpt),
332 UE_GET_DIR(endpt), sce));
333 sce->sc = sc;
334 sce->edesc = ed;
335 sce->iface = iface;
336 }
337 }
338 return (USBD_NORMAL_COMPLETION);
339 }
340
341 int
342 ugenopen(dev_t dev, int flag, int mode, struct lwp *l)
343 {
344 struct ugen_softc *sc;
345 int unit = UGENUNIT(dev);
346 int endpt = UGENENDPOINT(dev);
347 usb_endpoint_descriptor_t *edesc;
348 struct ugen_endpoint *sce;
349 int dir, isize;
350 usbd_status err;
351 usbd_xfer_handle xfer;
352 void *tbuf;
353 int i, j;
354
355 USB_GET_SC_OPEN(ugen, unit, sc);
356
357 DPRINTFN(5, ("ugenopen: flag=%d, mode=%d, unit=%d endpt=%d\n",
358 flag, mode, unit, endpt));
359
360 if (sc == NULL || sc->sc_dying)
361 return (ENXIO);
362
363 /* The control endpoint allows multiple opens. */
364 if (endpt == USB_CONTROL_ENDPOINT) {
365 sc->sc_is_open[USB_CONTROL_ENDPOINT] = 1;
366 return (0);
367 }
368
369 if (sc->sc_is_open[endpt])
370 return (EBUSY);
371
372 /* Make sure there are pipes for all directions. */
373 for (dir = OUT; dir <= IN; dir++) {
374 if (flag & (dir == OUT ? FWRITE : FREAD)) {
375 sce = &sc->sc_endpoints[endpt][dir];
376 if (sce == 0 || sce->edesc == 0)
377 return (ENXIO);
378 }
379 }
380
381 /* Actually open the pipes. */
382 /* XXX Should back out properly if it fails. */
383 for (dir = OUT; dir <= IN; dir++) {
384 if (!(flag & (dir == OUT ? FWRITE : FREAD)))
385 continue;
386 sce = &sc->sc_endpoints[endpt][dir];
387 sce->state = 0;
388 sce->timeout = USBD_NO_TIMEOUT;
389 DPRINTFN(5, ("ugenopen: sc=%p, endpt=%d, dir=%d, sce=%p\n",
390 sc, endpt, dir, sce));
391 edesc = sce->edesc;
392 switch (edesc->bmAttributes & UE_XFERTYPE) {
393 case UE_INTERRUPT:
394 if (dir == OUT) {
395 err = usbd_open_pipe(sce->iface,
396 edesc->bEndpointAddress, 0, &sce->pipeh);
397 if (err)
398 return (EIO);
399 break;
400 }
401 isize = UGETW(edesc->wMaxPacketSize);
402 if (isize == 0) /* shouldn't happen */
403 return (EINVAL);
404 sce->ibuf = malloc(isize, M_USBDEV, M_WAITOK);
405 DPRINTFN(5, ("ugenopen: intr endpt=%d,isize=%d\n",
406 endpt, isize));
407 if (clalloc(&sce->q, UGEN_IBSIZE, 0) == -1)
408 return (ENOMEM);
409 err = usbd_open_pipe_intr(sce->iface,
410 edesc->bEndpointAddress,
411 USBD_SHORT_XFER_OK, &sce->pipeh, sce,
412 sce->ibuf, isize, ugenintr,
413 USBD_DEFAULT_INTERVAL);
414 if (err) {
415 free(sce->ibuf, M_USBDEV);
416 clfree(&sce->q);
417 return (EIO);
418 }
419 DPRINTFN(5, ("ugenopen: interrupt open done\n"));
420 break;
421 case UE_BULK:
422 err = usbd_open_pipe(sce->iface,
423 edesc->bEndpointAddress, 0, &sce->pipeh);
424 if (err)
425 return (EIO);
426 #ifdef UGEN_BULK_RA_WB
427 sce->ra_wb_bufsize = UGEN_BULK_RA_WB_BUFSIZE;
428 /*
429 * Use request size for non-RA/WB transfers
430 * as the default.
431 */
432 sce->ra_wb_reqsize = UGEN_BBSIZE;
433 #endif
434 break;
435 case UE_ISOCHRONOUS:
436 if (dir == OUT)
437 return (EINVAL);
438 isize = UGETW(edesc->wMaxPacketSize);
439 if (isize == 0) /* shouldn't happen */
440 return (EINVAL);
441 sce->ibuf = malloc(isize * UGEN_NISOFRAMES,
442 M_USBDEV, M_WAITOK);
443 sce->cur = sce->fill = sce->ibuf;
444 sce->limit = sce->ibuf + isize * UGEN_NISOFRAMES;
445 DPRINTFN(5, ("ugenopen: isoc endpt=%d, isize=%d\n",
446 endpt, isize));
447 err = usbd_open_pipe(sce->iface,
448 edesc->bEndpointAddress, 0, &sce->pipeh);
449 if (err) {
450 free(sce->ibuf, M_USBDEV);
451 return (EIO);
452 }
453 for(i = 0; i < UGEN_NISOREQS; ++i) {
454 sce->isoreqs[i].sce = sce;
455 xfer = usbd_alloc_xfer(sc->sc_udev, sce->pipeh);
456 if (xfer == 0)
457 goto bad;
458 sce->isoreqs[i].xfer = xfer;
459 tbuf = usbd_alloc_buffer
460 (xfer, isize * UGEN_NISORFRMS);
461 if (tbuf == 0) {
462 i++;
463 goto bad;
464 }
465 sce->isoreqs[i].dmabuf = tbuf;
466 for(j = 0; j < UGEN_NISORFRMS; ++j)
467 sce->isoreqs[i].sizes[j] = isize;
468 usbd_setup_isoc_xfer
469 (xfer, sce->pipeh, &sce->isoreqs[i],
470 sce->isoreqs[i].sizes,
471 UGEN_NISORFRMS, USBD_NO_COPY,
472 ugen_isoc_rintr);
473 (void)usbd_transfer(xfer);
474 }
475 DPRINTFN(5, ("ugenopen: isoc open done\n"));
476 break;
477 bad:
478 while (--i >= 0) /* implicit buffer free */
479 usbd_free_xfer(sce->isoreqs[i].xfer);
480 return (ENOMEM);
481 case UE_CONTROL:
482 sce->timeout = USBD_DEFAULT_TIMEOUT;
483 return (EINVAL);
484 }
485 }
486 sc->sc_is_open[endpt] = 1;
487 return (0);
488 }
489
490 int
491 ugenclose(dev_t dev, int flag, int mode, struct lwp *l)
492 {
493 int endpt = UGENENDPOINT(dev);
494 struct ugen_softc *sc;
495 struct ugen_endpoint *sce;
496 int dir;
497 int i;
498
499 USB_GET_SC(ugen, UGENUNIT(dev), sc);
500
501 DPRINTFN(5, ("ugenclose: flag=%d, mode=%d, unit=%d, endpt=%d\n",
502 flag, mode, UGENUNIT(dev), endpt));
503
504 #ifdef DIAGNOSTIC
505 if (!sc->sc_is_open[endpt]) {
506 printf("ugenclose: not open\n");
507 return (EINVAL);
508 }
509 #endif
510
511 if (endpt == USB_CONTROL_ENDPOINT) {
512 DPRINTFN(5, ("ugenclose: close control\n"));
513 sc->sc_is_open[endpt] = 0;
514 return (0);
515 }
516
517 for (dir = OUT; dir <= IN; dir++) {
518 if (!(flag & (dir == OUT ? FWRITE : FREAD)))
519 continue;
520 sce = &sc->sc_endpoints[endpt][dir];
521 if (sce == NULL || sce->pipeh == NULL)
522 continue;
523 DPRINTFN(5, ("ugenclose: endpt=%d dir=%d sce=%p\n",
524 endpt, dir, sce));
525
526 usbd_abort_pipe(sce->pipeh);
527 usbd_close_pipe(sce->pipeh);
528 sce->pipeh = NULL;
529
530 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
531 case UE_INTERRUPT:
532 ndflush(&sce->q, sce->q.c_cc);
533 clfree(&sce->q);
534 break;
535 case UE_ISOCHRONOUS:
536 for (i = 0; i < UGEN_NISOREQS; ++i)
537 usbd_free_xfer(sce->isoreqs[i].xfer);
538 break;
539 #ifdef UGEN_BULK_RA_WB
540 case UE_BULK:
541 if (sce->state & (UGEN_BULK_RA | UGEN_BULK_WB))
542 /* ibuf freed below */
543 usbd_free_xfer(sce->ra_wb_xfer);
544 break;
545 #endif
546 default:
547 break;
548 }
549
550 if (sce->ibuf != NULL) {
551 free(sce->ibuf, M_USBDEV);
552 sce->ibuf = NULL;
553 clfree(&sce->q);
554 }
555 }
556 sc->sc_is_open[endpt] = 0;
557
558 return (0);
559 }
560
561 Static int
562 ugen_do_read(struct ugen_softc *sc, int endpt, struct uio *uio, int flag)
563 {
564 struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][IN];
565 u_int32_t n, tn;
566 usbd_xfer_handle xfer;
567 usbd_status err;
568 int s;
569 int error = 0;
570
571 DPRINTFN(5, ("%s: ugenread: %d\n", USBDEVNAME(sc->sc_dev), endpt));
572
573 if (sc->sc_dying)
574 return (EIO);
575
576 if (endpt == USB_CONTROL_ENDPOINT)
577 return (ENODEV);
578
579 #ifdef DIAGNOSTIC
580 if (sce->edesc == NULL) {
581 printf("ugenread: no edesc\n");
582 return (EIO);
583 }
584 if (sce->pipeh == NULL) {
585 printf("ugenread: no pipe\n");
586 return (EIO);
587 }
588 #endif
589
590 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
591 case UE_INTERRUPT:
592 /* Block until activity occurred. */
593 s = splusb();
594 while (sce->q.c_cc == 0) {
595 if (flag & IO_NDELAY) {
596 splx(s);
597 return (EWOULDBLOCK);
598 }
599 sce->state |= UGEN_ASLP;
600 DPRINTFN(5, ("ugenread: sleep on %p\n", sce));
601 error = tsleep(sce, PZERO | PCATCH, "ugenri", 0);
602 DPRINTFN(5, ("ugenread: woke, error=%d\n", error));
603 if (sc->sc_dying)
604 error = EIO;
605 if (error) {
606 sce->state &= ~UGEN_ASLP;
607 break;
608 }
609 }
610 splx(s);
611
612 /* Transfer as many chunks as possible. */
613 while (sce->q.c_cc > 0 && uio->uio_resid > 0 && !error) {
614 n = min(sce->q.c_cc, uio->uio_resid);
615 if (n > sizeof(sc->sc_buffer))
616 n = sizeof(sc->sc_buffer);
617
618 /* Remove a small chunk from the input queue. */
619 q_to_b(&sce->q, sc->sc_buffer, n);
620 DPRINTFN(5, ("ugenread: got %d chars\n", n));
621
622 /* Copy the data to the user process. */
623 error = uiomove(sc->sc_buffer, n, uio);
624 if (error)
625 break;
626 }
627 break;
628 case UE_BULK:
629 #ifdef UGEN_BULK_RA_WB
630 if (sce->state & UGEN_BULK_RA) {
631 DPRINTFN(5, ("ugenread: BULK_RA req: %zd used: %d\n",
632 uio->uio_resid, sce->ra_wb_used));
633 xfer = sce->ra_wb_xfer;
634
635 s = splusb();
636 if (sce->ra_wb_used == 0 && flag & IO_NDELAY) {
637 splx(s);
638 return (EWOULDBLOCK);
639 }
640 while (uio->uio_resid > 0 && !error) {
641 while (sce->ra_wb_used == 0) {
642 sce->state |= UGEN_ASLP;
643 DPRINTFN(5,
644 ("ugenread: sleep on %p\n",
645 sce));
646 error = tsleep(sce, PZERO | PCATCH,
647 "ugenrb", 0);
648 DPRINTFN(5,
649 ("ugenread: woke, error=%d\n",
650 error));
651 if (sc->sc_dying)
652 error = EIO;
653 if (error) {
654 sce->state &= ~UGEN_ASLP;
655 break;
656 }
657 }
658
659 /* Copy data to the process. */
660 while (uio->uio_resid > 0
661 && sce->ra_wb_used > 0) {
662 n = min(uio->uio_resid,
663 sce->ra_wb_used);
664 n = min(n, sce->limit - sce->cur);
665 error = uiomove(sce->cur, n, uio);
666 if (error)
667 break;
668 sce->cur += n;
669 sce->ra_wb_used -= n;
670 if (sce->cur == sce->limit)
671 sce->cur = sce->ibuf;
672 }
673
674 /*
675 * If the transfers stopped because the
676 * buffer was full, restart them.
677 */
678 if (sce->state & UGEN_RA_WB_STOP &&
679 sce->ra_wb_used < sce->limit - sce->ibuf) {
680 n = (sce->limit - sce->ibuf)
681 - sce->ra_wb_used;
682 usbd_setup_xfer(xfer,
683 sce->pipeh, sce, NULL,
684 min(n, sce->ra_wb_xferlen),
685 USBD_NO_COPY, USBD_NO_TIMEOUT,
686 ugen_bulkra_intr);
687 sce->state &= ~UGEN_RA_WB_STOP;
688 err = usbd_transfer(xfer);
689 if (err != USBD_IN_PROGRESS)
690 /*
691 * The transfer has not been
692 * queued. Setting STOP
693 * will make us try
694 * again at the next read.
695 */
696 sce->state |= UGEN_RA_WB_STOP;
697 }
698 }
699 splx(s);
700 break;
701 }
702 #endif
703 xfer = usbd_alloc_xfer(sc->sc_udev, sce->pipeh);
704 if (xfer == 0)
705 return (ENOMEM);
706 while ((n = min(UGEN_BBSIZE, uio->uio_resid)) != 0) {
707 DPRINTFN(1, ("ugenread: start transfer %d bytes\n",n));
708 tn = n;
709 err = usbd_bulk_transfer(
710 xfer, sce->pipeh,
711 sce->state & UGEN_SHORT_OK ?
712 USBD_SHORT_XFER_OK : 0,
713 sce->timeout, sc->sc_buffer, &tn, "ugenrb");
714 if (err) {
715 if (err == USBD_INTERRUPTED)
716 error = EINTR;
717 else if (err == USBD_TIMEOUT)
718 error = ETIMEDOUT;
719 else
720 error = EIO;
721 break;
722 }
723 DPRINTFN(1, ("ugenread: got %d bytes\n", tn));
724 error = uiomove(sc->sc_buffer, tn, uio);
725 if (error || tn < n)
726 break;
727 }
728 usbd_free_xfer(xfer);
729 break;
730 case UE_ISOCHRONOUS:
731 s = splusb();
732 while (sce->cur == sce->fill) {
733 if (flag & IO_NDELAY) {
734 splx(s);
735 return (EWOULDBLOCK);
736 }
737 sce->state |= UGEN_ASLP;
738 DPRINTFN(5, ("ugenread: sleep on %p\n", sce));
739 error = tsleep(sce, PZERO | PCATCH, "ugenri", 0);
740 DPRINTFN(5, ("ugenread: woke, error=%d\n", error));
741 if (sc->sc_dying)
742 error = EIO;
743 if (error) {
744 sce->state &= ~UGEN_ASLP;
745 break;
746 }
747 }
748
749 while (sce->cur != sce->fill && uio->uio_resid > 0 && !error) {
750 if(sce->fill > sce->cur)
751 n = min(sce->fill - sce->cur, uio->uio_resid);
752 else
753 n = min(sce->limit - sce->cur, uio->uio_resid);
754
755 DPRINTFN(5, ("ugenread: isoc got %d chars\n", n));
756
757 /* Copy the data to the user process. */
758 error = uiomove(sce->cur, n, uio);
759 if (error)
760 break;
761 sce->cur += n;
762 if(sce->cur >= sce->limit)
763 sce->cur = sce->ibuf;
764 }
765 splx(s);
766 break;
767
768
769 default:
770 return (ENXIO);
771 }
772 return (error);
773 }
774
775 int
776 ugenread(dev_t dev, struct uio *uio, int flag)
777 {
778 int endpt = UGENENDPOINT(dev);
779 struct ugen_softc *sc;
780 int error;
781
782 USB_GET_SC(ugen, UGENUNIT(dev), sc);
783
784 sc->sc_refcnt++;
785 error = ugen_do_read(sc, endpt, uio, flag);
786 if (--sc->sc_refcnt < 0)
787 usb_detach_wakeup(USBDEV(sc->sc_dev));
788 return (error);
789 }
790
791 Static int
792 ugen_do_write(struct ugen_softc *sc, int endpt, struct uio *uio,
793 int flag)
794 {
795 struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][OUT];
796 u_int32_t n;
797 int error = 0;
798 #ifdef UGEN_BULK_RA_WB
799 int s;
800 u_int32_t tn;
801 char *dbuf;
802 #endif
803 usbd_xfer_handle xfer;
804 usbd_status err;
805
806 DPRINTFN(5, ("%s: ugenwrite: %d\n", USBDEVNAME(sc->sc_dev), endpt));
807
808 if (sc->sc_dying)
809 return (EIO);
810
811 if (endpt == USB_CONTROL_ENDPOINT)
812 return (ENODEV);
813
814 #ifdef DIAGNOSTIC
815 if (sce->edesc == NULL) {
816 printf("ugenwrite: no edesc\n");
817 return (EIO);
818 }
819 if (sce->pipeh == NULL) {
820 printf("ugenwrite: no pipe\n");
821 return (EIO);
822 }
823 #endif
824
825 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
826 case UE_BULK:
827 #ifdef UGEN_BULK_RA_WB
828 if (sce->state & UGEN_BULK_WB) {
829 DPRINTFN(5, ("ugenwrite: BULK_WB req: %zd used: %d\n",
830 uio->uio_resid, sce->ra_wb_used));
831 xfer = sce->ra_wb_xfer;
832
833 s = splusb();
834 if (sce->ra_wb_used == sce->limit - sce->ibuf &&
835 flag & IO_NDELAY) {
836 splx(s);
837 return (EWOULDBLOCK);
838 }
839 while (uio->uio_resid > 0 && !error) {
840 while (sce->ra_wb_used ==
841 sce->limit - sce->ibuf) {
842 sce->state |= UGEN_ASLP;
843 DPRINTFN(5,
844 ("ugenwrite: sleep on %p\n",
845 sce));
846 error = tsleep(sce, PZERO | PCATCH,
847 "ugenwb", 0);
848 DPRINTFN(5,
849 ("ugenwrite: woke, error=%d\n",
850 error));
851 if (sc->sc_dying)
852 error = EIO;
853 if (error) {
854 sce->state &= ~UGEN_ASLP;
855 break;
856 }
857 }
858
859 /* Copy data from the process. */
860 while (uio->uio_resid > 0 &&
861 sce->ra_wb_used < sce->limit - sce->ibuf) {
862 n = min(uio->uio_resid,
863 (sce->limit - sce->ibuf)
864 - sce->ra_wb_used);
865 n = min(n, sce->limit - sce->fill);
866 error = uiomove(sce->fill, n, uio);
867 if (error)
868 break;
869 sce->fill += n;
870 sce->ra_wb_used += n;
871 if (sce->fill == sce->limit)
872 sce->fill = sce->ibuf;
873 }
874
875 /*
876 * If the transfers stopped because the
877 * buffer was empty, restart them.
878 */
879 if (sce->state & UGEN_RA_WB_STOP &&
880 sce->ra_wb_used > 0) {
881 dbuf = (char *)usbd_get_buffer(xfer);
882 n = min(sce->ra_wb_used,
883 sce->ra_wb_xferlen);
884 tn = min(n, sce->limit - sce->cur);
885 memcpy(dbuf, sce->cur, tn);
886 dbuf += tn;
887 if (n - tn > 0)
888 memcpy(dbuf, sce->ibuf,
889 n - tn);
890 usbd_setup_xfer(xfer,
891 sce->pipeh, sce, NULL, n,
892 USBD_NO_COPY, USBD_NO_TIMEOUT,
893 ugen_bulkwb_intr);
894 sce->state &= ~UGEN_RA_WB_STOP;
895 err = usbd_transfer(xfer);
896 if (err != USBD_IN_PROGRESS)
897 /*
898 * The transfer has not been
899 * queued. Setting STOP
900 * will make us try again
901 * at the next read.
902 */
903 sce->state |= UGEN_RA_WB_STOP;
904 }
905 }
906 splx(s);
907 break;
908 }
909 #endif
910 xfer = usbd_alloc_xfer(sc->sc_udev, sce->pipeh);
911 if (xfer == 0)
912 return (EIO);
913 while ((n = min(UGEN_BBSIZE, uio->uio_resid)) != 0) {
914 error = uiomove(sc->sc_buffer, n, uio);
915 if (error)
916 break;
917 DPRINTFN(1, ("ugenwrite: transfer %d bytes\n", n));
918 err = usbd_bulk_transfer(xfer, sce->pipeh, 0,
919 sce->timeout, sc->sc_buffer, &n,"ugenwb");
920 if (err) {
921 if (err == USBD_INTERRUPTED)
922 error = EINTR;
923 else if (err == USBD_TIMEOUT)
924 error = ETIMEDOUT;
925 else
926 error = EIO;
927 break;
928 }
929 }
930 usbd_free_xfer(xfer);
931 break;
932 case UE_INTERRUPT:
933 xfer = usbd_alloc_xfer(sc->sc_udev, sce->pipeh);
934 if (xfer == 0)
935 return (EIO);
936 while ((n = min(UGETW(sce->edesc->wMaxPacketSize),
937 uio->uio_resid)) != 0) {
938 error = uiomove(sc->sc_buffer, n, uio);
939 if (error)
940 break;
941 DPRINTFN(1, ("ugenwrite: transfer %d bytes\n", n));
942 err = usbd_intr_transfer(xfer, sce->pipeh, 0,
943 sce->timeout, sc->sc_buffer, &n, "ugenwi");
944 if (err) {
945 if (err == USBD_INTERRUPTED)
946 error = EINTR;
947 else if (err == USBD_TIMEOUT)
948 error = ETIMEDOUT;
949 else
950 error = EIO;
951 break;
952 }
953 }
954 usbd_free_xfer(xfer);
955 break;
956 default:
957 return (ENXIO);
958 }
959 return (error);
960 }
961
962 int
963 ugenwrite(dev_t dev, struct uio *uio, int flag)
964 {
965 int endpt = UGENENDPOINT(dev);
966 struct ugen_softc *sc;
967 int error;
968
969 USB_GET_SC(ugen, UGENUNIT(dev), sc);
970
971 sc->sc_refcnt++;
972 error = ugen_do_write(sc, endpt, uio, flag);
973 if (--sc->sc_refcnt < 0)
974 usb_detach_wakeup(USBDEV(sc->sc_dev));
975 return (error);
976 }
977
978 #if defined(__NetBSD__) || defined(__OpenBSD__)
979 int
980 ugen_activate(device_ptr_t self, enum devact act)
981 {
982 struct ugen_softc *sc = (struct ugen_softc *)self;
983
984 switch (act) {
985 case DVACT_ACTIVATE:
986 return (EOPNOTSUPP);
987
988 case DVACT_DEACTIVATE:
989 sc->sc_dying = 1;
990 break;
991 }
992 return (0);
993 }
994 #endif
995
996 USB_DETACH(ugen)
997 {
998 USB_DETACH_START(ugen, sc);
999 struct ugen_endpoint *sce;
1000 int i, dir;
1001 int s;
1002 #if defined(__NetBSD__) || defined(__OpenBSD__)
1003 int maj, mn;
1004
1005 DPRINTF(("ugen_detach: sc=%p flags=%d\n", sc, flags));
1006 #elif defined(__FreeBSD__)
1007 DPRINTF(("ugen_detach: sc=%p\n", sc));
1008 #endif
1009
1010 sc->sc_dying = 1;
1011 /* Abort all pipes. Causes processes waiting for transfer to wake. */
1012 for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
1013 for (dir = OUT; dir <= IN; dir++) {
1014 sce = &sc->sc_endpoints[i][dir];
1015 if (sce && sce->pipeh)
1016 usbd_abort_pipe(sce->pipeh);
1017 }
1018 }
1019
1020 s = splusb();
1021 if (--sc->sc_refcnt >= 0) {
1022 /* Wake everyone */
1023 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
1024 wakeup(&sc->sc_endpoints[i][IN]);
1025 /* Wait for processes to go away. */
1026 usb_detach_wait(USBDEV(sc->sc_dev));
1027 }
1028 splx(s);
1029
1030 #if defined(__NetBSD__) || defined(__OpenBSD__)
1031 /* locate the major number */
1032 #if defined(__NetBSD__)
1033 maj = cdevsw_lookup_major(&ugen_cdevsw);
1034 #elif defined(__OpenBSD__)
1035 for (maj = 0; maj < nchrdev; maj++)
1036 if (cdevsw[maj].d_open == ugenopen)
1037 break;
1038 #endif
1039
1040 /* Nuke the vnodes for any open instances (calls close). */
1041 mn = device_unit(self) * USB_MAX_ENDPOINTS;
1042 vdevgone(maj, mn, mn + USB_MAX_ENDPOINTS - 1, VCHR);
1043 #elif defined(__FreeBSD__)
1044 /* XXX not implemented yet */
1045 #endif
1046
1047 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
1048 USBDEV(sc->sc_dev));
1049
1050 return (0);
1051 }
1052
1053 Static void
1054 ugenintr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
1055 {
1056 struct ugen_endpoint *sce = addr;
1057 /*struct ugen_softc *sc = sce->sc;*/
1058 u_int32_t count;
1059 u_char *ibuf;
1060
1061 if (status == USBD_CANCELLED)
1062 return;
1063
1064 if (status != USBD_NORMAL_COMPLETION) {
1065 DPRINTF(("ugenintr: status=%d\n", status));
1066 if (status == USBD_STALLED)
1067 usbd_clear_endpoint_stall_async(sce->pipeh);
1068 return;
1069 }
1070
1071 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
1072 ibuf = sce->ibuf;
1073
1074 DPRINTFN(5, ("ugenintr: xfer=%p status=%d count=%d\n",
1075 xfer, status, count));
1076 DPRINTFN(5, (" data = %02x %02x %02x\n",
1077 ibuf[0], ibuf[1], ibuf[2]));
1078
1079 (void)b_to_q(ibuf, count, &sce->q);
1080
1081 if (sce->state & UGEN_ASLP) {
1082 sce->state &= ~UGEN_ASLP;
1083 DPRINTFN(5, ("ugen_intr: waking %p\n", sce));
1084 wakeup(sce);
1085 }
1086 selnotify(&sce->rsel, 0);
1087 }
1088
1089 Static void
1090 ugen_isoc_rintr(usbd_xfer_handle xfer, usbd_private_handle addr,
1091 usbd_status status)
1092 {
1093 struct isoreq *req = addr;
1094 struct ugen_endpoint *sce = req->sce;
1095 u_int32_t count, n;
1096 int i, isize;
1097
1098 /* Return if we are aborting. */
1099 if (status == USBD_CANCELLED)
1100 return;
1101
1102 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
1103 DPRINTFN(5,("ugen_isoc_rintr: xfer %ld, count=%d\n",
1104 (long)(req - sce->isoreqs), count));
1105
1106 /* throw away oldest input if the buffer is full */
1107 if(sce->fill < sce->cur && sce->cur <= sce->fill + count) {
1108 sce->cur += count;
1109 if(sce->cur >= sce->limit)
1110 sce->cur = sce->ibuf + (sce->limit - sce->cur);
1111 DPRINTFN(5, ("ugen_isoc_rintr: throwing away %d bytes\n",
1112 count));
1113 }
1114
1115 isize = UGETW(sce->edesc->wMaxPacketSize);
1116 for (i = 0; i < UGEN_NISORFRMS; i++) {
1117 u_int32_t actlen = req->sizes[i];
1118 char const *tbuf = (char const *)req->dmabuf + isize * i;
1119
1120 /* copy data to buffer */
1121 while (actlen > 0) {
1122 n = min(actlen, sce->limit - sce->fill);
1123 memcpy(sce->fill, tbuf, n);
1124
1125 tbuf += n;
1126 actlen -= n;
1127 sce->fill += n;
1128 if(sce->fill == sce->limit)
1129 sce->fill = sce->ibuf;
1130 }
1131
1132 /* setup size for next transfer */
1133 req->sizes[i] = isize;
1134 }
1135
1136 usbd_setup_isoc_xfer(xfer, sce->pipeh, req, req->sizes, UGEN_NISORFRMS,
1137 USBD_NO_COPY, ugen_isoc_rintr);
1138 (void)usbd_transfer(xfer);
1139
1140 if (sce->state & UGEN_ASLP) {
1141 sce->state &= ~UGEN_ASLP;
1142 DPRINTFN(5, ("ugen_isoc_rintr: waking %p\n", sce));
1143 wakeup(sce);
1144 }
1145 selnotify(&sce->rsel, 0);
1146 }
1147
1148 #ifdef UGEN_BULK_RA_WB
1149 Static void
1150 ugen_bulkra_intr(usbd_xfer_handle xfer, usbd_private_handle addr,
1151 usbd_status status)
1152 {
1153 struct ugen_endpoint *sce = addr;
1154 u_int32_t count, n;
1155 char const *tbuf;
1156 usbd_status err;
1157
1158 /* Return if we are aborting. */
1159 if (status == USBD_CANCELLED)
1160 return;
1161
1162 if (status != USBD_NORMAL_COMPLETION) {
1163 DPRINTF(("ugen_bulkra_intr: status=%d\n", status));
1164 sce->state |= UGEN_RA_WB_STOP;
1165 if (status == USBD_STALLED)
1166 usbd_clear_endpoint_stall_async(sce->pipeh);
1167 return;
1168 }
1169
1170 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
1171
1172 /* Keep track of how much is in the buffer. */
1173 sce->ra_wb_used += count;
1174
1175 /* Copy data to buffer. */
1176 tbuf = (char const *)usbd_get_buffer(sce->ra_wb_xfer);
1177 n = min(count, sce->limit - sce->fill);
1178 memcpy(sce->fill, tbuf, n);
1179 tbuf += n;
1180 count -= n;
1181 sce->fill += n;
1182 if (sce->fill == sce->limit)
1183 sce->fill = sce->ibuf;
1184 if (count > 0) {
1185 memcpy(sce->fill, tbuf, count);
1186 sce->fill += count;
1187 }
1188
1189 /* Set up the next request if necessary. */
1190 n = (sce->limit - sce->ibuf) - sce->ra_wb_used;
1191 if (n > 0) {
1192 usbd_setup_xfer(xfer, sce->pipeh, sce, NULL,
1193 min(n, sce->ra_wb_xferlen), USBD_NO_COPY,
1194 USBD_NO_TIMEOUT, ugen_bulkra_intr);
1195 err = usbd_transfer(xfer);
1196 if (err != USBD_IN_PROGRESS) {
1197 printf("usbd_bulkra_intr: error=%d\n", err);
1198 /*
1199 * The transfer has not been queued. Setting STOP
1200 * will make us try again at the next read.
1201 */
1202 sce->state |= UGEN_RA_WB_STOP;
1203 }
1204 }
1205 else
1206 sce->state |= UGEN_RA_WB_STOP;
1207
1208 if (sce->state & UGEN_ASLP) {
1209 sce->state &= ~UGEN_ASLP;
1210 DPRINTFN(5, ("ugen_bulkra_intr: waking %p\n", sce));
1211 wakeup(sce);
1212 }
1213 selnotify(&sce->rsel, 0);
1214 }
1215
1216 Static void
1217 ugen_bulkwb_intr(usbd_xfer_handle xfer, usbd_private_handle addr,
1218 usbd_status status)
1219 {
1220 struct ugen_endpoint *sce = addr;
1221 u_int32_t count, n;
1222 char *tbuf;
1223 usbd_status err;
1224
1225 /* Return if we are aborting. */
1226 if (status == USBD_CANCELLED)
1227 return;
1228
1229 if (status != USBD_NORMAL_COMPLETION) {
1230 DPRINTF(("ugen_bulkwb_intr: status=%d\n", status));
1231 sce->state |= UGEN_RA_WB_STOP;
1232 if (status == USBD_STALLED)
1233 usbd_clear_endpoint_stall_async(sce->pipeh);
1234 return;
1235 }
1236
1237 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
1238
1239 /* Keep track of how much is in the buffer. */
1240 sce->ra_wb_used -= count;
1241
1242 /* Update buffer pointers. */
1243 sce->cur += count;
1244 if (sce->cur >= sce->limit)
1245 sce->cur = sce->ibuf + (sce->cur - sce->limit);
1246
1247 /* Set up next request if necessary. */
1248 if (sce->ra_wb_used > 0) {
1249 /* copy data from buffer */
1250 tbuf = (char *)usbd_get_buffer(sce->ra_wb_xfer);
1251 count = min(sce->ra_wb_used, sce->ra_wb_xferlen);
1252 n = min(count, sce->limit - sce->cur);
1253 memcpy(tbuf, sce->cur, n);
1254 tbuf += n;
1255 if (count - n > 0)
1256 memcpy(tbuf, sce->ibuf, count - n);
1257
1258 usbd_setup_xfer(xfer, sce->pipeh, sce, NULL,
1259 count, USBD_NO_COPY, USBD_NO_TIMEOUT, ugen_bulkwb_intr);
1260 err = usbd_transfer(xfer);
1261 if (err != USBD_IN_PROGRESS) {
1262 printf("usbd_bulkwb_intr: error=%d\n", err);
1263 /*
1264 * The transfer has not been queued. Setting STOP
1265 * will make us try again at the next write.
1266 */
1267 sce->state |= UGEN_RA_WB_STOP;
1268 }
1269 }
1270 else
1271 sce->state |= UGEN_RA_WB_STOP;
1272
1273 if (sce->state & UGEN_ASLP) {
1274 sce->state &= ~UGEN_ASLP;
1275 DPRINTFN(5, ("ugen_bulkwb_intr: waking %p\n", sce));
1276 wakeup(sce);
1277 }
1278 selnotify(&sce->rsel, 0);
1279 }
1280 #endif
1281
1282 Static usbd_status
1283 ugen_set_interface(struct ugen_softc *sc, int ifaceidx, int altno)
1284 {
1285 usbd_interface_handle iface;
1286 usb_endpoint_descriptor_t *ed;
1287 usbd_status err;
1288 struct ugen_endpoint *sce;
1289 u_int8_t niface, nendpt, endptno, endpt;
1290 int dir;
1291
1292 DPRINTFN(15, ("ugen_set_interface %d %d\n", ifaceidx, altno));
1293
1294 err = usbd_interface_count(sc->sc_udev, &niface);
1295 if (err)
1296 return (err);
1297 if (ifaceidx < 0 || ifaceidx >= niface)
1298 return (USBD_INVAL);
1299
1300 err = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface);
1301 if (err)
1302 return (err);
1303 err = usbd_endpoint_count(iface, &nendpt);
1304 if (err)
1305 return (err);
1306 /* XXX should only do this after setting new altno has succeeded */
1307 for (endptno = 0; endptno < nendpt; endptno++) {
1308 ed = usbd_interface2endpoint_descriptor(iface,endptno);
1309 endpt = ed->bEndpointAddress;
1310 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
1311 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
1312 sce->sc = 0;
1313 sce->edesc = 0;
1314 sce->iface = 0;
1315 }
1316
1317 /* change setting */
1318 err = usbd_set_interface(iface, altno);
1319 if (err)
1320 return (err);
1321
1322 err = usbd_endpoint_count(iface, &nendpt);
1323 if (err)
1324 return (err);
1325 for (endptno = 0; endptno < nendpt; endptno++) {
1326 ed = usbd_interface2endpoint_descriptor(iface,endptno);
1327 KASSERT(ed != NULL);
1328 endpt = ed->bEndpointAddress;
1329 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
1330 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
1331 sce->sc = sc;
1332 sce->edesc = ed;
1333 sce->iface = iface;
1334 }
1335 return (0);
1336 }
1337
1338 /* Retrieve a complete descriptor for a certain device and index. */
1339 Static usb_config_descriptor_t *
1340 ugen_get_cdesc(struct ugen_softc *sc, int index, int *lenp)
1341 {
1342 usb_config_descriptor_t *cdesc, *tdesc, cdescr;
1343 int len;
1344 usbd_status err;
1345
1346 if (index == USB_CURRENT_CONFIG_INDEX) {
1347 tdesc = usbd_get_config_descriptor(sc->sc_udev);
1348 len = UGETW(tdesc->wTotalLength);
1349 if (lenp)
1350 *lenp = len;
1351 cdesc = malloc(len, M_TEMP, M_WAITOK);
1352 memcpy(cdesc, tdesc, len);
1353 DPRINTFN(5,("ugen_get_cdesc: current, len=%d\n", len));
1354 } else {
1355 err = usbd_get_config_desc(sc->sc_udev, index, &cdescr);
1356 if (err)
1357 return (0);
1358 len = UGETW(cdescr.wTotalLength);
1359 DPRINTFN(5,("ugen_get_cdesc: index=%d, len=%d\n", index, len));
1360 if (lenp)
1361 *lenp = len;
1362 cdesc = malloc(len, M_TEMP, M_WAITOK);
1363 err = usbd_get_config_desc_full(sc->sc_udev, index, cdesc, len);
1364 if (err) {
1365 free(cdesc, M_TEMP);
1366 return (0);
1367 }
1368 }
1369 return (cdesc);
1370 }
1371
1372 Static int
1373 ugen_get_alt_index(struct ugen_softc *sc, int ifaceidx)
1374 {
1375 usbd_interface_handle iface;
1376 usbd_status err;
1377
1378 err = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface);
1379 if (err)
1380 return (-1);
1381 return (usbd_get_interface_altindex(iface));
1382 }
1383
1384 Static int
1385 ugen_do_ioctl(struct ugen_softc *sc, int endpt, u_long cmd,
1386 caddr_t addr, int flag, struct lwp *l)
1387 {
1388 struct ugen_endpoint *sce;
1389 usbd_status err;
1390 usbd_interface_handle iface;
1391 struct usb_config_desc *cd;
1392 usb_config_descriptor_t *cdesc;
1393 struct usb_interface_desc *id;
1394 usb_interface_descriptor_t *idesc;
1395 struct usb_endpoint_desc *ed;
1396 usb_endpoint_descriptor_t *edesc;
1397 struct usb_alt_interface *ai;
1398 struct usb_string_desc *si;
1399 u_int8_t conf, alt;
1400
1401 DPRINTFN(5, ("ugenioctl: cmd=%08lx\n", cmd));
1402 if (sc->sc_dying)
1403 return (EIO);
1404
1405 switch (cmd) {
1406 case FIONBIO:
1407 /* All handled in the upper FS layer. */
1408 return (0);
1409 case USB_SET_SHORT_XFER:
1410 if (endpt == USB_CONTROL_ENDPOINT)
1411 return (EINVAL);
1412 /* This flag only affects read */
1413 sce = &sc->sc_endpoints[endpt][IN];
1414 if (sce == NULL || sce->pipeh == NULL)
1415 return (EINVAL);
1416 if (*(int *)addr)
1417 sce->state |= UGEN_SHORT_OK;
1418 else
1419 sce->state &= ~UGEN_SHORT_OK;
1420 return (0);
1421 case USB_SET_TIMEOUT:
1422 sce = &sc->sc_endpoints[endpt][IN];
1423 if (sce == NULL
1424 /* XXX this shouldn't happen, but the distinction between
1425 input and output pipes isn't clear enough.
1426 || sce->pipeh == NULL */
1427 )
1428 return (EINVAL);
1429 sce->timeout = *(int *)addr;
1430 return (0);
1431 case USB_SET_BULK_RA:
1432 #ifdef UGEN_BULK_RA_WB
1433 if (endpt == USB_CONTROL_ENDPOINT)
1434 return (EINVAL);
1435 sce = &sc->sc_endpoints[endpt][IN];
1436 if (sce == NULL || sce->pipeh == NULL)
1437 return (EINVAL);
1438 edesc = sce->edesc;
1439 if ((edesc->bmAttributes & UE_XFERTYPE) != UE_BULK)
1440 return (EINVAL);
1441
1442 if (*(int *)addr) {
1443 /* Only turn RA on if it's currently off. */
1444 if (sce->state & UGEN_BULK_RA)
1445 return (0);
1446
1447 if (sce->ra_wb_bufsize == 0 || sce->ra_wb_reqsize == 0)
1448 /* shouldn't happen */
1449 return (EINVAL);
1450 sce->ra_wb_xfer = usbd_alloc_xfer(sc->sc_udev,
1451 sce->pipeh);
1452 if (sce->ra_wb_xfer == NULL)
1453 return (ENOMEM);
1454 sce->ra_wb_xferlen = sce->ra_wb_reqsize;
1455 /*
1456 * Set up a dmabuf because we reuse the xfer with
1457 * the same (max) request length like isoc.
1458 */
1459 if (usbd_alloc_buffer(sce->ra_wb_xfer,
1460 sce->ra_wb_xferlen) == 0) {
1461 usbd_free_xfer(sce->ra_wb_xfer);
1462 return (ENOMEM);
1463 }
1464 sce->ibuf = malloc(sce->ra_wb_bufsize,
1465 M_USBDEV, M_WAITOK);
1466 sce->fill = sce->cur = sce->ibuf;
1467 sce->limit = sce->ibuf + sce->ra_wb_bufsize;
1468 sce->ra_wb_used = 0;
1469 sce->state |= UGEN_BULK_RA;
1470 sce->state &= ~UGEN_RA_WB_STOP;
1471 /* Now start reading. */
1472 usbd_setup_xfer(sce->ra_wb_xfer, sce->pipeh, sce,
1473 NULL,
1474 min(sce->ra_wb_xferlen, sce->ra_wb_bufsize),
1475 USBD_NO_COPY, USBD_NO_TIMEOUT,
1476 ugen_bulkra_intr);
1477 err = usbd_transfer(sce->ra_wb_xfer);
1478 if (err != USBD_IN_PROGRESS) {
1479 sce->state &= ~UGEN_BULK_RA;
1480 free(sce->ibuf, M_USBDEV);
1481 sce->ibuf = NULL;
1482 usbd_free_xfer(sce->ra_wb_xfer);
1483 return (EIO);
1484 }
1485 } else {
1486 /* Only turn RA off if it's currently on. */
1487 if (!(sce->state & UGEN_BULK_RA))
1488 return (0);
1489
1490 sce->state &= ~UGEN_BULK_RA;
1491 usbd_abort_pipe(sce->pipeh);
1492 usbd_free_xfer(sce->ra_wb_xfer);
1493 /*
1494 * XXX Discard whatever's in the buffer, but we
1495 * should keep it around and drain the buffer
1496 * instead.
1497 */
1498 free(sce->ibuf, M_USBDEV);
1499 sce->ibuf = NULL;
1500 }
1501 return (0);
1502 #else
1503 return (EOPNOTSUPP);
1504 #endif
1505 case USB_SET_BULK_WB:
1506 #ifdef UGEN_BULK_RA_WB
1507 if (endpt == USB_CONTROL_ENDPOINT)
1508 return (EINVAL);
1509 sce = &sc->sc_endpoints[endpt][OUT];
1510 if (sce == NULL || sce->pipeh == NULL)
1511 return (EINVAL);
1512 edesc = sce->edesc;
1513 if ((edesc->bmAttributes & UE_XFERTYPE) != UE_BULK)
1514 return (EINVAL);
1515
1516 if (*(int *)addr) {
1517 /* Only turn WB on if it's currently off. */
1518 if (sce->state & UGEN_BULK_WB)
1519 return (0);
1520
1521 if (sce->ra_wb_bufsize == 0 || sce->ra_wb_reqsize == 0)
1522 /* shouldn't happen */
1523 return (EINVAL);
1524 sce->ra_wb_xfer = usbd_alloc_xfer(sc->sc_udev,
1525 sce->pipeh);
1526 if (sce->ra_wb_xfer == NULL)
1527 return (ENOMEM);
1528 sce->ra_wb_xferlen = sce->ra_wb_reqsize;
1529 /*
1530 * Set up a dmabuf because we reuse the xfer with
1531 * the same (max) request length like isoc.
1532 */
1533 if (usbd_alloc_buffer(sce->ra_wb_xfer,
1534 sce->ra_wb_xferlen) == 0) {
1535 usbd_free_xfer(sce->ra_wb_xfer);
1536 return (ENOMEM);
1537 }
1538 sce->ibuf = malloc(sce->ra_wb_bufsize,
1539 M_USBDEV, M_WAITOK);
1540 sce->fill = sce->cur = sce->ibuf;
1541 sce->limit = sce->ibuf + sce->ra_wb_bufsize;
1542 sce->ra_wb_used = 0;
1543 sce->state |= UGEN_BULK_WB | UGEN_RA_WB_STOP;
1544 } else {
1545 /* Only turn WB off if it's currently on. */
1546 if (!(sce->state & UGEN_BULK_WB))
1547 return (0);
1548
1549 sce->state &= ~UGEN_BULK_WB;
1550 /*
1551 * XXX Discard whatever's in the buffer, but we
1552 * should keep it around and keep writing to
1553 * drain the buffer instead.
1554 */
1555 usbd_abort_pipe(sce->pipeh);
1556 usbd_free_xfer(sce->ra_wb_xfer);
1557 free(sce->ibuf, M_USBDEV);
1558 sce->ibuf = NULL;
1559 }
1560 return (0);
1561 #else
1562 return (EOPNOTSUPP);
1563 #endif
1564 case USB_SET_BULK_RA_OPT:
1565 case USB_SET_BULK_WB_OPT:
1566 #ifdef UGEN_BULK_RA_WB
1567 {
1568 struct usb_bulk_ra_wb_opt *opt;
1569
1570 if (endpt == USB_CONTROL_ENDPOINT)
1571 return (EINVAL);
1572 opt = (struct usb_bulk_ra_wb_opt *)addr;
1573 if (cmd == USB_SET_BULK_RA_OPT)
1574 sce = &sc->sc_endpoints[endpt][IN];
1575 else
1576 sce = &sc->sc_endpoints[endpt][OUT];
1577 if (sce == NULL || sce->pipeh == NULL)
1578 return (EINVAL);
1579 if (opt->ra_wb_buffer_size < 1 ||
1580 opt->ra_wb_buffer_size > UGEN_BULK_RA_WB_BUFMAX ||
1581 opt->ra_wb_request_size < 1 ||
1582 opt->ra_wb_request_size > opt->ra_wb_buffer_size)
1583 return (EINVAL);
1584 /*
1585 * XXX These changes do not take effect until the
1586 * next time RA/WB mode is enabled but they ought to
1587 * take effect immediately.
1588 */
1589 sce->ra_wb_bufsize = opt->ra_wb_buffer_size;
1590 sce->ra_wb_reqsize = opt->ra_wb_request_size;
1591 return (0);
1592 }
1593 #else
1594 return (EOPNOTSUPP);
1595 #endif
1596 default:
1597 break;
1598 }
1599
1600 if (endpt != USB_CONTROL_ENDPOINT)
1601 return (EINVAL);
1602
1603 switch (cmd) {
1604 #ifdef UGEN_DEBUG
1605 case USB_SETDEBUG:
1606 ugendebug = *(int *)addr;
1607 break;
1608 #endif
1609 case USB_GET_CONFIG:
1610 err = usbd_get_config(sc->sc_udev, &conf);
1611 if (err)
1612 return (EIO);
1613 *(int *)addr = conf;
1614 break;
1615 case USB_SET_CONFIG:
1616 if (!(flag & FWRITE))
1617 return (EPERM);
1618 err = ugen_set_config(sc, *(int *)addr);
1619 switch (err) {
1620 case USBD_NORMAL_COMPLETION:
1621 break;
1622 case USBD_IN_USE:
1623 return (EBUSY);
1624 default:
1625 return (EIO);
1626 }
1627 break;
1628 case USB_GET_ALTINTERFACE:
1629 ai = (struct usb_alt_interface *)addr;
1630 err = usbd_device2interface_handle(sc->sc_udev,
1631 ai->uai_interface_index, &iface);
1632 if (err)
1633 return (EINVAL);
1634 idesc = usbd_get_interface_descriptor(iface);
1635 if (idesc == NULL)
1636 return (EIO);
1637 ai->uai_alt_no = idesc->bAlternateSetting;
1638 break;
1639 case USB_SET_ALTINTERFACE:
1640 if (!(flag & FWRITE))
1641 return (EPERM);
1642 ai = (struct usb_alt_interface *)addr;
1643 err = usbd_device2interface_handle(sc->sc_udev,
1644 ai->uai_interface_index, &iface);
1645 if (err)
1646 return (EINVAL);
1647 err = ugen_set_interface(sc, ai->uai_interface_index,
1648 ai->uai_alt_no);
1649 if (err)
1650 return (EINVAL);
1651 break;
1652 case USB_GET_NO_ALT:
1653 ai = (struct usb_alt_interface *)addr;
1654 cdesc = ugen_get_cdesc(sc, ai->uai_config_index, 0);
1655 if (cdesc == NULL)
1656 return (EINVAL);
1657 idesc = usbd_find_idesc(cdesc, ai->uai_interface_index, 0);
1658 if (idesc == NULL) {
1659 free(cdesc, M_TEMP);
1660 return (EINVAL);
1661 }
1662 ai->uai_alt_no = usbd_get_no_alts(cdesc,
1663 idesc->bInterfaceNumber);
1664 free(cdesc, M_TEMP);
1665 break;
1666 case USB_GET_DEVICE_DESC:
1667 *(usb_device_descriptor_t *)addr =
1668 *usbd_get_device_descriptor(sc->sc_udev);
1669 break;
1670 case USB_GET_CONFIG_DESC:
1671 cd = (struct usb_config_desc *)addr;
1672 cdesc = ugen_get_cdesc(sc, cd->ucd_config_index, 0);
1673 if (cdesc == NULL)
1674 return (EINVAL);
1675 cd->ucd_desc = *cdesc;
1676 free(cdesc, M_TEMP);
1677 break;
1678 case USB_GET_INTERFACE_DESC:
1679 id = (struct usb_interface_desc *)addr;
1680 cdesc = ugen_get_cdesc(sc, id->uid_config_index, 0);
1681 if (cdesc == NULL)
1682 return (EINVAL);
1683 if (id->uid_config_index == USB_CURRENT_CONFIG_INDEX &&
1684 id->uid_alt_index == USB_CURRENT_ALT_INDEX)
1685 alt = ugen_get_alt_index(sc, id->uid_interface_index);
1686 else
1687 alt = id->uid_alt_index;
1688 idesc = usbd_find_idesc(cdesc, id->uid_interface_index, alt);
1689 if (idesc == NULL) {
1690 free(cdesc, M_TEMP);
1691 return (EINVAL);
1692 }
1693 id->uid_desc = *idesc;
1694 free(cdesc, M_TEMP);
1695 break;
1696 case USB_GET_ENDPOINT_DESC:
1697 ed = (struct usb_endpoint_desc *)addr;
1698 cdesc = ugen_get_cdesc(sc, ed->ued_config_index, 0);
1699 if (cdesc == NULL)
1700 return (EINVAL);
1701 if (ed->ued_config_index == USB_CURRENT_CONFIG_INDEX &&
1702 ed->ued_alt_index == USB_CURRENT_ALT_INDEX)
1703 alt = ugen_get_alt_index(sc, ed->ued_interface_index);
1704 else
1705 alt = ed->ued_alt_index;
1706 edesc = usbd_find_edesc(cdesc, ed->ued_interface_index,
1707 alt, ed->ued_endpoint_index);
1708 if (edesc == NULL) {
1709 free(cdesc, M_TEMP);
1710 return (EINVAL);
1711 }
1712 ed->ued_desc = *edesc;
1713 free(cdesc, M_TEMP);
1714 break;
1715 case USB_GET_FULL_DESC:
1716 {
1717 int len;
1718 struct iovec iov;
1719 struct uio uio;
1720 struct usb_full_desc *fd = (struct usb_full_desc *)addr;
1721 int error;
1722
1723 cdesc = ugen_get_cdesc(sc, fd->ufd_config_index, &len);
1724 if (len > fd->ufd_size)
1725 len = fd->ufd_size;
1726 iov.iov_base = (caddr_t)fd->ufd_data;
1727 iov.iov_len = len;
1728 uio.uio_iov = &iov;
1729 uio.uio_iovcnt = 1;
1730 uio.uio_resid = len;
1731 uio.uio_offset = 0;
1732 uio.uio_rw = UIO_READ;
1733 uio.uio_vmspace = l->l_proc->p_vmspace;
1734 error = uiomove((void *)cdesc, len, &uio);
1735 free(cdesc, M_TEMP);
1736 return (error);
1737 }
1738 case USB_GET_STRING_DESC: {
1739 int len;
1740 si = (struct usb_string_desc *)addr;
1741 err = usbd_get_string_desc(sc->sc_udev, si->usd_string_index,
1742 si->usd_language_id, &si->usd_desc, &len);
1743 if (err)
1744 return (EINVAL);
1745 break;
1746 }
1747 case USB_DO_REQUEST:
1748 {
1749 struct usb_ctl_request *ur = (void *)addr;
1750 int len = UGETW(ur->ucr_request.wLength);
1751 struct iovec iov;
1752 struct uio uio;
1753 void *ptr = 0;
1754 usbd_status xerr;
1755 int error = 0;
1756
1757 if (!(flag & FWRITE))
1758 return (EPERM);
1759 /* Avoid requests that would damage the bus integrity. */
1760 if ((ur->ucr_request.bmRequestType == UT_WRITE_DEVICE &&
1761 ur->ucr_request.bRequest == UR_SET_ADDRESS) ||
1762 (ur->ucr_request.bmRequestType == UT_WRITE_DEVICE &&
1763 ur->ucr_request.bRequest == UR_SET_CONFIG) ||
1764 (ur->ucr_request.bmRequestType == UT_WRITE_INTERFACE &&
1765 ur->ucr_request.bRequest == UR_SET_INTERFACE))
1766 return (EINVAL);
1767
1768 if (len < 0 || len > 32767)
1769 return (EINVAL);
1770 if (len != 0) {
1771 iov.iov_base = (caddr_t)ur->ucr_data;
1772 iov.iov_len = len;
1773 uio.uio_iov = &iov;
1774 uio.uio_iovcnt = 1;
1775 uio.uio_resid = len;
1776 uio.uio_offset = 0;
1777 uio.uio_rw =
1778 ur->ucr_request.bmRequestType & UT_READ ?
1779 UIO_READ : UIO_WRITE;
1780 uio.uio_vmspace = l->l_proc->p_vmspace;
1781 ptr = malloc(len, M_TEMP, M_WAITOK);
1782 if (uio.uio_rw == UIO_WRITE) {
1783 error = uiomove(ptr, len, &uio);
1784 if (error)
1785 goto ret;
1786 }
1787 }
1788 sce = &sc->sc_endpoints[endpt][IN];
1789 xerr = usbd_do_request_flags(sc->sc_udev, &ur->ucr_request,
1790 ptr, ur->ucr_flags, &ur->ucr_actlen, sce->timeout);
1791 if (xerr) {
1792 error = EIO;
1793 goto ret;
1794 }
1795 if (len != 0) {
1796 if (uio.uio_rw == UIO_READ) {
1797 error = uiomove(ptr, len, &uio);
1798 if (error)
1799 goto ret;
1800 }
1801 }
1802 ret:
1803 if (ptr)
1804 free(ptr, M_TEMP);
1805 return (error);
1806 }
1807 case USB_GET_DEVICEINFO:
1808 usbd_fill_deviceinfo(sc->sc_udev,
1809 (struct usb_device_info *)addr, 0);
1810 break;
1811 #ifdef COMPAT_30
1812 case USB_GET_DEVICEINFO_OLD:
1813 usbd_fill_deviceinfo_old(sc->sc_udev,
1814 (struct usb_device_info_old *)addr, 0);
1815
1816 break;
1817 #endif
1818 default:
1819 return (EINVAL);
1820 }
1821 return (0);
1822 }
1823
1824 int
1825 ugenioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct lwp *l)
1826 {
1827 int endpt = UGENENDPOINT(dev);
1828 struct ugen_softc *sc;
1829 int error;
1830
1831 USB_GET_SC(ugen, UGENUNIT(dev), sc);
1832
1833 sc->sc_refcnt++;
1834 error = ugen_do_ioctl(sc, endpt, cmd, addr, flag, l);
1835 if (--sc->sc_refcnt < 0)
1836 usb_detach_wakeup(USBDEV(sc->sc_dev));
1837 return (error);
1838 }
1839
1840 int
1841 ugenpoll(dev_t dev, int events, struct lwp *l)
1842 {
1843 struct ugen_softc *sc;
1844 struct ugen_endpoint *sce_in, *sce_out;
1845 int revents = 0;
1846 int s;
1847
1848 USB_GET_SC(ugen, UGENUNIT(dev), sc);
1849
1850 if (sc->sc_dying)
1851 return (POLLHUP);
1852
1853 sce_in = &sc->sc_endpoints[UGENENDPOINT(dev)][IN];
1854 sce_out = &sc->sc_endpoints[UGENENDPOINT(dev)][OUT];
1855 if (sce_in == NULL && sce_out == NULL)
1856 return (POLLERR);
1857 #ifdef DIAGNOSTIC
1858 if (!sce_in->edesc && !sce_out->edesc) {
1859 printf("ugenpoll: no edesc\n");
1860 return (POLLERR);
1861 }
1862 /* It's possible to have only one pipe open. */
1863 if (!sce_in->pipeh && !sce_out->pipeh) {
1864 printf("ugenpoll: no pipe\n");
1865 return (POLLERR);
1866 }
1867 #endif
1868 s = splusb();
1869 if (sce_in && sce_in->pipeh && (events & (POLLIN | POLLRDNORM)))
1870 switch (sce_in->edesc->bmAttributes & UE_XFERTYPE) {
1871 case UE_INTERRUPT:
1872 if (sce_in->q.c_cc > 0)
1873 revents |= events & (POLLIN | POLLRDNORM);
1874 else
1875 selrecord(l, &sce_in->rsel);
1876 break;
1877 case UE_ISOCHRONOUS:
1878 if (sce_in->cur != sce_in->fill)
1879 revents |= events & (POLLIN | POLLRDNORM);
1880 else
1881 selrecord(l, &sce_in->rsel);
1882 break;
1883 case UE_BULK:
1884 #ifdef UGEN_BULK_RA_WB
1885 if (sce_in->state & UGEN_BULK_RA) {
1886 if (sce_in->ra_wb_used > 0)
1887 revents |= events &
1888 (POLLIN | POLLRDNORM);
1889 else
1890 selrecord(l, &sce_in->rsel);
1891 break;
1892 }
1893 #endif
1894 /*
1895 * We have no easy way of determining if a read will
1896 * yield any data or a write will happen.
1897 * Pretend they will.
1898 */
1899 revents |= events & (POLLIN | POLLRDNORM);
1900 break;
1901 default:
1902 break;
1903 }
1904 if (sce_out && sce_out->pipeh && (events & (POLLOUT | POLLWRNORM)))
1905 switch (sce_out->edesc->bmAttributes & UE_XFERTYPE) {
1906 case UE_INTERRUPT:
1907 case UE_ISOCHRONOUS:
1908 /* XXX unimplemented */
1909 break;
1910 case UE_BULK:
1911 #ifdef UGEN_BULK_RA_WB
1912 if (sce_out->state & UGEN_BULK_WB) {
1913 if (sce_out->ra_wb_used <
1914 sce_out->limit - sce_out->ibuf)
1915 revents |= events &
1916 (POLLOUT | POLLWRNORM);
1917 else
1918 selrecord(l, &sce_out->rsel);
1919 break;
1920 }
1921 #endif
1922 /*
1923 * We have no easy way of determining if a read will
1924 * yield any data or a write will happen.
1925 * Pretend they will.
1926 */
1927 revents |= events & (POLLOUT | POLLWRNORM);
1928 break;
1929 default:
1930 break;
1931 }
1932
1933
1934 splx(s);
1935 return (revents);
1936 }
1937
1938 static void
1939 filt_ugenrdetach(struct knote *kn)
1940 {
1941 struct ugen_endpoint *sce = kn->kn_hook;
1942 int s;
1943
1944 s = splusb();
1945 SLIST_REMOVE(&sce->rsel.sel_klist, kn, knote, kn_selnext);
1946 splx(s);
1947 }
1948
1949 static int
1950 filt_ugenread_intr(struct knote *kn, long hint)
1951 {
1952 struct ugen_endpoint *sce = kn->kn_hook;
1953
1954 kn->kn_data = sce->q.c_cc;
1955 return (kn->kn_data > 0);
1956 }
1957
1958 static int
1959 filt_ugenread_isoc(struct knote *kn, long hint)
1960 {
1961 struct ugen_endpoint *sce = kn->kn_hook;
1962
1963 if (sce->cur == sce->fill)
1964 return (0);
1965
1966 if (sce->cur < sce->fill)
1967 kn->kn_data = sce->fill - sce->cur;
1968 else
1969 kn->kn_data = (sce->limit - sce->cur) +
1970 (sce->fill - sce->ibuf);
1971
1972 return (1);
1973 }
1974
1975 #ifdef UGEN_BULK_RA_WB
1976 static int
1977 filt_ugenread_bulk(struct knote *kn, long hint)
1978 {
1979 struct ugen_endpoint *sce = kn->kn_hook;
1980
1981 if (!(sce->state & UGEN_BULK_RA))
1982 /*
1983 * We have no easy way of determining if a read will
1984 * yield any data or a write will happen.
1985 * So, emulate "seltrue".
1986 */
1987 return (filt_seltrue(kn, hint));
1988
1989 if (sce->ra_wb_used == 0)
1990 return (0);
1991
1992 kn->kn_data = sce->ra_wb_used;
1993
1994 return (1);
1995 }
1996
1997 static int
1998 filt_ugenwrite_bulk(struct knote *kn, long hint)
1999 {
2000 struct ugen_endpoint *sce = kn->kn_hook;
2001
2002 if (!(sce->state & UGEN_BULK_WB))
2003 /*
2004 * We have no easy way of determining if a read will
2005 * yield any data or a write will happen.
2006 * So, emulate "seltrue".
2007 */
2008 return (filt_seltrue(kn, hint));
2009
2010 if (sce->ra_wb_used == sce->limit - sce->ibuf)
2011 return (0);
2012
2013 kn->kn_data = (sce->limit - sce->ibuf) - sce->ra_wb_used;
2014
2015 return (1);
2016 }
2017 #endif
2018
2019 static const struct filterops ugenread_intr_filtops =
2020 { 1, NULL, filt_ugenrdetach, filt_ugenread_intr };
2021
2022 static const struct filterops ugenread_isoc_filtops =
2023 { 1, NULL, filt_ugenrdetach, filt_ugenread_isoc };
2024
2025 #ifdef UGEN_BULK_RA_WB
2026 static const struct filterops ugenread_bulk_filtops =
2027 { 1, NULL, filt_ugenrdetach, filt_ugenread_bulk };
2028
2029 static const struct filterops ugenwrite_bulk_filtops =
2030 { 1, NULL, filt_ugenrdetach, filt_ugenwrite_bulk };
2031 #else
2032 static const struct filterops ugen_seltrue_filtops =
2033 { 1, NULL, filt_ugenrdetach, filt_seltrue };
2034 #endif
2035
2036 int
2037 ugenkqfilter(dev_t dev, struct knote *kn)
2038 {
2039 struct ugen_softc *sc;
2040 struct ugen_endpoint *sce;
2041 struct klist *klist;
2042 int s;
2043
2044 USB_GET_SC(ugen, UGENUNIT(dev), sc);
2045
2046 if (sc->sc_dying)
2047 return (1);
2048
2049 switch (kn->kn_filter) {
2050 case EVFILT_READ:
2051 sce = &sc->sc_endpoints[UGENENDPOINT(dev)][IN];
2052 if (sce == NULL)
2053 return (1);
2054
2055 klist = &sce->rsel.sel_klist;
2056 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
2057 case UE_INTERRUPT:
2058 kn->kn_fop = &ugenread_intr_filtops;
2059 break;
2060 case UE_ISOCHRONOUS:
2061 kn->kn_fop = &ugenread_isoc_filtops;
2062 break;
2063 case UE_BULK:
2064 #ifdef UGEN_BULK_RA_WB
2065 kn->kn_fop = &ugenread_bulk_filtops;
2066 break;
2067 #else
2068 /*
2069 * We have no easy way of determining if a read will
2070 * yield any data or a write will happen.
2071 * So, emulate "seltrue".
2072 */
2073 kn->kn_fop = &ugen_seltrue_filtops;
2074 #endif
2075 break;
2076 default:
2077 return (1);
2078 }
2079 break;
2080
2081 case EVFILT_WRITE:
2082 sce = &sc->sc_endpoints[UGENENDPOINT(dev)][OUT];
2083 if (sce == NULL)
2084 return (1);
2085
2086 klist = &sce->rsel.sel_klist;
2087 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
2088 case UE_INTERRUPT:
2089 case UE_ISOCHRONOUS:
2090 /* XXX poll doesn't support this */
2091 return (1);
2092
2093 case UE_BULK:
2094 #ifdef UGEN_BULK_RA_WB
2095 kn->kn_fop = &ugenwrite_bulk_filtops;
2096 #else
2097 /*
2098 * We have no easy way of determining if a read will
2099 * yield any data or a write will happen.
2100 * So, emulate "seltrue".
2101 */
2102 kn->kn_fop = &ugen_seltrue_filtops;
2103 #endif
2104 break;
2105 default:
2106 return (1);
2107 }
2108 break;
2109
2110 default:
2111 return (1);
2112 }
2113
2114 kn->kn_hook = sce;
2115
2116 s = splusb();
2117 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
2118 splx(s);
2119
2120 return (0);
2121 }
2122
2123 #if defined(__FreeBSD__)
2124 DRIVER_MODULE(ugen, uhub, ugen_driver, ugen_devclass, usbd_driver_load, 0);
2125 #endif
2126