f_fs.c 54 KB

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