f_fs.c 54 KB

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