f_fs.c 78 KB

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