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