xref: /freebsd/share/man/man9/usbdi.9 (revision 32a455e090f82853f4bbf610ce687740762a7f17)
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$
27*32a455e0SEdward Tomasz Napierala.Dd November 14, 2016
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
87*32a455e0SEdward Tomasz Napierala.Ft "usb_error_t"
88*32a455e0SEdward Tomasz Napierala.Fo "usbd_transfer_setup"
89*32a455e0SEdward Tomasz Napierala.Fa "struct usb_device *udev"
90*32a455e0SEdward Tomasz Napierala.Fa "const uint8_t *ifaces"
91*32a455e0SEdward Tomasz Napierala.Fa "struct usb_xfer **pxfer"
92*32a455e0SEdward Tomasz Napierala.Fa "const struct usb_config *setup_start"
93*32a455e0SEdward Tomasz Napierala.Fa "uint16_t n_setup"
94*32a455e0SEdward Tomasz Napierala.Fa "void *priv_sc"
95*32a455e0SEdward Tomasz Napierala.Fa "struct mtx *priv_mtx"
96*32a455e0SEdward Tomasz Napierala.Fc
97*32a455e0SEdward Tomasz Napierala.Ft "void"
98*32a455e0SEdward Tomasz Napierala.Fo "usbd_transfer_unsetup"
99*32a455e0SEdward Tomasz Napierala.Fa "struct usb_xfer **pxfer"
100*32a455e0SEdward Tomasz Napierala.Fa "uint16_t n_setup"
101*32a455e0SEdward Tomasz Napierala.Fc
102*32a455e0SEdward Tomasz Napierala.Ft "void"
103*32a455e0SEdward Tomasz Napierala.Fo "usbd_transfer_start"
104*32a455e0SEdward Tomasz Napierala.Fa "struct usb_xfer *xfer"
105*32a455e0SEdward Tomasz Napierala.Fc
106*32a455e0SEdward Tomasz Napierala.Ft "void"
107*32a455e0SEdward Tomasz Napierala.Fo "usbd_transfer_stop"
108*32a455e0SEdward Tomasz Napierala.Fa "struct usb_xfer *xfer"
109*32a455e0SEdward Tomasz Napierala.Fc
110*32a455e0SEdward Tomasz Napierala.Ft "void"
111*32a455e0SEdward Tomasz Napierala.Fo "usbd_transfer_drain"
112*32a455e0SEdward Tomasz Napierala.Fa "struct usb_xfer *xfer"
113*32a455e0SEdward Tomasz Napierala.Fc
1140a9a8c2eSIan Dowse.Sh DESCRIPTION
1150a9a8c2eSIan DowseThe Universal Serial Bus (USB) driver programming interface provides
1160a9a8c2eSIan DowseUSB peripheral drivers with a host controller independent API for
1170a9a8c2eSIan Dowsecontrolling and communicating with USB peripherals.
118b35f050eSAndrew ThompsonThe
119b35f050eSAndrew Thompson.Nm usb
120b35f050eSAndrew Thompsonmodule supports both USB Host and USB Device side mode.
121b35f050eSAndrew Thompson.Sh USB TRANSFER MANAGEMENT FUNCTIONS
122b35f050eSAndrew ThompsonThe USB standard defines four types of USB transfers.
123b35f050eSAndrew Thompson.
124b35f050eSAndrew ThompsonControl transfers, Bulk transfers, Interrupt transfers and Isochronous
125b35f050eSAndrew Thompsontransfers.
126b35f050eSAndrew Thompson.
127b35f050eSAndrew ThompsonAll the transfer types are managed using the following five functions:
128b35f050eSAndrew Thompson.
1290a9a8c2eSIan Dowse.Pp
130b35f050eSAndrew Thompson.
131b35f050eSAndrew Thompson.Fn usbd_transfer_setup
132b35f050eSAndrew ThompsonThis function will allocate memory for and initialise an array of USB
133b35f050eSAndrew Thompsontransfers and all required DMA memory.
134b35f050eSAndrew Thompson.
135b35f050eSAndrew ThompsonThis function can sleep or block waiting for resources to become
136b35f050eSAndrew Thompsonavailable.
137b35f050eSAndrew Thompson.Fa udev
138b35f050eSAndrew Thompsonis a pointer to "struct usb_device".
139b35f050eSAndrew Thompson.Fa ifaces
1407851d429SEdward Tomasz Napieralais an array of interface index numbers to use.
1417851d429SEdward Tomasz NapieralaSee "if_index".
142b35f050eSAndrew Thompson.Fa pxfer
143b35f050eSAndrew Thompsonis a pointer to an array of USB transfer pointers that are initialized
144b35f050eSAndrew Thompsonto NULL, and then pointed to allocated USB transfers.
145b35f050eSAndrew Thompson.Fa setup_start
146b35f050eSAndrew Thompsonis a pointer to an array of USB config structures.
147b35f050eSAndrew Thompson.Fa n_setup
148b35f050eSAndrew Thompsonis a number telling the USB system how many USB transfers should be
149b35f050eSAndrew Thompsonsetup.
150b35f050eSAndrew Thompson.Fa priv_sc
151b35f050eSAndrew Thompsonis the private softc pointer, which will be used to initialize
152b35f050eSAndrew Thompson"xfer->priv_sc".
153b35f050eSAndrew Thompson.Fa priv_mtx
154b35f050eSAndrew Thompsonis the private mutex protecting the transfer structure and the
1557851d429SEdward Tomasz Napieralasoftc.
1567851d429SEdward Tomasz NapieralaThis pointer is used to initialize "xfer->priv_mtx".
1577851d429SEdward Tomasz NapieralaThis function returns zero upon success.
1587851d429SEdward Tomasz NapieralaA non-zero return value indicates failure.
159b35f050eSAndrew Thompson.
1600a9a8c2eSIan Dowse.Pp
161b35f050eSAndrew Thompson.
162b35f050eSAndrew Thompson.Fn usbd_transfer_unsetup
163b35f050eSAndrew ThompsonThis function will release the given USB transfers and all allocated
164b35f050eSAndrew Thompsonresources associated with these USB transfers.
165b35f050eSAndrew Thompson.Fa pxfer
166b35f050eSAndrew Thompsonis a pointer to an array of USB transfer pointers, that may be NULL,
167b35f050eSAndrew Thompsonthat should be freed by the USB system.
168b35f050eSAndrew Thompson.Fa n_setup
169b35f050eSAndrew Thompsonis a number telling the USB system how many USB transfers should be
170b35f050eSAndrew Thompsonunsetup.
171b35f050eSAndrew Thompson.
172b35f050eSAndrew ThompsonThis function can sleep waiting for USB transfers to complete.
173b35f050eSAndrew Thompson.
174b35f050eSAndrew ThompsonThis function is NULL safe with regard to the USB transfer structure
175b35f050eSAndrew Thompsonpointer.
176b35f050eSAndrew Thompson.
177b35f050eSAndrew ThompsonIt is not allowed to call this function from the USB transfer
178b35f050eSAndrew Thompsoncallback.
179b35f050eSAndrew Thompson.
1800a9a8c2eSIan Dowse.Pp
181b35f050eSAndrew Thompson.
182b35f050eSAndrew Thompson.Fn usbd_transfer_start
183b35f050eSAndrew ThompsonThis function will start the USB transfer pointed to by
184b35f050eSAndrew Thompson.Fa xfer ,
185b35f050eSAndrew Thompsonif not already started.
186b35f050eSAndrew Thompson.
187b35f050eSAndrew ThompsonThis function is always non-blocking and must be called with the
188b35f050eSAndrew Thompsonso-called private USB mutex locked.
189b35f050eSAndrew Thompson.
190b35f050eSAndrew ThompsonThis function is NULL safe with regard to the USB transfer structure
191b35f050eSAndrew Thompsonpointer.
192b35f050eSAndrew Thompson.
1930a9a8c2eSIan Dowse.Pp
194b35f050eSAndrew Thompson.
195b35f050eSAndrew Thompson.Fn usbd_transfer_stop
196b35f050eSAndrew ThompsonThis function will stop the USB transfer pointed to by
197b35f050eSAndrew Thompson.Fa xfer ,
198b35f050eSAndrew Thompsonif not already stopped.
199b35f050eSAndrew Thompson.
200b35f050eSAndrew ThompsonThis function is always non-blocking and must be called with the
201b35f050eSAndrew Thompsonso-called private USB mutex locked.
202b35f050eSAndrew Thompson.
203b35f050eSAndrew ThompsonThis function can return before the USB callback has been called.
204b35f050eSAndrew Thompson.
205b35f050eSAndrew ThompsonThis function is NULL safe with regard to the USB transfer structure
206b35f050eSAndrew Thompsonpointer.
207b35f050eSAndrew Thompson.
208b35f050eSAndrew ThompsonIf the transfer was in progress, the callback will called with
209b35f050eSAndrew Thompson"USB_ST_ERROR" and "error = USB_ERR_CANCELLED".
210b35f050eSAndrew Thompson.
2110a9a8c2eSIan Dowse.Pp
212b35f050eSAndrew Thompson.
213b35f050eSAndrew Thompson.Fn usbd_transfer_drain
214b35f050eSAndrew ThompsonThis function will stop an USB transfer, if not already stopped and
215b35f050eSAndrew Thompsonwait for any additional USB hardware operations to complete.
216b35f050eSAndrew Thompson.
217b35f050eSAndrew ThompsonBuffers that are loaded into DMA using "usbd_xfer_set_frame_data()" can
218b35f050eSAndrew Thompsonsafely be freed after that this function has returned.
219b35f050eSAndrew Thompson.
220b35f050eSAndrew ThompsonThis function can block the caller and will not return before the USB
221b35f050eSAndrew Thompsoncallback has been called.
222b35f050eSAndrew Thompson.
223b35f050eSAndrew ThompsonThis function is NULL safe with regard to the USB transfer structure
224b35f050eSAndrew Thompsonpointer.
225b35f050eSAndrew Thompson.
226b35f050eSAndrew Thompson.Sh USB TRANSFER CALLBACK
227b35f050eSAndrew Thompson.
228b35f050eSAndrew ThompsonThe USB callback has three states.
229b35f050eSAndrew Thompson.
2307851d429SEdward Tomasz NapieralaUSB_ST_SETUP, USB_ST_TRANSFERRED and USB_ST_ERROR.
2317851d429SEdward Tomasz NapieralaUSB_ST_SETUP is the initial state.
232b35f050eSAndrew Thompson.
233b35f050eSAndrew ThompsonAfter the callback has been called with this state it will always be
234b35f050eSAndrew Thompsoncalled back at a later stage in one of the other two states.
235b35f050eSAndrew Thompson.
236b35f050eSAndrew ThompsonThe USB callback should not restart the USB transfer in case the error
237b35f050eSAndrew Thompsoncause is USB_ERR_CANCELLED.
238b35f050eSAndrew Thompson.
239b35f050eSAndrew ThompsonThe USB callback is protected from recursion.
240b35f050eSAndrew Thompson.
241b35f050eSAndrew ThompsonThat means one can start and stop whatever transfer from the callback
242b35f050eSAndrew Thompsonof another transfer one desires.
243b35f050eSAndrew Thompson.
244b35f050eSAndrew ThompsonAlso the transfer that is currently called back.
245b35f050eSAndrew Thompson.
246b35f050eSAndrew ThompsonRecursion is handled like this that when the callback that wants to
247b35f050eSAndrew Thompsonrecurse returns it is called one more time.
248b35f050eSAndrew Thompson.
249b35f050eSAndrew Thompson.
2500a9a8c2eSIan Dowse.Pp
251b35f050eSAndrew Thompson.
252b35f050eSAndrew Thompson.Fn usbd_transfer_submit
253b35f050eSAndrew ThompsonThis function should only be called from within the USB callback and
254b35f050eSAndrew Thompsonis used to start the USB hardware.
255b35f050eSAndrew Thompson.
256b35f050eSAndrew ThompsonAn USB transfer can have multiple frames consisting of one or more USB
257b35f050eSAndrew Thompsonpackets making up an I/O vector for all USB transfer types.
258b35f050eSAndrew Thompson.
259b35f050eSAndrew Thompson.Bd -literal -offset indent
260b35f050eSAndrew Thompsonvoid
261b35f050eSAndrew Thompsonusb_default_callback(struct usb_xfer *xfer, usb_error_t error)
262b35f050eSAndrew Thompson{
263b35f050eSAndrew Thompson	int actlen;
264b35f050eSAndrew Thompson
265b35f050eSAndrew Thompson	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
266b35f050eSAndrew Thompson
267b35f050eSAndrew Thompson	switch (USB_GET_STATE(xfer)) {
268b35f050eSAndrew Thompson	case USB_ST_SETUP:
269b35f050eSAndrew Thompson		/*
270b35f050eSAndrew Thompson		 * Setup xfer frame lengths/count and data
271b35f050eSAndrew Thompson		 */
272b35f050eSAndrew Thompson		usbd_transfer_submit(xfer);
273b35f050eSAndrew Thompson		break;
274b35f050eSAndrew Thompson
275b35f050eSAndrew Thompson	case USB_ST_TRANSFERRED:
276b35f050eSAndrew Thompson		/*
277b35f050eSAndrew Thompson		 * Read	usb frame data, if any.
278b35f050eSAndrew Thompson		 * "actlen" has the total length for all frames
2799ba47352SJoel Dahl		 * transferred.
280b35f050eSAndrew Thompson		 */
281b35f050eSAndrew Thompson		break;
282b35f050eSAndrew Thompson
283b35f050eSAndrew Thompson	default: /* Error */
284b35f050eSAndrew Thompson		/*
285b35f050eSAndrew Thompson		 * Print error message and clear stall
286b35f050eSAndrew Thompson		 * for example.
287b35f050eSAndrew Thompson		 */
288b35f050eSAndrew Thompson		break;
289b35f050eSAndrew Thompson	}
290b35f050eSAndrew Thompson	/*
291b35f050eSAndrew Thompson	 * Here it is safe to do something without the private
292b35f050eSAndrew Thompson	 * USB mutex locked.
293b35f050eSAndrew Thompson	 */
294b35f050eSAndrew Thompson	return;
2950a9a8c2eSIan Dowse}
2960a9a8c2eSIan Dowse.Ed
297b35f050eSAndrew Thompson.
298b35f050eSAndrew Thompson.Sh USB CONTROL TRANSFERS
299b35f050eSAndrew ThompsonAn USB control transfer has three parts.
300b35f050eSAndrew Thompson.
301b35f050eSAndrew ThompsonFirst the SETUP packet, then DATA packet(s) and then a STATUS
302b35f050eSAndrew Thompsonpacket.
303b35f050eSAndrew Thompson.
304b35f050eSAndrew ThompsonThe SETUP packet is always pointed to by frame 0 and the
305b35f050eSAndrew Thompsonlength is set by
306b35f050eSAndrew Thompson.Fn usbd_xfer_frame_len
307b35f050eSAndrew Thompsonalso if there should not be
3087851d429SEdward Tomasz Napieralasent any SETUP packet!
3097851d429SEdward Tomasz NapieralaIf an USB control transfer has no DATA stage,
310b35f050eSAndrew Thompsonthen the number of frames should be set to 1.
311b35f050eSAndrew Thompson.
312b35f050eSAndrew ThompsonElse the default number of frames is 2.
313b35f050eSAndrew Thompson.
314b35f050eSAndrew Thompson.Bd -literal -offset indent
315b35f050eSAndrew Thompson
316b35f050eSAndrew ThompsonExample1: SETUP + STATUS
317b35f050eSAndrew Thompson usbd_xfer_set_frames(xfer, 1);
318b35f050eSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, 8);
319b35f050eSAndrew Thompson usbd_transfer_submit(xfer);
320b35f050eSAndrew Thompson
321b35f050eSAndrew ThompsonExample2: SETUP + DATA + STATUS
322b35f050eSAndrew Thompson usbd_xfer_set_frames(xfer, 2);
323b35f050eSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, 8);
324b35f050eSAndrew Thompson usbd_xfer_set_frame_len(xfer, 1, 1);
325b35f050eSAndrew Thompson usbd_transfer_submit(xfer);
326b35f050eSAndrew Thompson
327b35f050eSAndrew ThompsonExample3: SETUP + DATA + STATUS - split
328b35f050eSAndrew Thompson1st callback:
329b35f050eSAndrew Thompson usbd_xfer_set_frames(xfer, 1);
330b35f050eSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, 8);
331b35f050eSAndrew Thompson usbd_transfer_submit(xfer);
332b35f050eSAndrew Thompson
333b35f050eSAndrew Thompson2nd callback:
334b35f050eSAndrew Thompson /* IMPORTANT: frbuffers[0] must still point at the setup packet! */
335b35f050eSAndrew Thompson usbd_xfer_set_frames(xfer, 2);
336b35f050eSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, 0);
337b35f050eSAndrew Thompson usbd_xfer_set_frame_len(xfer, 1, 1);
338b35f050eSAndrew Thompson usbd_transfer_submit(xfer);
339b35f050eSAndrew Thompson
340b35f050eSAndrew ThompsonExample4: SETUP + STATUS - split
341b35f050eSAndrew Thompson1st callback:
342b35f050eSAndrew Thompson usbd_xfer_set_frames(xfer, 1);
343b35f050eSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, 8);
344b35f050eSAndrew Thompson usbd_xfer_set_flag(xfer, USB_MANUAL_STATUS);
345b35f050eSAndrew Thompson usbd_transfer_submit(xfer);
346b35f050eSAndrew Thompson
347b35f050eSAndrew Thompson2nd callback:
348b35f050eSAndrew Thompson usbd_xfer_set_frames(xfer, 1);
349b35f050eSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, 0);
350b35f050eSAndrew Thompson usbd_xfer_clr_flag(xfer, USB_MANUAL_STATUS);
351b35f050eSAndrew Thompson usbd_transfer_submit(xfer);
352b35f050eSAndrew Thompson
353b35f050eSAndrew Thompson.Ed
354b35f050eSAndrew Thompson.Sh USB TRANSFER CONFIG
355b35f050eSAndrew ThompsonTo simply the search for endpoints the
356b35f050eSAndrew Thompson.Nm usb
357b35f050eSAndrew Thompsonmodule defines a USB config structure where it is possible to specify
358b35f050eSAndrew Thompsonthe characteristics of the wanted endpoint.
359b35f050eSAndrew Thompson.Bd -literal -offset indent
360b35f050eSAndrew Thompson
361b35f050eSAndrew Thompsonstruct usb_config {
362b35f050eSAndrew Thompson	bufsize,
363b35f050eSAndrew Thompson	callback
364b35f050eSAndrew Thompson	direction,
365b35f050eSAndrew Thompson	endpoint,
366b35f050eSAndrew Thompson	frames,
367b35f050eSAndrew Thompson	index flags,
368b35f050eSAndrew Thompson	interval,
369b35f050eSAndrew Thompson	timeout,
370b35f050eSAndrew Thompson	type,
3710a9a8c2eSIan Dowse};
372b35f050eSAndrew Thompson
3730a9a8c2eSIan Dowse.Ed
374b35f050eSAndrew Thompson.
3750a9a8c2eSIan Dowse.Pp
376b35f050eSAndrew Thompson.Fa type
377b35f050eSAndrew Thompsonfield selects the USB pipe type.
378b35f050eSAndrew Thompson.
379b35f050eSAndrew ThompsonValid values are: UE_INTERRUPT, UE_CONTROL, UE_BULK,
380b35f050eSAndrew ThompsonUE_ISOCHRONOUS.
381b35f050eSAndrew Thompson.
382b35f050eSAndrew ThompsonThe special value UE_BULK_INTR will select BULK and INTERRUPT pipes.
383b35f050eSAndrew Thompson.
384b35f050eSAndrew ThompsonThis field is mandatory.
385b35f050eSAndrew Thompson.
3860a9a8c2eSIan Dowse.Pp
387b35f050eSAndrew Thompson.Fa endpoint
388b35f050eSAndrew Thompsonfield selects the USB endpoint number.
389b35f050eSAndrew Thompson.
390b35f050eSAndrew ThompsonA value of 0xFF, "-1" or "UE_ADDR_ANY" will select the first matching
391b35f050eSAndrew Thompsonendpoint.
392b35f050eSAndrew Thompson.
393b35f050eSAndrew ThompsonThis field is mandatory.
394b35f050eSAndrew Thompson.
3950a9a8c2eSIan Dowse.Pp
396b35f050eSAndrew Thompson.Fa direction
397b35f050eSAndrew Thompsonfield selects the USB endpoint direction.
398b35f050eSAndrew Thompson.
399b35f050eSAndrew ThompsonA value of "UE_DIR_ANY" will select the first matching endpoint.
400b35f050eSAndrew Thompson.
401b35f050eSAndrew ThompsonElse valid values are: "UE_DIR_IN" and "UE_DIR_OUT".
402b35f050eSAndrew Thompson.
403b35f050eSAndrew Thompson"UE_DIR_IN" and "UE_DIR_OUT" can be binary OR'ed by "UE_DIR_SID" which
404b35f050eSAndrew Thompsonmeans that the direction will be swapped in case of
405b35f050eSAndrew ThompsonUSB_MODE_DEVICE.
406b35f050eSAndrew Thompson.
407b35f050eSAndrew ThompsonNote that "UE_DIR_IN" refers to the data transfer direction of the
408b35f050eSAndrew Thompson"IN" tokens and "UE_DIR_OUT" refers to the data transfer direction of
409b35f050eSAndrew Thompsonthe "OUT" tokens.
410b35f050eSAndrew Thompson.
411b35f050eSAndrew ThompsonThis field is mandatory.
412b35f050eSAndrew Thompson.
4130a9a8c2eSIan Dowse.Pp
414b35f050eSAndrew Thompson.Fa interval
415b35f050eSAndrew Thompsonfield selects the interrupt interval.
416b35f050eSAndrew Thompson.
417b35f050eSAndrew ThompsonThe value of this field is given in milliseconds and is independent of
418b35f050eSAndrew Thompsondevice speed.
419b35f050eSAndrew Thompson.
420b35f050eSAndrew ThompsonDepending on the endpoint type, this field has different meaning:
421e5254989SGlen Barber.Bl -tag -width "UE_ISOCHRONOUS"
422b35f050eSAndrew Thompson.It UE_INTERRUPT
423b35f050eSAndrew Thompson"0" use the default interrupt interval based on endpoint descriptor.
424b35f050eSAndrew Thompson"Else" use the given value for polling rate.
425b35f050eSAndrew Thompson.It UE_ISOCHRONOUS
4267851d429SEdward Tomasz Napierala"0" use default.
4277851d429SEdward Tomasz Napierala"Else" the value is ignored.
428b35f050eSAndrew Thompson.It UE_BULK
429b35f050eSAndrew Thompson.It UE_CONTROL
4307851d429SEdward Tomasz Napierala"0" no transfer pre-delay.
4317851d429SEdward Tomasz Napierala"Else" a delay as given by this field in
432b35f050eSAndrew Thompsonmilliseconds is inserted before the hardware is started when
433b35f050eSAndrew Thompson"usbd_transfer_submit()" is called.
4340a9a8c2eSIan Dowse.Pp
435b35f050eSAndrew ThompsonNOTE: The transfer timeout, if any, is started after that the
436b35f050eSAndrew Thompsonpre-delay has elapsed!
4370a9a8c2eSIan Dowse.El
438b35f050eSAndrew Thompson.
4390a9a8c2eSIan Dowse.Pp
4400a9a8c2eSIan Dowse.Fa timeout
4417851d429SEdward Tomasz Napieralafield, if non-zero, will set the transfer timeout in milliseconds.
4427851d429SEdward Tomasz NapieralaIf the "timeout" field is zero and the transfer type is ISOCHRONOUS a
443b35f050eSAndrew Thompsontimeout of 250ms will be used.
444b35f050eSAndrew Thompson.
4450a9a8c2eSIan Dowse.Pp
446b35f050eSAndrew Thompson.Fa frames
4477851d429SEdward Tomasz Napieralafield sets the maximum number of frames.
4487851d429SEdward Tomasz NapieralaIf zero is specified it will yield the following results:
449e5254989SGlen Barber.Bl -tag -width "UE_INTERRUPT"
450b35f050eSAndrew Thompson.It UE_BULK
451b35f050eSAndrew Thompsonxfer->nframes = 1;
452b35f050eSAndrew Thompson.It UE_INTERRUPT
453b35f050eSAndrew Thompsonxfer->nframes = 1;
454b35f050eSAndrew Thompson.It UE_CONTROL
455b35f050eSAndrew Thompsonxfer->nframes = 2;
456b35f050eSAndrew Thompson.It UE_ISOCHRONOUS
4577851d429SEdward Tomasz NapieralaNot allowed.
4587851d429SEdward Tomasz NapieralaWill cause an error.
4590a9a8c2eSIan Dowse.El
460b35f050eSAndrew Thompson.
4610a9a8c2eSIan Dowse.Pp
462b35f050eSAndrew Thompson.Fa ep_index
463b35f050eSAndrew Thompsonfield allows you to give a number, in case more endpoints match the
464b35f050eSAndrew Thompsondescription, that selects which matching "ep_index" should be used.
465b35f050eSAndrew Thompson.
466b35f050eSAndrew Thompson.Pp
467b35f050eSAndrew Thompson.Fa if_index
468b35f050eSAndrew Thompsonfield allows you to select which of the interface numbers in the
469b35f050eSAndrew Thompson"ifaces" array parameter passed to "usbd_transfer_setup" that should
470b35f050eSAndrew Thompsonbe used when setting up the given USB transfer.
471b35f050eSAndrew Thompson.
472b35f050eSAndrew Thompson.Pp
473b35f050eSAndrew Thompson.Fa flags
474b35f050eSAndrew Thompsonfield has type "struct usb_xfer_flags" and allows one to set initial
4757851d429SEdward Tomasz Napieralaflags an USB transfer.
4767851d429SEdward Tomasz NapieralaValid flags are:
477e5254989SGlen Barber.Bl -tag -width "force_short_xfer"
478b35f050eSAndrew Thompson.It force_short_xfer
4797851d429SEdward Tomasz NapieralaThis flag forces the last transmitted USB packet to be short.
4807851d429SEdward Tomasz NapieralaA short packet has a length of less than "xfer->max_packet_size", which
4817851d429SEdward Tomasz Napieraladerives from "wMaxPacketSize".
4827851d429SEdward Tomasz NapieralaThis flag can be changed during operation.
483b35f050eSAndrew Thompson.It short_xfer_ok
484b35f050eSAndrew ThompsonThis flag allows the received transfer length, "xfer->actlen" to be
4857851d429SEdward Tomasz Napieralaless than "xfer->sumlen" upon completion of a transfer.
4867851d429SEdward Tomasz NapieralaThis flag can be changed during operation.
487b35f050eSAndrew Thompson.It short_frames_ok
4887851d429SEdward Tomasz NapieralaThis flag allows the reception of multiple short USB frames.
4897851d429SEdward Tomasz NapieralaThis flag
490b35f050eSAndrew Thompsononly has effect for BULK and INTERRUPT endpoints and if the number of
4917851d429SEdward Tomasz Napieralaframes received is greater than 1.
4927851d429SEdward Tomasz NapieralaThis flag can be changed during operation.
493b35f050eSAndrew Thompson.It pipe_bof
494b35f050eSAndrew ThompsonThis flag causes a failing USB transfer to remain first in the PIPE
495b35f050eSAndrew Thompsonqueue except in the case of "xfer->error" equal to
4967851d429SEdward Tomasz Napierala"USB_ERR_CANCELLED".
4977851d429SEdward Tomasz NapieralaNo other USB transfers in the affected PIPE queue
498b35f050eSAndrew Thompsonwill be started until either:
49961c320f0SXin LI.Bl -tag -width "X"
500b35f050eSAndrew Thompson.It 1
501b35f050eSAndrew ThompsonThe failing USB transfer is stopped using "usbd_transfer_stop()".
502b35f050eSAndrew Thompson.It 2
503b35f050eSAndrew ThompsonThe failing USB transfer performs a successful transfer.
504b35f050eSAndrew Thompson.El
505b35f050eSAndrew ThompsonThe purpose of this flag is to avoid races when multiple transfers are
506b35f050eSAndrew Thompsonqueued for execution on an USB endpoint, and the first executing
507b35f050eSAndrew Thompsontransfer fails leading to the need for clearing of stall for
508b35f050eSAndrew Thompsonexample.
509b35f050eSAndrew Thompson.
510b35f050eSAndrew ThompsonIn this case this flag is used to prevent the following USB transfers
511b35f050eSAndrew Thompsonfrom being executed at the same time the clear-stall command is
512b35f050eSAndrew Thompsonexecuted on the USB control endpoint.
513b35f050eSAndrew Thompson.
514b35f050eSAndrew ThompsonThis flag can be changed during operation.
515b35f050eSAndrew Thompson.Pp
51673bbeaa5SGlen Barber"BOF" is short for "Block On Failure".
517b35f050eSAndrew Thompson.Pp
518b35f050eSAndrew ThompsonNOTE: This flag should be set on all BULK and INTERRUPT USB transfers
519b35f050eSAndrew Thompsonwhich use an endpoint that can be shared between userland and kernel.
520b35f050eSAndrew Thompson.
521b35f050eSAndrew Thompson.
522b35f050eSAndrew Thompson.It proxy_buffer
523b35f050eSAndrew ThompsonSetting this flag will cause that the total buffer size will be
524b35f050eSAndrew Thompsonrounded up to the nearest atomic hardware transfer size.
525b35f050eSAndrew Thompson.
526b35f050eSAndrew ThompsonThe maximum data length of any USB transfer is always stored in the
527b35f050eSAndrew Thompson"xfer->max_data_length".
528b35f050eSAndrew Thompson.
529b35f050eSAndrew ThompsonFor control transfers the USB kernel will allocate additional space
530b35f050eSAndrew Thompsonfor the 8-bytes of SETUP header.
531b35f050eSAndrew Thompson.
532b35f050eSAndrew ThompsonThese 8-bytes are not counted by the "xfer->max_data_length"
533b35f050eSAndrew Thompsonvariable.
534b35f050eSAndrew Thompson.
535b35f050eSAndrew ThompsonThis flag cannot be changed during operation.
536b35f050eSAndrew Thompson.
537b35f050eSAndrew Thompson.
538b35f050eSAndrew Thompson.It ext_buffer
539b35f050eSAndrew ThompsonSetting this flag will cause that no data buffer will be
540b35f050eSAndrew Thompsonallocated.
541b35f050eSAndrew Thompson.
542b35f050eSAndrew ThompsonInstead the USB client must supply a data buffer.
543b35f050eSAndrew Thompson.
544b35f050eSAndrew ThompsonThis flag cannot be changed during operation.
545b35f050eSAndrew Thompson.
546b35f050eSAndrew Thompson.
547b35f050eSAndrew Thompson.It manual_status
548b35f050eSAndrew ThompsonSetting this flag prevents an USB STATUS stage to be appended to the
549b35f050eSAndrew Thompsonend of the USB control transfer.
550b35f050eSAndrew Thompson.
551b35f050eSAndrew ThompsonIf no control data is transferred this flag must be cleared.
552b35f050eSAndrew Thompson.
553b35f050eSAndrew ThompsonElse an error will be returned to the USB callback.
554b35f050eSAndrew Thompson.
555b35f050eSAndrew ThompsonThis flag is mostly useful for the USB device side.
556b35f050eSAndrew Thompson.
557b35f050eSAndrew ThompsonThis flag can be changed during operation.
558b35f050eSAndrew Thompson.
559b35f050eSAndrew Thompson.
560b35f050eSAndrew Thompson.It no_pipe_ok
5617851d429SEdward Tomasz NapieralaSetting this flag causes the USB_ERR_NO_PIPE error to be ignored.
5627851d429SEdward Tomasz NapieralaThis flag cannot be changed during operation.
563b35f050eSAndrew Thompson.
564b35f050eSAndrew Thompson.
565b35f050eSAndrew Thompson.It stall_pipe
566e5254989SGlen Barber.Bl -tag -width "Device Side Mode"
567b35f050eSAndrew Thompson.It Device Side Mode
568b35f050eSAndrew ThompsonSetting this flag will cause STALL pids to be sent to the endpoint
569b35f050eSAndrew Thompsonbelonging to this transfer before the transfer is started.
570b35f050eSAndrew Thompson.
571b35f050eSAndrew ThompsonThe transfer is started at the moment the host issues a clear-stall
572b35f050eSAndrew Thompsoncommand on the STALL'ed endpoint.
573b35f050eSAndrew Thompson.
574b35f050eSAndrew ThompsonThis flag can be changed during operation.
575b35f050eSAndrew Thompson.It Host Side Mode
576b35f050eSAndrew ThompsonSetting this flag will cause a clear-stall control request to be
577b35f050eSAndrew Thompsonexecuted on the endpoint before the USB transfer is started.
578b35f050eSAndrew Thompson.El
579b35f050eSAndrew Thompson.Pp
580b35f050eSAndrew ThompsonIf this flag is changed outside the USB callback function you have to
581b35f050eSAndrew Thompsonuse the "usbd_xfer_set_stall()" and "usbd_transfer_clear_stall()"
582b35f050eSAndrew Thompsonfunctions! This flag is automatically cleared after that the stall or
583b35f050eSAndrew Thompsonclear stall has been executed.
584b35f050eSAndrew Thompson.
5851c497368SHans Petter Selasky.It pre_scale_frames
5861c497368SHans Petter SelaskyIf this flag is set the number of frames specified is assumed to give the buffering time in milliseconds instead of frames.
5871c497368SHans 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.
5881c497368SHans Petter SelaskyThis option only has effect for ISOCHRONOUS transfers.
589b35f050eSAndrew Thompson.El
590b35f050eSAndrew Thompson.Pp
591b35f050eSAndrew Thompson.Fa bufsize
592b35f050eSAndrew Thompsonfield sets the total buffer size in bytes.
593b35f050eSAndrew Thompson.
594b35f050eSAndrew ThompsonIf this field is zero, "wMaxPacketSize" will be used, multiplied by
595b35f050eSAndrew Thompsonthe "frames" field if the transfer type is ISOCHRONOUS.
596b35f050eSAndrew Thompson.
597b35f050eSAndrew ThompsonThis is useful for setting up interrupt pipes.
598b35f050eSAndrew Thompson.
599b35f050eSAndrew ThompsonThis field is mandatory.
600b35f050eSAndrew Thompson.Pp
601b35f050eSAndrew ThompsonNOTE: For control transfers "bufsize" includes the length of the
602b35f050eSAndrew Thompsonrequest structure.
603b35f050eSAndrew Thompson.
604b35f050eSAndrew Thompson.Pp
605b35f050eSAndrew Thompson.Fa callback
6067851d429SEdward Tomasz Napieralapointer sets the USB callback.
6077851d429SEdward Tomasz NapieralaThis field is mandatory.
608b35f050eSAndrew Thompson.
609b35f050eSAndrew Thompson.
610b35f050eSAndrew Thompson.Sh USB LINUX COMPAT LAYER
611b35f050eSAndrew ThompsonThe
612b35f050eSAndrew Thompson.Nm usb
613b35f050eSAndrew Thompsonmodule supports the Linux USB API.
614b35f050eSAndrew Thompson.
615b35f050eSAndrew Thompson.
616b35f050eSAndrew Thompson.Sh SEE ALSO
617b35f050eSAndrew Thompson.Xr libusb 3 ,
618b35f050eSAndrew Thompson.Xr usb 4 ,
619b35f050eSAndrew Thompson.Xr usbconfig 8
620b35f050eSAndrew Thompson.Sh STANDARDS
621b35f050eSAndrew ThompsonThe
622b35f050eSAndrew Thompson.Nm usb
623b35f050eSAndrew Thompsonmodule complies with the USB 2.0 standard.
624b35f050eSAndrew Thompson.Sh HISTORY
625b35f050eSAndrew ThompsonThe
626b35f050eSAndrew Thompson.Nm usb
627b35f050eSAndrew Thompsonmodule has been inspired by the NetBSD USB stack initially written by
6287851d429SEdward Tomasz NapieralaLennart Augustsson.
6297851d429SEdward Tomasz NapieralaThe
630b35f050eSAndrew Thompson.Nm usb
631b35f050eSAndrew Thompsonmodule was written by
6328a7314fcSBaptiste Daroussin.An Hans Petter Selasky Aq Mt hselasky@FreeBSD.org .
633