f_fs.c 59 KB

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