1 /* $NetBSD: ugen.c,v 1.57 2002/02/11 15:11:49 augustss 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 #if __FreeBSD_version >= 500014
60 #include <sys/selinfo.h>
62 #include <sys/select.h>
64 #include <sys/vnode.h>
66 #include <sys/sysctl.h>
68 #include <dev/usb/usb.h>
69 #include <dev/usb/usbdi.h>
70 #include <dev/usb/usbdi_util.h>
73 #define DPRINTF(x) if (ugendebug) logprintf x
74 #define DPRINTFN(n,x) if (ugendebug>(n)) logprintf x
76 SYSCTL_NODE(_hw_usb, OID_AUTO, ugen, CTLFLAG_RW, 0, "USB ugen");
77 SYSCTL_INT(_hw_usb_ugen, OID_AUTO, debug, CTLFLAG_RW,
78 &ugendebug, 0, "ugen debug level");
84 #define UGEN_CHUNK 128 /* chunk size for read */
85 #define UGEN_IBSIZE 1020 /* buffer size */
86 #define UGEN_BBSIZE 1024
88 #define UGEN_NISOFRAMES 500 /* 0.5 seconds worth */
89 #define UGEN_NISOREQS 6 /* number of outstanding xfer requests */
90 #define UGEN_NISORFRMS 4 /* number of frames (miliseconds) per req */
92 struct ugen_endpoint {
93 struct ugen_softc *sc;
94 usb_endpoint_descriptor_t *edesc;
95 usbd_interface_handle iface;
97 #define UGEN_ASLP 0x02 /* waiting for data */
98 #define UGEN_SHORT_OK 0x04 /* short xfers are OK */
99 usbd_pipe_handle pipeh;
102 u_char *ibuf; /* start of buffer (circular for isoc) */
103 u_char *fill; /* location for input (isoc) */
104 u_char *limit; /* end of circular buffer (isoc) */
105 u_char *cur; /* current read location (isoc) */
108 struct ugen_endpoint *sce;
109 usbd_xfer_handle xfer;
111 u_int16_t sizes[UGEN_NISORFRMS];
112 } isoreqs[UGEN_NISOREQS];
116 USBBASEDEVICE sc_dev; /* base device */
117 usbd_device_handle sc_udev;
119 char sc_is_open[USB_MAX_ENDPOINTS];
120 struct ugen_endpoint sc_endpoints[USB_MAX_ENDPOINTS][2];
128 #if defined(__NetBSD__) || defined(__OpenBSD__)
130 #elif defined(__FreeBSD__)
138 #define UGEN_CDEV_MAJOR 114
140 Static struct cdevsw ugen_cdevsw = {
142 /* close */ ugenclose,
144 /* write */ ugenwrite,
145 /* ioctl */ ugenioctl,
148 /* strategy */ nostrategy,
150 /* maj */ UGEN_CDEV_MAJOR,
154 #if __FreeBSD_version < 500014
160 Static void ugenintr(usbd_xfer_handle xfer, usbd_private_handle addr,
162 Static void ugen_isoc_rintr(usbd_xfer_handle xfer, usbd_private_handle addr,
164 Static int ugen_do_read(struct ugen_softc *, int, struct uio *, int);
165 Static int ugen_do_write(struct ugen_softc *, int, struct uio *, int);
166 Static int ugen_do_ioctl(struct ugen_softc *, int, u_long,
167 caddr_t, int, usb_proc_ptr);
168 #if defined(__FreeBSD__)
169 Static void ugen_make_devnodes(struct ugen_softc *sc);
170 Static void ugen_destroy_devnodes(struct ugen_softc *sc);
172 Static int ugen_set_config(struct ugen_softc *sc, int configno);
173 Static usb_config_descriptor_t *ugen_get_cdesc(struct ugen_softc *sc,
174 int index, int *lenp);
175 Static usbd_status ugen_set_interface(struct ugen_softc *, int, int);
176 Static int ugen_get_alt_index(struct ugen_softc *sc, int ifaceidx);
178 #define UGENUNIT(n) ((minor(n) >> 4) & 0xf)
179 #define UGENENDPOINT(n) (minor(n) & 0xf)
180 #define UGENMINOR(u, e) (((u) << 4) | (e))
182 USB_DECLARE_DRIVER(ugen);
186 USB_MATCH_START(ugen, uaa);
190 return (uaa->matchlvl);
193 return (UMATCH_GENERIC);
195 return (UMATCH_NONE);
200 USB_ATTACH_START(ugen, sc, uaa);
201 usbd_device_handle udev;
206 usbd_devinfo(uaa->device, 0, devinfo);
208 printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
210 sc->sc_udev = udev = uaa->device;
212 memset(sc->sc_endpoints, 0, sizeof sc->sc_endpoints);
214 /* First set configuration index 0, the default one for ugen. */
215 err = usbd_set_config_index(udev, 0, 0);
217 printf("%s: setting configuration index 0 failed\n",
218 USBDEVNAME(sc->sc_dev));
220 USB_ATTACH_ERROR_RETURN;
222 conf = usbd_get_config_descriptor(udev)->bConfigurationValue;
224 /* Set up all the local state for this configuration. */
225 err = ugen_set_config(sc, conf);
227 printf("%s: setting configuration %d failed\n",
228 USBDEVNAME(sc->sc_dev), conf);
230 USB_ATTACH_ERROR_RETURN;
233 #if defined(__FreeBSD__)
234 /* the main device, ctrl endpoint */
235 make_dev(&ugen_cdevsw, UGENMINOR(USBDEVUNIT(sc->sc_dev), 0),
236 UID_ROOT, GID_OPERATOR, 0644, "%s", USBDEVNAME(sc->sc_dev));
239 USB_ATTACH_SUCCESS_RETURN;
242 #if defined(__FreeBSD__)
244 ugen_make_devnodes(struct ugen_softc *sc)
248 for (endptno = 1; endptno < USB_MAX_ENDPOINTS; endptno++) {
249 if (sc->sc_endpoints[endptno][IN].sc != NULL ||
250 sc->sc_endpoints[endptno][OUT].sc != NULL ) {
251 /* endpt can be 0x81 and 0x01, representing
252 * endpoint address 0x01 and IN/OUT directions.
253 * We map both endpts to the same device,
254 * IN is reading from it, OUT is writing to it.
256 * In the if clause above we check whether one
257 * of the structs is populated.
259 make_dev(&ugen_cdevsw,
260 UGENMINOR(USBDEVUNIT(sc->sc_dev), endptno),
261 UID_ROOT, GID_OPERATOR, 0644,
263 USBDEVNAME(sc->sc_dev), endptno);
269 ugen_destroy_devnodes(struct ugen_softc *sc)
275 /* destroy all devices for the other (existing) endpoints as well */
276 for (endptno = 1; endptno < USB_MAX_ENDPOINTS; endptno++) {
277 if (sc->sc_endpoints[endptno][IN].sc != NULL ||
278 sc->sc_endpoints[endptno][OUT].sc != NULL ) {
279 /* endpt can be 0x81 and 0x01, representing
280 * endpoint address 0x01 and IN/OUT directions.
281 * We map both endpoint addresses to the same device,
282 * IN is reading from it, OUT is writing to it.
284 * In the if clause above we check whether one
285 * of the structs is populated.
287 dev = makedev(UGEN_CDEV_MAJOR,
288 UGENMINOR(USBDEVUNIT(sc->sc_dev), endptno));
289 vp = SLIST_FIRST(&dev->si_hlist);
291 VOP_REVOKE(vp, REVOKEALL);
300 ugen_set_config(struct ugen_softc *sc, int configno)
302 usbd_device_handle dev = sc->sc_udev;
303 usbd_interface_handle iface;
304 usb_endpoint_descriptor_t *ed;
305 struct ugen_endpoint *sce;
306 u_int8_t niface, nendpt;
307 int ifaceno, endptno, endpt;
311 DPRINTFN(1,("ugen_set_config: %s to configno %d, sc=%p\n",
312 USBDEVNAME(sc->sc_dev), configno, sc));
314 #if defined(__FreeBSD__)
315 ugen_destroy_devnodes(sc);
318 /* We start at 1, not 0, because we don't care whether the
319 * control endpoint is open or not. It is always present.
321 for (endptno = 1; endptno < USB_MAX_ENDPOINTS; endptno++)
322 if (sc->sc_is_open[endptno]) {
324 ("ugen_set_config: %s - endpoint %d is open\n",
325 USBDEVNAME(sc->sc_dev), endptno));
326 return (USBD_IN_USE);
329 /* Avoid setting the current value. */
330 if (usbd_get_config_descriptor(dev)->bConfigurationValue != configno) {
331 err = usbd_set_config_no(dev, configno, 1);
336 err = usbd_interface_count(dev, &niface);
339 memset(sc->sc_endpoints, 0, sizeof sc->sc_endpoints);
340 for (ifaceno = 0; ifaceno < niface; ifaceno++) {
341 DPRINTFN(1,("ugen_set_config: ifaceno %d\n", ifaceno));
342 err = usbd_device2interface_handle(dev, ifaceno, &iface);
345 err = usbd_endpoint_count(iface, &nendpt);
348 for (endptno = 0; endptno < nendpt; endptno++) {
349 ed = usbd_interface2endpoint_descriptor(iface,endptno);
350 endpt = ed->bEndpointAddress;
351 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
352 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
353 DPRINTFN(1,("ugen_set_config: endptno %d, endpt=0x%02x"
355 endptno, endpt, UE_GET_ADDR(endpt),
356 UE_GET_DIR(endpt), sce));
363 #if defined(__FreeBSD__)
364 ugen_make_devnodes(sc);
367 return (USBD_NORMAL_COMPLETION);
371 ugenopen(dev_t dev, int flag, int mode, usb_proc_ptr p)
373 struct ugen_softc *sc;
374 int unit = UGENUNIT(dev);
375 int endpt = UGENENDPOINT(dev);
376 usb_endpoint_descriptor_t *edesc;
377 struct ugen_endpoint *sce;
380 usbd_xfer_handle xfer;
384 USB_GET_SC_OPEN(ugen, unit, sc);
386 DPRINTFN(5, ("ugenopen: flag=%d, mode=%d, unit=%d endpt=%d\n",
387 flag, mode, unit, endpt));
389 if (sc == NULL || sc->sc_dying)
392 if (sc->sc_is_open[endpt])
395 if (endpt == USB_CONTROL_ENDPOINT) {
396 sc->sc_is_open[USB_CONTROL_ENDPOINT] = 1;
400 /* Make sure there are pipes for all directions. */
401 for (dir = OUT; dir <= IN; dir++) {
402 if (flag & (dir == OUT ? FWRITE : FREAD)) {
403 sce = &sc->sc_endpoints[endpt][dir];
404 if (sce == 0 || sce->edesc == 0)
409 /* Actually open the pipes. */
410 /* XXX Should back out properly if it fails. */
411 for (dir = OUT; dir <= IN; dir++) {
412 if (!(flag & (dir == OUT ? FWRITE : FREAD)))
414 sce = &sc->sc_endpoints[endpt][dir];
416 sce->timeout = USBD_NO_TIMEOUT;
417 DPRINTFN(5, ("ugenopen: sc=%p, endpt=%d, dir=%d, sce=%p\n",
418 sc, endpt, dir, sce));
420 switch (edesc->bmAttributes & UE_XFERTYPE) {
422 isize = UGETW(edesc->wMaxPacketSize);
423 if (isize == 0) /* shouldn't happen */
425 sce->ibuf = malloc(isize, M_USBDEV, M_WAITOK);
426 DPRINTFN(5, ("ugenopen: intr endpt=%d,isize=%d\n",
428 if (clalloc(&sce->q, UGEN_IBSIZE, 0) == -1)
430 err = usbd_open_pipe_intr(sce->iface,
431 edesc->bEndpointAddress,
432 USBD_SHORT_XFER_OK, &sce->pipeh, sce,
433 sce->ibuf, isize, ugenintr,
434 USBD_DEFAULT_INTERVAL);
436 free(sce->ibuf, M_USBDEV);
440 DPRINTFN(5, ("ugenopen: interrupt open done\n"));
443 err = usbd_open_pipe(sce->iface,
444 edesc->bEndpointAddress, 0, &sce->pipeh);
451 isize = UGETW(edesc->wMaxPacketSize);
452 if (isize == 0) /* shouldn't happen */
454 sce->ibuf = malloc(isize * UGEN_NISOFRAMES,
456 sce->cur = sce->fill = sce->ibuf;
457 sce->limit = sce->ibuf + isize * UGEN_NISOFRAMES;
458 DPRINTFN(5, ("ugenopen: isoc endpt=%d, isize=%d\n",
460 err = usbd_open_pipe(sce->iface,
461 edesc->bEndpointAddress, 0, &sce->pipeh);
463 free(sce->ibuf, M_USBDEV);
466 for(i = 0; i < UGEN_NISOREQS; ++i) {
467 sce->isoreqs[i].sce = sce;
468 xfer = usbd_alloc_xfer(sc->sc_udev);
471 sce->isoreqs[i].xfer = xfer;
472 buf = usbd_alloc_buffer
473 (xfer, isize * UGEN_NISORFRMS);
478 sce->isoreqs[i].dmabuf = buf;
479 for(j = 0; j < UGEN_NISORFRMS; ++j)
480 sce->isoreqs[i].sizes[j] = isize;
482 (xfer, sce->pipeh, &sce->isoreqs[i],
483 sce->isoreqs[i].sizes,
484 UGEN_NISORFRMS, USBD_NO_COPY,
486 (void)usbd_transfer(xfer);
488 DPRINTFN(5, ("ugenopen: isoc open done\n"));
491 while (--i >= 0) /* implicit buffer free */
492 usbd_free_xfer(sce->isoreqs[i].xfer);
495 sce->timeout = USBD_DEFAULT_TIMEOUT;
499 sc->sc_is_open[endpt] = 1;
504 ugenclose(dev_t dev, int flag, int mode, usb_proc_ptr p)
506 int endpt = UGENENDPOINT(dev);
507 struct ugen_softc *sc;
508 struct ugen_endpoint *sce;
512 USB_GET_SC(ugen, UGENUNIT(dev), sc);
514 DPRINTFN(5, ("ugenclose: flag=%d, mode=%d, unit=%d, endpt=%d\n",
515 flag, mode, UGENUNIT(dev), endpt));
518 if (!sc->sc_is_open[endpt]) {
519 printf("ugenclose: not open\n");
524 if (endpt == USB_CONTROL_ENDPOINT) {
525 DPRINTFN(5, ("ugenclose: close control\n"));
526 sc->sc_is_open[endpt] = 0;
530 for (dir = OUT; dir <= IN; dir++) {
531 if (!(flag & (dir == OUT ? FWRITE : FREAD)))
533 sce = &sc->sc_endpoints[endpt][dir];
534 if (sce == NULL || sce->pipeh == NULL)
536 DPRINTFN(5, ("ugenclose: endpt=%d dir=%d sce=%p\n",
539 usbd_abort_pipe(sce->pipeh);
540 usbd_close_pipe(sce->pipeh);
543 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
545 ndflush(&sce->q, sce->q.c_cc);
549 for (i = 0; i < UGEN_NISOREQS; ++i)
550 usbd_free_xfer(sce->isoreqs[i].xfer);
555 if (sce->ibuf != NULL) {
556 free(sce->ibuf, M_USBDEV);
561 sc->sc_is_open[endpt] = 0;
567 ugen_do_read(struct ugen_softc *sc, int endpt, struct uio *uio, int flag)
569 struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][IN];
571 char buf[UGEN_BBSIZE];
572 usbd_xfer_handle xfer;
576 u_char buffer[UGEN_CHUNK];
578 DPRINTFN(5, ("%s: ugenread: %d\n", USBDEVNAME(sc->sc_dev), endpt));
583 if (endpt == USB_CONTROL_ENDPOINT)
589 if (sce->edesc == NULL) {
590 printf("ugenread: no edesc\n");
593 if (sce->pipeh == NULL) {
594 printf("ugenread: no pipe\n");
598 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
600 /* Block until activity occurred. */
602 while (sce->q.c_cc == 0) {
603 if (flag & IO_NDELAY) {
605 return (EWOULDBLOCK);
607 sce->state |= UGEN_ASLP;
608 DPRINTFN(5, ("ugenread: sleep on %p\n", sce));
609 error = tsleep(sce, PZERO | PCATCH, "ugenri", 0);
610 DPRINTFN(5, ("ugenread: woke, error=%d\n", error));
614 sce->state &= ~UGEN_ASLP;
620 /* Transfer as many chunks as possible. */
621 while (sce->q.c_cc > 0 && uio->uio_resid > 0 && !error) {
622 n = min(sce->q.c_cc, uio->uio_resid);
623 if (n > sizeof(buffer))
626 /* Remove a small chunk from the input queue. */
627 q_to_b(&sce->q, buffer, n);
628 DPRINTFN(5, ("ugenread: got %d chars\n", n));
630 /* Copy the data to the user process. */
631 error = uiomove(buffer, n, uio);
637 xfer = usbd_alloc_xfer(sc->sc_udev);
640 while ((n = min(UGEN_BBSIZE, uio->uio_resid)) != 0) {
641 DPRINTFN(1, ("ugenread: start transfer %d bytes\n",n));
643 err = usbd_bulk_transfer(
645 sce->state & UGEN_SHORT_OK ?
646 USBD_SHORT_XFER_OK : 0,
647 sce->timeout, buf, &tn, "ugenrb");
649 if (err == USBD_INTERRUPTED)
651 else if (err == USBD_TIMEOUT)
657 DPRINTFN(1, ("ugenread: got %d bytes\n", tn));
658 error = uiomove(buf, tn, uio);
662 usbd_free_xfer(xfer);
666 while (sce->cur == sce->fill) {
667 if (flag & IO_NDELAY) {
669 return (EWOULDBLOCK);
671 sce->state |= UGEN_ASLP;
672 DPRINTFN(5, ("ugenread: sleep on %p\n", sce));
673 error = tsleep(sce, PZERO | PCATCH, "ugenri", 0);
674 DPRINTFN(5, ("ugenread: woke, error=%d\n", error));
678 sce->state &= ~UGEN_ASLP;
683 while (sce->cur != sce->fill && uio->uio_resid > 0 && !error) {
684 if(sce->fill > sce->cur)
685 n = min(sce->fill - sce->cur, uio->uio_resid);
687 n = min(sce->limit - sce->cur, uio->uio_resid);
689 DPRINTFN(5, ("ugenread: isoc got %d chars\n", n));
691 /* Copy the data to the user process. */
692 error = uiomove(sce->cur, n, uio);
696 if(sce->cur >= sce->limit)
697 sce->cur = sce->ibuf;
710 ugenread(dev_t dev, struct uio *uio, int flag)
712 int endpt = UGENENDPOINT(dev);
713 struct ugen_softc *sc;
716 USB_GET_SC(ugen, UGENUNIT(dev), sc);
719 error = ugen_do_read(sc, endpt, uio, flag);
720 if (--sc->sc_refcnt < 0)
721 usb_detach_wakeup(USBDEV(sc->sc_dev));
726 ugen_do_write(struct ugen_softc *sc, int endpt, struct uio *uio, int flag)
728 struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][OUT];
731 char buf[UGEN_BBSIZE];
732 usbd_xfer_handle xfer;
735 DPRINTFN(5, ("%s: ugenwrite: %d\n", USBDEVNAME(sc->sc_dev), endpt));
740 if (endpt == USB_CONTROL_ENDPOINT)
746 if (sce->edesc == NULL) {
747 printf("ugenwrite: no edesc\n");
750 if (sce->pipeh == NULL) {
751 printf("ugenwrite: no pipe\n");
755 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
757 xfer = usbd_alloc_xfer(sc->sc_udev);
760 while ((n = min(UGEN_BBSIZE, uio->uio_resid)) != 0) {
761 error = uiomove(buf, n, uio);
764 DPRINTFN(1, ("ugenwrite: transfer %d bytes\n", n));
765 err = usbd_bulk_transfer(xfer, sce->pipeh, 0,
766 sce->timeout, buf, &n,"ugenwb");
768 if (err == USBD_INTERRUPTED)
770 else if (err == USBD_TIMEOUT)
777 usbd_free_xfer(xfer);
786 ugenwrite(dev_t dev, struct uio *uio, int flag)
788 int endpt = UGENENDPOINT(dev);
789 struct ugen_softc *sc;
792 USB_GET_SC(ugen, UGENUNIT(dev), sc);
795 error = ugen_do_write(sc, endpt, uio, flag);
796 if (--sc->sc_refcnt < 0)
797 usb_detach_wakeup(USBDEV(sc->sc_dev));
801 #if defined(__NetBSD__) || defined(__OpenBSD__)
803 ugen_activate(device_ptr_t self, enum devact act)
805 struct ugen_softc *sc = (struct ugen_softc *)self;
812 case DVACT_DEACTIVATE:
822 USB_DETACH_START(ugen, sc);
823 struct ugen_endpoint *sce;
826 #if defined(__NetBSD__) || defined(__OpenBSD__)
828 #elif defined(__FreeBSD__)
833 #if defined(__NetBSD__) || defined(__OpenBSD__)
834 DPRINTF(("ugen_detach: sc=%p flags=%d\n", sc, flags));
835 #elif defined(__FreeBSD__)
836 DPRINTF(("ugen_detach: sc=%p\n", sc));
840 /* Abort all pipes. Causes processes waiting for transfer to wake. */
841 for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
842 for (dir = OUT; dir <= IN; dir++) {
843 sce = &sc->sc_endpoints[i][dir];
844 if (sce && sce->pipeh)
845 usbd_abort_pipe(sce->pipeh);
850 if (--sc->sc_refcnt >= 0) {
852 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
853 wakeup(&sc->sc_endpoints[i][IN]);
854 /* Wait for processes to go away. */
855 usb_detach_wait(USBDEV(sc->sc_dev));
859 #if defined(__NetBSD__) || defined(__OpenBSD__)
860 /* locate the major number */
861 for (maj = 0; maj < nchrdev; maj++)
862 if (cdevsw[maj].d_open == ugenopen)
865 /* Nuke the vnodes for any open instances (calls close). */
866 mn = self->dv_unit * USB_MAX_ENDPOINTS;
867 vdevgone(maj, mn, mn + USB_MAX_ENDPOINTS - 1, VCHR);
868 #elif defined(__FreeBSD__)
869 /* destroy the device for the control endpoint */
870 dev = makedev(UGEN_CDEV_MAJOR, UGENMINOR(USBDEVUNIT(sc->sc_dev), 0));
871 vp = SLIST_FIRST(&dev->si_hlist);
873 VOP_REVOKE(vp, REVOKEALL);
875 ugen_destroy_devnodes(sc);
882 ugenintr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
884 struct ugen_endpoint *sce = addr;
885 /*struct ugen_softc *sc = sce->sc;*/
889 if (status == USBD_CANCELLED)
892 if (status != USBD_NORMAL_COMPLETION) {
893 DPRINTF(("ugenintr: status=%d\n", status));
894 if (status == USBD_STALLED)
895 usbd_clear_endpoint_stall_async(sce->pipeh);
899 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
902 DPRINTFN(5, ("ugenintr: xfer=%p status=%d count=%d\n",
903 xfer, status, count));
904 DPRINTFN(5, (" data = %02x %02x %02x\n",
905 ibuf[0], ibuf[1], ibuf[2]));
907 (void)b_to_q(ibuf, count, &sce->q);
909 if (sce->state & UGEN_ASLP) {
910 sce->state &= ~UGEN_ASLP;
911 DPRINTFN(5, ("ugen_intr: waking %p\n", sce));
914 selwakeup(&sce->rsel);
918 ugen_isoc_rintr(usbd_xfer_handle xfer, usbd_private_handle addr,
921 struct isoreq *req = addr;
922 struct ugen_endpoint *sce = req->sce;
926 /* Return if we are aborting. */
927 if (status == USBD_CANCELLED)
930 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
931 DPRINTFN(5,("ugen_isoc_rintr: xfer %d, count=%d\n", req - sce->isoreqs,
934 /* throw away oldest input if the buffer is full */
935 if(sce->fill < sce->cur && sce->cur <= sce->fill + count) {
937 if(sce->cur >= sce->limit)
938 sce->cur = sce->ibuf + (sce->limit - sce->cur);
939 DPRINTFN(5, ("ugen_isoc_rintr: throwing away %d bytes\n",
943 isize = UGETW(sce->edesc->wMaxPacketSize);
944 for (i = 0; i < UGEN_NISORFRMS; i++) {
945 u_int32_t actlen = req->sizes[i];
946 char const *buf = (char const *)req->dmabuf + isize * i;
948 /* copy data to buffer */
950 n = min(actlen, sce->limit - sce->fill);
951 memcpy(sce->fill, buf, n);
956 if(sce->fill == sce->limit)
957 sce->fill = sce->ibuf;
960 /* setup size for next transfer */
961 req->sizes[i] = isize;
964 usbd_setup_isoc_xfer(xfer, sce->pipeh, req, req->sizes, UGEN_NISORFRMS,
965 USBD_NO_COPY, ugen_isoc_rintr);
966 (void)usbd_transfer(xfer);
968 if (sce->state & UGEN_ASLP) {
969 sce->state &= ~UGEN_ASLP;
970 DPRINTFN(5, ("ugen_isoc_rintr: waking %p\n", sce));
973 selwakeup(&sce->rsel);
977 ugen_set_interface(struct ugen_softc *sc, int ifaceidx, int altno)
979 usbd_interface_handle iface;
980 usb_endpoint_descriptor_t *ed;
982 struct ugen_endpoint *sce;
983 u_int8_t niface, nendpt, endptno, endpt;
986 DPRINTFN(15, ("ugen_set_interface %d %d\n", ifaceidx, altno));
988 err = usbd_interface_count(sc->sc_udev, &niface);
991 if (ifaceidx < 0 || ifaceidx >= niface)
994 err = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface);
997 err = usbd_endpoint_count(iface, &nendpt);
1000 /* XXX should only do this after setting new altno has succeeded */
1001 for (endptno = 0; endptno < nendpt; endptno++) {
1002 ed = usbd_interface2endpoint_descriptor(iface,endptno);
1003 endpt = ed->bEndpointAddress;
1004 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
1005 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
1011 /* change setting */
1012 err = usbd_set_interface(iface, altno);
1016 err = usbd_endpoint_count(iface, &nendpt);
1019 for (endptno = 0; endptno < nendpt; endptno++) {
1020 ed = usbd_interface2endpoint_descriptor(iface,endptno);
1021 endpt = ed->bEndpointAddress;
1022 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
1023 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
1031 /* Retrieve a complete descriptor for a certain device and index. */
1032 Static usb_config_descriptor_t *
1033 ugen_get_cdesc(struct ugen_softc *sc, int index, int *lenp)
1035 usb_config_descriptor_t *cdesc, *tdesc, cdescr;
1039 if (index == USB_CURRENT_CONFIG_INDEX) {
1040 tdesc = usbd_get_config_descriptor(sc->sc_udev);
1041 len = UGETW(tdesc->wTotalLength);
1044 cdesc = malloc(len, M_TEMP, M_WAITOK);
1045 memcpy(cdesc, tdesc, len);
1046 DPRINTFN(5,("ugen_get_cdesc: current, len=%d\n", len));
1048 err = usbd_get_config_desc(sc->sc_udev, index, &cdescr);
1051 len = UGETW(cdescr.wTotalLength);
1052 DPRINTFN(5,("ugen_get_cdesc: index=%d, len=%d\n", index, len));
1055 cdesc = malloc(len, M_TEMP, M_WAITOK);
1056 err = usbd_get_config_desc_full(sc->sc_udev, index, cdesc, len);
1058 free(cdesc, M_TEMP);
1066 ugen_get_alt_index(struct ugen_softc *sc, int ifaceidx)
1068 usbd_interface_handle iface;
1071 err = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface);
1074 return (usbd_get_interface_altindex(iface));
1078 ugen_do_ioctl(struct ugen_softc *sc, int endpt, u_long cmd,
1079 caddr_t addr, int flag, usb_proc_ptr p)
1081 struct ugen_endpoint *sce;
1083 usbd_interface_handle iface;
1084 struct usb_config_desc *cd;
1085 usb_config_descriptor_t *cdesc;
1086 struct usb_interface_desc *id;
1087 usb_interface_descriptor_t *idesc;
1088 struct usb_endpoint_desc *ed;
1089 usb_endpoint_descriptor_t *edesc;
1090 struct usb_alt_interface *ai;
1091 struct usb_string_desc *si;
1094 DPRINTFN(5, ("ugenioctl: cmd=%08lx\n", cmd));
1100 /* All handled in the upper FS layer. */
1102 case USB_SET_SHORT_XFER:
1103 /* This flag only affects read */
1104 if (endpt == USB_CONTROL_ENDPOINT)
1106 sce = &sc->sc_endpoints[endpt][IN];
1110 if (sce->pipeh == NULL) {
1111 printf("ugenioctl: USB_SET_SHORT_XFER, no pipe\n");
1116 sce->state |= UGEN_SHORT_OK;
1118 sce->state &= ~UGEN_SHORT_OK;
1120 case USB_SET_TIMEOUT:
1121 sce = &sc->sc_endpoints[endpt][IN];
1124 sce->timeout = *(int *)addr;
1130 if (endpt != USB_CONTROL_ENDPOINT)
1136 ugendebug = *(int *)addr;
1139 case USB_GET_CONFIG:
1140 err = usbd_get_config(sc->sc_udev, &conf);
1143 *(int *)addr = conf;
1145 case USB_SET_CONFIG:
1146 if (!(flag & FWRITE))
1148 err = ugen_set_config(sc, *(int *)addr);
1150 case USBD_NORMAL_COMPLETION:
1158 case USB_GET_ALTINTERFACE:
1159 ai = (struct usb_alt_interface *)addr;
1160 err = usbd_device2interface_handle(sc->sc_udev,
1161 ai->uai_interface_index, &iface);
1164 idesc = usbd_get_interface_descriptor(iface);
1167 ai->uai_alt_no = idesc->bAlternateSetting;
1169 case USB_SET_ALTINTERFACE:
1170 if (!(flag & FWRITE))
1172 ai = (struct usb_alt_interface *)addr;
1173 err = usbd_device2interface_handle(sc->sc_udev,
1174 ai->uai_interface_index, &iface);
1177 err = ugen_set_interface(sc, ai->uai_interface_index, ai->uai_alt_no);
1181 case USB_GET_NO_ALT:
1182 ai = (struct usb_alt_interface *)addr;
1183 cdesc = ugen_get_cdesc(sc, ai->uai_config_index, 0);
1186 idesc = usbd_find_idesc(cdesc, ai->uai_interface_index, 0);
1187 if (idesc == NULL) {
1188 free(cdesc, M_TEMP);
1191 ai->uai_alt_no = usbd_get_no_alts(cdesc, idesc->bInterfaceNumber);
1192 free(cdesc, M_TEMP);
1194 case USB_GET_DEVICE_DESC:
1195 *(usb_device_descriptor_t *)addr =
1196 *usbd_get_device_descriptor(sc->sc_udev);
1198 case USB_GET_CONFIG_DESC:
1199 cd = (struct usb_config_desc *)addr;
1200 cdesc = ugen_get_cdesc(sc, cd->ucd_config_index, 0);
1203 cd->ucd_desc = *cdesc;
1204 free(cdesc, M_TEMP);
1206 case USB_GET_INTERFACE_DESC:
1207 id = (struct usb_interface_desc *)addr;
1208 cdesc = ugen_get_cdesc(sc, id->uid_config_index, 0);
1211 if (id->uid_config_index == USB_CURRENT_CONFIG_INDEX &&
1212 id->uid_alt_index == USB_CURRENT_ALT_INDEX)
1213 alt = ugen_get_alt_index(sc, id->uid_interface_index);
1215 alt = id->uid_alt_index;
1216 idesc = usbd_find_idesc(cdesc, id->uid_interface_index, alt);
1217 if (idesc == NULL) {
1218 free(cdesc, M_TEMP);
1221 id->uid_desc = *idesc;
1222 free(cdesc, M_TEMP);
1224 case USB_GET_ENDPOINT_DESC:
1225 ed = (struct usb_endpoint_desc *)addr;
1226 cdesc = ugen_get_cdesc(sc, ed->ued_config_index, 0);
1229 if (ed->ued_config_index == USB_CURRENT_CONFIG_INDEX &&
1230 ed->ued_alt_index == USB_CURRENT_ALT_INDEX)
1231 alt = ugen_get_alt_index(sc, ed->ued_interface_index);
1233 alt = ed->ued_alt_index;
1234 edesc = usbd_find_edesc(cdesc, ed->ued_interface_index,
1235 alt, ed->ued_endpoint_index);
1236 if (edesc == NULL) {
1237 free(cdesc, M_TEMP);
1240 ed->ued_desc = *edesc;
1241 free(cdesc, M_TEMP);
1243 case USB_GET_FULL_DESC:
1248 struct usb_full_desc *fd = (struct usb_full_desc *)addr;
1251 cdesc = ugen_get_cdesc(sc, fd->ufd_config_index, &len);
1252 if (len > fd->ufd_size)
1254 iov.iov_base = (caddr_t)fd->ufd_data;
1258 uio.uio_resid = len;
1260 uio.uio_segflg = UIO_USERSPACE;
1261 uio.uio_rw = UIO_READ;
1263 error = uiomove((void *)cdesc, len, &uio);
1264 free(cdesc, M_TEMP);
1267 case USB_GET_STRING_DESC:
1268 si = (struct usb_string_desc *)addr;
1269 err = usbd_get_string_desc(sc->sc_udev, si->usd_string_index,
1270 si->usd_language_id, &si->usd_desc);
1274 case USB_DO_REQUEST:
1276 struct usb_ctl_request *ur = (void *)addr;
1277 int len = UGETW(ur->ucr_request.wLength);
1284 if (!(flag & FWRITE))
1286 /* Avoid requests that would damage the bus integrity. */
1287 if ((ur->ucr_request.bmRequestType == UT_WRITE_DEVICE &&
1288 ur->ucr_request.bRequest == UR_SET_ADDRESS) ||
1289 (ur->ucr_request.bmRequestType == UT_WRITE_DEVICE &&
1290 ur->ucr_request.bRequest == UR_SET_CONFIG) ||
1291 (ur->ucr_request.bmRequestType == UT_WRITE_INTERFACE &&
1292 ur->ucr_request.bRequest == UR_SET_INTERFACE))
1295 if (len < 0 || len > 32767)
1298 iov.iov_base = (caddr_t)ur->ucr_data;
1302 uio.uio_resid = len;
1304 uio.uio_segflg = UIO_USERSPACE;
1306 ur->ucr_request.bmRequestType & UT_READ ?
1307 UIO_READ : UIO_WRITE;
1309 ptr = malloc(len, M_TEMP, M_WAITOK);
1310 if (uio.uio_rw == UIO_WRITE) {
1311 error = uiomove(ptr, len, &uio);
1316 sce = &sc->sc_endpoints[endpt][IN];
1317 err = usbd_do_request_flags(sc->sc_udev, &ur->ucr_request,
1318 ptr, ur->ucr_flags, &ur->ucr_actlen, sce->timeout);
1324 if (uio.uio_rw == UIO_READ) {
1325 error = uiomove(ptr, len, &uio);
1335 case USB_GET_DEVICEINFO:
1336 usbd_fill_deviceinfo(sc->sc_udev,
1337 (struct usb_device_info *)addr, 1);
1346 ugenioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, usb_proc_ptr p)
1348 int endpt = UGENENDPOINT(dev);
1349 struct ugen_softc *sc;
1352 USB_GET_SC(ugen, UGENUNIT(dev), sc);
1355 error = ugen_do_ioctl(sc, endpt, cmd, addr, flag, p);
1356 if (--sc->sc_refcnt < 0)
1357 usb_detach_wakeup(USBDEV(sc->sc_dev));
1362 ugenpoll(dev_t dev, int events, usb_proc_ptr p)
1364 struct ugen_softc *sc;
1365 struct ugen_endpoint *sce;
1369 USB_GET_SC(ugen, UGENUNIT(dev), sc);
1375 sce = &sc->sc_endpoints[UGENENDPOINT(dev)][IN];
1380 printf("ugenpoll: no edesc\n");
1384 printf("ugenpoll: no pipe\n");
1389 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
1391 if (events & (POLLIN | POLLRDNORM)) {
1392 if (sce->q.c_cc > 0)
1393 revents |= events & (POLLIN | POLLRDNORM);
1395 selrecord(p, &sce->rsel);
1398 case UE_ISOCHRONOUS:
1399 if (events & (POLLIN | POLLRDNORM)) {
1400 if (sce->cur != sce->fill)
1401 revents |= events & (POLLIN | POLLRDNORM);
1403 selrecord(p, &sce->rsel);
1408 * We have no easy way of determining if a read will
1409 * yield any data or a write will happen.
1410 * Pretend they will.
1413 (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM);
1422 #if defined(__FreeBSD__)
1423 DRIVER_MODULE(ugen, uhub, ugen_driver, ugen_devclass, usbd_driver_load, 0);