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