xref: /netbsd-src/share/man/man4/ugen.4 (revision 64830a10b2911f42716d3537a82405dcb38e4643)
1.\" $NetBSD: ugen.4,v 1.39 2024/03/26 03:24:14 thorpej Exp $
2.\"
3.\" Copyright (c) 1999 The NetBSD Foundation, Inc.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to The NetBSD Foundation
7.\" by Lennart Augustsson.
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
12.\" 1. Redistributions of source code must retain the above copyright
13.\"    notice, this list of conditions and the following disclaimer.
14.\" 2. Redistributions in binary form must reproduce the above copyright
15.\"    notice, this list of conditions and the following disclaimer in the
16.\"    documentation and/or other materials provided with the distribution.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28.\" POSSIBILITY OF SUCH DAMAGE.
29.\"
30.Dd March 25, 2024
31.Dt UGEN 4
32.Os
33.Sh NAME
34.Nm ugen
35.Nd USB generic device support
36.Sh SYNOPSIS
37.Cd "ugen* at uhub? flags N"
38.Cd "ugen* at uhub? vendor V product P flags 1"
39.Cd "ugenif* at uhub? vendor V product P configuration C interface I"
40.Cd "ugenif* at uhub? vendor V product P configuration C interface I flags 1"
41.Sh DESCRIPTION
42The
43.Nm
44driver provides support for all USB devices that do not have
45a special driver.
46It supports access to all parts of the device,
47but not in a way that is as convenient as a special purpose driver.
48.Pp
49Normally the
50.Nm
51driver is used when no other driver attaches to a device.
52If
53.Dq flags 1
54is specified, the
55.Nm
56will instead attach with a very high priority and always be used.
57Together with the
58.Cd vendor
59and
60.Cd product
61locators this can be used to force the
62.Nm
63driver to be used for a certain
64device.
65.Pp
66The
67.Sq ugenif
68form of attachment can be used to
69.Dq steal
70only one interface from some device for use by the
71.Nm
72driver.
73Most likely you want to explicitly specify at least vendor,
74product and interface with this form,
75as otherwise the
76.Nm
77driver would capture all of your
78.Nm usb
79devices.
80If
81.Dq flags 1
82is specified, the
83.Sq ugenif
84form will match at the lowest priority, thus allowing it to match only
85otherwise unclaimed interfaces.
86.Em NOTE :
87You have to be extremely careful,
88when using this form,
89as the attached
90.Nm
91driver has access to all of the device
92and can easily interfere with the driver(s) used for
93the other interface(s).
94.Pp
95As an example of this second form of attachment there are
96various debugging boards available based on some FTDI chip,
97where one interface is used for JTAG debugging
98and the other is used as a serial interface.
99In this case you want to attach the
100.Nm
101driver to interface 0 of this particular board identified by
102.Cd vendor
103and
104.Cd product
105while letting
106.Xr uftdi 4
107together with
108.Xr ucom 4
109to attach at interface 1.
110.Pp
111There can be up to 127 USB devices connected to a USB bus.
112Each USB device can have up to 16 endpoints.
113Each of these endpoints will communicate in one of four different
114modes: control, isochronous, bulk, or interrupt.
115Each of the endpoints will have a different device node.
116The four least significant bits in the minor device
117number determines which endpoint the device accesses and the rest
118of the bits determines which USB device.
119.Pp
120If an endpoint address is used both for input and output the device
121can be opened for both read or write.
122.Pp
123To find out what endpoints exist there are a series of
124.Xr ioctl 2
125operations on the control endpoint that return the USB descriptors
126of the device, configurations, interfaces, and endpoints.
127.Pp
128The control transfer mode can only happen on the control endpoint
129which is always endpoint 0.
130The control endpoint accepts requests
131and may respond with an answer to such requests.
132Control requests are issued by
133.Xr ioctl 2
134calls.
135.\" .Pp
136.\" The isochronous transfer mode can be in or out depending on the
137.\" endpoint.
138.\" To perform IO on an isochronous endpoint
139.\" .Xr read 2
140.\" and
141.\" .Xr write 2
142.\" should be used.
143.\" Before any IO operations can take place the transfer rate in
144.\" bytes/second has to be set.
145.\" This is done with
146.\" .Xr ioctl 2
147.\" .Dv USB_SET_ISO_RATE .
148.\" Performing this call sets up a buffer corresponding to
149.\" about 1 second of data.
150.Pp
151The bulk transfer mode can be in or out depending on the
152endpoint.
153To perform IO on a bulk endpoint
154.Xr read 2
155and
156.Xr write 2
157should be used.
158All IO operations on a bulk endpoint are normally unbuffered.
159The
160.Dv USB_SET_BULK_RA
161and
162.Dv USB_SET_BULK_WB
163.Xr ioctl 2
164calls enable read-ahead and write-behind buffering, respectively.
165This buffering supports fixed-sized USB transfers and is intended for
166devices with regular and continuing data transfers.
167When read-ahead or write-behind are enabled, the file descriptor
168may be set to use non-blocking IO.
169.Pp
170When in a read-ahead/writeback mode,
171.Xr select 2
172for read and write operates normally, returning true if there is data
173in the read buffer and space in the write buffer, respectively.
174When not,
175.Xr select 2
176always returns true, because there is no way to predict how the device
177will respond to a read or write request.
178.Pp
179The interrupt transfer mode can be in or out depending on the
180endpoint.
181To perform IO on an interrupt endpoint
182.Xr read 2
183and
184.Xr write 2
185should be used.
186A moderate amount of buffering is done
187by the driver.
188.Pp
189All endpoints handle the following
190.Xr ioctl 2
191calls:
192.Pp
193.Bl -tag -width indent -compact
194.It Dv USB_SET_SHORT_XFER ( int )
195Allow short read transfer.
196Normally a transfer from the device which is shorter than the
197request specified is reported as an error.
198.It Dv USB_SET_TIMEOUT ( int )
199Set the timeout on the device operations, the time is specified
200in milliseconds.
201The value 0 is used to indicate that there is no timeout.
202.El
203.Pp
204The control endpoint (endpoint 0) handles the following
205.Xr ioctl 2
206calls:
207.Pp
208.Bl -tag -width indent -compact
209.It Dv USB_GET_CONFIG ( int )
210Get the device configuration number.
211.It Dv USB_SET_CONFIG ( int )
212Set the device into the given configuration number.
213.Pp
214This operation can only be performed when the control endpoint
215is the sole open endpoint.
216.It Dv USB_GET_ALTINTERFACE ( struct usb_alt_interface )
217Get the alternative setting number for the interface with the given
218index.
219The
220.Dv config_index
221is ignored in this call.
222.Bd -literal
223struct usb_alt_interface {
224	int	uai_config_index;
225	int	uai_interface_index;
226	int	uai_alt_no;
227};
228.Ed
229.It Dv USB_SET_ALTINTERFACE ( struct usb_alt_interface )
230Set the alternative setting to the given number in the interface with the
231given index.
232The
233.Dv uai_config_index
234is ignored in this call.
235.Pp
236This operation can only be performed when no endpoints for the interface
237are open.
238.It Dv USB_GET_NO_ALT ( struct usb_alt_interface )
239Return the number of different alternate settings in the
240.Dv uai_alt_no
241field.
242.It Dv USB_GET_DEVICE_DESC ( usb_device_descriptor_t )
243Return the device descriptor.
244.It Dv USB_GET_CONFIG_DESC ( struct usb_config_desc )
245Return the descriptor for the configuration with the given index.
246For convenience the current configuration can be specified by
247.Dv USB_CURRENT_CONFIG_INDEX .
248.Bd -literal
249struct usb_config_desc {
250	int	ucd_config_index;
251	usb_config_descriptor_t ucd_desc;
252};
253.Ed
254.It Dv USB_GET_INTERFACE_DESC ( struct usb_interface_desc )
255Return the interface descriptor for an interface specified by its
256configuration index, interface index, and alternative index.
257For convenience the current alternative can be specified by
258.Dv USB_CURRENT_ALT_INDEX .
259.Bd -literal
260struct usb_interface_desc {
261	int	uid_config_index;
262	int	uid_interface_index;
263	int	uid_alt_index;
264	usb_interface_descriptor_t uid_desc;
265};
266.Ed
267.It Dv USB_GET_ENDPOINT_DESC ( struct usb_endpoint_desc )
268Return the endpoint descriptor for the endpoint specified by its
269configuration index, interface index, alternative index, and
270endpoint index.
271.Bd -literal
272struct usb_endpoint_desc {
273	int	ued_config_index;
274	int	ued_interface_index;
275	int	ued_alt_index;
276	int	ued_endpoint_index;
277	usb_endpoint_descriptor_t ued_desc;
278};
279.Ed
280.It Dv USB_GET_FULL_DESC ( struct usb_full_desc )
281Return all the descriptors for the given configuration.
282.Bd -literal
283struct usb_full_desc {
284	int	ufd_config_index;
285	u_int	ufd_size;
286	u_char	*ufd_data;
287};
288.Ed
289The
290.Dv ufd_data
291field should point to a memory area of the size given in the
292.Dv ufd_size
293field.
294The proper size can be determined by first issuing a
295.Dv USB_GET_CONFIG_DESC
296and inspecting the
297.Dv wTotalLength
298field.
299.It Dv USB_GET_STRING_DESC ( struct usb_string_desc )
300Get a string descriptor for the given language id and
301string index.
302.Bd -literal
303struct usb_string_desc {
304	int	usd_string_index;
305	int	usd_language_id;
306	usb_string_descriptor_t usd_desc;
307};
308.Ed
309.It Dv USB_DO_REQUEST
310Send a USB request to the device on the control endpoint.
311Any data sent to/from the device is located at
312.Dv data .
313The size of the transferred data is determined from the
314.Dv request .
315The
316.Dv ucr_addr
317field is ignored in this call.
318The
319.Dv ucr_flags
320field can be used to flag that the request is allowed to
321be shorter than the requested size, and the
322.Dv ucr_actlen
323field will contain the actual size on completion.
324.Bd -literal
325struct usb_ctl_request {
326	int	ucr_addr;
327	usb_device_request_t ucr_request;
328	void	*ucr_data;
329	int	ucr_flags;
330#define USBD_SHORT_XFER_OK	0x04	/* allow short reads */
331	int	ucr_actlen;		/* actual length transferred */
332};
333.Ed
334This is a dangerous operation in that it can perform arbitrary operations
335on the device.
336Some of the most dangerous (e.g., changing the device
337address) are not allowed.
338.It Dv USB_GET_DEVICEINFO ( struct usb_device_info )
339Get an information summary for the device.
340This call will not issue any USB transactions.
341.El
342.Pp
343Bulk endpoints handle the following
344.Xr ioctl 2
345calls:
346.Pp
347.Bl -tag -width indent -compact
348.It Dv USB_SET_BULK_RA ( int )
349Enable or disable bulk read-ahead.
350When enabled, the driver will begin to read data from the device into
351a buffer, and will perform reads from the device whenever there is
352room in the buffer.
353The
354.Xr read 2
355call will read data from this buffer, blocking if necessary until
356there is enough data to read the length of data requested.
357The buffer size and the read request length can be set by the
358.Dv USB_SET_BULK_RA_OPT
359.Xr ioctl 2
360call.
361.It Dv USB_SET_BULK_WB ( int )
362Enable or disable bulk write-behind.
363When enabled, the driver will buffer data from the
364.Xr write 2
365call before writing it to the device, enabling the
366.Xr write 2
367call to return immediately.
368.Xr write 2
369will block if there is not enough room in the buffer for all
370the data.
371The buffer size and the write request length can be set by the
372.Dv USB_SET_BULK_WB_OPT
373.Xr ioctl 2
374call.
375.It Dv USB_SET_BULK_RA_OPT ( struct usb_bulk_ra_wb_opt )
376Set the size of the buffer and the length of the read requests used by
377the driver when bulk read-ahead is enabled.
378The changes do not take
379effect until the next time bulk read-ahead is enabled.
380Read requests
381are made for the length specified, and the host controller driver
382(i.e.,
383.Xr ehci 4 ,
384.Xr ohci 4 ,
385and
386.Xr uhci 4 )
387will perform as many bus transfers as required.
388If transfers from the device should be smaller than the maximum length,
389.Dv ra_wb_request_size
390must be set to the required length.
391.Bd -literal
392struct usb_bulk_ra_wb_opt {
393	u_int	ra_wb_buffer_size;
394	u_int	ra_wb_request_size;
395};
396.Ed
397.It Dv USB_SET_BULK_WB_OPT ( struct usb_bulk_ra_wb_opt )
398Set the size of the buffer and the length of the write requests used
399by the driver when bulk write-behind is enabled.
400The changes do not
401take effect until the next time bulk write-behind is enabled.
402.El
403.Pp
404Note that there are two different ways of addressing configurations, interfaces,
405alternatives, and endpoints: by index or by number.
406The index is the ordinal number (starting from 0) of the descriptor
407as presented by the device.
408The number is the respective number of
409the entity as found in its descriptor.
410Enumeration of descriptors
411use the index, getting and setting typically uses numbers.
412.Pp
413Example:
414All endpoints (except the control endpoint) for the current configuration
415can be found by iterating the
416.Dv interface_index
417from 0 to
418.Dv config_desc->bNumInterface-1
419and for each of these iterating the
420.Dv endpoint_index
421from 0 to
422.Dv interface_desc->bNumEndpoints .
423The
424.Dv config_index
425should set to
426.Dv USB_CURRENT_CONFIG_INDEX
427and
428.Dv alt_index
429should be set to
430.Dv USB_CURRENT_ALT_INDEX .
431.Sh FILES
432.Bl -tag -width Pa
433.It Pa /dev/ugenN.EE
434Endpoint
435.Pa EE
436of device
437.Pa N .
438.El
439.Sh SEE ALSO
440.Xr usb 4
441.Sh HISTORY
442The
443.Nm
444driver
445appeared in
446.Nx 1.4 .
447.\" .Sh BUGS
448.\" The driver is not yet finished; there is no access to isochronous endpoints.
449