usb 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 bulk
  99. .I "mode maxpkt nbuf
  100. .PP
  101. or
  102. .IP
  103. .B ep
  104. .I "n period mode samplesize hz
  105. .PP
  106. The first form configures a non-real-time stream, the second an
  107. .I isochronous
  108. (real-time) stream.
  109. In both forms,
  110. .I n
  111. is the endpoint to be configured, and
  112. .I mode
  113. is
  114. .B r
  115. for read only,
  116. .B w
  117. for write only, or
  118. .B rw
  119. for reading and writing.
  120. In the first form,
  121. .I maxpkt
  122. is the maximum packet size to be used (between 8 and 1023),
  123. and
  124. .I nbuf
  125. is the number of buffers to be allocated by the driver.
  126. .PP
  127. In the second form,
  128. .I period
  129. is the number of milliseconds between packets. This number is usually
  130. dictated by the device. It must be between 1 and 1000.
  131. The
  132. .I samplesize
  133. is the size in bytes of the data units making up packets, and
  134. .I hz
  135. is the number of data units transmitted or received per second.
  136. .PP
  137. The data rate is thus
  138. .IR hz × samplesize
  139. bytes per second, and the number of samples in a packet
  140. will be
  141. .RI ( period × hz )/1000,
  142. rounded up or down.
  143. Packets do not contain fractional samples.
  144. .PP
  145. The mouse, which produces 3-byte samples, is configured with
  146. .BR "ep 1 bulk r 3 32" :
  147. endpoint 1 is configured for non-real-time read-only 3-byte messages
  148. and allows 32 of them to be outstanding.
  149. .PP
  150. A usb audio output device at 44.1 KHz, 2 channels, 16-bit samples, on endpoint
  151. 4 will be configured with
  152. .BR "ep 4 1 w 4 44100" .
  153. .PP
  154. If the configuration is successful, a file named
  155. .BI ep n data
  156. is created which can be read and/or written depending on
  157. configuration. Configuration is not allowed when the data endpoint
  158. is open.
  159. .SS Isochronous Streams
  160. Forward
  161. .I seek
  162. operations on isochronous endpoints
  163. can be used to start the I/O at a specific time.
  164. The usb status file provides information that can be used to map file
  165. offsets to points in time: For each endpoint, the status file produces a line
  166. of the form:
  167. .IP
  168. .B "4 0x000201 \f2nnn\fP bytes \f2nnn\fP blocks
  169. .PP
  170. The fields are, from left to right,
  171. endpoint number, class/subclass/proto (as a six-digit hex number with class in the
  172. least significant byte), number of bytes read/written, number of blocks read/written.
  173. .PP
  174. For isochronous devices only, an additional line is produced of the
  175. form:
  176. .IP
  177. .B "bufsize \f2s\fP buffered \f2b\fP offset \f2o\fP time \f2t\fP
  178. .PP
  179. .I S
  180. is the size of the DMA operations on the device (i.e., the minimum useful size
  181. for reads and writes),
  182. .I b
  183. is the number of bytes currently buffered for input or output, and
  184. .I o
  185. and
  186. .I t
  187. should be interpreted to mean that byte offset
  188. .I o
  189. was/will be reached at time
  190. .I t
  191. (nanoseconds since the epoch).
  192. .PP
  193. To play or record samples exactly at some predetermined time, use
  194. .I o
  195. and
  196. .I t
  197. with the sampling rate to calculate the offset to seek to.
  198. .PP
  199. The number of bytes buffered can also be obtained using
  200. .IR stat (2)
  201. on the endpoint file. See also
  202. .IR audio (3).
  203. .SH FILES
  204. .TF "#U/usb n /*/status"
  205. .TP
  206. .BI #U/usb n /port
  207. USB port status file; for each port, space separated: port number, hexadecimal port status, port status string
  208. .TP
  209. .BI #U/usb n /*/status
  210. USB device status file; class/subclass/proto, vendor-id and product-id are found in line one
  211. .TP
  212. .BI #U/usb n /*/ctl
  213. USB
  214. .I driver
  215. control file, used to create driver endpoints, control debugging, etc.
  216. .TP
  217. .BI #U/usb n /*/setup
  218. USB
  219. .I device
  220. control file, used to exchange messages with a device's control channel.
  221. .B setup
  222. may be viewed as a preconfigured
  223. .B ep0data
  224. file.
  225. .TP
  226. .BI #U/usb n /*/ep k data
  227. USB device data channel for the
  228. .IR k 'th
  229. configuration.
  230. .SH SOURCE
  231. .B /sys/src/9/pc/usb.h
  232. .br
  233. .B /sys/src/9/pc/devusb.c
  234. .br
  235. .B /sys/src/9/pc/usbuhci.c
  236. .SH "SEE ALSO"
  237. .IR usb (4),
  238. .IR usbd (4),
  239. .IR plan9.ini (8)
  240. .SH BUGS
  241. OpenHCI (OHCI) and EHCI USB controllers are not yet supported.
  242. .PP
  243. The interface for configuring endpoints is at variance with the standard.
  244. .PP
  245. The notion that the endpoints themselves have a class and subclass
  246. is a distortion of the standard.
  247. It would be better to leave all handling of the notions of class to the
  248. user-level support programs, and remove it from the driver.
  249. .PP
  250. There may be a timing bug that makes disk accesses via UHCI much
  251. slower than necessary.