usb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. .TH USB 2
  2. .SH NAME
  3. usbcmd,
  4. classname,
  5. closedev,
  6. configdev,
  7. devctl,
  8. finddevs,
  9. loaddevstr,
  10. matchdevcsp,
  11. opendev,
  12. opendevdata,
  13. openep,
  14. startdevs,
  15. unstall,
  16. class,
  17. subclass,
  18. proto,
  19. CSP \- USB device driver library
  20. .SH SYNOPSIS
  21. .EX
  22. .ta 8n +8n +8n +8n +8n +8n +8n
  23. #include <u.h>
  24. #include <libc.h>
  25. #include <thread.h>
  26. #include "../lib/usb.h"
  27. .sp 0.3v
  28. struct Dev {
  29. Ref;
  30. char* dir; /* path for the endpoint dir */
  31. int id; /* usb id for device or ep. number */
  32. int dfd; /* descriptor for the data file */
  33. int cfd; /* descriptor for the control file */
  34. int maxpkt; /* cached from usb description */
  35. Usbdev* usb; /* USB description */
  36. void* aux; /* for the device driver */
  37. void (*free)(void*); /* idem. to release aux */
  38. };
  39. .sp 0.3v
  40. struct Usbdev {
  41. ulong csp; /* USB class/subclass/proto */
  42. int vid; /* vendor id */
  43. int did; /* product (device) id */
  44. char* vendor;
  45. char* product;
  46. char* serial;
  47. int ls; /* low speed */
  48. int class; /* from descriptor */
  49. int nconf; /* from descriptor */
  50. Conf* conf[Nconf]; /* configurations */
  51. Ep* ep[Nep]; /* all endpoints in device */
  52. Desc* ddesc[Nddesc]; /* (raw) device specific descriptors */
  53. };
  54. .sp 0.3v
  55. struct Ep {
  56. uchar addr; /* endpt address */
  57. uchar dir; /* direction, Ein/Eout */
  58. uchar type; /* Econtrol, Eiso, Ebulk, Eintr */
  59. uchar isotype; /* Eunknown, Easync, Eadapt, Esync */
  60. int id;
  61. int maxpkt; /* max. packet size */
  62. Conf* conf; /* the endpoint belongs to */
  63. Iface* iface; /* the endpoint belongs to */
  64. };
  65. .sp 0.3v
  66. struct Altc {
  67. int attrib;
  68. int interval;
  69. void* aux; /* for the driver program */
  70. };
  71. .sp 0.3v
  72. struct Iface {
  73. int id; /* interface number */
  74. ulong csp; /* USB class/subclass/proto */
  75. Altc* altc[Naltc];
  76. Ep* ep[Nep];
  77. void* aux; /* for the driver program */
  78. };
  79. .sp 0.3v
  80. struct Conf {
  81. int cval; /* value for set configuration */
  82. int attrib;
  83. int milliamps; /* maximum power in this config. */
  84. Iface* iface[Niface]; /* up to 16 interfaces */
  85. };
  86. .sp 0.3v
  87. struct Desc {
  88. Conf* conf; /* where this descriptor was read */
  89. Iface* iface; /* last iface before desc in conf. */
  90. Ep* ep; /* last endpt before desc in conf. */
  91. Altc* altc; /* last alt.c. before desc in conf. */
  92. DDesc data; /* unparsed standard USB descriptor */
  93. };
  94. .sp 0.3v
  95. struct DDesc {
  96. uchar bLength;
  97. uchar bDescriptorType;
  98. uchar bbytes[1];
  99. /* extra bytes allocated here to keep the rest of it */
  100. };
  101. .sp 0.3v
  102. #define Class(csp) ((csp)&0xff)
  103. #define Subclass(csp) (((csp)>>8)&0xff)
  104. #define Proto(csp) (((csp)>>16)&0xff)
  105. #define CSP(c, s, p) ((c) | ((s)<<8) | ((p)<<16))
  106. #define GET2(p) ...
  107. #define PUT2(p,v) ...
  108. #define GET4(p) ...
  109. #define PUT4(p,v) ...
  110. #define dprint if(usbdebug)fprint
  111. #define ddprint if(usbdebug > 1)fprint
  112. .sp 0.3v
  113. int Ufmt(Fmt *f);
  114. char* classname(int c);
  115. void closedev(Dev *d);
  116. int configdev(Dev *d);
  117. int devctl(Dev *dev, char *fmt, ...);
  118. void* emallocz(ulong size, int zero);
  119. char* estrdup(char *s);
  120. int finddevs(int (*matchf)(char*,void*), void *farg, char** dirs, int ndirs);
  121. char* hexstr(void *a, int n);
  122. char* loaddevstr(Dev *d, int sid);
  123. int matchdevcsp(char *info, void *a);
  124. Dev* opendev(char *fn);
  125. int opendevdata(Dev *d, int mode);
  126. Dev* openep(Dev *d, int id);
  127. void startdevs(char *args, char *argv[], int argc,
  128. int (*mf)(char*,void*), void*ma, int (*df)(Dev*,int,char**));
  129. int unstall(Dev *dev, Dev *ep, int dir);
  130. int usbcmd(Dev *d, int type, int req,
  131. int value, int index, uchar *data, int count);
  132. .sp 0.3v
  133. extern int usbdebug; /* more messages for bigger values */
  134. .EE
  135. .SH DESCRIPTION
  136. This library provides convenience structures and functions to write
  137. USB device drivers.
  138. It is not intended for user programs using USB devices.
  139. See
  140. .IR usb (3)
  141. for a description of the interfaces provided for that purpose.
  142. For drivers that provide a file system and may be embedded into
  143. .IR usbd ,
  144. the library includes a file system implementation toolkit described in
  145. .IR usbfs (2).
  146. .PP
  147. Usb drivers rely on
  148. .IR usb (3)
  149. to perform I/O through USB as well as on
  150. .IR usbd (4)
  151. to perform the initial configuration for the device's setup endpoint.
  152. The rest of the work is up to the driver and is where this library may help.
  153. .PP
  154. In most cases, a driver locates the devices of interest and configures them
  155. by calling
  156. .I startdevs
  157. and
  158. then sets up additional endpoints as needed (by calling
  159. .IR openep )
  160. to finally perform I/O by reading and writing the
  161. data files for the endpoints.
  162. .PP
  163. An endpoint as provided by
  164. .IR usb (3)
  165. is represented by a
  166. .B Dev
  167. data structure.
  168. The setup endpoint for a
  169. device represents the USB device, because it is the means to
  170. configure and operate the device.
  171. This structure is reference counted.
  172. Functions creating
  173. .B Devs
  174. adjust the number of references to one, initially.
  175. The driver is free to call
  176. .IR incref
  177. (in
  178. .IR lock (2))
  179. to add references and
  180. .I closedev
  181. to drop references (and release resources when the last one vanishes).
  182. As an aid to the driver, the field
  183. .B aux
  184. may keep driver-specific data and the function
  185. .B free
  186. will be called (if not null) to release the
  187. .B aux
  188. structure when the reference count goes down to zero.
  189. .PP
  190. .I Dev.dir
  191. holds the path for the endpoint's directory.
  192. .PP
  193. The field
  194. .B id
  195. keeps the device number for setup endpoints and the endpoint number
  196. for all other endpoints.
  197. For example, it would be
  198. .B 3
  199. for
  200. .B /dev/usb/ep3.0
  201. and
  202. .B 1
  203. for
  204. .BR /dev/usb/ep3.1 .
  205. It is easy to remember this because the former is created to operate
  206. on the device, while the later has been created as a particular endpoint
  207. to perform I/O.
  208. .PP
  209. Fields
  210. .B dfd
  211. and
  212. .B cfd
  213. keep the data and
  214. control file descriptors, respectively.
  215. When a
  216. .B Dev
  217. is created the control file is open, initially.
  218. Opening the data
  219. file requires calling
  220. .I opendevdata
  221. with the appropriate mode.
  222. .PP
  223. When the device configuration information has been loaded (see below),
  224. .B maxpkt
  225. holds the maximum packet size (in bytes) for the endpoint and
  226. .B usb
  227. keeps the rest of the USB information.
  228. .PP
  229. Most of the information in
  230. .B usb
  231. comes from parsing
  232. various device and configuration descriptors provided by the device,
  233. by calling one of the functions described later.
  234. Only descriptors unknown
  235. to the library are kept unparsed at
  236. .B usb.ddesc
  237. as an aid for the driver
  238. (which should know how to parse them and what to do with the information).
  239. .SS Configuration
  240. .I Startdevs
  241. is a wrapper that locates devices of interest, loads their configuration
  242. information, and starts a
  243. .IR thread (2)'s
  244. .I proc
  245. for each device located so that it executes
  246. .I f
  247. as its main entry point. The entry point is called with a pointer to
  248. the
  249. .B Dev
  250. for the device it has to process,
  251. .BR argc ,
  252. and
  253. .BR argv .
  254. Devices are located either from the arguments (after options) in
  255. .IR argv ,
  256. if any,
  257. or by calling the helper function
  258. .I mf
  259. with the argument
  260. .I ma
  261. to determine (for each device available) if the device belongs to
  262. the driver or not. If the function returns -1 then the device is not for us.
  263. .PP
  264. In many cases,
  265. .I matchdevcsp
  266. may be supplied as
  267. .I mf
  268. along with a (null terminated) vector of CSP values supplied as
  269. .IR ma .
  270. This function returns 0 for any device with a CSP matching one in the
  271. vector supplied as an argument and -1 otherwise.
  272. In other cases (eg., when a particular vendor and device ids are the
  273. ones identifying the device) the driver must include its own function
  274. and supply it as an argument to
  275. .IR startdevs .
  276. The first argument of the function corresponds to the information
  277. known about the device (the second line in its
  278. .B ctl
  279. file).
  280. .I Openep
  281. creates the endpoint number
  282. .I id
  283. for the device
  284. .I d
  285. and returns a
  286. .B Dev
  287. structure to operate on it (with just the control file open).
  288. .PP
  289. .I Opendev
  290. creates a
  291. .B Dev
  292. for the endpoint with directory
  293. .IR fn .
  294. Usually, the endpoint is a setup endpoint representing a device. The endpoint
  295. control file is open, but the data file is not. The USB description is void.
  296. In most cases drivers call
  297. .I startdevs
  298. and
  299. .I openep
  300. and do not call this function directly.
  301. .PP
  302. .I Configdev
  303. opens the data file for the device supplied and
  304. loads and parses its configuration information.
  305. After calling it, the device is ready for I/O and the USB description in
  306. .B Dev.usb
  307. is valid.
  308. When using
  309. .IR startdevs
  310. it is not desirable to call this function (because
  311. .IR startdevs
  312. already calls it).
  313. .PP
  314. Control requests for an endpoint may be written by calling
  315. .I devctl
  316. in the style of
  317. .IR print (2).
  318. It is better not to call
  319. .I print
  320. directly because the control request should be issued as a single
  321. .IR write (2).
  322. See
  323. .IR usb (3)
  324. for a list of available control requests (not to be confused with
  325. USB control transfers performed on a control endpoint).
  326. .SS Input/Output
  327. .I Opendevdata
  328. opens the data file for the device according to the given
  329. .IR mode .
  330. The mode must match that of the endpoint, doing otherwise is considered
  331. an error.
  332. Actual I/O is performed by reading/writing the descriptor kept in the
  333. .B dfd
  334. field of
  335. .BR Dev .
  336. .PP
  337. For control endpoints,
  338. it is not necessary to call
  339. .I read
  340. and
  341. .I write
  342. directly.
  343. Instead,
  344. .I usbcmd
  345. issues a USB control request to the device
  346. .I d
  347. (not to be confused with a
  348. .IR usb (3)
  349. control request sent to its control file).
  350. .I Usbcmd
  351. retries the control request several times upon failure because some devices
  352. require it.
  353. The format of requests is fixed per the USB standard:
  354. .I type
  355. is the type of request and
  356. .I req
  357. identifies the request. Arguments
  358. .I value
  359. and
  360. .I index
  361. are parameters to the request and the last two arguments,
  362. .I data
  363. and
  364. .IR count ,
  365. are similar to
  366. .I read
  367. and
  368. .I write
  369. arguments.
  370. However,
  371. .I data
  372. may be
  373. .B nil
  374. if no transfer (other than the control request) has to take place.
  375. The library header file includes numerous symbols defined to help writing
  376. the type and arguments for a request.
  377. .PP
  378. The return value from
  379. .I usbcmd
  380. is the number of bytes transferred, zero to indicate a stall and -1
  381. to indicate an error.
  382. .PP
  383. A common request is to unstall an endpoint that has been stalled
  384. due to some reason by the device (eg., when read or write indicate
  385. a count of zero bytes read or written on the endpoint). The function
  386. .I unstall
  387. does this.
  388. It is given the device that stalled the endpoint,
  389. .IR dev ,
  390. the
  391. stalled endpoint,
  392. .IR ep ,
  393. and the direction of the stall (one of
  394. .B Ein
  395. or
  396. .BR Eout ).
  397. The function takes care of notifying the device of the unstall as well
  398. as notifying the kernel.
  399. .SS Tools
  400. .I Class
  401. returns the class part of the number given, representing a CSP.
  402. .I Subclass
  403. does the same for the device subclass and
  404. .I Proto
  405. for the protocol.
  406. The counterpart is
  407. .IR CSP ,
  408. which builds a CSP from the device class, subclass, and protocol.
  409. For some classes,
  410. .I classname
  411. knows the name (for those with constants in the library header file).
  412. .PP
  413. The macros
  414. .I GET2
  415. and
  416. .I PUT2
  417. get and put a (little-endian) two-byte value and are useful to
  418. parse descriptors and replies for control requests.
  419. .PP
  420. Functions
  421. .I emallocz
  422. and
  423. .I estrdup
  424. are similar to
  425. .I mallocz
  426. and
  427. .I strdup
  428. but abort program operation upon failure.
  429. .PP
  430. The function
  431. .I Ufmt
  432. is a format routine suitable for
  433. .IR fmtinstall (2)
  434. to print a
  435. .B Dev
  436. data structure.
  437. The auxiliary
  438. .I hexstr
  439. returns a string representing a dump (in hexadecimal) of
  440. .I n
  441. bytes starting at
  442. .IR a .
  443. The string is allocated using
  444. .IR malloc (2)
  445. and memory must be released by the caller.
  446. .PP
  447. .I Loaddevstr
  448. returns the string obtained by reading the device string descriptor number
  449. .IR sid .
  450. .SH SOURCE
  451. .B /sys/src/cmd/usb/lib
  452. .SH "SEE ALSO"
  453. .IR usbfs (2),
  454. .IR usb (3),
  455. .IR usb (4),
  456. .IR usbd (4).
  457. .SH BUGS
  458. Not heavily exercised yet.