f_fs.c 87 KB

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