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 #ifdef USB_GLOBAL_INCLUDE_FILE
29 #include USB_GLOBAL_INCLUDE_FILE
31 #include <sys/stdint.h>
32 #include <sys/stddef.h>
33 #include <sys/param.h>
34 #include <sys/queue.h>
35 #include <sys/types.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
39 #include <sys/module.h>
41 #include <sys/mutex.h>
42 #include <sys/condvar.h>
43 #include <sys/sysctl.h>
45 #include <sys/unistd.h>
46 #include <sys/callout.h>
47 #include <sys/malloc.h>
50 #include <dev/usb/usb.h>
51 #include <dev/usb/usbdi.h>
52 #include <dev/usb/usbdi_util.h>
54 #define USB_DEBUG_VAR usb_debug
56 #include <dev/usb/usb_core.h>
57 #include <linux/usb.h>
58 #include <dev/usb/usb_process.h>
59 #include <dev/usb/usb_device.h>
60 #include <dev/usb/usb_util.h>
61 #include <dev/usb/usb_busdma.h>
62 #include <dev/usb/usb_transfer.h>
63 #include <dev/usb/usb_hub.h>
64 #include <dev/usb/usb_request.h>
65 #include <dev/usb/usb_debug.h>
66 #include <dev/usb/usb_dynamic.h>
67 #endif /* USB_GLOBAL_INCLUDE_FILE */
69 struct usb_linux_softc {
70 LIST_ENTRY(usb_linux_softc) sc_attached_list;
73 struct usb_device *sc_fbsd_udev;
74 struct usb_interface *sc_ui;
75 struct usb_driver *sc_udrv;
79 static device_probe_t usb_linux_probe;
80 static device_attach_t usb_linux_attach;
81 static device_detach_t usb_linux_detach;
82 static device_suspend_t usb_linux_suspend;
83 static device_resume_t usb_linux_resume;
85 static usb_callback_t usb_linux_isoc_callback;
86 static usb_callback_t usb_linux_non_isoc_callback;
88 static usb_complete_t usb_linux_wait_complete;
90 static uint16_t usb_max_isoc_frames(struct usb_device *);
91 static int usb_start_wait_urb(struct urb *, usb_timeout_t, uint16_t *);
92 static const struct usb_device_id *usb_linux_lookup_id(
93 const struct usb_device_id *, struct usb_attach_arg *);
94 static struct usb_driver *usb_linux_get_usb_driver(struct usb_linux_softc *);
95 static int usb_linux_create_usb_device(struct usb_device *, device_t);
96 static void usb_linux_cleanup_interface(struct usb_device *,
97 struct usb_interface *);
98 static void usb_linux_complete(struct usb_xfer *);
99 static int usb_unlink_urb_sub(struct urb *, uint8_t);
101 /*------------------------------------------------------------------------*
102 * FreeBSD USB interface
103 *------------------------------------------------------------------------*/
105 static LIST_HEAD(, usb_linux_softc) usb_linux_attached_list;
106 static LIST_HEAD(, usb_driver) usb_linux_driver_list;
108 static device_method_t usb_linux_methods[] = {
109 /* Device interface */
110 DEVMETHOD(device_probe, usb_linux_probe),
111 DEVMETHOD(device_attach, usb_linux_attach),
112 DEVMETHOD(device_detach, usb_linux_detach),
113 DEVMETHOD(device_suspend, usb_linux_suspend),
114 DEVMETHOD(device_resume, usb_linux_resume),
119 static driver_t usb_linux_driver = {
121 .methods = usb_linux_methods,
122 .size = sizeof(struct usb_linux_softc),
125 static devclass_t usb_linux_devclass;
127 DRIVER_MODULE(usb_linux, uhub, usb_linux_driver, usb_linux_devclass, NULL, 0);
128 MODULE_VERSION(usb_linux, 1);
130 /*------------------------------------------------------------------------*
131 * usb_linux_lookup_id
133 * This functions takes an array of "struct usb_device_id" and tries
134 * to match the entries with the information in "struct usb_attach_arg".
135 * If it finds a match the matching entry will be returned.
136 * Else "NULL" will be returned.
137 *------------------------------------------------------------------------*/
138 static const struct usb_device_id *
139 usb_linux_lookup_id(const struct usb_device_id *id, struct usb_attach_arg *uaa)
145 * Keep on matching array entries until we find one with
146 * "match_flags" equal to zero, which indicates the end of the
149 for (; id->match_flags; id++) {
151 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
152 (id->idVendor != uaa->info.idVendor)) {
155 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
156 (id->idProduct != uaa->info.idProduct)) {
159 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
160 (id->bcdDevice_lo > uaa->info.bcdDevice)) {
163 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
164 (id->bcdDevice_hi < uaa->info.bcdDevice)) {
167 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
168 (id->bDeviceClass != uaa->info.bDeviceClass)) {
171 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
172 (id->bDeviceSubClass != uaa->info.bDeviceSubClass)) {
175 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
176 (id->bDeviceProtocol != uaa->info.bDeviceProtocol)) {
179 if ((uaa->info.bDeviceClass == 0xFF) &&
180 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
181 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
182 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
183 USB_DEVICE_ID_MATCH_INT_PROTOCOL))) {
186 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
187 (id->bInterfaceClass != uaa->info.bInterfaceClass)) {
190 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
191 (id->bInterfaceSubClass != uaa->info.bInterfaceSubClass)) {
194 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
195 (id->bInterfaceProtocol != uaa->info.bInterfaceProtocol)) {
198 /* we found a match! */
206 /*------------------------------------------------------------------------*
209 * This function is the FreeBSD probe callback. It is called from the
210 * FreeBSD USB stack through the "device_probe_and_attach()" function.
211 *------------------------------------------------------------------------*/
213 usb_linux_probe(device_t dev)
215 struct usb_attach_arg *uaa = device_get_ivars(dev);
216 struct usb_driver *udrv;
219 if (uaa->usb_mode != USB_MODE_HOST) {
223 LIST_FOREACH(udrv, &usb_linux_driver_list, linux_driver_list) {
224 if (usb_linux_lookup_id(udrv->id_table, uaa)) {
234 /*------------------------------------------------------------------------*
235 * usb_linux_get_usb_driver
237 * This function returns the pointer to the "struct usb_driver" where
238 * the Linux USB device driver "struct usb_device_id" match was found.
239 * We apply a lock before reading out the pointer to avoid races.
240 *------------------------------------------------------------------------*/
241 static struct usb_driver *
242 usb_linux_get_usb_driver(struct usb_linux_softc *sc)
244 struct usb_driver *udrv;
252 /*------------------------------------------------------------------------*
255 * This function is the FreeBSD attach callback. It is called from the
256 * FreeBSD USB stack through the "device_probe_and_attach()" function.
257 * This function is called when "usb_linux_probe()" returns zero.
258 *------------------------------------------------------------------------*/
260 usb_linux_attach(device_t dev)
262 struct usb_attach_arg *uaa = device_get_ivars(dev);
263 struct usb_linux_softc *sc = device_get_softc(dev);
264 struct usb_driver *udrv;
265 const struct usb_device_id *id = NULL;
268 LIST_FOREACH(udrv, &usb_linux_driver_list, linux_driver_list) {
269 id = usb_linux_lookup_id(udrv->id_table, uaa);
278 if (usb_linux_create_usb_device(uaa->device, dev) != 0)
280 device_set_usb_desc(dev);
282 sc->sc_fbsd_udev = uaa->device;
283 sc->sc_fbsd_dev = dev;
285 sc->sc_ui = usb_ifnum_to_if(uaa->device, uaa->info.bIfaceNum);
286 if (sc->sc_ui == NULL) {
290 if ((udrv->probe) (sc->sc_ui, id)) {
295 LIST_INSERT_HEAD(&usb_linux_attached_list, sc, sc_attached_list);
302 /*------------------------------------------------------------------------*
305 * This function is the FreeBSD detach callback. It is called from the
306 * FreeBSD USB stack through the "device_detach()" function.
307 *------------------------------------------------------------------------*/
309 usb_linux_detach(device_t dev)
311 struct usb_linux_softc *sc = device_get_softc(dev);
312 struct usb_driver *udrv = NULL;
315 if (sc->sc_attached_list.le_prev) {
316 LIST_REMOVE(sc, sc_attached_list);
317 sc->sc_attached_list.le_prev = NULL;
323 if (udrv && udrv->disconnect) {
324 (udrv->disconnect) (sc->sc_ui);
327 * Make sure that we free all FreeBSD USB transfers belonging to
328 * this Linux "usb_interface", hence they will most likely not be
331 usb_linux_cleanup_interface(sc->sc_fbsd_udev, sc->sc_ui);
335 /*------------------------------------------------------------------------*
338 * This function is the FreeBSD suspend callback. Usually it does nothing.
339 *------------------------------------------------------------------------*/
341 usb_linux_suspend(device_t dev)
343 struct usb_linux_softc *sc = device_get_softc(dev);
344 struct usb_driver *udrv = usb_linux_get_usb_driver(sc);
347 if (udrv && udrv->suspend) {
348 err = (udrv->suspend) (sc->sc_ui, 0);
353 /*------------------------------------------------------------------------*
356 * This function is the FreeBSD resume callback. Usually it does nothing.
357 *------------------------------------------------------------------------*/
359 usb_linux_resume(device_t dev)
361 struct usb_linux_softc *sc = device_get_softc(dev);
362 struct usb_driver *udrv = usb_linux_get_usb_driver(sc);
365 if (udrv && udrv->resume) {
366 err = (udrv->resume) (sc->sc_ui);
371 /*------------------------------------------------------------------------*
372 * Linux emulation layer
373 *------------------------------------------------------------------------*/
375 /*------------------------------------------------------------------------*
376 * usb_max_isoc_frames
378 * The following function returns the maximum number of isochronous
379 * frames that we support per URB. It is not part of the Linux USB API.
380 *------------------------------------------------------------------------*/
382 usb_max_isoc_frames(struct usb_device *dev)
385 switch (usbd_get_speed(dev)) {
388 return (USB_MAX_FULL_SPEED_ISOC_FRAMES);
390 return (USB_MAX_HIGH_SPEED_ISOC_FRAMES);
394 /*------------------------------------------------------------------------*
397 * This function is used to queue an URB after that it has been
398 * initialized. If it returns non-zero, it means that the URB was not
400 *------------------------------------------------------------------------*/
402 usb_submit_urb(struct urb *urb, uint16_t mem_flags)
404 struct usb_host_endpoint *uhe;
411 do_unlock = mtx_owned(&Giant) ? 0 : 1;
415 if (urb->endpoint == NULL) {
421 * Check to see if the urb is in the process of being killed
422 * and stop a urb that is in the process of being killed from
423 * being re-submitted (e.g. from its completion callback
426 if (urb->kill_count != 0) {
434 * Check that we have got a FreeBSD USB transfer that will dequeue
435 * the URB structure and do the real transfer. If there are no USB
436 * transfers, then we return an error.
438 if (uhe->bsd_xfer[0] ||
442 TAILQ_INSERT_TAIL(&uhe->bsd_urb_list, urb, bsd_urb_list);
444 urb->status = -EINPROGRESS;
446 usbd_transfer_start(uhe->bsd_xfer[0]);
447 usbd_transfer_start(uhe->bsd_xfer[1]);
450 /* no pipes have been setup yet! */
451 urb->status = -EINVAL;
460 /*------------------------------------------------------------------------*
463 * This function is used to stop an URB after that it is been
464 * submitted, but before the "complete" callback has been called. On
465 *------------------------------------------------------------------------*/
467 usb_unlink_urb(struct urb *urb)
469 return (usb_unlink_urb_sub(urb, 0));
473 usb_unlink_bsd(struct usb_xfer *xfer,
474 struct urb *urb, uint8_t drain)
478 if (!usbd_transfer_pending(xfer))
480 if (xfer->priv_fifo == (void *)urb) {
483 usbd_transfer_drain(xfer);
486 usbd_transfer_stop(xfer);
488 usbd_transfer_start(xfer);
493 usb_unlink_urb_sub(struct urb *urb, uint8_t drain)
495 struct usb_host_endpoint *uhe;
503 do_unlock = mtx_owned(&Giant) ? 0 : 1;
509 if (urb->endpoint == NULL) {
515 if (urb->bsd_urb_list.tqe_prev) {
517 /* not started yet, just remove it from the queue */
518 TAILQ_REMOVE(&uhe->bsd_urb_list, urb, bsd_urb_list);
519 urb->bsd_urb_list.tqe_prev = NULL;
520 urb->status = -ECONNRESET;
521 urb->actual_length = 0;
523 for (x = 0; x < urb->number_of_packets; x++) {
524 urb->iso_frame_desc[x].actual_length = 0;
528 (urb->complete) (urb);
533 * If the URB is not on the URB list, then check if one of
534 * the FreeBSD USB transfer are processing the current URB.
535 * If so, re-start that transfer, which will lead to the
536 * termination of that URB:
538 usb_unlink_bsd(uhe->bsd_xfer[0], urb, drain);
539 usb_unlink_bsd(uhe->bsd_xfer[1], urb, drain);
550 /*------------------------------------------------------------------------*
553 * This function must always be used to clear the stall. Stall is when
554 * an USB endpoint returns a stall message to the USB host controller.
555 * Until the stall is cleared, no data can be transferred.
556 *------------------------------------------------------------------------*/
558 usb_clear_halt(struct usb_device *dev, struct usb_host_endpoint *uhe)
560 struct usb_config cfg[1];
561 struct usb_endpoint *ep;
568 type = uhe->desc.bmAttributes & UE_XFERTYPE;
569 addr = uhe->desc.bEndpointAddress;
571 memset(cfg, 0, sizeof(cfg));
574 cfg[0].endpoint = addr & UE_ADDR;
575 cfg[0].direction = addr & (UE_DIR_OUT | UE_DIR_IN);
577 ep = usbd_get_endpoint(dev, uhe->bsd_iface_index, cfg);
581 usbd_clear_data_toggle(dev, ep);
583 return (usb_control_msg(dev, &dev->ep0,
584 UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT,
585 UF_ENDPOINT_HALT, addr, NULL, 0, 1000));
588 /*------------------------------------------------------------------------*
591 * This is an internal function that is used to perform synchronous
592 * Linux USB transfers.
593 *------------------------------------------------------------------------*/
595 usb_start_wait_urb(struct urb *urb, usb_timeout_t timeout, uint16_t *p_actlen)
600 /* you must have a timeout! */
604 urb->complete = &usb_linux_wait_complete;
605 urb->timeout = timeout;
606 urb->transfer_flags |= URB_WAIT_WAKEUP;
607 urb->transfer_flags &= ~URB_IS_SLEEPING;
609 do_unlock = mtx_owned(&Giant) ? 0 : 1;
612 err = usb_submit_urb(urb, 0);
617 * the URB might have completed before we get here, so check that by
620 while (urb->transfer_flags & URB_WAIT_WAKEUP) {
621 urb->transfer_flags |= URB_IS_SLEEPING;
622 cv_wait(&urb->cv_wait, &Giant);
623 urb->transfer_flags &= ~URB_IS_SLEEPING;
631 if (p_actlen != NULL) {
635 *p_actlen = urb->actual_length;
640 /*------------------------------------------------------------------------*
643 * The following function performs a control transfer sequence one any
644 * control, bulk or interrupt endpoint, specified by "uhe". A control
645 * transfer means that you transfer an 8-byte header first followed by
646 * a data-phase as indicated by the 8-byte header. The "timeout" is
647 * given in milliseconds.
653 *------------------------------------------------------------------------*/
655 usb_control_msg(struct usb_device *dev, struct usb_host_endpoint *uhe,
656 uint8_t request, uint8_t requesttype,
657 uint16_t value, uint16_t index, void *data,
658 uint16_t size, usb_timeout_t timeout)
660 struct usb_device_request req;
667 req.bmRequestType = requesttype;
668 req.bRequest = request;
669 USETW(req.wValue, value);
670 USETW(req.wIndex, index);
671 USETW(req.wLength, size);
676 type = (uhe->desc.bmAttributes & UE_XFERTYPE);
677 addr = (uhe->desc.bEndpointAddress & UE_ADDR);
679 if (type != UE_CONTROL) {
684 * The FreeBSD USB stack supports standard control
685 * transfers on control endpoint zero:
687 err = usbd_do_request_flags(dev,
688 NULL, &req, data, USB_SHORT_XFER_OK,
697 if (dev->flags.usb_mode != USB_MODE_HOST) {
701 err = usb_setup_endpoint(dev, uhe, 1 /* dummy */ );
704 * NOTE: we need to allocate real memory here so that we don't
705 * transfer data to/from the stack!
707 * 0xFFFF is a FreeBSD specific magic value.
709 urb = usb_alloc_urb(0xFFFF, size);
716 memcpy(urb->setup_packet, &req, sizeof(req));
718 if (size && (!(req.bmRequestType & UT_READ))) {
719 /* move the data to a real buffer */
720 memcpy(USB_ADD_BYTES(urb->setup_packet, sizeof(req)),
723 err = usb_start_wait_urb(urb, timeout, &actlen);
725 if (req.bmRequestType & UT_READ) {
727 bcopy(USB_ADD_BYTES(urb->setup_packet,
728 sizeof(req)), data, actlen);
739 /*------------------------------------------------------------------------*
742 * The following function will select which alternate setting of an
743 * USB interface you plan to use. By default alternate setting with
744 * index zero is selected. Note that "iface_no" is not the interface
745 * index, but rather the value of "bInterfaceNumber".
746 *------------------------------------------------------------------------*/
748 usb_set_interface(struct usb_device *dev, uint8_t iface_no, uint8_t alt_index)
750 struct usb_interface *p_ui = usb_ifnum_to_if(dev, iface_no);
755 if (alt_index >= p_ui->num_altsetting)
757 usb_linux_cleanup_interface(dev, p_ui);
758 err = -usbd_set_alt_interface_index(dev,
759 p_ui->bsd_iface_index, alt_index);
761 p_ui->cur_altsetting = p_ui->altsetting + alt_index;
766 /*------------------------------------------------------------------------*
769 * The following function is an extension to the Linux USB API that
770 * allows you to set a maximum buffer size for a given USB endpoint.
771 * The maximum buffer size is per URB. If you don't call this function
772 * to set a maximum buffer size, the endpoint will not be functional.
773 * Note that for isochronous endpoints the maximum buffer size must be
774 * a non-zero dummy, hence this function will base the maximum buffer
775 * size on "wMaxPacketSize".
776 *------------------------------------------------------------------------*/
778 usb_setup_endpoint(struct usb_device *dev,
779 struct usb_host_endpoint *uhe, usb_size_t bufsize)
781 struct usb_config cfg[2];
782 uint8_t type = uhe->desc.bmAttributes & UE_XFERTYPE;
783 uint8_t addr = uhe->desc.bEndpointAddress;
785 if (uhe->fbsd_buf_size == bufsize) {
789 usbd_transfer_unsetup(uhe->bsd_xfer, 2);
791 uhe->fbsd_buf_size = bufsize;
796 memset(cfg, 0, sizeof(cfg));
798 if (type == UE_ISOCHRONOUS) {
801 * Isochronous transfers are special in that they don't fit
802 * into the BULK/INTR/CONTROL transfer model.
806 cfg[0].endpoint = addr & UE_ADDR;
807 cfg[0].direction = addr & (UE_DIR_OUT | UE_DIR_IN);
808 cfg[0].callback = &usb_linux_isoc_callback;
809 cfg[0].bufsize = 0; /* use wMaxPacketSize */
810 cfg[0].frames = usb_max_isoc_frames(dev);
811 cfg[0].flags.proxy_buffer = 1;
814 * The Linux USB API allows non back-to-back
815 * isochronous frames which we do not support. If the
816 * isochronous frames are not back-to-back we need to
817 * do a copy, and then we need a buffer for
818 * that. Enable this at your own risk.
820 cfg[0].flags.ext_buffer = 1;
822 cfg[0].flags.short_xfer_ok = 1;
824 bcopy(cfg, cfg + 1, sizeof(*cfg));
826 /* Allocate and setup two generic FreeBSD USB transfers */
828 if (usbd_transfer_setup(dev, &uhe->bsd_iface_index,
829 uhe->bsd_xfer, cfg, 2, uhe, &Giant)) {
833 if (bufsize > (1 << 22)) {
834 /* limit buffer size */
837 /* Allocate and setup one generic FreeBSD USB transfer */
840 cfg[0].endpoint = addr & UE_ADDR;
841 cfg[0].direction = addr & (UE_DIR_OUT | UE_DIR_IN);
842 cfg[0].callback = &usb_linux_non_isoc_callback;
843 cfg[0].bufsize = bufsize;
844 cfg[0].flags.ext_buffer = 1; /* enable zero-copy */
845 cfg[0].flags.proxy_buffer = 1;
846 cfg[0].flags.short_xfer_ok = 1;
848 if (usbd_transfer_setup(dev, &uhe->bsd_iface_index,
849 uhe->bsd_xfer, cfg, 1, uhe, &Giant)) {
856 /*------------------------------------------------------------------------*
857 * usb_linux_create_usb_device
859 * The following function is used to build up a per USB device
860 * structure tree, that mimics the Linux one. The root structure
861 * is returned by this function.
862 *------------------------------------------------------------------------*/
864 usb_linux_create_usb_device(struct usb_device *udev, device_t dev)
866 struct usb_config_descriptor *cd = usbd_get_config_descriptor(udev);
867 struct usb_descriptor *desc;
868 struct usb_interface_descriptor *id;
869 struct usb_endpoint_descriptor *ed;
870 struct usb_interface *p_ui = NULL;
871 struct usb_host_interface *p_uhi = NULL;
872 struct usb_host_endpoint *p_uhe = NULL;
874 uint16_t niface_total;
876 uint16_t iface_no_curr;
877 uint16_t iface_index;
882 * We do two passes. One pass for computing necessary memory size
883 * and one pass to initialize all the allocated memory structures.
885 for (pass = 0; pass < 2; pass++) {
887 iface_no_curr = 0xFFFF;
894 * Iterate over all the USB descriptors. Use the USB config
895 * descriptor pointer provided by the FreeBSD USB stack.
897 while ((desc = usb_desc_foreach(cd, desc))) {
900 * Build up a tree according to the descriptors we
903 switch (desc->bDescriptorType) {
909 if ((ed->bLength < sizeof(*ed)) ||
913 bcopy(ed, &p_uhe->desc, sizeof(p_uhe->desc));
914 p_uhe->bsd_iface_index = iface_index - 1;
915 TAILQ_INIT(&p_uhe->bsd_urb_list);
919 (p_uhi - 1)->desc.bNumEndpoints++;
924 case UDESC_INTERFACE:
926 if (id->bLength < sizeof(*id))
929 bcopy(id, &p_uhi->desc, sizeof(p_uhi->desc));
930 p_uhi->desc.bNumEndpoints = 0;
931 p_uhi->endpoint = p_uhe;
933 p_uhi->bsd_iface_index = iface_index;
936 iface_no = id->bInterfaceNumber;
938 if (iface_no_curr != iface_no) {
940 p_ui->altsetting = p_uhi - 1;
941 p_ui->cur_altsetting = p_uhi - 1;
942 p_ui->num_altsetting = 1;
943 p_ui->bsd_iface_index = iface_index;
944 p_ui->linux_udev = udev;
947 iface_no_curr = iface_no;
951 (p_ui - 1)->num_altsetting++;
963 size = (sizeof(*p_uhe) * nedesc) +
964 (sizeof(*p_ui) * iface_index) +
965 (sizeof(*p_uhi) * niface_total);
967 p_uhe = malloc(size, M_USBDEV, M_WAITOK | M_ZERO);
968 p_ui = (void *)(p_uhe + nedesc);
969 p_uhi = (void *)(p_ui + iface_index);
971 udev->linux_iface_start = p_ui;
972 udev->linux_iface_end = p_ui + iface_index;
973 udev->linux_endpoint_start = p_uhe;
974 udev->linux_endpoint_end = p_uhe + nedesc;
975 udev->devnum = device_get_unit(dev);
976 bcopy(&udev->ddesc, &udev->descriptor,
977 sizeof(udev->descriptor));
978 bcopy(udev->ctrl_ep.edesc, &udev->ep0.desc,
979 sizeof(udev->ep0.desc));
985 /*------------------------------------------------------------------------*
988 * This function should always be used when you allocate an URB for
989 * use with the USB Linux stack. In case of an isochronous transfer
990 * you must specifiy the maximum number of "iso_packets" which you
991 * plan to transfer per URB. This function is always blocking, and
992 * "mem_flags" are not regarded like on Linux.
993 *------------------------------------------------------------------------*/
995 usb_alloc_urb(uint16_t iso_packets, uint16_t mem_flags)
1000 if (iso_packets == 0xFFFF) {
1002 * FreeBSD specific magic value to ask for control transfer
1003 * memory allocation:
1005 size = sizeof(*urb) + sizeof(struct usb_device_request) + mem_flags;
1007 size = sizeof(*urb) + (iso_packets * sizeof(urb->iso_frame_desc[0]));
1010 urb = malloc(size, M_USBDEV, M_WAITOK | M_ZERO);
1013 cv_init(&urb->cv_wait, "URBWAIT");
1014 if (iso_packets == 0xFFFF) {
1015 urb->setup_packet = (void *)(urb + 1);
1016 urb->transfer_buffer = (void *)(urb->setup_packet +
1017 sizeof(struct usb_device_request));
1019 urb->number_of_packets = iso_packets;
1025 /*------------------------------------------------------------------------*
1026 * usb_find_host_endpoint
1028 * The following function will return the Linux USB host endpoint
1029 * structure that matches the given endpoint type and endpoint
1030 * value. If no match is found, NULL is returned. This function is not
1031 * part of the Linux USB API and is only used internally.
1032 *------------------------------------------------------------------------*/
1033 struct usb_host_endpoint *
1034 usb_find_host_endpoint(struct usb_device *dev, uint8_t type, uint8_t ep)
1036 struct usb_host_endpoint *uhe;
1037 struct usb_host_endpoint *uhe_end;
1038 struct usb_host_interface *uhi;
1039 struct usb_interface *ui;
1047 if (type == UE_CONTROL) {
1050 mask = (UE_DIR_IN | UE_DIR_OUT | UE_ADDR);
1056 * Iterate over all the interfaces searching the selected alternate
1057 * setting only, and all belonging endpoints.
1059 for (ui = dev->linux_iface_start;
1060 ui != dev->linux_iface_end;
1062 uhi = ui->cur_altsetting;
1064 uhe_end = uhi->endpoint + uhi->desc.bNumEndpoints;
1065 for (uhe = uhi->endpoint;
1068 ea = uhe->desc.bEndpointAddress;
1069 at = uhe->desc.bmAttributes;
1071 if (((ea & mask) == ep) &&
1072 ((at & UE_XFERTYPE) == type)) {
1079 if ((type == UE_CONTROL) && ((ep & UE_ADDR) == 0)) {
1085 /*------------------------------------------------------------------------*
1086 * usb_altnum_to_altsetting
1088 * The following function returns a pointer to an alternate setting by
1089 * index given a "usb_interface" pointer. If the alternate setting by
1090 * index does not exist, NULL is returned. And alternate setting is a
1091 * variant of an interface, but usually with slightly different
1093 *------------------------------------------------------------------------*/
1094 struct usb_host_interface *
1095 usb_altnum_to_altsetting(const struct usb_interface *intf, uint8_t alt_index)
1097 if (alt_index >= intf->num_altsetting) {
1100 return (intf->altsetting + alt_index);
1103 /*------------------------------------------------------------------------*
1106 * The following function searches up an USB interface by
1107 * "bInterfaceNumber". If no match is found, NULL is returned.
1108 *------------------------------------------------------------------------*/
1109 struct usb_interface *
1110 usb_ifnum_to_if(struct usb_device *dev, uint8_t iface_no)
1112 struct usb_interface *p_ui;
1114 for (p_ui = dev->linux_iface_start;
1115 p_ui != dev->linux_iface_end;
1117 if ((p_ui->num_altsetting > 0) &&
1118 (p_ui->altsetting->desc.bInterfaceNumber == iface_no)) {
1125 /*------------------------------------------------------------------------*
1127 *------------------------------------------------------------------------*/
1129 usb_buffer_alloc(struct usb_device *dev, usb_size_t size, uint16_t mem_flags, uint8_t *dma_addr)
1131 return (malloc(size, M_USBDEV, M_WAITOK | M_ZERO));
1134 /*------------------------------------------------------------------------*
1136 *------------------------------------------------------------------------*/
1138 usbd_get_intfdata(struct usb_interface *intf)
1140 return (intf->bsd_priv_sc);
1143 /*------------------------------------------------------------------------*
1144 * usb_linux_register
1146 * The following function is used by the "USB_DRIVER_EXPORT()" macro,
1147 * and is used to register a Linux USB driver, so that its
1148 * "usb_device_id" structures gets searched a probe time. This
1149 * function is not part of the Linux USB API, and is for internal use
1151 *------------------------------------------------------------------------*/
1153 usb_linux_register(void *arg)
1155 struct usb_driver *drv = arg;
1158 LIST_INSERT_HEAD(&usb_linux_driver_list, drv, linux_driver_list);
1161 usb_needs_explore_all();
1164 /*------------------------------------------------------------------------*
1165 * usb_linux_deregister
1167 * The following function is used by the "USB_DRIVER_EXPORT()" macro,
1168 * and is used to deregister a Linux USB driver. This function will
1169 * ensure that all driver instances belonging to the Linux USB device
1170 * driver in question, gets detached before the driver is
1171 * unloaded. This function is not part of the Linux USB API, and is
1172 * for internal use only.
1173 *------------------------------------------------------------------------*/
1175 usb_linux_deregister(void *arg)
1177 struct usb_driver *drv = arg;
1178 struct usb_linux_softc *sc;
1182 LIST_FOREACH(sc, &usb_linux_attached_list, sc_attached_list) {
1183 if (sc->sc_udrv == drv) {
1185 device_detach(sc->sc_fbsd_dev);
1189 LIST_REMOVE(drv, linux_driver_list);
1193 /*------------------------------------------------------------------------*
1194 * usb_linux_free_device
1196 * The following function is only used by the FreeBSD USB stack, to
1197 * cleanup and free memory after that a Linux USB device was attached.
1198 *------------------------------------------------------------------------*/
1200 usb_linux_free_device(struct usb_device *dev)
1202 struct usb_host_endpoint *uhe;
1203 struct usb_host_endpoint *uhe_end;
1206 uhe = dev->linux_endpoint_start;
1207 uhe_end = dev->linux_endpoint_end;
1208 while (uhe != uhe_end) {
1209 err = usb_setup_endpoint(dev, uhe, 0);
1212 err = usb_setup_endpoint(dev, &dev->ep0, 0);
1213 free(dev->linux_endpoint_start, M_USBDEV);
1216 /*------------------------------------------------------------------------*
1218 *------------------------------------------------------------------------*/
1220 usb_buffer_free(struct usb_device *dev, usb_size_t size,
1221 void *addr, uint8_t dma_addr)
1223 free(addr, M_USBDEV);
1226 /*------------------------------------------------------------------------*
1228 *------------------------------------------------------------------------*/
1230 usb_free_urb(struct urb *urb)
1235 /* make sure that the current URB is not active */
1238 /* destroy condition variable */
1239 cv_destroy(&urb->cv_wait);
1242 free(urb, M_USBDEV);
1245 /*------------------------------------------------------------------------*
1248 * The following function can be used to initialize a custom URB. It
1249 * is not recommended to use this function. Use "usb_alloc_urb()"
1251 *------------------------------------------------------------------------*/
1253 usb_init_urb(struct urb *urb)
1258 memset(urb, 0, sizeof(*urb));
1261 /*------------------------------------------------------------------------*
1263 *------------------------------------------------------------------------*/
1265 usb_kill_urb(struct urb *urb)
1267 usb_unlink_urb_sub(urb, 1);
1270 /*------------------------------------------------------------------------*
1273 * The following function sets the per Linux USB interface private
1274 * data pointer. It is used by most Linux USB device drivers.
1275 *------------------------------------------------------------------------*/
1277 usb_set_intfdata(struct usb_interface *intf, void *data)
1279 intf->bsd_priv_sc = data;
1282 /*------------------------------------------------------------------------*
1283 * usb_linux_cleanup_interface
1285 * The following function will release all FreeBSD USB transfers
1286 * associated with a Linux USB interface. It is for internal use only.
1287 *------------------------------------------------------------------------*/
1289 usb_linux_cleanup_interface(struct usb_device *dev, struct usb_interface *iface)
1291 struct usb_host_interface *uhi;
1292 struct usb_host_interface *uhi_end;
1293 struct usb_host_endpoint *uhe;
1294 struct usb_host_endpoint *uhe_end;
1297 uhi = iface->altsetting;
1298 uhi_end = iface->altsetting + iface->num_altsetting;
1299 while (uhi != uhi_end) {
1300 uhe = uhi->endpoint;
1301 uhe_end = uhi->endpoint + uhi->desc.bNumEndpoints;
1302 while (uhe != uhe_end) {
1303 err = usb_setup_endpoint(dev, uhe, 0);
1310 /*------------------------------------------------------------------------*
1311 * usb_linux_wait_complete
1313 * The following function is used by "usb_start_wait_urb()" to wake it
1314 * up, when an USB transfer has finished.
1315 *------------------------------------------------------------------------*/
1317 usb_linux_wait_complete(struct urb *urb)
1319 if (urb->transfer_flags & URB_IS_SLEEPING) {
1320 cv_signal(&urb->cv_wait);
1322 urb->transfer_flags &= ~URB_WAIT_WAKEUP;
1325 /*------------------------------------------------------------------------*
1326 * usb_linux_complete
1327 *------------------------------------------------------------------------*/
1329 usb_linux_complete(struct usb_xfer *xfer)
1333 urb = usbd_xfer_get_priv(xfer);
1334 usbd_xfer_set_priv(xfer, NULL);
1335 if (urb->complete) {
1336 (urb->complete) (urb);
1340 /*------------------------------------------------------------------------*
1341 * usb_linux_isoc_callback
1343 * The following is the FreeBSD isochronous USB callback. Isochronous
1344 * frames are USB packets transferred 1000 or 8000 times per second,
1345 * depending on whether a full- or high- speed USB transfer is
1347 *------------------------------------------------------------------------*/
1349 usb_linux_isoc_callback(struct usb_xfer *xfer, usb_error_t error)
1351 usb_frlength_t max_frame = xfer->max_frame_size;
1352 usb_frlength_t offset;
1354 struct urb *urb = usbd_xfer_get_priv(xfer);
1355 struct usb_host_endpoint *uhe = usbd_xfer_softc(xfer);
1356 struct usb_iso_packet_descriptor *uipd;
1360 switch (USB_GET_STATE(xfer)) {
1361 case USB_ST_TRANSFERRED:
1363 if (urb->bsd_isread) {
1365 /* copy in data with regard to the URB */
1369 for (x = 0; x < urb->number_of_packets; x++) {
1370 uipd = urb->iso_frame_desc + x;
1371 if (uipd->length > xfer->frlengths[x]) {
1372 if (urb->transfer_flags & URB_SHORT_NOT_OK) {
1373 /* XXX should be EREMOTEIO */
1374 uipd->status = -EPIPE;
1381 uipd->actual_length = xfer->frlengths[x];
1382 if (!xfer->flags.ext_buffer) {
1383 usbd_copy_out(xfer->frbuffers, offset,
1384 USB_ADD_BYTES(urb->transfer_buffer,
1385 uipd->offset), uipd->actual_length);
1387 offset += max_frame;
1390 for (x = 0; x < urb->number_of_packets; x++) {
1391 uipd = urb->iso_frame_desc + x;
1392 uipd->actual_length = xfer->frlengths[x];
1397 urb->actual_length = xfer->actlen;
1399 /* check for short transfer */
1400 if (xfer->actlen < xfer->sumlen) {
1401 /* short transfer */
1402 if (urb->transfer_flags & URB_SHORT_NOT_OK) {
1403 /* XXX should be EREMOTEIO */
1404 urb->status = -EPIPE;
1414 usb_linux_complete(xfer);
1419 if (xfer->priv_fifo == NULL) {
1421 /* get next transfer */
1422 urb = TAILQ_FIRST(&uhe->bsd_urb_list);
1427 TAILQ_REMOVE(&uhe->bsd_urb_list, urb, bsd_urb_list);
1428 urb->bsd_urb_list.tqe_prev = NULL;
1430 x = xfer->max_frame_count;
1431 if (urb->number_of_packets > x) {
1432 /* XXX simply truncate the transfer */
1433 urb->number_of_packets = x;
1436 DPRINTF("Already got a transfer\n");
1438 /* already got a transfer (should not happen) */
1439 urb = usbd_xfer_get_priv(xfer);
1442 urb->bsd_isread = (uhe->desc.bEndpointAddress & UE_DIR_IN) ? 1 : 0;
1444 if (xfer->flags.ext_buffer) {
1445 /* set virtual address to load */
1446 usbd_xfer_set_frame_data(xfer, 0, urb->transfer_buffer, 0);
1448 if (!(urb->bsd_isread)) {
1450 /* copy out data with regard to the URB */
1454 for (x = 0; x < urb->number_of_packets; x++) {
1455 uipd = urb->iso_frame_desc + x;
1456 usbd_xfer_set_frame_len(xfer, x, uipd->length);
1457 if (!xfer->flags.ext_buffer) {
1458 usbd_copy_in(xfer->frbuffers, offset,
1459 USB_ADD_BYTES(urb->transfer_buffer,
1460 uipd->offset), uipd->length);
1462 offset += uipd->length;
1467 * compute the transfer length into the "offset"
1471 offset = urb->number_of_packets * max_frame;
1473 /* setup "frlengths" array */
1475 for (x = 0; x < urb->number_of_packets; x++) {
1476 uipd = urb->iso_frame_desc + x;
1477 usbd_xfer_set_frame_len(xfer, x, max_frame);
1480 usbd_xfer_set_priv(xfer, urb);
1481 xfer->flags.force_short_xfer = 0;
1482 xfer->timeout = urb->timeout;
1483 xfer->nframes = urb->number_of_packets;
1484 usbd_transfer_submit(xfer);
1487 default: /* Error */
1488 if (xfer->error == USB_ERR_CANCELLED) {
1489 urb->status = -ECONNRESET;
1491 urb->status = -EPIPE; /* stalled */
1494 /* Set zero for "actual_length" */
1495 urb->actual_length = 0;
1497 /* Set zero for "actual_length" */
1498 for (x = 0; x < urb->number_of_packets; x++) {
1499 urb->iso_frame_desc[x].actual_length = 0;
1500 urb->iso_frame_desc[x].status = urb->status;
1504 usb_linux_complete(xfer);
1506 if (xfer->error == USB_ERR_CANCELLED) {
1507 /* we need to return in this case */
1515 /*------------------------------------------------------------------------*
1516 * usb_linux_non_isoc_callback
1518 * The following is the FreeBSD BULK/INTERRUPT and CONTROL USB
1519 * callback. It dequeues Linux USB stack compatible URB's, transforms
1520 * the URB fields into a FreeBSD USB transfer, and defragments the USB
1521 * transfer as required. When the transfer is complete the "complete"
1522 * callback is called.
1523 *------------------------------------------------------------------------*/
1525 usb_linux_non_isoc_callback(struct usb_xfer *xfer, usb_error_t error)
1528 REQ_SIZE = sizeof(struct usb_device_request)
1530 struct urb *urb = usbd_xfer_get_priv(xfer);
1531 struct usb_host_endpoint *uhe = usbd_xfer_softc(xfer);
1533 usb_frlength_t max_bulk = usbd_xfer_max_len(xfer);
1534 uint8_t data_frame = xfer->flags_int.control_xfr ? 1 : 0;
1538 switch (USB_GET_STATE(xfer)) {
1539 case USB_ST_TRANSFERRED:
1541 if (xfer->flags_int.control_xfr) {
1543 /* don't transfer the setup packet again: */
1545 usbd_xfer_set_frame_len(xfer, 0, 0);
1547 if (urb->bsd_isread && (!xfer->flags.ext_buffer)) {
1548 /* copy in data with regard to the URB */
1549 usbd_copy_out(xfer->frbuffers + data_frame, 0,
1550 urb->bsd_data_ptr, xfer->frlengths[data_frame]);
1552 urb->bsd_length_rem -= xfer->frlengths[data_frame];
1553 urb->bsd_data_ptr += xfer->frlengths[data_frame];
1554 urb->actual_length += xfer->frlengths[data_frame];
1556 /* check for short transfer */
1557 if (xfer->actlen < xfer->sumlen) {
1558 urb->bsd_length_rem = 0;
1560 /* short transfer */
1561 if (urb->transfer_flags & URB_SHORT_NOT_OK) {
1562 urb->status = -EPIPE;
1567 /* check remainder */
1568 if (urb->bsd_length_rem > 0) {
1576 usb_linux_complete(xfer);
1580 /* get next transfer */
1581 urb = TAILQ_FIRST(&uhe->bsd_urb_list);
1586 TAILQ_REMOVE(&uhe->bsd_urb_list, urb, bsd_urb_list);
1587 urb->bsd_urb_list.tqe_prev = NULL;
1589 usbd_xfer_set_priv(xfer, urb);
1590 xfer->flags.force_short_xfer = 0;
1591 xfer->timeout = urb->timeout;
1593 if (xfer->flags_int.control_xfr) {
1596 * USB control transfers need special handling.
1597 * First copy in the header, then copy in data!
1599 if (!xfer->flags.ext_buffer) {
1600 usbd_copy_in(xfer->frbuffers, 0,
1601 urb->setup_packet, REQ_SIZE);
1602 usbd_xfer_set_frame_len(xfer, 0, REQ_SIZE);
1604 /* set virtual address to load */
1605 usbd_xfer_set_frame_data(xfer, 0,
1606 urb->setup_packet, REQ_SIZE);
1609 ptr = urb->setup_packet;
1611 /* setup data transfer direction and length */
1612 urb->bsd_isread = (ptr[0] & UT_READ) ? 1 : 0;
1613 urb->bsd_length_rem = ptr[6] | (ptr[7] << 8);
1617 /* setup data transfer direction */
1619 urb->bsd_length_rem = urb->transfer_buffer_length;
1620 urb->bsd_isread = (uhe->desc.bEndpointAddress &
1624 urb->bsd_data_ptr = urb->transfer_buffer;
1625 urb->actual_length = 0;
1628 if (max_bulk > urb->bsd_length_rem) {
1629 max_bulk = urb->bsd_length_rem;
1631 /* check if we need to force a short transfer */
1633 if ((max_bulk == urb->bsd_length_rem) &&
1634 (urb->transfer_flags & URB_ZERO_PACKET) &&
1635 (!xfer->flags_int.control_xfr)) {
1636 xfer->flags.force_short_xfer = 1;
1638 /* check if we need to copy in data */
1640 if (xfer->flags.ext_buffer) {
1641 /* set virtual address to load */
1642 usbd_xfer_set_frame_data(xfer, data_frame,
1643 urb->bsd_data_ptr, max_bulk);
1644 } else if (!urb->bsd_isread) {
1645 /* copy out data with regard to the URB */
1646 usbd_copy_in(xfer->frbuffers + data_frame, 0,
1647 urb->bsd_data_ptr, max_bulk);
1648 usbd_xfer_set_frame_len(xfer, data_frame, max_bulk);
1650 if (xfer->flags_int.control_xfr) {
1659 usbd_transfer_submit(xfer);
1663 if (xfer->error == USB_ERR_CANCELLED) {
1664 urb->status = -ECONNRESET;
1666 urb->status = -EPIPE;
1669 /* Set zero for "actual_length" */
1670 urb->actual_length = 0;
1673 usb_linux_complete(xfer);
1675 if (xfer->error == USB_ERR_CANCELLED) {
1676 /* we need to return in this case */
1683 /*------------------------------------------------------------------------*
1685 *------------------------------------------------------------------------*/
1687 usb_fill_bulk_urb(struct urb *urb, struct usb_device *udev,
1688 struct usb_host_endpoint *uhe, void *buf,
1689 int length, usb_complete_t callback, void *arg)
1692 urb->endpoint = uhe;
1693 urb->transfer_buffer = buf;
1694 urb->transfer_buffer_length = length;
1695 urb->complete = callback;
1699 /*------------------------------------------------------------------------*
1702 * NOTE: This function can also be used for interrupt endpoints!
1707 *------------------------------------------------------------------------*/
1709 usb_bulk_msg(struct usb_device *udev, struct usb_host_endpoint *uhe,
1710 void *data, int len, uint16_t *pactlen, usb_timeout_t timeout)
1720 err = usb_setup_endpoint(udev, uhe, 4096 /* bytes */);
1724 urb = usb_alloc_urb(0, 0);
1728 usb_fill_bulk_urb(urb, udev, uhe, data, len,
1729 usb_linux_wait_complete, NULL);
1731 err = usb_start_wait_urb(urb, timeout, pactlen);
1737 MODULE_DEPEND(linuxkpi, usb, 1, 1, 1);
1740 usb_linux_init(void *arg)
1742 /* register our function */
1743 usb_linux_free_device_p = &usb_linux_free_device;
1745 SYSINIT(usb_linux_init, SI_SUB_LOCK, SI_ORDER_FIRST, usb_linux_init, NULL);
1746 SYSUNINIT(usb_linux_unload, SI_SUB_LOCK, SI_ORDER_ANY, usb_linux_unload, NULL);