f_fs.c 54 KB

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