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