3 * Copyright (c) 2007 Luigi Rizzo - Universita` di Pisa. All rights reserved.
4 * Copyright (c) 2007 Hans Petter Selasky. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/stdint.h>
29 #include <sys/stddef.h>
30 #include <sys/param.h>
31 #include <sys/queue.h>
32 #include <sys/types.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
36 #include <sys/module.h>
38 #include <sys/mutex.h>
39 #include <sys/condvar.h>
40 #include <sys/sysctl.h>
42 #include <sys/unistd.h>
43 #include <sys/callout.h>
44 #include <sys/malloc.h>
47 #include <dev/usb/usb.h>
48 #include <dev/usb/usb_ioctl.h>
49 #include <dev/usb/usbdi.h>
50 #include <dev/usb/usbdi_util.h>
52 #define USB_DEBUG_VAR usb_debug
54 #include <dev/usb/usb_core.h>
55 #include <dev/usb/usb_compat_linux.h>
56 #include <dev/usb/usb_process.h>
57 #include <dev/usb/usb_device.h>
58 #include <dev/usb/usb_util.h>
59 #include <dev/usb/usb_busdma.h>
60 #include <dev/usb/usb_transfer.h>
61 #include <dev/usb/usb_hub.h>
62 #include <dev/usb/usb_request.h>
63 #include <dev/usb/usb_debug.h>
65 struct usb_linux_softc {
66 LIST_ENTRY(usb_linux_softc) sc_attached_list;
69 struct usb_device *sc_fbsd_udev;
70 struct usb_interface *sc_ui;
71 struct usb_driver *sc_udrv;
75 static device_probe_t usb_linux_probe;
76 static device_attach_t usb_linux_attach;
77 static device_detach_t usb_linux_detach;
78 static device_suspend_t usb_linux_suspend;
79 static device_resume_t usb_linux_resume;
81 static usb_callback_t usb_linux_isoc_callback;
82 static usb_callback_t usb_linux_non_isoc_callback;
84 static usb_complete_t usb_linux_wait_complete;
86 static uint16_t usb_max_isoc_frames(struct usb_device *);
87 static int usb_start_wait_urb(struct urb *, usb_timeout_t, uint16_t *);
88 static const struct usb_device_id *usb_linux_lookup_id(
89 const struct usb_device_id *, struct usb_attach_arg *);
90 static struct usb_driver *usb_linux_get_usb_driver(struct usb_linux_softc *);
91 static int usb_linux_create_usb_device(struct usb_device *, device_t);
92 static void usb_linux_cleanup_interface(struct usb_device *,
93 struct usb_interface *);
94 static void usb_linux_complete(struct usb_xfer *);
95 static int usb_unlink_urb_sub(struct urb *, uint8_t);
97 /*------------------------------------------------------------------------*
98 * FreeBSD USB interface
99 *------------------------------------------------------------------------*/
101 static LIST_HEAD(, usb_linux_softc) usb_linux_attached_list;
102 static LIST_HEAD(, usb_driver) usb_linux_driver_list;
104 static device_method_t usb_linux_methods[] = {
105 /* Device interface */
106 DEVMETHOD(device_probe, usb_linux_probe),
107 DEVMETHOD(device_attach, usb_linux_attach),
108 DEVMETHOD(device_detach, usb_linux_detach),
109 DEVMETHOD(device_suspend, usb_linux_suspend),
110 DEVMETHOD(device_resume, usb_linux_resume),
115 static driver_t usb_linux_driver = {
117 .methods = usb_linux_methods,
118 .size = sizeof(struct usb_linux_softc),
121 static devclass_t usb_linux_devclass;
123 DRIVER_MODULE(usb_linux, uhub, usb_linux_driver, usb_linux_devclass, NULL, 0);
124 MODULE_VERSION(usb_linux, 1);
126 /*------------------------------------------------------------------------*
127 * usb_linux_lookup_id
129 * This functions takes an array of "struct usb_device_id" and tries
130 * to match the entries with the information in "struct usb_attach_arg".
131 * If it finds a match the matching entry will be returned.
132 * Else "NULL" will be returned.
133 *------------------------------------------------------------------------*/
134 static const struct usb_device_id *
135 usb_linux_lookup_id(const struct usb_device_id *id, struct usb_attach_arg *uaa)
141 * Keep on matching array entries until we find one with
142 * "match_flags" equal to zero, which indicates the end of the
145 for (; id->match_flags; id++) {
147 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
148 (id->idVendor != uaa->info.idVendor)) {
151 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
152 (id->idProduct != uaa->info.idProduct)) {
155 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
156 (id->bcdDevice_lo > uaa->info.bcdDevice)) {
159 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
160 (id->bcdDevice_hi < uaa->info.bcdDevice)) {
163 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
164 (id->bDeviceClass != uaa->info.bDeviceClass)) {
167 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
168 (id->bDeviceSubClass != uaa->info.bDeviceSubClass)) {
171 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
172 (id->bDeviceProtocol != uaa->info.bDeviceProtocol)) {
175 if ((uaa->info.bDeviceClass == 0xFF) &&
176 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
177 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
178 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
179 USB_DEVICE_ID_MATCH_INT_PROTOCOL))) {
182 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
183 (id->bInterfaceClass != uaa->info.bInterfaceClass)) {
186 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
187 (id->bInterfaceSubClass != uaa->info.bInterfaceSubClass)) {
190 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
191 (id->bInterfaceProtocol != uaa->info.bInterfaceProtocol)) {
194 /* we found a match! */
202 /*------------------------------------------------------------------------*
205 * This function is the FreeBSD probe callback. It is called from the
206 * FreeBSD USB stack through the "device_probe_and_attach()" function.
207 *------------------------------------------------------------------------*/
209 usb_linux_probe(device_t dev)
211 struct usb_attach_arg *uaa = device_get_ivars(dev);
212 struct usb_driver *udrv;
215 if (uaa->usb_mode != USB_MODE_HOST) {
219 LIST_FOREACH(udrv, &usb_linux_driver_list, linux_driver_list) {
220 if (usb_linux_lookup_id(udrv->id_table, uaa)) {
230 /*------------------------------------------------------------------------*
231 * usb_linux_get_usb_driver
233 * This function returns the pointer to the "struct usb_driver" where
234 * the Linux USB device driver "struct usb_device_id" match was found.
235 * We apply a lock before reading out the pointer to avoid races.
236 *------------------------------------------------------------------------*/
237 static struct usb_driver *
238 usb_linux_get_usb_driver(struct usb_linux_softc *sc)
240 struct usb_driver *udrv;
248 /*------------------------------------------------------------------------*
251 * This function is the FreeBSD attach callback. It is called from the
252 * FreeBSD USB stack through the "device_probe_and_attach()" function.
253 * This function is called when "usb_linux_probe()" returns zero.
254 *------------------------------------------------------------------------*/
256 usb_linux_attach(device_t dev)
258 struct usb_attach_arg *uaa = device_get_ivars(dev);
259 struct usb_linux_softc *sc = device_get_softc(dev);
260 struct usb_driver *udrv;
261 const struct usb_device_id *id = NULL;
264 LIST_FOREACH(udrv, &usb_linux_driver_list, linux_driver_list) {
265 id = usb_linux_lookup_id(udrv->id_table, uaa);
274 if (usb_linux_create_usb_device(uaa->device, dev) != 0)
276 device_set_usb_desc(dev);
278 sc->sc_fbsd_udev = uaa->device;
279 sc->sc_fbsd_dev = dev;
281 sc->sc_ui = usb_ifnum_to_if(uaa->device, uaa->info.bIfaceNum);
282 if (sc->sc_ui == NULL) {
286 if ((udrv->probe) (sc->sc_ui, id)) {
291 LIST_INSERT_HEAD(&usb_linux_attached_list, sc, sc_attached_list);
298 /*------------------------------------------------------------------------*
301 * This function is the FreeBSD detach callback. It is called from the
302 * FreeBSD USB stack through the "device_detach()" function.
303 *------------------------------------------------------------------------*/
305 usb_linux_detach(device_t dev)
307 struct usb_linux_softc *sc = device_get_softc(dev);
308 struct usb_driver *udrv = NULL;
311 if (sc->sc_attached_list.le_prev) {
312 LIST_REMOVE(sc, sc_attached_list);
313 sc->sc_attached_list.le_prev = NULL;
319 if (udrv && udrv->disconnect) {
320 (udrv->disconnect) (sc->sc_ui);
323 * Make sure that we free all FreeBSD USB transfers belonging to
324 * this Linux "usb_interface", hence they will most likely not be
327 usb_linux_cleanup_interface(sc->sc_fbsd_udev, sc->sc_ui);
331 /*------------------------------------------------------------------------*
334 * This function is the FreeBSD suspend callback. Usually it does nothing.
335 *------------------------------------------------------------------------*/
337 usb_linux_suspend(device_t dev)
339 struct usb_linux_softc *sc = device_get_softc(dev);
340 struct usb_driver *udrv = usb_linux_get_usb_driver(sc);
343 if (udrv && udrv->suspend) {
344 err = (udrv->suspend) (sc->sc_ui, 0);
349 /*------------------------------------------------------------------------*
352 * This function is the FreeBSD resume callback. Usually it does nothing.
353 *------------------------------------------------------------------------*/
355 usb_linux_resume(device_t dev)
357 struct usb_linux_softc *sc = device_get_softc(dev);
358 struct usb_driver *udrv = usb_linux_get_usb_driver(sc);
361 if (udrv && udrv->resume) {
362 err = (udrv->resume) (sc->sc_ui);
367 /*------------------------------------------------------------------------*
368 * Linux emulation layer
369 *------------------------------------------------------------------------*/
371 /*------------------------------------------------------------------------*
372 * usb_max_isoc_frames
374 * The following function returns the maximum number of isochronous
375 * frames that we support per URB. It is not part of the Linux USB API.
376 *------------------------------------------------------------------------*/
378 usb_max_isoc_frames(struct usb_device *dev)
381 switch (usbd_get_speed(dev)) {
384 return (USB_MAX_FULL_SPEED_ISOC_FRAMES);
386 return (USB_MAX_HIGH_SPEED_ISOC_FRAMES);
390 /*------------------------------------------------------------------------*
393 * This function is used to queue an URB after that it has been
394 * initialized. If it returns non-zero, it means that the URB was not
396 *------------------------------------------------------------------------*/
398 usb_submit_urb(struct urb *urb, uint16_t mem_flags)
400 struct usb_host_endpoint *uhe;
407 do_unlock = mtx_owned(&Giant) ? 0 : 1;
411 if (urb->endpoint == NULL) {
417 * Check to see if the urb is in the process of being killed
418 * and stop a urb that is in the process of being killed from
419 * being re-submitted (e.g. from its completion callback
422 if (urb->kill_count != 0) {
430 * Check that we have got a FreeBSD USB transfer that will dequeue
431 * the URB structure and do the real transfer. If there are no USB
432 * transfers, then we return an error.
434 if (uhe->bsd_xfer[0] ||
438 TAILQ_INSERT_TAIL(&uhe->bsd_urb_list, urb, bsd_urb_list);
440 urb->status = -EINPROGRESS;
442 usbd_transfer_start(uhe->bsd_xfer[0]);
443 usbd_transfer_start(uhe->bsd_xfer[1]);
446 /* no pipes have been setup yet! */
447 urb->status = -EINVAL;
456 /*------------------------------------------------------------------------*
459 * This function is used to stop an URB after that it is been
460 * submitted, but before the "complete" callback has been called. On
461 *------------------------------------------------------------------------*/
463 usb_unlink_urb(struct urb *urb)
465 return (usb_unlink_urb_sub(urb, 0));
469 usb_unlink_bsd(struct usb_xfer *xfer,
470 struct urb *urb, uint8_t drain)
474 if (!usbd_transfer_pending(xfer))
476 if (xfer->priv_fifo == (void *)urb) {
479 usbd_transfer_drain(xfer);
482 usbd_transfer_stop(xfer);
484 usbd_transfer_start(xfer);
489 usb_unlink_urb_sub(struct urb *urb, uint8_t drain)
491 struct usb_host_endpoint *uhe;
499 do_unlock = mtx_owned(&Giant) ? 0 : 1;
505 if (urb->endpoint == NULL) {
511 if (urb->bsd_urb_list.tqe_prev) {
513 /* not started yet, just remove it from the queue */
514 TAILQ_REMOVE(&uhe->bsd_urb_list, urb, bsd_urb_list);
515 urb->bsd_urb_list.tqe_prev = NULL;
516 urb->status = -ECONNRESET;
517 urb->actual_length = 0;
519 for (x = 0; x < urb->number_of_packets; x++) {
520 urb->iso_frame_desc[x].actual_length = 0;
524 (urb->complete) (urb);
529 * If the URB is not on the URB list, then check if one of
530 * the FreeBSD USB transfer are processing the current URB.
531 * If so, re-start that transfer, which will lead to the
532 * termination of that URB:
534 usb_unlink_bsd(uhe->bsd_xfer[0], urb, drain);
535 usb_unlink_bsd(uhe->bsd_xfer[1], urb, drain);
546 /*------------------------------------------------------------------------*
549 * This function must always be used to clear the stall. Stall is when
550 * an USB endpoint returns a stall message to the USB host controller.
551 * Until the stall is cleared, no data can be transferred.
552 *------------------------------------------------------------------------*/
554 usb_clear_halt(struct usb_device *dev, struct usb_host_endpoint *uhe)
556 struct usb_config cfg[1];
557 struct usb_endpoint *ep;
564 type = uhe->desc.bmAttributes & UE_XFERTYPE;
565 addr = uhe->desc.bEndpointAddress;
567 memset(cfg, 0, sizeof(cfg));
570 cfg[0].endpoint = addr & UE_ADDR;
571 cfg[0].direction = addr & (UE_DIR_OUT | UE_DIR_IN);
573 ep = usbd_get_endpoint(dev, uhe->bsd_iface_index, cfg);
577 usbd_clear_data_toggle(dev, ep);
579 return (usb_control_msg(dev, &dev->ep0,
580 UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT,
581 UF_ENDPOINT_HALT, addr, NULL, 0, 1000));
584 /*------------------------------------------------------------------------*
587 * This is an internal function that is used to perform synchronous
588 * Linux USB transfers.
589 *------------------------------------------------------------------------*/
591 usb_start_wait_urb(struct urb *urb, usb_timeout_t timeout, uint16_t *p_actlen)
596 /* you must have a timeout! */
600 urb->complete = &usb_linux_wait_complete;
601 urb->timeout = timeout;
602 urb->transfer_flags |= URB_WAIT_WAKEUP;
603 urb->transfer_flags &= ~URB_IS_SLEEPING;
605 do_unlock = mtx_owned(&Giant) ? 0 : 1;
608 err = usb_submit_urb(urb, 0);
613 * the URB might have completed before we get here, so check that by
616 while (urb->transfer_flags & URB_WAIT_WAKEUP) {
617 urb->transfer_flags |= URB_IS_SLEEPING;
618 cv_wait(&urb->cv_wait, &Giant);
619 urb->transfer_flags &= ~URB_IS_SLEEPING;
627 if (p_actlen != NULL) {
631 *p_actlen = urb->actual_length;
636 /*------------------------------------------------------------------------*
639 * The following function performs a control transfer sequence one any
640 * control, bulk or interrupt endpoint, specified by "uhe". A control
641 * transfer means that you transfer an 8-byte header first followed by
642 * a data-phase as indicated by the 8-byte header. The "timeout" is
643 * given in milliseconds.
649 *------------------------------------------------------------------------*/
651 usb_control_msg(struct usb_device *dev, struct usb_host_endpoint *uhe,
652 uint8_t request, uint8_t requesttype,
653 uint16_t value, uint16_t index, void *data,
654 uint16_t size, usb_timeout_t timeout)
656 struct usb_device_request req;
663 req.bmRequestType = requesttype;
664 req.bRequest = request;
665 USETW(req.wValue, value);
666 USETW(req.wIndex, index);
667 USETW(req.wLength, size);
672 type = (uhe->desc.bmAttributes & UE_XFERTYPE);
673 addr = (uhe->desc.bEndpointAddress & UE_ADDR);
675 if (type != UE_CONTROL) {
680 * The FreeBSD USB stack supports standard control
681 * transfers on control endpoint zero:
683 err = usbd_do_request_flags(dev,
684 NULL, &req, data, USB_SHORT_XFER_OK,
693 if (dev->flags.usb_mode != USB_MODE_HOST) {
697 err = usb_setup_endpoint(dev, uhe, 1 /* dummy */ );
700 * NOTE: we need to allocate real memory here so that we don't
701 * transfer data to/from the stack!
703 * 0xFFFF is a FreeBSD specific magic value.
705 urb = usb_alloc_urb(0xFFFF, size);
712 memcpy(urb->setup_packet, &req, sizeof(req));
714 if (size && (!(req.bmRequestType & UT_READ))) {
715 /* move the data to a real buffer */
716 memcpy(USB_ADD_BYTES(urb->setup_packet, sizeof(req)),
719 err = usb_start_wait_urb(urb, timeout, &actlen);
721 if (req.bmRequestType & UT_READ) {
723 bcopy(USB_ADD_BYTES(urb->setup_packet,
724 sizeof(req)), data, actlen);
735 /*------------------------------------------------------------------------*
738 * The following function will select which alternate setting of an
739 * USB interface you plan to use. By default alternate setting with
740 * index zero is selected. Note that "iface_no" is not the interface
741 * index, but rather the value of "bInterfaceNumber".
742 *------------------------------------------------------------------------*/
744 usb_set_interface(struct usb_device *dev, uint8_t iface_no, uint8_t alt_index)
746 struct usb_interface *p_ui = usb_ifnum_to_if(dev, iface_no);
751 if (alt_index >= p_ui->num_altsetting)
753 usb_linux_cleanup_interface(dev, p_ui);
754 err = -usbd_set_alt_interface_index(dev,
755 p_ui->bsd_iface_index, alt_index);
757 p_ui->cur_altsetting = p_ui->altsetting + alt_index;
762 /*------------------------------------------------------------------------*
765 * The following function is an extension to the Linux USB API that
766 * allows you to set a maximum buffer size for a given USB endpoint.
767 * The maximum buffer size is per URB. If you don't call this function
768 * to set a maximum buffer size, the endpoint will not be functional.
769 * Note that for isochronous endpoints the maximum buffer size must be
770 * a non-zero dummy, hence this function will base the maximum buffer
771 * size on "wMaxPacketSize".
772 *------------------------------------------------------------------------*/
774 usb_setup_endpoint(struct usb_device *dev,
775 struct usb_host_endpoint *uhe, usb_size_t bufsize)
777 struct usb_config cfg[2];
778 uint8_t type = uhe->desc.bmAttributes & UE_XFERTYPE;
779 uint8_t addr = uhe->desc.bEndpointAddress;
781 if (uhe->fbsd_buf_size == bufsize) {
785 usbd_transfer_unsetup(uhe->bsd_xfer, 2);
787 uhe->fbsd_buf_size = bufsize;
792 memset(cfg, 0, sizeof(cfg));
794 if (type == UE_ISOCHRONOUS) {
797 * Isochronous transfers are special in that they don't fit
798 * into the BULK/INTR/CONTROL transfer model.
802 cfg[0].endpoint = addr & UE_ADDR;
803 cfg[0].direction = addr & (UE_DIR_OUT | UE_DIR_IN);
804 cfg[0].callback = &usb_linux_isoc_callback;
805 cfg[0].bufsize = 0; /* use wMaxPacketSize */
806 cfg[0].frames = usb_max_isoc_frames(dev);
807 cfg[0].flags.proxy_buffer = 1;
810 * The Linux USB API allows non back-to-back
811 * isochronous frames which we do not support. If the
812 * isochronous frames are not back-to-back we need to
813 * do a copy, and then we need a buffer for
814 * that. Enable this at your own risk.
816 cfg[0].flags.ext_buffer = 1;
818 cfg[0].flags.short_xfer_ok = 1;
820 bcopy(cfg, cfg + 1, sizeof(*cfg));
822 /* Allocate and setup two generic FreeBSD USB transfers */
824 if (usbd_transfer_setup(dev, &uhe->bsd_iface_index,
825 uhe->bsd_xfer, cfg, 2, uhe, &Giant)) {
829 if (bufsize > (1 << 22)) {
830 /* limit buffer size */
833 /* Allocate and setup one generic FreeBSD USB transfer */
836 cfg[0].endpoint = addr & UE_ADDR;
837 cfg[0].direction = addr & (UE_DIR_OUT | UE_DIR_IN);
838 cfg[0].callback = &usb_linux_non_isoc_callback;
839 cfg[0].bufsize = bufsize;
840 cfg[0].flags.ext_buffer = 1; /* enable zero-copy */
841 cfg[0].flags.proxy_buffer = 1;
842 cfg[0].flags.short_xfer_ok = 1;
844 if (usbd_transfer_setup(dev, &uhe->bsd_iface_index,
845 uhe->bsd_xfer, cfg, 1, uhe, &Giant)) {
852 /*------------------------------------------------------------------------*
853 * usb_linux_create_usb_device
855 * The following function is used to build up a per USB device
856 * structure tree, that mimics the Linux one. The root structure
857 * is returned by this function.
858 *------------------------------------------------------------------------*/
860 usb_linux_create_usb_device(struct usb_device *udev, device_t dev)
862 struct usb_config_descriptor *cd = usbd_get_config_descriptor(udev);
863 struct usb_descriptor *desc;
864 struct usb_interface_descriptor *id;
865 struct usb_endpoint_descriptor *ed;
866 struct usb_interface *p_ui = NULL;
867 struct usb_host_interface *p_uhi = NULL;
868 struct usb_host_endpoint *p_uhe = NULL;
870 uint16_t niface_total;
872 uint16_t iface_no_curr;
873 uint16_t iface_index;
878 * We do two passes. One pass for computing necessary memory size
879 * and one pass to initialize all the allocated memory structures.
881 for (pass = 0; pass < 2; pass++) {
883 iface_no_curr = 0 - 1;
890 * Iterate over all the USB descriptors. Use the USB config
891 * descriptor pointer provided by the FreeBSD USB stack.
893 while ((desc = usb_desc_foreach(cd, desc))) {
896 * Build up a tree according to the descriptors we
899 switch (desc->bDescriptorType) {
905 if ((ed->bLength < sizeof(*ed)) ||
909 bcopy(ed, &p_uhe->desc, sizeof(p_uhe->desc));
910 p_uhe->bsd_iface_index = iface_index - 1;
911 TAILQ_INIT(&p_uhe->bsd_urb_list);
915 (p_uhi - 1)->desc.bNumEndpoints++;
920 case UDESC_INTERFACE:
922 if (id->bLength < sizeof(*id))
925 bcopy(id, &p_uhi->desc, sizeof(p_uhi->desc));
926 p_uhi->desc.bNumEndpoints = 0;
927 p_uhi->endpoint = p_uhe;
929 p_uhi->bsd_iface_index = iface_index;
932 iface_no = id->bInterfaceNumber;
934 if (iface_no_curr != iface_no) {
936 p_ui->altsetting = p_uhi - 1;
937 p_ui->cur_altsetting = p_uhi - 1;
938 p_ui->num_altsetting = 1;
939 p_ui->bsd_iface_index = iface_index;
940 p_ui->linux_udev = udev;
943 iface_no_curr = iface_no;
947 (p_ui - 1)->num_altsetting++;
959 size = (sizeof(*p_uhe) * nedesc) +
960 (sizeof(*p_ui) * iface_index) +
961 (sizeof(*p_uhi) * niface_total);
963 p_uhe = malloc(size, M_USBDEV, M_WAITOK | M_ZERO);
964 p_ui = (void *)(p_uhe + nedesc);
965 p_uhi = (void *)(p_ui + iface_index);
967 udev->linux_iface_start = p_ui;
968 udev->linux_iface_end = p_ui + iface_index;
969 udev->linux_endpoint_start = p_uhe;
970 udev->linux_endpoint_end = p_uhe + nedesc;
971 udev->devnum = device_get_unit(dev);
972 bcopy(&udev->ddesc, &udev->descriptor,
973 sizeof(udev->descriptor));
974 bcopy(udev->ctrl_ep.edesc, &udev->ep0.desc,
975 sizeof(udev->ep0.desc));
981 /*------------------------------------------------------------------------*
984 * This function should always be used when you allocate an URB for
985 * use with the USB Linux stack. In case of an isochronous transfer
986 * you must specifiy the maximum number of "iso_packets" which you
987 * plan to transfer per URB. This function is always blocking, and
988 * "mem_flags" are not regarded like on Linux.
989 *------------------------------------------------------------------------*/
991 usb_alloc_urb(uint16_t iso_packets, uint16_t mem_flags)
996 if (iso_packets == 0xFFFF) {
998 * FreeBSD specific magic value to ask for control transfer
1001 size = sizeof(*urb) + sizeof(struct usb_device_request) + mem_flags;
1003 size = sizeof(*urb) + (iso_packets * sizeof(urb->iso_frame_desc[0]));
1006 urb = malloc(size, M_USBDEV, M_WAITOK | M_ZERO);
1009 cv_init(&urb->cv_wait, "URBWAIT");
1010 if (iso_packets == 0xFFFF) {
1011 urb->setup_packet = (void *)(urb + 1);
1012 urb->transfer_buffer = (void *)(urb->setup_packet +
1013 sizeof(struct usb_device_request));
1015 urb->number_of_packets = iso_packets;
1021 /*------------------------------------------------------------------------*
1022 * usb_find_host_endpoint
1024 * The following function will return the Linux USB host endpoint
1025 * structure that matches the given endpoint type and endpoint
1026 * value. If no match is found, NULL is returned. This function is not
1027 * part of the Linux USB API and is only used internally.
1028 *------------------------------------------------------------------------*/
1029 struct usb_host_endpoint *
1030 usb_find_host_endpoint(struct usb_device *dev, uint8_t type, uint8_t ep)
1032 struct usb_host_endpoint *uhe;
1033 struct usb_host_endpoint *uhe_end;
1034 struct usb_host_interface *uhi;
1035 struct usb_interface *ui;
1043 if (type == UE_CONTROL) {
1046 mask = (UE_DIR_IN | UE_DIR_OUT | UE_ADDR);
1052 * Iterate over all the interfaces searching the selected alternate
1053 * setting only, and all belonging endpoints.
1055 for (ui = dev->linux_iface_start;
1056 ui != dev->linux_iface_end;
1058 uhi = ui->cur_altsetting;
1060 uhe_end = uhi->endpoint + uhi->desc.bNumEndpoints;
1061 for (uhe = uhi->endpoint;
1064 ea = uhe->desc.bEndpointAddress;
1065 at = uhe->desc.bmAttributes;
1067 if (((ea & mask) == ep) &&
1068 ((at & UE_XFERTYPE) == type)) {
1075 if ((type == UE_CONTROL) && ((ep & UE_ADDR) == 0)) {
1081 /*------------------------------------------------------------------------*
1082 * usb_altnum_to_altsetting
1084 * The following function returns a pointer to an alternate setting by
1085 * index given a "usb_interface" pointer. If the alternate setting by
1086 * index does not exist, NULL is returned. And alternate setting is a
1087 * variant of an interface, but usually with slightly different
1089 *------------------------------------------------------------------------*/
1090 struct usb_host_interface *
1091 usb_altnum_to_altsetting(const struct usb_interface *intf, uint8_t alt_index)
1093 if (alt_index >= intf->num_altsetting) {
1096 return (intf->altsetting + alt_index);
1099 /*------------------------------------------------------------------------*
1102 * The following function searches up an USB interface by
1103 * "bInterfaceNumber". If no match is found, NULL is returned.
1104 *------------------------------------------------------------------------*/
1105 struct usb_interface *
1106 usb_ifnum_to_if(struct usb_device *dev, uint8_t iface_no)
1108 struct usb_interface *p_ui;
1110 for (p_ui = dev->linux_iface_start;
1111 p_ui != dev->linux_iface_end;
1113 if ((p_ui->num_altsetting > 0) &&
1114 (p_ui->altsetting->desc.bInterfaceNumber == iface_no)) {
1121 /*------------------------------------------------------------------------*
1123 *------------------------------------------------------------------------*/
1125 usb_buffer_alloc(struct usb_device *dev, usb_size_t size, uint16_t mem_flags, uint8_t *dma_addr)
1127 return (malloc(size, M_USBDEV, M_WAITOK | M_ZERO));
1130 /*------------------------------------------------------------------------*
1132 *------------------------------------------------------------------------*/
1134 usbd_get_intfdata(struct usb_interface *intf)
1136 return (intf->bsd_priv_sc);
1139 /*------------------------------------------------------------------------*
1140 * usb_linux_register
1142 * The following function is used by the "USB_DRIVER_EXPORT()" macro,
1143 * and is used to register a Linux USB driver, so that its
1144 * "usb_device_id" structures gets searched a probe time. This
1145 * function is not part of the Linux USB API, and is for internal use
1147 *------------------------------------------------------------------------*/
1149 usb_linux_register(void *arg)
1151 struct usb_driver *drv = arg;
1154 LIST_INSERT_HEAD(&usb_linux_driver_list, drv, linux_driver_list);
1157 usb_needs_explore_all();
1160 /*------------------------------------------------------------------------*
1161 * usb_linux_deregister
1163 * The following function is used by the "USB_DRIVER_EXPORT()" macro,
1164 * and is used to deregister a Linux USB driver. This function will
1165 * ensure that all driver instances belonging to the Linux USB device
1166 * driver in question, gets detached before the driver is
1167 * unloaded. This function is not part of the Linux USB API, and is
1168 * for internal use only.
1169 *------------------------------------------------------------------------*/
1171 usb_linux_deregister(void *arg)
1173 struct usb_driver *drv = arg;
1174 struct usb_linux_softc *sc;
1178 LIST_FOREACH(sc, &usb_linux_attached_list, sc_attached_list) {
1179 if (sc->sc_udrv == drv) {
1181 device_detach(sc->sc_fbsd_dev);
1185 LIST_REMOVE(drv, linux_driver_list);
1189 /*------------------------------------------------------------------------*
1190 * usb_linux_free_device
1192 * The following function is only used by the FreeBSD USB stack, to
1193 * cleanup and free memory after that a Linux USB device was attached.
1194 *------------------------------------------------------------------------*/
1196 usb_linux_free_device(struct usb_device *dev)
1198 struct usb_host_endpoint *uhe;
1199 struct usb_host_endpoint *uhe_end;
1202 uhe = dev->linux_endpoint_start;
1203 uhe_end = dev->linux_endpoint_end;
1204 while (uhe != uhe_end) {
1205 err = usb_setup_endpoint(dev, uhe, 0);
1208 err = usb_setup_endpoint(dev, &dev->ep0, 0);
1209 free(dev->linux_endpoint_start, M_USBDEV);
1212 /*------------------------------------------------------------------------*
1214 *------------------------------------------------------------------------*/
1216 usb_buffer_free(struct usb_device *dev, usb_size_t size,
1217 void *addr, uint8_t dma_addr)
1219 free(addr, M_USBDEV);
1222 /*------------------------------------------------------------------------*
1224 *------------------------------------------------------------------------*/
1226 usb_free_urb(struct urb *urb)
1231 /* make sure that the current URB is not active */
1234 /* destroy condition variable */
1235 cv_destroy(&urb->cv_wait);
1238 free(urb, M_USBDEV);
1241 /*------------------------------------------------------------------------*
1244 * The following function can be used to initialize a custom URB. It
1245 * is not recommended to use this function. Use "usb_alloc_urb()"
1247 *------------------------------------------------------------------------*/
1249 usb_init_urb(struct urb *urb)
1254 memset(urb, 0, sizeof(*urb));
1257 /*------------------------------------------------------------------------*
1259 *------------------------------------------------------------------------*/
1261 usb_kill_urb(struct urb *urb)
1263 usb_unlink_urb_sub(urb, 1);
1266 /*------------------------------------------------------------------------*
1269 * The following function sets the per Linux USB interface private
1270 * data pointer. It is used by most Linux USB device drivers.
1271 *------------------------------------------------------------------------*/
1273 usb_set_intfdata(struct usb_interface *intf, void *data)
1275 intf->bsd_priv_sc = data;
1278 /*------------------------------------------------------------------------*
1279 * usb_linux_cleanup_interface
1281 * The following function will release all FreeBSD USB transfers
1282 * associated with a Linux USB interface. It is for internal use only.
1283 *------------------------------------------------------------------------*/
1285 usb_linux_cleanup_interface(struct usb_device *dev, struct usb_interface *iface)
1287 struct usb_host_interface *uhi;
1288 struct usb_host_interface *uhi_end;
1289 struct usb_host_endpoint *uhe;
1290 struct usb_host_endpoint *uhe_end;
1293 uhi = iface->altsetting;
1294 uhi_end = iface->altsetting + iface->num_altsetting;
1295 while (uhi != uhi_end) {
1296 uhe = uhi->endpoint;
1297 uhe_end = uhi->endpoint + uhi->desc.bNumEndpoints;
1298 while (uhe != uhe_end) {
1299 err = usb_setup_endpoint(dev, uhe, 0);
1306 /*------------------------------------------------------------------------*
1307 * usb_linux_wait_complete
1309 * The following function is used by "usb_start_wait_urb()" to wake it
1310 * up, when an USB transfer has finished.
1311 *------------------------------------------------------------------------*/
1313 usb_linux_wait_complete(struct urb *urb)
1315 if (urb->transfer_flags & URB_IS_SLEEPING) {
1316 cv_signal(&urb->cv_wait);
1318 urb->transfer_flags &= ~URB_WAIT_WAKEUP;
1321 /*------------------------------------------------------------------------*
1322 * usb_linux_complete
1323 *------------------------------------------------------------------------*/
1325 usb_linux_complete(struct usb_xfer *xfer)
1329 urb = usbd_xfer_get_priv(xfer);
1330 usbd_xfer_set_priv(xfer, NULL);
1331 if (urb->complete) {
1332 (urb->complete) (urb);
1336 /*------------------------------------------------------------------------*
1337 * usb_linux_isoc_callback
1339 * The following is the FreeBSD isochronous USB callback. Isochronous
1340 * frames are USB packets transferred 1000 or 8000 times per second,
1341 * depending on whether a full- or high- speed USB transfer is
1343 *------------------------------------------------------------------------*/
1345 usb_linux_isoc_callback(struct usb_xfer *xfer, usb_error_t error)
1347 usb_frlength_t max_frame = xfer->max_frame_size;
1348 usb_frlength_t offset;
1350 struct urb *urb = usbd_xfer_get_priv(xfer);
1351 struct usb_host_endpoint *uhe = usbd_xfer_softc(xfer);
1352 struct usb_iso_packet_descriptor *uipd;
1356 switch (USB_GET_STATE(xfer)) {
1357 case USB_ST_TRANSFERRED:
1359 if (urb->bsd_isread) {
1361 /* copy in data with regard to the URB */
1365 for (x = 0; x < urb->number_of_packets; x++) {
1366 uipd = urb->iso_frame_desc + x;
1367 if (uipd->length > xfer->frlengths[x]) {
1368 if (urb->transfer_flags & URB_SHORT_NOT_OK) {
1369 /* XXX should be EREMOTEIO */
1370 uipd->status = -EPIPE;
1377 uipd->actual_length = xfer->frlengths[x];
1378 if (!xfer->flags.ext_buffer) {
1379 usbd_copy_out(xfer->frbuffers, offset,
1380 USB_ADD_BYTES(urb->transfer_buffer,
1381 uipd->offset), uipd->actual_length);
1383 offset += max_frame;
1386 for (x = 0; x < urb->number_of_packets; x++) {
1387 uipd = urb->iso_frame_desc + x;
1388 uipd->actual_length = xfer->frlengths[x];
1393 urb->actual_length = xfer->actlen;
1395 /* check for short transfer */
1396 if (xfer->actlen < xfer->sumlen) {
1397 /* short transfer */
1398 if (urb->transfer_flags & URB_SHORT_NOT_OK) {
1399 /* XXX should be EREMOTEIO */
1400 urb->status = -EPIPE;
1410 usb_linux_complete(xfer);
1415 if (xfer->priv_fifo == NULL) {
1417 /* get next transfer */
1418 urb = TAILQ_FIRST(&uhe->bsd_urb_list);
1423 TAILQ_REMOVE(&uhe->bsd_urb_list, urb, bsd_urb_list);
1424 urb->bsd_urb_list.tqe_prev = NULL;
1426 x = xfer->max_frame_count;
1427 if (urb->number_of_packets > x) {
1428 /* XXX simply truncate the transfer */
1429 urb->number_of_packets = x;
1432 DPRINTF("Already got a transfer\n");
1434 /* already got a transfer (should not happen) */
1435 urb = usbd_xfer_get_priv(xfer);
1438 urb->bsd_isread = (uhe->desc.bEndpointAddress & UE_DIR_IN) ? 1 : 0;
1440 if (xfer->flags.ext_buffer) {
1441 /* set virtual address to load */
1442 usbd_xfer_set_frame_data(xfer, 0, urb->transfer_buffer, 0);
1444 if (!(urb->bsd_isread)) {
1446 /* copy out data with regard to the URB */
1450 for (x = 0; x < urb->number_of_packets; x++) {
1451 uipd = urb->iso_frame_desc + x;
1452 usbd_xfer_set_frame_len(xfer, x, uipd->length);
1453 if (!xfer->flags.ext_buffer) {
1454 usbd_copy_in(xfer->frbuffers, offset,
1455 USB_ADD_BYTES(urb->transfer_buffer,
1456 uipd->offset), uipd->length);
1458 offset += uipd->length;
1463 * compute the transfer length into the "offset"
1467 offset = urb->number_of_packets * max_frame;
1469 /* setup "frlengths" array */
1471 for (x = 0; x < urb->number_of_packets; x++) {
1472 uipd = urb->iso_frame_desc + x;
1473 usbd_xfer_set_frame_len(xfer, x, max_frame);
1476 usbd_xfer_set_priv(xfer, urb);
1477 xfer->flags.force_short_xfer = 0;
1478 xfer->timeout = urb->timeout;
1479 xfer->nframes = urb->number_of_packets;
1480 usbd_transfer_submit(xfer);
1483 default: /* Error */
1484 if (xfer->error == USB_ERR_CANCELLED) {
1485 urb->status = -ECONNRESET;
1487 urb->status = -EPIPE; /* stalled */
1490 /* Set zero for "actual_length" */
1491 urb->actual_length = 0;
1493 /* Set zero for "actual_length" */
1494 for (x = 0; x < urb->number_of_packets; x++) {
1495 urb->iso_frame_desc[x].actual_length = 0;
1496 urb->iso_frame_desc[x].status = urb->status;
1500 usb_linux_complete(xfer);
1502 if (xfer->error == USB_ERR_CANCELLED) {
1503 /* we need to return in this case */
1511 /*------------------------------------------------------------------------*
1512 * usb_linux_non_isoc_callback
1514 * The following is the FreeBSD BULK/INTERRUPT and CONTROL USB
1515 * callback. It dequeues Linux USB stack compatible URB's, transforms
1516 * the URB fields into a FreeBSD USB transfer, and defragments the USB
1517 * transfer as required. When the transfer is complete the "complete"
1518 * callback is called.
1519 *------------------------------------------------------------------------*/
1521 usb_linux_non_isoc_callback(struct usb_xfer *xfer, usb_error_t error)
1524 REQ_SIZE = sizeof(struct usb_device_request)
1526 struct urb *urb = usbd_xfer_get_priv(xfer);
1527 struct usb_host_endpoint *uhe = usbd_xfer_softc(xfer);
1529 usb_frlength_t max_bulk = usbd_xfer_max_len(xfer);
1530 uint8_t data_frame = xfer->flags_int.control_xfr ? 1 : 0;
1534 switch (USB_GET_STATE(xfer)) {
1535 case USB_ST_TRANSFERRED:
1537 if (xfer->flags_int.control_xfr) {
1539 /* don't transfer the setup packet again: */
1541 usbd_xfer_set_frame_len(xfer, 0, 0);
1543 if (urb->bsd_isread && (!xfer->flags.ext_buffer)) {
1544 /* copy in data with regard to the URB */
1545 usbd_copy_out(xfer->frbuffers + data_frame, 0,
1546 urb->bsd_data_ptr, xfer->frlengths[data_frame]);
1548 urb->bsd_length_rem -= xfer->frlengths[data_frame];
1549 urb->bsd_data_ptr += xfer->frlengths[data_frame];
1550 urb->actual_length += xfer->frlengths[data_frame];
1552 /* check for short transfer */
1553 if (xfer->actlen < xfer->sumlen) {
1554 urb->bsd_length_rem = 0;
1556 /* short transfer */
1557 if (urb->transfer_flags & URB_SHORT_NOT_OK) {
1558 urb->status = -EPIPE;
1563 /* check remainder */
1564 if (urb->bsd_length_rem > 0) {
1572 usb_linux_complete(xfer);
1576 /* get next transfer */
1577 urb = TAILQ_FIRST(&uhe->bsd_urb_list);
1582 TAILQ_REMOVE(&uhe->bsd_urb_list, urb, bsd_urb_list);
1583 urb->bsd_urb_list.tqe_prev = NULL;
1585 usbd_xfer_set_priv(xfer, urb);
1586 xfer->flags.force_short_xfer = 0;
1587 xfer->timeout = urb->timeout;
1589 if (xfer->flags_int.control_xfr) {
1592 * USB control transfers need special handling.
1593 * First copy in the header, then copy in data!
1595 if (!xfer->flags.ext_buffer) {
1596 usbd_copy_in(xfer->frbuffers, 0,
1597 urb->setup_packet, REQ_SIZE);
1598 usbd_xfer_set_frame_len(xfer, 0, REQ_SIZE);
1600 /* set virtual address to load */
1601 usbd_xfer_set_frame_data(xfer, 0,
1602 urb->setup_packet, REQ_SIZE);
1605 ptr = urb->setup_packet;
1607 /* setup data transfer direction and length */
1608 urb->bsd_isread = (ptr[0] & UT_READ) ? 1 : 0;
1609 urb->bsd_length_rem = ptr[6] | (ptr[7] << 8);
1613 /* setup data transfer direction */
1615 urb->bsd_length_rem = urb->transfer_buffer_length;
1616 urb->bsd_isread = (uhe->desc.bEndpointAddress &
1620 urb->bsd_data_ptr = urb->transfer_buffer;
1621 urb->actual_length = 0;
1624 if (max_bulk > urb->bsd_length_rem) {
1625 max_bulk = urb->bsd_length_rem;
1627 /* check if we need to force a short transfer */
1629 if ((max_bulk == urb->bsd_length_rem) &&
1630 (urb->transfer_flags & URB_ZERO_PACKET) &&
1631 (!xfer->flags_int.control_xfr)) {
1632 xfer->flags.force_short_xfer = 1;
1634 /* check if we need to copy in data */
1636 if (xfer->flags.ext_buffer) {
1637 /* set virtual address to load */
1638 usbd_xfer_set_frame_data(xfer, data_frame,
1639 urb->bsd_data_ptr, max_bulk);
1640 } else if (!urb->bsd_isread) {
1641 /* copy out data with regard to the URB */
1642 usbd_copy_in(xfer->frbuffers + data_frame, 0,
1643 urb->bsd_data_ptr, max_bulk);
1644 usbd_xfer_set_frame_len(xfer, data_frame, max_bulk);
1646 if (xfer->flags_int.control_xfr) {
1655 usbd_transfer_submit(xfer);
1659 if (xfer->error == USB_ERR_CANCELLED) {
1660 urb->status = -ECONNRESET;
1662 urb->status = -EPIPE;
1665 /* Set zero for "actual_length" */
1666 urb->actual_length = 0;
1669 usb_linux_complete(xfer);
1671 if (xfer->error == USB_ERR_CANCELLED) {
1672 /* we need to return in this case */
1679 /*------------------------------------------------------------------------*
1681 *------------------------------------------------------------------------*/
1683 usb_fill_bulk_urb(struct urb *urb, struct usb_device *udev,
1684 struct usb_host_endpoint *uhe, void *buf,
1685 int length, usb_complete_t callback, void *arg)
1688 urb->endpoint = uhe;
1689 urb->transfer_buffer = buf;
1690 urb->transfer_buffer_length = length;
1691 urb->complete = callback;
1695 /*------------------------------------------------------------------------*
1698 * NOTE: This function can also be used for interrupt endpoints!
1703 *------------------------------------------------------------------------*/
1705 usb_bulk_msg(struct usb_device *udev, struct usb_host_endpoint *uhe,
1706 void *data, int len, uint16_t *pactlen, usb_timeout_t timeout)
1716 err = usb_setup_endpoint(udev, uhe, 4096 /* bytes */);
1720 urb = usb_alloc_urb(0, 0);
1724 usb_fill_bulk_urb(urb, udev, uhe, data, len,
1725 usb_linux_wait_complete, NULL);
1727 err = usb_start_wait_urb(urb, timeout, pactlen);