f_fs.c 79 KB

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