xref: /freebsd/lib/libusb/libusb.3 (revision dc60165b73e4c4d829a2cb9fed5cce585e93d9a9)
1.\"
2.\" Copyright (c) 2008 Hans Petter Selasky
3.\"
4.\" All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\"
15.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25.\" SUCH DAMAGE.
26.\"
27.\" $FreeBSD$
28.\"
29.Dd Feb 14, 2009
30.Dt LIBUSB 3
31.Os
32.Sh NAME
33.Nm libusb
34.
35.Nd "USB access library"
36.
37.
38.Sh LIBRARY
39.
40.
41USB access library (libusb -lusb)
42.
43.
44.
45.Sh SYNOPSIS
46.
47.
48.In libusb20.h
49.
50.
51.Sh DESCRIPTION
52.
53The
54.Nm
55library implements functions to be able to easily access and control
56USB through the USB file system interface.
57.
58.
59.Sh USB TRANSFER OPERATIONS
60.
61.Pp
62.
63.Fn libusb20_tr_close pxfer
64This function will release all kernel resources associated with an USB
65.Fa pxfer .
66.
67This function returns zero upon success.
68.
69Non-zero return values indicate a LIBUSB20_ERROR value.
70.
71.Pp
72.
73.Fn libusb20_tr_open pxfer max_buf_size max_frame_count ep_no
74This function will allocate kernel resources like
75.Fa max_buf_size
76and
77.Fa max_frame_count
78associated with an USB
79.Fa pxfer
80and bind the transfer to the specified
81.Fa ep_no .
82.
83This function returns zero upon success.
84.
85Non-zero return values indicate a LIBUSB20_ERROR value.
86.
87.Pp
88.
89.Fn libusb20_tr_get_pointer pdev tr_index
90This function will return a pointer to the allocated USB transfer according to the
91.Fa pdev
92and
93.Fa tr_index
94arguments.
95.
96This function returns NULL in case of failure.
97.
98.Pp
99.
100.Fn libusb20_tr_get_time_complete pxfer
101This function will return the completion time of an USB transfer in
102millisecond units. This function is most useful for isochronous USB
103transfers when doing echo cancelling.
104.
105.Pp
106.
107.Fn libusb20_tr_get_actual_frames pxfer
108This function will return the actual number of USB frames after an USB
109transfer completed. A value of zero means that no data was transferred.
110.
111.Pp
112.
113.Fn libusb20_tr_get_actual_length pxfer
114This function will return the sum of the actual length for all
115transferred USB frames for the given USB transfer.
116.
117.Pp
118.
119.Fn libusb20_tr_get_max_frames pxfer
120This function will return the maximum number of USB frames that were
121allocated when an USB transfer was setup for the given USB transfer.
122.
123.Pp
124.
125.Fn libusb20_tr_get_max_packet_length pxfer
126This function will return the maximum packet length in bytes
127associated with the given USB transfer.
128.
129The packet length can be used round up buffer sizes so that short USB
130packets are avoided for proxy buffers.
131.
132.
133.Pp
134.
135.Fn libusb20_tr_get_max_total_length pxfer
136This function will return the maximum value for the length sum of all
137USB frames associated with an USB transfer.
138.
139.Pp
140.
141.Fn libusb20_tr_get_status pxfer
142This function will return the status of an USB transfer.
143.
144Status values are defined by a set of LIBUSB20_TRANSFER_XXX enums.
145.
146.Pp
147.
148.Fn libusb20_tr_pending pxfer
149This function will return non-zero if the given USB transfer is
150pending for completion.
151.
152Else this function returns zero.
153.
154.Pp
155.
156.Fn libusb20_tr_callback_wrapper pxfer
157This is an internal function used to wrap asynchronous USB callbacks.
158.
159.Pp
160.
161.Fn libusb20_tr_clear_stall_sync pxfer
162This is an internal function used to synchronously clear the stall on
163the given USB transfer.
164.
165Please see the USB specification for more information on stall
166clearing.
167.
168If the given USB transfer is pending when this function is called, the
169USB transfer will complete with an error after that this function has
170been called.
171.
172.Pp
173.
174.Fn libusb20_tr_drain pxfer
175This function will stop the given USB transfer and will not return
176until the USB transfer has been stopped in hardware.
177.
178.Pp
179.
180.Fn libusb20_tr_set_buffer pxfer pbuf fr_index
181This function is used to set the
182.Fa buffer
183pointer for the given USB transfer and
184.Fa fr_index .
185.
186Typically the frame index is zero.
187.
188.
189.Pp
190.
191.Fn libusb20_tr_set_callback pxfer pcallback
192This function is used to set the USB callback for asynchronous USB
193transfers.
194.
195The callback type is defined by libusb20_tr_callback_t.
196.
197.Pp
198.
199.Fn libusb20_tr_set_flags pxfer flags
200This function is used to set various USB flags for the given USB transfer.
201.Bl -tag
202.It LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK
203Report a short frame as error.
204.It LIBUSB20_TRANSFER_MULTI_SHORT_NOT_OK
205Multiple short frames are not allowed.
206.It LIBUSB20_TRANSFER_FORCE_SHORT
207All transmitted frames are short terminated.
208.It LIBUSB20_TRANSFER_DO_CLEAR_STALL
209Will do a clear-stall before starting the transfer.
210.El
211.
212.Pp
213.
214.Fn libusb20_tr_set_length pxfer length fr_index
215This function sets the length of a given USB transfer and frame index.
216.
217.Pp
218.
219.Fn libusb20_tr_set_priv_sc0 pxfer psc0
220This function sets private driver pointer number zero.
221.
222.Pp
223.
224.Fn libusb20_tr_set_priv_sc1 pxfer psc1
225This function sets private driver pointer number one.
226.
227.Pp
228.
229.Fn libusb20_tr_set_timeout pxfer timeout
230This function sets the timeout for the given USB transfer.
231.
232A timeout value of zero means no timeout.
233.
234The timeout is given in milliseconds.
235.
236.Pp
237.
238.Fn libusb20_tr_set_total_frames pxfer nframes
239This function sets the total number of frames that should be executed when the USB transfer is submitted.
240.
241The total number of USB frames must be less than the maximum number of USB frames associated with the given USB transfer.
242.
243.Pp
244.
245.Fn libusb20_tr_setup_bulk pxfer pbuf length timeout
246This function is a helper function for setting up a single frame USB BULK transfer.
247.
248.Pp
249.
250.Fn libusb20_tr_setup_control pxfer psetup pbuf timeout
251This function is a helper function for setting up a single or dual
252frame USB CONTROL transfer depending on the control transfer length.
253.
254.Pp
255.
256.Fn libusb20_tr_setup_intr pxfer pbuf length timeout
257This function is a helper function for setting up a single frame USB INTERRUPT transfer.
258.
259.Pp
260.
261.Fn libusb20_tr_setup_isoc pxfer pbuf length fr_index
262This function is a helper function for setting up a multi frame USB ISOCHRONOUS transfer.
263.
264.Pp
265.
266.Fn libusb20_tr_start pxfer
267This function will get the USB transfer started, if not already
268started.
269.
270This function will not get the transfer queued in hardware.
271.
272This function is non-blocking.
273.
274.Pp
275.
276.Fn libusb20_tr_stop pxfer
277This function will get the USB transfer stopped, if not already stopped.
278.
279This function is non-blocking, which means that the actual stop can
280happen after the return of this function.
281.
282.Pp
283.
284.Fn libusb20_tr_submit pxfer
285This function will get the USB transfer queued in hardware.
286.
287.
288.Pp
289.
290.Fn libusb20_tr_get_priv_sc0 pxfer
291This function returns private driver pointer number zero associated
292with an USB transfer.
293.
294.
295.Pp
296.
297.Fn libusb20_tr_get_priv_sc1 pxfer
298This function returns private driver pointer number one associated
299with an USB transfer.
300.
301.
302.Sh USB DEVICE OPERATIONS
303.
304.Pp
305.
306.Fn libusb20_dev_get_backend_name pdev
307This function returns a zero terminated string describing the backend used.
308.
309.Pp
310.
311.Fn libusb20_dev_get_info pdev pinfo
312This function retrives the BSD specific usb2_device_info structure into the memory location given by
313.Fa pinfo .
314The USB device given by
315.Fa pdev
316must be opened before this function will succeed.
317This function returns zero on success else a LIBUSB20_ERROR value is returned.
318.
319.Pp
320.
321.Fn libusb20_dev_get_iface_desc pdev iface_index pbuf len
322This function retrieves the kernel interface description for the given USB
323.Fa iface_index .
324The format of the USB interface description is: "drivername<unit>: <description>"
325The description string is always zero terminated.
326A zero length string is written in case no driver is attached to the given interface.
327The USB device given by
328.Fa pdev
329must be opened before this function will succeed.
330This function returns zero on success else a LIBUSB20_ERROR value is returned.
331.
332.Pp
333.
334.Fn libusb20_dev_get_desc pdev
335This function returns a zero terminated string describing the given USB device.
336The format of the string is: "drivername<unit>: <description>"
337.
338.Pp
339.
340.Fn libusb20_dev_claim_interface pdev iface_index
341This function will try to claim the given USB interface given by
342.Fa iface_index .
343This function returns zero on success else a LIBUSB20_ERROR value is
344returned.
345.
346.Pp
347.
348.Fn libusb20_dev_close pdev
349This function will close the given USB device.
350.
351This function returns zero on success else a LIBUSB20_ERROR value is
352returned.
353.
354.Pp
355.
356.Fn libusb20_dev_detach_kernel_driver pdev iface_index
357This function will try to detach the kernel driver for the USB interface given by
358.Fa iface_index .
359.
360This function returns zero on success else a LIBUSB20_ERROR value is
361returned.
362.
363.Pp
364.
365.Fn libusb20_dev_set_config_index pdev config_index
366This function will try to set the configuration index on an USB
367device.
368.
369The first configuration index is zero.
370.
371The un-configure index is 255.
372.
373This function returns zero on success else a LIBUSB20_ERROR value is returned.
374.
375.Pp
376.
377.Fn libusb20_dev_get_debug pdev
378This function returns the debug level of an USB device.
379.
380.Pp
381.
382.Fn libusb20_dev_get_fd pdev
383This function returns the file descriptor of the given USB device.
384.
385A negative value is returned when no file descriptor is present.
386.
387The file descriptor can be used for polling purposes.
388.
389.Pp
390.
391.Fn libusb20_dev_kernel_driver_active pdev iface_index
392This function returns a non-zero value if a kernel driver is active on
393the given USB interface.
394.
395Else zero is returned.
396.
397.Pp
398.
399.Fn libusb20_dev_open pdev transfer_max
400This function opens an USB device so that setting up USB transfers
401becomes possible.
402.
403The number of USB transfers can be zero which means only control
404transfers are allowed.
405.
406This function returns zero on success else a LIBUSB20_ERROR value is
407returned.
408.
409A return value of LIBUSB20_ERROR_BUSY means that the device is already
410opened.
411.
412.Pp
413.
414.Fn libusb20_dev_process pdev
415This function is called to sync kernel USB transfers with userland USB
416transfers.
417.
418This function returns zero on success else a LIBUSB20_ERROR value is
419returned typically indicating that the given USB device has been
420detached.
421.
422.Pp
423.
424.Fn libusb20_dev_release_interface pdev iface_index
425This function will try to release a claimed USB interface for the specified USB device.
426.
427This function returns zero on success else a LIBUSB20_ERROR value is
428returned.
429.
430.Pp
431.
432.Fn libusb20_dev_request_sync pdev psetup pdata pactlen timeout flags
433This function will perform a synchronous control request on the given
434USB device.
435.
436Before this call will succeed the USB device must be opened.
437.
438.Fa setup
439is a pointer to a decoded and host endian SETUP packet.
440.Fa data
441is a pointer to a data transfer buffer associated with the control transaction. This argument can be NULL.
442.Fa pactlen
443is a pointer to a variable that will hold the actual transfer length after the control transaction is complete.
444.Fa timeout
445is the transaction timeout given in milliseconds.
446A timeout of zero means no timeout.
447.Fa flags
448is used to specify transaction flags, for example LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK.
449.
450This function returns zero on success else a LIBUSB20_ERROR value is
451returned.
452.
453.Pp
454.
455.Fn libusb20_dev_req_string_sync pdev index lang_id pbuf len
456This function will synchronously request an USB string by language ID
457and string index into the given buffer limited by a maximum length.
458.
459This function returns zero on success else a LIBUSB20_ERROR value is
460returned.
461.
462.Pp
463.
464.Fn libusb20_dev_req_string_simple_sync pdev index pbuf len
465This function will synchronously request an USB string using the
466default language ID and convert the string into ASCII before storing
467the string into the given buffer limited by a maximum length which
468includes the terminating zero.
469.
470This function returns zero on success else a LIBUSB20_ERROR value is
471returned.
472.
473.
474.Pp
475.
476.Fn libusb20_dev_reset pdev
477This function will try to BUS reset the given USB device and restore
478the last set USB configuration.
479.
480This function returns zero on success else a LIBUSB20_ERROR value is
481returned.
482.
483.Pp
484.
485.Fn libusb20_dev_set_power_mode pdev power_mode
486This function sets the power mode of the USB device.
487.
488Valid power modes:
489.Bl -tag
490.It LIBUSB20_POWER_OFF
491.It LIBUSB20_POWER_ON
492.It LIBUSB20_POWER_SAVE
493.It LIBUSB20_POWER_SUSPEND
494.It LIBUSB20_POWER_RESUME
495.El
496.
497This function returns zero on success else a LIBUSB20_ERROR value is
498returned.
499.
500.Pp
501.
502.Fn libusb20_dev_get_power_mode pdev
503This function returns the currently selected power mode for the given
504USB device.
505.
506.Pp
507.
508.Fn libusb20_dev_set_alt_index pdev iface_index alt_index
509This function will try to set the given alternate index for the given
510USB interface index.
511.
512This function returns zero on success else a LIBUSB20_ERROR value is
513returned.
514.
515.Pp
516.
517.Fn libusb20_dev_get_device_desc pdev
518This function returns a pointer to the decoded and host endian version
519of the device descriptor.
520.
521The USB device need not be opened when calling this function.
522.
523.Pp
524.
525.Fn libusb20_dev_alloc_config pdev config_index
526This function will read out and decode the USB config descriptor for
527the given USB device and config index. This function returns a pointer
528to the decoded configuration which must eventually be passed to
529free(). NULL is returned in case of failure.
530.
531.Pp
532.
533.Fn libusb20_dev_alloc void
534This is an internal function to allocate a new USB device.
535.
536.Pp
537.
538.Fn libusb20_dev_get_address pdev
539This function returns the internal and not necessarily the real
540hardware address of the given USB device.
541.
542.Pp
543.
544.Fn libusb20_dev_get_bus_number pdev
545This function return the internal bus number which the given USB
546device belongs to.
547.
548.Pp
549.
550.Fn libusb20_dev_get_mode pdev
551This function returns the current operation mode of the USB entity.
552.
553Valid return values are:
554.Bl -tag
555.It LIBUSB20_MODE_HOST
556.It LIBUSB20_MODE_DEVICE
557.El
558.
559.Pp
560.
561.Fn libusb20_dev_get_speed pdev
562This function returns the current speed of the given USB device.
563.
564.Bl -tag
565.It LIBUSB20_SPEED_UNKNOWN
566.It LIBUSB20_SPEED_LOW
567.It LIBUSB20_SPEED_FULL
568.It LIBUSB20_SPEED_HIGH
569.It LIBUSB20_SPEED_VARIABLE
570.It LIBUSB20_SPEED_SUPER
571.El
572.
573.Pp
574.
575.Fn libusb20_dev_get_config_index pdev
576This function returns the currently select config index for the given
577USB device.
578.
579.Pp
580.
581.Fn libusb20_dev_free pdev
582This function will free the given USB device and all associated USB
583transfers.
584.
585.Pp
586.
587.Fn libusb20_dev_set_debug pdev debug_level
588This function will set the debug level for the given USB device.
589.
590.Pp
591.
592.Fn libusb20_dev_wait_process pdev timeout
593This function will wait until a pending USB transfer has completed on
594the given USB device.
595.
596A timeout value can be specified which is passed on to the
597.Xr 2 poll
598function.
599.
600.Sh USB BACKEND OPERATIONS
601.
602.Fn libusb20_be_get_template pbackend ptemp
603This function will return the currently selected global USB device
604side mode template into the integer pointer
605.Fa ptemp .
606This function returns zero on success else a LIBUSB20_ERROR value is
607returned.
608.
609.Pp
610.
611.Fn libusb20_be_set_template pbackend temp
612This function will set the global USB device side mode template to
613.Fa temp .
614The new template is not activated until after the next USB
615enumeration.
616The template number decides how the USB device will present itself to
617the USB Host, like Mass Storage Device, USB Ethernet Device. Also see
618the
619.Xr usb2_template 4
620module.
621This function returns zero on success else a LIBUSB20_ERROR value is
622returned.
623.
624.Pp
625.
626.Fn libusb20_be_get_dev_quirk pbackend index pquirk
627This function will return the device quirk according to
628.Fa index
629into the libusb20_quirk structure pointed to by
630.Fa pq .
631This function returns zero on success else a LIBUSB20_ERROR value is
632returned.
633.
634If the given quirk does not exist LIBUSB20_ERROR_NOT_FOUND is
635returned.
636.
637.Pp
638.
639.Fn libusb20_be_get_quirk_name pbackend index pquirk
640This function will return the quirk name according to
641.Fa index
642into the libusb20_quirk structure pointed to by
643.Fa pq .
644This function returns zero on success else a LIBUSB20_ERROR value is
645returned.
646.
647If the given quirk does not exist LIBUSB20_ERROR_NOT_FOUND is
648returned.
649.
650.Pp
651.
652.Fn libusb20_be_add_dev_quirk pbackend pquirk
653This function will add the libusb20_quirk structure pointed to by the
654.Fa pq
655argument into the device quirk list.
656.
657This function returns zero on success else a LIBUSB20_ERROR value is
658returned.
659.
660If the given quirk cannot be added LIBUSB20_ERROR_NO_MEM is
661returned.
662.
663.Pp
664.
665.Fn libusb20_be_remove_dev_quirk pbackend pquirk
666This function will remove the quirk matching the libusb20_quirk structure pointed to by the
667.Fa pq
668argument from the device quirk list.
669.
670This function returns zero on success else a LIBUSB20_ERROR value is
671returned.
672.
673If the given quirk does not exist LIBUSB20_ERROR_NOT_FOUND is
674returned.
675.
676.Fn libusb20_be_alloc_linux void
677These functions are used to allocate a specific USB backend or the
678operating system default USB backend. Allocating a backend is a way to
679scan for currently present USB devices.
680.
681.Pp
682.
683.Fn libusb20_be_device_foreach pbackend pdev
684This function is used to iterate USB devices present in a USB backend.
685.
686The starting value of
687.Fa pdev
688is NULL.
689.
690This function returns the next USB device in the list.
691.
692If NULL is returned the end of the USB device list has been reached.
693.
694.Pp
695.
696.Fn libusb20_be_dequeue_device pbackend pdev
697This function will dequeue the given USB device pointer from the
698backend USB device list.
699.
700Dequeued USB devices will not be freed when the backend is freed.
701.
702.Pp
703.
704.Fn libusb20_be_enqueue_device pbackend pdev
705This function will enqueue the given USB device pointer in the backend USB device list.
706.
707Enqueued USB devices will get freed when the backend is freed.
708.
709.Pp
710.
711.Fn libusb20_be_free pbackend
712This function will free the given backend and all USB devices in its device list.
713.
714.
715.Sh USB DESCRIPTOR PARSING
716.
717.Fn libusb20_me_get_1 pie offset
718This function will return a byte at the given byte offset of a message
719entity.
720.
721This function is safe against invalid offsets.
722.
723.Pp
724.
725.Fn libusb20_me_get_2 pie offset
726This function will return a little endian 16-bit value at the given byte offset of a message
727entity.
728.
729This function is safe against invalid offsets.
730.
731.Pp
732.
733.Fn libusb20_me_encode pbuf len pdecoded
734This function will encode a so-called *DECODED structure into binary
735format.
736.
737The total encoded length that will fit in the given buffer is
738returned.
739.
740If the buffer pointer is NULL no data will be written to the buffer
741location.
742.
743.Pp
744.
745.Fn libusb20_me_decode pbuf len pdecoded
746This function will decode a binary structure into a so-called *DECODED
747structure.
748.
749The total decoded length is returned.
750.
751The buffer pointer cannot be NULL.
752.
753.
754.Sh LIBUSB VERSION 0.1 COMPATIBILITY
755.
756.Fn usb_open
757.Fn usb_close
758.Fn usb_get_string
759.Fn usb_get_string_simple
760.Fn usb_get_descriptor_by_endpoint
761.Fn usb_get_descriptor
762.Fn usb_parse_descriptor
763.Fn usb_parse_configuration
764.Fn usb_destroy_configuration
765.Fn usb_fetch_and_parse_descriptors
766.Fn usb_bulk_write
767.Fn usb_bulk_read
768.Fn usb_interrupt_write
769.Fn usb_interrupt_read
770.Fn usb_control_msg
771.Fn usb_set_configuration
772.Fn usb_claim_interface
773.Fn usb_release_interface
774.Fn usb_set_altinterface
775.Fn usb_resetep
776.Fn usb_clear_halt
777.Fn usb_reset
778.Fn usb_strerror
779.Fn usb_init
780.Fn usb_set_debug
781.Fn usb_find_busses
782.Fn usb_find_devices
783.Fn usb_device
784.Fn usb_get_busses
785These functions are compliant with LibUSB version 0.1.12.
786.
787.Sh FILES
788.
789.
790/dev/usb
791.Sh SEE ALSO
792.Xr usb2_core 4 ,
793.Xr usbconfig 8
794.
795.
796.Sh HISTORY
797.
798.
799Some parts of the
800.Nm
801API derives from the libusb project at sourceforge.
802