f_fs.c 87 KB

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