xref: /freebsd/share/man/man9/usbdi.9 (revision 06bac0da1314907b8de37bbad559777ab418e811)
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.
2532a455e0SEdward Tomasz Napierala.Dd November 14, 2016
260a9a8c2eSIan Dowse.Dt USBDI 9
27aa12cea2SUlrich Spörlein.Os
280a9a8c2eSIan Dowse.Sh NAME
29b35f050eSAndrew Thompson.Nm usb_fifo_alloc_buffer ,
30b35f050eSAndrew Thompson.Nm usb_fifo_attach ,
31b35f050eSAndrew Thompson.Nm usb_fifo_detach ,
32b35f050eSAndrew Thompson.Nm usb_fifo_free_buffer ,
33b35f050eSAndrew Thompson.Nm usb_fifo_get_data ,
34b35f050eSAndrew Thompson.Nm usb_fifo_get_data_buffer ,
35b35f050eSAndrew Thompson.Nm usb_fifo_get_data_error ,
36b35f050eSAndrew Thompson.Nm usb_fifo_get_data_linear ,
37b35f050eSAndrew Thompson.Nm usb_fifo_put_bytes_max ,
38b35f050eSAndrew Thompson.Nm usb_fifo_put_data ,
39b35f050eSAndrew Thompson.Nm usb_fifo_put_data_buffer ,
40b35f050eSAndrew Thompson.Nm usb_fifo_put_data_error ,
41b35f050eSAndrew Thompson.Nm usb_fifo_put_data_linear ,
42b35f050eSAndrew Thompson.Nm usb_fifo_reset ,
43b35f050eSAndrew Thompson.Nm usb_fifo_softc ,
44b35f050eSAndrew Thompson.Nm usb_fifo_wakeup ,
450a9a8c2eSIan Dowse.Nm usbd_do_request ,
460a9a8c2eSIan Dowse.Nm usbd_do_request_flags ,
470a9a8c2eSIan Dowse.Nm usbd_errstr ,
48b35f050eSAndrew Thompson.Nm usbd_lookup_id_by_info ,
49b35f050eSAndrew Thompson.Nm usbd_lookup_id_by_uaa ,
50b35f050eSAndrew Thompson.Nm usbd_transfer_clear_stall ,
51b35f050eSAndrew Thompson.Nm usbd_transfer_drain ,
52b35f050eSAndrew Thompson.Nm usbd_transfer_pending ,
53b35f050eSAndrew Thompson.Nm usbd_transfer_poll ,
54b35f050eSAndrew Thompson.Nm usbd_transfer_setup ,
55b35f050eSAndrew Thompson.Nm usbd_transfer_start ,
56b35f050eSAndrew Thompson.Nm usbd_transfer_stop ,
57b35f050eSAndrew Thompson.Nm usbd_transfer_submit ,
58b35f050eSAndrew Thompson.Nm usbd_transfer_unsetup ,
59b35f050eSAndrew Thompson.Nm usbd_xfer_clr_flag ,
60b35f050eSAndrew Thompson.Nm usbd_xfer_frame_data ,
61b35f050eSAndrew Thompson.Nm usbd_xfer_frame_len ,
62b35f050eSAndrew Thompson.Nm usbd_xfer_get_frame ,
63b35f050eSAndrew Thompson.Nm usbd_xfer_get_priv ,
64b35f050eSAndrew Thompson.Nm usbd_xfer_is_stalled ,
65b35f050eSAndrew Thompson.Nm usbd_xfer_max_framelen ,
66b35f050eSAndrew Thompson.Nm usbd_xfer_max_frames ,
67b35f050eSAndrew Thompson.Nm usbd_xfer_max_len ,
68b35f050eSAndrew Thompson.Nm usbd_xfer_set_flag ,
69b35f050eSAndrew Thompson.Nm usbd_xfer_set_frame_data ,
70b35f050eSAndrew Thompson.Nm usbd_xfer_set_frame_len ,
71b35f050eSAndrew Thompson.Nm usbd_xfer_set_frame_offset ,
72b35f050eSAndrew Thompson.Nm usbd_xfer_set_frames ,
73b35f050eSAndrew Thompson.Nm usbd_xfer_set_interval ,
74b35f050eSAndrew Thompson.Nm usbd_xfer_set_priv ,
75b35f050eSAndrew Thompson.Nm usbd_xfer_set_stall ,
76b35f050eSAndrew Thompson.Nm usbd_xfer_set_timeout ,
77b35f050eSAndrew Thompson.Nm usbd_xfer_softc ,
78b35f050eSAndrew Thompson.Nm usbd_xfer_state ,
79b35f050eSAndrew Thompson.Nm usbd_xfer_status
800a9a8c2eSIan Dowse.Nd Universal Serial Bus driver programming interface
810a9a8c2eSIan Dowse.Sh SYNOPSIS
820a9a8c2eSIan Dowse.In dev/usb/usb.h
830a9a8c2eSIan Dowse.In dev/usb/usbdi.h
840a9a8c2eSIan Dowse.In dev/usb/usbdi_util.h
8532a455e0SEdward Tomasz Napierala.Ft "usb_error_t"
8632a455e0SEdward Tomasz Napierala.Fo "usbd_transfer_setup"
8732a455e0SEdward Tomasz Napierala.Fa "struct usb_device *udev"
8832a455e0SEdward Tomasz Napierala.Fa "const uint8_t *ifaces"
8932a455e0SEdward Tomasz Napierala.Fa "struct usb_xfer **pxfer"
9032a455e0SEdward Tomasz Napierala.Fa "const struct usb_config *setup_start"
9132a455e0SEdward Tomasz Napierala.Fa "uint16_t n_setup"
9232a455e0SEdward Tomasz Napierala.Fa "void *priv_sc"
9332a455e0SEdward Tomasz Napierala.Fa "struct mtx *priv_mtx"
9432a455e0SEdward Tomasz Napierala.Fc
9532a455e0SEdward Tomasz Napierala.Ft "void"
9632a455e0SEdward Tomasz Napierala.Fo "usbd_transfer_unsetup"
9732a455e0SEdward Tomasz Napierala.Fa "struct usb_xfer **pxfer"
9832a455e0SEdward Tomasz Napierala.Fa "uint16_t n_setup"
9932a455e0SEdward Tomasz Napierala.Fc
10032a455e0SEdward Tomasz Napierala.Ft "void"
10132a455e0SEdward Tomasz Napierala.Fo "usbd_transfer_start"
10232a455e0SEdward Tomasz Napierala.Fa "struct usb_xfer *xfer"
10332a455e0SEdward Tomasz Napierala.Fc
10432a455e0SEdward Tomasz Napierala.Ft "void"
10532a455e0SEdward Tomasz Napierala.Fo "usbd_transfer_stop"
10632a455e0SEdward Tomasz Napierala.Fa "struct usb_xfer *xfer"
10732a455e0SEdward Tomasz Napierala.Fc
10832a455e0SEdward Tomasz Napierala.Ft "void"
10932a455e0SEdward Tomasz Napierala.Fo "usbd_transfer_drain"
11032a455e0SEdward Tomasz Napierala.Fa "struct usb_xfer *xfer"
11132a455e0SEdward Tomasz Napierala.Fc
1120a9a8c2eSIan Dowse.Sh DESCRIPTION
1130a9a8c2eSIan DowseThe Universal Serial Bus (USB) driver programming interface provides
1140a9a8c2eSIan DowseUSB peripheral drivers with a host controller independent API for
1150a9a8c2eSIan Dowsecontrolling and communicating with USB peripherals.
116b35f050eSAndrew ThompsonThe
117b35f050eSAndrew Thompson.Nm usb
118b35f050eSAndrew Thompsonmodule supports both USB Host and USB Device side mode.
119b35f050eSAndrew Thompson.Sh USB TRANSFER MANAGEMENT FUNCTIONS
120b35f050eSAndrew ThompsonThe USB standard defines four types of USB transfers.
121b35f050eSAndrew Thompson.
122b35f050eSAndrew ThompsonControl transfers, Bulk transfers, Interrupt transfers and Isochronous
123b35f050eSAndrew Thompsontransfers.
124b35f050eSAndrew Thompson.
125b35f050eSAndrew ThompsonAll the transfer types are managed using the following five functions:
126b35f050eSAndrew Thompson.
1270a9a8c2eSIan Dowse.Pp
128b35f050eSAndrew Thompson.
129b35f050eSAndrew Thompson.Fn usbd_transfer_setup
130b35f050eSAndrew ThompsonThis function will allocate memory for and initialise an array of USB
131b35f050eSAndrew Thompsontransfers and all required DMA memory.
132b35f050eSAndrew Thompson.
133b35f050eSAndrew ThompsonThis function can sleep or block waiting for resources to become
134b35f050eSAndrew Thompsonavailable.
135b35f050eSAndrew Thompson.Fa udev
136b35f050eSAndrew Thompsonis a pointer to "struct usb_device".
137b35f050eSAndrew Thompson.Fa ifaces
1387851d429SEdward Tomasz Napieralais an array of interface index numbers to use.
1397851d429SEdward Tomasz NapieralaSee "if_index".
140b35f050eSAndrew Thompson.Fa pxfer
141b35f050eSAndrew Thompsonis a pointer to an array of USB transfer pointers that are initialized
142b35f050eSAndrew Thompsonto NULL, and then pointed to allocated USB transfers.
143b35f050eSAndrew Thompson.Fa setup_start
144b35f050eSAndrew Thompsonis a pointer to an array of USB config structures.
145b35f050eSAndrew Thompson.Fa n_setup
146b35f050eSAndrew Thompsonis a number telling the USB system how many USB transfers should be
147b35f050eSAndrew Thompsonsetup.
148b35f050eSAndrew Thompson.Fa priv_sc
149b35f050eSAndrew Thompsonis the private softc pointer, which will be used to initialize
150b35f050eSAndrew Thompson"xfer->priv_sc".
151b35f050eSAndrew Thompson.Fa priv_mtx
152b35f050eSAndrew Thompsonis the private mutex protecting the transfer structure and the
1537851d429SEdward Tomasz Napieralasoftc.
1547851d429SEdward Tomasz NapieralaThis pointer is used to initialize "xfer->priv_mtx".
1557851d429SEdward Tomasz NapieralaThis function returns zero upon success.
1567851d429SEdward Tomasz NapieralaA non-zero return value indicates failure.
157b35f050eSAndrew Thompson.
1580a9a8c2eSIan Dowse.Pp
159b35f050eSAndrew Thompson.
160b35f050eSAndrew Thompson.Fn usbd_transfer_unsetup
161b35f050eSAndrew ThompsonThis function will release the given USB transfers and all allocated
162b35f050eSAndrew Thompsonresources associated with these USB transfers.
163b35f050eSAndrew Thompson.Fa pxfer
164b35f050eSAndrew Thompsonis a pointer to an array of USB transfer pointers, that may be NULL,
165b35f050eSAndrew Thompsonthat should be freed by the USB system.
166b35f050eSAndrew Thompson.Fa n_setup
167b35f050eSAndrew Thompsonis a number telling the USB system how many USB transfers should be
168b35f050eSAndrew Thompsonunsetup.
169b35f050eSAndrew Thompson.
170b35f050eSAndrew ThompsonThis function can sleep waiting for USB transfers to complete.
171b35f050eSAndrew Thompson.
172b35f050eSAndrew ThompsonThis function is NULL safe with regard to the USB transfer structure
173b35f050eSAndrew Thompsonpointer.
174b35f050eSAndrew Thompson.
175b35f050eSAndrew ThompsonIt is not allowed to call this function from the USB transfer
176b35f050eSAndrew Thompsoncallback.
177b35f050eSAndrew Thompson.
1780a9a8c2eSIan Dowse.Pp
179b35f050eSAndrew Thompson.
180b35f050eSAndrew Thompson.Fn usbd_transfer_start
181b35f050eSAndrew ThompsonThis function will start the USB transfer pointed to by
182b35f050eSAndrew Thompson.Fa xfer ,
183b35f050eSAndrew Thompsonif not already started.
184b35f050eSAndrew Thompson.
185b35f050eSAndrew ThompsonThis function is always non-blocking and must be called with the
186b35f050eSAndrew Thompsonso-called private USB mutex locked.
187b35f050eSAndrew Thompson.
188b35f050eSAndrew ThompsonThis function is NULL safe with regard to the USB transfer structure
189b35f050eSAndrew Thompsonpointer.
190b35f050eSAndrew Thompson.
1910a9a8c2eSIan Dowse.Pp
192b35f050eSAndrew Thompson.
193b35f050eSAndrew Thompson.Fn usbd_transfer_stop
194b35f050eSAndrew ThompsonThis function will stop the USB transfer pointed to by
195b35f050eSAndrew Thompson.Fa xfer ,
196b35f050eSAndrew Thompsonif not already stopped.
197b35f050eSAndrew Thompson.
198b35f050eSAndrew ThompsonThis function is always non-blocking and must be called with the
199b35f050eSAndrew Thompsonso-called private USB mutex locked.
200b35f050eSAndrew Thompson.
201b35f050eSAndrew ThompsonThis function can return before the USB callback has been called.
202b35f050eSAndrew Thompson.
203b35f050eSAndrew ThompsonThis function is NULL safe with regard to the USB transfer structure
204b35f050eSAndrew Thompsonpointer.
205b35f050eSAndrew Thompson.
206b35f050eSAndrew ThompsonIf the transfer was in progress, the callback will called with
207b35f050eSAndrew Thompson"USB_ST_ERROR" and "error = USB_ERR_CANCELLED".
208b35f050eSAndrew Thompson.
2090a9a8c2eSIan Dowse.Pp
210b35f050eSAndrew Thompson.
211b35f050eSAndrew Thompson.Fn usbd_transfer_drain
212b35f050eSAndrew ThompsonThis function will stop an USB transfer, if not already stopped and
213b35f050eSAndrew Thompsonwait for any additional USB hardware operations to complete.
214b35f050eSAndrew Thompson.
215b35f050eSAndrew ThompsonBuffers that are loaded into DMA using "usbd_xfer_set_frame_data()" can
216b35f050eSAndrew Thompsonsafely be freed after that this function has returned.
217b35f050eSAndrew Thompson.
218b35f050eSAndrew ThompsonThis function can block the caller and will not return before the USB
219b35f050eSAndrew Thompsoncallback has been called.
220b35f050eSAndrew Thompson.
221b35f050eSAndrew ThompsonThis function is NULL safe with regard to the USB transfer structure
222b35f050eSAndrew Thompsonpointer.
223b35f050eSAndrew Thompson.
224b35f050eSAndrew Thompson.Sh USB TRANSFER CALLBACK
225b35f050eSAndrew Thompson.
226b35f050eSAndrew ThompsonThe USB callback has three states.
227b35f050eSAndrew Thompson.
2287851d429SEdward Tomasz NapieralaUSB_ST_SETUP, USB_ST_TRANSFERRED and USB_ST_ERROR.
2297851d429SEdward Tomasz NapieralaUSB_ST_SETUP is the initial state.
230b35f050eSAndrew Thompson.
231b35f050eSAndrew ThompsonAfter the callback has been called with this state it will always be
232b35f050eSAndrew Thompsoncalled back at a later stage in one of the other two states.
233b35f050eSAndrew Thompson.
234b35f050eSAndrew ThompsonThe USB callback should not restart the USB transfer in case the error
235b35f050eSAndrew Thompsoncause is USB_ERR_CANCELLED.
236b35f050eSAndrew Thompson.
237b35f050eSAndrew ThompsonThe USB callback is protected from recursion.
238b35f050eSAndrew Thompson.
239b35f050eSAndrew ThompsonThat means one can start and stop whatever transfer from the callback
240b35f050eSAndrew Thompsonof another transfer one desires.
241b35f050eSAndrew Thompson.
242b35f050eSAndrew ThompsonAlso the transfer that is currently called back.
243b35f050eSAndrew Thompson.
244b35f050eSAndrew ThompsonRecursion is handled like this that when the callback that wants to
245b35f050eSAndrew Thompsonrecurse returns it is called one more time.
246b35f050eSAndrew Thompson.
247b35f050eSAndrew Thompson.
2480a9a8c2eSIan Dowse.Pp
249b35f050eSAndrew Thompson.
250b35f050eSAndrew Thompson.Fn usbd_transfer_submit
251b35f050eSAndrew ThompsonThis function should only be called from within the USB callback and
252b35f050eSAndrew Thompsonis used to start the USB hardware.
253b35f050eSAndrew Thompson.
254b35f050eSAndrew ThompsonAn USB transfer can have multiple frames consisting of one or more USB
255b35f050eSAndrew Thompsonpackets making up an I/O vector for all USB transfer types.
256b35f050eSAndrew Thompson.
257b35f050eSAndrew Thompson.Bd -literal -offset indent
258b35f050eSAndrew Thompsonvoid
259b35f050eSAndrew Thompsonusb_default_callback(struct usb_xfer *xfer, usb_error_t error)
260b35f050eSAndrew Thompson{
261b35f050eSAndrew Thompson	int actlen;
262b35f050eSAndrew Thompson
263b35f050eSAndrew Thompson	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
264b35f050eSAndrew Thompson
265b35f050eSAndrew Thompson	switch (USB_GET_STATE(xfer)) {
266b35f050eSAndrew Thompson	case USB_ST_SETUP:
267b35f050eSAndrew Thompson		/*
268b35f050eSAndrew Thompson		 * Setup xfer frame lengths/count and data
269b35f050eSAndrew Thompson		 */
270b35f050eSAndrew Thompson		usbd_transfer_submit(xfer);
271b35f050eSAndrew Thompson		break;
272b35f050eSAndrew Thompson
273b35f050eSAndrew Thompson	case USB_ST_TRANSFERRED:
274b35f050eSAndrew Thompson		/*
275b35f050eSAndrew Thompson		 * Read	usb frame data, if any.
276b35f050eSAndrew Thompson		 * "actlen" has the total length for all frames
2779ba47352SJoel Dahl		 * transferred.
278b35f050eSAndrew Thompson		 */
279b35f050eSAndrew Thompson		break;
280b35f050eSAndrew Thompson
281b35f050eSAndrew Thompson	default: /* Error */
282b35f050eSAndrew Thompson		/*
283b35f050eSAndrew Thompson		 * Print error message and clear stall
284b35f050eSAndrew Thompson		 * for example.
285b35f050eSAndrew Thompson		 */
286b35f050eSAndrew Thompson		break;
287b35f050eSAndrew Thompson	}
288b35f050eSAndrew Thompson	/*
289b35f050eSAndrew Thompson	 * Here it is safe to do something without the private
290b35f050eSAndrew Thompson	 * USB mutex locked.
291b35f050eSAndrew Thompson	 */
292b35f050eSAndrew Thompson	return;
2930a9a8c2eSIan Dowse}
2940a9a8c2eSIan Dowse.Ed
295b35f050eSAndrew Thompson.
296b35f050eSAndrew Thompson.Sh USB CONTROL TRANSFERS
297b35f050eSAndrew ThompsonAn USB control transfer has three parts.
298b35f050eSAndrew Thompson.
299b35f050eSAndrew ThompsonFirst the SETUP packet, then DATA packet(s) and then a STATUS
300b35f050eSAndrew Thompsonpacket.
301b35f050eSAndrew Thompson.
302b35f050eSAndrew ThompsonThe SETUP packet is always pointed to by frame 0 and the
303b35f050eSAndrew Thompsonlength is set by
304b35f050eSAndrew Thompson.Fn usbd_xfer_frame_len
305b35f050eSAndrew Thompsonalso if there should not be
3067851d429SEdward Tomasz Napieralasent any SETUP packet!
3077851d429SEdward Tomasz NapieralaIf an USB control transfer has no DATA stage,
308b35f050eSAndrew Thompsonthen the number of frames should be set to 1.
309b35f050eSAndrew Thompson.
310b35f050eSAndrew ThompsonElse the default number of frames is 2.
311b35f050eSAndrew Thompson.
312b35f050eSAndrew Thompson.Bd -literal -offset indent
313b35f050eSAndrew Thompson
314b35f050eSAndrew ThompsonExample1: SETUP + STATUS
315b35f050eSAndrew Thompson usbd_xfer_set_frames(xfer, 1);
316b35f050eSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, 8);
317b35f050eSAndrew Thompson usbd_transfer_submit(xfer);
318b35f050eSAndrew Thompson
319b35f050eSAndrew ThompsonExample2: SETUP + DATA + STATUS
320b35f050eSAndrew Thompson usbd_xfer_set_frames(xfer, 2);
321b35f050eSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, 8);
322b35f050eSAndrew Thompson usbd_xfer_set_frame_len(xfer, 1, 1);
323b35f050eSAndrew Thompson usbd_transfer_submit(xfer);
324b35f050eSAndrew Thompson
325b35f050eSAndrew ThompsonExample3: SETUP + DATA + STATUS - split
326b35f050eSAndrew Thompson1st callback:
327b35f050eSAndrew Thompson usbd_xfer_set_frames(xfer, 1);
328b35f050eSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, 8);
329b35f050eSAndrew Thompson usbd_transfer_submit(xfer);
330b35f050eSAndrew Thompson
331b35f050eSAndrew Thompson2nd callback:
332b35f050eSAndrew Thompson /* IMPORTANT: frbuffers[0] must still point at the setup packet! */
333b35f050eSAndrew Thompson usbd_xfer_set_frames(xfer, 2);
334b35f050eSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, 0);
335b35f050eSAndrew Thompson usbd_xfer_set_frame_len(xfer, 1, 1);
336b35f050eSAndrew Thompson usbd_transfer_submit(xfer);
337b35f050eSAndrew Thompson
338b35f050eSAndrew ThompsonExample4: SETUP + STATUS - split
339b35f050eSAndrew Thompson1st callback:
340b35f050eSAndrew Thompson usbd_xfer_set_frames(xfer, 1);
341b35f050eSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, 8);
342b35f050eSAndrew Thompson usbd_xfer_set_flag(xfer, USB_MANUAL_STATUS);
343b35f050eSAndrew Thompson usbd_transfer_submit(xfer);
344b35f050eSAndrew Thompson
345b35f050eSAndrew Thompson2nd callback:
346b35f050eSAndrew Thompson usbd_xfer_set_frames(xfer, 1);
347b35f050eSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, 0);
348b35f050eSAndrew Thompson usbd_xfer_clr_flag(xfer, USB_MANUAL_STATUS);
349b35f050eSAndrew Thompson usbd_transfer_submit(xfer);
350b35f050eSAndrew Thompson
351b35f050eSAndrew Thompson.Ed
352b35f050eSAndrew Thompson.Sh USB TRANSFER CONFIG
353*06bac0daSDamien BrokaTo simplify the search for endpoints the
354b35f050eSAndrew Thompson.Nm usb
355b35f050eSAndrew Thompsonmodule defines a USB config structure where it is possible to specify
356b35f050eSAndrew Thompsonthe characteristics of the wanted endpoint.
357b35f050eSAndrew Thompson.Bd -literal -offset indent
358b35f050eSAndrew Thompson
359b35f050eSAndrew Thompsonstruct usb_config {
360b35f050eSAndrew Thompson	bufsize,
361b35f050eSAndrew Thompson	callback
362b35f050eSAndrew Thompson	direction,
363b35f050eSAndrew Thompson	endpoint,
364b35f050eSAndrew Thompson	frames,
365b35f050eSAndrew Thompson	index flags,
366b35f050eSAndrew Thompson	interval,
367b35f050eSAndrew Thompson	timeout,
368b35f050eSAndrew Thompson	type,
3690a9a8c2eSIan Dowse};
370b35f050eSAndrew Thompson
3710a9a8c2eSIan Dowse.Ed
372b35f050eSAndrew Thompson.
3730a9a8c2eSIan Dowse.Pp
374b35f050eSAndrew Thompson.Fa type
375b35f050eSAndrew Thompsonfield selects the USB pipe type.
376b35f050eSAndrew Thompson.
377b35f050eSAndrew ThompsonValid values are: UE_INTERRUPT, UE_CONTROL, UE_BULK,
378b35f050eSAndrew ThompsonUE_ISOCHRONOUS.
379b35f050eSAndrew Thompson.
380b35f050eSAndrew ThompsonThe special value UE_BULK_INTR will select BULK and INTERRUPT pipes.
381b35f050eSAndrew Thompson.
382b35f050eSAndrew ThompsonThis field is mandatory.
383b35f050eSAndrew Thompson.
3840a9a8c2eSIan Dowse.Pp
385b35f050eSAndrew Thompson.Fa endpoint
386b35f050eSAndrew Thompsonfield selects the USB endpoint number.
387b35f050eSAndrew Thompson.
388b35f050eSAndrew ThompsonA value of 0xFF, "-1" or "UE_ADDR_ANY" will select the first matching
389b35f050eSAndrew Thompsonendpoint.
390b35f050eSAndrew Thompson.
391b35f050eSAndrew ThompsonThis field is mandatory.
392b35f050eSAndrew Thompson.
3930a9a8c2eSIan Dowse.Pp
394b35f050eSAndrew Thompson.Fa direction
395b35f050eSAndrew Thompsonfield selects the USB endpoint direction.
396b35f050eSAndrew Thompson.
397b35f050eSAndrew ThompsonA value of "UE_DIR_ANY" will select the first matching endpoint.
398b35f050eSAndrew Thompson.
399b35f050eSAndrew ThompsonElse valid values are: "UE_DIR_IN" and "UE_DIR_OUT".
400b35f050eSAndrew Thompson.
401b35f050eSAndrew Thompson"UE_DIR_IN" and "UE_DIR_OUT" can be binary OR'ed by "UE_DIR_SID" which
402b35f050eSAndrew Thompsonmeans that the direction will be swapped in case of
403b35f050eSAndrew ThompsonUSB_MODE_DEVICE.
404b35f050eSAndrew Thompson.
405b35f050eSAndrew ThompsonNote that "UE_DIR_IN" refers to the data transfer direction of the
406b35f050eSAndrew Thompson"IN" tokens and "UE_DIR_OUT" refers to the data transfer direction of
407b35f050eSAndrew Thompsonthe "OUT" tokens.
408b35f050eSAndrew Thompson.
409b35f050eSAndrew ThompsonThis field is mandatory.
410b35f050eSAndrew Thompson.
4110a9a8c2eSIan Dowse.Pp
412b35f050eSAndrew Thompson.Fa interval
413b35f050eSAndrew Thompsonfield selects the interrupt interval.
414b35f050eSAndrew Thompson.
415b35f050eSAndrew ThompsonThe value of this field is given in milliseconds and is independent of
416b35f050eSAndrew Thompsondevice speed.
417b35f050eSAndrew Thompson.
418b35f050eSAndrew ThompsonDepending on the endpoint type, this field has different meaning:
419e5254989SGlen Barber.Bl -tag -width "UE_ISOCHRONOUS"
420b35f050eSAndrew Thompson.It UE_INTERRUPT
421b35f050eSAndrew Thompson"0" use the default interrupt interval based on endpoint descriptor.
422b35f050eSAndrew Thompson"Else" use the given value for polling rate.
423b35f050eSAndrew Thompson.It UE_ISOCHRONOUS
4247851d429SEdward Tomasz Napierala"0" use default.
4257851d429SEdward Tomasz Napierala"Else" the value is ignored.
426b35f050eSAndrew Thompson.It UE_BULK
427b35f050eSAndrew Thompson.It UE_CONTROL
4287851d429SEdward Tomasz Napierala"0" no transfer pre-delay.
4297851d429SEdward Tomasz Napierala"Else" a delay as given by this field in
430b35f050eSAndrew Thompsonmilliseconds is inserted before the hardware is started when
431b35f050eSAndrew Thompson"usbd_transfer_submit()" is called.
4320a9a8c2eSIan Dowse.Pp
433b35f050eSAndrew ThompsonNOTE: The transfer timeout, if any, is started after that the
434b35f050eSAndrew Thompsonpre-delay has elapsed!
4350a9a8c2eSIan Dowse.El
436b35f050eSAndrew Thompson.
4370a9a8c2eSIan Dowse.Pp
4380a9a8c2eSIan Dowse.Fa timeout
4397851d429SEdward Tomasz Napieralafield, if non-zero, will set the transfer timeout in milliseconds.
4407851d429SEdward Tomasz NapieralaIf the "timeout" field is zero and the transfer type is ISOCHRONOUS a
441b35f050eSAndrew Thompsontimeout of 250ms will be used.
442b35f050eSAndrew Thompson.
4430a9a8c2eSIan Dowse.Pp
444b35f050eSAndrew Thompson.Fa frames
4457851d429SEdward Tomasz Napieralafield sets the maximum number of frames.
4467851d429SEdward Tomasz NapieralaIf zero is specified it will yield the following results:
447e5254989SGlen Barber.Bl -tag -width "UE_INTERRUPT"
448b35f050eSAndrew Thompson.It UE_BULK
449b35f050eSAndrew Thompsonxfer->nframes = 1;
450b35f050eSAndrew Thompson.It UE_INTERRUPT
451b35f050eSAndrew Thompsonxfer->nframes = 1;
452b35f050eSAndrew Thompson.It UE_CONTROL
453b35f050eSAndrew Thompsonxfer->nframes = 2;
454b35f050eSAndrew Thompson.It UE_ISOCHRONOUS
4557851d429SEdward Tomasz NapieralaNot allowed.
4567851d429SEdward Tomasz NapieralaWill cause an error.
4570a9a8c2eSIan Dowse.El
458b35f050eSAndrew Thompson.
4590a9a8c2eSIan Dowse.Pp
460b35f050eSAndrew Thompson.Fa ep_index
461b35f050eSAndrew Thompsonfield allows you to give a number, in case more endpoints match the
462b35f050eSAndrew Thompsondescription, that selects which matching "ep_index" should be used.
463b35f050eSAndrew Thompson.
464b35f050eSAndrew Thompson.Pp
465b35f050eSAndrew Thompson.Fa if_index
466b35f050eSAndrew Thompsonfield allows you to select which of the interface numbers in the
467b35f050eSAndrew Thompson"ifaces" array parameter passed to "usbd_transfer_setup" that should
468b35f050eSAndrew Thompsonbe used when setting up the given USB transfer.
469b35f050eSAndrew Thompson.
470b35f050eSAndrew Thompson.Pp
471b35f050eSAndrew Thompson.Fa flags
472b35f050eSAndrew Thompsonfield has type "struct usb_xfer_flags" and allows one to set initial
4737851d429SEdward Tomasz Napieralaflags an USB transfer.
4747851d429SEdward Tomasz NapieralaValid flags are:
475e5254989SGlen Barber.Bl -tag -width "force_short_xfer"
476b35f050eSAndrew Thompson.It force_short_xfer
4777851d429SEdward Tomasz NapieralaThis flag forces the last transmitted USB packet to be short.
4787851d429SEdward Tomasz NapieralaA short packet has a length of less than "xfer->max_packet_size", which
4797851d429SEdward Tomasz Napieraladerives from "wMaxPacketSize".
4807851d429SEdward Tomasz NapieralaThis flag can be changed during operation.
481b35f050eSAndrew Thompson.It short_xfer_ok
482b35f050eSAndrew ThompsonThis flag allows the received transfer length, "xfer->actlen" to be
4837851d429SEdward Tomasz Napieralaless than "xfer->sumlen" upon completion of a transfer.
4847851d429SEdward Tomasz NapieralaThis flag can be changed during operation.
485b35f050eSAndrew Thompson.It short_frames_ok
4867851d429SEdward Tomasz NapieralaThis flag allows the reception of multiple short USB frames.
4877851d429SEdward Tomasz NapieralaThis flag
488b35f050eSAndrew Thompsononly has effect for BULK and INTERRUPT endpoints and if the number of
4897851d429SEdward Tomasz Napieralaframes received is greater than 1.
4907851d429SEdward Tomasz NapieralaThis flag can be changed during operation.
491b35f050eSAndrew Thompson.It pipe_bof
492b35f050eSAndrew ThompsonThis flag causes a failing USB transfer to remain first in the PIPE
493b35f050eSAndrew Thompsonqueue except in the case of "xfer->error" equal to
4947851d429SEdward Tomasz Napierala"USB_ERR_CANCELLED".
4957851d429SEdward Tomasz NapieralaNo other USB transfers in the affected PIPE queue
496b35f050eSAndrew Thompsonwill be started until either:
49761c320f0SXin LI.Bl -tag -width "X"
498b35f050eSAndrew Thompson.It 1
499b35f050eSAndrew ThompsonThe failing USB transfer is stopped using "usbd_transfer_stop()".
500b35f050eSAndrew Thompson.It 2
501b35f050eSAndrew ThompsonThe failing USB transfer performs a successful transfer.
502b35f050eSAndrew Thompson.El
503b35f050eSAndrew ThompsonThe purpose of this flag is to avoid races when multiple transfers are
504b35f050eSAndrew Thompsonqueued for execution on an USB endpoint, and the first executing
505b35f050eSAndrew Thompsontransfer fails leading to the need for clearing of stall for
506b35f050eSAndrew Thompsonexample.
507b35f050eSAndrew Thompson.
508b35f050eSAndrew ThompsonIn this case this flag is used to prevent the following USB transfers
509b35f050eSAndrew Thompsonfrom being executed at the same time the clear-stall command is
510b35f050eSAndrew Thompsonexecuted on the USB control endpoint.
511b35f050eSAndrew Thompson.
512b35f050eSAndrew ThompsonThis flag can be changed during operation.
513b35f050eSAndrew Thompson.Pp
51473bbeaa5SGlen Barber"BOF" is short for "Block On Failure".
515b35f050eSAndrew Thompson.Pp
516b35f050eSAndrew ThompsonNOTE: This flag should be set on all BULK and INTERRUPT USB transfers
517b35f050eSAndrew Thompsonwhich use an endpoint that can be shared between userland and kernel.
518b35f050eSAndrew Thompson.
519b35f050eSAndrew Thompson.
520b35f050eSAndrew Thompson.It proxy_buffer
521b35f050eSAndrew ThompsonSetting this flag will cause that the total buffer size will be
522b35f050eSAndrew Thompsonrounded up to the nearest atomic hardware transfer size.
523b35f050eSAndrew Thompson.
524b35f050eSAndrew ThompsonThe maximum data length of any USB transfer is always stored in the
525b35f050eSAndrew Thompson"xfer->max_data_length".
526b35f050eSAndrew Thompson.
527b35f050eSAndrew ThompsonFor control transfers the USB kernel will allocate additional space
528b35f050eSAndrew Thompsonfor the 8-bytes of SETUP header.
529b35f050eSAndrew Thompson.
530b35f050eSAndrew ThompsonThese 8-bytes are not counted by the "xfer->max_data_length"
531b35f050eSAndrew Thompsonvariable.
532b35f050eSAndrew Thompson.
533b35f050eSAndrew ThompsonThis flag cannot be changed during operation.
534b35f050eSAndrew Thompson.
535b35f050eSAndrew Thompson.
536b35f050eSAndrew Thompson.It ext_buffer
537b35f050eSAndrew ThompsonSetting this flag will cause that no data buffer will be
538b35f050eSAndrew Thompsonallocated.
539b35f050eSAndrew Thompson.
540b35f050eSAndrew ThompsonInstead the USB client must supply a data buffer.
541b35f050eSAndrew Thompson.
542b35f050eSAndrew ThompsonThis flag cannot be changed during operation.
543b35f050eSAndrew Thompson.
544b35f050eSAndrew Thompson.
545b35f050eSAndrew Thompson.It manual_status
546b35f050eSAndrew ThompsonSetting this flag prevents an USB STATUS stage to be appended to the
547b35f050eSAndrew Thompsonend of the USB control transfer.
548b35f050eSAndrew Thompson.
549b35f050eSAndrew ThompsonIf no control data is transferred this flag must be cleared.
550b35f050eSAndrew Thompson.
551b35f050eSAndrew ThompsonElse an error will be returned to the USB callback.
552b35f050eSAndrew Thompson.
553b35f050eSAndrew ThompsonThis flag is mostly useful for the USB device side.
554b35f050eSAndrew Thompson.
555b35f050eSAndrew ThompsonThis flag can be changed during operation.
556b35f050eSAndrew Thompson.
557b35f050eSAndrew Thompson.
558b35f050eSAndrew Thompson.It no_pipe_ok
5597851d429SEdward Tomasz NapieralaSetting this flag causes the USB_ERR_NO_PIPE error to be ignored.
5607851d429SEdward Tomasz NapieralaThis flag cannot be changed during operation.
561b35f050eSAndrew Thompson.
562b35f050eSAndrew Thompson.
563b35f050eSAndrew Thompson.It stall_pipe
564e5254989SGlen Barber.Bl -tag -width "Device Side Mode"
565b35f050eSAndrew Thompson.It Device Side Mode
566b35f050eSAndrew ThompsonSetting this flag will cause STALL pids to be sent to the endpoint
567b35f050eSAndrew Thompsonbelonging to this transfer before the transfer is started.
568b35f050eSAndrew Thompson.
569b35f050eSAndrew ThompsonThe transfer is started at the moment the host issues a clear-stall
570b35f050eSAndrew Thompsoncommand on the STALL'ed endpoint.
571b35f050eSAndrew Thompson.
572b35f050eSAndrew ThompsonThis flag can be changed during operation.
573b35f050eSAndrew Thompson.It Host Side Mode
574b35f050eSAndrew ThompsonSetting this flag will cause a clear-stall control request to be
575b35f050eSAndrew Thompsonexecuted on the endpoint before the USB transfer is started.
576b35f050eSAndrew Thompson.El
577b35f050eSAndrew Thompson.Pp
578b35f050eSAndrew ThompsonIf this flag is changed outside the USB callback function you have to
579b35f050eSAndrew Thompsonuse the "usbd_xfer_set_stall()" and "usbd_transfer_clear_stall()"
580b35f050eSAndrew Thompsonfunctions! This flag is automatically cleared after that the stall or
581b35f050eSAndrew Thompsonclear stall has been executed.
582b35f050eSAndrew Thompson.
5831c497368SHans Petter Selasky.It pre_scale_frames
5841c497368SHans Petter SelaskyIf this flag is set the number of frames specified is assumed to give the buffering time in milliseconds instead of frames.
5851c497368SHans Petter SelaskyDuring transfer setup the frames field is pre scaled with the corresponding value for the endpoint and rounded to the nearest number of frames greater than zero.
5861c497368SHans Petter SelaskyThis option only has effect for ISOCHRONOUS transfers.
587b35f050eSAndrew Thompson.El
588b35f050eSAndrew Thompson.Pp
589b35f050eSAndrew Thompson.Fa bufsize
590b35f050eSAndrew Thompsonfield sets the total buffer size in bytes.
591b35f050eSAndrew Thompson.
592b35f050eSAndrew ThompsonIf this field is zero, "wMaxPacketSize" will be used, multiplied by
593b35f050eSAndrew Thompsonthe "frames" field if the transfer type is ISOCHRONOUS.
594b35f050eSAndrew Thompson.
595b35f050eSAndrew ThompsonThis is useful for setting up interrupt pipes.
596b35f050eSAndrew Thompson.
597b35f050eSAndrew ThompsonThis field is mandatory.
598b35f050eSAndrew Thompson.Pp
599b35f050eSAndrew ThompsonNOTE: For control transfers "bufsize" includes the length of the
600b35f050eSAndrew Thompsonrequest structure.
601b35f050eSAndrew Thompson.
602b35f050eSAndrew Thompson.Pp
603b35f050eSAndrew Thompson.Fa callback
6047851d429SEdward Tomasz Napieralapointer sets the USB callback.
6057851d429SEdward Tomasz NapieralaThis field is mandatory.
606b35f050eSAndrew Thompson.
607b35f050eSAndrew Thompson.
608b35f050eSAndrew Thompson.Sh USB LINUX COMPAT LAYER
609b35f050eSAndrew ThompsonThe
610b35f050eSAndrew Thompson.Nm usb
611b35f050eSAndrew Thompsonmodule supports the Linux USB API.
612b35f050eSAndrew Thompson.
613b35f050eSAndrew Thompson.
614b35f050eSAndrew Thompson.Sh SEE ALSO
615b35f050eSAndrew Thompson.Xr libusb 3 ,
616b35f050eSAndrew Thompson.Xr usb 4 ,
617b35f050eSAndrew Thompson.Xr usbconfig 8
618b35f050eSAndrew Thompson.Sh STANDARDS
619b35f050eSAndrew ThompsonThe
620b35f050eSAndrew Thompson.Nm usb
621b35f050eSAndrew Thompsonmodule complies with the USB 2.0 standard.
622b35f050eSAndrew Thompson.Sh HISTORY
623b35f050eSAndrew ThompsonThe
624b35f050eSAndrew Thompson.Nm usb
625b35f050eSAndrew Thompsonmodule has been inspired by the NetBSD USB stack initially written by
6267851d429SEdward Tomasz NapieralaLennart Augustsson.
6277851d429SEdward Tomasz NapieralaThe
628b35f050eSAndrew Thompson.Nm usb
629b35f050eSAndrew Thompsonmodule was written by
6308a7314fcSBaptiste Daroussin.An Hans Petter Selasky Aq Mt hselasky@FreeBSD.org .
631