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 $ */
3 /* $DragonFly: src/sys/dev/usbmisc/ugen/ugen.c,v 1.6 2003/07/21 05:50:37 dillon Exp $ */
6 * Copyright (c) 1998 The NetBSD Foundation, Inc.
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Lennart Augustsson (lennart@augustsson.net) at
11 * Carlstedt Research & Technology.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the NetBSD
24 * Foundation, Inc. and its contributors.
25 * 4. Neither the name of The NetBSD Foundation nor the names of its
26 * contributors may be used to endorse or promote products derived
27 * from this software without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #if defined(__NetBSD__) || defined(__OpenBSD__)
48 #include <sys/device.h>
49 #include <sys/ioctl.h>
50 #elif defined(__FreeBSD__)
51 #include <sys/module.h>
53 #include <sys/ioccom.h>
55 #include <sys/fcntl.h>
56 #include <sys/filio.h>
60 #include <sys/select.h>
62 #include <sys/vnode.h>
64 #include <sys/sysctl.h>
66 #include <dev/usb/usb.h>
67 #include <dev/usb/usbdi.h>
68 #include <dev/usb/usbdi_util.h>
71 #define DPRINTF(x) if (ugendebug) logprintf x
72 #define DPRINTFN(n,x) if (ugendebug>(n)) logprintf x
74 SYSCTL_NODE(_hw_usb, OID_AUTO, ugen, CTLFLAG_RW, 0, "USB ugen");
75 SYSCTL_INT(_hw_usb_ugen, OID_AUTO, debug, CTLFLAG_RW,
76 &ugendebug, 0, "ugen debug level");
82 #define UGEN_CHUNK 128 /* chunk size for read */
83 #define UGEN_IBSIZE 1020 /* buffer size */
84 #define UGEN_BBSIZE 1024
86 #define UGEN_NISOFRAMES 500 /* 0.5 seconds worth */
87 #define UGEN_NISOREQS 6 /* number of outstanding xfer requests */
88 #define UGEN_NISORFRMS 4 /* number of frames (miliseconds) per req */
90 struct ugen_endpoint {
91 struct ugen_softc *sc;
92 usb_endpoint_descriptor_t *edesc;
93 usbd_interface_handle iface;
95 #define UGEN_ASLP 0x02 /* waiting for data */
96 #define UGEN_SHORT_OK 0x04 /* short xfers are OK */
97 usbd_pipe_handle pipeh;
100 u_char *ibuf; /* start of buffer (circular for isoc) */
101 u_char *fill; /* location for input (isoc) */
102 u_char *limit; /* end of circular buffer (isoc) */
103 u_char *cur; /* current read location (isoc) */
106 struct ugen_endpoint *sce;
107 usbd_xfer_handle xfer;
109 u_int16_t sizes[UGEN_NISORFRMS];
110 } isoreqs[UGEN_NISOREQS];
114 USBBASEDEVICE sc_dev; /* base device */
115 usbd_device_handle sc_udev;
117 char sc_is_open[USB_MAX_ENDPOINTS];
118 struct ugen_endpoint sc_endpoints[USB_MAX_ENDPOINTS][2];
126 #if defined(__NetBSD__) || defined(__OpenBSD__)
128 #elif defined(__FreeBSD__)
136 #define UGEN_CDEV_MAJOR 114
138 Static struct cdevsw ugen_cdevsw = {
140 /* maj */ UGEN_CDEV_MAJOR,
146 /* close */ ugenclose,
148 /* write */ ugenwrite,
149 /* ioctl */ ugenioctl,
152 /* strategy */ nostrategy,
158 Static void ugenintr(usbd_xfer_handle xfer, usbd_private_handle addr,
160 Static void ugen_isoc_rintr(usbd_xfer_handle xfer, usbd_private_handle addr,
162 Static int ugen_do_read(struct ugen_softc *, int, struct uio *, int);
163 Static int ugen_do_write(struct ugen_softc *, int, struct uio *, int);
164 Static int ugen_do_ioctl(struct ugen_softc *, int, u_long,
165 caddr_t, int, usb_proc_ptr);
166 Static int ugen_set_config(struct ugen_softc *sc, int configno);
167 Static usb_config_descriptor_t *ugen_get_cdesc(struct ugen_softc *sc,
168 int index, int *lenp);
169 Static usbd_status ugen_set_interface(struct ugen_softc *, int, int);
170 Static int ugen_get_alt_index(struct ugen_softc *sc, int ifaceidx);
172 #define UGENUNIT(n) ((minor(n) >> 4) & 0xf)
173 #define UGENENDPOINT(n) (minor(n) & 0xf)
174 #define UGENMINOR(u, e) (((u) << 4) | (e))
176 USB_DECLARE_DRIVER(ugen);
180 USB_MATCH_START(ugen, uaa);
183 return (UMATCH_GENERIC);
185 return (UMATCH_NONE);
190 USB_ATTACH_START(ugen, sc, uaa);
191 usbd_device_handle udev;
196 usbd_devinfo(uaa->device, 0, devinfo);
198 printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
200 sc->sc_udev = udev = uaa->device;
202 memset(sc->sc_endpoints, 0, sizeof sc->sc_endpoints);
204 /* First set configuration index 0, the default one for ugen. */
205 err = usbd_set_config_index(udev, 0, 0);
207 printf("%s: setting configuration index 0 failed\n",
208 USBDEVNAME(sc->sc_dev));
210 USB_ATTACH_ERROR_RETURN;
212 conf = usbd_get_config_descriptor(udev)->bConfigurationValue;
214 /* Set up all the local state for this configuration. */
215 err = ugen_set_config(sc, conf);
217 printf("%s: setting configuration %d failed\n",
218 USBDEVNAME(sc->sc_dev), conf);
220 USB_ATTACH_ERROR_RETURN;
223 USB_ATTACH_SUCCESS_RETURN;
227 ugen_set_config(struct ugen_softc *sc, int configno)
229 usbd_device_handle dev = sc->sc_udev;
230 usbd_interface_handle iface;
231 usb_endpoint_descriptor_t *ed;
232 struct ugen_endpoint *sce;
233 u_int8_t niface, nendpt;
234 int ifaceno, endptno, endpt;
238 DPRINTFN(1,("ugen_set_config: %s to configno %d, sc=%p\n",
239 USBDEVNAME(sc->sc_dev), configno, sc));
241 /* We start at 1, not 0, because we don't care whether the
242 * control endpoint is open or not. It is always present.
244 for (endptno = 1; endptno < USB_MAX_ENDPOINTS; endptno++)
245 if (sc->sc_is_open[endptno]) {
247 ("ugen_set_config: %s - endpoint %d is open\n",
248 USBDEVNAME(sc->sc_dev), endptno));
249 return (USBD_IN_USE);
252 if (usbd_get_config_descriptor(dev)->bConfigurationValue != configno) {
253 /* Avoid setting the current value. */
254 err = usbd_set_config_no(dev, configno, 0);
259 err = usbd_interface_count(dev, &niface);
262 memset(sc->sc_endpoints, 0, sizeof sc->sc_endpoints);
263 for (ifaceno = 0; ifaceno < niface; ifaceno++) {
264 DPRINTFN(1,("ugen_set_config: ifaceno %d\n", ifaceno));
265 err = usbd_device2interface_handle(dev, ifaceno, &iface);
268 err = usbd_endpoint_count(iface, &nendpt);
271 for (endptno = 0; endptno < nendpt; endptno++) {
272 ed = usbd_interface2endpoint_descriptor(iface,endptno);
273 endpt = UE_GET_ADDR(ed->bEndpointAddress);
274 dir = UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN?
277 sce = &sc->sc_endpoints[endpt][dir];
278 DPRINTFN(1,("ugen_set_config: endptno %d, endpt=0x%02x"
280 endptno, endpt, endpt, dir, sce));
289 #if defined(__FreeBSD__)
290 /* the main device, ctrl endpoint */
291 make_dev(&ugen_cdevsw, UGENMINOR(USBDEVUNIT(sc->sc_dev), 0),
292 UID_ROOT, GID_OPERATOR, 0644, "%s", USBDEVNAME(sc->sc_dev));
294 for (endptno = 1; endptno < USB_MAX_ENDPOINTS; endptno++) {
295 if (sc->sc_endpoints[endptno][IN].sc != NULL ||
296 sc->sc_endpoints[endptno][OUT].sc != NULL ) {
297 /* endpt can be 0x81 and 0x01, representing
298 * endpoint address 0x01 and IN/OUT directions.
299 * We map both endpts to the same device,
300 * IN is reading from it, OUT is writing to it.
302 * In the if clause above we check whether one
303 * of the structs is populated.
305 make_dev(&ugen_cdevsw,
306 UGENMINOR(USBDEVUNIT(sc->sc_dev), endptno),
307 UID_ROOT, GID_OPERATOR, 0644,
309 USBDEVNAME(sc->sc_dev), endptno);
314 return (USBD_NORMAL_COMPLETION);
318 ugenopen(dev_t dev, int flag, int mode, usb_proc_ptr p)
320 struct ugen_softc *sc;
321 int unit = UGENUNIT(dev);
322 int endpt = UGENENDPOINT(dev);
323 usb_endpoint_descriptor_t *edesc;
324 struct ugen_endpoint *sce;
327 usbd_xfer_handle xfer;
331 USB_GET_SC_OPEN(ugen, unit, sc);
333 DPRINTFN(5, ("ugenopen: flag=%d, mode=%d, unit=%d endpt=%d\n",
334 flag, mode, unit, endpt));
336 if (sc == NULL || sc->sc_dying)
339 if (sc->sc_is_open[endpt])
342 if (endpt == USB_CONTROL_ENDPOINT) {
343 sc->sc_is_open[USB_CONTROL_ENDPOINT] = 1;
347 /* Make sure there are pipes for all directions. */
348 for (dir = OUT; dir <= IN; dir++) {
349 if (flag & (dir == OUT ? FWRITE : FREAD)) {
350 sce = &sc->sc_endpoints[endpt][dir];
351 if (sce == 0 || sce->edesc == 0)
356 /* Actually open the pipes. */
357 /* XXX Should back out properly if it fails. */
358 for (dir = OUT; dir <= IN; dir++) {
359 if (!(flag & (dir == OUT ? FWRITE : FREAD)))
361 sce = &sc->sc_endpoints[endpt][dir];
363 sce->timeout = USBD_NO_TIMEOUT;
364 DPRINTFN(5, ("ugenopen: sc=%p, endpt=%d, dir=%d, sce=%p\n",
365 sc, endpt, dir, sce));
367 switch (edesc->bmAttributes & UE_XFERTYPE) {
369 isize = UGETW(edesc->wMaxPacketSize);
370 if (isize == 0) /* shouldn't happen */
372 sce->ibuf = malloc(isize, M_USBDEV, M_WAITOK);
373 DPRINTFN(5, ("ugenopen: intr endpt=%d,isize=%d\n",
375 if (clalloc(&sce->q, UGEN_IBSIZE, 0) == -1)
377 err = usbd_open_pipe_intr(sce->iface,
378 edesc->bEndpointAddress,
379 USBD_SHORT_XFER_OK, &sce->pipeh, sce,
380 sce->ibuf, isize, ugenintr,
381 USBD_DEFAULT_INTERVAL);
383 free(sce->ibuf, M_USBDEV);
387 DPRINTFN(5, ("ugenopen: interrupt open done\n"));
390 err = usbd_open_pipe(sce->iface,
391 edesc->bEndpointAddress, 0, &sce->pipeh);
398 isize = UGETW(edesc->wMaxPacketSize);
399 if (isize == 0) /* shouldn't happen */
401 sce->ibuf = malloc(isize * UGEN_NISOFRAMES,
403 sce->cur = sce->fill = sce->ibuf;
404 sce->limit = sce->ibuf + isize * UGEN_NISOFRAMES;
405 DPRINTFN(5, ("ugenopen: isoc endpt=%d, isize=%d\n",
407 err = usbd_open_pipe(sce->iface,
408 edesc->bEndpointAddress, 0, &sce->pipeh);
410 free(sce->ibuf, M_USBDEV);
413 for(i = 0; i < UGEN_NISOREQS; ++i) {
414 sce->isoreqs[i].sce = sce;
415 xfer = usbd_alloc_xfer(sc->sc_udev);
418 sce->isoreqs[i].xfer = xfer;
419 buf = usbd_alloc_buffer
420 (xfer, isize * UGEN_NISORFRMS);
425 sce->isoreqs[i].dmabuf = buf;
426 for(j = 0; j < UGEN_NISORFRMS; ++j)
427 sce->isoreqs[i].sizes[j] = isize;
429 (xfer, sce->pipeh, &sce->isoreqs[i],
430 sce->isoreqs[i].sizes,
431 UGEN_NISORFRMS, USBD_NO_COPY,
433 (void)usbd_transfer(xfer);
435 DPRINTFN(5, ("ugenopen: isoc open done\n"));
438 while (--i >= 0) /* implicit buffer free */
439 usbd_free_xfer(sce->isoreqs[i].xfer);
445 sc->sc_is_open[endpt] = 1;
450 ugenclose(dev_t dev, int flag, int mode, usb_proc_ptr p)
452 int endpt = UGENENDPOINT(dev);
453 struct ugen_softc *sc;
454 struct ugen_endpoint *sce;
458 USB_GET_SC(ugen, UGENUNIT(dev), sc);
460 DPRINTFN(5, ("ugenclose: flag=%d, mode=%d, unit=%d, endpt=%d\n",
461 flag, mode, UGENUNIT(dev), endpt));
464 if (!sc->sc_is_open[endpt]) {
465 printf("ugenclose: not open\n");
470 if (endpt == USB_CONTROL_ENDPOINT) {
471 DPRINTFN(5, ("ugenclose: close control\n"));
472 sc->sc_is_open[endpt] = 0;
476 for (dir = OUT; dir <= IN; dir++) {
477 if (!(flag & (dir == OUT ? FWRITE : FREAD)))
479 sce = &sc->sc_endpoints[endpt][dir];
480 if (sce == NULL || sce->pipeh == NULL)
482 DPRINTFN(5, ("ugenclose: endpt=%d dir=%d sce=%p\n",
485 usbd_abort_pipe(sce->pipeh);
486 usbd_close_pipe(sce->pipeh);
489 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
491 ndflush(&sce->q, sce->q.c_cc);
495 for (i = 0; i < UGEN_NISOREQS; ++i)
496 usbd_free_xfer(sce->isoreqs[i].xfer);
501 if (sce->ibuf != NULL) {
502 free(sce->ibuf, M_USBDEV);
506 sc->sc_is_open[endpt] = 0;
512 ugen_do_read(struct ugen_softc *sc, int endpt, struct uio *uio, int flag)
514 struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][IN];
516 char buf[UGEN_BBSIZE];
517 usbd_xfer_handle xfer;
521 u_char buffer[UGEN_CHUNK];
523 DPRINTFN(5, ("%s: ugenread: %d\n", USBDEVNAME(sc->sc_dev), endpt));
528 if (endpt == USB_CONTROL_ENDPOINT)
532 if (sce->edesc == NULL) {
533 printf("ugenread: no edesc\n");
536 if (sce->pipeh == NULL) {
537 printf("ugenread: no pipe\n");
542 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
544 /* Block until activity occured. */
546 while (sce->q.c_cc == 0) {
547 if (flag & IO_NDELAY) {
549 return (EWOULDBLOCK);
551 sce->state |= UGEN_ASLP;
552 DPRINTFN(5, ("ugenread: sleep on %p\n", sc));
553 error = tsleep(sce, PCATCH, "ugenri", 0);
554 DPRINTFN(5, ("ugenread: woke, error=%d\n", error));
558 sce->state &= ~UGEN_ASLP;
564 /* Transfer as many chunks as possible. */
565 while (sce->q.c_cc > 0 && uio->uio_resid > 0 && !error) {
566 n = min(sce->q.c_cc, uio->uio_resid);
567 if (n > sizeof(buffer))
570 /* Remove a small chunk from the input queue. */
571 q_to_b(&sce->q, buffer, n);
572 DPRINTFN(5, ("ugenread: got %d chars\n", n));
574 /* Copy the data to the user process. */
575 error = uiomove(buffer, n, uio);
581 xfer = usbd_alloc_xfer(sc->sc_udev);
584 while ((n = min(UGEN_BBSIZE, uio->uio_resid)) != 0) {
585 DPRINTFN(1, ("ugenread: start transfer %d bytes\n",n));
587 err = usbd_bulk_transfer(
589 sce->state & UGEN_SHORT_OK ?
590 USBD_SHORT_XFER_OK : 0,
591 sce->timeout, buf, &tn, "ugenrb");
593 if (err == USBD_INTERRUPTED)
595 else if (err == USBD_TIMEOUT)
601 DPRINTFN(1, ("ugenread: got %d bytes\n", tn));
602 error = uiomove(buf, tn, uio);
606 usbd_free_xfer(xfer);
610 while (sce->cur == sce->fill) {
611 if (flag & IO_NDELAY) {
613 return (EWOULDBLOCK);
615 sce->state |= UGEN_ASLP;
616 DPRINTFN(5, ("ugenread: sleep on %p\n", sc));
617 error = tsleep(sce, PCATCH, "ugenri", 0);
618 DPRINTFN(5, ("ugenread: woke, error=%d\n", error));
622 sce->state &= ~UGEN_ASLP;
627 while (sce->cur != sce->fill && uio->uio_resid > 0 && !error) {
628 if(sce->fill > sce->cur)
629 n = min(sce->fill - sce->cur, uio->uio_resid);
631 n = min(sce->limit - sce->cur, uio->uio_resid);
633 DPRINTFN(5, ("ugenread: isoc got %d chars\n", n));
635 /* Copy the data to the user process. */
636 error = uiomove(sce->cur, n, uio);
640 if(sce->cur >= sce->limit)
641 sce->cur = sce->ibuf;
654 ugenread(dev_t dev, struct uio *uio, int flag)
656 int endpt = UGENENDPOINT(dev);
657 struct ugen_softc *sc;
660 USB_GET_SC(ugen, UGENUNIT(dev), sc);
663 error = ugen_do_read(sc, endpt, uio, flag);
664 if (--sc->sc_refcnt < 0)
665 usb_detach_wakeup(USBDEV(sc->sc_dev));
670 ugen_do_write(struct ugen_softc *sc, int endpt, struct uio *uio, int flag)
672 struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][OUT];
675 char buf[UGEN_BBSIZE];
676 usbd_xfer_handle xfer;
679 DPRINTFN(5, ("%s: ugen_do_write: %d\n", USBDEVNAME(sc->sc_dev), endpt));
684 if (endpt == USB_CONTROL_ENDPOINT)
688 if (sce->edesc == NULL) {
689 printf("ugen_do_write: no edesc\n");
692 if (sce->pipeh == NULL) {
693 printf("ugen_do_write: no pipe\n");
698 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
700 xfer = usbd_alloc_xfer(sc->sc_udev);
703 while ((n = min(UGEN_BBSIZE, uio->uio_resid)) != 0) {
704 error = uiomove(buf, n, uio);
707 DPRINTFN(1, ("ugen_do_write: transfer %d bytes\n", n));
708 err = usbd_bulk_transfer(xfer, sce->pipeh, 0,
709 sce->timeout, buf, &n,"ugenwb");
711 if (err == USBD_INTERRUPTED)
718 usbd_free_xfer(xfer);
727 ugenwrite(dev_t dev, struct uio *uio, int flag)
729 int endpt = UGENENDPOINT(dev);
730 struct ugen_softc *sc;
733 USB_GET_SC(ugen, UGENUNIT(dev), sc);
736 error = ugen_do_write(sc, endpt, uio, flag);
737 if (--sc->sc_refcnt < 0)
738 usb_detach_wakeup(USBDEV(sc->sc_dev));
742 #if defined(__NetBSD__) || defined(__OpenBSD__)
744 ugen_activate(device_ptr_t self, enum devact act)
746 struct ugen_softc *sc = (struct ugen_softc *)self;
753 case DVACT_DEACTIVATE:
763 USB_DETACH_START(ugen, sc);
764 struct ugen_endpoint *sce;
767 #if defined(__NetBSD__) || defined(__OpenBSD__)
769 #elif defined(__FreeBSD__)
775 #if defined(__NetBSD__) || defined(__OpenBSD__)
776 DPRINTF(("ugen_detach: sc=%p flags=%d\n", sc, flags));
777 #elif defined(__FreeBSD__)
778 DPRINTF(("ugen_detach: sc=%p\n", sc));
782 /* Abort all pipes. Causes processes waiting for transfer to wake. */
783 for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
784 for (dir = OUT; dir <= IN; dir++) {
785 sce = &sc->sc_endpoints[i][dir];
786 if (sce && sce->pipeh)
787 usbd_abort_pipe(sce->pipeh);
792 if (--sc->sc_refcnt >= 0) {
794 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
795 wakeup(&sc->sc_endpoints[i][IN]);
796 /* Wait for processes to go away. */
797 usb_detach_wait(USBDEV(sc->sc_dev));
801 #if defined(__NetBSD__) || defined(__OpenBSD__)
802 /* locate the major number */
803 for (maj = 0; maj < nchrdev; maj++)
804 if (cdevsw[maj].d_open == ugenopen)
807 /* Nuke the vnodes for any open instances (calls close). */
808 mn = self->dv_unit * USB_MAX_ENDPOINTS;
809 vdevgone(maj, mn, mn + USB_MAX_ENDPOINTS - 1, VCHR);
810 #elif defined(__FreeBSD__)
811 /* destroy the device for the control endpoint */
812 dev = makedev(UGEN_CDEV_MAJOR, UGENMINOR(USBDEVUNIT(sc->sc_dev), 0));
813 vp = SLIST_FIRST(&dev->si_hlist);
815 VOP_REVOKE(vp, REVOKEALL);
818 /* destroy all devices for the other (existing) endpoints as well */
819 for (endptno = 1; endptno < USB_MAX_ENDPOINTS; endptno++) {
820 if (sc->sc_endpoints[endptno][IN].sc != NULL ||
821 sc->sc_endpoints[endptno][OUT].sc != NULL ) {
822 /* endpt can be 0x81 and 0x01, representing
823 * endpoint address 0x01 and IN/OUT directions.
824 * We map both endpoint addresses to the same device,
825 * IN is reading from it, OUT is writing to it.
827 * In the if clause above we check whether one
828 * of the structs is populated.
830 dev = makedev(UGEN_CDEV_MAJOR,
831 UGENMINOR(USBDEVUNIT(sc->sc_dev), endptno));
832 vp = SLIST_FIRST(&dev->si_hlist);
834 VOP_REVOKE(vp, REVOKEALL);
845 ugenintr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
847 struct ugen_endpoint *sce = addr;
848 /*struct ugen_softc *sc = sce->sc;*/
852 if (status == USBD_CANCELLED)
855 if (status != USBD_NORMAL_COMPLETION) {
856 DPRINTF(("ugenintr: status=%d\n", status));
857 usbd_clear_endpoint_stall_async(sce->pipeh);
861 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
864 DPRINTFN(5, ("ugenintr: xfer=%p status=%d count=%d\n",
865 xfer, status, count));
866 DPRINTFN(5, (" data = %02x %02x %02x\n",
867 ibuf[0], ibuf[1], ibuf[2]));
869 (void)b_to_q(ibuf, count, &sce->q);
871 if (sce->state & UGEN_ASLP) {
872 sce->state &= ~UGEN_ASLP;
873 DPRINTFN(5, ("ugen_intr: waking %p\n", sce));
876 selwakeup(&sce->rsel);
880 ugen_isoc_rintr(usbd_xfer_handle xfer, usbd_private_handle addr,
883 struct isoreq *req = addr;
884 struct ugen_endpoint *sce = req->sce;
887 /* Return if we are aborting. */
888 if (status == USBD_CANCELLED)
891 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
892 DPRINTFN(5,("ugen_isoc_rintr: xfer %d, count=%d\n", req - sce->isoreqs,
895 /* throw away oldest input if the buffer is full */
896 if(sce->fill < sce->cur && sce->cur <= sce->fill + count) {
898 if(sce->cur >= sce->limit)
899 sce->cur = sce->ibuf + (sce->limit - sce->cur);
900 DPRINTFN(5, ("ugen_isoc_rintr: throwing away %d bytes\n",
904 /* copy data to buffer */
906 n = min(count, sce->limit - sce->fill);
907 memcpy(sce->fill, req->dmabuf, n);
911 if(sce->fill == sce->limit)
912 sce->fill = sce->ibuf;
915 usbd_setup_isoc_xfer(xfer, sce->pipeh, req, req->sizes, UGEN_NISORFRMS,
916 USBD_NO_COPY, ugen_isoc_rintr);
917 (void)usbd_transfer(xfer);
919 if (sce->state & UGEN_ASLP) {
920 sce->state &= ~UGEN_ASLP;
921 DPRINTFN(5, ("ugen_isoc_rintr: waking %p\n", sce));
924 selwakeup(&sce->rsel);
928 ugen_set_interface(struct ugen_softc *sc, int ifaceidx, int altno)
930 usbd_interface_handle iface;
931 usb_endpoint_descriptor_t *ed;
933 struct ugen_endpoint *sce;
934 u_int8_t niface, nendpt, endptno, endpt;
937 DPRINTFN(15, ("ugen_set_interface %d %d\n", ifaceidx, altno));
939 err = usbd_interface_count(sc->sc_udev, &niface);
942 if (ifaceidx < 0 || ifaceidx >= niface)
945 err = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface);
948 err = usbd_endpoint_count(iface, &nendpt);
951 for (endptno = 0; endptno < nendpt; endptno++) {
952 ed = usbd_interface2endpoint_descriptor(iface,endptno);
953 endpt = ed->bEndpointAddress;
954 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
955 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
962 err = usbd_set_interface(iface, altno);
966 err = usbd_endpoint_count(iface, &nendpt);
969 for (endptno = 0; endptno < nendpt; endptno++) {
970 ed = usbd_interface2endpoint_descriptor(iface,endptno);
971 endpt = ed->bEndpointAddress;
972 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
973 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
981 /* Retrieve a complete descriptor for a certain device and index. */
982 Static usb_config_descriptor_t *
983 ugen_get_cdesc(struct ugen_softc *sc, int index, int *lenp)
985 usb_config_descriptor_t *cdesc, *tdesc, cdescr;
989 if (index == USB_CURRENT_CONFIG_INDEX) {
990 tdesc = usbd_get_config_descriptor(sc->sc_udev);
991 len = UGETW(tdesc->wTotalLength);
994 cdesc = malloc(len, M_TEMP, M_WAITOK);
995 memcpy(cdesc, tdesc, len);
996 DPRINTFN(5,("ugen_get_cdesc: current, len=%d\n", len));
998 err = usbd_get_config_desc(sc->sc_udev, index, &cdescr);
1001 len = UGETW(cdescr.wTotalLength);
1002 DPRINTFN(5,("ugen_get_cdesc: index=%d, len=%d\n", index, len));
1005 cdesc = malloc(len, M_TEMP, M_WAITOK);
1006 err = usbd_get_config_desc_full(sc->sc_udev, index, cdesc, len);
1008 free(cdesc, M_TEMP);
1016 ugen_get_alt_index(struct ugen_softc *sc, int ifaceidx)
1018 usbd_interface_handle iface;
1021 err = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface);
1024 return (usbd_get_interface_altindex(iface));
1028 ugen_do_ioctl(struct ugen_softc *sc, int endpt, u_long cmd,
1029 caddr_t addr, int flag, usb_proc_ptr p)
1031 struct ugen_endpoint *sce;
1033 usbd_interface_handle iface;
1034 struct usb_config_desc *cd;
1035 usb_config_descriptor_t *cdesc;
1036 struct usb_interface_desc *id;
1037 usb_interface_descriptor_t *idesc;
1038 struct usb_endpoint_desc *ed;
1039 usb_endpoint_descriptor_t *edesc;
1040 struct usb_alt_interface *ai;
1041 struct usb_string_desc *si;
1044 DPRINTFN(5, ("ugenioctl: cmd=%08lx\n", cmd));
1050 /* All handled in the upper FS layer. */
1052 case USB_SET_SHORT_XFER:
1053 /* This flag only affects read */
1054 if (endpt == USB_CONTROL_ENDPOINT)
1056 sce = &sc->sc_endpoints[endpt][IN];
1060 if (sce->pipeh == NULL) {
1061 printf("ugenioctl: USB_SET_SHORT_XFER, no pipe\n");
1066 sce->state |= UGEN_SHORT_OK;
1068 sce->state &= ~UGEN_SHORT_OK;
1070 case USB_SET_TIMEOUT:
1071 sce = &sc->sc_endpoints[endpt][IN];
1075 if (sce->pipeh == NULL) {
1076 printf("ugenioctl: USB_SET_TIMEOUT, no pipe\n");
1080 sce->timeout = *(int *)addr;
1086 if (endpt != USB_CONTROL_ENDPOINT)
1092 ugendebug = *(int *)addr;
1095 case USB_GET_CONFIG:
1096 err = usbd_get_config(sc->sc_udev, &conf);
1099 *(int *)addr = conf;
1101 case USB_SET_CONFIG:
1102 if (!(flag & FWRITE))
1104 err = ugen_set_config(sc, *(int *)addr);
1106 case USBD_NORMAL_COMPLETION:
1114 case USB_GET_ALTINTERFACE:
1115 ai = (struct usb_alt_interface *)addr;
1116 err = usbd_device2interface_handle(sc->sc_udev,
1117 ai->uai_interface_index, &iface);
1120 idesc = usbd_get_interface_descriptor(iface);
1123 ai->uai_alt_no = idesc->bAlternateSetting;
1125 case USB_SET_ALTINTERFACE:
1126 if (!(flag & FWRITE))
1128 ai = (struct usb_alt_interface *)addr;
1129 err = usbd_device2interface_handle(sc->sc_udev,
1130 ai->uai_interface_index, &iface);
1133 err = ugen_set_interface(sc, ai->uai_interface_index, ai->uai_alt_no);
1137 case USB_GET_NO_ALT:
1138 ai = (struct usb_alt_interface *)addr;
1139 cdesc = ugen_get_cdesc(sc, ai->uai_config_index, 0);
1142 idesc = usbd_find_idesc(cdesc, ai->uai_interface_index, 0);
1143 if (idesc == NULL) {
1144 free(cdesc, M_TEMP);
1147 ai->uai_alt_no = usbd_get_no_alts(cdesc, idesc->bInterfaceNumber);
1148 free(cdesc, M_TEMP);
1150 case USB_GET_DEVICE_DESC:
1151 *(usb_device_descriptor_t *)addr =
1152 *usbd_get_device_descriptor(sc->sc_udev);
1154 case USB_GET_CONFIG_DESC:
1155 cd = (struct usb_config_desc *)addr;
1156 cdesc = ugen_get_cdesc(sc, cd->ucd_config_index, 0);
1159 cd->ucd_desc = *cdesc;
1160 free(cdesc, M_TEMP);
1162 case USB_GET_INTERFACE_DESC:
1163 id = (struct usb_interface_desc *)addr;
1164 cdesc = ugen_get_cdesc(sc, id->uid_config_index, 0);
1167 if (id->uid_config_index == USB_CURRENT_CONFIG_INDEX &&
1168 id->uid_alt_index == USB_CURRENT_ALT_INDEX)
1169 alt = ugen_get_alt_index(sc, id->uid_interface_index);
1171 alt = id->uid_alt_index;
1172 idesc = usbd_find_idesc(cdesc, id->uid_interface_index, alt);
1173 if (idesc == NULL) {
1174 free(cdesc, M_TEMP);
1177 id->uid_desc = *idesc;
1178 free(cdesc, M_TEMP);
1180 case USB_GET_ENDPOINT_DESC:
1181 ed = (struct usb_endpoint_desc *)addr;
1182 cdesc = ugen_get_cdesc(sc, ed->ued_config_index, 0);
1185 if (ed->ued_config_index == USB_CURRENT_CONFIG_INDEX &&
1186 ed->ued_alt_index == USB_CURRENT_ALT_INDEX)
1187 alt = ugen_get_alt_index(sc, ed->ued_interface_index);
1189 alt = ed->ued_alt_index;
1190 edesc = usbd_find_edesc(cdesc, ed->ued_interface_index,
1191 alt, ed->ued_endpoint_index);
1192 if (edesc == NULL) {
1193 free(cdesc, M_TEMP);
1196 ed->ued_desc = *edesc;
1197 free(cdesc, M_TEMP);
1199 case USB_GET_FULL_DESC:
1204 struct usb_full_desc *fd = (struct usb_full_desc *)addr;
1207 cdesc = ugen_get_cdesc(sc, fd->ufd_config_index, &len);
1208 if (len > fd->ufd_size)
1210 iov.iov_base = (caddr_t)fd->ufd_data;
1214 uio.uio_resid = len;
1216 uio.uio_segflg = UIO_USERSPACE;
1217 uio.uio_rw = UIO_READ;
1219 #if defined(__NetBSD__) || defined(__OpenBSD__)
1220 error = uiomove((caddr_t)cdesc, len, &uio);
1221 #elif defined(__FreeBSD__)
1222 error = uiomove((void *)cdesc, len, &uio);
1224 free(cdesc, M_TEMP);
1227 case USB_GET_STRING_DESC:
1228 si = (struct usb_string_desc *)addr;
1229 err = usbd_get_string_desc(sc->sc_udev, si->usd_string_index,
1230 si->usd_language_id, &si->usd_desc);
1234 case USB_DO_REQUEST:
1236 struct usb_ctl_request *ur = (void *)addr;
1237 int len = UGETW(ur->ucr_request.wLength);
1244 if (!(flag & FWRITE))
1246 /* Avoid requests that would damage the bus integrity. */
1247 if ((ur->ucr_request.bmRequestType == UT_WRITE_DEVICE &&
1248 ur->ucr_request.bRequest == UR_SET_ADDRESS) ||
1249 (ur->ucr_request.bmRequestType == UT_WRITE_DEVICE &&
1250 ur->ucr_request.bRequest == UR_SET_CONFIG) ||
1251 (ur->ucr_request.bmRequestType == UT_WRITE_INTERFACE &&
1252 ur->ucr_request.bRequest == UR_SET_INTERFACE))
1255 if (len < 0 || len > 32767)
1258 iov.iov_base = (caddr_t)ur->ucr_data;
1262 uio.uio_resid = len;
1264 uio.uio_segflg = UIO_USERSPACE;
1266 ur->ucr_request.bmRequestType & UT_READ ?
1267 UIO_READ : UIO_WRITE;
1269 ptr = malloc(len, M_TEMP, M_WAITOK);
1270 if (uio.uio_rw == UIO_WRITE) {
1271 error = uiomove(ptr, len, &uio);
1276 err = usbd_do_request_flags(sc->sc_udev, &ur->ucr_request,
1277 ptr, ur->ucr_flags, &ur->ucr_actlen);
1283 if (uio.uio_rw == UIO_READ) {
1284 error = uiomove(ptr, len, &uio);
1294 case USB_GET_DEVICEINFO:
1295 usbd_fill_deviceinfo(sc->sc_udev,
1296 (struct usb_device_info *)addr);
1305 ugenioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, usb_proc_ptr p)
1307 int endpt = UGENENDPOINT(dev);
1308 struct ugen_softc *sc;
1311 USB_GET_SC(ugen, UGENUNIT(dev), sc);
1314 error = ugen_do_ioctl(sc, endpt, cmd, addr, flag, p);
1315 if (--sc->sc_refcnt < 0)
1316 usb_detach_wakeup(USBDEV(sc->sc_dev));
1321 ugenpoll(dev_t dev, int events, usb_proc_ptr p)
1323 struct ugen_softc *sc;
1324 struct ugen_endpoint *sce;
1328 USB_GET_SC(ugen, UGENUNIT(dev), sc);
1334 sce = &sc->sc_endpoints[UGENENDPOINT(dev)][IN];
1339 printf("ugenpoll: no edesc\n");
1343 printf("ugenpoll: no pipe\n");
1348 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
1350 if (events & (POLLIN | POLLRDNORM)) {
1351 if (sce->q.c_cc > 0)
1352 revents |= events & (POLLIN | POLLRDNORM);
1354 selrecord(p, &sce->rsel);
1357 case UE_ISOCHRONOUS:
1358 if (events & (POLLIN | POLLRDNORM)) {
1359 if (sce->cur != sce->fill)
1360 revents |= events & (POLLIN | POLLRDNORM);
1362 selrecord(p, &sce->rsel);
1367 * We have no easy way of determining if a read will
1368 * yield any data or a write will happen.
1369 * Pretend they will.
1372 (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM);
1381 #if defined(__FreeBSD__)
1382 DRIVER_MODULE(ugen, uhub, ugen_driver, ugen_devclass, usbd_driver_load, 0);