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