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