f_fs.c 77 KB

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