usb 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. .TH USB 3
  2. .SH NAME
  3. usb \- USB Host Controller Interface
  4. .SH SYNOPSIS
  5. .nf
  6. .B bind -a #U /dev
  7. .PP
  8. .nf
  9. .BI /dev/usb m
  10. .BI /dev/usb m /new
  11. .BI /dev/usb m /port
  12. .BI /dev/usb m / n
  13. .BI /dev/usb m / n /ctl
  14. .BI /dev/usb m / n /status
  15. .BI /dev/usb m / n /setup
  16. .BI /dev/usb m / n /ep k\fLdata
  17. \&...
  18. .fi
  19. .SH DESCRIPTION
  20. The Universal Serial Bus is a complex yet popular bus
  21. for connecting devices, such as mice, keyboards, printers, scanners,
  22. and (eventually with USB 2) disks to a PC. It is
  23. a four-wire tree-shaped bus that provides both communication and (limited)
  24. power to devices. Branching points in the tree are provided by devices
  25. called
  26. .IR hubs .
  27. .PP
  28. Most PCs have a two-slot hub built in, accommodating two USB devices. To
  29. attach more devices, one or more hubs have to be plugged in to the USB
  30. slots of the PC. The topology of the network is a tree with at most
  31. 127 nodes, counting both internal and leaf nodes.
  32. .PP
  33. The USB bus is fully controlled by the host; all devices are polled.
  34. Hubs are passive in the sense that they do not poll the devices attached
  35. to them. The host polls those devices and the hubs merely route the
  36. messages.
  37. .PP
  38. Devices may be added to or removed from the bus at any time.
  39. When a device is attached, the host queries it to determine its type
  40. and its speed. The querying process is standardized. The first level
  41. of querying is the same for all devices, the next is somewhat specialized
  42. for particular classes of devices (such as mice, keyboards, or audio devices).
  43. Specialization continues as subclasses and subsubclasses are explored.
  44. .SS Discovery
  45. For each connected device there is a directory in
  46. .BI #U/usb n\fR.
  47. Reading
  48. .BI #U/usb n /*/status
  49. yields the state, class/subclass/proto, vendor-id and product-id of each device in the
  50. first line. The remaining lines give the state of each of the
  51. interfaces.
  52. .PP
  53. To find a mouse, for example, scan the status files for the line beginning with
  54. .IP
  55. .EX
  56. .B "Enabled 0x020103"
  57. .EE
  58. .PP
  59. A mouse belongs to class 3 (in the least significant byte),
  60. .IR "human interface device" ,
  61. subclass 1,
  62. .IR boot ,
  63. proto 2,
  64. .I mouse
  65. (proto 1 would be the keyboard).
  66. USB class, subclass and proto codes can be found on
  67. .BR www.usb.org .
  68. .SS Device Control
  69. The control interface for each device is
  70. .I "``endpoint 0''"
  71. and is named
  72. .BI #U/usb n /*/setup \fR.
  73. The control interface of the device is accessed by reading and writing
  74. this file.
  75. .PP
  76. There is a separate
  77. .I "control interface
  78. named
  79. .BI #U/usb n /*/ctl
  80. which is used to configure the USB device
  81. .IR driver .
  82. By writing to this
  83. file, driver endpoints can be created and configured for communication with a
  84. device's data streams. A mouse, for example, has one control interface
  85. and one data interface. By communicating with the control interface,
  86. one can find out the device type (i.e., `mouse'), power consumption, number
  87. on interfaces, etc.
  88. .IR Usbd (4)
  89. will extract all this information and, in verbose mode, print it.
  90. .PP
  91. By sending an `endpoint message' to the
  92. .I ctl
  93. file, new driver endpoints can be created. The syntax of these messages
  94. is
  95. .IP
  96. .B ep
  97. .I n
  98. .B ctl
  99. .I "mode maxpkt nbuf
  100. .PP
  101. or
  102. .IP
  103. .B ep
  104. .I n
  105. .B bulk
  106. .I "mode maxpkt nbuf
  107. .PP
  108. or
  109. .IP
  110. .B ep
  111. .I "n period mode maxpkt
  112. .PP
  113. or
  114. .IP
  115. .B ep
  116. .I "n period mode samplesize hz
  117. .PP
  118. There are four forms for, respectively, Control, Bulk, Interrupt and
  119. Isochronous traffic (see USB specs for what that means).
  120. In all forms,
  121. .I n
  122. is the endpoint to be configured, and
  123. .I mode
  124. is
  125. .B r
  126. for read only,
  127. .B w
  128. for write only, or
  129. .B rw
  130. for reading and writing.
  131. .I Maxpkt
  132. is the maximum packet size to be used (between 8 and 1023),
  133. and
  134. .I nbuf
  135. is the number of buffers to be allocated by the driver.
  136. .PP
  137. .I Period
  138. is the number of milliseconds between packets (iso) or polls (interrupt).
  139. This number is usually dictated by the device. It must be between 1 and 1000.
  140. The
  141. .I samplesize
  142. is the size in bytes of the data units making up packets, and
  143. .I hz
  144. is the number of data units transmitted or received per second.
  145. .PP
  146. The data rate for an isochronous channel is
  147. .IR hz × samplesize
  148. bytes per second, and the number of samples in a packet
  149. will be
  150. .RI ( period × hz )/1000,
  151. rounded up or down.
  152. Packets do not contain fractional samples.
  153. A 16-bit stereo 44.1 KHz audio stream will thus have 44100 4-byte samples
  154. per second, typically in a 1ms period. Ove a 10 ms period, this yields 9
  155. packets of 176 bytes followed by a 180-byte packet (the driver figures it
  156. out for you).
  157. .PP
  158. The mouse, which produces 3-byte samples, is configured with
  159. .BR "ep 1 ctl r 3 32" :
  160. endpoint 1 is configured for non-real-time read-only 3-byte messages
  161. and allows 32 of them to be outstanding.
  162. .PP
  163. A usb audio output device at 44.1 KHz, 2 channels, 16-bit samples, on endpoint
  164. 4 will be configured with
  165. .BR "ep 4 1 w 4 44100" .
  166. .PP
  167. If the configuration is successful, a file named
  168. .BI ep n data
  169. is created which can be read and/or written depending on
  170. configuration. Configuration is not allowed when the data endpoint
  171. is open.
  172. .SS Isochronous Streams
  173. Forward
  174. .I seek
  175. operations on isochronous endpoints
  176. can be used to start the I/O at a specific time.
  177. The usb status file provides information that can be used to map file
  178. offsets to points in time: For each endpoint, the status file produces a line
  179. of the form:
  180. .IP
  181. .B "4 0x000201 \f2nnn\fP bytes \f2nnn\fP blocks
  182. .PP
  183. The fields are, from left to right,
  184. endpoint number, class/subclass/proto (as a six-digit hex number with class in the
  185. least significant byte), number of bytes read/written, number of blocks read/written.
  186. .PP
  187. For isochronous devices only, an additional line is produced of the
  188. form:
  189. .IP
  190. .B "bufsize \f2s\fP buffered \f2b\fP offset \f2o\fP time \f2t\fP
  191. .PP
  192. .I S
  193. is the size of the DMA operations on the device (i.e., the minimum useful size
  194. for reads and writes),
  195. .I b
  196. is the number of bytes currently buffered for input or output, and
  197. .I o
  198. and
  199. .I t
  200. should be interpreted to mean that byte offset
  201. .I o
  202. was/will be reached at time
  203. .I t
  204. (nanoseconds since the epoch).
  205. .PP
  206. To play or record samples exactly at some predetermined time, use
  207. .I o
  208. and
  209. .I t
  210. with the sampling rate to calculate the offset to seek to.
  211. .PP
  212. The number of bytes buffered can also be obtained using
  213. .IR stat (2)
  214. on the endpoint file. See also
  215. .IR audio (3).
  216. .SH FILES
  217. .TF "#U/usb n /*/status"
  218. .TP
  219. .BI #U/usb n /port
  220. USB port status file; for each port, space separated: port number, hexadecimal port status, port status string
  221. .TP
  222. .BI #U/usb n /*/status
  223. USB device status file; class/subclass/proto, vendor-id and product-id are found in line one
  224. .TP
  225. .BI #U/usb n /*/ctl
  226. USB
  227. .I driver
  228. control file, used to create driver endpoints, control debugging, etc.
  229. .TP
  230. .BI #U/usb n /*/setup
  231. USB
  232. .I device
  233. control file, used to exchange messages with a device's control channel.
  234. .B setup
  235. may be viewed as a preconfigured
  236. .B ep0data
  237. file.
  238. .TP
  239. .BI #U/usb n /*/ep k data
  240. USB device data channel for the
  241. .IR k 'th
  242. configuration.
  243. .SH SOURCE
  244. .B /sys/src/9/pc/usb.h
  245. .br
  246. .B /sys/src/9/pc/devusb.c
  247. .br
  248. .B /sys/src/9/pc/usb[ou]hci.c
  249. .SH "SEE ALSO"
  250. .IR usb (4),
  251. .IR usbd (4),
  252. .IR plan9.ini (8)
  253. .SH BUGS
  254. EHCI USB 2 controllers are not yet supported.
  255. .PP
  256. The interface for configuring endpoints is at variance with the standard.
  257. .PP
  258. The notion that the endpoints themselves have a class and subclass
  259. is a distortion of the standard.
  260. It would be better to leave all handling of the notions of class to the
  261. user-level support programs, and remove it from the driver.
  262. .PP
  263. There may be a timing bug that makes disk accesses via UHCI much
  264. slower than necessary.