f_fs.c 54 KB

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