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.2 2003/06/17 04:28:32 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 /* close */ ugenclose,
142 /* write */ ugenwrite,
143 /* ioctl */ ugenioctl,
146 /* strategy */ nostrategy,
148 /* maj */ UGEN_CDEV_MAJOR,
156 Static void ugenintr(usbd_xfer_handle xfer, usbd_private_handle addr,
158 Static void ugen_isoc_rintr(usbd_xfer_handle xfer, usbd_private_handle addr,
160 Static int ugen_do_read(struct ugen_softc *, int, struct uio *, int);
161 Static int ugen_do_write(struct ugen_softc *, int, struct uio *, int);
162 Static int ugen_do_ioctl(struct ugen_softc *, int, u_long,
163 caddr_t, int, usb_proc_ptr);
164 Static int ugen_set_config(struct ugen_softc *sc, int configno);
165 Static usb_config_descriptor_t *ugen_get_cdesc(struct ugen_softc *sc,
166 int index, int *lenp);
167 Static usbd_status ugen_set_interface(struct ugen_softc *, int, int);
168 Static int ugen_get_alt_index(struct ugen_softc *sc, int ifaceidx);
170 #define UGENUNIT(n) ((minor(n) >> 4) & 0xf)
171 #define UGENENDPOINT(n) (minor(n) & 0xf)
172 #define UGENMINOR(u, e) (((u) << 4) | (e))
174 USB_DECLARE_DRIVER(ugen);
178 USB_MATCH_START(ugen, uaa);
181 return (UMATCH_GENERIC);
183 return (UMATCH_NONE);
188 USB_ATTACH_START(ugen, sc, uaa);
189 usbd_device_handle udev;
194 usbd_devinfo(uaa->device, 0, devinfo);
196 printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
198 sc->sc_udev = udev = uaa->device;
200 memset(sc->sc_endpoints, 0, sizeof sc->sc_endpoints);
202 /* First set configuration index 0, the default one for ugen. */
203 err = usbd_set_config_index(udev, 0, 0);
205 printf("%s: setting configuration index 0 failed\n",
206 USBDEVNAME(sc->sc_dev));
208 USB_ATTACH_ERROR_RETURN;
210 conf = usbd_get_config_descriptor(udev)->bConfigurationValue;
212 /* Set up all the local state for this configuration. */
213 err = ugen_set_config(sc, conf);
215 printf("%s: setting configuration %d failed\n",
216 USBDEVNAME(sc->sc_dev), conf);
218 USB_ATTACH_ERROR_RETURN;
221 USB_ATTACH_SUCCESS_RETURN;
225 ugen_set_config(struct ugen_softc *sc, int configno)
227 usbd_device_handle dev = sc->sc_udev;
228 usbd_interface_handle iface;
229 usb_endpoint_descriptor_t *ed;
230 struct ugen_endpoint *sce;
231 u_int8_t niface, nendpt;
232 int ifaceno, endptno, endpt;
236 DPRINTFN(1,("ugen_set_config: %s to configno %d, sc=%p\n",
237 USBDEVNAME(sc->sc_dev), configno, sc));
239 /* We start at 1, not 0, because we don't care whether the
240 * control endpoint is open or not. It is always present.
242 for (endptno = 1; endptno < USB_MAX_ENDPOINTS; endptno++)
243 if (sc->sc_is_open[endptno]) {
245 ("ugen_set_config: %s - endpoint %d is open\n",
246 USBDEVNAME(sc->sc_dev), endptno));
247 return (USBD_IN_USE);
250 if (usbd_get_config_descriptor(dev)->bConfigurationValue != configno) {
251 /* Avoid setting the current value. */
252 err = usbd_set_config_no(dev, configno, 0);
257 err = usbd_interface_count(dev, &niface);
260 memset(sc->sc_endpoints, 0, sizeof sc->sc_endpoints);
261 for (ifaceno = 0; ifaceno < niface; ifaceno++) {
262 DPRINTFN(1,("ugen_set_config: ifaceno %d\n", ifaceno));
263 err = usbd_device2interface_handle(dev, ifaceno, &iface);
266 err = usbd_endpoint_count(iface, &nendpt);
269 for (endptno = 0; endptno < nendpt; endptno++) {
270 ed = usbd_interface2endpoint_descriptor(iface,endptno);
271 endpt = UE_GET_ADDR(ed->bEndpointAddress);
272 dir = UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN?
275 sce = &sc->sc_endpoints[endpt][dir];
276 DPRINTFN(1,("ugen_set_config: endptno %d, endpt=0x%02x"
278 endptno, endpt, endpt, dir, sce));
287 #if defined(__FreeBSD__)
288 /* the main device, ctrl endpoint */
289 make_dev(&ugen_cdevsw, UGENMINOR(USBDEVUNIT(sc->sc_dev), 0),
290 UID_ROOT, GID_OPERATOR, 0644, "%s", USBDEVNAME(sc->sc_dev));
292 for (endptno = 1; endptno < USB_MAX_ENDPOINTS; endptno++) {
293 if (sc->sc_endpoints[endptno][IN].sc != NULL ||
294 sc->sc_endpoints[endptno][OUT].sc != NULL ) {
295 /* endpt can be 0x81 and 0x01, representing
296 * endpoint address 0x01 and IN/OUT directions.
297 * We map both endpts to the same device,
298 * IN is reading from it, OUT is writing to it.
300 * In the if clause above we check whether one
301 * of the structs is populated.
303 make_dev(&ugen_cdevsw,
304 UGENMINOR(USBDEVUNIT(sc->sc_dev), endptno),
305 UID_ROOT, GID_OPERATOR, 0644,
307 USBDEVNAME(sc->sc_dev), endptno);
312 return (USBD_NORMAL_COMPLETION);
316 ugenopen(dev_t dev, int flag, int mode, usb_proc_ptr p)
318 struct ugen_softc *sc;
319 int unit = UGENUNIT(dev);
320 int endpt = UGENENDPOINT(dev);
321 usb_endpoint_descriptor_t *edesc;
322 struct ugen_endpoint *sce;
325 usbd_xfer_handle xfer;
329 USB_GET_SC_OPEN(ugen, unit, sc);
331 DPRINTFN(5, ("ugenopen: flag=%d, mode=%d, unit=%d endpt=%d\n",
332 flag, mode, unit, endpt));
334 if (sc == NULL || sc->sc_dying)
337 if (sc->sc_is_open[endpt])
340 if (endpt == USB_CONTROL_ENDPOINT) {
341 sc->sc_is_open[USB_CONTROL_ENDPOINT] = 1;
345 /* Make sure there are pipes for all directions. */
346 for (dir = OUT; dir <= IN; dir++) {
347 if (flag & (dir == OUT ? FWRITE : FREAD)) {
348 sce = &sc->sc_endpoints[endpt][dir];
349 if (sce == 0 || sce->edesc == 0)
354 /* Actually open the pipes. */
355 /* XXX Should back out properly if it fails. */
356 for (dir = OUT; dir <= IN; dir++) {
357 if (!(flag & (dir == OUT ? FWRITE : FREAD)))
359 sce = &sc->sc_endpoints[endpt][dir];
361 sce->timeout = USBD_NO_TIMEOUT;
362 DPRINTFN(5, ("ugenopen: sc=%p, endpt=%d, dir=%d, sce=%p\n",
363 sc, endpt, dir, sce));
365 switch (edesc->bmAttributes & UE_XFERTYPE) {
367 isize = UGETW(edesc->wMaxPacketSize);
368 if (isize == 0) /* shouldn't happen */
370 sce->ibuf = malloc(isize, M_USBDEV, M_WAITOK);
371 DPRINTFN(5, ("ugenopen: intr endpt=%d,isize=%d\n",
373 if (clalloc(&sce->q, UGEN_IBSIZE, 0) == -1)
375 err = usbd_open_pipe_intr(sce->iface,
376 edesc->bEndpointAddress,
377 USBD_SHORT_XFER_OK, &sce->pipeh, sce,
378 sce->ibuf, isize, ugenintr,
379 USBD_DEFAULT_INTERVAL);
381 free(sce->ibuf, M_USBDEV);
385 DPRINTFN(5, ("ugenopen: interrupt open done\n"));
388 err = usbd_open_pipe(sce->iface,
389 edesc->bEndpointAddress, 0, &sce->pipeh);
396 isize = UGETW(edesc->wMaxPacketSize);
397 if (isize == 0) /* shouldn't happen */
399 sce->ibuf = malloc(isize * UGEN_NISOFRAMES,
401 sce->cur = sce->fill = sce->ibuf;
402 sce->limit = sce->ibuf + isize * UGEN_NISOFRAMES;
403 DPRINTFN(5, ("ugenopen: isoc endpt=%d, isize=%d\n",
405 err = usbd_open_pipe(sce->iface,
406 edesc->bEndpointAddress, 0, &sce->pipeh);
408 free(sce->ibuf, M_USBDEV);
411 for(i = 0; i < UGEN_NISOREQS; ++i) {
412 sce->isoreqs[i].sce = sce;
413 xfer = usbd_alloc_xfer(sc->sc_udev);
416 sce->isoreqs[i].xfer = xfer;
417 buf = usbd_alloc_buffer
418 (xfer, isize * UGEN_NISORFRMS);
423 sce->isoreqs[i].dmabuf = buf;
424 for(j = 0; j < UGEN_NISORFRMS; ++j)
425 sce->isoreqs[i].sizes[j] = isize;
427 (xfer, sce->pipeh, &sce->isoreqs[i],
428 sce->isoreqs[i].sizes,
429 UGEN_NISORFRMS, USBD_NO_COPY,
431 (void)usbd_transfer(xfer);
433 DPRINTFN(5, ("ugenopen: isoc open done\n"));
436 while (--i >= 0) /* implicit buffer free */
437 usbd_free_xfer(sce->isoreqs[i].xfer);
443 sc->sc_is_open[endpt] = 1;
448 ugenclose(dev_t dev, int flag, int mode, usb_proc_ptr p)
450 int endpt = UGENENDPOINT(dev);
451 struct ugen_softc *sc;
452 struct ugen_endpoint *sce;
456 USB_GET_SC(ugen, UGENUNIT(dev), sc);
458 DPRINTFN(5, ("ugenclose: flag=%d, mode=%d, unit=%d, endpt=%d\n",
459 flag, mode, UGENUNIT(dev), endpt));
462 if (!sc->sc_is_open[endpt]) {
463 printf("ugenclose: not open\n");
468 if (endpt == USB_CONTROL_ENDPOINT) {
469 DPRINTFN(5, ("ugenclose: close control\n"));
470 sc->sc_is_open[endpt] = 0;
474 for (dir = OUT; dir <= IN; dir++) {
475 if (!(flag & (dir == OUT ? FWRITE : FREAD)))
477 sce = &sc->sc_endpoints[endpt][dir];
478 if (sce == NULL || sce->pipeh == NULL)
480 DPRINTFN(5, ("ugenclose: endpt=%d dir=%d sce=%p\n",
483 usbd_abort_pipe(sce->pipeh);
484 usbd_close_pipe(sce->pipeh);
487 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
489 ndflush(&sce->q, sce->q.c_cc);
493 for (i = 0; i < UGEN_NISOREQS; ++i)
494 usbd_free_xfer(sce->isoreqs[i].xfer);
499 if (sce->ibuf != NULL) {
500 free(sce->ibuf, M_USBDEV);
504 sc->sc_is_open[endpt] = 0;
510 ugen_do_read(struct ugen_softc *sc, int endpt, struct uio *uio, int flag)
512 struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][IN];
514 char buf[UGEN_BBSIZE];
515 usbd_xfer_handle xfer;
519 u_char buffer[UGEN_CHUNK];
521 DPRINTFN(5, ("%s: ugenread: %d\n", USBDEVNAME(sc->sc_dev), endpt));
526 if (endpt == USB_CONTROL_ENDPOINT)
530 if (sce->edesc == NULL) {
531 printf("ugenread: no edesc\n");
534 if (sce->pipeh == NULL) {
535 printf("ugenread: no pipe\n");
540 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
542 /* Block until activity occured. */
544 while (sce->q.c_cc == 0) {
545 if (flag & IO_NDELAY) {
547 return (EWOULDBLOCK);
549 sce->state |= UGEN_ASLP;
550 DPRINTFN(5, ("ugenread: sleep on %p\n", sc));
551 error = tsleep(sce, PZERO | PCATCH, "ugenri", 0);
552 DPRINTFN(5, ("ugenread: woke, error=%d\n", error));
556 sce->state &= ~UGEN_ASLP;
562 /* Transfer as many chunks as possible. */
563 while (sce->q.c_cc > 0 && uio->uio_resid > 0 && !error) {
564 n = min(sce->q.c_cc, uio->uio_resid);
565 if (n > sizeof(buffer))
568 /* Remove a small chunk from the input queue. */
569 q_to_b(&sce->q, buffer, n);
570 DPRINTFN(5, ("ugenread: got %d chars\n", n));
572 /* Copy the data to the user process. */
573 error = uiomove(buffer, n, uio);
579 xfer = usbd_alloc_xfer(sc->sc_udev);
582 while ((n = min(UGEN_BBSIZE, uio->uio_resid)) != 0) {
583 DPRINTFN(1, ("ugenread: start transfer %d bytes\n",n));
585 err = usbd_bulk_transfer(
587 sce->state & UGEN_SHORT_OK ?
588 USBD_SHORT_XFER_OK : 0,
589 sce->timeout, buf, &tn, "ugenrb");
591 if (err == USBD_INTERRUPTED)
593 else if (err == USBD_TIMEOUT)
599 DPRINTFN(1, ("ugenread: got %d bytes\n", tn));
600 error = uiomove(buf, tn, uio);
604 usbd_free_xfer(xfer);
608 while (sce->cur == sce->fill) {
609 if (flag & IO_NDELAY) {
611 return (EWOULDBLOCK);
613 sce->state |= UGEN_ASLP;
614 DPRINTFN(5, ("ugenread: sleep on %p\n", sc));
615 error = tsleep(sce, PZERO | PCATCH, "ugenri", 0);
616 DPRINTFN(5, ("ugenread: woke, error=%d\n", error));
620 sce->state &= ~UGEN_ASLP;
625 while (sce->cur != sce->fill && uio->uio_resid > 0 && !error) {
626 if(sce->fill > sce->cur)
627 n = min(sce->fill - sce->cur, uio->uio_resid);
629 n = min(sce->limit - sce->cur, uio->uio_resid);
631 DPRINTFN(5, ("ugenread: isoc got %d chars\n", n));
633 /* Copy the data to the user process. */
634 error = uiomove(sce->cur, n, uio);
638 if(sce->cur >= sce->limit)
639 sce->cur = sce->ibuf;
652 ugenread(dev_t dev, struct uio *uio, int flag)
654 int endpt = UGENENDPOINT(dev);
655 struct ugen_softc *sc;
658 USB_GET_SC(ugen, UGENUNIT(dev), sc);
661 error = ugen_do_read(sc, endpt, uio, flag);
662 if (--sc->sc_refcnt < 0)
663 usb_detach_wakeup(USBDEV(sc->sc_dev));
668 ugen_do_write(struct ugen_softc *sc, int endpt, struct uio *uio, int flag)
670 struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][OUT];
673 char buf[UGEN_BBSIZE];
674 usbd_xfer_handle xfer;
677 DPRINTFN(5, ("%s: ugen_do_write: %d\n", USBDEVNAME(sc->sc_dev), endpt));
682 if (endpt == USB_CONTROL_ENDPOINT)
686 if (sce->edesc == NULL) {
687 printf("ugen_do_write: no edesc\n");
690 if (sce->pipeh == NULL) {
691 printf("ugen_do_write: no pipe\n");
696 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
698 xfer = usbd_alloc_xfer(sc->sc_udev);
701 while ((n = min(UGEN_BBSIZE, uio->uio_resid)) != 0) {
702 error = uiomove(buf, n, uio);
705 DPRINTFN(1, ("ugen_do_write: transfer %d bytes\n", n));
706 err = usbd_bulk_transfer(xfer, sce->pipeh, 0,
707 sce->timeout, buf, &n,"ugenwb");
709 if (err == USBD_INTERRUPTED)
716 usbd_free_xfer(xfer);
725 ugenwrite(dev_t dev, struct uio *uio, int flag)
727 int endpt = UGENENDPOINT(dev);
728 struct ugen_softc *sc;
731 USB_GET_SC(ugen, UGENUNIT(dev), sc);
734 error = ugen_do_write(sc, endpt, uio, flag);
735 if (--sc->sc_refcnt < 0)
736 usb_detach_wakeup(USBDEV(sc->sc_dev));
740 #if defined(__NetBSD__) || defined(__OpenBSD__)
742 ugen_activate(device_ptr_t self, enum devact act)
744 struct ugen_softc *sc = (struct ugen_softc *)self;
751 case DVACT_DEACTIVATE:
761 USB_DETACH_START(ugen, sc);
762 struct ugen_endpoint *sce;
765 #if defined(__NetBSD__) || defined(__OpenBSD__)
767 #elif defined(__FreeBSD__)
773 #if defined(__NetBSD__) || defined(__OpenBSD__)
774 DPRINTF(("ugen_detach: sc=%p flags=%d\n", sc, flags));
775 #elif defined(__FreeBSD__)
776 DPRINTF(("ugen_detach: sc=%p\n", sc));
780 /* Abort all pipes. Causes processes waiting for transfer to wake. */
781 for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
782 for (dir = OUT; dir <= IN; dir++) {
783 sce = &sc->sc_endpoints[i][dir];
784 if (sce && sce->pipeh)
785 usbd_abort_pipe(sce->pipeh);
790 if (--sc->sc_refcnt >= 0) {
792 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
793 wakeup(&sc->sc_endpoints[i][IN]);
794 /* Wait for processes to go away. */
795 usb_detach_wait(USBDEV(sc->sc_dev));
799 #if defined(__NetBSD__) || defined(__OpenBSD__)
800 /* locate the major number */
801 for (maj = 0; maj < nchrdev; maj++)
802 if (cdevsw[maj].d_open == ugenopen)
805 /* Nuke the vnodes for any open instances (calls close). */
806 mn = self->dv_unit * USB_MAX_ENDPOINTS;
807 vdevgone(maj, mn, mn + USB_MAX_ENDPOINTS - 1, VCHR);
808 #elif defined(__FreeBSD__)
809 /* destroy the device for the control endpoint */
810 dev = makedev(UGEN_CDEV_MAJOR, UGENMINOR(USBDEVUNIT(sc->sc_dev), 0));
811 vp = SLIST_FIRST(&dev->si_hlist);
813 VOP_REVOKE(vp, REVOKEALL);
816 /* destroy all devices for the other (existing) endpoints as well */
817 for (endptno = 1; endptno < USB_MAX_ENDPOINTS; endptno++) {
818 if (sc->sc_endpoints[endptno][IN].sc != NULL ||
819 sc->sc_endpoints[endptno][OUT].sc != NULL ) {
820 /* endpt can be 0x81 and 0x01, representing
821 * endpoint address 0x01 and IN/OUT directions.
822 * We map both endpoint addresses to the same device,
823 * IN is reading from it, OUT is writing to it.
825 * In the if clause above we check whether one
826 * of the structs is populated.
828 dev = makedev(UGEN_CDEV_MAJOR,
829 UGENMINOR(USBDEVUNIT(sc->sc_dev), endptno));
830 vp = SLIST_FIRST(&dev->si_hlist);
832 VOP_REVOKE(vp, REVOKEALL);
843 ugenintr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
845 struct ugen_endpoint *sce = addr;
846 /*struct ugen_softc *sc = sce->sc;*/
850 if (status == USBD_CANCELLED)
853 if (status != USBD_NORMAL_COMPLETION) {
854 DPRINTF(("ugenintr: status=%d\n", status));
855 usbd_clear_endpoint_stall_async(sce->pipeh);
859 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
862 DPRINTFN(5, ("ugenintr: xfer=%p status=%d count=%d\n",
863 xfer, status, count));
864 DPRINTFN(5, (" data = %02x %02x %02x\n",
865 ibuf[0], ibuf[1], ibuf[2]));
867 (void)b_to_q(ibuf, count, &sce->q);
869 if (sce->state & UGEN_ASLP) {
870 sce->state &= ~UGEN_ASLP;
871 DPRINTFN(5, ("ugen_intr: waking %p\n", sce));
874 selwakeup(&sce->rsel);
878 ugen_isoc_rintr(usbd_xfer_handle xfer, usbd_private_handle addr,
881 struct isoreq *req = addr;
882 struct ugen_endpoint *sce = req->sce;
885 /* Return if we are aborting. */
886 if (status == USBD_CANCELLED)
889 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
890 DPRINTFN(5,("ugen_isoc_rintr: xfer %d, count=%d\n", req - sce->isoreqs,
893 /* throw away oldest input if the buffer is full */
894 if(sce->fill < sce->cur && sce->cur <= sce->fill + count) {
896 if(sce->cur >= sce->limit)
897 sce->cur = sce->ibuf + (sce->limit - sce->cur);
898 DPRINTFN(5, ("ugen_isoc_rintr: throwing away %d bytes\n",
902 /* copy data to buffer */
904 n = min(count, sce->limit - sce->fill);
905 memcpy(sce->fill, req->dmabuf, n);
909 if(sce->fill == sce->limit)
910 sce->fill = sce->ibuf;
913 usbd_setup_isoc_xfer(xfer, sce->pipeh, req, req->sizes, UGEN_NISORFRMS,
914 USBD_NO_COPY, ugen_isoc_rintr);
915 (void)usbd_transfer(xfer);
917 if (sce->state & UGEN_ASLP) {
918 sce->state &= ~UGEN_ASLP;
919 DPRINTFN(5, ("ugen_isoc_rintr: waking %p\n", sce));
922 selwakeup(&sce->rsel);
926 ugen_set_interface(struct ugen_softc *sc, int ifaceidx, int altno)
928 usbd_interface_handle iface;
929 usb_endpoint_descriptor_t *ed;
931 struct ugen_endpoint *sce;
932 u_int8_t niface, nendpt, endptno, endpt;
935 DPRINTFN(15, ("ugen_set_interface %d %d\n", ifaceidx, altno));
937 err = usbd_interface_count(sc->sc_udev, &niface);
940 if (ifaceidx < 0 || ifaceidx >= niface)
943 err = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface);
946 err = usbd_endpoint_count(iface, &nendpt);
949 for (endptno = 0; endptno < nendpt; endptno++) {
950 ed = usbd_interface2endpoint_descriptor(iface,endptno);
951 endpt = ed->bEndpointAddress;
952 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
953 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
960 err = usbd_set_interface(iface, altno);
964 err = usbd_endpoint_count(iface, &nendpt);
967 for (endptno = 0; endptno < nendpt; endptno++) {
968 ed = usbd_interface2endpoint_descriptor(iface,endptno);
969 endpt = ed->bEndpointAddress;
970 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
971 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
979 /* Retrieve a complete descriptor for a certain device and index. */
980 Static usb_config_descriptor_t *
981 ugen_get_cdesc(struct ugen_softc *sc, int index, int *lenp)
983 usb_config_descriptor_t *cdesc, *tdesc, cdescr;
987 if (index == USB_CURRENT_CONFIG_INDEX) {
988 tdesc = usbd_get_config_descriptor(sc->sc_udev);
989 len = UGETW(tdesc->wTotalLength);
992 cdesc = malloc(len, M_TEMP, M_WAITOK);
993 memcpy(cdesc, tdesc, len);
994 DPRINTFN(5,("ugen_get_cdesc: current, len=%d\n", len));
996 err = usbd_get_config_desc(sc->sc_udev, index, &cdescr);
999 len = UGETW(cdescr.wTotalLength);
1000 DPRINTFN(5,("ugen_get_cdesc: index=%d, len=%d\n", index, len));
1003 cdesc = malloc(len, M_TEMP, M_WAITOK);
1004 err = usbd_get_config_desc_full(sc->sc_udev, index, cdesc, len);
1006 free(cdesc, M_TEMP);
1014 ugen_get_alt_index(struct ugen_softc *sc, int ifaceidx)
1016 usbd_interface_handle iface;
1019 err = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface);
1022 return (usbd_get_interface_altindex(iface));
1026 ugen_do_ioctl(struct ugen_softc *sc, int endpt, u_long cmd,
1027 caddr_t addr, int flag, usb_proc_ptr p)
1029 struct ugen_endpoint *sce;
1031 usbd_interface_handle iface;
1032 struct usb_config_desc *cd;
1033 usb_config_descriptor_t *cdesc;
1034 struct usb_interface_desc *id;
1035 usb_interface_descriptor_t *idesc;
1036 struct usb_endpoint_desc *ed;
1037 usb_endpoint_descriptor_t *edesc;
1038 struct usb_alt_interface *ai;
1039 struct usb_string_desc *si;
1042 DPRINTFN(5, ("ugenioctl: cmd=%08lx\n", cmd));
1048 /* All handled in the upper FS layer. */
1050 case USB_SET_SHORT_XFER:
1051 /* This flag only affects read */
1052 if (endpt == USB_CONTROL_ENDPOINT)
1054 sce = &sc->sc_endpoints[endpt][IN];
1058 if (sce->pipeh == NULL) {
1059 printf("ugenioctl: USB_SET_SHORT_XFER, no pipe\n");
1064 sce->state |= UGEN_SHORT_OK;
1066 sce->state &= ~UGEN_SHORT_OK;
1068 case USB_SET_TIMEOUT:
1069 sce = &sc->sc_endpoints[endpt][IN];
1073 if (sce->pipeh == NULL) {
1074 printf("ugenioctl: USB_SET_TIMEOUT, no pipe\n");
1078 sce->timeout = *(int *)addr;
1084 if (endpt != USB_CONTROL_ENDPOINT)
1090 ugendebug = *(int *)addr;
1093 case USB_GET_CONFIG:
1094 err = usbd_get_config(sc->sc_udev, &conf);
1097 *(int *)addr = conf;
1099 case USB_SET_CONFIG:
1100 if (!(flag & FWRITE))
1102 err = ugen_set_config(sc, *(int *)addr);
1104 case USBD_NORMAL_COMPLETION:
1112 case USB_GET_ALTINTERFACE:
1113 ai = (struct usb_alt_interface *)addr;
1114 err = usbd_device2interface_handle(sc->sc_udev,
1115 ai->uai_interface_index, &iface);
1118 idesc = usbd_get_interface_descriptor(iface);
1121 ai->uai_alt_no = idesc->bAlternateSetting;
1123 case USB_SET_ALTINTERFACE:
1124 if (!(flag & FWRITE))
1126 ai = (struct usb_alt_interface *)addr;
1127 err = usbd_device2interface_handle(sc->sc_udev,
1128 ai->uai_interface_index, &iface);
1131 err = ugen_set_interface(sc, ai->uai_interface_index, ai->uai_alt_no);
1135 case USB_GET_NO_ALT:
1136 ai = (struct usb_alt_interface *)addr;
1137 cdesc = ugen_get_cdesc(sc, ai->uai_config_index, 0);
1140 idesc = usbd_find_idesc(cdesc, ai->uai_interface_index, 0);
1141 if (idesc == NULL) {
1142 free(cdesc, M_TEMP);
1145 ai->uai_alt_no = usbd_get_no_alts(cdesc, idesc->bInterfaceNumber);
1146 free(cdesc, M_TEMP);
1148 case USB_GET_DEVICE_DESC:
1149 *(usb_device_descriptor_t *)addr =
1150 *usbd_get_device_descriptor(sc->sc_udev);
1152 case USB_GET_CONFIG_DESC:
1153 cd = (struct usb_config_desc *)addr;
1154 cdesc = ugen_get_cdesc(sc, cd->ucd_config_index, 0);
1157 cd->ucd_desc = *cdesc;
1158 free(cdesc, M_TEMP);
1160 case USB_GET_INTERFACE_DESC:
1161 id = (struct usb_interface_desc *)addr;
1162 cdesc = ugen_get_cdesc(sc, id->uid_config_index, 0);
1165 if (id->uid_config_index == USB_CURRENT_CONFIG_INDEX &&
1166 id->uid_alt_index == USB_CURRENT_ALT_INDEX)
1167 alt = ugen_get_alt_index(sc, id->uid_interface_index);
1169 alt = id->uid_alt_index;
1170 idesc = usbd_find_idesc(cdesc, id->uid_interface_index, alt);
1171 if (idesc == NULL) {
1172 free(cdesc, M_TEMP);
1175 id->uid_desc = *idesc;
1176 free(cdesc, M_TEMP);
1178 case USB_GET_ENDPOINT_DESC:
1179 ed = (struct usb_endpoint_desc *)addr;
1180 cdesc = ugen_get_cdesc(sc, ed->ued_config_index, 0);
1183 if (ed->ued_config_index == USB_CURRENT_CONFIG_INDEX &&
1184 ed->ued_alt_index == USB_CURRENT_ALT_INDEX)
1185 alt = ugen_get_alt_index(sc, ed->ued_interface_index);
1187 alt = ed->ued_alt_index;
1188 edesc = usbd_find_edesc(cdesc, ed->ued_interface_index,
1189 alt, ed->ued_endpoint_index);
1190 if (edesc == NULL) {
1191 free(cdesc, M_TEMP);
1194 ed->ued_desc = *edesc;
1195 free(cdesc, M_TEMP);
1197 case USB_GET_FULL_DESC:
1202 struct usb_full_desc *fd = (struct usb_full_desc *)addr;
1205 cdesc = ugen_get_cdesc(sc, fd->ufd_config_index, &len);
1206 if (len > fd->ufd_size)
1208 iov.iov_base = (caddr_t)fd->ufd_data;
1212 uio.uio_resid = len;
1214 uio.uio_segflg = UIO_USERSPACE;
1215 uio.uio_rw = UIO_READ;
1217 #if defined(__NetBSD__) || defined(__OpenBSD__)
1218 error = uiomove((caddr_t)cdesc, len, &uio);
1219 #elif defined(__FreeBSD__)
1220 error = uiomove((void *)cdesc, len, &uio);
1222 free(cdesc, M_TEMP);
1225 case USB_GET_STRING_DESC:
1226 si = (struct usb_string_desc *)addr;
1227 err = usbd_get_string_desc(sc->sc_udev, si->usd_string_index,
1228 si->usd_language_id, &si->usd_desc);
1232 case USB_DO_REQUEST:
1234 struct usb_ctl_request *ur = (void *)addr;
1235 int len = UGETW(ur->ucr_request.wLength);
1242 if (!(flag & FWRITE))
1244 /* Avoid requests that would damage the bus integrity. */
1245 if ((ur->ucr_request.bmRequestType == UT_WRITE_DEVICE &&
1246 ur->ucr_request.bRequest == UR_SET_ADDRESS) ||
1247 (ur->ucr_request.bmRequestType == UT_WRITE_DEVICE &&
1248 ur->ucr_request.bRequest == UR_SET_CONFIG) ||
1249 (ur->ucr_request.bmRequestType == UT_WRITE_INTERFACE &&
1250 ur->ucr_request.bRequest == UR_SET_INTERFACE))
1253 if (len < 0 || len > 32767)
1256 iov.iov_base = (caddr_t)ur->ucr_data;
1260 uio.uio_resid = len;
1262 uio.uio_segflg = UIO_USERSPACE;
1264 ur->ucr_request.bmRequestType & UT_READ ?
1265 UIO_READ : UIO_WRITE;
1267 ptr = malloc(len, M_TEMP, M_WAITOK);
1268 if (uio.uio_rw == UIO_WRITE) {
1269 error = uiomove(ptr, len, &uio);
1274 err = usbd_do_request_flags(sc->sc_udev, &ur->ucr_request,
1275 ptr, ur->ucr_flags, &ur->ucr_actlen);
1281 if (uio.uio_rw == UIO_READ) {
1282 error = uiomove(ptr, len, &uio);
1292 case USB_GET_DEVICEINFO:
1293 usbd_fill_deviceinfo(sc->sc_udev,
1294 (struct usb_device_info *)addr);
1303 ugenioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, usb_proc_ptr p)
1305 int endpt = UGENENDPOINT(dev);
1306 struct ugen_softc *sc;
1309 USB_GET_SC(ugen, UGENUNIT(dev), sc);
1312 error = ugen_do_ioctl(sc, endpt, cmd, addr, flag, p);
1313 if (--sc->sc_refcnt < 0)
1314 usb_detach_wakeup(USBDEV(sc->sc_dev));
1319 ugenpoll(dev_t dev, int events, usb_proc_ptr p)
1321 struct ugen_softc *sc;
1322 struct ugen_endpoint *sce;
1326 USB_GET_SC(ugen, UGENUNIT(dev), sc);
1332 sce = &sc->sc_endpoints[UGENENDPOINT(dev)][IN];
1337 printf("ugenpoll: no edesc\n");
1341 printf("ugenpoll: no pipe\n");
1346 switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
1348 if (events & (POLLIN | POLLRDNORM)) {
1349 if (sce->q.c_cc > 0)
1350 revents |= events & (POLLIN | POLLRDNORM);
1352 selrecord(p, &sce->rsel);
1355 case UE_ISOCHRONOUS:
1356 if (events & (POLLIN | POLLRDNORM)) {
1357 if (sce->cur != sce->fill)
1358 revents |= events & (POLLIN | POLLRDNORM);
1360 selrecord(p, &sce->rsel);
1365 * We have no easy way of determining if a read will
1366 * yield any data or a write will happen.
1367 * Pretend they will.
1370 (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM);
1379 #if defined(__FreeBSD__)
1380 DRIVER_MODULE(ugen, uhub, ugen_driver, ugen_devclass, usbd_driver_load, 0);