f_fs.c 54 KB

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