f_fs.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409
  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 <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 ffs_data *ffs;
  833. ENTER();
  834. /* Initialise data */
  835. ffs = ffs_data_new();
  836. if (unlikely(!ffs))
  837. goto Enomem;
  838. ffs->sb = sb;
  839. ffs->dev_name = data->dev_name;
  840. ffs->file_perms = data->perms;
  841. sb->s_fs_info = ffs;
  842. sb->s_blocksize = PAGE_CACHE_SIZE;
  843. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  844. sb->s_magic = FUNCTIONFS_MAGIC;
  845. sb->s_op = &ffs_sb_operations;
  846. sb->s_time_gran = 1;
  847. /* Root inode */
  848. data->perms.mode = data->root_mode;
  849. inode = ffs_sb_make_inode(sb, NULL,
  850. &simple_dir_operations,
  851. &simple_dir_inode_operations,
  852. &data->perms);
  853. sb->s_root = d_make_root(inode);
  854. if (unlikely(!sb->s_root))
  855. goto Enomem;
  856. /* EP0 file */
  857. if (unlikely(!ffs_sb_create_file(sb, "ep0", ffs,
  858. &ffs_ep0_operations, NULL)))
  859. goto Enomem;
  860. return 0;
  861. Enomem:
  862. return -ENOMEM;
  863. }
  864. static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
  865. {
  866. ENTER();
  867. if (!opts || !*opts)
  868. return 0;
  869. for (;;) {
  870. char *end, *eq, *comma;
  871. unsigned long value;
  872. /* Option limit */
  873. comma = strchr(opts, ',');
  874. if (comma)
  875. *comma = 0;
  876. /* Value limit */
  877. eq = strchr(opts, '=');
  878. if (unlikely(!eq)) {
  879. pr_err("'=' missing in %s\n", opts);
  880. return -EINVAL;
  881. }
  882. *eq = 0;
  883. /* Parse value */
  884. value = simple_strtoul(eq + 1, &end, 0);
  885. if (unlikely(*end != ',' && *end != 0)) {
  886. pr_err("%s: invalid value: %s\n", opts, eq + 1);
  887. return -EINVAL;
  888. }
  889. /* Interpret option */
  890. switch (eq - opts) {
  891. case 5:
  892. if (!memcmp(opts, "rmode", 5))
  893. data->root_mode = (value & 0555) | S_IFDIR;
  894. else if (!memcmp(opts, "fmode", 5))
  895. data->perms.mode = (value & 0666) | S_IFREG;
  896. else
  897. goto invalid;
  898. break;
  899. case 4:
  900. if (!memcmp(opts, "mode", 4)) {
  901. data->root_mode = (value & 0555) | S_IFDIR;
  902. data->perms.mode = (value & 0666) | S_IFREG;
  903. } else {
  904. goto invalid;
  905. }
  906. break;
  907. case 3:
  908. if (!memcmp(opts, "uid", 3))
  909. data->perms.uid = value;
  910. else if (!memcmp(opts, "gid", 3))
  911. data->perms.gid = value;
  912. else
  913. goto invalid;
  914. break;
  915. default:
  916. invalid:
  917. pr_err("%s: invalid option\n", opts);
  918. return -EINVAL;
  919. }
  920. /* Next iteration */
  921. if (!comma)
  922. break;
  923. opts = comma + 1;
  924. }
  925. return 0;
  926. }
  927. /* "mount -t functionfs dev_name /dev/function" ends up here */
  928. static struct dentry *
  929. ffs_fs_mount(struct file_system_type *t, int flags,
  930. const char *dev_name, void *opts)
  931. {
  932. struct ffs_sb_fill_data data = {
  933. .perms = {
  934. .mode = S_IFREG | 0600,
  935. .uid = 0,
  936. .gid = 0
  937. },
  938. .root_mode = S_IFDIR | 0500,
  939. };
  940. int ret;
  941. ENTER();
  942. ret = functionfs_check_dev_callback(dev_name);
  943. if (unlikely(ret < 0))
  944. return ERR_PTR(ret);
  945. ret = ffs_fs_parse_opts(&data, opts);
  946. if (unlikely(ret < 0))
  947. return ERR_PTR(ret);
  948. data.dev_name = dev_name;
  949. return mount_single(t, flags, &data, ffs_sb_fill);
  950. }
  951. static void
  952. ffs_fs_kill_sb(struct super_block *sb)
  953. {
  954. ENTER();
  955. kill_litter_super(sb);
  956. if (sb->s_fs_info)
  957. ffs_data_put(sb->s_fs_info);
  958. }
  959. static struct file_system_type ffs_fs_type = {
  960. .owner = THIS_MODULE,
  961. .name = "functionfs",
  962. .mount = ffs_fs_mount,
  963. .kill_sb = ffs_fs_kill_sb,
  964. };
  965. /* Driver's main init/cleanup functions *************************************/
  966. static int functionfs_init(void)
  967. {
  968. int ret;
  969. ENTER();
  970. ret = register_filesystem(&ffs_fs_type);
  971. if (likely(!ret))
  972. pr_info("file system registered\n");
  973. else
  974. pr_err("failed registering file system (%d)\n", ret);
  975. return ret;
  976. }
  977. static void functionfs_cleanup(void)
  978. {
  979. ENTER();
  980. pr_info("unloading\n");
  981. unregister_filesystem(&ffs_fs_type);
  982. }
  983. /* ffs_data and ffs_function construction and destruction code **************/
  984. static void ffs_data_clear(struct ffs_data *ffs);
  985. static void ffs_data_reset(struct ffs_data *ffs);
  986. static void ffs_data_get(struct ffs_data *ffs)
  987. {
  988. ENTER();
  989. atomic_inc(&ffs->ref);
  990. }
  991. static void ffs_data_opened(struct ffs_data *ffs)
  992. {
  993. ENTER();
  994. atomic_inc(&ffs->ref);
  995. atomic_inc(&ffs->opened);
  996. }
  997. static void ffs_data_put(struct ffs_data *ffs)
  998. {
  999. ENTER();
  1000. if (unlikely(atomic_dec_and_test(&ffs->ref))) {
  1001. pr_info("%s(): freeing\n", __func__);
  1002. ffs_data_clear(ffs);
  1003. BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
  1004. waitqueue_active(&ffs->ep0req_completion.wait));
  1005. kfree(ffs);
  1006. }
  1007. }
  1008. static void ffs_data_closed(struct ffs_data *ffs)
  1009. {
  1010. ENTER();
  1011. if (atomic_dec_and_test(&ffs->opened)) {
  1012. ffs->state = FFS_CLOSING;
  1013. ffs_data_reset(ffs);
  1014. }
  1015. ffs_data_put(ffs);
  1016. }
  1017. static struct ffs_data *ffs_data_new(void)
  1018. {
  1019. struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL);
  1020. if (unlikely(!ffs))
  1021. return 0;
  1022. ENTER();
  1023. atomic_set(&ffs->ref, 1);
  1024. atomic_set(&ffs->opened, 0);
  1025. ffs->state = FFS_READ_DESCRIPTORS;
  1026. mutex_init(&ffs->mutex);
  1027. spin_lock_init(&ffs->eps_lock);
  1028. init_waitqueue_head(&ffs->ev.waitq);
  1029. init_completion(&ffs->ep0req_completion);
  1030. /* XXX REVISIT need to update it in some places, or do we? */
  1031. ffs->ev.can_stall = 1;
  1032. return ffs;
  1033. }
  1034. static void ffs_data_clear(struct ffs_data *ffs)
  1035. {
  1036. ENTER();
  1037. if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags))
  1038. functionfs_closed_callback(ffs);
  1039. BUG_ON(ffs->gadget);
  1040. if (ffs->epfiles)
  1041. ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count);
  1042. kfree(ffs->raw_descs);
  1043. kfree(ffs->raw_strings);
  1044. kfree(ffs->stringtabs);
  1045. }
  1046. static void ffs_data_reset(struct ffs_data *ffs)
  1047. {
  1048. ENTER();
  1049. ffs_data_clear(ffs);
  1050. ffs->epfiles = NULL;
  1051. ffs->raw_descs = NULL;
  1052. ffs->raw_strings = NULL;
  1053. ffs->stringtabs = NULL;
  1054. ffs->raw_descs_length = 0;
  1055. ffs->raw_fs_descs_length = 0;
  1056. ffs->fs_descs_count = 0;
  1057. ffs->hs_descs_count = 0;
  1058. ffs->strings_count = 0;
  1059. ffs->interfaces_count = 0;
  1060. ffs->eps_count = 0;
  1061. ffs->ev.count = 0;
  1062. ffs->state = FFS_READ_DESCRIPTORS;
  1063. ffs->setup_state = FFS_NO_SETUP;
  1064. ffs->flags = 0;
  1065. }
  1066. static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
  1067. {
  1068. struct usb_gadget_strings **lang;
  1069. int first_id;
  1070. ENTER();
  1071. if (WARN_ON(ffs->state != FFS_ACTIVE
  1072. || test_and_set_bit(FFS_FL_BOUND, &ffs->flags)))
  1073. return -EBADFD;
  1074. first_id = usb_string_ids_n(cdev, ffs->strings_count);
  1075. if (unlikely(first_id < 0))
  1076. return first_id;
  1077. ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
  1078. if (unlikely(!ffs->ep0req))
  1079. return -ENOMEM;
  1080. ffs->ep0req->complete = ffs_ep0_complete;
  1081. ffs->ep0req->context = ffs;
  1082. lang = ffs->stringtabs;
  1083. for (lang = ffs->stringtabs; *lang; ++lang) {
  1084. struct usb_string *str = (*lang)->strings;
  1085. int id = first_id;
  1086. for (; str->s; ++id, ++str)
  1087. str->id = id;
  1088. }
  1089. ffs->gadget = cdev->gadget;
  1090. ffs_data_get(ffs);
  1091. return 0;
  1092. }
  1093. static void functionfs_unbind(struct ffs_data *ffs)
  1094. {
  1095. ENTER();
  1096. if (!WARN_ON(!ffs->gadget)) {
  1097. usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req);
  1098. ffs->ep0req = NULL;
  1099. ffs->gadget = NULL;
  1100. ffs_data_put(ffs);
  1101. }
  1102. }
  1103. static int ffs_epfiles_create(struct ffs_data *ffs)
  1104. {
  1105. struct ffs_epfile *epfile, *epfiles;
  1106. unsigned i, count;
  1107. ENTER();
  1108. count = ffs->eps_count;
  1109. epfiles = kcalloc(count, sizeof(*epfiles), GFP_KERNEL);
  1110. if (!epfiles)
  1111. return -ENOMEM;
  1112. epfile = epfiles;
  1113. for (i = 1; i <= count; ++i, ++epfile) {
  1114. epfile->ffs = ffs;
  1115. mutex_init(&epfile->mutex);
  1116. init_waitqueue_head(&epfile->wait);
  1117. sprintf(epfiles->name, "ep%u", i);
  1118. if (!unlikely(ffs_sb_create_file(ffs->sb, epfiles->name, epfile,
  1119. &ffs_epfile_operations,
  1120. &epfile->dentry))) {
  1121. ffs_epfiles_destroy(epfiles, i - 1);
  1122. return -ENOMEM;
  1123. }
  1124. }
  1125. ffs->epfiles = epfiles;
  1126. return 0;
  1127. }
  1128. static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
  1129. {
  1130. struct ffs_epfile *epfile = epfiles;
  1131. ENTER();
  1132. for (; count; --count, ++epfile) {
  1133. BUG_ON(mutex_is_locked(&epfile->mutex) ||
  1134. waitqueue_active(&epfile->wait));
  1135. if (epfile->dentry) {
  1136. d_delete(epfile->dentry);
  1137. dput(epfile->dentry);
  1138. epfile->dentry = NULL;
  1139. }
  1140. }
  1141. kfree(epfiles);
  1142. }
  1143. static int functionfs_bind_config(struct usb_composite_dev *cdev,
  1144. struct usb_configuration *c,
  1145. struct ffs_data *ffs)
  1146. {
  1147. struct ffs_function *func;
  1148. int ret;
  1149. ENTER();
  1150. func = kzalloc(sizeof *func, GFP_KERNEL);
  1151. if (unlikely(!func))
  1152. return -ENOMEM;
  1153. func->function.name = "Function FS Gadget";
  1154. func->function.strings = ffs->stringtabs;
  1155. func->function.bind = ffs_func_bind;
  1156. func->function.unbind = ffs_func_unbind;
  1157. func->function.set_alt = ffs_func_set_alt;
  1158. func->function.disable = ffs_func_disable;
  1159. func->function.setup = ffs_func_setup;
  1160. func->function.suspend = ffs_func_suspend;
  1161. func->function.resume = ffs_func_resume;
  1162. func->conf = c;
  1163. func->gadget = cdev->gadget;
  1164. func->ffs = ffs;
  1165. ffs_data_get(ffs);
  1166. ret = usb_add_function(c, &func->function);
  1167. if (unlikely(ret))
  1168. ffs_func_free(func);
  1169. return ret;
  1170. }
  1171. static void ffs_func_free(struct ffs_function *func)
  1172. {
  1173. ENTER();
  1174. ffs_data_put(func->ffs);
  1175. kfree(func->eps);
  1176. /*
  1177. * eps and interfaces_nums are allocated in the same chunk so
  1178. * only one free is required. Descriptors are also allocated
  1179. * in the same chunk.
  1180. */
  1181. kfree(func);
  1182. }
  1183. static void ffs_func_eps_disable(struct ffs_function *func)
  1184. {
  1185. struct ffs_ep *ep = func->eps;
  1186. struct ffs_epfile *epfile = func->ffs->epfiles;
  1187. unsigned count = func->ffs->eps_count;
  1188. unsigned long flags;
  1189. spin_lock_irqsave(&func->ffs->eps_lock, flags);
  1190. do {
  1191. /* pending requests get nuked */
  1192. if (likely(ep->ep))
  1193. usb_ep_disable(ep->ep);
  1194. epfile->ep = NULL;
  1195. ++ep;
  1196. ++epfile;
  1197. } while (--count);
  1198. spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
  1199. }
  1200. static int ffs_func_eps_enable(struct ffs_function *func)
  1201. {
  1202. struct ffs_data *ffs = func->ffs;
  1203. struct ffs_ep *ep = func->eps;
  1204. struct ffs_epfile *epfile = ffs->epfiles;
  1205. unsigned count = ffs->eps_count;
  1206. unsigned long flags;
  1207. int ret = 0;
  1208. spin_lock_irqsave(&func->ffs->eps_lock, flags);
  1209. do {
  1210. struct usb_endpoint_descriptor *ds;
  1211. ds = ep->descs[ep->descs[1] ? 1 : 0];
  1212. ep->ep->driver_data = ep;
  1213. ep->ep->desc = ds;
  1214. ret = usb_ep_enable(ep->ep);
  1215. if (likely(!ret)) {
  1216. epfile->ep = ep;
  1217. epfile->in = usb_endpoint_dir_in(ds);
  1218. epfile->isoc = usb_endpoint_xfer_isoc(ds);
  1219. } else {
  1220. break;
  1221. }
  1222. wake_up(&epfile->wait);
  1223. ++ep;
  1224. ++epfile;
  1225. } while (--count);
  1226. spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
  1227. return ret;
  1228. }
  1229. /* Parsing and building descriptors and strings *****************************/
  1230. /*
  1231. * This validates if data pointed by data is a valid USB descriptor as
  1232. * well as record how many interfaces, endpoints and strings are
  1233. * required by given configuration. Returns address after the
  1234. * descriptor or NULL if data is invalid.
  1235. */
  1236. enum ffs_entity_type {
  1237. FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT
  1238. };
  1239. typedef int (*ffs_entity_callback)(enum ffs_entity_type entity,
  1240. u8 *valuep,
  1241. struct usb_descriptor_header *desc,
  1242. void *priv);
  1243. static int __must_check ffs_do_desc(char *data, unsigned len,
  1244. ffs_entity_callback entity, void *priv)
  1245. {
  1246. struct usb_descriptor_header *_ds = (void *)data;
  1247. u8 length;
  1248. int ret;
  1249. ENTER();
  1250. /* At least two bytes are required: length and type */
  1251. if (len < 2) {
  1252. pr_vdebug("descriptor too short\n");
  1253. return -EINVAL;
  1254. }
  1255. /* If we have at least as many bytes as the descriptor takes? */
  1256. length = _ds->bLength;
  1257. if (len < length) {
  1258. pr_vdebug("descriptor longer then available data\n");
  1259. return -EINVAL;
  1260. }
  1261. #define __entity_check_INTERFACE(val) 1
  1262. #define __entity_check_STRING(val) (val)
  1263. #define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
  1264. #define __entity(type, val) do { \
  1265. pr_vdebug("entity " #type "(%02x)\n", (val)); \
  1266. if (unlikely(!__entity_check_ ##type(val))) { \
  1267. pr_vdebug("invalid entity's value\n"); \
  1268. return -EINVAL; \
  1269. } \
  1270. ret = entity(FFS_ ##type, &val, _ds, priv); \
  1271. if (unlikely(ret < 0)) { \
  1272. pr_debug("entity " #type "(%02x); ret = %d\n", \
  1273. (val), ret); \
  1274. return ret; \
  1275. } \
  1276. } while (0)
  1277. /* Parse descriptor depending on type. */
  1278. switch (_ds->bDescriptorType) {
  1279. case USB_DT_DEVICE:
  1280. case USB_DT_CONFIG:
  1281. case USB_DT_STRING:
  1282. case USB_DT_DEVICE_QUALIFIER:
  1283. /* function can't have any of those */
  1284. pr_vdebug("descriptor reserved for gadget: %d\n",
  1285. _ds->bDescriptorType);
  1286. return -EINVAL;
  1287. case USB_DT_INTERFACE: {
  1288. struct usb_interface_descriptor *ds = (void *)_ds;
  1289. pr_vdebug("interface descriptor\n");
  1290. if (length != sizeof *ds)
  1291. goto inv_length;
  1292. __entity(INTERFACE, ds->bInterfaceNumber);
  1293. if (ds->iInterface)
  1294. __entity(STRING, ds->iInterface);
  1295. }
  1296. break;
  1297. case USB_DT_ENDPOINT: {
  1298. struct usb_endpoint_descriptor *ds = (void *)_ds;
  1299. pr_vdebug("endpoint descriptor\n");
  1300. if (length != USB_DT_ENDPOINT_SIZE &&
  1301. length != USB_DT_ENDPOINT_AUDIO_SIZE)
  1302. goto inv_length;
  1303. __entity(ENDPOINT, ds->bEndpointAddress);
  1304. }
  1305. break;
  1306. case USB_DT_OTG:
  1307. if (length != sizeof(struct usb_otg_descriptor))
  1308. goto inv_length;
  1309. break;
  1310. case USB_DT_INTERFACE_ASSOCIATION: {
  1311. struct usb_interface_assoc_descriptor *ds = (void *)_ds;
  1312. pr_vdebug("interface association descriptor\n");
  1313. if (length != sizeof *ds)
  1314. goto inv_length;
  1315. if (ds->iFunction)
  1316. __entity(STRING, ds->iFunction);
  1317. }
  1318. break;
  1319. case USB_DT_OTHER_SPEED_CONFIG:
  1320. case USB_DT_INTERFACE_POWER:
  1321. case USB_DT_DEBUG:
  1322. case USB_DT_SECURITY:
  1323. case USB_DT_CS_RADIO_CONTROL:
  1324. /* TODO */
  1325. pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType);
  1326. return -EINVAL;
  1327. default:
  1328. /* We should never be here */
  1329. pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType);
  1330. return -EINVAL;
  1331. inv_length:
  1332. pr_vdebug("invalid length: %d (descriptor %d)\n",
  1333. _ds->bLength, _ds->bDescriptorType);
  1334. return -EINVAL;
  1335. }
  1336. #undef __entity
  1337. #undef __entity_check_DESCRIPTOR
  1338. #undef __entity_check_INTERFACE
  1339. #undef __entity_check_STRING
  1340. #undef __entity_check_ENDPOINT
  1341. return length;
  1342. }
  1343. static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
  1344. ffs_entity_callback entity, void *priv)
  1345. {
  1346. const unsigned _len = len;
  1347. unsigned long num = 0;
  1348. ENTER();
  1349. for (;;) {
  1350. int ret;
  1351. if (num == count)
  1352. data = NULL;
  1353. /* Record "descriptor" entity */
  1354. ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv);
  1355. if (unlikely(ret < 0)) {
  1356. pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n",
  1357. num, ret);
  1358. return ret;
  1359. }
  1360. if (!data)
  1361. return _len - len;
  1362. ret = ffs_do_desc(data, len, entity, priv);
  1363. if (unlikely(ret < 0)) {
  1364. pr_debug("%s returns %d\n", __func__, ret);
  1365. return ret;
  1366. }
  1367. len -= ret;
  1368. data += ret;
  1369. ++num;
  1370. }
  1371. }
  1372. static int __ffs_data_do_entity(enum ffs_entity_type type,
  1373. u8 *valuep, struct usb_descriptor_header *desc,
  1374. void *priv)
  1375. {
  1376. struct ffs_data *ffs = priv;
  1377. ENTER();
  1378. switch (type) {
  1379. case FFS_DESCRIPTOR:
  1380. break;
  1381. case FFS_INTERFACE:
  1382. /*
  1383. * Interfaces are indexed from zero so if we
  1384. * encountered interface "n" then there are at least
  1385. * "n+1" interfaces.
  1386. */
  1387. if (*valuep >= ffs->interfaces_count)
  1388. ffs->interfaces_count = *valuep + 1;
  1389. break;
  1390. case FFS_STRING:
  1391. /*
  1392. * Strings are indexed from 1 (0 is magic ;) reserved
  1393. * for languages list or some such)
  1394. */
  1395. if (*valuep > ffs->strings_count)
  1396. ffs->strings_count = *valuep;
  1397. break;
  1398. case FFS_ENDPOINT:
  1399. /* Endpoints are indexed from 1 as well. */
  1400. if ((*valuep & USB_ENDPOINT_NUMBER_MASK) > ffs->eps_count)
  1401. ffs->eps_count = (*valuep & USB_ENDPOINT_NUMBER_MASK);
  1402. break;
  1403. }
  1404. return 0;
  1405. }
  1406. static int __ffs_data_got_descs(struct ffs_data *ffs,
  1407. char *const _data, size_t len)
  1408. {
  1409. unsigned fs_count, hs_count;
  1410. int fs_len, ret = -EINVAL;
  1411. char *data = _data;
  1412. ENTER();
  1413. if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_DESCRIPTORS_MAGIC ||
  1414. get_unaligned_le32(data + 4) != len))
  1415. goto error;
  1416. fs_count = get_unaligned_le32(data + 8);
  1417. hs_count = get_unaligned_le32(data + 12);
  1418. if (!fs_count && !hs_count)
  1419. goto einval;
  1420. data += 16;
  1421. len -= 16;
  1422. if (likely(fs_count)) {
  1423. fs_len = ffs_do_descs(fs_count, data, len,
  1424. __ffs_data_do_entity, ffs);
  1425. if (unlikely(fs_len < 0)) {
  1426. ret = fs_len;
  1427. goto error;
  1428. }
  1429. data += fs_len;
  1430. len -= fs_len;
  1431. } else {
  1432. fs_len = 0;
  1433. }
  1434. if (likely(hs_count)) {
  1435. ret = ffs_do_descs(hs_count, data, len,
  1436. __ffs_data_do_entity, ffs);
  1437. if (unlikely(ret < 0))
  1438. goto error;
  1439. } else {
  1440. ret = 0;
  1441. }
  1442. if (unlikely(len != ret))
  1443. goto einval;
  1444. ffs->raw_fs_descs_length = fs_len;
  1445. ffs->raw_descs_length = fs_len + ret;
  1446. ffs->raw_descs = _data;
  1447. ffs->fs_descs_count = fs_count;
  1448. ffs->hs_descs_count = hs_count;
  1449. return 0;
  1450. einval:
  1451. ret = -EINVAL;
  1452. error:
  1453. kfree(_data);
  1454. return ret;
  1455. }
  1456. static int __ffs_data_got_strings(struct ffs_data *ffs,
  1457. char *const _data, size_t len)
  1458. {
  1459. u32 str_count, needed_count, lang_count;
  1460. struct usb_gadget_strings **stringtabs, *t;
  1461. struct usb_string *strings, *s;
  1462. const char *data = _data;
  1463. ENTER();
  1464. if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC ||
  1465. get_unaligned_le32(data + 4) != len))
  1466. goto error;
  1467. str_count = get_unaligned_le32(data + 8);
  1468. lang_count = get_unaligned_le32(data + 12);
  1469. /* if one is zero the other must be zero */
  1470. if (unlikely(!str_count != !lang_count))
  1471. goto error;
  1472. /* Do we have at least as many strings as descriptors need? */
  1473. needed_count = ffs->strings_count;
  1474. if (unlikely(str_count < needed_count))
  1475. goto error;
  1476. /*
  1477. * If we don't need any strings just return and free all
  1478. * memory.
  1479. */
  1480. if (!needed_count) {
  1481. kfree(_data);
  1482. return 0;
  1483. }
  1484. /* Allocate everything in one chunk so there's less maintenance. */
  1485. {
  1486. struct {
  1487. struct usb_gadget_strings *stringtabs[lang_count + 1];
  1488. struct usb_gadget_strings stringtab[lang_count];
  1489. struct usb_string strings[lang_count*(needed_count+1)];
  1490. } *d;
  1491. unsigned i = 0;
  1492. d = kmalloc(sizeof *d, GFP_KERNEL);
  1493. if (unlikely(!d)) {
  1494. kfree(_data);
  1495. return -ENOMEM;
  1496. }
  1497. stringtabs = d->stringtabs;
  1498. t = d->stringtab;
  1499. i = lang_count;
  1500. do {
  1501. *stringtabs++ = t++;
  1502. } while (--i);
  1503. *stringtabs = NULL;
  1504. stringtabs = d->stringtabs;
  1505. t = d->stringtab;
  1506. s = d->strings;
  1507. strings = s;
  1508. }
  1509. /* For each language */
  1510. data += 16;
  1511. len -= 16;
  1512. do { /* lang_count > 0 so we can use do-while */
  1513. unsigned needed = needed_count;
  1514. if (unlikely(len < 3))
  1515. goto error_free;
  1516. t->language = get_unaligned_le16(data);
  1517. t->strings = s;
  1518. ++t;
  1519. data += 2;
  1520. len -= 2;
  1521. /* For each string */
  1522. do { /* str_count > 0 so we can use do-while */
  1523. size_t length = strnlen(data, len);
  1524. if (unlikely(length == len))
  1525. goto error_free;
  1526. /*
  1527. * User may provide more strings then we need,
  1528. * if that's the case we simply ignore the
  1529. * rest
  1530. */
  1531. if (likely(needed)) {
  1532. /*
  1533. * s->id will be set while adding
  1534. * function to configuration so for
  1535. * now just leave garbage here.
  1536. */
  1537. s->s = data;
  1538. --needed;
  1539. ++s;
  1540. }
  1541. data += length + 1;
  1542. len -= length + 1;
  1543. } while (--str_count);
  1544. s->id = 0; /* terminator */
  1545. s->s = NULL;
  1546. ++s;
  1547. } while (--lang_count);
  1548. /* Some garbage left? */
  1549. if (unlikely(len))
  1550. goto error_free;
  1551. /* Done! */
  1552. ffs->stringtabs = stringtabs;
  1553. ffs->raw_strings = _data;
  1554. return 0;
  1555. error_free:
  1556. kfree(stringtabs);
  1557. error:
  1558. kfree(_data);
  1559. return -EINVAL;
  1560. }
  1561. /* Events handling and management *******************************************/
  1562. static void __ffs_event_add(struct ffs_data *ffs,
  1563. enum usb_functionfs_event_type type)
  1564. {
  1565. enum usb_functionfs_event_type rem_type1, rem_type2 = type;
  1566. int neg = 0;
  1567. /*
  1568. * Abort any unhandled setup
  1569. *
  1570. * We do not need to worry about some cmpxchg() changing value
  1571. * of ffs->setup_state without holding the lock because when
  1572. * state is FFS_SETUP_PENDING cmpxchg() in several places in
  1573. * the source does nothing.
  1574. */
  1575. if (ffs->setup_state == FFS_SETUP_PENDING)
  1576. ffs->setup_state = FFS_SETUP_CANCELED;
  1577. switch (type) {
  1578. case FUNCTIONFS_RESUME:
  1579. rem_type2 = FUNCTIONFS_SUSPEND;
  1580. /* FALL THROUGH */
  1581. case FUNCTIONFS_SUSPEND:
  1582. case FUNCTIONFS_SETUP:
  1583. rem_type1 = type;
  1584. /* Discard all similar events */
  1585. break;
  1586. case FUNCTIONFS_BIND:
  1587. case FUNCTIONFS_UNBIND:
  1588. case FUNCTIONFS_DISABLE:
  1589. case FUNCTIONFS_ENABLE:
  1590. /* Discard everything other then power management. */
  1591. rem_type1 = FUNCTIONFS_SUSPEND;
  1592. rem_type2 = FUNCTIONFS_RESUME;
  1593. neg = 1;
  1594. break;
  1595. default:
  1596. BUG();
  1597. }
  1598. {
  1599. u8 *ev = ffs->ev.types, *out = ev;
  1600. unsigned n = ffs->ev.count;
  1601. for (; n; --n, ++ev)
  1602. if ((*ev == rem_type1 || *ev == rem_type2) == neg)
  1603. *out++ = *ev;
  1604. else
  1605. pr_vdebug("purging event %d\n", *ev);
  1606. ffs->ev.count = out - ffs->ev.types;
  1607. }
  1608. pr_vdebug("adding event %d\n", type);
  1609. ffs->ev.types[ffs->ev.count++] = type;
  1610. wake_up_locked(&ffs->ev.waitq);
  1611. }
  1612. static void ffs_event_add(struct ffs_data *ffs,
  1613. enum usb_functionfs_event_type type)
  1614. {
  1615. unsigned long flags;
  1616. spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
  1617. __ffs_event_add(ffs, type);
  1618. spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
  1619. }
  1620. /* Bind/unbind USB function hooks *******************************************/
  1621. static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
  1622. struct usb_descriptor_header *desc,
  1623. void *priv)
  1624. {
  1625. struct usb_endpoint_descriptor *ds = (void *)desc;
  1626. struct ffs_function *func = priv;
  1627. struct ffs_ep *ffs_ep;
  1628. /*
  1629. * If hs_descriptors is not NULL then we are reading hs
  1630. * descriptors now
  1631. */
  1632. const int isHS = func->function.hs_descriptors != NULL;
  1633. unsigned idx;
  1634. if (type != FFS_DESCRIPTOR)
  1635. return 0;
  1636. if (isHS)
  1637. func->function.hs_descriptors[(long)valuep] = desc;
  1638. else
  1639. func->function.descriptors[(long)valuep] = desc;
  1640. if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
  1641. return 0;
  1642. idx = (ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) - 1;
  1643. ffs_ep = func->eps + idx;
  1644. if (unlikely(ffs_ep->descs[isHS])) {
  1645. pr_vdebug("two %sspeed descriptors for EP %d\n",
  1646. isHS ? "high" : "full",
  1647. ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
  1648. return -EINVAL;
  1649. }
  1650. ffs_ep->descs[isHS] = ds;
  1651. ffs_dump_mem(": Original ep desc", ds, ds->bLength);
  1652. if (ffs_ep->ep) {
  1653. ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress;
  1654. if (!ds->wMaxPacketSize)
  1655. ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize;
  1656. } else {
  1657. struct usb_request *req;
  1658. struct usb_ep *ep;
  1659. pr_vdebug("autoconfig\n");
  1660. ep = usb_ep_autoconfig(func->gadget, ds);
  1661. if (unlikely(!ep))
  1662. return -ENOTSUPP;
  1663. ep->driver_data = func->eps + idx;
  1664. req = usb_ep_alloc_request(ep, GFP_KERNEL);
  1665. if (unlikely(!req))
  1666. return -ENOMEM;
  1667. ffs_ep->ep = ep;
  1668. ffs_ep->req = req;
  1669. func->eps_revmap[ds->bEndpointAddress &
  1670. USB_ENDPOINT_NUMBER_MASK] = idx + 1;
  1671. }
  1672. ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength);
  1673. return 0;
  1674. }
  1675. static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
  1676. struct usb_descriptor_header *desc,
  1677. void *priv)
  1678. {
  1679. struct ffs_function *func = priv;
  1680. unsigned idx;
  1681. u8 newValue;
  1682. switch (type) {
  1683. default:
  1684. case FFS_DESCRIPTOR:
  1685. /* Handled in previous pass by __ffs_func_bind_do_descs() */
  1686. return 0;
  1687. case FFS_INTERFACE:
  1688. idx = *valuep;
  1689. if (func->interfaces_nums[idx] < 0) {
  1690. int id = usb_interface_id(func->conf, &func->function);
  1691. if (unlikely(id < 0))
  1692. return id;
  1693. func->interfaces_nums[idx] = id;
  1694. }
  1695. newValue = func->interfaces_nums[idx];
  1696. break;
  1697. case FFS_STRING:
  1698. /* String' IDs are allocated when fsf_data is bound to cdev */
  1699. newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id;
  1700. break;
  1701. case FFS_ENDPOINT:
  1702. /*
  1703. * USB_DT_ENDPOINT are handled in
  1704. * __ffs_func_bind_do_descs().
  1705. */
  1706. if (desc->bDescriptorType == USB_DT_ENDPOINT)
  1707. return 0;
  1708. idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1;
  1709. if (unlikely(!func->eps[idx].ep))
  1710. return -EINVAL;
  1711. {
  1712. struct usb_endpoint_descriptor **descs;
  1713. descs = func->eps[idx].descs;
  1714. newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress;
  1715. }
  1716. break;
  1717. }
  1718. pr_vdebug("%02x -> %02x\n", *valuep, newValue);
  1719. *valuep = newValue;
  1720. return 0;
  1721. }
  1722. static int ffs_func_bind(struct usb_configuration *c,
  1723. struct usb_function *f)
  1724. {
  1725. struct ffs_function *func = ffs_func_from_usb(f);
  1726. struct ffs_data *ffs = func->ffs;
  1727. const int full = !!func->ffs->fs_descs_count;
  1728. const int high = gadget_is_dualspeed(func->gadget) &&
  1729. func->ffs->hs_descs_count;
  1730. int ret;
  1731. /* Make it a single chunk, less management later on */
  1732. struct {
  1733. struct ffs_ep eps[ffs->eps_count];
  1734. struct usb_descriptor_header
  1735. *fs_descs[full ? ffs->fs_descs_count + 1 : 0];
  1736. struct usb_descriptor_header
  1737. *hs_descs[high ? ffs->hs_descs_count + 1 : 0];
  1738. short inums[ffs->interfaces_count];
  1739. char raw_descs[high ? ffs->raw_descs_length
  1740. : ffs->raw_fs_descs_length];
  1741. } *data;
  1742. ENTER();
  1743. /* Only high speed but not supported by gadget? */
  1744. if (unlikely(!(full | high)))
  1745. return -ENOTSUPP;
  1746. /* Allocate */
  1747. data = kmalloc(sizeof *data, GFP_KERNEL);
  1748. if (unlikely(!data))
  1749. return -ENOMEM;
  1750. /* Zero */
  1751. memset(data->eps, 0, sizeof data->eps);
  1752. memcpy(data->raw_descs, ffs->raw_descs + 16, sizeof data->raw_descs);
  1753. memset(data->inums, 0xff, sizeof data->inums);
  1754. for (ret = ffs->eps_count; ret; --ret)
  1755. data->eps[ret].num = -1;
  1756. /* Save pointers */
  1757. func->eps = data->eps;
  1758. func->interfaces_nums = data->inums;
  1759. /*
  1760. * Go through all the endpoint descriptors and allocate
  1761. * endpoints first, so that later we can rewrite the endpoint
  1762. * numbers without worrying that it may be described later on.
  1763. */
  1764. if (likely(full)) {
  1765. func->function.descriptors = data->fs_descs;
  1766. ret = ffs_do_descs(ffs->fs_descs_count,
  1767. data->raw_descs,
  1768. sizeof data->raw_descs,
  1769. __ffs_func_bind_do_descs, func);
  1770. if (unlikely(ret < 0))
  1771. goto error;
  1772. } else {
  1773. ret = 0;
  1774. }
  1775. if (likely(high)) {
  1776. func->function.hs_descriptors = data->hs_descs;
  1777. ret = ffs_do_descs(ffs->hs_descs_count,
  1778. data->raw_descs + ret,
  1779. (sizeof data->raw_descs) - ret,
  1780. __ffs_func_bind_do_descs, func);
  1781. }
  1782. /*
  1783. * Now handle interface numbers allocation and interface and
  1784. * endpoint numbers rewriting. We can do that in one go
  1785. * now.
  1786. */
  1787. ret = ffs_do_descs(ffs->fs_descs_count +
  1788. (high ? ffs->hs_descs_count : 0),
  1789. data->raw_descs, sizeof data->raw_descs,
  1790. __ffs_func_bind_do_nums, func);
  1791. if (unlikely(ret < 0))
  1792. goto error;
  1793. /* And we're done */
  1794. ffs_event_add(ffs, FUNCTIONFS_BIND);
  1795. return 0;
  1796. error:
  1797. /* XXX Do we need to release all claimed endpoints here? */
  1798. return ret;
  1799. }
  1800. /* Other USB function hooks *************************************************/
  1801. static void ffs_func_unbind(struct usb_configuration *c,
  1802. struct usb_function *f)
  1803. {
  1804. struct ffs_function *func = ffs_func_from_usb(f);
  1805. struct ffs_data *ffs = func->ffs;
  1806. ENTER();
  1807. if (ffs->func == func) {
  1808. ffs_func_eps_disable(func);
  1809. ffs->func = NULL;
  1810. }
  1811. ffs_event_add(ffs, FUNCTIONFS_UNBIND);
  1812. ffs_func_free(func);
  1813. }
  1814. static int ffs_func_set_alt(struct usb_function *f,
  1815. unsigned interface, unsigned alt)
  1816. {
  1817. struct ffs_function *func = ffs_func_from_usb(f);
  1818. struct ffs_data *ffs = func->ffs;
  1819. int ret = 0, intf;
  1820. if (alt != (unsigned)-1) {
  1821. intf = ffs_func_revmap_intf(func, interface);
  1822. if (unlikely(intf < 0))
  1823. return intf;
  1824. }
  1825. if (ffs->func)
  1826. ffs_func_eps_disable(ffs->func);
  1827. if (ffs->state != FFS_ACTIVE)
  1828. return -ENODEV;
  1829. if (alt == (unsigned)-1) {
  1830. ffs->func = NULL;
  1831. ffs_event_add(ffs, FUNCTIONFS_DISABLE);
  1832. return 0;
  1833. }
  1834. ffs->func = func;
  1835. ret = ffs_func_eps_enable(func);
  1836. if (likely(ret >= 0))
  1837. ffs_event_add(ffs, FUNCTIONFS_ENABLE);
  1838. return ret;
  1839. }
  1840. static void ffs_func_disable(struct usb_function *f)
  1841. {
  1842. ffs_func_set_alt(f, 0, (unsigned)-1);
  1843. }
  1844. static int ffs_func_setup(struct usb_function *f,
  1845. const struct usb_ctrlrequest *creq)
  1846. {
  1847. struct ffs_function *func = ffs_func_from_usb(f);
  1848. struct ffs_data *ffs = func->ffs;
  1849. unsigned long flags;
  1850. int ret;
  1851. ENTER();
  1852. pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType);
  1853. pr_vdebug("creq->bRequest = %02x\n", creq->bRequest);
  1854. pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq->wValue));
  1855. pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq->wIndex));
  1856. pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq->wLength));
  1857. /*
  1858. * Most requests directed to interface go through here
  1859. * (notable exceptions are set/get interface) so we need to
  1860. * handle them. All other either handled by composite or
  1861. * passed to usb_configuration->setup() (if one is set). No
  1862. * matter, we will handle requests directed to endpoint here
  1863. * as well (as it's straightforward) but what to do with any
  1864. * other request?
  1865. */
  1866. if (ffs->state != FFS_ACTIVE)
  1867. return -ENODEV;
  1868. switch (creq->bRequestType & USB_RECIP_MASK) {
  1869. case USB_RECIP_INTERFACE:
  1870. ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex));
  1871. if (unlikely(ret < 0))
  1872. return ret;
  1873. break;
  1874. case USB_RECIP_ENDPOINT:
  1875. ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex));
  1876. if (unlikely(ret < 0))
  1877. return ret;
  1878. break;
  1879. default:
  1880. return -EOPNOTSUPP;
  1881. }
  1882. spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
  1883. ffs->ev.setup = *creq;
  1884. ffs->ev.setup.wIndex = cpu_to_le16(ret);
  1885. __ffs_event_add(ffs, FUNCTIONFS_SETUP);
  1886. spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
  1887. return 0;
  1888. }
  1889. static void ffs_func_suspend(struct usb_function *f)
  1890. {
  1891. ENTER();
  1892. ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND);
  1893. }
  1894. static void ffs_func_resume(struct usb_function *f)
  1895. {
  1896. ENTER();
  1897. ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME);
  1898. }
  1899. /* Endpoint and interface numbers reverse mapping ***************************/
  1900. static int ffs_func_revmap_ep(struct ffs_function *func, u8 num)
  1901. {
  1902. num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK];
  1903. return num ? num : -EDOM;
  1904. }
  1905. static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf)
  1906. {
  1907. short *nums = func->interfaces_nums;
  1908. unsigned count = func->ffs->interfaces_count;
  1909. for (; count; --count, ++nums) {
  1910. if (*nums >= 0 && *nums == intf)
  1911. return nums - func->interfaces_nums;
  1912. }
  1913. return -EDOM;
  1914. }
  1915. /* Misc helper functions ****************************************************/
  1916. static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
  1917. {
  1918. return nonblock
  1919. ? likely(mutex_trylock(mutex)) ? 0 : -EAGAIN
  1920. : mutex_lock_interruptible(mutex);
  1921. }
  1922. static char *ffs_prepare_buffer(const char * __user buf, size_t len)
  1923. {
  1924. char *data;
  1925. if (unlikely(!len))
  1926. return NULL;
  1927. data = kmalloc(len, GFP_KERNEL);
  1928. if (unlikely(!data))
  1929. return ERR_PTR(-ENOMEM);
  1930. if (unlikely(__copy_from_user(data, buf, len))) {
  1931. kfree(data);
  1932. return ERR_PTR(-EFAULT);
  1933. }
  1934. pr_vdebug("Buffer from user space:\n");
  1935. ffs_dump_mem("", data, len);
  1936. return data;
  1937. }