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