f_fs.c 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534
  1. /*
  2. * f_fs.c -- user mode file system API for USB composite function controllers
  3. *
  4. * Copyright (C) 2010 Samsung Electronics
  5. * Author: Michal Nazarewicz <mina86@mina86.com>
  6. *
  7. * Based on inode.c (GadgetFS) which was:
  8. * Copyright (C) 2003-2004 David Brownell
  9. * Copyright (C) 2003 Agilent Technologies
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. */
  16. /* #define DEBUG */
  17. /* #define VERBOSE_DEBUG */
  18. #include <linux/blkdev.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/export.h>
  21. #include <linux/hid.h>
  22. #include <linux/module.h>
  23. #include <linux/uio.h>
  24. #include <asm/unaligned.h>
  25. #include <linux/usb/composite.h>
  26. #include <linux/usb/functionfs.h>
  27. #include <linux/aio.h>
  28. #include <linux/mmu_context.h>
  29. #include <linux/poll.h>
  30. #include <linux/eventfd.h>
  31. #include "u_fs.h"
  32. #include "u_f.h"
  33. #include "u_os_desc.h"
  34. #include "configfs.h"
  35. #define FUNCTIONFS_MAGIC 0xa647361 /* Chosen by a honest dice roll ;) */
  36. /* Reference counter handling */
  37. static void ffs_data_get(struct ffs_data *ffs);
  38. static void ffs_data_put(struct ffs_data *ffs);
  39. /* Creates new ffs_data object. */
  40. static struct ffs_data *__must_check ffs_data_new(void) __attribute__((malloc));
  41. /* Opened counter handling. */
  42. static void ffs_data_opened(struct ffs_data *ffs);
  43. static void ffs_data_closed(struct ffs_data *ffs);
  44. /* Called with ffs->mutex held; take over ownership of data. */
  45. static int __must_check
  46. __ffs_data_got_descs(struct ffs_data *ffs, char *data, size_t len);
  47. static int __must_check
  48. __ffs_data_got_strings(struct ffs_data *ffs, char *data, size_t len);
  49. /* The function structure ***************************************************/
  50. struct ffs_ep;
  51. struct ffs_function {
  52. struct usb_configuration *conf;
  53. struct usb_gadget *gadget;
  54. struct ffs_data *ffs;
  55. struct ffs_ep *eps;
  56. u8 eps_revmap[16];
  57. short *interfaces_nums;
  58. struct usb_function function;
  59. };
  60. static struct ffs_function *ffs_func_from_usb(struct usb_function *f)
  61. {
  62. return container_of(f, struct ffs_function, function);
  63. }
  64. static inline enum ffs_setup_state
  65. ffs_setup_state_clear_cancelled(struct ffs_data *ffs)
  66. {
  67. return (enum ffs_setup_state)
  68. cmpxchg(&ffs->setup_state, FFS_SETUP_CANCELLED, FFS_NO_SETUP);
  69. }
  70. static void ffs_func_eps_disable(struct ffs_function *func);
  71. static int __must_check ffs_func_eps_enable(struct ffs_function *func);
  72. static int ffs_func_bind(struct usb_configuration *,
  73. struct usb_function *);
  74. static int ffs_func_set_alt(struct usb_function *, unsigned, unsigned);
  75. static void ffs_func_disable(struct usb_function *);
  76. static int ffs_func_setup(struct usb_function *,
  77. const struct usb_ctrlrequest *);
  78. static void ffs_func_suspend(struct usb_function *);
  79. static void ffs_func_resume(struct usb_function *);
  80. static int ffs_func_revmap_ep(struct ffs_function *func, u8 num);
  81. static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf);
  82. /* The endpoints structures *************************************************/
  83. struct ffs_ep {
  84. struct usb_ep *ep; /* P: ffs->eps_lock */
  85. struct usb_request *req; /* P: epfile->mutex */
  86. /* [0]: full speed, [1]: high speed, [2]: super speed */
  87. struct usb_endpoint_descriptor *descs[3];
  88. u8 num;
  89. int status; /* P: epfile->mutex */
  90. };
  91. struct ffs_epfile {
  92. /* Protects ep->ep and ep->req. */
  93. struct mutex mutex;
  94. wait_queue_head_t wait;
  95. struct ffs_data *ffs;
  96. struct ffs_ep *ep; /* P: ffs->eps_lock */
  97. struct dentry *dentry;
  98. char name[5];
  99. unsigned char in; /* P: ffs->eps_lock */
  100. unsigned char isoc; /* P: ffs->eps_lock */
  101. unsigned char _pad;
  102. };
  103. /* ffs_io_data structure ***************************************************/
  104. struct ffs_io_data {
  105. bool aio;
  106. bool read;
  107. struct kiocb *kiocb;
  108. struct iov_iter data;
  109. const void *to_free;
  110. char *buf;
  111. struct mm_struct *mm;
  112. struct work_struct work;
  113. struct usb_ep *ep;
  114. struct usb_request *req;
  115. struct ffs_data *ffs;
  116. };
  117. struct ffs_desc_helper {
  118. struct ffs_data *ffs;
  119. unsigned interfaces_count;
  120. unsigned eps_count;
  121. };
  122. static int __must_check ffs_epfiles_create(struct ffs_data *ffs);
  123. static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count);
  124. static struct dentry *
  125. ffs_sb_create_file(struct super_block *sb, const char *name, void *data,
  126. const struct file_operations *fops);
  127. /* Devices management *******************************************************/
  128. DEFINE_MUTEX(ffs_lock);
  129. EXPORT_SYMBOL_GPL(ffs_lock);
  130. static struct ffs_dev *_ffs_find_dev(const char *name);
  131. static struct ffs_dev *_ffs_alloc_dev(void);
  132. static int _ffs_name_dev(struct ffs_dev *dev, const char *name);
  133. static void _ffs_free_dev(struct ffs_dev *dev);
  134. static void *ffs_acquire_dev(const char *dev_name);
  135. static void ffs_release_dev(struct ffs_data *ffs_data);
  136. static int ffs_ready(struct ffs_data *ffs);
  137. static void ffs_closed(struct ffs_data *ffs);
  138. /* Misc helper functions ****************************************************/
  139. static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
  140. __attribute__((warn_unused_result, nonnull));
  141. static char *ffs_prepare_buffer(const char __user *buf, size_t len)
  142. __attribute__((warn_unused_result, nonnull));
  143. /* Control file aka ep0 *****************************************************/
  144. static void ffs_ep0_complete(struct usb_ep *ep, struct usb_request *req)
  145. {
  146. struct ffs_data *ffs = req->context;
  147. complete_all(&ffs->ep0req_completion);
  148. }
  149. static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)
  150. {
  151. struct usb_request *req = ffs->ep0req;
  152. int ret;
  153. req->zero = len < le16_to_cpu(ffs->ev.setup.wLength);
  154. spin_unlock_irq(&ffs->ev.waitq.lock);
  155. req->buf = data;
  156. req->length = len;
  157. /*
  158. * UDC layer requires to provide a buffer even for ZLP, but should
  159. * not use it at all. Let's provide some poisoned pointer to catch
  160. * possible bug in the driver.
  161. */
  162. if (req->buf == NULL)
  163. req->buf = (void *)0xDEADBABE;
  164. reinit_completion(&ffs->ep0req_completion);
  165. ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC);
  166. if (unlikely(ret < 0))
  167. return ret;
  168. ret = wait_for_completion_interruptible(&ffs->ep0req_completion);
  169. if (unlikely(ret)) {
  170. usb_ep_dequeue(ffs->gadget->ep0, req);
  171. return -EINTR;
  172. }
  173. ffs->setup_state = FFS_NO_SETUP;
  174. return req->status ? req->status : req->actual;
  175. }
  176. static int __ffs_ep0_stall(struct ffs_data *ffs)
  177. {
  178. if (ffs->ev.can_stall) {
  179. pr_vdebug("ep0 stall\n");
  180. usb_ep_set_halt(ffs->gadget->ep0);
  181. ffs->setup_state = FFS_NO_SETUP;
  182. return -EL2HLT;
  183. } else {
  184. pr_debug("bogus ep0 stall!\n");
  185. return -ESRCH;
  186. }
  187. }
  188. static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
  189. size_t len, loff_t *ptr)
  190. {
  191. struct ffs_data *ffs = file->private_data;
  192. ssize_t ret;
  193. char *data;
  194. ENTER();
  195. /* Fast check if setup was canceled */
  196. if (ffs_setup_state_clear_cancelled(ffs) == FFS_SETUP_CANCELLED)
  197. return -EIDRM;
  198. /* Acquire mutex */
  199. ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
  200. if (unlikely(ret < 0))
  201. return ret;
  202. /* Check state */
  203. switch (ffs->state) {
  204. case FFS_READ_DESCRIPTORS:
  205. case FFS_READ_STRINGS:
  206. /* Copy data */
  207. if (unlikely(len < 16)) {
  208. ret = -EINVAL;
  209. break;
  210. }
  211. data = ffs_prepare_buffer(buf, len);
  212. if (IS_ERR(data)) {
  213. ret = PTR_ERR(data);
  214. break;
  215. }
  216. /* Handle data */
  217. if (ffs->state == FFS_READ_DESCRIPTORS) {
  218. pr_info("read descriptors\n");
  219. ret = __ffs_data_got_descs(ffs, data, len);
  220. if (unlikely(ret < 0))
  221. break;
  222. ffs->state = FFS_READ_STRINGS;
  223. ret = len;
  224. } else {
  225. pr_info("read strings\n");
  226. ret = __ffs_data_got_strings(ffs, data, len);
  227. if (unlikely(ret < 0))
  228. break;
  229. ret = ffs_epfiles_create(ffs);
  230. if (unlikely(ret)) {
  231. ffs->state = FFS_CLOSING;
  232. break;
  233. }
  234. ffs->state = FFS_ACTIVE;
  235. mutex_unlock(&ffs->mutex);
  236. ret = ffs_ready(ffs);
  237. if (unlikely(ret < 0)) {
  238. ffs->state = FFS_CLOSING;
  239. return ret;
  240. }
  241. return len;
  242. }
  243. break;
  244. case FFS_ACTIVE:
  245. data = NULL;
  246. /*
  247. * We're called from user space, we can use _irq
  248. * rather then _irqsave
  249. */
  250. spin_lock_irq(&ffs->ev.waitq.lock);
  251. switch (ffs_setup_state_clear_cancelled(ffs)) {
  252. case FFS_SETUP_CANCELLED:
  253. ret = -EIDRM;
  254. goto done_spin;
  255. case FFS_NO_SETUP:
  256. ret = -ESRCH;
  257. goto done_spin;
  258. case FFS_SETUP_PENDING:
  259. break;
  260. }
  261. /* FFS_SETUP_PENDING */
  262. if (!(ffs->ev.setup.bRequestType & USB_DIR_IN)) {
  263. spin_unlock_irq(&ffs->ev.waitq.lock);
  264. ret = __ffs_ep0_stall(ffs);
  265. break;
  266. }
  267. /* FFS_SETUP_PENDING and not stall */
  268. len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
  269. spin_unlock_irq(&ffs->ev.waitq.lock);
  270. data = ffs_prepare_buffer(buf, len);
  271. if (IS_ERR(data)) {
  272. ret = PTR_ERR(data);
  273. break;
  274. }
  275. spin_lock_irq(&ffs->ev.waitq.lock);
  276. /*
  277. * We are guaranteed to be still in FFS_ACTIVE state
  278. * but the state of setup could have changed from
  279. * FFS_SETUP_PENDING to FFS_SETUP_CANCELLED so we need
  280. * to check for that. If that happened we copied data
  281. * from user space in vain but it's unlikely.
  282. *
  283. * For sure we are not in FFS_NO_SETUP since this is
  284. * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP
  285. * transition can be performed and it's protected by
  286. * mutex.
  287. */
  288. if (ffs_setup_state_clear_cancelled(ffs) ==
  289. FFS_SETUP_CANCELLED) {
  290. ret = -EIDRM;
  291. done_spin:
  292. spin_unlock_irq(&ffs->ev.waitq.lock);
  293. } else {
  294. /* unlocks spinlock */
  295. ret = __ffs_ep0_queue_wait(ffs, data, len);
  296. }
  297. kfree(data);
  298. break;
  299. default:
  300. ret = -EBADFD;
  301. break;
  302. }
  303. mutex_unlock(&ffs->mutex);
  304. return ret;
  305. }
  306. /* Called with ffs->ev.waitq.lock and ffs->mutex held, both released on exit. */
  307. static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf,
  308. size_t n)
  309. {
  310. /*
  311. * n cannot be bigger than ffs->ev.count, which cannot be bigger than
  312. * size of ffs->ev.types array (which is four) so that's how much space
  313. * we reserve.
  314. */
  315. struct usb_functionfs_event events[ARRAY_SIZE(ffs->ev.types)];
  316. const size_t size = n * sizeof *events;
  317. unsigned i = 0;
  318. memset(events, 0, size);
  319. do {
  320. events[i].type = ffs->ev.types[i];
  321. if (events[i].type == FUNCTIONFS_SETUP) {
  322. events[i].u.setup = ffs->ev.setup;
  323. ffs->setup_state = FFS_SETUP_PENDING;
  324. }
  325. } while (++i < n);
  326. ffs->ev.count -= n;
  327. if (ffs->ev.count)
  328. memmove(ffs->ev.types, ffs->ev.types + n,
  329. ffs->ev.count * sizeof *ffs->ev.types);
  330. spin_unlock_irq(&ffs->ev.waitq.lock);
  331. mutex_unlock(&ffs->mutex);
  332. return unlikely(copy_to_user(buf, events, size)) ? -EFAULT : size;
  333. }
  334. static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
  335. size_t len, loff_t *ptr)
  336. {
  337. struct ffs_data *ffs = file->private_data;
  338. char *data = NULL;
  339. size_t n;
  340. int ret;
  341. ENTER();
  342. /* Fast check if setup was canceled */
  343. if (ffs_setup_state_clear_cancelled(ffs) == FFS_SETUP_CANCELLED)
  344. return -EIDRM;
  345. /* Acquire mutex */
  346. ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
  347. if (unlikely(ret < 0))
  348. return ret;
  349. /* Check state */
  350. if (ffs->state != FFS_ACTIVE) {
  351. ret = -EBADFD;
  352. goto done_mutex;
  353. }
  354. /*
  355. * We're called from user space, we can use _irq rather then
  356. * _irqsave
  357. */
  358. spin_lock_irq(&ffs->ev.waitq.lock);
  359. switch (ffs_setup_state_clear_cancelled(ffs)) {
  360. case FFS_SETUP_CANCELLED:
  361. ret = -EIDRM;
  362. break;
  363. case FFS_NO_SETUP:
  364. n = len / sizeof(struct usb_functionfs_event);
  365. if (unlikely(!n)) {
  366. ret = -EINVAL;
  367. break;
  368. }
  369. if ((file->f_flags & O_NONBLOCK) && !ffs->ev.count) {
  370. ret = -EAGAIN;
  371. break;
  372. }
  373. if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq,
  374. ffs->ev.count)) {
  375. ret = -EINTR;
  376. break;
  377. }
  378. return __ffs_ep0_read_events(ffs, buf,
  379. min(n, (size_t)ffs->ev.count));
  380. case FFS_SETUP_PENDING:
  381. if (ffs->ev.setup.bRequestType & USB_DIR_IN) {
  382. spin_unlock_irq(&ffs->ev.waitq.lock);
  383. ret = __ffs_ep0_stall(ffs);
  384. goto done_mutex;
  385. }
  386. len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
  387. spin_unlock_irq(&ffs->ev.waitq.lock);
  388. if (likely(len)) {
  389. data = kmalloc(len, GFP_KERNEL);
  390. if (unlikely(!data)) {
  391. ret = -ENOMEM;
  392. goto done_mutex;
  393. }
  394. }
  395. spin_lock_irq(&ffs->ev.waitq.lock);
  396. /* See ffs_ep0_write() */
  397. if (ffs_setup_state_clear_cancelled(ffs) ==
  398. FFS_SETUP_CANCELLED) {
  399. ret = -EIDRM;
  400. break;
  401. }
  402. /* unlocks spinlock */
  403. ret = __ffs_ep0_queue_wait(ffs, data, len);
  404. if (likely(ret > 0) && unlikely(copy_to_user(buf, data, len)))
  405. ret = -EFAULT;
  406. goto done_mutex;
  407. default:
  408. ret = -EBADFD;
  409. break;
  410. }
  411. spin_unlock_irq(&ffs->ev.waitq.lock);
  412. done_mutex:
  413. mutex_unlock(&ffs->mutex);
  414. kfree(data);
  415. return ret;
  416. }
  417. static int ffs_ep0_open(struct inode *inode, struct file *file)
  418. {
  419. struct ffs_data *ffs = inode->i_private;
  420. ENTER();
  421. if (unlikely(ffs->state == FFS_CLOSING))
  422. return -EBUSY;
  423. file->private_data = ffs;
  424. ffs_data_opened(ffs);
  425. return 0;
  426. }
  427. static int ffs_ep0_release(struct inode *inode, struct file *file)
  428. {
  429. struct ffs_data *ffs = file->private_data;
  430. ENTER();
  431. ffs_data_closed(ffs);
  432. return 0;
  433. }
  434. static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)
  435. {
  436. struct ffs_data *ffs = file->private_data;
  437. struct usb_gadget *gadget = ffs->gadget;
  438. long ret;
  439. ENTER();
  440. if (code == FUNCTIONFS_INTERFACE_REVMAP) {
  441. struct ffs_function *func = ffs->func;
  442. ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV;
  443. } else if (gadget && gadget->ops->ioctl) {
  444. ret = gadget->ops->ioctl(gadget, code, value);
  445. } else {
  446. ret = -ENOTTY;
  447. }
  448. return ret;
  449. }
  450. static unsigned int ffs_ep0_poll(struct file *file, poll_table *wait)
  451. {
  452. struct ffs_data *ffs = file->private_data;
  453. unsigned int mask = POLLWRNORM;
  454. int ret;
  455. poll_wait(file, &ffs->ev.waitq, wait);
  456. ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
  457. if (unlikely(ret < 0))
  458. return mask;
  459. switch (ffs->state) {
  460. case FFS_READ_DESCRIPTORS:
  461. case FFS_READ_STRINGS:
  462. mask |= POLLOUT;
  463. break;
  464. case FFS_ACTIVE:
  465. switch (ffs->setup_state) {
  466. case FFS_NO_SETUP:
  467. if (ffs->ev.count)
  468. mask |= POLLIN;
  469. break;
  470. case FFS_SETUP_PENDING:
  471. case FFS_SETUP_CANCELLED:
  472. mask |= (POLLIN | POLLOUT);
  473. break;
  474. }
  475. case FFS_CLOSING:
  476. break;
  477. case FFS_DEACTIVATED:
  478. break;
  479. }
  480. mutex_unlock(&ffs->mutex);
  481. return mask;
  482. }
  483. static const struct file_operations ffs_ep0_operations = {
  484. .llseek = no_llseek,
  485. .open = ffs_ep0_open,
  486. .write = ffs_ep0_write,
  487. .read = ffs_ep0_read,
  488. .release = ffs_ep0_release,
  489. .unlocked_ioctl = ffs_ep0_ioctl,
  490. .poll = ffs_ep0_poll,
  491. };
  492. /* "Normal" endpoints operations ********************************************/
  493. static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
  494. {
  495. ENTER();
  496. if (likely(req->context)) {
  497. struct ffs_ep *ep = _ep->driver_data;
  498. ep->status = req->status ? req->status : req->actual;
  499. complete(req->context);
  500. }
  501. }
  502. static void ffs_user_copy_worker(struct work_struct *work)
  503. {
  504. struct ffs_io_data *io_data = container_of(work, struct ffs_io_data,
  505. work);
  506. int ret = io_data->req->status ? io_data->req->status :
  507. io_data->req->actual;
  508. bool kiocb_has_eventfd = io_data->kiocb->ki_flags & IOCB_EVENTFD;
  509. if (io_data->read && ret > 0) {
  510. mm_segment_t oldfs = get_fs();
  511. set_fs(USER_DS);
  512. use_mm(io_data->mm);
  513. ret = copy_to_iter(io_data->buf, ret, &io_data->data);
  514. if (ret != io_data->req->actual && iov_iter_count(&io_data->data))
  515. ret = -EFAULT;
  516. unuse_mm(io_data->mm);
  517. set_fs(oldfs);
  518. }
  519. io_data->kiocb->ki_complete(io_data->kiocb, ret, ret);
  520. if (io_data->ffs->ffs_eventfd && !kiocb_has_eventfd)
  521. eventfd_signal(io_data->ffs->ffs_eventfd, 1);
  522. usb_ep_free_request(io_data->ep, io_data->req);
  523. if (io_data->read)
  524. kfree(io_data->to_free);
  525. kfree(io_data->buf);
  526. kfree(io_data);
  527. }
  528. static void ffs_epfile_async_io_complete(struct usb_ep *_ep,
  529. struct usb_request *req)
  530. {
  531. struct ffs_io_data *io_data = req->context;
  532. ENTER();
  533. INIT_WORK(&io_data->work, ffs_user_copy_worker);
  534. schedule_work(&io_data->work);
  535. }
  536. static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data)
  537. {
  538. struct ffs_epfile *epfile = file->private_data;
  539. struct ffs_ep *ep;
  540. char *data = NULL;
  541. ssize_t ret, data_len = -EINVAL;
  542. int halt;
  543. /* Are we still active? */
  544. if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) {
  545. ret = -ENODEV;
  546. goto error;
  547. }
  548. /* Wait for endpoint to be enabled */
  549. ep = epfile->ep;
  550. if (!ep) {
  551. if (file->f_flags & O_NONBLOCK) {
  552. ret = -EAGAIN;
  553. goto error;
  554. }
  555. ret = wait_event_interruptible(epfile->wait, (ep = epfile->ep));
  556. if (ret) {
  557. ret = -EINTR;
  558. goto error;
  559. }
  560. }
  561. /* Do we halt? */
  562. halt = (!io_data->read == !epfile->in);
  563. if (halt && epfile->isoc) {
  564. ret = -EINVAL;
  565. goto error;
  566. }
  567. /* Allocate & copy */
  568. if (!halt) {
  569. /*
  570. * if we _do_ wait above, the epfile->ffs->gadget might be NULL
  571. * before the waiting completes, so do not assign to 'gadget' earlier
  572. */
  573. struct usb_gadget *gadget = epfile->ffs->gadget;
  574. size_t copied;
  575. spin_lock_irq(&epfile->ffs->eps_lock);
  576. /* In the meantime, endpoint got disabled or changed. */
  577. if (epfile->ep != ep) {
  578. spin_unlock_irq(&epfile->ffs->eps_lock);
  579. return -ESHUTDOWN;
  580. }
  581. data_len = iov_iter_count(&io_data->data);
  582. /*
  583. * Controller may require buffer size to be aligned to
  584. * maxpacketsize of an out endpoint.
  585. */
  586. if (io_data->read)
  587. data_len = usb_ep_align_maybe(gadget, ep->ep, data_len);
  588. spin_unlock_irq(&epfile->ffs->eps_lock);
  589. data = kmalloc(data_len, GFP_KERNEL);
  590. if (unlikely(!data))
  591. return -ENOMEM;
  592. if (!io_data->read) {
  593. copied = copy_from_iter(data, data_len, &io_data->data);
  594. if (copied != data_len) {
  595. ret = -EFAULT;
  596. goto error;
  597. }
  598. }
  599. }
  600. /* We will be using request */
  601. ret = ffs_mutex_lock(&epfile->mutex, file->f_flags & O_NONBLOCK);
  602. if (unlikely(ret))
  603. goto error;
  604. spin_lock_irq(&epfile->ffs->eps_lock);
  605. if (epfile->ep != ep) {
  606. /* In the meantime, endpoint got disabled or changed. */
  607. ret = -ESHUTDOWN;
  608. spin_unlock_irq(&epfile->ffs->eps_lock);
  609. } else if (halt) {
  610. /* Halt */
  611. if (likely(epfile->ep == ep) && !WARN_ON(!ep->ep))
  612. usb_ep_set_halt(ep->ep);
  613. spin_unlock_irq(&epfile->ffs->eps_lock);
  614. ret = -EBADMSG;
  615. } else {
  616. /* Fire the request */
  617. struct usb_request *req;
  618. /*
  619. * Sanity Check: even though data_len can't be used
  620. * uninitialized at the time I write this comment, some
  621. * compilers complain about this situation.
  622. * In order to keep the code clean from warnings, data_len is
  623. * being initialized to -EINVAL during its declaration, which
  624. * means we can't rely on compiler anymore to warn no future
  625. * changes won't result in data_len being used uninitialized.
  626. * For such reason, we're adding this redundant sanity check
  627. * here.
  628. */
  629. if (unlikely(data_len == -EINVAL)) {
  630. WARN(1, "%s: data_len == -EINVAL\n", __func__);
  631. ret = -EINVAL;
  632. goto error_lock;
  633. }
  634. if (io_data->aio) {
  635. req = usb_ep_alloc_request(ep->ep, GFP_ATOMIC);
  636. if (unlikely(!req))
  637. goto error_lock;
  638. req->buf = data;
  639. req->length = data_len;
  640. io_data->buf = data;
  641. io_data->ep = ep->ep;
  642. io_data->req = req;
  643. io_data->ffs = epfile->ffs;
  644. req->context = io_data;
  645. req->complete = ffs_epfile_async_io_complete;
  646. ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
  647. if (unlikely(ret)) {
  648. usb_ep_free_request(ep->ep, req);
  649. goto error_lock;
  650. }
  651. ret = -EIOCBQUEUED;
  652. spin_unlock_irq(&epfile->ffs->eps_lock);
  653. } else {
  654. DECLARE_COMPLETION_ONSTACK(done);
  655. req = ep->req;
  656. req->buf = data;
  657. req->length = data_len;
  658. req->context = &done;
  659. req->complete = ffs_epfile_io_complete;
  660. ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
  661. spin_unlock_irq(&epfile->ffs->eps_lock);
  662. if (unlikely(ret < 0)) {
  663. /* nop */
  664. } else if (unlikely(
  665. wait_for_completion_interruptible(&done))) {
  666. ret = -EINTR;
  667. usb_ep_dequeue(ep->ep, req);
  668. } else {
  669. /*
  670. * XXX We may end up silently droping data
  671. * here. Since data_len (i.e. req->length) may
  672. * be bigger than len (after being rounded up
  673. * to maxpacketsize), we may end up with more
  674. * data then user space has space for.
  675. */
  676. ret = ep->status;
  677. if (io_data->read && ret > 0) {
  678. ret = copy_to_iter(data, ret, &io_data->data);
  679. if (!ret)
  680. ret = -EFAULT;
  681. }
  682. }
  683. kfree(data);
  684. }
  685. }
  686. mutex_unlock(&epfile->mutex);
  687. return ret;
  688. error_lock:
  689. spin_unlock_irq(&epfile->ffs->eps_lock);
  690. mutex_unlock(&epfile->mutex);
  691. error:
  692. kfree(data);
  693. return ret;
  694. }
  695. static int
  696. ffs_epfile_open(struct inode *inode, struct file *file)
  697. {
  698. struct ffs_epfile *epfile = inode->i_private;
  699. ENTER();
  700. if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
  701. return -ENODEV;
  702. file->private_data = epfile;
  703. ffs_data_opened(epfile->ffs);
  704. return 0;
  705. }
  706. static int ffs_aio_cancel(struct kiocb *kiocb)
  707. {
  708. struct ffs_io_data *io_data = kiocb->private;
  709. struct ffs_epfile *epfile = kiocb->ki_filp->private_data;
  710. int value;
  711. ENTER();
  712. spin_lock_irq(&epfile->ffs->eps_lock);
  713. if (likely(io_data && io_data->ep && io_data->req))
  714. value = usb_ep_dequeue(io_data->ep, io_data->req);
  715. else
  716. value = -EINVAL;
  717. spin_unlock_irq(&epfile->ffs->eps_lock);
  718. return value;
  719. }
  720. static ssize_t ffs_epfile_write_iter(struct kiocb *kiocb, struct iov_iter *from)
  721. {
  722. struct ffs_io_data io_data, *p = &io_data;
  723. ssize_t res;
  724. ENTER();
  725. if (!is_sync_kiocb(kiocb)) {
  726. p = kmalloc(sizeof(io_data), GFP_KERNEL);
  727. if (unlikely(!p))
  728. return -ENOMEM;
  729. p->aio = true;
  730. } else {
  731. p->aio = false;
  732. }
  733. p->read = false;
  734. p->kiocb = kiocb;
  735. p->data = *from;
  736. p->mm = current->mm;
  737. kiocb->private = p;
  738. if (p->aio)
  739. kiocb_set_cancel_fn(kiocb, ffs_aio_cancel);
  740. res = ffs_epfile_io(kiocb->ki_filp, p);
  741. if (res == -EIOCBQUEUED)
  742. return res;
  743. if (p->aio)
  744. kfree(p);
  745. else
  746. *from = p->data;
  747. return res;
  748. }
  749. static ssize_t ffs_epfile_read_iter(struct kiocb *kiocb, struct iov_iter *to)
  750. {
  751. struct ffs_io_data io_data, *p = &io_data;
  752. ssize_t res;
  753. ENTER();
  754. if (!is_sync_kiocb(kiocb)) {
  755. p = kmalloc(sizeof(io_data), GFP_KERNEL);
  756. if (unlikely(!p))
  757. return -ENOMEM;
  758. p->aio = true;
  759. } else {
  760. p->aio = false;
  761. }
  762. p->read = true;
  763. p->kiocb = kiocb;
  764. if (p->aio) {
  765. p->to_free = dup_iter(&p->data, to, GFP_KERNEL);
  766. if (!p->to_free) {
  767. kfree(p);
  768. return -ENOMEM;
  769. }
  770. } else {
  771. p->data = *to;
  772. p->to_free = NULL;
  773. }
  774. p->mm = current->mm;
  775. kiocb->private = p;
  776. if (p->aio)
  777. kiocb_set_cancel_fn(kiocb, ffs_aio_cancel);
  778. res = ffs_epfile_io(kiocb->ki_filp, p);
  779. if (res == -EIOCBQUEUED)
  780. return res;
  781. if (p->aio) {
  782. kfree(p->to_free);
  783. kfree(p);
  784. } else {
  785. *to = p->data;
  786. }
  787. return res;
  788. }
  789. static int
  790. ffs_epfile_release(struct inode *inode, struct file *file)
  791. {
  792. struct ffs_epfile *epfile = inode->i_private;
  793. ENTER();
  794. ffs_data_closed(epfile->ffs);
  795. return 0;
  796. }
  797. static long ffs_epfile_ioctl(struct file *file, unsigned code,
  798. unsigned long value)
  799. {
  800. struct ffs_epfile *epfile = file->private_data;
  801. int ret;
  802. ENTER();
  803. if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
  804. return -ENODEV;
  805. spin_lock_irq(&epfile->ffs->eps_lock);
  806. if (likely(epfile->ep)) {
  807. switch (code) {
  808. case FUNCTIONFS_FIFO_STATUS:
  809. ret = usb_ep_fifo_status(epfile->ep->ep);
  810. break;
  811. case FUNCTIONFS_FIFO_FLUSH:
  812. usb_ep_fifo_flush(epfile->ep->ep);
  813. ret = 0;
  814. break;
  815. case FUNCTIONFS_CLEAR_HALT:
  816. ret = usb_ep_clear_halt(epfile->ep->ep);
  817. break;
  818. case FUNCTIONFS_ENDPOINT_REVMAP:
  819. ret = epfile->ep->num;
  820. break;
  821. case FUNCTIONFS_ENDPOINT_DESC:
  822. {
  823. int desc_idx;
  824. struct usb_endpoint_descriptor *desc;
  825. switch (epfile->ffs->gadget->speed) {
  826. case USB_SPEED_SUPER:
  827. desc_idx = 2;
  828. break;
  829. case USB_SPEED_HIGH:
  830. desc_idx = 1;
  831. break;
  832. default:
  833. desc_idx = 0;
  834. }
  835. desc = epfile->ep->descs[desc_idx];
  836. spin_unlock_irq(&epfile->ffs->eps_lock);
  837. ret = copy_to_user((void *)value, desc, sizeof(*desc));
  838. if (ret)
  839. ret = -EFAULT;
  840. return ret;
  841. }
  842. default:
  843. ret = -ENOTTY;
  844. }
  845. } else {
  846. ret = -ENODEV;
  847. }
  848. spin_unlock_irq(&epfile->ffs->eps_lock);
  849. return ret;
  850. }
  851. static const struct file_operations ffs_epfile_operations = {
  852. .llseek = no_llseek,
  853. .open = ffs_epfile_open,
  854. .write_iter = ffs_epfile_write_iter,
  855. .read_iter = ffs_epfile_read_iter,
  856. .release = ffs_epfile_release,
  857. .unlocked_ioctl = ffs_epfile_ioctl,
  858. };
  859. /* File system and super block operations ***********************************/
  860. /*
  861. * Mounting the file system creates a controller file, used first for
  862. * function configuration then later for event monitoring.
  863. */
  864. static struct inode *__must_check
  865. ffs_sb_make_inode(struct super_block *sb, void *data,
  866. const struct file_operations *fops,
  867. const struct inode_operations *iops,
  868. struct ffs_file_perms *perms)
  869. {
  870. struct inode *inode;
  871. ENTER();
  872. inode = new_inode(sb);
  873. if (likely(inode)) {
  874. struct timespec current_time = CURRENT_TIME;
  875. inode->i_ino = get_next_ino();
  876. inode->i_mode = perms->mode;
  877. inode->i_uid = perms->uid;
  878. inode->i_gid = perms->gid;
  879. inode->i_atime = current_time;
  880. inode->i_mtime = current_time;
  881. inode->i_ctime = current_time;
  882. inode->i_private = data;
  883. if (fops)
  884. inode->i_fop = fops;
  885. if (iops)
  886. inode->i_op = iops;
  887. }
  888. return inode;
  889. }
  890. /* Create "regular" file */
  891. static struct dentry *ffs_sb_create_file(struct super_block *sb,
  892. const char *name, void *data,
  893. const struct file_operations *fops)
  894. {
  895. struct ffs_data *ffs = sb->s_fs_info;
  896. struct dentry *dentry;
  897. struct inode *inode;
  898. ENTER();
  899. dentry = d_alloc_name(sb->s_root, name);
  900. if (unlikely(!dentry))
  901. return NULL;
  902. inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms);
  903. if (unlikely(!inode)) {
  904. dput(dentry);
  905. return NULL;
  906. }
  907. d_add(dentry, inode);
  908. return dentry;
  909. }
  910. /* Super block */
  911. static const struct super_operations ffs_sb_operations = {
  912. .statfs = simple_statfs,
  913. .drop_inode = generic_delete_inode,
  914. };
  915. struct ffs_sb_fill_data {
  916. struct ffs_file_perms perms;
  917. umode_t root_mode;
  918. const char *dev_name;
  919. bool no_disconnect;
  920. struct ffs_data *ffs_data;
  921. };
  922. static int ffs_sb_fill(struct super_block *sb, void *_data, int silent)
  923. {
  924. struct ffs_sb_fill_data *data = _data;
  925. struct inode *inode;
  926. struct ffs_data *ffs = data->ffs_data;
  927. ENTER();
  928. ffs->sb = sb;
  929. data->ffs_data = NULL;
  930. sb->s_fs_info = ffs;
  931. sb->s_blocksize = PAGE_CACHE_SIZE;
  932. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  933. sb->s_magic = FUNCTIONFS_MAGIC;
  934. sb->s_op = &ffs_sb_operations;
  935. sb->s_time_gran = 1;
  936. /* Root inode */
  937. data->perms.mode = data->root_mode;
  938. inode = ffs_sb_make_inode(sb, NULL,
  939. &simple_dir_operations,
  940. &simple_dir_inode_operations,
  941. &data->perms);
  942. sb->s_root = d_make_root(inode);
  943. if (unlikely(!sb->s_root))
  944. return -ENOMEM;
  945. /* EP0 file */
  946. if (unlikely(!ffs_sb_create_file(sb, "ep0", ffs,
  947. &ffs_ep0_operations)))
  948. return -ENOMEM;
  949. return 0;
  950. }
  951. static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
  952. {
  953. ENTER();
  954. if (!opts || !*opts)
  955. return 0;
  956. for (;;) {
  957. unsigned long value;
  958. char *eq, *comma;
  959. /* Option limit */
  960. comma = strchr(opts, ',');
  961. if (comma)
  962. *comma = 0;
  963. /* Value limit */
  964. eq = strchr(opts, '=');
  965. if (unlikely(!eq)) {
  966. pr_err("'=' missing in %s\n", opts);
  967. return -EINVAL;
  968. }
  969. *eq = 0;
  970. /* Parse value */
  971. if (kstrtoul(eq + 1, 0, &value)) {
  972. pr_err("%s: invalid value: %s\n", opts, eq + 1);
  973. return -EINVAL;
  974. }
  975. /* Interpret option */
  976. switch (eq - opts) {
  977. case 13:
  978. if (!memcmp(opts, "no_disconnect", 13))
  979. data->no_disconnect = !!value;
  980. else
  981. goto invalid;
  982. break;
  983. case 5:
  984. if (!memcmp(opts, "rmode", 5))
  985. data->root_mode = (value & 0555) | S_IFDIR;
  986. else if (!memcmp(opts, "fmode", 5))
  987. data->perms.mode = (value & 0666) | S_IFREG;
  988. else
  989. goto invalid;
  990. break;
  991. case 4:
  992. if (!memcmp(opts, "mode", 4)) {
  993. data->root_mode = (value & 0555) | S_IFDIR;
  994. data->perms.mode = (value & 0666) | S_IFREG;
  995. } else {
  996. goto invalid;
  997. }
  998. break;
  999. case 3:
  1000. if (!memcmp(opts, "uid", 3)) {
  1001. data->perms.uid = make_kuid(current_user_ns(), value);
  1002. if (!uid_valid(data->perms.uid)) {
  1003. pr_err("%s: unmapped value: %lu\n", opts, value);
  1004. return -EINVAL;
  1005. }
  1006. } else if (!memcmp(opts, "gid", 3)) {
  1007. data->perms.gid = make_kgid(current_user_ns(), value);
  1008. if (!gid_valid(data->perms.gid)) {
  1009. pr_err("%s: unmapped value: %lu\n", opts, value);
  1010. return -EINVAL;
  1011. }
  1012. } else {
  1013. goto invalid;
  1014. }
  1015. break;
  1016. default:
  1017. invalid:
  1018. pr_err("%s: invalid option\n", opts);
  1019. return -EINVAL;
  1020. }
  1021. /* Next iteration */
  1022. if (!comma)
  1023. break;
  1024. opts = comma + 1;
  1025. }
  1026. return 0;
  1027. }
  1028. /* "mount -t functionfs dev_name /dev/function" ends up here */
  1029. static struct dentry *
  1030. ffs_fs_mount(struct file_system_type *t, int flags,
  1031. const char *dev_name, void *opts)
  1032. {
  1033. struct ffs_sb_fill_data data = {
  1034. .perms = {
  1035. .mode = S_IFREG | 0600,
  1036. .uid = GLOBAL_ROOT_UID,
  1037. .gid = GLOBAL_ROOT_GID,
  1038. },
  1039. .root_mode = S_IFDIR | 0500,
  1040. .no_disconnect = false,
  1041. };
  1042. struct dentry *rv;
  1043. int ret;
  1044. void *ffs_dev;
  1045. struct ffs_data *ffs;
  1046. ENTER();
  1047. ret = ffs_fs_parse_opts(&data, opts);
  1048. if (unlikely(ret < 0))
  1049. return ERR_PTR(ret);
  1050. ffs = ffs_data_new();
  1051. if (unlikely(!ffs))
  1052. return ERR_PTR(-ENOMEM);
  1053. ffs->file_perms = data.perms;
  1054. ffs->no_disconnect = data.no_disconnect;
  1055. ffs->dev_name = kstrdup(dev_name, GFP_KERNEL);
  1056. if (unlikely(!ffs->dev_name)) {
  1057. ffs_data_put(ffs);
  1058. return ERR_PTR(-ENOMEM);
  1059. }
  1060. ffs_dev = ffs_acquire_dev(dev_name);
  1061. if (IS_ERR(ffs_dev)) {
  1062. ffs_data_put(ffs);
  1063. return ERR_CAST(ffs_dev);
  1064. }
  1065. ffs->private_data = ffs_dev;
  1066. data.ffs_data = ffs;
  1067. rv = mount_nodev(t, flags, &data, ffs_sb_fill);
  1068. if (IS_ERR(rv) && data.ffs_data) {
  1069. ffs_release_dev(data.ffs_data);
  1070. ffs_data_put(data.ffs_data);
  1071. }
  1072. return rv;
  1073. }
  1074. static void
  1075. ffs_fs_kill_sb(struct super_block *sb)
  1076. {
  1077. ENTER();
  1078. kill_litter_super(sb);
  1079. if (sb->s_fs_info) {
  1080. ffs_release_dev(sb->s_fs_info);
  1081. ffs_data_closed(sb->s_fs_info);
  1082. }
  1083. }
  1084. static struct file_system_type ffs_fs_type = {
  1085. .owner = THIS_MODULE,
  1086. .name = "functionfs",
  1087. .mount = ffs_fs_mount,
  1088. .kill_sb = ffs_fs_kill_sb,
  1089. };
  1090. MODULE_ALIAS_FS("functionfs");
  1091. /* Driver's main init/cleanup functions *************************************/
  1092. static int functionfs_init(void)
  1093. {
  1094. int ret;
  1095. ENTER();
  1096. ret = register_filesystem(&ffs_fs_type);
  1097. if (likely(!ret))
  1098. pr_info("file system registered\n");
  1099. else
  1100. pr_err("failed registering file system (%d)\n", ret);
  1101. return ret;
  1102. }
  1103. static void functionfs_cleanup(void)
  1104. {
  1105. ENTER();
  1106. pr_info("unloading\n");
  1107. unregister_filesystem(&ffs_fs_type);
  1108. }
  1109. /* ffs_data and ffs_function construction and destruction code **************/
  1110. static void ffs_data_clear(struct ffs_data *ffs);
  1111. static void ffs_data_reset(struct ffs_data *ffs);
  1112. static void ffs_data_get(struct ffs_data *ffs)
  1113. {
  1114. ENTER();
  1115. atomic_inc(&ffs->ref);
  1116. }
  1117. static void ffs_data_opened(struct ffs_data *ffs)
  1118. {
  1119. ENTER();
  1120. atomic_inc(&ffs->ref);
  1121. if (atomic_add_return(1, &ffs->opened) == 1 &&
  1122. ffs->state == FFS_DEACTIVATED) {
  1123. ffs->state = FFS_CLOSING;
  1124. ffs_data_reset(ffs);
  1125. }
  1126. }
  1127. static void ffs_data_put(struct ffs_data *ffs)
  1128. {
  1129. ENTER();
  1130. if (unlikely(atomic_dec_and_test(&ffs->ref))) {
  1131. pr_info("%s(): freeing\n", __func__);
  1132. ffs_data_clear(ffs);
  1133. BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
  1134. waitqueue_active(&ffs->ep0req_completion.wait));
  1135. kfree(ffs->dev_name);
  1136. kfree(ffs);
  1137. }
  1138. }
  1139. static void ffs_data_closed(struct ffs_data *ffs)
  1140. {
  1141. ENTER();
  1142. if (atomic_dec_and_test(&ffs->opened)) {
  1143. if (ffs->no_disconnect) {
  1144. ffs->state = FFS_DEACTIVATED;
  1145. if (ffs->epfiles) {
  1146. ffs_epfiles_destroy(ffs->epfiles,
  1147. ffs->eps_count);
  1148. ffs->epfiles = NULL;
  1149. }
  1150. if (ffs->setup_state == FFS_SETUP_PENDING)
  1151. __ffs_ep0_stall(ffs);
  1152. } else {
  1153. ffs->state = FFS_CLOSING;
  1154. ffs_data_reset(ffs);
  1155. }
  1156. }
  1157. if (atomic_read(&ffs->opened) < 0) {
  1158. ffs->state = FFS_CLOSING;
  1159. ffs_data_reset(ffs);
  1160. }
  1161. ffs_data_put(ffs);
  1162. }
  1163. static struct ffs_data *ffs_data_new(void)
  1164. {
  1165. struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL);
  1166. if (unlikely(!ffs))
  1167. return NULL;
  1168. ENTER();
  1169. atomic_set(&ffs->ref, 1);
  1170. atomic_set(&ffs->opened, 0);
  1171. ffs->state = FFS_READ_DESCRIPTORS;
  1172. mutex_init(&ffs->mutex);
  1173. spin_lock_init(&ffs->eps_lock);
  1174. init_waitqueue_head(&ffs->ev.waitq);
  1175. init_completion(&ffs->ep0req_completion);
  1176. /* XXX REVISIT need to update it in some places, or do we? */
  1177. ffs->ev.can_stall = 1;
  1178. return ffs;
  1179. }
  1180. static void ffs_data_clear(struct ffs_data *ffs)
  1181. {
  1182. ENTER();
  1183. ffs_closed(ffs);
  1184. BUG_ON(ffs->gadget);
  1185. if (ffs->epfiles)
  1186. ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count);
  1187. if (ffs->ffs_eventfd)
  1188. eventfd_ctx_put(ffs->ffs_eventfd);
  1189. kfree(ffs->raw_descs_data);
  1190. kfree(ffs->raw_strings);
  1191. kfree(ffs->stringtabs);
  1192. }
  1193. static void ffs_data_reset(struct ffs_data *ffs)
  1194. {
  1195. ENTER();
  1196. ffs_data_clear(ffs);
  1197. ffs->epfiles = NULL;
  1198. ffs->raw_descs_data = NULL;
  1199. ffs->raw_descs = NULL;
  1200. ffs->raw_strings = NULL;
  1201. ffs->stringtabs = NULL;
  1202. ffs->raw_descs_length = 0;
  1203. ffs->fs_descs_count = 0;
  1204. ffs->hs_descs_count = 0;
  1205. ffs->ss_descs_count = 0;
  1206. ffs->strings_count = 0;
  1207. ffs->interfaces_count = 0;
  1208. ffs->eps_count = 0;
  1209. ffs->ev.count = 0;
  1210. ffs->state = FFS_READ_DESCRIPTORS;
  1211. ffs->setup_state = FFS_NO_SETUP;
  1212. ffs->flags = 0;
  1213. }
  1214. static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
  1215. {
  1216. struct usb_gadget_strings **lang;
  1217. int first_id;
  1218. ENTER();
  1219. if (WARN_ON(ffs->state != FFS_ACTIVE
  1220. || test_and_set_bit(FFS_FL_BOUND, &ffs->flags)))
  1221. return -EBADFD;
  1222. first_id = usb_string_ids_n(cdev, ffs->strings_count);
  1223. if (unlikely(first_id < 0))
  1224. return first_id;
  1225. ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
  1226. if (unlikely(!ffs->ep0req))
  1227. return -ENOMEM;
  1228. ffs->ep0req->complete = ffs_ep0_complete;
  1229. ffs->ep0req->context = ffs;
  1230. lang = ffs->stringtabs;
  1231. if (lang) {
  1232. for (; *lang; ++lang) {
  1233. struct usb_string *str = (*lang)->strings;
  1234. int id = first_id;
  1235. for (; str->s; ++id, ++str)
  1236. str->id = id;
  1237. }
  1238. }
  1239. ffs->gadget = cdev->gadget;
  1240. ffs_data_get(ffs);
  1241. return 0;
  1242. }
  1243. static void functionfs_unbind(struct ffs_data *ffs)
  1244. {
  1245. ENTER();
  1246. if (!WARN_ON(!ffs->gadget)) {
  1247. usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req);
  1248. ffs->ep0req = NULL;
  1249. ffs->gadget = NULL;
  1250. clear_bit(FFS_FL_BOUND, &ffs->flags);
  1251. ffs_data_put(ffs);
  1252. }
  1253. }
  1254. static int ffs_epfiles_create(struct ffs_data *ffs)
  1255. {
  1256. struct ffs_epfile *epfile, *epfiles;
  1257. unsigned i, count;
  1258. ENTER();
  1259. count = ffs->eps_count;
  1260. epfiles = kcalloc(count, sizeof(*epfiles), GFP_KERNEL);
  1261. if (!epfiles)
  1262. return -ENOMEM;
  1263. epfile = epfiles;
  1264. for (i = 1; i <= count; ++i, ++epfile) {
  1265. epfile->ffs = ffs;
  1266. mutex_init(&epfile->mutex);
  1267. init_waitqueue_head(&epfile->wait);
  1268. if (ffs->user_flags & FUNCTIONFS_VIRTUAL_ADDR)
  1269. sprintf(epfile->name, "ep%02x", ffs->eps_addrmap[i]);
  1270. else
  1271. sprintf(epfile->name, "ep%u", i);
  1272. epfile->dentry = ffs_sb_create_file(ffs->sb, epfile->name,
  1273. epfile,
  1274. &ffs_epfile_operations);
  1275. if (unlikely(!epfile->dentry)) {
  1276. ffs_epfiles_destroy(epfiles, i - 1);
  1277. return -ENOMEM;
  1278. }
  1279. }
  1280. ffs->epfiles = epfiles;
  1281. return 0;
  1282. }
  1283. static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
  1284. {
  1285. struct ffs_epfile *epfile = epfiles;
  1286. ENTER();
  1287. for (; count; --count, ++epfile) {
  1288. BUG_ON(mutex_is_locked(&epfile->mutex) ||
  1289. waitqueue_active(&epfile->wait));
  1290. if (epfile->dentry) {
  1291. d_delete(epfile->dentry);
  1292. dput(epfile->dentry);
  1293. epfile->dentry = NULL;
  1294. }
  1295. }
  1296. kfree(epfiles);
  1297. }
  1298. static void ffs_func_eps_disable(struct ffs_function *func)
  1299. {
  1300. struct ffs_ep *ep = func->eps;
  1301. struct ffs_epfile *epfile = func->ffs->epfiles;
  1302. unsigned count = func->ffs->eps_count;
  1303. unsigned long flags;
  1304. spin_lock_irqsave(&func->ffs->eps_lock, flags);
  1305. do {
  1306. /* pending requests get nuked */
  1307. if (likely(ep->ep))
  1308. usb_ep_disable(ep->ep);
  1309. ++ep;
  1310. if (epfile) {
  1311. epfile->ep = NULL;
  1312. ++epfile;
  1313. }
  1314. } while (--count);
  1315. spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
  1316. }
  1317. static int ffs_func_eps_enable(struct ffs_function *func)
  1318. {
  1319. struct ffs_data *ffs = func->ffs;
  1320. struct ffs_ep *ep = func->eps;
  1321. struct ffs_epfile *epfile = ffs->epfiles;
  1322. unsigned count = ffs->eps_count;
  1323. unsigned long flags;
  1324. int ret = 0;
  1325. spin_lock_irqsave(&func->ffs->eps_lock, flags);
  1326. do {
  1327. struct usb_endpoint_descriptor *ds;
  1328. struct usb_ss_ep_comp_descriptor *comp_desc = NULL;
  1329. int needs_comp_desc = false;
  1330. int desc_idx;
  1331. if (ffs->gadget->speed == USB_SPEED_SUPER) {
  1332. desc_idx = 2;
  1333. needs_comp_desc = true;
  1334. } else if (ffs->gadget->speed == USB_SPEED_HIGH)
  1335. desc_idx = 1;
  1336. else
  1337. desc_idx = 0;
  1338. /* fall-back to lower speed if desc missing for current speed */
  1339. do {
  1340. ds = ep->descs[desc_idx];
  1341. } while (!ds && --desc_idx >= 0);
  1342. if (!ds) {
  1343. ret = -EINVAL;
  1344. break;
  1345. }
  1346. ep->ep->driver_data = ep;
  1347. ep->ep->desc = ds;
  1348. if (needs_comp_desc) {
  1349. comp_desc = (struct usb_ss_ep_comp_descriptor *)(ds +
  1350. USB_DT_ENDPOINT_SIZE);
  1351. ep->ep->maxburst = comp_desc->bMaxBurst + 1;
  1352. ep->ep->comp_desc = comp_desc;
  1353. }
  1354. ret = usb_ep_enable(ep->ep);
  1355. if (likely(!ret)) {
  1356. epfile->ep = ep;
  1357. epfile->in = usb_endpoint_dir_in(ds);
  1358. epfile->isoc = usb_endpoint_xfer_isoc(ds);
  1359. } else {
  1360. break;
  1361. }
  1362. wake_up(&epfile->wait);
  1363. ++ep;
  1364. ++epfile;
  1365. } while (--count);
  1366. spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
  1367. return ret;
  1368. }
  1369. /* Parsing and building descriptors and strings *****************************/
  1370. /*
  1371. * This validates if data pointed by data is a valid USB descriptor as
  1372. * well as record how many interfaces, endpoints and strings are
  1373. * required by given configuration. Returns address after the
  1374. * descriptor or NULL if data is invalid.
  1375. */
  1376. enum ffs_entity_type {
  1377. FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT
  1378. };
  1379. enum ffs_os_desc_type {
  1380. FFS_OS_DESC, FFS_OS_DESC_EXT_COMPAT, FFS_OS_DESC_EXT_PROP
  1381. };
  1382. typedef int (*ffs_entity_callback)(enum ffs_entity_type entity,
  1383. u8 *valuep,
  1384. struct usb_descriptor_header *desc,
  1385. void *priv);
  1386. typedef int (*ffs_os_desc_callback)(enum ffs_os_desc_type entity,
  1387. struct usb_os_desc_header *h, void *data,
  1388. unsigned len, void *priv);
  1389. static int __must_check ffs_do_single_desc(char *data, unsigned len,
  1390. ffs_entity_callback entity,
  1391. void *priv)
  1392. {
  1393. struct usb_descriptor_header *_ds = (void *)data;
  1394. u8 length;
  1395. int ret;
  1396. ENTER();
  1397. /* At least two bytes are required: length and type */
  1398. if (len < 2) {
  1399. pr_vdebug("descriptor too short\n");
  1400. return -EINVAL;
  1401. }
  1402. /* If we have at least as many bytes as the descriptor takes? */
  1403. length = _ds->bLength;
  1404. if (len < length) {
  1405. pr_vdebug("descriptor longer then available data\n");
  1406. return -EINVAL;
  1407. }
  1408. #define __entity_check_INTERFACE(val) 1
  1409. #define __entity_check_STRING(val) (val)
  1410. #define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
  1411. #define __entity(type, val) do { \
  1412. pr_vdebug("entity " #type "(%02x)\n", (val)); \
  1413. if (unlikely(!__entity_check_ ##type(val))) { \
  1414. pr_vdebug("invalid entity's value\n"); \
  1415. return -EINVAL; \
  1416. } \
  1417. ret = entity(FFS_ ##type, &val, _ds, priv); \
  1418. if (unlikely(ret < 0)) { \
  1419. pr_debug("entity " #type "(%02x); ret = %d\n", \
  1420. (val), ret); \
  1421. return ret; \
  1422. } \
  1423. } while (0)
  1424. /* Parse descriptor depending on type. */
  1425. switch (_ds->bDescriptorType) {
  1426. case USB_DT_DEVICE:
  1427. case USB_DT_CONFIG:
  1428. case USB_DT_STRING:
  1429. case USB_DT_DEVICE_QUALIFIER:
  1430. /* function can't have any of those */
  1431. pr_vdebug("descriptor reserved for gadget: %d\n",
  1432. _ds->bDescriptorType);
  1433. return -EINVAL;
  1434. case USB_DT_INTERFACE: {
  1435. struct usb_interface_descriptor *ds = (void *)_ds;
  1436. pr_vdebug("interface descriptor\n");
  1437. if (length != sizeof *ds)
  1438. goto inv_length;
  1439. __entity(INTERFACE, ds->bInterfaceNumber);
  1440. if (ds->iInterface)
  1441. __entity(STRING, ds->iInterface);
  1442. }
  1443. break;
  1444. case USB_DT_ENDPOINT: {
  1445. struct usb_endpoint_descriptor *ds = (void *)_ds;
  1446. pr_vdebug("endpoint descriptor\n");
  1447. if (length != USB_DT_ENDPOINT_SIZE &&
  1448. length != USB_DT_ENDPOINT_AUDIO_SIZE)
  1449. goto inv_length;
  1450. __entity(ENDPOINT, ds->bEndpointAddress);
  1451. }
  1452. break;
  1453. case HID_DT_HID:
  1454. pr_vdebug("hid descriptor\n");
  1455. if (length != sizeof(struct hid_descriptor))
  1456. goto inv_length;
  1457. break;
  1458. case USB_DT_OTG:
  1459. if (length != sizeof(struct usb_otg_descriptor))
  1460. goto inv_length;
  1461. break;
  1462. case USB_DT_INTERFACE_ASSOCIATION: {
  1463. struct usb_interface_assoc_descriptor *ds = (void *)_ds;
  1464. pr_vdebug("interface association descriptor\n");
  1465. if (length != sizeof *ds)
  1466. goto inv_length;
  1467. if (ds->iFunction)
  1468. __entity(STRING, ds->iFunction);
  1469. }
  1470. break;
  1471. case USB_DT_SS_ENDPOINT_COMP:
  1472. pr_vdebug("EP SS companion descriptor\n");
  1473. if (length != sizeof(struct usb_ss_ep_comp_descriptor))
  1474. goto inv_length;
  1475. break;
  1476. case USB_DT_OTHER_SPEED_CONFIG:
  1477. case USB_DT_INTERFACE_POWER:
  1478. case USB_DT_DEBUG:
  1479. case USB_DT_SECURITY:
  1480. case USB_DT_CS_RADIO_CONTROL:
  1481. /* TODO */
  1482. pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType);
  1483. return -EINVAL;
  1484. default:
  1485. /* We should never be here */
  1486. pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType);
  1487. return -EINVAL;
  1488. inv_length:
  1489. pr_vdebug("invalid length: %d (descriptor %d)\n",
  1490. _ds->bLength, _ds->bDescriptorType);
  1491. return -EINVAL;
  1492. }
  1493. #undef __entity
  1494. #undef __entity_check_DESCRIPTOR
  1495. #undef __entity_check_INTERFACE
  1496. #undef __entity_check_STRING
  1497. #undef __entity_check_ENDPOINT
  1498. return length;
  1499. }
  1500. static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
  1501. ffs_entity_callback entity, void *priv)
  1502. {
  1503. const unsigned _len = len;
  1504. unsigned long num = 0;
  1505. ENTER();
  1506. for (;;) {
  1507. int ret;
  1508. if (num == count)
  1509. data = NULL;
  1510. /* Record "descriptor" entity */
  1511. ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv);
  1512. if (unlikely(ret < 0)) {
  1513. pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n",
  1514. num, ret);
  1515. return ret;
  1516. }
  1517. if (!data)
  1518. return _len - len;
  1519. ret = ffs_do_single_desc(data, len, entity, priv);
  1520. if (unlikely(ret < 0)) {
  1521. pr_debug("%s returns %d\n", __func__, ret);
  1522. return ret;
  1523. }
  1524. len -= ret;
  1525. data += ret;
  1526. ++num;
  1527. }
  1528. }
  1529. static int __ffs_data_do_entity(enum ffs_entity_type type,
  1530. u8 *valuep, struct usb_descriptor_header *desc,
  1531. void *priv)
  1532. {
  1533. struct ffs_desc_helper *helper = priv;
  1534. struct usb_endpoint_descriptor *d;
  1535. ENTER();
  1536. switch (type) {
  1537. case FFS_DESCRIPTOR:
  1538. break;
  1539. case FFS_INTERFACE:
  1540. /*
  1541. * Interfaces are indexed from zero so if we
  1542. * encountered interface "n" then there are at least
  1543. * "n+1" interfaces.
  1544. */
  1545. if (*valuep >= helper->interfaces_count)
  1546. helper->interfaces_count = *valuep + 1;
  1547. break;
  1548. case FFS_STRING:
  1549. /*
  1550. * Strings are indexed from 1 (0 is magic ;) reserved
  1551. * for languages list or some such)
  1552. */
  1553. if (*valuep > helper->ffs->strings_count)
  1554. helper->ffs->strings_count = *valuep;
  1555. break;
  1556. case FFS_ENDPOINT:
  1557. d = (void *)desc;
  1558. helper->eps_count++;
  1559. if (helper->eps_count >= 15)
  1560. return -EINVAL;
  1561. /* Check if descriptors for any speed were already parsed */
  1562. if (!helper->ffs->eps_count && !helper->ffs->interfaces_count)
  1563. helper->ffs->eps_addrmap[helper->eps_count] =
  1564. d->bEndpointAddress;
  1565. else if (helper->ffs->eps_addrmap[helper->eps_count] !=
  1566. d->bEndpointAddress)
  1567. return -EINVAL;
  1568. break;
  1569. }
  1570. return 0;
  1571. }
  1572. static int __ffs_do_os_desc_header(enum ffs_os_desc_type *next_type,
  1573. struct usb_os_desc_header *desc)
  1574. {
  1575. u16 bcd_version = le16_to_cpu(desc->bcdVersion);
  1576. u16 w_index = le16_to_cpu(desc->wIndex);
  1577. if (bcd_version != 1) {
  1578. pr_vdebug("unsupported os descriptors version: %d",
  1579. bcd_version);
  1580. return -EINVAL;
  1581. }
  1582. switch (w_index) {
  1583. case 0x4:
  1584. *next_type = FFS_OS_DESC_EXT_COMPAT;
  1585. break;
  1586. case 0x5:
  1587. *next_type = FFS_OS_DESC_EXT_PROP;
  1588. break;
  1589. default:
  1590. pr_vdebug("unsupported os descriptor type: %d", w_index);
  1591. return -EINVAL;
  1592. }
  1593. return sizeof(*desc);
  1594. }
  1595. /*
  1596. * Process all extended compatibility/extended property descriptors
  1597. * of a feature descriptor
  1598. */
  1599. static int __must_check ffs_do_single_os_desc(char *data, unsigned len,
  1600. enum ffs_os_desc_type type,
  1601. u16 feature_count,
  1602. ffs_os_desc_callback entity,
  1603. void *priv,
  1604. struct usb_os_desc_header *h)
  1605. {
  1606. int ret;
  1607. const unsigned _len = len;
  1608. ENTER();
  1609. /* loop over all ext compat/ext prop descriptors */
  1610. while (feature_count--) {
  1611. ret = entity(type, h, data, len, priv);
  1612. if (unlikely(ret < 0)) {
  1613. pr_debug("bad OS descriptor, type: %d\n", type);
  1614. return ret;
  1615. }
  1616. data += ret;
  1617. len -= ret;
  1618. }
  1619. return _len - len;
  1620. }
  1621. /* Process a number of complete Feature Descriptors (Ext Compat or Ext Prop) */
  1622. static int __must_check ffs_do_os_descs(unsigned count,
  1623. char *data, unsigned len,
  1624. ffs_os_desc_callback entity, void *priv)
  1625. {
  1626. const unsigned _len = len;
  1627. unsigned long num = 0;
  1628. ENTER();
  1629. for (num = 0; num < count; ++num) {
  1630. int ret;
  1631. enum ffs_os_desc_type type;
  1632. u16 feature_count;
  1633. struct usb_os_desc_header *desc = (void *)data;
  1634. if (len < sizeof(*desc))
  1635. return -EINVAL;
  1636. /*
  1637. * Record "descriptor" entity.
  1638. * Process dwLength, bcdVersion, wIndex, get b/wCount.
  1639. * Move the data pointer to the beginning of extended
  1640. * compatibilities proper or extended properties proper
  1641. * portions of the data
  1642. */
  1643. if (le32_to_cpu(desc->dwLength) > len)
  1644. return -EINVAL;
  1645. ret = __ffs_do_os_desc_header(&type, desc);
  1646. if (unlikely(ret < 0)) {
  1647. pr_debug("entity OS_DESCRIPTOR(%02lx); ret = %d\n",
  1648. num, ret);
  1649. return ret;
  1650. }
  1651. /*
  1652. * 16-bit hex "?? 00" Little Endian looks like 8-bit hex "??"
  1653. */
  1654. feature_count = le16_to_cpu(desc->wCount);
  1655. if (type == FFS_OS_DESC_EXT_COMPAT &&
  1656. (feature_count > 255 || desc->Reserved))
  1657. return -EINVAL;
  1658. len -= ret;
  1659. data += ret;
  1660. /*
  1661. * Process all function/property descriptors
  1662. * of this Feature Descriptor
  1663. */
  1664. ret = ffs_do_single_os_desc(data, len, type,
  1665. feature_count, entity, priv, desc);
  1666. if (unlikely(ret < 0)) {
  1667. pr_debug("%s returns %d\n", __func__, ret);
  1668. return ret;
  1669. }
  1670. len -= ret;
  1671. data += ret;
  1672. }
  1673. return _len - len;
  1674. }
  1675. /**
  1676. * Validate contents of the buffer from userspace related to OS descriptors.
  1677. */
  1678. static int __ffs_data_do_os_desc(enum ffs_os_desc_type type,
  1679. struct usb_os_desc_header *h, void *data,
  1680. unsigned len, void *priv)
  1681. {
  1682. struct ffs_data *ffs = priv;
  1683. u8 length;
  1684. ENTER();
  1685. switch (type) {
  1686. case FFS_OS_DESC_EXT_COMPAT: {
  1687. struct usb_ext_compat_desc *d = data;
  1688. int i;
  1689. if (len < sizeof(*d) ||
  1690. d->bFirstInterfaceNumber >= ffs->interfaces_count ||
  1691. d->Reserved1)
  1692. return -EINVAL;
  1693. for (i = 0; i < ARRAY_SIZE(d->Reserved2); ++i)
  1694. if (d->Reserved2[i])
  1695. return -EINVAL;
  1696. length = sizeof(struct usb_ext_compat_desc);
  1697. }
  1698. break;
  1699. case FFS_OS_DESC_EXT_PROP: {
  1700. struct usb_ext_prop_desc *d = data;
  1701. u32 type, pdl;
  1702. u16 pnl;
  1703. if (len < sizeof(*d) || h->interface >= ffs->interfaces_count)
  1704. return -EINVAL;
  1705. length = le32_to_cpu(d->dwSize);
  1706. if (len < length)
  1707. return -EINVAL;
  1708. type = le32_to_cpu(d->dwPropertyDataType);
  1709. if (type < USB_EXT_PROP_UNICODE ||
  1710. type > USB_EXT_PROP_UNICODE_MULTI) {
  1711. pr_vdebug("unsupported os descriptor property type: %d",
  1712. type);
  1713. return -EINVAL;
  1714. }
  1715. pnl = le16_to_cpu(d->wPropertyNameLength);
  1716. if (length < 14 + pnl) {
  1717. pr_vdebug("invalid os descriptor length: %d pnl:%d (descriptor %d)\n",
  1718. length, pnl, type);
  1719. return -EINVAL;
  1720. }
  1721. pdl = le32_to_cpu(*(u32 *)((u8 *)data + 10 + pnl));
  1722. if (length != 14 + pnl + pdl) {
  1723. pr_vdebug("invalid os descriptor length: %d pnl:%d pdl:%d (descriptor %d)\n",
  1724. length, pnl, pdl, type);
  1725. return -EINVAL;
  1726. }
  1727. ++ffs->ms_os_descs_ext_prop_count;
  1728. /* property name reported to the host as "WCHAR"s */
  1729. ffs->ms_os_descs_ext_prop_name_len += pnl * 2;
  1730. ffs->ms_os_descs_ext_prop_data_len += pdl;
  1731. }
  1732. break;
  1733. default:
  1734. pr_vdebug("unknown descriptor: %d\n", type);
  1735. return -EINVAL;
  1736. }
  1737. return length;
  1738. }
  1739. static int __ffs_data_got_descs(struct ffs_data *ffs,
  1740. char *const _data, size_t len)
  1741. {
  1742. char *data = _data, *raw_descs;
  1743. unsigned os_descs_count = 0, counts[3], flags;
  1744. int ret = -EINVAL, i;
  1745. struct ffs_desc_helper helper;
  1746. ENTER();
  1747. if (get_unaligned_le32(data + 4) != len)
  1748. goto error;
  1749. switch (get_unaligned_le32(data)) {
  1750. case FUNCTIONFS_DESCRIPTORS_MAGIC:
  1751. flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC;
  1752. data += 8;
  1753. len -= 8;
  1754. break;
  1755. case FUNCTIONFS_DESCRIPTORS_MAGIC_V2:
  1756. flags = get_unaligned_le32(data + 8);
  1757. ffs->user_flags = flags;
  1758. if (flags & ~(FUNCTIONFS_HAS_FS_DESC |
  1759. FUNCTIONFS_HAS_HS_DESC |
  1760. FUNCTIONFS_HAS_SS_DESC |
  1761. FUNCTIONFS_HAS_MS_OS_DESC |
  1762. FUNCTIONFS_VIRTUAL_ADDR |
  1763. FUNCTIONFS_EVENTFD)) {
  1764. ret = -ENOSYS;
  1765. goto error;
  1766. }
  1767. data += 12;
  1768. len -= 12;
  1769. break;
  1770. default:
  1771. goto error;
  1772. }
  1773. if (flags & FUNCTIONFS_EVENTFD) {
  1774. if (len < 4)
  1775. goto error;
  1776. ffs->ffs_eventfd =
  1777. eventfd_ctx_fdget((int)get_unaligned_le32(data));
  1778. if (IS_ERR(ffs->ffs_eventfd)) {
  1779. ret = PTR_ERR(ffs->ffs_eventfd);
  1780. ffs->ffs_eventfd = NULL;
  1781. goto error;
  1782. }
  1783. data += 4;
  1784. len -= 4;
  1785. }
  1786. /* Read fs_count, hs_count and ss_count (if present) */
  1787. for (i = 0; i < 3; ++i) {
  1788. if (!(flags & (1 << i))) {
  1789. counts[i] = 0;
  1790. } else if (len < 4) {
  1791. goto error;
  1792. } else {
  1793. counts[i] = get_unaligned_le32(data);
  1794. data += 4;
  1795. len -= 4;
  1796. }
  1797. }
  1798. if (flags & (1 << i)) {
  1799. if (len < 4) {
  1800. goto error;
  1801. }
  1802. os_descs_count = get_unaligned_le32(data);
  1803. data += 4;
  1804. len -= 4;
  1805. };
  1806. /* Read descriptors */
  1807. raw_descs = data;
  1808. helper.ffs = ffs;
  1809. for (i = 0; i < 3; ++i) {
  1810. if (!counts[i])
  1811. continue;
  1812. helper.interfaces_count = 0;
  1813. helper.eps_count = 0;
  1814. ret = ffs_do_descs(counts[i], data, len,
  1815. __ffs_data_do_entity, &helper);
  1816. if (ret < 0)
  1817. goto error;
  1818. if (!ffs->eps_count && !ffs->interfaces_count) {
  1819. ffs->eps_count = helper.eps_count;
  1820. ffs->interfaces_count = helper.interfaces_count;
  1821. } else {
  1822. if (ffs->eps_count != helper.eps_count) {
  1823. ret = -EINVAL;
  1824. goto error;
  1825. }
  1826. if (ffs->interfaces_count != helper.interfaces_count) {
  1827. ret = -EINVAL;
  1828. goto error;
  1829. }
  1830. }
  1831. data += ret;
  1832. len -= ret;
  1833. }
  1834. if (os_descs_count) {
  1835. ret = ffs_do_os_descs(os_descs_count, data, len,
  1836. __ffs_data_do_os_desc, ffs);
  1837. if (ret < 0)
  1838. goto error;
  1839. data += ret;
  1840. len -= ret;
  1841. }
  1842. if (raw_descs == data || len) {
  1843. ret = -EINVAL;
  1844. goto error;
  1845. }
  1846. ffs->raw_descs_data = _data;
  1847. ffs->raw_descs = raw_descs;
  1848. ffs->raw_descs_length = data - raw_descs;
  1849. ffs->fs_descs_count = counts[0];
  1850. ffs->hs_descs_count = counts[1];
  1851. ffs->ss_descs_count = counts[2];
  1852. ffs->ms_os_descs_count = os_descs_count;
  1853. return 0;
  1854. error:
  1855. kfree(_data);
  1856. return ret;
  1857. }
  1858. static int __ffs_data_got_strings(struct ffs_data *ffs,
  1859. char *const _data, size_t len)
  1860. {
  1861. u32 str_count, needed_count, lang_count;
  1862. struct usb_gadget_strings **stringtabs, *t;
  1863. struct usb_string *strings, *s;
  1864. const char *data = _data;
  1865. ENTER();
  1866. if (unlikely(len < 16 ||
  1867. get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC ||
  1868. get_unaligned_le32(data + 4) != len))
  1869. goto error;
  1870. str_count = get_unaligned_le32(data + 8);
  1871. lang_count = get_unaligned_le32(data + 12);
  1872. /* if one is zero the other must be zero */
  1873. if (unlikely(!str_count != !lang_count))
  1874. goto error;
  1875. /* Do we have at least as many strings as descriptors need? */
  1876. needed_count = ffs->strings_count;
  1877. if (unlikely(str_count < needed_count))
  1878. goto error;
  1879. /*
  1880. * If we don't need any strings just return and free all
  1881. * memory.
  1882. */
  1883. if (!needed_count) {
  1884. kfree(_data);
  1885. return 0;
  1886. }
  1887. /* Allocate everything in one chunk so there's less maintenance. */
  1888. {
  1889. unsigned i = 0;
  1890. vla_group(d);
  1891. vla_item(d, struct usb_gadget_strings *, stringtabs,
  1892. lang_count + 1);
  1893. vla_item(d, struct usb_gadget_strings, stringtab, lang_count);
  1894. vla_item(d, struct usb_string, strings,
  1895. lang_count*(needed_count+1));
  1896. char *vlabuf = kmalloc(vla_group_size(d), GFP_KERNEL);
  1897. if (unlikely(!vlabuf)) {
  1898. kfree(_data);
  1899. return -ENOMEM;
  1900. }
  1901. /* Initialize the VLA pointers */
  1902. stringtabs = vla_ptr(vlabuf, d, stringtabs);
  1903. t = vla_ptr(vlabuf, d, stringtab);
  1904. i = lang_count;
  1905. do {
  1906. *stringtabs++ = t++;
  1907. } while (--i);
  1908. *stringtabs = NULL;
  1909. /* stringtabs = vlabuf = d_stringtabs for later kfree */
  1910. stringtabs = vla_ptr(vlabuf, d, stringtabs);
  1911. t = vla_ptr(vlabuf, d, stringtab);
  1912. s = vla_ptr(vlabuf, d, strings);
  1913. strings = s;
  1914. }
  1915. /* For each language */
  1916. data += 16;
  1917. len -= 16;
  1918. do { /* lang_count > 0 so we can use do-while */
  1919. unsigned needed = needed_count;
  1920. if (unlikely(len < 3))
  1921. goto error_free;
  1922. t->language = get_unaligned_le16(data);
  1923. t->strings = s;
  1924. ++t;
  1925. data += 2;
  1926. len -= 2;
  1927. /* For each string */
  1928. do { /* str_count > 0 so we can use do-while */
  1929. size_t length = strnlen(data, len);
  1930. if (unlikely(length == len))
  1931. goto error_free;
  1932. /*
  1933. * User may provide more strings then we need,
  1934. * if that's the case we simply ignore the
  1935. * rest
  1936. */
  1937. if (likely(needed)) {
  1938. /*
  1939. * s->id will be set while adding
  1940. * function to configuration so for
  1941. * now just leave garbage here.
  1942. */
  1943. s->s = data;
  1944. --needed;
  1945. ++s;
  1946. }
  1947. data += length + 1;
  1948. len -= length + 1;
  1949. } while (--str_count);
  1950. s->id = 0; /* terminator */
  1951. s->s = NULL;
  1952. ++s;
  1953. } while (--lang_count);
  1954. /* Some garbage left? */
  1955. if (unlikely(len))
  1956. goto error_free;
  1957. /* Done! */
  1958. ffs->stringtabs = stringtabs;
  1959. ffs->raw_strings = _data;
  1960. return 0;
  1961. error_free:
  1962. kfree(stringtabs);
  1963. error:
  1964. kfree(_data);
  1965. return -EINVAL;
  1966. }
  1967. /* Events handling and management *******************************************/
  1968. static void __ffs_event_add(struct ffs_data *ffs,
  1969. enum usb_functionfs_event_type type)
  1970. {
  1971. enum usb_functionfs_event_type rem_type1, rem_type2 = type;
  1972. int neg = 0;
  1973. /*
  1974. * Abort any unhandled setup
  1975. *
  1976. * We do not need to worry about some cmpxchg() changing value
  1977. * of ffs->setup_state without holding the lock because when
  1978. * state is FFS_SETUP_PENDING cmpxchg() in several places in
  1979. * the source does nothing.
  1980. */
  1981. if (ffs->setup_state == FFS_SETUP_PENDING)
  1982. ffs->setup_state = FFS_SETUP_CANCELLED;
  1983. /*
  1984. * Logic of this function guarantees that there are at most four pending
  1985. * evens on ffs->ev.types queue. This is important because the queue
  1986. * has space for four elements only and __ffs_ep0_read_events function
  1987. * depends on that limit as well. If more event types are added, those
  1988. * limits have to be revisited or guaranteed to still hold.
  1989. */
  1990. switch (type) {
  1991. case FUNCTIONFS_RESUME:
  1992. rem_type2 = FUNCTIONFS_SUSPEND;
  1993. /* FALL THROUGH */
  1994. case FUNCTIONFS_SUSPEND:
  1995. case FUNCTIONFS_SETUP:
  1996. rem_type1 = type;
  1997. /* Discard all similar events */
  1998. break;
  1999. case FUNCTIONFS_BIND:
  2000. case FUNCTIONFS_UNBIND:
  2001. case FUNCTIONFS_DISABLE:
  2002. case FUNCTIONFS_ENABLE:
  2003. /* Discard everything other then power management. */
  2004. rem_type1 = FUNCTIONFS_SUSPEND;
  2005. rem_type2 = FUNCTIONFS_RESUME;
  2006. neg = 1;
  2007. break;
  2008. default:
  2009. WARN(1, "%d: unknown event, this should not happen\n", type);
  2010. return;
  2011. }
  2012. {
  2013. u8 *ev = ffs->ev.types, *out = ev;
  2014. unsigned n = ffs->ev.count;
  2015. for (; n; --n, ++ev)
  2016. if ((*ev == rem_type1 || *ev == rem_type2) == neg)
  2017. *out++ = *ev;
  2018. else
  2019. pr_vdebug("purging event %d\n", *ev);
  2020. ffs->ev.count = out - ffs->ev.types;
  2021. }
  2022. pr_vdebug("adding event %d\n", type);
  2023. ffs->ev.types[ffs->ev.count++] = type;
  2024. wake_up_locked(&ffs->ev.waitq);
  2025. if (ffs->ffs_eventfd)
  2026. eventfd_signal(ffs->ffs_eventfd, 1);
  2027. }
  2028. static void ffs_event_add(struct ffs_data *ffs,
  2029. enum usb_functionfs_event_type type)
  2030. {
  2031. unsigned long flags;
  2032. spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
  2033. __ffs_event_add(ffs, type);
  2034. spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
  2035. }
  2036. /* Bind/unbind USB function hooks *******************************************/
  2037. static int ffs_ep_addr2idx(struct ffs_data *ffs, u8 endpoint_address)
  2038. {
  2039. int i;
  2040. for (i = 1; i < ARRAY_SIZE(ffs->eps_addrmap); ++i)
  2041. if (ffs->eps_addrmap[i] == endpoint_address)
  2042. return i;
  2043. return -ENOENT;
  2044. }
  2045. static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
  2046. struct usb_descriptor_header *desc,
  2047. void *priv)
  2048. {
  2049. struct usb_endpoint_descriptor *ds = (void *)desc;
  2050. struct ffs_function *func = priv;
  2051. struct ffs_ep *ffs_ep;
  2052. unsigned ep_desc_id;
  2053. int idx;
  2054. static const char *speed_names[] = { "full", "high", "super" };
  2055. if (type != FFS_DESCRIPTOR)
  2056. return 0;
  2057. /*
  2058. * If ss_descriptors is not NULL, we are reading super speed
  2059. * descriptors; if hs_descriptors is not NULL, we are reading high
  2060. * speed descriptors; otherwise, we are reading full speed
  2061. * descriptors.
  2062. */
  2063. if (func->function.ss_descriptors) {
  2064. ep_desc_id = 2;
  2065. func->function.ss_descriptors[(long)valuep] = desc;
  2066. } else if (func->function.hs_descriptors) {
  2067. ep_desc_id = 1;
  2068. func->function.hs_descriptors[(long)valuep] = desc;
  2069. } else {
  2070. ep_desc_id = 0;
  2071. func->function.fs_descriptors[(long)valuep] = desc;
  2072. }
  2073. if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
  2074. return 0;
  2075. idx = ffs_ep_addr2idx(func->ffs, ds->bEndpointAddress) - 1;
  2076. if (idx < 0)
  2077. return idx;
  2078. ffs_ep = func->eps + idx;
  2079. if (unlikely(ffs_ep->descs[ep_desc_id])) {
  2080. pr_err("two %sspeed descriptors for EP %d\n",
  2081. speed_names[ep_desc_id],
  2082. ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
  2083. return -EINVAL;
  2084. }
  2085. ffs_ep->descs[ep_desc_id] = ds;
  2086. ffs_dump_mem(": Original ep desc", ds, ds->bLength);
  2087. if (ffs_ep->ep) {
  2088. ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress;
  2089. if (!ds->wMaxPacketSize)
  2090. ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize;
  2091. } else {
  2092. struct usb_request *req;
  2093. struct usb_ep *ep;
  2094. u8 bEndpointAddress;
  2095. /*
  2096. * We back up bEndpointAddress because autoconfig overwrites
  2097. * it with physical endpoint address.
  2098. */
  2099. bEndpointAddress = ds->bEndpointAddress;
  2100. pr_vdebug("autoconfig\n");
  2101. ep = usb_ep_autoconfig(func->gadget, ds);
  2102. if (unlikely(!ep))
  2103. return -ENOTSUPP;
  2104. ep->driver_data = func->eps + idx;
  2105. req = usb_ep_alloc_request(ep, GFP_KERNEL);
  2106. if (unlikely(!req))
  2107. return -ENOMEM;
  2108. ffs_ep->ep = ep;
  2109. ffs_ep->req = req;
  2110. func->eps_revmap[ds->bEndpointAddress &
  2111. USB_ENDPOINT_NUMBER_MASK] = idx + 1;
  2112. /*
  2113. * If we use virtual address mapping, we restore
  2114. * original bEndpointAddress value.
  2115. */
  2116. if (func->ffs->user_flags & FUNCTIONFS_VIRTUAL_ADDR)
  2117. ds->bEndpointAddress = bEndpointAddress;
  2118. }
  2119. ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength);
  2120. return 0;
  2121. }
  2122. static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
  2123. struct usb_descriptor_header *desc,
  2124. void *priv)
  2125. {
  2126. struct ffs_function *func = priv;
  2127. unsigned idx;
  2128. u8 newValue;
  2129. switch (type) {
  2130. default:
  2131. case FFS_DESCRIPTOR:
  2132. /* Handled in previous pass by __ffs_func_bind_do_descs() */
  2133. return 0;
  2134. case FFS_INTERFACE:
  2135. idx = *valuep;
  2136. if (func->interfaces_nums[idx] < 0) {
  2137. int id = usb_interface_id(func->conf, &func->function);
  2138. if (unlikely(id < 0))
  2139. return id;
  2140. func->interfaces_nums[idx] = id;
  2141. }
  2142. newValue = func->interfaces_nums[idx];
  2143. break;
  2144. case FFS_STRING:
  2145. /* String' IDs are allocated when fsf_data is bound to cdev */
  2146. newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id;
  2147. break;
  2148. case FFS_ENDPOINT:
  2149. /*
  2150. * USB_DT_ENDPOINT are handled in
  2151. * __ffs_func_bind_do_descs().
  2152. */
  2153. if (desc->bDescriptorType == USB_DT_ENDPOINT)
  2154. return 0;
  2155. idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1;
  2156. if (unlikely(!func->eps[idx].ep))
  2157. return -EINVAL;
  2158. {
  2159. struct usb_endpoint_descriptor **descs;
  2160. descs = func->eps[idx].descs;
  2161. newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress;
  2162. }
  2163. break;
  2164. }
  2165. pr_vdebug("%02x -> %02x\n", *valuep, newValue);
  2166. *valuep = newValue;
  2167. return 0;
  2168. }
  2169. static int __ffs_func_bind_do_os_desc(enum ffs_os_desc_type type,
  2170. struct usb_os_desc_header *h, void *data,
  2171. unsigned len, void *priv)
  2172. {
  2173. struct ffs_function *func = priv;
  2174. u8 length = 0;
  2175. switch (type) {
  2176. case FFS_OS_DESC_EXT_COMPAT: {
  2177. struct usb_ext_compat_desc *desc = data;
  2178. struct usb_os_desc_table *t;
  2179. t = &func->function.os_desc_table[desc->bFirstInterfaceNumber];
  2180. t->if_id = func->interfaces_nums[desc->bFirstInterfaceNumber];
  2181. memcpy(t->os_desc->ext_compat_id, &desc->CompatibleID,
  2182. ARRAY_SIZE(desc->CompatibleID) +
  2183. ARRAY_SIZE(desc->SubCompatibleID));
  2184. length = sizeof(*desc);
  2185. }
  2186. break;
  2187. case FFS_OS_DESC_EXT_PROP: {
  2188. struct usb_ext_prop_desc *desc = data;
  2189. struct usb_os_desc_table *t;
  2190. struct usb_os_desc_ext_prop *ext_prop;
  2191. char *ext_prop_name;
  2192. char *ext_prop_data;
  2193. t = &func->function.os_desc_table[h->interface];
  2194. t->if_id = func->interfaces_nums[h->interface];
  2195. ext_prop = func->ffs->ms_os_descs_ext_prop_avail;
  2196. func->ffs->ms_os_descs_ext_prop_avail += sizeof(*ext_prop);
  2197. ext_prop->type = le32_to_cpu(desc->dwPropertyDataType);
  2198. ext_prop->name_len = le16_to_cpu(desc->wPropertyNameLength);
  2199. ext_prop->data_len = le32_to_cpu(*(u32 *)
  2200. usb_ext_prop_data_len_ptr(data, ext_prop->name_len));
  2201. length = ext_prop->name_len + ext_prop->data_len + 14;
  2202. ext_prop_name = func->ffs->ms_os_descs_ext_prop_name_avail;
  2203. func->ffs->ms_os_descs_ext_prop_name_avail +=
  2204. ext_prop->name_len;
  2205. ext_prop_data = func->ffs->ms_os_descs_ext_prop_data_avail;
  2206. func->ffs->ms_os_descs_ext_prop_data_avail +=
  2207. ext_prop->data_len;
  2208. memcpy(ext_prop_data,
  2209. usb_ext_prop_data_ptr(data, ext_prop->name_len),
  2210. ext_prop->data_len);
  2211. /* unicode data reported to the host as "WCHAR"s */
  2212. switch (ext_prop->type) {
  2213. case USB_EXT_PROP_UNICODE:
  2214. case USB_EXT_PROP_UNICODE_ENV:
  2215. case USB_EXT_PROP_UNICODE_LINK:
  2216. case USB_EXT_PROP_UNICODE_MULTI:
  2217. ext_prop->data_len *= 2;
  2218. break;
  2219. }
  2220. ext_prop->data = ext_prop_data;
  2221. memcpy(ext_prop_name, usb_ext_prop_name_ptr(data),
  2222. ext_prop->name_len);
  2223. /* property name reported to the host as "WCHAR"s */
  2224. ext_prop->name_len *= 2;
  2225. ext_prop->name = ext_prop_name;
  2226. t->os_desc->ext_prop_len +=
  2227. ext_prop->name_len + ext_prop->data_len + 14;
  2228. ++t->os_desc->ext_prop_count;
  2229. list_add_tail(&ext_prop->entry, &t->os_desc->ext_prop);
  2230. }
  2231. break;
  2232. default:
  2233. pr_vdebug("unknown descriptor: %d\n", type);
  2234. }
  2235. return length;
  2236. }
  2237. static inline struct f_fs_opts *ffs_do_functionfs_bind(struct usb_function *f,
  2238. struct usb_configuration *c)
  2239. {
  2240. struct ffs_function *func = ffs_func_from_usb(f);
  2241. struct f_fs_opts *ffs_opts =
  2242. container_of(f->fi, struct f_fs_opts, func_inst);
  2243. int ret;
  2244. ENTER();
  2245. /*
  2246. * Legacy gadget triggers binding in functionfs_ready_callback,
  2247. * which already uses locking; taking the same lock here would
  2248. * cause a deadlock.
  2249. *
  2250. * Configfs-enabled gadgets however do need ffs_dev_lock.
  2251. */
  2252. if (!ffs_opts->no_configfs)
  2253. ffs_dev_lock();
  2254. ret = ffs_opts->dev->desc_ready ? 0 : -ENODEV;
  2255. func->ffs = ffs_opts->dev->ffs_data;
  2256. if (!ffs_opts->no_configfs)
  2257. ffs_dev_unlock();
  2258. if (ret)
  2259. return ERR_PTR(ret);
  2260. func->conf = c;
  2261. func->gadget = c->cdev->gadget;
  2262. /*
  2263. * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
  2264. * configurations are bound in sequence with list_for_each_entry,
  2265. * in each configuration its functions are bound in sequence
  2266. * with list_for_each_entry, so we assume no race condition
  2267. * with regard to ffs_opts->bound access
  2268. */
  2269. if (!ffs_opts->refcnt) {
  2270. ret = functionfs_bind(func->ffs, c->cdev);
  2271. if (ret)
  2272. return ERR_PTR(ret);
  2273. }
  2274. ffs_opts->refcnt++;
  2275. func->function.strings = func->ffs->stringtabs;
  2276. return ffs_opts;
  2277. }
  2278. static int _ffs_func_bind(struct usb_configuration *c,
  2279. struct usb_function *f)
  2280. {
  2281. struct ffs_function *func = ffs_func_from_usb(f);
  2282. struct ffs_data *ffs = func->ffs;
  2283. const int full = !!func->ffs->fs_descs_count;
  2284. const int high = !!func->ffs->hs_descs_count;
  2285. const int super = !!func->ffs->ss_descs_count;
  2286. int fs_len, hs_len, ss_len, ret, i;
  2287. struct ffs_ep *eps_ptr;
  2288. /* Make it a single chunk, less management later on */
  2289. vla_group(d);
  2290. vla_item_with_sz(d, struct ffs_ep, eps, ffs->eps_count);
  2291. vla_item_with_sz(d, struct usb_descriptor_header *, fs_descs,
  2292. full ? ffs->fs_descs_count + 1 : 0);
  2293. vla_item_with_sz(d, struct usb_descriptor_header *, hs_descs,
  2294. high ? ffs->hs_descs_count + 1 : 0);
  2295. vla_item_with_sz(d, struct usb_descriptor_header *, ss_descs,
  2296. super ? ffs->ss_descs_count + 1 : 0);
  2297. vla_item_with_sz(d, short, inums, ffs->interfaces_count);
  2298. vla_item_with_sz(d, struct usb_os_desc_table, os_desc_table,
  2299. c->cdev->use_os_string ? ffs->interfaces_count : 0);
  2300. vla_item_with_sz(d, char[16], ext_compat,
  2301. c->cdev->use_os_string ? ffs->interfaces_count : 0);
  2302. vla_item_with_sz(d, struct usb_os_desc, os_desc,
  2303. c->cdev->use_os_string ? ffs->interfaces_count : 0);
  2304. vla_item_with_sz(d, struct usb_os_desc_ext_prop, ext_prop,
  2305. ffs->ms_os_descs_ext_prop_count);
  2306. vla_item_with_sz(d, char, ext_prop_name,
  2307. ffs->ms_os_descs_ext_prop_name_len);
  2308. vla_item_with_sz(d, char, ext_prop_data,
  2309. ffs->ms_os_descs_ext_prop_data_len);
  2310. vla_item_with_sz(d, char, raw_descs, ffs->raw_descs_length);
  2311. char *vlabuf;
  2312. ENTER();
  2313. /* Has descriptors only for speeds gadget does not support */
  2314. if (unlikely(!(full | high | super)))
  2315. return -ENOTSUPP;
  2316. /* Allocate a single chunk, less management later on */
  2317. vlabuf = kzalloc(vla_group_size(d), GFP_KERNEL);
  2318. if (unlikely(!vlabuf))
  2319. return -ENOMEM;
  2320. ffs->ms_os_descs_ext_prop_avail = vla_ptr(vlabuf, d, ext_prop);
  2321. ffs->ms_os_descs_ext_prop_name_avail =
  2322. vla_ptr(vlabuf, d, ext_prop_name);
  2323. ffs->ms_os_descs_ext_prop_data_avail =
  2324. vla_ptr(vlabuf, d, ext_prop_data);
  2325. /* Copy descriptors */
  2326. memcpy(vla_ptr(vlabuf, d, raw_descs), ffs->raw_descs,
  2327. ffs->raw_descs_length);
  2328. memset(vla_ptr(vlabuf, d, inums), 0xff, d_inums__sz);
  2329. eps_ptr = vla_ptr(vlabuf, d, eps);
  2330. for (i = 0; i < ffs->eps_count; i++)
  2331. eps_ptr[i].num = -1;
  2332. /* Save pointers
  2333. * d_eps == vlabuf, func->eps used to kfree vlabuf later
  2334. */
  2335. func->eps = vla_ptr(vlabuf, d, eps);
  2336. func->interfaces_nums = vla_ptr(vlabuf, d, inums);
  2337. /*
  2338. * Go through all the endpoint descriptors and allocate
  2339. * endpoints first, so that later we can rewrite the endpoint
  2340. * numbers without worrying that it may be described later on.
  2341. */
  2342. if (likely(full)) {
  2343. func->function.fs_descriptors = vla_ptr(vlabuf, d, fs_descs);
  2344. fs_len = ffs_do_descs(ffs->fs_descs_count,
  2345. vla_ptr(vlabuf, d, raw_descs),
  2346. d_raw_descs__sz,
  2347. __ffs_func_bind_do_descs, func);
  2348. if (unlikely(fs_len < 0)) {
  2349. ret = fs_len;
  2350. goto error;
  2351. }
  2352. } else {
  2353. fs_len = 0;
  2354. }
  2355. if (likely(high)) {
  2356. func->function.hs_descriptors = vla_ptr(vlabuf, d, hs_descs);
  2357. hs_len = ffs_do_descs(ffs->hs_descs_count,
  2358. vla_ptr(vlabuf, d, raw_descs) + fs_len,
  2359. d_raw_descs__sz - fs_len,
  2360. __ffs_func_bind_do_descs, func);
  2361. if (unlikely(hs_len < 0)) {
  2362. ret = hs_len;
  2363. goto error;
  2364. }
  2365. } else {
  2366. hs_len = 0;
  2367. }
  2368. if (likely(super)) {
  2369. func->function.ss_descriptors = vla_ptr(vlabuf, d, ss_descs);
  2370. ss_len = ffs_do_descs(ffs->ss_descs_count,
  2371. vla_ptr(vlabuf, d, raw_descs) + fs_len + hs_len,
  2372. d_raw_descs__sz - fs_len - hs_len,
  2373. __ffs_func_bind_do_descs, func);
  2374. if (unlikely(ss_len < 0)) {
  2375. ret = ss_len;
  2376. goto error;
  2377. }
  2378. } else {
  2379. ss_len = 0;
  2380. }
  2381. /*
  2382. * Now handle interface numbers allocation and interface and
  2383. * endpoint numbers rewriting. We can do that in one go
  2384. * now.
  2385. */
  2386. ret = ffs_do_descs(ffs->fs_descs_count +
  2387. (high ? ffs->hs_descs_count : 0) +
  2388. (super ? ffs->ss_descs_count : 0),
  2389. vla_ptr(vlabuf, d, raw_descs), d_raw_descs__sz,
  2390. __ffs_func_bind_do_nums, func);
  2391. if (unlikely(ret < 0))
  2392. goto error;
  2393. func->function.os_desc_table = vla_ptr(vlabuf, d, os_desc_table);
  2394. if (c->cdev->use_os_string)
  2395. for (i = 0; i < ffs->interfaces_count; ++i) {
  2396. struct usb_os_desc *desc;
  2397. desc = func->function.os_desc_table[i].os_desc =
  2398. vla_ptr(vlabuf, d, os_desc) +
  2399. i * sizeof(struct usb_os_desc);
  2400. desc->ext_compat_id =
  2401. vla_ptr(vlabuf, d, ext_compat) + i * 16;
  2402. INIT_LIST_HEAD(&desc->ext_prop);
  2403. }
  2404. ret = ffs_do_os_descs(ffs->ms_os_descs_count,
  2405. vla_ptr(vlabuf, d, raw_descs) +
  2406. fs_len + hs_len + ss_len,
  2407. d_raw_descs__sz - fs_len - hs_len - ss_len,
  2408. __ffs_func_bind_do_os_desc, func);
  2409. if (unlikely(ret < 0))
  2410. goto error;
  2411. func->function.os_desc_n =
  2412. c->cdev->use_os_string ? ffs->interfaces_count : 0;
  2413. /* And we're done */
  2414. ffs_event_add(ffs, FUNCTIONFS_BIND);
  2415. return 0;
  2416. error:
  2417. /* XXX Do we need to release all claimed endpoints here? */
  2418. return ret;
  2419. }
  2420. static int ffs_func_bind(struct usb_configuration *c,
  2421. struct usb_function *f)
  2422. {
  2423. struct f_fs_opts *ffs_opts = ffs_do_functionfs_bind(f, c);
  2424. struct ffs_function *func = ffs_func_from_usb(f);
  2425. int ret;
  2426. if (IS_ERR(ffs_opts))
  2427. return PTR_ERR(ffs_opts);
  2428. ret = _ffs_func_bind(c, f);
  2429. if (ret && !--ffs_opts->refcnt)
  2430. functionfs_unbind(func->ffs);
  2431. return ret;
  2432. }
  2433. /* Other USB function hooks *************************************************/
  2434. static void ffs_reset_work(struct work_struct *work)
  2435. {
  2436. struct ffs_data *ffs = container_of(work,
  2437. struct ffs_data, reset_work);
  2438. ffs_data_reset(ffs);
  2439. }
  2440. static int ffs_func_set_alt(struct usb_function *f,
  2441. unsigned interface, unsigned alt)
  2442. {
  2443. struct ffs_function *func = ffs_func_from_usb(f);
  2444. struct ffs_data *ffs = func->ffs;
  2445. int ret = 0, intf;
  2446. if (alt != (unsigned)-1) {
  2447. intf = ffs_func_revmap_intf(func, interface);
  2448. if (unlikely(intf < 0))
  2449. return intf;
  2450. }
  2451. if (ffs->func)
  2452. ffs_func_eps_disable(ffs->func);
  2453. if (ffs->state == FFS_DEACTIVATED) {
  2454. ffs->state = FFS_CLOSING;
  2455. INIT_WORK(&ffs->reset_work, ffs_reset_work);
  2456. schedule_work(&ffs->reset_work);
  2457. return -ENODEV;
  2458. }
  2459. if (ffs->state != FFS_ACTIVE)
  2460. return -ENODEV;
  2461. if (alt == (unsigned)-1) {
  2462. ffs->func = NULL;
  2463. ffs_event_add(ffs, FUNCTIONFS_DISABLE);
  2464. return 0;
  2465. }
  2466. ffs->func = func;
  2467. ret = ffs_func_eps_enable(func);
  2468. if (likely(ret >= 0))
  2469. ffs_event_add(ffs, FUNCTIONFS_ENABLE);
  2470. return ret;
  2471. }
  2472. static void ffs_func_disable(struct usb_function *f)
  2473. {
  2474. ffs_func_set_alt(f, 0, (unsigned)-1);
  2475. }
  2476. static int ffs_func_setup(struct usb_function *f,
  2477. const struct usb_ctrlrequest *creq)
  2478. {
  2479. struct ffs_function *func = ffs_func_from_usb(f);
  2480. struct ffs_data *ffs = func->ffs;
  2481. unsigned long flags;
  2482. int ret;
  2483. ENTER();
  2484. pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType);
  2485. pr_vdebug("creq->bRequest = %02x\n", creq->bRequest);
  2486. pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq->wValue));
  2487. pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq->wIndex));
  2488. pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq->wLength));
  2489. /*
  2490. * Most requests directed to interface go through here
  2491. * (notable exceptions are set/get interface) so we need to
  2492. * handle them. All other either handled by composite or
  2493. * passed to usb_configuration->setup() (if one is set). No
  2494. * matter, we will handle requests directed to endpoint here
  2495. * as well (as it's straightforward) but what to do with any
  2496. * other request?
  2497. */
  2498. if (ffs->state != FFS_ACTIVE)
  2499. return -ENODEV;
  2500. switch (creq->bRequestType & USB_RECIP_MASK) {
  2501. case USB_RECIP_INTERFACE:
  2502. ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex));
  2503. if (unlikely(ret < 0))
  2504. return ret;
  2505. break;
  2506. case USB_RECIP_ENDPOINT:
  2507. ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex));
  2508. if (unlikely(ret < 0))
  2509. return ret;
  2510. if (func->ffs->user_flags & FUNCTIONFS_VIRTUAL_ADDR)
  2511. ret = func->ffs->eps_addrmap[ret];
  2512. break;
  2513. default:
  2514. return -EOPNOTSUPP;
  2515. }
  2516. spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
  2517. ffs->ev.setup = *creq;
  2518. ffs->ev.setup.wIndex = cpu_to_le16(ret);
  2519. __ffs_event_add(ffs, FUNCTIONFS_SETUP);
  2520. spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
  2521. return creq->wLength == 0 ? USB_GADGET_DELAYED_STATUS : 0;
  2522. }
  2523. static void ffs_func_suspend(struct usb_function *f)
  2524. {
  2525. ENTER();
  2526. ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND);
  2527. }
  2528. static void ffs_func_resume(struct usb_function *f)
  2529. {
  2530. ENTER();
  2531. ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME);
  2532. }
  2533. /* Endpoint and interface numbers reverse mapping ***************************/
  2534. static int ffs_func_revmap_ep(struct ffs_function *func, u8 num)
  2535. {
  2536. num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK];
  2537. return num ? num : -EDOM;
  2538. }
  2539. static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf)
  2540. {
  2541. short *nums = func->interfaces_nums;
  2542. unsigned count = func->ffs->interfaces_count;
  2543. for (; count; --count, ++nums) {
  2544. if (*nums >= 0 && *nums == intf)
  2545. return nums - func->interfaces_nums;
  2546. }
  2547. return -EDOM;
  2548. }
  2549. /* Devices management *******************************************************/
  2550. static LIST_HEAD(ffs_devices);
  2551. static struct ffs_dev *_ffs_do_find_dev(const char *name)
  2552. {
  2553. struct ffs_dev *dev;
  2554. list_for_each_entry(dev, &ffs_devices, entry) {
  2555. if (!dev->name || !name)
  2556. continue;
  2557. if (strcmp(dev->name, name) == 0)
  2558. return dev;
  2559. }
  2560. return NULL;
  2561. }
  2562. /*
  2563. * ffs_lock must be taken by the caller of this function
  2564. */
  2565. static struct ffs_dev *_ffs_get_single_dev(void)
  2566. {
  2567. struct ffs_dev *dev;
  2568. if (list_is_singular(&ffs_devices)) {
  2569. dev = list_first_entry(&ffs_devices, struct ffs_dev, entry);
  2570. if (dev->single)
  2571. return dev;
  2572. }
  2573. return NULL;
  2574. }
  2575. /*
  2576. * ffs_lock must be taken by the caller of this function
  2577. */
  2578. static struct ffs_dev *_ffs_find_dev(const char *name)
  2579. {
  2580. struct ffs_dev *dev;
  2581. dev = _ffs_get_single_dev();
  2582. if (dev)
  2583. return dev;
  2584. return _ffs_do_find_dev(name);
  2585. }
  2586. /* Configfs support *********************************************************/
  2587. static inline struct f_fs_opts *to_ffs_opts(struct config_item *item)
  2588. {
  2589. return container_of(to_config_group(item), struct f_fs_opts,
  2590. func_inst.group);
  2591. }
  2592. static void ffs_attr_release(struct config_item *item)
  2593. {
  2594. struct f_fs_opts *opts = to_ffs_opts(item);
  2595. usb_put_function_instance(&opts->func_inst);
  2596. }
  2597. static struct configfs_item_operations ffs_item_ops = {
  2598. .release = ffs_attr_release,
  2599. };
  2600. static struct config_item_type ffs_func_type = {
  2601. .ct_item_ops = &ffs_item_ops,
  2602. .ct_owner = THIS_MODULE,
  2603. };
  2604. /* Function registration interface ******************************************/
  2605. static void ffs_free_inst(struct usb_function_instance *f)
  2606. {
  2607. struct f_fs_opts *opts;
  2608. opts = to_f_fs_opts(f);
  2609. ffs_dev_lock();
  2610. _ffs_free_dev(opts->dev);
  2611. ffs_dev_unlock();
  2612. kfree(opts);
  2613. }
  2614. #define MAX_INST_NAME_LEN 40
  2615. static int ffs_set_inst_name(struct usb_function_instance *fi, const char *name)
  2616. {
  2617. struct f_fs_opts *opts;
  2618. char *ptr;
  2619. const char *tmp;
  2620. int name_len, ret;
  2621. name_len = strlen(name) + 1;
  2622. if (name_len > MAX_INST_NAME_LEN)
  2623. return -ENAMETOOLONG;
  2624. ptr = kstrndup(name, name_len, GFP_KERNEL);
  2625. if (!ptr)
  2626. return -ENOMEM;
  2627. opts = to_f_fs_opts(fi);
  2628. tmp = NULL;
  2629. ffs_dev_lock();
  2630. tmp = opts->dev->name_allocated ? opts->dev->name : NULL;
  2631. ret = _ffs_name_dev(opts->dev, ptr);
  2632. if (ret) {
  2633. kfree(ptr);
  2634. ffs_dev_unlock();
  2635. return ret;
  2636. }
  2637. opts->dev->name_allocated = true;
  2638. ffs_dev_unlock();
  2639. kfree(tmp);
  2640. return 0;
  2641. }
  2642. static struct usb_function_instance *ffs_alloc_inst(void)
  2643. {
  2644. struct f_fs_opts *opts;
  2645. struct ffs_dev *dev;
  2646. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  2647. if (!opts)
  2648. return ERR_PTR(-ENOMEM);
  2649. opts->func_inst.set_inst_name = ffs_set_inst_name;
  2650. opts->func_inst.free_func_inst = ffs_free_inst;
  2651. ffs_dev_lock();
  2652. dev = _ffs_alloc_dev();
  2653. ffs_dev_unlock();
  2654. if (IS_ERR(dev)) {
  2655. kfree(opts);
  2656. return ERR_CAST(dev);
  2657. }
  2658. opts->dev = dev;
  2659. dev->opts = opts;
  2660. config_group_init_type_name(&opts->func_inst.group, "",
  2661. &ffs_func_type);
  2662. return &opts->func_inst;
  2663. }
  2664. static void ffs_free(struct usb_function *f)
  2665. {
  2666. kfree(ffs_func_from_usb(f));
  2667. }
  2668. static void ffs_func_unbind(struct usb_configuration *c,
  2669. struct usb_function *f)
  2670. {
  2671. struct ffs_function *func = ffs_func_from_usb(f);
  2672. struct ffs_data *ffs = func->ffs;
  2673. struct f_fs_opts *opts =
  2674. container_of(f->fi, struct f_fs_opts, func_inst);
  2675. struct ffs_ep *ep = func->eps;
  2676. unsigned count = ffs->eps_count;
  2677. unsigned long flags;
  2678. ENTER();
  2679. if (ffs->func == func) {
  2680. ffs_func_eps_disable(func);
  2681. ffs->func = NULL;
  2682. }
  2683. if (!--opts->refcnt)
  2684. functionfs_unbind(ffs);
  2685. /* cleanup after autoconfig */
  2686. spin_lock_irqsave(&func->ffs->eps_lock, flags);
  2687. do {
  2688. if (ep->ep && ep->req)
  2689. usb_ep_free_request(ep->ep, ep->req);
  2690. ep->req = NULL;
  2691. ++ep;
  2692. } while (--count);
  2693. spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
  2694. kfree(func->eps);
  2695. func->eps = NULL;
  2696. /*
  2697. * eps, descriptors and interfaces_nums are allocated in the
  2698. * same chunk so only one free is required.
  2699. */
  2700. func->function.fs_descriptors = NULL;
  2701. func->function.hs_descriptors = NULL;
  2702. func->function.ss_descriptors = NULL;
  2703. func->interfaces_nums = NULL;
  2704. ffs_event_add(ffs, FUNCTIONFS_UNBIND);
  2705. }
  2706. static struct usb_function *ffs_alloc(struct usb_function_instance *fi)
  2707. {
  2708. struct ffs_function *func;
  2709. ENTER();
  2710. func = kzalloc(sizeof(*func), GFP_KERNEL);
  2711. if (unlikely(!func))
  2712. return ERR_PTR(-ENOMEM);
  2713. func->function.name = "Function FS Gadget";
  2714. func->function.bind = ffs_func_bind;
  2715. func->function.unbind = ffs_func_unbind;
  2716. func->function.set_alt = ffs_func_set_alt;
  2717. func->function.disable = ffs_func_disable;
  2718. func->function.setup = ffs_func_setup;
  2719. func->function.suspend = ffs_func_suspend;
  2720. func->function.resume = ffs_func_resume;
  2721. func->function.free_func = ffs_free;
  2722. return &func->function;
  2723. }
  2724. /*
  2725. * ffs_lock must be taken by the caller of this function
  2726. */
  2727. static struct ffs_dev *_ffs_alloc_dev(void)
  2728. {
  2729. struct ffs_dev *dev;
  2730. int ret;
  2731. if (_ffs_get_single_dev())
  2732. return ERR_PTR(-EBUSY);
  2733. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  2734. if (!dev)
  2735. return ERR_PTR(-ENOMEM);
  2736. if (list_empty(&ffs_devices)) {
  2737. ret = functionfs_init();
  2738. if (ret) {
  2739. kfree(dev);
  2740. return ERR_PTR(ret);
  2741. }
  2742. }
  2743. list_add(&dev->entry, &ffs_devices);
  2744. return dev;
  2745. }
  2746. /*
  2747. * ffs_lock must be taken by the caller of this function
  2748. * The caller is responsible for "name" being available whenever f_fs needs it
  2749. */
  2750. static int _ffs_name_dev(struct ffs_dev *dev, const char *name)
  2751. {
  2752. struct ffs_dev *existing;
  2753. existing = _ffs_do_find_dev(name);
  2754. if (existing)
  2755. return -EBUSY;
  2756. dev->name = name;
  2757. return 0;
  2758. }
  2759. /*
  2760. * The caller is responsible for "name" being available whenever f_fs needs it
  2761. */
  2762. int ffs_name_dev(struct ffs_dev *dev, const char *name)
  2763. {
  2764. int ret;
  2765. ffs_dev_lock();
  2766. ret = _ffs_name_dev(dev, name);
  2767. ffs_dev_unlock();
  2768. return ret;
  2769. }
  2770. EXPORT_SYMBOL_GPL(ffs_name_dev);
  2771. int ffs_single_dev(struct ffs_dev *dev)
  2772. {
  2773. int ret;
  2774. ret = 0;
  2775. ffs_dev_lock();
  2776. if (!list_is_singular(&ffs_devices))
  2777. ret = -EBUSY;
  2778. else
  2779. dev->single = true;
  2780. ffs_dev_unlock();
  2781. return ret;
  2782. }
  2783. EXPORT_SYMBOL_GPL(ffs_single_dev);
  2784. /*
  2785. * ffs_lock must be taken by the caller of this function
  2786. */
  2787. static void _ffs_free_dev(struct ffs_dev *dev)
  2788. {
  2789. list_del(&dev->entry);
  2790. if (dev->name_allocated)
  2791. kfree(dev->name);
  2792. kfree(dev);
  2793. if (list_empty(&ffs_devices))
  2794. functionfs_cleanup();
  2795. }
  2796. static void *ffs_acquire_dev(const char *dev_name)
  2797. {
  2798. struct ffs_dev *ffs_dev;
  2799. ENTER();
  2800. ffs_dev_lock();
  2801. ffs_dev = _ffs_find_dev(dev_name);
  2802. if (!ffs_dev)
  2803. ffs_dev = ERR_PTR(-ENOENT);
  2804. else if (ffs_dev->mounted)
  2805. ffs_dev = ERR_PTR(-EBUSY);
  2806. else if (ffs_dev->ffs_acquire_dev_callback &&
  2807. ffs_dev->ffs_acquire_dev_callback(ffs_dev))
  2808. ffs_dev = ERR_PTR(-ENOENT);
  2809. else
  2810. ffs_dev->mounted = true;
  2811. ffs_dev_unlock();
  2812. return ffs_dev;
  2813. }
  2814. static void ffs_release_dev(struct ffs_data *ffs_data)
  2815. {
  2816. struct ffs_dev *ffs_dev;
  2817. ENTER();
  2818. ffs_dev_lock();
  2819. ffs_dev = ffs_data->private_data;
  2820. if (ffs_dev) {
  2821. ffs_dev->mounted = false;
  2822. if (ffs_dev->ffs_release_dev_callback)
  2823. ffs_dev->ffs_release_dev_callback(ffs_dev);
  2824. }
  2825. ffs_dev_unlock();
  2826. }
  2827. static int ffs_ready(struct ffs_data *ffs)
  2828. {
  2829. struct ffs_dev *ffs_obj;
  2830. int ret = 0;
  2831. ENTER();
  2832. ffs_dev_lock();
  2833. ffs_obj = ffs->private_data;
  2834. if (!ffs_obj) {
  2835. ret = -EINVAL;
  2836. goto done;
  2837. }
  2838. if (WARN_ON(ffs_obj->desc_ready)) {
  2839. ret = -EBUSY;
  2840. goto done;
  2841. }
  2842. ffs_obj->desc_ready = true;
  2843. ffs_obj->ffs_data = ffs;
  2844. if (ffs_obj->ffs_ready_callback) {
  2845. ret = ffs_obj->ffs_ready_callback(ffs);
  2846. if (ret)
  2847. goto done;
  2848. }
  2849. set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags);
  2850. done:
  2851. ffs_dev_unlock();
  2852. return ret;
  2853. }
  2854. static void ffs_closed(struct ffs_data *ffs)
  2855. {
  2856. struct ffs_dev *ffs_obj;
  2857. struct f_fs_opts *opts;
  2858. struct config_item *ci;
  2859. ENTER();
  2860. ffs_dev_lock();
  2861. ffs_obj = ffs->private_data;
  2862. if (!ffs_obj)
  2863. goto done;
  2864. ffs_obj->desc_ready = false;
  2865. if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags) &&
  2866. ffs_obj->ffs_closed_callback)
  2867. ffs_obj->ffs_closed_callback(ffs);
  2868. if (ffs_obj->opts)
  2869. opts = ffs_obj->opts;
  2870. else
  2871. goto done;
  2872. if (opts->no_configfs || !opts->func_inst.group.cg_item.ci_parent
  2873. || !atomic_read(&opts->func_inst.group.cg_item.ci_kref.refcount))
  2874. goto done;
  2875. ci = opts->func_inst.group.cg_item.ci_parent->ci_parent;
  2876. ffs_dev_unlock();
  2877. if (test_bit(FFS_FL_BOUND, &ffs->flags))
  2878. unregister_gadget_item(ci);
  2879. return;
  2880. done:
  2881. ffs_dev_unlock();
  2882. }
  2883. /* Misc helper functions ****************************************************/
  2884. static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
  2885. {
  2886. return nonblock
  2887. ? likely(mutex_trylock(mutex)) ? 0 : -EAGAIN
  2888. : mutex_lock_interruptible(mutex);
  2889. }
  2890. static char *ffs_prepare_buffer(const char __user *buf, size_t len)
  2891. {
  2892. char *data;
  2893. if (unlikely(!len))
  2894. return NULL;
  2895. data = kmalloc(len, GFP_KERNEL);
  2896. if (unlikely(!data))
  2897. return ERR_PTR(-ENOMEM);
  2898. if (unlikely(copy_from_user(data, buf, len))) {
  2899. kfree(data);
  2900. return ERR_PTR(-EFAULT);
  2901. }
  2902. pr_vdebug("Buffer from user space:\n");
  2903. ffs_dump_mem("", data, len);
  2904. return data;
  2905. }
  2906. DECLARE_USB_FUNCTION_INIT(ffs, ffs_alloc_inst, ffs_alloc);
  2907. MODULE_LICENSE("GPL");
  2908. MODULE_AUTHOR("Michal Nazarewicz");