2 * $NetBSD: ugen.c,v 1.27 1999/10/28 12:08:38 augustss Exp $
3 * $NetBSD: ugen.c,v 1.59 2002/07/11 21:14:28 augustss Exp $
4 * $FreeBSD: src/sys/dev/usb/ugen.c,v 1.81 2003/11/09 09:17:22 tanimura Exp $
5 * $DragonFly: src/sys/dev/usbmisc/ugen/ugen.c,v 1.34 2008/05/13 08:35:12 hasso Exp $
9 * Also already merged from NetBSD:
10 * $NetBSD: ugen.c,v 1.61 2002/09/23 05:51:20 simonb Exp $
11 * $NetBSD: ugen.c,v 1.64 2003/06/28 14:21:46 darrenr Exp $
12 * $NetBSD: ugen.c,v 1.65 2003/06/29 22:30:56 fvdl Exp $
16 * Copyright (c) 1998 The NetBSD Foundation, Inc.
17 * All rights reserved.
19 * This code is derived from software contributed to The NetBSD Foundation
20 * by Lennart Augustsson (lennart@augustsson.net) at
21 * Carlstedt Research & Technology.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * This product includes software developed by the NetBSD
34 * Foundation, Inc. and its contributors.
35 * 4. Neither the name of The NetBSD Foundation nor the names of its
36 * contributors may be used to endorse or promote products derived
37 * from this software without specific prior written permission.
39 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
40 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
43 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
44 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
45 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
46 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 * POSSIBILITY OF SUCH DAMAGE.
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
56 #include <sys/malloc.h>
57 #include <sys/module.h>
60 #include <sys/fcntl.h>
61 #include <sys/filio.h>
64 #include <sys/select.h>
65 #include <sys/vnode.h>
67 #include <sys/event.h>
68 #include <sys/sysctl.h>
69 #include <sys/thread2.h>
71 #include <bus/usb/usb.h>
72 #include <bus/usb/usbdi.h>
73 #include <bus/usb/usbdi_util.h>
77 SYSCTL_NODE(_hw_usb, OID_AUTO, ugen, CTLFLAG_RW, 0, "USB ugen");
80 #define DPRINTF(x) if (ugendebug) kprintf x
81 #define DPRINTFN(n,x) if (ugendebug>(n)) kprintf x
83 SYSCTL_INT(_hw_usb_ugen, OID_AUTO, debug, CTLFLAG_RW,
84 &ugendebug, 0, "ugen debug level");
90 static int ugen_bufsize = 16384;
91 SYSCTL_INT(_hw_usb_ugen, OID_AUTO, bufsize, CTLFLAG_RW,
92 &ugen_bufsize, 0, "ugen temporary buffer size");
94 #define UGEN_CHUNK 128 /* chunk size for read */
95 #define UGEN_IBSIZE 1020 /* buffer size */
97 #define UGEN_NISOFRAMES 500 /* 0.5 seconds worth */
98 #define UGEN_NISOREQS 6 /* number of outstanding xfer requests */
99 #define UGEN_NISORFRMS 4 /* number of frames (miliseconds) per req */
101 struct ugen_endpoint {
102 struct ugen_softc *sc;
104 usb_endpoint_descriptor_t *edesc;
105 usbd_interface_handle iface;
107 #define UGEN_ASLP 0x02 /* waiting for data */
108 #define UGEN_SHORT_OK 0x04 /* short xfers are OK */
109 usbd_pipe_handle pipeh;
112 u_char *ibuf; /* start of buffer (circular for isoc) */
113 u_char *fill; /* location for input (isoc) */
114 u_char *limit; /* end of circular buffer (isoc) */
115 u_char *cur; /* current read location (isoc) */
118 struct ugen_endpoint *sce;
119 usbd_xfer_handle xfer;
121 u_int16_t sizes[UGEN_NISORFRMS];
122 } isoreqs[UGEN_NISOREQS];
126 device_t sc_dev; /* base device */
127 usbd_device_handle sc_udev;
129 char sc_is_open[USB_MAX_ENDPOINTS];
130 struct ugen_endpoint sc_endpoints[USB_MAX_ENDPOINTS][2];
144 d_kqfilter_t ugenkqfilter;
146 static void ugen_filt_detach(struct knote *);
147 static int ugen_filt_read(struct knote *, long);
148 static int ugen_filt_write(struct knote *, long);
150 #define UGEN_CDEV_MAJOR 114
152 static struct dev_ops ugen_ops = {
153 { "ugen", UGEN_CDEV_MAJOR, D_KQFILTER },
155 .d_close = ugenclose,
157 .d_write = ugenwrite,
158 .d_ioctl = ugenioctl,
160 .d_kqfilter = ugenkqfilter
163 static void ugenintr(usbd_xfer_handle xfer, usbd_private_handle addr,
165 static void ugen_isoc_rintr(usbd_xfer_handle xfer, usbd_private_handle addr,
167 static int ugen_do_read(struct ugen_softc *, int, struct uio *, int);
168 static int ugen_do_write(struct ugen_softc *, int, struct uio *, int);
169 static int ugen_do_ioctl(struct ugen_softc *, int, u_long,
171 static void ugen_make_devnodes(struct ugen_softc *sc);
172 static void ugen_destroy_devnodes(struct ugen_softc *sc);
173 static int ugen_set_config(struct ugen_softc *sc, int configno);
174 static usb_config_descriptor_t *ugen_get_cdesc(struct ugen_softc *sc,
175 int index, int *lenp);
176 static usbd_status ugen_set_interface(struct ugen_softc *, int, int);
177 static int ugen_get_alt_index(struct ugen_softc *sc, int ifaceidx);
179 #define UGENUNIT(n) ((lminor(n) >> 4) & 0xff)
180 #define UGENENDPOINT(n) (minor(n) & 0xf)
181 #define UGENMINOR(u, e) (((u & 0xf) << 4) | ((u & 0xf0) << 12) | (e))
182 #define UGENUNITMASK 0xffff00f0
184 static device_probe_t ugen_match;
185 static device_attach_t ugen_attach;
186 static device_detach_t ugen_detach;
188 static devclass_t ugen_devclass;
190 static kobj_method_t ugen_methods[] = {
191 DEVMETHOD(device_probe, ugen_match),
192 DEVMETHOD(device_attach, ugen_attach),
193 DEVMETHOD(device_detach, ugen_detach),
197 static driver_t ugen_driver = {
200 sizeof(struct ugen_softc)
203 MODULE_DEPEND(ugen, usb, 1, 1, 1);
206 ugen_match(device_t self)
208 struct usb_attach_arg *uaa = device_get_ivars(self);
212 return (uaa->matchlvl);
215 return (UMATCH_GENERIC);
217 return (UMATCH_NONE);
221 ugen_attach(device_t self)
223 struct ugen_softc *sc = device_get_softc(self);
224 struct usb_attach_arg *uaa = device_get_ivars(self);
225 usbd_device_handle udev;
230 sc->sc_udev = udev = uaa->device;
232 memset(sc->sc_endpoints, 0, sizeof sc->sc_endpoints);
234 /* First set configuration index 0, the default one for ugen. */
235 err = usbd_set_config_index(udev, 0, 0);
237 kprintf("%s: setting configuration index 0 failed\n",
238 device_get_nameunit(sc->sc_dev));
242 conf = usbd_get_config_descriptor(udev)->bConfigurationValue;
244 /* Set up all the local state for this configuration. */
245 err = ugen_set_config(sc, conf);
247 kprintf("%s: setting configuration %d failed\n",
248 device_get_nameunit(sc->sc_dev), conf);
253 /* the main device, ctrl endpoint */
254 make_dev(&ugen_ops, UGENMINOR(device_get_unit(sc->sc_dev), 0),
255 UID_ROOT, GID_OPERATOR, 0644,
256 "%s", device_get_nameunit(sc->sc_dev));
258 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
263 ugen_make_devnodes(struct ugen_softc *sc)
268 for (endptno = 1; endptno < USB_MAX_ENDPOINTS; endptno++) {
269 if (sc->sc_endpoints[endptno][IN].sc != NULL ||
270 sc->sc_endpoints[endptno][OUT].sc != NULL ) {
271 /* endpt can be 0x81 and 0x01, representing
272 * endpoint address 0x01 and IN/OUT directions.
273 * We map both endpts to the same device,
274 * IN is reading from it, OUT is writing to it.
276 * In the if clause above we check whether one
277 * of the structs is populated.
279 dev = make_dev(&ugen_ops,
280 UGENMINOR(device_get_unit(sc->sc_dev), endptno),
281 UID_ROOT, GID_OPERATOR, 0644,
283 device_get_nameunit(sc->sc_dev), endptno);
284 if (sc->sc_endpoints[endptno][IN].sc != NULL) {
286 if (sc->sc_endpoints[endptno][IN].dev)
287 release_dev(sc->sc_endpoints[endptno][IN].dev);
288 sc->sc_endpoints[endptno][IN].dev = dev;
290 if (sc->sc_endpoints[endptno][OUT].sc != NULL) {
292 if (sc->sc_endpoints[endptno][OUT].dev)
293 release_dev(sc->sc_endpoints[endptno][OUT].dev);
294 sc->sc_endpoints[endptno][OUT].dev = dev;
301 ugen_destroy_devnodes(struct ugen_softc *sc)
303 int endptno, prev_sc_dying;
306 prev_sc_dying = sc->sc_dying;
309 /* destroy all devices for the other (existing) endpoints as well */
310 for (endptno = 1; endptno < USB_MAX_ENDPOINTS; endptno++) {
311 if (sc->sc_endpoints[endptno][IN].sc != NULL ||
312 sc->sc_endpoints[endptno][OUT].sc != NULL ) {
313 /* endpt can be 0x81 and 0x01, representing
314 * endpoint address 0x01 and IN/OUT directions.
315 * We map both endpoint addresses to the same device,
316 * IN is reading from it, OUT is writing to it.
318 * In the if clause above we check whether one
319 * of the structs is populated.
321 dev = sc->sc_endpoints[endptno][IN].dev;
324 sc->sc_endpoints[endptno][IN].dev = NULL;
326 dev = sc->sc_endpoints[endptno][OUT].dev;
329 sc->sc_endpoints[endptno][OUT].dev = NULL;
333 sc->sc_dying = prev_sc_dying;
337 ugen_set_config(struct ugen_softc *sc, int configno)
339 usbd_device_handle dev = sc->sc_udev;
340 usbd_interface_handle iface;
341 usb_endpoint_descriptor_t *ed;
342 struct ugen_endpoint *sce;
343 u_int8_t niface, nendpt;
344 int ifaceno, endptno, endpt;
348 DPRINTFN(1,("ugen_set_config: %s to configno %d, sc=%p\n",
349 device_get_nameunit(sc->sc_dev), configno, sc));
351 ugen_destroy_devnodes(sc);
353 /* We start at 1, not 0, because we don't care whether the
354 * control endpoint is open or not. It is always present.
356 for (endptno = 1; endptno < USB_MAX_ENDPOINTS; endptno++) {
357 if (sc->sc_is_open[endptno]) {
359 ("ugen_set_config: %s - endpoint %d is open\n",
360 device_get_nameunit(sc->sc_dev), endptno));
361 return (USBD_IN_USE);
365 /* Avoid setting the current value. */
366 if (usbd_get_config_descriptor(dev)->bConfigurationValue != configno) {
367 err = usbd_set_config_no(dev, configno, 1);
372 err = usbd_interface_count(dev, &niface);
375 memset(sc->sc_endpoints, 0, sizeof sc->sc_endpoints);
376 for (ifaceno = 0; ifaceno < niface; ifaceno++) {
377 DPRINTFN(1,("ugen_set_config: ifaceno %d\n", ifaceno));
378 err = usbd_device2interface_handle(dev, ifaceno, &iface);
381 err = usbd_endpoint_count(iface, &nendpt);
384 for (endptno = 0; endptno < nendpt; endptno++) {
385 ed = usbd_interface2endpoint_descriptor(iface,endptno);
386 endpt = ed->bEndpointAddress;
387 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
388 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
389 DPRINTFN(1,("ugen_set_config: endptno %d, endpt=0x%02x"
391 endptno, endpt, UE_GET_ADDR(endpt),
392 UE_GET_DIR(endpt), sce));
399 ugen_make_devnodes(sc);
401 return (USBD_NORMAL_COMPLETION);
405 ugenopen(struct dev_open_args *ap)
407 cdev_t dev = ap->a_head.a_dev;
408 struct ugen_softc *sc;
409 int unit = UGENUNIT(dev);
410 int endpt = UGENENDPOINT(dev);
411 usb_endpoint_descriptor_t *edesc;
412 struct ugen_endpoint *sce;
415 usbd_xfer_handle xfer;
419 sc = devclass_get_softc(ugen_devclass, unit);
423 DPRINTFN(5, ("ugenopen: flag=%d, mode=%d, unit=%d endpt=%d\n",
424 ap->a_oflags, ap->a_devtype, unit, endpt));
429 if (sc->sc_is_open[endpt])
432 if (endpt == USB_CONTROL_ENDPOINT) {
433 sc->sc_is_open[USB_CONTROL_ENDPOINT] = 1;
437 /* Make sure there are pipes for all directions. */
438 for (dir = OUT; dir <= IN; dir++) {
439 if (ap->a_oflags & (dir == OUT ? FWRITE : FREAD)) {
440 sce = &sc->sc_endpoints[endpt][dir];
441 if (sce == 0 || sce->edesc == 0)
446 /* Actually open the pipes. */
447 /* XXX Should back out properly if it fails. */
448 for (dir = OUT; dir <= IN; dir++) {
449 if (!(ap->a_oflags & (dir == OUT ? FWRITE : FREAD)))
451 sce = &sc->sc_endpoints[endpt][dir];
453 sce->timeout = USBD_NO_TIMEOUT;
454 DPRINTFN(5, ("ugenopen: sc=%p, endpt=%d, dir=%d, sce=%p\n",
455 sc, endpt, dir, sce));
457 switch (edesc->bmAttributes & UE_XFERTYPE) {
460 err = usbd_open_pipe(sce->iface,
461 edesc->bEndpointAddress, 0, &sce->pipeh);
466 isize = UGETW(edesc->wMaxPacketSize);
467 if (isize == 0) /* shouldn't happen */
469 sce->ibuf = kmalloc(isize, M_USBDEV, M_WAITOK);
470 DPRINTFN(5, ("ugenopen: intr endpt=%d,isize=%d\n",
472 if ((clist_alloc_cblocks(&sce->q, UGEN_IBSIZE,
473 UGEN_IBSIZE), 0) == -1)
475 err = usbd_open_pipe_intr(sce->iface,
476 edesc->bEndpointAddress,
477 USBD_SHORT_XFER_OK, &sce->pipeh, sce,
478 sce->ibuf, isize, ugenintr,
479 USBD_DEFAULT_INTERVAL);
481 kfree(sce->ibuf, M_USBDEV);
482 clist_free_cblocks(&sce->q);
485 DPRINTFN(5, ("ugenopen: interrupt open done\n"));
488 err = usbd_open_pipe(sce->iface,
489 edesc->bEndpointAddress, 0, &sce->pipeh);
496 isize = UGETW(edesc->wMaxPacketSize);
497 if (isize == 0) /* shouldn't happen */
499 sce->ibuf = kmalloc(isize * UGEN_NISOFRAMES,
501 sce->cur = sce->fill = sce->ibuf;
502 sce->limit = sce->ibuf + isize * UGEN_NISOFRAMES;
503 DPRINTFN(5, ("ugenopen: isoc endpt=%d, isize=%d\n",
505 err = usbd_open_pipe(sce->iface,
506 edesc->bEndpointAddress, 0, &sce->pipeh);
508 kfree(sce->ibuf, M_USBDEV);
511 for(i = 0; i < UGEN_NISOREQS; ++i) {
512 sce->isoreqs[i].sce = sce;
513 xfer = usbd_alloc_xfer(sc->sc_udev);
516 sce->isoreqs[i].xfer = xfer;
517 buf = usbd_alloc_buffer
518 (xfer, isize * UGEN_NISORFRMS);
523 sce->isoreqs[i].dmabuf = buf;
524 for(j = 0; j < UGEN_NISORFRMS; ++j)
525 sce->isoreqs[i].sizes[j] = isize;
527 (xfer, sce->pipeh, &sce->isoreqs[i],
528 sce->isoreqs[i].sizes,
529 UGEN_NISORFRMS, USBD_NO_COPY,
531 (void)usbd_transfer(xfer);
533 DPRINTFN(5, ("ugenopen: isoc open done\n"));
536 while (--i >= 0) /* implicit buffer free */
537 usbd_free_xfer(sce->isoreqs[i].xfer);
540 sce->timeout = USBD_DEFAULT_TIMEOUT;
544 sc->sc_is_open[endpt] = 1;
549 ugenclose(struct dev_close_args *ap)
551 cdev_t dev = ap->a_head.a_dev;
552 int endpt = UGENENDPOINT(dev);
553 struct ugen_softc *sc;
554 struct ugen_endpoint *sce;
558 sc = devclass_get_softc(ugen_devclass, UGENUNIT(dev));
560 DPRINTFN(5, ("ugenclose: flag=%d, mode=%d, unit=%d, endpt=%d\n",
561 ap->a_fflag, ap->a_devtype, UGENUNIT(dev), endpt));
564 if (!sc->sc_is_open[endpt]) {
565 kprintf("ugenclose: not open\n");
570 if (endpt == USB_CONTROL_ENDPOINT) {
571 DPRINTFN(5, ("ugenclose: close control\n"));
572 sc->sc_is_open[endpt] = 0;
576 for (dir = OUT; dir <= IN; dir++) {
577 if (!(ap->a_fflag & (dir == OUT ? FWRITE : FREAD)))
579 sce = &sc->sc_endpoints[endpt][dir];
580 if (sce->pipeh == NULL)
582 DPRINTFN(5, ("ugenclose: endpt=%d dir=%d sce=%p\n",
585 usbd_abort_pipe(sce->pipeh);
586 usbd_close_pipe(sce->pipeh);
589 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
591 ndflush(&sce->q, sce->q.c_cc);
592 clist_free_cblocks(&sce->q);
595 for (i = 0; i < UGEN_NISOREQS; ++i)
596 usbd_free_xfer(sce->isoreqs[i].xfer);
601 if (sce->ibuf != NULL) {
602 kfree(sce->ibuf, M_USBDEV);
604 clist_free_cblocks(&sce->q);
607 sc->sc_is_open[endpt] = 0;
613 ugen_do_read(struct ugen_softc *sc, int endpt, struct uio *uio, int flag)
615 struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][IN];
618 usbd_xfer_handle xfer;
622 u_char buffer[UGEN_CHUNK];
624 DPRINTFN(5, ("%s: ugenread: %d\n", device_get_nameunit(sc->sc_dev), endpt));
629 if (endpt == USB_CONTROL_ENDPOINT)
633 if (sce->edesc == NULL) {
634 kprintf("ugenread: no edesc\n");
637 if (sce->pipeh == NULL) {
638 kprintf("ugenread: no pipe\n");
643 buf = getugenbuf(ugen_bufsize, &ugen_bbsize);
645 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
647 /* Block until activity occurred. */
649 while (sce->q.c_cc == 0) {
650 if (flag & IO_NDELAY) {
655 sce->state |= UGEN_ASLP;
656 DPRINTFN(5, ("ugenread: sleep on %p\n", sce));
657 error = tsleep(sce, PCATCH, "ugenri",
658 (sce->timeout * hz + 999) / 1000);
659 sce->state &= ~UGEN_ASLP;
660 DPRINTFN(5, ("ugenread: woke, error=%d\n", error));
663 if (error == EAGAIN) {
664 error = 0; /* timeout, return 0 bytes */
672 /* Transfer as many chunks as possible. */
673 while (sce->q.c_cc > 0 && uio->uio_resid > 0 && !error) {
674 n = szmin(sce->q.c_cc, uio->uio_resid);
675 if (n > sizeof(buffer))
678 /* Remove a small chunk from the input queue. */
679 q_to_b(&sce->q, buffer, n);
680 DPRINTFN(5, ("ugenread: got %d chars\n", n));
682 /* Copy the data to the user process. */
683 error = uiomove(buffer, n, uio);
689 xfer = usbd_alloc_xfer(sc->sc_udev);
694 while ((n = szmin(ugen_bbsize, uio->uio_resid)) != 0) {
695 DPRINTFN(1, ("ugenread: start transfer %d bytes\n",n));
697 err = usbd_bulk_transfer(
699 sce->state & UGEN_SHORT_OK ?
700 USBD_SHORT_XFER_OK : 0,
701 sce->timeout, buf, &tn, "ugenrb");
703 if (err == USBD_INTERRUPTED)
705 else if (err == USBD_TIMEOUT)
711 DPRINTFN(1, ("ugenread: got %d bytes\n", tn));
712 error = uiomove(buf, tn, uio);
716 usbd_free_xfer(xfer);
720 while (sce->cur == sce->fill) {
721 if (flag & IO_NDELAY) {
726 sce->state |= UGEN_ASLP;
727 DPRINTFN(5, ("ugenread: sleep on %p\n", sce));
728 error = tsleep(sce, PCATCH, "ugenri",
729 (sce->timeout * hz + 999) / 1000);
730 sce->state &= ~UGEN_ASLP;
731 DPRINTFN(5, ("ugenread: woke, error=%d\n", error));
734 if (error == EAGAIN) {
735 error = 0; /* timeout, return 0 bytes */
742 while (sce->cur != sce->fill && uio->uio_resid > 0 && !error) {
743 if (sce->fill > sce->cur)
744 n = szmin(sce->fill - sce->cur, uio->uio_resid);
746 n = szmin(sce->limit- sce->cur, uio->uio_resid);
748 DPRINTFN(5, ("ugenread: isoc got %d chars\n", n));
750 /* Copy the data to the user process. */
751 error = uiomove(sce->cur, n, uio);
755 if(sce->cur >= sce->limit)
756 sce->cur = sce->ibuf;
767 relugenbuf(buf, ugen_bbsize);
772 ugenread(struct dev_read_args *ap)
774 cdev_t dev = ap->a_head.a_dev;
775 int endpt = UGENENDPOINT(dev);
776 struct ugen_softc *sc;
779 sc = devclass_get_softc(ugen_devclass, UGENUNIT(dev));
785 error = ugen_do_read(sc, endpt, ap->a_uio, ap->a_ioflag);
786 if (--sc->sc_refcnt < 0)
787 usb_detach_wakeup(sc->sc_dev);
792 ugen_do_write(struct ugen_softc *sc, int endpt, struct uio *uio, int flag)
794 struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][OUT];
799 usbd_xfer_handle xfer;
802 DPRINTFN(5, ("%s: ugenwrite: %d\n", device_get_nameunit(sc->sc_dev), endpt));
807 if (endpt == USB_CONTROL_ENDPOINT)
811 if (sce->edesc == NULL) {
812 kprintf("ugenwrite: no edesc\n");
815 if (sce->pipeh == NULL) {
816 kprintf("ugenwrite: no pipe\n");
821 buf = getugenbuf(ugen_bufsize, &ugen_bbsize);
823 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
825 xfer = usbd_alloc_xfer(sc->sc_udev);
830 while ((n = szmin(ugen_bbsize, uio->uio_resid)) != 0) {
831 error = uiomove(buf, n, uio);
834 DPRINTFN(1, ("ugenwrite: transfer %d bytes\n", n));
835 err = usbd_bulk_transfer(xfer, sce->pipeh, 0,
836 sce->timeout, buf, &n,"ugenwb");
838 if (err == USBD_INTERRUPTED)
840 else if (err == USBD_TIMEOUT)
847 usbd_free_xfer(xfer);
850 xfer = usbd_alloc_xfer(sc->sc_udev);
855 while ((n = szmin(UGETW(sce->edesc->wMaxPacketSize),
856 uio->uio_resid)) != 0) {
857 error = uiomove(buf, n, uio);
860 DPRINTFN(1, ("ugenwrite: transfer %d bytes\n", n));
861 err = usbd_intr_transfer(xfer, sce->pipeh, 0,
862 sce->timeout, buf, &n,"ugenwi");
864 if (err == USBD_INTERRUPTED)
866 else if (err == USBD_TIMEOUT)
873 usbd_free_xfer(xfer);
880 relugenbuf(buf, ugen_bbsize);
885 ugenwrite(struct dev_write_args *ap)
887 cdev_t dev = ap->a_head.a_dev;
888 int endpt = UGENENDPOINT(dev);
889 struct ugen_softc *sc;
892 sc = devclass_get_softc(ugen_devclass, UGENUNIT(dev));
898 error = ugen_do_write(sc, endpt, ap->a_uio, ap->a_ioflag);
899 if (--sc->sc_refcnt < 0)
900 usb_detach_wakeup(sc->sc_dev);
905 ugen_detach(device_t self)
907 struct ugen_softc *sc = device_get_softc(self);
908 struct ugen_endpoint *sce;
911 DPRINTF(("ugen_detach: sc=%p\n", sc));
914 /* Abort all pipes. Causes processes waiting for transfer to wake. */
915 for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
916 for (dir = OUT; dir <= IN; dir++) {
917 sce = &sc->sc_endpoints[i][dir];
918 if (sce && sce->pipeh)
919 usbd_abort_pipe(sce->pipeh);
920 selwakeup(&sce->rsel);
924 if (--sc->sc_refcnt >= 0) {
926 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
927 wakeup(&sc->sc_endpoints[i][IN]);
928 /* Wait for processes to go away. */
929 usb_detach_wait(sc->sc_dev);
933 /* destroy the device for the control endpoint */
934 ugen_destroy_devnodes(sc);
935 dev_ops_remove_minor(&ugen_ops,
936 /*UGENUNITMASK,*/ UGENMINOR(device_get_unit(sc->sc_dev), 0));
937 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
942 ugenintr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
944 struct ugen_endpoint *sce = addr;
945 /*struct ugen_softc *sc = sce->sc;*/
949 if (status == USBD_CANCELLED)
952 if (status != USBD_NORMAL_COMPLETION) {
953 DPRINTF(("ugenintr: status=%d\n", status));
954 if (status == USBD_STALLED)
955 usbd_clear_endpoint_stall_async(sce->pipeh);
959 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
962 DPRINTFN(5, ("ugenintr: xfer=%p status=%d count=%d\n",
963 xfer, status, count));
964 DPRINTFN(5, (" data = %02x %02x %02x\n",
965 ibuf[0], ibuf[1], ibuf[2]));
967 (void)b_to_q(ibuf, count, &sce->q);
969 if (sce->state & UGEN_ASLP) {
970 sce->state &= ~UGEN_ASLP;
971 DPRINTFN(5, ("ugen_intr: waking %p\n", sce));
974 selwakeup(&sce->rsel);
978 ugen_isoc_rintr(usbd_xfer_handle xfer, usbd_private_handle addr,
981 struct isoreq *req = addr;
982 struct ugen_endpoint *sce = req->sce;
986 /* Return if we are aborting. */
987 if (status == USBD_CANCELLED)
990 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
991 DPRINTFN(5,("ugen_isoc_rintr: xfer %d, count=%d\n",
992 (int)(req - sce->isoreqs),
995 /* throw away oldest input if the buffer is full */
996 if(sce->fill < sce->cur && sce->cur <= sce->fill + count) {
998 if(sce->cur >= sce->limit)
999 sce->cur = sce->ibuf + (sce->limit - sce->cur);
1000 DPRINTFN(5, ("ugen_isoc_rintr: throwing away %d bytes\n",
1004 isize = UGETW(sce->edesc->wMaxPacketSize);
1005 for (i = 0; i < UGEN_NISORFRMS; i++) {
1006 u_int32_t actlen = req->sizes[i];
1007 char const *buf = (char const *)req->dmabuf + isize * i;
1009 /* copy data to buffer */
1010 while (actlen > 0) {
1011 n = min(actlen, sce->limit - sce->fill);
1012 memcpy(sce->fill, buf, n);
1017 if(sce->fill == sce->limit)
1018 sce->fill = sce->ibuf;
1021 /* setup size for next transfer */
1022 req->sizes[i] = isize;
1025 usbd_setup_isoc_xfer(xfer, sce->pipeh, req, req->sizes, UGEN_NISORFRMS,
1026 USBD_NO_COPY, ugen_isoc_rintr);
1027 (void)usbd_transfer(xfer);
1029 if (sce->state & UGEN_ASLP) {
1030 sce->state &= ~UGEN_ASLP;
1031 DPRINTFN(5, ("ugen_isoc_rintr: waking %p\n", sce));
1034 selwakeup(&sce->rsel);
1038 ugen_set_interface(struct ugen_softc *sc, int ifaceidx, int altno)
1040 usbd_interface_handle iface;
1041 usb_endpoint_descriptor_t *ed;
1043 struct ugen_endpoint *sce;
1044 u_int8_t niface, nendpt, endptno, endpt;
1047 DPRINTFN(15, ("ugen_set_interface %d %d\n", ifaceidx, altno));
1049 err = usbd_interface_count(sc->sc_udev, &niface);
1052 if (ifaceidx < 0 || ifaceidx >= niface)
1053 return (USBD_INVAL);
1055 err = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface);
1058 err = usbd_endpoint_count(iface, &nendpt);
1062 /* destroy the existing devices, we remake the new ones in a moment */
1063 ugen_destroy_devnodes(sc);
1065 /* XXX should only do this after setting new altno has succeeded */
1066 for (endptno = 0; endptno < nendpt; endptno++) {
1067 ed = usbd_interface2endpoint_descriptor(iface,endptno);
1068 endpt = ed->bEndpointAddress;
1069 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
1070 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
1076 /* change setting */
1077 err = usbd_set_interface(iface, altno);
1081 err = usbd_endpoint_count(iface, &nendpt);
1084 for (endptno = 0; endptno < nendpt; endptno++) {
1085 ed = usbd_interface2endpoint_descriptor(iface,endptno);
1086 endpt = ed->bEndpointAddress;
1087 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
1088 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
1094 /* make the new devices */
1095 ugen_make_devnodes(sc);
1100 /* Retrieve a complete descriptor for a certain device and index. */
1101 static usb_config_descriptor_t *
1102 ugen_get_cdesc(struct ugen_softc *sc, int index, int *lenp)
1104 usb_config_descriptor_t *cdesc, *tdesc, cdescr;
1108 if (index == USB_CURRENT_CONFIG_INDEX) {
1109 tdesc = usbd_get_config_descriptor(sc->sc_udev);
1110 len = UGETW(tdesc->wTotalLength);
1113 cdesc = kmalloc(len, M_TEMP, M_INTWAIT);
1114 memcpy(cdesc, tdesc, len);
1115 DPRINTFN(5,("ugen_get_cdesc: current, len=%d\n", len));
1117 err = usbd_get_config_desc(sc->sc_udev, index, &cdescr);
1120 len = UGETW(cdescr.wTotalLength);
1121 DPRINTFN(5,("ugen_get_cdesc: index=%d, len=%d\n", index, len));
1124 cdesc = kmalloc(len, M_TEMP, M_INTWAIT);
1125 err = usbd_get_config_desc_full(sc->sc_udev, index, cdesc, len);
1127 kfree(cdesc, M_TEMP);
1135 ugen_get_alt_index(struct ugen_softc *sc, int ifaceidx)
1137 usbd_interface_handle iface;
1140 err = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface);
1143 return (usbd_get_interface_altindex(iface));
1147 ugen_do_ioctl(struct ugen_softc *sc, int endpt, u_long cmd,
1148 caddr_t addr, int flag)
1150 struct ugen_endpoint *sce;
1152 usbd_interface_handle iface;
1153 struct usb_config_desc *cd;
1154 usb_config_descriptor_t *cdesc;
1155 struct usb_interface_desc *id;
1156 usb_interface_descriptor_t *idesc;
1157 struct usb_endpoint_desc *ed;
1158 usb_endpoint_descriptor_t *edesc;
1159 struct usb_alt_interface *ai;
1160 struct usb_string_desc *si;
1163 DPRINTFN(5, ("ugenioctl: cmd=%08lx\n", cmd));
1168 case USB_SET_SHORT_XFER:
1169 /* This flag only affects read */
1170 if (endpt == USB_CONTROL_ENDPOINT)
1172 sce = &sc->sc_endpoints[endpt][IN];
1174 if (sce->pipeh == NULL) {
1175 kprintf("ugenioctl: USB_SET_SHORT_XFER, no pipe\n");
1180 sce->state |= UGEN_SHORT_OK;
1182 sce->state &= ~UGEN_SHORT_OK;
1184 case USB_SET_TIMEOUT:
1185 sce = &sc->sc_endpoints[endpt][IN];
1186 sce->timeout = *(int *)addr;
1187 sce = &sc->sc_endpoints[endpt][OUT];
1188 sce->timeout = *(int *)addr;
1194 if (endpt != USB_CONTROL_ENDPOINT)
1200 ugendebug = *(int *)addr;
1203 case USB_GET_CONFIG:
1204 err = usbd_get_config(sc->sc_udev, &conf);
1207 *(int *)addr = conf;
1209 case USB_SET_CONFIG:
1210 if (!(flag & FWRITE))
1212 err = ugen_set_config(sc, *(int *)addr);
1214 case USBD_NORMAL_COMPLETION:
1222 case USB_GET_ALTINTERFACE:
1223 ai = (struct usb_alt_interface *)addr;
1224 err = usbd_device2interface_handle(sc->sc_udev,
1225 ai->uai_interface_index, &iface);
1228 idesc = usbd_get_interface_descriptor(iface);
1231 ai->uai_alt_no = idesc->bAlternateSetting;
1233 case USB_SET_ALTINTERFACE:
1234 if (!(flag & FWRITE))
1236 ai = (struct usb_alt_interface *)addr;
1237 err = usbd_device2interface_handle(sc->sc_udev,
1238 ai->uai_interface_index, &iface);
1241 err = ugen_set_interface(sc, ai->uai_interface_index, ai->uai_alt_no);
1245 case USB_GET_NO_ALT:
1246 ai = (struct usb_alt_interface *)addr;
1247 cdesc = ugen_get_cdesc(sc, ai->uai_config_index, 0);
1250 idesc = usbd_find_idesc(cdesc, ai->uai_interface_index, 0);
1251 if (idesc == NULL) {
1252 kfree(cdesc, M_TEMP);
1255 ai->uai_alt_no = usbd_get_no_alts(cdesc, idesc->bInterfaceNumber);
1256 kfree(cdesc, M_TEMP);
1258 case USB_GET_DEVICE_DESC:
1259 *(usb_device_descriptor_t *)addr =
1260 *usbd_get_device_descriptor(sc->sc_udev);
1262 case USB_GET_CONFIG_DESC:
1263 cd = (struct usb_config_desc *)addr;
1264 cdesc = ugen_get_cdesc(sc, cd->ucd_config_index, 0);
1267 cd->ucd_desc = *cdesc;
1268 kfree(cdesc, M_TEMP);
1270 case USB_GET_INTERFACE_DESC:
1271 id = (struct usb_interface_desc *)addr;
1272 cdesc = ugen_get_cdesc(sc, id->uid_config_index, 0);
1275 if (id->uid_config_index == USB_CURRENT_CONFIG_INDEX &&
1276 id->uid_alt_index == USB_CURRENT_ALT_INDEX)
1277 alt = ugen_get_alt_index(sc, id->uid_interface_index);
1279 alt = id->uid_alt_index;
1280 idesc = usbd_find_idesc(cdesc, id->uid_interface_index, alt);
1281 if (idesc == NULL) {
1282 kfree(cdesc, M_TEMP);
1285 id->uid_desc = *idesc;
1286 kfree(cdesc, M_TEMP);
1288 case USB_GET_ENDPOINT_DESC:
1289 ed = (struct usb_endpoint_desc *)addr;
1290 cdesc = ugen_get_cdesc(sc, ed->ued_config_index, 0);
1293 if (ed->ued_config_index == USB_CURRENT_CONFIG_INDEX &&
1294 ed->ued_alt_index == USB_CURRENT_ALT_INDEX)
1295 alt = ugen_get_alt_index(sc, ed->ued_interface_index);
1297 alt = ed->ued_alt_index;
1298 edesc = usbd_find_edesc(cdesc, ed->ued_interface_index,
1299 alt, ed->ued_endpoint_index);
1300 if (edesc == NULL) {
1301 kfree(cdesc, M_TEMP);
1304 ed->ued_desc = *edesc;
1305 kfree(cdesc, M_TEMP);
1307 case USB_GET_FULL_DESC:
1312 struct usb_full_desc *fd = (struct usb_full_desc *)addr;
1315 cdesc = ugen_get_cdesc(sc, fd->ufd_config_index, &len);
1316 if (len > fd->ufd_size)
1318 iov.iov_base = (caddr_t)fd->ufd_data;
1322 uio.uio_resid = len;
1324 uio.uio_segflg = UIO_USERSPACE;
1325 uio.uio_rw = UIO_READ;
1326 uio.uio_td = curthread;
1327 error = uiomove((void *)cdesc, len, &uio);
1328 kfree(cdesc, M_TEMP);
1331 case USB_GET_STRING_DESC:
1335 si = (struct usb_string_desc *)addr;
1336 err = usbd_get_string_desc(sc->sc_udev, si->usd_string_index,
1337 si->usd_language_id, &si->usd_desc, &size);
1342 case USB_DO_REQUEST:
1344 struct usb_ctl_request *ur = (void *)addr;
1345 int len = UGETW(ur->ucr_request.wLength);
1352 if (!(flag & FWRITE))
1354 /* Avoid requests that would damage the bus integrity. */
1355 if ((ur->ucr_request.bmRequestType == UT_WRITE_DEVICE &&
1356 ur->ucr_request.bRequest == UR_SET_ADDRESS) ||
1357 (ur->ucr_request.bmRequestType == UT_WRITE_DEVICE &&
1358 ur->ucr_request.bRequest == UR_SET_CONFIG) ||
1359 (ur->ucr_request.bmRequestType == UT_WRITE_INTERFACE &&
1360 ur->ucr_request.bRequest == UR_SET_INTERFACE))
1363 if (len < 0 || len > 32767)
1366 iov.iov_base = (caddr_t)ur->ucr_data;
1370 uio.uio_resid = len;
1372 uio.uio_segflg = UIO_USERSPACE;
1374 ur->ucr_request.bmRequestType & UT_READ ?
1375 UIO_READ : UIO_WRITE;
1376 uio.uio_td = curthread;
1377 ptr = kmalloc(len, M_TEMP, M_WAITOK);
1378 if (uio.uio_rw == UIO_WRITE) {
1379 error = uiomove(ptr, len, &uio);
1384 sce = &sc->sc_endpoints[endpt][IN];
1385 err = usbd_do_request_flags(sc->sc_udev, &ur->ucr_request,
1386 ptr, ur->ucr_flags, &ur->ucr_actlen, sce->timeout);
1392 if (uio.uio_rw == UIO_READ) {
1393 error = uiomove(ptr, len, &uio);
1403 case USB_GET_DEVICEINFO:
1404 usbd_fill_deviceinfo(sc->sc_udev,
1405 (struct usb_device_info *)addr, 1);
1414 ugenioctl(struct dev_ioctl_args *ap)
1416 cdev_t dev = ap->a_head.a_dev;
1417 int endpt = UGENENDPOINT(dev);
1418 struct ugen_softc *sc;
1421 sc = devclass_get_softc(ugen_devclass, UGENUNIT(dev));
1426 error = ugen_do_ioctl(sc, endpt, ap->a_cmd, ap->a_data, ap->a_fflag);
1427 if (--sc->sc_refcnt < 0)
1428 usb_detach_wakeup(sc->sc_dev);
1433 ugenpoll(struct dev_poll_args *ap)
1435 cdev_t dev = ap->a_head.a_dev;
1436 struct ugen_softc *sc;
1437 struct ugen_endpoint *sce_in, *sce_out;
1438 usb_endpoint_descriptor_t *edesc;
1441 sc = devclass_get_softc(ugen_devclass, UGENUNIT(dev));
1444 return ((ap->a_events & (POLLIN | POLLOUT | POLLRDNORM |
1445 POLLWRNORM)) | POLLHUP);
1448 /* Do not allow to poll a control endpoint */
1449 if (UGENENDPOINT(dev) == USB_CONTROL_ENDPOINT) {
1450 return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM |
1454 sce_in = &sc->sc_endpoints[UGENENDPOINT(dev)][IN];
1455 sce_out = &sc->sc_endpoints[UGENENDPOINT(dev)][OUT];
1456 edesc = (sce_in->edesc != NULL) ? sce_in->edesc : sce_out->edesc;
1457 KASSERT(edesc != NULL, ("ugenpoll: NULL edesc"));
1459 if (sce_in->edesc == NULL || sce_in->pipeh == NULL)
1461 if (sce_out->edesc == NULL || sce_out->pipeh == NULL)
1465 switch (edesc->bmAttributes & UE_XFERTYPE) {
1467 if (sce_in != NULL && (ap->a_events & (POLLIN | POLLRDNORM))) {
1468 if (sce_in->q.c_cc > 0)
1469 revents |= ap->a_events & (POLLIN | POLLRDNORM);
1471 selrecord(curthread, &sce_in->rsel);
1473 if (sce_out != NULL && (ap->a_events & (POLLOUT | POLLWRNORM))) {
1474 if (sce_out->q.c_cc > 0)
1475 revents |= ap->a_events & (POLLOUT | POLLWRNORM);
1477 selrecord(curthread, &sce_out->rsel);
1480 case UE_ISOCHRONOUS:
1481 if (sce_in != NULL && (ap->a_events & (POLLIN | POLLRDNORM))) {
1482 if (sce_in->cur != sce_in->fill)
1483 revents |= ap->a_events & (POLLIN | POLLRDNORM);
1485 selrecord(curthread, &sce_in->rsel);
1487 if (sce_out != NULL && (ap->a_events & (POLLOUT | POLLWRNORM))) {
1488 if (sce_out->cur != sce_out->fill)
1489 revents |= ap->a_events & (POLLOUT | POLLWRNORM);
1491 selrecord(curthread, &sce_out->rsel);
1496 * We have no easy way of determining if a read will
1497 * yield any data or a write will happen.
1498 * Pretend they will.
1500 revents |= ap->a_events &
1501 (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM);
1507 ap->a_events = revents;
1511 static struct filterops ugen_filtops_read =
1512 { 1, NULL, ugen_filt_detach, ugen_filt_read };
1513 static struct filterops ugen_filtops_write =
1514 { 1, NULL, ugen_filt_detach, ugen_filt_write };
1517 ugenkqfilter(struct dev_kqfilter_args *ap)
1519 cdev_t dev = ap->a_head.a_dev;
1520 struct knote *kn = ap->a_kn;
1521 struct klist *klist;
1522 struct ugen_softc *sc;
1523 struct ugen_endpoint *sce;
1525 sc = devclass_get_softc(ugen_devclass, UGENUNIT(dev));
1532 /* Do not allow filter on a control endpoint */
1533 if (UGENENDPOINT(dev) == USB_CONTROL_ENDPOINT)
1538 switch (kn->kn_filter) {
1540 sce = &sc->sc_endpoints[UGENENDPOINT(dev)][IN];
1541 kn->kn_fop = &ugen_filtops_read;
1542 kn->kn_hook = (caddr_t)dev;
1545 sce = &sc->sc_endpoints[UGENENDPOINT(dev)][OUT];
1546 kn->kn_fop = &ugen_filtops_write;
1547 kn->kn_hook = (caddr_t)dev;
1554 if (sce->edesc != NULL || sce->pipeh != NULL) {
1556 klist = &sce->rsel.si_note;
1557 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
1565 ugen_filt_detach(struct knote *kn)
1567 cdev_t dev = (cdev_t)kn->kn_hook;
1568 struct ugen_softc *sc;
1569 struct ugen_endpoint *sce;
1570 struct klist *klist;
1572 sc = devclass_get_softc(ugen_devclass, UGENUNIT(dev));
1574 switch (kn->kn_filter) {
1576 sce = &sc->sc_endpoints[UGENENDPOINT(dev)][IN];
1579 sce = &sc->sc_endpoints[UGENENDPOINT(dev)][OUT];
1585 if (sce->edesc != NULL || sce->pipeh != NULL) {
1587 klist = &sce->rsel.si_note;
1588 SLIST_REMOVE(klist, kn, knote, kn_selnext);
1594 ugen_filt_read(struct knote *kn, long hint)
1596 cdev_t dev = (cdev_t)kn->kn_hook;
1597 struct ugen_softc *sc;
1598 struct ugen_endpoint *sce;
1601 sc = devclass_get_softc(ugen_devclass, UGENUNIT(dev));
1602 sce = &sc->sc_endpoints[UGENENDPOINT(dev)][IN];
1604 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
1606 if (sce->q.c_cc > 0)
1609 case UE_ISOCHRONOUS:
1610 if (sce->cur != sce->fill)
1624 ugen_filt_write(struct knote *kn, long hint)
1626 cdev_t dev = (cdev_t)kn->kn_hook;
1627 struct ugen_softc *sc;
1628 struct ugen_endpoint *sce;
1631 sc = devclass_get_softc(ugen_devclass, UGENUNIT(dev));
1632 sce = &sc->sc_endpoints[UGENENDPOINT(dev)][OUT];
1634 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
1636 if (sce->q.c_cc > 0)
1639 case UE_ISOCHRONOUS:
1640 if (sce->cur != sce->fill)
1653 DRIVER_MODULE(ugen, uhub, ugen_driver, ugen_devclass, usbd_driver_load, 0);