f_fs.c 55 KB

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