f_fs.c 54 KB

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