1 /* $NetBSD: ugen.c,v 1.27 1999/10/28 12:08:38 augustss Exp $ */
2 /* $FreeBSD: src/sys/dev/usb/ugen.c,v 1.38.2.9 2002/11/06 14:41:01 joe Exp $ */
5 * Copyright (c) 1998 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (lennart@augustsson.net) at
10 * Carlstedt Research & Technology.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the NetBSD
23 * Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #if defined(__NetBSD__) || defined(__OpenBSD__)
47 #include <sys/device.h>
48 #include <sys/ioctl.h>
49 #elif defined(__FreeBSD__)
50 #include <sys/module.h>
52 #include <sys/ioccom.h>
54 #include <sys/fcntl.h>
55 #include <sys/filio.h>
59 #include <sys/select.h>
61 #include <sys/vnode.h>
63 #include <sys/sysctl.h>
65 #include <dev/usb/usb.h>
66 #include <dev/usb/usbdi.h>
67 #include <dev/usb/usbdi_util.h>
70 #define DPRINTF(x) if (ugendebug) logprintf x
71 #define DPRINTFN(n,x) if (ugendebug>(n)) logprintf x
73 SYSCTL_NODE(_hw_usb, OID_AUTO, ugen, CTLFLAG_RW, 0, "USB ugen");
74 SYSCTL_INT(_hw_usb_ugen, OID_AUTO, debug, CTLFLAG_RW,
75 &ugendebug, 0, "ugen debug level");
81 #define UGEN_CHUNK 128 /* chunk size for read */
82 #define UGEN_IBSIZE 1020 /* buffer size */
83 #define UGEN_BBSIZE 1024
85 #define UGEN_NISOFRAMES 500 /* 0.5 seconds worth */
86 #define UGEN_NISOREQS 6 /* number of outstanding xfer requests */
87 #define UGEN_NISORFRMS 4 /* number of frames (miliseconds) per req */
89 struct ugen_endpoint {
90 struct ugen_softc *sc;
91 usb_endpoint_descriptor_t *edesc;
92 usbd_interface_handle iface;
94 #define UGEN_ASLP 0x02 /* waiting for data */
95 #define UGEN_SHORT_OK 0x04 /* short xfers are OK */
96 usbd_pipe_handle pipeh;
99 u_char *ibuf; /* start of buffer (circular for isoc) */
100 u_char *fill; /* location for input (isoc) */
101 u_char *limit; /* end of circular buffer (isoc) */
102 u_char *cur; /* current read location (isoc) */
105 struct ugen_endpoint *sce;
106 usbd_xfer_handle xfer;
108 u_int16_t sizes[UGEN_NISORFRMS];
109 } isoreqs[UGEN_NISOREQS];
113 USBBASEDEVICE sc_dev; /* base device */
114 usbd_device_handle sc_udev;
116 char sc_is_open[USB_MAX_ENDPOINTS];
117 struct ugen_endpoint sc_endpoints[USB_MAX_ENDPOINTS][2];
125 #if defined(__NetBSD__) || defined(__OpenBSD__)
127 #elif defined(__FreeBSD__)
135 #define UGEN_CDEV_MAJOR 114
137 Static struct cdevsw ugen_cdevsw = {
139 /* close */ ugenclose,
141 /* write */ ugenwrite,
142 /* ioctl */ ugenioctl,
145 /* strategy */ nostrategy,
147 /* maj */ UGEN_CDEV_MAJOR,
155 Static void ugenintr(usbd_xfer_handle xfer, usbd_private_handle addr,
157 Static void ugen_isoc_rintr(usbd_xfer_handle xfer, usbd_private_handle addr,
159 Static int ugen_do_read(struct ugen_softc *, int, struct uio *, int);
160 Static int ugen_do_write(struct ugen_softc *, int, struct uio *, int);
161 Static int ugen_do_ioctl(struct ugen_softc *, int, u_long,
162 caddr_t, int, usb_proc_ptr);
163 Static int ugen_set_config(struct ugen_softc *sc, int configno);
164 Static usb_config_descriptor_t *ugen_get_cdesc(struct ugen_softc *sc,
165 int index, int *lenp);
166 Static usbd_status ugen_set_interface(struct ugen_softc *, int, int);
167 Static int ugen_get_alt_index(struct ugen_softc *sc, int ifaceidx);
169 #define UGENUNIT(n) ((minor(n) >> 4) & 0xf)
170 #define UGENENDPOINT(n) (minor(n) & 0xf)
171 #define UGENMINOR(u, e) (((u) << 4) | (e))
173 USB_DECLARE_DRIVER(ugen);
177 USB_MATCH_START(ugen, uaa);
180 return (UMATCH_GENERIC);
182 return (UMATCH_NONE);
187 USB_ATTACH_START(ugen, sc, uaa);
188 usbd_device_handle udev;
193 usbd_devinfo(uaa->device, 0, devinfo);
195 printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
197 sc->sc_udev = udev = uaa->device;
199 memset(sc->sc_endpoints, 0, sizeof sc->sc_endpoints);
201 /* First set configuration index 0, the default one for ugen. */
202 err = usbd_set_config_index(udev, 0, 0);
204 printf("%s: setting configuration index 0 failed\n",
205 USBDEVNAME(sc->sc_dev));
207 USB_ATTACH_ERROR_RETURN;
209 conf = usbd_get_config_descriptor(udev)->bConfigurationValue;
211 /* Set up all the local state for this configuration. */
212 err = ugen_set_config(sc, conf);
214 printf("%s: setting configuration %d failed\n",
215 USBDEVNAME(sc->sc_dev), conf);
217 USB_ATTACH_ERROR_RETURN;
220 USB_ATTACH_SUCCESS_RETURN;
224 ugen_set_config(struct ugen_softc *sc, int configno)
226 usbd_device_handle dev = sc->sc_udev;
227 usbd_interface_handle iface;
228 usb_endpoint_descriptor_t *ed;
229 struct ugen_endpoint *sce;
230 u_int8_t niface, nendpt;
231 int ifaceno, endptno, endpt;
235 DPRINTFN(1,("ugen_set_config: %s to configno %d, sc=%p\n",
236 USBDEVNAME(sc->sc_dev), configno, sc));
238 /* We start at 1, not 0, because we don't care whether the
239 * control endpoint is open or not. It is always present.
241 for (endptno = 1; endptno < USB_MAX_ENDPOINTS; endptno++)
242 if (sc->sc_is_open[endptno]) {
244 ("ugen_set_config: %s - endpoint %d is open\n",
245 USBDEVNAME(sc->sc_dev), endptno));
246 return (USBD_IN_USE);
249 if (usbd_get_config_descriptor(dev)->bConfigurationValue != configno) {
250 /* Avoid setting the current value. */
251 err = usbd_set_config_no(dev, configno, 0);
256 err = usbd_interface_count(dev, &niface);
259 memset(sc->sc_endpoints, 0, sizeof sc->sc_endpoints);
260 for (ifaceno = 0; ifaceno < niface; ifaceno++) {
261 DPRINTFN(1,("ugen_set_config: ifaceno %d\n", ifaceno));
262 err = usbd_device2interface_handle(dev, ifaceno, &iface);
265 err = usbd_endpoint_count(iface, &nendpt);
268 for (endptno = 0; endptno < nendpt; endptno++) {
269 ed = usbd_interface2endpoint_descriptor(iface,endptno);
270 endpt = UE_GET_ADDR(ed->bEndpointAddress);
271 dir = UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN?
274 sce = &sc->sc_endpoints[endpt][dir];
275 DPRINTFN(1,("ugen_set_config: endptno %d, endpt=0x%02x"
277 endptno, endpt, endpt, dir, sce));
286 #if defined(__FreeBSD__)
287 /* the main device, ctrl endpoint */
288 make_dev(&ugen_cdevsw, UGENMINOR(USBDEVUNIT(sc->sc_dev), 0),
289 UID_ROOT, GID_OPERATOR, 0644, "%s", USBDEVNAME(sc->sc_dev));
291 for (endptno = 1; endptno < USB_MAX_ENDPOINTS; endptno++) {
292 if (sc->sc_endpoints[endptno][IN].sc != NULL ||
293 sc->sc_endpoints[endptno][OUT].sc != NULL ) {
294 /* endpt can be 0x81 and 0x01, representing
295 * endpoint address 0x01 and IN/OUT directions.
296 * We map both endpts to the same device,
297 * IN is reading from it, OUT is writing to it.
299 * In the if clause above we check whether one
300 * of the structs is populated.
302 make_dev(&ugen_cdevsw,
303 UGENMINOR(USBDEVUNIT(sc->sc_dev), endptno),
304 UID_ROOT, GID_OPERATOR, 0644,
306 USBDEVNAME(sc->sc_dev), endptno);
311 return (USBD_NORMAL_COMPLETION);
315 ugenopen(dev_t dev, int flag, int mode, usb_proc_ptr p)
317 struct ugen_softc *sc;
318 int unit = UGENUNIT(dev);
319 int endpt = UGENENDPOINT(dev);
320 usb_endpoint_descriptor_t *edesc;
321 struct ugen_endpoint *sce;
324 usbd_xfer_handle xfer;
328 USB_GET_SC_OPEN(ugen, unit, sc);
330 DPRINTFN(5, ("ugenopen: flag=%d, mode=%d, unit=%d endpt=%d\n",
331 flag, mode, unit, endpt));
333 if (sc == NULL || sc->sc_dying)
336 if (sc->sc_is_open[endpt])
339 if (endpt == USB_CONTROL_ENDPOINT) {
340 sc->sc_is_open[USB_CONTROL_ENDPOINT] = 1;
344 /* Make sure there are pipes for all directions. */
345 for (dir = OUT; dir <= IN; dir++) {
346 if (flag & (dir == OUT ? FWRITE : FREAD)) {
347 sce = &sc->sc_endpoints[endpt][dir];
348 if (sce == 0 || sce->edesc == 0)
353 /* Actually open the pipes. */
354 /* XXX Should back out properly if it fails. */
355 for (dir = OUT; dir <= IN; dir++) {
356 if (!(flag & (dir == OUT ? FWRITE : FREAD)))
358 sce = &sc->sc_endpoints[endpt][dir];
360 sce->timeout = USBD_NO_TIMEOUT;
361 DPRINTFN(5, ("ugenopen: sc=%p, endpt=%d, dir=%d, sce=%p\n",
362 sc, endpt, dir, sce));
364 switch (edesc->bmAttributes & UE_XFERTYPE) {
366 isize = UGETW(edesc->wMaxPacketSize);
367 if (isize == 0) /* shouldn't happen */
369 sce->ibuf = malloc(isize, M_USBDEV, M_WAITOK);
370 DPRINTFN(5, ("ugenopen: intr endpt=%d,isize=%d\n",
372 if (clalloc(&sce->q, UGEN_IBSIZE, 0) == -1)
374 err = usbd_open_pipe_intr(sce->iface,
375 edesc->bEndpointAddress,
376 USBD_SHORT_XFER_OK, &sce->pipeh, sce,
377 sce->ibuf, isize, ugenintr,
378 USBD_DEFAULT_INTERVAL);
380 free(sce->ibuf, M_USBDEV);
384 DPRINTFN(5, ("ugenopen: interrupt open done\n"));
387 err = usbd_open_pipe(sce->iface,
388 edesc->bEndpointAddress, 0, &sce->pipeh);
395 isize = UGETW(edesc->wMaxPacketSize);
396 if (isize == 0) /* shouldn't happen */
398 sce->ibuf = malloc(isize * UGEN_NISOFRAMES,
400 sce->cur = sce->fill = sce->ibuf;
401 sce->limit = sce->ibuf + isize * UGEN_NISOFRAMES;
402 DPRINTFN(5, ("ugenopen: isoc endpt=%d, isize=%d\n",
404 err = usbd_open_pipe(sce->iface,
405 edesc->bEndpointAddress, 0, &sce->pipeh);
407 free(sce->ibuf, M_USBDEV);
410 for(i = 0; i < UGEN_NISOREQS; ++i) {
411 sce->isoreqs[i].sce = sce;
412 xfer = usbd_alloc_xfer(sc->sc_udev);
415 sce->isoreqs[i].xfer = xfer;
416 buf = usbd_alloc_buffer
417 (xfer, isize * UGEN_NISORFRMS);
422 sce->isoreqs[i].dmabuf = buf;
423 for(j = 0; j < UGEN_NISORFRMS; ++j)
424 sce->isoreqs[i].sizes[j] = isize;
426 (xfer, sce->pipeh, &sce->isoreqs[i],
427 sce->isoreqs[i].sizes,
428 UGEN_NISORFRMS, USBD_NO_COPY,
430 (void)usbd_transfer(xfer);
432 DPRINTFN(5, ("ugenopen: isoc open done\n"));
435 while (--i >= 0) /* implicit buffer free */
436 usbd_free_xfer(sce->isoreqs[i].xfer);
442 sc->sc_is_open[endpt] = 1;
447 ugenclose(dev_t dev, int flag, int mode, usb_proc_ptr p)
449 int endpt = UGENENDPOINT(dev);
450 struct ugen_softc *sc;
451 struct ugen_endpoint *sce;
455 USB_GET_SC(ugen, UGENUNIT(dev), sc);
457 DPRINTFN(5, ("ugenclose: flag=%d, mode=%d, unit=%d, endpt=%d\n",
458 flag, mode, UGENUNIT(dev), endpt));
461 if (!sc->sc_is_open[endpt]) {
462 printf("ugenclose: not open\n");
467 if (endpt == USB_CONTROL_ENDPOINT) {
468 DPRINTFN(5, ("ugenclose: close control\n"));
469 sc->sc_is_open[endpt] = 0;
473 for (dir = OUT; dir <= IN; dir++) {
474 if (!(flag & (dir == OUT ? FWRITE : FREAD)))
476 sce = &sc->sc_endpoints[endpt][dir];
477 if (sce == NULL || sce->pipeh == NULL)
479 DPRINTFN(5, ("ugenclose: endpt=%d dir=%d sce=%p\n",
482 usbd_abort_pipe(sce->pipeh);
483 usbd_close_pipe(sce->pipeh);
486 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
488 ndflush(&sce->q, sce->q.c_cc);
492 for (i = 0; i < UGEN_NISOREQS; ++i)
493 usbd_free_xfer(sce->isoreqs[i].xfer);
498 if (sce->ibuf != NULL) {
499 free(sce->ibuf, M_USBDEV);
503 sc->sc_is_open[endpt] = 0;
509 ugen_do_read(struct ugen_softc *sc, int endpt, struct uio *uio, int flag)
511 struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][IN];
513 char buf[UGEN_BBSIZE];
514 usbd_xfer_handle xfer;
518 u_char buffer[UGEN_CHUNK];
520 DPRINTFN(5, ("%s: ugenread: %d\n", USBDEVNAME(sc->sc_dev), endpt));
525 if (endpt == USB_CONTROL_ENDPOINT)
529 if (sce->edesc == NULL) {
530 printf("ugenread: no edesc\n");
533 if (sce->pipeh == NULL) {
534 printf("ugenread: no pipe\n");
539 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
541 /* Block until activity occured. */
543 while (sce->q.c_cc == 0) {
544 if (flag & IO_NDELAY) {
546 return (EWOULDBLOCK);
548 sce->state |= UGEN_ASLP;
549 DPRINTFN(5, ("ugenread: sleep on %p\n", sc));
550 error = tsleep(sce, PZERO | PCATCH, "ugenri", 0);
551 DPRINTFN(5, ("ugenread: woke, error=%d\n", error));
555 sce->state &= ~UGEN_ASLP;
561 /* Transfer as many chunks as possible. */
562 while (sce->q.c_cc > 0 && uio->uio_resid > 0 && !error) {
563 n = min(sce->q.c_cc, uio->uio_resid);
564 if (n > sizeof(buffer))
567 /* Remove a small chunk from the input queue. */
568 q_to_b(&sce->q, buffer, n);
569 DPRINTFN(5, ("ugenread: got %d chars\n", n));
571 /* Copy the data to the user process. */
572 error = uiomove(buffer, n, uio);
578 xfer = usbd_alloc_xfer(sc->sc_udev);
581 while ((n = min(UGEN_BBSIZE, uio->uio_resid)) != 0) {
582 DPRINTFN(1, ("ugenread: start transfer %d bytes\n",n));
584 err = usbd_bulk_transfer(
586 sce->state & UGEN_SHORT_OK ?
587 USBD_SHORT_XFER_OK : 0,
588 sce->timeout, buf, &tn, "ugenrb");
590 if (err == USBD_INTERRUPTED)
592 else if (err == USBD_TIMEOUT)
598 DPRINTFN(1, ("ugenread: got %d bytes\n", tn));
599 error = uiomove(buf, tn, uio);
603 usbd_free_xfer(xfer);
607 while (sce->cur == sce->fill) {
608 if (flag & IO_NDELAY) {
610 return (EWOULDBLOCK);
612 sce->state |= UGEN_ASLP;
613 DPRINTFN(5, ("ugenread: sleep on %p\n", sc));
614 error = tsleep(sce, PZERO | PCATCH, "ugenri", 0);
615 DPRINTFN(5, ("ugenread: woke, error=%d\n", error));
619 sce->state &= ~UGEN_ASLP;
624 while (sce->cur != sce->fill && uio->uio_resid > 0 && !error) {
625 if(sce->fill > sce->cur)
626 n = min(sce->fill - sce->cur, uio->uio_resid);
628 n = min(sce->limit - sce->cur, uio->uio_resid);
630 DPRINTFN(5, ("ugenread: isoc got %d chars\n", n));
632 /* Copy the data to the user process. */
633 error = uiomove(sce->cur, n, uio);
637 if(sce->cur >= sce->limit)
638 sce->cur = sce->ibuf;
651 ugenread(dev_t dev, struct uio *uio, int flag)
653 int endpt = UGENENDPOINT(dev);
654 struct ugen_softc *sc;
657 USB_GET_SC(ugen, UGENUNIT(dev), sc);
660 error = ugen_do_read(sc, endpt, uio, flag);
661 if (--sc->sc_refcnt < 0)
662 usb_detach_wakeup(USBDEV(sc->sc_dev));
667 ugen_do_write(struct ugen_softc *sc, int endpt, struct uio *uio, int flag)
669 struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][OUT];
672 char buf[UGEN_BBSIZE];
673 usbd_xfer_handle xfer;
676 DPRINTFN(5, ("%s: ugen_do_write: %d\n", USBDEVNAME(sc->sc_dev), endpt));
681 if (endpt == USB_CONTROL_ENDPOINT)
685 if (sce->edesc == NULL) {
686 printf("ugen_do_write: no edesc\n");
689 if (sce->pipeh == NULL) {
690 printf("ugen_do_write: no pipe\n");
695 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
697 xfer = usbd_alloc_xfer(sc->sc_udev);
700 while ((n = min(UGEN_BBSIZE, uio->uio_resid)) != 0) {
701 error = uiomove(buf, n, uio);
704 DPRINTFN(1, ("ugen_do_write: transfer %d bytes\n", n));
705 err = usbd_bulk_transfer(xfer, sce->pipeh, 0,
706 sce->timeout, buf, &n,"ugenwb");
708 if (err == USBD_INTERRUPTED)
715 usbd_free_xfer(xfer);
724 ugenwrite(dev_t dev, struct uio *uio, int flag)
726 int endpt = UGENENDPOINT(dev);
727 struct ugen_softc *sc;
730 USB_GET_SC(ugen, UGENUNIT(dev), sc);
733 error = ugen_do_write(sc, endpt, uio, flag);
734 if (--sc->sc_refcnt < 0)
735 usb_detach_wakeup(USBDEV(sc->sc_dev));
739 #if defined(__NetBSD__) || defined(__OpenBSD__)
741 ugen_activate(device_ptr_t self, enum devact act)
743 struct ugen_softc *sc = (struct ugen_softc *)self;
750 case DVACT_DEACTIVATE:
760 USB_DETACH_START(ugen, sc);
761 struct ugen_endpoint *sce;
764 #if defined(__NetBSD__) || defined(__OpenBSD__)
766 #elif defined(__FreeBSD__)
772 #if defined(__NetBSD__) || defined(__OpenBSD__)
773 DPRINTF(("ugen_detach: sc=%p flags=%d\n", sc, flags));
774 #elif defined(__FreeBSD__)
775 DPRINTF(("ugen_detach: sc=%p\n", sc));
779 /* Abort all pipes. Causes processes waiting for transfer to wake. */
780 for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
781 for (dir = OUT; dir <= IN; dir++) {
782 sce = &sc->sc_endpoints[i][dir];
783 if (sce && sce->pipeh)
784 usbd_abort_pipe(sce->pipeh);
789 if (--sc->sc_refcnt >= 0) {
791 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
792 wakeup(&sc->sc_endpoints[i][IN]);
793 /* Wait for processes to go away. */
794 usb_detach_wait(USBDEV(sc->sc_dev));
798 #if defined(__NetBSD__) || defined(__OpenBSD__)
799 /* locate the major number */
800 for (maj = 0; maj < nchrdev; maj++)
801 if (cdevsw[maj].d_open == ugenopen)
804 /* Nuke the vnodes for any open instances (calls close). */
805 mn = self->dv_unit * USB_MAX_ENDPOINTS;
806 vdevgone(maj, mn, mn + USB_MAX_ENDPOINTS - 1, VCHR);
807 #elif defined(__FreeBSD__)
808 /* destroy the device for the control endpoint */
809 dev = makedev(UGEN_CDEV_MAJOR, UGENMINOR(USBDEVUNIT(sc->sc_dev), 0));
810 vp = SLIST_FIRST(&dev->si_hlist);
812 VOP_REVOKE(vp, REVOKEALL);
815 /* destroy all devices for the other (existing) endpoints as well */
816 for (endptno = 1; endptno < USB_MAX_ENDPOINTS; endptno++) {
817 if (sc->sc_endpoints[endptno][IN].sc != NULL ||
818 sc->sc_endpoints[endptno][OUT].sc != NULL ) {
819 /* endpt can be 0x81 and 0x01, representing
820 * endpoint address 0x01 and IN/OUT directions.
821 * We map both endpoint addresses to the same device,
822 * IN is reading from it, OUT is writing to it.
824 * In the if clause above we check whether one
825 * of the structs is populated.
827 dev = makedev(UGEN_CDEV_MAJOR,
828 UGENMINOR(USBDEVUNIT(sc->sc_dev), endptno));
829 vp = SLIST_FIRST(&dev->si_hlist);
831 VOP_REVOKE(vp, REVOKEALL);
842 ugenintr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
844 struct ugen_endpoint *sce = addr;
845 /*struct ugen_softc *sc = sce->sc;*/
849 if (status == USBD_CANCELLED)
852 if (status != USBD_NORMAL_COMPLETION) {
853 DPRINTF(("ugenintr: status=%d\n", status));
854 usbd_clear_endpoint_stall_async(sce->pipeh);
858 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
861 DPRINTFN(5, ("ugenintr: xfer=%p status=%d count=%d\n",
862 xfer, status, count));
863 DPRINTFN(5, (" data = %02x %02x %02x\n",
864 ibuf[0], ibuf[1], ibuf[2]));
866 (void)b_to_q(ibuf, count, &sce->q);
868 if (sce->state & UGEN_ASLP) {
869 sce->state &= ~UGEN_ASLP;
870 DPRINTFN(5, ("ugen_intr: waking %p\n", sce));
873 selwakeup(&sce->rsel);
877 ugen_isoc_rintr(usbd_xfer_handle xfer, usbd_private_handle addr,
880 struct isoreq *req = addr;
881 struct ugen_endpoint *sce = req->sce;
884 /* Return if we are aborting. */
885 if (status == USBD_CANCELLED)
888 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
889 DPRINTFN(5,("ugen_isoc_rintr: xfer %d, count=%d\n", req - sce->isoreqs,
892 /* throw away oldest input if the buffer is full */
893 if(sce->fill < sce->cur && sce->cur <= sce->fill + count) {
895 if(sce->cur >= sce->limit)
896 sce->cur = sce->ibuf + (sce->limit - sce->cur);
897 DPRINTFN(5, ("ugen_isoc_rintr: throwing away %d bytes\n",
901 /* copy data to buffer */
903 n = min(count, sce->limit - sce->fill);
904 memcpy(sce->fill, req->dmabuf, n);
908 if(sce->fill == sce->limit)
909 sce->fill = sce->ibuf;
912 usbd_setup_isoc_xfer(xfer, sce->pipeh, req, req->sizes, UGEN_NISORFRMS,
913 USBD_NO_COPY, ugen_isoc_rintr);
914 (void)usbd_transfer(xfer);
916 if (sce->state & UGEN_ASLP) {
917 sce->state &= ~UGEN_ASLP;
918 DPRINTFN(5, ("ugen_isoc_rintr: waking %p\n", sce));
921 selwakeup(&sce->rsel);
925 ugen_set_interface(struct ugen_softc *sc, int ifaceidx, int altno)
927 usbd_interface_handle iface;
928 usb_endpoint_descriptor_t *ed;
930 struct ugen_endpoint *sce;
931 u_int8_t niface, nendpt, endptno, endpt;
934 DPRINTFN(15, ("ugen_set_interface %d %d\n", ifaceidx, altno));
936 err = usbd_interface_count(sc->sc_udev, &niface);
939 if (ifaceidx < 0 || ifaceidx >= niface)
942 err = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface);
945 err = usbd_endpoint_count(iface, &nendpt);
948 for (endptno = 0; endptno < nendpt; endptno++) {
949 ed = usbd_interface2endpoint_descriptor(iface,endptno);
950 endpt = ed->bEndpointAddress;
951 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
952 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
959 err = usbd_set_interface(iface, altno);
963 err = usbd_endpoint_count(iface, &nendpt);
966 for (endptno = 0; endptno < nendpt; endptno++) {
967 ed = usbd_interface2endpoint_descriptor(iface,endptno);
968 endpt = ed->bEndpointAddress;
969 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
970 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
978 /* Retrieve a complete descriptor for a certain device and index. */
979 Static usb_config_descriptor_t *
980 ugen_get_cdesc(struct ugen_softc *sc, int index, int *lenp)
982 usb_config_descriptor_t *cdesc, *tdesc, cdescr;
986 if (index == USB_CURRENT_CONFIG_INDEX) {
987 tdesc = usbd_get_config_descriptor(sc->sc_udev);
988 len = UGETW(tdesc->wTotalLength);
991 cdesc = malloc(len, M_TEMP, M_WAITOK);
992 memcpy(cdesc, tdesc, len);
993 DPRINTFN(5,("ugen_get_cdesc: current, len=%d\n", len));
995 err = usbd_get_config_desc(sc->sc_udev, index, &cdescr);
998 len = UGETW(cdescr.wTotalLength);
999 DPRINTFN(5,("ugen_get_cdesc: index=%d, len=%d\n", index, len));
1002 cdesc = malloc(len, M_TEMP, M_WAITOK);
1003 err = usbd_get_config_desc_full(sc->sc_udev, index, cdesc, len);
1005 free(cdesc, M_TEMP);
1013 ugen_get_alt_index(struct ugen_softc *sc, int ifaceidx)
1015 usbd_interface_handle iface;
1018 err = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface);
1021 return (usbd_get_interface_altindex(iface));
1025 ugen_do_ioctl(struct ugen_softc *sc, int endpt, u_long cmd,
1026 caddr_t addr, int flag, usb_proc_ptr p)
1028 struct ugen_endpoint *sce;
1030 usbd_interface_handle iface;
1031 struct usb_config_desc *cd;
1032 usb_config_descriptor_t *cdesc;
1033 struct usb_interface_desc *id;
1034 usb_interface_descriptor_t *idesc;
1035 struct usb_endpoint_desc *ed;
1036 usb_endpoint_descriptor_t *edesc;
1037 struct usb_alt_interface *ai;
1038 struct usb_string_desc *si;
1041 DPRINTFN(5, ("ugenioctl: cmd=%08lx\n", cmd));
1047 /* All handled in the upper FS layer. */
1049 case USB_SET_SHORT_XFER:
1050 /* This flag only affects read */
1051 if (endpt == USB_CONTROL_ENDPOINT)
1053 sce = &sc->sc_endpoints[endpt][IN];
1057 if (sce->pipeh == NULL) {
1058 printf("ugenioctl: USB_SET_SHORT_XFER, no pipe\n");
1063 sce->state |= UGEN_SHORT_OK;
1065 sce->state &= ~UGEN_SHORT_OK;
1067 case USB_SET_TIMEOUT:
1068 sce = &sc->sc_endpoints[endpt][IN];
1072 if (sce->pipeh == NULL) {
1073 printf("ugenioctl: USB_SET_TIMEOUT, no pipe\n");
1077 sce->timeout = *(int *)addr;
1083 if (endpt != USB_CONTROL_ENDPOINT)
1089 ugendebug = *(int *)addr;
1092 case USB_GET_CONFIG:
1093 err = usbd_get_config(sc->sc_udev, &conf);
1096 *(int *)addr = conf;
1098 case USB_SET_CONFIG:
1099 if (!(flag & FWRITE))
1101 err = ugen_set_config(sc, *(int *)addr);
1103 case USBD_NORMAL_COMPLETION:
1111 case USB_GET_ALTINTERFACE:
1112 ai = (struct usb_alt_interface *)addr;
1113 err = usbd_device2interface_handle(sc->sc_udev,
1114 ai->uai_interface_index, &iface);
1117 idesc = usbd_get_interface_descriptor(iface);
1120 ai->uai_alt_no = idesc->bAlternateSetting;
1122 case USB_SET_ALTINTERFACE:
1123 if (!(flag & FWRITE))
1125 ai = (struct usb_alt_interface *)addr;
1126 err = usbd_device2interface_handle(sc->sc_udev,
1127 ai->uai_interface_index, &iface);
1130 err = ugen_set_interface(sc, ai->uai_interface_index, ai->uai_alt_no);
1134 case USB_GET_NO_ALT:
1135 ai = (struct usb_alt_interface *)addr;
1136 cdesc = ugen_get_cdesc(sc, ai->uai_config_index, 0);
1139 idesc = usbd_find_idesc(cdesc, ai->uai_interface_index, 0);
1140 if (idesc == NULL) {
1141 free(cdesc, M_TEMP);
1144 ai->uai_alt_no = usbd_get_no_alts(cdesc, idesc->bInterfaceNumber);
1145 free(cdesc, M_TEMP);
1147 case USB_GET_DEVICE_DESC:
1148 *(usb_device_descriptor_t *)addr =
1149 *usbd_get_device_descriptor(sc->sc_udev);
1151 case USB_GET_CONFIG_DESC:
1152 cd = (struct usb_config_desc *)addr;
1153 cdesc = ugen_get_cdesc(sc, cd->ucd_config_index, 0);
1156 cd->ucd_desc = *cdesc;
1157 free(cdesc, M_TEMP);
1159 case USB_GET_INTERFACE_DESC:
1160 id = (struct usb_interface_desc *)addr;
1161 cdesc = ugen_get_cdesc(sc, id->uid_config_index, 0);
1164 if (id->uid_config_index == USB_CURRENT_CONFIG_INDEX &&
1165 id->uid_alt_index == USB_CURRENT_ALT_INDEX)
1166 alt = ugen_get_alt_index(sc, id->uid_interface_index);
1168 alt = id->uid_alt_index;
1169 idesc = usbd_find_idesc(cdesc, id->uid_interface_index, alt);
1170 if (idesc == NULL) {
1171 free(cdesc, M_TEMP);
1174 id->uid_desc = *idesc;
1175 free(cdesc, M_TEMP);
1177 case USB_GET_ENDPOINT_DESC:
1178 ed = (struct usb_endpoint_desc *)addr;
1179 cdesc = ugen_get_cdesc(sc, ed->ued_config_index, 0);
1182 if (ed->ued_config_index == USB_CURRENT_CONFIG_INDEX &&
1183 ed->ued_alt_index == USB_CURRENT_ALT_INDEX)
1184 alt = ugen_get_alt_index(sc, ed->ued_interface_index);
1186 alt = ed->ued_alt_index;
1187 edesc = usbd_find_edesc(cdesc, ed->ued_interface_index,
1188 alt, ed->ued_endpoint_index);
1189 if (edesc == NULL) {
1190 free(cdesc, M_TEMP);
1193 ed->ued_desc = *edesc;
1194 free(cdesc, M_TEMP);
1196 case USB_GET_FULL_DESC:
1201 struct usb_full_desc *fd = (struct usb_full_desc *)addr;
1204 cdesc = ugen_get_cdesc(sc, fd->ufd_config_index, &len);
1205 if (len > fd->ufd_size)
1207 iov.iov_base = (caddr_t)fd->ufd_data;
1211 uio.uio_resid = len;
1213 uio.uio_segflg = UIO_USERSPACE;
1214 uio.uio_rw = UIO_READ;
1216 #if defined(__NetBSD__) || defined(__OpenBSD__)
1217 error = uiomove((caddr_t)cdesc, len, &uio);
1218 #elif defined(__FreeBSD__)
1219 error = uiomove((void *)cdesc, len, &uio);
1221 free(cdesc, M_TEMP);
1224 case USB_GET_STRING_DESC:
1225 si = (struct usb_string_desc *)addr;
1226 err = usbd_get_string_desc(sc->sc_udev, si->usd_string_index,
1227 si->usd_language_id, &si->usd_desc);
1231 case USB_DO_REQUEST:
1233 struct usb_ctl_request *ur = (void *)addr;
1234 int len = UGETW(ur->ucr_request.wLength);
1241 if (!(flag & FWRITE))
1243 /* Avoid requests that would damage the bus integrity. */
1244 if ((ur->ucr_request.bmRequestType == UT_WRITE_DEVICE &&
1245 ur->ucr_request.bRequest == UR_SET_ADDRESS) ||
1246 (ur->ucr_request.bmRequestType == UT_WRITE_DEVICE &&
1247 ur->ucr_request.bRequest == UR_SET_CONFIG) ||
1248 (ur->ucr_request.bmRequestType == UT_WRITE_INTERFACE &&
1249 ur->ucr_request.bRequest == UR_SET_INTERFACE))
1252 if (len < 0 || len > 32767)
1255 iov.iov_base = (caddr_t)ur->ucr_data;
1259 uio.uio_resid = len;
1261 uio.uio_segflg = UIO_USERSPACE;
1263 ur->ucr_request.bmRequestType & UT_READ ?
1264 UIO_READ : UIO_WRITE;
1266 ptr = malloc(len, M_TEMP, M_WAITOK);
1267 if (uio.uio_rw == UIO_WRITE) {
1268 error = uiomove(ptr, len, &uio);
1273 err = usbd_do_request_flags(sc->sc_udev, &ur->ucr_request,
1274 ptr, ur->ucr_flags, &ur->ucr_actlen);
1280 if (uio.uio_rw == UIO_READ) {
1281 error = uiomove(ptr, len, &uio);
1291 case USB_GET_DEVICEINFO:
1292 usbd_fill_deviceinfo(sc->sc_udev,
1293 (struct usb_device_info *)addr);
1302 ugenioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, usb_proc_ptr p)
1304 int endpt = UGENENDPOINT(dev);
1305 struct ugen_softc *sc;
1308 USB_GET_SC(ugen, UGENUNIT(dev), sc);
1311 error = ugen_do_ioctl(sc, endpt, cmd, addr, flag, p);
1312 if (--sc->sc_refcnt < 0)
1313 usb_detach_wakeup(USBDEV(sc->sc_dev));
1318 ugenpoll(dev_t dev, int events, usb_proc_ptr p)
1320 struct ugen_softc *sc;
1321 struct ugen_endpoint *sce;
1325 USB_GET_SC(ugen, UGENUNIT(dev), sc);
1331 sce = &sc->sc_endpoints[UGENENDPOINT(dev)][IN];
1336 printf("ugenpoll: no edesc\n");
1340 printf("ugenpoll: no pipe\n");
1345 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
1347 if (events & (POLLIN | POLLRDNORM)) {
1348 if (sce->q.c_cc > 0)
1349 revents |= events & (POLLIN | POLLRDNORM);
1351 selrecord(p, &sce->rsel);
1354 case UE_ISOCHRONOUS:
1355 if (events & (POLLIN | POLLRDNORM)) {
1356 if (sce->cur != sce->fill)
1357 revents |= events & (POLLIN | POLLRDNORM);
1359 selrecord(p, &sce->rsel);
1364 * We have no easy way of determining if a read will
1365 * yield any data or a write will happen.
1366 * Pretend they will.
1369 (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM);
1378 #if defined(__FreeBSD__)
1379 DRIVER_MODULE(ugen, uhub, ugen_driver, ugen_devclass, usbd_driver_load, 0);