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 <dev/usb/usb_mfunc.h>
29 #include <dev/usb/usb.h>
30 #include <dev/usb/usb_error.h>
31 #include <dev/usb/usb_ioctl.h>
33 #define USB_DEBUG_VAR usb2_debug
35 #include <dev/usb/usb_core.h>
36 #include <dev/usb/usb_compat_linux.h>
37 #include <dev/usb/usb_process.h>
38 #include <dev/usb/usb_device.h>
39 #include <dev/usb/usb_util.h>
40 #include <dev/usb/usb_busdma.h>
41 #include <dev/usb/usb_transfer.h>
42 #include <dev/usb/usb_parse.h>
43 #include <dev/usb/usb_hub.h>
44 #include <dev/usb/usb_request.h>
45 #include <dev/usb/usb_debug.h>
47 struct usb_linux_softc {
48 LIST_ENTRY(usb_linux_softc) sc_attached_list;
51 struct usb_device *sc_fbsd_udev;
52 struct usb_interface *sc_ui;
53 struct usb_driver *sc_udrv;
57 static device_probe_t usb_linux_probe;
58 static device_attach_t usb_linux_attach;
59 static device_detach_t usb_linux_detach;
60 static device_suspend_t usb_linux_suspend;
61 static device_resume_t usb_linux_resume;
63 static usb_callback_t usb_linux_isoc_callback;
64 static usb_callback_t usb_linux_non_isoc_callback;
66 static usb_complete_t usb_linux_wait_complete;
68 static uint16_t usb_max_isoc_frames(struct usb_device *);
69 static int usb_start_wait_urb(struct urb *, usb_timeout_t, uint16_t *);
70 static const struct usb_device_id *usb_linux_lookup_id(
71 const struct usb_device_id *, struct usb_attach_arg *);
72 static struct usb_driver *usb_linux_get_usb_driver(struct usb_linux_softc *);
73 static int usb_linux_create_usb_device(struct usb_device *, device_t);
74 static void usb_linux_cleanup_interface(struct usb_device *,
75 struct usb_interface *);
76 static void usb_linux_complete(struct usb_xfer *);
77 static int usb_unlink_urb_sub(struct urb *, uint8_t);
79 /*------------------------------------------------------------------------*
80 * FreeBSD USB interface
81 *------------------------------------------------------------------------*/
83 static LIST_HEAD(, usb_linux_softc) usb_linux_attached_list;
84 static LIST_HEAD(, usb_driver) usb_linux_driver_list;
86 static device_method_t usb_linux_methods[] = {
87 /* Device interface */
88 DEVMETHOD(device_probe, usb_linux_probe),
89 DEVMETHOD(device_attach, usb_linux_attach),
90 DEVMETHOD(device_detach, usb_linux_detach),
91 DEVMETHOD(device_suspend, usb_linux_suspend),
92 DEVMETHOD(device_resume, usb_linux_resume),
97 static driver_t usb_linux_driver = {
99 .methods = usb_linux_methods,
100 .size = sizeof(struct usb_linux_softc),
103 static devclass_t usb_linux_devclass;
105 DRIVER_MODULE(usb_linux, uhub, usb_linux_driver, usb_linux_devclass, NULL, 0);
107 /*------------------------------------------------------------------------*
108 * usb_linux_lookup_id
110 * This functions takes an array of "struct usb_device_id" and tries
111 * to match the entries with the information in "struct usb_attach_arg".
112 * If it finds a match the matching entry will be returned.
113 * Else "NULL" will be returned.
114 *------------------------------------------------------------------------*/
115 static const struct usb_device_id *
116 usb_linux_lookup_id(const struct usb_device_id *id, struct usb_attach_arg *uaa)
122 * Keep on matching array entries until we find one with
123 * "match_flags" equal to zero, which indicates the end of the
126 for (; id->match_flags; id++) {
128 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
129 (id->idVendor != uaa->info.idVendor)) {
132 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
133 (id->idProduct != uaa->info.idProduct)) {
136 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
137 (id->bcdDevice_lo > uaa->info.bcdDevice)) {
140 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
141 (id->bcdDevice_hi < uaa->info.bcdDevice)) {
144 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
145 (id->bDeviceClass != uaa->info.bDeviceClass)) {
148 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
149 (id->bDeviceSubClass != uaa->info.bDeviceSubClass)) {
152 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
153 (id->bDeviceProtocol != uaa->info.bDeviceProtocol)) {
156 if ((uaa->info.bDeviceClass == 0xFF) &&
157 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
158 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
159 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
160 USB_DEVICE_ID_MATCH_INT_PROTOCOL))) {
163 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
164 (id->bInterfaceClass != uaa->info.bInterfaceClass)) {
167 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
168 (id->bInterfaceSubClass != uaa->info.bInterfaceSubClass)) {
171 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
172 (id->bInterfaceProtocol != uaa->info.bInterfaceProtocol)) {
175 /* we found a match! */
183 /*------------------------------------------------------------------------*
186 * This function is the FreeBSD probe callback. It is called from the
187 * FreeBSD USB stack through the "device_probe_and_attach()" function.
188 *------------------------------------------------------------------------*/
190 usb_linux_probe(device_t dev)
192 struct usb_attach_arg *uaa = device_get_ivars(dev);
193 struct usb_driver *udrv;
196 if (uaa->usb_mode != USB_MODE_HOST) {
200 LIST_FOREACH(udrv, &usb_linux_driver_list, linux_driver_list) {
201 if (usb_linux_lookup_id(udrv->id_table, uaa)) {
211 /*------------------------------------------------------------------------*
212 * usb_linux_get_usb_driver
214 * This function returns the pointer to the "struct usb_driver" where
215 * the Linux USB device driver "struct usb_device_id" match was found.
216 * We apply a lock before reading out the pointer to avoid races.
217 *------------------------------------------------------------------------*/
218 static struct usb_driver *
219 usb_linux_get_usb_driver(struct usb_linux_softc *sc)
221 struct usb_driver *udrv;
229 /*------------------------------------------------------------------------*
232 * This function is the FreeBSD attach callback. It is called from the
233 * FreeBSD USB stack through the "device_probe_and_attach()" function.
234 * This function is called when "usb_linux_probe()" returns zero.
235 *------------------------------------------------------------------------*/
237 usb_linux_attach(device_t dev)
239 struct usb_attach_arg *uaa = device_get_ivars(dev);
240 struct usb_linux_softc *sc = device_get_softc(dev);
241 struct usb_driver *udrv;
242 const struct usb_device_id *id = NULL;
245 LIST_FOREACH(udrv, &usb_linux_driver_list, linux_driver_list) {
246 id = usb_linux_lookup_id(udrv->id_table, uaa);
255 if (usb_linux_create_usb_device(uaa->device, dev) != 0)
257 device_set_usb2_desc(dev);
259 sc->sc_fbsd_udev = uaa->device;
260 sc->sc_fbsd_dev = dev;
262 sc->sc_ui = usb_ifnum_to_if(uaa->device, uaa->info.bIfaceNum);
263 if (sc->sc_ui == NULL) {
267 if ((udrv->probe) (sc->sc_ui, id)) {
272 LIST_INSERT_HEAD(&usb_linux_attached_list, sc, sc_attached_list);
279 /*------------------------------------------------------------------------*
282 * This function is the FreeBSD detach callback. It is called from the
283 * FreeBSD USB stack through the "device_detach()" function.
284 *------------------------------------------------------------------------*/
286 usb_linux_detach(device_t dev)
288 struct usb_linux_softc *sc = device_get_softc(dev);
289 struct usb_driver *udrv = NULL;
292 if (sc->sc_attached_list.le_prev) {
293 LIST_REMOVE(sc, sc_attached_list);
294 sc->sc_attached_list.le_prev = NULL;
300 if (udrv && udrv->disconnect) {
301 (udrv->disconnect) (sc->sc_ui);
304 * Make sure that we free all FreeBSD USB transfers belonging to
305 * this Linux "usb_interface", hence they will most likely not be
308 usb_linux_cleanup_interface(sc->sc_fbsd_udev, sc->sc_ui);
312 /*------------------------------------------------------------------------*
315 * This function is the FreeBSD suspend callback. Usually it does nothing.
316 *------------------------------------------------------------------------*/
318 usb_linux_suspend(device_t dev)
320 struct usb_linux_softc *sc = device_get_softc(dev);
321 struct usb_driver *udrv = usb_linux_get_usb_driver(sc);
324 if (udrv && udrv->suspend) {
325 err = (udrv->suspend) (sc->sc_ui, 0);
330 /*------------------------------------------------------------------------*
333 * This function is the FreeBSD resume callback. Usually it does nothing.
334 *------------------------------------------------------------------------*/
336 usb_linux_resume(device_t dev)
338 struct usb_linux_softc *sc = device_get_softc(dev);
339 struct usb_driver *udrv = usb_linux_get_usb_driver(sc);
342 if (udrv && udrv->resume) {
343 err = (udrv->resume) (sc->sc_ui);
348 /*------------------------------------------------------------------------*
349 * Linux emulation layer
350 *------------------------------------------------------------------------*/
352 /*------------------------------------------------------------------------*
353 * usb_max_isoc_frames
355 * The following function returns the maximum number of isochronous
356 * frames that we support per URB. It is not part of the Linux USB API.
357 *------------------------------------------------------------------------*/
359 usb_max_isoc_frames(struct usb_device *dev)
362 switch (usb2_get_speed(dev)) {
365 return (USB_MAX_FULL_SPEED_ISOC_FRAMES);
367 return (USB_MAX_HIGH_SPEED_ISOC_FRAMES);
371 /*------------------------------------------------------------------------*
374 * This function is used to queue an URB after that it has been
375 * initialized. If it returns non-zero, it means that the URB was not
377 *------------------------------------------------------------------------*/
379 usb_submit_urb(struct urb *urb, uint16_t mem_flags)
381 struct usb_host_endpoint *uhe;
386 mtx_assert(&Giant, MA_OWNED);
388 if (urb->pipe == NULL) {
394 * Check that we have got a FreeBSD USB transfer that will dequeue
395 * the URB structure and do the real transfer. If there are no USB
396 * transfers, then we return an error.
398 if (uhe->bsd_xfer[0] ||
402 TAILQ_INSERT_HEAD(&uhe->bsd_urb_list, urb, bsd_urb_list);
404 urb->status = -EINPROGRESS;
406 usb2_transfer_start(uhe->bsd_xfer[0]);
407 usb2_transfer_start(uhe->bsd_xfer[1]);
409 /* no pipes have been setup yet! */
410 urb->status = -EINVAL;
416 /*------------------------------------------------------------------------*
419 * This function is used to stop an URB after that it is been
420 * submitted, but before the "complete" callback has been called. On
421 *------------------------------------------------------------------------*/
423 usb_unlink_urb(struct urb *urb)
425 return (usb_unlink_urb_sub(urb, 0));
429 usb_unlink_bsd(struct usb_xfer *xfer,
430 struct urb *urb, uint8_t drain)
433 usb2_transfer_pending(xfer) &&
434 (xfer->priv_fifo == (void *)urb)) {
437 usb2_transfer_drain(xfer);
440 usb2_transfer_stop(xfer);
442 usb2_transfer_start(xfer);
447 usb_unlink_urb_sub(struct urb *urb, uint8_t drain)
449 struct usb_host_endpoint *uhe;
455 mtx_assert(&Giant, MA_OWNED);
457 if (urb->pipe == NULL) {
462 if (urb->bsd_urb_list.tqe_prev) {
464 /* not started yet, just remove it from the queue */
465 TAILQ_REMOVE(&uhe->bsd_urb_list, urb, bsd_urb_list);
466 urb->bsd_urb_list.tqe_prev = NULL;
467 urb->status = -ECONNRESET;
468 urb->actual_length = 0;
470 for (x = 0; x < urb->number_of_packets; x++) {
471 urb->iso_frame_desc[x].actual_length = 0;
475 (urb->complete) (urb);
480 * If the URB is not on the URB list, then check if one of
481 * the FreeBSD USB transfer are processing the current URB.
482 * If so, re-start that transfer, which will lead to the
483 * termination of that URB:
485 usb_unlink_bsd(uhe->bsd_xfer[0], urb, drain);
486 usb_unlink_bsd(uhe->bsd_xfer[1], urb, drain);
491 /*------------------------------------------------------------------------*
494 * This function must always be used to clear the stall. Stall is when
495 * an USB endpoint returns a stall message to the USB host controller.
496 * Until the stall is cleared, no data can be transferred.
497 *------------------------------------------------------------------------*/
499 usb_clear_halt(struct usb_device *dev, struct usb_host_endpoint *uhe)
501 struct usb_config cfg[1];
502 struct usb_pipe *pipe;
509 type = uhe->desc.bmAttributes & UE_XFERTYPE;
510 addr = uhe->desc.bEndpointAddress;
512 bzero(cfg, sizeof(cfg));
515 cfg[0].endpoint = addr & UE_ADDR;
516 cfg[0].direction = addr & (UE_DIR_OUT | UE_DIR_IN);
518 pipe = usb2_get_pipe(dev, uhe->bsd_iface_index, cfg);
522 usb2_clear_data_toggle(dev, pipe);
524 return (usb_control_msg(dev, &dev->ep0,
525 UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT,
526 UF_ENDPOINT_HALT, addr, NULL, 0, 1000));
529 /*------------------------------------------------------------------------*
532 * This is an internal function that is used to perform synchronous
533 * Linux USB transfers.
534 *------------------------------------------------------------------------*/
536 usb_start_wait_urb(struct urb *urb, usb_timeout_t timeout, uint16_t *p_actlen)
540 /* you must have a timeout! */
544 urb->complete = &usb_linux_wait_complete;
545 urb->timeout = timeout;
546 urb->transfer_flags |= URB_WAIT_WAKEUP;
547 urb->transfer_flags &= ~URB_IS_SLEEPING;
549 err = usb_submit_urb(urb, 0);
554 * the URB might have completed before we get here, so check that by
557 while (urb->transfer_flags & URB_WAIT_WAKEUP) {
558 urb->transfer_flags |= URB_IS_SLEEPING;
559 usb2_cv_wait(&urb->cv_wait, &Giant);
560 urb->transfer_flags &= ~URB_IS_SLEEPING;
569 *p_actlen = urb->actual_length;
574 /*------------------------------------------------------------------------*
577 * The following function performs a control transfer sequence one any
578 * control, bulk or interrupt endpoint, specified by "uhe". A control
579 * transfer means that you transfer an 8-byte header first followed by
580 * a data-phase as indicated by the 8-byte header. The "timeout" is
581 * given in milliseconds.
587 *------------------------------------------------------------------------*/
589 usb_control_msg(struct usb_device *dev, struct usb_host_endpoint *uhe,
590 uint8_t request, uint8_t requesttype,
591 uint16_t value, uint16_t index, void *data,
592 uint16_t size, usb_timeout_t timeout)
594 struct usb_device_request req;
601 req.bmRequestType = requesttype;
602 req.bRequest = request;
603 USETW(req.wValue, value);
604 USETW(req.wIndex, index);
605 USETW(req.wLength, size);
610 type = (uhe->desc.bmAttributes & UE_XFERTYPE);
611 addr = (uhe->desc.bEndpointAddress & UE_ADDR);
613 if (type != UE_CONTROL) {
618 * The FreeBSD USB stack supports standard control
619 * transfers on control endpoint zero:
621 err = usb2_do_request_flags(dev,
622 &Giant, &req, data, USB_SHORT_XFER_OK,
631 if (dev->flags.usb_mode != USB_MODE_HOST) {
635 err = usb_setup_endpoint(dev, uhe, 1 /* dummy */ );
638 * NOTE: we need to allocate real memory here so that we don't
639 * transfer data to/from the stack!
641 * 0xFFFF is a FreeBSD specific magic value.
643 urb = usb_alloc_urb(0xFFFF, size);
650 bcopy(&req, urb->setup_packet, sizeof(req));
652 if (size && (!(req.bmRequestType & UT_READ))) {
653 /* move the data to a real buffer */
654 bcopy(data, USB_ADD_BYTES(urb->setup_packet,
657 err = usb_start_wait_urb(urb, timeout, &actlen);
659 if (req.bmRequestType & UT_READ) {
661 bcopy(USB_ADD_BYTES(urb->setup_packet,
662 sizeof(req)), data, actlen);
673 /*------------------------------------------------------------------------*
676 * The following function will select which alternate setting of an
677 * USB interface you plan to use. By default alternate setting with
678 * index zero is selected. Note that "iface_no" is not the interface
679 * index, but rather the value of "bInterfaceNumber".
680 *------------------------------------------------------------------------*/
682 usb_set_interface(struct usb_device *dev, uint8_t iface_no, uint8_t alt_index)
684 struct usb_interface *p_ui = usb_ifnum_to_if(dev, iface_no);
689 if (alt_index >= p_ui->num_altsetting)
691 usb_linux_cleanup_interface(dev, p_ui);
692 err = -usb2_set_alt_interface_index(dev,
693 p_ui->bsd_iface_index, alt_index);
695 p_ui->cur_altsetting = p_ui->altsetting + alt_index;
700 /*------------------------------------------------------------------------*
703 * The following function is an extension to the Linux USB API that
704 * allows you to set a maximum buffer size for a given USB endpoint.
705 * The maximum buffer size is per URB. If you don't call this function
706 * to set a maximum buffer size, the endpoint will not be functional.
707 * Note that for isochronous endpoints the maximum buffer size must be
708 * a non-zero dummy, hence this function will base the maximum buffer
709 * size on "wMaxPacketSize".
710 *------------------------------------------------------------------------*/
712 usb_setup_endpoint(struct usb_device *dev,
713 struct usb_host_endpoint *uhe, size_t bufsize)
715 struct usb_config cfg[2];
716 uint8_t type = uhe->desc.bmAttributes & UE_XFERTYPE;
717 uint8_t addr = uhe->desc.bEndpointAddress;
719 if (uhe->fbsd_buf_size == bufsize) {
723 usb2_transfer_unsetup(uhe->bsd_xfer, 2);
725 uhe->fbsd_buf_size = bufsize;
730 bzero(cfg, sizeof(cfg));
732 if (type == UE_ISOCHRONOUS) {
735 * Isochronous transfers are special in that they don't fit
736 * into the BULK/INTR/CONTROL transfer model.
740 cfg[0].endpoint = addr & UE_ADDR;
741 cfg[0].direction = addr & (UE_DIR_OUT | UE_DIR_IN);
742 cfg[0].callback = &usb_linux_isoc_callback;
743 cfg[0].bufsize = 0; /* use wMaxPacketSize */
744 cfg[0].frames = usb_max_isoc_frames(dev);
745 cfg[0].flags.proxy_buffer = 1;
748 * The Linux USB API allows non back-to-back
749 * isochronous frames which we do not support. If the
750 * isochronous frames are not back-to-back we need to
751 * do a copy, and then we need a buffer for
752 * that. Enable this at your own risk.
754 cfg[0].flags.ext_buffer = 1;
756 cfg[0].flags.short_xfer_ok = 1;
758 bcopy(cfg, cfg + 1, sizeof(*cfg));
760 /* Allocate and setup two generic FreeBSD USB transfers */
762 if (usb2_transfer_setup(dev, &uhe->bsd_iface_index,
763 uhe->bsd_xfer, cfg, 2, uhe, &Giant)) {
767 if (bufsize > (1 << 22)) {
768 /* limit buffer size */
771 /* Allocate and setup one generic FreeBSD USB transfer */
774 cfg[0].endpoint = addr & UE_ADDR;
775 cfg[0].direction = addr & (UE_DIR_OUT | UE_DIR_IN);
776 cfg[0].callback = &usb_linux_non_isoc_callback;
777 cfg[0].bufsize = bufsize;
778 cfg[0].flags.ext_buffer = 1; /* enable zero-copy */
779 cfg[0].flags.proxy_buffer = 1;
780 cfg[0].flags.short_xfer_ok = 1;
782 if (usb2_transfer_setup(dev, &uhe->bsd_iface_index,
783 uhe->bsd_xfer, cfg, 1, uhe, &Giant)) {
790 /*------------------------------------------------------------------------*
791 * usb_linux_create_usb_device
793 * The following function is used to build up a per USB device
794 * structure tree, that mimics the Linux one. The root structure
795 * is returned by this function.
796 *------------------------------------------------------------------------*/
798 usb_linux_create_usb_device(struct usb_device *udev, device_t dev)
800 struct usb_config_descriptor *cd = usb2_get_config_descriptor(udev);
801 struct usb_descriptor *desc;
802 struct usb_interface_descriptor *id;
803 struct usb_endpoint_descriptor *ed;
804 struct usb_interface *p_ui = NULL;
805 struct usb_host_interface *p_uhi = NULL;
806 struct usb_host_endpoint *p_uhe = NULL;
808 uint16_t niface_total;
810 uint16_t iface_no_curr;
811 uint16_t iface_index;
816 * We do two passes. One pass for computing necessary memory size
817 * and one pass to initialize all the allocated memory structures.
819 for (pass = 0; pass < 2; pass++) {
821 iface_no_curr = 0 - 1;
828 * Iterate over all the USB descriptors. Use the USB config
829 * descriptor pointer provided by the FreeBSD USB stack.
831 while ((desc = usb2_desc_foreach(cd, desc))) {
834 * Build up a tree according to the descriptors we
837 switch (desc->bDescriptorType) {
843 if ((ed->bLength < sizeof(*ed)) ||
847 bcopy(ed, &p_uhe->desc, sizeof(p_uhe->desc));
848 p_uhe->bsd_iface_index = iface_index - 1;
852 (p_uhi - 1)->desc.bNumEndpoints++;
857 case UDESC_INTERFACE:
859 if (id->bLength < sizeof(*id))
862 bcopy(id, &p_uhi->desc, sizeof(p_uhi->desc));
863 p_uhi->desc.bNumEndpoints = 0;
864 p_uhi->endpoint = p_uhe;
866 p_uhi->bsd_iface_index = iface_index;
869 iface_no = id->bInterfaceNumber;
871 if (iface_no_curr != iface_no) {
873 p_ui->altsetting = p_uhi - 1;
874 p_ui->cur_altsetting = p_uhi - 1;
875 p_ui->num_altsetting = 1;
876 p_ui->bsd_iface_index = iface_index;
877 p_ui->linux_udev = udev;
880 iface_no_curr = iface_no;
884 (p_ui - 1)->num_altsetting++;
896 size = (sizeof(*p_uhe) * nedesc) +
897 (sizeof(*p_ui) * iface_index) +
898 (sizeof(*p_uhi) * niface_total);
900 p_uhe = malloc(size, M_USBDEV, M_WAITOK | M_ZERO);
901 p_ui = (void *)(p_uhe + nedesc);
902 p_uhi = (void *)(p_ui + iface_index);
904 udev->linux_iface_start = p_ui;
905 udev->linux_iface_end = p_ui + iface_index;
906 udev->linux_endpoint_start = p_uhe;
907 udev->linux_endpoint_end = p_uhe + nedesc;
908 udev->devnum = device_get_unit(dev);
909 bcopy(&udev->ddesc, &udev->descriptor,
910 sizeof(udev->descriptor));
911 bcopy(udev->default_pipe.edesc, &udev->ep0.desc,
912 sizeof(udev->ep0.desc));
918 /*------------------------------------------------------------------------*
921 * This function should always be used when you allocate an URB for
922 * use with the USB Linux stack. In case of an isochronous transfer
923 * you must specifiy the maximum number of "iso_packets" which you
924 * plan to transfer per URB. This function is always blocking, and
925 * "mem_flags" are not regarded like on Linux.
926 *------------------------------------------------------------------------*/
928 usb_alloc_urb(uint16_t iso_packets, uint16_t mem_flags)
933 if (iso_packets == 0xFFFF) {
935 * FreeBSD specific magic value to ask for control transfer
938 size = sizeof(*urb) + sizeof(struct usb_device_request) + mem_flags;
940 size = sizeof(*urb) + (iso_packets * sizeof(urb->iso_frame_desc[0]));
943 urb = malloc(size, M_USBDEV, M_WAITOK | M_ZERO);
946 usb2_cv_init(&urb->cv_wait, "URBWAIT");
947 if (iso_packets == 0xFFFF) {
948 urb->setup_packet = (void *)(urb + 1);
949 urb->transfer_buffer = (void *)(urb->setup_packet +
950 sizeof(struct usb_device_request));
952 urb->number_of_packets = iso_packets;
958 /*------------------------------------------------------------------------*
959 * usb_find_host_endpoint
961 * The following function will return the Linux USB host endpoint
962 * structure that matches the given endpoint type and endpoint
963 * value. If no match is found, NULL is returned. This function is not
964 * part of the Linux USB API and is only used internally.
965 *------------------------------------------------------------------------*/
966 struct usb_host_endpoint *
967 usb_find_host_endpoint(struct usb_device *dev, uint8_t type, uint8_t ep)
969 struct usb_host_endpoint *uhe;
970 struct usb_host_endpoint *uhe_end;
971 struct usb_host_interface *uhi;
972 struct usb_interface *ui;
980 if (type == UE_CONTROL) {
983 mask = (UE_DIR_IN | UE_DIR_OUT | UE_ADDR);
989 * Iterate over all the interfaces searching the selected alternate
990 * setting only, and all belonging endpoints.
992 for (ui = dev->linux_iface_start;
993 ui != dev->linux_iface_end;
995 uhi = ui->cur_altsetting;
997 uhe_end = uhi->endpoint + uhi->desc.bNumEndpoints;
998 for (uhe = uhi->endpoint;
1001 ea = uhe->desc.bEndpointAddress;
1002 at = uhe->desc.bmAttributes;
1004 if (((ea & mask) == ep) &&
1005 ((at & UE_XFERTYPE) == type)) {
1012 if ((type == UE_CONTROL) && ((ep & UE_ADDR) == 0)) {
1018 /*------------------------------------------------------------------------*
1019 * usb_altnum_to_altsetting
1021 * The following function returns a pointer to an alternate setting by
1022 * index given a "usb_interface" pointer. If the alternate setting by
1023 * index does not exist, NULL is returned. And alternate setting is a
1024 * variant of an interface, but usually with slightly different
1026 *------------------------------------------------------------------------*/
1027 struct usb_host_interface *
1028 usb_altnum_to_altsetting(const struct usb_interface *intf, uint8_t alt_index)
1030 if (alt_index >= intf->num_altsetting) {
1033 return (intf->altsetting + alt_index);
1036 /*------------------------------------------------------------------------*
1039 * The following function searches up an USB interface by
1040 * "bInterfaceNumber". If no match is found, NULL is returned.
1041 *------------------------------------------------------------------------*/
1042 struct usb_interface *
1043 usb_ifnum_to_if(struct usb_device *dev, uint8_t iface_no)
1045 struct usb_interface *p_ui;
1047 for (p_ui = dev->linux_iface_start;
1048 p_ui != dev->linux_iface_end;
1050 if ((p_ui->num_altsetting > 0) &&
1051 (p_ui->altsetting->desc.bInterfaceNumber == iface_no)) {
1058 /*------------------------------------------------------------------------*
1060 *------------------------------------------------------------------------*/
1062 usb_buffer_alloc(struct usb_device *dev, size_t size, uint16_t mem_flags, uint8_t *dma_addr)
1064 return (malloc(size, M_USBDEV, M_WAITOK | M_ZERO));
1067 /*------------------------------------------------------------------------*
1069 *------------------------------------------------------------------------*/
1071 usb_get_intfdata(struct usb_interface *intf)
1073 return (intf->bsd_priv_sc);
1076 /*------------------------------------------------------------------------*
1077 * usb_linux_register
1079 * The following function is used by the "USB_DRIVER_EXPORT()" macro,
1080 * and is used to register a Linux USB driver, so that its
1081 * "usb_device_id" structures gets searched a probe time. This
1082 * function is not part of the Linux USB API, and is for internal use
1084 *------------------------------------------------------------------------*/
1086 usb_linux_register(void *arg)
1088 struct usb_driver *drv = arg;
1091 LIST_INSERT_HEAD(&usb_linux_driver_list, drv, linux_driver_list);
1094 usb2_needs_explore_all();
1097 /*------------------------------------------------------------------------*
1098 * usb_linux_deregister
1100 * The following function is used by the "USB_DRIVER_EXPORT()" macro,
1101 * and is used to deregister a Linux USB driver. This function will
1102 * ensure that all driver instances belonging to the Linux USB device
1103 * driver in question, gets detached before the driver is
1104 * unloaded. This function is not part of the Linux USB API, and is
1105 * for internal use only.
1106 *------------------------------------------------------------------------*/
1108 usb_linux_deregister(void *arg)
1110 struct usb_driver *drv = arg;
1111 struct usb_linux_softc *sc;
1115 LIST_FOREACH(sc, &usb_linux_attached_list, sc_attached_list) {
1116 if (sc->sc_udrv == drv) {
1118 device_detach(sc->sc_fbsd_dev);
1122 LIST_REMOVE(drv, linux_driver_list);
1126 /*------------------------------------------------------------------------*
1127 * usb_linux_free_device
1129 * The following function is only used by the FreeBSD USB stack, to
1130 * cleanup and free memory after that a Linux USB device was attached.
1131 *------------------------------------------------------------------------*/
1133 usb_linux_free_device(struct usb_device *dev)
1135 struct usb_host_endpoint *uhe;
1136 struct usb_host_endpoint *uhe_end;
1139 uhe = dev->linux_endpoint_start;
1140 uhe_end = dev->linux_endpoint_end;
1141 while (uhe != uhe_end) {
1142 err = usb_setup_endpoint(dev, uhe, 0);
1145 err = usb_setup_endpoint(dev, &dev->ep0, 0);
1146 free(uhe, M_USBDEV);
1149 /*------------------------------------------------------------------------*
1151 *------------------------------------------------------------------------*/
1153 usb_buffer_free(struct usb_device *dev, size_t size,
1154 void *addr, uint8_t dma_addr)
1156 free(addr, M_USBDEV);
1159 /*------------------------------------------------------------------------*
1161 *------------------------------------------------------------------------*/
1163 usb_free_urb(struct urb *urb)
1168 /* make sure that the current URB is not active */
1171 /* destroy condition variable */
1172 usb2_cv_destroy(&urb->cv_wait);
1175 free(urb, M_USBDEV);
1178 /*------------------------------------------------------------------------*
1181 * The following function can be used to initialize a custom URB. It
1182 * is not recommended to use this function. Use "usb_alloc_urb()"
1184 *------------------------------------------------------------------------*/
1186 usb_init_urb(struct urb *urb)
1191 bzero(urb, sizeof(*urb));
1194 /*------------------------------------------------------------------------*
1196 *------------------------------------------------------------------------*/
1198 usb_kill_urb(struct urb *urb)
1200 if (usb_unlink_urb_sub(urb, 1)) {
1205 /*------------------------------------------------------------------------*
1208 * The following function sets the per Linux USB interface private
1209 * data pointer. It is used by most Linux USB device drivers.
1210 *------------------------------------------------------------------------*/
1212 usb_set_intfdata(struct usb_interface *intf, void *data)
1214 intf->bsd_priv_sc = data;
1217 /*------------------------------------------------------------------------*
1218 * usb_linux_cleanup_interface
1220 * The following function will release all FreeBSD USB transfers
1221 * associated with a Linux USB interface. It is for internal use only.
1222 *------------------------------------------------------------------------*/
1224 usb_linux_cleanup_interface(struct usb_device *dev, struct usb_interface *iface)
1226 struct usb_host_interface *uhi;
1227 struct usb_host_interface *uhi_end;
1228 struct usb_host_endpoint *uhe;
1229 struct usb_host_endpoint *uhe_end;
1232 uhi = iface->altsetting;
1233 uhi_end = iface->altsetting + iface->num_altsetting;
1234 while (uhi != uhi_end) {
1235 uhe = uhi->endpoint;
1236 uhe_end = uhi->endpoint + uhi->desc.bNumEndpoints;
1237 while (uhe != uhe_end) {
1238 err = usb_setup_endpoint(dev, uhe, 0);
1245 /*------------------------------------------------------------------------*
1246 * usb_linux_wait_complete
1248 * The following function is used by "usb_start_wait_urb()" to wake it
1249 * up, when an USB transfer has finished.
1250 *------------------------------------------------------------------------*/
1252 usb_linux_wait_complete(struct urb *urb)
1254 if (urb->transfer_flags & URB_IS_SLEEPING) {
1255 usb2_cv_signal(&urb->cv_wait);
1257 urb->transfer_flags &= ~URB_WAIT_WAKEUP;
1260 /*------------------------------------------------------------------------*
1261 * usb_linux_complete
1262 *------------------------------------------------------------------------*/
1264 usb_linux_complete(struct usb_xfer *xfer)
1268 urb = xfer->priv_fifo;
1269 xfer->priv_fifo = NULL;
1270 if (urb->complete) {
1271 (urb->complete) (urb);
1275 /*------------------------------------------------------------------------*
1276 * usb_linux_isoc_callback
1278 * The following is the FreeBSD isochronous USB callback. Isochronous
1279 * frames are USB packets transferred 1000 or 8000 times per second,
1280 * depending on whether a full- or high- speed USB transfer is
1282 *------------------------------------------------------------------------*/
1284 usb_linux_isoc_callback(struct usb_xfer *xfer)
1286 usb_frlength_t max_frame = xfer->max_frame_size;
1287 usb_frlength_t offset;
1289 struct urb *urb = xfer->priv_fifo;
1290 struct usb_host_endpoint *uhe = xfer->priv_sc;
1291 struct usb_iso_packet_descriptor *uipd;
1295 switch (USB_GET_STATE(xfer)) {
1296 case USB_ST_TRANSFERRED:
1298 if (urb->bsd_isread) {
1300 /* copy in data with regard to the URB */
1304 for (x = 0; x < urb->number_of_packets; x++) {
1305 uipd = urb->iso_frame_desc + x;
1306 uipd->actual_length = xfer->frlengths[x];
1308 if (!xfer->flags.ext_buffer) {
1309 usb2_copy_out(xfer->frbuffers, offset,
1310 USB_ADD_BYTES(urb->transfer_buffer,
1311 uipd->offset), uipd->actual_length);
1313 offset += max_frame;
1316 for (x = 0; x < urb->number_of_packets; x++) {
1317 uipd = urb->iso_frame_desc + x;
1318 uipd->actual_length = xfer->frlengths[x];
1323 urb->actual_length = xfer->actlen;
1325 /* check for short transfer */
1326 if (xfer->actlen < xfer->sumlen) {
1327 /* short transfer */
1328 if (urb->transfer_flags & URB_SHORT_NOT_OK) {
1329 urb->status = -EPIPE; /* XXX should be
1340 usb_linux_complete(xfer);
1345 if (xfer->priv_fifo == NULL) {
1347 /* get next transfer */
1348 urb = TAILQ_FIRST(&uhe->bsd_urb_list);
1353 TAILQ_REMOVE(&uhe->bsd_urb_list, urb, bsd_urb_list);
1354 urb->bsd_urb_list.tqe_prev = NULL;
1356 x = xfer->max_frame_count;
1357 if (urb->number_of_packets > x) {
1358 /* XXX simply truncate the transfer */
1359 urb->number_of_packets = x;
1362 DPRINTF("Already got a transfer\n");
1364 /* already got a transfer (should not happen) */
1365 urb = xfer->priv_fifo;
1368 urb->bsd_isread = (uhe->desc.bEndpointAddress & UE_DIR_IN) ? 1 : 0;
1370 if (!(urb->bsd_isread)) {
1372 /* copy out data with regard to the URB */
1376 for (x = 0; x < urb->number_of_packets; x++) {
1377 uipd = urb->iso_frame_desc + x;
1378 xfer->frlengths[x] = uipd->length;
1379 if (!xfer->flags.ext_buffer) {
1380 usb2_copy_in(xfer->frbuffers, offset,
1381 USB_ADD_BYTES(urb->transfer_buffer,
1382 uipd->offset), uipd->length);
1384 offset += uipd->length;
1389 * compute the transfer length into the "offset"
1393 offset = urb->number_of_packets * max_frame;
1395 /* setup "frlengths" array */
1397 for (x = 0; x < urb->number_of_packets; x++) {
1398 uipd = urb->iso_frame_desc + x;
1399 xfer->frlengths[x] = max_frame;
1403 if (xfer->flags.ext_buffer) {
1404 /* set virtual address to load */
1405 usb2_set_frame_data(xfer,
1406 urb->transfer_buffer, 0);
1408 xfer->priv_fifo = urb;
1409 xfer->flags.force_short_xfer = 0;
1410 xfer->timeout = urb->timeout;
1411 xfer->nframes = urb->number_of_packets;
1412 usb2_start_hardware(xfer);
1415 default: /* Error */
1416 if (xfer->error == USB_ERR_CANCELLED) {
1417 urb->status = -ECONNRESET;
1419 urb->status = -EPIPE; /* stalled */
1422 /* Set zero for "actual_length" */
1423 urb->actual_length = 0;
1425 /* Set zero for "actual_length" */
1426 for (x = 0; x < urb->number_of_packets; x++) {
1427 urb->iso_frame_desc[x].actual_length = 0;
1431 usb_linux_complete(xfer);
1433 if (xfer->error == USB_ERR_CANCELLED) {
1434 /* we need to return in this case */
1442 /*------------------------------------------------------------------------*
1443 * usb_linux_non_isoc_callback
1445 * The following is the FreeBSD BULK/INTERRUPT and CONTROL USB
1446 * callback. It dequeues Linux USB stack compatible URB's, transforms
1447 * the URB fields into a FreeBSD USB transfer, and defragments the USB
1448 * transfer as required. When the transfer is complete the "complete"
1449 * callback is called.
1450 *------------------------------------------------------------------------*/
1452 usb_linux_non_isoc_callback(struct usb_xfer *xfer)
1455 REQ_SIZE = sizeof(struct usb_device_request)
1457 struct urb *urb = xfer->priv_fifo;
1458 struct usb_host_endpoint *uhe = xfer->priv_sc;
1460 usb_frlength_t max_bulk = xfer->max_data_length;
1461 uint8_t data_frame = xfer->flags_int.control_xfr ? 1 : 0;
1465 switch (USB_GET_STATE(xfer)) {
1466 case USB_ST_TRANSFERRED:
1468 if (xfer->flags_int.control_xfr) {
1470 /* don't transfer the setup packet again: */
1472 xfer->frlengths[0] = 0;
1474 if (urb->bsd_isread && (!xfer->flags.ext_buffer)) {
1475 /* copy in data with regard to the URB */
1476 usb2_copy_out(xfer->frbuffers + data_frame, 0,
1477 urb->bsd_data_ptr, xfer->frlengths[data_frame]);
1479 urb->bsd_length_rem -= xfer->frlengths[data_frame];
1480 urb->bsd_data_ptr += xfer->frlengths[data_frame];
1481 urb->actual_length += xfer->frlengths[data_frame];
1483 /* check for short transfer */
1484 if (xfer->actlen < xfer->sumlen) {
1485 urb->bsd_length_rem = 0;
1487 /* short transfer */
1488 if (urb->transfer_flags & URB_SHORT_NOT_OK) {
1489 urb->status = -EPIPE;
1494 /* check remainder */
1495 if (urb->bsd_length_rem > 0) {
1503 usb_linux_complete(xfer);
1507 /* get next transfer */
1508 urb = TAILQ_FIRST(&uhe->bsd_urb_list);
1513 TAILQ_REMOVE(&uhe->bsd_urb_list, urb, bsd_urb_list);
1514 urb->bsd_urb_list.tqe_prev = NULL;
1516 xfer->priv_fifo = urb;
1517 xfer->flags.force_short_xfer = 0;
1518 xfer->timeout = urb->timeout;
1520 if (xfer->flags_int.control_xfr) {
1523 * USB control transfers need special handling.
1524 * First copy in the header, then copy in data!
1526 if (!xfer->flags.ext_buffer) {
1527 usb2_copy_in(xfer->frbuffers, 0,
1528 urb->setup_packet, REQ_SIZE);
1530 /* set virtual address to load */
1531 usb2_set_frame_data(xfer,
1532 urb->setup_packet, 0);
1535 xfer->frlengths[0] = REQ_SIZE;
1537 ptr = urb->setup_packet;
1539 /* setup data transfer direction and length */
1540 urb->bsd_isread = (ptr[0] & UT_READ) ? 1 : 0;
1541 urb->bsd_length_rem = ptr[6] | (ptr[7] << 8);
1545 /* setup data transfer direction */
1547 urb->bsd_length_rem = urb->transfer_buffer_length;
1548 urb->bsd_isread = (uhe->desc.bEndpointAddress &
1552 urb->bsd_data_ptr = urb->transfer_buffer;
1553 urb->actual_length = 0;
1556 if (max_bulk > urb->bsd_length_rem) {
1557 max_bulk = urb->bsd_length_rem;
1559 /* check if we need to force a short transfer */
1561 if ((max_bulk == urb->bsd_length_rem) &&
1562 (urb->transfer_flags & URB_ZERO_PACKET) &&
1563 (!xfer->flags_int.control_xfr)) {
1564 xfer->flags.force_short_xfer = 1;
1566 /* check if we need to copy in data */
1568 if (xfer->flags.ext_buffer) {
1569 /* set virtual address to load */
1570 usb2_set_frame_data(xfer, urb->bsd_data_ptr,
1572 } else if (!urb->bsd_isread) {
1573 /* copy out data with regard to the URB */
1574 usb2_copy_in(xfer->frbuffers + data_frame, 0,
1575 urb->bsd_data_ptr, max_bulk);
1577 xfer->frlengths[data_frame] = max_bulk;
1578 if (xfer->flags_int.control_xfr) {
1587 usb2_start_hardware(xfer);
1591 if (xfer->error == USB_ERR_CANCELLED) {
1592 urb->status = -ECONNRESET;
1594 urb->status = -EPIPE;
1597 /* Set zero for "actual_length" */
1598 urb->actual_length = 0;
1601 usb_linux_complete(xfer);
1603 if (xfer->error == USB_ERR_CANCELLED) {
1604 /* we need to return in this case */