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/param.h>
30 #include <sys/queue.h>
31 #include <sys/types.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
35 #include <sys/module.h>
37 #include <sys/mutex.h>
38 #include <sys/condvar.h>
39 #include <sys/sysctl.h>
40 #include <sys/unistd.h>
41 #include <sys/callout.h>
42 #include <sys/malloc.h>
45 #include <bus/u4b/usb.h>
46 #include <bus/u4b/usb_ioctl.h>
47 #include <bus/u4b/usbdi.h>
48 #include <bus/u4b/usbdi_util.h>
50 #define USB_DEBUG_VAR usb_debug
52 #include <bus/u4b/usb_core.h>
53 #include <bus/u4b/usb_compat_linux.h>
54 #include <bus/u4b/usb_process.h>
55 #include <bus/u4b/usb_device.h>
56 #include <bus/u4b/usb_util.h>
57 #include <bus/u4b/usb_busdma.h>
58 #include <bus/u4b/usb_transfer.h>
59 #include <bus/u4b/usb_hub.h>
60 #include <bus/u4b/usb_request.h>
61 #include <bus/u4b/usb_debug.h>
63 struct usb_linux_softc
{
64 LIST_ENTRY(usb_linux_softc
) sc_attached_list
;
67 struct usb_device
*sc_fbsd_udev
;
68 struct usb_interface
*sc_ui
;
69 struct usb_driver
*sc_udrv
;
73 static device_probe_t usb_linux_probe
;
74 static device_attach_t usb_linux_attach
;
75 static device_detach_t usb_linux_detach
;
76 static device_suspend_t usb_linux_suspend
;
77 static device_resume_t usb_linux_resume
;
79 static usb_callback_t usb_linux_isoc_callback
;
80 static usb_callback_t usb_linux_non_isoc_callback
;
82 static usb_complete_t usb_linux_wait_complete
;
84 static uint16_t usb_max_isoc_frames(struct usb_device
*);
85 static int usb_start_wait_urb(struct urb
*, usb_timeout_t
, uint16_t *);
86 static const struct usb_device_id
*usb_linux_lookup_id(
87 const struct usb_device_id
*, struct usb_attach_arg
*);
88 static struct usb_driver
*usb_linux_get_usb_driver(struct usb_linux_softc
*);
89 static int usb_linux_create_usb_device(struct usb_device
*, device_t
);
90 static void usb_linux_cleanup_interface(struct usb_device
*,
91 struct usb_interface
*);
92 static void usb_linux_complete(struct usb_xfer
*);
93 static int usb_unlink_urb_sub(struct urb
*, uint8_t);
95 /*------------------------------------------------------------------------*
96 * FreeBSD USB interface
97 *------------------------------------------------------------------------*/
99 static LIST_HEAD(, usb_linux_softc
) usb_linux_attached_list
;
100 static LIST_HEAD(, usb_driver
) usb_linux_driver_list
;
102 static device_method_t usb_linux_methods
[] = {
103 /* Device interface */
104 DEVMETHOD(device_probe
, usb_linux_probe
),
105 DEVMETHOD(device_attach
, usb_linux_attach
),
106 DEVMETHOD(device_detach
, usb_linux_detach
),
107 DEVMETHOD(device_suspend
, usb_linux_suspend
),
108 DEVMETHOD(device_resume
, usb_linux_resume
),
113 static driver_t usb_linux_driver
= {
115 .methods
= usb_linux_methods
,
116 .size
= sizeof(struct usb_linux_softc
),
119 static devclass_t usb_linux_devclass
;
121 DRIVER_MODULE(usb_linux
, uhub
, usb_linux_driver
, usb_linux_devclass
, NULL
, NULL
);
122 MODULE_VERSION(usb_linux
, 1);
124 /*------------------------------------------------------------------------*
125 * usb_linux_lookup_id
127 * This functions takes an array of "struct usb_device_id" and tries
128 * to match the entries with the information in "struct usb_attach_arg".
129 * If it finds a match the matching entry will be returned.
130 * Else "NULL" will be returned.
131 *------------------------------------------------------------------------*/
132 static const struct usb_device_id
*
133 usb_linux_lookup_id(const struct usb_device_id
*id
, struct usb_attach_arg
*uaa
)
139 * Keep on matching array entries until we find one with
140 * "match_flags" equal to zero, which indicates the end of the
143 for (; id
->match_flags
; id
++) {
145 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_VENDOR
) &&
146 (id
->idVendor
!= uaa
->info
.idVendor
)) {
149 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_PRODUCT
) &&
150 (id
->idProduct
!= uaa
->info
.idProduct
)) {
153 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_LO
) &&
154 (id
->bcdDevice_lo
> uaa
->info
.bcdDevice
)) {
157 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_HI
) &&
158 (id
->bcdDevice_hi
< uaa
->info
.bcdDevice
)) {
161 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_CLASS
) &&
162 (id
->bDeviceClass
!= uaa
->info
.bDeviceClass
)) {
165 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_SUBCLASS
) &&
166 (id
->bDeviceSubClass
!= uaa
->info
.bDeviceSubClass
)) {
169 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_PROTOCOL
) &&
170 (id
->bDeviceProtocol
!= uaa
->info
.bDeviceProtocol
)) {
173 if ((uaa
->info
.bDeviceClass
== 0xFF) &&
174 !(id
->match_flags
& USB_DEVICE_ID_MATCH_VENDOR
) &&
175 (id
->match_flags
& (USB_DEVICE_ID_MATCH_INT_CLASS
|
176 USB_DEVICE_ID_MATCH_INT_SUBCLASS
|
177 USB_DEVICE_ID_MATCH_INT_PROTOCOL
))) {
180 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_CLASS
) &&
181 (id
->bInterfaceClass
!= uaa
->info
.bInterfaceClass
)) {
184 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_SUBCLASS
) &&
185 (id
->bInterfaceSubClass
!= uaa
->info
.bInterfaceSubClass
)) {
188 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_PROTOCOL
) &&
189 (id
->bInterfaceProtocol
!= uaa
->info
.bInterfaceProtocol
)) {
192 /* we found a match! */
200 /*------------------------------------------------------------------------*
203 * This function is the FreeBSD probe callback. It is called from the
204 * FreeBSD USB stack through the "device_probe_and_attach()" function.
205 *------------------------------------------------------------------------*/
207 usb_linux_probe(device_t dev
)
209 struct usb_attach_arg
*uaa
= device_get_ivars(dev
);
210 struct usb_driver
*udrv
;
213 if (uaa
->usb_mode
!= USB_MODE_HOST
) {
217 LIST_FOREACH(udrv
, &usb_linux_driver_list
, linux_driver_list
) {
218 if (usb_linux_lookup_id(udrv
->id_table
, uaa
)) {
228 /*------------------------------------------------------------------------*
229 * usb_linux_get_usb_driver
231 * This function returns the pointer to the "struct usb_driver" where
232 * the Linux USB device driver "struct usb_device_id" match was found.
233 * We apply a lock before reading out the pointer to avoid races.
234 *------------------------------------------------------------------------*/
235 static struct usb_driver
*
236 usb_linux_get_usb_driver(struct usb_linux_softc
*sc
)
238 struct usb_driver
*udrv
;
246 /*------------------------------------------------------------------------*
249 * This function is the FreeBSD attach callback. It is called from the
250 * FreeBSD USB stack through the "device_probe_and_attach()" function.
251 * This function is called when "usb_linux_probe()" returns zero.
252 *------------------------------------------------------------------------*/
254 usb_linux_attach(device_t dev
)
256 struct usb_attach_arg
*uaa
= device_get_ivars(dev
);
257 struct usb_linux_softc
*sc
= device_get_softc(dev
);
258 struct usb_driver
*udrv
;
259 const struct usb_device_id
*id
= NULL
;
262 LIST_FOREACH(udrv
, &usb_linux_driver_list
, linux_driver_list
) {
263 id
= usb_linux_lookup_id(udrv
->id_table
, uaa
);
272 if (usb_linux_create_usb_device(uaa
->device
, dev
) != 0)
274 device_set_usb_desc(dev
);
276 sc
->sc_fbsd_udev
= uaa
->device
;
277 sc
->sc_fbsd_dev
= dev
;
279 sc
->sc_ui
= usb_ifnum_to_if(uaa
->device
, uaa
->info
.bIfaceNum
);
280 if (sc
->sc_ui
== NULL
) {
284 if ((udrv
->probe
) (sc
->sc_ui
, id
)) {
289 LIST_INSERT_HEAD(&usb_linux_attached_list
, sc
, sc_attached_list
);
296 /*------------------------------------------------------------------------*
299 * This function is the FreeBSD detach callback. It is called from the
300 * FreeBSD USB stack through the "device_detach()" function.
301 *------------------------------------------------------------------------*/
303 usb_linux_detach(device_t dev
)
305 struct usb_linux_softc
*sc
= device_get_softc(dev
);
306 struct usb_driver
*udrv
= NULL
;
309 if (sc
->sc_attached_list
.le_prev
) {
310 LIST_REMOVE(sc
, sc_attached_list
);
311 sc
->sc_attached_list
.le_prev
= NULL
;
317 if (udrv
&& udrv
->disconnect
) {
318 (udrv
->disconnect
) (sc
->sc_ui
);
321 * Make sure that we free all FreeBSD USB transfers belonging to
322 * this Linux "usb_interface", hence they will most likely not be
325 usb_linux_cleanup_interface(sc
->sc_fbsd_udev
, sc
->sc_ui
);
329 /*------------------------------------------------------------------------*
332 * This function is the FreeBSD suspend callback. Usually it does nothing.
333 *------------------------------------------------------------------------*/
335 usb_linux_suspend(device_t dev
)
337 struct usb_linux_softc
*sc
= device_get_softc(dev
);
338 struct usb_driver
*udrv
= usb_linux_get_usb_driver(sc
);
341 if (udrv
&& udrv
->suspend
) {
342 err
= (udrv
->suspend
) (sc
->sc_ui
, 0);
347 /*------------------------------------------------------------------------*
350 * This function is the FreeBSD resume callback. Usually it does nothing.
351 *------------------------------------------------------------------------*/
353 usb_linux_resume(device_t dev
)
355 struct usb_linux_softc
*sc
= device_get_softc(dev
);
356 struct usb_driver
*udrv
= usb_linux_get_usb_driver(sc
);
359 if (udrv
&& udrv
->resume
) {
360 err
= (udrv
->resume
) (sc
->sc_ui
);
365 /*------------------------------------------------------------------------*
366 * Linux emulation layer
367 *------------------------------------------------------------------------*/
369 /*------------------------------------------------------------------------*
370 * usb_max_isoc_frames
372 * The following function returns the maximum number of isochronous
373 * frames that we support per URB. It is not part of the Linux USB API.
374 *------------------------------------------------------------------------*/
376 usb_max_isoc_frames(struct usb_device
*dev
)
379 switch (usbd_get_speed(dev
)) {
382 return (USB_MAX_FULL_SPEED_ISOC_FRAMES
);
384 return (USB_MAX_HIGH_SPEED_ISOC_FRAMES
);
388 /*------------------------------------------------------------------------*
391 * This function is used to queue an URB after that it has been
392 * initialized. If it returns non-zero, it means that the URB was not
394 *------------------------------------------------------------------------*/
396 usb_submit_urb(struct urb
*urb
, uint16_t mem_flags
)
398 struct usb_host_endpoint
*uhe
;
405 do_unlock
= mtx_owned(&Giant
) ? 0 : 1;
409 if (urb
->endpoint
== NULL
) {
415 * Check to see if the urb is in the process of being killed
416 * and stop a urb that is in the process of being killed from
417 * being re-submitted (e.g. from its completion callback
420 if (urb
->kill_count
!= 0) {
428 * Check that we have got a FreeBSD USB transfer that will dequeue
429 * the URB structure and do the real transfer. If there are no USB
430 * transfers, then we return an error.
432 if (uhe
->bsd_xfer
[0] ||
436 TAILQ_INSERT_TAIL(&uhe
->bsd_urb_list
, urb
, bsd_urb_list
);
438 urb
->status
= -EINPROGRESS
;
440 usbd_transfer_start(uhe
->bsd_xfer
[0]);
441 usbd_transfer_start(uhe
->bsd_xfer
[1]);
444 /* no pipes have been setup yet! */
445 urb
->status
= -EINVAL
;
454 /*------------------------------------------------------------------------*
457 * This function is used to stop an URB after that it is been
458 * submitted, but before the "complete" callback has been called. On
459 *------------------------------------------------------------------------*/
461 usb_unlink_urb(struct urb
*urb
)
463 return (usb_unlink_urb_sub(urb
, 0));
467 usb_unlink_bsd(struct usb_xfer
*xfer
,
468 struct urb
*urb
, uint8_t drain
)
472 if (!usbd_transfer_pending(xfer
))
474 if (xfer
->priv_fifo
== (void *)urb
) {
477 usbd_transfer_drain(xfer
);
480 usbd_transfer_stop(xfer
);
482 usbd_transfer_start(xfer
);
487 usb_unlink_urb_sub(struct urb
*urb
, uint8_t drain
)
489 struct usb_host_endpoint
*uhe
;
497 do_unlock
= mtx_owned(&Giant
) ? 0 : 1;
503 if (urb
->endpoint
== NULL
) {
509 if (urb
->bsd_urb_list
.tqe_prev
) {
511 /* not started yet, just remove it from the queue */
512 TAILQ_REMOVE(&uhe
->bsd_urb_list
, urb
, bsd_urb_list
);
513 urb
->bsd_urb_list
.tqe_prev
= NULL
;
514 urb
->status
= -ECONNRESET
;
515 urb
->actual_length
= 0;
517 for (x
= 0; x
< urb
->number_of_packets
; x
++) {
518 urb
->iso_frame_desc
[x
].actual_length
= 0;
522 (urb
->complete
) (urb
);
527 * If the URB is not on the URB list, then check if one of
528 * the FreeBSD USB transfer are processing the current URB.
529 * If so, re-start that transfer, which will lead to the
530 * termination of that URB:
532 usb_unlink_bsd(uhe
->bsd_xfer
[0], urb
, drain
);
533 usb_unlink_bsd(uhe
->bsd_xfer
[1], urb
, drain
);
544 /*------------------------------------------------------------------------*
547 * This function must always be used to clear the stall. Stall is when
548 * an USB endpoint returns a stall message to the USB host controller.
549 * Until the stall is cleared, no data can be transferred.
550 *------------------------------------------------------------------------*/
552 usb_clear_halt(struct usb_device
*dev
, struct usb_host_endpoint
*uhe
)
554 struct usb_config cfg
[1];
555 struct usb_endpoint
*ep
;
562 type
= uhe
->desc
.bmAttributes
& UE_XFERTYPE
;
563 addr
= uhe
->desc
.bEndpointAddress
;
565 memset(cfg
, 0, sizeof(cfg
));
568 cfg
[0].endpoint
= addr
& UE_ADDR
;
569 cfg
[0].direction
= addr
& (UE_DIR_OUT
| UE_DIR_IN
);
571 ep
= usbd_get_endpoint(dev
, uhe
->bsd_iface_index
, cfg
);
575 usbd_clear_data_toggle(dev
, ep
);
577 return (usb_control_msg(dev
, &dev
->ep0
,
578 UR_CLEAR_FEATURE
, UT_WRITE_ENDPOINT
,
579 UF_ENDPOINT_HALT
, addr
, NULL
, 0, 1000));
582 /*------------------------------------------------------------------------*
585 * This is an internal function that is used to perform synchronous
586 * Linux USB transfers.
587 *------------------------------------------------------------------------*/
589 usb_start_wait_urb(struct urb
*urb
, usb_timeout_t timeout
, uint16_t *p_actlen
)
594 /* you must have a timeout! */
598 urb
->complete
= &usb_linux_wait_complete
;
599 urb
->timeout
= timeout
;
600 urb
->transfer_flags
|= URB_WAIT_WAKEUP
;
601 urb
->transfer_flags
&= ~URB_IS_SLEEPING
;
603 do_unlock
= mtx_owned(&Giant
) ? 0 : 1;
606 err
= usb_submit_urb(urb
, 0);
611 * the URB might have completed before we get here, so check that by
614 while (urb
->transfer_flags
& URB_WAIT_WAKEUP
) {
615 urb
->transfer_flags
|= URB_IS_SLEEPING
;
616 cv_wait(&urb
->cv_wait
, &Giant
);
617 urb
->transfer_flags
&= ~URB_IS_SLEEPING
;
625 if (p_actlen
!= NULL
) {
629 *p_actlen
= urb
->actual_length
;
634 /*------------------------------------------------------------------------*
637 * The following function performs a control transfer sequence one any
638 * control, bulk or interrupt endpoint, specified by "uhe". A control
639 * transfer means that you transfer an 8-byte header first followed by
640 * a data-phase as indicated by the 8-byte header. The "timeout" is
641 * given in milliseconds.
647 *------------------------------------------------------------------------*/
649 usb_control_msg(struct usb_device
*dev
, struct usb_host_endpoint
*uhe
,
650 uint8_t request
, uint8_t requesttype
,
651 uint16_t value
, uint16_t index
, void *data
,
652 uint16_t size
, usb_timeout_t timeout
)
654 struct usb_device_request req
;
661 req
.bmRequestType
= requesttype
;
662 req
.bRequest
= request
;
663 USETW(req
.wValue
, value
);
664 USETW(req
.wIndex
, index
);
665 USETW(req
.wLength
, size
);
670 type
= (uhe
->desc
.bmAttributes
& UE_XFERTYPE
);
671 addr
= (uhe
->desc
.bEndpointAddress
& UE_ADDR
);
673 if (type
!= UE_CONTROL
) {
678 * The FreeBSD USB stack supports standard control
679 * transfers on control endpoint zero:
681 err
= usbd_do_request_flags(dev
,
682 NULL
, &req
, data
, USB_SHORT_XFER_OK
,
691 if (dev
->flags
.usb_mode
!= USB_MODE_HOST
) {
695 err
= usb_setup_endpoint(dev
, uhe
, 1 /* dummy */ );
698 * NOTE: we need to allocate real memory here so that we don't
699 * transfer data to/from the stack!
701 * 0xFFFF is a FreeBSD specific magic value.
703 urb
= usb_alloc_urb(0xFFFF, size
);
710 memcpy(urb
->setup_packet
, &req
, sizeof(req
));
712 if (size
&& (!(req
.bmRequestType
& UT_READ
))) {
713 /* move the data to a real buffer */
714 memcpy(USB_ADD_BYTES(urb
->setup_packet
, sizeof(req
)),
717 err
= usb_start_wait_urb(urb
, timeout
, &actlen
);
719 if (req
.bmRequestType
& UT_READ
) {
721 bcopy(USB_ADD_BYTES(urb
->setup_packet
,
722 sizeof(req
)), data
, actlen
);
733 /*------------------------------------------------------------------------*
736 * The following function will select which alternate setting of an
737 * USB interface you plan to use. By default alternate setting with
738 * index zero is selected. Note that "iface_no" is not the interface
739 * index, but rather the value of "bInterfaceNumber".
740 *------------------------------------------------------------------------*/
742 usb_set_interface(struct usb_device
*dev
, uint8_t iface_no
, uint8_t alt_index
)
744 struct usb_interface
*p_ui
= usb_ifnum_to_if(dev
, iface_no
);
749 if (alt_index
>= p_ui
->num_altsetting
)
751 usb_linux_cleanup_interface(dev
, p_ui
);
752 err
= -usbd_set_alt_interface_index(dev
,
753 p_ui
->bsd_iface_index
, alt_index
);
755 p_ui
->cur_altsetting
= p_ui
->altsetting
+ alt_index
;
760 /*------------------------------------------------------------------------*
763 * The following function is an extension to the Linux USB API that
764 * allows you to set a maximum buffer size for a given USB endpoint.
765 * The maximum buffer size is per URB. If you don't call this function
766 * to set a maximum buffer size, the endpoint will not be functional.
767 * Note that for isochronous endpoints the maximum buffer size must be
768 * a non-zero dummy, hence this function will base the maximum buffer
769 * size on "wMaxPacketSize".
770 *------------------------------------------------------------------------*/
772 usb_setup_endpoint(struct usb_device
*dev
,
773 struct usb_host_endpoint
*uhe
, usb_size_t bufsize
)
775 struct usb_config cfg
[2];
776 uint8_t type
= uhe
->desc
.bmAttributes
& UE_XFERTYPE
;
777 uint8_t addr
= uhe
->desc
.bEndpointAddress
;
779 if (uhe
->fbsd_buf_size
== bufsize
) {
783 usbd_transfer_unsetup(uhe
->bsd_xfer
, 2);
785 uhe
->fbsd_buf_size
= bufsize
;
790 memset(cfg
, 0, sizeof(cfg
));
792 if (type
== UE_ISOCHRONOUS
) {
795 * Isochronous transfers are special in that they don't fit
796 * into the BULK/INTR/CONTROL transfer model.
800 cfg
[0].endpoint
= addr
& UE_ADDR
;
801 cfg
[0].direction
= addr
& (UE_DIR_OUT
| UE_DIR_IN
);
802 cfg
[0].callback
= &usb_linux_isoc_callback
;
803 cfg
[0].bufsize
= 0; /* use wMaxPacketSize */
804 cfg
[0].frames
= usb_max_isoc_frames(dev
);
805 cfg
[0].flags
.proxy_buffer
= 1;
808 * The Linux USB API allows non back-to-back
809 * isochronous frames which we do not support. If the
810 * isochronous frames are not back-to-back we need to
811 * do a copy, and then we need a buffer for
812 * that. Enable this at your own risk.
814 cfg
[0].flags
.ext_buffer
= 1;
816 cfg
[0].flags
.short_xfer_ok
= 1;
818 bcopy(cfg
, cfg
+ 1, sizeof(*cfg
));
820 /* Allocate and setup two generic FreeBSD USB transfers */
822 if (usbd_transfer_setup(dev
, &uhe
->bsd_iface_index
,
823 uhe
->bsd_xfer
, cfg
, 2, uhe
, &Giant
)) {
827 if (bufsize
> (1 << 22)) {
828 /* limit buffer size */
831 /* Allocate and setup one generic FreeBSD USB transfer */
834 cfg
[0].endpoint
= addr
& UE_ADDR
;
835 cfg
[0].direction
= addr
& (UE_DIR_OUT
| UE_DIR_IN
);
836 cfg
[0].callback
= &usb_linux_non_isoc_callback
;
837 cfg
[0].bufsize
= bufsize
;
838 cfg
[0].flags
.ext_buffer
= 1; /* enable zero-copy */
839 cfg
[0].flags
.proxy_buffer
= 1;
840 cfg
[0].flags
.short_xfer_ok
= 1;
842 if (usbd_transfer_setup(dev
, &uhe
->bsd_iface_index
,
843 uhe
->bsd_xfer
, cfg
, 1, uhe
, &Giant
)) {
850 /*------------------------------------------------------------------------*
851 * usb_linux_create_usb_device
853 * The following function is used to build up a per USB device
854 * structure tree, that mimics the Linux one. The root structure
855 * is returned by this function.
856 *------------------------------------------------------------------------*/
858 usb_linux_create_usb_device(struct usb_device
*udev
, device_t dev
)
860 struct usb_config_descriptor
*cd
= usbd_get_config_descriptor(udev
);
861 struct usb_descriptor
*desc
;
862 struct usb_interface_descriptor
*id
;
863 struct usb_endpoint_descriptor
*ed
;
864 struct usb_interface
*p_ui
= NULL
;
865 struct usb_host_interface
*p_uhi
= NULL
;
866 struct usb_host_endpoint
*p_uhe
= NULL
;
868 uint16_t niface_total
;
870 uint16_t iface_no_curr
;
871 uint16_t iface_index
;
876 * We do two passes. One pass for computing necessary memory size
877 * and one pass to initialize all the allocated memory structures.
879 for (pass
= 0; pass
< 2; pass
++) {
881 iface_no_curr
= 0 - 1;
888 * Iterate over all the USB descriptors. Use the USB config
889 * descriptor pointer provided by the FreeBSD USB stack.
891 while ((desc
= usb_desc_foreach(cd
, desc
))) {
894 * Build up a tree according to the descriptors we
897 switch (desc
->bDescriptorType
) {
903 if ((ed
->bLength
< sizeof(*ed
)) ||
907 bcopy(ed
, &p_uhe
->desc
, sizeof(p_uhe
->desc
));
908 p_uhe
->bsd_iface_index
= iface_index
- 1;
909 TAILQ_INIT(&p_uhe
->bsd_urb_list
);
913 (p_uhi
- 1)->desc
.bNumEndpoints
++;
918 case UDESC_INTERFACE
:
920 if (id
->bLength
< sizeof(*id
))
923 bcopy(id
, &p_uhi
->desc
, sizeof(p_uhi
->desc
));
924 p_uhi
->desc
.bNumEndpoints
= 0;
925 p_uhi
->endpoint
= p_uhe
;
927 p_uhi
->bsd_iface_index
= iface_index
;
930 iface_no
= id
->bInterfaceNumber
;
932 if (iface_no_curr
!= iface_no
) {
934 p_ui
->altsetting
= p_uhi
- 1;
935 p_ui
->cur_altsetting
= p_uhi
- 1;
936 p_ui
->num_altsetting
= 1;
937 p_ui
->bsd_iface_index
= iface_index
;
938 p_ui
->linux_udev
= udev
;
941 iface_no_curr
= iface_no
;
945 (p_ui
- 1)->num_altsetting
++;
957 size
= (sizeof(*p_uhe
) * nedesc
) +
958 (sizeof(*p_ui
) * iface_index
) +
959 (sizeof(*p_uhi
) * niface_total
);
961 p_uhe
= malloc(size
, M_USBDEV
, M_WAITOK
| M_ZERO
);
962 p_ui
= (void *)(p_uhe
+ nedesc
);
963 p_uhi
= (void *)(p_ui
+ iface_index
);
965 udev
->linux_iface_start
= p_ui
;
966 udev
->linux_iface_end
= p_ui
+ iface_index
;
967 udev
->linux_endpoint_start
= p_uhe
;
968 udev
->linux_endpoint_end
= p_uhe
+ nedesc
;
969 udev
->devnum
= device_get_unit(dev
);
970 bcopy(&udev
->ddesc
, &udev
->descriptor
,
971 sizeof(udev
->descriptor
));
972 bcopy(udev
->ctrl_ep
.edesc
, &udev
->ep0
.desc
,
973 sizeof(udev
->ep0
.desc
));
979 /*------------------------------------------------------------------------*
982 * This function should always be used when you allocate an URB for
983 * use with the USB Linux stack. In case of an isochronous transfer
984 * you must specifiy the maximum number of "iso_packets" which you
985 * plan to transfer per URB. This function is always blocking, and
986 * "mem_flags" are not regarded like on Linux.
987 *------------------------------------------------------------------------*/
989 usb_alloc_urb(uint16_t iso_packets
, uint16_t mem_flags
)
994 if (iso_packets
== 0xFFFF) {
996 * FreeBSD specific magic value to ask for control transfer
999 size
= sizeof(*urb
) + sizeof(struct usb_device_request
) + mem_flags
;
1001 size
= sizeof(*urb
) + (iso_packets
* sizeof(urb
->iso_frame_desc
[0]));
1004 urb
= malloc(size
, M_USBDEV
, M_WAITOK
| M_ZERO
);
1007 cv_init(&urb
->cv_wait
, "URBWAIT");
1008 if (iso_packets
== 0xFFFF) {
1009 urb
->setup_packet
= (void *)(urb
+ 1);
1010 urb
->transfer_buffer
= (void *)(urb
->setup_packet
+
1011 sizeof(struct usb_device_request
));
1013 urb
->number_of_packets
= iso_packets
;
1019 /*------------------------------------------------------------------------*
1020 * usb_find_host_endpoint
1022 * The following function will return the Linux USB host endpoint
1023 * structure that matches the given endpoint type and endpoint
1024 * value. If no match is found, NULL is returned. This function is not
1025 * part of the Linux USB API and is only used internally.
1026 *------------------------------------------------------------------------*/
1027 struct usb_host_endpoint
*
1028 usb_find_host_endpoint(struct usb_device
*dev
, uint8_t type
, uint8_t ep
)
1030 struct usb_host_endpoint
*uhe
;
1031 struct usb_host_endpoint
*uhe_end
;
1032 struct usb_host_interface
*uhi
;
1033 struct usb_interface
*ui
;
1041 if (type
== UE_CONTROL
) {
1044 mask
= (UE_DIR_IN
| UE_DIR_OUT
| UE_ADDR
);
1050 * Iterate over all the interfaces searching the selected alternate
1051 * setting only, and all belonging endpoints.
1053 for (ui
= dev
->linux_iface_start
;
1054 ui
!= dev
->linux_iface_end
;
1056 uhi
= ui
->cur_altsetting
;
1058 uhe_end
= uhi
->endpoint
+ uhi
->desc
.bNumEndpoints
;
1059 for (uhe
= uhi
->endpoint
;
1062 ea
= uhe
->desc
.bEndpointAddress
;
1063 at
= uhe
->desc
.bmAttributes
;
1065 if (((ea
& mask
) == ep
) &&
1066 ((at
& UE_XFERTYPE
) == type
)) {
1073 if ((type
== UE_CONTROL
) && ((ep
& UE_ADDR
) == 0)) {
1079 /*------------------------------------------------------------------------*
1080 * usb_altnum_to_altsetting
1082 * The following function returns a pointer to an alternate setting by
1083 * index given a "usb_interface" pointer. If the alternate setting by
1084 * index does not exist, NULL is returned. And alternate setting is a
1085 * variant of an interface, but usually with slightly different
1087 *------------------------------------------------------------------------*/
1088 struct usb_host_interface
*
1089 usb_altnum_to_altsetting(const struct usb_interface
*intf
, uint8_t alt_index
)
1091 if (alt_index
>= intf
->num_altsetting
) {
1094 return (intf
->altsetting
+ alt_index
);
1097 /*------------------------------------------------------------------------*
1100 * The following function searches up an USB interface by
1101 * "bInterfaceNumber". If no match is found, NULL is returned.
1102 *------------------------------------------------------------------------*/
1103 struct usb_interface
*
1104 usb_ifnum_to_if(struct usb_device
*dev
, uint8_t iface_no
)
1106 struct usb_interface
*p_ui
;
1108 for (p_ui
= dev
->linux_iface_start
;
1109 p_ui
!= dev
->linux_iface_end
;
1111 if ((p_ui
->num_altsetting
> 0) &&
1112 (p_ui
->altsetting
->desc
.bInterfaceNumber
== iface_no
)) {
1119 /*------------------------------------------------------------------------*
1121 *------------------------------------------------------------------------*/
1123 usb_buffer_alloc(struct usb_device
*dev
, usb_size_t size
, uint16_t mem_flags
, uint8_t *dma_addr
)
1125 return (malloc(size
, M_USBDEV
, M_WAITOK
| M_ZERO
));
1128 /*------------------------------------------------------------------------*
1130 *------------------------------------------------------------------------*/
1132 usbd_get_intfdata(struct usb_interface
*intf
)
1134 return (intf
->bsd_priv_sc
);
1137 /*------------------------------------------------------------------------*
1138 * usb_linux_register
1140 * The following function is used by the "USB_DRIVER_EXPORT()" macro,
1141 * and is used to register a Linux USB driver, so that its
1142 * "usb_device_id" structures gets searched a probe time. This
1143 * function is not part of the Linux USB API, and is for internal use
1145 *------------------------------------------------------------------------*/
1147 usb_linux_register(void *arg
)
1149 struct usb_driver
*drv
= arg
;
1152 LIST_INSERT_HEAD(&usb_linux_driver_list
, drv
, linux_driver_list
);
1155 usb_needs_explore_all();
1158 /*------------------------------------------------------------------------*
1159 * usb_linux_deregister
1161 * The following function is used by the "USB_DRIVER_EXPORT()" macro,
1162 * and is used to deregister a Linux USB driver. This function will
1163 * ensure that all driver instances belonging to the Linux USB device
1164 * driver in question, gets detached before the driver is
1165 * unloaded. This function is not part of the Linux USB API, and is
1166 * for internal use only.
1167 *------------------------------------------------------------------------*/
1169 usb_linux_deregister(void *arg
)
1171 struct usb_driver
*drv
= arg
;
1172 struct usb_linux_softc
*sc
;
1176 LIST_FOREACH(sc
, &usb_linux_attached_list
, sc_attached_list
) {
1177 if (sc
->sc_udrv
== drv
) {
1179 device_detach(sc
->sc_fbsd_dev
);
1183 LIST_REMOVE(drv
, linux_driver_list
);
1187 /*------------------------------------------------------------------------*
1188 * usb_linux_free_device
1190 * The following function is only used by the FreeBSD USB stack, to
1191 * cleanup and free memory after that a Linux USB device was attached.
1192 *------------------------------------------------------------------------*/
1194 usb_linux_free_device(struct usb_device
*dev
)
1196 struct usb_host_endpoint
*uhe
;
1197 struct usb_host_endpoint
*uhe_end
;
1200 uhe
= dev
->linux_endpoint_start
;
1201 uhe_end
= dev
->linux_endpoint_end
;
1202 while (uhe
!= uhe_end
) {
1203 err
= usb_setup_endpoint(dev
, uhe
, 0);
1206 err
= usb_setup_endpoint(dev
, &dev
->ep0
, 0);
1207 free(dev
->linux_endpoint_start
, M_USBDEV
);
1210 /*------------------------------------------------------------------------*
1212 *------------------------------------------------------------------------*/
1214 usb_buffer_free(struct usb_device
*dev
, usb_size_t size
,
1215 void *addr
, uint8_t dma_addr
)
1217 free(addr
, M_USBDEV
);
1220 /*------------------------------------------------------------------------*
1222 *------------------------------------------------------------------------*/
1224 usb_free_urb(struct urb
*urb
)
1229 /* make sure that the current URB is not active */
1232 /* destroy condition variable */
1233 cv_destroy(&urb
->cv_wait
);
1236 free(urb
, M_USBDEV
);
1239 /*------------------------------------------------------------------------*
1242 * The following function can be used to initialize a custom URB. It
1243 * is not recommended to use this function. Use "usb_alloc_urb()"
1245 *------------------------------------------------------------------------*/
1247 usb_init_urb(struct urb
*urb
)
1252 memset(urb
, 0, sizeof(*urb
));
1255 /*------------------------------------------------------------------------*
1257 *------------------------------------------------------------------------*/
1259 usb_kill_urb(struct urb
*urb
)
1261 usb_unlink_urb_sub(urb
, 1);
1264 /*------------------------------------------------------------------------*
1267 * The following function sets the per Linux USB interface private
1268 * data pointer. It is used by most Linux USB device drivers.
1269 *------------------------------------------------------------------------*/
1271 usb_set_intfdata(struct usb_interface
*intf
, void *data
)
1273 intf
->bsd_priv_sc
= data
;
1276 /*------------------------------------------------------------------------*
1277 * usb_linux_cleanup_interface
1279 * The following function will release all FreeBSD USB transfers
1280 * associated with a Linux USB interface. It is for internal use only.
1281 *------------------------------------------------------------------------*/
1283 usb_linux_cleanup_interface(struct usb_device
*dev
, struct usb_interface
*iface
)
1285 struct usb_host_interface
*uhi
;
1286 struct usb_host_interface
*uhi_end
;
1287 struct usb_host_endpoint
*uhe
;
1288 struct usb_host_endpoint
*uhe_end
;
1291 uhi
= iface
->altsetting
;
1292 uhi_end
= iface
->altsetting
+ iface
->num_altsetting
;
1293 while (uhi
!= uhi_end
) {
1294 uhe
= uhi
->endpoint
;
1295 uhe_end
= uhi
->endpoint
+ uhi
->desc
.bNumEndpoints
;
1296 while (uhe
!= uhe_end
) {
1297 err
= usb_setup_endpoint(dev
, uhe
, 0);
1304 /*------------------------------------------------------------------------*
1305 * usb_linux_wait_complete
1307 * The following function is used by "usb_start_wait_urb()" to wake it
1308 * up, when an USB transfer has finished.
1309 *------------------------------------------------------------------------*/
1311 usb_linux_wait_complete(struct urb
*urb
)
1313 if (urb
->transfer_flags
& URB_IS_SLEEPING
) {
1314 cv_signal(&urb
->cv_wait
);
1316 urb
->transfer_flags
&= ~URB_WAIT_WAKEUP
;
1319 /*------------------------------------------------------------------------*
1320 * usb_linux_complete
1321 *------------------------------------------------------------------------*/
1323 usb_linux_complete(struct usb_xfer
*xfer
)
1327 urb
= usbd_xfer_get_priv(xfer
);
1328 usbd_xfer_set_priv(xfer
, NULL
);
1329 if (urb
->complete
) {
1330 (urb
->complete
) (urb
);
1334 /*------------------------------------------------------------------------*
1335 * usb_linux_isoc_callback
1337 * The following is the FreeBSD isochronous USB callback. Isochronous
1338 * frames are USB packets transferred 1000 or 8000 times per second,
1339 * depending on whether a full- or high- speed USB transfer is
1341 *------------------------------------------------------------------------*/
1343 usb_linux_isoc_callback(struct usb_xfer
*xfer
, usb_error_t error
)
1345 usb_frlength_t max_frame
= xfer
->max_frame_size
;
1346 usb_frlength_t offset
;
1348 struct urb
*urb
= usbd_xfer_get_priv(xfer
);
1349 struct usb_host_endpoint
*uhe
= usbd_xfer_softc(xfer
);
1350 struct usb_iso_packet_descriptor
*uipd
;
1354 switch (USB_GET_STATE(xfer
)) {
1355 case USB_ST_TRANSFERRED
:
1357 if (urb
->bsd_isread
) {
1359 /* copy in data with regard to the URB */
1363 for (x
= 0; x
< urb
->number_of_packets
; x
++) {
1364 uipd
= urb
->iso_frame_desc
+ x
;
1365 if (uipd
->length
> xfer
->frlengths
[x
]) {
1366 if (urb
->transfer_flags
& URB_SHORT_NOT_OK
) {
1367 /* XXX should be EREMOTEIO */
1368 uipd
->status
= -EPIPE
;
1375 uipd
->actual_length
= xfer
->frlengths
[x
];
1376 if (!xfer
->flags
.ext_buffer
) {
1377 usbd_copy_out(xfer
->frbuffers
, offset
,
1378 USB_ADD_BYTES(urb
->transfer_buffer
,
1379 uipd
->offset
), uipd
->actual_length
);
1381 offset
+= max_frame
;
1384 for (x
= 0; x
< urb
->number_of_packets
; x
++) {
1385 uipd
= urb
->iso_frame_desc
+ x
;
1386 uipd
->actual_length
= xfer
->frlengths
[x
];
1391 urb
->actual_length
= xfer
->actlen
;
1393 /* check for short transfer */
1394 if (xfer
->actlen
< xfer
->sumlen
) {
1395 /* short transfer */
1396 if (urb
->transfer_flags
& URB_SHORT_NOT_OK
) {
1397 /* XXX should be EREMOTEIO */
1398 urb
->status
= -EPIPE
;
1408 usb_linux_complete(xfer
);
1413 if (xfer
->priv_fifo
== NULL
) {
1415 /* get next transfer */
1416 urb
= TAILQ_FIRST(&uhe
->bsd_urb_list
);
1421 TAILQ_REMOVE(&uhe
->bsd_urb_list
, urb
, bsd_urb_list
);
1422 urb
->bsd_urb_list
.tqe_prev
= NULL
;
1424 x
= xfer
->max_frame_count
;
1425 if (urb
->number_of_packets
> x
) {
1426 /* XXX simply truncate the transfer */
1427 urb
->number_of_packets
= x
;
1430 DPRINTF("Already got a transfer\n");
1432 /* already got a transfer (should not happen) */
1433 urb
= usbd_xfer_get_priv(xfer
);
1436 urb
->bsd_isread
= (uhe
->desc
.bEndpointAddress
& UE_DIR_IN
) ? 1 : 0;
1438 if (xfer
->flags
.ext_buffer
) {
1439 /* set virtual address to load */
1440 usbd_xfer_set_frame_data(xfer
, 0, urb
->transfer_buffer
, 0);
1442 if (!(urb
->bsd_isread
)) {
1444 /* copy out data with regard to the URB */
1448 for (x
= 0; x
< urb
->number_of_packets
; x
++) {
1449 uipd
= urb
->iso_frame_desc
+ x
;
1450 usbd_xfer_set_frame_len(xfer
, x
, uipd
->length
);
1451 if (!xfer
->flags
.ext_buffer
) {
1452 usbd_copy_in(xfer
->frbuffers
, offset
,
1453 USB_ADD_BYTES(urb
->transfer_buffer
,
1454 uipd
->offset
), uipd
->length
);
1456 offset
+= uipd
->length
;
1461 * compute the transfer length into the "offset"
1465 offset
= urb
->number_of_packets
* max_frame
;
1467 /* setup "frlengths" array */
1469 for (x
= 0; x
< urb
->number_of_packets
; x
++) {
1470 uipd
= urb
->iso_frame_desc
+ x
;
1471 usbd_xfer_set_frame_len(xfer
, x
, max_frame
);
1474 usbd_xfer_set_priv(xfer
, urb
);
1475 xfer
->flags
.force_short_xfer
= 0;
1476 xfer
->timeout
= urb
->timeout
;
1477 xfer
->nframes
= urb
->number_of_packets
;
1478 usbd_transfer_submit(xfer
);
1481 default: /* Error */
1482 if (xfer
->error
== USB_ERR_CANCELLED
) {
1483 urb
->status
= -ECONNRESET
;
1485 urb
->status
= -EPIPE
; /* stalled */
1488 /* Set zero for "actual_length" */
1489 urb
->actual_length
= 0;
1491 /* Set zero for "actual_length" */
1492 for (x
= 0; x
< urb
->number_of_packets
; x
++) {
1493 urb
->iso_frame_desc
[x
].actual_length
= 0;
1494 urb
->iso_frame_desc
[x
].status
= urb
->status
;
1498 usb_linux_complete(xfer
);
1500 if (xfer
->error
== USB_ERR_CANCELLED
) {
1501 /* we need to return in this case */
1509 /*------------------------------------------------------------------------*
1510 * usb_linux_non_isoc_callback
1512 * The following is the FreeBSD BULK/INTERRUPT and CONTROL USB
1513 * callback. It dequeues Linux USB stack compatible URB's, transforms
1514 * the URB fields into a FreeBSD USB transfer, and defragments the USB
1515 * transfer as required. When the transfer is complete the "complete"
1516 * callback is called.
1517 *------------------------------------------------------------------------*/
1519 usb_linux_non_isoc_callback(struct usb_xfer
*xfer
, usb_error_t error
)
1522 REQ_SIZE
= sizeof(struct usb_device_request
)
1524 struct urb
*urb
= usbd_xfer_get_priv(xfer
);
1525 struct usb_host_endpoint
*uhe
= usbd_xfer_softc(xfer
);
1527 usb_frlength_t max_bulk
= usbd_xfer_max_len(xfer
);
1528 uint8_t data_frame
= xfer
->flags_int
.control_xfr
? 1 : 0;
1532 switch (USB_GET_STATE(xfer
)) {
1533 case USB_ST_TRANSFERRED
:
1535 if (xfer
->flags_int
.control_xfr
) {
1537 /* don't transfer the setup packet again: */
1539 usbd_xfer_set_frame_len(xfer
, 0, 0);
1541 if (urb
->bsd_isread
&& (!xfer
->flags
.ext_buffer
)) {
1542 /* copy in data with regard to the URB */
1543 usbd_copy_out(xfer
->frbuffers
+ data_frame
, 0,
1544 urb
->bsd_data_ptr
, xfer
->frlengths
[data_frame
]);
1546 urb
->bsd_length_rem
-= xfer
->frlengths
[data_frame
];
1547 urb
->bsd_data_ptr
+= xfer
->frlengths
[data_frame
];
1548 urb
->actual_length
+= xfer
->frlengths
[data_frame
];
1550 /* check for short transfer */
1551 if (xfer
->actlen
< xfer
->sumlen
) {
1552 urb
->bsd_length_rem
= 0;
1554 /* short transfer */
1555 if (urb
->transfer_flags
& URB_SHORT_NOT_OK
) {
1556 urb
->status
= -EPIPE
;
1561 /* check remainder */
1562 if (urb
->bsd_length_rem
> 0) {
1570 usb_linux_complete(xfer
);
1574 /* get next transfer */
1575 urb
= TAILQ_FIRST(&uhe
->bsd_urb_list
);
1580 TAILQ_REMOVE(&uhe
->bsd_urb_list
, urb
, bsd_urb_list
);
1581 urb
->bsd_urb_list
.tqe_prev
= NULL
;
1583 usbd_xfer_set_priv(xfer
, urb
);
1584 xfer
->flags
.force_short_xfer
= 0;
1585 xfer
->timeout
= urb
->timeout
;
1587 if (xfer
->flags_int
.control_xfr
) {
1590 * USB control transfers need special handling.
1591 * First copy in the header, then copy in data!
1593 if (!xfer
->flags
.ext_buffer
) {
1594 usbd_copy_in(xfer
->frbuffers
, 0,
1595 urb
->setup_packet
, REQ_SIZE
);
1596 usbd_xfer_set_frame_len(xfer
, 0, REQ_SIZE
);
1598 /* set virtual address to load */
1599 usbd_xfer_set_frame_data(xfer
, 0,
1600 urb
->setup_packet
, REQ_SIZE
);
1603 ptr
= urb
->setup_packet
;
1605 /* setup data transfer direction and length */
1606 urb
->bsd_isread
= (ptr
[0] & UT_READ
) ? 1 : 0;
1607 urb
->bsd_length_rem
= ptr
[6] | (ptr
[7] << 8);
1611 /* setup data transfer direction */
1613 urb
->bsd_length_rem
= urb
->transfer_buffer_length
;
1614 urb
->bsd_isread
= (uhe
->desc
.bEndpointAddress
&
1618 urb
->bsd_data_ptr
= urb
->transfer_buffer
;
1619 urb
->actual_length
= 0;
1622 if (max_bulk
> urb
->bsd_length_rem
) {
1623 max_bulk
= urb
->bsd_length_rem
;
1625 /* check if we need to force a short transfer */
1627 if ((max_bulk
== urb
->bsd_length_rem
) &&
1628 (urb
->transfer_flags
& URB_ZERO_PACKET
) &&
1629 (!xfer
->flags_int
.control_xfr
)) {
1630 xfer
->flags
.force_short_xfer
= 1;
1632 /* check if we need to copy in data */
1634 if (xfer
->flags
.ext_buffer
) {
1635 /* set virtual address to load */
1636 usbd_xfer_set_frame_data(xfer
, data_frame
,
1637 urb
->bsd_data_ptr
, max_bulk
);
1638 } else if (!urb
->bsd_isread
) {
1639 /* copy out data with regard to the URB */
1640 usbd_copy_in(xfer
->frbuffers
+ data_frame
, 0,
1641 urb
->bsd_data_ptr
, max_bulk
);
1642 usbd_xfer_set_frame_len(xfer
, data_frame
, max_bulk
);
1644 if (xfer
->flags_int
.control_xfr
) {
1653 usbd_transfer_submit(xfer
);
1657 if (xfer
->error
== USB_ERR_CANCELLED
) {
1658 urb
->status
= -ECONNRESET
;
1660 urb
->status
= -EPIPE
;
1663 /* Set zero for "actual_length" */
1664 urb
->actual_length
= 0;
1667 usb_linux_complete(xfer
);
1669 if (xfer
->error
== USB_ERR_CANCELLED
) {
1670 /* we need to return in this case */
1677 /*------------------------------------------------------------------------*
1679 *------------------------------------------------------------------------*/
1681 usb_fill_bulk_urb(struct urb
*urb
, struct usb_device
*udev
,
1682 struct usb_host_endpoint
*uhe
, void *buf
,
1683 int length
, usb_complete_t callback
, void *arg
)
1686 urb
->endpoint
= uhe
;
1687 urb
->transfer_buffer
= buf
;
1688 urb
->transfer_buffer_length
= length
;
1689 urb
->complete
= callback
;
1693 /*------------------------------------------------------------------------*
1696 * NOTE: This function can also be used for interrupt endpoints!
1701 *------------------------------------------------------------------------*/
1703 usb_bulk_msg(struct usb_device
*udev
, struct usb_host_endpoint
*uhe
,
1704 void *data
, int len
, uint16_t *pactlen
, usb_timeout_t timeout
)
1714 err
= usb_setup_endpoint(udev
, uhe
, 4096 /* bytes */);
1718 urb
= usb_alloc_urb(0, 0);
1722 usb_fill_bulk_urb(urb
, udev
, uhe
, data
, len
,
1723 usb_linux_wait_complete
, NULL
);
1725 err
= usb_start_wait_urb(urb
, timeout
, pactlen
);