f_fs.c 88 KB

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