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.26 2007/06/28 06:32:32 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) logprintf x
81 #define DPRINTFN(n,x) if (ugendebug>(n)) logprintf 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);
182 USB_MATCH_START(ugen, uaa);
186 return (uaa->matchlvl);
189 return (UMATCH_GENERIC);
191 return (UMATCH_NONE);
196 USB_ATTACH_START(ugen, sc, uaa);
197 usbd_device_handle udev;
202 usbd_devinfo(uaa->device, 0, devinfo);
204 kprintf("%s: %s\n", device_get_nameunit(sc->sc_dev), devinfo);
206 sc->sc_udev = udev = uaa->device;
208 memset(sc->sc_endpoints, 0, sizeof sc->sc_endpoints);
210 /* First set configuration index 0, the default one for ugen. */
211 err = usbd_set_config_index(udev, 0, 0);
213 kprintf("%s: setting configuration index 0 failed\n",
214 device_get_nameunit(sc->sc_dev));
216 USB_ATTACH_ERROR_RETURN;
218 conf = usbd_get_config_descriptor(udev)->bConfigurationValue;
220 /* Set up all the local state for this configuration. */
221 err = ugen_set_config(sc, conf);
223 kprintf("%s: setting configuration %d failed\n",
224 device_get_nameunit(sc->sc_dev), conf);
226 USB_ATTACH_ERROR_RETURN;
229 /* the main device, ctrl endpoint */
230 dev_ops_add(&ugen_ops,
231 UGENUNITMASK, UGENMINOR(device_get_unit(sc->sc_dev), 0));
232 make_dev(&ugen_ops, UGENMINOR(device_get_unit(sc->sc_dev), 0),
233 UID_ROOT, GID_OPERATOR, 0644, "%s", device_get_nameunit(sc->sc_dev));
235 USB_ATTACH_SUCCESS_RETURN;
239 ugen_make_devnodes(struct ugen_softc *sc)
244 for (endptno = 1; endptno < USB_MAX_ENDPOINTS; endptno++) {
245 if (sc->sc_endpoints[endptno][IN].sc != NULL ||
246 sc->sc_endpoints[endptno][OUT].sc != NULL ) {
247 /* endpt can be 0x81 and 0x01, representing
248 * endpoint address 0x01 and IN/OUT directions.
249 * We map both endpts to the same device,
250 * IN is reading from it, OUT is writing to it.
252 * In the if clause above we check whether one
253 * of the structs is populated.
255 dev = make_dev(&ugen_ops,
256 UGENMINOR(device_get_unit(sc->sc_dev), endptno),
257 UID_ROOT, GID_OPERATOR, 0644,
259 device_get_nameunit(sc->sc_dev), endptno);
260 if (sc->sc_endpoints[endptno][IN].sc != NULL) {
262 if (sc->sc_endpoints[endptno][IN].dev)
263 release_dev(sc->sc_endpoints[endptno][IN].dev);
264 sc->sc_endpoints[endptno][IN].dev = dev;
266 if (sc->sc_endpoints[endptno][OUT].sc != NULL) {
268 if (sc->sc_endpoints[endptno][OUT].dev)
269 release_dev(sc->sc_endpoints[endptno][OUT].dev);
270 sc->sc_endpoints[endptno][OUT].dev = dev;
277 ugen_destroy_devnodes(struct ugen_softc *sc)
282 /* destroy all devices for the other (existing) endpoints as well */
283 for (endptno = 1; endptno < USB_MAX_ENDPOINTS; endptno++) {
284 if (sc->sc_endpoints[endptno][IN].sc != NULL ||
285 sc->sc_endpoints[endptno][OUT].sc != NULL ) {
286 /* endpt can be 0x81 and 0x01, representing
287 * endpoint address 0x01 and IN/OUT directions.
288 * We map both endpoint addresses to the same device,
289 * IN is reading from it, OUT is writing to it.
291 * In the if clause above we check whether one
292 * of the structs is populated.
294 dev = sc->sc_endpoints[endptno][IN].dev;
297 sc->sc_endpoints[endptno][IN].dev = NULL;
299 dev = sc->sc_endpoints[endptno][OUT].dev;
302 sc->sc_endpoints[endptno][OUT].dev = NULL;
309 ugen_set_config(struct ugen_softc *sc, int configno)
311 usbd_device_handle dev = sc->sc_udev;
312 usbd_interface_handle iface;
313 usb_endpoint_descriptor_t *ed;
314 struct ugen_endpoint *sce;
315 u_int8_t niface, nendpt;
316 int ifaceno, endptno, endpt;
320 DPRINTFN(1,("ugen_set_config: %s to configno %d, sc=%p\n",
321 device_get_nameunit(sc->sc_dev), configno, sc));
323 ugen_destroy_devnodes(sc);
325 /* We start at 1, not 0, because we don't care whether the
326 * control endpoint is open or not. It is always present.
328 for (endptno = 1; endptno < USB_MAX_ENDPOINTS; endptno++) {
329 if (sc->sc_is_open[endptno]) {
331 ("ugen_set_config: %s - endpoint %d is open\n",
332 device_get_nameunit(sc->sc_dev), endptno));
333 return (USBD_IN_USE);
337 /* Avoid setting the current value. */
338 if (usbd_get_config_descriptor(dev)->bConfigurationValue != configno) {
339 err = usbd_set_config_no(dev, configno, 1);
344 err = usbd_interface_count(dev, &niface);
347 memset(sc->sc_endpoints, 0, sizeof sc->sc_endpoints);
348 for (ifaceno = 0; ifaceno < niface; ifaceno++) {
349 DPRINTFN(1,("ugen_set_config: ifaceno %d\n", ifaceno));
350 err = usbd_device2interface_handle(dev, ifaceno, &iface);
353 err = usbd_endpoint_count(iface, &nendpt);
356 for (endptno = 0; endptno < nendpt; endptno++) {
357 ed = usbd_interface2endpoint_descriptor(iface,endptno);
358 endpt = ed->bEndpointAddress;
359 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
360 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
361 DPRINTFN(1,("ugen_set_config: endptno %d, endpt=0x%02x"
363 endptno, endpt, UE_GET_ADDR(endpt),
364 UE_GET_DIR(endpt), sce));
371 ugen_make_devnodes(sc);
373 return (USBD_NORMAL_COMPLETION);
377 ugenopen(struct dev_open_args *ap)
379 cdev_t dev = ap->a_head.a_dev;
380 struct ugen_softc *sc;
381 int unit = UGENUNIT(dev);
382 int endpt = UGENENDPOINT(dev);
383 usb_endpoint_descriptor_t *edesc;
384 struct ugen_endpoint *sce;
387 usbd_xfer_handle xfer;
391 USB_GET_SC_OPEN(ugen, unit, sc);
393 DPRINTFN(5, ("ugenopen: flag=%d, mode=%d, unit=%d endpt=%d\n",
394 ap->a_oflags, ap->a_devtype, unit, endpt));
396 if (sc == NULL || sc->sc_dying)
399 if (sc->sc_is_open[endpt])
402 if (endpt == USB_CONTROL_ENDPOINT) {
403 sc->sc_is_open[USB_CONTROL_ENDPOINT] = 1;
407 /* Make sure there are pipes for all directions. */
408 for (dir = OUT; dir <= IN; dir++) {
409 if (ap->a_oflags & (dir == OUT ? FWRITE : FREAD)) {
410 sce = &sc->sc_endpoints[endpt][dir];
411 if (sce == 0 || sce->edesc == 0)
416 /* Actually open the pipes. */
417 /* XXX Should back out properly if it fails. */
418 for (dir = OUT; dir <= IN; dir++) {
419 if (!(ap->a_oflags & (dir == OUT ? FWRITE : FREAD)))
421 sce = &sc->sc_endpoints[endpt][dir];
423 sce->timeout = USBD_NO_TIMEOUT;
424 DPRINTFN(5, ("ugenopen: sc=%p, endpt=%d, dir=%d, sce=%p\n",
425 sc, endpt, dir, sce));
427 switch (edesc->bmAttributes & UE_XFERTYPE) {
430 err = usbd_open_pipe(sce->iface,
431 edesc->bEndpointAddress, 0, &sce->pipeh);
436 isize = UGETW(edesc->wMaxPacketSize);
437 if (isize == 0) /* shouldn't happen */
439 sce->ibuf = kmalloc(isize, M_USBDEV, M_WAITOK);
440 DPRINTFN(5, ("ugenopen: intr endpt=%d,isize=%d\n",
442 if (clalloc(&sce->q, UGEN_IBSIZE, 0) == -1)
444 err = usbd_open_pipe_intr(sce->iface,
445 edesc->bEndpointAddress,
446 USBD_SHORT_XFER_OK, &sce->pipeh, sce,
447 sce->ibuf, isize, ugenintr,
448 USBD_DEFAULT_INTERVAL);
450 kfree(sce->ibuf, M_USBDEV);
454 DPRINTFN(5, ("ugenopen: interrupt open done\n"));
457 err = usbd_open_pipe(sce->iface,
458 edesc->bEndpointAddress, 0, &sce->pipeh);
465 isize = UGETW(edesc->wMaxPacketSize);
466 if (isize == 0) /* shouldn't happen */
468 sce->ibuf = kmalloc(isize * UGEN_NISOFRAMES,
470 sce->cur = sce->fill = sce->ibuf;
471 sce->limit = sce->ibuf + isize * UGEN_NISOFRAMES;
472 DPRINTFN(5, ("ugenopen: isoc endpt=%d, isize=%d\n",
474 err = usbd_open_pipe(sce->iface,
475 edesc->bEndpointAddress, 0, &sce->pipeh);
477 kfree(sce->ibuf, M_USBDEV);
480 for(i = 0; i < UGEN_NISOREQS; ++i) {
481 sce->isoreqs[i].sce = sce;
482 xfer = usbd_alloc_xfer(sc->sc_udev);
485 sce->isoreqs[i].xfer = xfer;
486 buf = usbd_alloc_buffer
487 (xfer, isize * UGEN_NISORFRMS);
492 sce->isoreqs[i].dmabuf = buf;
493 for(j = 0; j < UGEN_NISORFRMS; ++j)
494 sce->isoreqs[i].sizes[j] = isize;
496 (xfer, sce->pipeh, &sce->isoreqs[i],
497 sce->isoreqs[i].sizes,
498 UGEN_NISORFRMS, USBD_NO_COPY,
500 (void)usbd_transfer(xfer);
502 DPRINTFN(5, ("ugenopen: isoc open done\n"));
505 while (--i >= 0) /* implicit buffer free */
506 usbd_free_xfer(sce->isoreqs[i].xfer);
509 sce->timeout = USBD_DEFAULT_TIMEOUT;
513 sc->sc_is_open[endpt] = 1;
518 ugenclose(struct dev_close_args *ap)
520 cdev_t dev = ap->a_head.a_dev;
521 int endpt = UGENENDPOINT(dev);
522 struct ugen_softc *sc;
523 struct ugen_endpoint *sce;
527 USB_GET_SC(ugen, UGENUNIT(dev), sc);
529 DPRINTFN(5, ("ugenclose: flag=%d, mode=%d, unit=%d, endpt=%d\n",
530 ap->a_fflag, ap->a_devtype, UGENUNIT(dev), endpt));
533 if (!sc->sc_is_open[endpt]) {
534 kprintf("ugenclose: not open\n");
539 if (endpt == USB_CONTROL_ENDPOINT) {
540 DPRINTFN(5, ("ugenclose: close control\n"));
541 sc->sc_is_open[endpt] = 0;
545 for (dir = OUT; dir <= IN; dir++) {
546 if (!(ap->a_fflag & (dir == OUT ? FWRITE : FREAD)))
548 sce = &sc->sc_endpoints[endpt][dir];
549 if (sce == NULL || sce->pipeh == NULL)
551 DPRINTFN(5, ("ugenclose: endpt=%d dir=%d sce=%p\n",
554 usbd_abort_pipe(sce->pipeh);
555 usbd_close_pipe(sce->pipeh);
558 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
560 ndflush(&sce->q, sce->q.c_cc);
564 for (i = 0; i < UGEN_NISOREQS; ++i)
565 usbd_free_xfer(sce->isoreqs[i].xfer);
570 if (sce->ibuf != NULL) {
571 kfree(sce->ibuf, M_USBDEV);
576 sc->sc_is_open[endpt] = 0;
582 ugen_do_read(struct ugen_softc *sc, int endpt, struct uio *uio, int flag)
584 struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][IN];
587 usbd_xfer_handle xfer;
591 u_char buffer[UGEN_CHUNK];
593 DPRINTFN(5, ("%s: ugenread: %d\n", device_get_nameunit(sc->sc_dev), endpt));
598 if (endpt == USB_CONTROL_ENDPOINT)
604 if (sce->edesc == NULL) {
605 kprintf("ugenread: no edesc\n");
608 if (sce->pipeh == NULL) {
609 kprintf("ugenread: no pipe\n");
613 buf = getugenbuf(ugen_bufsize, &ugen_bbsize);
615 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
617 /* Block until activity occurred. */
619 while (sce->q.c_cc == 0) {
620 if (flag & IO_NDELAY) {
625 sce->state |= UGEN_ASLP;
626 DPRINTFN(5, ("ugenread: sleep on %p\n", sce));
627 error = tsleep(sce, PCATCH, "ugenri", 0);
628 DPRINTFN(5, ("ugenread: woke, error=%d\n", error));
632 sce->state &= ~UGEN_ASLP;
638 /* Transfer as many chunks as possible. */
639 while (sce->q.c_cc > 0 && uio->uio_resid > 0 && !error) {
640 n = min(sce->q.c_cc, uio->uio_resid);
641 if (n > sizeof(buffer))
644 /* Remove a small chunk from the input queue. */
645 q_to_b(&sce->q, buffer, n);
646 DPRINTFN(5, ("ugenread: got %d chars\n", n));
648 /* Copy the data to the user process. */
649 error = uiomove(buffer, n, uio);
655 xfer = usbd_alloc_xfer(sc->sc_udev);
660 while ((n = min(ugen_bbsize, uio->uio_resid)) != 0) {
661 DPRINTFN(1, ("ugenread: start transfer %d bytes\n",n));
663 err = usbd_bulk_transfer(
665 sce->state & UGEN_SHORT_OK ?
666 USBD_SHORT_XFER_OK : 0,
667 sce->timeout, buf, &tn, "ugenrb");
669 if (err == USBD_INTERRUPTED)
671 else if (err == USBD_TIMEOUT)
677 DPRINTFN(1, ("ugenread: got %d bytes\n", tn));
678 error = uiomove(buf, tn, uio);
682 usbd_free_xfer(xfer);
686 while (sce->cur == sce->fill) {
687 if (flag & IO_NDELAY) {
692 sce->state |= UGEN_ASLP;
693 DPRINTFN(5, ("ugenread: sleep on %p\n", sce));
694 error = tsleep(sce, PCATCH, "ugenri", 0);
695 DPRINTFN(5, ("ugenread: woke, error=%d\n", error));
699 sce->state &= ~UGEN_ASLP;
704 while (sce->cur != sce->fill && uio->uio_resid > 0 && !error) {
705 if(sce->fill > sce->cur)
706 n = min(sce->fill - sce->cur, uio->uio_resid);
708 n = min(sce->limit - sce->cur, uio->uio_resid);
710 DPRINTFN(5, ("ugenread: isoc got %d chars\n", n));
712 /* Copy the data to the user process. */
713 error = uiomove(sce->cur, n, uio);
717 if(sce->cur >= sce->limit)
718 sce->cur = sce->ibuf;
729 relugenbuf(buf, ugen_bbsize);
734 ugenread(struct dev_read_args *ap)
736 cdev_t dev = ap->a_head.a_dev;
737 int endpt = UGENENDPOINT(dev);
738 struct ugen_softc *sc;
741 USB_GET_SC(ugen, UGENUNIT(dev), sc);
744 error = ugen_do_read(sc, endpt, ap->a_uio, ap->a_ioflag);
745 if (--sc->sc_refcnt < 0)
746 usb_detach_wakeup(sc->sc_dev);
751 ugen_do_write(struct ugen_softc *sc, int endpt, struct uio *uio, int flag)
753 struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][OUT];
758 usbd_xfer_handle xfer;
761 DPRINTFN(5, ("%s: ugenwrite: %d\n", device_get_nameunit(sc->sc_dev), endpt));
766 if (endpt == USB_CONTROL_ENDPOINT)
772 if (sce->edesc == NULL) {
773 kprintf("ugenwrite: no edesc\n");
776 if (sce->pipeh == NULL) {
777 kprintf("ugenwrite: no pipe\n");
781 buf = getugenbuf(ugen_bufsize, &ugen_bbsize);
783 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
785 xfer = usbd_alloc_xfer(sc->sc_udev);
790 while ((n = min(ugen_bbsize, uio->uio_resid)) != 0) {
791 error = uiomove(buf, n, uio);
794 DPRINTFN(1, ("ugenwrite: transfer %d bytes\n", n));
795 err = usbd_bulk_transfer(xfer, sce->pipeh, 0,
796 sce->timeout, buf, &n,"ugenwb");
798 if (err == USBD_INTERRUPTED)
800 else if (err == USBD_TIMEOUT)
807 usbd_free_xfer(xfer);
810 xfer = usbd_alloc_xfer(sc->sc_udev);
815 while ((n = min(UGETW(sce->edesc->wMaxPacketSize),
816 uio->uio_resid)) != 0) {
817 error = uiomove(buf, n, uio);
820 DPRINTFN(1, ("ugenwrite: transfer %d bytes\n", n));
821 err = usbd_intr_transfer(xfer, sce->pipeh, 0,
822 sce->timeout, buf, &n,"ugenwi");
824 if (err == USBD_INTERRUPTED)
826 else if (err == USBD_TIMEOUT)
833 usbd_free_xfer(xfer);
840 relugenbuf(buf, ugen_bbsize);
845 ugenwrite(struct dev_write_args *ap)
847 cdev_t dev = ap->a_head.a_dev;
848 int endpt = UGENENDPOINT(dev);
849 struct ugen_softc *sc;
852 USB_GET_SC(ugen, UGENUNIT(dev), sc);
855 error = ugen_do_write(sc, endpt, ap->a_uio, ap->a_ioflag);
856 if (--sc->sc_refcnt < 0)
857 usb_detach_wakeup(sc->sc_dev);
863 USB_DETACH_START(ugen, sc);
864 struct ugen_endpoint *sce;
867 DPRINTF(("ugen_detach: sc=%p\n", sc));
870 /* Abort all pipes. Causes processes waiting for transfer to wake. */
871 for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
872 for (dir = OUT; dir <= IN; dir++) {
873 sce = &sc->sc_endpoints[i][dir];
874 if (sce && sce->pipeh)
875 usbd_abort_pipe(sce->pipeh);
879 if (--sc->sc_refcnt >= 0) {
881 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
882 wakeup(&sc->sc_endpoints[i][IN]);
883 /* Wait for processes to go away. */
884 usb_detach_wait(sc->sc_dev);
888 /* destroy the device for the control endpoint */
889 ugen_destroy_devnodes(sc);
890 dev_ops_remove(&ugen_ops,
891 UGENUNITMASK, UGENMINOR(device_get_unit(sc->sc_dev), 0));
896 ugenintr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
898 struct ugen_endpoint *sce = addr;
899 /*struct ugen_softc *sc = sce->sc;*/
903 if (status == USBD_CANCELLED)
906 if (status != USBD_NORMAL_COMPLETION) {
907 DPRINTF(("ugenintr: status=%d\n", status));
908 if (status == USBD_STALLED)
909 usbd_clear_endpoint_stall_async(sce->pipeh);
913 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
916 DPRINTFN(5, ("ugenintr: xfer=%p status=%d count=%d\n",
917 xfer, status, count));
918 DPRINTFN(5, (" data = %02x %02x %02x\n",
919 ibuf[0], ibuf[1], ibuf[2]));
921 (void)b_to_q(ibuf, count, &sce->q);
923 if (sce->state & UGEN_ASLP) {
924 sce->state &= ~UGEN_ASLP;
925 DPRINTFN(5, ("ugen_intr: waking %p\n", sce));
928 selwakeuppri(&sce->rsel, 0);
932 ugen_isoc_rintr(usbd_xfer_handle xfer, usbd_private_handle addr,
935 struct isoreq *req = addr;
936 struct ugen_endpoint *sce = req->sce;
940 /* Return if we are aborting. */
941 if (status == USBD_CANCELLED)
944 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
945 DPRINTFN(5,("ugen_isoc_rintr: xfer %d, count=%d\n",
946 (int)(req - sce->isoreqs),
949 /* throw away oldest input if the buffer is full */
950 if(sce->fill < sce->cur && sce->cur <= sce->fill + count) {
952 if(sce->cur >= sce->limit)
953 sce->cur = sce->ibuf + (sce->limit - sce->cur);
954 DPRINTFN(5, ("ugen_isoc_rintr: throwing away %d bytes\n",
958 isize = UGETW(sce->edesc->wMaxPacketSize);
959 for (i = 0; i < UGEN_NISORFRMS; i++) {
960 u_int32_t actlen = req->sizes[i];
961 char const *buf = (char const *)req->dmabuf + isize * i;
963 /* copy data to buffer */
965 n = min(actlen, sce->limit - sce->fill);
966 memcpy(sce->fill, buf, n);
971 if(sce->fill == sce->limit)
972 sce->fill = sce->ibuf;
975 /* setup size for next transfer */
976 req->sizes[i] = isize;
979 usbd_setup_isoc_xfer(xfer, sce->pipeh, req, req->sizes, UGEN_NISORFRMS,
980 USBD_NO_COPY, ugen_isoc_rintr);
981 (void)usbd_transfer(xfer);
983 if (sce->state & UGEN_ASLP) {
984 sce->state &= ~UGEN_ASLP;
985 DPRINTFN(5, ("ugen_isoc_rintr: waking %p\n", sce));
988 selwakeuppri(&sce->rsel, 0);
992 ugen_set_interface(struct ugen_softc *sc, int ifaceidx, int altno)
994 usbd_interface_handle iface;
995 usb_endpoint_descriptor_t *ed;
997 struct ugen_endpoint *sce;
998 u_int8_t niface, nendpt, endptno, endpt;
1001 DPRINTFN(15, ("ugen_set_interface %d %d\n", ifaceidx, altno));
1003 err = usbd_interface_count(sc->sc_udev, &niface);
1006 if (ifaceidx < 0 || ifaceidx >= niface)
1007 return (USBD_INVAL);
1009 err = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface);
1012 err = usbd_endpoint_count(iface, &nendpt);
1016 /* destroy the existing devices, we remake the new ones in a moment */
1017 ugen_destroy_devnodes(sc);
1019 /* XXX should only do this after setting new altno has succeeded */
1020 for (endptno = 0; endptno < nendpt; endptno++) {
1021 ed = usbd_interface2endpoint_descriptor(iface,endptno);
1022 endpt = ed->bEndpointAddress;
1023 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
1024 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
1030 /* change setting */
1031 err = usbd_set_interface(iface, altno);
1035 err = usbd_endpoint_count(iface, &nendpt);
1038 for (endptno = 0; endptno < nendpt; endptno++) {
1039 ed = usbd_interface2endpoint_descriptor(iface,endptno);
1040 endpt = ed->bEndpointAddress;
1041 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
1042 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
1048 /* make the new devices */
1049 ugen_make_devnodes(sc);
1054 /* Retrieve a complete descriptor for a certain device and index. */
1055 static usb_config_descriptor_t *
1056 ugen_get_cdesc(struct ugen_softc *sc, int index, int *lenp)
1058 usb_config_descriptor_t *cdesc, *tdesc, cdescr;
1062 if (index == USB_CURRENT_CONFIG_INDEX) {
1063 tdesc = usbd_get_config_descriptor(sc->sc_udev);
1064 len = UGETW(tdesc->wTotalLength);
1067 cdesc = kmalloc(len, M_TEMP, M_INTWAIT);
1068 memcpy(cdesc, tdesc, len);
1069 DPRINTFN(5,("ugen_get_cdesc: current, len=%d\n", len));
1071 err = usbd_get_config_desc(sc->sc_udev, index, &cdescr);
1074 len = UGETW(cdescr.wTotalLength);
1075 DPRINTFN(5,("ugen_get_cdesc: index=%d, len=%d\n", index, len));
1078 cdesc = kmalloc(len, M_TEMP, M_INTWAIT);
1079 err = usbd_get_config_desc_full(sc->sc_udev, index, cdesc, len);
1081 kfree(cdesc, M_TEMP);
1089 ugen_get_alt_index(struct ugen_softc *sc, int ifaceidx)
1091 usbd_interface_handle iface;
1094 err = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface);
1097 return (usbd_get_interface_altindex(iface));
1101 ugen_do_ioctl(struct ugen_softc *sc, int endpt, u_long cmd,
1102 caddr_t addr, int flag)
1104 struct ugen_endpoint *sce;
1106 usbd_interface_handle iface;
1107 struct usb_config_desc *cd;
1108 usb_config_descriptor_t *cdesc;
1109 struct usb_interface_desc *id;
1110 usb_interface_descriptor_t *idesc;
1111 struct usb_endpoint_desc *ed;
1112 usb_endpoint_descriptor_t *edesc;
1113 struct usb_alt_interface *ai;
1114 struct usb_string_desc *si;
1117 DPRINTFN(5, ("ugenioctl: cmd=%08lx\n", cmd));
1122 case USB_SET_SHORT_XFER:
1123 /* This flag only affects read */
1124 if (endpt == USB_CONTROL_ENDPOINT)
1126 sce = &sc->sc_endpoints[endpt][IN];
1130 if (sce->pipeh == NULL) {
1131 kprintf("ugenioctl: USB_SET_SHORT_XFER, no pipe\n");
1136 sce->state |= UGEN_SHORT_OK;
1138 sce->state &= ~UGEN_SHORT_OK;
1140 case USB_SET_TIMEOUT:
1141 sce = &sc->sc_endpoints[endpt][IN];
1144 sce->timeout = *(int *)addr;
1150 if (endpt != USB_CONTROL_ENDPOINT)
1156 ugendebug = *(int *)addr;
1159 case USB_GET_CONFIG:
1160 err = usbd_get_config(sc->sc_udev, &conf);
1163 *(int *)addr = conf;
1165 case USB_SET_CONFIG:
1166 if (!(flag & FWRITE))
1168 err = ugen_set_config(sc, *(int *)addr);
1170 case USBD_NORMAL_COMPLETION:
1178 case USB_GET_ALTINTERFACE:
1179 ai = (struct usb_alt_interface *)addr;
1180 err = usbd_device2interface_handle(sc->sc_udev,
1181 ai->uai_interface_index, &iface);
1184 idesc = usbd_get_interface_descriptor(iface);
1187 ai->uai_alt_no = idesc->bAlternateSetting;
1189 case USB_SET_ALTINTERFACE:
1190 if (!(flag & FWRITE))
1192 ai = (struct usb_alt_interface *)addr;
1193 err = usbd_device2interface_handle(sc->sc_udev,
1194 ai->uai_interface_index, &iface);
1197 err = ugen_set_interface(sc, ai->uai_interface_index, ai->uai_alt_no);
1201 case USB_GET_NO_ALT:
1202 ai = (struct usb_alt_interface *)addr;
1203 cdesc = ugen_get_cdesc(sc, ai->uai_config_index, 0);
1206 idesc = usbd_find_idesc(cdesc, ai->uai_interface_index, 0);
1207 if (idesc == NULL) {
1208 kfree(cdesc, M_TEMP);
1211 ai->uai_alt_no = usbd_get_no_alts(cdesc, idesc->bInterfaceNumber);
1212 kfree(cdesc, M_TEMP);
1214 case USB_GET_DEVICE_DESC:
1215 *(usb_device_descriptor_t *)addr =
1216 *usbd_get_device_descriptor(sc->sc_udev);
1218 case USB_GET_CONFIG_DESC:
1219 cd = (struct usb_config_desc *)addr;
1220 cdesc = ugen_get_cdesc(sc, cd->ucd_config_index, 0);
1223 cd->ucd_desc = *cdesc;
1224 kfree(cdesc, M_TEMP);
1226 case USB_GET_INTERFACE_DESC:
1227 id = (struct usb_interface_desc *)addr;
1228 cdesc = ugen_get_cdesc(sc, id->uid_config_index, 0);
1231 if (id->uid_config_index == USB_CURRENT_CONFIG_INDEX &&
1232 id->uid_alt_index == USB_CURRENT_ALT_INDEX)
1233 alt = ugen_get_alt_index(sc, id->uid_interface_index);
1235 alt = id->uid_alt_index;
1236 idesc = usbd_find_idesc(cdesc, id->uid_interface_index, alt);
1237 if (idesc == NULL) {
1238 kfree(cdesc, M_TEMP);
1241 id->uid_desc = *idesc;
1242 kfree(cdesc, M_TEMP);
1244 case USB_GET_ENDPOINT_DESC:
1245 ed = (struct usb_endpoint_desc *)addr;
1246 cdesc = ugen_get_cdesc(sc, ed->ued_config_index, 0);
1249 if (ed->ued_config_index == USB_CURRENT_CONFIG_INDEX &&
1250 ed->ued_alt_index == USB_CURRENT_ALT_INDEX)
1251 alt = ugen_get_alt_index(sc, ed->ued_interface_index);
1253 alt = ed->ued_alt_index;
1254 edesc = usbd_find_edesc(cdesc, ed->ued_interface_index,
1255 alt, ed->ued_endpoint_index);
1256 if (edesc == NULL) {
1257 kfree(cdesc, M_TEMP);
1260 ed->ued_desc = *edesc;
1261 kfree(cdesc, M_TEMP);
1263 case USB_GET_FULL_DESC:
1268 struct usb_full_desc *fd = (struct usb_full_desc *)addr;
1271 cdesc = ugen_get_cdesc(sc, fd->ufd_config_index, &len);
1272 if (len > fd->ufd_size)
1274 iov.iov_base = (caddr_t)fd->ufd_data;
1278 uio.uio_resid = len;
1280 uio.uio_segflg = UIO_USERSPACE;
1281 uio.uio_rw = UIO_READ;
1282 uio.uio_procp = curthread;
1283 error = uiomove((void *)cdesc, len, &uio);
1284 kfree(cdesc, M_TEMP);
1287 case USB_GET_STRING_DESC:
1291 si = (struct usb_string_desc *)addr;
1292 err = usbd_get_string_desc(sc->sc_udev, si->usd_string_index,
1293 si->usd_language_id, &si->usd_desc, &size);
1298 case USB_DO_REQUEST:
1300 struct usb_ctl_request *ur = (void *)addr;
1301 int len = UGETW(ur->ucr_request.wLength);
1308 if (!(flag & FWRITE))
1310 /* Avoid requests that would damage the bus integrity. */
1311 if ((ur->ucr_request.bmRequestType == UT_WRITE_DEVICE &&
1312 ur->ucr_request.bRequest == UR_SET_ADDRESS) ||
1313 (ur->ucr_request.bmRequestType == UT_WRITE_DEVICE &&
1314 ur->ucr_request.bRequest == UR_SET_CONFIG) ||
1315 (ur->ucr_request.bmRequestType == UT_WRITE_INTERFACE &&
1316 ur->ucr_request.bRequest == UR_SET_INTERFACE))
1319 if (len < 0 || len > 32767)
1322 iov.iov_base = (caddr_t)ur->ucr_data;
1326 uio.uio_resid = len;
1328 uio.uio_segflg = UIO_USERSPACE;
1330 ur->ucr_request.bmRequestType & UT_READ ?
1331 UIO_READ : UIO_WRITE;
1332 uio.uio_td = curthread;
1333 ptr = kmalloc(len, M_TEMP, M_WAITOK);
1334 if (uio.uio_rw == UIO_WRITE) {
1335 error = uiomove(ptr, len, &uio);
1340 sce = &sc->sc_endpoints[endpt][IN];
1341 err = usbd_do_request_flags(sc->sc_udev, &ur->ucr_request,
1342 ptr, ur->ucr_flags, &ur->ucr_actlen, sce->timeout);
1348 if (uio.uio_rw == UIO_READ) {
1349 error = uiomove(ptr, len, &uio);
1359 case USB_GET_DEVICEINFO:
1360 usbd_fill_deviceinfo(sc->sc_udev,
1361 (struct usb_device_info *)addr, 1);
1370 ugenioctl(struct dev_ioctl_args *ap)
1372 cdev_t dev = ap->a_head.a_dev;
1373 int endpt = UGENENDPOINT(dev);
1374 struct ugen_softc *sc;
1377 USB_GET_SC(ugen, UGENUNIT(dev), sc);
1380 error = ugen_do_ioctl(sc, endpt, ap->a_cmd, ap->a_data, ap->a_fflag);
1381 if (--sc->sc_refcnt < 0)
1382 usb_detach_wakeup(sc->sc_dev);
1387 ugenpoll(struct dev_poll_args *ap)
1389 cdev_t dev = ap->a_head.a_dev;
1390 struct ugen_softc *sc;
1391 struct ugen_endpoint *sce;
1394 USB_GET_SC(ugen, UGENUNIT(dev), sc);
1400 sce = &sc->sc_endpoints[UGENENDPOINT(dev)][IN];
1405 kprintf("ugenpoll: no edesc\n");
1409 kprintf("ugenpoll: no pipe\n");
1414 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
1416 if (ap->a_events & (POLLIN | POLLRDNORM)) {
1417 if (sce->q.c_cc > 0)
1418 revents |= ap->a_events & (POLLIN | POLLRDNORM);
1420 selrecord(curthread, &sce->rsel);
1423 case UE_ISOCHRONOUS:
1424 if (ap->a_events & (POLLIN | POLLRDNORM)) {
1425 if (sce->cur != sce->fill)
1426 revents |= ap->a_events & (POLLIN | POLLRDNORM);
1428 selrecord(curthread, &sce->rsel);
1433 * We have no easy way of determining if a read will
1434 * yield any data or a write will happen.
1435 * Pretend they will.
1437 revents |= ap->a_events &
1438 (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM);
1444 ap->a_events = revents;
1448 DRIVER_MODULE(ugen, uhub, ugen_driver, ugen_devclass, usbd_driver_load, 0);