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.30 2007/07/02 06:43:31 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>
59 #include <sys/ioccom.h>
61 #include <sys/fcntl.h>
62 #include <sys/filio.h>
65 #include <sys/select.h>
66 #include <sys/vnode.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];
145 #define UGEN_CDEV_MAJOR 114
147 static struct dev_ops ugen_ops = {
148 { "ugen", UGEN_CDEV_MAJOR, 0 },
150 .d_close = ugenclose,
152 .d_write = ugenwrite,
153 .d_ioctl = ugenioctl,
157 static void ugenintr(usbd_xfer_handle xfer, usbd_private_handle addr,
159 static void ugen_isoc_rintr(usbd_xfer_handle xfer, usbd_private_handle addr,
161 static int ugen_do_read(struct ugen_softc *, int, struct uio *, int);
162 static int ugen_do_write(struct ugen_softc *, int, struct uio *, int);
163 static int ugen_do_ioctl(struct ugen_softc *, int, u_long,
165 static void ugen_make_devnodes(struct ugen_softc *sc);
166 static void ugen_destroy_devnodes(struct ugen_softc *sc);
167 static int ugen_set_config(struct ugen_softc *sc, int configno);
168 static usb_config_descriptor_t *ugen_get_cdesc(struct ugen_softc *sc,
169 int index, int *lenp);
170 static usbd_status ugen_set_interface(struct ugen_softc *, int, int);
171 static int ugen_get_alt_index(struct ugen_softc *sc, int ifaceidx);
173 #define UGENUNIT(n) ((lminor(n) >> 4) & 0xff)
174 #define UGENENDPOINT(n) (minor(n) & 0xf)
175 #define UGENMINOR(u, e) (((u & 0xf) << 4) | ((u & 0xf0) << 12) | (e))
176 #define UGENUNITMASK 0xffff00f0
178 USB_DECLARE_DRIVER(ugen);
181 ugen_match(device_t self)
183 struct usb_attach_arg *uaa = device_get_ivars(self);
187 return (uaa->matchlvl);
190 return (UMATCH_GENERIC);
192 return (UMATCH_NONE);
196 ugen_attach(device_t self)
198 struct ugen_softc *sc = device_get_softc(self);
199 struct usb_attach_arg *uaa = device_get_ivars(self);
200 usbd_device_handle udev;
205 usbd_devinfo(uaa->device, 0, devinfo);
207 device_set_desc_copy(self, devinfo);
208 kprintf("%s: %s\n", device_get_nameunit(sc->sc_dev), devinfo);
210 sc->sc_udev = udev = uaa->device;
212 memset(sc->sc_endpoints, 0, sizeof sc->sc_endpoints);
214 /* First set configuration index 0, the default one for ugen. */
215 err = usbd_set_config_index(udev, 0, 0);
217 kprintf("%s: setting configuration index 0 failed\n",
218 device_get_nameunit(sc->sc_dev));
222 conf = usbd_get_config_descriptor(udev)->bConfigurationValue;
224 /* Set up all the local state for this configuration. */
225 err = ugen_set_config(sc, conf);
227 kprintf("%s: setting configuration %d failed\n",
228 device_get_nameunit(sc->sc_dev), conf);
233 /* the main device, ctrl endpoint */
234 dev_ops_add(&ugen_ops,
235 UGENUNITMASK, UGENMINOR(device_get_unit(sc->sc_dev), 0));
236 make_dev(&ugen_ops, UGENMINOR(device_get_unit(sc->sc_dev), 0),
237 UID_ROOT, GID_OPERATOR, 0644, "%s", device_get_nameunit(sc->sc_dev));
243 ugen_make_devnodes(struct ugen_softc *sc)
248 for (endptno = 1; endptno < USB_MAX_ENDPOINTS; endptno++) {
249 if (sc->sc_endpoints[endptno][IN].sc != NULL ||
250 sc->sc_endpoints[endptno][OUT].sc != NULL ) {
251 /* endpt can be 0x81 and 0x01, representing
252 * endpoint address 0x01 and IN/OUT directions.
253 * We map both endpts to the same device,
254 * IN is reading from it, OUT is writing to it.
256 * In the if clause above we check whether one
257 * of the structs is populated.
259 dev = make_dev(&ugen_ops,
260 UGENMINOR(device_get_unit(sc->sc_dev), endptno),
261 UID_ROOT, GID_OPERATOR, 0644,
263 device_get_nameunit(sc->sc_dev), endptno);
264 if (sc->sc_endpoints[endptno][IN].sc != NULL) {
266 if (sc->sc_endpoints[endptno][IN].dev)
267 release_dev(sc->sc_endpoints[endptno][IN].dev);
268 sc->sc_endpoints[endptno][IN].dev = dev;
270 if (sc->sc_endpoints[endptno][OUT].sc != NULL) {
272 if (sc->sc_endpoints[endptno][OUT].dev)
273 release_dev(sc->sc_endpoints[endptno][OUT].dev);
274 sc->sc_endpoints[endptno][OUT].dev = dev;
281 ugen_destroy_devnodes(struct ugen_softc *sc)
286 /* destroy all devices for the other (existing) endpoints as well */
287 for (endptno = 1; endptno < USB_MAX_ENDPOINTS; endptno++) {
288 if (sc->sc_endpoints[endptno][IN].sc != NULL ||
289 sc->sc_endpoints[endptno][OUT].sc != NULL ) {
290 /* endpt can be 0x81 and 0x01, representing
291 * endpoint address 0x01 and IN/OUT directions.
292 * We map both endpoint addresses to the same device,
293 * IN is reading from it, OUT is writing to it.
295 * In the if clause above we check whether one
296 * of the structs is populated.
298 dev = sc->sc_endpoints[endptno][IN].dev;
301 sc->sc_endpoints[endptno][IN].dev = NULL;
303 dev = sc->sc_endpoints[endptno][OUT].dev;
306 sc->sc_endpoints[endptno][OUT].dev = NULL;
313 ugen_set_config(struct ugen_softc *sc, int configno)
315 usbd_device_handle dev = sc->sc_udev;
316 usbd_interface_handle iface;
317 usb_endpoint_descriptor_t *ed;
318 struct ugen_endpoint *sce;
319 u_int8_t niface, nendpt;
320 int ifaceno, endptno, endpt;
324 DPRINTFN(1,("ugen_set_config: %s to configno %d, sc=%p\n",
325 device_get_nameunit(sc->sc_dev), configno, sc));
327 ugen_destroy_devnodes(sc);
329 /* We start at 1, not 0, because we don't care whether the
330 * control endpoint is open or not. It is always present.
332 for (endptno = 1; endptno < USB_MAX_ENDPOINTS; endptno++) {
333 if (sc->sc_is_open[endptno]) {
335 ("ugen_set_config: %s - endpoint %d is open\n",
336 device_get_nameunit(sc->sc_dev), endptno));
337 return (USBD_IN_USE);
341 /* Avoid setting the current value. */
342 if (usbd_get_config_descriptor(dev)->bConfigurationValue != configno) {
343 err = usbd_set_config_no(dev, configno, 1);
348 err = usbd_interface_count(dev, &niface);
351 memset(sc->sc_endpoints, 0, sizeof sc->sc_endpoints);
352 for (ifaceno = 0; ifaceno < niface; ifaceno++) {
353 DPRINTFN(1,("ugen_set_config: ifaceno %d\n", ifaceno));
354 err = usbd_device2interface_handle(dev, ifaceno, &iface);
357 err = usbd_endpoint_count(iface, &nendpt);
360 for (endptno = 0; endptno < nendpt; endptno++) {
361 ed = usbd_interface2endpoint_descriptor(iface,endptno);
362 endpt = ed->bEndpointAddress;
363 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
364 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
365 DPRINTFN(1,("ugen_set_config: endptno %d, endpt=0x%02x"
367 endptno, endpt, UE_GET_ADDR(endpt),
368 UE_GET_DIR(endpt), sce));
375 ugen_make_devnodes(sc);
377 return (USBD_NORMAL_COMPLETION);
381 ugenopen(struct dev_open_args *ap)
383 cdev_t dev = ap->a_head.a_dev;
384 struct ugen_softc *sc;
385 int unit = UGENUNIT(dev);
386 int endpt = UGENENDPOINT(dev);
387 usb_endpoint_descriptor_t *edesc;
388 struct ugen_endpoint *sce;
391 usbd_xfer_handle xfer;
395 sc = devclass_get_softc(ugen_devclass, unit);
399 DPRINTFN(5, ("ugenopen: flag=%d, mode=%d, unit=%d endpt=%d\n",
400 ap->a_oflags, ap->a_devtype, unit, endpt));
402 if (sc == NULL || sc->sc_dying)
405 if (sc->sc_is_open[endpt])
408 if (endpt == USB_CONTROL_ENDPOINT) {
409 sc->sc_is_open[USB_CONTROL_ENDPOINT] = 1;
413 /* Make sure there are pipes for all directions. */
414 for (dir = OUT; dir <= IN; dir++) {
415 if (ap->a_oflags & (dir == OUT ? FWRITE : FREAD)) {
416 sce = &sc->sc_endpoints[endpt][dir];
417 if (sce == 0 || sce->edesc == 0)
422 /* Actually open the pipes. */
423 /* XXX Should back out properly if it fails. */
424 for (dir = OUT; dir <= IN; dir++) {
425 if (!(ap->a_oflags & (dir == OUT ? FWRITE : FREAD)))
427 sce = &sc->sc_endpoints[endpt][dir];
429 sce->timeout = USBD_NO_TIMEOUT;
430 DPRINTFN(5, ("ugenopen: sc=%p, endpt=%d, dir=%d, sce=%p\n",
431 sc, endpt, dir, sce));
433 switch (edesc->bmAttributes & UE_XFERTYPE) {
436 err = usbd_open_pipe(sce->iface,
437 edesc->bEndpointAddress, 0, &sce->pipeh);
442 isize = UGETW(edesc->wMaxPacketSize);
443 if (isize == 0) /* shouldn't happen */
445 sce->ibuf = kmalloc(isize, M_USBDEV, M_WAITOK);
446 DPRINTFN(5, ("ugenopen: intr endpt=%d,isize=%d\n",
448 if (clalloc(&sce->q, UGEN_IBSIZE, 0) == -1)
450 err = usbd_open_pipe_intr(sce->iface,
451 edesc->bEndpointAddress,
452 USBD_SHORT_XFER_OK, &sce->pipeh, sce,
453 sce->ibuf, isize, ugenintr,
454 USBD_DEFAULT_INTERVAL);
456 kfree(sce->ibuf, M_USBDEV);
460 DPRINTFN(5, ("ugenopen: interrupt open done\n"));
463 err = usbd_open_pipe(sce->iface,
464 edesc->bEndpointAddress, 0, &sce->pipeh);
471 isize = UGETW(edesc->wMaxPacketSize);
472 if (isize == 0) /* shouldn't happen */
474 sce->ibuf = kmalloc(isize * UGEN_NISOFRAMES,
476 sce->cur = sce->fill = sce->ibuf;
477 sce->limit = sce->ibuf + isize * UGEN_NISOFRAMES;
478 DPRINTFN(5, ("ugenopen: isoc endpt=%d, isize=%d\n",
480 err = usbd_open_pipe(sce->iface,
481 edesc->bEndpointAddress, 0, &sce->pipeh);
483 kfree(sce->ibuf, M_USBDEV);
486 for(i = 0; i < UGEN_NISOREQS; ++i) {
487 sce->isoreqs[i].sce = sce;
488 xfer = usbd_alloc_xfer(sc->sc_udev);
491 sce->isoreqs[i].xfer = xfer;
492 buf = usbd_alloc_buffer
493 (xfer, isize * UGEN_NISORFRMS);
498 sce->isoreqs[i].dmabuf = buf;
499 for(j = 0; j < UGEN_NISORFRMS; ++j)
500 sce->isoreqs[i].sizes[j] = isize;
502 (xfer, sce->pipeh, &sce->isoreqs[i],
503 sce->isoreqs[i].sizes,
504 UGEN_NISORFRMS, USBD_NO_COPY,
506 (void)usbd_transfer(xfer);
508 DPRINTFN(5, ("ugenopen: isoc open done\n"));
511 while (--i >= 0) /* implicit buffer free */
512 usbd_free_xfer(sce->isoreqs[i].xfer);
515 sce->timeout = USBD_DEFAULT_TIMEOUT;
519 sc->sc_is_open[endpt] = 1;
524 ugenclose(struct dev_close_args *ap)
526 cdev_t dev = ap->a_head.a_dev;
527 int endpt = UGENENDPOINT(dev);
528 struct ugen_softc *sc;
529 struct ugen_endpoint *sce;
533 sc = devclass_get_softc(ugen_devclass, UGENUNIT(dev));
535 DPRINTFN(5, ("ugenclose: flag=%d, mode=%d, unit=%d, endpt=%d\n",
536 ap->a_fflag, ap->a_devtype, UGENUNIT(dev), endpt));
539 if (!sc->sc_is_open[endpt]) {
540 kprintf("ugenclose: not open\n");
545 if (endpt == USB_CONTROL_ENDPOINT) {
546 DPRINTFN(5, ("ugenclose: close control\n"));
547 sc->sc_is_open[endpt] = 0;
551 for (dir = OUT; dir <= IN; dir++) {
552 if (!(ap->a_fflag & (dir == OUT ? FWRITE : FREAD)))
554 sce = &sc->sc_endpoints[endpt][dir];
555 if (sce == NULL || sce->pipeh == NULL)
557 DPRINTFN(5, ("ugenclose: endpt=%d dir=%d sce=%p\n",
560 usbd_abort_pipe(sce->pipeh);
561 usbd_close_pipe(sce->pipeh);
564 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
566 ndflush(&sce->q, sce->q.c_cc);
570 for (i = 0; i < UGEN_NISOREQS; ++i)
571 usbd_free_xfer(sce->isoreqs[i].xfer);
576 if (sce->ibuf != NULL) {
577 kfree(sce->ibuf, M_USBDEV);
582 sc->sc_is_open[endpt] = 0;
588 ugen_do_read(struct ugen_softc *sc, int endpt, struct uio *uio, int flag)
590 struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][IN];
593 usbd_xfer_handle xfer;
597 u_char buffer[UGEN_CHUNK];
599 DPRINTFN(5, ("%s: ugenread: %d\n", device_get_nameunit(sc->sc_dev), endpt));
604 if (endpt == USB_CONTROL_ENDPOINT)
610 if (sce->edesc == NULL) {
611 kprintf("ugenread: no edesc\n");
614 if (sce->pipeh == NULL) {
615 kprintf("ugenread: no pipe\n");
619 buf = getugenbuf(ugen_bufsize, &ugen_bbsize);
621 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
623 /* Block until activity occurred. */
625 while (sce->q.c_cc == 0) {
626 if (flag & IO_NDELAY) {
631 sce->state |= UGEN_ASLP;
632 DPRINTFN(5, ("ugenread: sleep on %p\n", sce));
633 error = tsleep(sce, PCATCH, "ugenri", 0);
634 DPRINTFN(5, ("ugenread: woke, error=%d\n", error));
638 sce->state &= ~UGEN_ASLP;
644 /* Transfer as many chunks as possible. */
645 while (sce->q.c_cc > 0 && uio->uio_resid > 0 && !error) {
646 n = min(sce->q.c_cc, uio->uio_resid);
647 if (n > sizeof(buffer))
650 /* Remove a small chunk from the input queue. */
651 q_to_b(&sce->q, buffer, n);
652 DPRINTFN(5, ("ugenread: got %d chars\n", n));
654 /* Copy the data to the user process. */
655 error = uiomove(buffer, n, uio);
661 xfer = usbd_alloc_xfer(sc->sc_udev);
666 while ((n = min(ugen_bbsize, uio->uio_resid)) != 0) {
667 DPRINTFN(1, ("ugenread: start transfer %d bytes\n",n));
669 err = usbd_bulk_transfer(
671 sce->state & UGEN_SHORT_OK ?
672 USBD_SHORT_XFER_OK : 0,
673 sce->timeout, buf, &tn, "ugenrb");
675 if (err == USBD_INTERRUPTED)
677 else if (err == USBD_TIMEOUT)
683 DPRINTFN(1, ("ugenread: got %d bytes\n", tn));
684 error = uiomove(buf, tn, uio);
688 usbd_free_xfer(xfer);
692 while (sce->cur == sce->fill) {
693 if (flag & IO_NDELAY) {
698 sce->state |= UGEN_ASLP;
699 DPRINTFN(5, ("ugenread: sleep on %p\n", sce));
700 error = tsleep(sce, PCATCH, "ugenri", 0);
701 DPRINTFN(5, ("ugenread: woke, error=%d\n", error));
705 sce->state &= ~UGEN_ASLP;
710 while (sce->cur != sce->fill && uio->uio_resid > 0 && !error) {
711 if(sce->fill > sce->cur)
712 n = min(sce->fill - sce->cur, uio->uio_resid);
714 n = min(sce->limit - sce->cur, uio->uio_resid);
716 DPRINTFN(5, ("ugenread: isoc got %d chars\n", n));
718 /* Copy the data to the user process. */
719 error = uiomove(sce->cur, n, uio);
723 if(sce->cur >= sce->limit)
724 sce->cur = sce->ibuf;
735 relugenbuf(buf, ugen_bbsize);
740 ugenread(struct dev_read_args *ap)
742 cdev_t dev = ap->a_head.a_dev;
743 int endpt = UGENENDPOINT(dev);
744 struct ugen_softc *sc;
747 sc = devclass_get_softc(ugen_devclass, UGENUNIT(dev));
750 error = ugen_do_read(sc, endpt, ap->a_uio, ap->a_ioflag);
751 if (--sc->sc_refcnt < 0)
752 usb_detach_wakeup(sc->sc_dev);
757 ugen_do_write(struct ugen_softc *sc, int endpt, struct uio *uio, int flag)
759 struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][OUT];
764 usbd_xfer_handle xfer;
767 DPRINTFN(5, ("%s: ugenwrite: %d\n", device_get_nameunit(sc->sc_dev), endpt));
772 if (endpt == USB_CONTROL_ENDPOINT)
778 if (sce->edesc == NULL) {
779 kprintf("ugenwrite: no edesc\n");
782 if (sce->pipeh == NULL) {
783 kprintf("ugenwrite: no pipe\n");
787 buf = getugenbuf(ugen_bufsize, &ugen_bbsize);
789 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
791 xfer = usbd_alloc_xfer(sc->sc_udev);
796 while ((n = min(ugen_bbsize, uio->uio_resid)) != 0) {
797 error = uiomove(buf, n, uio);
800 DPRINTFN(1, ("ugenwrite: transfer %d bytes\n", n));
801 err = usbd_bulk_transfer(xfer, sce->pipeh, 0,
802 sce->timeout, buf, &n,"ugenwb");
804 if (err == USBD_INTERRUPTED)
806 else if (err == USBD_TIMEOUT)
813 usbd_free_xfer(xfer);
816 xfer = usbd_alloc_xfer(sc->sc_udev);
821 while ((n = min(UGETW(sce->edesc->wMaxPacketSize),
822 uio->uio_resid)) != 0) {
823 error = uiomove(buf, n, uio);
826 DPRINTFN(1, ("ugenwrite: transfer %d bytes\n", n));
827 err = usbd_intr_transfer(xfer, sce->pipeh, 0,
828 sce->timeout, buf, &n,"ugenwi");
830 if (err == USBD_INTERRUPTED)
832 else if (err == USBD_TIMEOUT)
839 usbd_free_xfer(xfer);
846 relugenbuf(buf, ugen_bbsize);
851 ugenwrite(struct dev_write_args *ap)
853 cdev_t dev = ap->a_head.a_dev;
854 int endpt = UGENENDPOINT(dev);
855 struct ugen_softc *sc;
858 sc = devclass_get_softc(ugen_devclass, UGENUNIT(dev));
861 error = ugen_do_write(sc, endpt, ap->a_uio, ap->a_ioflag);
862 if (--sc->sc_refcnt < 0)
863 usb_detach_wakeup(sc->sc_dev);
868 ugen_detach(device_t self)
870 struct ugen_softc *sc = device_get_softc(self);
871 struct ugen_endpoint *sce;
874 DPRINTF(("ugen_detach: sc=%p\n", sc));
877 /* Abort all pipes. Causes processes waiting for transfer to wake. */
878 for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
879 for (dir = OUT; dir <= IN; dir++) {
880 sce = &sc->sc_endpoints[i][dir];
881 if (sce && sce->pipeh)
882 usbd_abort_pipe(sce->pipeh);
886 if (--sc->sc_refcnt >= 0) {
888 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
889 wakeup(&sc->sc_endpoints[i][IN]);
890 /* Wait for processes to go away. */
891 usb_detach_wait(sc->sc_dev);
895 /* destroy the device for the control endpoint */
896 ugen_destroy_devnodes(sc);
897 dev_ops_remove(&ugen_ops,
898 UGENUNITMASK, UGENMINOR(device_get_unit(sc->sc_dev), 0));
903 ugenintr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
905 struct ugen_endpoint *sce = addr;
906 /*struct ugen_softc *sc = sce->sc;*/
910 if (status == USBD_CANCELLED)
913 if (status != USBD_NORMAL_COMPLETION) {
914 DPRINTF(("ugenintr: status=%d\n", status));
915 if (status == USBD_STALLED)
916 usbd_clear_endpoint_stall_async(sce->pipeh);
920 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
923 DPRINTFN(5, ("ugenintr: xfer=%p status=%d count=%d\n",
924 xfer, status, count));
925 DPRINTFN(5, (" data = %02x %02x %02x\n",
926 ibuf[0], ibuf[1], ibuf[2]));
928 (void)b_to_q(ibuf, count, &sce->q);
930 if (sce->state & UGEN_ASLP) {
931 sce->state &= ~UGEN_ASLP;
932 DPRINTFN(5, ("ugen_intr: waking %p\n", sce));
935 selwakeuppri(&sce->rsel, 0);
939 ugen_isoc_rintr(usbd_xfer_handle xfer, usbd_private_handle addr,
942 struct isoreq *req = addr;
943 struct ugen_endpoint *sce = req->sce;
947 /* Return if we are aborting. */
948 if (status == USBD_CANCELLED)
951 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
952 DPRINTFN(5,("ugen_isoc_rintr: xfer %d, count=%d\n",
953 (int)(req - sce->isoreqs),
956 /* throw away oldest input if the buffer is full */
957 if(sce->fill < sce->cur && sce->cur <= sce->fill + count) {
959 if(sce->cur >= sce->limit)
960 sce->cur = sce->ibuf + (sce->limit - sce->cur);
961 DPRINTFN(5, ("ugen_isoc_rintr: throwing away %d bytes\n",
965 isize = UGETW(sce->edesc->wMaxPacketSize);
966 for (i = 0; i < UGEN_NISORFRMS; i++) {
967 u_int32_t actlen = req->sizes[i];
968 char const *buf = (char const *)req->dmabuf + isize * i;
970 /* copy data to buffer */
972 n = min(actlen, sce->limit - sce->fill);
973 memcpy(sce->fill, buf, n);
978 if(sce->fill == sce->limit)
979 sce->fill = sce->ibuf;
982 /* setup size for next transfer */
983 req->sizes[i] = isize;
986 usbd_setup_isoc_xfer(xfer, sce->pipeh, req, req->sizes, UGEN_NISORFRMS,
987 USBD_NO_COPY, ugen_isoc_rintr);
988 (void)usbd_transfer(xfer);
990 if (sce->state & UGEN_ASLP) {
991 sce->state &= ~UGEN_ASLP;
992 DPRINTFN(5, ("ugen_isoc_rintr: waking %p\n", sce));
995 selwakeuppri(&sce->rsel, 0);
999 ugen_set_interface(struct ugen_softc *sc, int ifaceidx, int altno)
1001 usbd_interface_handle iface;
1002 usb_endpoint_descriptor_t *ed;
1004 struct ugen_endpoint *sce;
1005 u_int8_t niface, nendpt, endptno, endpt;
1008 DPRINTFN(15, ("ugen_set_interface %d %d\n", ifaceidx, altno));
1010 err = usbd_interface_count(sc->sc_udev, &niface);
1013 if (ifaceidx < 0 || ifaceidx >= niface)
1014 return (USBD_INVAL);
1016 err = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface);
1019 err = usbd_endpoint_count(iface, &nendpt);
1023 /* destroy the existing devices, we remake the new ones in a moment */
1024 ugen_destroy_devnodes(sc);
1026 /* XXX should only do this after setting new altno has succeeded */
1027 for (endptno = 0; endptno < nendpt; endptno++) {
1028 ed = usbd_interface2endpoint_descriptor(iface,endptno);
1029 endpt = ed->bEndpointAddress;
1030 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
1031 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
1037 /* change setting */
1038 err = usbd_set_interface(iface, altno);
1042 err = usbd_endpoint_count(iface, &nendpt);
1045 for (endptno = 0; endptno < nendpt; endptno++) {
1046 ed = usbd_interface2endpoint_descriptor(iface,endptno);
1047 endpt = ed->bEndpointAddress;
1048 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
1049 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
1055 /* make the new devices */
1056 ugen_make_devnodes(sc);
1061 /* Retrieve a complete descriptor for a certain device and index. */
1062 static usb_config_descriptor_t *
1063 ugen_get_cdesc(struct ugen_softc *sc, int index, int *lenp)
1065 usb_config_descriptor_t *cdesc, *tdesc, cdescr;
1069 if (index == USB_CURRENT_CONFIG_INDEX) {
1070 tdesc = usbd_get_config_descriptor(sc->sc_udev);
1071 len = UGETW(tdesc->wTotalLength);
1074 cdesc = kmalloc(len, M_TEMP, M_INTWAIT);
1075 memcpy(cdesc, tdesc, len);
1076 DPRINTFN(5,("ugen_get_cdesc: current, len=%d\n", len));
1078 err = usbd_get_config_desc(sc->sc_udev, index, &cdescr);
1081 len = UGETW(cdescr.wTotalLength);
1082 DPRINTFN(5,("ugen_get_cdesc: index=%d, len=%d\n", index, len));
1085 cdesc = kmalloc(len, M_TEMP, M_INTWAIT);
1086 err = usbd_get_config_desc_full(sc->sc_udev, index, cdesc, len);
1088 kfree(cdesc, M_TEMP);
1096 ugen_get_alt_index(struct ugen_softc *sc, int ifaceidx)
1098 usbd_interface_handle iface;
1101 err = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface);
1104 return (usbd_get_interface_altindex(iface));
1108 ugen_do_ioctl(struct ugen_softc *sc, int endpt, u_long cmd,
1109 caddr_t addr, int flag)
1111 struct ugen_endpoint *sce;
1113 usbd_interface_handle iface;
1114 struct usb_config_desc *cd;
1115 usb_config_descriptor_t *cdesc;
1116 struct usb_interface_desc *id;
1117 usb_interface_descriptor_t *idesc;
1118 struct usb_endpoint_desc *ed;
1119 usb_endpoint_descriptor_t *edesc;
1120 struct usb_alt_interface *ai;
1121 struct usb_string_desc *si;
1124 DPRINTFN(5, ("ugenioctl: cmd=%08lx\n", cmd));
1129 case USB_SET_SHORT_XFER:
1130 /* This flag only affects read */
1131 if (endpt == USB_CONTROL_ENDPOINT)
1133 sce = &sc->sc_endpoints[endpt][IN];
1137 if (sce->pipeh == NULL) {
1138 kprintf("ugenioctl: USB_SET_SHORT_XFER, no pipe\n");
1143 sce->state |= UGEN_SHORT_OK;
1145 sce->state &= ~UGEN_SHORT_OK;
1147 case USB_SET_TIMEOUT:
1148 sce = &sc->sc_endpoints[endpt][IN];
1151 sce->timeout = *(int *)addr;
1157 if (endpt != USB_CONTROL_ENDPOINT)
1163 ugendebug = *(int *)addr;
1166 case USB_GET_CONFIG:
1167 err = usbd_get_config(sc->sc_udev, &conf);
1170 *(int *)addr = conf;
1172 case USB_SET_CONFIG:
1173 if (!(flag & FWRITE))
1175 err = ugen_set_config(sc, *(int *)addr);
1177 case USBD_NORMAL_COMPLETION:
1185 case USB_GET_ALTINTERFACE:
1186 ai = (struct usb_alt_interface *)addr;
1187 err = usbd_device2interface_handle(sc->sc_udev,
1188 ai->uai_interface_index, &iface);
1191 idesc = usbd_get_interface_descriptor(iface);
1194 ai->uai_alt_no = idesc->bAlternateSetting;
1196 case USB_SET_ALTINTERFACE:
1197 if (!(flag & FWRITE))
1199 ai = (struct usb_alt_interface *)addr;
1200 err = usbd_device2interface_handle(sc->sc_udev,
1201 ai->uai_interface_index, &iface);
1204 err = ugen_set_interface(sc, ai->uai_interface_index, ai->uai_alt_no);
1208 case USB_GET_NO_ALT:
1209 ai = (struct usb_alt_interface *)addr;
1210 cdesc = ugen_get_cdesc(sc, ai->uai_config_index, 0);
1213 idesc = usbd_find_idesc(cdesc, ai->uai_interface_index, 0);
1214 if (idesc == NULL) {
1215 kfree(cdesc, M_TEMP);
1218 ai->uai_alt_no = usbd_get_no_alts(cdesc, idesc->bInterfaceNumber);
1219 kfree(cdesc, M_TEMP);
1221 case USB_GET_DEVICE_DESC:
1222 *(usb_device_descriptor_t *)addr =
1223 *usbd_get_device_descriptor(sc->sc_udev);
1225 case USB_GET_CONFIG_DESC:
1226 cd = (struct usb_config_desc *)addr;
1227 cdesc = ugen_get_cdesc(sc, cd->ucd_config_index, 0);
1230 cd->ucd_desc = *cdesc;
1231 kfree(cdesc, M_TEMP);
1233 case USB_GET_INTERFACE_DESC:
1234 id = (struct usb_interface_desc *)addr;
1235 cdesc = ugen_get_cdesc(sc, id->uid_config_index, 0);
1238 if (id->uid_config_index == USB_CURRENT_CONFIG_INDEX &&
1239 id->uid_alt_index == USB_CURRENT_ALT_INDEX)
1240 alt = ugen_get_alt_index(sc, id->uid_interface_index);
1242 alt = id->uid_alt_index;
1243 idesc = usbd_find_idesc(cdesc, id->uid_interface_index, alt);
1244 if (idesc == NULL) {
1245 kfree(cdesc, M_TEMP);
1248 id->uid_desc = *idesc;
1249 kfree(cdesc, M_TEMP);
1251 case USB_GET_ENDPOINT_DESC:
1252 ed = (struct usb_endpoint_desc *)addr;
1253 cdesc = ugen_get_cdesc(sc, ed->ued_config_index, 0);
1256 if (ed->ued_config_index == USB_CURRENT_CONFIG_INDEX &&
1257 ed->ued_alt_index == USB_CURRENT_ALT_INDEX)
1258 alt = ugen_get_alt_index(sc, ed->ued_interface_index);
1260 alt = ed->ued_alt_index;
1261 edesc = usbd_find_edesc(cdesc, ed->ued_interface_index,
1262 alt, ed->ued_endpoint_index);
1263 if (edesc == NULL) {
1264 kfree(cdesc, M_TEMP);
1267 ed->ued_desc = *edesc;
1268 kfree(cdesc, M_TEMP);
1270 case USB_GET_FULL_DESC:
1275 struct usb_full_desc *fd = (struct usb_full_desc *)addr;
1278 cdesc = ugen_get_cdesc(sc, fd->ufd_config_index, &len);
1279 if (len > fd->ufd_size)
1281 iov.iov_base = (caddr_t)fd->ufd_data;
1285 uio.uio_resid = len;
1287 uio.uio_segflg = UIO_USERSPACE;
1288 uio.uio_rw = UIO_READ;
1289 uio.uio_td = curthread;
1290 error = uiomove((void *)cdesc, len, &uio);
1291 kfree(cdesc, M_TEMP);
1294 case USB_GET_STRING_DESC:
1298 si = (struct usb_string_desc *)addr;
1299 err = usbd_get_string_desc(sc->sc_udev, si->usd_string_index,
1300 si->usd_language_id, &si->usd_desc, &size);
1305 case USB_DO_REQUEST:
1307 struct usb_ctl_request *ur = (void *)addr;
1308 int len = UGETW(ur->ucr_request.wLength);
1315 if (!(flag & FWRITE))
1317 /* Avoid requests that would damage the bus integrity. */
1318 if ((ur->ucr_request.bmRequestType == UT_WRITE_DEVICE &&
1319 ur->ucr_request.bRequest == UR_SET_ADDRESS) ||
1320 (ur->ucr_request.bmRequestType == UT_WRITE_DEVICE &&
1321 ur->ucr_request.bRequest == UR_SET_CONFIG) ||
1322 (ur->ucr_request.bmRequestType == UT_WRITE_INTERFACE &&
1323 ur->ucr_request.bRequest == UR_SET_INTERFACE))
1326 if (len < 0 || len > 32767)
1329 iov.iov_base = (caddr_t)ur->ucr_data;
1333 uio.uio_resid = len;
1335 uio.uio_segflg = UIO_USERSPACE;
1337 ur->ucr_request.bmRequestType & UT_READ ?
1338 UIO_READ : UIO_WRITE;
1339 uio.uio_td = curthread;
1340 ptr = kmalloc(len, M_TEMP, M_WAITOK);
1341 if (uio.uio_rw == UIO_WRITE) {
1342 error = uiomove(ptr, len, &uio);
1347 sce = &sc->sc_endpoints[endpt][IN];
1348 err = usbd_do_request_flags(sc->sc_udev, &ur->ucr_request,
1349 ptr, ur->ucr_flags, &ur->ucr_actlen, sce->timeout);
1355 if (uio.uio_rw == UIO_READ) {
1356 error = uiomove(ptr, len, &uio);
1366 case USB_GET_DEVICEINFO:
1367 usbd_fill_deviceinfo(sc->sc_udev,
1368 (struct usb_device_info *)addr, 1);
1377 ugenioctl(struct dev_ioctl_args *ap)
1379 cdev_t dev = ap->a_head.a_dev;
1380 int endpt = UGENENDPOINT(dev);
1381 struct ugen_softc *sc;
1384 sc = devclass_get_softc(ugen_devclass, UGENUNIT(dev));
1387 error = ugen_do_ioctl(sc, endpt, ap->a_cmd, ap->a_data, ap->a_fflag);
1388 if (--sc->sc_refcnt < 0)
1389 usb_detach_wakeup(sc->sc_dev);
1394 ugenpoll(struct dev_poll_args *ap)
1396 cdev_t dev = ap->a_head.a_dev;
1397 struct ugen_softc *sc;
1398 struct ugen_endpoint *sce;
1401 sc = devclass_get_softc(ugen_devclass, UGENUNIT(dev));
1407 sce = &sc->sc_endpoints[UGENENDPOINT(dev)][IN];
1412 kprintf("ugenpoll: no edesc\n");
1416 kprintf("ugenpoll: no pipe\n");
1421 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
1423 if (ap->a_events & (POLLIN | POLLRDNORM)) {
1424 if (sce->q.c_cc > 0)
1425 revents |= ap->a_events & (POLLIN | POLLRDNORM);
1427 selrecord(curthread, &sce->rsel);
1430 case UE_ISOCHRONOUS:
1431 if (ap->a_events & (POLLIN | POLLRDNORM)) {
1432 if (sce->cur != sce->fill)
1433 revents |= ap->a_events & (POLLIN | POLLRDNORM);
1435 selrecord(curthread, &sce->rsel);
1440 * We have no easy way of determining if a read will
1441 * yield any data or a write will happen.
1442 * Pretend they will.
1444 revents |= ap->a_events &
1445 (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM);
1451 ap->a_events = revents;
1455 DRIVER_MODULE(ugen, uhub, ugen_driver, ugen_devclass, usbd_driver_load, 0);