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