1
0

usbfs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. .TH USBFS 2
  2. .SH NAME
  3. usbreadbuf,
  4. usbfsadd,
  5. usbfsdel,
  6. usbdirread,
  7. usbfsinit,
  8. usbdirfs,
  9. usbfs \- USB device driver file system library
  10. .SH SYNOPSIS
  11. .EX
  12. .ta 8n +8n +8n +8n +8n +8n +8n
  13. #include <u.h>
  14. #include <libc.h>
  15. #include <thread.h>
  16. #include "../lib/usb.h"
  17. #include "../lib/usbfs.h"
  18. .sp 0.3v
  19. enum {
  20. Hdrsize = 128, /* plenty of room for headers */
  21. Msgsize = 8 * 1024,
  22. Bufsize = Hdrsize + Msgsize,
  23. Namesz = 40,
  24. Errmax = 128,
  25. ONONE = ~0, /* omode in Fid when not open */
  26. };
  27. .sp 0.3v
  28. struct Fid {
  29. int fid;
  30. Qid qid;
  31. int omode;
  32. Fid* next;
  33. void* aux;
  34. };
  35. .sp 0.3v
  36. struct Usbfs {
  37. char name[Namesz];
  38. uvlong qid;
  39. Dev* dev;
  40. void* aux;
  41. .sp 0.3v
  42. int (*walk)(Usbfs *fs, Fid *f, char *name);
  43. void (*clone)(Usbfs *fs, Fid *of, Fid *nf);
  44. void (*clunk)(Usbfs *fs, Fid *f);
  45. int (*open)(Usbfs *fs, Fid *f, int mode);
  46. long (*read)(Usbfs *fs, Fid *f,
  47. void *data, long count, vlong offset);
  48. long (*write)(Usbfs *fs, Fid*f,
  49. void *data, long count, vlong offset);
  50. int (*stat)(Usbfs *fs, Qid q, Dir *d);
  51. void (*end)(Usbfs *fs);
  52. };
  53. .sp 0.3v
  54. typedef int (*Dirgen)(Usbfs*, Qid, int, Dir*, void*);
  55. .sp 0.3v
  56. long usbreadbuf(void *data, long count,
  57. vlong offset, void *buf, long n);
  58. void usbfsadd(Usbfs *dfs);
  59. void usbfsdel(Usbfs *dfs);
  60. int usbdirread(Usbfs*f, Qid q, char *data, long cnt,
  61. vlong off, Dirgen gen, void *arg);
  62. void usbfsinit(char* srv, char *mnt, Usbfs *f, int flag);
  63. void usbfsdirdump(void);
  64. .sp 0.3v
  65. extern char Enotfound[], Etoosmall[], Eio[], Eperm[], Ebadcall[],
  66. Ebadfid[], Einuse[], Eisopen[], Ebadctl[];
  67. .sp 0.3v
  68. extern Usbfs usbdirfs;
  69. extern int usbfsdebug;
  70. .EE
  71. .SH DESCRIPTION
  72. This library provides an alternative to
  73. .IR 9p (2)
  74. for implementing a file server within a USB driver.
  75. Drivers using this
  76. library may be embedded into
  77. .IR usbd (4).
  78. It may be also desirable to use this library when drivers are
  79. not embedded because it is tailored to work well with the
  80. library for handling USB devices.
  81. .PP
  82. A USB file system is described by a
  83. .I Usbfs
  84. structure.
  85. In most cases, the driver is not responsible for the root of the
  86. file tree.
  87. It is customary that a driver creates a file server
  88. for each device handled and links all of them to a root directory
  89. implemented by the
  90. .I usbdirfs
  91. file system implemented by the library.
  92. This root directory is bound to
  93. .B /dev
  94. in most cases.
  95. .PP
  96. .I Usbdirfs
  97. implements a root directory populated by named file trees,
  98. each one described by a
  99. .B Usbfs
  100. structure.
  101. .PP
  102. The field
  103. .B Usbfs.name
  104. contains the name for the root directory of the file system, usually
  105. a directory seen at
  106. .BI /dev/ name
  107. when the driver is embedded.
  108. .PP
  109. .B Usbfs.qid
  110. maintains a value used to decorate qids for the file tree.
  111. This may be ignored when
  112. .I usbdirfs
  113. is not used.
  114. Otherwise,
  115. .I usbdirfs
  116. assigns a unique value kept at the high 32 bits of
  117. .B Qid.path
  118. for all files on each file tree bound to it.
  119. Each
  120. .I Usbfs
  121. server must bitwise OR
  122. .B Usbfs.qid
  123. to all
  124. .B Qid.path
  125. values returned by its functions.
  126. In the same way,
  127. functions usually clear bits in
  128. .B Usbfs.qid
  129. before processing
  130. .B Qid.path
  131. values supplied as input.
  132. .PP
  133. The USB device handled by a file tree is referenced from
  134. .B Usbfs.dev
  135. (and a reference must be counted for it).
  136. This permits the following functions to quickly locate the device of
  137. interest, and also permits releasing the device when
  138. no request is outstanding.
  139. .PP
  140. The field
  141. .B Usbfs.aux
  142. is for the device to use.
  143. The rest of the fields implement the 9P protocol for the device.
  144. Not all the operations need be implemented.
  145. Only
  146. .IR walk ,
  147. .IR open ,
  148. .IR read ,
  149. .IR write ,
  150. and
  151. .IR stat ,
  152. must be implemented (and their corresponding fields in
  153. .B Usbfs
  154. may never be
  155. .BR nil ).
  156. These functions must return -1 upon failure
  157. and set the error string to reflect the cause of a failure.
  158. .PP
  159. In all the functions, a 9P fid is represented by a
  160. .B Fid
  161. structure.
  162. It contains the 9P
  163. .IR fid ,
  164. the corresponding
  165. .IR qid ,
  166. and an auxiliary pointer for the driver to use.
  167. Open
  168. .IR fid s
  169. have a valid open mode in
  170. .I omode
  171. while others have
  172. .B ONONE
  173. to indicate that the
  174. .I fid
  175. is not open.
  176. The library takes care of which
  177. fids
  178. exist and which ones do not.
  179. .PP
  180. .I Walk
  181. must walk
  182. .I f
  183. to
  184. .I name
  185. (a single name, not a file path)
  186. in the supplied
  187. .IR fs .
  188. Its implementation should update the qid in
  189. .I f
  190. to reflect the walk.
  191. This function must bitwise OR any returned Qid with
  192. .B Usbfs.qid ,
  193. if
  194. .I usbdirfs
  195. is used.
  196. .PP
  197. .I Clone
  198. must clone fid
  199. .I of
  200. onto
  201. .I nf
  202. so that,
  203. upon successful completion,
  204. .I nf
  205. also refers to the file that
  206. .I f
  207. refers to.
  208. An implementation must update the Qid of the cloned
  209. fid.
  210. If this function is not supplied, the library copies the
  211. .I aux
  212. field to the cloned fid.
  213. .PP
  214. .I Clunk
  215. clunks
  216. .IR f .
  217. It usually releases data kept in the
  218. .I aux
  219. field, but may be
  220. set to
  221. .B nil
  222. otherwise.
  223. .PP
  224. .I Open
  225. prepares the fid
  226. .I f
  227. for I/O according to
  228. .IR mode .
  229. The open mode in the fid is updated by the library upon return.
  230. The library checks trivial cases like opening already-open fids.
  231. The implementation performs most permission checking.
  232. .PP
  233. .I Read
  234. reads up to
  235. .I count
  236. bytes into
  237. .I data
  238. starting at
  239. .I offset
  240. in the file referenced by
  241. .IR f .
  242. .I Write
  243. is the counterpart.
  244. To read from directories,
  245. the function
  246. .I usbdirread
  247. may be called.
  248. It returns the return value of
  249. .I read
  250. or -1.
  251. .I usbdirread
  252. calls
  253. .I gen
  254. to iterate through files as needed.
  255. The
  256. .B Dirgen
  257. function will be called with index values of 0
  258. and up to ask for the first file and following files.
  259. To read from data already in buffers, the function
  260. .I usbreadbuf
  261. may help.
  262. It must be given the arguments supplied
  263. by the user, plus the buffer and buffer size.
  264. .PP
  265. .I Stat
  266. must fill
  267. .I d
  268. with the directory entry for the file identified by
  269. .IR q.
  270. As an aid,
  271. .I d
  272. is initialized to fake access and modification times,
  273. and user and group ids.
  274. Also, the field
  275. .B name
  276. in
  277. .I d
  278. is initialized to point to a 40-byte buffer.
  279. If the file name fits,
  280. it may be copied directly into
  281. .B d->name
  282. without allocating memory for that purpose.
  283. Otherwise
  284. .B d->name
  285. must be initialized to point to static memory.
  286. .PP
  287. The function
  288. .I end
  289. is called upon termination of the file tree to
  290. release resources.
  291. .PP
  292. Calling
  293. .I usbfsinit
  294. starts a file server for
  295. .I f
  296. that mounts itself at
  297. .I mnt
  298. and posts
  299. .I srv
  300. at
  301. .IR srv (3).
  302. In most cases, the file system supplied is
  303. .IR usbdirfs .
  304. The
  305. .I flag
  306. is used for
  307. .IR mount
  308. (see
  309. .IR bind (2)).
  310. Once
  311. .I usbdirfs
  312. is started, calls to
  313. .IR usbfsadd
  314. add a file tree implemented by
  315. .I dfs
  316. to the root directory of
  317. .I usbdirfs
  318. and
  319. calls to
  320. .I usbfsdel
  321. remove that binding (and release resources including
  322. the reference to the USB device).
  323. .PP
  324. Various error strings are declared as an aid.
  325. The global
  326. .B usbfsdebug
  327. may be set to trigger diagnostics and protocol tracing.
  328. .SH EXAMPLE
  329. See
  330. .B /sys/src/cmd/usb/disk
  331. for an example driver that uses this library.
  332. Looking at an example is strongly suggested
  333. to see how reference counts for the USB device
  334. and the file system are handled.
  335. .SH SOURCE
  336. .B /sys/src/cmd/usb/lib
  337. .SH "SEE ALSO"
  338. .IR usb (2),
  339. .IR usb (3),
  340. .IR usb (4),
  341. .IR usbd (4)