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/vnode.h>
65 #include <sys/event.h>
66 #include <sys/sysctl.h>
67 #include <sys/thread2.h>
69 #include <bus/usb/usb.h>
70 #include <bus/usb/usbdi.h>
71 #include <bus/usb/usbdi_util.h>
75 SYSCTL_NODE(_hw_usb, OID_AUTO, ugen, CTLFLAG_RW, 0, "USB ugen");
78 #define DPRINTF(x) if (ugendebug) kprintf x
79 #define DPRINTFN(n,x) if (ugendebug>(n)) kprintf x
81 SYSCTL_INT(_hw_usb_ugen, OID_AUTO, debug, CTLFLAG_RW,
82 &ugendebug, 0, "ugen debug level");
88 static int ugen_bufsize = 16384;
89 SYSCTL_INT(_hw_usb_ugen, OID_AUTO, bufsize, CTLFLAG_RW,
90 &ugen_bufsize, 0, "ugen temporary buffer size");
92 #define UGEN_CHUNK 128 /* chunk size for read */
93 #define UGEN_IBSIZE 1020 /* buffer size */
95 #define UGEN_NISOFRAMES 500 /* 0.5 seconds worth */
96 #define UGEN_NISOREQS 6 /* number of outstanding xfer requests */
97 #define UGEN_NISORFRMS 4 /* number of frames (miliseconds) per req */
99 struct ugen_endpoint {
100 struct ugen_softc *sc;
102 usb_endpoint_descriptor_t *edesc;
103 usbd_interface_handle iface;
105 #define UGEN_ASLP 0x02 /* waiting for data */
106 #define UGEN_SHORT_OK 0x04 /* short xfers are OK */
107 usbd_pipe_handle pipeh;
110 u_char *ibuf; /* start of buffer (circular for isoc) */
111 u_char *fill; /* location for input (isoc) */
112 u_char *limit; /* end of circular buffer (isoc) */
113 u_char *cur; /* current read location (isoc) */
116 struct ugen_endpoint *sce;
117 usbd_xfer_handle xfer;
119 u_int16_t sizes[UGEN_NISORFRMS];
120 } isoreqs[UGEN_NISOREQS];
124 device_t sc_dev; /* base device */
125 usbd_device_handle sc_udev;
127 char sc_is_open[USB_MAX_ENDPOINTS];
128 struct ugen_endpoint sc_endpoints[USB_MAX_ENDPOINTS][2];
141 d_kqfilter_t ugenkqfilter;
143 static void ugen_filt_detach(struct knote *);
144 static int ugen_filt_read(struct knote *, long);
145 static int ugen_filt_write(struct knote *, long);
147 #define UGEN_CDEV_MAJOR 114
149 static struct dev_ops ugen_ops = {
150 { "ugen", UGEN_CDEV_MAJOR, D_KQFILTER },
152 .d_close = ugenclose,
154 .d_write = ugenwrite,
155 .d_ioctl = ugenioctl,
156 .d_kqfilter = ugenkqfilter
159 static void ugenintr(usbd_xfer_handle xfer, usbd_private_handle addr,
161 static void ugen_isoc_rintr(usbd_xfer_handle xfer, usbd_private_handle addr,
163 static int ugen_do_read(struct ugen_softc *, int, struct uio *, int);
164 static int ugen_do_write(struct ugen_softc *, int, struct uio *, int);
165 static int ugen_do_ioctl(struct ugen_softc *, int, u_long,
167 static void ugen_make_devnodes(struct ugen_softc *sc);
168 static void ugen_destroy_devnodes(struct ugen_softc *sc);
169 static int ugen_set_config(struct ugen_softc *sc, int configno);
170 static usb_config_descriptor_t *ugen_get_cdesc(struct ugen_softc *sc,
171 int index, int *lenp);
172 static usbd_status ugen_set_interface(struct ugen_softc *, int, int);
173 static int ugen_get_alt_index(struct ugen_softc *sc, int ifaceidx);
175 #define UGENUNIT(n) ((lminor(n) >> 4) & 0xff)
176 #define UGENENDPOINT(n) (minor(n) & 0xf)
177 #define UGENMINOR(u, e) (((u & 0xf) << 4) | ((u & 0xf0) << 12) | (e))
178 #define UGENUNITMASK 0xffff00f0
180 static device_probe_t ugen_match;
181 static device_attach_t ugen_attach;
182 static device_detach_t ugen_detach;
184 static devclass_t ugen_devclass;
186 static kobj_method_t ugen_methods[] = {
187 DEVMETHOD(device_probe, ugen_match),
188 DEVMETHOD(device_attach, ugen_attach),
189 DEVMETHOD(device_detach, ugen_detach),
193 static driver_t ugen_driver = {
196 sizeof(struct ugen_softc)
199 MODULE_DEPEND(ugen, usb, 1, 1, 1);
202 ugen_match(device_t self)
204 struct usb_attach_arg *uaa = device_get_ivars(self);
208 return (uaa->matchlvl);
211 return (UMATCH_GENERIC);
213 return (UMATCH_NONE);
217 ugen_attach(device_t self)
219 struct ugen_softc *sc = device_get_softc(self);
220 struct usb_attach_arg *uaa = device_get_ivars(self);
221 usbd_device_handle udev;
226 sc->sc_udev = udev = uaa->device;
228 memset(sc->sc_endpoints, 0, sizeof sc->sc_endpoints);
230 /* First set configuration index 0, the default one for ugen. */
231 err = usbd_set_config_index(udev, 0, 0);
233 kprintf("%s: setting configuration index 0 failed\n",
234 device_get_nameunit(sc->sc_dev));
238 conf = usbd_get_config_descriptor(udev)->bConfigurationValue;
240 /* Set up all the local state for this configuration. */
241 err = ugen_set_config(sc, conf);
243 kprintf("%s: setting configuration %d failed\n",
244 device_get_nameunit(sc->sc_dev), conf);
249 /* the main device, ctrl endpoint */
250 make_dev(&ugen_ops, UGENMINOR(device_get_unit(sc->sc_dev), 0),
251 UID_ROOT, GID_OPERATOR, 0644,
252 "%s", device_get_nameunit(sc->sc_dev));
254 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
259 ugen_make_devnodes(struct ugen_softc *sc)
264 for (endptno = 1; endptno < USB_MAX_ENDPOINTS; endptno++) {
265 if (sc->sc_endpoints[endptno][IN].sc != NULL ||
266 sc->sc_endpoints[endptno][OUT].sc != NULL ) {
267 /* endpt can be 0x81 and 0x01, representing
268 * endpoint address 0x01 and IN/OUT directions.
269 * We map both endpts to the same device,
270 * IN is reading from it, OUT is writing to it.
272 * In the if clause above we check whether one
273 * of the structs is populated.
275 dev = make_dev(&ugen_ops,
276 UGENMINOR(device_get_unit(sc->sc_dev), endptno),
277 UID_ROOT, GID_OPERATOR, 0644,
279 device_get_nameunit(sc->sc_dev), endptno);
280 if (sc->sc_endpoints[endptno][IN].sc != NULL) {
282 if (sc->sc_endpoints[endptno][IN].dev)
283 release_dev(sc->sc_endpoints[endptno][IN].dev);
284 sc->sc_endpoints[endptno][IN].dev = dev;
286 if (sc->sc_endpoints[endptno][OUT].sc != NULL) {
288 if (sc->sc_endpoints[endptno][OUT].dev)
289 release_dev(sc->sc_endpoints[endptno][OUT].dev);
290 sc->sc_endpoints[endptno][OUT].dev = dev;
297 ugen_destroy_devnodes(struct ugen_softc *sc)
299 int endptno, prev_sc_dying;
302 prev_sc_dying = sc->sc_dying;
305 /* destroy all devices for the other (existing) endpoints as well */
306 for (endptno = 1; endptno < USB_MAX_ENDPOINTS; endptno++) {
307 if (sc->sc_endpoints[endptno][IN].sc != NULL ||
308 sc->sc_endpoints[endptno][OUT].sc != NULL ) {
309 /* endpt can be 0x81 and 0x01, representing
310 * endpoint address 0x01 and IN/OUT directions.
311 * We map both endpoint addresses to the same device,
312 * IN is reading from it, OUT is writing to it.
314 * In the if clause above we check whether one
315 * of the structs is populated.
317 dev = sc->sc_endpoints[endptno][IN].dev;
320 sc->sc_endpoints[endptno][IN].dev = NULL;
322 dev = sc->sc_endpoints[endptno][OUT].dev;
325 sc->sc_endpoints[endptno][OUT].dev = NULL;
329 sc->sc_dying = prev_sc_dying;
333 ugen_set_config(struct ugen_softc *sc, int configno)
335 usbd_device_handle dev = sc->sc_udev;
336 usbd_interface_handle iface;
337 usb_endpoint_descriptor_t *ed;
338 struct ugen_endpoint *sce;
339 u_int8_t niface, nendpt;
340 int ifaceno, endptno, endpt;
344 DPRINTFN(1,("ugen_set_config: %s to configno %d, sc=%p\n",
345 device_get_nameunit(sc->sc_dev), configno, sc));
347 ugen_destroy_devnodes(sc);
349 /* We start at 1, not 0, because we don't care whether the
350 * control endpoint is open or not. It is always present.
352 for (endptno = 1; endptno < USB_MAX_ENDPOINTS; endptno++) {
353 if (sc->sc_is_open[endptno]) {
355 ("ugen_set_config: %s - endpoint %d is open\n",
356 device_get_nameunit(sc->sc_dev), endptno));
357 return (USBD_IN_USE);
361 /* Avoid setting the current value. */
362 if (usbd_get_config_descriptor(dev)->bConfigurationValue != configno) {
363 err = usbd_set_config_no(dev, configno, 1);
368 err = usbd_interface_count(dev, &niface);
371 memset(sc->sc_endpoints, 0, sizeof sc->sc_endpoints);
372 for (ifaceno = 0; ifaceno < niface; ifaceno++) {
373 DPRINTFN(1,("ugen_set_config: ifaceno %d\n", ifaceno));
374 err = usbd_device2interface_handle(dev, ifaceno, &iface);
377 err = usbd_endpoint_count(iface, &nendpt);
380 for (endptno = 0; endptno < nendpt; endptno++) {
381 ed = usbd_interface2endpoint_descriptor(iface,endptno);
382 endpt = ed->bEndpointAddress;
383 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
384 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
385 DPRINTFN(1,("ugen_set_config: endptno %d, endpt=0x%02x"
387 endptno, endpt, UE_GET_ADDR(endpt),
388 UE_GET_DIR(endpt), sce));
395 ugen_make_devnodes(sc);
397 return (USBD_NORMAL_COMPLETION);
401 ugenopen(struct dev_open_args *ap)
403 cdev_t dev = ap->a_head.a_dev;
404 struct ugen_softc *sc;
405 int unit = UGENUNIT(dev);
406 int endpt = UGENENDPOINT(dev);
407 usb_endpoint_descriptor_t *edesc;
408 struct ugen_endpoint *sce;
411 usbd_xfer_handle xfer;
415 sc = devclass_get_softc(ugen_devclass, unit);
419 DPRINTFN(5, ("ugenopen: flag=%d, mode=%d, unit=%d endpt=%d\n",
420 ap->a_oflags, ap->a_devtype, unit, endpt));
425 if (sc->sc_is_open[endpt])
428 if (endpt == USB_CONTROL_ENDPOINT) {
429 sc->sc_is_open[USB_CONTROL_ENDPOINT] = 1;
433 /* Make sure there are pipes for all directions. */
434 for (dir = OUT; dir <= IN; dir++) {
435 if (ap->a_oflags & (dir == OUT ? FWRITE : FREAD)) {
436 sce = &sc->sc_endpoints[endpt][dir];
437 if (sce == 0 || sce->edesc == 0)
442 /* Actually open the pipes. */
443 /* XXX Should back out properly if it fails. */
444 for (dir = OUT; dir <= IN; dir++) {
445 if (!(ap->a_oflags & (dir == OUT ? FWRITE : FREAD)))
447 sce = &sc->sc_endpoints[endpt][dir];
449 sce->timeout = USBD_NO_TIMEOUT;
450 DPRINTFN(5, ("ugenopen: sc=%p, endpt=%d, dir=%d, sce=%p\n",
451 sc, endpt, dir, sce));
453 switch (edesc->bmAttributes & UE_XFERTYPE) {
456 err = usbd_open_pipe(sce->iface,
457 edesc->bEndpointAddress, 0, &sce->pipeh);
462 isize = UGETW(edesc->wMaxPacketSize);
463 if (isize == 0) /* shouldn't happen */
465 sce->ibuf = kmalloc(isize, M_USBDEV, M_WAITOK);
466 DPRINTFN(5, ("ugenopen: intr endpt=%d,isize=%d\n",
468 if ((clist_alloc_cblocks(&sce->q, UGEN_IBSIZE,
469 UGEN_IBSIZE), 0) == -1)
471 err = usbd_open_pipe_intr(sce->iface,
472 edesc->bEndpointAddress,
473 USBD_SHORT_XFER_OK, &sce->pipeh, sce,
474 sce->ibuf, isize, ugenintr,
475 USBD_DEFAULT_INTERVAL);
477 kfree(sce->ibuf, M_USBDEV);
478 clist_free_cblocks(&sce->q);
481 DPRINTFN(5, ("ugenopen: interrupt open done\n"));
484 err = usbd_open_pipe(sce->iface,
485 edesc->bEndpointAddress, 0, &sce->pipeh);
492 isize = UGETW(edesc->wMaxPacketSize);
493 if (isize == 0) /* shouldn't happen */
495 sce->ibuf = kmalloc(isize * UGEN_NISOFRAMES,
497 sce->cur = sce->fill = sce->ibuf;
498 sce->limit = sce->ibuf + isize * UGEN_NISOFRAMES;
499 DPRINTFN(5, ("ugenopen: isoc endpt=%d, isize=%d\n",
501 err = usbd_open_pipe(sce->iface,
502 edesc->bEndpointAddress, 0, &sce->pipeh);
504 kfree(sce->ibuf, M_USBDEV);
507 for(i = 0; i < UGEN_NISOREQS; ++i) {
508 sce->isoreqs[i].sce = sce;
509 xfer = usbd_alloc_xfer(sc->sc_udev);
512 sce->isoreqs[i].xfer = xfer;
513 buf = usbd_alloc_buffer
514 (xfer, isize * UGEN_NISORFRMS);
519 sce->isoreqs[i].dmabuf = buf;
520 for(j = 0; j < UGEN_NISORFRMS; ++j)
521 sce->isoreqs[i].sizes[j] = isize;
523 (xfer, sce->pipeh, &sce->isoreqs[i],
524 sce->isoreqs[i].sizes,
525 UGEN_NISORFRMS, USBD_NO_COPY,
527 (void)usbd_transfer(xfer);
529 DPRINTFN(5, ("ugenopen: isoc open done\n"));
532 while (--i >= 0) /* implicit buffer free */
533 usbd_free_xfer(sce->isoreqs[i].xfer);
536 sce->timeout = USBD_DEFAULT_TIMEOUT;
540 sc->sc_is_open[endpt] = 1;
545 ugenclose(struct dev_close_args *ap)
547 cdev_t dev = ap->a_head.a_dev;
548 int endpt = UGENENDPOINT(dev);
549 struct ugen_softc *sc;
550 struct ugen_endpoint *sce;
554 sc = devclass_get_softc(ugen_devclass, UGENUNIT(dev));
556 DPRINTFN(5, ("ugenclose: flag=%d, mode=%d, unit=%d, endpt=%d\n",
557 ap->a_fflag, ap->a_devtype, UGENUNIT(dev), endpt));
560 if (!sc->sc_is_open[endpt]) {
561 kprintf("ugenclose: not open\n");
566 if (endpt == USB_CONTROL_ENDPOINT) {
567 DPRINTFN(5, ("ugenclose: close control\n"));
568 sc->sc_is_open[endpt] = 0;
572 for (dir = OUT; dir <= IN; dir++) {
573 if (!(ap->a_fflag & (dir == OUT ? FWRITE : FREAD)))
575 sce = &sc->sc_endpoints[endpt][dir];
576 if (sce->pipeh == NULL)
578 DPRINTFN(5, ("ugenclose: endpt=%d dir=%d sce=%p\n",
581 usbd_abort_pipe(sce->pipeh);
582 usbd_close_pipe(sce->pipeh);
585 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
587 ndflush(&sce->q, sce->q.c_cc);
588 clist_free_cblocks(&sce->q);
591 for (i = 0; i < UGEN_NISOREQS; ++i)
592 usbd_free_xfer(sce->isoreqs[i].xfer);
597 if (sce->ibuf != NULL) {
598 kfree(sce->ibuf, M_USBDEV);
600 clist_free_cblocks(&sce->q);
603 sc->sc_is_open[endpt] = 0;
609 ugen_do_read(struct ugen_softc *sc, int endpt, struct uio *uio, int flag)
611 struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][IN];
614 usbd_xfer_handle xfer;
618 u_char buffer[UGEN_CHUNK];
620 DPRINTFN(5, ("%s: ugenread: %d\n", device_get_nameunit(sc->sc_dev), endpt));
625 if (endpt == USB_CONTROL_ENDPOINT)
629 if (sce->edesc == NULL) {
630 kprintf("ugenread: no edesc\n");
633 if (sce->pipeh == NULL) {
634 kprintf("ugenread: no pipe\n");
639 buf = getugenbuf(ugen_bufsize, &ugen_bbsize);
641 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
643 /* Block until activity occurred. */
645 while (sce->q.c_cc == 0) {
646 if (flag & IO_NDELAY) {
651 sce->state |= UGEN_ASLP;
652 DPRINTFN(5, ("ugenread: sleep on %p\n", sce));
653 error = tsleep(sce, PCATCH, "ugenri",
654 (sce->timeout * hz + 999) / 1000);
655 sce->state &= ~UGEN_ASLP;
656 DPRINTFN(5, ("ugenread: woke, error=%d\n", error));
659 if (error == EAGAIN) {
660 error = 0; /* timeout, return 0 bytes */
668 /* Transfer as many chunks as possible. */
669 while (sce->q.c_cc > 0 && uio->uio_resid > 0 && !error) {
670 n = szmin(sce->q.c_cc, uio->uio_resid);
671 if (n > sizeof(buffer))
674 /* Remove a small chunk from the input queue. */
675 q_to_b(&sce->q, buffer, n);
676 DPRINTFN(5, ("ugenread: got %d chars\n", n));
678 /* Copy the data to the user process. */
679 error = uiomove(buffer, n, uio);
685 xfer = usbd_alloc_xfer(sc->sc_udev);
690 while ((n = szmin(ugen_bbsize, uio->uio_resid)) != 0) {
691 DPRINTFN(1, ("ugenread: start transfer %d bytes\n",n));
693 err = usbd_bulk_transfer(
695 sce->state & UGEN_SHORT_OK ?
696 USBD_SHORT_XFER_OK : 0,
697 sce->timeout, buf, &tn, "ugenrb");
699 if (err == USBD_INTERRUPTED)
701 else if (err == USBD_TIMEOUT)
707 DPRINTFN(1, ("ugenread: got %d bytes\n", tn));
708 error = uiomove(buf, tn, uio);
712 usbd_free_xfer(xfer);
716 while (sce->cur == sce->fill) {
717 if (flag & IO_NDELAY) {
722 sce->state |= UGEN_ASLP;
723 DPRINTFN(5, ("ugenread: sleep on %p\n", sce));
724 error = tsleep(sce, PCATCH, "ugenri",
725 (sce->timeout * hz + 999) / 1000);
726 sce->state &= ~UGEN_ASLP;
727 DPRINTFN(5, ("ugenread: woke, error=%d\n", error));
730 if (error == EAGAIN) {
731 error = 0; /* timeout, return 0 bytes */
738 while (sce->cur != sce->fill && uio->uio_resid > 0 && !error) {
739 if (sce->fill > sce->cur)
740 n = szmin(sce->fill - sce->cur, uio->uio_resid);
742 n = szmin(sce->limit- sce->cur, uio->uio_resid);
744 DPRINTFN(5, ("ugenread: isoc got %d chars\n", n));
746 /* Copy the data to the user process. */
747 error = uiomove(sce->cur, n, uio);
751 if(sce->cur >= sce->limit)
752 sce->cur = sce->ibuf;
763 relugenbuf(buf, ugen_bbsize);
768 ugenread(struct dev_read_args *ap)
770 cdev_t dev = ap->a_head.a_dev;
771 int endpt = UGENENDPOINT(dev);
772 struct ugen_softc *sc;
775 sc = devclass_get_softc(ugen_devclass, UGENUNIT(dev));
781 error = ugen_do_read(sc, endpt, ap->a_uio, ap->a_ioflag);
782 if (--sc->sc_refcnt < 0)
783 usb_detach_wakeup(sc->sc_dev);
788 ugen_do_write(struct ugen_softc *sc, int endpt, struct uio *uio, int flag)
790 struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][OUT];
795 usbd_xfer_handle xfer;
798 DPRINTFN(5, ("%s: ugenwrite: %d\n", device_get_nameunit(sc->sc_dev), endpt));
803 if (endpt == USB_CONTROL_ENDPOINT)
807 if (sce->edesc == NULL) {
808 kprintf("ugenwrite: no edesc\n");
811 if (sce->pipeh == NULL) {
812 kprintf("ugenwrite: no pipe\n");
817 buf = getugenbuf(ugen_bufsize, &ugen_bbsize);
819 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
821 xfer = usbd_alloc_xfer(sc->sc_udev);
826 while ((n = szmin(ugen_bbsize, uio->uio_resid)) != 0) {
827 error = uiomove(buf, n, uio);
830 DPRINTFN(1, ("ugenwrite: transfer %d bytes\n", n));
831 err = usbd_bulk_transfer(xfer, sce->pipeh, 0,
832 sce->timeout, buf, &n,"ugenwb");
834 if (err == USBD_INTERRUPTED)
836 else if (err == USBD_TIMEOUT)
843 usbd_free_xfer(xfer);
846 xfer = usbd_alloc_xfer(sc->sc_udev);
851 while ((n = szmin(UGETW(sce->edesc->wMaxPacketSize),
852 uio->uio_resid)) != 0) {
853 error = uiomove(buf, n, uio);
856 DPRINTFN(1, ("ugenwrite: transfer %d bytes\n", n));
857 err = usbd_intr_transfer(xfer, sce->pipeh, 0,
858 sce->timeout, buf, &n,"ugenwi");
860 if (err == USBD_INTERRUPTED)
862 else if (err == USBD_TIMEOUT)
869 usbd_free_xfer(xfer);
876 relugenbuf(buf, ugen_bbsize);
881 ugenwrite(struct dev_write_args *ap)
883 cdev_t dev = ap->a_head.a_dev;
884 int endpt = UGENENDPOINT(dev);
885 struct ugen_softc *sc;
888 sc = devclass_get_softc(ugen_devclass, UGENUNIT(dev));
894 error = ugen_do_write(sc, endpt, ap->a_uio, ap->a_ioflag);
895 if (--sc->sc_refcnt < 0)
896 usb_detach_wakeup(sc->sc_dev);
901 ugen_detach(device_t self)
903 struct ugen_softc *sc = device_get_softc(self);
904 struct ugen_endpoint *sce;
907 DPRINTF(("ugen_detach: sc=%p\n", sc));
910 /* Abort all pipes. Causes processes waiting for transfer to wake. */
911 for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
912 for (dir = OUT; dir <= IN; dir++) {
913 sce = &sc->sc_endpoints[i][dir];
914 if (sce && sce->pipeh)
915 usbd_abort_pipe(sce->pipeh);
916 KNOTE(&sce->rkq.ki_note, 0);
920 if (--sc->sc_refcnt >= 0) {
922 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
923 wakeup(&sc->sc_endpoints[i][IN]);
924 /* Wait for processes to go away. */
925 usb_detach_wait(sc->sc_dev);
929 /* destroy the device for the control endpoint */
930 ugen_destroy_devnodes(sc);
931 dev_ops_remove_minor(&ugen_ops,
932 /*UGENUNITMASK,*/ UGENMINOR(device_get_unit(sc->sc_dev), 0));
933 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
938 ugenintr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
940 struct ugen_endpoint *sce = addr;
941 /*struct ugen_softc *sc = sce->sc;*/
945 if (status == USBD_CANCELLED)
948 if (status != USBD_NORMAL_COMPLETION) {
949 DPRINTF(("ugenintr: status=%d\n", status));
950 if (status == USBD_STALLED)
951 usbd_clear_endpoint_stall_async(sce->pipeh);
955 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
958 DPRINTFN(5, ("ugenintr: xfer=%p status=%d count=%d\n",
959 xfer, status, count));
960 DPRINTFN(5, (" data = %02x %02x %02x\n",
961 ibuf[0], ibuf[1], ibuf[2]));
963 (void)b_to_q(ibuf, count, &sce->q);
965 if (sce->state & UGEN_ASLP) {
966 sce->state &= ~UGEN_ASLP;
967 DPRINTFN(5, ("ugen_intr: waking %p\n", sce));
970 KNOTE(&sce->rkq.ki_note, 0);
974 ugen_isoc_rintr(usbd_xfer_handle xfer, usbd_private_handle addr,
977 struct isoreq *req = addr;
978 struct ugen_endpoint *sce = req->sce;
982 /* Return if we are aborting. */
983 if (status == USBD_CANCELLED)
986 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
987 DPRINTFN(5,("ugen_isoc_rintr: xfer %d, count=%d\n",
988 (int)(req - sce->isoreqs),
991 /* throw away oldest input if the buffer is full */
992 if(sce->fill < sce->cur && sce->cur <= sce->fill + count) {
994 if(sce->cur >= sce->limit)
995 sce->cur = sce->ibuf + (sce->limit - sce->cur);
996 DPRINTFN(5, ("ugen_isoc_rintr: throwing away %d bytes\n",
1000 isize = UGETW(sce->edesc->wMaxPacketSize);
1001 for (i = 0; i < UGEN_NISORFRMS; i++) {
1002 u_int32_t actlen = req->sizes[i];
1003 char const *buf = (char const *)req->dmabuf + isize * i;
1005 /* copy data to buffer */
1006 while (actlen > 0) {
1007 n = min(actlen, sce->limit - sce->fill);
1008 memcpy(sce->fill, buf, n);
1013 if(sce->fill == sce->limit)
1014 sce->fill = sce->ibuf;
1017 /* setup size for next transfer */
1018 req->sizes[i] = isize;
1021 usbd_setup_isoc_xfer(xfer, sce->pipeh, req, req->sizes, UGEN_NISORFRMS,
1022 USBD_NO_COPY, ugen_isoc_rintr);
1023 (void)usbd_transfer(xfer);
1025 if (sce->state & UGEN_ASLP) {
1026 sce->state &= ~UGEN_ASLP;
1027 DPRINTFN(5, ("ugen_isoc_rintr: waking %p\n", sce));
1030 KNOTE(&sce->rkq.ki_note, 0);
1034 ugen_set_interface(struct ugen_softc *sc, int ifaceidx, int altno)
1036 usbd_interface_handle iface;
1037 usb_endpoint_descriptor_t *ed;
1039 struct ugen_endpoint *sce;
1040 u_int8_t niface, nendpt, endptno, endpt;
1043 DPRINTFN(15, ("ugen_set_interface %d %d\n", ifaceidx, altno));
1045 err = usbd_interface_count(sc->sc_udev, &niface);
1048 if (ifaceidx < 0 || ifaceidx >= niface)
1049 return (USBD_INVAL);
1051 err = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface);
1054 err = usbd_endpoint_count(iface, &nendpt);
1058 /* destroy the existing devices, we remake the new ones in a moment */
1059 ugen_destroy_devnodes(sc);
1061 /* XXX should only do this after setting new altno has succeeded */
1062 for (endptno = 0; endptno < nendpt; endptno++) {
1063 ed = usbd_interface2endpoint_descriptor(iface,endptno);
1064 endpt = ed->bEndpointAddress;
1065 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
1066 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
1072 /* change setting */
1073 err = usbd_set_interface(iface, altno);
1077 err = usbd_endpoint_count(iface, &nendpt);
1080 for (endptno = 0; endptno < nendpt; endptno++) {
1081 ed = usbd_interface2endpoint_descriptor(iface,endptno);
1082 endpt = ed->bEndpointAddress;
1083 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
1084 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
1090 /* make the new devices */
1091 ugen_make_devnodes(sc);
1096 /* Retrieve a complete descriptor for a certain device and index. */
1097 static usb_config_descriptor_t *
1098 ugen_get_cdesc(struct ugen_softc *sc, int index, int *lenp)
1100 usb_config_descriptor_t *cdesc, *tdesc, cdescr;
1104 if (index == USB_CURRENT_CONFIG_INDEX) {
1105 tdesc = usbd_get_config_descriptor(sc->sc_udev);
1106 len = UGETW(tdesc->wTotalLength);
1109 cdesc = kmalloc(len, M_TEMP, M_INTWAIT);
1110 memcpy(cdesc, tdesc, len);
1111 DPRINTFN(5,("ugen_get_cdesc: current, len=%d\n", len));
1113 err = usbd_get_config_desc(sc->sc_udev, index, &cdescr);
1116 len = UGETW(cdescr.wTotalLength);
1117 DPRINTFN(5,("ugen_get_cdesc: index=%d, len=%d\n", index, len));
1120 cdesc = kmalloc(len, M_TEMP, M_INTWAIT);
1121 err = usbd_get_config_desc_full(sc->sc_udev, index, cdesc, len);
1123 kfree(cdesc, M_TEMP);
1131 ugen_get_alt_index(struct ugen_softc *sc, int ifaceidx)
1133 usbd_interface_handle iface;
1136 err = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface);
1139 return (usbd_get_interface_altindex(iface));
1143 ugen_do_ioctl(struct ugen_softc *sc, int endpt, u_long cmd,
1144 caddr_t addr, int flag)
1146 struct ugen_endpoint *sce;
1148 usbd_interface_handle iface;
1149 struct usb_config_desc *cd;
1150 usb_config_descriptor_t *cdesc;
1151 struct usb_interface_desc *id;
1152 usb_interface_descriptor_t *idesc;
1153 struct usb_endpoint_desc *ed;
1154 usb_endpoint_descriptor_t *edesc;
1155 struct usb_alt_interface *ai;
1156 struct usb_string_desc *si;
1159 DPRINTFN(5, ("ugenioctl: cmd=%08lx\n", cmd));
1164 case USB_SET_SHORT_XFER:
1165 /* This flag only affects read */
1166 if (endpt == USB_CONTROL_ENDPOINT)
1168 sce = &sc->sc_endpoints[endpt][IN];
1170 if (sce->pipeh == NULL) {
1171 kprintf("ugenioctl: USB_SET_SHORT_XFER, no pipe\n");
1176 sce->state |= UGEN_SHORT_OK;
1178 sce->state &= ~UGEN_SHORT_OK;
1180 case USB_SET_TIMEOUT:
1181 sce = &sc->sc_endpoints[endpt][IN];
1182 sce->timeout = *(int *)addr;
1183 sce = &sc->sc_endpoints[endpt][OUT];
1184 sce->timeout = *(int *)addr;
1190 if (endpt != USB_CONTROL_ENDPOINT)
1196 ugendebug = *(int *)addr;
1199 case USB_GET_CONFIG:
1200 err = usbd_get_config(sc->sc_udev, &conf);
1203 *(int *)addr = conf;
1205 case USB_SET_CONFIG:
1206 if (!(flag & FWRITE))
1208 err = ugen_set_config(sc, *(int *)addr);
1210 case USBD_NORMAL_COMPLETION:
1218 case USB_GET_ALTINTERFACE:
1219 ai = (struct usb_alt_interface *)addr;
1220 err = usbd_device2interface_handle(sc->sc_udev,
1221 ai->uai_interface_index, &iface);
1224 idesc = usbd_get_interface_descriptor(iface);
1227 ai->uai_alt_no = idesc->bAlternateSetting;
1229 case USB_SET_ALTINTERFACE:
1230 if (!(flag & FWRITE))
1232 ai = (struct usb_alt_interface *)addr;
1233 err = usbd_device2interface_handle(sc->sc_udev,
1234 ai->uai_interface_index, &iface);
1237 err = ugen_set_interface(sc, ai->uai_interface_index, ai->uai_alt_no);
1241 case USB_GET_NO_ALT:
1242 ai = (struct usb_alt_interface *)addr;
1243 cdesc = ugen_get_cdesc(sc, ai->uai_config_index, 0);
1246 idesc = usbd_find_idesc(cdesc, ai->uai_interface_index, 0);
1247 if (idesc == NULL) {
1248 kfree(cdesc, M_TEMP);
1251 ai->uai_alt_no = usbd_get_no_alts(cdesc, idesc->bInterfaceNumber);
1252 kfree(cdesc, M_TEMP);
1254 case USB_GET_DEVICE_DESC:
1255 *(usb_device_descriptor_t *)addr =
1256 *usbd_get_device_descriptor(sc->sc_udev);
1258 case USB_GET_CONFIG_DESC:
1259 cd = (struct usb_config_desc *)addr;
1260 cdesc = ugen_get_cdesc(sc, cd->ucd_config_index, 0);
1263 cd->ucd_desc = *cdesc;
1264 kfree(cdesc, M_TEMP);
1266 case USB_GET_INTERFACE_DESC:
1267 id = (struct usb_interface_desc *)addr;
1268 cdesc = ugen_get_cdesc(sc, id->uid_config_index, 0);
1271 if (id->uid_config_index == USB_CURRENT_CONFIG_INDEX &&
1272 id->uid_alt_index == USB_CURRENT_ALT_INDEX)
1273 alt = ugen_get_alt_index(sc, id->uid_interface_index);
1275 alt = id->uid_alt_index;
1276 idesc = usbd_find_idesc(cdesc, id->uid_interface_index, alt);
1277 if (idesc == NULL) {
1278 kfree(cdesc, M_TEMP);
1281 id->uid_desc = *idesc;
1282 kfree(cdesc, M_TEMP);
1284 case USB_GET_ENDPOINT_DESC:
1285 ed = (struct usb_endpoint_desc *)addr;
1286 cdesc = ugen_get_cdesc(sc, ed->ued_config_index, 0);
1289 if (ed->ued_config_index == USB_CURRENT_CONFIG_INDEX &&
1290 ed->ued_alt_index == USB_CURRENT_ALT_INDEX)
1291 alt = ugen_get_alt_index(sc, ed->ued_interface_index);
1293 alt = ed->ued_alt_index;
1294 edesc = usbd_find_edesc(cdesc, ed->ued_interface_index,
1295 alt, ed->ued_endpoint_index);
1296 if (edesc == NULL) {
1297 kfree(cdesc, M_TEMP);
1300 ed->ued_desc = *edesc;
1301 kfree(cdesc, M_TEMP);
1303 case USB_GET_FULL_DESC:
1308 struct usb_full_desc *fd = (struct usb_full_desc *)addr;
1311 cdesc = ugen_get_cdesc(sc, fd->ufd_config_index, &len);
1312 if (len > fd->ufd_size)
1314 iov.iov_base = (caddr_t)fd->ufd_data;
1318 uio.uio_resid = len;
1320 uio.uio_segflg = UIO_USERSPACE;
1321 uio.uio_rw = UIO_READ;
1322 uio.uio_td = curthread;
1323 error = uiomove((void *)cdesc, len, &uio);
1324 kfree(cdesc, M_TEMP);
1327 case USB_GET_STRING_DESC:
1331 si = (struct usb_string_desc *)addr;
1332 err = usbd_get_string_desc(sc->sc_udev, si->usd_string_index,
1333 si->usd_language_id, &si->usd_desc, &size);
1338 case USB_DO_REQUEST:
1340 struct usb_ctl_request *ur = (void *)addr;
1341 int len = UGETW(ur->ucr_request.wLength);
1348 if (!(flag & FWRITE))
1350 /* Avoid requests that would damage the bus integrity. */
1351 if ((ur->ucr_request.bmRequestType == UT_WRITE_DEVICE &&
1352 ur->ucr_request.bRequest == UR_SET_ADDRESS) ||
1353 (ur->ucr_request.bmRequestType == UT_WRITE_DEVICE &&
1354 ur->ucr_request.bRequest == UR_SET_CONFIG) ||
1355 (ur->ucr_request.bmRequestType == UT_WRITE_INTERFACE &&
1356 ur->ucr_request.bRequest == UR_SET_INTERFACE))
1359 if (len < 0 || len > 32767)
1362 iov.iov_base = (caddr_t)ur->ucr_data;
1366 uio.uio_resid = len;
1368 uio.uio_segflg = UIO_USERSPACE;
1370 ur->ucr_request.bmRequestType & UT_READ ?
1371 UIO_READ : UIO_WRITE;
1372 uio.uio_td = curthread;
1373 ptr = kmalloc(len, M_TEMP, M_WAITOK);
1374 if (uio.uio_rw == UIO_WRITE) {
1375 error = uiomove(ptr, len, &uio);
1380 sce = &sc->sc_endpoints[endpt][IN];
1381 err = usbd_do_request_flags(sc->sc_udev, &ur->ucr_request,
1382 ptr, ur->ucr_flags, &ur->ucr_actlen, sce->timeout);
1388 if (uio.uio_rw == UIO_READ) {
1389 error = uiomove(ptr, len, &uio);
1399 case USB_GET_DEVICEINFO:
1400 usbd_fill_deviceinfo(sc->sc_udev,
1401 (struct usb_device_info *)addr, 1);
1410 ugenioctl(struct dev_ioctl_args *ap)
1412 cdev_t dev = ap->a_head.a_dev;
1413 int endpt = UGENENDPOINT(dev);
1414 struct ugen_softc *sc;
1417 sc = devclass_get_softc(ugen_devclass, UGENUNIT(dev));
1422 error = ugen_do_ioctl(sc, endpt, ap->a_cmd, ap->a_data, ap->a_fflag);
1423 if (--sc->sc_refcnt < 0)
1424 usb_detach_wakeup(sc->sc_dev);
1428 static struct filterops ugen_filtops_read =
1429 { 1, NULL, ugen_filt_detach, ugen_filt_read };
1430 static struct filterops ugen_filtops_write =
1431 { 1, NULL, ugen_filt_detach, ugen_filt_write };
1434 ugenkqfilter(struct dev_kqfilter_args *ap)
1436 cdev_t dev = ap->a_head.a_dev;
1437 struct knote *kn = ap->a_kn;
1438 struct klist *klist;
1439 struct ugen_softc *sc;
1440 struct ugen_endpoint *sce;
1442 sc = devclass_get_softc(ugen_devclass, UGENUNIT(dev));
1449 /* Do not allow filter on a control endpoint */
1450 if (UGENENDPOINT(dev) == USB_CONTROL_ENDPOINT)
1455 switch (kn->kn_filter) {
1457 sce = &sc->sc_endpoints[UGENENDPOINT(dev)][IN];
1458 kn->kn_fop = &ugen_filtops_read;
1459 kn->kn_hook = (caddr_t)dev;
1462 sce = &sc->sc_endpoints[UGENENDPOINT(dev)][OUT];
1463 kn->kn_fop = &ugen_filtops_write;
1464 kn->kn_hook = (caddr_t)dev;
1467 ap->a_result = EOPNOTSUPP;
1471 if (sce->edesc != NULL || sce->pipeh != NULL) {
1472 klist = &sce->rkq.ki_note;
1473 knote_insert(klist, kn);
1480 ugen_filt_detach(struct knote *kn)
1482 cdev_t dev = (cdev_t)kn->kn_hook;
1483 struct ugen_softc *sc;
1484 struct ugen_endpoint *sce;
1485 struct klist *klist;
1487 sc = devclass_get_softc(ugen_devclass, UGENUNIT(dev));
1489 switch (kn->kn_filter) {
1491 sce = &sc->sc_endpoints[UGENENDPOINT(dev)][IN];
1494 sce = &sc->sc_endpoints[UGENENDPOINT(dev)][OUT];
1500 if (sce->edesc != NULL || sce->pipeh != NULL) {
1501 klist = &sce->rkq.ki_note;
1502 knote_remove(klist, kn);
1507 ugen_filt_read(struct knote *kn, long hint)
1509 cdev_t dev = (cdev_t)kn->kn_hook;
1510 struct ugen_softc *sc;
1511 struct ugen_endpoint *sce;
1514 sc = devclass_get_softc(ugen_devclass, UGENUNIT(dev));
1515 sce = &sc->sc_endpoints[UGENENDPOINT(dev)][IN];
1517 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
1519 if (sce->q.c_cc > 0)
1522 case UE_ISOCHRONOUS:
1523 if (sce->cur != sce->fill)
1537 ugen_filt_write(struct knote *kn, long hint)
1539 cdev_t dev = (cdev_t)kn->kn_hook;
1540 struct ugen_softc *sc;
1541 struct ugen_endpoint *sce;
1544 sc = devclass_get_softc(ugen_devclass, UGENUNIT(dev));
1545 sce = &sc->sc_endpoints[UGENENDPOINT(dev)][OUT];
1547 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
1549 if (sce->q.c_cc > 0)
1552 case UE_ISOCHRONOUS:
1553 if (sce->cur != sce->fill)
1566 DRIVER_MODULE(ugen, uhub, ugen_driver, ugen_devclass, usbd_driver_load, 0);