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