f_fs.c 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349
  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 <linux/aio.h>
  27. #include <linux/mmu_context.h>
  28. #include <linux/poll.h>
  29. #include "u_fs.h"
  30. #include "u_f.h"
  31. #include "u_os_desc.h"
  32. #include "configfs.h"
  33. #define FUNCTIONFS_MAGIC 0xa647361 /* Chosen by a honest dice roll ;) */
  34. /* Reference counter handling */
  35. static void ffs_data_get(struct ffs_data *ffs);
  36. static void ffs_data_put(struct ffs_data *ffs);
  37. /* Creates new ffs_data object. */
  38. static struct ffs_data *__must_check ffs_data_new(void) __attribute__((malloc));
  39. /* Opened counter handling. */
  40. static void ffs_data_opened(struct ffs_data *ffs);
  41. static void ffs_data_closed(struct ffs_data *ffs);
  42. /* Called with ffs->mutex held; take over ownership of data. */
  43. static int __must_check
  44. __ffs_data_got_descs(struct ffs_data *ffs, char *data, size_t len);
  45. static int __must_check
  46. __ffs_data_got_strings(struct ffs_data *ffs, char *data, size_t len);
  47. /* The function structure ***************************************************/
  48. struct ffs_ep;
  49. struct ffs_function {
  50. struct usb_configuration *conf;
  51. struct usb_gadget *gadget;
  52. struct ffs_data *ffs;
  53. struct ffs_ep *eps;
  54. u8 eps_revmap[16];
  55. short *interfaces_nums;
  56. struct usb_function function;
  57. };
  58. static struct ffs_function *ffs_func_from_usb(struct usb_function *f)
  59. {
  60. return container_of(f, struct ffs_function, function);
  61. }
  62. static inline enum ffs_setup_state
  63. ffs_setup_state_clear_cancelled(struct ffs_data *ffs)
  64. {
  65. return (enum ffs_setup_state)
  66. cmpxchg(&ffs->setup_state, FFS_SETUP_CANCELLED, FFS_NO_SETUP);
  67. }
  68. static void ffs_func_eps_disable(struct ffs_function *func);
  69. static int __must_check ffs_func_eps_enable(struct ffs_function *func);
  70. static int ffs_func_bind(struct usb_configuration *,
  71. struct usb_function *);
  72. static int ffs_func_set_alt(struct usb_function *, unsigned, unsigned);
  73. static void ffs_func_disable(struct usb_function *);
  74. static int ffs_func_setup(struct usb_function *,
  75. const struct usb_ctrlrequest *);
  76. static void ffs_func_suspend(struct usb_function *);
  77. static void ffs_func_resume(struct usb_function *);
  78. static int ffs_func_revmap_ep(struct ffs_function *func, u8 num);
  79. static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf);
  80. /* The endpoints structures *************************************************/
  81. struct ffs_ep {
  82. struct usb_ep *ep; /* P: ffs->eps_lock */
  83. struct usb_request *req; /* P: epfile->mutex */
  84. /* [0]: full speed, [1]: high speed, [2]: super speed */
  85. struct usb_endpoint_descriptor *descs[3];
  86. u8 num;
  87. int status; /* P: epfile->mutex */
  88. };
  89. struct ffs_epfile {
  90. /* Protects ep->ep and ep->req. */
  91. struct mutex mutex;
  92. wait_queue_head_t wait;
  93. struct ffs_data *ffs;
  94. struct ffs_ep *ep; /* P: ffs->eps_lock */
  95. struct dentry *dentry;
  96. char name[5];
  97. unsigned char in; /* P: ffs->eps_lock */
  98. unsigned char isoc; /* P: ffs->eps_lock */
  99. unsigned char _pad;
  100. };
  101. /* ffs_io_data structure ***************************************************/
  102. struct ffs_io_data {
  103. bool aio;
  104. bool read;
  105. struct kiocb *kiocb;
  106. const struct iovec *iovec;
  107. unsigned long nr_segs;
  108. char __user *buf;
  109. size_t len;
  110. struct mm_struct *mm;
  111. struct work_struct work;
  112. struct usb_ep *ep;
  113. struct usb_request *req;
  114. };
  115. static int __must_check ffs_epfiles_create(struct ffs_data *ffs);
  116. static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count);
  117. static struct inode *__must_check
  118. ffs_sb_create_file(struct super_block *sb, const char *name, void *data,
  119. const struct file_operations *fops,
  120. struct dentry **dentry_p);
  121. /* Devices management *******************************************************/
  122. DEFINE_MUTEX(ffs_lock);
  123. EXPORT_SYMBOL_GPL(ffs_lock);
  124. static struct ffs_dev *_ffs_find_dev(const char *name);
  125. static struct ffs_dev *_ffs_alloc_dev(void);
  126. static int _ffs_name_dev(struct ffs_dev *dev, const char *name);
  127. static void _ffs_free_dev(struct ffs_dev *dev);
  128. static void *ffs_acquire_dev(const char *dev_name);
  129. static void ffs_release_dev(struct ffs_data *ffs_data);
  130. static int ffs_ready(struct ffs_data *ffs);
  131. static void ffs_closed(struct ffs_data *ffs);
  132. /* Misc helper functions ****************************************************/
  133. static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
  134. __attribute__((warn_unused_result, nonnull));
  135. static char *ffs_prepare_buffer(const char __user *buf, size_t len)
  136. __attribute__((warn_unused_result, nonnull));
  137. /* Control file aka ep0 *****************************************************/
  138. static void ffs_ep0_complete(struct usb_ep *ep, struct usb_request *req)
  139. {
  140. struct ffs_data *ffs = req->context;
  141. complete_all(&ffs->ep0req_completion);
  142. }
  143. static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)
  144. {
  145. struct usb_request *req = ffs->ep0req;
  146. int ret;
  147. req->zero = len < le16_to_cpu(ffs->ev.setup.wLength);
  148. spin_unlock_irq(&ffs->ev.waitq.lock);
  149. req->buf = data;
  150. req->length = len;
  151. /*
  152. * UDC layer requires to provide a buffer even for ZLP, but should
  153. * not use it at all. Let's provide some poisoned pointer to catch
  154. * possible bug in the driver.
  155. */
  156. if (req->buf == NULL)
  157. req->buf = (void *)0xDEADBABE;
  158. reinit_completion(&ffs->ep0req_completion);
  159. ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC);
  160. if (unlikely(ret < 0))
  161. return ret;
  162. ret = wait_for_completion_interruptible(&ffs->ep0req_completion);
  163. if (unlikely(ret)) {
  164. usb_ep_dequeue(ffs->gadget->ep0, req);
  165. return -EINTR;
  166. }
  167. ffs->setup_state = FFS_NO_SETUP;
  168. return req->status ? req->status : req->actual;
  169. }
  170. static int __ffs_ep0_stall(struct ffs_data *ffs)
  171. {
  172. if (ffs->ev.can_stall) {
  173. pr_vdebug("ep0 stall\n");
  174. usb_ep_set_halt(ffs->gadget->ep0);
  175. ffs->setup_state = FFS_NO_SETUP;
  176. return -EL2HLT;
  177. } else {
  178. pr_debug("bogus ep0 stall!\n");
  179. return -ESRCH;
  180. }
  181. }
  182. static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
  183. size_t len, loff_t *ptr)
  184. {
  185. struct ffs_data *ffs = file->private_data;
  186. ssize_t ret;
  187. char *data;
  188. ENTER();
  189. /* Fast check if setup was canceled */
  190. if (ffs_setup_state_clear_cancelled(ffs) == FFS_SETUP_CANCELLED)
  191. return -EIDRM;
  192. /* Acquire mutex */
  193. ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
  194. if (unlikely(ret < 0))
  195. return ret;
  196. /* Check state */
  197. switch (ffs->state) {
  198. case FFS_READ_DESCRIPTORS:
  199. case FFS_READ_STRINGS:
  200. /* Copy data */
  201. if (unlikely(len < 16)) {
  202. ret = -EINVAL;
  203. break;
  204. }
  205. data = ffs_prepare_buffer(buf, len);
  206. if (IS_ERR(data)) {
  207. ret = PTR_ERR(data);
  208. break;
  209. }
  210. /* Handle data */
  211. if (ffs->state == FFS_READ_DESCRIPTORS) {
  212. pr_info("read descriptors\n");
  213. ret = __ffs_data_got_descs(ffs, data, len);
  214. if (unlikely(ret < 0))
  215. break;
  216. ffs->state = FFS_READ_STRINGS;
  217. ret = len;
  218. } else {
  219. pr_info("read strings\n");
  220. ret = __ffs_data_got_strings(ffs, data, len);
  221. if (unlikely(ret < 0))
  222. break;
  223. ret = ffs_epfiles_create(ffs);
  224. if (unlikely(ret)) {
  225. ffs->state = FFS_CLOSING;
  226. break;
  227. }
  228. ffs->state = FFS_ACTIVE;
  229. mutex_unlock(&ffs->mutex);
  230. ret = ffs_ready(ffs);
  231. if (unlikely(ret < 0)) {
  232. ffs->state = FFS_CLOSING;
  233. return ret;
  234. }
  235. set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags);
  236. return len;
  237. }
  238. break;
  239. case FFS_ACTIVE:
  240. data = NULL;
  241. /*
  242. * We're called from user space, we can use _irq
  243. * rather then _irqsave
  244. */
  245. spin_lock_irq(&ffs->ev.waitq.lock);
  246. switch (ffs_setup_state_clear_cancelled(ffs)) {
  247. case FFS_SETUP_CANCELLED:
  248. ret = -EIDRM;
  249. goto done_spin;
  250. case FFS_NO_SETUP:
  251. ret = -ESRCH;
  252. goto done_spin;
  253. case FFS_SETUP_PENDING:
  254. break;
  255. }
  256. /* FFS_SETUP_PENDING */
  257. if (!(ffs->ev.setup.bRequestType & USB_DIR_IN)) {
  258. spin_unlock_irq(&ffs->ev.waitq.lock);
  259. ret = __ffs_ep0_stall(ffs);
  260. break;
  261. }
  262. /* FFS_SETUP_PENDING and not stall */
  263. len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
  264. spin_unlock_irq(&ffs->ev.waitq.lock);
  265. data = ffs_prepare_buffer(buf, len);
  266. if (IS_ERR(data)) {
  267. ret = PTR_ERR(data);
  268. break;
  269. }
  270. spin_lock_irq(&ffs->ev.waitq.lock);
  271. /*
  272. * We are guaranteed to be still in FFS_ACTIVE state
  273. * but the state of setup could have changed from
  274. * FFS_SETUP_PENDING to FFS_SETUP_CANCELLED so we need
  275. * to check for that. If that happened we copied data
  276. * from user space in vain but it's unlikely.
  277. *
  278. * For sure we are not in FFS_NO_SETUP since this is
  279. * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP
  280. * transition can be performed and it's protected by
  281. * mutex.
  282. */
  283. if (ffs_setup_state_clear_cancelled(ffs) ==
  284. FFS_SETUP_CANCELLED) {
  285. ret = -EIDRM;
  286. done_spin:
  287. spin_unlock_irq(&ffs->ev.waitq.lock);
  288. } else {
  289. /* unlocks spinlock */
  290. ret = __ffs_ep0_queue_wait(ffs, data, len);
  291. }
  292. kfree(data);
  293. break;
  294. default:
  295. ret = -EBADFD;
  296. break;
  297. }
  298. mutex_unlock(&ffs->mutex);
  299. return ret;
  300. }
  301. static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf,
  302. size_t n)
  303. {
  304. /*
  305. * We are holding ffs->ev.waitq.lock and ffs->mutex and we need
  306. * to release them.
  307. */
  308. struct usb_functionfs_event events[n];
  309. unsigned i = 0;
  310. memset(events, 0, sizeof events);
  311. do {
  312. events[i].type = ffs->ev.types[i];
  313. if (events[i].type == FUNCTIONFS_SETUP) {
  314. events[i].u.setup = ffs->ev.setup;
  315. ffs->setup_state = FFS_SETUP_PENDING;
  316. }
  317. } while (++i < n);
  318. if (n < ffs->ev.count) {
  319. ffs->ev.count -= n;
  320. memmove(ffs->ev.types, ffs->ev.types + n,
  321. ffs->ev.count * sizeof *ffs->ev.types);
  322. } else {
  323. ffs->ev.count = 0;
  324. }
  325. spin_unlock_irq(&ffs->ev.waitq.lock);
  326. mutex_unlock(&ffs->mutex);
  327. return unlikely(__copy_to_user(buf, events, sizeof events))
  328. ? -EFAULT : sizeof events;
  329. }
  330. static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
  331. size_t len, loff_t *ptr)
  332. {
  333. struct ffs_data *ffs = file->private_data;
  334. char *data = NULL;
  335. size_t n;
  336. int ret;
  337. ENTER();
  338. /* Fast check if setup was canceled */
  339. if (ffs_setup_state_clear_cancelled(ffs) == FFS_SETUP_CANCELLED)
  340. return -EIDRM;
  341. /* Acquire mutex */
  342. ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
  343. if (unlikely(ret < 0))
  344. return ret;
  345. /* Check state */
  346. if (ffs->state != FFS_ACTIVE) {
  347. ret = -EBADFD;
  348. goto done_mutex;
  349. }
  350. /*
  351. * We're called from user space, we can use _irq rather then
  352. * _irqsave
  353. */
  354. spin_lock_irq(&ffs->ev.waitq.lock);
  355. switch (ffs_setup_state_clear_cancelled(ffs)) {
  356. case FFS_SETUP_CANCELLED:
  357. ret = -EIDRM;
  358. break;
  359. case FFS_NO_SETUP:
  360. n = len / sizeof(struct usb_functionfs_event);
  361. if (unlikely(!n)) {
  362. ret = -EINVAL;
  363. break;
  364. }
  365. if ((file->f_flags & O_NONBLOCK) && !ffs->ev.count) {
  366. ret = -EAGAIN;
  367. break;
  368. }
  369. if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq,
  370. ffs->ev.count)) {
  371. ret = -EINTR;
  372. break;
  373. }
  374. return __ffs_ep0_read_events(ffs, buf,
  375. min(n, (size_t)ffs->ev.count));
  376. case FFS_SETUP_PENDING:
  377. if (ffs->ev.setup.bRequestType & USB_DIR_IN) {
  378. spin_unlock_irq(&ffs->ev.waitq.lock);
  379. ret = __ffs_ep0_stall(ffs);
  380. goto done_mutex;
  381. }
  382. len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
  383. spin_unlock_irq(&ffs->ev.waitq.lock);
  384. if (likely(len)) {
  385. data = kmalloc(len, GFP_KERNEL);
  386. if (unlikely(!data)) {
  387. ret = -ENOMEM;
  388. goto done_mutex;
  389. }
  390. }
  391. spin_lock_irq(&ffs->ev.waitq.lock);
  392. /* See ffs_ep0_write() */
  393. if (ffs_setup_state_clear_cancelled(ffs) ==
  394. FFS_SETUP_CANCELLED) {
  395. ret = -EIDRM;
  396. break;
  397. }
  398. /* unlocks spinlock */
  399. ret = __ffs_ep0_queue_wait(ffs, data, len);
  400. if (likely(ret > 0) && unlikely(__copy_to_user(buf, data, len)))
  401. ret = -EFAULT;
  402. goto done_mutex;
  403. default:
  404. ret = -EBADFD;
  405. break;
  406. }
  407. spin_unlock_irq(&ffs->ev.waitq.lock);
  408. done_mutex:
  409. mutex_unlock(&ffs->mutex);
  410. kfree(data);
  411. return ret;
  412. }
  413. static int ffs_ep0_open(struct inode *inode, struct file *file)
  414. {
  415. struct ffs_data *ffs = inode->i_private;
  416. ENTER();
  417. if (unlikely(ffs->state == FFS_CLOSING))
  418. return -EBUSY;
  419. file->private_data = ffs;
  420. ffs_data_opened(ffs);
  421. return 0;
  422. }
  423. static int ffs_ep0_release(struct inode *inode, struct file *file)
  424. {
  425. struct ffs_data *ffs = file->private_data;
  426. ENTER();
  427. ffs_data_closed(ffs);
  428. return 0;
  429. }
  430. static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)
  431. {
  432. struct ffs_data *ffs = file->private_data;
  433. struct usb_gadget *gadget = ffs->gadget;
  434. long ret;
  435. ENTER();
  436. if (code == FUNCTIONFS_INTERFACE_REVMAP) {
  437. struct ffs_function *func = ffs->func;
  438. ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV;
  439. } else if (gadget && gadget->ops->ioctl) {
  440. ret = gadget->ops->ioctl(gadget, code, value);
  441. } else {
  442. ret = -ENOTTY;
  443. }
  444. return ret;
  445. }
  446. static unsigned int ffs_ep0_poll(struct file *file, poll_table *wait)
  447. {
  448. struct ffs_data *ffs = file->private_data;
  449. unsigned int mask = POLLWRNORM;
  450. int ret;
  451. poll_wait(file, &ffs->ev.waitq, wait);
  452. ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
  453. if (unlikely(ret < 0))
  454. return mask;
  455. switch (ffs->state) {
  456. case FFS_READ_DESCRIPTORS:
  457. case FFS_READ_STRINGS:
  458. mask |= POLLOUT;
  459. break;
  460. case FFS_ACTIVE:
  461. switch (ffs->setup_state) {
  462. case FFS_NO_SETUP:
  463. if (ffs->ev.count)
  464. mask |= POLLIN;
  465. break;
  466. case FFS_SETUP_PENDING:
  467. case FFS_SETUP_CANCELLED:
  468. mask |= (POLLIN | POLLOUT);
  469. break;
  470. }
  471. case FFS_CLOSING:
  472. break;
  473. }
  474. mutex_unlock(&ffs->mutex);
  475. return mask;
  476. }
  477. static const struct file_operations ffs_ep0_operations = {
  478. .llseek = no_llseek,
  479. .open = ffs_ep0_open,
  480. .write = ffs_ep0_write,
  481. .read = ffs_ep0_read,
  482. .release = ffs_ep0_release,
  483. .unlocked_ioctl = ffs_ep0_ioctl,
  484. .poll = ffs_ep0_poll,
  485. };
  486. /* "Normal" endpoints operations ********************************************/
  487. static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
  488. {
  489. ENTER();
  490. if (likely(req->context)) {
  491. struct ffs_ep *ep = _ep->driver_data;
  492. ep->status = req->status ? req->status : req->actual;
  493. complete(req->context);
  494. }
  495. }
  496. static void ffs_user_copy_worker(struct work_struct *work)
  497. {
  498. struct ffs_io_data *io_data = container_of(work, struct ffs_io_data,
  499. work);
  500. int ret = io_data->req->status ? io_data->req->status :
  501. io_data->req->actual;
  502. if (io_data->read && ret > 0) {
  503. int i;
  504. size_t pos = 0;
  505. use_mm(io_data->mm);
  506. for (i = 0; i < io_data->nr_segs; i++) {
  507. if (unlikely(copy_to_user(io_data->iovec[i].iov_base,
  508. &io_data->buf[pos],
  509. io_data->iovec[i].iov_len))) {
  510. ret = -EFAULT;
  511. break;
  512. }
  513. pos += io_data->iovec[i].iov_len;
  514. }
  515. unuse_mm(io_data->mm);
  516. }
  517. aio_complete(io_data->kiocb, ret, ret);
  518. usb_ep_free_request(io_data->ep, io_data->req);
  519. io_data->kiocb->private = NULL;
  520. if (io_data->read)
  521. kfree(io_data->iovec);
  522. kfree(io_data->buf);
  523. kfree(io_data);
  524. }
  525. static void ffs_epfile_async_io_complete(struct usb_ep *_ep,
  526. struct usb_request *req)
  527. {
  528. struct ffs_io_data *io_data = req->context;
  529. ENTER();
  530. INIT_WORK(&io_data->work, ffs_user_copy_worker);
  531. schedule_work(&io_data->work);
  532. }
  533. static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data)
  534. {
  535. struct ffs_epfile *epfile = file->private_data;
  536. struct ffs_ep *ep;
  537. char *data = NULL;
  538. ssize_t ret, data_len;
  539. int halt;
  540. /* Are we still active? */
  541. if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) {
  542. ret = -ENODEV;
  543. goto error;
  544. }
  545. /* Wait for endpoint to be enabled */
  546. ep = epfile->ep;
  547. if (!ep) {
  548. if (file->f_flags & O_NONBLOCK) {
  549. ret = -EAGAIN;
  550. goto error;
  551. }
  552. ret = wait_event_interruptible(epfile->wait, (ep = epfile->ep));
  553. if (ret) {
  554. ret = -EINTR;
  555. goto error;
  556. }
  557. }
  558. /* Do we halt? */
  559. halt = (!io_data->read == !epfile->in);
  560. if (halt && epfile->isoc) {
  561. ret = -EINVAL;
  562. goto error;
  563. }
  564. /* Allocate & copy */
  565. if (!halt) {
  566. /*
  567. * if we _do_ wait above, the epfile->ffs->gadget might be NULL
  568. * before the waiting completes, so do not assign to 'gadget' earlier
  569. */
  570. struct usb_gadget *gadget = epfile->ffs->gadget;
  571. spin_lock_irq(&epfile->ffs->eps_lock);
  572. /* In the meantime, endpoint got disabled or changed. */
  573. if (epfile->ep != ep) {
  574. spin_unlock_irq(&epfile->ffs->eps_lock);
  575. return -ESHUTDOWN;
  576. }
  577. /*
  578. * Controller may require buffer size to be aligned to
  579. * maxpacketsize of an out endpoint.
  580. */
  581. data_len = io_data->read ?
  582. usb_ep_align_maybe(gadget, ep->ep, io_data->len) :
  583. io_data->len;
  584. spin_unlock_irq(&epfile->ffs->eps_lock);
  585. data = kmalloc(data_len, GFP_KERNEL);
  586. if (unlikely(!data))
  587. return -ENOMEM;
  588. if (io_data->aio && !io_data->read) {
  589. int i;
  590. size_t pos = 0;
  591. for (i = 0; i < io_data->nr_segs; i++) {
  592. if (unlikely(copy_from_user(&data[pos],
  593. io_data->iovec[i].iov_base,
  594. io_data->iovec[i].iov_len))) {
  595. ret = -EFAULT;
  596. goto error;
  597. }
  598. pos += io_data->iovec[i].iov_len;
  599. }
  600. } else {
  601. if (!io_data->read &&
  602. unlikely(__copy_from_user(data, io_data->buf,
  603. io_data->len))) {
  604. ret = -EFAULT;
  605. goto error;
  606. }
  607. }
  608. }
  609. /* We will be using request */
  610. ret = ffs_mutex_lock(&epfile->mutex, file->f_flags & O_NONBLOCK);
  611. if (unlikely(ret))
  612. goto error;
  613. spin_lock_irq(&epfile->ffs->eps_lock);
  614. if (epfile->ep != ep) {
  615. /* In the meantime, endpoint got disabled or changed. */
  616. ret = -ESHUTDOWN;
  617. spin_unlock_irq(&epfile->ffs->eps_lock);
  618. } else if (halt) {
  619. /* Halt */
  620. if (likely(epfile->ep == ep) && !WARN_ON(!ep->ep))
  621. usb_ep_set_halt(ep->ep);
  622. spin_unlock_irq(&epfile->ffs->eps_lock);
  623. ret = -EBADMSG;
  624. } else {
  625. /* Fire the request */
  626. struct usb_request *req;
  627. if (io_data->aio) {
  628. req = usb_ep_alloc_request(ep->ep, GFP_KERNEL);
  629. if (unlikely(!req))
  630. goto error_lock;
  631. req->buf = data;
  632. req->length = io_data->len;
  633. io_data->buf = data;
  634. io_data->ep = ep->ep;
  635. io_data->req = req;
  636. req->context = io_data;
  637. req->complete = ffs_epfile_async_io_complete;
  638. ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
  639. if (unlikely(ret)) {
  640. usb_ep_free_request(ep->ep, req);
  641. goto error_lock;
  642. }
  643. ret = -EIOCBQUEUED;
  644. spin_unlock_irq(&epfile->ffs->eps_lock);
  645. } else {
  646. DECLARE_COMPLETION_ONSTACK(done);
  647. req = ep->req;
  648. req->buf = data;
  649. req->length = io_data->len;
  650. req->context = &done;
  651. req->complete = ffs_epfile_io_complete;
  652. ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
  653. spin_unlock_irq(&epfile->ffs->eps_lock);
  654. if (unlikely(ret < 0)) {
  655. /* nop */
  656. } else if (unlikely(
  657. wait_for_completion_interruptible(&done))) {
  658. ret = -EINTR;
  659. usb_ep_dequeue(ep->ep, req);
  660. } else {
  661. /*
  662. * XXX We may end up silently droping data
  663. * here. Since data_len (i.e. req->length) may
  664. * be bigger than len (after being rounded up
  665. * to maxpacketsize), we may end up with more
  666. * data then user space has space for.
  667. */
  668. ret = ep->status;
  669. if (io_data->read && ret > 0) {
  670. ret = min_t(size_t, ret, io_data->len);
  671. if (unlikely(copy_to_user(io_data->buf,
  672. data, ret)))
  673. ret = -EFAULT;
  674. }
  675. }
  676. kfree(data);
  677. }
  678. }
  679. mutex_unlock(&epfile->mutex);
  680. return ret;
  681. error_lock:
  682. spin_unlock_irq(&epfile->ffs->eps_lock);
  683. mutex_unlock(&epfile->mutex);
  684. error:
  685. kfree(data);
  686. return ret;
  687. }
  688. static ssize_t
  689. ffs_epfile_write(struct file *file, const char __user *buf, size_t len,
  690. loff_t *ptr)
  691. {
  692. struct ffs_io_data io_data;
  693. ENTER();
  694. io_data.aio = false;
  695. io_data.read = false;
  696. io_data.buf = (char * __user)buf;
  697. io_data.len = len;
  698. return ffs_epfile_io(file, &io_data);
  699. }
  700. static ssize_t
  701. ffs_epfile_read(struct file *file, char __user *buf, size_t len, loff_t *ptr)
  702. {
  703. struct ffs_io_data io_data;
  704. ENTER();
  705. io_data.aio = false;
  706. io_data.read = true;
  707. io_data.buf = buf;
  708. io_data.len = len;
  709. return ffs_epfile_io(file, &io_data);
  710. }
  711. static int
  712. ffs_epfile_open(struct inode *inode, struct file *file)
  713. {
  714. struct ffs_epfile *epfile = inode->i_private;
  715. ENTER();
  716. if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
  717. return -ENODEV;
  718. file->private_data = epfile;
  719. ffs_data_opened(epfile->ffs);
  720. return 0;
  721. }
  722. static int ffs_aio_cancel(struct kiocb *kiocb)
  723. {
  724. struct ffs_io_data *io_data = kiocb->private;
  725. struct ffs_epfile *epfile = kiocb->ki_filp->private_data;
  726. int value;
  727. ENTER();
  728. spin_lock_irq(&epfile->ffs->eps_lock);
  729. if (likely(io_data && io_data->ep && io_data->req))
  730. value = usb_ep_dequeue(io_data->ep, io_data->req);
  731. else
  732. value = -EINVAL;
  733. spin_unlock_irq(&epfile->ffs->eps_lock);
  734. return value;
  735. }
  736. static ssize_t ffs_epfile_aio_write(struct kiocb *kiocb,
  737. const struct iovec *iovec,
  738. unsigned long nr_segs, loff_t loff)
  739. {
  740. struct ffs_io_data *io_data;
  741. ENTER();
  742. io_data = kmalloc(sizeof(*io_data), GFP_KERNEL);
  743. if (unlikely(!io_data))
  744. return -ENOMEM;
  745. io_data->aio = true;
  746. io_data->read = false;
  747. io_data->kiocb = kiocb;
  748. io_data->iovec = iovec;
  749. io_data->nr_segs = nr_segs;
  750. io_data->len = kiocb->ki_nbytes;
  751. io_data->mm = current->mm;
  752. kiocb->private = io_data;
  753. kiocb_set_cancel_fn(kiocb, ffs_aio_cancel);
  754. return ffs_epfile_io(kiocb->ki_filp, io_data);
  755. }
  756. static ssize_t ffs_epfile_aio_read(struct kiocb *kiocb,
  757. const struct iovec *iovec,
  758. unsigned long nr_segs, loff_t loff)
  759. {
  760. struct ffs_io_data *io_data;
  761. struct iovec *iovec_copy;
  762. ENTER();
  763. iovec_copy = kmalloc_array(nr_segs, sizeof(*iovec_copy), GFP_KERNEL);
  764. if (unlikely(!iovec_copy))
  765. return -ENOMEM;
  766. memcpy(iovec_copy, iovec, sizeof(struct iovec)*nr_segs);
  767. io_data = kmalloc(sizeof(*io_data), GFP_KERNEL);
  768. if (unlikely(!io_data)) {
  769. kfree(iovec_copy);
  770. return -ENOMEM;
  771. }
  772. io_data->aio = true;
  773. io_data->read = true;
  774. io_data->kiocb = kiocb;
  775. io_data->iovec = iovec_copy;
  776. io_data->nr_segs = nr_segs;
  777. io_data->len = kiocb->ki_nbytes;
  778. io_data->mm = current->mm;
  779. kiocb->private = io_data;
  780. kiocb_set_cancel_fn(kiocb, ffs_aio_cancel);
  781. return ffs_epfile_io(kiocb->ki_filp, io_data);
  782. }
  783. static int
  784. ffs_epfile_release(struct inode *inode, struct file *file)
  785. {
  786. struct ffs_epfile *epfile = inode->i_private;
  787. ENTER();
  788. ffs_data_closed(epfile->ffs);
  789. return 0;
  790. }
  791. static long ffs_epfile_ioctl(struct file *file, unsigned code,
  792. unsigned long value)
  793. {
  794. struct ffs_epfile *epfile = file->private_data;
  795. int ret;
  796. ENTER();
  797. if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
  798. return -ENODEV;
  799. spin_lock_irq(&epfile->ffs->eps_lock);
  800. if (likely(epfile->ep)) {
  801. switch (code) {
  802. case FUNCTIONFS_FIFO_STATUS:
  803. ret = usb_ep_fifo_status(epfile->ep->ep);
  804. break;
  805. case FUNCTIONFS_FIFO_FLUSH:
  806. usb_ep_fifo_flush(epfile->ep->ep);
  807. ret = 0;
  808. break;
  809. case FUNCTIONFS_CLEAR_HALT:
  810. ret = usb_ep_clear_halt(epfile->ep->ep);
  811. break;
  812. case FUNCTIONFS_ENDPOINT_REVMAP:
  813. ret = epfile->ep->num;
  814. break;
  815. default:
  816. ret = -ENOTTY;
  817. }
  818. } else {
  819. ret = -ENODEV;
  820. }
  821. spin_unlock_irq(&epfile->ffs->eps_lock);
  822. return ret;
  823. }
  824. static const struct file_operations ffs_epfile_operations = {
  825. .llseek = no_llseek,
  826. .open = ffs_epfile_open,
  827. .write = ffs_epfile_write,
  828. .read = ffs_epfile_read,
  829. .aio_write = ffs_epfile_aio_write,
  830. .aio_read = ffs_epfile_aio_read,
  831. .release = ffs_epfile_release,
  832. .unlocked_ioctl = ffs_epfile_ioctl,
  833. };
  834. /* File system and super block operations ***********************************/
  835. /*
  836. * Mounting the file system creates a controller file, used first for
  837. * function configuration then later for event monitoring.
  838. */
  839. static struct inode *__must_check
  840. ffs_sb_make_inode(struct super_block *sb, void *data,
  841. const struct file_operations *fops,
  842. const struct inode_operations *iops,
  843. struct ffs_file_perms *perms)
  844. {
  845. struct inode *inode;
  846. ENTER();
  847. inode = new_inode(sb);
  848. if (likely(inode)) {
  849. struct timespec current_time = CURRENT_TIME;
  850. inode->i_ino = get_next_ino();
  851. inode->i_mode = perms->mode;
  852. inode->i_uid = perms->uid;
  853. inode->i_gid = perms->gid;
  854. inode->i_atime = current_time;
  855. inode->i_mtime = current_time;
  856. inode->i_ctime = current_time;
  857. inode->i_private = data;
  858. if (fops)
  859. inode->i_fop = fops;
  860. if (iops)
  861. inode->i_op = iops;
  862. }
  863. return inode;
  864. }
  865. /* Create "regular" file */
  866. static struct inode *ffs_sb_create_file(struct super_block *sb,
  867. const char *name, void *data,
  868. const struct file_operations *fops,
  869. struct dentry **dentry_p)
  870. {
  871. struct ffs_data *ffs = sb->s_fs_info;
  872. struct dentry *dentry;
  873. struct inode *inode;
  874. ENTER();
  875. dentry = d_alloc_name(sb->s_root, name);
  876. if (unlikely(!dentry))
  877. return NULL;
  878. inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms);
  879. if (unlikely(!inode)) {
  880. dput(dentry);
  881. return NULL;
  882. }
  883. d_add(dentry, inode);
  884. if (dentry_p)
  885. *dentry_p = dentry;
  886. return inode;
  887. }
  888. /* Super block */
  889. static const struct super_operations ffs_sb_operations = {
  890. .statfs = simple_statfs,
  891. .drop_inode = generic_delete_inode,
  892. };
  893. struct ffs_sb_fill_data {
  894. struct ffs_file_perms perms;
  895. umode_t root_mode;
  896. const char *dev_name;
  897. struct ffs_data *ffs_data;
  898. };
  899. static int ffs_sb_fill(struct super_block *sb, void *_data, int silent)
  900. {
  901. struct ffs_sb_fill_data *data = _data;
  902. struct inode *inode;
  903. struct ffs_data *ffs = data->ffs_data;
  904. ENTER();
  905. ffs->sb = sb;
  906. data->ffs_data = NULL;
  907. sb->s_fs_info = ffs;
  908. sb->s_blocksize = PAGE_CACHE_SIZE;
  909. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  910. sb->s_magic = FUNCTIONFS_MAGIC;
  911. sb->s_op = &ffs_sb_operations;
  912. sb->s_time_gran = 1;
  913. /* Root inode */
  914. data->perms.mode = data->root_mode;
  915. inode = ffs_sb_make_inode(sb, NULL,
  916. &simple_dir_operations,
  917. &simple_dir_inode_operations,
  918. &data->perms);
  919. sb->s_root = d_make_root(inode);
  920. if (unlikely(!sb->s_root))
  921. return -ENOMEM;
  922. /* EP0 file */
  923. if (unlikely(!ffs_sb_create_file(sb, "ep0", ffs,
  924. &ffs_ep0_operations, NULL)))
  925. return -ENOMEM;
  926. return 0;
  927. }
  928. static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
  929. {
  930. ENTER();
  931. if (!opts || !*opts)
  932. return 0;
  933. for (;;) {
  934. unsigned long value;
  935. char *eq, *comma;
  936. /* Option limit */
  937. comma = strchr(opts, ',');
  938. if (comma)
  939. *comma = 0;
  940. /* Value limit */
  941. eq = strchr(opts, '=');
  942. if (unlikely(!eq)) {
  943. pr_err("'=' missing in %s\n", opts);
  944. return -EINVAL;
  945. }
  946. *eq = 0;
  947. /* Parse value */
  948. if (kstrtoul(eq + 1, 0, &value)) {
  949. pr_err("%s: invalid value: %s\n", opts, eq + 1);
  950. return -EINVAL;
  951. }
  952. /* Interpret option */
  953. switch (eq - opts) {
  954. case 5:
  955. if (!memcmp(opts, "rmode", 5))
  956. data->root_mode = (value & 0555) | S_IFDIR;
  957. else if (!memcmp(opts, "fmode", 5))
  958. data->perms.mode = (value & 0666) | S_IFREG;
  959. else
  960. goto invalid;
  961. break;
  962. case 4:
  963. if (!memcmp(opts, "mode", 4)) {
  964. data->root_mode = (value & 0555) | S_IFDIR;
  965. data->perms.mode = (value & 0666) | S_IFREG;
  966. } else {
  967. goto invalid;
  968. }
  969. break;
  970. case 3:
  971. if (!memcmp(opts, "uid", 3)) {
  972. data->perms.uid = make_kuid(current_user_ns(), value);
  973. if (!uid_valid(data->perms.uid)) {
  974. pr_err("%s: unmapped value: %lu\n", opts, value);
  975. return -EINVAL;
  976. }
  977. } else if (!memcmp(opts, "gid", 3)) {
  978. data->perms.gid = make_kgid(current_user_ns(), value);
  979. if (!gid_valid(data->perms.gid)) {
  980. pr_err("%s: unmapped value: %lu\n", opts, value);
  981. return -EINVAL;
  982. }
  983. } else {
  984. goto invalid;
  985. }
  986. break;
  987. default:
  988. invalid:
  989. pr_err("%s: invalid option\n", opts);
  990. return -EINVAL;
  991. }
  992. /* Next iteration */
  993. if (!comma)
  994. break;
  995. opts = comma + 1;
  996. }
  997. return 0;
  998. }
  999. /* "mount -t functionfs dev_name /dev/function" ends up here */
  1000. static struct dentry *
  1001. ffs_fs_mount(struct file_system_type *t, int flags,
  1002. const char *dev_name, void *opts)
  1003. {
  1004. struct ffs_sb_fill_data data = {
  1005. .perms = {
  1006. .mode = S_IFREG | 0600,
  1007. .uid = GLOBAL_ROOT_UID,
  1008. .gid = GLOBAL_ROOT_GID,
  1009. },
  1010. .root_mode = S_IFDIR | 0500,
  1011. };
  1012. struct dentry *rv;
  1013. int ret;
  1014. void *ffs_dev;
  1015. struct ffs_data *ffs;
  1016. ENTER();
  1017. ret = ffs_fs_parse_opts(&data, opts);
  1018. if (unlikely(ret < 0))
  1019. return ERR_PTR(ret);
  1020. ffs = ffs_data_new();
  1021. if (unlikely(!ffs))
  1022. return ERR_PTR(-ENOMEM);
  1023. ffs->file_perms = data.perms;
  1024. ffs->dev_name = kstrdup(dev_name, GFP_KERNEL);
  1025. if (unlikely(!ffs->dev_name)) {
  1026. ffs_data_put(ffs);
  1027. return ERR_PTR(-ENOMEM);
  1028. }
  1029. ffs_dev = ffs_acquire_dev(dev_name);
  1030. if (IS_ERR(ffs_dev)) {
  1031. ffs_data_put(ffs);
  1032. return ERR_CAST(ffs_dev);
  1033. }
  1034. ffs->private_data = ffs_dev;
  1035. data.ffs_data = ffs;
  1036. rv = mount_nodev(t, flags, &data, ffs_sb_fill);
  1037. if (IS_ERR(rv) && data.ffs_data) {
  1038. ffs_release_dev(data.ffs_data);
  1039. ffs_data_put(data.ffs_data);
  1040. }
  1041. return rv;
  1042. }
  1043. static void
  1044. ffs_fs_kill_sb(struct super_block *sb)
  1045. {
  1046. ENTER();
  1047. kill_litter_super(sb);
  1048. if (sb->s_fs_info) {
  1049. ffs_release_dev(sb->s_fs_info);
  1050. ffs_data_put(sb->s_fs_info);
  1051. }
  1052. }
  1053. static struct file_system_type ffs_fs_type = {
  1054. .owner = THIS_MODULE,
  1055. .name = "functionfs",
  1056. .mount = ffs_fs_mount,
  1057. .kill_sb = ffs_fs_kill_sb,
  1058. };
  1059. MODULE_ALIAS_FS("functionfs");
  1060. /* Driver's main init/cleanup functions *************************************/
  1061. static int functionfs_init(void)
  1062. {
  1063. int ret;
  1064. ENTER();
  1065. ret = register_filesystem(&ffs_fs_type);
  1066. if (likely(!ret))
  1067. pr_info("file system registered\n");
  1068. else
  1069. pr_err("failed registering file system (%d)\n", ret);
  1070. return ret;
  1071. }
  1072. static void functionfs_cleanup(void)
  1073. {
  1074. ENTER();
  1075. pr_info("unloading\n");
  1076. unregister_filesystem(&ffs_fs_type);
  1077. }
  1078. /* ffs_data and ffs_function construction and destruction code **************/
  1079. static void ffs_data_clear(struct ffs_data *ffs);
  1080. static void ffs_data_reset(struct ffs_data *ffs);
  1081. static void ffs_data_get(struct ffs_data *ffs)
  1082. {
  1083. ENTER();
  1084. atomic_inc(&ffs->ref);
  1085. }
  1086. static void ffs_data_opened(struct ffs_data *ffs)
  1087. {
  1088. ENTER();
  1089. atomic_inc(&ffs->ref);
  1090. atomic_inc(&ffs->opened);
  1091. }
  1092. static void ffs_data_put(struct ffs_data *ffs)
  1093. {
  1094. ENTER();
  1095. if (unlikely(atomic_dec_and_test(&ffs->ref))) {
  1096. pr_info("%s(): freeing\n", __func__);
  1097. ffs_data_clear(ffs);
  1098. BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
  1099. waitqueue_active(&ffs->ep0req_completion.wait));
  1100. kfree(ffs->dev_name);
  1101. kfree(ffs);
  1102. }
  1103. }
  1104. static void ffs_data_closed(struct ffs_data *ffs)
  1105. {
  1106. ENTER();
  1107. if (atomic_dec_and_test(&ffs->opened)) {
  1108. ffs->state = FFS_CLOSING;
  1109. ffs_data_reset(ffs);
  1110. }
  1111. ffs_data_put(ffs);
  1112. }
  1113. static struct ffs_data *ffs_data_new(void)
  1114. {
  1115. struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL);
  1116. if (unlikely(!ffs))
  1117. return NULL;
  1118. ENTER();
  1119. atomic_set(&ffs->ref, 1);
  1120. atomic_set(&ffs->opened, 0);
  1121. ffs->state = FFS_READ_DESCRIPTORS;
  1122. mutex_init(&ffs->mutex);
  1123. spin_lock_init(&ffs->eps_lock);
  1124. init_waitqueue_head(&ffs->ev.waitq);
  1125. init_completion(&ffs->ep0req_completion);
  1126. /* XXX REVISIT need to update it in some places, or do we? */
  1127. ffs->ev.can_stall = 1;
  1128. return ffs;
  1129. }
  1130. static void ffs_data_clear(struct ffs_data *ffs)
  1131. {
  1132. ENTER();
  1133. if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags))
  1134. ffs_closed(ffs);
  1135. BUG_ON(ffs->gadget);
  1136. if (ffs->epfiles)
  1137. ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count);
  1138. kfree(ffs->raw_descs_data);
  1139. kfree(ffs->raw_strings);
  1140. kfree(ffs->stringtabs);
  1141. }
  1142. static void ffs_data_reset(struct ffs_data *ffs)
  1143. {
  1144. ENTER();
  1145. ffs_data_clear(ffs);
  1146. ffs->epfiles = NULL;
  1147. ffs->raw_descs_data = NULL;
  1148. ffs->raw_descs = NULL;
  1149. ffs->raw_strings = NULL;
  1150. ffs->stringtabs = NULL;
  1151. ffs->raw_descs_length = 0;
  1152. ffs->fs_descs_count = 0;
  1153. ffs->hs_descs_count = 0;
  1154. ffs->ss_descs_count = 0;
  1155. ffs->strings_count = 0;
  1156. ffs->interfaces_count = 0;
  1157. ffs->eps_count = 0;
  1158. ffs->ev.count = 0;
  1159. ffs->state = FFS_READ_DESCRIPTORS;
  1160. ffs->setup_state = FFS_NO_SETUP;
  1161. ffs->flags = 0;
  1162. }
  1163. static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
  1164. {
  1165. struct usb_gadget_strings **lang;
  1166. int first_id;
  1167. ENTER();
  1168. if (WARN_ON(ffs->state != FFS_ACTIVE
  1169. || test_and_set_bit(FFS_FL_BOUND, &ffs->flags)))
  1170. return -EBADFD;
  1171. first_id = usb_string_ids_n(cdev, ffs->strings_count);
  1172. if (unlikely(first_id < 0))
  1173. return first_id;
  1174. ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
  1175. if (unlikely(!ffs->ep0req))
  1176. return -ENOMEM;
  1177. ffs->ep0req->complete = ffs_ep0_complete;
  1178. ffs->ep0req->context = ffs;
  1179. lang = ffs->stringtabs;
  1180. if (lang) {
  1181. for (; *lang; ++lang) {
  1182. struct usb_string *str = (*lang)->strings;
  1183. int id = first_id;
  1184. for (; str->s; ++id, ++str)
  1185. str->id = id;
  1186. }
  1187. }
  1188. ffs->gadget = cdev->gadget;
  1189. ffs_data_get(ffs);
  1190. return 0;
  1191. }
  1192. static void functionfs_unbind(struct ffs_data *ffs)
  1193. {
  1194. ENTER();
  1195. if (!WARN_ON(!ffs->gadget)) {
  1196. usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req);
  1197. ffs->ep0req = NULL;
  1198. ffs->gadget = NULL;
  1199. clear_bit(FFS_FL_BOUND, &ffs->flags);
  1200. ffs_data_put(ffs);
  1201. }
  1202. }
  1203. static int ffs_epfiles_create(struct ffs_data *ffs)
  1204. {
  1205. struct ffs_epfile *epfile, *epfiles;
  1206. unsigned i, count;
  1207. ENTER();
  1208. count = ffs->eps_count;
  1209. epfiles = kcalloc(count, sizeof(*epfiles), GFP_KERNEL);
  1210. if (!epfiles)
  1211. return -ENOMEM;
  1212. epfile = epfiles;
  1213. for (i = 1; i <= count; ++i, ++epfile) {
  1214. epfile->ffs = ffs;
  1215. mutex_init(&epfile->mutex);
  1216. init_waitqueue_head(&epfile->wait);
  1217. sprintf(epfiles->name, "ep%u", i);
  1218. if (!unlikely(ffs_sb_create_file(ffs->sb, epfiles->name, epfile,
  1219. &ffs_epfile_operations,
  1220. &epfile->dentry))) {
  1221. ffs_epfiles_destroy(epfiles, i - 1);
  1222. return -ENOMEM;
  1223. }
  1224. }
  1225. ffs->epfiles = epfiles;
  1226. return 0;
  1227. }
  1228. static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
  1229. {
  1230. struct ffs_epfile *epfile = epfiles;
  1231. ENTER();
  1232. for (; count; --count, ++epfile) {
  1233. BUG_ON(mutex_is_locked(&epfile->mutex) ||
  1234. waitqueue_active(&epfile->wait));
  1235. if (epfile->dentry) {
  1236. d_delete(epfile->dentry);
  1237. dput(epfile->dentry);
  1238. epfile->dentry = NULL;
  1239. }
  1240. }
  1241. kfree(epfiles);
  1242. }
  1243. static void ffs_func_eps_disable(struct ffs_function *func)
  1244. {
  1245. struct ffs_ep *ep = func->eps;
  1246. struct ffs_epfile *epfile = func->ffs->epfiles;
  1247. unsigned count = func->ffs->eps_count;
  1248. unsigned long flags;
  1249. spin_lock_irqsave(&func->ffs->eps_lock, flags);
  1250. do {
  1251. /* pending requests get nuked */
  1252. if (likely(ep->ep))
  1253. usb_ep_disable(ep->ep);
  1254. epfile->ep = NULL;
  1255. ++ep;
  1256. ++epfile;
  1257. } while (--count);
  1258. spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
  1259. }
  1260. static int ffs_func_eps_enable(struct ffs_function *func)
  1261. {
  1262. struct ffs_data *ffs = func->ffs;
  1263. struct ffs_ep *ep = func->eps;
  1264. struct ffs_epfile *epfile = ffs->epfiles;
  1265. unsigned count = ffs->eps_count;
  1266. unsigned long flags;
  1267. int ret = 0;
  1268. spin_lock_irqsave(&func->ffs->eps_lock, flags);
  1269. do {
  1270. struct usb_endpoint_descriptor *ds;
  1271. int desc_idx;
  1272. if (ffs->gadget->speed == USB_SPEED_SUPER)
  1273. desc_idx = 2;
  1274. else if (ffs->gadget->speed == USB_SPEED_HIGH)
  1275. desc_idx = 1;
  1276. else
  1277. desc_idx = 0;
  1278. /* fall-back to lower speed if desc missing for current speed */
  1279. do {
  1280. ds = ep->descs[desc_idx];
  1281. } while (!ds && --desc_idx >= 0);
  1282. if (!ds) {
  1283. ret = -EINVAL;
  1284. break;
  1285. }
  1286. ep->ep->driver_data = ep;
  1287. ep->ep->desc = ds;
  1288. ret = usb_ep_enable(ep->ep);
  1289. if (likely(!ret)) {
  1290. epfile->ep = ep;
  1291. epfile->in = usb_endpoint_dir_in(ds);
  1292. epfile->isoc = usb_endpoint_xfer_isoc(ds);
  1293. } else {
  1294. break;
  1295. }
  1296. wake_up(&epfile->wait);
  1297. ++ep;
  1298. ++epfile;
  1299. } while (--count);
  1300. spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
  1301. return ret;
  1302. }
  1303. /* Parsing and building descriptors and strings *****************************/
  1304. /*
  1305. * This validates if data pointed by data is a valid USB descriptor as
  1306. * well as record how many interfaces, endpoints and strings are
  1307. * required by given configuration. Returns address after the
  1308. * descriptor or NULL if data is invalid.
  1309. */
  1310. enum ffs_entity_type {
  1311. FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT
  1312. };
  1313. enum ffs_os_desc_type {
  1314. FFS_OS_DESC, FFS_OS_DESC_EXT_COMPAT, FFS_OS_DESC_EXT_PROP
  1315. };
  1316. typedef int (*ffs_entity_callback)(enum ffs_entity_type entity,
  1317. u8 *valuep,
  1318. struct usb_descriptor_header *desc,
  1319. void *priv);
  1320. typedef int (*ffs_os_desc_callback)(enum ffs_os_desc_type entity,
  1321. struct usb_os_desc_header *h, void *data,
  1322. unsigned len, void *priv);
  1323. static int __must_check ffs_do_single_desc(char *data, unsigned len,
  1324. ffs_entity_callback entity,
  1325. void *priv)
  1326. {
  1327. struct usb_descriptor_header *_ds = (void *)data;
  1328. u8 length;
  1329. int ret;
  1330. ENTER();
  1331. /* At least two bytes are required: length and type */
  1332. if (len < 2) {
  1333. pr_vdebug("descriptor too short\n");
  1334. return -EINVAL;
  1335. }
  1336. /* If we have at least as many bytes as the descriptor takes? */
  1337. length = _ds->bLength;
  1338. if (len < length) {
  1339. pr_vdebug("descriptor longer then available data\n");
  1340. return -EINVAL;
  1341. }
  1342. #define __entity_check_INTERFACE(val) 1
  1343. #define __entity_check_STRING(val) (val)
  1344. #define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
  1345. #define __entity(type, val) do { \
  1346. pr_vdebug("entity " #type "(%02x)\n", (val)); \
  1347. if (unlikely(!__entity_check_ ##type(val))) { \
  1348. pr_vdebug("invalid entity's value\n"); \
  1349. return -EINVAL; \
  1350. } \
  1351. ret = entity(FFS_ ##type, &val, _ds, priv); \
  1352. if (unlikely(ret < 0)) { \
  1353. pr_debug("entity " #type "(%02x); ret = %d\n", \
  1354. (val), ret); \
  1355. return ret; \
  1356. } \
  1357. } while (0)
  1358. /* Parse descriptor depending on type. */
  1359. switch (_ds->bDescriptorType) {
  1360. case USB_DT_DEVICE:
  1361. case USB_DT_CONFIG:
  1362. case USB_DT_STRING:
  1363. case USB_DT_DEVICE_QUALIFIER:
  1364. /* function can't have any of those */
  1365. pr_vdebug("descriptor reserved for gadget: %d\n",
  1366. _ds->bDescriptorType);
  1367. return -EINVAL;
  1368. case USB_DT_INTERFACE: {
  1369. struct usb_interface_descriptor *ds = (void *)_ds;
  1370. pr_vdebug("interface descriptor\n");
  1371. if (length != sizeof *ds)
  1372. goto inv_length;
  1373. __entity(INTERFACE, ds->bInterfaceNumber);
  1374. if (ds->iInterface)
  1375. __entity(STRING, ds->iInterface);
  1376. }
  1377. break;
  1378. case USB_DT_ENDPOINT: {
  1379. struct usb_endpoint_descriptor *ds = (void *)_ds;
  1380. pr_vdebug("endpoint descriptor\n");
  1381. if (length != USB_DT_ENDPOINT_SIZE &&
  1382. length != USB_DT_ENDPOINT_AUDIO_SIZE)
  1383. goto inv_length;
  1384. __entity(ENDPOINT, ds->bEndpointAddress);
  1385. }
  1386. break;
  1387. case HID_DT_HID:
  1388. pr_vdebug("hid descriptor\n");
  1389. if (length != sizeof(struct hid_descriptor))
  1390. goto inv_length;
  1391. break;
  1392. case USB_DT_OTG:
  1393. if (length != sizeof(struct usb_otg_descriptor))
  1394. goto inv_length;
  1395. break;
  1396. case USB_DT_INTERFACE_ASSOCIATION: {
  1397. struct usb_interface_assoc_descriptor *ds = (void *)_ds;
  1398. pr_vdebug("interface association descriptor\n");
  1399. if (length != sizeof *ds)
  1400. goto inv_length;
  1401. if (ds->iFunction)
  1402. __entity(STRING, ds->iFunction);
  1403. }
  1404. break;
  1405. case USB_DT_SS_ENDPOINT_COMP:
  1406. pr_vdebug("EP SS companion descriptor\n");
  1407. if (length != sizeof(struct usb_ss_ep_comp_descriptor))
  1408. goto inv_length;
  1409. break;
  1410. case USB_DT_OTHER_SPEED_CONFIG:
  1411. case USB_DT_INTERFACE_POWER:
  1412. case USB_DT_DEBUG:
  1413. case USB_DT_SECURITY:
  1414. case USB_DT_CS_RADIO_CONTROL:
  1415. /* TODO */
  1416. pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType);
  1417. return -EINVAL;
  1418. default:
  1419. /* We should never be here */
  1420. pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType);
  1421. return -EINVAL;
  1422. inv_length:
  1423. pr_vdebug("invalid length: %d (descriptor %d)\n",
  1424. _ds->bLength, _ds->bDescriptorType);
  1425. return -EINVAL;
  1426. }
  1427. #undef __entity
  1428. #undef __entity_check_DESCRIPTOR
  1429. #undef __entity_check_INTERFACE
  1430. #undef __entity_check_STRING
  1431. #undef __entity_check_ENDPOINT
  1432. return length;
  1433. }
  1434. static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
  1435. ffs_entity_callback entity, void *priv)
  1436. {
  1437. const unsigned _len = len;
  1438. unsigned long num = 0;
  1439. ENTER();
  1440. for (;;) {
  1441. int ret;
  1442. if (num == count)
  1443. data = NULL;
  1444. /* Record "descriptor" entity */
  1445. ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv);
  1446. if (unlikely(ret < 0)) {
  1447. pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n",
  1448. num, ret);
  1449. return ret;
  1450. }
  1451. if (!data)
  1452. return _len - len;
  1453. ret = ffs_do_single_desc(data, len, entity, priv);
  1454. if (unlikely(ret < 0)) {
  1455. pr_debug("%s returns %d\n", __func__, ret);
  1456. return ret;
  1457. }
  1458. len -= ret;
  1459. data += ret;
  1460. ++num;
  1461. }
  1462. }
  1463. static int __ffs_data_do_entity(enum ffs_entity_type type,
  1464. u8 *valuep, struct usb_descriptor_header *desc,
  1465. void *priv)
  1466. {
  1467. struct ffs_data *ffs = priv;
  1468. ENTER();
  1469. switch (type) {
  1470. case FFS_DESCRIPTOR:
  1471. break;
  1472. case FFS_INTERFACE:
  1473. /*
  1474. * Interfaces are indexed from zero so if we
  1475. * encountered interface "n" then there are at least
  1476. * "n+1" interfaces.
  1477. */
  1478. if (*valuep >= ffs->interfaces_count)
  1479. ffs->interfaces_count = *valuep + 1;
  1480. break;
  1481. case FFS_STRING:
  1482. /*
  1483. * Strings are indexed from 1 (0 is magic ;) reserved
  1484. * for languages list or some such)
  1485. */
  1486. if (*valuep > ffs->strings_count)
  1487. ffs->strings_count = *valuep;
  1488. break;
  1489. case FFS_ENDPOINT:
  1490. /* Endpoints are indexed from 1 as well. */
  1491. if ((*valuep & USB_ENDPOINT_NUMBER_MASK) > ffs->eps_count)
  1492. ffs->eps_count = (*valuep & USB_ENDPOINT_NUMBER_MASK);
  1493. break;
  1494. }
  1495. return 0;
  1496. }
  1497. static int __ffs_do_os_desc_header(enum ffs_os_desc_type *next_type,
  1498. struct usb_os_desc_header *desc)
  1499. {
  1500. u16 bcd_version = le16_to_cpu(desc->bcdVersion);
  1501. u16 w_index = le16_to_cpu(desc->wIndex);
  1502. if (bcd_version != 1) {
  1503. pr_vdebug("unsupported os descriptors version: %d",
  1504. bcd_version);
  1505. return -EINVAL;
  1506. }
  1507. switch (w_index) {
  1508. case 0x4:
  1509. *next_type = FFS_OS_DESC_EXT_COMPAT;
  1510. break;
  1511. case 0x5:
  1512. *next_type = FFS_OS_DESC_EXT_PROP;
  1513. break;
  1514. default:
  1515. pr_vdebug("unsupported os descriptor type: %d", w_index);
  1516. return -EINVAL;
  1517. }
  1518. return sizeof(*desc);
  1519. }
  1520. /*
  1521. * Process all extended compatibility/extended property descriptors
  1522. * of a feature descriptor
  1523. */
  1524. static int __must_check ffs_do_single_os_desc(char *data, unsigned len,
  1525. enum ffs_os_desc_type type,
  1526. u16 feature_count,
  1527. ffs_os_desc_callback entity,
  1528. void *priv,
  1529. struct usb_os_desc_header *h)
  1530. {
  1531. int ret;
  1532. const unsigned _len = len;
  1533. ENTER();
  1534. /* loop over all ext compat/ext prop descriptors */
  1535. while (feature_count--) {
  1536. ret = entity(type, h, data, len, priv);
  1537. if (unlikely(ret < 0)) {
  1538. pr_debug("bad OS descriptor, type: %d\n", type);
  1539. return ret;
  1540. }
  1541. data += ret;
  1542. len -= ret;
  1543. }
  1544. return _len - len;
  1545. }
  1546. /* Process a number of complete Feature Descriptors (Ext Compat or Ext Prop) */
  1547. static int __must_check ffs_do_os_descs(unsigned count,
  1548. char *data, unsigned len,
  1549. ffs_os_desc_callback entity, void *priv)
  1550. {
  1551. const unsigned _len = len;
  1552. unsigned long num = 0;
  1553. ENTER();
  1554. for (num = 0; num < count; ++num) {
  1555. int ret;
  1556. enum ffs_os_desc_type type;
  1557. u16 feature_count;
  1558. struct usb_os_desc_header *desc = (void *)data;
  1559. if (len < sizeof(*desc))
  1560. return -EINVAL;
  1561. /*
  1562. * Record "descriptor" entity.
  1563. * Process dwLength, bcdVersion, wIndex, get b/wCount.
  1564. * Move the data pointer to the beginning of extended
  1565. * compatibilities proper or extended properties proper
  1566. * portions of the data
  1567. */
  1568. if (le32_to_cpu(desc->dwLength) > len)
  1569. return -EINVAL;
  1570. ret = __ffs_do_os_desc_header(&type, desc);
  1571. if (unlikely(ret < 0)) {
  1572. pr_debug("entity OS_DESCRIPTOR(%02lx); ret = %d\n",
  1573. num, ret);
  1574. return ret;
  1575. }
  1576. /*
  1577. * 16-bit hex "?? 00" Little Endian looks like 8-bit hex "??"
  1578. */
  1579. feature_count = le16_to_cpu(desc->wCount);
  1580. if (type == FFS_OS_DESC_EXT_COMPAT &&
  1581. (feature_count > 255 || desc->Reserved))
  1582. return -EINVAL;
  1583. len -= ret;
  1584. data += ret;
  1585. /*
  1586. * Process all function/property descriptors
  1587. * of this Feature Descriptor
  1588. */
  1589. ret = ffs_do_single_os_desc(data, len, type,
  1590. feature_count, entity, priv, desc);
  1591. if (unlikely(ret < 0)) {
  1592. pr_debug("%s returns %d\n", __func__, ret);
  1593. return ret;
  1594. }
  1595. len -= ret;
  1596. data += ret;
  1597. }
  1598. return _len - len;
  1599. }
  1600. /**
  1601. * Validate contents of the buffer from userspace related to OS descriptors.
  1602. */
  1603. static int __ffs_data_do_os_desc(enum ffs_os_desc_type type,
  1604. struct usb_os_desc_header *h, void *data,
  1605. unsigned len, void *priv)
  1606. {
  1607. struct ffs_data *ffs = priv;
  1608. u8 length;
  1609. ENTER();
  1610. switch (type) {
  1611. case FFS_OS_DESC_EXT_COMPAT: {
  1612. struct usb_ext_compat_desc *d = data;
  1613. int i;
  1614. if (len < sizeof(*d) ||
  1615. d->bFirstInterfaceNumber >= ffs->interfaces_count ||
  1616. d->Reserved1)
  1617. return -EINVAL;
  1618. for (i = 0; i < ARRAY_SIZE(d->Reserved2); ++i)
  1619. if (d->Reserved2[i])
  1620. return -EINVAL;
  1621. length = sizeof(struct usb_ext_compat_desc);
  1622. }
  1623. break;
  1624. case FFS_OS_DESC_EXT_PROP: {
  1625. struct usb_ext_prop_desc *d = data;
  1626. u32 type, pdl;
  1627. u16 pnl;
  1628. if (len < sizeof(*d) || h->interface >= ffs->interfaces_count)
  1629. return -EINVAL;
  1630. length = le32_to_cpu(d->dwSize);
  1631. type = le32_to_cpu(d->dwPropertyDataType);
  1632. if (type < USB_EXT_PROP_UNICODE ||
  1633. type > USB_EXT_PROP_UNICODE_MULTI) {
  1634. pr_vdebug("unsupported os descriptor property type: %d",
  1635. type);
  1636. return -EINVAL;
  1637. }
  1638. pnl = le16_to_cpu(d->wPropertyNameLength);
  1639. pdl = le32_to_cpu(*(u32 *)((u8 *)data + 10 + pnl));
  1640. if (length != 14 + pnl + pdl) {
  1641. pr_vdebug("invalid os descriptor length: %d pnl:%d pdl:%d (descriptor %d)\n",
  1642. length, pnl, pdl, type);
  1643. return -EINVAL;
  1644. }
  1645. ++ffs->ms_os_descs_ext_prop_count;
  1646. /* property name reported to the host as "WCHAR"s */
  1647. ffs->ms_os_descs_ext_prop_name_len += pnl * 2;
  1648. ffs->ms_os_descs_ext_prop_data_len += pdl;
  1649. }
  1650. break;
  1651. default:
  1652. pr_vdebug("unknown descriptor: %d\n", type);
  1653. return -EINVAL;
  1654. }
  1655. return length;
  1656. }
  1657. static int __ffs_data_got_descs(struct ffs_data *ffs,
  1658. char *const _data, size_t len)
  1659. {
  1660. char *data = _data, *raw_descs;
  1661. unsigned os_descs_count = 0, counts[3], flags;
  1662. int ret = -EINVAL, i;
  1663. ENTER();
  1664. if (get_unaligned_le32(data + 4) != len)
  1665. goto error;
  1666. switch (get_unaligned_le32(data)) {
  1667. case FUNCTIONFS_DESCRIPTORS_MAGIC:
  1668. flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC;
  1669. data += 8;
  1670. len -= 8;
  1671. break;
  1672. case FUNCTIONFS_DESCRIPTORS_MAGIC_V2:
  1673. flags = get_unaligned_le32(data + 8);
  1674. if (flags & ~(FUNCTIONFS_HAS_FS_DESC |
  1675. FUNCTIONFS_HAS_HS_DESC |
  1676. FUNCTIONFS_HAS_SS_DESC |
  1677. FUNCTIONFS_HAS_MS_OS_DESC)) {
  1678. ret = -ENOSYS;
  1679. goto error;
  1680. }
  1681. data += 12;
  1682. len -= 12;
  1683. break;
  1684. default:
  1685. goto error;
  1686. }
  1687. /* Read fs_count, hs_count and ss_count (if present) */
  1688. for (i = 0; i < 3; ++i) {
  1689. if (!(flags & (1 << i))) {
  1690. counts[i] = 0;
  1691. } else if (len < 4) {
  1692. goto error;
  1693. } else {
  1694. counts[i] = get_unaligned_le32(data);
  1695. data += 4;
  1696. len -= 4;
  1697. }
  1698. }
  1699. if (flags & (1 << i)) {
  1700. os_descs_count = get_unaligned_le32(data);
  1701. data += 4;
  1702. len -= 4;
  1703. };
  1704. /* Read descriptors */
  1705. raw_descs = data;
  1706. for (i = 0; i < 3; ++i) {
  1707. if (!counts[i])
  1708. continue;
  1709. ret = ffs_do_descs(counts[i], data, len,
  1710. __ffs_data_do_entity, ffs);
  1711. if (ret < 0)
  1712. goto error;
  1713. data += ret;
  1714. len -= ret;
  1715. }
  1716. if (os_descs_count) {
  1717. ret = ffs_do_os_descs(os_descs_count, data, len,
  1718. __ffs_data_do_os_desc, ffs);
  1719. if (ret < 0)
  1720. goto error;
  1721. data += ret;
  1722. len -= ret;
  1723. }
  1724. if (raw_descs == data || len) {
  1725. ret = -EINVAL;
  1726. goto error;
  1727. }
  1728. ffs->raw_descs_data = _data;
  1729. ffs->raw_descs = raw_descs;
  1730. ffs->raw_descs_length = data - raw_descs;
  1731. ffs->fs_descs_count = counts[0];
  1732. ffs->hs_descs_count = counts[1];
  1733. ffs->ss_descs_count = counts[2];
  1734. ffs->ms_os_descs_count = os_descs_count;
  1735. return 0;
  1736. error:
  1737. kfree(_data);
  1738. return ret;
  1739. }
  1740. static int __ffs_data_got_strings(struct ffs_data *ffs,
  1741. char *const _data, size_t len)
  1742. {
  1743. u32 str_count, needed_count, lang_count;
  1744. struct usb_gadget_strings **stringtabs, *t;
  1745. struct usb_string *strings, *s;
  1746. const char *data = _data;
  1747. ENTER();
  1748. if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC ||
  1749. get_unaligned_le32(data + 4) != len))
  1750. goto error;
  1751. str_count = get_unaligned_le32(data + 8);
  1752. lang_count = get_unaligned_le32(data + 12);
  1753. /* if one is zero the other must be zero */
  1754. if (unlikely(!str_count != !lang_count))
  1755. goto error;
  1756. /* Do we have at least as many strings as descriptors need? */
  1757. needed_count = ffs->strings_count;
  1758. if (unlikely(str_count < needed_count))
  1759. goto error;
  1760. /*
  1761. * If we don't need any strings just return and free all
  1762. * memory.
  1763. */
  1764. if (!needed_count) {
  1765. kfree(_data);
  1766. return 0;
  1767. }
  1768. /* Allocate everything in one chunk so there's less maintenance. */
  1769. {
  1770. unsigned i = 0;
  1771. vla_group(d);
  1772. vla_item(d, struct usb_gadget_strings *, stringtabs,
  1773. lang_count + 1);
  1774. vla_item(d, struct usb_gadget_strings, stringtab, lang_count);
  1775. vla_item(d, struct usb_string, strings,
  1776. lang_count*(needed_count+1));
  1777. char *vlabuf = kmalloc(vla_group_size(d), GFP_KERNEL);
  1778. if (unlikely(!vlabuf)) {
  1779. kfree(_data);
  1780. return -ENOMEM;
  1781. }
  1782. /* Initialize the VLA pointers */
  1783. stringtabs = vla_ptr(vlabuf, d, stringtabs);
  1784. t = vla_ptr(vlabuf, d, stringtab);
  1785. i = lang_count;
  1786. do {
  1787. *stringtabs++ = t++;
  1788. } while (--i);
  1789. *stringtabs = NULL;
  1790. /* stringtabs = vlabuf = d_stringtabs for later kfree */
  1791. stringtabs = vla_ptr(vlabuf, d, stringtabs);
  1792. t = vla_ptr(vlabuf, d, stringtab);
  1793. s = vla_ptr(vlabuf, d, strings);
  1794. strings = s;
  1795. }
  1796. /* For each language */
  1797. data += 16;
  1798. len -= 16;
  1799. do { /* lang_count > 0 so we can use do-while */
  1800. unsigned needed = needed_count;
  1801. if (unlikely(len < 3))
  1802. goto error_free;
  1803. t->language = get_unaligned_le16(data);
  1804. t->strings = s;
  1805. ++t;
  1806. data += 2;
  1807. len -= 2;
  1808. /* For each string */
  1809. do { /* str_count > 0 so we can use do-while */
  1810. size_t length = strnlen(data, len);
  1811. if (unlikely(length == len))
  1812. goto error_free;
  1813. /*
  1814. * User may provide more strings then we need,
  1815. * if that's the case we simply ignore the
  1816. * rest
  1817. */
  1818. if (likely(needed)) {
  1819. /*
  1820. * s->id will be set while adding
  1821. * function to configuration so for
  1822. * now just leave garbage here.
  1823. */
  1824. s->s = data;
  1825. --needed;
  1826. ++s;
  1827. }
  1828. data += length + 1;
  1829. len -= length + 1;
  1830. } while (--str_count);
  1831. s->id = 0; /* terminator */
  1832. s->s = NULL;
  1833. ++s;
  1834. } while (--lang_count);
  1835. /* Some garbage left? */
  1836. if (unlikely(len))
  1837. goto error_free;
  1838. /* Done! */
  1839. ffs->stringtabs = stringtabs;
  1840. ffs->raw_strings = _data;
  1841. return 0;
  1842. error_free:
  1843. kfree(stringtabs);
  1844. error:
  1845. kfree(_data);
  1846. return -EINVAL;
  1847. }
  1848. /* Events handling and management *******************************************/
  1849. static void __ffs_event_add(struct ffs_data *ffs,
  1850. enum usb_functionfs_event_type type)
  1851. {
  1852. enum usb_functionfs_event_type rem_type1, rem_type2 = type;
  1853. int neg = 0;
  1854. /*
  1855. * Abort any unhandled setup
  1856. *
  1857. * We do not need to worry about some cmpxchg() changing value
  1858. * of ffs->setup_state without holding the lock because when
  1859. * state is FFS_SETUP_PENDING cmpxchg() in several places in
  1860. * the source does nothing.
  1861. */
  1862. if (ffs->setup_state == FFS_SETUP_PENDING)
  1863. ffs->setup_state = FFS_SETUP_CANCELLED;
  1864. switch (type) {
  1865. case FUNCTIONFS_RESUME:
  1866. rem_type2 = FUNCTIONFS_SUSPEND;
  1867. /* FALL THROUGH */
  1868. case FUNCTIONFS_SUSPEND:
  1869. case FUNCTIONFS_SETUP:
  1870. rem_type1 = type;
  1871. /* Discard all similar events */
  1872. break;
  1873. case FUNCTIONFS_BIND:
  1874. case FUNCTIONFS_UNBIND:
  1875. case FUNCTIONFS_DISABLE:
  1876. case FUNCTIONFS_ENABLE:
  1877. /* Discard everything other then power management. */
  1878. rem_type1 = FUNCTIONFS_SUSPEND;
  1879. rem_type2 = FUNCTIONFS_RESUME;
  1880. neg = 1;
  1881. break;
  1882. default:
  1883. BUG();
  1884. }
  1885. {
  1886. u8 *ev = ffs->ev.types, *out = ev;
  1887. unsigned n = ffs->ev.count;
  1888. for (; n; --n, ++ev)
  1889. if ((*ev == rem_type1 || *ev == rem_type2) == neg)
  1890. *out++ = *ev;
  1891. else
  1892. pr_vdebug("purging event %d\n", *ev);
  1893. ffs->ev.count = out - ffs->ev.types;
  1894. }
  1895. pr_vdebug("adding event %d\n", type);
  1896. ffs->ev.types[ffs->ev.count++] = type;
  1897. wake_up_locked(&ffs->ev.waitq);
  1898. }
  1899. static void ffs_event_add(struct ffs_data *ffs,
  1900. enum usb_functionfs_event_type type)
  1901. {
  1902. unsigned long flags;
  1903. spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
  1904. __ffs_event_add(ffs, type);
  1905. spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
  1906. }
  1907. /* Bind/unbind USB function hooks *******************************************/
  1908. static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
  1909. struct usb_descriptor_header *desc,
  1910. void *priv)
  1911. {
  1912. struct usb_endpoint_descriptor *ds = (void *)desc;
  1913. struct ffs_function *func = priv;
  1914. struct ffs_ep *ffs_ep;
  1915. unsigned ep_desc_id, idx;
  1916. static const char *speed_names[] = { "full", "high", "super" };
  1917. if (type != FFS_DESCRIPTOR)
  1918. return 0;
  1919. /*
  1920. * If ss_descriptors is not NULL, we are reading super speed
  1921. * descriptors; if hs_descriptors is not NULL, we are reading high
  1922. * speed descriptors; otherwise, we are reading full speed
  1923. * descriptors.
  1924. */
  1925. if (func->function.ss_descriptors) {
  1926. ep_desc_id = 2;
  1927. func->function.ss_descriptors[(long)valuep] = desc;
  1928. } else if (func->function.hs_descriptors) {
  1929. ep_desc_id = 1;
  1930. func->function.hs_descriptors[(long)valuep] = desc;
  1931. } else {
  1932. ep_desc_id = 0;
  1933. func->function.fs_descriptors[(long)valuep] = desc;
  1934. }
  1935. if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
  1936. return 0;
  1937. idx = (ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) - 1;
  1938. ffs_ep = func->eps + idx;
  1939. if (unlikely(ffs_ep->descs[ep_desc_id])) {
  1940. pr_err("two %sspeed descriptors for EP %d\n",
  1941. speed_names[ep_desc_id],
  1942. ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
  1943. return -EINVAL;
  1944. }
  1945. ffs_ep->descs[ep_desc_id] = ds;
  1946. ffs_dump_mem(": Original ep desc", ds, ds->bLength);
  1947. if (ffs_ep->ep) {
  1948. ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress;
  1949. if (!ds->wMaxPacketSize)
  1950. ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize;
  1951. } else {
  1952. struct usb_request *req;
  1953. struct usb_ep *ep;
  1954. pr_vdebug("autoconfig\n");
  1955. ep = usb_ep_autoconfig(func->gadget, ds);
  1956. if (unlikely(!ep))
  1957. return -ENOTSUPP;
  1958. ep->driver_data = func->eps + idx;
  1959. req = usb_ep_alloc_request(ep, GFP_KERNEL);
  1960. if (unlikely(!req))
  1961. return -ENOMEM;
  1962. ffs_ep->ep = ep;
  1963. ffs_ep->req = req;
  1964. func->eps_revmap[ds->bEndpointAddress &
  1965. USB_ENDPOINT_NUMBER_MASK] = idx + 1;
  1966. }
  1967. ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength);
  1968. return 0;
  1969. }
  1970. static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
  1971. struct usb_descriptor_header *desc,
  1972. void *priv)
  1973. {
  1974. struct ffs_function *func = priv;
  1975. unsigned idx;
  1976. u8 newValue;
  1977. switch (type) {
  1978. default:
  1979. case FFS_DESCRIPTOR:
  1980. /* Handled in previous pass by __ffs_func_bind_do_descs() */
  1981. return 0;
  1982. case FFS_INTERFACE:
  1983. idx = *valuep;
  1984. if (func->interfaces_nums[idx] < 0) {
  1985. int id = usb_interface_id(func->conf, &func->function);
  1986. if (unlikely(id < 0))
  1987. return id;
  1988. func->interfaces_nums[idx] = id;
  1989. }
  1990. newValue = func->interfaces_nums[idx];
  1991. break;
  1992. case FFS_STRING:
  1993. /* String' IDs are allocated when fsf_data is bound to cdev */
  1994. newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id;
  1995. break;
  1996. case FFS_ENDPOINT:
  1997. /*
  1998. * USB_DT_ENDPOINT are handled in
  1999. * __ffs_func_bind_do_descs().
  2000. */
  2001. if (desc->bDescriptorType == USB_DT_ENDPOINT)
  2002. return 0;
  2003. idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1;
  2004. if (unlikely(!func->eps[idx].ep))
  2005. return -EINVAL;
  2006. {
  2007. struct usb_endpoint_descriptor **descs;
  2008. descs = func->eps[idx].descs;
  2009. newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress;
  2010. }
  2011. break;
  2012. }
  2013. pr_vdebug("%02x -> %02x\n", *valuep, newValue);
  2014. *valuep = newValue;
  2015. return 0;
  2016. }
  2017. static int __ffs_func_bind_do_os_desc(enum ffs_os_desc_type type,
  2018. struct usb_os_desc_header *h, void *data,
  2019. unsigned len, void *priv)
  2020. {
  2021. struct ffs_function *func = priv;
  2022. u8 length = 0;
  2023. switch (type) {
  2024. case FFS_OS_DESC_EXT_COMPAT: {
  2025. struct usb_ext_compat_desc *desc = data;
  2026. struct usb_os_desc_table *t;
  2027. t = &func->function.os_desc_table[desc->bFirstInterfaceNumber];
  2028. t->if_id = func->interfaces_nums[desc->bFirstInterfaceNumber];
  2029. memcpy(t->os_desc->ext_compat_id, &desc->CompatibleID,
  2030. ARRAY_SIZE(desc->CompatibleID) +
  2031. ARRAY_SIZE(desc->SubCompatibleID));
  2032. length = sizeof(*desc);
  2033. }
  2034. break;
  2035. case FFS_OS_DESC_EXT_PROP: {
  2036. struct usb_ext_prop_desc *desc = data;
  2037. struct usb_os_desc_table *t;
  2038. struct usb_os_desc_ext_prop *ext_prop;
  2039. char *ext_prop_name;
  2040. char *ext_prop_data;
  2041. t = &func->function.os_desc_table[h->interface];
  2042. t->if_id = func->interfaces_nums[h->interface];
  2043. ext_prop = func->ffs->ms_os_descs_ext_prop_avail;
  2044. func->ffs->ms_os_descs_ext_prop_avail += sizeof(*ext_prop);
  2045. ext_prop->type = le32_to_cpu(desc->dwPropertyDataType);
  2046. ext_prop->name_len = le16_to_cpu(desc->wPropertyNameLength);
  2047. ext_prop->data_len = le32_to_cpu(*(u32 *)
  2048. usb_ext_prop_data_len_ptr(data, ext_prop->name_len));
  2049. length = ext_prop->name_len + ext_prop->data_len + 14;
  2050. ext_prop_name = func->ffs->ms_os_descs_ext_prop_name_avail;
  2051. func->ffs->ms_os_descs_ext_prop_name_avail +=
  2052. ext_prop->name_len;
  2053. ext_prop_data = func->ffs->ms_os_descs_ext_prop_data_avail;
  2054. func->ffs->ms_os_descs_ext_prop_data_avail +=
  2055. ext_prop->data_len;
  2056. memcpy(ext_prop_data,
  2057. usb_ext_prop_data_ptr(data, ext_prop->name_len),
  2058. ext_prop->data_len);
  2059. /* unicode data reported to the host as "WCHAR"s */
  2060. switch (ext_prop->type) {
  2061. case USB_EXT_PROP_UNICODE:
  2062. case USB_EXT_PROP_UNICODE_ENV:
  2063. case USB_EXT_PROP_UNICODE_LINK:
  2064. case USB_EXT_PROP_UNICODE_MULTI:
  2065. ext_prop->data_len *= 2;
  2066. break;
  2067. }
  2068. ext_prop->data = ext_prop_data;
  2069. memcpy(ext_prop_name, usb_ext_prop_name_ptr(data),
  2070. ext_prop->name_len);
  2071. /* property name reported to the host as "WCHAR"s */
  2072. ext_prop->name_len *= 2;
  2073. ext_prop->name = ext_prop_name;
  2074. t->os_desc->ext_prop_len +=
  2075. ext_prop->name_len + ext_prop->data_len + 14;
  2076. ++t->os_desc->ext_prop_count;
  2077. list_add_tail(&ext_prop->entry, &t->os_desc->ext_prop);
  2078. }
  2079. break;
  2080. default:
  2081. pr_vdebug("unknown descriptor: %d\n", type);
  2082. }
  2083. return length;
  2084. }
  2085. static inline struct f_fs_opts *ffs_do_functionfs_bind(struct usb_function *f,
  2086. struct usb_configuration *c)
  2087. {
  2088. struct ffs_function *func = ffs_func_from_usb(f);
  2089. struct f_fs_opts *ffs_opts =
  2090. container_of(f->fi, struct f_fs_opts, func_inst);
  2091. int ret;
  2092. ENTER();
  2093. /*
  2094. * Legacy gadget triggers binding in functionfs_ready_callback,
  2095. * which already uses locking; taking the same lock here would
  2096. * cause a deadlock.
  2097. *
  2098. * Configfs-enabled gadgets however do need ffs_dev_lock.
  2099. */
  2100. if (!ffs_opts->no_configfs)
  2101. ffs_dev_lock();
  2102. ret = ffs_opts->dev->desc_ready ? 0 : -ENODEV;
  2103. func->ffs = ffs_opts->dev->ffs_data;
  2104. if (!ffs_opts->no_configfs)
  2105. ffs_dev_unlock();
  2106. if (ret)
  2107. return ERR_PTR(ret);
  2108. func->conf = c;
  2109. func->gadget = c->cdev->gadget;
  2110. ffs_data_get(func->ffs);
  2111. /*
  2112. * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
  2113. * configurations are bound in sequence with list_for_each_entry,
  2114. * in each configuration its functions are bound in sequence
  2115. * with list_for_each_entry, so we assume no race condition
  2116. * with regard to ffs_opts->bound access
  2117. */
  2118. if (!ffs_opts->refcnt) {
  2119. ret = functionfs_bind(func->ffs, c->cdev);
  2120. if (ret)
  2121. return ERR_PTR(ret);
  2122. }
  2123. ffs_opts->refcnt++;
  2124. func->function.strings = func->ffs->stringtabs;
  2125. return ffs_opts;
  2126. }
  2127. static int _ffs_func_bind(struct usb_configuration *c,
  2128. struct usb_function *f)
  2129. {
  2130. struct ffs_function *func = ffs_func_from_usb(f);
  2131. struct ffs_data *ffs = func->ffs;
  2132. const int full = !!func->ffs->fs_descs_count;
  2133. const int high = gadget_is_dualspeed(func->gadget) &&
  2134. func->ffs->hs_descs_count;
  2135. const int super = gadget_is_superspeed(func->gadget) &&
  2136. func->ffs->ss_descs_count;
  2137. int fs_len, hs_len, ss_len, ret, i;
  2138. /* Make it a single chunk, less management later on */
  2139. vla_group(d);
  2140. vla_item_with_sz(d, struct ffs_ep, eps, ffs->eps_count);
  2141. vla_item_with_sz(d, struct usb_descriptor_header *, fs_descs,
  2142. full ? ffs->fs_descs_count + 1 : 0);
  2143. vla_item_with_sz(d, struct usb_descriptor_header *, hs_descs,
  2144. high ? ffs->hs_descs_count + 1 : 0);
  2145. vla_item_with_sz(d, struct usb_descriptor_header *, ss_descs,
  2146. super ? ffs->ss_descs_count + 1 : 0);
  2147. vla_item_with_sz(d, short, inums, ffs->interfaces_count);
  2148. vla_item_with_sz(d, struct usb_os_desc_table, os_desc_table,
  2149. c->cdev->use_os_string ? ffs->interfaces_count : 0);
  2150. vla_item_with_sz(d, char[16], ext_compat,
  2151. c->cdev->use_os_string ? ffs->interfaces_count : 0);
  2152. vla_item_with_sz(d, struct usb_os_desc, os_desc,
  2153. c->cdev->use_os_string ? ffs->interfaces_count : 0);
  2154. vla_item_with_sz(d, struct usb_os_desc_ext_prop, ext_prop,
  2155. ffs->ms_os_descs_ext_prop_count);
  2156. vla_item_with_sz(d, char, ext_prop_name,
  2157. ffs->ms_os_descs_ext_prop_name_len);
  2158. vla_item_with_sz(d, char, ext_prop_data,
  2159. ffs->ms_os_descs_ext_prop_data_len);
  2160. vla_item_with_sz(d, char, raw_descs, ffs->raw_descs_length);
  2161. char *vlabuf;
  2162. ENTER();
  2163. /* Has descriptors only for speeds gadget does not support */
  2164. if (unlikely(!(full | high | super)))
  2165. return -ENOTSUPP;
  2166. /* Allocate a single chunk, less management later on */
  2167. vlabuf = kzalloc(vla_group_size(d), GFP_KERNEL);
  2168. if (unlikely(!vlabuf))
  2169. return -ENOMEM;
  2170. ffs->ms_os_descs_ext_prop_avail = vla_ptr(vlabuf, d, ext_prop);
  2171. ffs->ms_os_descs_ext_prop_name_avail =
  2172. vla_ptr(vlabuf, d, ext_prop_name);
  2173. ffs->ms_os_descs_ext_prop_data_avail =
  2174. vla_ptr(vlabuf, d, ext_prop_data);
  2175. /* Copy descriptors */
  2176. memcpy(vla_ptr(vlabuf, d, raw_descs), ffs->raw_descs,
  2177. ffs->raw_descs_length);
  2178. memset(vla_ptr(vlabuf, d, inums), 0xff, d_inums__sz);
  2179. for (ret = ffs->eps_count; ret; --ret) {
  2180. struct ffs_ep *ptr;
  2181. ptr = vla_ptr(vlabuf, d, eps);
  2182. ptr[ret].num = -1;
  2183. }
  2184. /* Save pointers
  2185. * d_eps == vlabuf, func->eps used to kfree vlabuf later
  2186. */
  2187. func->eps = vla_ptr(vlabuf, d, eps);
  2188. func->interfaces_nums = vla_ptr(vlabuf, d, inums);
  2189. /*
  2190. * Go through all the endpoint descriptors and allocate
  2191. * endpoints first, so that later we can rewrite the endpoint
  2192. * numbers without worrying that it may be described later on.
  2193. */
  2194. if (likely(full)) {
  2195. func->function.fs_descriptors = vla_ptr(vlabuf, d, fs_descs);
  2196. fs_len = ffs_do_descs(ffs->fs_descs_count,
  2197. vla_ptr(vlabuf, d, raw_descs),
  2198. d_raw_descs__sz,
  2199. __ffs_func_bind_do_descs, func);
  2200. if (unlikely(fs_len < 0)) {
  2201. ret = fs_len;
  2202. goto error;
  2203. }
  2204. } else {
  2205. fs_len = 0;
  2206. }
  2207. if (likely(high)) {
  2208. func->function.hs_descriptors = vla_ptr(vlabuf, d, hs_descs);
  2209. hs_len = ffs_do_descs(ffs->hs_descs_count,
  2210. vla_ptr(vlabuf, d, raw_descs) + fs_len,
  2211. d_raw_descs__sz - fs_len,
  2212. __ffs_func_bind_do_descs, func);
  2213. if (unlikely(hs_len < 0)) {
  2214. ret = hs_len;
  2215. goto error;
  2216. }
  2217. } else {
  2218. hs_len = 0;
  2219. }
  2220. if (likely(super)) {
  2221. func->function.ss_descriptors = vla_ptr(vlabuf, d, ss_descs);
  2222. ss_len = ffs_do_descs(ffs->ss_descs_count,
  2223. vla_ptr(vlabuf, d, raw_descs) + fs_len + hs_len,
  2224. d_raw_descs__sz - fs_len - hs_len,
  2225. __ffs_func_bind_do_descs, func);
  2226. if (unlikely(ss_len < 0)) {
  2227. ret = ss_len;
  2228. goto error;
  2229. }
  2230. } else {
  2231. ss_len = 0;
  2232. }
  2233. /*
  2234. * Now handle interface numbers allocation and interface and
  2235. * endpoint numbers rewriting. We can do that in one go
  2236. * now.
  2237. */
  2238. ret = ffs_do_descs(ffs->fs_descs_count +
  2239. (high ? ffs->hs_descs_count : 0) +
  2240. (super ? ffs->ss_descs_count : 0),
  2241. vla_ptr(vlabuf, d, raw_descs), d_raw_descs__sz,
  2242. __ffs_func_bind_do_nums, func);
  2243. if (unlikely(ret < 0))
  2244. goto error;
  2245. func->function.os_desc_table = vla_ptr(vlabuf, d, os_desc_table);
  2246. if (c->cdev->use_os_string)
  2247. for (i = 0; i < ffs->interfaces_count; ++i) {
  2248. struct usb_os_desc *desc;
  2249. desc = func->function.os_desc_table[i].os_desc =
  2250. vla_ptr(vlabuf, d, os_desc) +
  2251. i * sizeof(struct usb_os_desc);
  2252. desc->ext_compat_id =
  2253. vla_ptr(vlabuf, d, ext_compat) + i * 16;
  2254. INIT_LIST_HEAD(&desc->ext_prop);
  2255. }
  2256. ret = ffs_do_os_descs(ffs->ms_os_descs_count,
  2257. vla_ptr(vlabuf, d, raw_descs) +
  2258. fs_len + hs_len + ss_len,
  2259. d_raw_descs__sz - fs_len - hs_len - ss_len,
  2260. __ffs_func_bind_do_os_desc, func);
  2261. if (unlikely(ret < 0))
  2262. goto error;
  2263. func->function.os_desc_n =
  2264. c->cdev->use_os_string ? ffs->interfaces_count : 0;
  2265. /* And we're done */
  2266. ffs_event_add(ffs, FUNCTIONFS_BIND);
  2267. return 0;
  2268. error:
  2269. /* XXX Do we need to release all claimed endpoints here? */
  2270. return ret;
  2271. }
  2272. static int ffs_func_bind(struct usb_configuration *c,
  2273. struct usb_function *f)
  2274. {
  2275. struct f_fs_opts *ffs_opts = ffs_do_functionfs_bind(f, c);
  2276. if (IS_ERR(ffs_opts))
  2277. return PTR_ERR(ffs_opts);
  2278. return _ffs_func_bind(c, f);
  2279. }
  2280. /* Other USB function hooks *************************************************/
  2281. static int ffs_func_set_alt(struct usb_function *f,
  2282. unsigned interface, unsigned alt)
  2283. {
  2284. struct ffs_function *func = ffs_func_from_usb(f);
  2285. struct ffs_data *ffs = func->ffs;
  2286. int ret = 0, intf;
  2287. if (alt != (unsigned)-1) {
  2288. intf = ffs_func_revmap_intf(func, interface);
  2289. if (unlikely(intf < 0))
  2290. return intf;
  2291. }
  2292. if (ffs->func)
  2293. ffs_func_eps_disable(ffs->func);
  2294. if (ffs->state != FFS_ACTIVE)
  2295. return -ENODEV;
  2296. if (alt == (unsigned)-1) {
  2297. ffs->func = NULL;
  2298. ffs_event_add(ffs, FUNCTIONFS_DISABLE);
  2299. return 0;
  2300. }
  2301. ffs->func = func;
  2302. ret = ffs_func_eps_enable(func);
  2303. if (likely(ret >= 0))
  2304. ffs_event_add(ffs, FUNCTIONFS_ENABLE);
  2305. return ret;
  2306. }
  2307. static void ffs_func_disable(struct usb_function *f)
  2308. {
  2309. ffs_func_set_alt(f, 0, (unsigned)-1);
  2310. }
  2311. static int ffs_func_setup(struct usb_function *f,
  2312. const struct usb_ctrlrequest *creq)
  2313. {
  2314. struct ffs_function *func = ffs_func_from_usb(f);
  2315. struct ffs_data *ffs = func->ffs;
  2316. unsigned long flags;
  2317. int ret;
  2318. ENTER();
  2319. pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType);
  2320. pr_vdebug("creq->bRequest = %02x\n", creq->bRequest);
  2321. pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq->wValue));
  2322. pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq->wIndex));
  2323. pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq->wLength));
  2324. /*
  2325. * Most requests directed to interface go through here
  2326. * (notable exceptions are set/get interface) so we need to
  2327. * handle them. All other either handled by composite or
  2328. * passed to usb_configuration->setup() (if one is set). No
  2329. * matter, we will handle requests directed to endpoint here
  2330. * as well (as it's straightforward) but what to do with any
  2331. * other request?
  2332. */
  2333. if (ffs->state != FFS_ACTIVE)
  2334. return -ENODEV;
  2335. switch (creq->bRequestType & USB_RECIP_MASK) {
  2336. case USB_RECIP_INTERFACE:
  2337. ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex));
  2338. if (unlikely(ret < 0))
  2339. return ret;
  2340. break;
  2341. case USB_RECIP_ENDPOINT:
  2342. ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex));
  2343. if (unlikely(ret < 0))
  2344. return ret;
  2345. break;
  2346. default:
  2347. return -EOPNOTSUPP;
  2348. }
  2349. spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
  2350. ffs->ev.setup = *creq;
  2351. ffs->ev.setup.wIndex = cpu_to_le16(ret);
  2352. __ffs_event_add(ffs, FUNCTIONFS_SETUP);
  2353. spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
  2354. return 0;
  2355. }
  2356. static void ffs_func_suspend(struct usb_function *f)
  2357. {
  2358. ENTER();
  2359. ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND);
  2360. }
  2361. static void ffs_func_resume(struct usb_function *f)
  2362. {
  2363. ENTER();
  2364. ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME);
  2365. }
  2366. /* Endpoint and interface numbers reverse mapping ***************************/
  2367. static int ffs_func_revmap_ep(struct ffs_function *func, u8 num)
  2368. {
  2369. num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK];
  2370. return num ? num : -EDOM;
  2371. }
  2372. static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf)
  2373. {
  2374. short *nums = func->interfaces_nums;
  2375. unsigned count = func->ffs->interfaces_count;
  2376. for (; count; --count, ++nums) {
  2377. if (*nums >= 0 && *nums == intf)
  2378. return nums - func->interfaces_nums;
  2379. }
  2380. return -EDOM;
  2381. }
  2382. /* Devices management *******************************************************/
  2383. static LIST_HEAD(ffs_devices);
  2384. static struct ffs_dev *_ffs_do_find_dev(const char *name)
  2385. {
  2386. struct ffs_dev *dev;
  2387. list_for_each_entry(dev, &ffs_devices, entry) {
  2388. if (!dev->name || !name)
  2389. continue;
  2390. if (strcmp(dev->name, name) == 0)
  2391. return dev;
  2392. }
  2393. return NULL;
  2394. }
  2395. /*
  2396. * ffs_lock must be taken by the caller of this function
  2397. */
  2398. static struct ffs_dev *_ffs_get_single_dev(void)
  2399. {
  2400. struct ffs_dev *dev;
  2401. if (list_is_singular(&ffs_devices)) {
  2402. dev = list_first_entry(&ffs_devices, struct ffs_dev, entry);
  2403. if (dev->single)
  2404. return dev;
  2405. }
  2406. return NULL;
  2407. }
  2408. /*
  2409. * ffs_lock must be taken by the caller of this function
  2410. */
  2411. static struct ffs_dev *_ffs_find_dev(const char *name)
  2412. {
  2413. struct ffs_dev *dev;
  2414. dev = _ffs_get_single_dev();
  2415. if (dev)
  2416. return dev;
  2417. return _ffs_do_find_dev(name);
  2418. }
  2419. /* Configfs support *********************************************************/
  2420. static inline struct f_fs_opts *to_ffs_opts(struct config_item *item)
  2421. {
  2422. return container_of(to_config_group(item), struct f_fs_opts,
  2423. func_inst.group);
  2424. }
  2425. static void ffs_attr_release(struct config_item *item)
  2426. {
  2427. struct f_fs_opts *opts = to_ffs_opts(item);
  2428. usb_put_function_instance(&opts->func_inst);
  2429. }
  2430. static struct configfs_item_operations ffs_item_ops = {
  2431. .release = ffs_attr_release,
  2432. };
  2433. static struct config_item_type ffs_func_type = {
  2434. .ct_item_ops = &ffs_item_ops,
  2435. .ct_owner = THIS_MODULE,
  2436. };
  2437. /* Function registration interface ******************************************/
  2438. static void ffs_free_inst(struct usb_function_instance *f)
  2439. {
  2440. struct f_fs_opts *opts;
  2441. opts = to_f_fs_opts(f);
  2442. ffs_dev_lock();
  2443. _ffs_free_dev(opts->dev);
  2444. ffs_dev_unlock();
  2445. kfree(opts);
  2446. }
  2447. #define MAX_INST_NAME_LEN 40
  2448. static int ffs_set_inst_name(struct usb_function_instance *fi, const char *name)
  2449. {
  2450. struct f_fs_opts *opts;
  2451. char *ptr;
  2452. const char *tmp;
  2453. int name_len, ret;
  2454. name_len = strlen(name) + 1;
  2455. if (name_len > MAX_INST_NAME_LEN)
  2456. return -ENAMETOOLONG;
  2457. ptr = kstrndup(name, name_len, GFP_KERNEL);
  2458. if (!ptr)
  2459. return -ENOMEM;
  2460. opts = to_f_fs_opts(fi);
  2461. tmp = NULL;
  2462. ffs_dev_lock();
  2463. tmp = opts->dev->name_allocated ? opts->dev->name : NULL;
  2464. ret = _ffs_name_dev(opts->dev, ptr);
  2465. if (ret) {
  2466. kfree(ptr);
  2467. ffs_dev_unlock();
  2468. return ret;
  2469. }
  2470. opts->dev->name_allocated = true;
  2471. ffs_dev_unlock();
  2472. kfree(tmp);
  2473. return 0;
  2474. }
  2475. static struct usb_function_instance *ffs_alloc_inst(void)
  2476. {
  2477. struct f_fs_opts *opts;
  2478. struct ffs_dev *dev;
  2479. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  2480. if (!opts)
  2481. return ERR_PTR(-ENOMEM);
  2482. opts->func_inst.set_inst_name = ffs_set_inst_name;
  2483. opts->func_inst.free_func_inst = ffs_free_inst;
  2484. ffs_dev_lock();
  2485. dev = _ffs_alloc_dev();
  2486. ffs_dev_unlock();
  2487. if (IS_ERR(dev)) {
  2488. kfree(opts);
  2489. return ERR_CAST(dev);
  2490. }
  2491. opts->dev = dev;
  2492. dev->opts = opts;
  2493. config_group_init_type_name(&opts->func_inst.group, "",
  2494. &ffs_func_type);
  2495. return &opts->func_inst;
  2496. }
  2497. static void ffs_free(struct usb_function *f)
  2498. {
  2499. kfree(ffs_func_from_usb(f));
  2500. }
  2501. static void ffs_func_unbind(struct usb_configuration *c,
  2502. struct usb_function *f)
  2503. {
  2504. struct ffs_function *func = ffs_func_from_usb(f);
  2505. struct ffs_data *ffs = func->ffs;
  2506. struct f_fs_opts *opts =
  2507. container_of(f->fi, struct f_fs_opts, func_inst);
  2508. struct ffs_ep *ep = func->eps;
  2509. unsigned count = ffs->eps_count;
  2510. unsigned long flags;
  2511. ENTER();
  2512. if (ffs->func == func) {
  2513. ffs_func_eps_disable(func);
  2514. ffs->func = NULL;
  2515. }
  2516. if (!--opts->refcnt)
  2517. functionfs_unbind(ffs);
  2518. /* cleanup after autoconfig */
  2519. spin_lock_irqsave(&func->ffs->eps_lock, flags);
  2520. do {
  2521. if (ep->ep && ep->req)
  2522. usb_ep_free_request(ep->ep, ep->req);
  2523. ep->req = NULL;
  2524. ++ep;
  2525. } while (--count);
  2526. spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
  2527. kfree(func->eps);
  2528. func->eps = NULL;
  2529. /*
  2530. * eps, descriptors and interfaces_nums are allocated in the
  2531. * same chunk so only one free is required.
  2532. */
  2533. func->function.fs_descriptors = NULL;
  2534. func->function.hs_descriptors = NULL;
  2535. func->function.ss_descriptors = NULL;
  2536. func->interfaces_nums = NULL;
  2537. ffs_event_add(ffs, FUNCTIONFS_UNBIND);
  2538. }
  2539. static struct usb_function *ffs_alloc(struct usb_function_instance *fi)
  2540. {
  2541. struct ffs_function *func;
  2542. ENTER();
  2543. func = kzalloc(sizeof(*func), GFP_KERNEL);
  2544. if (unlikely(!func))
  2545. return ERR_PTR(-ENOMEM);
  2546. func->function.name = "Function FS Gadget";
  2547. func->function.bind = ffs_func_bind;
  2548. func->function.unbind = ffs_func_unbind;
  2549. func->function.set_alt = ffs_func_set_alt;
  2550. func->function.disable = ffs_func_disable;
  2551. func->function.setup = ffs_func_setup;
  2552. func->function.suspend = ffs_func_suspend;
  2553. func->function.resume = ffs_func_resume;
  2554. func->function.free_func = ffs_free;
  2555. return &func->function;
  2556. }
  2557. /*
  2558. * ffs_lock must be taken by the caller of this function
  2559. */
  2560. static struct ffs_dev *_ffs_alloc_dev(void)
  2561. {
  2562. struct ffs_dev *dev;
  2563. int ret;
  2564. if (_ffs_get_single_dev())
  2565. return ERR_PTR(-EBUSY);
  2566. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  2567. if (!dev)
  2568. return ERR_PTR(-ENOMEM);
  2569. if (list_empty(&ffs_devices)) {
  2570. ret = functionfs_init();
  2571. if (ret) {
  2572. kfree(dev);
  2573. return ERR_PTR(ret);
  2574. }
  2575. }
  2576. list_add(&dev->entry, &ffs_devices);
  2577. return dev;
  2578. }
  2579. /*
  2580. * ffs_lock must be taken by the caller of this function
  2581. * The caller is responsible for "name" being available whenever f_fs needs it
  2582. */
  2583. static int _ffs_name_dev(struct ffs_dev *dev, const char *name)
  2584. {
  2585. struct ffs_dev *existing;
  2586. existing = _ffs_do_find_dev(name);
  2587. if (existing)
  2588. return -EBUSY;
  2589. dev->name = name;
  2590. return 0;
  2591. }
  2592. /*
  2593. * The caller is responsible for "name" being available whenever f_fs needs it
  2594. */
  2595. int ffs_name_dev(struct ffs_dev *dev, const char *name)
  2596. {
  2597. int ret;
  2598. ffs_dev_lock();
  2599. ret = _ffs_name_dev(dev, name);
  2600. ffs_dev_unlock();
  2601. return ret;
  2602. }
  2603. EXPORT_SYMBOL_GPL(ffs_name_dev);
  2604. int ffs_single_dev(struct ffs_dev *dev)
  2605. {
  2606. int ret;
  2607. ret = 0;
  2608. ffs_dev_lock();
  2609. if (!list_is_singular(&ffs_devices))
  2610. ret = -EBUSY;
  2611. else
  2612. dev->single = true;
  2613. ffs_dev_unlock();
  2614. return ret;
  2615. }
  2616. EXPORT_SYMBOL_GPL(ffs_single_dev);
  2617. /*
  2618. * ffs_lock must be taken by the caller of this function
  2619. */
  2620. static void _ffs_free_dev(struct ffs_dev *dev)
  2621. {
  2622. list_del(&dev->entry);
  2623. if (dev->name_allocated)
  2624. kfree(dev->name);
  2625. kfree(dev);
  2626. if (list_empty(&ffs_devices))
  2627. functionfs_cleanup();
  2628. }
  2629. static void *ffs_acquire_dev(const char *dev_name)
  2630. {
  2631. struct ffs_dev *ffs_dev;
  2632. ENTER();
  2633. ffs_dev_lock();
  2634. ffs_dev = _ffs_find_dev(dev_name);
  2635. if (!ffs_dev)
  2636. ffs_dev = ERR_PTR(-ENOENT);
  2637. else if (ffs_dev->mounted)
  2638. ffs_dev = ERR_PTR(-EBUSY);
  2639. else if (ffs_dev->ffs_acquire_dev_callback &&
  2640. ffs_dev->ffs_acquire_dev_callback(ffs_dev))
  2641. ffs_dev = ERR_PTR(-ENOENT);
  2642. else
  2643. ffs_dev->mounted = true;
  2644. ffs_dev_unlock();
  2645. return ffs_dev;
  2646. }
  2647. static void ffs_release_dev(struct ffs_data *ffs_data)
  2648. {
  2649. struct ffs_dev *ffs_dev;
  2650. ENTER();
  2651. ffs_dev_lock();
  2652. ffs_dev = ffs_data->private_data;
  2653. if (ffs_dev) {
  2654. ffs_dev->mounted = false;
  2655. if (ffs_dev->ffs_release_dev_callback)
  2656. ffs_dev->ffs_release_dev_callback(ffs_dev);
  2657. }
  2658. ffs_dev_unlock();
  2659. }
  2660. static int ffs_ready(struct ffs_data *ffs)
  2661. {
  2662. struct ffs_dev *ffs_obj;
  2663. int ret = 0;
  2664. ENTER();
  2665. ffs_dev_lock();
  2666. ffs_obj = ffs->private_data;
  2667. if (!ffs_obj) {
  2668. ret = -EINVAL;
  2669. goto done;
  2670. }
  2671. if (WARN_ON(ffs_obj->desc_ready)) {
  2672. ret = -EBUSY;
  2673. goto done;
  2674. }
  2675. ffs_obj->desc_ready = true;
  2676. ffs_obj->ffs_data = ffs;
  2677. if (ffs_obj->ffs_ready_callback)
  2678. ret = ffs_obj->ffs_ready_callback(ffs);
  2679. done:
  2680. ffs_dev_unlock();
  2681. return ret;
  2682. }
  2683. static void ffs_closed(struct ffs_data *ffs)
  2684. {
  2685. struct ffs_dev *ffs_obj;
  2686. ENTER();
  2687. ffs_dev_lock();
  2688. ffs_obj = ffs->private_data;
  2689. if (!ffs_obj)
  2690. goto done;
  2691. ffs_obj->desc_ready = false;
  2692. if (ffs_obj->ffs_closed_callback)
  2693. ffs_obj->ffs_closed_callback(ffs);
  2694. if (!ffs_obj->opts || ffs_obj->opts->no_configfs
  2695. || !ffs_obj->opts->func_inst.group.cg_item.ci_parent)
  2696. goto done;
  2697. unregister_gadget_item(ffs_obj->opts->
  2698. func_inst.group.cg_item.ci_parent->ci_parent);
  2699. done:
  2700. ffs_dev_unlock();
  2701. }
  2702. /* Misc helper functions ****************************************************/
  2703. static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
  2704. {
  2705. return nonblock
  2706. ? likely(mutex_trylock(mutex)) ? 0 : -EAGAIN
  2707. : mutex_lock_interruptible(mutex);
  2708. }
  2709. static char *ffs_prepare_buffer(const char __user *buf, size_t len)
  2710. {
  2711. char *data;
  2712. if (unlikely(!len))
  2713. return NULL;
  2714. data = kmalloc(len, GFP_KERNEL);
  2715. if (unlikely(!data))
  2716. return ERR_PTR(-ENOMEM);
  2717. if (unlikely(__copy_from_user(data, buf, len))) {
  2718. kfree(data);
  2719. return ERR_PTR(-EFAULT);
  2720. }
  2721. pr_vdebug("Buffer from user space:\n");
  2722. ffs_dump_mem("", data, len);
  2723. return data;
  2724. }
  2725. DECLARE_USB_FUNCTION_INIT(ffs, ffs_alloc_inst, ffs_alloc);
  2726. MODULE_LICENSE("GPL");
  2727. MODULE_AUTHOR("Michal Nazarewicz");