usb 6.5 KB

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