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