f_fs.c 88 KB

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