f_fs.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424
  1. /*
  2. * f_fs.c -- user mode file system API for USB composite function controllers
  3. *
  4. * Copyright (C) 2010 Samsung Electronics
  5. * Author: Michal Nazarewicz <m.nazarewicz@samsung.com>
  6. *
  7. * Based on inode.c (GadgetFS) which was:
  8. * Copyright (C) 2003-2004 David Brownell
  9. * Copyright (C) 2003 Agilent Technologies
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. */
  16. /* #define DEBUG */
  17. /* #define VERBOSE_DEBUG */
  18. #include <linux/blkdev.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/export.h>
  21. #include <asm/unaligned.h>
  22. #include <linux/usb/composite.h>
  23. #include <linux/usb/functionfs.h>
  24. #define FUNCTIONFS_MAGIC 0xa647361 /* Chosen by a honest dice roll ;) */
  25. /* Debugging ****************************************************************/
  26. #ifdef VERBOSE_DEBUG
  27. # define pr_vdebug pr_debug
  28. # define ffs_dump_mem(prefix, ptr, len) \
  29. print_hex_dump_bytes(pr_fmt(prefix ": "), DUMP_PREFIX_NONE, ptr, len)
  30. #else
  31. # define pr_vdebug(...) do { } while (0)
  32. # define ffs_dump_mem(prefix, ptr, len) do { } while (0)
  33. #endif /* VERBOSE_DEBUG */
  34. #define ENTER() pr_vdebug("%s()\n", __func__)
  35. /* The data structure and setup file ****************************************/
  36. enum ffs_state {
  37. /*
  38. * Waiting for descriptors and strings.
  39. *
  40. * In this state no open(2), read(2) or write(2) on epfiles
  41. * may succeed (which should not be the problem as there
  42. * should be no such files opened in the first place).
  43. */
  44. FFS_READ_DESCRIPTORS,
  45. FFS_READ_STRINGS,
  46. /*
  47. * We've got descriptors and strings. We are or have called
  48. * functionfs_ready_callback(). functionfs_bind() may have
  49. * been called but we don't know.
  50. *
  51. * This is the only state in which operations on epfiles may
  52. * succeed.
  53. */
  54. FFS_ACTIVE,
  55. /*
  56. * All endpoints have been closed. This state is also set if
  57. * we encounter an unrecoverable error. The only
  58. * unrecoverable error is situation when after reading strings
  59. * from user space we fail to initialise epfiles or
  60. * functionfs_ready_callback() returns with error (<0).
  61. *
  62. * In this state no open(2), read(2) or write(2) (both on ep0
  63. * as well as epfile) may succeed (at this point epfiles are
  64. * unlinked and all closed so this is not a problem; ep0 is
  65. * also closed but ep0 file exists and so open(2) on ep0 must
  66. * fail).
  67. */
  68. FFS_CLOSING
  69. };
  70. enum ffs_setup_state {
  71. /* There is no setup request pending. */
  72. FFS_NO_SETUP,
  73. /*
  74. * User has read events and there was a setup request event
  75. * there. The next read/write on ep0 will handle the
  76. * request.
  77. */
  78. FFS_SETUP_PENDING,
  79. /*
  80. * There was event pending but before user space handled it
  81. * some other event was introduced which canceled existing
  82. * setup. If this state is set read/write on ep0 return
  83. * -EIDRM. This state is only set when adding event.
  84. */
  85. FFS_SETUP_CANCELED
  86. };
  87. struct ffs_epfile;
  88. struct ffs_function;
  89. struct ffs_data {
  90. struct usb_gadget *gadget;
  91. /*
  92. * Protect access read/write operations, only one read/write
  93. * at a time. As a consequence protects ep0req and company.
  94. * While setup request is being processed (queued) this is
  95. * held.
  96. */
  97. struct mutex mutex;
  98. /*
  99. * Protect access to endpoint related structures (basically
  100. * usb_ep_queue(), usb_ep_dequeue(), etc. calls) except for
  101. * endpoint zero.
  102. */
  103. spinlock_t eps_lock;
  104. /*
  105. * XXX REVISIT do we need our own request? Since we are not
  106. * handling setup requests immediately user space may be so
  107. * slow that another setup will be sent to the gadget but this
  108. * time not to us but another function and then there could be
  109. * a race. Is that the case? Or maybe we can use cdev->req
  110. * after all, maybe we just need some spinlock for that?
  111. */
  112. struct usb_request *ep0req; /* P: mutex */
  113. struct completion ep0req_completion; /* P: mutex */
  114. int ep0req_status; /* P: mutex */
  115. /* reference counter */
  116. atomic_t ref;
  117. /* how many files are opened (EP0 and others) */
  118. atomic_t opened;
  119. /* EP0 state */
  120. enum ffs_state state;
  121. /*
  122. * Possible transitions:
  123. * + FFS_NO_SETUP -> FFS_SETUP_PENDING -- P: ev.waitq.lock
  124. * happens only in ep0 read which is P: mutex
  125. * + FFS_SETUP_PENDING -> FFS_NO_SETUP -- P: ev.waitq.lock
  126. * happens only in ep0 i/o which is P: mutex
  127. * + FFS_SETUP_PENDING -> FFS_SETUP_CANCELED -- P: ev.waitq.lock
  128. * + FFS_SETUP_CANCELED -> FFS_NO_SETUP -- cmpxchg
  129. */
  130. enum ffs_setup_state setup_state;
  131. #define FFS_SETUP_STATE(ffs) \
  132. ((enum ffs_setup_state)cmpxchg(&(ffs)->setup_state, \
  133. FFS_SETUP_CANCELED, FFS_NO_SETUP))
  134. /* Events & such. */
  135. struct {
  136. u8 types[4];
  137. unsigned short count;
  138. /* XXX REVISIT need to update it in some places, or do we? */
  139. unsigned short can_stall;
  140. struct usb_ctrlrequest setup;
  141. wait_queue_head_t waitq;
  142. } ev; /* the whole structure, P: ev.waitq.lock */
  143. /* Flags */
  144. unsigned long flags;
  145. #define FFS_FL_CALL_CLOSED_CALLBACK 0
  146. #define FFS_FL_BOUND 1
  147. /* Active function */
  148. struct ffs_function *func;
  149. /*
  150. * Device name, write once when file system is mounted.
  151. * Intended for user to read if she wants.
  152. */
  153. const char *dev_name;
  154. /* Private data for our user (ie. gadget). Managed by user. */
  155. void *private_data;
  156. /* filled by __ffs_data_got_descs() */
  157. /*
  158. * Real descriptors are 16 bytes after raw_descs (so you need
  159. * to skip 16 bytes (ie. ffs->raw_descs + 16) to get to the
  160. * first full speed descriptor). raw_descs_length and
  161. * raw_fs_descs_length do not have those 16 bytes added.
  162. */
  163. const void *raw_descs;
  164. unsigned raw_descs_length;
  165. unsigned raw_fs_descs_length;
  166. unsigned fs_descs_count;
  167. unsigned hs_descs_count;
  168. unsigned short strings_count;
  169. unsigned short interfaces_count;
  170. unsigned short eps_count;
  171. unsigned short _pad1;
  172. /* filled by __ffs_data_got_strings() */
  173. /* ids in stringtabs are set in functionfs_bind() */
  174. const void *raw_strings;
  175. struct usb_gadget_strings **stringtabs;
  176. /*
  177. * File system's super block, write once when file system is
  178. * mounted.
  179. */
  180. struct super_block *sb;
  181. /* File permissions, written once when fs is mounted */
  182. struct ffs_file_perms {
  183. umode_t mode;
  184. uid_t uid;
  185. gid_t gid;
  186. } file_perms;
  187. /*
  188. * The endpoint files, filled by ffs_epfiles_create(),
  189. * destroyed by ffs_epfiles_destroy().
  190. */
  191. struct ffs_epfile *epfiles;
  192. };
  193. /* Reference counter handling */
  194. static void ffs_data_get(struct ffs_data *ffs);
  195. static void ffs_data_put(struct ffs_data *ffs);
  196. /* Creates new ffs_data object. */
  197. static struct ffs_data *__must_check ffs_data_new(void) __attribute__((malloc));
  198. /* Opened counter handling. */
  199. static void ffs_data_opened(struct ffs_data *ffs);
  200. static void ffs_data_closed(struct ffs_data *ffs);
  201. /* Called with ffs->mutex held; take over ownership of data. */
  202. static int __must_check
  203. __ffs_data_got_descs(struct ffs_data *ffs, char *data, size_t len);
  204. static int __must_check
  205. __ffs_data_got_strings(struct ffs_data *ffs, char *data, size_t len);
  206. /* The function structure ***************************************************/
  207. struct ffs_ep;
  208. struct ffs_function {
  209. struct usb_configuration *conf;
  210. struct usb_gadget *gadget;
  211. struct ffs_data *ffs;
  212. struct ffs_ep *eps;
  213. u8 eps_revmap[16];
  214. short *interfaces_nums;
  215. struct usb_function function;
  216. };
  217. static struct ffs_function *ffs_func_from_usb(struct usb_function *f)
  218. {
  219. return container_of(f, struct ffs_function, function);
  220. }
  221. static void ffs_func_free(struct ffs_function *func);
  222. static void ffs_func_eps_disable(struct ffs_function *func);
  223. static int __must_check ffs_func_eps_enable(struct ffs_function *func);
  224. static int ffs_func_bind(struct usb_configuration *,
  225. struct usb_function *);
  226. static void ffs_func_unbind(struct usb_configuration *,
  227. struct usb_function *);
  228. static int ffs_func_set_alt(struct usb_function *, unsigned, unsigned);
  229. static void ffs_func_disable(struct usb_function *);
  230. static int ffs_func_setup(struct usb_function *,
  231. const struct usb_ctrlrequest *);
  232. static void ffs_func_suspend(struct usb_function *);
  233. static void ffs_func_resume(struct usb_function *);
  234. static int ffs_func_revmap_ep(struct ffs_function *func, u8 num);
  235. static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf);
  236. /* The endpoints structures *************************************************/
  237. struct ffs_ep {
  238. struct usb_ep *ep; /* P: ffs->eps_lock */
  239. struct usb_request *req; /* P: epfile->mutex */
  240. /* [0]: full speed, [1]: high speed */
  241. struct usb_endpoint_descriptor *descs[2];
  242. u8 num;
  243. int status; /* P: epfile->mutex */
  244. };
  245. struct ffs_epfile {
  246. /* Protects ep->ep and ep->req. */
  247. struct mutex mutex;
  248. wait_queue_head_t wait;
  249. struct ffs_data *ffs;
  250. struct ffs_ep *ep; /* P: ffs->eps_lock */
  251. struct dentry *dentry;
  252. char name[5];
  253. unsigned char in; /* P: ffs->eps_lock */
  254. unsigned char isoc; /* P: ffs->eps_lock */
  255. unsigned char _pad;
  256. };
  257. static int __must_check ffs_epfiles_create(struct ffs_data *ffs);
  258. static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count);
  259. static struct inode *__must_check
  260. ffs_sb_create_file(struct super_block *sb, const char *name, void *data,
  261. const struct file_operations *fops,
  262. struct dentry **dentry_p);
  263. /* Misc helper functions ****************************************************/
  264. static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
  265. __attribute__((warn_unused_result, nonnull));
  266. static char *ffs_prepare_buffer(const char * __user buf, size_t len)
  267. __attribute__((warn_unused_result, nonnull));
  268. /* Control file aka ep0 *****************************************************/
  269. static void ffs_ep0_complete(struct usb_ep *ep, struct usb_request *req)
  270. {
  271. struct ffs_data *ffs = req->context;
  272. complete_all(&ffs->ep0req_completion);
  273. }
  274. static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)
  275. {
  276. struct usb_request *req = ffs->ep0req;
  277. int ret;
  278. req->zero = len < le16_to_cpu(ffs->ev.setup.wLength);
  279. spin_unlock_irq(&ffs->ev.waitq.lock);
  280. req->buf = data;
  281. req->length = len;
  282. /*
  283. * UDC layer requires to provide a buffer even for ZLP, but should
  284. * not use it at all. Let's provide some poisoned pointer to catch
  285. * possible bug in the driver.
  286. */
  287. if (req->buf == NULL)
  288. req->buf = (void *)0xDEADBABE;
  289. INIT_COMPLETION(ffs->ep0req_completion);
  290. ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC);
  291. if (unlikely(ret < 0))
  292. return ret;
  293. ret = wait_for_completion_interruptible(&ffs->ep0req_completion);
  294. if (unlikely(ret)) {
  295. usb_ep_dequeue(ffs->gadget->ep0, req);
  296. return -EINTR;
  297. }
  298. ffs->setup_state = FFS_NO_SETUP;
  299. return ffs->ep0req_status;
  300. }
  301. static int __ffs_ep0_stall(struct ffs_data *ffs)
  302. {
  303. if (ffs->ev.can_stall) {
  304. pr_vdebug("ep0 stall\n");
  305. usb_ep_set_halt(ffs->gadget->ep0);
  306. ffs->setup_state = FFS_NO_SETUP;
  307. return -EL2HLT;
  308. } else {
  309. pr_debug("bogus ep0 stall!\n");
  310. return -ESRCH;
  311. }
  312. }
  313. static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
  314. size_t len, loff_t *ptr)
  315. {
  316. struct ffs_data *ffs = file->private_data;
  317. ssize_t ret;
  318. char *data;
  319. ENTER();
  320. /* Fast check if setup was canceled */
  321. if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED)
  322. return -EIDRM;
  323. /* Acquire mutex */
  324. ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
  325. if (unlikely(ret < 0))
  326. return ret;
  327. /* Check state */
  328. switch (ffs->state) {
  329. case FFS_READ_DESCRIPTORS:
  330. case FFS_READ_STRINGS:
  331. /* Copy data */
  332. if (unlikely(len < 16)) {
  333. ret = -EINVAL;
  334. break;
  335. }
  336. data = ffs_prepare_buffer(buf, len);
  337. if (IS_ERR(data)) {
  338. ret = PTR_ERR(data);
  339. break;
  340. }
  341. /* Handle data */
  342. if (ffs->state == FFS_READ_DESCRIPTORS) {
  343. pr_info("read descriptors\n");
  344. ret = __ffs_data_got_descs(ffs, data, len);
  345. if (unlikely(ret < 0))
  346. break;
  347. ffs->state = FFS_READ_STRINGS;
  348. ret = len;
  349. } else {
  350. pr_info("read strings\n");
  351. ret = __ffs_data_got_strings(ffs, data, len);
  352. if (unlikely(ret < 0))
  353. break;
  354. ret = ffs_epfiles_create(ffs);
  355. if (unlikely(ret)) {
  356. ffs->state = FFS_CLOSING;
  357. break;
  358. }
  359. ffs->state = FFS_ACTIVE;
  360. mutex_unlock(&ffs->mutex);
  361. ret = functionfs_ready_callback(ffs);
  362. if (unlikely(ret < 0)) {
  363. ffs->state = FFS_CLOSING;
  364. return ret;
  365. }
  366. set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags);
  367. return len;
  368. }
  369. break;
  370. case FFS_ACTIVE:
  371. data = NULL;
  372. /*
  373. * We're called from user space, we can use _irq
  374. * rather then _irqsave
  375. */
  376. spin_lock_irq(&ffs->ev.waitq.lock);
  377. switch (FFS_SETUP_STATE(ffs)) {
  378. case FFS_SETUP_CANCELED:
  379. ret = -EIDRM;
  380. goto done_spin;
  381. case FFS_NO_SETUP:
  382. ret = -ESRCH;
  383. goto done_spin;
  384. case FFS_SETUP_PENDING:
  385. break;
  386. }
  387. /* FFS_SETUP_PENDING */
  388. if (!(ffs->ev.setup.bRequestType & USB_DIR_IN)) {
  389. spin_unlock_irq(&ffs->ev.waitq.lock);
  390. ret = __ffs_ep0_stall(ffs);
  391. break;
  392. }
  393. /* FFS_SETUP_PENDING and not stall */
  394. len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
  395. spin_unlock_irq(&ffs->ev.waitq.lock);
  396. data = ffs_prepare_buffer(buf, len);
  397. if (IS_ERR(data)) {
  398. ret = PTR_ERR(data);
  399. break;
  400. }
  401. spin_lock_irq(&ffs->ev.waitq.lock);
  402. /*
  403. * We are guaranteed to be still in FFS_ACTIVE state
  404. * but the state of setup could have changed from
  405. * FFS_SETUP_PENDING to FFS_SETUP_CANCELED so we need
  406. * to check for that. If that happened we copied data
  407. * from user space in vain but it's unlikely.
  408. *
  409. * For sure we are not in FFS_NO_SETUP since this is
  410. * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP
  411. * transition can be performed and it's protected by
  412. * mutex.
  413. */
  414. if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
  415. ret = -EIDRM;
  416. done_spin:
  417. spin_unlock_irq(&ffs->ev.waitq.lock);
  418. } else {
  419. /* unlocks spinlock */
  420. ret = __ffs_ep0_queue_wait(ffs, data, len);
  421. }
  422. kfree(data);
  423. break;
  424. default:
  425. ret = -EBADFD;
  426. break;
  427. }
  428. mutex_unlock(&ffs->mutex);
  429. return ret;
  430. }
  431. static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf,
  432. size_t n)
  433. {
  434. /*
  435. * We are holding ffs->ev.waitq.lock and ffs->mutex and we need
  436. * to release them.
  437. */
  438. struct usb_functionfs_event events[n];
  439. unsigned i = 0;
  440. memset(events, 0, sizeof events);
  441. do {
  442. events[i].type = ffs->ev.types[i];
  443. if (events[i].type == FUNCTIONFS_SETUP) {
  444. events[i].u.setup = ffs->ev.setup;
  445. ffs->setup_state = FFS_SETUP_PENDING;
  446. }
  447. } while (++i < n);
  448. if (n < ffs->ev.count) {
  449. ffs->ev.count -= n;
  450. memmove(ffs->ev.types, ffs->ev.types + n,
  451. ffs->ev.count * sizeof *ffs->ev.types);
  452. } else {
  453. ffs->ev.count = 0;
  454. }
  455. spin_unlock_irq(&ffs->ev.waitq.lock);
  456. mutex_unlock(&ffs->mutex);
  457. return unlikely(__copy_to_user(buf, events, sizeof events))
  458. ? -EFAULT : sizeof events;
  459. }
  460. static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
  461. size_t len, loff_t *ptr)
  462. {
  463. struct ffs_data *ffs = file->private_data;
  464. char *data = NULL;
  465. size_t n;
  466. int ret;
  467. ENTER();
  468. /* Fast check if setup was canceled */
  469. if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED)
  470. return -EIDRM;
  471. /* Acquire mutex */
  472. ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
  473. if (unlikely(ret < 0))
  474. return ret;
  475. /* Check state */
  476. if (ffs->state != FFS_ACTIVE) {
  477. ret = -EBADFD;
  478. goto done_mutex;
  479. }
  480. /*
  481. * We're called from user space, we can use _irq rather then
  482. * _irqsave
  483. */
  484. spin_lock_irq(&ffs->ev.waitq.lock);
  485. switch (FFS_SETUP_STATE(ffs)) {
  486. case FFS_SETUP_CANCELED:
  487. ret = -EIDRM;
  488. break;
  489. case FFS_NO_SETUP:
  490. n = len / sizeof(struct usb_functionfs_event);
  491. if (unlikely(!n)) {
  492. ret = -EINVAL;
  493. break;
  494. }
  495. if ((file->f_flags & O_NONBLOCK) && !ffs->ev.count) {
  496. ret = -EAGAIN;
  497. break;
  498. }
  499. if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq,
  500. ffs->ev.count)) {
  501. ret = -EINTR;
  502. break;
  503. }
  504. return __ffs_ep0_read_events(ffs, buf,
  505. min(n, (size_t)ffs->ev.count));
  506. case FFS_SETUP_PENDING:
  507. if (ffs->ev.setup.bRequestType & USB_DIR_IN) {
  508. spin_unlock_irq(&ffs->ev.waitq.lock);
  509. ret = __ffs_ep0_stall(ffs);
  510. goto done_mutex;
  511. }
  512. len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
  513. spin_unlock_irq(&ffs->ev.waitq.lock);
  514. if (likely(len)) {
  515. data = kmalloc(len, GFP_KERNEL);
  516. if (unlikely(!data)) {
  517. ret = -ENOMEM;
  518. goto done_mutex;
  519. }
  520. }
  521. spin_lock_irq(&ffs->ev.waitq.lock);
  522. /* See ffs_ep0_write() */
  523. if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
  524. ret = -EIDRM;
  525. break;
  526. }
  527. /* unlocks spinlock */
  528. ret = __ffs_ep0_queue_wait(ffs, data, len);
  529. if (likely(ret > 0) && unlikely(__copy_to_user(buf, data, len)))
  530. ret = -EFAULT;
  531. goto done_mutex;
  532. default:
  533. ret = -EBADFD;
  534. break;
  535. }
  536. spin_unlock_irq(&ffs->ev.waitq.lock);
  537. done_mutex:
  538. mutex_unlock(&ffs->mutex);
  539. kfree(data);
  540. return ret;
  541. }
  542. static int ffs_ep0_open(struct inode *inode, struct file *file)
  543. {
  544. struct ffs_data *ffs = inode->i_private;
  545. ENTER();
  546. if (unlikely(ffs->state == FFS_CLOSING))
  547. return -EBUSY;
  548. file->private_data = ffs;
  549. ffs_data_opened(ffs);
  550. return 0;
  551. }
  552. static int ffs_ep0_release(struct inode *inode, struct file *file)
  553. {
  554. struct ffs_data *ffs = file->private_data;
  555. ENTER();
  556. ffs_data_closed(ffs);
  557. return 0;
  558. }
  559. static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)
  560. {
  561. struct ffs_data *ffs = file->private_data;
  562. struct usb_gadget *gadget = ffs->gadget;
  563. long ret;
  564. ENTER();
  565. if (code == FUNCTIONFS_INTERFACE_REVMAP) {
  566. struct ffs_function *func = ffs->func;
  567. ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV;
  568. } else if (gadget->ops->ioctl) {
  569. ret = gadget->ops->ioctl(gadget, code, value);
  570. } else {
  571. ret = -ENOTTY;
  572. }
  573. return ret;
  574. }
  575. static const struct file_operations ffs_ep0_operations = {
  576. .owner = THIS_MODULE,
  577. .llseek = no_llseek,
  578. .open = ffs_ep0_open,
  579. .write = ffs_ep0_write,
  580. .read = ffs_ep0_read,
  581. .release = ffs_ep0_release,
  582. .unlocked_ioctl = ffs_ep0_ioctl,
  583. };
  584. /* "Normal" endpoints operations ********************************************/
  585. static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
  586. {
  587. ENTER();
  588. if (likely(req->context)) {
  589. struct ffs_ep *ep = _ep->driver_data;
  590. ep->status = req->status ? req->status : req->actual;
  591. complete(req->context);
  592. }
  593. }
  594. static ssize_t ffs_epfile_io(struct file *file,
  595. char __user *buf, size_t len, int read)
  596. {
  597. struct ffs_epfile *epfile = file->private_data;
  598. struct ffs_ep *ep;
  599. char *data = NULL;
  600. ssize_t ret;
  601. int halt;
  602. goto first_try;
  603. do {
  604. spin_unlock_irq(&epfile->ffs->eps_lock);
  605. mutex_unlock(&epfile->mutex);
  606. first_try:
  607. /* Are we still active? */
  608. if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) {
  609. ret = -ENODEV;
  610. goto error;
  611. }
  612. /* Wait for endpoint to be enabled */
  613. ep = epfile->ep;
  614. if (!ep) {
  615. if (file->f_flags & O_NONBLOCK) {
  616. ret = -EAGAIN;
  617. goto error;
  618. }
  619. if (wait_event_interruptible(epfile->wait,
  620. (ep = epfile->ep))) {
  621. ret = -EINTR;
  622. goto error;
  623. }
  624. }
  625. /* Do we halt? */
  626. halt = !read == !epfile->in;
  627. if (halt && epfile->isoc) {
  628. ret = -EINVAL;
  629. goto error;
  630. }
  631. /* Allocate & copy */
  632. if (!halt && !data) {
  633. data = kzalloc(len, GFP_KERNEL);
  634. if (unlikely(!data))
  635. return -ENOMEM;
  636. if (!read &&
  637. unlikely(__copy_from_user(data, buf, len))) {
  638. ret = -EFAULT;
  639. goto error;
  640. }
  641. }
  642. /* We will be using request */
  643. ret = ffs_mutex_lock(&epfile->mutex,
  644. file->f_flags & O_NONBLOCK);
  645. if (unlikely(ret))
  646. goto error;
  647. /*
  648. * We're called from user space, we can use _irq rather then
  649. * _irqsave
  650. */
  651. spin_lock_irq(&epfile->ffs->eps_lock);
  652. /*
  653. * While we were acquiring mutex endpoint got disabled
  654. * or changed?
  655. */
  656. } while (unlikely(epfile->ep != ep));
  657. /* Halt */
  658. if (unlikely(halt)) {
  659. if (likely(epfile->ep == ep) && !WARN_ON(!ep->ep))
  660. usb_ep_set_halt(ep->ep);
  661. spin_unlock_irq(&epfile->ffs->eps_lock);
  662. ret = -EBADMSG;
  663. } else {
  664. /* Fire the request */
  665. DECLARE_COMPLETION_ONSTACK(done);
  666. struct usb_request *req = ep->req;
  667. req->context = &done;
  668. req->complete = ffs_epfile_io_complete;
  669. req->buf = data;
  670. req->length = len;
  671. ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
  672. spin_unlock_irq(&epfile->ffs->eps_lock);
  673. if (unlikely(ret < 0)) {
  674. /* nop */
  675. } else if (unlikely(wait_for_completion_interruptible(&done))) {
  676. ret = -EINTR;
  677. usb_ep_dequeue(ep->ep, req);
  678. } else {
  679. ret = ep->status;
  680. if (read && ret > 0 &&
  681. unlikely(copy_to_user(buf, data, ret)))
  682. ret = -EFAULT;
  683. }
  684. }
  685. mutex_unlock(&epfile->mutex);
  686. error:
  687. kfree(data);
  688. return ret;
  689. }
  690. static ssize_t
  691. ffs_epfile_write(struct file *file, const char __user *buf, size_t len,
  692. loff_t *ptr)
  693. {
  694. ENTER();
  695. return ffs_epfile_io(file, (char __user *)buf, len, 0);
  696. }
  697. static ssize_t
  698. ffs_epfile_read(struct file *file, char __user *buf, size_t len, loff_t *ptr)
  699. {
  700. ENTER();
  701. return ffs_epfile_io(file, buf, len, 1);
  702. }
  703. static int
  704. ffs_epfile_open(struct inode *inode, struct file *file)
  705. {
  706. struct ffs_epfile *epfile = inode->i_private;
  707. ENTER();
  708. if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
  709. return -ENODEV;
  710. file->private_data = epfile;
  711. ffs_data_opened(epfile->ffs);
  712. return 0;
  713. }
  714. static int
  715. ffs_epfile_release(struct inode *inode, struct file *file)
  716. {
  717. struct ffs_epfile *epfile = inode->i_private;
  718. ENTER();
  719. ffs_data_closed(epfile->ffs);
  720. return 0;
  721. }
  722. static long ffs_epfile_ioctl(struct file *file, unsigned code,
  723. unsigned long value)
  724. {
  725. struct ffs_epfile *epfile = file->private_data;
  726. int ret;
  727. ENTER();
  728. if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
  729. return -ENODEV;
  730. spin_lock_irq(&epfile->ffs->eps_lock);
  731. if (likely(epfile->ep)) {
  732. switch (code) {
  733. case FUNCTIONFS_FIFO_STATUS:
  734. ret = usb_ep_fifo_status(epfile->ep->ep);
  735. break;
  736. case FUNCTIONFS_FIFO_FLUSH:
  737. usb_ep_fifo_flush(epfile->ep->ep);
  738. ret = 0;
  739. break;
  740. case FUNCTIONFS_CLEAR_HALT:
  741. ret = usb_ep_clear_halt(epfile->ep->ep);
  742. break;
  743. case FUNCTIONFS_ENDPOINT_REVMAP:
  744. ret = epfile->ep->num;
  745. break;
  746. default:
  747. ret = -ENOTTY;
  748. }
  749. } else {
  750. ret = -ENODEV;
  751. }
  752. spin_unlock_irq(&epfile->ffs->eps_lock);
  753. return ret;
  754. }
  755. static const struct file_operations ffs_epfile_operations = {
  756. .owner = THIS_MODULE,
  757. .llseek = no_llseek,
  758. .open = ffs_epfile_open,
  759. .write = ffs_epfile_write,
  760. .read = ffs_epfile_read,
  761. .release = ffs_epfile_release,
  762. .unlocked_ioctl = ffs_epfile_ioctl,
  763. };
  764. /* File system and super block operations ***********************************/
  765. /*
  766. * Mounting the file system creates a controller file, used first for
  767. * function configuration then later for event monitoring.
  768. */
  769. static struct inode *__must_check
  770. ffs_sb_make_inode(struct super_block *sb, void *data,
  771. const struct file_operations *fops,
  772. const struct inode_operations *iops,
  773. struct ffs_file_perms *perms)
  774. {
  775. struct inode *inode;
  776. ENTER();
  777. inode = new_inode(sb);
  778. if (likely(inode)) {
  779. struct timespec current_time = CURRENT_TIME;
  780. inode->i_ino = get_next_ino();
  781. inode->i_mode = perms->mode;
  782. inode->i_uid = perms->uid;
  783. inode->i_gid = perms->gid;
  784. inode->i_atime = current_time;
  785. inode->i_mtime = current_time;
  786. inode->i_ctime = current_time;
  787. inode->i_private = data;
  788. if (fops)
  789. inode->i_fop = fops;
  790. if (iops)
  791. inode->i_op = iops;
  792. }
  793. return inode;
  794. }
  795. /* Create "regular" file */
  796. static struct inode *ffs_sb_create_file(struct super_block *sb,
  797. const char *name, void *data,
  798. const struct file_operations *fops,
  799. struct dentry **dentry_p)
  800. {
  801. struct ffs_data *ffs = sb->s_fs_info;
  802. struct dentry *dentry;
  803. struct inode *inode;
  804. ENTER();
  805. dentry = d_alloc_name(sb->s_root, name);
  806. if (unlikely(!dentry))
  807. return NULL;
  808. inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms);
  809. if (unlikely(!inode)) {
  810. dput(dentry);
  811. return NULL;
  812. }
  813. d_add(dentry, inode);
  814. if (dentry_p)
  815. *dentry_p = dentry;
  816. return inode;
  817. }
  818. /* Super block */
  819. static const struct super_operations ffs_sb_operations = {
  820. .statfs = simple_statfs,
  821. .drop_inode = generic_delete_inode,
  822. };
  823. struct ffs_sb_fill_data {
  824. struct ffs_file_perms perms;
  825. umode_t root_mode;
  826. const char *dev_name;
  827. };
  828. static int ffs_sb_fill(struct super_block *sb, void *_data, int silent)
  829. {
  830. struct ffs_sb_fill_data *data = _data;
  831. struct inode *inode;
  832. struct dentry *d;
  833. struct ffs_data *ffs;
  834. ENTER();
  835. /* Initialise data */
  836. ffs = ffs_data_new();
  837. if (unlikely(!ffs))
  838. goto enomem0;
  839. ffs->sb = sb;
  840. ffs->dev_name = data->dev_name;
  841. ffs->file_perms = data->perms;
  842. sb->s_fs_info = ffs;
  843. sb->s_blocksize = PAGE_CACHE_SIZE;
  844. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  845. sb->s_magic = FUNCTIONFS_MAGIC;
  846. sb->s_op = &ffs_sb_operations;
  847. sb->s_time_gran = 1;
  848. /* Root inode */
  849. data->perms.mode = data->root_mode;
  850. inode = ffs_sb_make_inode(sb, NULL,
  851. &simple_dir_operations,
  852. &simple_dir_inode_operations,
  853. &data->perms);
  854. if (unlikely(!inode))
  855. goto enomem1;
  856. d = d_alloc_root(inode);
  857. if (unlikely(!d))
  858. goto enomem2;
  859. sb->s_root = d;
  860. /* EP0 file */
  861. if (unlikely(!ffs_sb_create_file(sb, "ep0", ffs,
  862. &ffs_ep0_operations, NULL)))
  863. goto enomem3;
  864. return 0;
  865. enomem3:
  866. dput(d);
  867. enomem2:
  868. iput(inode);
  869. enomem1:
  870. ffs_data_put(ffs);
  871. enomem0:
  872. return -ENOMEM;
  873. }
  874. static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
  875. {
  876. ENTER();
  877. if (!opts || !*opts)
  878. return 0;
  879. for (;;) {
  880. char *end, *eq, *comma;
  881. unsigned long value;
  882. /* Option limit */
  883. comma = strchr(opts, ',');
  884. if (comma)
  885. *comma = 0;
  886. /* Value limit */
  887. eq = strchr(opts, '=');
  888. if (unlikely(!eq)) {
  889. pr_err("'=' missing in %s\n", opts);
  890. return -EINVAL;
  891. }
  892. *eq = 0;
  893. /* Parse value */
  894. value = simple_strtoul(eq + 1, &end, 0);
  895. if (unlikely(*end != ',' && *end != 0)) {
  896. pr_err("%s: invalid value: %s\n", opts, eq + 1);
  897. return -EINVAL;
  898. }
  899. /* Interpret option */
  900. switch (eq - opts) {
  901. case 5:
  902. if (!memcmp(opts, "rmode", 5))
  903. data->root_mode = (value & 0555) | S_IFDIR;
  904. else if (!memcmp(opts, "fmode", 5))
  905. data->perms.mode = (value & 0666) | S_IFREG;
  906. else
  907. goto invalid;
  908. break;
  909. case 4:
  910. if (!memcmp(opts, "mode", 4)) {
  911. data->root_mode = (value & 0555) | S_IFDIR;
  912. data->perms.mode = (value & 0666) | S_IFREG;
  913. } else {
  914. goto invalid;
  915. }
  916. break;
  917. case 3:
  918. if (!memcmp(opts, "uid", 3))
  919. data->perms.uid = value;
  920. else if (!memcmp(opts, "gid", 3))
  921. data->perms.gid = value;
  922. else
  923. goto invalid;
  924. break;
  925. default:
  926. invalid:
  927. pr_err("%s: invalid option\n", opts);
  928. return -EINVAL;
  929. }
  930. /* Next iteration */
  931. if (!comma)
  932. break;
  933. opts = comma + 1;
  934. }
  935. return 0;
  936. }
  937. /* "mount -t functionfs dev_name /dev/function" ends up here */
  938. static struct dentry *
  939. ffs_fs_mount(struct file_system_type *t, int flags,
  940. const char *dev_name, void *opts)
  941. {
  942. struct ffs_sb_fill_data data = {
  943. .perms = {
  944. .mode = S_IFREG | 0600,
  945. .uid = 0,
  946. .gid = 0
  947. },
  948. .root_mode = S_IFDIR | 0500,
  949. };
  950. int ret;
  951. ENTER();
  952. ret = functionfs_check_dev_callback(dev_name);
  953. if (unlikely(ret < 0))
  954. return ERR_PTR(ret);
  955. ret = ffs_fs_parse_opts(&data, opts);
  956. if (unlikely(ret < 0))
  957. return ERR_PTR(ret);
  958. data.dev_name = dev_name;
  959. return mount_single(t, flags, &data, ffs_sb_fill);
  960. }
  961. static void
  962. ffs_fs_kill_sb(struct super_block *sb)
  963. {
  964. void *ptr;
  965. ENTER();
  966. kill_litter_super(sb);
  967. ptr = xchg(&sb->s_fs_info, NULL);
  968. if (ptr)
  969. ffs_data_put(ptr);
  970. }
  971. static struct file_system_type ffs_fs_type = {
  972. .owner = THIS_MODULE,
  973. .name = "functionfs",
  974. .mount = ffs_fs_mount,
  975. .kill_sb = ffs_fs_kill_sb,
  976. };
  977. /* Driver's main init/cleanup functions *************************************/
  978. static int functionfs_init(void)
  979. {
  980. int ret;
  981. ENTER();
  982. ret = register_filesystem(&ffs_fs_type);
  983. if (likely(!ret))
  984. pr_info("file system registered\n");
  985. else
  986. pr_err("failed registering file system (%d)\n", ret);
  987. return ret;
  988. }
  989. static void functionfs_cleanup(void)
  990. {
  991. ENTER();
  992. pr_info("unloading\n");
  993. unregister_filesystem(&ffs_fs_type);
  994. }
  995. /* ffs_data and ffs_function construction and destruction code **************/
  996. static void ffs_data_clear(struct ffs_data *ffs);
  997. static void ffs_data_reset(struct ffs_data *ffs);
  998. static void ffs_data_get(struct ffs_data *ffs)
  999. {
  1000. ENTER();
  1001. atomic_inc(&ffs->ref);
  1002. }
  1003. static void ffs_data_opened(struct ffs_data *ffs)
  1004. {
  1005. ENTER();
  1006. atomic_inc(&ffs->ref);
  1007. atomic_inc(&ffs->opened);
  1008. }
  1009. static void ffs_data_put(struct ffs_data *ffs)
  1010. {
  1011. ENTER();
  1012. if (unlikely(atomic_dec_and_test(&ffs->ref))) {
  1013. pr_info("%s(): freeing\n", __func__);
  1014. ffs_data_clear(ffs);
  1015. BUG_ON(mutex_is_locked(&ffs->mutex) ||
  1016. spin_is_locked(&ffs->ev.waitq.lock) ||
  1017. waitqueue_active(&ffs->ev.waitq) ||
  1018. waitqueue_active(&ffs->ep0req_completion.wait));
  1019. kfree(ffs);
  1020. }
  1021. }
  1022. static void ffs_data_closed(struct ffs_data *ffs)
  1023. {
  1024. ENTER();
  1025. if (atomic_dec_and_test(&ffs->opened)) {
  1026. ffs->state = FFS_CLOSING;
  1027. ffs_data_reset(ffs);
  1028. }
  1029. ffs_data_put(ffs);
  1030. }
  1031. static struct ffs_data *ffs_data_new(void)
  1032. {
  1033. struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL);
  1034. if (unlikely(!ffs))
  1035. return 0;
  1036. ENTER();
  1037. atomic_set(&ffs->ref, 1);
  1038. atomic_set(&ffs->opened, 0);
  1039. ffs->state = FFS_READ_DESCRIPTORS;
  1040. mutex_init(&ffs->mutex);
  1041. spin_lock_init(&ffs->eps_lock);
  1042. init_waitqueue_head(&ffs->ev.waitq);
  1043. init_completion(&ffs->ep0req_completion);
  1044. /* XXX REVISIT need to update it in some places, or do we? */
  1045. ffs->ev.can_stall = 1;
  1046. return ffs;
  1047. }
  1048. static void ffs_data_clear(struct ffs_data *ffs)
  1049. {
  1050. ENTER();
  1051. if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags))
  1052. functionfs_closed_callback(ffs);
  1053. BUG_ON(ffs->gadget);
  1054. if (ffs->epfiles)
  1055. ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count);
  1056. kfree(ffs->raw_descs);
  1057. kfree(ffs->raw_strings);
  1058. kfree(ffs->stringtabs);
  1059. }
  1060. static void ffs_data_reset(struct ffs_data *ffs)
  1061. {
  1062. ENTER();
  1063. ffs_data_clear(ffs);
  1064. ffs->epfiles = NULL;
  1065. ffs->raw_descs = NULL;
  1066. ffs->raw_strings = NULL;
  1067. ffs->stringtabs = NULL;
  1068. ffs->raw_descs_length = 0;
  1069. ffs->raw_fs_descs_length = 0;
  1070. ffs->fs_descs_count = 0;
  1071. ffs->hs_descs_count = 0;
  1072. ffs->strings_count = 0;
  1073. ffs->interfaces_count = 0;
  1074. ffs->eps_count = 0;
  1075. ffs->ev.count = 0;
  1076. ffs->state = FFS_READ_DESCRIPTORS;
  1077. ffs->setup_state = FFS_NO_SETUP;
  1078. ffs->flags = 0;
  1079. }
  1080. static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
  1081. {
  1082. struct usb_gadget_strings **lang;
  1083. int first_id;
  1084. ENTER();
  1085. if (WARN_ON(ffs->state != FFS_ACTIVE
  1086. || test_and_set_bit(FFS_FL_BOUND, &ffs->flags)))
  1087. return -EBADFD;
  1088. first_id = usb_string_ids_n(cdev, ffs->strings_count);
  1089. if (unlikely(first_id < 0))
  1090. return first_id;
  1091. ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
  1092. if (unlikely(!ffs->ep0req))
  1093. return -ENOMEM;
  1094. ffs->ep0req->complete = ffs_ep0_complete;
  1095. ffs->ep0req->context = ffs;
  1096. lang = ffs->stringtabs;
  1097. for (lang = ffs->stringtabs; *lang; ++lang) {
  1098. struct usb_string *str = (*lang)->strings;
  1099. int id = first_id;
  1100. for (; str->s; ++id, ++str)
  1101. str->id = id;
  1102. }
  1103. ffs->gadget = cdev->gadget;
  1104. ffs_data_get(ffs);
  1105. return 0;
  1106. }
  1107. static void functionfs_unbind(struct ffs_data *ffs)
  1108. {
  1109. ENTER();
  1110. if (!WARN_ON(!ffs->gadget)) {
  1111. usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req);
  1112. ffs->ep0req = NULL;
  1113. ffs->gadget = NULL;
  1114. ffs_data_put(ffs);
  1115. }
  1116. }
  1117. static int ffs_epfiles_create(struct ffs_data *ffs)
  1118. {
  1119. struct ffs_epfile *epfile, *epfiles;
  1120. unsigned i, count;
  1121. ENTER();
  1122. count = ffs->eps_count;
  1123. epfiles = kcalloc(count, sizeof(*epfiles), GFP_KERNEL);
  1124. if (!epfiles)
  1125. return -ENOMEM;
  1126. epfile = epfiles;
  1127. for (i = 1; i <= count; ++i, ++epfile) {
  1128. epfile->ffs = ffs;
  1129. mutex_init(&epfile->mutex);
  1130. init_waitqueue_head(&epfile->wait);
  1131. sprintf(epfiles->name, "ep%u", i);
  1132. if (!unlikely(ffs_sb_create_file(ffs->sb, epfiles->name, epfile,
  1133. &ffs_epfile_operations,
  1134. &epfile->dentry))) {
  1135. ffs_epfiles_destroy(epfiles, i - 1);
  1136. return -ENOMEM;
  1137. }
  1138. }
  1139. ffs->epfiles = epfiles;
  1140. return 0;
  1141. }
  1142. static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
  1143. {
  1144. struct ffs_epfile *epfile = epfiles;
  1145. ENTER();
  1146. for (; count; --count, ++epfile) {
  1147. BUG_ON(mutex_is_locked(&epfile->mutex) ||
  1148. waitqueue_active(&epfile->wait));
  1149. if (epfile->dentry) {
  1150. d_delete(epfile->dentry);
  1151. dput(epfile->dentry);
  1152. epfile->dentry = NULL;
  1153. }
  1154. }
  1155. kfree(epfiles);
  1156. }
  1157. static int functionfs_bind_config(struct usb_composite_dev *cdev,
  1158. struct usb_configuration *c,
  1159. struct ffs_data *ffs)
  1160. {
  1161. struct ffs_function *func;
  1162. int ret;
  1163. ENTER();
  1164. func = kzalloc(sizeof *func, GFP_KERNEL);
  1165. if (unlikely(!func))
  1166. return -ENOMEM;
  1167. func->function.name = "Function FS Gadget";
  1168. func->function.strings = ffs->stringtabs;
  1169. func->function.bind = ffs_func_bind;
  1170. func->function.unbind = ffs_func_unbind;
  1171. func->function.set_alt = ffs_func_set_alt;
  1172. func->function.disable = ffs_func_disable;
  1173. func->function.setup = ffs_func_setup;
  1174. func->function.suspend = ffs_func_suspend;
  1175. func->function.resume = ffs_func_resume;
  1176. func->conf = c;
  1177. func->gadget = cdev->gadget;
  1178. func->ffs = ffs;
  1179. ffs_data_get(ffs);
  1180. ret = usb_add_function(c, &func->function);
  1181. if (unlikely(ret))
  1182. ffs_func_free(func);
  1183. return ret;
  1184. }
  1185. static void ffs_func_free(struct ffs_function *func)
  1186. {
  1187. ENTER();
  1188. ffs_data_put(func->ffs);
  1189. kfree(func->eps);
  1190. /*
  1191. * eps and interfaces_nums are allocated in the same chunk so
  1192. * only one free is required. Descriptors are also allocated
  1193. * in the same chunk.
  1194. */
  1195. kfree(func);
  1196. }
  1197. static void ffs_func_eps_disable(struct ffs_function *func)
  1198. {
  1199. struct ffs_ep *ep = func->eps;
  1200. struct ffs_epfile *epfile = func->ffs->epfiles;
  1201. unsigned count = func->ffs->eps_count;
  1202. unsigned long flags;
  1203. spin_lock_irqsave(&func->ffs->eps_lock, flags);
  1204. do {
  1205. /* pending requests get nuked */
  1206. if (likely(ep->ep))
  1207. usb_ep_disable(ep->ep);
  1208. epfile->ep = NULL;
  1209. ++ep;
  1210. ++epfile;
  1211. } while (--count);
  1212. spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
  1213. }
  1214. static int ffs_func_eps_enable(struct ffs_function *func)
  1215. {
  1216. struct ffs_data *ffs = func->ffs;
  1217. struct ffs_ep *ep = func->eps;
  1218. struct ffs_epfile *epfile = ffs->epfiles;
  1219. unsigned count = ffs->eps_count;
  1220. unsigned long flags;
  1221. int ret = 0;
  1222. spin_lock_irqsave(&func->ffs->eps_lock, flags);
  1223. do {
  1224. struct usb_endpoint_descriptor *ds;
  1225. ds = ep->descs[ep->descs[1] ? 1 : 0];
  1226. ep->ep->driver_data = ep;
  1227. ep->ep->desc = ds;
  1228. ret = usb_ep_enable(ep->ep);
  1229. if (likely(!ret)) {
  1230. epfile->ep = ep;
  1231. epfile->in = usb_endpoint_dir_in(ds);
  1232. epfile->isoc = usb_endpoint_xfer_isoc(ds);
  1233. } else {
  1234. break;
  1235. }
  1236. wake_up(&epfile->wait);
  1237. ++ep;
  1238. ++epfile;
  1239. } while (--count);
  1240. spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
  1241. return ret;
  1242. }
  1243. /* Parsing and building descriptors and strings *****************************/
  1244. /*
  1245. * This validates if data pointed by data is a valid USB descriptor as
  1246. * well as record how many interfaces, endpoints and strings are
  1247. * required by given configuration. Returns address after the
  1248. * descriptor or NULL if data is invalid.
  1249. */
  1250. enum ffs_entity_type {
  1251. FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT
  1252. };
  1253. typedef int (*ffs_entity_callback)(enum ffs_entity_type entity,
  1254. u8 *valuep,
  1255. struct usb_descriptor_header *desc,
  1256. void *priv);
  1257. static int __must_check ffs_do_desc(char *data, unsigned len,
  1258. ffs_entity_callback entity, void *priv)
  1259. {
  1260. struct usb_descriptor_header *_ds = (void *)data;
  1261. u8 length;
  1262. int ret;
  1263. ENTER();
  1264. /* At least two bytes are required: length and type */
  1265. if (len < 2) {
  1266. pr_vdebug("descriptor too short\n");
  1267. return -EINVAL;
  1268. }
  1269. /* If we have at least as many bytes as the descriptor takes? */
  1270. length = _ds->bLength;
  1271. if (len < length) {
  1272. pr_vdebug("descriptor longer then available data\n");
  1273. return -EINVAL;
  1274. }
  1275. #define __entity_check_INTERFACE(val) 1
  1276. #define __entity_check_STRING(val) (val)
  1277. #define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
  1278. #define __entity(type, val) do { \
  1279. pr_vdebug("entity " #type "(%02x)\n", (val)); \
  1280. if (unlikely(!__entity_check_ ##type(val))) { \
  1281. pr_vdebug("invalid entity's value\n"); \
  1282. return -EINVAL; \
  1283. } \
  1284. ret = entity(FFS_ ##type, &val, _ds, priv); \
  1285. if (unlikely(ret < 0)) { \
  1286. pr_debug("entity " #type "(%02x); ret = %d\n", \
  1287. (val), ret); \
  1288. return ret; \
  1289. } \
  1290. } while (0)
  1291. /* Parse descriptor depending on type. */
  1292. switch (_ds->bDescriptorType) {
  1293. case USB_DT_DEVICE:
  1294. case USB_DT_CONFIG:
  1295. case USB_DT_STRING:
  1296. case USB_DT_DEVICE_QUALIFIER:
  1297. /* function can't have any of those */
  1298. pr_vdebug("descriptor reserved for gadget: %d\n",
  1299. _ds->bDescriptorType);
  1300. return -EINVAL;
  1301. case USB_DT_INTERFACE: {
  1302. struct usb_interface_descriptor *ds = (void *)_ds;
  1303. pr_vdebug("interface descriptor\n");
  1304. if (length != sizeof *ds)
  1305. goto inv_length;
  1306. __entity(INTERFACE, ds->bInterfaceNumber);
  1307. if (ds->iInterface)
  1308. __entity(STRING, ds->iInterface);
  1309. }
  1310. break;
  1311. case USB_DT_ENDPOINT: {
  1312. struct usb_endpoint_descriptor *ds = (void *)_ds;
  1313. pr_vdebug("endpoint descriptor\n");
  1314. if (length != USB_DT_ENDPOINT_SIZE &&
  1315. length != USB_DT_ENDPOINT_AUDIO_SIZE)
  1316. goto inv_length;
  1317. __entity(ENDPOINT, ds->bEndpointAddress);
  1318. }
  1319. break;
  1320. case USB_DT_OTG:
  1321. if (length != sizeof(struct usb_otg_descriptor))
  1322. goto inv_length;
  1323. break;
  1324. case USB_DT_INTERFACE_ASSOCIATION: {
  1325. struct usb_interface_assoc_descriptor *ds = (void *)_ds;
  1326. pr_vdebug("interface association descriptor\n");
  1327. if (length != sizeof *ds)
  1328. goto inv_length;
  1329. if (ds->iFunction)
  1330. __entity(STRING, ds->iFunction);
  1331. }
  1332. break;
  1333. case USB_DT_OTHER_SPEED_CONFIG:
  1334. case USB_DT_INTERFACE_POWER:
  1335. case USB_DT_DEBUG:
  1336. case USB_DT_SECURITY:
  1337. case USB_DT_CS_RADIO_CONTROL:
  1338. /* TODO */
  1339. pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType);
  1340. return -EINVAL;
  1341. default:
  1342. /* We should never be here */
  1343. pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType);
  1344. return -EINVAL;
  1345. inv_length:
  1346. pr_vdebug("invalid length: %d (descriptor %d)\n",
  1347. _ds->bLength, _ds->bDescriptorType);
  1348. return -EINVAL;
  1349. }
  1350. #undef __entity
  1351. #undef __entity_check_DESCRIPTOR
  1352. #undef __entity_check_INTERFACE
  1353. #undef __entity_check_STRING
  1354. #undef __entity_check_ENDPOINT
  1355. return length;
  1356. }
  1357. static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
  1358. ffs_entity_callback entity, void *priv)
  1359. {
  1360. const unsigned _len = len;
  1361. unsigned long num = 0;
  1362. ENTER();
  1363. for (;;) {
  1364. int ret;
  1365. if (num == count)
  1366. data = NULL;
  1367. /* Record "descriptor" entity */
  1368. ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv);
  1369. if (unlikely(ret < 0)) {
  1370. pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n",
  1371. num, ret);
  1372. return ret;
  1373. }
  1374. if (!data)
  1375. return _len - len;
  1376. ret = ffs_do_desc(data, len, entity, priv);
  1377. if (unlikely(ret < 0)) {
  1378. pr_debug("%s returns %d\n", __func__, ret);
  1379. return ret;
  1380. }
  1381. len -= ret;
  1382. data += ret;
  1383. ++num;
  1384. }
  1385. }
  1386. static int __ffs_data_do_entity(enum ffs_entity_type type,
  1387. u8 *valuep, struct usb_descriptor_header *desc,
  1388. void *priv)
  1389. {
  1390. struct ffs_data *ffs = priv;
  1391. ENTER();
  1392. switch (type) {
  1393. case FFS_DESCRIPTOR:
  1394. break;
  1395. case FFS_INTERFACE:
  1396. /*
  1397. * Interfaces are indexed from zero so if we
  1398. * encountered interface "n" then there are at least
  1399. * "n+1" interfaces.
  1400. */
  1401. if (*valuep >= ffs->interfaces_count)
  1402. ffs->interfaces_count = *valuep + 1;
  1403. break;
  1404. case FFS_STRING:
  1405. /*
  1406. * Strings are indexed from 1 (0 is magic ;) reserved
  1407. * for languages list or some such)
  1408. */
  1409. if (*valuep > ffs->strings_count)
  1410. ffs->strings_count = *valuep;
  1411. break;
  1412. case FFS_ENDPOINT:
  1413. /* Endpoints are indexed from 1 as well. */
  1414. if ((*valuep & USB_ENDPOINT_NUMBER_MASK) > ffs->eps_count)
  1415. ffs->eps_count = (*valuep & USB_ENDPOINT_NUMBER_MASK);
  1416. break;
  1417. }
  1418. return 0;
  1419. }
  1420. static int __ffs_data_got_descs(struct ffs_data *ffs,
  1421. char *const _data, size_t len)
  1422. {
  1423. unsigned fs_count, hs_count;
  1424. int fs_len, ret = -EINVAL;
  1425. char *data = _data;
  1426. ENTER();
  1427. if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_DESCRIPTORS_MAGIC ||
  1428. get_unaligned_le32(data + 4) != len))
  1429. goto error;
  1430. fs_count = get_unaligned_le32(data + 8);
  1431. hs_count = get_unaligned_le32(data + 12);
  1432. if (!fs_count && !hs_count)
  1433. goto einval;
  1434. data += 16;
  1435. len -= 16;
  1436. if (likely(fs_count)) {
  1437. fs_len = ffs_do_descs(fs_count, data, len,
  1438. __ffs_data_do_entity, ffs);
  1439. if (unlikely(fs_len < 0)) {
  1440. ret = fs_len;
  1441. goto error;
  1442. }
  1443. data += fs_len;
  1444. len -= fs_len;
  1445. } else {
  1446. fs_len = 0;
  1447. }
  1448. if (likely(hs_count)) {
  1449. ret = ffs_do_descs(hs_count, data, len,
  1450. __ffs_data_do_entity, ffs);
  1451. if (unlikely(ret < 0))
  1452. goto error;
  1453. } else {
  1454. ret = 0;
  1455. }
  1456. if (unlikely(len != ret))
  1457. goto einval;
  1458. ffs->raw_fs_descs_length = fs_len;
  1459. ffs->raw_descs_length = fs_len + ret;
  1460. ffs->raw_descs = _data;
  1461. ffs->fs_descs_count = fs_count;
  1462. ffs->hs_descs_count = hs_count;
  1463. return 0;
  1464. einval:
  1465. ret = -EINVAL;
  1466. error:
  1467. kfree(_data);
  1468. return ret;
  1469. }
  1470. static int __ffs_data_got_strings(struct ffs_data *ffs,
  1471. char *const _data, size_t len)
  1472. {
  1473. u32 str_count, needed_count, lang_count;
  1474. struct usb_gadget_strings **stringtabs, *t;
  1475. struct usb_string *strings, *s;
  1476. const char *data = _data;
  1477. ENTER();
  1478. if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC ||
  1479. get_unaligned_le32(data + 4) != len))
  1480. goto error;
  1481. str_count = get_unaligned_le32(data + 8);
  1482. lang_count = get_unaligned_le32(data + 12);
  1483. /* if one is zero the other must be zero */
  1484. if (unlikely(!str_count != !lang_count))
  1485. goto error;
  1486. /* Do we have at least as many strings as descriptors need? */
  1487. needed_count = ffs->strings_count;
  1488. if (unlikely(str_count < needed_count))
  1489. goto error;
  1490. /*
  1491. * If we don't need any strings just return and free all
  1492. * memory.
  1493. */
  1494. if (!needed_count) {
  1495. kfree(_data);
  1496. return 0;
  1497. }
  1498. /* Allocate everything in one chunk so there's less maintenance. */
  1499. {
  1500. struct {
  1501. struct usb_gadget_strings *stringtabs[lang_count + 1];
  1502. struct usb_gadget_strings stringtab[lang_count];
  1503. struct usb_string strings[lang_count*(needed_count+1)];
  1504. } *d;
  1505. unsigned i = 0;
  1506. d = kmalloc(sizeof *d, GFP_KERNEL);
  1507. if (unlikely(!d)) {
  1508. kfree(_data);
  1509. return -ENOMEM;
  1510. }
  1511. stringtabs = d->stringtabs;
  1512. t = d->stringtab;
  1513. i = lang_count;
  1514. do {
  1515. *stringtabs++ = t++;
  1516. } while (--i);
  1517. *stringtabs = NULL;
  1518. stringtabs = d->stringtabs;
  1519. t = d->stringtab;
  1520. s = d->strings;
  1521. strings = s;
  1522. }
  1523. /* For each language */
  1524. data += 16;
  1525. len -= 16;
  1526. do { /* lang_count > 0 so we can use do-while */
  1527. unsigned needed = needed_count;
  1528. if (unlikely(len < 3))
  1529. goto error_free;
  1530. t->language = get_unaligned_le16(data);
  1531. t->strings = s;
  1532. ++t;
  1533. data += 2;
  1534. len -= 2;
  1535. /* For each string */
  1536. do { /* str_count > 0 so we can use do-while */
  1537. size_t length = strnlen(data, len);
  1538. if (unlikely(length == len))
  1539. goto error_free;
  1540. /*
  1541. * User may provide more strings then we need,
  1542. * if that's the case we simply ignore the
  1543. * rest
  1544. */
  1545. if (likely(needed)) {
  1546. /*
  1547. * s->id will be set while adding
  1548. * function to configuration so for
  1549. * now just leave garbage here.
  1550. */
  1551. s->s = data;
  1552. --needed;
  1553. ++s;
  1554. }
  1555. data += length + 1;
  1556. len -= length + 1;
  1557. } while (--str_count);
  1558. s->id = 0; /* terminator */
  1559. s->s = NULL;
  1560. ++s;
  1561. } while (--lang_count);
  1562. /* Some garbage left? */
  1563. if (unlikely(len))
  1564. goto error_free;
  1565. /* Done! */
  1566. ffs->stringtabs = stringtabs;
  1567. ffs->raw_strings = _data;
  1568. return 0;
  1569. error_free:
  1570. kfree(stringtabs);
  1571. error:
  1572. kfree(_data);
  1573. return -EINVAL;
  1574. }
  1575. /* Events handling and management *******************************************/
  1576. static void __ffs_event_add(struct ffs_data *ffs,
  1577. enum usb_functionfs_event_type type)
  1578. {
  1579. enum usb_functionfs_event_type rem_type1, rem_type2 = type;
  1580. int neg = 0;
  1581. /*
  1582. * Abort any unhandled setup
  1583. *
  1584. * We do not need to worry about some cmpxchg() changing value
  1585. * of ffs->setup_state without holding the lock because when
  1586. * state is FFS_SETUP_PENDING cmpxchg() in several places in
  1587. * the source does nothing.
  1588. */
  1589. if (ffs->setup_state == FFS_SETUP_PENDING)
  1590. ffs->setup_state = FFS_SETUP_CANCELED;
  1591. switch (type) {
  1592. case FUNCTIONFS_RESUME:
  1593. rem_type2 = FUNCTIONFS_SUSPEND;
  1594. /* FALL THROUGH */
  1595. case FUNCTIONFS_SUSPEND:
  1596. case FUNCTIONFS_SETUP:
  1597. rem_type1 = type;
  1598. /* Discard all similar events */
  1599. break;
  1600. case FUNCTIONFS_BIND:
  1601. case FUNCTIONFS_UNBIND:
  1602. case FUNCTIONFS_DISABLE:
  1603. case FUNCTIONFS_ENABLE:
  1604. /* Discard everything other then power management. */
  1605. rem_type1 = FUNCTIONFS_SUSPEND;
  1606. rem_type2 = FUNCTIONFS_RESUME;
  1607. neg = 1;
  1608. break;
  1609. default:
  1610. BUG();
  1611. }
  1612. {
  1613. u8 *ev = ffs->ev.types, *out = ev;
  1614. unsigned n = ffs->ev.count;
  1615. for (; n; --n, ++ev)
  1616. if ((*ev == rem_type1 || *ev == rem_type2) == neg)
  1617. *out++ = *ev;
  1618. else
  1619. pr_vdebug("purging event %d\n", *ev);
  1620. ffs->ev.count = out - ffs->ev.types;
  1621. }
  1622. pr_vdebug("adding event %d\n", type);
  1623. ffs->ev.types[ffs->ev.count++] = type;
  1624. wake_up_locked(&ffs->ev.waitq);
  1625. }
  1626. static void ffs_event_add(struct ffs_data *ffs,
  1627. enum usb_functionfs_event_type type)
  1628. {
  1629. unsigned long flags;
  1630. spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
  1631. __ffs_event_add(ffs, type);
  1632. spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
  1633. }
  1634. /* Bind/unbind USB function hooks *******************************************/
  1635. static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
  1636. struct usb_descriptor_header *desc,
  1637. void *priv)
  1638. {
  1639. struct usb_endpoint_descriptor *ds = (void *)desc;
  1640. struct ffs_function *func = priv;
  1641. struct ffs_ep *ffs_ep;
  1642. /*
  1643. * If hs_descriptors is not NULL then we are reading hs
  1644. * descriptors now
  1645. */
  1646. const int isHS = func->function.hs_descriptors != NULL;
  1647. unsigned idx;
  1648. if (type != FFS_DESCRIPTOR)
  1649. return 0;
  1650. if (isHS)
  1651. func->function.hs_descriptors[(long)valuep] = desc;
  1652. else
  1653. func->function.descriptors[(long)valuep] = desc;
  1654. if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
  1655. return 0;
  1656. idx = (ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) - 1;
  1657. ffs_ep = func->eps + idx;
  1658. if (unlikely(ffs_ep->descs[isHS])) {
  1659. pr_vdebug("two %sspeed descriptors for EP %d\n",
  1660. isHS ? "high" : "full",
  1661. ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
  1662. return -EINVAL;
  1663. }
  1664. ffs_ep->descs[isHS] = ds;
  1665. ffs_dump_mem(": Original ep desc", ds, ds->bLength);
  1666. if (ffs_ep->ep) {
  1667. ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress;
  1668. if (!ds->wMaxPacketSize)
  1669. ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize;
  1670. } else {
  1671. struct usb_request *req;
  1672. struct usb_ep *ep;
  1673. pr_vdebug("autoconfig\n");
  1674. ep = usb_ep_autoconfig(func->gadget, ds);
  1675. if (unlikely(!ep))
  1676. return -ENOTSUPP;
  1677. ep->driver_data = func->eps + idx;
  1678. req = usb_ep_alloc_request(ep, GFP_KERNEL);
  1679. if (unlikely(!req))
  1680. return -ENOMEM;
  1681. ffs_ep->ep = ep;
  1682. ffs_ep->req = req;
  1683. func->eps_revmap[ds->bEndpointAddress &
  1684. USB_ENDPOINT_NUMBER_MASK] = idx + 1;
  1685. }
  1686. ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength);
  1687. return 0;
  1688. }
  1689. static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
  1690. struct usb_descriptor_header *desc,
  1691. void *priv)
  1692. {
  1693. struct ffs_function *func = priv;
  1694. unsigned idx;
  1695. u8 newValue;
  1696. switch (type) {
  1697. default:
  1698. case FFS_DESCRIPTOR:
  1699. /* Handled in previous pass by __ffs_func_bind_do_descs() */
  1700. return 0;
  1701. case FFS_INTERFACE:
  1702. idx = *valuep;
  1703. if (func->interfaces_nums[idx] < 0) {
  1704. int id = usb_interface_id(func->conf, &func->function);
  1705. if (unlikely(id < 0))
  1706. return id;
  1707. func->interfaces_nums[idx] = id;
  1708. }
  1709. newValue = func->interfaces_nums[idx];
  1710. break;
  1711. case FFS_STRING:
  1712. /* String' IDs are allocated when fsf_data is bound to cdev */
  1713. newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id;
  1714. break;
  1715. case FFS_ENDPOINT:
  1716. /*
  1717. * USB_DT_ENDPOINT are handled in
  1718. * __ffs_func_bind_do_descs().
  1719. */
  1720. if (desc->bDescriptorType == USB_DT_ENDPOINT)
  1721. return 0;
  1722. idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1;
  1723. if (unlikely(!func->eps[idx].ep))
  1724. return -EINVAL;
  1725. {
  1726. struct usb_endpoint_descriptor **descs;
  1727. descs = func->eps[idx].descs;
  1728. newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress;
  1729. }
  1730. break;
  1731. }
  1732. pr_vdebug("%02x -> %02x\n", *valuep, newValue);
  1733. *valuep = newValue;
  1734. return 0;
  1735. }
  1736. static int ffs_func_bind(struct usb_configuration *c,
  1737. struct usb_function *f)
  1738. {
  1739. struct ffs_function *func = ffs_func_from_usb(f);
  1740. struct ffs_data *ffs = func->ffs;
  1741. const int full = !!func->ffs->fs_descs_count;
  1742. const int high = gadget_is_dualspeed(func->gadget) &&
  1743. func->ffs->hs_descs_count;
  1744. int ret;
  1745. /* Make it a single chunk, less management later on */
  1746. struct {
  1747. struct ffs_ep eps[ffs->eps_count];
  1748. struct usb_descriptor_header
  1749. *fs_descs[full ? ffs->fs_descs_count + 1 : 0];
  1750. struct usb_descriptor_header
  1751. *hs_descs[high ? ffs->hs_descs_count + 1 : 0];
  1752. short inums[ffs->interfaces_count];
  1753. char raw_descs[high ? ffs->raw_descs_length
  1754. : ffs->raw_fs_descs_length];
  1755. } *data;
  1756. ENTER();
  1757. /* Only high speed but not supported by gadget? */
  1758. if (unlikely(!(full | high)))
  1759. return -ENOTSUPP;
  1760. /* Allocate */
  1761. data = kmalloc(sizeof *data, GFP_KERNEL);
  1762. if (unlikely(!data))
  1763. return -ENOMEM;
  1764. /* Zero */
  1765. memset(data->eps, 0, sizeof data->eps);
  1766. memcpy(data->raw_descs, ffs->raw_descs + 16, sizeof data->raw_descs);
  1767. memset(data->inums, 0xff, sizeof data->inums);
  1768. for (ret = ffs->eps_count; ret; --ret)
  1769. data->eps[ret].num = -1;
  1770. /* Save pointers */
  1771. func->eps = data->eps;
  1772. func->interfaces_nums = data->inums;
  1773. /*
  1774. * Go through all the endpoint descriptors and allocate
  1775. * endpoints first, so that later we can rewrite the endpoint
  1776. * numbers without worrying that it may be described later on.
  1777. */
  1778. if (likely(full)) {
  1779. func->function.descriptors = data->fs_descs;
  1780. ret = ffs_do_descs(ffs->fs_descs_count,
  1781. data->raw_descs,
  1782. sizeof data->raw_descs,
  1783. __ffs_func_bind_do_descs, func);
  1784. if (unlikely(ret < 0))
  1785. goto error;
  1786. } else {
  1787. ret = 0;
  1788. }
  1789. if (likely(high)) {
  1790. func->function.hs_descriptors = data->hs_descs;
  1791. ret = ffs_do_descs(ffs->hs_descs_count,
  1792. data->raw_descs + ret,
  1793. (sizeof data->raw_descs) - ret,
  1794. __ffs_func_bind_do_descs, func);
  1795. }
  1796. /*
  1797. * Now handle interface numbers allocation and interface and
  1798. * endpoint numbers rewriting. We can do that in one go
  1799. * now.
  1800. */
  1801. ret = ffs_do_descs(ffs->fs_descs_count +
  1802. (high ? ffs->hs_descs_count : 0),
  1803. data->raw_descs, sizeof data->raw_descs,
  1804. __ffs_func_bind_do_nums, func);
  1805. if (unlikely(ret < 0))
  1806. goto error;
  1807. /* And we're done */
  1808. ffs_event_add(ffs, FUNCTIONFS_BIND);
  1809. return 0;
  1810. error:
  1811. /* XXX Do we need to release all claimed endpoints here? */
  1812. return ret;
  1813. }
  1814. /* Other USB function hooks *************************************************/
  1815. static void ffs_func_unbind(struct usb_configuration *c,
  1816. struct usb_function *f)
  1817. {
  1818. struct ffs_function *func = ffs_func_from_usb(f);
  1819. struct ffs_data *ffs = func->ffs;
  1820. ENTER();
  1821. if (ffs->func == func) {
  1822. ffs_func_eps_disable(func);
  1823. ffs->func = NULL;
  1824. }
  1825. ffs_event_add(ffs, FUNCTIONFS_UNBIND);
  1826. ffs_func_free(func);
  1827. }
  1828. static int ffs_func_set_alt(struct usb_function *f,
  1829. unsigned interface, unsigned alt)
  1830. {
  1831. struct ffs_function *func = ffs_func_from_usb(f);
  1832. struct ffs_data *ffs = func->ffs;
  1833. int ret = 0, intf;
  1834. if (alt != (unsigned)-1) {
  1835. intf = ffs_func_revmap_intf(func, interface);
  1836. if (unlikely(intf < 0))
  1837. return intf;
  1838. }
  1839. if (ffs->func)
  1840. ffs_func_eps_disable(ffs->func);
  1841. if (ffs->state != FFS_ACTIVE)
  1842. return -ENODEV;
  1843. if (alt == (unsigned)-1) {
  1844. ffs->func = NULL;
  1845. ffs_event_add(ffs, FUNCTIONFS_DISABLE);
  1846. return 0;
  1847. }
  1848. ffs->func = func;
  1849. ret = ffs_func_eps_enable(func);
  1850. if (likely(ret >= 0))
  1851. ffs_event_add(ffs, FUNCTIONFS_ENABLE);
  1852. return ret;
  1853. }
  1854. static void ffs_func_disable(struct usb_function *f)
  1855. {
  1856. ffs_func_set_alt(f, 0, (unsigned)-1);
  1857. }
  1858. static int ffs_func_setup(struct usb_function *f,
  1859. const struct usb_ctrlrequest *creq)
  1860. {
  1861. struct ffs_function *func = ffs_func_from_usb(f);
  1862. struct ffs_data *ffs = func->ffs;
  1863. unsigned long flags;
  1864. int ret;
  1865. ENTER();
  1866. pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType);
  1867. pr_vdebug("creq->bRequest = %02x\n", creq->bRequest);
  1868. pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq->wValue));
  1869. pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq->wIndex));
  1870. pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq->wLength));
  1871. /*
  1872. * Most requests directed to interface go through here
  1873. * (notable exceptions are set/get interface) so we need to
  1874. * handle them. All other either handled by composite or
  1875. * passed to usb_configuration->setup() (if one is set). No
  1876. * matter, we will handle requests directed to endpoint here
  1877. * as well (as it's straightforward) but what to do with any
  1878. * other request?
  1879. */
  1880. if (ffs->state != FFS_ACTIVE)
  1881. return -ENODEV;
  1882. switch (creq->bRequestType & USB_RECIP_MASK) {
  1883. case USB_RECIP_INTERFACE:
  1884. ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex));
  1885. if (unlikely(ret < 0))
  1886. return ret;
  1887. break;
  1888. case USB_RECIP_ENDPOINT:
  1889. ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex));
  1890. if (unlikely(ret < 0))
  1891. return ret;
  1892. break;
  1893. default:
  1894. return -EOPNOTSUPP;
  1895. }
  1896. spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
  1897. ffs->ev.setup = *creq;
  1898. ffs->ev.setup.wIndex = cpu_to_le16(ret);
  1899. __ffs_event_add(ffs, FUNCTIONFS_SETUP);
  1900. spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
  1901. return 0;
  1902. }
  1903. static void ffs_func_suspend(struct usb_function *f)
  1904. {
  1905. ENTER();
  1906. ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND);
  1907. }
  1908. static void ffs_func_resume(struct usb_function *f)
  1909. {
  1910. ENTER();
  1911. ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME);
  1912. }
  1913. /* Endpoint and interface numbers reverse mapping ***************************/
  1914. static int ffs_func_revmap_ep(struct ffs_function *func, u8 num)
  1915. {
  1916. num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK];
  1917. return num ? num : -EDOM;
  1918. }
  1919. static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf)
  1920. {
  1921. short *nums = func->interfaces_nums;
  1922. unsigned count = func->ffs->interfaces_count;
  1923. for (; count; --count, ++nums) {
  1924. if (*nums >= 0 && *nums == intf)
  1925. return nums - func->interfaces_nums;
  1926. }
  1927. return -EDOM;
  1928. }
  1929. /* Misc helper functions ****************************************************/
  1930. static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
  1931. {
  1932. return nonblock
  1933. ? likely(mutex_trylock(mutex)) ? 0 : -EAGAIN
  1934. : mutex_lock_interruptible(mutex);
  1935. }
  1936. static char *ffs_prepare_buffer(const char * __user buf, size_t len)
  1937. {
  1938. char *data;
  1939. if (unlikely(!len))
  1940. return NULL;
  1941. data = kmalloc(len, GFP_KERNEL);
  1942. if (unlikely(!data))
  1943. return ERR_PTR(-ENOMEM);
  1944. if (unlikely(__copy_from_user(data, buf, len))) {
  1945. kfree(data);
  1946. return ERR_PTR(-EFAULT);
  1947. }
  1948. pr_vdebug("Buffer from user space:\n");
  1949. ffs_dump_mem("", data, len);
  1950. return data;
  1951. }