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/usb/composite.h>
  31. #include <linux/usb/functionfs.h>
  32. #define FUNCTIONFS_MAGIC 0xa647361 /* Chosen by a honest dice roll ;) */
  33. /* Debuging *****************************************************************/
  34. #define ffs_printk(level, fmt, args...) printk(level "f_fs: " fmt "\n", ## args)
  35. #define FERR(...) ffs_printk(KERN_ERR, __VA_ARGS__)
  36. #define FINFO(...) ffs_printk(KERN_INFO, __VA_ARGS__)
  37. #ifdef DEBUG
  38. # define FDBG(...) ffs_printk(KERN_DEBUG, __VA_ARGS__)
  39. #else
  40. # define FDBG(...) do { } while (0)
  41. #endif /* DEBUG */
  42. #ifdef VERBOSE_DEBUG
  43. # define FVDBG FDBG
  44. #else
  45. # define FVDBG(...) do { } while (0)
  46. #endif /* VERBOSE_DEBUG */
  47. #define ENTER() FVDBG("%s()", __func__)
  48. #ifdef VERBOSE_DEBUG
  49. # define ffs_dump_mem(prefix, ptr, len) \
  50. print_hex_dump_bytes("f_fs" prefix ": ", DUMP_PREFIX_NONE, ptr, len)
  51. #else
  52. # define ffs_dump_mem(prefix, ptr, len) do { } while (0)
  53. #endif
  54. /* The data structure and setup file ****************************************/
  55. enum ffs_state {
  56. /* Waiting for descriptors and strings. */
  57. /* In this state no open(2), read(2) or write(2) on epfiles
  58. * may succeed (which should not be the problem as there
  59. * should be no such files opened in the firts place). */
  60. FFS_READ_DESCRIPTORS,
  61. FFS_READ_STRINGS,
  62. /* We've got descriptors and strings. We are or have called
  63. * functionfs_ready_callback(). functionfs_bind() may have
  64. * been called but we don't know. */
  65. /* This is the only state in which operations on epfiles may
  66. * succeed. */
  67. FFS_ACTIVE,
  68. /* All endpoints have been closed. This state is also set if
  69. * we encounter an unrecoverable error. The only
  70. * unrecoverable error is situation when after reading strings
  71. * from user space we fail to initialise EP files or
  72. * functionfs_ready_callback() returns with error (<0). */
  73. /* In this state no open(2), read(2) or write(2) (both on ep0
  74. * as well as epfile) may succeed (at this point epfiles are
  75. * unlinked and all closed so this is not a problem; ep0 is
  76. * also closed but ep0 file exists and so open(2) on ep0 must
  77. * fail). */
  78. FFS_CLOSING
  79. };
  80. enum ffs_setup_state {
  81. /* There is no setup request pending. */
  82. FFS_NO_SETUP,
  83. /* User has read events and there was a setup request event
  84. * there. The next read/write on ep0 will handle the
  85. * request. */
  86. FFS_SETUP_PENDING,
  87. /* There was event pending but before user space handled it
  88. * some other event was introduced which canceled existing
  89. * setup. If this state is set read/write on ep0 return
  90. * -EIDRM. This state is only set when adding event. */
  91. FFS_SETUP_CANCELED
  92. };
  93. struct ffs_epfile;
  94. struct ffs_function;
  95. struct ffs_data {
  96. struct usb_gadget *gadget;
  97. /* Protect access read/write operations, only one read/write
  98. * at a time. As a consequence protects ep0req and company.
  99. * While setup request is being processed (queued) this is
  100. * held. */
  101. struct mutex mutex;
  102. /* Protect access to enpoint related structures (basically
  103. * usb_ep_queue(), usb_ep_dequeue(), etc. calls) except for
  104. * endpint zero. */
  105. spinlock_t eps_lock;
  106. /* XXX REVISIT do we need our own request? Since we are not
  107. * handling setup requests immidiatelly 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. struct usb_request *ep0req; /* P: mutex */
  113. struct completion ep0req_completion; /* P: mutex */
  114. int ep0req_status; /* P: mutex */
  115. /* reference counter */
  116. atomic_t ref;
  117. /* how many files are opened (EP0 and others) */
  118. atomic_t opened;
  119. /* EP0 state */
  120. enum ffs_state state;
  121. /*
  122. * Possible transations:
  123. * + FFS_NO_SETUP -> FFS_SETUP_PENDING -- P: ev.waitq.lock
  124. * happens only in ep0 read which is P: mutex
  125. * + FFS_SETUP_PENDING -> FFS_NO_SETUP -- P: ev.waitq.lock
  126. * happens only in ep0 i/o which is P: mutex
  127. * + FFS_SETUP_PENDING -> FFS_SETUP_CANCELED -- P: ev.waitq.lock
  128. * + FFS_SETUP_CANCELED -> FFS_NO_SETUP -- cmpxchg
  129. */
  130. enum ffs_setup_state setup_state;
  131. #define FFS_SETUP_STATE(ffs) \
  132. ((enum ffs_setup_state)cmpxchg(&(ffs)->setup_state, \
  133. FFS_SETUP_CANCELED, FFS_NO_SETUP))
  134. /* Events & such. */
  135. struct {
  136. u8 types[4];
  137. unsigned short count;
  138. /* XXX REVISIT need to update it in some places, or do we? */
  139. unsigned short can_stall;
  140. struct usb_ctrlrequest setup;
  141. wait_queue_head_t waitq;
  142. } ev; /* the whole structure, P: ev.waitq.lock */
  143. /* Flags */
  144. unsigned long flags;
  145. #define FFS_FL_CALL_CLOSED_CALLBACK 0
  146. #define FFS_FL_BOUND 1
  147. /* Active function */
  148. struct ffs_function *func;
  149. /* Device name, write once when file system is mounted.
  150. * Intendet for user to read if she wants. */
  151. const char *dev_name;
  152. /* Private data for our user (ie. gadget). Managed by
  153. * user. */
  154. void *private_data;
  155. /* filled by __ffs_data_got_descs() */
  156. /* real descriptors are 16 bytes after raw_descs (so you need
  157. * to skip 16 bytes (ie. ffs->raw_descs + 16) to get to the
  158. * first full speed descriptor). raw_descs_length and
  159. * raw_fs_descs_length do not have those 16 bytes added. */
  160. const void *raw_descs;
  161. unsigned raw_descs_length;
  162. unsigned raw_fs_descs_length;
  163. unsigned fs_descs_count;
  164. unsigned hs_descs_count;
  165. unsigned short strings_count;
  166. unsigned short interfaces_count;
  167. unsigned short eps_count;
  168. unsigned short _pad1;
  169. /* filled by __ffs_data_got_strings() */
  170. /* ids in stringtabs are set in functionfs_bind() */
  171. const void *raw_strings;
  172. struct usb_gadget_strings **stringtabs;
  173. /* File system's super block, write once when file system is mounted. */
  174. struct super_block *sb;
  175. /* File permissions, written once when fs is mounted*/
  176. struct ffs_file_perms {
  177. umode_t mode;
  178. uid_t uid;
  179. gid_t gid;
  180. } file_perms;
  181. /* The endpoint files, filled by ffs_epfiles_create(),
  182. * destroyed by ffs_epfiles_destroy(). */
  183. struct ffs_epfile *epfiles;
  184. };
  185. /* Reference counter handling */
  186. static void ffs_data_get(struct ffs_data *ffs);
  187. static void ffs_data_put(struct ffs_data *ffs);
  188. /* Creates new ffs_data object. */
  189. static struct ffs_data *__must_check ffs_data_new(void) __attribute__((malloc));
  190. /* Opened counter handling. */
  191. static void ffs_data_opened(struct ffs_data *ffs);
  192. static void ffs_data_closed(struct ffs_data *ffs);
  193. /* Called with ffs->mutex held; take over ownerrship of data. */
  194. static int __must_check
  195. __ffs_data_got_descs(struct ffs_data *ffs, char *data, size_t len);
  196. static int __must_check
  197. __ffs_data_got_strings(struct ffs_data *ffs, char *data, size_t len);
  198. /* The function structure ***************************************************/
  199. struct ffs_ep;
  200. struct ffs_function {
  201. struct usb_configuration *conf;
  202. struct usb_gadget *gadget;
  203. struct ffs_data *ffs;
  204. struct ffs_ep *eps;
  205. u8 eps_revmap[16];
  206. short *interfaces_nums;
  207. struct usb_function function;
  208. };
  209. static struct ffs_function *ffs_func_from_usb(struct usb_function *f)
  210. {
  211. return container_of(f, struct ffs_function, function);
  212. }
  213. static void ffs_func_free(struct ffs_function *func);
  214. static void ffs_func_eps_disable(struct ffs_function *func);
  215. static int __must_check ffs_func_eps_enable(struct ffs_function *func);
  216. static int ffs_func_bind(struct usb_configuration *,
  217. struct usb_function *);
  218. static void ffs_func_unbind(struct usb_configuration *,
  219. struct usb_function *);
  220. static int ffs_func_set_alt(struct usb_function *, unsigned, unsigned);
  221. static void ffs_func_disable(struct usb_function *);
  222. static int ffs_func_setup(struct usb_function *,
  223. const struct usb_ctrlrequest *);
  224. static void ffs_func_suspend(struct usb_function *);
  225. static void ffs_func_resume(struct usb_function *);
  226. static int ffs_func_revmap_ep(struct ffs_function *func, u8 num);
  227. static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf);
  228. /* The endpoints structures *************************************************/
  229. struct ffs_ep {
  230. struct usb_ep *ep; /* P: ffs->eps_lock */
  231. struct usb_request *req; /* P: epfile->mutex */
  232. /* [0]: full speed, [1]: high speed */
  233. struct usb_endpoint_descriptor *descs[2];
  234. u8 num;
  235. int status; /* P: epfile->mutex */
  236. };
  237. struct ffs_epfile {
  238. /* Protects ep->ep and ep->req. */
  239. struct mutex mutex;
  240. wait_queue_head_t wait;
  241. struct ffs_data *ffs;
  242. struct ffs_ep *ep; /* P: ffs->eps_lock */
  243. struct dentry *dentry;
  244. char name[5];
  245. unsigned char in; /* P: ffs->eps_lock */
  246. unsigned char isoc; /* P: ffs->eps_lock */
  247. unsigned char _pad;
  248. };
  249. static int __must_check ffs_epfiles_create(struct ffs_data *ffs);
  250. static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count);
  251. static struct inode *__must_check
  252. ffs_sb_create_file(struct super_block *sb, const char *name, void *data,
  253. const struct file_operations *fops,
  254. struct dentry **dentry_p);
  255. /* Misc helper functions ****************************************************/
  256. static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
  257. __attribute__((warn_unused_result, nonnull));
  258. static char *ffs_prepare_buffer(const char * __user buf, size_t len)
  259. __attribute__((warn_unused_result, nonnull));
  260. /* Control file aka ep0 *****************************************************/
  261. static void ffs_ep0_complete(struct usb_ep *ep, struct usb_request *req)
  262. {
  263. struct ffs_data *ffs = req->context;
  264. complete_all(&ffs->ep0req_completion);
  265. }
  266. static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)
  267. {
  268. struct usb_request *req = ffs->ep0req;
  269. int ret;
  270. req->zero = len < le16_to_cpu(ffs->ev.setup.wLength);
  271. spin_unlock_irq(&ffs->ev.waitq.lock);
  272. req->buf = data;
  273. req->length = len;
  274. INIT_COMPLETION(ffs->ep0req_completion);
  275. ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC);
  276. if (unlikely(ret < 0))
  277. return ret;
  278. ret = wait_for_completion_interruptible(&ffs->ep0req_completion);
  279. if (unlikely(ret)) {
  280. usb_ep_dequeue(ffs->gadget->ep0, req);
  281. return -EINTR;
  282. }
  283. ffs->setup_state = FFS_NO_SETUP;
  284. return ffs->ep0req_status;
  285. }
  286. static int __ffs_ep0_stall(struct ffs_data *ffs)
  287. {
  288. if (ffs->ev.can_stall) {
  289. FVDBG("ep0 stall\n");
  290. usb_ep_set_halt(ffs->gadget->ep0);
  291. ffs->setup_state = FFS_NO_SETUP;
  292. return -EL2HLT;
  293. } else {
  294. FDBG("bogus ep0 stall!\n");
  295. return -ESRCH;
  296. }
  297. }
  298. static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
  299. size_t len, loff_t *ptr)
  300. {
  301. struct ffs_data *ffs = file->private_data;
  302. ssize_t ret;
  303. char *data;
  304. ENTER();
  305. /* Fast check if setup was canceled */
  306. if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED)
  307. return -EIDRM;
  308. /* Acquire mutex */
  309. ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
  310. if (unlikely(ret < 0))
  311. return ret;
  312. /* Check state */
  313. switch (ffs->state) {
  314. case FFS_READ_DESCRIPTORS:
  315. case FFS_READ_STRINGS:
  316. /* Copy data */
  317. if (unlikely(len < 16)) {
  318. ret = -EINVAL;
  319. break;
  320. }
  321. data = ffs_prepare_buffer(buf, len);
  322. if (unlikely(IS_ERR(data))) {
  323. ret = PTR_ERR(data);
  324. break;
  325. }
  326. /* Handle data */
  327. if (ffs->state == FFS_READ_DESCRIPTORS) {
  328. FINFO("read descriptors");
  329. ret = __ffs_data_got_descs(ffs, data, len);
  330. if (unlikely(ret < 0))
  331. break;
  332. ffs->state = FFS_READ_STRINGS;
  333. ret = len;
  334. } else {
  335. FINFO("read strings");
  336. ret = __ffs_data_got_strings(ffs, data, len);
  337. if (unlikely(ret < 0))
  338. break;
  339. ret = ffs_epfiles_create(ffs);
  340. if (unlikely(ret)) {
  341. ffs->state = FFS_CLOSING;
  342. break;
  343. }
  344. ffs->state = FFS_ACTIVE;
  345. mutex_unlock(&ffs->mutex);
  346. ret = functionfs_ready_callback(ffs);
  347. if (unlikely(ret < 0)) {
  348. ffs->state = FFS_CLOSING;
  349. return ret;
  350. }
  351. set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags);
  352. return len;
  353. }
  354. break;
  355. case FFS_ACTIVE:
  356. data = NULL;
  357. /* We're called from user space, we can use _irq
  358. * rather then _irqsave */
  359. spin_lock_irq(&ffs->ev.waitq.lock);
  360. switch (FFS_SETUP_STATE(ffs)) {
  361. case FFS_SETUP_CANCELED:
  362. ret = -EIDRM;
  363. goto done_spin;
  364. case FFS_NO_SETUP:
  365. ret = -ESRCH;
  366. goto done_spin;
  367. case FFS_SETUP_PENDING:
  368. break;
  369. }
  370. /* FFS_SETUP_PENDING */
  371. if (!(ffs->ev.setup.bRequestType & USB_DIR_IN)) {
  372. spin_unlock_irq(&ffs->ev.waitq.lock);
  373. ret = __ffs_ep0_stall(ffs);
  374. break;
  375. }
  376. /* FFS_SETUP_PENDING and not stall */
  377. len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
  378. spin_unlock_irq(&ffs->ev.waitq.lock);
  379. data = ffs_prepare_buffer(buf, len);
  380. if (unlikely(IS_ERR(data))) {
  381. ret = PTR_ERR(data);
  382. break;
  383. }
  384. spin_lock_irq(&ffs->ev.waitq.lock);
  385. /* We are guaranteed to be still in FFS_ACTIVE state
  386. * but the state of setup could have changed from
  387. * FFS_SETUP_PENDING to FFS_SETUP_CANCELED so we need
  388. * to check for that. If that happened we copied data
  389. * from user space in vain but it's unlikely. */
  390. /* For sure we are not in FFS_NO_SETUP since this is
  391. * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP
  392. * transition can be performed and it's protected by
  393. * mutex. */
  394. if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
  395. ret = -EIDRM;
  396. done_spin:
  397. spin_unlock_irq(&ffs->ev.waitq.lock);
  398. } else {
  399. /* unlocks spinlock */
  400. ret = __ffs_ep0_queue_wait(ffs, data, len);
  401. }
  402. kfree(data);
  403. break;
  404. default:
  405. ret = -EBADFD;
  406. break;
  407. }
  408. mutex_unlock(&ffs->mutex);
  409. return ret;
  410. }
  411. static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf,
  412. size_t n)
  413. {
  414. /* We are holding ffs->ev.waitq.lock and ffs->mutex and we need
  415. * to release them. */
  416. struct usb_functionfs_event events[n];
  417. unsigned i = 0;
  418. memset(events, 0, sizeof events);
  419. do {
  420. events[i].type = ffs->ev.types[i];
  421. if (events[i].type == FUNCTIONFS_SETUP) {
  422. events[i].u.setup = ffs->ev.setup;
  423. ffs->setup_state = FFS_SETUP_PENDING;
  424. }
  425. } while (++i < n);
  426. if (n < ffs->ev.count) {
  427. ffs->ev.count -= n;
  428. memmove(ffs->ev.types, ffs->ev.types + n,
  429. ffs->ev.count * sizeof *ffs->ev.types);
  430. } else {
  431. ffs->ev.count = 0;
  432. }
  433. spin_unlock_irq(&ffs->ev.waitq.lock);
  434. mutex_unlock(&ffs->mutex);
  435. return unlikely(__copy_to_user(buf, events, sizeof events))
  436. ? -EFAULT : sizeof events;
  437. }
  438. static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
  439. size_t len, loff_t *ptr)
  440. {
  441. struct ffs_data *ffs = file->private_data;
  442. char *data = NULL;
  443. size_t n;
  444. int ret;
  445. ENTER();
  446. /* Fast check if setup was canceled */
  447. if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED)
  448. return -EIDRM;
  449. /* Acquire mutex */
  450. ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
  451. if (unlikely(ret < 0))
  452. return ret;
  453. /* Check state */
  454. if (ffs->state != FFS_ACTIVE) {
  455. ret = -EBADFD;
  456. goto done_mutex;
  457. }
  458. /* We're called from user space, we can use _irq rather then
  459. * _irqsave */
  460. spin_lock_irq(&ffs->ev.waitq.lock);
  461. switch (FFS_SETUP_STATE(ffs)) {
  462. case FFS_SETUP_CANCELED:
  463. ret = -EIDRM;
  464. break;
  465. case FFS_NO_SETUP:
  466. n = len / sizeof(struct usb_functionfs_event);
  467. if (unlikely(!n)) {
  468. ret = -EINVAL;
  469. break;
  470. }
  471. if ((file->f_flags & O_NONBLOCK) && !ffs->ev.count) {
  472. ret = -EAGAIN;
  473. break;
  474. }
  475. if (unlikely(wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq, ffs->ev.count))) {
  476. ret = -EINTR;
  477. break;
  478. }
  479. return __ffs_ep0_read_events(ffs, buf,
  480. min(n, (size_t)ffs->ev.count));
  481. case FFS_SETUP_PENDING:
  482. if (ffs->ev.setup.bRequestType & USB_DIR_IN) {
  483. spin_unlock_irq(&ffs->ev.waitq.lock);
  484. ret = __ffs_ep0_stall(ffs);
  485. goto done_mutex;
  486. }
  487. len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
  488. spin_unlock_irq(&ffs->ev.waitq.lock);
  489. if (likely(len)) {
  490. data = kmalloc(len, GFP_KERNEL);
  491. if (unlikely(!data)) {
  492. ret = -ENOMEM;
  493. goto done_mutex;
  494. }
  495. }
  496. spin_lock_irq(&ffs->ev.waitq.lock);
  497. /* See ffs_ep0_write() */
  498. if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
  499. ret = -EIDRM;
  500. break;
  501. }
  502. /* unlocks spinlock */
  503. ret = __ffs_ep0_queue_wait(ffs, data, len);
  504. if (likely(ret > 0) && unlikely(__copy_to_user(buf, data, len)))
  505. ret = -EFAULT;
  506. goto done_mutex;
  507. default:
  508. ret = -EBADFD;
  509. break;
  510. }
  511. spin_unlock_irq(&ffs->ev.waitq.lock);
  512. done_mutex:
  513. mutex_unlock(&ffs->mutex);
  514. kfree(data);
  515. return ret;
  516. }
  517. static int ffs_ep0_open(struct inode *inode, struct file *file)
  518. {
  519. struct ffs_data *ffs = inode->i_private;
  520. ENTER();
  521. if (unlikely(ffs->state == FFS_CLOSING))
  522. return -EBUSY;
  523. file->private_data = ffs;
  524. ffs_data_opened(ffs);
  525. return 0;
  526. }
  527. static int ffs_ep0_release(struct inode *inode, struct file *file)
  528. {
  529. struct ffs_data *ffs = file->private_data;
  530. ENTER();
  531. ffs_data_closed(ffs);
  532. return 0;
  533. }
  534. static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)
  535. {
  536. struct ffs_data *ffs = file->private_data;
  537. struct usb_gadget *gadget = ffs->gadget;
  538. long ret;
  539. ENTER();
  540. if (code == FUNCTIONFS_INTERFACE_REVMAP) {
  541. struct ffs_function *func = ffs->func;
  542. ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV;
  543. } else if (gadget->ops->ioctl) {
  544. ret = gadget->ops->ioctl(gadget, code, value);
  545. } else {
  546. ret = -ENOTTY;
  547. }
  548. return ret;
  549. }
  550. static const struct file_operations ffs_ep0_operations = {
  551. .owner = THIS_MODULE,
  552. .llseek = no_llseek,
  553. .open = ffs_ep0_open,
  554. .write = ffs_ep0_write,
  555. .read = ffs_ep0_read,
  556. .release = ffs_ep0_release,
  557. .unlocked_ioctl = ffs_ep0_ioctl,
  558. };
  559. /* "Normal" endpoints operations ********************************************/
  560. static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
  561. {
  562. ENTER();
  563. if (likely(req->context)) {
  564. struct ffs_ep *ep = _ep->driver_data;
  565. ep->status = req->status ? req->status : req->actual;
  566. complete(req->context);
  567. }
  568. }
  569. static ssize_t ffs_epfile_io(struct file *file,
  570. char __user *buf, size_t len, int read)
  571. {
  572. struct ffs_epfile *epfile = file->private_data;
  573. struct ffs_ep *ep;
  574. char *data = NULL;
  575. ssize_t ret;
  576. int halt;
  577. goto first_try;
  578. do {
  579. spin_unlock_irq(&epfile->ffs->eps_lock);
  580. mutex_unlock(&epfile->mutex);
  581. first_try:
  582. /* Are we still active? */
  583. if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) {
  584. ret = -ENODEV;
  585. goto error;
  586. }
  587. /* Wait for endpoint to be enabled */
  588. ep = epfile->ep;
  589. if (!ep) {
  590. if (file->f_flags & O_NONBLOCK) {
  591. ret = -EAGAIN;
  592. goto error;
  593. }
  594. if (unlikely(wait_event_interruptible
  595. (epfile->wait, (ep = epfile->ep)))) {
  596. ret = -EINTR;
  597. goto error;
  598. }
  599. }
  600. /* Do we halt? */
  601. halt = !read == !epfile->in;
  602. if (halt && epfile->isoc) {
  603. ret = -EINVAL;
  604. goto error;
  605. }
  606. /* Allocate & copy */
  607. if (!halt && !data) {
  608. data = kzalloc(len, GFP_KERNEL);
  609. if (unlikely(!data))
  610. return -ENOMEM;
  611. if (!read &&
  612. unlikely(__copy_from_user(data, buf, len))) {
  613. ret = -EFAULT;
  614. goto error;
  615. }
  616. }
  617. /* We will be using request */
  618. ret = ffs_mutex_lock(&epfile->mutex,
  619. file->f_flags & O_NONBLOCK);
  620. if (unlikely(ret))
  621. goto error;
  622. /* We're called from user space, we can use _irq rather then
  623. * _irqsave */
  624. spin_lock_irq(&epfile->ffs->eps_lock);
  625. /* While we were acquiring mutex endpoint got disabled
  626. * or changed? */
  627. } while (unlikely(epfile->ep != ep));
  628. /* Halt */
  629. if (unlikely(halt)) {
  630. if (likely(epfile->ep == ep) && !WARN_ON(!ep->ep))
  631. usb_ep_set_halt(ep->ep);
  632. spin_unlock_irq(&epfile->ffs->eps_lock);
  633. ret = -EBADMSG;
  634. } else {
  635. /* Fire the request */
  636. DECLARE_COMPLETION_ONSTACK(done);
  637. struct usb_request *req = ep->req;
  638. req->context = &done;
  639. req->complete = ffs_epfile_io_complete;
  640. req->buf = data;
  641. req->length = len;
  642. ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
  643. spin_unlock_irq(&epfile->ffs->eps_lock);
  644. if (unlikely(ret < 0)) {
  645. /* nop */
  646. } else if (unlikely(wait_for_completion_interruptible(&done))) {
  647. ret = -EINTR;
  648. usb_ep_dequeue(ep->ep, req);
  649. } else {
  650. ret = ep->status;
  651. if (read && ret > 0 &&
  652. unlikely(copy_to_user(buf, data, ret)))
  653. ret = -EFAULT;
  654. }
  655. }
  656. mutex_unlock(&epfile->mutex);
  657. error:
  658. kfree(data);
  659. return ret;
  660. }
  661. static ssize_t
  662. ffs_epfile_write(struct file *file, const char __user *buf, size_t len,
  663. loff_t *ptr)
  664. {
  665. ENTER();
  666. return ffs_epfile_io(file, (char __user *)buf, len, 0);
  667. }
  668. static ssize_t
  669. ffs_epfile_read(struct file *file, char __user *buf, size_t len, loff_t *ptr)
  670. {
  671. ENTER();
  672. return ffs_epfile_io(file, buf, len, 1);
  673. }
  674. static int
  675. ffs_epfile_open(struct inode *inode, struct file *file)
  676. {
  677. struct ffs_epfile *epfile = inode->i_private;
  678. ENTER();
  679. if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
  680. return -ENODEV;
  681. file->private_data = epfile;
  682. ffs_data_opened(epfile->ffs);
  683. return 0;
  684. }
  685. static int
  686. ffs_epfile_release(struct inode *inode, struct file *file)
  687. {
  688. struct ffs_epfile *epfile = inode->i_private;
  689. ENTER();
  690. ffs_data_closed(epfile->ffs);
  691. return 0;
  692. }
  693. static long ffs_epfile_ioctl(struct file *file, unsigned code,
  694. unsigned long value)
  695. {
  696. struct ffs_epfile *epfile = file->private_data;
  697. int ret;
  698. ENTER();
  699. if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
  700. return -ENODEV;
  701. spin_lock_irq(&epfile->ffs->eps_lock);
  702. if (likely(epfile->ep)) {
  703. switch (code) {
  704. case FUNCTIONFS_FIFO_STATUS:
  705. ret = usb_ep_fifo_status(epfile->ep->ep);
  706. break;
  707. case FUNCTIONFS_FIFO_FLUSH:
  708. usb_ep_fifo_flush(epfile->ep->ep);
  709. ret = 0;
  710. break;
  711. case FUNCTIONFS_CLEAR_HALT:
  712. ret = usb_ep_clear_halt(epfile->ep->ep);
  713. break;
  714. case FUNCTIONFS_ENDPOINT_REVMAP:
  715. ret = epfile->ep->num;
  716. break;
  717. default:
  718. ret = -ENOTTY;
  719. }
  720. } else {
  721. ret = -ENODEV;
  722. }
  723. spin_unlock_irq(&epfile->ffs->eps_lock);
  724. return ret;
  725. }
  726. static const struct file_operations ffs_epfile_operations = {
  727. .owner = THIS_MODULE,
  728. .llseek = no_llseek,
  729. .open = ffs_epfile_open,
  730. .write = ffs_epfile_write,
  731. .read = ffs_epfile_read,
  732. .release = ffs_epfile_release,
  733. .unlocked_ioctl = ffs_epfile_ioctl,
  734. };
  735. /* File system and super block operations ***********************************/
  736. /*
  737. * Mounting the filesystem creates a controller file, used first for
  738. * function configuration then later for event monitoring.
  739. */
  740. static struct inode *__must_check
  741. ffs_sb_make_inode(struct super_block *sb, void *data,
  742. const struct file_operations *fops,
  743. const struct inode_operations *iops,
  744. struct ffs_file_perms *perms)
  745. {
  746. struct inode *inode;
  747. ENTER();
  748. inode = new_inode(sb);
  749. if (likely(inode)) {
  750. struct timespec current_time = CURRENT_TIME;
  751. inode->i_ino = get_next_ino();
  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 struct dentry *
  910. ffs_fs_mount(struct file_system_type *t, int flags,
  911. const char *dev_name, void *opts)
  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 ERR_PTR(ret);
  926. ret = ffs_fs_parse_opts(&data, opts);
  927. if (unlikely(ret < 0))
  928. return ERR_PTR(ret);
  929. data.dev_name = dev_name;
  930. return mount_single(t, flags, &data, ffs_sb_fill);
  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. .mount = ffs_fs_mount,
  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. }