f_fs.c 55 KB

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