f_fs.c 79 KB

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