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/linker_set.h>
37 #include <sys/module.h>
39 #include <sys/mutex.h>
40 #include <sys/condvar.h>
41 #include <sys/sysctl.h>
43 #include <sys/unistd.h>
44 #include <sys/callout.h>
45 #include <sys/malloc.h>
48 #include <dev/usb/usb.h>
49 #include <dev/usb/usb_ioctl.h>
50 #include <dev/usb/usbdi.h>
51 #include <dev/usb/usbdi_util.h>
53 #define USB_DEBUG_VAR usb_debug
55 #include <dev/usb/usb_core.h>
56 #include <dev/usb/usb_compat_linux.h>
57 #include <dev/usb/usb_process.h>
58 #include <dev/usb/usb_device.h>
59 #include <dev/usb/usb_util.h>
60 #include <dev/usb/usb_busdma.h>
61 #include <dev/usb/usb_transfer.h>
62 #include <dev/usb/usb_hub.h>
63 #include <dev/usb/usb_request.h>
64 #include <dev/usb/usb_debug.h>
66 struct usb_linux_softc {
67 LIST_ENTRY(usb_linux_softc) sc_attached_list;
70 struct usb_device *sc_fbsd_udev;
71 struct usb_interface *sc_ui;
72 struct usb_driver *sc_udrv;
76 static device_probe_t usb_linux_probe;
77 static device_attach_t usb_linux_attach;
78 static device_detach_t usb_linux_detach;
79 static device_suspend_t usb_linux_suspend;
80 static device_resume_t usb_linux_resume;
82 static usb_callback_t usb_linux_isoc_callback;
83 static usb_callback_t usb_linux_non_isoc_callback;
85 static usb_complete_t usb_linux_wait_complete;
87 static uint16_t usb_max_isoc_frames(struct usb_device *);
88 static int usb_start_wait_urb(struct urb *, usb_timeout_t, uint16_t *);
89 static const struct usb_device_id *usb_linux_lookup_id(
90 const struct usb_device_id *, struct usb_attach_arg *);
91 static struct usb_driver *usb_linux_get_usb_driver(struct usb_linux_softc *);
92 static int usb_linux_create_usb_device(struct usb_device *, device_t);
93 static void usb_linux_cleanup_interface(struct usb_device *,
94 struct usb_interface *);
95 static void usb_linux_complete(struct usb_xfer *);
96 static int usb_unlink_urb_sub(struct urb *, uint8_t);
98 /*------------------------------------------------------------------------*
99 * FreeBSD USB interface
100 *------------------------------------------------------------------------*/
102 static LIST_HEAD(, usb_linux_softc) usb_linux_attached_list;
103 static LIST_HEAD(, usb_driver) usb_linux_driver_list;
105 static device_method_t usb_linux_methods[] = {
106 /* Device interface */
107 DEVMETHOD(device_probe, usb_linux_probe),
108 DEVMETHOD(device_attach, usb_linux_attach),
109 DEVMETHOD(device_detach, usb_linux_detach),
110 DEVMETHOD(device_suspend, usb_linux_suspend),
111 DEVMETHOD(device_resume, usb_linux_resume),
116 static driver_t usb_linux_driver = {
118 .methods = usb_linux_methods,
119 .size = sizeof(struct usb_linux_softc),
122 static devclass_t usb_linux_devclass;
124 DRIVER_MODULE(usb_linux, uhub, usb_linux_driver, usb_linux_devclass, NULL, 0);
125 MODULE_VERSION(usb_linux, 1);
127 /*------------------------------------------------------------------------*
128 * usb_linux_lookup_id
130 * This functions takes an array of "struct usb_device_id" and tries
131 * to match the entries with the information in "struct usb_attach_arg".
132 * If it finds a match the matching entry will be returned.
133 * Else "NULL" will be returned.
134 *------------------------------------------------------------------------*/
135 static const struct usb_device_id *
136 usb_linux_lookup_id(const struct usb_device_id *id, struct usb_attach_arg *uaa)
142 * Keep on matching array entries until we find one with
143 * "match_flags" equal to zero, which indicates the end of the
146 for (; id->match_flags; id++) {
148 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
149 (id->idVendor != uaa->info.idVendor)) {
152 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
153 (id->idProduct != uaa->info.idProduct)) {
156 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
157 (id->bcdDevice_lo > uaa->info.bcdDevice)) {
160 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
161 (id->bcdDevice_hi < uaa->info.bcdDevice)) {
164 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
165 (id->bDeviceClass != uaa->info.bDeviceClass)) {
168 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
169 (id->bDeviceSubClass != uaa->info.bDeviceSubClass)) {
172 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
173 (id->bDeviceProtocol != uaa->info.bDeviceProtocol)) {
176 if ((uaa->info.bDeviceClass == 0xFF) &&
177 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
178 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
179 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
180 USB_DEVICE_ID_MATCH_INT_PROTOCOL))) {
183 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
184 (id->bInterfaceClass != uaa->info.bInterfaceClass)) {
187 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
188 (id->bInterfaceSubClass != uaa->info.bInterfaceSubClass)) {
191 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
192 (id->bInterfaceProtocol != uaa->info.bInterfaceProtocol)) {
195 /* we found a match! */
203 /*------------------------------------------------------------------------*
206 * This function is the FreeBSD probe callback. It is called from the
207 * FreeBSD USB stack through the "device_probe_and_attach()" function.
208 *------------------------------------------------------------------------*/
210 usb_linux_probe(device_t dev)
212 struct usb_attach_arg *uaa = device_get_ivars(dev);
213 struct usb_driver *udrv;
216 if (uaa->usb_mode != USB_MODE_HOST) {
220 LIST_FOREACH(udrv, &usb_linux_driver_list, linux_driver_list) {
221 if (usb_linux_lookup_id(udrv->id_table, uaa)) {
231 /*------------------------------------------------------------------------*
232 * usb_linux_get_usb_driver
234 * This function returns the pointer to the "struct usb_driver" where
235 * the Linux USB device driver "struct usb_device_id" match was found.
236 * We apply a lock before reading out the pointer to avoid races.
237 *------------------------------------------------------------------------*/
238 static struct usb_driver *
239 usb_linux_get_usb_driver(struct usb_linux_softc *sc)
241 struct usb_driver *udrv;
249 /*------------------------------------------------------------------------*
252 * This function is the FreeBSD attach callback. It is called from the
253 * FreeBSD USB stack through the "device_probe_and_attach()" function.
254 * This function is called when "usb_linux_probe()" returns zero.
255 *------------------------------------------------------------------------*/
257 usb_linux_attach(device_t dev)
259 struct usb_attach_arg *uaa = device_get_ivars(dev);
260 struct usb_linux_softc *sc = device_get_softc(dev);
261 struct usb_driver *udrv;
262 const struct usb_device_id *id = NULL;
265 LIST_FOREACH(udrv, &usb_linux_driver_list, linux_driver_list) {
266 id = usb_linux_lookup_id(udrv->id_table, uaa);
275 if (usb_linux_create_usb_device(uaa->device, dev) != 0)
277 device_set_usb_desc(dev);
279 sc->sc_fbsd_udev = uaa->device;
280 sc->sc_fbsd_dev = dev;
282 sc->sc_ui = usb_ifnum_to_if(uaa->device, uaa->info.bIfaceNum);
283 if (sc->sc_ui == NULL) {
287 if ((udrv->probe) (sc->sc_ui, id)) {
292 LIST_INSERT_HEAD(&usb_linux_attached_list, sc, sc_attached_list);
299 /*------------------------------------------------------------------------*
302 * This function is the FreeBSD detach callback. It is called from the
303 * FreeBSD USB stack through the "device_detach()" function.
304 *------------------------------------------------------------------------*/
306 usb_linux_detach(device_t dev)
308 struct usb_linux_softc *sc = device_get_softc(dev);
309 struct usb_driver *udrv = NULL;
312 if (sc->sc_attached_list.le_prev) {
313 LIST_REMOVE(sc, sc_attached_list);
314 sc->sc_attached_list.le_prev = NULL;
320 if (udrv && udrv->disconnect) {
321 (udrv->disconnect) (sc->sc_ui);
324 * Make sure that we free all FreeBSD USB transfers belonging to
325 * this Linux "usb_interface", hence they will most likely not be
328 usb_linux_cleanup_interface(sc->sc_fbsd_udev, sc->sc_ui);
332 /*------------------------------------------------------------------------*
335 * This function is the FreeBSD suspend callback. Usually it does nothing.
336 *------------------------------------------------------------------------*/
338 usb_linux_suspend(device_t dev)
340 struct usb_linux_softc *sc = device_get_softc(dev);
341 struct usb_driver *udrv = usb_linux_get_usb_driver(sc);
344 if (udrv && udrv->suspend) {
345 err = (udrv->suspend) (sc->sc_ui, 0);
350 /*------------------------------------------------------------------------*
353 * This function is the FreeBSD resume callback. Usually it does nothing.
354 *------------------------------------------------------------------------*/
356 usb_linux_resume(device_t dev)
358 struct usb_linux_softc *sc = device_get_softc(dev);
359 struct usb_driver *udrv = usb_linux_get_usb_driver(sc);
362 if (udrv && udrv->resume) {
363 err = (udrv->resume) (sc->sc_ui);
368 /*------------------------------------------------------------------------*
369 * Linux emulation layer
370 *------------------------------------------------------------------------*/
372 /*------------------------------------------------------------------------*
373 * usb_max_isoc_frames
375 * The following function returns the maximum number of isochronous
376 * frames that we support per URB. It is not part of the Linux USB API.
377 *------------------------------------------------------------------------*/
379 usb_max_isoc_frames(struct usb_device *dev)
382 switch (usbd_get_speed(dev)) {
385 return (USB_MAX_FULL_SPEED_ISOC_FRAMES);
387 return (USB_MAX_HIGH_SPEED_ISOC_FRAMES);
391 /*------------------------------------------------------------------------*
394 * This function is used to queue an URB after that it has been
395 * initialized. If it returns non-zero, it means that the URB was not
397 *------------------------------------------------------------------------*/
399 usb_submit_urb(struct urb *urb, uint16_t mem_flags)
401 struct usb_host_endpoint *uhe;
408 do_unlock = mtx_owned(&Giant) ? 0 : 1;
412 if (urb->endpoint == NULL) {
418 * Check to see if the urb is in the process of being killed
419 * and stop a urb that is in the process of being killed from
420 * being re-submitted (e.g. from its completion callback
423 if (urb->kill_count != 0) {
431 * Check that we have got a FreeBSD USB transfer that will dequeue
432 * the URB structure and do the real transfer. If there are no USB
433 * transfers, then we return an error.
435 if (uhe->bsd_xfer[0] ||
439 TAILQ_INSERT_TAIL(&uhe->bsd_urb_list, urb, bsd_urb_list);
441 urb->status = -EINPROGRESS;
443 usbd_transfer_start(uhe->bsd_xfer[0]);
444 usbd_transfer_start(uhe->bsd_xfer[1]);
447 /* no pipes have been setup yet! */
448 urb->status = -EINVAL;
457 /*------------------------------------------------------------------------*
460 * This function is used to stop an URB after that it is been
461 * submitted, but before the "complete" callback has been called. On
462 *------------------------------------------------------------------------*/
464 usb_unlink_urb(struct urb *urb)
466 return (usb_unlink_urb_sub(urb, 0));
470 usb_unlink_bsd(struct usb_xfer *xfer,
471 struct urb *urb, uint8_t drain)
475 if (!usbd_transfer_pending(xfer))
477 if (xfer->priv_fifo == (void *)urb) {
480 usbd_transfer_drain(xfer);
483 usbd_transfer_stop(xfer);
485 usbd_transfer_start(xfer);
490 usb_unlink_urb_sub(struct urb *urb, uint8_t drain)
492 struct usb_host_endpoint *uhe;
500 do_unlock = mtx_owned(&Giant) ? 0 : 1;
506 if (urb->endpoint == NULL) {
512 if (urb->bsd_urb_list.tqe_prev) {
514 /* not started yet, just remove it from the queue */
515 TAILQ_REMOVE(&uhe->bsd_urb_list, urb, bsd_urb_list);
516 urb->bsd_urb_list.tqe_prev = NULL;
517 urb->status = -ECONNRESET;
518 urb->actual_length = 0;
520 for (x = 0; x < urb->number_of_packets; x++) {
521 urb->iso_frame_desc[x].actual_length = 0;
525 (urb->complete) (urb);
530 * If the URB is not on the URB list, then check if one of
531 * the FreeBSD USB transfer are processing the current URB.
532 * If so, re-start that transfer, which will lead to the
533 * termination of that URB:
535 usb_unlink_bsd(uhe->bsd_xfer[0], urb, drain);
536 usb_unlink_bsd(uhe->bsd_xfer[1], urb, drain);
547 /*------------------------------------------------------------------------*
550 * This function must always be used to clear the stall. Stall is when
551 * an USB endpoint returns a stall message to the USB host controller.
552 * Until the stall is cleared, no data can be transferred.
553 *------------------------------------------------------------------------*/
555 usb_clear_halt(struct usb_device *dev, struct usb_host_endpoint *uhe)
557 struct usb_config cfg[1];
558 struct usb_endpoint *ep;
565 type = uhe->desc.bmAttributes & UE_XFERTYPE;
566 addr = uhe->desc.bEndpointAddress;
568 bzero(cfg, sizeof(cfg));
571 cfg[0].endpoint = addr & UE_ADDR;
572 cfg[0].direction = addr & (UE_DIR_OUT | UE_DIR_IN);
574 ep = usbd_get_endpoint(dev, uhe->bsd_iface_index, cfg);
578 usbd_clear_data_toggle(dev, ep);
580 return (usb_control_msg(dev, &dev->ep0,
581 UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT,
582 UF_ENDPOINT_HALT, addr, NULL, 0, 1000));
585 /*------------------------------------------------------------------------*
588 * This is an internal function that is used to perform synchronous
589 * Linux USB transfers.
590 *------------------------------------------------------------------------*/
592 usb_start_wait_urb(struct urb *urb, usb_timeout_t timeout, uint16_t *p_actlen)
597 /* you must have a timeout! */
601 urb->complete = &usb_linux_wait_complete;
602 urb->timeout = timeout;
603 urb->transfer_flags |= URB_WAIT_WAKEUP;
604 urb->transfer_flags &= ~URB_IS_SLEEPING;
606 do_unlock = mtx_owned(&Giant) ? 0 : 1;
609 err = usb_submit_urb(urb, 0);
614 * the URB might have completed before we get here, so check that by
617 while (urb->transfer_flags & URB_WAIT_WAKEUP) {
618 urb->transfer_flags |= URB_IS_SLEEPING;
619 cv_wait(&urb->cv_wait, &Giant);
620 urb->transfer_flags &= ~URB_IS_SLEEPING;
628 if (p_actlen != NULL) {
632 *p_actlen = urb->actual_length;
637 /*------------------------------------------------------------------------*
640 * The following function performs a control transfer sequence one any
641 * control, bulk or interrupt endpoint, specified by "uhe". A control
642 * transfer means that you transfer an 8-byte header first followed by
643 * a data-phase as indicated by the 8-byte header. The "timeout" is
644 * given in milliseconds.
650 *------------------------------------------------------------------------*/
652 usb_control_msg(struct usb_device *dev, struct usb_host_endpoint *uhe,
653 uint8_t request, uint8_t requesttype,
654 uint16_t value, uint16_t index, void *data,
655 uint16_t size, usb_timeout_t timeout)
657 struct usb_device_request req;
664 req.bmRequestType = requesttype;
665 req.bRequest = request;
666 USETW(req.wValue, value);
667 USETW(req.wIndex, index);
668 USETW(req.wLength, size);
673 type = (uhe->desc.bmAttributes & UE_XFERTYPE);
674 addr = (uhe->desc.bEndpointAddress & UE_ADDR);
676 if (type != UE_CONTROL) {
681 * The FreeBSD USB stack supports standard control
682 * transfers on control endpoint zero:
684 err = usbd_do_request_flags(dev,
685 NULL, &req, data, USB_SHORT_XFER_OK,
694 if (dev->flags.usb_mode != USB_MODE_HOST) {
698 err = usb_setup_endpoint(dev, uhe, 1 /* dummy */ );
701 * NOTE: we need to allocate real memory here so that we don't
702 * transfer data to/from the stack!
704 * 0xFFFF is a FreeBSD specific magic value.
706 urb = usb_alloc_urb(0xFFFF, size);
713 bcopy(&req, urb->setup_packet, sizeof(req));
715 if (size && (!(req.bmRequestType & UT_READ))) {
716 /* move the data to a real buffer */
717 bcopy(data, USB_ADD_BYTES(urb->setup_packet,
720 err = usb_start_wait_urb(urb, timeout, &actlen);
722 if (req.bmRequestType & UT_READ) {
724 bcopy(USB_ADD_BYTES(urb->setup_packet,
725 sizeof(req)), data, actlen);
736 /*------------------------------------------------------------------------*
739 * The following function will select which alternate setting of an
740 * USB interface you plan to use. By default alternate setting with
741 * index zero is selected. Note that "iface_no" is not the interface
742 * index, but rather the value of "bInterfaceNumber".
743 *------------------------------------------------------------------------*/
745 usb_set_interface(struct usb_device *dev, uint8_t iface_no, uint8_t alt_index)
747 struct usb_interface *p_ui = usb_ifnum_to_if(dev, iface_no);
752 if (alt_index >= p_ui->num_altsetting)
754 usb_linux_cleanup_interface(dev, p_ui);
755 err = -usbd_set_alt_interface_index(dev,
756 p_ui->bsd_iface_index, alt_index);
758 p_ui->cur_altsetting = p_ui->altsetting + alt_index;
763 /*------------------------------------------------------------------------*
766 * The following function is an extension to the Linux USB API that
767 * allows you to set a maximum buffer size for a given USB endpoint.
768 * The maximum buffer size is per URB. If you don't call this function
769 * to set a maximum buffer size, the endpoint will not be functional.
770 * Note that for isochronous endpoints the maximum buffer size must be
771 * a non-zero dummy, hence this function will base the maximum buffer
772 * size on "wMaxPacketSize".
773 *------------------------------------------------------------------------*/
775 usb_setup_endpoint(struct usb_device *dev,
776 struct usb_host_endpoint *uhe, usb_size_t bufsize)
778 struct usb_config cfg[2];
779 uint8_t type = uhe->desc.bmAttributes & UE_XFERTYPE;
780 uint8_t addr = uhe->desc.bEndpointAddress;
782 if (uhe->fbsd_buf_size == bufsize) {
786 usbd_transfer_unsetup(uhe->bsd_xfer, 2);
788 uhe->fbsd_buf_size = bufsize;
793 bzero(cfg, sizeof(cfg));
795 if (type == UE_ISOCHRONOUS) {
798 * Isochronous transfers are special in that they don't fit
799 * into the BULK/INTR/CONTROL transfer model.
803 cfg[0].endpoint = addr & UE_ADDR;
804 cfg[0].direction = addr & (UE_DIR_OUT | UE_DIR_IN);
805 cfg[0].callback = &usb_linux_isoc_callback;
806 cfg[0].bufsize = 0; /* use wMaxPacketSize */
807 cfg[0].frames = usb_max_isoc_frames(dev);
808 cfg[0].flags.proxy_buffer = 1;
811 * The Linux USB API allows non back-to-back
812 * isochronous frames which we do not support. If the
813 * isochronous frames are not back-to-back we need to
814 * do a copy, and then we need a buffer for
815 * that. Enable this at your own risk.
817 cfg[0].flags.ext_buffer = 1;
819 cfg[0].flags.short_xfer_ok = 1;
821 bcopy(cfg, cfg + 1, sizeof(*cfg));
823 /* Allocate and setup two generic FreeBSD USB transfers */
825 if (usbd_transfer_setup(dev, &uhe->bsd_iface_index,
826 uhe->bsd_xfer, cfg, 2, uhe, &Giant)) {
830 if (bufsize > (1 << 22)) {
831 /* limit buffer size */
834 /* Allocate and setup one generic FreeBSD USB transfer */
837 cfg[0].endpoint = addr & UE_ADDR;
838 cfg[0].direction = addr & (UE_DIR_OUT | UE_DIR_IN);
839 cfg[0].callback = &usb_linux_non_isoc_callback;
840 cfg[0].bufsize = bufsize;
841 cfg[0].flags.ext_buffer = 1; /* enable zero-copy */
842 cfg[0].flags.proxy_buffer = 1;
843 cfg[0].flags.short_xfer_ok = 1;
845 if (usbd_transfer_setup(dev, &uhe->bsd_iface_index,
846 uhe->bsd_xfer, cfg, 1, uhe, &Giant)) {
853 /*------------------------------------------------------------------------*
854 * usb_linux_create_usb_device
856 * The following function is used to build up a per USB device
857 * structure tree, that mimics the Linux one. The root structure
858 * is returned by this function.
859 *------------------------------------------------------------------------*/
861 usb_linux_create_usb_device(struct usb_device *udev, device_t dev)
863 struct usb_config_descriptor *cd = usbd_get_config_descriptor(udev);
864 struct usb_descriptor *desc;
865 struct usb_interface_descriptor *id;
866 struct usb_endpoint_descriptor *ed;
867 struct usb_interface *p_ui = NULL;
868 struct usb_host_interface *p_uhi = NULL;
869 struct usb_host_endpoint *p_uhe = NULL;
871 uint16_t niface_total;
873 uint16_t iface_no_curr;
874 uint16_t iface_index;
879 * We do two passes. One pass for computing necessary memory size
880 * and one pass to initialize all the allocated memory structures.
882 for (pass = 0; pass < 2; pass++) {
884 iface_no_curr = 0 - 1;
891 * Iterate over all the USB descriptors. Use the USB config
892 * descriptor pointer provided by the FreeBSD USB stack.
894 while ((desc = usb_desc_foreach(cd, desc))) {
897 * Build up a tree according to the descriptors we
900 switch (desc->bDescriptorType) {
906 if ((ed->bLength < sizeof(*ed)) ||
910 bcopy(ed, &p_uhe->desc, sizeof(p_uhe->desc));
911 p_uhe->bsd_iface_index = iface_index - 1;
912 TAILQ_INIT(&p_uhe->bsd_urb_list);
916 (p_uhi - 1)->desc.bNumEndpoints++;
921 case UDESC_INTERFACE:
923 if (id->bLength < sizeof(*id))
926 bcopy(id, &p_uhi->desc, sizeof(p_uhi->desc));
927 p_uhi->desc.bNumEndpoints = 0;
928 p_uhi->endpoint = p_uhe;
930 p_uhi->bsd_iface_index = iface_index;
933 iface_no = id->bInterfaceNumber;
935 if (iface_no_curr != iface_no) {
937 p_ui->altsetting = p_uhi - 1;
938 p_ui->cur_altsetting = p_uhi - 1;
939 p_ui->num_altsetting = 1;
940 p_ui->bsd_iface_index = iface_index;
941 p_ui->linux_udev = udev;
944 iface_no_curr = iface_no;
948 (p_ui - 1)->num_altsetting++;
960 size = (sizeof(*p_uhe) * nedesc) +
961 (sizeof(*p_ui) * iface_index) +
962 (sizeof(*p_uhi) * niface_total);
964 p_uhe = malloc(size, M_USBDEV, M_WAITOK | M_ZERO);
965 p_ui = (void *)(p_uhe + nedesc);
966 p_uhi = (void *)(p_ui + iface_index);
968 udev->linux_iface_start = p_ui;
969 udev->linux_iface_end = p_ui + iface_index;
970 udev->linux_endpoint_start = p_uhe;
971 udev->linux_endpoint_end = p_uhe + nedesc;
972 udev->devnum = device_get_unit(dev);
973 bcopy(&udev->ddesc, &udev->descriptor,
974 sizeof(udev->descriptor));
975 bcopy(udev->ctrl_ep.edesc, &udev->ep0.desc,
976 sizeof(udev->ep0.desc));
982 /*------------------------------------------------------------------------*
985 * This function should always be used when you allocate an URB for
986 * use with the USB Linux stack. In case of an isochronous transfer
987 * you must specifiy the maximum number of "iso_packets" which you
988 * plan to transfer per URB. This function is always blocking, and
989 * "mem_flags" are not regarded like on Linux.
990 *------------------------------------------------------------------------*/
992 usb_alloc_urb(uint16_t iso_packets, uint16_t mem_flags)
997 if (iso_packets == 0xFFFF) {
999 * FreeBSD specific magic value to ask for control transfer
1000 * memory allocation:
1002 size = sizeof(*urb) + sizeof(struct usb_device_request) + mem_flags;
1004 size = sizeof(*urb) + (iso_packets * sizeof(urb->iso_frame_desc[0]));
1007 urb = malloc(size, M_USBDEV, M_WAITOK | M_ZERO);
1010 cv_init(&urb->cv_wait, "URBWAIT");
1011 if (iso_packets == 0xFFFF) {
1012 urb->setup_packet = (void *)(urb + 1);
1013 urb->transfer_buffer = (void *)(urb->setup_packet +
1014 sizeof(struct usb_device_request));
1016 urb->number_of_packets = iso_packets;
1022 /*------------------------------------------------------------------------*
1023 * usb_find_host_endpoint
1025 * The following function will return the Linux USB host endpoint
1026 * structure that matches the given endpoint type and endpoint
1027 * value. If no match is found, NULL is returned. This function is not
1028 * part of the Linux USB API and is only used internally.
1029 *------------------------------------------------------------------------*/
1030 struct usb_host_endpoint *
1031 usb_find_host_endpoint(struct usb_device *dev, uint8_t type, uint8_t ep)
1033 struct usb_host_endpoint *uhe;
1034 struct usb_host_endpoint *uhe_end;
1035 struct usb_host_interface *uhi;
1036 struct usb_interface *ui;
1044 if (type == UE_CONTROL) {
1047 mask = (UE_DIR_IN | UE_DIR_OUT | UE_ADDR);
1053 * Iterate over all the interfaces searching the selected alternate
1054 * setting only, and all belonging endpoints.
1056 for (ui = dev->linux_iface_start;
1057 ui != dev->linux_iface_end;
1059 uhi = ui->cur_altsetting;
1061 uhe_end = uhi->endpoint + uhi->desc.bNumEndpoints;
1062 for (uhe = uhi->endpoint;
1065 ea = uhe->desc.bEndpointAddress;
1066 at = uhe->desc.bmAttributes;
1068 if (((ea & mask) == ep) &&
1069 ((at & UE_XFERTYPE) == type)) {
1076 if ((type == UE_CONTROL) && ((ep & UE_ADDR) == 0)) {
1082 /*------------------------------------------------------------------------*
1083 * usb_altnum_to_altsetting
1085 * The following function returns a pointer to an alternate setting by
1086 * index given a "usb_interface" pointer. If the alternate setting by
1087 * index does not exist, NULL is returned. And alternate setting is a
1088 * variant of an interface, but usually with slightly different
1090 *------------------------------------------------------------------------*/
1091 struct usb_host_interface *
1092 usb_altnum_to_altsetting(const struct usb_interface *intf, uint8_t alt_index)
1094 if (alt_index >= intf->num_altsetting) {
1097 return (intf->altsetting + alt_index);
1100 /*------------------------------------------------------------------------*
1103 * The following function searches up an USB interface by
1104 * "bInterfaceNumber". If no match is found, NULL is returned.
1105 *------------------------------------------------------------------------*/
1106 struct usb_interface *
1107 usb_ifnum_to_if(struct usb_device *dev, uint8_t iface_no)
1109 struct usb_interface *p_ui;
1111 for (p_ui = dev->linux_iface_start;
1112 p_ui != dev->linux_iface_end;
1114 if ((p_ui->num_altsetting > 0) &&
1115 (p_ui->altsetting->desc.bInterfaceNumber == iface_no)) {
1122 /*------------------------------------------------------------------------*
1124 *------------------------------------------------------------------------*/
1126 usb_buffer_alloc(struct usb_device *dev, usb_size_t size, uint16_t mem_flags, uint8_t *dma_addr)
1128 return (malloc(size, M_USBDEV, M_WAITOK | M_ZERO));
1131 /*------------------------------------------------------------------------*
1133 *------------------------------------------------------------------------*/
1135 usbd_get_intfdata(struct usb_interface *intf)
1137 return (intf->bsd_priv_sc);
1140 /*------------------------------------------------------------------------*
1141 * usb_linux_register
1143 * The following function is used by the "USB_DRIVER_EXPORT()" macro,
1144 * and is used to register a Linux USB driver, so that its
1145 * "usb_device_id" structures gets searched a probe time. This
1146 * function is not part of the Linux USB API, and is for internal use
1148 *------------------------------------------------------------------------*/
1150 usb_linux_register(void *arg)
1152 struct usb_driver *drv = arg;
1155 LIST_INSERT_HEAD(&usb_linux_driver_list, drv, linux_driver_list);
1158 usb_needs_explore_all();
1161 /*------------------------------------------------------------------------*
1162 * usb_linux_deregister
1164 * The following function is used by the "USB_DRIVER_EXPORT()" macro,
1165 * and is used to deregister a Linux USB driver. This function will
1166 * ensure that all driver instances belonging to the Linux USB device
1167 * driver in question, gets detached before the driver is
1168 * unloaded. This function is not part of the Linux USB API, and is
1169 * for internal use only.
1170 *------------------------------------------------------------------------*/
1172 usb_linux_deregister(void *arg)
1174 struct usb_driver *drv = arg;
1175 struct usb_linux_softc *sc;
1179 LIST_FOREACH(sc, &usb_linux_attached_list, sc_attached_list) {
1180 if (sc->sc_udrv == drv) {
1182 device_detach(sc->sc_fbsd_dev);
1186 LIST_REMOVE(drv, linux_driver_list);
1190 /*------------------------------------------------------------------------*
1191 * usb_linux_free_device
1193 * The following function is only used by the FreeBSD USB stack, to
1194 * cleanup and free memory after that a Linux USB device was attached.
1195 *------------------------------------------------------------------------*/
1197 usb_linux_free_device(struct usb_device *dev)
1199 struct usb_host_endpoint *uhe;
1200 struct usb_host_endpoint *uhe_end;
1203 uhe = dev->linux_endpoint_start;
1204 uhe_end = dev->linux_endpoint_end;
1205 while (uhe != uhe_end) {
1206 err = usb_setup_endpoint(dev, uhe, 0);
1209 err = usb_setup_endpoint(dev, &dev->ep0, 0);
1210 free(dev->linux_endpoint_start, M_USBDEV);
1213 /*------------------------------------------------------------------------*
1215 *------------------------------------------------------------------------*/
1217 usb_buffer_free(struct usb_device *dev, usb_size_t size,
1218 void *addr, uint8_t dma_addr)
1220 free(addr, M_USBDEV);
1223 /*------------------------------------------------------------------------*
1225 *------------------------------------------------------------------------*/
1227 usb_free_urb(struct urb *urb)
1232 /* make sure that the current URB is not active */
1235 /* destroy condition variable */
1236 cv_destroy(&urb->cv_wait);
1239 free(urb, M_USBDEV);
1242 /*------------------------------------------------------------------------*
1245 * The following function can be used to initialize a custom URB. It
1246 * is not recommended to use this function. Use "usb_alloc_urb()"
1248 *------------------------------------------------------------------------*/
1250 usb_init_urb(struct urb *urb)
1255 bzero(urb, sizeof(*urb));
1258 /*------------------------------------------------------------------------*
1260 *------------------------------------------------------------------------*/
1262 usb_kill_urb(struct urb *urb)
1264 usb_unlink_urb_sub(urb, 1);
1267 /*------------------------------------------------------------------------*
1270 * The following function sets the per Linux USB interface private
1271 * data pointer. It is used by most Linux USB device drivers.
1272 *------------------------------------------------------------------------*/
1274 usb_set_intfdata(struct usb_interface *intf, void *data)
1276 intf->bsd_priv_sc = data;
1279 /*------------------------------------------------------------------------*
1280 * usb_linux_cleanup_interface
1282 * The following function will release all FreeBSD USB transfers
1283 * associated with a Linux USB interface. It is for internal use only.
1284 *------------------------------------------------------------------------*/
1286 usb_linux_cleanup_interface(struct usb_device *dev, struct usb_interface *iface)
1288 struct usb_host_interface *uhi;
1289 struct usb_host_interface *uhi_end;
1290 struct usb_host_endpoint *uhe;
1291 struct usb_host_endpoint *uhe_end;
1294 uhi = iface->altsetting;
1295 uhi_end = iface->altsetting + iface->num_altsetting;
1296 while (uhi != uhi_end) {
1297 uhe = uhi->endpoint;
1298 uhe_end = uhi->endpoint + uhi->desc.bNumEndpoints;
1299 while (uhe != uhe_end) {
1300 err = usb_setup_endpoint(dev, uhe, 0);
1307 /*------------------------------------------------------------------------*
1308 * usb_linux_wait_complete
1310 * The following function is used by "usb_start_wait_urb()" to wake it
1311 * up, when an USB transfer has finished.
1312 *------------------------------------------------------------------------*/
1314 usb_linux_wait_complete(struct urb *urb)
1316 if (urb->transfer_flags & URB_IS_SLEEPING) {
1317 cv_signal(&urb->cv_wait);
1319 urb->transfer_flags &= ~URB_WAIT_WAKEUP;
1322 /*------------------------------------------------------------------------*
1323 * usb_linux_complete
1324 *------------------------------------------------------------------------*/
1326 usb_linux_complete(struct usb_xfer *xfer)
1330 urb = usbd_xfer_get_priv(xfer);
1331 usbd_xfer_set_priv(xfer, NULL);
1332 if (urb->complete) {
1333 (urb->complete) (urb);
1337 /*------------------------------------------------------------------------*
1338 * usb_linux_isoc_callback
1340 * The following is the FreeBSD isochronous USB callback. Isochronous
1341 * frames are USB packets transferred 1000 or 8000 times per second,
1342 * depending on whether a full- or high- speed USB transfer is
1344 *------------------------------------------------------------------------*/
1346 usb_linux_isoc_callback(struct usb_xfer *xfer, usb_error_t error)
1348 usb_frlength_t max_frame = xfer->max_frame_size;
1349 usb_frlength_t offset;
1351 struct urb *urb = usbd_xfer_get_priv(xfer);
1352 struct usb_host_endpoint *uhe = usbd_xfer_softc(xfer);
1353 struct usb_iso_packet_descriptor *uipd;
1357 switch (USB_GET_STATE(xfer)) {
1358 case USB_ST_TRANSFERRED:
1360 if (urb->bsd_isread) {
1362 /* copy in data with regard to the URB */
1366 for (x = 0; x < urb->number_of_packets; x++) {
1367 uipd = urb->iso_frame_desc + x;
1368 if (uipd->length > xfer->frlengths[x]) {
1369 if (urb->transfer_flags & URB_SHORT_NOT_OK) {
1370 /* XXX should be EREMOTEIO */
1371 uipd->status = -EPIPE;
1378 uipd->actual_length = xfer->frlengths[x];
1379 if (!xfer->flags.ext_buffer) {
1380 usbd_copy_out(xfer->frbuffers, offset,
1381 USB_ADD_BYTES(urb->transfer_buffer,
1382 uipd->offset), uipd->actual_length);
1384 offset += max_frame;
1387 for (x = 0; x < urb->number_of_packets; x++) {
1388 uipd = urb->iso_frame_desc + x;
1389 uipd->actual_length = xfer->frlengths[x];
1394 urb->actual_length = xfer->actlen;
1396 /* check for short transfer */
1397 if (xfer->actlen < xfer->sumlen) {
1398 /* short transfer */
1399 if (urb->transfer_flags & URB_SHORT_NOT_OK) {
1400 /* XXX should be EREMOTEIO */
1401 urb->status = -EPIPE;
1411 usb_linux_complete(xfer);
1416 if (xfer->priv_fifo == NULL) {
1418 /* get next transfer */
1419 urb = TAILQ_FIRST(&uhe->bsd_urb_list);
1424 TAILQ_REMOVE(&uhe->bsd_urb_list, urb, bsd_urb_list);
1425 urb->bsd_urb_list.tqe_prev = NULL;
1427 x = xfer->max_frame_count;
1428 if (urb->number_of_packets > x) {
1429 /* XXX simply truncate the transfer */
1430 urb->number_of_packets = x;
1433 DPRINTF("Already got a transfer\n");
1435 /* already got a transfer (should not happen) */
1436 urb = usbd_xfer_get_priv(xfer);
1439 urb->bsd_isread = (uhe->desc.bEndpointAddress & UE_DIR_IN) ? 1 : 0;
1441 if (xfer->flags.ext_buffer) {
1442 /* set virtual address to load */
1443 usbd_xfer_set_frame_data(xfer, 0, urb->transfer_buffer, 0);
1445 if (!(urb->bsd_isread)) {
1447 /* copy out data with regard to the URB */
1451 for (x = 0; x < urb->number_of_packets; x++) {
1452 uipd = urb->iso_frame_desc + x;
1453 usbd_xfer_set_frame_len(xfer, x, uipd->length);
1454 if (!xfer->flags.ext_buffer) {
1455 usbd_copy_in(xfer->frbuffers, offset,
1456 USB_ADD_BYTES(urb->transfer_buffer,
1457 uipd->offset), uipd->length);
1459 offset += uipd->length;
1464 * compute the transfer length into the "offset"
1468 offset = urb->number_of_packets * max_frame;
1470 /* setup "frlengths" array */
1472 for (x = 0; x < urb->number_of_packets; x++) {
1473 uipd = urb->iso_frame_desc + x;
1474 usbd_xfer_set_frame_len(xfer, x, max_frame);
1477 usbd_xfer_set_priv(xfer, urb);
1478 xfer->flags.force_short_xfer = 0;
1479 xfer->timeout = urb->timeout;
1480 xfer->nframes = urb->number_of_packets;
1481 usbd_transfer_submit(xfer);
1484 default: /* Error */
1485 if (xfer->error == USB_ERR_CANCELLED) {
1486 urb->status = -ECONNRESET;
1488 urb->status = -EPIPE; /* stalled */
1491 /* Set zero for "actual_length" */
1492 urb->actual_length = 0;
1494 /* Set zero for "actual_length" */
1495 for (x = 0; x < urb->number_of_packets; x++) {
1496 urb->iso_frame_desc[x].actual_length = 0;
1497 urb->iso_frame_desc[x].status = urb->status;
1501 usb_linux_complete(xfer);
1503 if (xfer->error == USB_ERR_CANCELLED) {
1504 /* we need to return in this case */
1512 /*------------------------------------------------------------------------*
1513 * usb_linux_non_isoc_callback
1515 * The following is the FreeBSD BULK/INTERRUPT and CONTROL USB
1516 * callback. It dequeues Linux USB stack compatible URB's, transforms
1517 * the URB fields into a FreeBSD USB transfer, and defragments the USB
1518 * transfer as required. When the transfer is complete the "complete"
1519 * callback is called.
1520 *------------------------------------------------------------------------*/
1522 usb_linux_non_isoc_callback(struct usb_xfer *xfer, usb_error_t error)
1525 REQ_SIZE = sizeof(struct usb_device_request)
1527 struct urb *urb = usbd_xfer_get_priv(xfer);
1528 struct usb_host_endpoint *uhe = usbd_xfer_softc(xfer);
1530 usb_frlength_t max_bulk = usbd_xfer_max_len(xfer);
1531 uint8_t data_frame = xfer->flags_int.control_xfr ? 1 : 0;
1535 switch (USB_GET_STATE(xfer)) {
1536 case USB_ST_TRANSFERRED:
1538 if (xfer->flags_int.control_xfr) {
1540 /* don't transfer the setup packet again: */
1542 usbd_xfer_set_frame_len(xfer, 0, 0);
1544 if (urb->bsd_isread && (!xfer->flags.ext_buffer)) {
1545 /* copy in data with regard to the URB */
1546 usbd_copy_out(xfer->frbuffers + data_frame, 0,
1547 urb->bsd_data_ptr, xfer->frlengths[data_frame]);
1549 urb->bsd_length_rem -= xfer->frlengths[data_frame];
1550 urb->bsd_data_ptr += xfer->frlengths[data_frame];
1551 urb->actual_length += xfer->frlengths[data_frame];
1553 /* check for short transfer */
1554 if (xfer->actlen < xfer->sumlen) {
1555 urb->bsd_length_rem = 0;
1557 /* short transfer */
1558 if (urb->transfer_flags & URB_SHORT_NOT_OK) {
1559 urb->status = -EPIPE;
1564 /* check remainder */
1565 if (urb->bsd_length_rem > 0) {
1573 usb_linux_complete(xfer);
1577 /* get next transfer */
1578 urb = TAILQ_FIRST(&uhe->bsd_urb_list);
1583 TAILQ_REMOVE(&uhe->bsd_urb_list, urb, bsd_urb_list);
1584 urb->bsd_urb_list.tqe_prev = NULL;
1586 usbd_xfer_set_priv(xfer, urb);
1587 xfer->flags.force_short_xfer = 0;
1588 xfer->timeout = urb->timeout;
1590 if (xfer->flags_int.control_xfr) {
1593 * USB control transfers need special handling.
1594 * First copy in the header, then copy in data!
1596 if (!xfer->flags.ext_buffer) {
1597 usbd_copy_in(xfer->frbuffers, 0,
1598 urb->setup_packet, REQ_SIZE);
1599 usbd_xfer_set_frame_len(xfer, 0, REQ_SIZE);
1601 /* set virtual address to load */
1602 usbd_xfer_set_frame_data(xfer, 0,
1603 urb->setup_packet, REQ_SIZE);
1606 ptr = urb->setup_packet;
1608 /* setup data transfer direction and length */
1609 urb->bsd_isread = (ptr[0] & UT_READ) ? 1 : 0;
1610 urb->bsd_length_rem = ptr[6] | (ptr[7] << 8);
1614 /* setup data transfer direction */
1616 urb->bsd_length_rem = urb->transfer_buffer_length;
1617 urb->bsd_isread = (uhe->desc.bEndpointAddress &
1621 urb->bsd_data_ptr = urb->transfer_buffer;
1622 urb->actual_length = 0;
1625 if (max_bulk > urb->bsd_length_rem) {
1626 max_bulk = urb->bsd_length_rem;
1628 /* check if we need to force a short transfer */
1630 if ((max_bulk == urb->bsd_length_rem) &&
1631 (urb->transfer_flags & URB_ZERO_PACKET) &&
1632 (!xfer->flags_int.control_xfr)) {
1633 xfer->flags.force_short_xfer = 1;
1635 /* check if we need to copy in data */
1637 if (xfer->flags.ext_buffer) {
1638 /* set virtual address to load */
1639 usbd_xfer_set_frame_data(xfer, data_frame,
1640 urb->bsd_data_ptr, max_bulk);
1641 } else if (!urb->bsd_isread) {
1642 /* copy out data with regard to the URB */
1643 usbd_copy_in(xfer->frbuffers + data_frame, 0,
1644 urb->bsd_data_ptr, max_bulk);
1645 usbd_xfer_set_frame_len(xfer, data_frame, max_bulk);
1647 if (xfer->flags_int.control_xfr) {
1656 usbd_transfer_submit(xfer);
1660 if (xfer->error == USB_ERR_CANCELLED) {
1661 urb->status = -ECONNRESET;
1663 urb->status = -EPIPE;
1666 /* Set zero for "actual_length" */
1667 urb->actual_length = 0;
1670 usb_linux_complete(xfer);
1672 if (xfer->error == USB_ERR_CANCELLED) {
1673 /* we need to return in this case */
1680 /*------------------------------------------------------------------------*
1682 *------------------------------------------------------------------------*/
1684 usb_fill_bulk_urb(struct urb *urb, struct usb_device *udev,
1685 struct usb_host_endpoint *uhe, void *buf,
1686 int length, usb_complete_t callback, void *arg)
1689 urb->endpoint = uhe;
1690 urb->transfer_buffer = buf;
1691 urb->transfer_buffer_length = length;
1692 urb->complete = callback;
1696 /*------------------------------------------------------------------------*
1699 * NOTE: This function can also be used for interrupt endpoints!
1704 *------------------------------------------------------------------------*/
1706 usb_bulk_msg(struct usb_device *udev, struct usb_host_endpoint *uhe,
1707 void *data, int len, uint16_t *pactlen, usb_timeout_t timeout)
1717 err = usb_setup_endpoint(udev, uhe, 4096 /* bytes */);
1721 urb = usb_alloc_urb(0, 0);
1725 usb_fill_bulk_urb(urb, udev, uhe, data, len,
1726 usb_linux_wait_complete, NULL);
1728 err = usb_start_wait_urb(urb, timeout, pactlen);