f_fs.c 83 KB

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