f_fs.c 55 KB

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