f_fs.c 54 KB

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