f_fs.c 54 KB

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