f_fs.c 79 KB

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