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.15 2004/07/08 03:53:54 dillon 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 #if defined(__NetBSD__) || defined(__OpenBSD__)
58 #include <sys/device.h>
59 #include <sys/ioctl.h>
60 #elif defined(__FreeBSD__) || defined(__DragonFly__)
61 #include <sys/module.h>
63 #include <sys/ioccom.h>
65 #include <sys/fcntl.h>
66 #include <sys/filio.h>
70 #if defined(__FreeBSD__) && __FreeBSD_version >= 500014
71 #include <sys/selinfo.h>
73 #include <sys/select.h>
75 #include <sys/vnode.h>
77 #include <sys/sysctl.h>
79 #include <bus/usb/usb.h>
80 #include <bus/usb/usbdi.h>
81 #include <bus/usb/usbdi_util.h>
85 SYSCTL_NODE(_hw_usb, OID_AUTO, ugen, CTLFLAG_RW, 0, "USB ugen");
88 #define DPRINTF(x) if (ugendebug) logprintf x
89 #define DPRINTFN(n,x) if (ugendebug>(n)) logprintf x
91 SYSCTL_INT(_hw_usb_ugen, OID_AUTO, debug, CTLFLAG_RW,
92 &ugendebug, 0, "ugen debug level");
98 static int ugen_bufsize = 16384;
99 SYSCTL_INT(_hw_usb_ugen, OID_AUTO, bufsize, CTLFLAG_RW,
100 &ugen_bufsize, 0, "ugen temporary buffer size");
102 #define UGEN_CHUNK 128 /* chunk size for read */
103 #define UGEN_IBSIZE 1020 /* buffer size */
105 #define UGEN_NISOFRAMES 500 /* 0.5 seconds worth */
106 #define UGEN_NISOREQS 6 /* number of outstanding xfer requests */
107 #define UGEN_NISORFRMS 4 /* number of frames (miliseconds) per req */
109 struct ugen_endpoint {
110 struct ugen_softc *sc;
111 #if defined(__FreeBSD__) || defined(__DragonFly__)
114 usb_endpoint_descriptor_t *edesc;
115 usbd_interface_handle iface;
117 #define UGEN_ASLP 0x02 /* waiting for data */
118 #define UGEN_SHORT_OK 0x04 /* short xfers are OK */
119 usbd_pipe_handle pipeh;
122 u_char *ibuf; /* start of buffer (circular for isoc) */
123 u_char *fill; /* location for input (isoc) */
124 u_char *limit; /* end of circular buffer (isoc) */
125 u_char *cur; /* current read location (isoc) */
128 struct ugen_endpoint *sce;
129 usbd_xfer_handle xfer;
131 u_int16_t sizes[UGEN_NISORFRMS];
132 } isoreqs[UGEN_NISOREQS];
136 USBBASEDEVICE sc_dev; /* base device */
137 usbd_device_handle sc_udev;
139 char sc_is_open[USB_MAX_ENDPOINTS];
140 struct ugen_endpoint sc_endpoints[USB_MAX_ENDPOINTS][2];
148 #if defined(__NetBSD__) || defined(__OpenBSD__)
150 #elif defined(__FreeBSD__) || defined(__DragonFly__)
158 #define UGEN_CDEV_MAJOR 114
160 Static struct cdevsw ugen_cdevsw = {
162 /* maj */ UGEN_CDEV_MAJOR,
168 /* close */ ugenclose,
170 /* write */ ugenwrite,
171 /* ioctl */ ugenioctl,
174 /* strategy */ nostrategy,
180 Static void ugenintr(usbd_xfer_handle xfer, usbd_private_handle addr,
182 Static void ugen_isoc_rintr(usbd_xfer_handle xfer, usbd_private_handle addr,
184 Static int ugen_do_read(struct ugen_softc *, int, struct uio *, int);
185 Static int ugen_do_write(struct ugen_softc *, int, struct uio *, int);
186 Static int ugen_do_ioctl(struct ugen_softc *, int, u_long,
187 caddr_t, int, usb_proc_ptr);
188 #if defined(__FreeBSD__) || defined(__DragonFly__)
189 Static void ugen_make_devnodes(struct ugen_softc *sc);
190 Static void ugen_destroy_devnodes(struct ugen_softc *sc);
192 Static int ugen_set_config(struct ugen_softc *sc, int configno);
193 Static usb_config_descriptor_t *ugen_get_cdesc(struct ugen_softc *sc,
194 int index, int *lenp);
195 Static usbd_status ugen_set_interface(struct ugen_softc *, int, int);
196 Static int ugen_get_alt_index(struct ugen_softc *sc, int ifaceidx);
198 #define UGENUNIT(n) ((lminor(n) >> 4) & 0xff)
199 #define UGENENDPOINT(n) (minor(n) & 0xf)
200 #define UGENMINOR(u, e) (((u & 0xf) << 4) | ((u & 0xf0) << 12) | (e))
201 #define UGENUNITMASK 0xffff00f0
203 USB_DECLARE_DRIVER(ugen);
207 USB_MATCH_START(ugen, uaa);
211 return (uaa->matchlvl);
214 return (UMATCH_GENERIC);
216 return (UMATCH_NONE);
221 USB_ATTACH_START(ugen, sc, uaa);
222 usbd_device_handle udev;
227 usbd_devinfo(uaa->device, 0, devinfo);
229 printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
231 sc->sc_udev = udev = uaa->device;
233 memset(sc->sc_endpoints, 0, sizeof sc->sc_endpoints);
235 /* First set configuration index 0, the default one for ugen. */
236 err = usbd_set_config_index(udev, 0, 0);
238 printf("%s: setting configuration index 0 failed\n",
239 USBDEVNAME(sc->sc_dev));
241 USB_ATTACH_ERROR_RETURN;
243 conf = usbd_get_config_descriptor(udev)->bConfigurationValue;
245 /* Set up all the local state for this configuration. */
246 err = ugen_set_config(sc, conf);
248 printf("%s: setting configuration %d failed\n",
249 USBDEVNAME(sc->sc_dev), conf);
251 USB_ATTACH_ERROR_RETURN;
254 #if defined(__FreeBSD__) || defined(__DragonFly__)
255 /* the main device, ctrl endpoint */
256 cdevsw_add(&ugen_cdevsw,
257 UGENUNITMASK, UGENMINOR(USBDEVUNIT(sc->sc_dev), 0));
258 make_dev(&ugen_cdevsw, UGENMINOR(USBDEVUNIT(sc->sc_dev), 0),
259 UID_ROOT, GID_OPERATOR, 0644, "%s", USBDEVNAME(sc->sc_dev));
262 USB_ATTACH_SUCCESS_RETURN;
265 #if defined(__FreeBSD__) || defined(__DragonFly__)
267 ugen_make_devnodes(struct ugen_softc *sc)
272 for (endptno = 1; endptno < USB_MAX_ENDPOINTS; endptno++) {
273 if (sc->sc_endpoints[endptno][IN].sc != NULL ||
274 sc->sc_endpoints[endptno][OUT].sc != NULL ) {
275 /* endpt can be 0x81 and 0x01, representing
276 * endpoint address 0x01 and IN/OUT directions.
277 * We map both endpts to the same device,
278 * IN is reading from it, OUT is writing to it.
280 * In the if clause above we check whether one
281 * of the structs is populated.
283 dev = make_dev(&ugen_cdevsw,
284 UGENMINOR(USBDEVUNIT(sc->sc_dev), endptno),
285 UID_ROOT, GID_OPERATOR, 0644,
287 USBDEVNAME(sc->sc_dev), endptno);
288 if (sc->sc_endpoints[endptno][IN].sc != NULL) {
290 if (sc->sc_endpoints[endptno][IN].dev)
291 release_dev(sc->sc_endpoints[endptno][IN].dev);
292 sc->sc_endpoints[endptno][IN].dev = dev;
294 if (sc->sc_endpoints[endptno][OUT].sc != NULL) {
296 if (sc->sc_endpoints[endptno][OUT].dev)
297 release_dev(sc->sc_endpoints[endptno][OUT].dev);
298 sc->sc_endpoints[endptno][OUT].dev = dev;
305 ugen_destroy_devnodes(struct ugen_softc *sc)
310 /* destroy all devices for the other (existing) endpoints as well */
311 for (endptno = 1; endptno < USB_MAX_ENDPOINTS; endptno++) {
312 if (sc->sc_endpoints[endptno][IN].sc != NULL ||
313 sc->sc_endpoints[endptno][OUT].sc != NULL ) {
314 /* endpt can be 0x81 and 0x01, representing
315 * endpoint address 0x01 and IN/OUT directions.
316 * We map both endpoint addresses to the same device,
317 * IN is reading from it, OUT is writing to it.
319 * In the if clause above we check whether one
320 * of the structs is populated.
322 dev = sc->sc_endpoints[endptno][IN].dev;
325 sc->sc_endpoints[endptno][IN].dev = NULL;
327 dev = sc->sc_endpoints[endptno][OUT].dev;
330 sc->sc_endpoints[endptno][OUT].dev = NULL;
338 ugen_set_config(struct ugen_softc *sc, int configno)
340 usbd_device_handle dev = sc->sc_udev;
341 usbd_interface_handle iface;
342 usb_endpoint_descriptor_t *ed;
343 struct ugen_endpoint *sce;
344 u_int8_t niface, nendpt;
345 int ifaceno, endptno, endpt;
349 DPRINTFN(1,("ugen_set_config: %s to configno %d, sc=%p\n",
350 USBDEVNAME(sc->sc_dev), configno, sc));
352 #if defined(__FreeBSD__) || defined(__DragonFly__)
353 ugen_destroy_devnodes(sc);
356 /* We start at 1, not 0, because we don't care whether the
357 * control endpoint is open or not. It is always present.
359 for (endptno = 1; endptno < USB_MAX_ENDPOINTS; endptno++) {
360 if (sc->sc_is_open[endptno]) {
362 ("ugen_set_config: %s - endpoint %d is open\n",
363 USBDEVNAME(sc->sc_dev), endptno));
364 return (USBD_IN_USE);
368 /* Avoid setting the current value. */
369 if (usbd_get_config_descriptor(dev)->bConfigurationValue != configno) {
370 err = usbd_set_config_no(dev, configno, 1);
375 err = usbd_interface_count(dev, &niface);
378 memset(sc->sc_endpoints, 0, sizeof sc->sc_endpoints);
379 for (ifaceno = 0; ifaceno < niface; ifaceno++) {
380 DPRINTFN(1,("ugen_set_config: ifaceno %d\n", ifaceno));
381 err = usbd_device2interface_handle(dev, ifaceno, &iface);
384 err = usbd_endpoint_count(iface, &nendpt);
387 for (endptno = 0; endptno < nendpt; endptno++) {
388 ed = usbd_interface2endpoint_descriptor(iface,endptno);
389 endpt = ed->bEndpointAddress;
390 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
391 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
392 DPRINTFN(1,("ugen_set_config: endptno %d, endpt=0x%02x"
394 endptno, endpt, UE_GET_ADDR(endpt),
395 UE_GET_DIR(endpt), sce));
402 #if defined(__FreeBSD__) || defined(__DragonFly__)
403 ugen_make_devnodes(sc);
406 return (USBD_NORMAL_COMPLETION);
410 ugenopen(dev_t dev, int flag, int mode, usb_proc_ptr p)
412 struct ugen_softc *sc;
413 int unit = UGENUNIT(dev);
414 int endpt = UGENENDPOINT(dev);
415 usb_endpoint_descriptor_t *edesc;
416 struct ugen_endpoint *sce;
419 usbd_xfer_handle xfer;
423 USB_GET_SC_OPEN(ugen, unit, sc);
425 DPRINTFN(5, ("ugenopen: flag=%d, mode=%d, unit=%d endpt=%d\n",
426 flag, mode, unit, endpt));
428 if (sc == NULL || sc->sc_dying)
431 if (sc->sc_is_open[endpt])
434 if (endpt == USB_CONTROL_ENDPOINT) {
435 sc->sc_is_open[USB_CONTROL_ENDPOINT] = 1;
439 /* Make sure there are pipes for all directions. */
440 for (dir = OUT; dir <= IN; dir++) {
441 if (flag & (dir == OUT ? FWRITE : FREAD)) {
442 sce = &sc->sc_endpoints[endpt][dir];
443 if (sce == 0 || sce->edesc == 0)
448 /* Actually open the pipes. */
449 /* XXX Should back out properly if it fails. */
450 for (dir = OUT; dir <= IN; dir++) {
451 if (!(flag & (dir == OUT ? FWRITE : FREAD)))
453 sce = &sc->sc_endpoints[endpt][dir];
455 sce->timeout = USBD_NO_TIMEOUT;
456 DPRINTFN(5, ("ugenopen: sc=%p, endpt=%d, dir=%d, sce=%p\n",
457 sc, endpt, dir, sce));
459 switch (edesc->bmAttributes & UE_XFERTYPE) {
462 err = usbd_open_pipe(sce->iface,
463 edesc->bEndpointAddress, 0, &sce->pipeh);
468 isize = UGETW(edesc->wMaxPacketSize);
469 if (isize == 0) /* shouldn't happen */
471 sce->ibuf = malloc(isize, M_USBDEV, M_WAITOK);
472 DPRINTFN(5, ("ugenopen: intr endpt=%d,isize=%d\n",
474 if (clalloc(&sce->q, UGEN_IBSIZE, 0) == -1)
476 err = usbd_open_pipe_intr(sce->iface,
477 edesc->bEndpointAddress,
478 USBD_SHORT_XFER_OK, &sce->pipeh, sce,
479 sce->ibuf, isize, ugenintr,
480 USBD_DEFAULT_INTERVAL);
482 free(sce->ibuf, M_USBDEV);
486 DPRINTFN(5, ("ugenopen: interrupt open done\n"));
489 err = usbd_open_pipe(sce->iface,
490 edesc->bEndpointAddress, 0, &sce->pipeh);
497 isize = UGETW(edesc->wMaxPacketSize);
498 if (isize == 0) /* shouldn't happen */
500 sce->ibuf = malloc(isize * UGEN_NISOFRAMES,
502 sce->cur = sce->fill = sce->ibuf;
503 sce->limit = sce->ibuf + isize * UGEN_NISOFRAMES;
504 DPRINTFN(5, ("ugenopen: isoc endpt=%d, isize=%d\n",
506 err = usbd_open_pipe(sce->iface,
507 edesc->bEndpointAddress, 0, &sce->pipeh);
509 free(sce->ibuf, M_USBDEV);
512 for(i = 0; i < UGEN_NISOREQS; ++i) {
513 sce->isoreqs[i].sce = sce;
514 xfer = usbd_alloc_xfer(sc->sc_udev);
517 sce->isoreqs[i].xfer = xfer;
518 buf = usbd_alloc_buffer
519 (xfer, isize * UGEN_NISORFRMS);
524 sce->isoreqs[i].dmabuf = buf;
525 for(j = 0; j < UGEN_NISORFRMS; ++j)
526 sce->isoreqs[i].sizes[j] = isize;
528 (xfer, sce->pipeh, &sce->isoreqs[i],
529 sce->isoreqs[i].sizes,
530 UGEN_NISORFRMS, USBD_NO_COPY,
532 (void)usbd_transfer(xfer);
534 DPRINTFN(5, ("ugenopen: isoc open done\n"));
537 while (--i >= 0) /* implicit buffer free */
538 usbd_free_xfer(sce->isoreqs[i].xfer);
541 sce->timeout = USBD_DEFAULT_TIMEOUT;
545 sc->sc_is_open[endpt] = 1;
550 ugenclose(dev_t dev, int flag, int mode, usb_proc_ptr p)
552 int endpt = UGENENDPOINT(dev);
553 struct ugen_softc *sc;
554 struct ugen_endpoint *sce;
558 USB_GET_SC(ugen, UGENUNIT(dev), sc);
560 DPRINTFN(5, ("ugenclose: flag=%d, mode=%d, unit=%d, endpt=%d\n",
561 flag, mode, UGENUNIT(dev), endpt));
564 if (!sc->sc_is_open[endpt]) {
565 printf("ugenclose: not open\n");
570 if (endpt == USB_CONTROL_ENDPOINT) {
571 DPRINTFN(5, ("ugenclose: close control\n"));
572 sc->sc_is_open[endpt] = 0;
576 for (dir = OUT; dir <= IN; dir++) {
577 if (!(flag & (dir == OUT ? FWRITE : FREAD)))
579 sce = &sc->sc_endpoints[endpt][dir];
580 if (sce == NULL || sce->pipeh == NULL)
582 DPRINTFN(5, ("ugenclose: endpt=%d dir=%d sce=%p\n",
585 usbd_abort_pipe(sce->pipeh);
586 usbd_close_pipe(sce->pipeh);
589 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
591 ndflush(&sce->q, sce->q.c_cc);
595 for (i = 0; i < UGEN_NISOREQS; ++i)
596 usbd_free_xfer(sce->isoreqs[i].xfer);
601 if (sce->ibuf != NULL) {
602 free(sce->ibuf, M_USBDEV);
607 sc->sc_is_open[endpt] = 0;
613 ugen_do_read(struct ugen_softc *sc, int endpt, struct uio *uio, int flag)
615 struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][IN];
618 usbd_xfer_handle xfer;
623 u_char buffer[UGEN_CHUNK];
625 DPRINTFN(5, ("%s: ugenread: %d\n", USBDEVNAME(sc->sc_dev), endpt));
630 if (endpt == USB_CONTROL_ENDPOINT)
636 if (sce->edesc == NULL) {
637 printf("ugenread: no edesc\n");
640 if (sce->pipeh == NULL) {
641 printf("ugenread: no pipe\n");
645 buf = getugenbuf(ugen_bufsize, &ugen_bbsize);
647 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
649 /* Block until activity occurred. */
651 while (sce->q.c_cc == 0) {
652 if (flag & IO_NDELAY) {
657 sce->state |= UGEN_ASLP;
658 DPRINTFN(5, ("ugenread: sleep on %p\n", sce));
659 error = tsleep(sce, PCATCH, "ugenri", 0);
660 DPRINTFN(5, ("ugenread: woke, error=%d\n", error));
664 sce->state &= ~UGEN_ASLP;
670 /* Transfer as many chunks as possible. */
671 while (sce->q.c_cc > 0 && uio->uio_resid > 0 && !error) {
672 n = min(sce->q.c_cc, uio->uio_resid);
673 if (n > sizeof(buffer))
676 /* Remove a small chunk from the input queue. */
677 q_to_b(&sce->q, buffer, n);
678 DPRINTFN(5, ("ugenread: got %d chars\n", n));
680 /* Copy the data to the user process. */
681 error = uiomove(buffer, n, uio);
687 xfer = usbd_alloc_xfer(sc->sc_udev);
692 while ((n = min(ugen_bbsize, uio->uio_resid)) != 0) {
693 DPRINTFN(1, ("ugenread: start transfer %d bytes\n",n));
695 err = usbd_bulk_transfer(
697 sce->state & UGEN_SHORT_OK ?
698 USBD_SHORT_XFER_OK : 0,
699 sce->timeout, buf, &tn, "ugenrb");
701 if (err == USBD_INTERRUPTED)
703 else if (err == USBD_TIMEOUT)
709 DPRINTFN(1, ("ugenread: got %d bytes\n", tn));
710 error = uiomove(buf, tn, uio);
714 usbd_free_xfer(xfer);
718 while (sce->cur == sce->fill) {
719 if (flag & IO_NDELAY) {
724 sce->state |= UGEN_ASLP;
725 DPRINTFN(5, ("ugenread: sleep on %p\n", sce));
726 error = tsleep(sce, PCATCH, "ugenri", 0);
727 DPRINTFN(5, ("ugenread: woke, error=%d\n", error));
731 sce->state &= ~UGEN_ASLP;
736 while (sce->cur != sce->fill && uio->uio_resid > 0 && !error) {
737 if(sce->fill > sce->cur)
738 n = min(sce->fill - sce->cur, uio->uio_resid);
740 n = min(sce->limit - sce->cur, uio->uio_resid);
742 DPRINTFN(5, ("ugenread: isoc got %d chars\n", n));
744 /* Copy the data to the user process. */
745 error = uiomove(sce->cur, n, uio);
749 if(sce->cur >= sce->limit)
750 sce->cur = sce->ibuf;
761 relugenbuf(buf, ugen_bbsize);
766 ugenread(dev_t dev, struct uio *uio, int flag)
768 int endpt = UGENENDPOINT(dev);
769 struct ugen_softc *sc;
772 USB_GET_SC(ugen, UGENUNIT(dev), sc);
775 error = ugen_do_read(sc, endpt, uio, flag);
776 if (--sc->sc_refcnt < 0)
777 usb_detach_wakeup(USBDEV(sc->sc_dev));
782 ugen_do_write(struct ugen_softc *sc, int endpt, struct uio *uio, int flag)
784 struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][OUT];
789 usbd_xfer_handle xfer;
792 DPRINTFN(5, ("%s: ugenwrite: %d\n", USBDEVNAME(sc->sc_dev), endpt));
797 if (endpt == USB_CONTROL_ENDPOINT)
803 if (sce->edesc == NULL) {
804 printf("ugenwrite: no edesc\n");
807 if (sce->pipeh == NULL) {
808 printf("ugenwrite: no pipe\n");
812 buf = getugenbuf(ugen_bufsize, &ugen_bbsize);
814 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
816 xfer = usbd_alloc_xfer(sc->sc_udev);
821 while ((n = min(ugen_bbsize, uio->uio_resid)) != 0) {
822 error = uiomove(buf, n, uio);
825 DPRINTFN(1, ("ugenwrite: transfer %d bytes\n", n));
826 err = usbd_bulk_transfer(xfer, sce->pipeh, 0,
827 sce->timeout, buf, &n,"ugenwb");
829 if (err == USBD_INTERRUPTED)
831 else if (err == USBD_TIMEOUT)
838 usbd_free_xfer(xfer);
841 xfer = usbd_alloc_xfer(sc->sc_udev);
846 while ((n = min(UGETW(sce->edesc->wMaxPacketSize),
847 uio->uio_resid)) != 0) {
848 error = uiomove(buf, n, uio);
851 DPRINTFN(1, ("ugenwrite: transfer %d bytes\n", n));
852 err = usbd_intr_transfer(xfer, sce->pipeh, 0,
853 sce->timeout, buf, &n,"ugenwi");
855 if (err == USBD_INTERRUPTED)
857 else if (err == USBD_TIMEOUT)
864 usbd_free_xfer(xfer);
871 relugenbuf(buf, ugen_bbsize);
876 ugenwrite(dev_t dev, struct uio *uio, int flag)
878 int endpt = UGENENDPOINT(dev);
879 struct ugen_softc *sc;
882 USB_GET_SC(ugen, UGENUNIT(dev), sc);
885 error = ugen_do_write(sc, endpt, uio, flag);
886 if (--sc->sc_refcnt < 0)
887 usb_detach_wakeup(USBDEV(sc->sc_dev));
891 #if defined(__NetBSD__) || defined(__OpenBSD__)
893 ugen_activate(device_ptr_t self, enum devact act)
895 struct ugen_softc *sc = (struct ugen_softc *)self;
901 case DVACT_DEACTIVATE:
911 USB_DETACH_START(ugen, sc);
912 struct ugen_endpoint *sce;
915 #if defined(__NetBSD__) || defined(__OpenBSD__)
919 #if defined(__NetBSD__) || defined(__OpenBSD__)
920 DPRINTF(("ugen_detach: sc=%p flags=%d\n", sc, flags));
921 #elif defined(__FreeBSD__) || defined(__DragonFly__)
922 DPRINTF(("ugen_detach: sc=%p\n", sc));
926 /* Abort all pipes. Causes processes waiting for transfer to wake. */
927 for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
928 for (dir = OUT; dir <= IN; dir++) {
929 sce = &sc->sc_endpoints[i][dir];
930 if (sce && sce->pipeh)
931 usbd_abort_pipe(sce->pipeh);
935 if (--sc->sc_refcnt >= 0) {
937 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
938 wakeup(&sc->sc_endpoints[i][IN]);
939 /* Wait for processes to go away. */
940 usb_detach_wait(USBDEV(sc->sc_dev));
944 #if defined(__NetBSD__) || defined(__OpenBSD__)
945 /* locate the major number */
946 for (maj = 0; maj < nchrdev; maj++)
947 if (cdevsw[maj].d_open == ugenopen)
950 /* Nuke the vnodes for any open instances (calls close). */
951 mn = self->dv_unit * USB_MAX_ENDPOINTS;
952 vdevgone(maj, mn, mn + USB_MAX_ENDPOINTS - 1, VCHR);
953 #elif defined(__FreeBSD__) || defined(__DragonFly__)
954 /* destroy the device for the control endpoint */
955 ugen_destroy_devnodes(sc);
956 cdevsw_remove(&ugen_cdevsw,
957 UGENUNITMASK, UGENMINOR(USBDEVUNIT(sc->sc_dev), 0));
963 ugenintr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
965 struct ugen_endpoint *sce = addr;
966 /*struct ugen_softc *sc = sce->sc;*/
970 if (status == USBD_CANCELLED)
973 if (status != USBD_NORMAL_COMPLETION) {
974 DPRINTF(("ugenintr: status=%d\n", status));
975 if (status == USBD_STALLED)
976 usbd_clear_endpoint_stall_async(sce->pipeh);
980 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
983 DPRINTFN(5, ("ugenintr: xfer=%p status=%d count=%d\n",
984 xfer, status, count));
985 DPRINTFN(5, (" data = %02x %02x %02x\n",
986 ibuf[0], ibuf[1], ibuf[2]));
988 (void)b_to_q(ibuf, count, &sce->q);
990 if (sce->state & UGEN_ASLP) {
991 sce->state &= ~UGEN_ASLP;
992 DPRINTFN(5, ("ugen_intr: waking %p\n", sce));
995 selwakeuppri(&sce->rsel, 0);
999 ugen_isoc_rintr(usbd_xfer_handle xfer, usbd_private_handle addr,
1002 struct isoreq *req = addr;
1003 struct ugen_endpoint *sce = req->sce;
1007 /* Return if we are aborting. */
1008 if (status == USBD_CANCELLED)
1011 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
1012 DPRINTFN(5,("ugen_isoc_rintr: xfer %d, count=%d\n",
1013 (int)(req - sce->isoreqs),
1016 /* throw away oldest input if the buffer is full */
1017 if(sce->fill < sce->cur && sce->cur <= sce->fill + count) {
1019 if(sce->cur >= sce->limit)
1020 sce->cur = sce->ibuf + (sce->limit - sce->cur);
1021 DPRINTFN(5, ("ugen_isoc_rintr: throwing away %d bytes\n",
1025 isize = UGETW(sce->edesc->wMaxPacketSize);
1026 for (i = 0; i < UGEN_NISORFRMS; i++) {
1027 u_int32_t actlen = req->sizes[i];
1028 char const *buf = (char const *)req->dmabuf + isize * i;
1030 /* copy data to buffer */
1031 while (actlen > 0) {
1032 n = min(actlen, sce->limit - sce->fill);
1033 memcpy(sce->fill, buf, n);
1038 if(sce->fill == sce->limit)
1039 sce->fill = sce->ibuf;
1042 /* setup size for next transfer */
1043 req->sizes[i] = isize;
1046 usbd_setup_isoc_xfer(xfer, sce->pipeh, req, req->sizes, UGEN_NISORFRMS,
1047 USBD_NO_COPY, ugen_isoc_rintr);
1048 (void)usbd_transfer(xfer);
1050 if (sce->state & UGEN_ASLP) {
1051 sce->state &= ~UGEN_ASLP;
1052 DPRINTFN(5, ("ugen_isoc_rintr: waking %p\n", sce));
1055 selwakeuppri(&sce->rsel, 0);
1059 ugen_set_interface(struct ugen_softc *sc, int ifaceidx, int altno)
1061 usbd_interface_handle iface;
1062 usb_endpoint_descriptor_t *ed;
1064 struct ugen_endpoint *sce;
1065 u_int8_t niface, nendpt, endptno, endpt;
1068 DPRINTFN(15, ("ugen_set_interface %d %d\n", ifaceidx, altno));
1070 err = usbd_interface_count(sc->sc_udev, &niface);
1073 if (ifaceidx < 0 || ifaceidx >= niface)
1074 return (USBD_INVAL);
1076 err = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface);
1079 err = usbd_endpoint_count(iface, &nendpt);
1083 #if defined(__FreeBSD__) || defined(__DragonFly__)
1084 /* destroy the existing devices, we remake the new ones in a moment */
1085 ugen_destroy_devnodes(sc);
1088 /* XXX should only do this after setting new altno has succeeded */
1089 for (endptno = 0; endptno < nendpt; endptno++) {
1090 ed = usbd_interface2endpoint_descriptor(iface,endptno);
1091 endpt = ed->bEndpointAddress;
1092 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
1093 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
1099 /* change setting */
1100 err = usbd_set_interface(iface, altno);
1104 err = usbd_endpoint_count(iface, &nendpt);
1107 for (endptno = 0; endptno < nendpt; endptno++) {
1108 ed = usbd_interface2endpoint_descriptor(iface,endptno);
1109 endpt = ed->bEndpointAddress;
1110 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
1111 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
1117 #if defined(__FreeBSD__) || defined(__DragonFly__)
1118 /* make the new devices */
1119 ugen_make_devnodes(sc);
1125 /* Retrieve a complete descriptor for a certain device and index. */
1126 Static usb_config_descriptor_t *
1127 ugen_get_cdesc(struct ugen_softc *sc, int index, int *lenp)
1129 usb_config_descriptor_t *cdesc, *tdesc, cdescr;
1133 if (index == USB_CURRENT_CONFIG_INDEX) {
1134 tdesc = usbd_get_config_descriptor(sc->sc_udev);
1135 len = UGETW(tdesc->wTotalLength);
1138 cdesc = malloc(len, M_TEMP, M_INTWAIT);
1139 memcpy(cdesc, tdesc, len);
1140 DPRINTFN(5,("ugen_get_cdesc: current, len=%d\n", len));
1142 err = usbd_get_config_desc(sc->sc_udev, index, &cdescr);
1145 len = UGETW(cdescr.wTotalLength);
1146 DPRINTFN(5,("ugen_get_cdesc: index=%d, len=%d\n", index, len));
1149 cdesc = malloc(len, M_TEMP, M_INTWAIT);
1150 err = usbd_get_config_desc_full(sc->sc_udev, index, cdesc, len);
1152 free(cdesc, M_TEMP);
1160 ugen_get_alt_index(struct ugen_softc *sc, int ifaceidx)
1162 usbd_interface_handle iface;
1165 err = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface);
1168 return (usbd_get_interface_altindex(iface));
1172 ugen_do_ioctl(struct ugen_softc *sc, int endpt, u_long cmd,
1173 caddr_t addr, int flag, usb_proc_ptr p)
1175 struct ugen_endpoint *sce;
1177 usbd_interface_handle iface;
1178 struct usb_config_desc *cd;
1179 usb_config_descriptor_t *cdesc;
1180 struct usb_interface_desc *id;
1181 usb_interface_descriptor_t *idesc;
1182 struct usb_endpoint_desc *ed;
1183 usb_endpoint_descriptor_t *edesc;
1184 struct usb_alt_interface *ai;
1185 struct usb_string_desc *si;
1188 DPRINTFN(5, ("ugenioctl: cmd=%08lx\n", cmd));
1194 /* All handled in the upper FS layer. */
1196 case USB_SET_SHORT_XFER:
1197 /* This flag only affects read */
1198 if (endpt == USB_CONTROL_ENDPOINT)
1200 sce = &sc->sc_endpoints[endpt][IN];
1204 if (sce->pipeh == NULL) {
1205 printf("ugenioctl: USB_SET_SHORT_XFER, no pipe\n");
1210 sce->state |= UGEN_SHORT_OK;
1212 sce->state &= ~UGEN_SHORT_OK;
1214 case USB_SET_TIMEOUT:
1215 sce = &sc->sc_endpoints[endpt][IN];
1218 sce->timeout = *(int *)addr;
1224 if (endpt != USB_CONTROL_ENDPOINT)
1230 ugendebug = *(int *)addr;
1233 case USB_GET_CONFIG:
1234 err = usbd_get_config(sc->sc_udev, &conf);
1237 *(int *)addr = conf;
1239 case USB_SET_CONFIG:
1240 if (!(flag & FWRITE))
1242 err = ugen_set_config(sc, *(int *)addr);
1244 case USBD_NORMAL_COMPLETION:
1252 case USB_GET_ALTINTERFACE:
1253 ai = (struct usb_alt_interface *)addr;
1254 err = usbd_device2interface_handle(sc->sc_udev,
1255 ai->uai_interface_index, &iface);
1258 idesc = usbd_get_interface_descriptor(iface);
1261 ai->uai_alt_no = idesc->bAlternateSetting;
1263 case USB_SET_ALTINTERFACE:
1264 if (!(flag & FWRITE))
1266 ai = (struct usb_alt_interface *)addr;
1267 err = usbd_device2interface_handle(sc->sc_udev,
1268 ai->uai_interface_index, &iface);
1271 err = ugen_set_interface(sc, ai->uai_interface_index, ai->uai_alt_no);
1275 case USB_GET_NO_ALT:
1276 ai = (struct usb_alt_interface *)addr;
1277 cdesc = ugen_get_cdesc(sc, ai->uai_config_index, 0);
1280 idesc = usbd_find_idesc(cdesc, ai->uai_interface_index, 0);
1281 if (idesc == NULL) {
1282 free(cdesc, M_TEMP);
1285 ai->uai_alt_no = usbd_get_no_alts(cdesc, idesc->bInterfaceNumber);
1286 free(cdesc, M_TEMP);
1288 case USB_GET_DEVICE_DESC:
1289 *(usb_device_descriptor_t *)addr =
1290 *usbd_get_device_descriptor(sc->sc_udev);
1292 case USB_GET_CONFIG_DESC:
1293 cd = (struct usb_config_desc *)addr;
1294 cdesc = ugen_get_cdesc(sc, cd->ucd_config_index, 0);
1297 cd->ucd_desc = *cdesc;
1298 free(cdesc, M_TEMP);
1300 case USB_GET_INTERFACE_DESC:
1301 id = (struct usb_interface_desc *)addr;
1302 cdesc = ugen_get_cdesc(sc, id->uid_config_index, 0);
1305 if (id->uid_config_index == USB_CURRENT_CONFIG_INDEX &&
1306 id->uid_alt_index == USB_CURRENT_ALT_INDEX)
1307 alt = ugen_get_alt_index(sc, id->uid_interface_index);
1309 alt = id->uid_alt_index;
1310 idesc = usbd_find_idesc(cdesc, id->uid_interface_index, alt);
1311 if (idesc == NULL) {
1312 free(cdesc, M_TEMP);
1315 id->uid_desc = *idesc;
1316 free(cdesc, M_TEMP);
1318 case USB_GET_ENDPOINT_DESC:
1319 ed = (struct usb_endpoint_desc *)addr;
1320 cdesc = ugen_get_cdesc(sc, ed->ued_config_index, 0);
1323 if (ed->ued_config_index == USB_CURRENT_CONFIG_INDEX &&
1324 ed->ued_alt_index == USB_CURRENT_ALT_INDEX)
1325 alt = ugen_get_alt_index(sc, ed->ued_interface_index);
1327 alt = ed->ued_alt_index;
1328 edesc = usbd_find_edesc(cdesc, ed->ued_interface_index,
1329 alt, ed->ued_endpoint_index);
1330 if (edesc == NULL) {
1331 free(cdesc, M_TEMP);
1334 ed->ued_desc = *edesc;
1335 free(cdesc, M_TEMP);
1337 case USB_GET_FULL_DESC:
1342 struct usb_full_desc *fd = (struct usb_full_desc *)addr;
1345 cdesc = ugen_get_cdesc(sc, fd->ufd_config_index, &len);
1346 if (len > fd->ufd_size)
1348 iov.iov_base = (caddr_t)fd->ufd_data;
1352 uio.uio_resid = len;
1354 uio.uio_segflg = UIO_USERSPACE;
1355 uio.uio_rw = UIO_READ;
1357 error = uiomove((void *)cdesc, len, &uio);
1358 free(cdesc, M_TEMP);
1361 case USB_GET_STRING_DESC:
1362 si = (struct usb_string_desc *)addr;
1363 err = usbd_get_string_desc(sc->sc_udev, si->usd_string_index,
1364 si->usd_language_id, &si->usd_desc);
1368 case USB_DO_REQUEST:
1370 struct usb_ctl_request *ur = (void *)addr;
1371 int len = UGETW(ur->ucr_request.wLength);
1378 if (!(flag & FWRITE))
1380 /* Avoid requests that would damage the bus integrity. */
1381 if ((ur->ucr_request.bmRequestType == UT_WRITE_DEVICE &&
1382 ur->ucr_request.bRequest == UR_SET_ADDRESS) ||
1383 (ur->ucr_request.bmRequestType == UT_WRITE_DEVICE &&
1384 ur->ucr_request.bRequest == UR_SET_CONFIG) ||
1385 (ur->ucr_request.bmRequestType == UT_WRITE_INTERFACE &&
1386 ur->ucr_request.bRequest == UR_SET_INTERFACE))
1389 if (len < 0 || len > 32767)
1392 iov.iov_base = (caddr_t)ur->ucr_data;
1396 uio.uio_resid = len;
1398 uio.uio_segflg = UIO_USERSPACE;
1400 ur->ucr_request.bmRequestType & UT_READ ?
1401 UIO_READ : UIO_WRITE;
1403 ptr = malloc(len, M_TEMP, M_WAITOK);
1404 if (uio.uio_rw == UIO_WRITE) {
1405 error = uiomove(ptr, len, &uio);
1410 sce = &sc->sc_endpoints[endpt][IN];
1411 err = usbd_do_request_flags(sc->sc_udev, &ur->ucr_request,
1412 ptr, ur->ucr_flags, &ur->ucr_actlen, sce->timeout);
1418 if (uio.uio_rw == UIO_READ) {
1419 error = uiomove(ptr, len, &uio);
1429 case USB_GET_DEVICEINFO:
1430 usbd_fill_deviceinfo(sc->sc_udev,
1431 (struct usb_device_info *)addr, 1);
1440 ugenioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, usb_proc_ptr p)
1442 int endpt = UGENENDPOINT(dev);
1443 struct ugen_softc *sc;
1446 USB_GET_SC(ugen, UGENUNIT(dev), sc);
1449 error = ugen_do_ioctl(sc, endpt, cmd, addr, flag, p);
1450 if (--sc->sc_refcnt < 0)
1451 usb_detach_wakeup(USBDEV(sc->sc_dev));
1456 ugenpoll(dev_t dev, int events, usb_proc_ptr p)
1458 struct ugen_softc *sc;
1459 struct ugen_endpoint *sce;
1463 USB_GET_SC(ugen, UGENUNIT(dev), sc);
1469 sce = &sc->sc_endpoints[UGENENDPOINT(dev)][IN];
1474 printf("ugenpoll: no edesc\n");
1478 printf("ugenpoll: no pipe\n");
1483 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
1485 if (events & (POLLIN | POLLRDNORM)) {
1486 if (sce->q.c_cc > 0)
1487 revents |= events & (POLLIN | POLLRDNORM);
1489 selrecord(p, &sce->rsel);
1492 case UE_ISOCHRONOUS:
1493 if (events & (POLLIN | POLLRDNORM)) {
1494 if (sce->cur != sce->fill)
1495 revents |= events & (POLLIN | POLLRDNORM);
1497 selrecord(p, &sce->rsel);
1502 * We have no easy way of determining if a read will
1503 * yield any data or a write will happen.
1504 * Pretend they will.
1507 (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM);
1516 #if defined(__FreeBSD__) || defined(__DragonFly__)
1517 DRIVER_MODULE(ugen, uhub, ugen_driver, ugen_devclass, usbd_driver_load, 0);