2 .\" Copyright (c) 2005 Ian Dowse <iedowse@FreeBSD.org>
3 .\" All rights reserved.
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
8 .\" 1. Redistributions of source code must retain the above copyright
9 .\" notice, this list of conditions and the following disclaimer.
10 .\" 2. Redistributions in binary form must reproduce the above copyright
11 .\" notice, this list of conditions and the following disclaimer in the
12 .\" documentation and/or other materials provided with the distribution.
14 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 .Nm usb_fifo_alloc_buffer ,
34 .Nm usb_fifo_free_buffer ,
35 .Nm usb_fifo_get_data ,
36 .Nm usb_fifo_get_data_buffer ,
37 .Nm usb_fifo_get_data_error ,
38 .Nm usb_fifo_get_data_linear ,
39 .Nm usb_fifo_put_bytes_max ,
40 .Nm usb_fifo_put_data ,
41 .Nm usb_fifo_put_data_buffer ,
42 .Nm usb_fifo_put_data_error ,
43 .Nm usb_fifo_put_data_linear ,
48 .Nm usbd_do_request_flags ,
50 .Nm usbd_lookup_id_by_info ,
51 .Nm usbd_lookup_id_by_uaa ,
52 .Nm usbd_transfer_clear_stall ,
53 .Nm usbd_transfer_drain ,
54 .Nm usbd_transfer_pending ,
55 .Nm usbd_transfer_poll ,
56 .Nm usbd_transfer_setup ,
57 .Nm usbd_transfer_start ,
58 .Nm usbd_transfer_stop ,
59 .Nm usbd_transfer_submit ,
60 .Nm usbd_transfer_unsetup ,
61 .Nm usbd_xfer_clr_flag ,
62 .Nm usbd_xfer_frame_data ,
63 .Nm usbd_xfer_frame_len ,
64 .Nm usbd_xfer_get_frame ,
65 .Nm usbd_xfer_get_priv ,
66 .Nm usbd_xfer_is_stalled ,
67 .Nm usbd_xfer_max_framelen ,
68 .Nm usbd_xfer_max_frames ,
69 .Nm usbd_xfer_max_len ,
70 .Nm usbd_xfer_set_flag ,
71 .Nm usbd_xfer_set_frame_data ,
72 .Nm usbd_xfer_set_frame_len ,
73 .Nm usbd_xfer_set_frame_offset ,
74 .Nm usbd_xfer_set_frames ,
75 .Nm usbd_xfer_set_interval ,
76 .Nm usbd_xfer_set_priv ,
77 .Nm usbd_xfer_set_stall ,
78 .Nm usbd_xfer_set_timeout ,
82 .Nd Universal Serial Bus driver programming interface
86 .In dev/usb/usbdi_util.h
88 .Fo "usbd_transfer_setup"
89 .Fa "struct usb_device *udev"
90 .Fa "const uint8_t *ifaces"
91 .Fa "struct usb_xfer **pxfer"
92 .Fa "const struct usb_config *setup_start"
93 .Fa "uint16_t n_setup"
95 .Fa "struct mtx *priv_mtx"
98 .Fo "usbd_transfer_unsetup"
99 .Fa "struct usb_xfer **pxfer"
100 .Fa "uint16_t n_setup"
103 .Fo "usbd_transfer_start"
104 .Fa "struct usb_xfer *xfer"
107 .Fo "usbd_transfer_stop"
108 .Fa "struct usb_xfer *xfer"
111 .Fo "usbd_transfer_drain"
112 .Fa "struct usb_xfer *xfer"
115 The Universal Serial Bus (USB) driver programming interface provides
116 USB peripheral drivers with a host controller independent API for
117 controlling and communicating with USB peripherals.
120 module supports both USB Host and USB Device side mode.
121 .Sh USB TRANSFER MANAGEMENT FUNCTIONS
122 The USB standard defines four types of USB transfers.
124 Control transfers, Bulk transfers, Interrupt transfers and Isochronous
127 All the transfer types are managed using the following five functions:
131 .Fn usbd_transfer_setup
132 This function will allocate memory for and initialise an array of USB
133 transfers and all required DMA memory.
135 This function can sleep or block waiting for resources to become
138 is a pointer to "struct usb_device".
140 is an array of interface index numbers to use.
143 is a pointer to an array of USB transfer pointers that are initialized
144 to NULL, and then pointed to allocated USB transfers.
146 is a pointer to an array of USB config structures.
148 is a number telling the USB system how many USB transfers should be
151 is the private softc pointer, which will be used to initialize
154 is the private mutex protecting the transfer structure and the
156 This pointer is used to initialize "xfer->priv_mtx".
157 This function returns zero upon success.
158 A non-zero return value indicates failure.
162 .Fn usbd_transfer_unsetup
163 This function will release the given USB transfers and all allocated
164 resources associated with these USB transfers.
166 is a pointer to an array of USB transfer pointers, that may be NULL,
167 that should be freed by the USB system.
169 is a number telling the USB system how many USB transfers should be
172 This function can sleep waiting for USB transfers to complete.
174 This function is NULL safe with regard to the USB transfer structure
177 It is not allowed to call this function from the USB transfer
182 .Fn usbd_transfer_start
183 This function will start the USB transfer pointed to by
185 if not already started.
187 This function is always non-blocking and must be called with the
188 so-called private USB mutex locked.
190 This function is NULL safe with regard to the USB transfer structure
195 .Fn usbd_transfer_stop
196 This function will stop the USB transfer pointed to by
198 if not already stopped.
200 This function is always non-blocking and must be called with the
201 so-called private USB mutex locked.
203 This function can return before the USB callback has been called.
205 This function is NULL safe with regard to the USB transfer structure
208 If the transfer was in progress, the callback will called with
209 "USB_ST_ERROR" and "error = USB_ERR_CANCELLED".
213 .Fn usbd_transfer_drain
214 This function will stop an USB transfer, if not already stopped and
215 wait for any additional USB hardware operations to complete.
217 Buffers that are loaded into DMA using "usbd_xfer_set_frame_data()" can
218 safely be freed after that this function has returned.
220 This function can block the caller and will not return before the USB
221 callback has been called.
223 This function is NULL safe with regard to the USB transfer structure
226 .Sh USB TRANSFER CALLBACK
228 The USB callback has three states.
230 USB_ST_SETUP, USB_ST_TRANSFERRED and USB_ST_ERROR.
231 USB_ST_SETUP is the initial state.
233 After the callback has been called with this state it will always be
234 called back at a later stage in one of the other two states.
236 The USB callback should not restart the USB transfer in case the error
237 cause is USB_ERR_CANCELLED.
239 The USB callback is protected from recursion.
241 That means one can start and stop whatever transfer from the callback
242 of another transfer one desires.
244 Also the transfer that is currently called back.
246 Recursion is handled like this that when the callback that wants to
247 recurse returns it is called one more time.
252 .Fn usbd_transfer_submit
253 This function should only be called from within the USB callback and
254 is used to start the USB hardware.
256 An USB transfer can have multiple frames consisting of one or more USB
257 packets making up an I/O vector for all USB transfer types.
259 .Bd -literal -offset indent
261 usb_default_callback(struct usb_xfer *xfer, usb_error_t error)
265 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
267 switch (USB_GET_STATE(xfer)) {
270 * Setup xfer frame lengths/count and data
272 usbd_transfer_submit(xfer);
275 case USB_ST_TRANSFERRED:
277 * Read usb frame data, if any.
278 * "actlen" has the total length for all frames
285 * Print error message and clear stall
291 * Here it is safe to do something without the private
298 .Sh USB CONTROL TRANSFERS
299 An USB control transfer has three parts.
301 First the SETUP packet, then DATA packet(s) and then a STATUS
304 The SETUP packet is always pointed to by frame 0 and the
306 .Fn usbd_xfer_frame_len
307 also if there should not be
308 sent any SETUP packet!
309 If an USB control transfer has no DATA stage,
310 then the number of frames should be set to 1.
312 Else the default number of frames is 2.
314 .Bd -literal -offset indent
316 Example1: SETUP + STATUS
317 usbd_xfer_set_frames(xfer, 1);
318 usbd_xfer_set_frame_len(xfer, 0, 8);
319 usbd_transfer_submit(xfer);
321 Example2: SETUP + DATA + STATUS
322 usbd_xfer_set_frames(xfer, 2);
323 usbd_xfer_set_frame_len(xfer, 0, 8);
324 usbd_xfer_set_frame_len(xfer, 1, 1);
325 usbd_transfer_submit(xfer);
327 Example3: SETUP + DATA + STATUS - split
329 usbd_xfer_set_frames(xfer, 1);
330 usbd_xfer_set_frame_len(xfer, 0, 8);
331 usbd_transfer_submit(xfer);
334 /* IMPORTANT: frbuffers[0] must still point at the setup packet! */
335 usbd_xfer_set_frames(xfer, 2);
336 usbd_xfer_set_frame_len(xfer, 0, 0);
337 usbd_xfer_set_frame_len(xfer, 1, 1);
338 usbd_transfer_submit(xfer);
340 Example4: SETUP + STATUS - split
342 usbd_xfer_set_frames(xfer, 1);
343 usbd_xfer_set_frame_len(xfer, 0, 8);
344 usbd_xfer_set_flag(xfer, USB_MANUAL_STATUS);
345 usbd_transfer_submit(xfer);
348 usbd_xfer_set_frames(xfer, 1);
349 usbd_xfer_set_frame_len(xfer, 0, 0);
350 usbd_xfer_clr_flag(xfer, USB_MANUAL_STATUS);
351 usbd_transfer_submit(xfer);
354 .Sh USB TRANSFER CONFIG
355 To simply the search for endpoints the
357 module defines a USB config structure where it is possible to specify
358 the characteristics of the wanted endpoint.
359 .Bd -literal -offset indent
377 field selects the USB pipe type.
379 Valid values are: UE_INTERRUPT, UE_CONTROL, UE_BULK,
382 The special value UE_BULK_INTR will select BULK and INTERRUPT pipes.
384 This field is mandatory.
388 field selects the USB endpoint number.
390 A value of 0xFF, "-1" or "UE_ADDR_ANY" will select the first matching
393 This field is mandatory.
397 field selects the USB endpoint direction.
399 A value of "UE_DIR_ANY" will select the first matching endpoint.
401 Else valid values are: "UE_DIR_IN" and "UE_DIR_OUT".
403 "UE_DIR_IN" and "UE_DIR_OUT" can be binary OR'ed by "UE_DIR_SID" which
404 means that the direction will be swapped in case of
407 Note that "UE_DIR_IN" refers to the data transfer direction of the
408 "IN" tokens and "UE_DIR_OUT" refers to the data transfer direction of
411 This field is mandatory.
415 field selects the interrupt interval.
417 The value of this field is given in milliseconds and is independent of
420 Depending on the endpoint type, this field has different meaning:
421 .Bl -tag -width "UE_ISOCHRONOUS"
423 "0" use the default interrupt interval based on endpoint descriptor.
424 "Else" use the given value for polling rate.
427 "Else" the value is ignored.
430 "0" no transfer pre-delay.
431 "Else" a delay as given by this field in
432 milliseconds is inserted before the hardware is started when
433 "usbd_transfer_submit()" is called.
435 NOTE: The transfer timeout, if any, is started after that the
436 pre-delay has elapsed!
441 field, if non-zero, will set the transfer timeout in milliseconds.
442 If the "timeout" field is zero and the transfer type is ISOCHRONOUS a
443 timeout of 250ms will be used.
447 field sets the maximum number of frames.
448 If zero is specified it will yield the following results:
449 .Bl -tag -width "UE_INTERRUPT"
463 field allows you to give a number, in case more endpoints match the
464 description, that selects which matching "ep_index" should be used.
468 field allows you to select which of the interface numbers in the
469 "ifaces" array parameter passed to "usbd_transfer_setup" that should
470 be used when setting up the given USB transfer.
474 field has type "struct usb_xfer_flags" and allows one to set initial
475 flags an USB transfer.
477 .Bl -tag -width "force_short_xfer"
479 This flag forces the last transmitted USB packet to be short.
480 A short packet has a length of less than "xfer->max_packet_size", which
481 derives from "wMaxPacketSize".
482 This flag can be changed during operation.
484 This flag allows the received transfer length, "xfer->actlen" to be
485 less than "xfer->sumlen" upon completion of a transfer.
486 This flag can be changed during operation.
488 This flag allows the reception of multiple short USB frames.
490 only has effect for BULK and INTERRUPT endpoints and if the number of
491 frames received is greater than 1.
492 This flag can be changed during operation.
494 This flag causes a failing USB transfer to remain first in the PIPE
495 queue except in the case of "xfer->error" equal to
497 No other USB transfers in the affected PIPE queue
498 will be started until either:
501 The failing USB transfer is stopped using "usbd_transfer_stop()".
503 The failing USB transfer performs a successful transfer.
505 The purpose of this flag is to avoid races when multiple transfers are
506 queued for execution on an USB endpoint, and the first executing
507 transfer fails leading to the need for clearing of stall for
510 In this case this flag is used to prevent the following USB transfers
511 from being executed at the same time the clear-stall command is
512 executed on the USB control endpoint.
514 This flag can be changed during operation.
516 "BOF" is short for "Block On Failure".
518 NOTE: This flag should be set on all BULK and INTERRUPT USB transfers
519 which use an endpoint that can be shared between userland and kernel.
523 Setting this flag will cause that the total buffer size will be
524 rounded up to the nearest atomic hardware transfer size.
526 The maximum data length of any USB transfer is always stored in the
527 "xfer->max_data_length".
529 For control transfers the USB kernel will allocate additional space
530 for the 8-bytes of SETUP header.
532 These 8-bytes are not counted by the "xfer->max_data_length"
535 This flag cannot be changed during operation.
539 Setting this flag will cause that no data buffer will be
542 Instead the USB client must supply a data buffer.
544 This flag cannot be changed during operation.
548 Setting this flag prevents an USB STATUS stage to be appended to the
549 end of the USB control transfer.
551 If no control data is transferred this flag must be cleared.
553 Else an error will be returned to the USB callback.
555 This flag is mostly useful for the USB device side.
557 This flag can be changed during operation.
561 Setting this flag causes the USB_ERR_NO_PIPE error to be ignored.
562 This flag cannot be changed during operation.
566 .Bl -tag -width "Device Side Mode"
568 Setting this flag will cause STALL pids to be sent to the endpoint
569 belonging to this transfer before the transfer is started.
571 The transfer is started at the moment the host issues a clear-stall
572 command on the STALL'ed endpoint.
574 This flag can be changed during operation.
576 Setting this flag will cause a clear-stall control request to be
577 executed on the endpoint before the USB transfer is started.
580 If this flag is changed outside the USB callback function you have to
581 use the "usbd_xfer_set_stall()" and "usbd_transfer_clear_stall()"
582 functions! This flag is automatically cleared after that the stall or
583 clear stall has been executed.
586 If this flag is set the number of frames specified is assumed to give the buffering time in milliseconds instead of frames.
587 During 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.
588 This option only has effect for ISOCHRONOUS transfers.
592 field sets the total buffer size in bytes.
594 If this field is zero, "wMaxPacketSize" will be used, multiplied by
595 the "frames" field if the transfer type is ISOCHRONOUS.
597 This is useful for setting up interrupt pipes.
599 This field is mandatory.
601 NOTE: For control transfers "bufsize" includes the length of the
606 pointer sets the USB callback.
607 This field is mandatory.
610 .Sh USB LINUX COMPAT LAYER
613 module supports the Linux USB API.
623 module complies with the USB 2.0 standard.
627 module has been inspired by the NetBSD USB stack initially written by
631 module was written by
632 .An Hans Petter Selasky Aq Mt hselasky@FreeBSD.org .