f_fs.c 79 KB

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