]> git.lizzy.rs Git - plan9front.git/blob - sys/man/3/usb
audiohda: fix syntax error
[plan9front.git] / sys / man / 3 / usb
1 .TH USB 3
2 .EQ
3 delim $$
4 .EN
5 .SH NAME
6 usb \- USB Host Controller Interface
7 .SH SYNOPSIS
8 .nf
9 .B bind -a #u /dev
10 .PP
11 .nf
12 .B /dev/usb
13 .B /dev/usb/ctl
14 .BI /dev/usb/ep N . M
15 .BI /dev/usb/ep N . M /data
16 .BI /dev/usb/ep N . M /ctl
17 \&...
18 .fi
19 .SH DESCRIPTION
20 The Universal Serial Bus is a complex yet popular bus
21 for connecting all kind of devices to a computer.
22 It is a tree-shaped bus that provides both communication and (limited)
23 power to devices.
24 Branching points in the tree are provided by devices called
25 .IR hubs .
26 Hubs provide ports where USB devices (also hubs) can be attached.
27 .PP
28 Most PCs have one or more USB controllers called
29 .I host
30 controllers.
31 Each one has a built-in hub called a
32 .I "root hub"
33 providing several ports.
34 In some cases, more hubs are built-in
35 and attached to a root hub port.
36 The topology of the network is a tree with at most
37 127 nodes, counting both internal and leaf nodes.
38 .PP
39 Host controllers come in four flavours:
40 UHCI and OHCI for USB 1 (up to 12 Mb/s),
41 EHCI for USB 2 (up to 480 Mb/s)
42 and
43 XHCI for USB 3 (up to 5 Gb/s).
44 .PP
45 The USB bus is fully controlled by the host; all devices are polled.
46 Hubs are passive in the sense that they do not poll the devices attached
47 to them.
48 The host polls those devices and the hubs merely route the messages.
49 .PP
50 Devices may be added to or removed from the bus at any time.
51 When a device is attached, the host queries it to determine its type and speed.
52 The querying process is standardized.
53 The first level of querying is the same for all devices,
54 the next is somewhat specialized
55 for particular classes of devices (such as mice, keyboards, or audio devices).
56 Specialization continues as subclasses and subsubclasses are explored.
57 .PP
58 Enumeration of the bus and initial configuration of devices is done
59 by a user-level program,
60 .IR usbd .
61 Device drivers are implemented by separate user programs, although
62 some of them may be statically linked into
63 .IR usbd .
64 .PP
65 The kernel device described in this page is responsible for providing
66 I/O for using the devices through so called
67 .IR endpoints .
68 Access to the host controller is hidden from user programs, which see
69 just a set of endpoints.
70 After system initialization, some endpoints
71 are created by the device to permit I/O to root hubs.
72 All other devices must be configured by
73 .IR usbd .
74 .SS Devices and Endpoints
75 A device includes one or more functions (e.g., audio output,
76 volume control buttons, mouse input, etc.)
77 Communication with device functions is performed
78 by some combination of
79 issuing control requests to, sending data to, and receiving data from
80 device
81 .IR endpoints .
82 Endpoints can be understood as addresses in the bus.
83 There are several types:
84 .TF "\fIIsochronous
85 .TP
86 .I Control
87 Their main use is to configure devices.
88 Writing a message with a specific format
89 (specified in the USB specification)
90 issues a request to the device.
91 If the request implies a reply,
92 a read can be made next to retrieve the requested data (if the write succeeded).
93 .TP
94 .I Interrupt
95 Used to send and receive messages to or from a specific device function
96 (e.g., to read events from a mouse).
97 .TP
98 .I Bulk
99 Used to send and receive larger amounts of data through streams
100 (e.g., to write blocks to a disk).
101 .TP
102 .I Isochronous
103 Used to send and receive data in a timely manner
104 (e.g., to write audio samples to a speaker).
105 .PD
106 .PP
107 All USB devices include at least
108 a control endpoint to perform device configuration.
109 This is called the
110 .I setup
111 endpoint or
112 .IR "endpoint zero" .
113 After configuring a device, other endpoints may be created
114 as dictated by the device to perform actual I/O.
115 .SS Operation
116 Bus enumeration and device configuration is performed by
117 .I usbd 
118 and not by this driver.
119 The driver provides an interface
120 to access existing endpoints (initially those for the built-in root hubs),
121 to create and configure other ones, and to perform I/O through them.
122 .PP
123 Each directory
124 .BI /dev/usb/ep N . M
125 represents an endpoint, where
126 .I N
127 is a number identifying a device and
128 .I M
129 is a number identifying one of its endpoints.
130 .PP
131 For each device attached to the bus, and configured by
132 .IR usbd ,
133 an endpoint zero (a
134 .I setup
135 endpoint)
136 is provided at
137 .BI /dev/usb/ep N .0
138 for configuring the device.
139 This is always a control endpoint and represents the device itself.
140 .PP
141 The device driver may use the setup endpoint
142 to issue control requests and perhaps to create more endpoints for the device.
143 Each new endpoint created has its own directory as said above.
144 For example, if the driver for the device
145 .BI /dev/usb/ep N . 0
146 creates the endpoint number 3 for that device, a directory
147 .BI /dev/usb/ep N .3
148 will be available to access that endpoint.
149 .PP
150 All endpoint directories contain two files:
151 .B data
152 and
153 .BR ctl .
154 The former has mode bit
155 .B DMEXCL
156 set and can be open by only one process at a time.
157 .SS data
158 .PP
159 The
160 .B data
161 file is used to perform actual I/O.
162 In general, reading from it retrieves
163 data from the endpoint and writing into it sends data to the endpoint.
164 For control endpoints, writing to this file issues a control request
165 (which may include data); if the request retrieves data from the device,
166 a following read on the file will provide such data.
167 .PP
168 USB errors reported by the endpoint upon I/O failures
169 are passed to the user process through the error string.
170 I/O stalls not resulting from an error, usually
171 an indication from the device, are reported by indicating that the
172 number of bytes transferred has been zero.
173 In most cases, the correct course of action after noticing the stall
174 is for the device driver to issue a `clear halt' request (see
175 .I unstall
176 in
177 .IR nusb (2))
178 to resume I/O.
179 The most common error is
180 .L crc/timeout
181 indicating problems in communication with the device (e.g., a physical
182 detach of the device or a wiring problem).
183 .PP
184 For control and isochronous transfers, there is an implicit
185 timeout performed by the kernel and it is not necessary for applications
186 to place their own timers.
187 For other transfer types, the kernel will not time out any operation
188 by default
189 (but see the
190 .L timeout
191 control request).
192 .SS "ctl and status"
193 .PP
194 The
195 .B ctl
196 file can be read to learn about the endpoint.
197 It contains information that can be used
198 to locate a particular device (or endpoint).
199 It also accepts writes with textual control requests described later.
200 .PP
201 This may result from the read of an endpoint control file:
202 .IP
203 .EX
204 .I "(the first line is wrapped to make it fit here)"
205 .ft L
206 enabled control rw speed full maxpkt 64 pollival 0
207         samplesz 0 hz 0 hub 1 port 3 busy
208 storage csp 0x500608 vid 0x951 did 0x1613 Kingston 'DT 101 II'
209 .ft
210 .EE
211 .LP
212 The first line contains status information.
213 The rest is information supplied by
214 .I usbd
215 as an aid to locate devices.
216 The status information includes:
217 .TF "\fREndpoint mode
218 .PD
219 .TP
220 Device state
221 One of
222 .BR config ,
223 .BR enabled ,
224 and
225 .BR detached .
226 An endpoint starts in the
227 .B config
228 state, and accepts control commands written to its
229 .B ctl
230 file to configure the endpoint.
231 When configured, the
232 state is
233 .B enabled
234 and the
235 .B data
236 file is used as described above (several control requests can still
237 be issued to its
238 .B ctl
239 file, but most will not be accepted from now on).
240 Upon severe errors, perhaps a physical
241 detachment from the bus, the endpoint enters the
242 .B detached
243 state and no further I/O is accepted on it.
244 Files for an endpoint (including its directory)
245 vanish when the device is detached and its files are no longer open.
246 Root hubs may not be detached.
247 .TP
248 Endpoint type
249 .BR control ,
250 .BR iso ,
251 .BR interrupt ,
252 or
253 .BR bulk ,
254 indicating the type of transfer supported by the endpoint.
255 .TP
256 Endpoint mode
257 One of
258 .BR r ,
259 .BR w ,
260 or
261 .BR rw ,
262 depending on the direction of the endpoint (in, out, or inout).
263 .TP
264 Speed
265 .BR low
266 (1.5 Mb/s),
267 .BR full
268 (12 Mb/s),
269 .BR high
270 (480 Mb/s)
271 or
272 .BR super
273 (5 Gb/s).
274 .TP
275 Maximum packet size
276 Used when performing I/O on the data file.
277 .TP
278 Polling interval
279 The polling period expressed as a number of µframes
280 (for high-speed endpoints) or frames (for low- and full-speed endpoints).
281 Note that a µframe takes 125 µs while a frame takes 1 ms.
282 This is only of relevance for interrupt and isochronous endpoints.
283 This value determines how often I/O happens.
284 Note that the control request adjusting the polling interval does
285 .I not
286 use these units, to make things easier for USB device drivers.
287 .TP
288 Sample size
289 Number of bytes per I/O sample (isochronous endpoints only).
290 .TP
291 Frequency
292 Number of samples per second (Hertz).
293 .TP
294 Hub address
295 Device address of the hub where the device is attached.
296 .TP
297 Port number
298 Port number (in the hub) where the device is attached.
299 .TP
300 Usage
301 .L busy
302 while the data file is open and
303 .L idle
304 otherwise.
305 This is useful to avoid disturbing endpoints already run
306 by a device driver.
307 .LP
308 The second line contains information describing the device:
309 .TF "\fRDevice strings
310 .PD
311 .TP
312 Class name
313 As provided by the device itself.
314 .TP
315 CSP
316 Class, Subclass, and Protocol for the device.
317 If the device contains different functions and has more CSPs,
318 all of them will be listed.
319 The first one is that of the device itself.
320 For example,
321 a mouse and keyboard combo may identify itself as a keyboard but
322 then include two CSPs, one for the keyboard and another one for the mouse.
323 .TP
324 Vid and Did
325 Vendor and device identifiers.
326 .TP
327 Device strings
328 Provided by the device and identifying the manufacturer and type of device.
329 .LP
330 For example, to find a mouse not yet in use by a driver, scan the
331 .B ctl
332 files for
333 .BR enabled ,
334 .BR idle ,
335 and
336 .BR "csp 0x020103" .
337 A mouse belongs to class 3 (in the least significant byte),
338 .IR "human interface device" ,
339 subclass 1,
340 .IR boot ,
341 protocol 2,
342 .I mouse
343 (protocol 1 would be the keyboard).
344 USB class, subclass and proto codes can be found at
345 .BR http://www.usb.org .
346 .SS Control requests
347 Endpoint control files accept the following requests.
348 In most cases
349 the driver does not issue them, leaving the task to either
350 .I usbd
351 or the usb driver library documented in
352 .IR nusb (2).
353 .TF "\fLsamplehz\fI n
354 .TP
355 .B detach
356 Prevent further I/O on the device (delete the endpoint)
357 and remove its file interface as soon as no process is using their files.
358 .TP
359 .BI maxpkt " n"
360 Set the maximum packet size to
361 .I n
362 bytes.
363 .TP
364 .BI pollival " n"
365 Only for interrupt and isochronous endpoints.
366 Set the polling interval as a function of the value
367 .I n
368 given by the endpoint descriptor.
369 The interval value used is the period
370 .I n
371 in bus time units for low- and full-speed interrupt endpoints.
372 Otherwise, the actual interval is
373 $2 sup n$
374 and not
375 .IR n .
376 Bus time units are 1 ms for low- and full-speed endpoints and 125 µs for
377 high-speed endpoints.
378 In most cases, the device driver may ignore
379 all this and issue the control request supplying the
380 polling interval value as found
381 in the endpoint descriptor.
382 The kernel adjusts the value according
383 to the endpoint configuration and converts it into the number of
384 frames or µframes between two consecutive polls.
385 .TP
386 .BI samplesz " n"
387 Use
388 .I n
389 as the number of bytes per sample.
390 .TP
391 .BI hz " n"
392 Use
393 .I n
394 as the number of samples per second.
395 .TP
396 .BI ntds " n"
397 Use
398 .I n
399 as the number of transactions per frame (or µframe), as reported
400 by the descriptor.
401 .TP
402 .BI uframes " n"
403 If
404 .I n
405 is set to 1 for an isochronous endpoint,
406 .IR read (2)
407 from the data file will not cross μframe boundaries.
408 .TP
409 .B clrhalt
410 Clear the halt condition for an endpoint.
411 Used to recover from a stall caused by a device to signal its driver
412 (usually due to an unknown request or a failure to complete one).
413 .TP
414 .BI info " string"
415 Replaces description information in
416 .B ctl
417 with
418 .IR string .
419 .I Usbd
420 uses this to add device descriptions.
421 .TP
422 .B address
423 Tell this driver that the device has been given an address,
424 which causes the device to enter the
425 .I enabled
426 state.
427 .TP
428 .BI name " str"
429 Generates an additional file name,
430 .IR str ,
431 for the
432 .B data
433 file of the endpoint.
434 This file name appears in the root directory of the
435 .L #u
436 tree.
437 For example, this is used by the audio device
438 driver to make the
439 .B data
440 file also available as
441 .BR /dev/audio .
442 .TP
443 .BI debug " n"
444 Enable debugging of the endpoint.
445 .I N
446 is an integer;
447 larger values make diagnostics more verbose.
448 .L 0
449 stops debugging diagnostics.
450 .L 1
451 causes just problem reports.
452 Bigger values report almost everything.
453 .TP
454 .BI timeout " n"
455 Enable time-outs for the endpoint.
456 Transfers are timed out by the kernel after
457 .I n
458 ms.
459 This should not be used for control and isochronous endpoints,
460 which are always timed out.
461 .PD
462 .LP
463 Setup endpoints
464 (those represented by
465 .BI ep N .0
466 directories)
467 also accept the following requests:
468 .TP
469 .BI new " n type mode"
470 Creates a new endpoint with number
471 .I n
472 of the given
473 .IR type
474 (\c
475 .BR ctl ,
476 .BR bulk ,
477 .BR intr ,
478 or
479 .BR iso ).
480 .I Mode
481 may be
482 .BR r ,
483 .BR w ,
484 or
485 .BR rw ,
486 which creates, respectively, an input, output, or input/output endpoint.
487 .TP
488 .B "speed {low|full|high|super}
489 Set the endpoint speed to full, low, high, or SuperSpeed, respectively.
490 .TP
491 .B hub
492 Tell this driver that the endpoint corresponds to a hub device.
493 .PD
494 .PP
495 Setup endpoints for hub devices also accept
496 .TP
497 .B "newdev {low|full|high|super} \fIport\fP
498 Create a new setup endpoint to represent a new device.
499 The first argument is the device speed.
500 .I Port
501 is the port number where the device is attached
502 (the hub is implied by the endpoint where the control request is issued).
503 .PD
504 .PP
505 The file
506 .B /dev/usb/ctl
507 provides all the information provided by the various
508 .B ctl
509 files when read.
510 It accepts several requests that refer to
511 the entire driver and not to particular endpoints:
512 .TF "\fLdebug \fIn"
513 .TP
514 .B "debug \fIn\fP
515 Sets the global debug flag to
516 .IR n .
517 .TP
518 .B dump
519 Dumps data structures for inspection.
520 .SH FILES
521 .TF #u/usb
522 .TP
523 .B #u/usb
524 root of the USB interface
525 .SH SOURCE
526 .B /sys/src/9/port/usb.h
527 .br
528 .B /sys/src/9/port/devusb.c
529 .br
530 .B /sys/src/9/*/usb*.c
531 .SH "SEE ALSO"
532 .IR nusb (2),
533 .IR nusb (4),
534 .IR plan9.ini (8)
535 .SH BUGS
536 USB controllers limit the speed of all their ports
537 to that of the slowest device connected to any one of them.
538 .PP
539 Isochronous input streams are not implemented for OHCI.
540 .PP
541 Some EHCI controllers drop completion interrupts and so must
542 be polled, which hurts throughput.