f_fs.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645
  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 <asm/unaligned.h>
  23. #include <linux/usb/composite.h>
  24. #include <linux/usb/functionfs.h>
  25. #define FUNCTIONFS_MAGIC 0xa647361 /* Chosen by a honest dice roll ;) */
  26. /* Debugging ****************************************************************/
  27. #ifdef VERBOSE_DEBUG
  28. #ifndef pr_vdebug
  29. # define pr_vdebug pr_debug
  30. #endif /* pr_vdebug */
  31. # define ffs_dump_mem(prefix, ptr, len) \
  32. print_hex_dump_bytes(pr_fmt(prefix ": "), DUMP_PREFIX_NONE, ptr, len)
  33. #else
  34. #ifndef pr_vdebug
  35. # define pr_vdebug(...) do { } while (0)
  36. #endif /* pr_vdebug */
  37. # define ffs_dump_mem(prefix, ptr, len) do { } while (0)
  38. #endif /* VERBOSE_DEBUG */
  39. #define ENTER() pr_vdebug("%s()\n", __func__)
  40. /* The data structure and setup file ****************************************/
  41. enum ffs_state {
  42. /*
  43. * Waiting for descriptors and strings.
  44. *
  45. * In this state no open(2), read(2) or write(2) on epfiles
  46. * may succeed (which should not be the problem as there
  47. * should be no such files opened in the first place).
  48. */
  49. FFS_READ_DESCRIPTORS,
  50. FFS_READ_STRINGS,
  51. /*
  52. * We've got descriptors and strings. We are or have called
  53. * functionfs_ready_callback(). functionfs_bind() may have
  54. * been called but we don't know.
  55. *
  56. * This is the only state in which operations on epfiles may
  57. * succeed.
  58. */
  59. FFS_ACTIVE,
  60. /*
  61. * All endpoints have been closed. This state is also set if
  62. * we encounter an unrecoverable error. The only
  63. * unrecoverable error is situation when after reading strings
  64. * from user space we fail to initialise epfiles or
  65. * functionfs_ready_callback() returns with error (<0).
  66. *
  67. * In this state no open(2), read(2) or write(2) (both on ep0
  68. * as well as epfile) may succeed (at this point epfiles are
  69. * unlinked and all closed so this is not a problem; ep0 is
  70. * also closed but ep0 file exists and so open(2) on ep0 must
  71. * fail).
  72. */
  73. FFS_CLOSING
  74. };
  75. enum ffs_setup_state {
  76. /* There is no setup request pending. */
  77. FFS_NO_SETUP,
  78. /*
  79. * User has read events and there was a setup request event
  80. * there. The next read/write on ep0 will handle the
  81. * request.
  82. */
  83. FFS_SETUP_PENDING,
  84. /*
  85. * There was event pending but before user space handled it
  86. * some other event was introduced which canceled existing
  87. * setup. If this state is set read/write on ep0 return
  88. * -EIDRM. This state is only set when adding event.
  89. */
  90. FFS_SETUP_CANCELED
  91. };
  92. struct ffs_epfile;
  93. struct ffs_function;
  94. struct ffs_data {
  95. struct usb_gadget *gadget;
  96. /*
  97. * Protect access read/write operations, only one read/write
  98. * at a time. As a consequence protects ep0req and company.
  99. * While setup request is being processed (queued) this is
  100. * held.
  101. */
  102. struct mutex mutex;
  103. /*
  104. * Protect access to endpoint related structures (basically
  105. * usb_ep_queue(), usb_ep_dequeue(), etc. calls) except for
  106. * endpoint zero.
  107. */
  108. spinlock_t eps_lock;
  109. /*
  110. * XXX REVISIT do we need our own request? Since we are not
  111. * handling setup requests immediately user space may be so
  112. * slow that another setup will be sent to the gadget but this
  113. * time not to us but another function and then there could be
  114. * a race. Is that the case? Or maybe we can use cdev->req
  115. * after all, maybe we just need some spinlock for that?
  116. */
  117. struct usb_request *ep0req; /* P: mutex */
  118. struct completion ep0req_completion; /* P: mutex */
  119. int ep0req_status; /* P: mutex */
  120. struct completion epin_completion;
  121. struct completion epout_completion;
  122. /* reference counter */
  123. atomic_t ref;
  124. /* how many files are opened (EP0 and others) */
  125. atomic_t opened;
  126. /* EP0 state */
  127. enum ffs_state state;
  128. /*
  129. * Possible transitions:
  130. * + FFS_NO_SETUP -> FFS_SETUP_PENDING -- P: ev.waitq.lock
  131. * happens only in ep0 read which is P: mutex
  132. * + FFS_SETUP_PENDING -> FFS_NO_SETUP -- P: ev.waitq.lock
  133. * happens only in ep0 i/o which is P: mutex
  134. * + FFS_SETUP_PENDING -> FFS_SETUP_CANCELED -- P: ev.waitq.lock
  135. * + FFS_SETUP_CANCELED -> FFS_NO_SETUP -- cmpxchg
  136. */
  137. enum ffs_setup_state setup_state;
  138. #define FFS_SETUP_STATE(ffs) \
  139. ((enum ffs_setup_state)cmpxchg(&(ffs)->setup_state, \
  140. FFS_SETUP_CANCELED, FFS_NO_SETUP))
  141. /* Events & such. */
  142. struct {
  143. u8 types[4];
  144. unsigned short count;
  145. /* XXX REVISIT need to update it in some places, or do we? */
  146. unsigned short can_stall;
  147. struct usb_ctrlrequest setup;
  148. wait_queue_head_t waitq;
  149. } ev; /* the whole structure, P: ev.waitq.lock */
  150. /* Flags */
  151. unsigned long flags;
  152. #define FFS_FL_CALL_CLOSED_CALLBACK 0
  153. #define FFS_FL_BOUND 1
  154. /* Active function */
  155. struct ffs_function *func;
  156. /*
  157. * Device name, write once when file system is mounted.
  158. * Intended for user to read if she wants.
  159. */
  160. const char *dev_name;
  161. /* Private data for our user (ie. gadget). Managed by user. */
  162. void *private_data;
  163. /* filled by __ffs_data_got_descs() */
  164. /*
  165. * Real descriptors are 16 bytes after raw_descs (so you need
  166. * to skip 16 bytes (ie. ffs->raw_descs + 16) to get to the
  167. * first full speed descriptor). raw_descs_length and
  168. * raw_fs_hs_descs_length do not have those 16 bytes added.
  169. * ss_desc are 8 bytes (ss_magic + count) pass the hs_descs
  170. */
  171. const void *raw_descs;
  172. unsigned raw_descs_length;
  173. unsigned raw_fs_hs_descs_length;
  174. unsigned raw_ss_descs_offset;
  175. unsigned raw_ss_descs_length;
  176. unsigned fs_descs_count;
  177. unsigned hs_descs_count;
  178. unsigned ss_descs_count;
  179. unsigned short strings_count;
  180. unsigned short interfaces_count;
  181. unsigned short eps_count;
  182. unsigned short _pad1;
  183. /* filled by __ffs_data_got_strings() */
  184. /* ids in stringtabs are set in functionfs_bind() */
  185. const void *raw_strings;
  186. struct usb_gadget_strings **stringtabs;
  187. /*
  188. * File system's super block, write once when file system is
  189. * mounted.
  190. */
  191. struct super_block *sb;
  192. /* File permissions, written once when fs is mounted */
  193. struct ffs_file_perms {
  194. umode_t mode;
  195. kuid_t uid;
  196. kgid_t gid;
  197. } file_perms;
  198. /*
  199. * The endpoint files, filled by ffs_epfiles_create(),
  200. * destroyed by ffs_epfiles_destroy().
  201. */
  202. struct ffs_epfile *epfiles;
  203. };
  204. /* Reference counter handling */
  205. static void ffs_data_get(struct ffs_data *ffs);
  206. static void ffs_data_put(struct ffs_data *ffs);
  207. /* Creates new ffs_data object. */
  208. static struct ffs_data *__must_check ffs_data_new(void) __attribute__((malloc));
  209. /* Opened counter handling. */
  210. static void ffs_data_opened(struct ffs_data *ffs);
  211. static void ffs_data_closed(struct ffs_data *ffs);
  212. /* Called with ffs->mutex held; take over ownership of data. */
  213. static int __must_check
  214. __ffs_data_got_descs(struct ffs_data *ffs, char *data, size_t len);
  215. static int __must_check
  216. __ffs_data_got_strings(struct ffs_data *ffs, char *data, size_t len);
  217. /* The function structure ***************************************************/
  218. struct ffs_ep;
  219. struct ffs_function {
  220. struct usb_configuration *conf;
  221. struct usb_gadget *gadget;
  222. struct ffs_data *ffs;
  223. struct ffs_ep *eps;
  224. u8 eps_revmap[16];
  225. short *interfaces_nums;
  226. struct usb_function function;
  227. };
  228. static struct ffs_function *ffs_func_from_usb(struct usb_function *f)
  229. {
  230. return container_of(f, struct ffs_function, function);
  231. }
  232. static void ffs_func_free(struct ffs_function *func);
  233. static void ffs_func_eps_disable(struct ffs_function *func);
  234. static int __must_check ffs_func_eps_enable(struct ffs_function *func);
  235. static int ffs_func_bind(struct usb_configuration *,
  236. struct usb_function *);
  237. static void ffs_func_unbind(struct usb_configuration *,
  238. struct usb_function *);
  239. static int ffs_func_set_alt(struct usb_function *, unsigned, unsigned);
  240. static void ffs_func_disable(struct usb_function *);
  241. static int ffs_func_setup(struct usb_function *,
  242. const struct usb_ctrlrequest *);
  243. static void ffs_func_suspend(struct usb_function *);
  244. static void ffs_func_resume(struct usb_function *);
  245. static int ffs_func_revmap_ep(struct ffs_function *func, u8 num);
  246. static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf);
  247. /* The endpoints structures *************************************************/
  248. struct ffs_ep {
  249. struct usb_ep *ep; /* P: ffs->eps_lock */
  250. struct usb_request *req; /* P: epfile->mutex */
  251. /* [0]: full speed, [1]: high speed, [2]: super speed */
  252. struct usb_endpoint_descriptor *descs[3];
  253. u8 num;
  254. int status; /* P: epfile->mutex */
  255. };
  256. struct ffs_epfile {
  257. /* Protects ep->ep and ep->req. */
  258. struct mutex mutex;
  259. wait_queue_head_t wait;
  260. atomic_t error;
  261. struct ffs_data *ffs;
  262. struct ffs_ep *ep; /* P: ffs->eps_lock */
  263. struct dentry *dentry;
  264. char name[5];
  265. unsigned char in; /* P: ffs->eps_lock */
  266. unsigned char isoc; /* P: ffs->eps_lock */
  267. unsigned char _pad;
  268. };
  269. static int __must_check ffs_epfiles_create(struct ffs_data *ffs);
  270. static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count);
  271. static struct inode *__must_check
  272. ffs_sb_create_file(struct super_block *sb, const char *name, void *data,
  273. const struct file_operations *fops,
  274. struct dentry **dentry_p);
  275. /* Misc helper functions ****************************************************/
  276. static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
  277. __attribute__((warn_unused_result, nonnull));
  278. static char *ffs_prepare_buffer(const char __user *buf, size_t len)
  279. __attribute__((warn_unused_result, nonnull));
  280. /* Control file aka ep0 *****************************************************/
  281. static void ffs_ep0_complete(struct usb_ep *ep, struct usb_request *req)
  282. {
  283. struct ffs_data *ffs = req->context;
  284. complete_all(&ffs->ep0req_completion);
  285. }
  286. static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)
  287. {
  288. struct usb_request *req = ffs->ep0req;
  289. int ret;
  290. req->zero = len < le16_to_cpu(ffs->ev.setup.wLength);
  291. spin_unlock_irq(&ffs->ev.waitq.lock);
  292. req->buf = data;
  293. req->length = len;
  294. /*
  295. * UDC layer requires to provide a buffer even for ZLP, but should
  296. * not use it at all. Let's provide some poisoned pointer to catch
  297. * possible bug in the driver.
  298. */
  299. if (req->buf == NULL)
  300. req->buf = (void *)0xDEADBABE;
  301. INIT_COMPLETION(ffs->ep0req_completion);
  302. ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC);
  303. if (unlikely(ret < 0))
  304. return ret;
  305. ret = wait_for_completion_interruptible(&ffs->ep0req_completion);
  306. if (unlikely(ret)) {
  307. usb_ep_dequeue(ffs->gadget->ep0, req);
  308. return -EINTR;
  309. }
  310. ffs->setup_state = FFS_NO_SETUP;
  311. return ffs->ep0req_status;
  312. }
  313. static int __ffs_ep0_stall(struct ffs_data *ffs)
  314. {
  315. if (ffs->ev.can_stall) {
  316. pr_vdebug("ep0 stall\n");
  317. usb_ep_set_halt(ffs->gadget->ep0);
  318. ffs->setup_state = FFS_NO_SETUP;
  319. return -EL2HLT;
  320. } else {
  321. pr_debug("bogus ep0 stall!\n");
  322. return -ESRCH;
  323. }
  324. }
  325. static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
  326. size_t len, loff_t *ptr)
  327. {
  328. struct ffs_data *ffs = file->private_data;
  329. ssize_t ret;
  330. char *data;
  331. ENTER();
  332. /* Fast check if setup was canceled */
  333. if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED)
  334. return -EIDRM;
  335. /* Acquire mutex */
  336. ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
  337. if (unlikely(ret < 0))
  338. return ret;
  339. /* Check state */
  340. switch (ffs->state) {
  341. case FFS_READ_DESCRIPTORS:
  342. case FFS_READ_STRINGS:
  343. /* Copy data */
  344. if (unlikely(len < 16)) {
  345. ret = -EINVAL;
  346. break;
  347. }
  348. data = ffs_prepare_buffer(buf, len);
  349. if (IS_ERR(data)) {
  350. ret = PTR_ERR(data);
  351. break;
  352. }
  353. /* Handle data */
  354. if (ffs->state == FFS_READ_DESCRIPTORS) {
  355. pr_info("read descriptors\n");
  356. ret = __ffs_data_got_descs(ffs, data, len);
  357. if (unlikely(ret < 0))
  358. break;
  359. ffs->state = FFS_READ_STRINGS;
  360. ret = len;
  361. } else {
  362. pr_info("read strings\n");
  363. ret = __ffs_data_got_strings(ffs, data, len);
  364. if (unlikely(ret < 0))
  365. break;
  366. ret = ffs_epfiles_create(ffs);
  367. if (unlikely(ret)) {
  368. ffs->state = FFS_CLOSING;
  369. break;
  370. }
  371. ffs->state = FFS_ACTIVE;
  372. mutex_unlock(&ffs->mutex);
  373. ret = functionfs_ready_callback(ffs);
  374. if (unlikely(ret < 0)) {
  375. ffs->state = FFS_CLOSING;
  376. return ret;
  377. }
  378. set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags);
  379. return len;
  380. }
  381. break;
  382. case FFS_ACTIVE:
  383. data = NULL;
  384. /*
  385. * We're called from user space, we can use _irq
  386. * rather then _irqsave
  387. */
  388. spin_lock_irq(&ffs->ev.waitq.lock);
  389. switch (FFS_SETUP_STATE(ffs)) {
  390. case FFS_SETUP_CANCELED:
  391. ret = -EIDRM;
  392. goto done_spin;
  393. case FFS_NO_SETUP:
  394. ret = -ESRCH;
  395. goto done_spin;
  396. case FFS_SETUP_PENDING:
  397. break;
  398. }
  399. /* FFS_SETUP_PENDING */
  400. if (!(ffs->ev.setup.bRequestType & USB_DIR_IN)) {
  401. spin_unlock_irq(&ffs->ev.waitq.lock);
  402. ret = __ffs_ep0_stall(ffs);
  403. break;
  404. }
  405. /* FFS_SETUP_PENDING and not stall */
  406. len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
  407. spin_unlock_irq(&ffs->ev.waitq.lock);
  408. data = ffs_prepare_buffer(buf, len);
  409. if (IS_ERR(data)) {
  410. ret = PTR_ERR(data);
  411. break;
  412. }
  413. spin_lock_irq(&ffs->ev.waitq.lock);
  414. /*
  415. * We are guaranteed to be still in FFS_ACTIVE state
  416. * but the state of setup could have changed from
  417. * FFS_SETUP_PENDING to FFS_SETUP_CANCELED so we need
  418. * to check for that. If that happened we copied data
  419. * from user space in vain but it's unlikely.
  420. *
  421. * For sure we are not in FFS_NO_SETUP since this is
  422. * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP
  423. * transition can be performed and it's protected by
  424. * mutex.
  425. */
  426. if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
  427. ret = -EIDRM;
  428. done_spin:
  429. spin_unlock_irq(&ffs->ev.waitq.lock);
  430. } else {
  431. /* unlocks spinlock */
  432. ret = __ffs_ep0_queue_wait(ffs, data, len);
  433. }
  434. kfree(data);
  435. break;
  436. default:
  437. ret = -EBADFD;
  438. break;
  439. }
  440. mutex_unlock(&ffs->mutex);
  441. return ret;
  442. }
  443. static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf,
  444. size_t n)
  445. {
  446. /*
  447. * We are holding ffs->ev.waitq.lock and ffs->mutex and we need
  448. * to release them.
  449. */
  450. struct usb_functionfs_event events[n];
  451. unsigned i = 0;
  452. memset(events, 0, sizeof events);
  453. do {
  454. events[i].type = ffs->ev.types[i];
  455. if (events[i].type == FUNCTIONFS_SETUP) {
  456. events[i].u.setup = ffs->ev.setup;
  457. ffs->setup_state = FFS_SETUP_PENDING;
  458. }
  459. } while (++i < n);
  460. if (n < ffs->ev.count) {
  461. ffs->ev.count -= n;
  462. memmove(ffs->ev.types, ffs->ev.types + n,
  463. ffs->ev.count * sizeof *ffs->ev.types);
  464. } else {
  465. ffs->ev.count = 0;
  466. }
  467. spin_unlock_irq(&ffs->ev.waitq.lock);
  468. mutex_unlock(&ffs->mutex);
  469. return unlikely(__copy_to_user(buf, events, sizeof events))
  470. ? -EFAULT : sizeof events;
  471. }
  472. static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
  473. size_t len, loff_t *ptr)
  474. {
  475. struct ffs_data *ffs = file->private_data;
  476. char *data = NULL;
  477. size_t n;
  478. int ret;
  479. ENTER();
  480. /* Fast check if setup was canceled */
  481. if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED)
  482. return -EIDRM;
  483. /* Acquire mutex */
  484. ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
  485. if (unlikely(ret < 0))
  486. return ret;
  487. /* Check state */
  488. if (ffs->state != FFS_ACTIVE) {
  489. ret = -EBADFD;
  490. goto done_mutex;
  491. }
  492. /*
  493. * We're called from user space, we can use _irq rather then
  494. * _irqsave
  495. */
  496. spin_lock_irq(&ffs->ev.waitq.lock);
  497. switch (FFS_SETUP_STATE(ffs)) {
  498. case FFS_SETUP_CANCELED:
  499. ret = -EIDRM;
  500. break;
  501. case FFS_NO_SETUP:
  502. n = len / sizeof(struct usb_functionfs_event);
  503. if (unlikely(!n)) {
  504. ret = -EINVAL;
  505. break;
  506. }
  507. if ((file->f_flags & O_NONBLOCK) && !ffs->ev.count) {
  508. ret = -EAGAIN;
  509. break;
  510. }
  511. if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq,
  512. ffs->ev.count)) {
  513. ret = -EINTR;
  514. break;
  515. }
  516. return __ffs_ep0_read_events(ffs, buf,
  517. min(n, (size_t)ffs->ev.count));
  518. case FFS_SETUP_PENDING:
  519. if (ffs->ev.setup.bRequestType & USB_DIR_IN) {
  520. spin_unlock_irq(&ffs->ev.waitq.lock);
  521. ret = __ffs_ep0_stall(ffs);
  522. goto done_mutex;
  523. }
  524. len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
  525. spin_unlock_irq(&ffs->ev.waitq.lock);
  526. if (likely(len)) {
  527. data = kmalloc(len, GFP_KERNEL);
  528. if (unlikely(!data)) {
  529. ret = -ENOMEM;
  530. goto done_mutex;
  531. }
  532. }
  533. spin_lock_irq(&ffs->ev.waitq.lock);
  534. /* See ffs_ep0_write() */
  535. if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
  536. ret = -EIDRM;
  537. break;
  538. }
  539. /* unlocks spinlock */
  540. ret = __ffs_ep0_queue_wait(ffs, data, len);
  541. if (likely(ret > 0) && unlikely(__copy_to_user(buf, data, len)))
  542. ret = -EFAULT;
  543. goto done_mutex;
  544. default:
  545. ret = -EBADFD;
  546. break;
  547. }
  548. spin_unlock_irq(&ffs->ev.waitq.lock);
  549. done_mutex:
  550. mutex_unlock(&ffs->mutex);
  551. kfree(data);
  552. return ret;
  553. }
  554. static int ffs_ep0_open(struct inode *inode, struct file *file)
  555. {
  556. struct ffs_data *ffs = inode->i_private;
  557. ENTER();
  558. if (unlikely(ffs->state == FFS_CLOSING))
  559. return -EBUSY;
  560. file->private_data = ffs;
  561. ffs_data_opened(ffs);
  562. return 0;
  563. }
  564. static int ffs_ep0_release(struct inode *inode, struct file *file)
  565. {
  566. struct ffs_data *ffs = file->private_data;
  567. ENTER();
  568. ffs_data_closed(ffs);
  569. return 0;
  570. }
  571. static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)
  572. {
  573. struct ffs_data *ffs = file->private_data;
  574. struct usb_gadget *gadget = ffs->gadget;
  575. long ret;
  576. ENTER();
  577. if (code == FUNCTIONFS_INTERFACE_REVMAP) {
  578. struct ffs_function *func = ffs->func;
  579. ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV;
  580. } else if (gadget && gadget->ops->ioctl) {
  581. ret = gadget->ops->ioctl(gadget, code, value);
  582. } else {
  583. ret = -ENOTTY;
  584. }
  585. return ret;
  586. }
  587. static const struct file_operations ffs_ep0_operations = {
  588. .llseek = no_llseek,
  589. .open = ffs_ep0_open,
  590. .write = ffs_ep0_write,
  591. .read = ffs_ep0_read,
  592. .release = ffs_ep0_release,
  593. .unlocked_ioctl = ffs_ep0_ioctl,
  594. };
  595. /* "Normal" endpoints operations ********************************************/
  596. static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
  597. {
  598. struct ffs_ep *ep = _ep->driver_data;
  599. ENTER();
  600. /* req may be freed during unbind */
  601. if (ep && ep->req && likely(req->context)) {
  602. struct ffs_ep *ep = _ep->driver_data;
  603. ep->status = req->status ? req->status : req->actual;
  604. complete(req->context);
  605. }
  606. }
  607. static ssize_t ffs_epfile_io(struct file *file,
  608. char __user *buf, size_t len, int read)
  609. {
  610. struct ffs_epfile *epfile = file->private_data;
  611. struct ffs_ep *ep;
  612. struct ffs_data *ffs = epfile->ffs;
  613. char *data = NULL;
  614. ssize_t ret;
  615. int halt;
  616. int buffer_len = !read ? len : round_up(len, 1024);
  617. pr_debug("%s: len %d, buffer_len %d, read %d\n", __func__, len, buffer_len, read);
  618. if (atomic_read(&epfile->error))
  619. return -ENODEV;
  620. goto first_try;
  621. do {
  622. spin_unlock_irq(&epfile->ffs->eps_lock);
  623. mutex_unlock(&epfile->mutex);
  624. first_try:
  625. /* Are we still active? */
  626. if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) {
  627. ret = -ENODEV;
  628. goto error;
  629. }
  630. /* Wait for endpoint to be enabled */
  631. ep = epfile->ep;
  632. if (!ep) {
  633. if (file->f_flags & O_NONBLOCK) {
  634. ret = -EAGAIN;
  635. goto error;
  636. }
  637. /* Don't wait on write if device is offline */
  638. if (!read) {
  639. ret = -ENODEV;
  640. goto error;
  641. }
  642. /*
  643. * if ep is disabled, this fails all current IOs
  644. * and wait for next epfile open to happen
  645. */
  646. if (!atomic_read(&epfile->error)) {
  647. ret = wait_event_interruptible(epfile->wait,
  648. (ep = epfile->ep));
  649. if (ret < 0)
  650. goto error;
  651. }
  652. if (!ep) {
  653. ret = -ENODEV;
  654. goto error;
  655. }
  656. }
  657. /* Do we halt? */
  658. halt = !read == !epfile->in;
  659. if (halt && epfile->isoc) {
  660. ret = -EINVAL;
  661. goto error;
  662. }
  663. /* Allocate & copy */
  664. if (!halt && !data) {
  665. data = kzalloc(buffer_len, GFP_KERNEL);
  666. if (unlikely(!data))
  667. return -ENOMEM;
  668. if (!read &&
  669. unlikely(__copy_from_user(data, buf, len))) {
  670. ret = -EFAULT;
  671. goto error;
  672. }
  673. }
  674. /* We will be using request */
  675. ret = ffs_mutex_lock(&epfile->mutex,
  676. file->f_flags & O_NONBLOCK);
  677. if (unlikely(ret))
  678. goto error;
  679. /*
  680. * We're called from user space, we can use _irq rather then
  681. * _irqsave
  682. */
  683. spin_lock_irq(&epfile->ffs->eps_lock);
  684. /*
  685. * While we were acquiring mutex endpoint got disabled
  686. * or changed?
  687. */
  688. } while (unlikely(epfile->ep != ep));
  689. /* Halt */
  690. if (unlikely(halt)) {
  691. if (likely(epfile->ep == ep) && !WARN_ON(!ep->ep))
  692. usb_ep_set_halt(ep->ep);
  693. spin_unlock_irq(&epfile->ffs->eps_lock);
  694. ret = -EBADMSG;
  695. } else {
  696. /* Fire the request */
  697. struct completion *done;
  698. struct usb_request *req = ep->req;
  699. req->complete = ffs_epfile_io_complete;
  700. req->buf = data;
  701. req->length = buffer_len;
  702. if (read) {
  703. INIT_COMPLETION(ffs->epout_completion);
  704. req->context = done = &ffs->epout_completion;
  705. } else {
  706. INIT_COMPLETION(ffs->epin_completion);
  707. req->context = done = &ffs->epin_completion;
  708. }
  709. ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
  710. spin_unlock_irq(&epfile->ffs->eps_lock);
  711. if (unlikely(ret < 0)) {
  712. ret = -EIO;
  713. } else if (unlikely(wait_for_completion_interruptible(done))) {
  714. spin_lock_irq(&epfile->ffs->eps_lock);
  715. /*
  716. * While we were acquiring lock endpoint got disabled
  717. * (disconnect) or changed (composition switch) ?
  718. */
  719. if (epfile->ep == ep)
  720. usb_ep_dequeue(ep->ep, req);
  721. spin_unlock_irq(&epfile->ffs->eps_lock);
  722. ret = -EINTR;
  723. } else {
  724. spin_lock_irq(&epfile->ffs->eps_lock);
  725. /*
  726. * While we were acquiring lock endpoint got disabled
  727. * (disconnect) or changed (composition switch) ?
  728. */
  729. if (epfile->ep == ep)
  730. ret = ep->status;
  731. else
  732. ret = -ENODEV;
  733. spin_unlock_irq(&epfile->ffs->eps_lock);
  734. if (read && ret > 0) {
  735. if (ret > len)
  736. ret = -EOVERFLOW;
  737. else if (unlikely(copy_to_user(buf, data, ret)))
  738. ret = -EFAULT;
  739. }
  740. }
  741. }
  742. mutex_unlock(&epfile->mutex);
  743. error:
  744. kfree(data);
  745. return ret;
  746. }
  747. static ssize_t
  748. ffs_epfile_write(struct file *file, const char __user *buf, size_t len,
  749. loff_t *ptr)
  750. {
  751. ENTER();
  752. return ffs_epfile_io(file, (char __user *)buf, len, 0);
  753. }
  754. static ssize_t
  755. ffs_epfile_read(struct file *file, char __user *buf, size_t len, loff_t *ptr)
  756. {
  757. ENTER();
  758. return ffs_epfile_io(file, buf, len, 1);
  759. }
  760. static int
  761. ffs_epfile_open(struct inode *inode, struct file *file)
  762. {
  763. struct ffs_epfile *epfile = inode->i_private;
  764. ENTER();
  765. if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
  766. return -ENODEV;
  767. file->private_data = epfile;
  768. ffs_data_opened(epfile->ffs);
  769. atomic_set(&epfile->error, 0);
  770. return 0;
  771. }
  772. static int
  773. ffs_epfile_release(struct inode *inode, struct file *file)
  774. {
  775. struct ffs_epfile *epfile = inode->i_private;
  776. ENTER();
  777. atomic_set(&epfile->error, 1);
  778. ffs_data_closed(epfile->ffs);
  779. file->private_data = NULL;
  780. return 0;
  781. }
  782. static long ffs_epfile_ioctl(struct file *file, unsigned code,
  783. unsigned long value)
  784. {
  785. struct ffs_epfile *epfile = file->private_data;
  786. int ret;
  787. ENTER();
  788. if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
  789. return -ENODEV;
  790. spin_lock_irq(&epfile->ffs->eps_lock);
  791. if (likely(epfile->ep)) {
  792. switch (code) {
  793. case FUNCTIONFS_FIFO_STATUS:
  794. ret = usb_ep_fifo_status(epfile->ep->ep);
  795. break;
  796. case FUNCTIONFS_FIFO_FLUSH:
  797. usb_ep_fifo_flush(epfile->ep->ep);
  798. ret = 0;
  799. break;
  800. case FUNCTIONFS_CLEAR_HALT:
  801. ret = usb_ep_clear_halt(epfile->ep->ep);
  802. break;
  803. case FUNCTIONFS_ENDPOINT_REVMAP:
  804. ret = epfile->ep->num;
  805. break;
  806. case FUNCTIONFS_ENDPOINT_DESC:
  807. {
  808. int desc_idx;
  809. struct usb_endpoint_descriptor *desc;
  810. switch (epfile->ffs->gadget->speed) {
  811. case USB_SPEED_SUPER:
  812. desc_idx = 2;
  813. break;
  814. case USB_SPEED_HIGH:
  815. desc_idx = 1;
  816. break;
  817. default:
  818. desc_idx = 0;
  819. }
  820. desc = epfile->ep->descs[desc_idx];
  821. spin_unlock_irq(&epfile->ffs->eps_lock);
  822. ret = copy_to_user((void *)value, desc, sizeof(*desc));
  823. if (ret)
  824. ret = -EFAULT;
  825. return ret;
  826. }
  827. default:
  828. ret = -ENOTTY;
  829. }
  830. } else {
  831. ret = -ENODEV;
  832. }
  833. spin_unlock_irq(&epfile->ffs->eps_lock);
  834. return ret;
  835. }
  836. static const struct file_operations ffs_epfile_operations = {
  837. .llseek = no_llseek,
  838. .open = ffs_epfile_open,
  839. .write = ffs_epfile_write,
  840. .read = ffs_epfile_read,
  841. .release = ffs_epfile_release,
  842. .unlocked_ioctl = ffs_epfile_ioctl,
  843. };
  844. /* File system and super block operations ***********************************/
  845. /*
  846. * Mounting the file system creates a controller file, used first for
  847. * function configuration then later for event monitoring.
  848. */
  849. static struct inode *__must_check
  850. ffs_sb_make_inode(struct super_block *sb, void *data,
  851. const struct file_operations *fops,
  852. const struct inode_operations *iops,
  853. struct ffs_file_perms *perms)
  854. {
  855. struct inode *inode;
  856. ENTER();
  857. inode = new_inode(sb);
  858. if (likely(inode)) {
  859. struct timespec current_time = CURRENT_TIME;
  860. inode->i_ino = get_next_ino();
  861. inode->i_mode = perms->mode;
  862. inode->i_uid = perms->uid;
  863. inode->i_gid = perms->gid;
  864. inode->i_atime = current_time;
  865. inode->i_mtime = current_time;
  866. inode->i_ctime = current_time;
  867. inode->i_private = data;
  868. if (fops)
  869. inode->i_fop = fops;
  870. if (iops)
  871. inode->i_op = iops;
  872. }
  873. return inode;
  874. }
  875. /* Create "regular" file */
  876. static struct inode *ffs_sb_create_file(struct super_block *sb,
  877. const char *name, void *data,
  878. const struct file_operations *fops,
  879. struct dentry **dentry_p)
  880. {
  881. struct ffs_data *ffs = sb->s_fs_info;
  882. struct dentry *dentry;
  883. struct inode *inode;
  884. ENTER();
  885. dentry = d_alloc_name(sb->s_root, name);
  886. if (unlikely(!dentry))
  887. return NULL;
  888. inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms);
  889. if (unlikely(!inode)) {
  890. dput(dentry);
  891. return NULL;
  892. }
  893. d_add(dentry, inode);
  894. if (dentry_p)
  895. *dentry_p = dentry;
  896. return inode;
  897. }
  898. /* Super block */
  899. static const struct super_operations ffs_sb_operations = {
  900. .statfs = simple_statfs,
  901. .drop_inode = generic_delete_inode,
  902. };
  903. struct ffs_sb_fill_data {
  904. struct ffs_file_perms perms;
  905. umode_t root_mode;
  906. const char *dev_name;
  907. struct ffs_data *ffs_data;
  908. };
  909. static int ffs_sb_fill(struct super_block *sb, void *_data, int silent)
  910. {
  911. struct ffs_sb_fill_data *data = _data;
  912. struct inode *inode;
  913. struct ffs_data *ffs = data->ffs_data;
  914. ENTER();
  915. ffs->sb = sb;
  916. data->ffs_data = NULL;
  917. sb->s_fs_info = ffs;
  918. sb->s_blocksize = PAGE_CACHE_SIZE;
  919. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  920. sb->s_magic = FUNCTIONFS_MAGIC;
  921. sb->s_op = &ffs_sb_operations;
  922. sb->s_time_gran = 1;
  923. /* Root inode */
  924. data->perms.mode = data->root_mode;
  925. inode = ffs_sb_make_inode(sb, NULL,
  926. &simple_dir_operations,
  927. &simple_dir_inode_operations,
  928. &data->perms);
  929. sb->s_root = d_make_root(inode);
  930. if (unlikely(!sb->s_root))
  931. return -ENOMEM;
  932. /* EP0 file */
  933. if (unlikely(!ffs_sb_create_file(sb, "ep0", ffs,
  934. &ffs_ep0_operations, NULL)))
  935. return -ENOMEM;
  936. return 0;
  937. }
  938. static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
  939. {
  940. ENTER();
  941. if (!opts || !*opts)
  942. return 0;
  943. for (;;) {
  944. unsigned long value;
  945. char *eq, *comma;
  946. /* Option limit */
  947. comma = strchr(opts, ',');
  948. if (comma)
  949. *comma = 0;
  950. /* Value limit */
  951. eq = strchr(opts, '=');
  952. if (unlikely(!eq)) {
  953. pr_err("'=' missing in %s\n", opts);
  954. return -EINVAL;
  955. }
  956. *eq = 0;
  957. /* Parse value */
  958. if (kstrtoul(eq + 1, 0, &value)) {
  959. pr_err("%s: invalid value: %s\n", opts, eq + 1);
  960. return -EINVAL;
  961. }
  962. /* Interpret option */
  963. switch (eq - opts) {
  964. case 5:
  965. if (!memcmp(opts, "rmode", 5))
  966. data->root_mode = (value & 0555) | S_IFDIR;
  967. else if (!memcmp(opts, "fmode", 5))
  968. data->perms.mode = (value & 0666) | S_IFREG;
  969. else
  970. goto invalid;
  971. break;
  972. case 4:
  973. if (!memcmp(opts, "mode", 4)) {
  974. data->root_mode = (value & 0555) | S_IFDIR;
  975. data->perms.mode = (value & 0666) | S_IFREG;
  976. } else {
  977. goto invalid;
  978. }
  979. break;
  980. case 3:
  981. if (!memcmp(opts, "uid", 3)) {
  982. data->perms.uid = make_kuid(current_user_ns(), value);
  983. if (!uid_valid(data->perms.uid)) {
  984. pr_err("%s: unmapped value: %lu\n", opts, value);
  985. return -EINVAL;
  986. }
  987. } else if (!memcmp(opts, "gid", 3)) {
  988. data->perms.gid = make_kgid(current_user_ns(), value);
  989. if (!gid_valid(data->perms.gid)) {
  990. pr_err("%s: unmapped value: %lu\n", opts, value);
  991. return -EINVAL;
  992. }
  993. } else {
  994. goto invalid;
  995. }
  996. break;
  997. default:
  998. invalid:
  999. pr_err("%s: invalid option\n", opts);
  1000. return -EINVAL;
  1001. }
  1002. /* Next iteration */
  1003. if (!comma)
  1004. break;
  1005. opts = comma + 1;
  1006. }
  1007. return 0;
  1008. }
  1009. /* "mount -t functionfs dev_name /dev/function" ends up here */
  1010. static struct dentry *
  1011. ffs_fs_mount(struct file_system_type *t, int flags,
  1012. const char *dev_name, void *opts)
  1013. {
  1014. struct ffs_sb_fill_data data = {
  1015. .perms = {
  1016. .mode = S_IFREG | 0600,
  1017. .uid = GLOBAL_ROOT_UID,
  1018. .gid = GLOBAL_ROOT_GID,
  1019. },
  1020. .root_mode = S_IFDIR | 0500,
  1021. };
  1022. struct dentry *rv;
  1023. int ret;
  1024. void *ffs_dev;
  1025. struct ffs_data *ffs;
  1026. ENTER();
  1027. ret = ffs_fs_parse_opts(&data, opts);
  1028. if (unlikely(ret < 0))
  1029. return ERR_PTR(ret);
  1030. ffs = ffs_data_new();
  1031. if (unlikely(!ffs))
  1032. return ERR_PTR(-ENOMEM);
  1033. ffs->file_perms = data.perms;
  1034. ffs->dev_name = kstrdup(dev_name, GFP_KERNEL);
  1035. if (unlikely(!ffs->dev_name)) {
  1036. ffs_data_put(ffs);
  1037. return ERR_PTR(-ENOMEM);
  1038. }
  1039. ffs_dev = functionfs_acquire_dev_callback(dev_name);
  1040. if (IS_ERR(ffs_dev)) {
  1041. ffs_data_put(ffs);
  1042. return ERR_CAST(ffs_dev);
  1043. }
  1044. ffs->private_data = ffs_dev;
  1045. data.ffs_data = ffs;
  1046. rv = mount_nodev(t, flags, &data, ffs_sb_fill);
  1047. if (IS_ERR(rv) && data.ffs_data) {
  1048. functionfs_release_dev_callback(data.ffs_data);
  1049. ffs_data_put(data.ffs_data);
  1050. }
  1051. return rv;
  1052. }
  1053. static void
  1054. ffs_fs_kill_sb(struct super_block *sb)
  1055. {
  1056. ENTER();
  1057. kill_litter_super(sb);
  1058. if (sb->s_fs_info) {
  1059. functionfs_release_dev_callback(sb->s_fs_info);
  1060. ffs_data_put(sb->s_fs_info);
  1061. }
  1062. }
  1063. static struct file_system_type ffs_fs_type = {
  1064. .owner = THIS_MODULE,
  1065. .name = "functionfs",
  1066. .mount = ffs_fs_mount,
  1067. .kill_sb = ffs_fs_kill_sb,
  1068. };
  1069. MODULE_ALIAS_FS("functionfs");
  1070. /* Driver's main init/cleanup functions *************************************/
  1071. static int functionfs_init(void)
  1072. {
  1073. int ret;
  1074. ENTER();
  1075. ret = register_filesystem(&ffs_fs_type);
  1076. if (likely(!ret))
  1077. pr_info("file system registered\n");
  1078. else
  1079. pr_err("failed registering file system (%d)\n", ret);
  1080. return ret;
  1081. }
  1082. static void functionfs_cleanup(void)
  1083. {
  1084. ENTER();
  1085. pr_info("unloading\n");
  1086. unregister_filesystem(&ffs_fs_type);
  1087. }
  1088. /* ffs_data and ffs_function construction and destruction code **************/
  1089. static void ffs_data_clear(struct ffs_data *ffs);
  1090. static void ffs_data_reset(struct ffs_data *ffs);
  1091. static void ffs_data_get(struct ffs_data *ffs)
  1092. {
  1093. ENTER();
  1094. atomic_inc(&ffs->ref);
  1095. }
  1096. static void ffs_data_opened(struct ffs_data *ffs)
  1097. {
  1098. ENTER();
  1099. atomic_inc(&ffs->ref);
  1100. atomic_inc(&ffs->opened);
  1101. }
  1102. static void ffs_data_put(struct ffs_data *ffs)
  1103. {
  1104. ENTER();
  1105. if (unlikely(atomic_dec_and_test(&ffs->ref))) {
  1106. pr_info("%s(): freeing\n", __func__);
  1107. ffs_data_clear(ffs);
  1108. BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
  1109. waitqueue_active(&ffs->ep0req_completion.wait));
  1110. kfree(ffs->dev_name);
  1111. kfree(ffs);
  1112. }
  1113. }
  1114. static void ffs_data_closed(struct ffs_data *ffs)
  1115. {
  1116. ENTER();
  1117. if (atomic_dec_and_test(&ffs->opened)) {
  1118. ffs->state = FFS_CLOSING;
  1119. ffs_data_reset(ffs);
  1120. }
  1121. ffs_data_put(ffs);
  1122. }
  1123. static struct ffs_data *ffs_data_new(void)
  1124. {
  1125. struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL);
  1126. if (unlikely(!ffs))
  1127. return 0;
  1128. ENTER();
  1129. atomic_set(&ffs->ref, 1);
  1130. atomic_set(&ffs->opened, 0);
  1131. ffs->state = FFS_READ_DESCRIPTORS;
  1132. mutex_init(&ffs->mutex);
  1133. spin_lock_init(&ffs->eps_lock);
  1134. init_waitqueue_head(&ffs->ev.waitq);
  1135. init_completion(&ffs->ep0req_completion);
  1136. init_completion(&ffs->epout_completion);
  1137. init_completion(&ffs->epin_completion);
  1138. /* XXX REVISIT need to update it in some places, or do we? */
  1139. ffs->ev.can_stall = 1;
  1140. return ffs;
  1141. }
  1142. static void ffs_data_clear(struct ffs_data *ffs)
  1143. {
  1144. ENTER();
  1145. if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags))
  1146. functionfs_closed_callback(ffs);
  1147. BUG_ON(ffs->gadget);
  1148. if (ffs->epfiles)
  1149. ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count);
  1150. kfree(ffs->raw_descs);
  1151. kfree(ffs->raw_strings);
  1152. kfree(ffs->stringtabs);
  1153. }
  1154. static void ffs_data_reset(struct ffs_data *ffs)
  1155. {
  1156. ENTER();
  1157. ffs_data_clear(ffs);
  1158. ffs->epfiles = NULL;
  1159. ffs->raw_descs = NULL;
  1160. ffs->raw_strings = NULL;
  1161. ffs->stringtabs = NULL;
  1162. ffs->raw_descs_length = 0;
  1163. ffs->raw_fs_hs_descs_length = 0;
  1164. ffs->raw_ss_descs_offset = 0;
  1165. ffs->raw_ss_descs_length = 0;
  1166. ffs->fs_descs_count = 0;
  1167. ffs->hs_descs_count = 0;
  1168. ffs->ss_descs_count = 0;
  1169. ffs->strings_count = 0;
  1170. ffs->interfaces_count = 0;
  1171. ffs->eps_count = 0;
  1172. ffs->ev.count = 0;
  1173. ffs->state = FFS_READ_DESCRIPTORS;
  1174. ffs->setup_state = FFS_NO_SETUP;
  1175. ffs->flags = 0;
  1176. }
  1177. static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
  1178. {
  1179. struct usb_gadget_strings **lang;
  1180. int first_id;
  1181. ENTER();
  1182. if (WARN_ON(ffs->state != FFS_ACTIVE
  1183. || test_and_set_bit(FFS_FL_BOUND, &ffs->flags)))
  1184. return -EBADFD;
  1185. first_id = usb_string_ids_n(cdev, ffs->strings_count);
  1186. if (unlikely(first_id < 0))
  1187. return first_id;
  1188. ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
  1189. if (unlikely(!ffs->ep0req))
  1190. return -ENOMEM;
  1191. ffs->ep0req->complete = ffs_ep0_complete;
  1192. ffs->ep0req->context = ffs;
  1193. lang = ffs->stringtabs;
  1194. if (lang) {
  1195. for (; *lang; ++lang) {
  1196. struct usb_string *str = (*lang)->strings;
  1197. int id = first_id;
  1198. for (; str->s; ++id, ++str)
  1199. str->id = id;
  1200. }
  1201. }
  1202. ffs->gadget = cdev->gadget;
  1203. ffs_data_get(ffs);
  1204. return 0;
  1205. }
  1206. static void functionfs_unbind(struct ffs_data *ffs)
  1207. {
  1208. ENTER();
  1209. if (!WARN_ON(!ffs->gadget)) {
  1210. usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req);
  1211. ffs->ep0req = NULL;
  1212. ffs->gadget = NULL;
  1213. ffs_data_put(ffs);
  1214. clear_bit(FFS_FL_BOUND, &ffs->flags);
  1215. }
  1216. }
  1217. static int ffs_epfiles_create(struct ffs_data *ffs)
  1218. {
  1219. struct ffs_epfile *epfile, *epfiles;
  1220. unsigned i, count;
  1221. ENTER();
  1222. count = ffs->eps_count;
  1223. epfiles = kcalloc(count, sizeof(*epfiles), GFP_KERNEL);
  1224. if (!epfiles)
  1225. return -ENOMEM;
  1226. epfile = epfiles;
  1227. for (i = 1; i <= count; ++i, ++epfile) {
  1228. epfile->ffs = ffs;
  1229. mutex_init(&epfile->mutex);
  1230. init_waitqueue_head(&epfile->wait);
  1231. sprintf(epfiles->name, "ep%u", i);
  1232. if (!unlikely(ffs_sb_create_file(ffs->sb, epfiles->name, epfile,
  1233. &ffs_epfile_operations,
  1234. &epfile->dentry))) {
  1235. ffs_epfiles_destroy(epfiles, i - 1);
  1236. return -ENOMEM;
  1237. }
  1238. }
  1239. ffs->epfiles = epfiles;
  1240. return 0;
  1241. }
  1242. static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
  1243. {
  1244. struct ffs_epfile *epfile = epfiles;
  1245. ENTER();
  1246. for (; count; --count, ++epfile) {
  1247. BUG_ON(mutex_is_locked(&epfile->mutex) ||
  1248. waitqueue_active(&epfile->wait));
  1249. if (epfile->dentry) {
  1250. d_delete(epfile->dentry);
  1251. dput(epfile->dentry);
  1252. epfile->dentry = NULL;
  1253. }
  1254. }
  1255. kfree(epfiles);
  1256. }
  1257. static int functionfs_bind_config(struct usb_composite_dev *cdev,
  1258. struct usb_configuration *c,
  1259. struct ffs_data *ffs)
  1260. {
  1261. struct ffs_function *func;
  1262. int ret;
  1263. ENTER();
  1264. func = kzalloc(sizeof *func, GFP_KERNEL);
  1265. if (unlikely(!func))
  1266. return -ENOMEM;
  1267. func->function.name = "Function FS Gadget";
  1268. func->function.strings = ffs->stringtabs;
  1269. func->function.bind = ffs_func_bind;
  1270. func->function.unbind = ffs_func_unbind;
  1271. func->function.set_alt = ffs_func_set_alt;
  1272. func->function.disable = ffs_func_disable;
  1273. func->function.setup = ffs_func_setup;
  1274. func->function.suspend = ffs_func_suspend;
  1275. func->function.resume = ffs_func_resume;
  1276. func->conf = c;
  1277. func->gadget = cdev->gadget;
  1278. func->ffs = ffs;
  1279. ffs_data_get(ffs);
  1280. ret = usb_add_function(c, &func->function);
  1281. if (unlikely(ret))
  1282. ffs_func_free(func);
  1283. return ret;
  1284. }
  1285. static void ffs_func_free(struct ffs_function *func)
  1286. {
  1287. struct ffs_ep *ep = func->eps;
  1288. unsigned count = func->ffs->eps_count;
  1289. unsigned long flags;
  1290. ENTER();
  1291. /* cleanup after autoconfig */
  1292. spin_lock_irqsave(&func->ffs->eps_lock, flags);
  1293. do {
  1294. if (ep->ep && ep->req)
  1295. usb_ep_free_request(ep->ep, ep->req);
  1296. ep->req = NULL;
  1297. ep->ep = NULL;
  1298. ++ep;
  1299. } while (--count);
  1300. spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
  1301. ffs_data_put(func->ffs);
  1302. kfree(func->eps);
  1303. /*
  1304. * eps and interfaces_nums are allocated in the same chunk so
  1305. * only one free is required. Descriptors are also allocated
  1306. * in the same chunk.
  1307. */
  1308. kfree(func);
  1309. }
  1310. static void ffs_func_eps_disable(struct ffs_function *func)
  1311. {
  1312. struct ffs_ep *ep = func->eps;
  1313. struct ffs_epfile *epfile = func->ffs->epfiles;
  1314. unsigned count = func->ffs->eps_count;
  1315. unsigned long flags;
  1316. spin_lock_irqsave(&func->ffs->eps_lock, flags);
  1317. do {
  1318. atomic_set(&epfile->error, 1);
  1319. /* pending requests get nuked */
  1320. if (likely(ep->ep)) {
  1321. usb_ep_disable(ep->ep);
  1322. ep->ep->driver_data = NULL;
  1323. }
  1324. epfile->ep = NULL;
  1325. ++ep;
  1326. ++epfile;
  1327. } while (--count);
  1328. spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
  1329. }
  1330. static int ffs_func_eps_enable(struct ffs_function *func)
  1331. {
  1332. struct ffs_data *ffs = func->ffs;
  1333. struct ffs_ep *ep = func->eps;
  1334. struct ffs_epfile *epfile = ffs->epfiles;
  1335. unsigned count = ffs->eps_count;
  1336. unsigned long flags;
  1337. int ret = 0;
  1338. spin_lock_irqsave(&func->ffs->eps_lock, flags);
  1339. do {
  1340. struct usb_endpoint_descriptor *ds;
  1341. int desc_idx;
  1342. if (ffs->gadget->speed == USB_SPEED_SUPER)
  1343. desc_idx = 2;
  1344. else if (ffs->gadget->speed == USB_SPEED_HIGH)
  1345. desc_idx = 1;
  1346. else
  1347. desc_idx = 0;
  1348. ds = ep->descs[desc_idx];
  1349. if (!ds) {
  1350. ret = -EINVAL;
  1351. break;
  1352. }
  1353. ep->ep->driver_data = ep;
  1354. ep->ep->desc = ds;
  1355. ret = usb_ep_enable(ep->ep);
  1356. if (likely(!ret)) {
  1357. epfile->ep = ep;
  1358. epfile->in = usb_endpoint_dir_in(ds);
  1359. epfile->isoc = usb_endpoint_xfer_isoc(ds);
  1360. } else {
  1361. break;
  1362. }
  1363. wake_up(&epfile->wait);
  1364. ++ep;
  1365. ++epfile;
  1366. } while (--count);
  1367. spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
  1368. return ret;
  1369. }
  1370. /* Parsing and building descriptors and strings *****************************/
  1371. /*
  1372. * This validates if data pointed by data is a valid USB descriptor as
  1373. * well as record how many interfaces, endpoints and strings are
  1374. * required by given configuration. Returns address after the
  1375. * descriptor or NULL if data is invalid.
  1376. */
  1377. enum ffs_entity_type {
  1378. FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT
  1379. };
  1380. typedef int (*ffs_entity_callback)(enum ffs_entity_type entity,
  1381. u8 *valuep,
  1382. struct usb_descriptor_header *desc,
  1383. void *priv);
  1384. static int __must_check ffs_do_desc(char *data, unsigned len,
  1385. ffs_entity_callback entity, void *priv)
  1386. {
  1387. struct usb_descriptor_header *_ds = (void *)data;
  1388. u8 length;
  1389. int ret;
  1390. ENTER();
  1391. /* At least two bytes are required: length and type */
  1392. if (len < 2) {
  1393. pr_vdebug("descriptor too short\n");
  1394. return -EINVAL;
  1395. }
  1396. /* If we have at least as many bytes as the descriptor takes? */
  1397. length = _ds->bLength;
  1398. if (len < length) {
  1399. pr_vdebug("descriptor longer then available data\n");
  1400. return -EINVAL;
  1401. }
  1402. #define __entity_check_INTERFACE(val) 1
  1403. #define __entity_check_STRING(val) (val)
  1404. #define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
  1405. #define __entity(type, val) do { \
  1406. pr_vdebug("entity " #type "(%02x)\n", (val)); \
  1407. if (unlikely(!__entity_check_ ##type(val))) { \
  1408. pr_vdebug("invalid entity's value\n"); \
  1409. return -EINVAL; \
  1410. } \
  1411. ret = entity(FFS_ ##type, &val, _ds, priv); \
  1412. if (unlikely(ret < 0)) { \
  1413. pr_debug("entity " #type "(%02x); ret = %d\n", \
  1414. (val), ret); \
  1415. return ret; \
  1416. } \
  1417. } while (0)
  1418. /* Parse descriptor depending on type. */
  1419. switch (_ds->bDescriptorType) {
  1420. case USB_DT_DEVICE:
  1421. case USB_DT_CONFIG:
  1422. case USB_DT_STRING:
  1423. case USB_DT_DEVICE_QUALIFIER:
  1424. /* function can't have any of those */
  1425. pr_vdebug("descriptor reserved for gadget: %d\n",
  1426. _ds->bDescriptorType);
  1427. return -EINVAL;
  1428. case USB_DT_INTERFACE: {
  1429. struct usb_interface_descriptor *ds = (void *)_ds;
  1430. pr_vdebug("interface descriptor\n");
  1431. if (length != sizeof *ds)
  1432. goto inv_length;
  1433. __entity(INTERFACE, ds->bInterfaceNumber);
  1434. if (ds->iInterface)
  1435. __entity(STRING, ds->iInterface);
  1436. }
  1437. break;
  1438. case USB_DT_ENDPOINT: {
  1439. struct usb_endpoint_descriptor *ds = (void *)_ds;
  1440. pr_vdebug("endpoint descriptor\n");
  1441. if (length != USB_DT_ENDPOINT_SIZE &&
  1442. length != USB_DT_ENDPOINT_AUDIO_SIZE)
  1443. goto inv_length;
  1444. __entity(ENDPOINT, ds->bEndpointAddress);
  1445. }
  1446. break;
  1447. case HID_DT_HID:
  1448. pr_vdebug("hid descriptor\n");
  1449. if (length != sizeof(struct hid_descriptor))
  1450. goto inv_length;
  1451. break;
  1452. case USB_DT_OTG:
  1453. if (length != sizeof(struct usb_otg_descriptor))
  1454. goto inv_length;
  1455. break;
  1456. case USB_DT_INTERFACE_ASSOCIATION: {
  1457. struct usb_interface_assoc_descriptor *ds = (void *)_ds;
  1458. pr_vdebug("interface association descriptor\n");
  1459. if (length != sizeof *ds)
  1460. goto inv_length;
  1461. if (ds->iFunction)
  1462. __entity(STRING, ds->iFunction);
  1463. }
  1464. break;
  1465. case USB_DT_SS_ENDPOINT_COMP:
  1466. pr_vdebug("EP SS companion descriptor\n");
  1467. if (length != sizeof(struct usb_ss_ep_comp_descriptor))
  1468. goto inv_length;
  1469. break;
  1470. case USB_DT_OTHER_SPEED_CONFIG:
  1471. case USB_DT_INTERFACE_POWER:
  1472. case USB_DT_DEBUG:
  1473. case USB_DT_SECURITY:
  1474. case USB_DT_CS_RADIO_CONTROL:
  1475. /* TODO */
  1476. pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType);
  1477. return -EINVAL;
  1478. default:
  1479. /* We should never be here */
  1480. pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType);
  1481. return -EINVAL;
  1482. inv_length:
  1483. pr_vdebug("invalid length: %d (descriptor %d)\n",
  1484. _ds->bLength, _ds->bDescriptorType);
  1485. return -EINVAL;
  1486. }
  1487. #undef __entity
  1488. #undef __entity_check_DESCRIPTOR
  1489. #undef __entity_check_INTERFACE
  1490. #undef __entity_check_STRING
  1491. #undef __entity_check_ENDPOINT
  1492. return length;
  1493. }
  1494. static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
  1495. ffs_entity_callback entity, void *priv)
  1496. {
  1497. const unsigned _len = len;
  1498. unsigned long num = 0;
  1499. ENTER();
  1500. for (;;) {
  1501. int ret;
  1502. if (num == count)
  1503. data = NULL;
  1504. /* Record "descriptor" entity */
  1505. ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv);
  1506. if (unlikely(ret < 0)) {
  1507. pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n",
  1508. num, ret);
  1509. return ret;
  1510. }
  1511. if (!data)
  1512. return _len - len;
  1513. ret = ffs_do_desc(data, len, entity, priv);
  1514. if (unlikely(ret < 0)) {
  1515. pr_debug("%s returns %d\n", __func__, ret);
  1516. return ret;
  1517. }
  1518. len -= ret;
  1519. data += ret;
  1520. ++num;
  1521. }
  1522. }
  1523. static int __ffs_data_do_entity(enum ffs_entity_type type,
  1524. u8 *valuep, struct usb_descriptor_header *desc,
  1525. void *priv)
  1526. {
  1527. struct ffs_data *ffs = priv;
  1528. ENTER();
  1529. switch (type) {
  1530. case FFS_DESCRIPTOR:
  1531. break;
  1532. case FFS_INTERFACE:
  1533. /*
  1534. * Interfaces are indexed from zero so if we
  1535. * encountered interface "n" then there are at least
  1536. * "n+1" interfaces.
  1537. */
  1538. if (*valuep >= ffs->interfaces_count)
  1539. ffs->interfaces_count = *valuep + 1;
  1540. break;
  1541. case FFS_STRING:
  1542. /*
  1543. * Strings are indexed from 1 (0 is magic ;) reserved
  1544. * for languages list or some such)
  1545. */
  1546. if (*valuep > ffs->strings_count)
  1547. ffs->strings_count = *valuep;
  1548. break;
  1549. case FFS_ENDPOINT:
  1550. /* Endpoints are indexed from 1 as well. */
  1551. if ((*valuep & USB_ENDPOINT_NUMBER_MASK) > ffs->eps_count)
  1552. ffs->eps_count = (*valuep & USB_ENDPOINT_NUMBER_MASK);
  1553. break;
  1554. }
  1555. return 0;
  1556. }
  1557. static int __ffs_data_got_descs(struct ffs_data *ffs,
  1558. char *const _data, size_t len)
  1559. {
  1560. unsigned fs_count, hs_count, ss_count = 0;
  1561. int fs_len, hs_len, ss_len, ss_magic, ret = -EINVAL;
  1562. char *data = _data;
  1563. ENTER();
  1564. if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_DESCRIPTORS_MAGIC ||
  1565. get_unaligned_le32(data + 4) != len))
  1566. goto error;
  1567. fs_count = get_unaligned_le32(data + 8);
  1568. hs_count = get_unaligned_le32(data + 12);
  1569. data += 16;
  1570. len -= 16;
  1571. if (likely(fs_count)) {
  1572. fs_len = ffs_do_descs(fs_count, data, len,
  1573. __ffs_data_do_entity, ffs);
  1574. if (unlikely(fs_len < 0)) {
  1575. ret = fs_len;
  1576. goto error;
  1577. }
  1578. data += fs_len;
  1579. len -= fs_len;
  1580. } else {
  1581. fs_len = 0;
  1582. }
  1583. if (likely(hs_count)) {
  1584. hs_len = ffs_do_descs(hs_count, data, len,
  1585. __ffs_data_do_entity, ffs);
  1586. if (unlikely(hs_len < 0)) {
  1587. ret = hs_len;
  1588. goto error;
  1589. }
  1590. } else {
  1591. hs_len = 0;
  1592. }
  1593. if ((len >= hs_len + 8)) {
  1594. /* Check SS_MAGIC for presence of ss_descs and get SS_COUNT */
  1595. ss_magic = get_unaligned_le32(data + hs_len);
  1596. if (ss_magic != FUNCTIONFS_SS_DESC_MAGIC)
  1597. goto einval;
  1598. ss_count = get_unaligned_le32(data + hs_len + 4);
  1599. data += hs_len + 8;
  1600. len -= hs_len + 8;
  1601. } else {
  1602. data += hs_len;
  1603. len -= hs_len;
  1604. }
  1605. if (!fs_count && !hs_count && !ss_count)
  1606. goto einval;
  1607. if (ss_count) {
  1608. ss_len = ffs_do_descs(ss_count, data, len,
  1609. __ffs_data_do_entity, ffs);
  1610. if (unlikely(ss_len < 0)) {
  1611. ret = ss_len;
  1612. goto error;
  1613. }
  1614. ret = ss_len;
  1615. } else {
  1616. ss_len = 0;
  1617. ret = 0;
  1618. }
  1619. if (unlikely(len != ret))
  1620. goto einval;
  1621. ffs->raw_fs_hs_descs_length = fs_len + hs_len;
  1622. ffs->raw_ss_descs_length = ss_len;
  1623. ffs->raw_descs_length = ffs->raw_fs_hs_descs_length + ss_len;
  1624. ffs->raw_descs = _data;
  1625. ffs->fs_descs_count = fs_count;
  1626. ffs->hs_descs_count = hs_count;
  1627. ffs->ss_descs_count = ss_count;
  1628. if (ffs->ss_descs_count)
  1629. ffs->raw_ss_descs_offset = 16 + ffs->raw_fs_hs_descs_length + 8;
  1630. return 0;
  1631. einval:
  1632. ret = -EINVAL;
  1633. error:
  1634. kfree(_data);
  1635. return ret;
  1636. }
  1637. static int __ffs_data_got_strings(struct ffs_data *ffs,
  1638. char *const _data, size_t len)
  1639. {
  1640. u32 str_count, needed_count, lang_count;
  1641. struct usb_gadget_strings **stringtabs, *t;
  1642. struct usb_string *strings, *s;
  1643. const char *data = _data;
  1644. ENTER();
  1645. if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC ||
  1646. get_unaligned_le32(data + 4) != len))
  1647. goto error;
  1648. str_count = get_unaligned_le32(data + 8);
  1649. lang_count = get_unaligned_le32(data + 12);
  1650. /* if one is zero the other must be zero */
  1651. if (unlikely(!str_count != !lang_count))
  1652. goto error;
  1653. /* Do we have at least as many strings as descriptors need? */
  1654. needed_count = ffs->strings_count;
  1655. if (unlikely(str_count < needed_count))
  1656. goto error;
  1657. /*
  1658. * If we don't need any strings just return and free all
  1659. * memory.
  1660. */
  1661. if (!needed_count) {
  1662. kfree(_data);
  1663. return 0;
  1664. }
  1665. /* Allocate everything in one chunk so there's less maintenance. */
  1666. {
  1667. struct {
  1668. struct usb_gadget_strings *stringtabs[lang_count + 1];
  1669. struct usb_gadget_strings stringtab[lang_count];
  1670. struct usb_string strings[lang_count*(needed_count+1)];
  1671. } *d;
  1672. unsigned i = 0;
  1673. d = kmalloc(sizeof *d, GFP_KERNEL);
  1674. if (unlikely(!d)) {
  1675. kfree(_data);
  1676. return -ENOMEM;
  1677. }
  1678. stringtabs = d->stringtabs;
  1679. t = d->stringtab;
  1680. i = lang_count;
  1681. do {
  1682. *stringtabs++ = t++;
  1683. } while (--i);
  1684. *stringtabs = NULL;
  1685. stringtabs = d->stringtabs;
  1686. t = d->stringtab;
  1687. s = d->strings;
  1688. strings = s;
  1689. }
  1690. /* For each language */
  1691. data += 16;
  1692. len -= 16;
  1693. do { /* lang_count > 0 so we can use do-while */
  1694. unsigned needed = needed_count;
  1695. if (unlikely(len < 3))
  1696. goto error_free;
  1697. t->language = get_unaligned_le16(data);
  1698. t->strings = s;
  1699. ++t;
  1700. data += 2;
  1701. len -= 2;
  1702. /* For each string */
  1703. do { /* str_count > 0 so we can use do-while */
  1704. size_t length = strnlen(data, len);
  1705. if (unlikely(length == len))
  1706. goto error_free;
  1707. /*
  1708. * User may provide more strings then we need,
  1709. * if that's the case we simply ignore the
  1710. * rest
  1711. */
  1712. if (likely(needed)) {
  1713. /*
  1714. * s->id will be set while adding
  1715. * function to configuration so for
  1716. * now just leave garbage here.
  1717. */
  1718. s->s = data;
  1719. --needed;
  1720. ++s;
  1721. }
  1722. data += length + 1;
  1723. len -= length + 1;
  1724. } while (--str_count);
  1725. s->id = 0; /* terminator */
  1726. s->s = NULL;
  1727. ++s;
  1728. } while (--lang_count);
  1729. /* Some garbage left? */
  1730. if (unlikely(len))
  1731. goto error_free;
  1732. /* Done! */
  1733. ffs->stringtabs = stringtabs;
  1734. ffs->raw_strings = _data;
  1735. return 0;
  1736. error_free:
  1737. kfree(stringtabs);
  1738. error:
  1739. kfree(_data);
  1740. return -EINVAL;
  1741. }
  1742. /* Events handling and management *******************************************/
  1743. static void __ffs_event_add(struct ffs_data *ffs,
  1744. enum usb_functionfs_event_type type)
  1745. {
  1746. enum usb_functionfs_event_type rem_type1, rem_type2 = type;
  1747. int neg = 0;
  1748. /*
  1749. * Abort any unhandled setup
  1750. *
  1751. * We do not need to worry about some cmpxchg() changing value
  1752. * of ffs->setup_state without holding the lock because when
  1753. * state is FFS_SETUP_PENDING cmpxchg() in several places in
  1754. * the source does nothing.
  1755. */
  1756. if (ffs->setup_state == FFS_SETUP_PENDING)
  1757. ffs->setup_state = FFS_SETUP_CANCELED;
  1758. switch (type) {
  1759. case FUNCTIONFS_RESUME:
  1760. rem_type2 = FUNCTIONFS_SUSPEND;
  1761. /* FALL THROUGH */
  1762. case FUNCTIONFS_SUSPEND:
  1763. case FUNCTIONFS_SETUP:
  1764. rem_type1 = type;
  1765. /* Discard all similar events */
  1766. break;
  1767. case FUNCTIONFS_BIND:
  1768. case FUNCTIONFS_UNBIND:
  1769. case FUNCTIONFS_DISABLE:
  1770. case FUNCTIONFS_ENABLE:
  1771. /* Discard everything other then power management. */
  1772. rem_type1 = FUNCTIONFS_SUSPEND;
  1773. rem_type2 = FUNCTIONFS_RESUME;
  1774. neg = 1;
  1775. break;
  1776. default:
  1777. BUG();
  1778. }
  1779. {
  1780. u8 *ev = ffs->ev.types, *out = ev;
  1781. unsigned n = ffs->ev.count;
  1782. for (; n; --n, ++ev)
  1783. if ((*ev == rem_type1 || *ev == rem_type2) == neg)
  1784. *out++ = *ev;
  1785. else
  1786. pr_vdebug("purging event %d\n", *ev);
  1787. ffs->ev.count = out - ffs->ev.types;
  1788. }
  1789. pr_vdebug("adding event %d\n", type);
  1790. ffs->ev.types[ffs->ev.count++] = type;
  1791. wake_up_locked(&ffs->ev.waitq);
  1792. }
  1793. static void ffs_event_add(struct ffs_data *ffs,
  1794. enum usb_functionfs_event_type type)
  1795. {
  1796. unsigned long flags;
  1797. spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
  1798. __ffs_event_add(ffs, type);
  1799. spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
  1800. }
  1801. /* Bind/unbind USB function hooks *******************************************/
  1802. static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
  1803. struct usb_descriptor_header *desc,
  1804. void *priv)
  1805. {
  1806. struct usb_endpoint_descriptor *ds = (void *)desc;
  1807. struct ffs_function *func = priv;
  1808. struct ffs_ep *ffs_ep;
  1809. /*
  1810. * If hs_descriptors is not NULL then we are reading hs
  1811. * descriptors now
  1812. */
  1813. const int is_hs = func->function.hs_descriptors != NULL;
  1814. const int is_ss = func->function.ss_descriptors != NULL;
  1815. unsigned ep_desc_id, idx;
  1816. if (type != FFS_DESCRIPTOR)
  1817. return 0;
  1818. if (is_ss) {
  1819. func->function.ss_descriptors[(long)valuep] = desc;
  1820. ep_desc_id = 2;
  1821. } else if (is_hs) {
  1822. func->function.hs_descriptors[(long)valuep] = desc;
  1823. ep_desc_id = 1;
  1824. } else {
  1825. func->function.fs_descriptors[(long)valuep] = desc;
  1826. ep_desc_id = 0;
  1827. }
  1828. if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
  1829. return 0;
  1830. idx = (ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) - 1;
  1831. ffs_ep = func->eps + idx;
  1832. if (unlikely(ffs_ep->descs[ep_desc_id])) {
  1833. pr_vdebug("two %sspeed descriptors for EP %d\n",
  1834. is_ss ? "super" : "high/full",
  1835. ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
  1836. return -EINVAL;
  1837. }
  1838. ffs_ep->descs[ep_desc_id] = ds;
  1839. ffs_dump_mem(": Original ep desc", ds, ds->bLength);
  1840. if (ffs_ep->ep) {
  1841. ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress;
  1842. if (!ds->wMaxPacketSize)
  1843. ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize;
  1844. } else {
  1845. struct usb_request *req;
  1846. struct usb_ep *ep;
  1847. pr_vdebug("autoconfig\n");
  1848. ep = usb_ep_autoconfig(func->gadget, ds);
  1849. if (unlikely(!ep))
  1850. return -ENOTSUPP;
  1851. ep->driver_data = func->eps + idx;
  1852. req = usb_ep_alloc_request(ep, GFP_KERNEL);
  1853. if (unlikely(!req))
  1854. return -ENOMEM;
  1855. ffs_ep->ep = ep;
  1856. ffs_ep->req = req;
  1857. func->eps_revmap[ds->bEndpointAddress &
  1858. USB_ENDPOINT_NUMBER_MASK] = idx + 1;
  1859. }
  1860. ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength);
  1861. return 0;
  1862. }
  1863. static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
  1864. struct usb_descriptor_header *desc,
  1865. void *priv)
  1866. {
  1867. struct ffs_function *func = priv;
  1868. unsigned idx;
  1869. u8 newValue;
  1870. switch (type) {
  1871. default:
  1872. case FFS_DESCRIPTOR:
  1873. /* Handled in previous pass by __ffs_func_bind_do_descs() */
  1874. return 0;
  1875. case FFS_INTERFACE:
  1876. idx = *valuep;
  1877. if (func->interfaces_nums[idx] < 0) {
  1878. int id = usb_interface_id(func->conf, &func->function);
  1879. if (unlikely(id < 0))
  1880. return id;
  1881. func->interfaces_nums[idx] = id;
  1882. }
  1883. newValue = func->interfaces_nums[idx];
  1884. break;
  1885. case FFS_STRING:
  1886. /* String' IDs are allocated when fsf_data is bound to cdev */
  1887. newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id;
  1888. break;
  1889. case FFS_ENDPOINT:
  1890. /*
  1891. * USB_DT_ENDPOINT are handled in
  1892. * __ffs_func_bind_do_descs().
  1893. */
  1894. if (desc->bDescriptorType == USB_DT_ENDPOINT)
  1895. return 0;
  1896. idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1;
  1897. if (unlikely(!func->eps[idx].ep))
  1898. return -EINVAL;
  1899. {
  1900. struct usb_endpoint_descriptor **descs;
  1901. descs = func->eps[idx].descs;
  1902. newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress;
  1903. }
  1904. break;
  1905. }
  1906. pr_vdebug("%02x -> %02x\n", *valuep, newValue);
  1907. *valuep = newValue;
  1908. return 0;
  1909. }
  1910. static int ffs_func_bind(struct usb_configuration *c,
  1911. struct usb_function *f)
  1912. {
  1913. struct ffs_function *func = ffs_func_from_usb(f);
  1914. struct ffs_data *ffs = func->ffs;
  1915. const int full = !!func->ffs->fs_descs_count;
  1916. const int high = gadget_is_dualspeed(func->gadget) &&
  1917. func->ffs->hs_descs_count;
  1918. const int super = gadget_is_superspeed(func->gadget) &&
  1919. func->ffs->ss_descs_count;
  1920. int fs_len, hs_len, ret;
  1921. /* Make it a single chunk, less management later on */
  1922. struct {
  1923. struct ffs_ep eps[ffs->eps_count];
  1924. struct usb_descriptor_header
  1925. *fs_descs[full ? ffs->fs_descs_count + 1 : 0];
  1926. struct usb_descriptor_header
  1927. *hs_descs[high ? ffs->hs_descs_count + 1 : 0];
  1928. struct usb_descriptor_header
  1929. *ss_descs[super ? ffs->ss_descs_count + 1 : 0];
  1930. short inums[ffs->interfaces_count];
  1931. char raw_descs[ffs->raw_descs_length];
  1932. } *data;
  1933. ENTER();
  1934. /* Only high/super speed but not supported by gadget? */
  1935. if (unlikely(!(full | high | super)))
  1936. return -ENOTSUPP;
  1937. /* Allocate */
  1938. data = kmalloc(sizeof *data, GFP_KERNEL);
  1939. if (unlikely(!data))
  1940. return -ENOMEM;
  1941. /* Zero */
  1942. memset(data->eps, 0, sizeof data->eps);
  1943. /* Copy only raw (hs,fs) descriptors (until ss_magic and ss_count) */
  1944. memcpy(data->raw_descs, ffs->raw_descs + 16,
  1945. ffs->raw_fs_hs_descs_length);
  1946. /* Copy SS descriptors */
  1947. if (func->ffs->ss_descs_count)
  1948. memcpy(data->raw_descs + ffs->raw_fs_hs_descs_length,
  1949. ffs->raw_descs + ffs->raw_ss_descs_offset,
  1950. ffs->raw_ss_descs_length);
  1951. memset(data->inums, 0xff, sizeof data->inums);
  1952. for (ret = ffs->eps_count; ret; --ret)
  1953. data->eps[ret].num = -1;
  1954. /* Save pointers */
  1955. func->eps = data->eps;
  1956. func->interfaces_nums = data->inums;
  1957. /*
  1958. * Go through all the endpoint descriptors and allocate
  1959. * endpoints first, so that later we can rewrite the endpoint
  1960. * numbers without worrying that it may be described later on.
  1961. */
  1962. if (likely(full)) {
  1963. func->function.fs_descriptors = data->fs_descs;
  1964. fs_len = ffs_do_descs(ffs->fs_descs_count,
  1965. data->raw_descs,
  1966. sizeof(data->raw_descs),
  1967. __ffs_func_bind_do_descs, func);
  1968. if (unlikely(fs_len < 0)) {
  1969. ret = fs_len;
  1970. goto error;
  1971. }
  1972. } else {
  1973. fs_len = 0;
  1974. }
  1975. if (likely(high)) {
  1976. func->function.hs_descriptors = data->hs_descs;
  1977. hs_len = ffs_do_descs(ffs->hs_descs_count,
  1978. data->raw_descs + fs_len,
  1979. (sizeof(data->raw_descs)) - fs_len,
  1980. __ffs_func_bind_do_descs, func);
  1981. if (unlikely(hs_len < 0)) {
  1982. ret = hs_len;
  1983. goto error;
  1984. }
  1985. } else {
  1986. hs_len = 0;
  1987. }
  1988. if (likely(super)) {
  1989. func->function.ss_descriptors = data->ss_descs;
  1990. ret = ffs_do_descs(ffs->ss_descs_count,
  1991. data->raw_descs + fs_len + hs_len,
  1992. (sizeof(data->raw_descs)) - fs_len - hs_len,
  1993. __ffs_func_bind_do_descs, func);
  1994. if (unlikely(ret < 0))
  1995. goto error;
  1996. }
  1997. /*
  1998. * Now handle interface numbers allocation and interface and
  1999. * endpoint numbers rewriting. We can do that in one go
  2000. * now.
  2001. */
  2002. ret = ffs_do_descs(ffs->fs_descs_count +
  2003. (high ? ffs->hs_descs_count : 0) +
  2004. (super ? ffs->ss_descs_count : 0),
  2005. data->raw_descs, sizeof(data->raw_descs),
  2006. __ffs_func_bind_do_nums, func);
  2007. if (unlikely(ret < 0))
  2008. goto error;
  2009. /* And we're done */
  2010. ffs_event_add(ffs, FUNCTIONFS_BIND);
  2011. return 0;
  2012. error:
  2013. /* XXX Do we need to release all claimed endpoints here? */
  2014. return ret;
  2015. }
  2016. /* Other USB function hooks *************************************************/
  2017. static void ffs_func_unbind(struct usb_configuration *c,
  2018. struct usb_function *f)
  2019. {
  2020. struct ffs_function *func = ffs_func_from_usb(f);
  2021. struct ffs_data *ffs = func->ffs;
  2022. ENTER();
  2023. if (ffs->func == func) {
  2024. ffs_func_eps_disable(func);
  2025. ffs->func = NULL;
  2026. }
  2027. ffs_event_add(ffs, FUNCTIONFS_UNBIND);
  2028. ffs_func_free(func);
  2029. }
  2030. static int ffs_func_set_alt(struct usb_function *f,
  2031. unsigned interface, unsigned alt)
  2032. {
  2033. struct ffs_function *func = ffs_func_from_usb(f);
  2034. struct ffs_data *ffs = func->ffs;
  2035. int ret = 0, intf;
  2036. if (alt != (unsigned)-1) {
  2037. intf = ffs_func_revmap_intf(func, interface);
  2038. if (unlikely(intf < 0))
  2039. return intf;
  2040. }
  2041. if (ffs->func) {
  2042. ffs_func_eps_disable(ffs->func);
  2043. ffs->func = NULL;
  2044. }
  2045. if (ffs->state != FFS_ACTIVE)
  2046. return -ENODEV;
  2047. if (alt == (unsigned)-1) {
  2048. ffs->func = NULL;
  2049. ffs_event_add(ffs, FUNCTIONFS_DISABLE);
  2050. return 0;
  2051. }
  2052. ffs->func = func;
  2053. ret = ffs_func_eps_enable(func);
  2054. if (likely(ret >= 0))
  2055. ffs_event_add(ffs, FUNCTIONFS_ENABLE);
  2056. return ret;
  2057. }
  2058. static void ffs_func_disable(struct usb_function *f)
  2059. {
  2060. ffs_func_set_alt(f, 0, (unsigned)-1);
  2061. }
  2062. static int ffs_func_setup(struct usb_function *f,
  2063. const struct usb_ctrlrequest *creq)
  2064. {
  2065. struct ffs_function *func = ffs_func_from_usb(f);
  2066. struct ffs_data *ffs = func->ffs;
  2067. unsigned long flags;
  2068. int ret;
  2069. ENTER();
  2070. pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType);
  2071. pr_vdebug("creq->bRequest = %02x\n", creq->bRequest);
  2072. pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq->wValue));
  2073. pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq->wIndex));
  2074. pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq->wLength));
  2075. /*
  2076. * Most requests directed to interface go through here
  2077. * (notable exceptions are set/get interface) so we need to
  2078. * handle them. All other either handled by composite or
  2079. * passed to usb_configuration->setup() (if one is set). No
  2080. * matter, we will handle requests directed to endpoint here
  2081. * as well (as it's straightforward) but what to do with any
  2082. * other request?
  2083. */
  2084. if (ffs->state != FFS_ACTIVE)
  2085. return -ENODEV;
  2086. switch (creq->bRequestType & USB_RECIP_MASK) {
  2087. case USB_RECIP_INTERFACE:
  2088. ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex));
  2089. if (unlikely(ret < 0))
  2090. return ret;
  2091. break;
  2092. case USB_RECIP_ENDPOINT:
  2093. ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex));
  2094. if (unlikely(ret < 0))
  2095. return ret;
  2096. break;
  2097. default:
  2098. return -EOPNOTSUPP;
  2099. }
  2100. spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
  2101. ffs->ev.setup = *creq;
  2102. ffs->ev.setup.wIndex = cpu_to_le16(ret);
  2103. __ffs_event_add(ffs, FUNCTIONFS_SETUP);
  2104. spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
  2105. return 0;
  2106. }
  2107. static void ffs_func_suspend(struct usb_function *f)
  2108. {
  2109. ENTER();
  2110. ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND);
  2111. }
  2112. static void ffs_func_resume(struct usb_function *f)
  2113. {
  2114. ENTER();
  2115. ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME);
  2116. }
  2117. /* Endpoint and interface numbers reverse mapping ***************************/
  2118. static int ffs_func_revmap_ep(struct ffs_function *func, u8 num)
  2119. {
  2120. num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK];
  2121. return num ? num : -EDOM;
  2122. }
  2123. static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf)
  2124. {
  2125. short *nums = func->interfaces_nums;
  2126. unsigned count = func->ffs->interfaces_count;
  2127. for (; count; --count, ++nums) {
  2128. if (*nums >= 0 && *nums == intf)
  2129. return nums - func->interfaces_nums;
  2130. }
  2131. return -EDOM;
  2132. }
  2133. /* Misc helper functions ****************************************************/
  2134. static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
  2135. {
  2136. return nonblock
  2137. ? likely(mutex_trylock(mutex)) ? 0 : -EAGAIN
  2138. : mutex_lock_interruptible(mutex);
  2139. }
  2140. static char *ffs_prepare_buffer(const char __user *buf, size_t len)
  2141. {
  2142. char *data;
  2143. if (unlikely(!len))
  2144. return NULL;
  2145. data = kmalloc(len, GFP_KERNEL);
  2146. if (unlikely(!data))
  2147. return ERR_PTR(-ENOMEM);
  2148. if (unlikely(__copy_from_user(data, buf, len))) {
  2149. kfree(data);
  2150. return ERR_PTR(-EFAULT);
  2151. }
  2152. pr_vdebug("Buffer from user space:\n");
  2153. ffs_dump_mem("", data, len);
  2154. return data;
  2155. }