f_fs.c 79 KB

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