f_fs.c 55 KB

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