f_fs.c 54 KB

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