98d86de28d09d7bf281f4d34d7866aa05e99e5c2
[dragonfly.git] / share / man / man9 / usbdi.9
1 .\"
2 .\" Copyright (c) 2005 Ian Dowse <iedowse@FreeBSD.org>
3 .\" All rights reserved.
4 .\"
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
7 .\" are met:
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.
13 .\"
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
24 .\" SUCH DAMAGE.
25 .\"
26 .\" $FreeBSD: src/share/man/man9/usbdi.9,v 1.2 2006/12/14 14:33:13 mpp Exp $
27 .\" $DragonFly: src/share/man/man9/usbdi.9,v 1.2 2008/02/09 09:32:05 swildner Exp $
28 .Dd December 30, 2005
29 .Os
30 .Dt USBDI 9
31 .Sh NAME
32 .Nm usb_detach_wait ,
33 .Nm usb_detach_wakeup ,
34 .Nm usb_find_desc ,
35 .Nm usbd_abort_default_pipe ,
36 .Nm usbd_abort_pipe ,
37 .Nm usbd_alloc_buffer ,
38 .Nm usbd_alloc_xfer ,
39 .Nm usbd_bulk_transfer ,
40 .Nm usbd_clear_endpoint_stall ,
41 .Nm usbd_clear_endpoint_stall_async ,
42 .Nm usbd_clear_endpoint_toggle ,
43 .Nm usbd_close_pipe ,
44 .Nm usbd_device2interface_handle ,
45 .Nm usbd_devinfo ,
46 .Nm usbd_do_request ,
47 .Nm usbd_do_request_async ,
48 .Nm usbd_do_request_flags ,
49 .Nm usbd_do_request_flags_pipe ,
50 .Nm usbd_dopoll ,
51 .Nm usbd_endpoint_count ,
52 .Nm usbd_errstr ,
53 .Nm usbd_fill_deviceinfo ,
54 .Nm usbd_find_edesc ,
55 .Nm usbd_find_idesc ,
56 .Nm usbd_free_buffer ,
57 .Nm usbd_free_xfer ,
58 .Nm usbd_get_buffer ,
59 .Nm usbd_get_config ,
60 .Nm usbd_get_config_desc ,
61 .Nm usbd_get_config_desc_full ,
62 .Nm usbd_get_config_descriptor ,
63 .Nm usbd_get_device_descriptor ,
64 .Nm usbd_get_endpoint_descriptor ,
65 .Nm usbd_get_interface_altindex ,
66 .Nm usbd_get_interface_descriptor ,
67 .Nm usbd_get_no_alts ,
68 .Nm usbd_get_quirks ,
69 .Nm usbd_get_speed ,
70 .Nm usbd_get_string ,
71 .Nm usbd_get_string_desc ,
72 .Nm usbd_get_xfer_status ,
73 .Nm usbd_interface2device_handle ,
74 .Nm usbd_interface2endpoint_descriptor ,
75 .Nm usbd_interface_count ,
76 .Nm usbd_intr_transfer ,
77 .Nm usbd_open_pipe ,
78 .Nm usbd_open_pipe_intr ,
79 .Nm usbd_pipe2device_handle ,
80 .Nm usbd_ratecheck ,
81 .Nm usbd_set_config_index ,
82 .Nm usbd_set_config_no ,
83 .Nm usbd_set_interface ,
84 .Nm usbd_set_polling ,
85 .Nm usbd_setup_default_xfer ,
86 .Nm usbd_setup_isoc_xfer ,
87 .Nm usbd_setup_xfer ,
88 .Nm usbd_sync_transfer ,
89 .Nm usbd_transfer
90 .Nd Universal Serial Bus driver programming interface
91 .Sh SYNOPSIS
92 .In bus/usb/usb.h
93 .In bus/usb/usbdi.h
94 .In bus/usb/usbdi_util.h
95 .Pp
96 .Ft void
97 .Fn usb_detach_wait "device_t dv"
98 .Ft void
99 .Fn usb_detach_wakeup "device_t dv"
100 .Ft "const usb_descriptor_t *"
101 .Fn usb_find_desc "usbd_device_handle dev" "int type" "int subtype"
102 .Ft usbd_status
103 .Fn usbd_abort_default_pipe "usbd_device_handle dev"
104 .Ft usbd_status
105 .Fn usbd_abort_pipe "usbd_pipe_handle pipe"
106 .Ft "void *"
107 .Fn usbd_alloc_buffer "usbd_xfer_handle xfer" "u_int32_t size"
108 .Ft usbd_xfer_handle
109 .Fn usbd_alloc_xfer "usbd_device_handle dev"
110 .Ft usbd_status
111 .Fo usbd_bulk_transfer
112 .Fa "usbd_xfer_handle xfer"
113 .Fa "usbd_pipe_handle pipe"
114 .Fa "u_int16_t flags"
115 .Fa "u_int32_t timeout"
116 .Fa "void *buf"
117 .Fa "u_int32_t *size"
118 .Fa "char *lbl"
119 .Fc
120 .Ft usbd_status
121 .Fn usbd_clear_endpoint_stall "usbd_pipe_handle pipe"
122 .Ft usbd_status
123 .Fn usbd_clear_endpoint_stall_async "usbd_pipe_handle pipe"
124 .Ft void
125 .Fn usbd_clear_endpoint_toggle "usbd_pipe_handle pipe"
126 .Ft usbd_status
127 .Fn usbd_close_pipe "usbd_pipe_handle pipe"
128 .Ft usbd_status
129 .Fo usbd_device2interface_handle
130 .Fa "usbd_device_handle dev"
131 .Fa "u_int8_t ifaceno"
132 .Fa "usbd_interface_handle *iface"
133 .Fc
134 .Ft void
135 .Fn usbd_devinfo "usbd_device_handle dev" "int showclass" "char *cp"
136 .Ft usbd_status
137 .Fo usbd_do_request
138 .Fa "usbd_device_handle dev"
139 .Fa "usb_device_request_t *req"
140 .Fa "void *data"
141 .Fc
142 .Ft usbd_status
143 .Fo usbd_do_request_async
144 .Fa "usbd_device_handle dev"
145 .Fa "usb_device_request_t *req"
146 .Fa "void *data"
147 .Fc
148 .Ft usbd_status
149 .Fo usbd_do_request_flags
150 .Fa "usbd_device_handle dev"
151 .Fa "usb_device_request_t *req"
152 .Fa "void *data"
153 .Fa "u_int16_t flags"
154 .Fa "int *actlen"
155 .Fa "u_int32_t timo"
156 .Fc
157 .Ft usbd_status
158 .Fo usbd_do_request_flags_pipe
159 .Fa "usbd_device_handle dev"
160 .Fa "usbd_pipe_handle pipe"
161 .Fa "usb_device_request_t *req"
162 .Fa "void *data"
163 .Fa "u_int16_t flags"
164 .Fa "int *actlen"
165 .Fa "u_int32_t timeout"
166 .Fc
167 .Ft void
168 .Fn usbd_dopoll "usbd_interface_handle iface"
169 .Ft usbd_status
170 .Fn usbd_endpoint_count "usbd_interface_handle iface" "u_int8_t *count"
171 .Ft "const char *"
172 .Fn usbd_errstr "usbd_status err"
173 .Ft void
174 .Fo usbd_fill_deviceinfo
175 .Fa "usbd_device_handle dev"
176 .Fa "struct usb_device_info *di"
177 .Fa "int usedev"
178 .Fc
179 .Ft "usb_endpoint_descriptor_t *"
180 .Fo usbd_find_edesc
181 .Fa "usb_config_descriptor_t *cd"
182 .Fa "int ifaceidx"
183 .Fa "int altidx"
184 .Fa "int endptidx"
185 .Fc
186 .Ft "usb_interface_descriptor_t *"
187 .Fn usbd_find_idesc "usb_config_descriptor_t *cd" "int ifaceidx" "int altidx"
188 .Ft void
189 .Fn usbd_free_buffer "usbd_xfer_handle xfer"
190 .Ft usbd_status
191 .Fn usbd_free_xfer "usbd_xfer_handle xfer"
192 .Ft "void *"
193 .Fn usbd_get_buffer "usbd_xfer_handle xfer"
194 .Ft usbd_status
195 .Fn usbd_get_config "usbd_device_handle dev" "u_int8_t *conf"
196 .Ft usbd_status
197 .Fo usbd_get_config_desc
198 .Fa "usbd_device_handle dev"
199 .Fa "int confidx"
200 .Fa "usb_config_descriptor_t *d"
201 .Fc
202 .Ft usbd_status
203 .Fo usbd_get_config_desc_full
204 .Fa "usbd_device_handle dev"
205 .Fa "int conf"
206 .Fa "void *d"
207 .Fa "int size"
208 .Fc
209 .Ft "usb_config_descriptor_t *"
210 .Fn usbd_get_config_descriptor "usbd_device_handle dev"
211 .Ft "usb_device_descriptor_t *"
212 .Fn usbd_get_device_descriptor "usbd_device_handle dev"
213 .Ft "usb_endpoint_descriptor_t *"
214 .Fo usbd_get_endpoint_descriptor
215 .Fa "usbd_interface_handle iface"
216 .Fa "u_int8_t address"
217 .Fc
218 .Ft int
219 .Fn usbd_get_interface_altindex "usbd_interface_handle iface"
220 .Ft "usb_interface_descriptor_t *"
221 .Fn usbd_get_interface_descriptor "usbd_interface_handle iface"
222 .Ft int
223 .Fn usbd_get_no_alts "usb_config_descriptor_t *cdesc" "int ifaceno"
224 .Ft "const struct usbd_quirks *"
225 .Fn usbd_get_quirks "usbd_device_handle dev"
226 .Ft int
227 .Fn usbd_get_speed "usbd_device_handle dev"
228 .Ft usbd_status
229 .Fn usbd_get_string "usbd_device_handle dev" "int si" "char *buf"
230 .Ft usbd_status
231 .Fo usbd_get_string_desc
232 .Fa "usbd_device_handle dev"
233 .Fa "int sindex"
234 .Fa "int langid"
235 .Fa "usb_string_descriptor_t *sdesc"
236 .Fa "int *sizep"
237 .Fc
238 .Ft void
239 .Fo usbd_get_xfer_status
240 .Fa "usbd_xfer_handle xfer"
241 .Fa "usbd_private_handle *priv"
242 .Fa "void **buffer"
243 .Fa "u_int32_t *count"
244 .Fa "usbd_status *status"
245 .Fc
246 .Ft void
247 .Fo usbd_interface2device_handle
248 .Fa "usbd_interface_handle iface"
249 .Fa "usbd_device_handle *dev"
250 .Fc
251 .Ft "usb_endpoint_descriptor_t *"
252 .Fo usbd_interface2endpoint_descriptor
253 .Fa "usbd_interface_handle iface"
254 .Fa "u_int8_t index"
255 .Fc
256 .Ft usbd_status
257 .Fn usbd_interface_count "usbd_device_handle dev" "u_int8_t *count"
258 .Ft usbd_status
259 .Fo usbd_intr_transfer
260 .Fa "usbd_xfer_handle xfer"
261 .Fa "usbd_pipe_handle pipe"
262 .Fa "u_int16_t flags"
263 .Fa "u_int32_t timeout"
264 .Fa "void *buf"
265 .Fa "u_int32_t *size"
266 .Fa "char *lbl"
267 .Fc
268 .Ft usbd_status
269 .Fo usbd_open_pipe
270 .Fa "usbd_interface_handle iface"
271 .Fa "u_int8_t address"
272 .Fa "u_int8_t flags"
273 .Fa "usbd_pipe_handle *pipe"
274 .Fc
275 .Ft usbd_status
276 .Fo usbd_open_pipe_intr
277 .Fa "usbd_interface_handle iface"
278 .Fa "u_int8_t address"
279 .Fa "u_int8_t flags"
280 .Fa "usbd_pipe_handle *pipe"
281 .Fa "usbd_private_handle priv"
282 .Fa "void *buffer"
283 .Fa "u_int32_t len"
284 .Fa "usbd_callback cb"
285 .Fa "int ival"
286 .Fc
287 .Ft usbd_device_handle
288 .Fn usbd_pipe2device_handle "usbd_pipe_handle pipe"
289 .Ft int
290 .Fn usbd_ratecheck "struct timeval *last"
291 .Ft usbd_status
292 .Fn usbd_set_config_index "usbd_device_handle dev" "int index" "int msg"
293 .Ft usbd_status
294 .Fn usbd_set_config_no "usbd_device_handle dev" "int no" "int msg"
295 .Ft usbd_status
296 .Fn usbd_set_interface "usbd_interface_handle iface" "int altidx"
297 .Ft void
298 .Fn usbd_set_polling "usbd_device_handle dev" "int on"
299 .Ft void
300 .Fo usbd_setup_default_xfer
301 .Fa "usbd_xfer_handle xfer"
302 .Fa "usbd_device_handle dev"
303 .Fa "usbd_private_handle priv"
304 .Fa "u_int32_t timeout"
305 .Fa "usb_device_request_t *req"
306 .Fa "void *buffer"
307 .Fa "u_int32_t length"
308 .Fa "u_int16_t flags"
309 .Fa "usbd_callback callback"
310 .Fc
311 .Ft void
312 .Fo usbd_setup_isoc_xfer
313 .Fa "usbd_xfer_handle xfer"
314 .Fa "usbd_pipe_handle pipe"
315 .Fa "usbd_private_handle priv"
316 .Fa "u_int16_t *frlengths"
317 .Fa "u_int32_t nframes"
318 .Fa "u_int16_t flags"
319 .Fa "usbd_callback callback"
320 .Fc
321 .Ft void
322 .Fo usbd_setup_xfer
323 .Fa "usbd_xfer_handle xfer"
324 .Fa "usbd_pipe_handle pipe"
325 .Fa "usbd_private_handle priv"
326 .Fa "void *buffer"
327 .Fa "u_int32_t length"
328 .Fa "u_int16_t flags"
329 .Fa "u_int32_t timeout"
330 .Fa "usbd_callback callback"
331 .Fc
332 .Ft usbd_status
333 .Fn usbd_sync_transfer "usbd_xfer_handle xfer"
334 .Ft usbd_status
335 .Fn usbd_transfer "usbd_xfer_handle xfer"
336 .Sh DESCRIPTION
337 The Universal Serial Bus (USB) driver programming interface provides
338 USB peripheral drivers with a host controller independent API for
339 controlling and communicating with USB peripherals.
340 .Pp
341 Typically, drivers will first use some combination of the functions
342 .Fn usbd_set_config_no ,
343 .Fn usbd_get_config_descriptor ,
344 .Fn usbd_set_interface ,
345 .Fn usbd_get_interface_descriptor ,
346 .Fn usbd_device2interface_handle ,
347 .Fn usbd_endpoint_count
348 and
349 .Fn usbd_interface2endpoint_descriptor
350 to query the device's properties and prepare it for use.
351 Drivers can then perform requests on the USB control pipe using
352 .Fn usbd_do_request ,
353 they can open pipes using the functions
354 .Fn usbd_open_pipe
355 and
356 .Fn usbd_open_pipe_intr ,
357 and perform transfers over these pipes using
358 .Fn usbd_alloc_xfer ,
359 .Fn usbd_setup_xfer
360 and
361 .Fn usbd_transfer .
362 Finally, the functions
363 .Fn usbd_abort_pipe ,
364 .Fn usbd_close_pipe
365 and
366 .Fn usbd_free_xfer
367 are used to cancel outstanding transfers, close open pipes and deallocate
368 transfer structures.
369 .Pp
370 The
371 .Fn usbd_get_device_descriptor
372 function returns a pointer to the USB device descriptor for
373 .Fa dev .
374 See
375 .Sx "USB Descriptors"
376 below for information about the USB device descriptor.
377 .Pp
378 The
379 .Fn usbd_get_config_desc
380 function retrieves the specified configuration descriptor from the device.
381 The
382 .Fa confidx
383 parameter specifies the configuration descriptor index, which must be less
384 than the
385 .Fa bNumConfigurations
386 value in the device descriptor.
387 The function
388 .Fn usbd_get_config_desc_full
389 retrieves a full configuration descriptor, which has all related interface
390 and endpoint descriptors appended to a normal configuration descriptor.
391 The parameter
392 .Fa d
393 should point to memory that is at least
394 .Fa size
395 bytes in length, and this should be at least as long as the
396 .Fa wTotalLength
397 value from the configuration descriptor.
398 See
399 .Sx "USB Descriptors"
400 below for information about the USB configuration descriptor.
401 .Pp
402 The
403 .Fn usbd_get_config
404 function retrieves the current configuration number from the device, i.e.\&
405 the
406 .Fa bConfigurationValue
407 value from the configuration that is active.
408 If the device is unconfigured then
409 .Dv USB_UNCONFIG_NO
410 is returned.
411 The current configuration can be changed by calling either
412 .Fn usbd_set_config_index
413 or
414 .Fn usbd_set_config_no .
415 The difference between these functions is that
416 .Fn usbd_set_config_index
417 accepts a configuration index number that is less than the
418 .Fa bNumConfigurations
419 value from the device descriptor, whereas
420 .Fn usbd_set_config_no
421 requires the
422 .Fa bConfigurationValue
423 value of the desired configuration to be provided instead.
424 To unconfigure the device, supply a configuration index of
425 .Dv USB_UNCONFIG_INDEX
426 to
427 .Fn usbd_set_config_index ,
428 or else specify a configuration number of
429 .Dv USB_UNCONFIG_NO
430 to
431 .Fn usbd_set_config_no .
432 .Pp
433 The
434 .Fn usbd_get_config_descriptor
435 function returns a pointer to an in-memory copy of the full configuration
436 descriptor of the configuration that is currently active.
437 The returned pointer remains valid until the device configuration
438 is changed using
439 .Fn usbd_set_config_index
440 or
441 .Fn usbd_set_config_no .
442 If the device is unconfigured then
443 .Dv NULL
444 is returned instead.
445 .Pp
446 The function
447 .Fn usbd_interface_count
448 returns the number of interfaces available in the current device
449 configuration.
450 The
451 .Fn usbd_get_no_alts
452 function determines the number of alternate interfaces in a full
453 configuration descriptor by counting the interface descriptors with
454 .Fa bInterfaceNumber
455 equal to
456 .Fa ifaceno
457 (the count includes alternate index zero).
458 The
459 .Fn usbd_find_idesc
460 function locates an interface descriptor within a full configuration
461 descriptor.
462 The
463 .Fa ifaceidx
464 parameter specifies the interface index number, which should be less than
465 the number of interfaces in the configuration descriptor (i.e.\& the value
466 returned by
467 .Fn usbd_interface_count
468 or the
469 .Fa bNumInterface
470 field from the configuration descriptor).
471 An alternate interface can be specified using a non-zero
472 .Fa altidx ,
473 which should be less than the value returned by
474 .Fn usbd_get_no_alts .
475 The return value is a pointer to the requested interface descriptor
476 within the full configuration descriptor, or
477 .Dv NULL
478 if the specified interface descriptor does not exist.
479 Note that the
480 .Fa altidx
481 parameter specifies the alternate setting by index number starting
482 at zero; it is not the alternate setting number defined in the
483 interface descriptor.
484 .Pp
485 The function
486 .Fn usbd_find_edesc
487 locates an endpoint descriptor within a full configuration descriptor.
488 The
489 .Fa ifaceidx
490 and
491 .Fa altidx
492 parameters are the same as described for
493 .Fn usbd_find_idesc ,
494 and the
495 .Fa endptidx
496 parameter is an endpoint index number that should be less than the
497 .Fa bNumEndpoints
498 field in the interface descriptor.
499 The return value is a pointer to the requested endpoint descriptor
500 within the full configuration descriptor, or
501 .Dv NULL
502 if the specified endpoint descriptor does not exist.
503 Note that the
504 .Fa altidx
505 and
506 .Fa endptidx
507 parameters are index numbers starting at zero; they are not the
508 alternate setting and endpoint address defined in the descriptors.
509 .Pp
510 The
511 .Fn usbd_get_speed
512 function returns the device speed.
513 This can be
514 .Dv USB_SPEED_LOW ,
515 .Dv USB_SPEED_FULL
516 or
517 .Dv USB_SPEED_HIGH .
518 .Pp
519 USB devices optionally support string descriptors, which can be
520 retrieved using the
521 .Fn usbd_get_string
522 or
523 .Fn usbd_get_string_desc
524 functions.
525 Device, configuration and interface descriptors reference strings by
526 an index number that can be supplied to these functions.
527 The
528 .Fn usbd_get_string
529 function should be used unless a non-default language is required.
530 It requires that
531 .Fa buf
532 points to a buffer of at least
533 .Dv USB_MAX_STRING_LEN
534 bytes in size.
535 The
536 .Fa si
537 parameter specified which string to retrieve.
538 .Pp
539 The
540 .Fn usb_find_desc
541 function searches through the in-memory full configuration descriptor
542 for the active configuration and finds the first descriptor that has a
543 .Fa bDescriptorType
544 equal to
545 .Fa type ,
546 and if
547 .Fa subtype
548 is not equal to
549 .Dv USBD_SUBTYPE_ANY ,
550 the descriptor must also have a
551 .Fa bDescriptorSubtype
552 equal to
553 .Fa subtype .
554 If found, then a pointer to the descriptor is returned.
555 Otherwise,
556 .Fn usb_find_desc
557 returns
558 .Dv NULL .
559 The returned pointer is valid until the device configuration is changed using
560 .Fn usbd_set_config_index
561 or
562 .Fn usbd_set_config_no .
563 .Pp
564 The USB driver interface uses opaque interface handles to refer to
565 configuration interfaces.
566 These handles remain valid until the device configuration is changed using
567 .Fn usbd_set_config_index
568 or
569 .Fn usbd_set_config_no .
570 The
571 .Fn usbd_device2interface_handle
572 function retrieves an interface handle.
573 The
574 .Fa ifaceno
575 parameter is an interface index number starting at zero.
576 If the device is configured and the specified interface exists, then
577 .Dv USBD_NORMAL_COMPLETION
578 is returned and the interface handle is stored in
579 .Fa *iface .
580 Otherwise an error code is returned and
581 .Fa *iface
582 is not changed.
583 The
584 .Fn usbd_interface2device_handle
585 function retrieves the device handle from an interface handle.
586 This is just for convenience to save passing around the device
587 handle as well as the interface handle.
588 The
589 .Fn usbd_set_interface
590 function changes the alternate setting number for an interface to
591 the alternate setting identified by the zero-based index number
592 .Fa altidx .
593 This operation invalidates any existing endpoints on this
594 interface and their descriptors.
595 The
596 .Fn usbd_get_interface_altindex
597 function returns the current alternative setting index as was
598 specified when calling
599 .Fn usbd_set_interface .
600 The
601 .Fn usbd_endpoint_count
602 function retrieves the number of endpoints associated with the
603 specified interface.
604 The
605 .Fn usbd_interface2endpoint_descriptor
606 function returns a pointer to an in-memory endpoint descriptor for
607 the endpoint that has an index number of
608 .Fa index .
609 This pointer remains valid until the configuration or alternate setting
610 number are changed.
611 The function
612 .Fn usbd_get_endpoint_descriptor
613 is like
614 .Fn usbd_interface2endpoint_descriptor
615 but it accepts a
616 .Fa bEndpointAddress
617 address value instead of an index.
618 .Pp
619 The
620 .Fn usbd_fill_deviceinfo
621 function fills out a
622 .Vt usb_device_info
623 structure with information about the device.
624 The vendor and product names come from the device itself, falling back to
625 a table lookup or just providing the IDs in hexadecimal.
626 If
627 .Fa usedev
628 is zero then
629 .Fn usbd_fill_deviceinfo
630 will not attempt to retrieve the vendor and product names from the
631 device.
632 The usb_device_info structure is defined in
633 .In bus/usb/usb.h
634 as follows:
635 .Bd -literal
636 struct usb_device_info {
637         u_int8_t        udi_bus;
638         u_int8_t        udi_addr;       /* device address */
639         usb_event_cookie_t udi_cookie;
640         char            udi_product[USB_MAX_STRING_LEN];
641         char            udi_vendor[USB_MAX_STRING_LEN];
642         char            udi_release[8];
643         u_int16_t       udi_productNo;
644         u_int16_t       udi_vendorNo;
645         u_int16_t       udi_releaseNo;
646         u_int8_t        udi_class;
647         u_int8_t        udi_subclass;
648         u_int8_t        udi_protocol;
649         u_int8_t        udi_config;
650         u_int8_t        udi_speed;
651 #define USB_SPEED_LOW  1
652 #define USB_SPEED_FULL 2
653 #define USB_SPEED_HIGH 3
654         int             udi_power;      /* power consumption in mA */
655         int             udi_nports;
656         char            udi_devnames[USB_MAX_DEVNAMES][USB_MAX_DEVNAMELEN];
657         /* hub only: addresses of devices on ports */
658         u_int8_t        udi_ports[16];
659 #define USB_PORT_ENABLED 0xff
660 #define USB_PORT_SUSPENDED 0xfe
661 #define USB_PORT_POWERED 0xfd
662 #define USB_PORT_DISABLED 0xfc
663 }
664 .Ed
665 .Pp
666 The
667 .Fn usbd_devinfo
668 function generates a string description of the USB device.
669 The
670 .Fa cp
671 argument should point to a 1024-byte buffer (XXX the maximum length
672 is approximately 320 chars, but there is no sanity checking and everything uses
673 1024-character buffers).
674 Device class information is included if the
675 .Fa showclass
676 parameter is non-zero.
677 .Pp
678 The
679 .Fn usbd_get_quirks
680 function returns information from a table of devices that require
681 special workarounds in order to function correctly.
682 The returned structure is defined in
683 .In bus/usb/usb_quirks.h
684 as follows:
685 .Bd -literal
686 struct usbd_quirks {
687         u_int32_t uq_flags;     /* Device problems */
688 };
689 .Ed
690 .Pp
691 See
692 .In bus/usb/usb_quirks.h
693 for a list of all currently defined quirks.
694 .Pp
695 USB control requests are performed via
696 .Vt usb_device_request_t
697 structures, defined in
698 .In bus/usb/usb.h
699 as follows:
700 .Bd -literal
701 typedef struct {
702         uByte           bmRequestType;
703         uByte           bRequest;
704         uWord           wValue;
705         uWord           wIndex;
706         uWord           wLength;
707 } UPACKED usb_device_request_t;
708 .Ed
709 .Pp
710 The
711 .Fn usbd_do_request
712 function performs a single request synchronously.
713 The
714 .Fa req
715 parameter should point to a properly initialized
716 .Vt usb_device_request_t ,
717 and when the
718 .Fa wLength
719 field is non-zero,
720 .Fa data
721 should point at a buffer that is at least
722 .Fa wLength
723 bytes in length.
724 The request timeout is set to 5 seconds, so the operation will fail
725 with
726 .Er USBD_TIMEOUT
727 if the device does not respond within that time.
728 The
729 .Fn usbd_do_request_async
730 function is like
731 .Fn usbd_do_request ,
732 but it does not wait for the request to complete before returning.
733 This routine does not block so it can be used from contexts where
734 sleeping is not allowed.
735 Note that there is no notification mechanism to report when the
736 operation completed nor is there a way to determine whether the
737 request succeeded, so this function is of limited use.
738 See
739 .Fn usbd_setup_default_xfer
740 and
741 .Fn usbd_transfer
742 for a way to invoke an asynchronous callback upon completion of
743 a control request.
744 The
745 .Fn usbd_do_request_flags
746 function is like
747 .Fn usbd_do_request ,
748 but additional flags can be specified, the timeout is configurable,
749 and the actual number of bytes transferred is made available to the
750 caller.
751 The
752 .Fn usbd_do_request_flags_pipe
753 function uses a specified pipe instead of the default pipe.
754 .Pp
755 The function
756 .Fn usbd_open_pipe
757 creates a pipe connected to a specified endpoint on a specified interface.
758 The parameter
759 .Fa address
760 should be the
761 .Fa bEndpointAddress
762 value from one of this interface's endpoint descriptors.
763 If
764 .Fa flags
765 contains
766 .Dv USBD_EXCLUSIVE_USE
767 then the operation will only succeed if there are no open pipes
768 already connected to the specified endpoint.
769 The
770 .Fn usbd_open_pipe_intr
771 function creates an interrupt pipe connected to the specified endpoint.
772 The parameter
773 .Fa address
774 should be the
775 .Fa bEndpointAddress
776 value from one of this interface's endpoint descriptors.
777 The
778 .Fa flags
779 parameter is passed to
780 .Fn usbd_setup_xfer .
781 The
782 .Fa buffer
783 and
784 .Fa len
785 parameters define a buffer that is to be used for the interrupt transfers.
786 The callback to be invoked each time a transfer completes is specified by
787 .Fa cb ,
788 and
789 .Fa priv
790 is an argument to be passed to the callback function.
791 The
792 .Fa ival
793 parameter specifies the maximum acceptable interval between transfers;
794 in practice the transfers may occur more frequently.
795 The function
796 .Fn usbd_pipe2device_handle
797 returns the device associated with the specified
798 .Fa pipe .
799 .Pp
800 The
801 .Fn usbd_abort_pipe
802 function aborts all active or waiting transfers on the specified pipe.
803 Each transfer is aborted with a
804 .Dv USBD_CANCELLED
805 status; callback routines must detect this error code to ensure that
806 they do not attempt to initiate a new transfer in response to one being
807 aborted.
808 This routine blocks while it is waiting for the hardware to complete
809 processing of aborted transfers, so it is only safe to call it in
810 contexts where sleeping is allowed.
811 The function
812 .Fn usbd_abort_default_pipe
813 aborts all active or waiting transfers on the default pipe.
814 Like
815 .Fn usbd_abort_pipe ,
816 it blocks waiting for the hardware processing to complete.
817 .Pp
818 When a pipe has no active or waiting transfers, the pipe may be closed
819 using the
820 .Fn usbd_close_pipe
821 function.
822 Once a pipe is closed, its pipe handle becomes invalid and may no longer
823 be used.
824 .Pp
825 USB transfer handles are allocated using the function
826 .Fn usbd_alloc_xfer
827 and may be freed using
828 .Fn usbd_free_xfer .
829 .Pp
830 The function
831 .Fn usbd_setup_xfer
832 initializes a transfer handle with the details of a transfer to or from
833 a USB device.
834 The
835 .Fa xfer
836 parameter specifies the transfer handle to initialize,
837 .Fa pipe
838 specifies the pipe on which the transfer is to take place, and
839 .Fa priv
840 is an argument that will be passed to callback function.
841 The arguments
842 .Fa buffer
843 and
844 .Fa length
845 define the data buffer for the transfer.
846 If
847 .Fa length
848 is zero then the
849 .Fa buffer
850 may be
851 .Dv NULL .
852 The
853 .Fa flags
854 parameter may contain the following flags:
855 .Bl -tag -width ".Dv USBD_FORCE_SHORT_XFER"
856 .It Dv USBD_NO_COPY
857 This is used in association with
858 .Fn usbd_alloc_buffer
859 and
860 .Fn usbd_free_buffer
861 to use a dedicated DMA-capable buffer for the transfer.
862 .It Dv USBD_SYNCHRONOUS
863 Wait for the transfer to compete in
864 .Fn usbd_transfer .
865 .It Dv USBD_SHORT_XFER_OK
866 Permit transfers shorter than the requested data length.
867 .It Dv USBD_FORCE_SHORT_XFER
868 Force a short transfer at the end of a write operation to let the
869 device know that the transfer has ended.
870 .El
871 .Pp
872 The
873 .Fa timeout
874 parameter specifies a timeout for the transfer in milliseconds.
875 A value of
876 .Dv USBD_NO_TIMEOUT
877 indicates that no timeout should be configured.
878 The parameter
879 .Fa callback
880 specifies the function to call when the transfer completes.
881 Note that
882 .Fn usbd_setup_xfer
883 does not actually initiate the transfer.
884 The
885 .Fn usbd_setup_default_xfer
886 initializes a control transfer for the default pipe.
887 The
888 .Fa req
889 parameter should point at a completed
890 .Vt usb_device_request_t
891 structure.
892 The function
893 .Fa usbd_setup_isoc_xfer
894 initializes a transfer for an isochronous pipe.
895 .Pp
896 The function
897 .Fn usbd_transfer
898 initiates a transfer.
899 Normally it returns
900 .Dv USBD_IN_PROGRESS
901 to indicate that the transfer has been queued.
902 If the USB stack is operating in polling mode, or if the transfer
903 is synchronous, then
904 .Dv USBD_NORMAL_COMPLETION
905 may be returned.
906 Other return values indicate that the transfer could not be
907 initiated due to an error.
908 The
909 .Fn usbd_sync_transfer
910 function executes a transfer synchronously.
911 It will sleep waiting for the transfer to complete and then return
912 the transfer status.
913 Note that if the transfer has a callback routine, this will be
914 invoked before
915 .Fn usbd_sync_transfer
916 returns.
917 .Pp
918 The
919 .Fn usbd_intr_transfer
920 and
921 .Fn usbd_bulk_transfer
922 functions set up a transfer and wait synchronously for it to complete
923 but they allows signals to interrupt the wait.
924 They returns
925 .Dv USBD_INTERRUPTED
926 if the transfer was interrupted by a signal.
927 XXX these two functions are identical apart from their names.
928 .Pp
929 The function
930 .Fn usbd_get_xfer_status
931 retrieves various information from a completed transfer.
932 If the
933 .Fa priv
934 parameter is not NULL then the callback private argument is
935 stored in
936 .Fa *priv .
937 If
938 .Fa buffer
939 is not NULL then the transfer buffer pointer is stored in
940 .Fa *buffer .
941 The actual number of bytes transferred is stored in
942 .Fa *count
943 if
944 .Fa count
945 is not NULL.
946 Finally, the transfer status is stored in
947 .Fa *status
948 if
949 .Fa status
950 is not NULL.
951 .Pp
952 The
953 .Fn usbd_clear_endpoint_stall
954 function clears an endpoint stall condition synchronously, i.e.\&
955 it sleeps waiting for the stall clear request to complete.
956 The function
957 .Fn usbd_clear_endpoint_stall_async
958 performs the same function asynchronously, but it provides no
959 way to determine when the request completed, or whether it was
960 successful.
961 The
962 .Fn usbd_clear_endpoint_toggle
963 function instructs the host controller driver to reset the toggle bit
964 on a pipe.
965 This is used when manually clearing an endpoint stall using a
966 control pipe request, in order to ensure that the host controller
967 driver and the USB device restart with the same toggle value.
968 .Pp
969 Normally the USB subsystem maps and copies data to and from
970 DMA-capable memory each time a transfer is performed.
971 The function
972 .Fn usbd_alloc_buffer
973 allocates a permanent DMA-capable buffer associated with the
974 transfer to avoid this overhead.
975 The return value is the virtual address of the buffer.
976 Any time that
977 .Fn usbd_setup_xfer
978 is called on the transfer with the
979 .Dv USBD_NO_COPY
980 flag enabled, the allocated buffer will be used directly and
981 the
982 .Fa buffer
983 argument passed to
984 .Fn usbd_setup_xfer
985 will be ignored.
986 The
987 .Fn usbd_get_buffer
988 function returns a pointer to the virtual address of a buffer previously
989 allocated by
990 .Fn usbd_alloc_buffer .
991 Finally,
992 .Fn usbd_free_buffer
993 deallocates the buffer.
994 .Pp
995 The
996 .Fn usbd_errstr
997 function converts a status code into a string for display.
998 .Pp
999 The function
1000 .Fn usbd_set_polling
1001 enables or disables polling mode.
1002 In polling mode, all operations will busy-wait for the device to
1003 respond, so its use is effectively limited to boot time and kernel
1004 debuggers.
1005 It is important to match up calls that enable and disable polling
1006 mode, because the implementation just increments a polling reference
1007 count when
1008 .Fa on
1009 is non-zero and decrements it when
1010 .Fa on
1011 is zero.
1012 The
1013 .Fn usbd_dopoll
1014 causes the host controller driver to poll for any activity.
1015 This should only be used when polling mode is enabled.
1016 .Pp
1017 The
1018 .Fn usbd_ratecheck
1019 function is used to limit the rate at which error messages are
1020 printed to approximately once per second.
1021 The
1022 .Fa last
1023 argument should point at a persistent
1024 .Vt "struct timeval" .
1025 A value of 1 will be returned if a message should be printed, but if
1026 .Fn usbd_ratecheck
1027 has already been called with the same
1028 .Vt "struct timeval"
1029 parameter in the last second then 0 is returned and the error message
1030 should be suppressed.
1031 .Pp
1032 The functions
1033 .Fn usb_detach_wait
1034 and
1035 .Fn usb_detach_wakeup
1036 are used to wait for references to drain before completing the
1037 detachment of a device.
1038 The
1039 .Fn usb_detach_wait
1040 function will wait up to 60 seconds to receive a signal from
1041 .Fn usb_detach_wait .
1042 .Ss "USB Descriptors"
1043 The USB specification defines a number of standard descriptors by
1044 which USB devices report their attributes.
1045 These descriptors are fixed-format structures that all USB devices
1046 make available through USB control pipe requests.
1047 .Pp
1048 Every USB device has exactly one USB device descriptor.
1049 The USB subsystem retrieves this automatically when a device is
1050 attached, and a copy of the descriptor is kept in memory.
1051 The
1052 .Fn usbd_get_device_descriptor
1053 function returns a pointer to the descriptor.
1054 The device descriptor structure is defined in
1055 .In bus/usb/usb.h
1056 as follows:
1057 .Bd -literal
1058 typedef struct {
1059         uByte           bLength;
1060         uByte           bDescriptorType;
1061         uWord           bcdUSB;
1062 #define UD_USB_2_0              0x0200
1063 #define UD_IS_USB2(d) (UGETW((d)->bcdUSB) >= UD_USB_2_0)
1064         uByte           bDeviceClass;
1065         uByte           bDeviceSubClass;
1066         uByte           bDeviceProtocol;
1067         uByte           bMaxPacketSize;
1068         /* The fields below are not part of the initial descriptor. */
1069         uWord           idVendor;
1070         uWord           idProduct;
1071         uWord           bcdDevice;
1072         uByte           iManufacturer;
1073         uByte           iProduct;
1074         uByte           iSerialNumber;
1075         uByte           bNumConfigurations;
1076 } UPACKED usb_device_descriptor_t;
1077 #define USB_DEVICE_DESCRIPTOR_SIZE 18
1078 .Ed
1079 .Pp
1080 USB devices have at least one configuration descriptor.
1081 The
1082 .Fa bNumConfigurations
1083 field of the device descriptor specifies the number of configuration
1084 descriptors that a device supports.
1085 The
1086 .Fn usbd_get_config_desc
1087 function retrieves a particular configuration descriptor from the device
1088 and the
1089 .Fn usbd_get_config_desc_full
1090 function retrieves a full
1091 .Fa wTotalLength
1092 length configuration descriptor, which includes all related interface
1093 and endpoint descriptors.
1094 Only one configuration may be active at a time.
1095 The
1096 .Fn usbd_set_config_index
1097 function activates a specified configuration.
1098 The configuration descriptor structure is defined in
1099 .In bus/usb/usb.h
1100 as follows:
1101 .Bd -literal
1102 typedef struct {
1103         uByte           bLength;
1104         uByte           bDescriptorType;
1105         uWord           wTotalLength;
1106         uByte           bNumInterface;
1107         uByte           bConfigurationValue;
1108         uByte           iConfiguration;
1109         uByte           bmAttributes;
1110 #define UC_BUS_POWERED          0x80
1111 #define UC_SELF_POWERED         0x40
1112 #define UC_REMOTE_WAKEUP        0x20
1113         uByte           bMaxPower; /* max current in 2 mA units */
1114 #define UC_POWER_FACTOR 2
1115 } UPACKED usb_config_descriptor_t;
1116 #define USB_CONFIG_DESCRIPTOR_SIZE 9
1117 .Ed
1118 .Pp
1119 Each device configuration provides one or more interfaces.
1120 The
1121 .Fa bNumInterface
1122 field of the configuration descriptor specifies the number of
1123 interfaces associated with a device configuration.
1124 Interfaces are described by an interface descriptor, which is defined in
1125 .In bus/usb/usb.h
1126 as follows:
1127 .Bd -literal
1128 typedef struct {
1129         uByte           bLength;
1130         uByte           bDescriptorType;
1131         uByte           bInterfaceNumber;
1132         uByte           bAlternateSetting;
1133         uByte           bNumEndpoints;
1134         uByte           bInterfaceClass;
1135         uByte           bInterfaceSubClass;
1136         uByte           bInterfaceProtocol;
1137         uByte           iInterface;
1138 } UPACKED usb_interface_descriptor_t;
1139 #define USB_INTERFACE_DESCRIPTOR_SIZE 9
1140 .Ed
1141 .Pp
1142 Configurations may also have alternate interfaces with the same
1143 .Fa bInterfaceNumber
1144 but different
1145 .Fa bAlternateSetting
1146 values.
1147 These alternate interface settings may be selected by passing a
1148 non-zero
1149 .Fa altidx
1150 parameter to
1151 .Fn usbd_set_interface .
1152 .Pp
1153 Interfaces have zero or more endpoints, and each endpoint has an
1154 endpoint descriptor.
1155 Note that endpoint zero, which is always present, does not have an
1156 endpoint descriptor, and it is never included in the
1157 .Fa bNumEndpoints
1158 count of endpoints.
1159 The endpoint descriptor is defined in
1160 .In bus/usb/usb.h
1161 as follows:
1162 .Bd -literal
1163 typedef struct {
1164         uByte           bLength;
1165         uByte           bDescriptorType;
1166         uByte           bEndpointAddress;
1167 #define UE_GET_DIR(a)   ((a) & 0x80)
1168 #define UE_SET_DIR(a,d) ((a) | (((d)&1) << 7))
1169 #define UE_DIR_IN       0x80
1170 #define UE_DIR_OUT      0x00
1171 #define UE_ADDR         0x0f
1172 #define UE_GET_ADDR(a)  ((a) & UE_ADDR)
1173         uByte           bmAttributes;
1174 #define UE_XFERTYPE     0x03
1175 #define  UE_CONTROL     0x00
1176 #define  UE_ISOCHRONOUS 0x01
1177 #define  UE_BULK        0x02
1178 #define  UE_INTERRUPT   0x03
1179 #define UE_GET_XFERTYPE(a)      ((a) & UE_XFERTYPE)
1180 #define UE_ISO_TYPE     0x0c
1181 #define  UE_ISO_ASYNC   0x04
1182 #define  UE_ISO_ADAPT   0x08
1183 #define  UE_ISO_SYNC    0x0c
1184 #define UE_GET_ISO_TYPE(a)      ((a) & UE_ISO_TYPE)
1185         uWord           wMaxPacketSize;
1186         uByte           bInterval;
1187 } UPACKED usb_endpoint_descriptor_t;
1188 #define USB_ENDPOINT_DESCRIPTOR_SIZE 7
1189 .Ed
1190 .Sh RETURN VALUES
1191 Many functions return a
1192 .Vt usbd_status
1193 type to indicate the outcome of the operation.
1194 If the operation completed successfully then
1195 .Dv USBD_NORMAL_COMPLETION
1196 is returned.
1197 Operations that have been started but not yet completed will return
1198 .Dv USBD_IN_PROGRESS .
1199 Other errors usually indicate a problem.
1200 Error codes can be converted to strings using
1201 .Fn usbd_errstr .
1202 .Sh ERRORS
1203 .Bl -tag -width ".Er USBD_PENDING_REQUESTS"
1204 .It Bq Er USBD_PENDING_REQUESTS
1205 A pipe could not be closed because there are active requests.
1206 .It Bq Er USBD_NOT_STARTED
1207 The transfer has not yet been started.
1208 .It Bq Er USBD_INVAL
1209 An invalid value was supplied.
1210 .It Bq Er USBD_NOMEM
1211 An attempt to allocate memory failed.
1212 .It Bq Er USBD_CANCELLED
1213 The transfer was aborted.
1214 .It Bq Er USBD_BAD_ADDRESS
1215 The specified endpoint address was not found.
1216 .It Bq Er USBD_IN_USE
1217 The endpoint is already in use, or the configuration cannot be changed
1218 because some of its endpoints are in use.
1219 .It Bq Er USBD_NO_ADDR
1220 No free USB devices addresses were found to assign to the device.
1221 .It Bq Er USBD_SET_ADDR_FAILED
1222 The device address could not be set.
1223 .It Bq Er USBD_NO_POWER
1224 Insufficient power was available for the device.
1225 .It Bq Er USBD_TOO_DEEP
1226 Too many levels of chained hubs were found.
1227 .It Bq Er USBD_IOERROR
1228 There was an error communicating with the device.
1229 .It Bq Er USBD_NOT_CONFIGURED
1230 An operation that requires an active configuration was attempted while
1231 the device was in an unconfigured state.
1232 .It Bq Er USBD_TIMEOUT
1233 A transfer timed out.
1234 .It Bq Er USBD_SHORT_XFER
1235 A transfer that disallowed short data lengths completed with less than
1236 the requested length transferred.
1237 .It Bq Er USBD_STALLED
1238 A transfer failed because the pipe is stalled.
1239 .It Bq Er USBD_INTERRUPTED
1240 An interruptible operation caught a signal.
1241 .El
1242 .Sh SEE ALSO
1243 .Xr usb 4
1244 .Sh HISTORY
1245 The USB driver interface first appeared in
1246 .Fx 3.0 .
1247 .Sh AUTHORS
1248 The USB driver was written by
1249 .An Lennart Augustsson
1250 for the
1251 .Nx
1252 project.
1253 .Pp
1254 .An -nosplit
1255 This manual page was written by
1256 .An Ian Dowse Aq iedowse@FreeBSD.org .