f_fs.c 54 KB

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