10a9a8c2eSIan Dowse.\" 20a9a8c2eSIan Dowse.\" Copyright (c) 2005 Ian Dowse <iedowse@FreeBSD.org> 30a9a8c2eSIan Dowse.\" All rights reserved. 40a9a8c2eSIan Dowse.\" 50a9a8c2eSIan Dowse.\" Redistribution and use in source and binary forms, with or without 60a9a8c2eSIan Dowse.\" modification, are permitted provided that the following conditions 70a9a8c2eSIan Dowse.\" are met: 80a9a8c2eSIan Dowse.\" 1. Redistributions of source code must retain the above copyright 90a9a8c2eSIan Dowse.\" notice, this list of conditions and the following disclaimer. 100a9a8c2eSIan Dowse.\" 2. Redistributions in binary form must reproduce the above copyright 110a9a8c2eSIan Dowse.\" notice, this list of conditions and the following disclaimer in the 120a9a8c2eSIan Dowse.\" documentation and/or other materials provided with the distribution. 130a9a8c2eSIan Dowse.\" 140a9a8c2eSIan Dowse.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 150a9a8c2eSIan Dowse.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 160a9a8c2eSIan Dowse.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 170a9a8c2eSIan Dowse.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 180a9a8c2eSIan Dowse.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 190a9a8c2eSIan Dowse.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 200a9a8c2eSIan Dowse.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 210a9a8c2eSIan Dowse.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 220a9a8c2eSIan Dowse.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 230a9a8c2eSIan Dowse.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 240a9a8c2eSIan Dowse.\" SUCH DAMAGE. 250a9a8c2eSIan Dowse.\" 260a9a8c2eSIan Dowse.\" $FreeBSD$ 27b35f050eSAndrew Thompson.Dd June 24, 2009 280a9a8c2eSIan Dowse.Dt USBDI 9 29aa12cea2SUlrich Spörlein.Os 300a9a8c2eSIan Dowse.Sh NAME 31b35f050eSAndrew Thompson.Nm usb_fifo_alloc_buffer , 32b35f050eSAndrew Thompson.Nm usb_fifo_attach , 33b35f050eSAndrew Thompson.Nm usb_fifo_detach , 34b35f050eSAndrew Thompson.Nm usb_fifo_free_buffer , 35b35f050eSAndrew Thompson.Nm usb_fifo_get_data , 36b35f050eSAndrew Thompson.Nm usb_fifo_get_data_buffer , 37b35f050eSAndrew Thompson.Nm usb_fifo_get_data_error , 38b35f050eSAndrew Thompson.Nm usb_fifo_get_data_linear , 39b35f050eSAndrew Thompson.Nm usb_fifo_put_bytes_max , 40b35f050eSAndrew Thompson.Nm usb_fifo_put_data , 41b35f050eSAndrew Thompson.Nm usb_fifo_put_data_buffer , 42b35f050eSAndrew Thompson.Nm usb_fifo_put_data_error , 43b35f050eSAndrew Thompson.Nm usb_fifo_put_data_linear , 44b35f050eSAndrew Thompson.Nm usb_fifo_reset , 45b35f050eSAndrew Thompson.Nm usb_fifo_softc , 46b35f050eSAndrew Thompson.Nm usb_fifo_wakeup , 470a9a8c2eSIan Dowse.Nm usbd_do_request , 480a9a8c2eSIan Dowse.Nm usbd_do_request_flags , 490a9a8c2eSIan Dowse.Nm usbd_errstr , 50b35f050eSAndrew Thompson.Nm usbd_lookup_id_by_info , 51b35f050eSAndrew Thompson.Nm usbd_lookup_id_by_uaa , 52b35f050eSAndrew Thompson.Nm usbd_transfer_clear_stall , 53b35f050eSAndrew Thompson.Nm usbd_transfer_drain , 54b35f050eSAndrew Thompson.Nm usbd_transfer_pending , 55b35f050eSAndrew Thompson.Nm usbd_transfer_poll , 56b35f050eSAndrew Thompson.Nm usbd_transfer_setup , 57b35f050eSAndrew Thompson.Nm usbd_transfer_start , 58b35f050eSAndrew Thompson.Nm usbd_transfer_stop , 59b35f050eSAndrew Thompson.Nm usbd_transfer_submit , 60b35f050eSAndrew Thompson.Nm usbd_transfer_unsetup , 61b35f050eSAndrew Thompson.Nm usbd_xfer_clr_flag , 62b35f050eSAndrew Thompson.Nm usbd_xfer_frame_data , 63b35f050eSAndrew Thompson.Nm usbd_xfer_frame_len , 64b35f050eSAndrew Thompson.Nm usbd_xfer_get_frame , 65b35f050eSAndrew Thompson.Nm usbd_xfer_get_priv , 66b35f050eSAndrew Thompson.Nm usbd_xfer_is_stalled , 67b35f050eSAndrew Thompson.Nm usbd_xfer_max_framelen , 68b35f050eSAndrew Thompson.Nm usbd_xfer_max_frames , 69b35f050eSAndrew Thompson.Nm usbd_xfer_max_len , 70b35f050eSAndrew Thompson.Nm usbd_xfer_set_flag , 71b35f050eSAndrew Thompson.Nm usbd_xfer_set_frame_data , 72b35f050eSAndrew Thompson.Nm usbd_xfer_set_frame_len , 73b35f050eSAndrew Thompson.Nm usbd_xfer_set_frame_offset , 74b35f050eSAndrew Thompson.Nm usbd_xfer_set_frames , 75b35f050eSAndrew Thompson.Nm usbd_xfer_set_interval , 76b35f050eSAndrew Thompson.Nm usbd_xfer_set_priv , 77b35f050eSAndrew Thompson.Nm usbd_xfer_set_stall , 78b35f050eSAndrew Thompson.Nm usbd_xfer_set_timeout , 79b35f050eSAndrew Thompson.Nm usbd_xfer_softc , 80b35f050eSAndrew Thompson.Nm usbd_xfer_state , 81b35f050eSAndrew Thompson.Nm usbd_xfer_status 820a9a8c2eSIan Dowse.Nd Universal Serial Bus driver programming interface 830a9a8c2eSIan Dowse.Sh SYNOPSIS 840a9a8c2eSIan Dowse.In dev/usb/usb.h 850a9a8c2eSIan Dowse.In dev/usb/usbdi.h 860a9a8c2eSIan Dowse.In dev/usb/usbdi_util.h 870a9a8c2eSIan Dowse.Sh DESCRIPTION 880a9a8c2eSIan DowseThe Universal Serial Bus (USB) driver programming interface provides 890a9a8c2eSIan DowseUSB peripheral drivers with a host controller independent API for 900a9a8c2eSIan Dowsecontrolling and communicating with USB peripherals. 91b35f050eSAndrew ThompsonThe 92b35f050eSAndrew Thompson.Nm usb 93b35f050eSAndrew Thompsonmodule supports both USB Host and USB Device side mode. 94b35f050eSAndrew Thompson. 95b35f050eSAndrew Thompson.Sh USB KERNEL PROGRAMMING 96b35f050eSAndrew ThompsonHere is a list of commonly used functions: 970a9a8c2eSIan Dowse.Pp 98b35f050eSAndrew Thompson. 99b35f050eSAndrew Thompson.Ft "usb_error_t" 100b35f050eSAndrew Thompson.Fo "usbd_transfer_setup" 101b35f050eSAndrew Thompson.Fa "udev" 102b35f050eSAndrew Thompson.Fa "ifaces" 103b35f050eSAndrew Thompson.Fa "pxfer" 104b35f050eSAndrew Thompson.Fa "setup_start" 105b35f050eSAndrew Thompson.Fa "n_setup" 106b35f050eSAndrew Thompson.Fa "priv_sc" 107b35f050eSAndrew Thompson.Fa "priv_mtx" 108b35f050eSAndrew Thompson.Fc 109b35f050eSAndrew Thompson. 1100a9a8c2eSIan Dowse.Pp 111b35f050eSAndrew Thompson. 112b35f050eSAndrew Thompson.Ft "void" 113b35f050eSAndrew Thompson.Fo "usbd_transfer_unsetup" 114b35f050eSAndrew Thompson.Fa "pxfer" 115b35f050eSAndrew Thompson.Fa "n_setup" 116b35f050eSAndrew Thompson.Fc 117b35f050eSAndrew Thompson. 1180a9a8c2eSIan Dowse.Pp 119b35f050eSAndrew Thompson. 120b35f050eSAndrew Thompson.Ft "void" 121b35f050eSAndrew Thompson.Fo "usbd_transfer_start" 122b35f050eSAndrew Thompson.Fa "xfer" 123b35f050eSAndrew Thompson.Fc 124b35f050eSAndrew Thompson. 1250a9a8c2eSIan Dowse.Pp 126b35f050eSAndrew Thompson. 127b35f050eSAndrew Thompson.Ft "void" 128b35f050eSAndrew Thompson.Fo "usbd_transfer_stop" 129b35f050eSAndrew Thompson.Fa "xfer" 130b35f050eSAndrew Thompson.Fc 131b35f050eSAndrew Thompson. 1320a9a8c2eSIan Dowse.Pp 133b35f050eSAndrew Thompson. 134b35f050eSAndrew Thompson.Ft "void" 135b35f050eSAndrew Thompson.Fo "usbd_transfer_drain" 136b35f050eSAndrew Thompson.Fa "xfer" 137b35f050eSAndrew Thompson.Fc 138b35f050eSAndrew Thompson. 139b35f050eSAndrew Thompson. 140b35f050eSAndrew Thompson. 141b35f050eSAndrew Thompson.Sh USB TRANSFER MANAGEMENT FUNCTIONS 142b35f050eSAndrew ThompsonThe USB standard defines four types of USB transfers. 143b35f050eSAndrew Thompson. 144b35f050eSAndrew ThompsonControl transfers, Bulk transfers, Interrupt transfers and Isochronous 145b35f050eSAndrew Thompsontransfers. 146b35f050eSAndrew Thompson. 147b35f050eSAndrew ThompsonAll the transfer types are managed using the following five functions: 148b35f050eSAndrew Thompson. 1490a9a8c2eSIan Dowse.Pp 150b35f050eSAndrew Thompson. 151b35f050eSAndrew Thompson.Fn usbd_transfer_setup 152b35f050eSAndrew ThompsonThis function will allocate memory for and initialise an array of USB 153b35f050eSAndrew Thompsontransfers and all required DMA memory. 154b35f050eSAndrew Thompson. 155b35f050eSAndrew ThompsonThis function can sleep or block waiting for resources to become 156b35f050eSAndrew Thompsonavailable. 157b35f050eSAndrew Thompson.Fa udev 158b35f050eSAndrew Thompsonis a pointer to "struct usb_device". 159b35f050eSAndrew Thompson.Fa ifaces 160*7851d429SEdward Tomasz Napieralais an array of interface index numbers to use. 161*7851d429SEdward Tomasz NapieralaSee "if_index". 162b35f050eSAndrew Thompson.Fa pxfer 163b35f050eSAndrew Thompsonis a pointer to an array of USB transfer pointers that are initialized 164b35f050eSAndrew Thompsonto NULL, and then pointed to allocated USB transfers. 165b35f050eSAndrew Thompson.Fa setup_start 166b35f050eSAndrew Thompsonis a pointer to an array of USB config structures. 167b35f050eSAndrew Thompson.Fa n_setup 168b35f050eSAndrew Thompsonis a number telling the USB system how many USB transfers should be 169b35f050eSAndrew Thompsonsetup. 170b35f050eSAndrew Thompson.Fa priv_sc 171b35f050eSAndrew Thompsonis the private softc pointer, which will be used to initialize 172b35f050eSAndrew Thompson"xfer->priv_sc". 173b35f050eSAndrew Thompson.Fa priv_mtx 174b35f050eSAndrew Thompsonis the private mutex protecting the transfer structure and the 175*7851d429SEdward Tomasz Napieralasoftc. 176*7851d429SEdward Tomasz NapieralaThis pointer is used to initialize "xfer->priv_mtx". 177*7851d429SEdward Tomasz NapieralaThis function returns zero upon success. 178*7851d429SEdward Tomasz NapieralaA non-zero return value indicates failure. 179b35f050eSAndrew Thompson. 1800a9a8c2eSIan Dowse.Pp 181b35f050eSAndrew Thompson. 182b35f050eSAndrew Thompson.Fn usbd_transfer_unsetup 183b35f050eSAndrew ThompsonThis function will release the given USB transfers and all allocated 184b35f050eSAndrew Thompsonresources associated with these USB transfers. 185b35f050eSAndrew Thompson.Fa pxfer 186b35f050eSAndrew Thompsonis a pointer to an array of USB transfer pointers, that may be NULL, 187b35f050eSAndrew Thompsonthat should be freed by the USB system. 188b35f050eSAndrew Thompson.Fa n_setup 189b35f050eSAndrew Thompsonis a number telling the USB system how many USB transfers should be 190b35f050eSAndrew Thompsonunsetup. 191b35f050eSAndrew Thompson. 192b35f050eSAndrew ThompsonThis function can sleep waiting for USB transfers to complete. 193b35f050eSAndrew Thompson. 194b35f050eSAndrew ThompsonThis function is NULL safe with regard to the USB transfer structure 195b35f050eSAndrew Thompsonpointer. 196b35f050eSAndrew Thompson. 197b35f050eSAndrew ThompsonIt is not allowed to call this function from the USB transfer 198b35f050eSAndrew Thompsoncallback. 199b35f050eSAndrew Thompson. 2000a9a8c2eSIan Dowse.Pp 201b35f050eSAndrew Thompson. 202b35f050eSAndrew Thompson.Fn usbd_transfer_start 203b35f050eSAndrew ThompsonThis function will start the USB transfer pointed to by 204b35f050eSAndrew Thompson.Fa xfer , 205b35f050eSAndrew Thompsonif not already started. 206b35f050eSAndrew Thompson. 207b35f050eSAndrew ThompsonThis function is always non-blocking and must be called with the 208b35f050eSAndrew Thompsonso-called private USB mutex locked. 209b35f050eSAndrew Thompson. 210b35f050eSAndrew ThompsonThis function is NULL safe with regard to the USB transfer structure 211b35f050eSAndrew Thompsonpointer. 212b35f050eSAndrew Thompson. 2130a9a8c2eSIan Dowse.Pp 214b35f050eSAndrew Thompson. 215b35f050eSAndrew Thompson.Fn usbd_transfer_stop 216b35f050eSAndrew ThompsonThis function will stop the USB transfer pointed to by 217b35f050eSAndrew Thompson.Fa xfer , 218b35f050eSAndrew Thompsonif not already stopped. 219b35f050eSAndrew Thompson. 220b35f050eSAndrew ThompsonThis function is always non-blocking and must be called with the 221b35f050eSAndrew Thompsonso-called private USB mutex locked. 222b35f050eSAndrew Thompson. 223b35f050eSAndrew ThompsonThis function can return before the USB callback has been called. 224b35f050eSAndrew Thompson. 225b35f050eSAndrew ThompsonThis function is NULL safe with regard to the USB transfer structure 226b35f050eSAndrew Thompsonpointer. 227b35f050eSAndrew Thompson. 228b35f050eSAndrew ThompsonIf the transfer was in progress, the callback will called with 229b35f050eSAndrew Thompson"USB_ST_ERROR" and "error = USB_ERR_CANCELLED". 230b35f050eSAndrew Thompson. 2310a9a8c2eSIan Dowse.Pp 232b35f050eSAndrew Thompson. 233b35f050eSAndrew Thompson.Fn usbd_transfer_drain 234b35f050eSAndrew ThompsonThis function will stop an USB transfer, if not already stopped and 235b35f050eSAndrew Thompsonwait for any additional USB hardware operations to complete. 236b35f050eSAndrew Thompson. 237b35f050eSAndrew ThompsonBuffers that are loaded into DMA using "usbd_xfer_set_frame_data()" can 238b35f050eSAndrew Thompsonsafely be freed after that this function has returned. 239b35f050eSAndrew Thompson. 240b35f050eSAndrew ThompsonThis function can block the caller and will not return before the USB 241b35f050eSAndrew Thompsoncallback has been called. 242b35f050eSAndrew Thompson. 243b35f050eSAndrew ThompsonThis function is NULL safe with regard to the USB transfer structure 244b35f050eSAndrew Thompsonpointer. 245b35f050eSAndrew Thompson. 246b35f050eSAndrew Thompson.Sh USB TRANSFER CALLBACK 247b35f050eSAndrew Thompson. 248b35f050eSAndrew ThompsonThe USB callback has three states. 249b35f050eSAndrew Thompson. 250*7851d429SEdward Tomasz NapieralaUSB_ST_SETUP, USB_ST_TRANSFERRED and USB_ST_ERROR. 251*7851d429SEdward Tomasz NapieralaUSB_ST_SETUP is the initial state. 252b35f050eSAndrew Thompson. 253b35f050eSAndrew ThompsonAfter the callback has been called with this state it will always be 254b35f050eSAndrew Thompsoncalled back at a later stage in one of the other two states. 255b35f050eSAndrew Thompson. 256b35f050eSAndrew ThompsonThe USB callback should not restart the USB transfer in case the error 257b35f050eSAndrew Thompsoncause is USB_ERR_CANCELLED. 258b35f050eSAndrew Thompson. 259b35f050eSAndrew ThompsonThe USB callback is protected from recursion. 260b35f050eSAndrew Thompson. 261b35f050eSAndrew ThompsonThat means one can start and stop whatever transfer from the callback 262b35f050eSAndrew Thompsonof another transfer one desires. 263b35f050eSAndrew Thompson. 264b35f050eSAndrew ThompsonAlso the transfer that is currently called back. 265b35f050eSAndrew Thompson. 266b35f050eSAndrew ThompsonRecursion is handled like this that when the callback that wants to 267b35f050eSAndrew Thompsonrecurse returns it is called one more time. 268b35f050eSAndrew Thompson. 269b35f050eSAndrew Thompson. 2700a9a8c2eSIan Dowse.Pp 271b35f050eSAndrew Thompson. 272b35f050eSAndrew Thompson.Fn usbd_transfer_submit 273b35f050eSAndrew ThompsonThis function should only be called from within the USB callback and 274b35f050eSAndrew Thompsonis used to start the USB hardware. 275b35f050eSAndrew Thompson. 276b35f050eSAndrew ThompsonAn USB transfer can have multiple frames consisting of one or more USB 277b35f050eSAndrew Thompsonpackets making up an I/O vector for all USB transfer types. 278b35f050eSAndrew Thompson. 279b35f050eSAndrew Thompson.Bd -literal -offset indent 280b35f050eSAndrew Thompsonvoid 281b35f050eSAndrew Thompsonusb_default_callback(struct usb_xfer *xfer, usb_error_t error) 282b35f050eSAndrew Thompson{ 283b35f050eSAndrew Thompson int actlen; 284b35f050eSAndrew Thompson 285b35f050eSAndrew Thompson usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 286b35f050eSAndrew Thompson 287b35f050eSAndrew Thompson switch (USB_GET_STATE(xfer)) { 288b35f050eSAndrew Thompson case USB_ST_SETUP: 289b35f050eSAndrew Thompson /* 290b35f050eSAndrew Thompson * Setup xfer frame lengths/count and data 291b35f050eSAndrew Thompson */ 292b35f050eSAndrew Thompson usbd_transfer_submit(xfer); 293b35f050eSAndrew Thompson break; 294b35f050eSAndrew Thompson 295b35f050eSAndrew Thompson case USB_ST_TRANSFERRED: 296b35f050eSAndrew Thompson /* 297b35f050eSAndrew Thompson * Read usb frame data, if any. 298b35f050eSAndrew Thompson * "actlen" has the total length for all frames 2999ba47352SJoel Dahl * transferred. 300b35f050eSAndrew Thompson */ 301b35f050eSAndrew Thompson break; 302b35f050eSAndrew Thompson 303b35f050eSAndrew Thompson default: /* Error */ 304b35f050eSAndrew Thompson /* 305b35f050eSAndrew Thompson * Print error message and clear stall 306b35f050eSAndrew Thompson * for example. 307b35f050eSAndrew Thompson */ 308b35f050eSAndrew Thompson break; 309b35f050eSAndrew Thompson } 310b35f050eSAndrew Thompson /* 311b35f050eSAndrew Thompson * Here it is safe to do something without the private 312b35f050eSAndrew Thompson * USB mutex locked. 313b35f050eSAndrew Thompson */ 314b35f050eSAndrew Thompson return; 3150a9a8c2eSIan Dowse} 3160a9a8c2eSIan Dowse.Ed 317b35f050eSAndrew Thompson. 318b35f050eSAndrew Thompson.Sh USB CONTROL TRANSFERS 319b35f050eSAndrew ThompsonAn USB control transfer has three parts. 320b35f050eSAndrew Thompson. 321b35f050eSAndrew ThompsonFirst the SETUP packet, then DATA packet(s) and then a STATUS 322b35f050eSAndrew Thompsonpacket. 323b35f050eSAndrew Thompson. 324b35f050eSAndrew ThompsonThe SETUP packet is always pointed to by frame 0 and the 325b35f050eSAndrew Thompsonlength is set by 326b35f050eSAndrew Thompson.Fn usbd_xfer_frame_len 327b35f050eSAndrew Thompsonalso if there should not be 328*7851d429SEdward Tomasz Napieralasent any SETUP packet! 329*7851d429SEdward Tomasz NapieralaIf an USB control transfer has no DATA stage, 330b35f050eSAndrew Thompsonthen the number of frames should be set to 1. 331b35f050eSAndrew Thompson. 332b35f050eSAndrew ThompsonElse the default number of frames is 2. 333b35f050eSAndrew Thompson. 334b35f050eSAndrew Thompson.Bd -literal -offset indent 335b35f050eSAndrew Thompson 336b35f050eSAndrew ThompsonExample1: SETUP + STATUS 337b35f050eSAndrew Thompson usbd_xfer_set_frames(xfer, 1); 338b35f050eSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, 8); 339b35f050eSAndrew Thompson usbd_transfer_submit(xfer); 340b35f050eSAndrew Thompson 341b35f050eSAndrew ThompsonExample2: SETUP + DATA + STATUS 342b35f050eSAndrew Thompson usbd_xfer_set_frames(xfer, 2); 343b35f050eSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, 8); 344b35f050eSAndrew Thompson usbd_xfer_set_frame_len(xfer, 1, 1); 345b35f050eSAndrew Thompson usbd_transfer_submit(xfer); 346b35f050eSAndrew Thompson 347b35f050eSAndrew ThompsonExample3: SETUP + DATA + STATUS - split 348b35f050eSAndrew Thompson1st callback: 349b35f050eSAndrew Thompson usbd_xfer_set_frames(xfer, 1); 350b35f050eSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, 8); 351b35f050eSAndrew Thompson usbd_transfer_submit(xfer); 352b35f050eSAndrew Thompson 353b35f050eSAndrew Thompson2nd callback: 354b35f050eSAndrew Thompson /* IMPORTANT: frbuffers[0] must still point at the setup packet! */ 355b35f050eSAndrew Thompson usbd_xfer_set_frames(xfer, 2); 356b35f050eSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, 0); 357b35f050eSAndrew Thompson usbd_xfer_set_frame_len(xfer, 1, 1); 358b35f050eSAndrew Thompson usbd_transfer_submit(xfer); 359b35f050eSAndrew Thompson 360b35f050eSAndrew ThompsonExample4: SETUP + STATUS - split 361b35f050eSAndrew Thompson1st callback: 362b35f050eSAndrew Thompson usbd_xfer_set_frames(xfer, 1); 363b35f050eSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, 8); 364b35f050eSAndrew Thompson usbd_xfer_set_flag(xfer, USB_MANUAL_STATUS); 365b35f050eSAndrew Thompson usbd_transfer_submit(xfer); 366b35f050eSAndrew Thompson 367b35f050eSAndrew Thompson2nd callback: 368b35f050eSAndrew Thompson usbd_xfer_set_frames(xfer, 1); 369b35f050eSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, 0); 370b35f050eSAndrew Thompson usbd_xfer_clr_flag(xfer, USB_MANUAL_STATUS); 371b35f050eSAndrew Thompson usbd_transfer_submit(xfer); 372b35f050eSAndrew Thompson 373b35f050eSAndrew Thompson.Ed 374b35f050eSAndrew Thompson.Sh USB TRANSFER CONFIG 375b35f050eSAndrew ThompsonTo simply the search for endpoints the 376b35f050eSAndrew Thompson.Nm usb 377b35f050eSAndrew Thompsonmodule defines a USB config structure where it is possible to specify 378b35f050eSAndrew Thompsonthe characteristics of the wanted endpoint. 379b35f050eSAndrew Thompson.Bd -literal -offset indent 380b35f050eSAndrew Thompson 381b35f050eSAndrew Thompsonstruct usb_config { 382b35f050eSAndrew Thompson bufsize, 383b35f050eSAndrew Thompson callback 384b35f050eSAndrew Thompson direction, 385b35f050eSAndrew Thompson endpoint, 386b35f050eSAndrew Thompson frames, 387b35f050eSAndrew Thompson index flags, 388b35f050eSAndrew Thompson interval, 389b35f050eSAndrew Thompson timeout, 390b35f050eSAndrew Thompson type, 3910a9a8c2eSIan Dowse}; 392b35f050eSAndrew Thompson 3930a9a8c2eSIan Dowse.Ed 394b35f050eSAndrew Thompson. 3950a9a8c2eSIan Dowse.Pp 396b35f050eSAndrew Thompson.Fa type 397b35f050eSAndrew Thompsonfield selects the USB pipe type. 398b35f050eSAndrew Thompson. 399b35f050eSAndrew ThompsonValid values are: UE_INTERRUPT, UE_CONTROL, UE_BULK, 400b35f050eSAndrew ThompsonUE_ISOCHRONOUS. 401b35f050eSAndrew Thompson. 402b35f050eSAndrew ThompsonThe special value UE_BULK_INTR will select BULK and INTERRUPT pipes. 403b35f050eSAndrew Thompson. 404b35f050eSAndrew ThompsonThis field is mandatory. 405b35f050eSAndrew Thompson. 4060a9a8c2eSIan Dowse.Pp 407b35f050eSAndrew Thompson.Fa endpoint 408b35f050eSAndrew Thompsonfield selects the USB endpoint number. 409b35f050eSAndrew Thompson. 410b35f050eSAndrew ThompsonA value of 0xFF, "-1" or "UE_ADDR_ANY" will select the first matching 411b35f050eSAndrew Thompsonendpoint. 412b35f050eSAndrew Thompson. 413b35f050eSAndrew ThompsonThis field is mandatory. 414b35f050eSAndrew Thompson. 4150a9a8c2eSIan Dowse.Pp 416b35f050eSAndrew Thompson.Fa direction 417b35f050eSAndrew Thompsonfield selects the USB endpoint direction. 418b35f050eSAndrew Thompson. 419b35f050eSAndrew ThompsonA value of "UE_DIR_ANY" will select the first matching endpoint. 420b35f050eSAndrew Thompson. 421b35f050eSAndrew ThompsonElse valid values are: "UE_DIR_IN" and "UE_DIR_OUT". 422b35f050eSAndrew Thompson. 423b35f050eSAndrew Thompson"UE_DIR_IN" and "UE_DIR_OUT" can be binary OR'ed by "UE_DIR_SID" which 424b35f050eSAndrew Thompsonmeans that the direction will be swapped in case of 425b35f050eSAndrew ThompsonUSB_MODE_DEVICE. 426b35f050eSAndrew Thompson. 427b35f050eSAndrew ThompsonNote that "UE_DIR_IN" refers to the data transfer direction of the 428b35f050eSAndrew Thompson"IN" tokens and "UE_DIR_OUT" refers to the data transfer direction of 429b35f050eSAndrew Thompsonthe "OUT" tokens. 430b35f050eSAndrew Thompson. 431b35f050eSAndrew ThompsonThis field is mandatory. 432b35f050eSAndrew Thompson. 4330a9a8c2eSIan Dowse.Pp 434b35f050eSAndrew Thompson.Fa interval 435b35f050eSAndrew Thompsonfield selects the interrupt interval. 436b35f050eSAndrew Thompson. 437b35f050eSAndrew ThompsonThe value of this field is given in milliseconds and is independent of 438b35f050eSAndrew Thompsondevice speed. 439b35f050eSAndrew Thompson. 440b35f050eSAndrew ThompsonDepending on the endpoint type, this field has different meaning: 441e5254989SGlen Barber.Bl -tag -width "UE_ISOCHRONOUS" 442b35f050eSAndrew Thompson.It UE_INTERRUPT 443b35f050eSAndrew Thompson"0" use the default interrupt interval based on endpoint descriptor. 444b35f050eSAndrew Thompson"Else" use the given value for polling rate. 445b35f050eSAndrew Thompson.It UE_ISOCHRONOUS 446*7851d429SEdward Tomasz Napierala"0" use default. 447*7851d429SEdward Tomasz Napierala"Else" the value is ignored. 448b35f050eSAndrew Thompson.It UE_BULK 449b35f050eSAndrew Thompson.It UE_CONTROL 450*7851d429SEdward Tomasz Napierala"0" no transfer pre-delay. 451*7851d429SEdward Tomasz Napierala"Else" a delay as given by this field in 452b35f050eSAndrew Thompsonmilliseconds is inserted before the hardware is started when 453b35f050eSAndrew Thompson"usbd_transfer_submit()" is called. 4540a9a8c2eSIan Dowse.Pp 455b35f050eSAndrew ThompsonNOTE: The transfer timeout, if any, is started after that the 456b35f050eSAndrew Thompsonpre-delay has elapsed! 4570a9a8c2eSIan Dowse.El 458b35f050eSAndrew Thompson. 4590a9a8c2eSIan Dowse.Pp 4600a9a8c2eSIan Dowse.Fa timeout 461*7851d429SEdward Tomasz Napieralafield, if non-zero, will set the transfer timeout in milliseconds. 462*7851d429SEdward Tomasz NapieralaIf the "timeout" field is zero and the transfer type is ISOCHRONOUS a 463b35f050eSAndrew Thompsontimeout of 250ms will be used. 464b35f050eSAndrew Thompson. 4650a9a8c2eSIan Dowse.Pp 466b35f050eSAndrew Thompson.Fa frames 467*7851d429SEdward Tomasz Napieralafield sets the maximum number of frames. 468*7851d429SEdward Tomasz NapieralaIf zero is specified it will yield the following results: 469e5254989SGlen Barber.Bl -tag -width "UE_INTERRUPT" 470b35f050eSAndrew Thompson.It UE_BULK 471b35f050eSAndrew Thompsonxfer->nframes = 1; 472b35f050eSAndrew Thompson.It UE_INTERRUPT 473b35f050eSAndrew Thompsonxfer->nframes = 1; 474b35f050eSAndrew Thompson.It UE_CONTROL 475b35f050eSAndrew Thompsonxfer->nframes = 2; 476b35f050eSAndrew Thompson.It UE_ISOCHRONOUS 477*7851d429SEdward Tomasz NapieralaNot allowed. 478*7851d429SEdward Tomasz NapieralaWill cause an error. 4790a9a8c2eSIan Dowse.El 480b35f050eSAndrew Thompson. 4810a9a8c2eSIan Dowse.Pp 482b35f050eSAndrew Thompson.Fa ep_index 483b35f050eSAndrew Thompsonfield allows you to give a number, in case more endpoints match the 484b35f050eSAndrew Thompsondescription, that selects which matching "ep_index" should be used. 485b35f050eSAndrew Thompson. 486b35f050eSAndrew Thompson.Pp 487b35f050eSAndrew Thompson.Fa if_index 488b35f050eSAndrew Thompsonfield allows you to select which of the interface numbers in the 489b35f050eSAndrew Thompson"ifaces" array parameter passed to "usbd_transfer_setup" that should 490b35f050eSAndrew Thompsonbe used when setting up the given USB transfer. 491b35f050eSAndrew Thompson. 492b35f050eSAndrew Thompson.Pp 493b35f050eSAndrew Thompson.Fa flags 494b35f050eSAndrew Thompsonfield has type "struct usb_xfer_flags" and allows one to set initial 495*7851d429SEdward Tomasz Napieralaflags an USB transfer. 496*7851d429SEdward Tomasz NapieralaValid flags are: 497e5254989SGlen Barber.Bl -tag -width "force_short_xfer" 498b35f050eSAndrew Thompson.It force_short_xfer 499*7851d429SEdward Tomasz NapieralaThis flag forces the last transmitted USB packet to be short. 500*7851d429SEdward Tomasz NapieralaA short packet has a length of less than "xfer->max_packet_size", which 501*7851d429SEdward Tomasz Napieraladerives from "wMaxPacketSize". 502*7851d429SEdward Tomasz NapieralaThis flag can be changed during operation. 503b35f050eSAndrew Thompson.It short_xfer_ok 504b35f050eSAndrew ThompsonThis flag allows the received transfer length, "xfer->actlen" to be 505*7851d429SEdward Tomasz Napieralaless than "xfer->sumlen" upon completion of a transfer. 506*7851d429SEdward Tomasz NapieralaThis flag can be changed during operation. 507b35f050eSAndrew Thompson.It short_frames_ok 508*7851d429SEdward Tomasz NapieralaThis flag allows the reception of multiple short USB frames. 509*7851d429SEdward Tomasz NapieralaThis flag 510b35f050eSAndrew Thompsononly has effect for BULK and INTERRUPT endpoints and if the number of 511*7851d429SEdward Tomasz Napieralaframes received is greater than 1. 512*7851d429SEdward Tomasz NapieralaThis flag can be changed during operation. 513b35f050eSAndrew Thompson.It pipe_bof 514b35f050eSAndrew ThompsonThis flag causes a failing USB transfer to remain first in the PIPE 515b35f050eSAndrew Thompsonqueue except in the case of "xfer->error" equal to 516*7851d429SEdward Tomasz Napierala"USB_ERR_CANCELLED". 517*7851d429SEdward Tomasz NapieralaNo other USB transfers in the affected PIPE queue 518b35f050eSAndrew Thompsonwill be started until either: 51961c320f0SXin LI.Bl -tag -width "X" 520b35f050eSAndrew Thompson.It 1 521b35f050eSAndrew ThompsonThe failing USB transfer is stopped using "usbd_transfer_stop()". 522b35f050eSAndrew Thompson.It 2 523b35f050eSAndrew ThompsonThe failing USB transfer performs a successful transfer. 524b35f050eSAndrew Thompson.El 525b35f050eSAndrew ThompsonThe purpose of this flag is to avoid races when multiple transfers are 526b35f050eSAndrew Thompsonqueued for execution on an USB endpoint, and the first executing 527b35f050eSAndrew Thompsontransfer fails leading to the need for clearing of stall for 528b35f050eSAndrew Thompsonexample. 529b35f050eSAndrew Thompson. 530b35f050eSAndrew ThompsonIn this case this flag is used to prevent the following USB transfers 531b35f050eSAndrew Thompsonfrom being executed at the same time the clear-stall command is 532b35f050eSAndrew Thompsonexecuted on the USB control endpoint. 533b35f050eSAndrew Thompson. 534b35f050eSAndrew ThompsonThis flag can be changed during operation. 535b35f050eSAndrew Thompson.Pp 53673bbeaa5SGlen Barber"BOF" is short for "Block On Failure". 537b35f050eSAndrew Thompson.Pp 538b35f050eSAndrew ThompsonNOTE: This flag should be set on all BULK and INTERRUPT USB transfers 539b35f050eSAndrew Thompsonwhich use an endpoint that can be shared between userland and kernel. 540b35f050eSAndrew Thompson. 541b35f050eSAndrew Thompson. 542b35f050eSAndrew Thompson.It proxy_buffer 543b35f050eSAndrew ThompsonSetting this flag will cause that the total buffer size will be 544b35f050eSAndrew Thompsonrounded up to the nearest atomic hardware transfer size. 545b35f050eSAndrew Thompson. 546b35f050eSAndrew ThompsonThe maximum data length of any USB transfer is always stored in the 547b35f050eSAndrew Thompson"xfer->max_data_length". 548b35f050eSAndrew Thompson. 549b35f050eSAndrew ThompsonFor control transfers the USB kernel will allocate additional space 550b35f050eSAndrew Thompsonfor the 8-bytes of SETUP header. 551b35f050eSAndrew Thompson. 552b35f050eSAndrew ThompsonThese 8-bytes are not counted by the "xfer->max_data_length" 553b35f050eSAndrew Thompsonvariable. 554b35f050eSAndrew Thompson. 555b35f050eSAndrew ThompsonThis flag can not be changed during operation. 556b35f050eSAndrew Thompson. 557b35f050eSAndrew Thompson. 558b35f050eSAndrew Thompson.It ext_buffer 559b35f050eSAndrew ThompsonSetting this flag will cause that no data buffer will be 560b35f050eSAndrew Thompsonallocated. 561b35f050eSAndrew Thompson. 562b35f050eSAndrew ThompsonInstead the USB client must supply a data buffer. 563b35f050eSAndrew Thompson. 564b35f050eSAndrew ThompsonThis flag can not be changed during operation. 565b35f050eSAndrew Thompson. 566b35f050eSAndrew Thompson. 567b35f050eSAndrew Thompson.It manual_status 568b35f050eSAndrew ThompsonSetting this flag prevents an USB STATUS stage to be appended to the 569b35f050eSAndrew Thompsonend of the USB control transfer. 570b35f050eSAndrew Thompson. 571b35f050eSAndrew ThompsonIf no control data is transferred this flag must be cleared. 572b35f050eSAndrew Thompson. 573b35f050eSAndrew ThompsonElse an error will be returned to the USB callback. 574b35f050eSAndrew Thompson. 575b35f050eSAndrew ThompsonThis flag is mostly useful for the USB device side. 576b35f050eSAndrew Thompson. 577b35f050eSAndrew ThompsonThis flag can be changed during operation. 578b35f050eSAndrew Thompson. 579b35f050eSAndrew Thompson. 580b35f050eSAndrew Thompson.It no_pipe_ok 581*7851d429SEdward Tomasz NapieralaSetting this flag causes the USB_ERR_NO_PIPE error to be ignored. 582*7851d429SEdward Tomasz NapieralaThis flag can not be changed during operation. 583b35f050eSAndrew Thompson. 584b35f050eSAndrew Thompson. 585b35f050eSAndrew Thompson.It stall_pipe 586e5254989SGlen Barber.Bl -tag -width "Device Side Mode" 587b35f050eSAndrew Thompson.It Device Side Mode 588b35f050eSAndrew ThompsonSetting this flag will cause STALL pids to be sent to the endpoint 589b35f050eSAndrew Thompsonbelonging to this transfer before the transfer is started. 590b35f050eSAndrew Thompson. 591b35f050eSAndrew ThompsonThe transfer is started at the moment the host issues a clear-stall 592b35f050eSAndrew Thompsoncommand on the STALL'ed endpoint. 593b35f050eSAndrew Thompson. 594b35f050eSAndrew ThompsonThis flag can be changed during operation. 595b35f050eSAndrew Thompson.It Host Side Mode 596b35f050eSAndrew ThompsonSetting this flag will cause a clear-stall control request to be 597b35f050eSAndrew Thompsonexecuted on the endpoint before the USB transfer is started. 598b35f050eSAndrew Thompson.El 599b35f050eSAndrew Thompson.Pp 600b35f050eSAndrew ThompsonIf this flag is changed outside the USB callback function you have to 601b35f050eSAndrew Thompsonuse the "usbd_xfer_set_stall()" and "usbd_transfer_clear_stall()" 602b35f050eSAndrew Thompsonfunctions! This flag is automatically cleared after that the stall or 603b35f050eSAndrew Thompsonclear stall has been executed. 604b35f050eSAndrew Thompson. 6051c497368SHans Petter Selasky.It pre_scale_frames 6061c497368SHans Petter SelaskyIf this flag is set the number of frames specified is assumed to give the buffering time in milliseconds instead of frames. 6071c497368SHans Petter SelaskyDuring transfer setup the frames field is pre scaled with the corresponding value for the endpoint and rounded to the nearest number of frames greater than zero. 6081c497368SHans Petter SelaskyThis option only has effect for ISOCHRONOUS transfers. 609b35f050eSAndrew Thompson.El 610b35f050eSAndrew Thompson.Pp 611b35f050eSAndrew Thompson.Fa bufsize 612b35f050eSAndrew Thompsonfield sets the total buffer size in bytes. 613b35f050eSAndrew Thompson. 614b35f050eSAndrew ThompsonIf this field is zero, "wMaxPacketSize" will be used, multiplied by 615b35f050eSAndrew Thompsonthe "frames" field if the transfer type is ISOCHRONOUS. 616b35f050eSAndrew Thompson. 617b35f050eSAndrew ThompsonThis is useful for setting up interrupt pipes. 618b35f050eSAndrew Thompson. 619b35f050eSAndrew ThompsonThis field is mandatory. 620b35f050eSAndrew Thompson.Pp 621b35f050eSAndrew ThompsonNOTE: For control transfers "bufsize" includes the length of the 622b35f050eSAndrew Thompsonrequest structure. 623b35f050eSAndrew Thompson. 624b35f050eSAndrew Thompson.Pp 625b35f050eSAndrew Thompson.Fa callback 626*7851d429SEdward Tomasz Napieralapointer sets the USB callback. 627*7851d429SEdward Tomasz NapieralaThis field is mandatory. 628b35f050eSAndrew Thompson. 629b35f050eSAndrew Thompson. 630b35f050eSAndrew Thompson.Sh USB LINUX COMPAT LAYER 631b35f050eSAndrew ThompsonThe 632b35f050eSAndrew Thompson.Nm usb 633b35f050eSAndrew Thompsonmodule supports the Linux USB API. 634b35f050eSAndrew Thompson. 635b35f050eSAndrew Thompson. 636b35f050eSAndrew Thompson.Sh SEE ALSO 637b35f050eSAndrew Thompson.Xr libusb 3 , 638b35f050eSAndrew Thompson.Xr usb 4 , 639b35f050eSAndrew Thompson.Xr usbconfig 8 640b35f050eSAndrew Thompson.Sh STANDARDS 641b35f050eSAndrew ThompsonThe 642b35f050eSAndrew Thompson.Nm usb 643b35f050eSAndrew Thompsonmodule complies with the USB 2.0 standard. 644b35f050eSAndrew Thompson.Sh HISTORY 645b35f050eSAndrew ThompsonThe 646b35f050eSAndrew Thompson.Nm usb 647b35f050eSAndrew Thompsonmodule has been inspired by the NetBSD USB stack initially written by 648*7851d429SEdward Tomasz NapieralaLennart Augustsson. 649*7851d429SEdward Tomasz NapieralaThe 650b35f050eSAndrew Thompson.Nm usb 651b35f050eSAndrew Thompsonmodule was written by 6528a7314fcSBaptiste Daroussin.An Hans Petter Selasky Aq Mt hselasky@FreeBSD.org . 653