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
32 .Nm usb_detach_wakeup ,
34 .Nm usbd_abort_default_pipe ,
36 .Nm usbd_alloc_buffer ,
38 .Nm usbd_bulk_transfer ,
39 .Nm usbd_clear_endpoint_stall ,
40 .Nm usbd_clear_endpoint_stall_async ,
41 .Nm usbd_clear_endpoint_toggle ,
43 .Nm usbd_device2interface_handle ,
46 .Nm usbd_do_request_async ,
47 .Nm usbd_do_request_flags ,
48 .Nm usbd_do_request_flags_pipe ,
50 .Nm usbd_endpoint_count ,
52 .Nm usbd_fill_deviceinfo ,
55 .Nm usbd_free_buffer ,
59 .Nm usbd_get_config_desc ,
60 .Nm usbd_get_config_desc_full ,
61 .Nm usbd_get_config_descriptor ,
62 .Nm usbd_get_device_descriptor ,
63 .Nm usbd_get_endpoint_descriptor ,
64 .Nm usbd_get_interface_altindex ,
65 .Nm usbd_get_interface_descriptor ,
66 .Nm usbd_get_no_alts ,
70 .Nm usbd_get_string_desc ,
71 .Nm usbd_get_xfer_status ,
72 .Nm usbd_interface2device_handle ,
73 .Nm usbd_interface2endpoint_descriptor ,
74 .Nm usbd_interface_count ,
75 .Nm usbd_intr_transfer ,
77 .Nm usbd_open_pipe_intr ,
78 .Nm usbd_pipe2device_handle ,
80 .Nm usbd_set_config_index ,
81 .Nm usbd_set_config_no ,
82 .Nm usbd_set_interface ,
83 .Nm usbd_set_polling ,
84 .Nm usbd_setup_default_xfer ,
85 .Nm usbd_setup_isoc_xfer ,
87 .Nm usbd_sync_transfer ,
89 .Nd Universal Serial Bus driver programming interface
93 .In dev/usb/usbdi_util.h
96 .Fn usb_detach_wait "device_ptr_t dv"
98 .Fn usb_detach_wakeup "device_ptr_t dv"
99 .Ft "const usb_descriptor_t *"
100 .Fn usb_find_desc "usbd_device_handle dev" "int type" "int subtype"
102 .Fn usbd_abort_default_pipe "usbd_device_handle dev"
104 .Fn usbd_abort_pipe "usbd_pipe_handle pipe"
106 .Fn usbd_alloc_buffer "usbd_xfer_handle xfer" "u_int32_t size"
108 .Fn usbd_alloc_xfer "usbd_device_handle dev"
110 .Fo usbd_bulk_transfer
111 .Fa "usbd_xfer_handle xfer"
112 .Fa "usbd_pipe_handle pipe"
113 .Fa "u_int16_t flags"
114 .Fa "u_int32_t timeout"
116 .Fa "u_int32_t *size"
120 .Fn usbd_clear_endpoint_stall "usbd_pipe_handle pipe"
122 .Fn usbd_clear_endpoint_stall_async "usbd_pipe_handle"
124 .Fn usbd_clear_endpoint_toggle "usbd_pipe_handle pipe"
126 .Fn usbd_close_pipe "usbd_pipe_handle pipe"
128 .Fo usbd_device2interface_handle
129 .Fa "usbd_device_handle dev"
130 .Fa "u_int8_t ifaceno"
131 .Fa "usbd_interface_handle *iface"
134 .Fn usbd_devinfo "usbd_device_handle dev" "int showclass" "char *cp"
137 .Fa "usbd_device_handle dev"
138 .Fa "usb_device_request_t *req"
142 .Fo usbd_do_request_async
143 .Fa "usbd_device_handle dev"
144 .Fa "usb_device_request_t *req"
148 .Fo usbd_do_request_flags
149 .Fa "usbd_device_handle dev"
150 .Fa "usb_device_request_t *req"
152 .Fa "u_int16_t flags"
157 .Fo usbd_do_request_flags_pipe
158 .Fa "usbd_device_handle dev"
159 .Fa "usbd_pipe_handle pipe"
160 .Fa "usb_device_request_t *req"
162 .Fa "u_int16_t flags"
164 .Fa "u_int32_t timeout"
167 .Fn usbd_dopoll "usbd_interface_handle iface"
169 .Fn usbd_endpoint_count "usbd_interface_handle iface" "u_int8_t *count"
171 .Fn usbd_errstr "usbd_status err"
173 .Fo usbd_fill_deviceinfo
174 .Fa "usbd_device_handle dev"
175 .Fa "struct usb_device_info *di"
178 .Ft "usb_endpoint_descriptor_t *"
180 .Fa "usb_config_descriptor_t *cd"
185 .Ft "usb_interface_descriptor_t *"
186 .Fn usbd_find_idesc "usb_config_descriptor_t *cd" "int ifaceidx" "int altidx"
188 .Fn usbd_free_buffer "usbd_xfer_handle xfer"
190 .Fn usbd_free_xfer "usbd_xfer_handle xfer"
192 .Fn usbd_get_buffer "usbd_xfer_handle xfer"
194 .Fn usbd_get_config "usbd_device_handle dev" "u_int8_t *conf"
196 .Fo usbd_get_config_desc
197 .Fa "usbd_device_handle dev"
199 .Fa "usb_config_descriptor_t *d"
202 .Fo usbd_get_config_desc_full
203 .Fa "usbd_device_handle dev"
208 .Ft "usb_config_descriptor_t *"
209 .Fn usbd_get_config_descriptor "usbd_device_handle dev"
210 .Ft "usb_device_descriptor_t *"
211 .Fn usbd_get_device_descriptor "usbd_device_handle dev"
212 .Ft "usb_endpoint_descriptor_t *"
213 .Fo usbd_get_endpoint_descriptor
214 .Fa "usbd_interface_handle iface"
215 .Fa "u_int8_t address"
218 .Fn usbd_get_interface_altindex "usbd_interface_handle iface"
219 .Ft "usb_interface_descriptor_t *"
220 .Fn usbd_get_interface_descriptor "usbd_interface_handle iface"
222 .Fn usbd_get_no_alts "usb_config_descriptor_t *cdesc" "int ifaceno"
223 .Ft "const struct usbd_quirks *"
224 .Fn usbd_get_quirks "usbd_device_handle dev"
226 .Fn usbd_get_speed "usbd_device_handle dev"
228 .Fn usbd_get_string "usbd_device_handle dev" "int si" "char *buf"
230 .Fo usbd_get_string_desc
231 .Fa "usbd_device_handle dev"
234 .Fa "usb_string_descriptor_t *sdesc"
238 .Fo usbd_get_xfer_status
239 .Fa "usbd_xfer_handle xfer"
240 .Fa "usbd_private_handle *priv"
242 .Fa "u_int32_t *count"
243 .Fa "usbd_status *status"
246 .Fo usbd_interface2device_handle
247 .Fa "usbd_interface_handle iface"
248 .Fa "usbd_device_handle *dev"
250 .Ft "usb_endpoint_descriptor_t *"
251 .Fo usbd_interface2endpoint_descriptor
252 .Fa "usbd_interface_handle iface"
256 .Fn usbd_interface_count "usbd_device_handle dev" "u_int8_t *count"
258 .Fo usbd_intr_transfer
259 .Fa "usbd_xfer_handle xfer"
260 .Fa "usbd_pipe_handle pipe"
261 .Fa "u_int16_t flags"
262 .Fa "u_int32_t timeout"
264 .Fa "u_int32_t *size"
269 .Fa "usbd_interface_handle iface"
270 .Fa "u_int8_t address"
272 .Fa "usbd_pipe_handle *pipe"
275 .Fo usbd_open_pipe_intr
276 .Fa "usbd_interface_handle iface"
277 .Fa "u_int8_t address"
279 .Fa "usbd_pipe_handle *pipe"
280 .Fa "usbd_private_handle priv"
283 .Fa "usbd_callback cb"
286 .Ft usbd_device_handle
287 .Fn usbd_pipe2device_handle "usbd_pipe_handle pipe"
289 .Fn usbd_ratecheck "struct timeval *last"
291 .Fn usbd_set_config_index "usbd_device_handle dev" "int index" "int msg"
293 .Fn usbd_set_config_no "usbd_device_handle dev" "int no" "int msg"
295 .Fn usbd_set_interface "usbd_interface_handle iface" "int altidx"
297 .Fn usbd_set_polling "usbd_device_handle dev" "int on"
299 .Fo usbd_setup_default_xfer
300 .Fa "usbd_xfer_handle xfer"
301 .Fa "usbd_device_handle dev"
302 .Fa "usbd_private_handle priv"
303 .Fa "u_int32_t timeout"
304 .Fa "usb_device_request_t *req"
306 .Fa "u_int32_t length"
307 .Fa "u_int16_t flags"
308 .Fa "usbd_callback callback"
311 .Fo usbd_setup_isoc_xfer
312 .Fa "usbd_xfer_handle xfer"
313 .Fa "usbd_pipe_handle pipe"
314 .Fa "usbd_private_handle priv"
315 .Fa "u_int16_t *frlengths"
316 .Fa "u_int32_t nframes"
317 .Fa "u_int16_t flags"
318 .Fa "usbd_callback callback"
322 .Fa "usbd_xfer_handle xfer"
323 .Fa "usbd_pipe_handle pipe"
324 .Fa "usbd_private_handle priv"
326 .Fa "u_int32_t length"
327 .Fa "u_int16_t flags"
328 .Fa "u_int32_t timeout"
329 .Fa "usbd_callback callback"
332 .Fn usbd_sync_transfer "usbd_xfer_handle xfer"
334 .Fn usbd_transfer "usbd_xfer_handle xfer"
336 The Universal Serial Bus (USB) driver programming interface provides
337 USB peripheral drivers with a host controller independent API for
338 controlling and communicating with USB peripherals.
340 Typically, drivers will first use some combination of the functions
341 .Fn usbd_set_config_no ,
342 .Fn usbd_get_config_descriptor ,
343 .Fn usbd_set_interface ,
344 .Fn usbd_get_interface_descriptor ,
345 .Fn usbd_device2interface_handle ,
346 .Fn usbd_endpoint_count
348 .Fn usbd_interface2endpoint_descriptor
349 to query the device's properties and prepare it for use.
350 Drivers can then perform requests on the USB control pipe using
351 .Fn usbd_do_request ,
352 they can open pipes using the functions
355 .Fn usbd_open_pipe_intr ,
356 and perform transfers over these pipes using
357 .Fn usbd_alloc_xfer ,
361 Finally, the functions
362 .Fn usbd_abort_pipe ,
366 are used to cancel outstanding transfers, close open pipes and deallocate
370 .Fn usbd_get_device_descriptor
371 function returns a pointer to the USB device descriptor for
374 .Sx "USB Descriptors"
375 below for information about the USB device descriptor.
378 .Fn usbd_get_config_desc
379 function retrieves the specified configuration descriptor from the device.
382 parameter specifies the configuration descriptor index, which must be less
384 .Fa bNumConfigurations
385 value in the device descriptor.
387 .Fn usbd_get_config_desc_full
388 retrieves a full configuration descriptor, which has all related interface
389 and endpoint descriptors appended to a normal configuration descriptor.
392 should point to memory that is at least
394 bytes in length, and this should be at least as long as the
396 value from the configuration descriptor.
398 .Sx "USB Descriptors"
399 below for information about the USB configuration descriptor.
403 function retrieves the current configuration number from the device, i.e.\&
405 .Fa bConfigurationValue
406 value from the configuration that is active.
407 If the device is unconfigured then
410 The current configuration can be changed by calling either
411 .Fn usbd_set_config_index
413 .Fn usbd_set_config_no .
414 The difference between these functions is that
415 .Fn usbd_set_config_index
416 accepts a configuration index number that is less than the
417 .Fa bNumConfigurations
418 value from the device descriptor, whereas
419 .Fn usbd_set_config_no
421 .Fa bConfigurationValue
422 value of the desired configuration to be provided instead.
423 To unconfigure the device, supply a configuration index of
424 .Dv USB_UNCONFIG_INDEX
426 .Fn usbd_set_config_index ,
427 or else specify a configuration number of
430 .Fn usbd_set_config_no .
433 .Fn usbd_get_config_descriptor
434 function returns a pointer to an in-memory copy of the full configuration
435 descriptor of the configuration that is currently active.
436 The returned pointer remains valid until the device configuration
438 .Fn usbd_set_config_index
440 .Fn usbd_set_config_no .
441 If the device is unconfigured then
446 .Fn usbd_interface_count
447 returns the number of interfaces available in the current device
451 function determines the number of alternate interfaces in a full
452 configuration descriptor by counting the interface descriptors with
456 (the count includes alternate index zero).
459 function locates an interface descriptor within a full configuration
463 parameter specifies the interface index number, which should be less than
464 the number of interfaces in the configuration descriptor (i.e.\& the value
466 .Fn usbd_interface_count
469 field from the configuration descriptor).
470 An alternate interface can be specified using a non-zero
472 which should be less than the value returned by
473 .Fn usbd_get_no_alts .
474 The return value is a pointer to the requested interface descriptor
475 within the full configuration descriptor, or
477 if the specified interface descriptor does not exist.
480 parameter specifies the alternate setting by index number starting
481 at zero; it is not the alternate setting number defined in the
482 interface descriptor.
486 locates an endpoint descriptor within a full configuration descriptor.
491 parameters are the same as described for
492 .Fn usbd_find_idesc ,
495 parameter is an endpoint index number that should be less than the
497 field in the interface descriptor.
498 The return value is a pointer to the requested endpoint descriptor
499 within the full configuration descriptor, or
501 if the specified endpoint descriptor does not exist.
506 parameters are index numbers starting at zero; they are not the
507 alternate setting and endpoint address defined in the descriptors.
511 function returns the device speed.
518 USB devices optionally support string descriptors, which can be
522 .Fn usbd_get_string_desc
524 Device, configuration and interface descriptors reference strings by
525 an index number that can be supplied to these functions.
528 function should be used unless a non-default language is required.
531 points to a buffer of at least
532 .Dv USB_MAX_STRING_LEN
536 parameter specified which string to retrieve.
540 function searches through the in-memory full configuration descriptor
541 for the active configuration and finds the first descriptor that has a
548 .Dv USBD_SUBTYPE_ANY ,
549 the descriptor must also have a
550 .Fa bDescriptorSubtype
553 If found, then a pointer to the descriptor is returned.
558 The returned pointer is valid until the device configuration is changed using
559 .Fn usbd_set_config_index
561 .Fn usbd_set_config_no .
563 The USB driver interface uses opaque interface handles to refer to
564 configuration interfaces.
565 These handles remain valid until the device configuration is changed using
566 .Fn usbd_set_config_index
568 .Fn usbd_set_config_no .
570 .Fn usbd_device2interface_handle
571 function retrieves an interface handle.
574 parameter is an interface index number starting at zero.
575 If the device is configured and the specified interface exists, then
576 .Dv USBD_NORMAL_COMPLETION
577 is returned and the interface handle is stored in
579 Otherwise an error code is returned and
583 .Fn usbd_interface2device_handle
584 function retrieves the device handle from an interface handle.
585 This is just for convenience to save passing around the device
586 handle as well as the interface handle.
588 .Fn usbd_set_interface
589 function changes the alternate setting number for an interface to
590 the alternate setting identified by the zero-based index number
592 This operation invalidates any existing endpoints on this
593 interface and their descriptors.
595 .Fn usbd_get_interface_altindex
596 function returns the current alternative setting index as was
597 specified when calling
598 .Fn usbd_set_interface .
600 .Fn usbd_endpoint_count
601 function retrieves the number of endpoints associated with the
604 .Fn usbd_interface2endpoint_descriptor
605 function returns a pointer to an in-memory endpoint descriptor for
606 the endpoint that has an index number of
608 This pointer remains valid until the configuration or alternate setting
611 .Fn usbd_get_endpoint_descriptor
613 .Fn usbd_interface2endpoint_descriptor
616 address value instead of an index.
619 .Fn usbd_fill_deviceinfo
622 structure with information about the device.
623 The vendor and product names come from the device itself, falling back to
624 a table lookup or just providing the IDs in hexadecimal.
628 .Fn usbd_fill_deviceinfo
629 will not attempt to retrieve the vendor and product names from the
631 The usb_device_info structure is defined in
635 struct usb_device_info {
637 u_int8_t udi_addr; /* device address */
638 usb_event_cookie_t udi_cookie;
639 char udi_product[USB_MAX_STRING_LEN];
640 char udi_vendor[USB_MAX_STRING_LEN];
642 u_int16_t udi_productNo;
643 u_int16_t udi_vendorNo;
644 u_int16_t udi_releaseNo;
646 u_int8_t udi_subclass;
647 u_int8_t udi_protocol;
650 #define USB_SPEED_LOW 1
651 #define USB_SPEED_FULL 2
652 #define USB_SPEED_HIGH 3
653 int udi_power; /* power consumption in mA */
655 char udi_devnames[USB_MAX_DEVNAMES][USB_MAX_DEVNAMELEN];
656 /* hub only: addresses of devices on ports */
657 u_int8_t udi_ports[16];
658 #define USB_PORT_ENABLED 0xff
659 #define USB_PORT_SUSPENDED 0xfe
660 #define USB_PORT_POWERED 0xfd
666 function generates a string description of the USB device.
669 argument should point to a 1024-byte buffer (XXX the maximum length
670 is approximately 320 chars, but there is no sanity checking and everything uses
671 1024-character buffers).
672 Device class information is included if the
674 parameter is non-zero.
678 function returns information from a table of devices that require
679 special workarounds in order to function correctly.
680 The returned structure is defined in
681 .In dev/usb/usb_quirks.h
685 u_int32_t uq_flags; /* Device problems */
690 .In dev/usb/usb_quirks.h
691 for a list of all currently defined quirks.
693 USB control requests are performed via
694 .Vt usb_device_request_t
695 structures, defined in
705 } UPACKED usb_device_request_t;
710 function performs a single request synchronously.
713 parameter should point to a properly initialized
714 .Vt usb_device_request_t ,
719 should point at a buffer that is at least
722 The request timeout is set to 5 seconds, so the operation will fail
725 if the device does not respond within that time.
727 .Fn usbd_do_request_async
729 .Fn usbd_do_request ,
730 but it does not wait for the request to complete before returning.
731 This routine does not block so it can be used from contexts where
732 sleeping is not allowed.
733 Note that there is no notification mechanism to report when the
734 operation completed nor is there a way to determine whether the
735 request succeeded, so this function is of limited use.
737 .Fn usbd_setup_default_xfer
740 for a way to invoke an asynchronous callback upon completion of
743 .Fn usbd_do_request_flags
745 .Fn usbd_do_request ,
746 but additional flags can be specified, the timeout is configurable,
747 and the actual number of bytes transferred is made available to the
750 .Fn usbd_do_request_flags_pipe
751 function uses a specified pipe instead of the default pipe.
755 creates a pipe connected to a specified endpoint on a specified interface.
760 value from one of this interface's endpoint descriptors.
764 .Dv USBD_EXCLUSIVE_USE
765 then the operation will only succeed if there are no open pipes
766 already connected to the specified endpoint.
768 .Fn usbd_open_pipe_intr
769 function creates an interrupt pipe connected to the specified endpoint.
774 value from one of this interface's endpoint descriptors.
777 parameter is passed to
778 .Fn usbd_setup_xfer .
783 parameters define a buffer that is to be used for the interrupt transfers.
784 The callback to be invoked each time a transfer completes is specified by
788 is an argument to be passed to the callback function.
791 parameter specifies the maximum acceptable interval between transfers;
792 in practice the transfers may occur more frequently.
794 .Fn usbd_pipe2device_handle
795 returns the device associated with the specified
800 function aborts all active or waiting transfers on the specified pipe.
801 Each transfer is aborted with a
803 status; callback routines must detect this error code to ensure that
804 they do not attempt to initiate a new transfer in response to one being
806 This routine blocks while it is waiting for the hardware to complete
807 processing of aborted transfers, so it is only safe to call it in
808 contexts where sleeping is allowed.
810 .Fn usbd_abort_default_pipe
811 aborts all active or waiting transfers on the default pipe.
813 .Fn usbd_abort_pipe ,
814 it blocks waiting for the hardware processing to complete.
816 When a pipe has no active or waiting transfers, the pipe may be closed
820 Once a pipe is closed, its pipe handle becomes invalid and may no longer
823 USB transfer handles are allocated using the function
825 and may be freed using
830 initializes a transfer handle with the details of a transfer to or from
834 parameter specifies the transfer handle to initialize,
836 specifies the pipe on which the transfer is to take place, and
838 is an argument that will be passed to callback function.
843 define the data buffer for the transfer.
852 parameter may contain the following flags:
853 .Bl -tag -width ".Dv USBD_FORCE_SHORT_XFER"
855 This is used in association with
856 .Fn usbd_alloc_buffer
859 to use a dedicated DMA-capable buffer for the transfer.
860 .It Dv USBD_SYNCHRONOUS
861 Wait for the transfer to compete in
863 .It Dv USBD_SHORT_XFER_OK
864 Permit transfers shorter than the requested data length.
865 .It Dv USBD_FORCE_SHORT_XFER
866 Force a short transfer at the end of a write operation to let the
867 device know that the transfer has ended.
872 parameter specifies a timeout for the transfer in milliseconds.
875 indicates that no timeout should be configured.
878 specifies the function to call when the transfer completes.
881 does not actually initiate the transfer.
883 .Fn usbd_setup_default_xfer
884 initializes a control transfer for the default pipe.
887 parameter should point at a completed
888 .Vt usb_device_request_t
891 .Fa usbd_setup_isoc_xfer
892 initializes a transfer for an isochronous pipe.
896 initiates a transfer.
899 to indicate that the transfer has been queued.
900 If the USB stack is operating in polling mode, or if the transfer
902 .Dv USBD_NORMAL_COMPLETION
904 Other return values indicate that the transfer could not be
905 initiated due to an error.
907 .Fn usbd_sync_transfer
908 function executes a transfer synchronously.
909 It will sleep waiting for the transfer to complete and then return
911 Note that if the transfer has a callback routine, this will be
913 .Fn usbd_sync_transfer
917 .Fn usbd_intr_transfer
919 .Fn usbd_bulk_transfer
920 functions set up a transfer and wait synchronously for it to complete
921 but they allows signals to interrupt the wait.
924 if the transfer was interrupted by a signal.
925 XXX these two functions are identical apart from their names.
928 .Fn usbd_get_xfer_status
929 retrieves various information from a completed transfer.
932 parameter is not NULL then the callback private argument is
937 is not NULL then the transfer buffer pointer is stored in
939 The actual number of bytes transferred is stored in
942 .Fa count is not NULL.
943 Finally, the transfer status is stored in
950 .Fn usbd_clear_endpoint_stall
951 function clears an endpoint stall condition synchronously, i.e.\&
952 it sleeps waiting for the stall clear request to complete.
954 .Fn usbd_clear_endpoint_stall_async
955 performs the same function asynchronously, but it provides no
956 way to determine when the request completed, or whether it was
959 .Fn usbd_clear_endpoint_toggle
960 function instructs the host controller driver to reset the toggle bit
962 This is used when manually clearing an endpoint stall using a
963 control pipe request, in order to ensure that the host controller
964 driver and the USB device restart with the same toggle value.
966 Normally the USB subsystem maps and copies data to and from
967 DMA-capable memory each time a transfer is performed.
969 .Fn usbd_alloc_buffer
970 allocates a permanent DMA-capable buffer associated with the
971 transfer to avoid this overhead.
972 The return value is the virtual address of the buffer.
975 is called on the transfer with the
977 flag enabled, the allocated buffer will be used directly and
985 function returns a pointer to the virtual address of a buffer previously
987 .Fn usbd_alloc_buffer .
990 deallocates the buffer.
994 function converts a status code into a string for display.
998 enables or disables polling mode.
999 In polling mode, all operations will busy-wait for the device to
1000 respond, so its use is effectively limited to boot time and kernel
1002 It is important to match up calls that enable and disable polling
1003 mode, because the implementation just increments a polling reference
1006 is non-zero and decrements it when
1011 causes the host controller driver to poll for any activity.
1012 This should only be used when polling mode is enabled.
1016 function is used to limit the rate at which error messages are
1017 printed to approximately once per second.
1020 argument should point at a persistent
1021 .Vt "struct timeval" .
1022 A value of 1 will be returned if a message should be printed, but if
1024 has already been called with the same
1025 .Vt "struct timeval"
1026 parameter in the last second then 0 is returned and the error message
1027 should be suppressed.
1032 .Fn usb_detach_wakeup
1033 are used to wait for references to drain before completing the
1034 detachment of a device.
1037 function will wait up to 60 seconds to receive a signal from
1038 .Fn usb_detach_wait .
1039 .Ss "USB Descriptors"
1040 The USB specification defines a number of standard descriptors by
1041 which USB devices report their attributes.
1042 These descriptors are fixed-format structures that all USB devices
1043 make available through USB control pipe requests.
1045 Every USB device has exactly one USB device descriptor.
1046 The USB subsystem retrieves this automatically when a device is
1047 attached, and a copy of the descriptor is kept in memory.
1049 .Fn usbd_get_device_descriptor
1050 function returns a pointer to the descriptor.
1051 The device descriptor structure is defined in
1057 uByte bDescriptorType;
1059 #define UD_USB_2_0 0x0200
1060 #define UD_IS_USB2(d) (UGETW((d)->bcdUSB) >= UD_USB_2_0)
1062 uByte bDeviceSubClass;
1063 uByte bDeviceProtocol;
1064 uByte bMaxPacketSize;
1065 /* The fields below are not part of the initial descriptor. */
1069 uByte iManufacturer;
1071 uByte iSerialNumber;
1072 uByte bNumConfigurations;
1073 } UPACKED usb_device_descriptor_t;
1074 #define USB_DEVICE_DESCRIPTOR_SIZE 18
1077 USB devices have at least one configuration descriptor.
1079 .Fa bNumConfigurations
1080 field of the device descriptor specifies the number of configuration
1081 descriptors that a device supports.
1083 .Fn usbd_get_config_desc
1084 function retrieves a particular configuration descriptor from the device
1086 .Fn usbd_get_config_desc_full
1087 function retrieves a full
1089 length configuration descriptor, which includes all related interface
1090 and endpoint descriptors.
1091 Only one configuration may be active at a time.
1093 .Fn usbd_set_config_index
1094 function activates a specified configuration.
1095 The configuration descriptor structure is defined in
1101 uByte bDescriptorType;
1103 uByte bNumInterface;
1104 uByte bConfigurationValue;
1105 uByte iConfiguration;
1107 #define UC_BUS_POWERED 0x80
1108 #define UC_SELF_POWERED 0x40
1109 #define UC_REMOTE_WAKEUP 0x20
1110 uByte bMaxPower; /* max current in 2 mA units */
1111 #define UC_POWER_FACTOR 2
1112 } UPACKED usb_config_descriptor_t;
1113 #define USB_CONFIG_DESCRIPTOR_SIZE 9
1116 Each device configuration provides one or more interfaces.
1119 field of the configuration descriptor specifies the number of
1120 interfaces associated with a device configuration.
1121 Interfaces are described by an interface descriptor, which is defined in
1127 uByte bDescriptorType;
1128 uByte bInterfaceNumber;
1129 uByte bAlternateSetting;
1130 uByte bNumEndpoints;
1131 uByte bInterfaceClass;
1132 uByte bInterfaceSubClass;
1133 uByte bInterfaceProtocol;
1135 } UPACKED usb_interface_descriptor_t;
1136 #define USB_INTERFACE_DESCRIPTOR_SIZE 9
1139 Configurations may also have alternate interfaces with the same
1140 .Fa bInterfaceNumber
1142 .Fa bAlternateSetting
1144 These alternate interface settings may be selected by passing a
1148 .Fn usbd_set_interface .
1150 Interfaces have zero or more endpoints, and each endpoint has an
1151 endpoint descriptor.
1152 Note that endpoint zero, which is always present, does not have an
1153 endpoint descriptor, and it is never included in the
1156 The endpoint descriptor is defined in
1162 uByte bDescriptorType;
1163 uByte bEndpointAddress;
1164 #define UE_GET_DIR(a) ((a) & 0x80)
1165 #define UE_SET_DIR(a,d) ((a) | (((d)&1) << 7))
1166 #define UE_DIR_IN 0x80
1167 #define UE_DIR_OUT 0x00
1168 #define UE_ADDR 0x0f
1169 #define UE_GET_ADDR(a) ((a) & UE_ADDR)
1171 #define UE_XFERTYPE 0x03
1172 #define UE_CONTROL 0x00
1173 #define UE_ISOCHRONOUS 0x01
1174 #define UE_BULK 0x02
1175 #define UE_INTERRUPT 0x03
1176 #define UE_GET_XFERTYPE(a) ((a) & UE_XFERTYPE)
1177 #define UE_ISO_TYPE 0x0c
1178 #define UE_ISO_ASYNC 0x04
1179 #define UE_ISO_ADAPT 0x08
1180 #define UE_ISO_SYNC 0x0c
1181 #define UE_GET_ISO_TYPE(a) ((a) & UE_ISO_TYPE)
1182 uWord wMaxPacketSize;
1184 } UPACKED usb_endpoint_descriptor_t;
1185 #define USB_ENDPOINT_DESCRIPTOR_SIZE 7
1188 Many functions return a
1190 type to indicate the outcome of the operation.
1191 If the operation completed successfully then
1192 .Dv USBD_NORMAL_COMPLETION
1194 Operations that have been started but not yet completed will return
1195 .Dv USBD_IN_PROGRESS .
1196 Other errors usually indicate a problem.
1197 Error codes can be converted to strings using
1200 .Bl -tag -width ".Er USBD_PENDING_REQUESTS"
1201 .It Bq Er USBD_PENDING_REQUESTS
1202 A pipe could not be closed because there are active requests.
1203 .It Bq Er USBD_NOT_STARTED
1204 The transfer has not yet been started.
1205 .It Bq Er USBD_INVAL
1206 An invalid value was supplied.
1207 .It Bq Er USBD_NOMEM
1208 An attempt to allocate memory failed.
1209 .It Bq Er USBD_CANCELLED
1210 The transfer was aborted.
1211 .It Bq Er USBD_BAD_ADDRESS
1212 The specified endpoint address was not found.
1213 .It Bq Er USBD_IN_USE
1214 The endpoint is already in use, or the configuration cannot be changed
1215 because some of its endpoints are in use.
1216 .It Bq Er USBD_NO_ADDR
1217 No free USB devices addresses were found to assign to the device.
1218 .It Bq Er USBD_SET_ADDR_FAILED
1219 The device address could not be set.
1220 .It Bq Er USBD_NO_POWER
1221 Insufficient power was available for the device.
1222 .It Bq Er USBD_TOO_DEEP
1223 Too many levels of chained hubs were found.
1224 .It Bq Er USBD_IOERROR
1225 There was an error communicating with the device.
1226 .It Bq Er USBD_NOT_CONFIGURED
1227 An operation that requires an active configuration was attempted while
1228 the device was in an unconfigured state.
1229 .It Bq Er USBD_TIMEOUT
1230 A transfer timed out.
1231 .It Bq Er USBD_SHORT_XFER
1232 A transfer that disallowed short data lengths completed with less than
1233 the requested length transferred.
1234 .It Bq Er USBD_STALLED
1235 A transfer failed because the pipe is stalled.
1236 .It Bq Er USBD_INTERRUPTED
1237 An interruptible operation caught a signal.
1242 The USB driver interface first appeared in
1245 The USB driver was written by
1246 .An Lennart Augustsson
1252 This manual page was written by
1253 .An Ian Dowse Aq iedowse@FreeBSD.org .