f_fs.c 54 KB

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