f_mass_storage.c 97 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667
  1. /*
  2. * f_mass_storage.c -- Mass Storage USB Composite Function
  3. *
  4. * Copyright (C) 2003-2008 Alan Stern
  5. * Copyright (C) 2009 Samsung Electronics
  6. * Author: Michal Nazarewicz <mina86@mina86.com>
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions, and the following disclaimer,
  14. * without modification.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. The names of the above-listed copyright holders may not be used
  19. * to endorse or promote products derived from this software without
  20. * specific prior written permission.
  21. *
  22. * ALTERNATIVELY, this software may be distributed under the terms of the
  23. * GNU General Public License ("GPL") as published by the Free Software
  24. * Foundation, either version 2 of that License or (at your option) any
  25. * later version.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  28. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  29. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  31. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  32. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  33. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  34. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  35. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  36. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  37. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. */
  39. /*
  40. * The Mass Storage Function acts as a USB Mass Storage device,
  41. * appearing to the host as a disk drive or as a CD-ROM drive. In
  42. * addition to providing an example of a genuinely useful composite
  43. * function for a USB device, it also illustrates a technique of
  44. * double-buffering for increased throughput.
  45. *
  46. * For more information about MSF and in particular its module
  47. * parameters and sysfs interface read the
  48. * <Documentation/usb/mass-storage.txt> file.
  49. */
  50. /*
  51. * MSF is configured by specifying a fsg_config structure. It has the
  52. * following fields:
  53. *
  54. * nluns Number of LUNs function have (anywhere from 1
  55. * to FSG_MAX_LUNS which is 8).
  56. * luns An array of LUN configuration values. This
  57. * should be filled for each LUN that
  58. * function will include (ie. for "nluns"
  59. * LUNs). Each element of the array has
  60. * the following fields:
  61. * ->filename The path to the backing file for the LUN.
  62. * Required if LUN is not marked as
  63. * removable.
  64. * ->ro Flag specifying access to the LUN shall be
  65. * read-only. This is implied if CD-ROM
  66. * emulation is enabled as well as when
  67. * it was impossible to open "filename"
  68. * in R/W mode.
  69. * ->removable Flag specifying that LUN shall be indicated as
  70. * being removable.
  71. * ->cdrom Flag specifying that LUN shall be reported as
  72. * being a CD-ROM.
  73. * ->nofua Flag specifying that FUA flag in SCSI WRITE(10,12)
  74. * commands for this LUN shall be ignored.
  75. *
  76. * vendor_name
  77. * product_name
  78. * release Information used as a reply to INQUIRY
  79. * request. To use default set to NULL,
  80. * NULL, 0xffff respectively. The first
  81. * field should be 8 and the second 16
  82. * characters or less.
  83. *
  84. * can_stall Set to permit function to halt bulk endpoints.
  85. * Disabled on some USB devices known not
  86. * to work correctly. You should set it
  87. * to true.
  88. *
  89. * If "removable" is not set for a LUN then a backing file must be
  90. * specified. If it is set, then NULL filename means the LUN's medium
  91. * is not loaded (an empty string as "filename" in the fsg_config
  92. * structure causes error). The CD-ROM emulation includes a single
  93. * data track and no audio tracks; hence there need be only one
  94. * backing file per LUN.
  95. *
  96. * This function is heavily based on "File-backed Storage Gadget" by
  97. * Alan Stern which in turn is heavily based on "Gadget Zero" by David
  98. * Brownell. The driver's SCSI command interface was based on the
  99. * "Information technology - Small Computer System Interface - 2"
  100. * document from X3T9.2 Project 375D, Revision 10L, 7-SEP-93,
  101. * available at <http://www.t10.org/ftp/t10/drafts/s2/s2-r10l.pdf>.
  102. * The single exception is opcode 0x23 (READ FORMAT CAPACITIES), which
  103. * was based on the "Universal Serial Bus Mass Storage Class UFI
  104. * Command Specification" document, Revision 1.0, December 14, 1998,
  105. * available at
  106. * <http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf>.
  107. */
  108. /*
  109. * Driver Design
  110. *
  111. * The MSF is fairly straightforward. There is a main kernel
  112. * thread that handles most of the work. Interrupt routines field
  113. * callbacks from the controller driver: bulk- and interrupt-request
  114. * completion notifications, endpoint-0 events, and disconnect events.
  115. * Completion events are passed to the main thread by wakeup calls. Many
  116. * ep0 requests are handled at interrupt time, but SetInterface,
  117. * SetConfiguration, and device reset requests are forwarded to the
  118. * thread in the form of "exceptions" using SIGUSR1 signals (since they
  119. * should interrupt any ongoing file I/O operations).
  120. *
  121. * The thread's main routine implements the standard command/data/status
  122. * parts of a SCSI interaction. It and its subroutines are full of tests
  123. * for pending signals/exceptions -- all this polling is necessary since
  124. * the kernel has no setjmp/longjmp equivalents. (Maybe this is an
  125. * indication that the driver really wants to be running in userspace.)
  126. * An important point is that so long as the thread is alive it keeps an
  127. * open reference to the backing file. This will prevent unmounting
  128. * the backing file's underlying filesystem and could cause problems
  129. * during system shutdown, for example. To prevent such problems, the
  130. * thread catches INT, TERM, and KILL signals and converts them into
  131. * an EXIT exception.
  132. *
  133. * In normal operation the main thread is started during the gadget's
  134. * fsg_bind() callback and stopped during fsg_unbind(). But it can
  135. * also exit when it receives a signal, and there's no point leaving
  136. * the gadget running when the thread is dead. As of this moment, MSF
  137. * provides no way to deregister the gadget when thread dies -- maybe
  138. * a callback functions is needed.
  139. *
  140. * To provide maximum throughput, the driver uses a circular pipeline of
  141. * buffer heads (struct fsg_buffhd). In principle the pipeline can be
  142. * arbitrarily long; in practice the benefits don't justify having more
  143. * than 2 stages (i.e., double buffering). But it helps to think of the
  144. * pipeline as being a long one. Each buffer head contains a bulk-in and
  145. * a bulk-out request pointer (since the buffer can be used for both
  146. * output and input -- directions always are given from the host's
  147. * point of view) as well as a pointer to the buffer and various state
  148. * variables.
  149. *
  150. * Use of the pipeline follows a simple protocol. There is a variable
  151. * (fsg->next_buffhd_to_fill) that points to the next buffer head to use.
  152. * At any time that buffer head may still be in use from an earlier
  153. * request, so each buffer head has a state variable indicating whether
  154. * it is EMPTY, FULL, or BUSY. Typical use involves waiting for the
  155. * buffer head to be EMPTY, filling the buffer either by file I/O or by
  156. * USB I/O (during which the buffer head is BUSY), and marking the buffer
  157. * head FULL when the I/O is complete. Then the buffer will be emptied
  158. * (again possibly by USB I/O, during which it is marked BUSY) and
  159. * finally marked EMPTY again (possibly by a completion routine).
  160. *
  161. * A module parameter tells the driver to avoid stalling the bulk
  162. * endpoints wherever the transport specification allows. This is
  163. * necessary for some UDCs like the SuperH, which cannot reliably clear a
  164. * halt on a bulk endpoint. However, under certain circumstances the
  165. * Bulk-only specification requires a stall. In such cases the driver
  166. * will halt the endpoint and set a flag indicating that it should clear
  167. * the halt in software during the next device reset. Hopefully this
  168. * will permit everything to work correctly. Furthermore, although the
  169. * specification allows the bulk-out endpoint to halt when the host sends
  170. * too much data, implementing this would cause an unavoidable race.
  171. * The driver will always use the "no-stall" approach for OUT transfers.
  172. *
  173. * One subtle point concerns sending status-stage responses for ep0
  174. * requests. Some of these requests, such as device reset, can involve
  175. * interrupting an ongoing file I/O operation, which might take an
  176. * arbitrarily long time. During that delay the host might give up on
  177. * the original ep0 request and issue a new one. When that happens the
  178. * driver should not notify the host about completion of the original
  179. * request, as the host will no longer be waiting for it. So the driver
  180. * assigns to each ep0 request a unique tag, and it keeps track of the
  181. * tag value of the request associated with a long-running exception
  182. * (device-reset, interface-change, or configuration-change). When the
  183. * exception handler is finished, the status-stage response is submitted
  184. * only if the current ep0 request tag is equal to the exception request
  185. * tag. Thus only the most recently received ep0 request will get a
  186. * status-stage response.
  187. *
  188. * Warning: This driver source file is too long. It ought to be split up
  189. * into a header file plus about 3 separate .c files, to handle the details
  190. * of the Gadget, USB Mass Storage, and SCSI protocols.
  191. */
  192. /* #define VERBOSE_DEBUG */
  193. /* #define DUMP_MSGS */
  194. #include <linux/blkdev.h>
  195. #include <linux/completion.h>
  196. #include <linux/dcache.h>
  197. #include <linux/delay.h>
  198. #include <linux/device.h>
  199. #include <linux/fcntl.h>
  200. #include <linux/file.h>
  201. #include <linux/fs.h>
  202. #include <linux/kref.h>
  203. #include <linux/kthread.h>
  204. #include <linux/limits.h>
  205. #include <linux/rwsem.h>
  206. #include <linux/slab.h>
  207. #include <linux/spinlock.h>
  208. #include <linux/string.h>
  209. #include <linux/freezer.h>
  210. #include <linux/module.h>
  211. #include <linux/usb/ch9.h>
  212. #include <linux/usb/gadget.h>
  213. #include <linux/usb/composite.h>
  214. #include "gadget_chips.h"
  215. #include "configfs.h"
  216. /*------------------------------------------------------------------------*/
  217. #define FSG_DRIVER_DESC "Mass Storage Function"
  218. #define FSG_DRIVER_VERSION "2009/09/11"
  219. static const char fsg_string_interface[] = "Mass Storage";
  220. #include "storage_common.h"
  221. #include "f_mass_storage.h"
  222. /* Static strings, in UTF-8 (for simplicity we use only ASCII characters) */
  223. static struct usb_string fsg_strings[] = {
  224. {FSG_STRING_INTERFACE, fsg_string_interface},
  225. {}
  226. };
  227. static struct usb_gadget_strings fsg_stringtab = {
  228. .language = 0x0409, /* en-us */
  229. .strings = fsg_strings,
  230. };
  231. static struct usb_gadget_strings *fsg_strings_array[] = {
  232. &fsg_stringtab,
  233. NULL,
  234. };
  235. /*-------------------------------------------------------------------------*/
  236. struct fsg_dev;
  237. struct fsg_common;
  238. /* Data shared by all the FSG instances. */
  239. struct fsg_common {
  240. struct usb_gadget *gadget;
  241. struct usb_composite_dev *cdev;
  242. struct fsg_dev *fsg, *new_fsg;
  243. wait_queue_head_t fsg_wait;
  244. /* filesem protects: backing files in use */
  245. struct rw_semaphore filesem;
  246. /* lock protects: state, all the req_busy's */
  247. spinlock_t lock;
  248. struct usb_ep *ep0; /* Copy of gadget->ep0 */
  249. struct usb_request *ep0req; /* Copy of cdev->req */
  250. unsigned int ep0_req_tag;
  251. struct fsg_buffhd *next_buffhd_to_fill;
  252. struct fsg_buffhd *next_buffhd_to_drain;
  253. struct fsg_buffhd *buffhds;
  254. unsigned int fsg_num_buffers;
  255. int cmnd_size;
  256. u8 cmnd[MAX_COMMAND_SIZE];
  257. unsigned int nluns;
  258. unsigned int lun;
  259. struct fsg_lun **luns;
  260. struct fsg_lun *curlun;
  261. unsigned int bulk_out_maxpacket;
  262. enum fsg_state state; /* For exception handling */
  263. unsigned int exception_req_tag;
  264. enum data_direction data_dir;
  265. u32 data_size;
  266. u32 data_size_from_cmnd;
  267. u32 tag;
  268. u32 residue;
  269. u32 usb_amount_left;
  270. unsigned int can_stall:1;
  271. unsigned int free_storage_on_release:1;
  272. unsigned int phase_error:1;
  273. unsigned int short_packet_received:1;
  274. unsigned int bad_lun_okay:1;
  275. unsigned int running:1;
  276. unsigned int sysfs:1;
  277. int thread_wakeup_needed;
  278. struct completion thread_notifier;
  279. struct task_struct *thread_task;
  280. /* Callback functions. */
  281. const struct fsg_operations *ops;
  282. /* Gadget's private data. */
  283. void *private_data;
  284. /*
  285. * Vendor (8 chars), product (16 chars), release (4
  286. * hexadecimal digits) and NUL byte
  287. */
  288. char inquiry_string[8 + 16 + 4 + 1];
  289. struct kref ref;
  290. };
  291. struct fsg_dev {
  292. struct usb_function function;
  293. struct usb_gadget *gadget; /* Copy of cdev->gadget */
  294. struct fsg_common *common;
  295. u16 interface_number;
  296. unsigned int bulk_in_enabled:1;
  297. unsigned int bulk_out_enabled:1;
  298. unsigned long atomic_bitflags;
  299. #define IGNORE_BULK_OUT 0
  300. struct usb_ep *bulk_in;
  301. struct usb_ep *bulk_out;
  302. };
  303. static inline int __fsg_is_set(struct fsg_common *common,
  304. const char *func, unsigned line)
  305. {
  306. if (common->fsg)
  307. return 1;
  308. ERROR(common, "common->fsg is NULL in %s at %u\n", func, line);
  309. WARN_ON(1);
  310. return 0;
  311. }
  312. #define fsg_is_set(common) likely(__fsg_is_set(common, __func__, __LINE__))
  313. static inline struct fsg_dev *fsg_from_func(struct usb_function *f)
  314. {
  315. return container_of(f, struct fsg_dev, function);
  316. }
  317. typedef void (*fsg_routine_t)(struct fsg_dev *);
  318. static int exception_in_progress(struct fsg_common *common)
  319. {
  320. return common->state > FSG_STATE_IDLE;
  321. }
  322. /* Make bulk-out requests be divisible by the maxpacket size */
  323. static void set_bulk_out_req_length(struct fsg_common *common,
  324. struct fsg_buffhd *bh, unsigned int length)
  325. {
  326. unsigned int rem;
  327. bh->bulk_out_intended_length = length;
  328. rem = length % common->bulk_out_maxpacket;
  329. if (rem > 0)
  330. length += common->bulk_out_maxpacket - rem;
  331. bh->outreq->length = length;
  332. }
  333. /*-------------------------------------------------------------------------*/
  334. static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep)
  335. {
  336. const char *name;
  337. if (ep == fsg->bulk_in)
  338. name = "bulk-in";
  339. else if (ep == fsg->bulk_out)
  340. name = "bulk-out";
  341. else
  342. name = ep->name;
  343. DBG(fsg, "%s set halt\n", name);
  344. return usb_ep_set_halt(ep);
  345. }
  346. /*-------------------------------------------------------------------------*/
  347. /* These routines may be called in process context or in_irq */
  348. /* Caller must hold fsg->lock */
  349. static void wakeup_thread(struct fsg_common *common)
  350. {
  351. smp_wmb(); /* ensure the write of bh->state is complete */
  352. /* Tell the main thread that something has happened */
  353. common->thread_wakeup_needed = 1;
  354. if (common->thread_task)
  355. wake_up_process(common->thread_task);
  356. }
  357. static void raise_exception(struct fsg_common *common, enum fsg_state new_state)
  358. {
  359. unsigned long flags;
  360. /*
  361. * Do nothing if a higher-priority exception is already in progress.
  362. * If a lower-or-equal priority exception is in progress, preempt it
  363. * and notify the main thread by sending it a signal.
  364. */
  365. spin_lock_irqsave(&common->lock, flags);
  366. if (common->state <= new_state) {
  367. common->exception_req_tag = common->ep0_req_tag;
  368. common->state = new_state;
  369. if (common->thread_task)
  370. send_sig_info(SIGUSR1, SEND_SIG_FORCED,
  371. common->thread_task);
  372. }
  373. spin_unlock_irqrestore(&common->lock, flags);
  374. }
  375. /*-------------------------------------------------------------------------*/
  376. static int ep0_queue(struct fsg_common *common)
  377. {
  378. int rc;
  379. rc = usb_ep_queue(common->ep0, common->ep0req, GFP_ATOMIC);
  380. common->ep0->driver_data = common;
  381. if (rc != 0 && rc != -ESHUTDOWN) {
  382. /* We can't do much more than wait for a reset */
  383. WARNING(common, "error in submission: %s --> %d\n",
  384. common->ep0->name, rc);
  385. }
  386. return rc;
  387. }
  388. /*-------------------------------------------------------------------------*/
  389. /* Completion handlers. These always run in_irq. */
  390. static void bulk_in_complete(struct usb_ep *ep, struct usb_request *req)
  391. {
  392. struct fsg_common *common = ep->driver_data;
  393. struct fsg_buffhd *bh = req->context;
  394. if (req->status || req->actual != req->length)
  395. DBG(common, "%s --> %d, %u/%u\n", __func__,
  396. req->status, req->actual, req->length);
  397. if (req->status == -ECONNRESET) /* Request was cancelled */
  398. usb_ep_fifo_flush(ep);
  399. /* Hold the lock while we update the request and buffer states */
  400. smp_wmb();
  401. spin_lock(&common->lock);
  402. bh->inreq_busy = 0;
  403. bh->state = BUF_STATE_EMPTY;
  404. wakeup_thread(common);
  405. spin_unlock(&common->lock);
  406. }
  407. static void bulk_out_complete(struct usb_ep *ep, struct usb_request *req)
  408. {
  409. struct fsg_common *common = ep->driver_data;
  410. struct fsg_buffhd *bh = req->context;
  411. dump_msg(common, "bulk-out", req->buf, req->actual);
  412. if (req->status || req->actual != bh->bulk_out_intended_length)
  413. DBG(common, "%s --> %d, %u/%u\n", __func__,
  414. req->status, req->actual, bh->bulk_out_intended_length);
  415. if (req->status == -ECONNRESET) /* Request was cancelled */
  416. usb_ep_fifo_flush(ep);
  417. /* Hold the lock while we update the request and buffer states */
  418. smp_wmb();
  419. spin_lock(&common->lock);
  420. bh->outreq_busy = 0;
  421. bh->state = BUF_STATE_FULL;
  422. wakeup_thread(common);
  423. spin_unlock(&common->lock);
  424. }
  425. static int fsg_setup(struct usb_function *f,
  426. const struct usb_ctrlrequest *ctrl)
  427. {
  428. struct fsg_dev *fsg = fsg_from_func(f);
  429. struct usb_request *req = fsg->common->ep0req;
  430. u16 w_index = le16_to_cpu(ctrl->wIndex);
  431. u16 w_value = le16_to_cpu(ctrl->wValue);
  432. u16 w_length = le16_to_cpu(ctrl->wLength);
  433. if (!fsg_is_set(fsg->common))
  434. return -EOPNOTSUPP;
  435. ++fsg->common->ep0_req_tag; /* Record arrival of a new request */
  436. req->context = NULL;
  437. req->length = 0;
  438. dump_msg(fsg, "ep0-setup", (u8 *) ctrl, sizeof(*ctrl));
  439. switch (ctrl->bRequest) {
  440. case US_BULK_RESET_REQUEST:
  441. if (ctrl->bRequestType !=
  442. (USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE))
  443. break;
  444. if (w_index != fsg->interface_number || w_value != 0 ||
  445. w_length != 0)
  446. return -EDOM;
  447. /*
  448. * Raise an exception to stop the current operation
  449. * and reinitialize our state.
  450. */
  451. DBG(fsg, "bulk reset request\n");
  452. raise_exception(fsg->common, FSG_STATE_RESET);
  453. return DELAYED_STATUS;
  454. case US_BULK_GET_MAX_LUN:
  455. if (ctrl->bRequestType !=
  456. (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE))
  457. break;
  458. if (w_index != fsg->interface_number || w_value != 0 ||
  459. w_length != 1)
  460. return -EDOM;
  461. VDBG(fsg, "get max LUN\n");
  462. *(u8 *)req->buf = fsg->common->nluns - 1;
  463. /* Respond with data/status */
  464. req->length = min((u16)1, w_length);
  465. return ep0_queue(fsg->common);
  466. }
  467. VDBG(fsg,
  468. "unknown class-specific control req %02x.%02x v%04x i%04x l%u\n",
  469. ctrl->bRequestType, ctrl->bRequest,
  470. le16_to_cpu(ctrl->wValue), w_index, w_length);
  471. return -EOPNOTSUPP;
  472. }
  473. /*-------------------------------------------------------------------------*/
  474. /* All the following routines run in process context */
  475. /* Use this for bulk or interrupt transfers, not ep0 */
  476. static void start_transfer(struct fsg_dev *fsg, struct usb_ep *ep,
  477. struct usb_request *req, int *pbusy,
  478. enum fsg_buffer_state *state)
  479. {
  480. int rc;
  481. if (ep == fsg->bulk_in)
  482. dump_msg(fsg, "bulk-in", req->buf, req->length);
  483. spin_lock_irq(&fsg->common->lock);
  484. *pbusy = 1;
  485. *state = BUF_STATE_BUSY;
  486. spin_unlock_irq(&fsg->common->lock);
  487. rc = usb_ep_queue(ep, req, GFP_KERNEL);
  488. if (rc != 0) {
  489. *pbusy = 0;
  490. *state = BUF_STATE_EMPTY;
  491. /* We can't do much more than wait for a reset */
  492. /*
  493. * Note: currently the net2280 driver fails zero-length
  494. * submissions if DMA is enabled.
  495. */
  496. if (rc != -ESHUTDOWN &&
  497. !(rc == -EOPNOTSUPP && req->length == 0))
  498. WARNING(fsg, "error in submission: %s --> %d\n",
  499. ep->name, rc);
  500. }
  501. }
  502. static bool start_in_transfer(struct fsg_common *common, struct fsg_buffhd *bh)
  503. {
  504. if (!fsg_is_set(common))
  505. return false;
  506. start_transfer(common->fsg, common->fsg->bulk_in,
  507. bh->inreq, &bh->inreq_busy, &bh->state);
  508. return true;
  509. }
  510. static bool start_out_transfer(struct fsg_common *common, struct fsg_buffhd *bh)
  511. {
  512. if (!fsg_is_set(common))
  513. return false;
  514. start_transfer(common->fsg, common->fsg->bulk_out,
  515. bh->outreq, &bh->outreq_busy, &bh->state);
  516. return true;
  517. }
  518. static int sleep_thread(struct fsg_common *common)
  519. {
  520. int rc = 0;
  521. /* Wait until a signal arrives or we are woken up */
  522. for (;;) {
  523. try_to_freeze();
  524. set_current_state(TASK_INTERRUPTIBLE);
  525. if (signal_pending(current)) {
  526. rc = -EINTR;
  527. break;
  528. }
  529. if (common->thread_wakeup_needed)
  530. break;
  531. schedule();
  532. }
  533. __set_current_state(TASK_RUNNING);
  534. common->thread_wakeup_needed = 0;
  535. smp_rmb(); /* ensure the latest bh->state is visible */
  536. return rc;
  537. }
  538. /*-------------------------------------------------------------------------*/
  539. static int do_read(struct fsg_common *common)
  540. {
  541. struct fsg_lun *curlun = common->curlun;
  542. u32 lba;
  543. struct fsg_buffhd *bh;
  544. int rc;
  545. u32 amount_left;
  546. loff_t file_offset, file_offset_tmp;
  547. unsigned int amount;
  548. ssize_t nread;
  549. /*
  550. * Get the starting Logical Block Address and check that it's
  551. * not too big.
  552. */
  553. if (common->cmnd[0] == READ_6)
  554. lba = get_unaligned_be24(&common->cmnd[1]);
  555. else {
  556. lba = get_unaligned_be32(&common->cmnd[2]);
  557. /*
  558. * We allow DPO (Disable Page Out = don't save data in the
  559. * cache) and FUA (Force Unit Access = don't read from the
  560. * cache), but we don't implement them.
  561. */
  562. if ((common->cmnd[1] & ~0x18) != 0) {
  563. curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  564. return -EINVAL;
  565. }
  566. }
  567. if (lba >= curlun->num_sectors) {
  568. curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  569. return -EINVAL;
  570. }
  571. file_offset = ((loff_t) lba) << curlun->blkbits;
  572. /* Carry out the file reads */
  573. amount_left = common->data_size_from_cmnd;
  574. if (unlikely(amount_left == 0))
  575. return -EIO; /* No default reply */
  576. for (;;) {
  577. /*
  578. * Figure out how much we need to read:
  579. * Try to read the remaining amount.
  580. * But don't read more than the buffer size.
  581. * And don't try to read past the end of the file.
  582. */
  583. amount = min(amount_left, FSG_BUFLEN);
  584. amount = min((loff_t)amount,
  585. curlun->file_length - file_offset);
  586. /* Wait for the next buffer to become available */
  587. bh = common->next_buffhd_to_fill;
  588. while (bh->state != BUF_STATE_EMPTY) {
  589. rc = sleep_thread(common);
  590. if (rc)
  591. return rc;
  592. }
  593. /*
  594. * If we were asked to read past the end of file,
  595. * end with an empty buffer.
  596. */
  597. if (amount == 0) {
  598. curlun->sense_data =
  599. SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  600. curlun->sense_data_info =
  601. file_offset >> curlun->blkbits;
  602. curlun->info_valid = 1;
  603. bh->inreq->length = 0;
  604. bh->state = BUF_STATE_FULL;
  605. break;
  606. }
  607. /* Perform the read */
  608. file_offset_tmp = file_offset;
  609. nread = vfs_read(curlun->filp,
  610. (char __user *)bh->buf,
  611. amount, &file_offset_tmp);
  612. VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
  613. (unsigned long long)file_offset, (int)nread);
  614. if (signal_pending(current))
  615. return -EINTR;
  616. if (nread < 0) {
  617. LDBG(curlun, "error in file read: %d\n", (int)nread);
  618. nread = 0;
  619. } else if (nread < amount) {
  620. LDBG(curlun, "partial file read: %d/%u\n",
  621. (int)nread, amount);
  622. nread = round_down(nread, curlun->blksize);
  623. }
  624. file_offset += nread;
  625. amount_left -= nread;
  626. common->residue -= nread;
  627. /*
  628. * Except at the end of the transfer, nread will be
  629. * equal to the buffer size, which is divisible by the
  630. * bulk-in maxpacket size.
  631. */
  632. bh->inreq->length = nread;
  633. bh->state = BUF_STATE_FULL;
  634. /* If an error occurred, report it and its position */
  635. if (nread < amount) {
  636. curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
  637. curlun->sense_data_info =
  638. file_offset >> curlun->blkbits;
  639. curlun->info_valid = 1;
  640. break;
  641. }
  642. if (amount_left == 0)
  643. break; /* No more left to read */
  644. /* Send this buffer and go read some more */
  645. bh->inreq->zero = 0;
  646. if (!start_in_transfer(common, bh))
  647. /* Don't know what to do if common->fsg is NULL */
  648. return -EIO;
  649. common->next_buffhd_to_fill = bh->next;
  650. }
  651. return -EIO; /* No default reply */
  652. }
  653. /*-------------------------------------------------------------------------*/
  654. static int do_write(struct fsg_common *common)
  655. {
  656. struct fsg_lun *curlun = common->curlun;
  657. u32 lba;
  658. struct fsg_buffhd *bh;
  659. int get_some_more;
  660. u32 amount_left_to_req, amount_left_to_write;
  661. loff_t usb_offset, file_offset, file_offset_tmp;
  662. unsigned int amount;
  663. ssize_t nwritten;
  664. int rc;
  665. if (curlun->ro) {
  666. curlun->sense_data = SS_WRITE_PROTECTED;
  667. return -EINVAL;
  668. }
  669. spin_lock(&curlun->filp->f_lock);
  670. curlun->filp->f_flags &= ~O_SYNC; /* Default is not to wait */
  671. spin_unlock(&curlun->filp->f_lock);
  672. /*
  673. * Get the starting Logical Block Address and check that it's
  674. * not too big
  675. */
  676. if (common->cmnd[0] == WRITE_6)
  677. lba = get_unaligned_be24(&common->cmnd[1]);
  678. else {
  679. lba = get_unaligned_be32(&common->cmnd[2]);
  680. /*
  681. * We allow DPO (Disable Page Out = don't save data in the
  682. * cache) and FUA (Force Unit Access = write directly to the
  683. * medium). We don't implement DPO; we implement FUA by
  684. * performing synchronous output.
  685. */
  686. if (common->cmnd[1] & ~0x18) {
  687. curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  688. return -EINVAL;
  689. }
  690. if (!curlun->nofua && (common->cmnd[1] & 0x08)) { /* FUA */
  691. spin_lock(&curlun->filp->f_lock);
  692. curlun->filp->f_flags |= O_SYNC;
  693. spin_unlock(&curlun->filp->f_lock);
  694. }
  695. }
  696. if (lba >= curlun->num_sectors) {
  697. curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  698. return -EINVAL;
  699. }
  700. /* Carry out the file writes */
  701. get_some_more = 1;
  702. file_offset = usb_offset = ((loff_t) lba) << curlun->blkbits;
  703. amount_left_to_req = common->data_size_from_cmnd;
  704. amount_left_to_write = common->data_size_from_cmnd;
  705. while (amount_left_to_write > 0) {
  706. /* Queue a request for more data from the host */
  707. bh = common->next_buffhd_to_fill;
  708. if (bh->state == BUF_STATE_EMPTY && get_some_more) {
  709. /*
  710. * Figure out how much we want to get:
  711. * Try to get the remaining amount,
  712. * but not more than the buffer size.
  713. */
  714. amount = min(amount_left_to_req, FSG_BUFLEN);
  715. /* Beyond the end of the backing file? */
  716. if (usb_offset >= curlun->file_length) {
  717. get_some_more = 0;
  718. curlun->sense_data =
  719. SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  720. curlun->sense_data_info =
  721. usb_offset >> curlun->blkbits;
  722. curlun->info_valid = 1;
  723. continue;
  724. }
  725. /* Get the next buffer */
  726. usb_offset += amount;
  727. common->usb_amount_left -= amount;
  728. amount_left_to_req -= amount;
  729. if (amount_left_to_req == 0)
  730. get_some_more = 0;
  731. /*
  732. * Except at the end of the transfer, amount will be
  733. * equal to the buffer size, which is divisible by
  734. * the bulk-out maxpacket size.
  735. */
  736. set_bulk_out_req_length(common, bh, amount);
  737. if (!start_out_transfer(common, bh))
  738. /* Dunno what to do if common->fsg is NULL */
  739. return -EIO;
  740. common->next_buffhd_to_fill = bh->next;
  741. continue;
  742. }
  743. /* Write the received data to the backing file */
  744. bh = common->next_buffhd_to_drain;
  745. if (bh->state == BUF_STATE_EMPTY && !get_some_more)
  746. break; /* We stopped early */
  747. if (bh->state == BUF_STATE_FULL) {
  748. smp_rmb();
  749. common->next_buffhd_to_drain = bh->next;
  750. bh->state = BUF_STATE_EMPTY;
  751. /* Did something go wrong with the transfer? */
  752. if (bh->outreq->status != 0) {
  753. curlun->sense_data = SS_COMMUNICATION_FAILURE;
  754. curlun->sense_data_info =
  755. file_offset >> curlun->blkbits;
  756. curlun->info_valid = 1;
  757. break;
  758. }
  759. amount = bh->outreq->actual;
  760. if (curlun->file_length - file_offset < amount) {
  761. LERROR(curlun,
  762. "write %u @ %llu beyond end %llu\n",
  763. amount, (unsigned long long)file_offset,
  764. (unsigned long long)curlun->file_length);
  765. amount = curlun->file_length - file_offset;
  766. }
  767. /* Don't accept excess data. The spec doesn't say
  768. * what to do in this case. We'll ignore the error.
  769. */
  770. amount = min(amount, bh->bulk_out_intended_length);
  771. /* Don't write a partial block */
  772. amount = round_down(amount, curlun->blksize);
  773. if (amount == 0)
  774. goto empty_write;
  775. /* Perform the write */
  776. file_offset_tmp = file_offset;
  777. nwritten = vfs_write(curlun->filp,
  778. (char __user *)bh->buf,
  779. amount, &file_offset_tmp);
  780. VLDBG(curlun, "file write %u @ %llu -> %d\n", amount,
  781. (unsigned long long)file_offset, (int)nwritten);
  782. if (signal_pending(current))
  783. return -EINTR; /* Interrupted! */
  784. if (nwritten < 0) {
  785. LDBG(curlun, "error in file write: %d\n",
  786. (int)nwritten);
  787. nwritten = 0;
  788. } else if (nwritten < amount) {
  789. LDBG(curlun, "partial file write: %d/%u\n",
  790. (int)nwritten, amount);
  791. nwritten = round_down(nwritten, curlun->blksize);
  792. }
  793. file_offset += nwritten;
  794. amount_left_to_write -= nwritten;
  795. common->residue -= nwritten;
  796. /* If an error occurred, report it and its position */
  797. if (nwritten < amount) {
  798. curlun->sense_data = SS_WRITE_ERROR;
  799. curlun->sense_data_info =
  800. file_offset >> curlun->blkbits;
  801. curlun->info_valid = 1;
  802. break;
  803. }
  804. empty_write:
  805. /* Did the host decide to stop early? */
  806. if (bh->outreq->actual < bh->bulk_out_intended_length) {
  807. common->short_packet_received = 1;
  808. break;
  809. }
  810. continue;
  811. }
  812. /* Wait for something to happen */
  813. rc = sleep_thread(common);
  814. if (rc)
  815. return rc;
  816. }
  817. return -EIO; /* No default reply */
  818. }
  819. /*-------------------------------------------------------------------------*/
  820. static int do_synchronize_cache(struct fsg_common *common)
  821. {
  822. struct fsg_lun *curlun = common->curlun;
  823. int rc;
  824. /* We ignore the requested LBA and write out all file's
  825. * dirty data buffers. */
  826. rc = fsg_lun_fsync_sub(curlun);
  827. if (rc)
  828. curlun->sense_data = SS_WRITE_ERROR;
  829. return 0;
  830. }
  831. /*-------------------------------------------------------------------------*/
  832. static void invalidate_sub(struct fsg_lun *curlun)
  833. {
  834. struct file *filp = curlun->filp;
  835. struct inode *inode = file_inode(filp);
  836. unsigned long rc;
  837. rc = invalidate_mapping_pages(inode->i_mapping, 0, -1);
  838. VLDBG(curlun, "invalidate_mapping_pages -> %ld\n", rc);
  839. }
  840. static int do_verify(struct fsg_common *common)
  841. {
  842. struct fsg_lun *curlun = common->curlun;
  843. u32 lba;
  844. u32 verification_length;
  845. struct fsg_buffhd *bh = common->next_buffhd_to_fill;
  846. loff_t file_offset, file_offset_tmp;
  847. u32 amount_left;
  848. unsigned int amount;
  849. ssize_t nread;
  850. /*
  851. * Get the starting Logical Block Address and check that it's
  852. * not too big.
  853. */
  854. lba = get_unaligned_be32(&common->cmnd[2]);
  855. if (lba >= curlun->num_sectors) {
  856. curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  857. return -EINVAL;
  858. }
  859. /*
  860. * We allow DPO (Disable Page Out = don't save data in the
  861. * cache) but we don't implement it.
  862. */
  863. if (common->cmnd[1] & ~0x10) {
  864. curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  865. return -EINVAL;
  866. }
  867. verification_length = get_unaligned_be16(&common->cmnd[7]);
  868. if (unlikely(verification_length == 0))
  869. return -EIO; /* No default reply */
  870. /* Prepare to carry out the file verify */
  871. amount_left = verification_length << curlun->blkbits;
  872. file_offset = ((loff_t) lba) << curlun->blkbits;
  873. /* Write out all the dirty buffers before invalidating them */
  874. fsg_lun_fsync_sub(curlun);
  875. if (signal_pending(current))
  876. return -EINTR;
  877. invalidate_sub(curlun);
  878. if (signal_pending(current))
  879. return -EINTR;
  880. /* Just try to read the requested blocks */
  881. while (amount_left > 0) {
  882. /*
  883. * Figure out how much we need to read:
  884. * Try to read the remaining amount, but not more than
  885. * the buffer size.
  886. * And don't try to read past the end of the file.
  887. */
  888. amount = min(amount_left, FSG_BUFLEN);
  889. amount = min((loff_t)amount,
  890. curlun->file_length - file_offset);
  891. if (amount == 0) {
  892. curlun->sense_data =
  893. SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  894. curlun->sense_data_info =
  895. file_offset >> curlun->blkbits;
  896. curlun->info_valid = 1;
  897. break;
  898. }
  899. /* Perform the read */
  900. file_offset_tmp = file_offset;
  901. nread = vfs_read(curlun->filp,
  902. (char __user *) bh->buf,
  903. amount, &file_offset_tmp);
  904. VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
  905. (unsigned long long) file_offset,
  906. (int) nread);
  907. if (signal_pending(current))
  908. return -EINTR;
  909. if (nread < 0) {
  910. LDBG(curlun, "error in file verify: %d\n", (int)nread);
  911. nread = 0;
  912. } else if (nread < amount) {
  913. LDBG(curlun, "partial file verify: %d/%u\n",
  914. (int)nread, amount);
  915. nread = round_down(nread, curlun->blksize);
  916. }
  917. if (nread == 0) {
  918. curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
  919. curlun->sense_data_info =
  920. file_offset >> curlun->blkbits;
  921. curlun->info_valid = 1;
  922. break;
  923. }
  924. file_offset += nread;
  925. amount_left -= nread;
  926. }
  927. return 0;
  928. }
  929. /*-------------------------------------------------------------------------*/
  930. static int do_inquiry(struct fsg_common *common, struct fsg_buffhd *bh)
  931. {
  932. struct fsg_lun *curlun = common->curlun;
  933. u8 *buf = (u8 *) bh->buf;
  934. if (!curlun) { /* Unsupported LUNs are okay */
  935. common->bad_lun_okay = 1;
  936. memset(buf, 0, 36);
  937. buf[0] = 0x7f; /* Unsupported, no device-type */
  938. buf[4] = 31; /* Additional length */
  939. return 36;
  940. }
  941. buf[0] = curlun->cdrom ? TYPE_ROM : TYPE_DISK;
  942. buf[1] = curlun->removable ? 0x80 : 0;
  943. buf[2] = 2; /* ANSI SCSI level 2 */
  944. buf[3] = 2; /* SCSI-2 INQUIRY data format */
  945. buf[4] = 31; /* Additional length */
  946. buf[5] = 0; /* No special options */
  947. buf[6] = 0;
  948. buf[7] = 0;
  949. memcpy(buf + 8, common->inquiry_string, sizeof common->inquiry_string);
  950. return 36;
  951. }
  952. static int do_request_sense(struct fsg_common *common, struct fsg_buffhd *bh)
  953. {
  954. struct fsg_lun *curlun = common->curlun;
  955. u8 *buf = (u8 *) bh->buf;
  956. u32 sd, sdinfo;
  957. int valid;
  958. /*
  959. * From the SCSI-2 spec., section 7.9 (Unit attention condition):
  960. *
  961. * If a REQUEST SENSE command is received from an initiator
  962. * with a pending unit attention condition (before the target
  963. * generates the contingent allegiance condition), then the
  964. * target shall either:
  965. * a) report any pending sense data and preserve the unit
  966. * attention condition on the logical unit, or,
  967. * b) report the unit attention condition, may discard any
  968. * pending sense data, and clear the unit attention
  969. * condition on the logical unit for that initiator.
  970. *
  971. * FSG normally uses option a); enable this code to use option b).
  972. */
  973. #if 0
  974. if (curlun && curlun->unit_attention_data != SS_NO_SENSE) {
  975. curlun->sense_data = curlun->unit_attention_data;
  976. curlun->unit_attention_data = SS_NO_SENSE;
  977. }
  978. #endif
  979. if (!curlun) { /* Unsupported LUNs are okay */
  980. common->bad_lun_okay = 1;
  981. sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
  982. sdinfo = 0;
  983. valid = 0;
  984. } else {
  985. sd = curlun->sense_data;
  986. sdinfo = curlun->sense_data_info;
  987. valid = curlun->info_valid << 7;
  988. curlun->sense_data = SS_NO_SENSE;
  989. curlun->sense_data_info = 0;
  990. curlun->info_valid = 0;
  991. }
  992. memset(buf, 0, 18);
  993. buf[0] = valid | 0x70; /* Valid, current error */
  994. buf[2] = SK(sd);
  995. put_unaligned_be32(sdinfo, &buf[3]); /* Sense information */
  996. buf[7] = 18 - 8; /* Additional sense length */
  997. buf[12] = ASC(sd);
  998. buf[13] = ASCQ(sd);
  999. return 18;
  1000. }
  1001. static int do_read_capacity(struct fsg_common *common, struct fsg_buffhd *bh)
  1002. {
  1003. struct fsg_lun *curlun = common->curlun;
  1004. u32 lba = get_unaligned_be32(&common->cmnd[2]);
  1005. int pmi = common->cmnd[8];
  1006. u8 *buf = (u8 *)bh->buf;
  1007. /* Check the PMI and LBA fields */
  1008. if (pmi > 1 || (pmi == 0 && lba != 0)) {
  1009. curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  1010. return -EINVAL;
  1011. }
  1012. put_unaligned_be32(curlun->num_sectors - 1, &buf[0]);
  1013. /* Max logical block */
  1014. put_unaligned_be32(curlun->blksize, &buf[4]);/* Block length */
  1015. return 8;
  1016. }
  1017. static int do_read_header(struct fsg_common *common, struct fsg_buffhd *bh)
  1018. {
  1019. struct fsg_lun *curlun = common->curlun;
  1020. int msf = common->cmnd[1] & 0x02;
  1021. u32 lba = get_unaligned_be32(&common->cmnd[2]);
  1022. u8 *buf = (u8 *)bh->buf;
  1023. if (common->cmnd[1] & ~0x02) { /* Mask away MSF */
  1024. curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  1025. return -EINVAL;
  1026. }
  1027. if (lba >= curlun->num_sectors) {
  1028. curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  1029. return -EINVAL;
  1030. }
  1031. memset(buf, 0, 8);
  1032. buf[0] = 0x01; /* 2048 bytes of user data, rest is EC */
  1033. store_cdrom_address(&buf[4], msf, lba);
  1034. return 8;
  1035. }
  1036. static int do_read_toc(struct fsg_common *common, struct fsg_buffhd *bh)
  1037. {
  1038. struct fsg_lun *curlun = common->curlun;
  1039. int msf = common->cmnd[1] & 0x02;
  1040. int start_track = common->cmnd[6];
  1041. u8 *buf = (u8 *)bh->buf;
  1042. if ((common->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */
  1043. start_track > 1) {
  1044. curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  1045. return -EINVAL;
  1046. }
  1047. memset(buf, 0, 20);
  1048. buf[1] = (20-2); /* TOC data length */
  1049. buf[2] = 1; /* First track number */
  1050. buf[3] = 1; /* Last track number */
  1051. buf[5] = 0x16; /* Data track, copying allowed */
  1052. buf[6] = 0x01; /* Only track is number 1 */
  1053. store_cdrom_address(&buf[8], msf, 0);
  1054. buf[13] = 0x16; /* Lead-out track is data */
  1055. buf[14] = 0xAA; /* Lead-out track number */
  1056. store_cdrom_address(&buf[16], msf, curlun->num_sectors);
  1057. return 20;
  1058. }
  1059. static int do_mode_sense(struct fsg_common *common, struct fsg_buffhd *bh)
  1060. {
  1061. struct fsg_lun *curlun = common->curlun;
  1062. int mscmnd = common->cmnd[0];
  1063. u8 *buf = (u8 *) bh->buf;
  1064. u8 *buf0 = buf;
  1065. int pc, page_code;
  1066. int changeable_values, all_pages;
  1067. int valid_page = 0;
  1068. int len, limit;
  1069. if ((common->cmnd[1] & ~0x08) != 0) { /* Mask away DBD */
  1070. curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  1071. return -EINVAL;
  1072. }
  1073. pc = common->cmnd[2] >> 6;
  1074. page_code = common->cmnd[2] & 0x3f;
  1075. if (pc == 3) {
  1076. curlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
  1077. return -EINVAL;
  1078. }
  1079. changeable_values = (pc == 1);
  1080. all_pages = (page_code == 0x3f);
  1081. /*
  1082. * Write the mode parameter header. Fixed values are: default
  1083. * medium type, no cache control (DPOFUA), and no block descriptors.
  1084. * The only variable value is the WriteProtect bit. We will fill in
  1085. * the mode data length later.
  1086. */
  1087. memset(buf, 0, 8);
  1088. if (mscmnd == MODE_SENSE) {
  1089. buf[2] = (curlun->ro ? 0x80 : 0x00); /* WP, DPOFUA */
  1090. buf += 4;
  1091. limit = 255;
  1092. } else { /* MODE_SENSE_10 */
  1093. buf[3] = (curlun->ro ? 0x80 : 0x00); /* WP, DPOFUA */
  1094. buf += 8;
  1095. limit = 65535; /* Should really be FSG_BUFLEN */
  1096. }
  1097. /* No block descriptors */
  1098. /*
  1099. * The mode pages, in numerical order. The only page we support
  1100. * is the Caching page.
  1101. */
  1102. if (page_code == 0x08 || all_pages) {
  1103. valid_page = 1;
  1104. buf[0] = 0x08; /* Page code */
  1105. buf[1] = 10; /* Page length */
  1106. memset(buf+2, 0, 10); /* None of the fields are changeable */
  1107. if (!changeable_values) {
  1108. buf[2] = 0x04; /* Write cache enable, */
  1109. /* Read cache not disabled */
  1110. /* No cache retention priorities */
  1111. put_unaligned_be16(0xffff, &buf[4]);
  1112. /* Don't disable prefetch */
  1113. /* Minimum prefetch = 0 */
  1114. put_unaligned_be16(0xffff, &buf[8]);
  1115. /* Maximum prefetch */
  1116. put_unaligned_be16(0xffff, &buf[10]);
  1117. /* Maximum prefetch ceiling */
  1118. }
  1119. buf += 12;
  1120. }
  1121. /*
  1122. * Check that a valid page was requested and the mode data length
  1123. * isn't too long.
  1124. */
  1125. len = buf - buf0;
  1126. if (!valid_page || len > limit) {
  1127. curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  1128. return -EINVAL;
  1129. }
  1130. /* Store the mode data length */
  1131. if (mscmnd == MODE_SENSE)
  1132. buf0[0] = len - 1;
  1133. else
  1134. put_unaligned_be16(len - 2, buf0);
  1135. return len;
  1136. }
  1137. static int do_start_stop(struct fsg_common *common)
  1138. {
  1139. struct fsg_lun *curlun = common->curlun;
  1140. int loej, start;
  1141. if (!curlun) {
  1142. return -EINVAL;
  1143. } else if (!curlun->removable) {
  1144. curlun->sense_data = SS_INVALID_COMMAND;
  1145. return -EINVAL;
  1146. } else if ((common->cmnd[1] & ~0x01) != 0 || /* Mask away Immed */
  1147. (common->cmnd[4] & ~0x03) != 0) { /* Mask LoEj, Start */
  1148. curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  1149. return -EINVAL;
  1150. }
  1151. loej = common->cmnd[4] & 0x02;
  1152. start = common->cmnd[4] & 0x01;
  1153. /*
  1154. * Our emulation doesn't support mounting; the medium is
  1155. * available for use as soon as it is loaded.
  1156. */
  1157. if (start) {
  1158. if (!fsg_lun_is_open(curlun)) {
  1159. curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
  1160. return -EINVAL;
  1161. }
  1162. return 0;
  1163. }
  1164. /* Are we allowed to unload the media? */
  1165. if (curlun->prevent_medium_removal) {
  1166. LDBG(curlun, "unload attempt prevented\n");
  1167. curlun->sense_data = SS_MEDIUM_REMOVAL_PREVENTED;
  1168. return -EINVAL;
  1169. }
  1170. if (!loej)
  1171. return 0;
  1172. up_read(&common->filesem);
  1173. down_write(&common->filesem);
  1174. fsg_lun_close(curlun);
  1175. up_write(&common->filesem);
  1176. down_read(&common->filesem);
  1177. return 0;
  1178. }
  1179. static int do_prevent_allow(struct fsg_common *common)
  1180. {
  1181. struct fsg_lun *curlun = common->curlun;
  1182. int prevent;
  1183. if (!common->curlun) {
  1184. return -EINVAL;
  1185. } else if (!common->curlun->removable) {
  1186. common->curlun->sense_data = SS_INVALID_COMMAND;
  1187. return -EINVAL;
  1188. }
  1189. prevent = common->cmnd[4] & 0x01;
  1190. if ((common->cmnd[4] & ~0x01) != 0) { /* Mask away Prevent */
  1191. curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  1192. return -EINVAL;
  1193. }
  1194. if (curlun->prevent_medium_removal && !prevent)
  1195. fsg_lun_fsync_sub(curlun);
  1196. curlun->prevent_medium_removal = prevent;
  1197. return 0;
  1198. }
  1199. static int do_read_format_capacities(struct fsg_common *common,
  1200. struct fsg_buffhd *bh)
  1201. {
  1202. struct fsg_lun *curlun = common->curlun;
  1203. u8 *buf = (u8 *) bh->buf;
  1204. buf[0] = buf[1] = buf[2] = 0;
  1205. buf[3] = 8; /* Only the Current/Maximum Capacity Descriptor */
  1206. buf += 4;
  1207. put_unaligned_be32(curlun->num_sectors, &buf[0]);
  1208. /* Number of blocks */
  1209. put_unaligned_be32(curlun->blksize, &buf[4]);/* Block length */
  1210. buf[4] = 0x02; /* Current capacity */
  1211. return 12;
  1212. }
  1213. static int do_mode_select(struct fsg_common *common, struct fsg_buffhd *bh)
  1214. {
  1215. struct fsg_lun *curlun = common->curlun;
  1216. /* We don't support MODE SELECT */
  1217. if (curlun)
  1218. curlun->sense_data = SS_INVALID_COMMAND;
  1219. return -EINVAL;
  1220. }
  1221. /*-------------------------------------------------------------------------*/
  1222. static int halt_bulk_in_endpoint(struct fsg_dev *fsg)
  1223. {
  1224. int rc;
  1225. rc = fsg_set_halt(fsg, fsg->bulk_in);
  1226. if (rc == -EAGAIN)
  1227. VDBG(fsg, "delayed bulk-in endpoint halt\n");
  1228. while (rc != 0) {
  1229. if (rc != -EAGAIN) {
  1230. WARNING(fsg, "usb_ep_set_halt -> %d\n", rc);
  1231. rc = 0;
  1232. break;
  1233. }
  1234. /* Wait for a short time and then try again */
  1235. if (msleep_interruptible(100) != 0)
  1236. return -EINTR;
  1237. rc = usb_ep_set_halt(fsg->bulk_in);
  1238. }
  1239. return rc;
  1240. }
  1241. static int wedge_bulk_in_endpoint(struct fsg_dev *fsg)
  1242. {
  1243. int rc;
  1244. DBG(fsg, "bulk-in set wedge\n");
  1245. rc = usb_ep_set_wedge(fsg->bulk_in);
  1246. if (rc == -EAGAIN)
  1247. VDBG(fsg, "delayed bulk-in endpoint wedge\n");
  1248. while (rc != 0) {
  1249. if (rc != -EAGAIN) {
  1250. WARNING(fsg, "usb_ep_set_wedge -> %d\n", rc);
  1251. rc = 0;
  1252. break;
  1253. }
  1254. /* Wait for a short time and then try again */
  1255. if (msleep_interruptible(100) != 0)
  1256. return -EINTR;
  1257. rc = usb_ep_set_wedge(fsg->bulk_in);
  1258. }
  1259. return rc;
  1260. }
  1261. static int throw_away_data(struct fsg_common *common)
  1262. {
  1263. struct fsg_buffhd *bh;
  1264. u32 amount;
  1265. int rc;
  1266. for (bh = common->next_buffhd_to_drain;
  1267. bh->state != BUF_STATE_EMPTY || common->usb_amount_left > 0;
  1268. bh = common->next_buffhd_to_drain) {
  1269. /* Throw away the data in a filled buffer */
  1270. if (bh->state == BUF_STATE_FULL) {
  1271. smp_rmb();
  1272. bh->state = BUF_STATE_EMPTY;
  1273. common->next_buffhd_to_drain = bh->next;
  1274. /* A short packet or an error ends everything */
  1275. if (bh->outreq->actual < bh->bulk_out_intended_length ||
  1276. bh->outreq->status != 0) {
  1277. raise_exception(common,
  1278. FSG_STATE_ABORT_BULK_OUT);
  1279. return -EINTR;
  1280. }
  1281. continue;
  1282. }
  1283. /* Try to submit another request if we need one */
  1284. bh = common->next_buffhd_to_fill;
  1285. if (bh->state == BUF_STATE_EMPTY
  1286. && common->usb_amount_left > 0) {
  1287. amount = min(common->usb_amount_left, FSG_BUFLEN);
  1288. /*
  1289. * Except at the end of the transfer, amount will be
  1290. * equal to the buffer size, which is divisible by
  1291. * the bulk-out maxpacket size.
  1292. */
  1293. set_bulk_out_req_length(common, bh, amount);
  1294. if (!start_out_transfer(common, bh))
  1295. /* Dunno what to do if common->fsg is NULL */
  1296. return -EIO;
  1297. common->next_buffhd_to_fill = bh->next;
  1298. common->usb_amount_left -= amount;
  1299. continue;
  1300. }
  1301. /* Otherwise wait for something to happen */
  1302. rc = sleep_thread(common);
  1303. if (rc)
  1304. return rc;
  1305. }
  1306. return 0;
  1307. }
  1308. static int finish_reply(struct fsg_common *common)
  1309. {
  1310. struct fsg_buffhd *bh = common->next_buffhd_to_fill;
  1311. int rc = 0;
  1312. switch (common->data_dir) {
  1313. case DATA_DIR_NONE:
  1314. break; /* Nothing to send */
  1315. /*
  1316. * If we don't know whether the host wants to read or write,
  1317. * this must be CB or CBI with an unknown command. We mustn't
  1318. * try to send or receive any data. So stall both bulk pipes
  1319. * if we can and wait for a reset.
  1320. */
  1321. case DATA_DIR_UNKNOWN:
  1322. if (!common->can_stall) {
  1323. /* Nothing */
  1324. } else if (fsg_is_set(common)) {
  1325. fsg_set_halt(common->fsg, common->fsg->bulk_out);
  1326. rc = halt_bulk_in_endpoint(common->fsg);
  1327. } else {
  1328. /* Don't know what to do if common->fsg is NULL */
  1329. rc = -EIO;
  1330. }
  1331. break;
  1332. /* All but the last buffer of data must have already been sent */
  1333. case DATA_DIR_TO_HOST:
  1334. if (common->data_size == 0) {
  1335. /* Nothing to send */
  1336. /* Don't know what to do if common->fsg is NULL */
  1337. } else if (!fsg_is_set(common)) {
  1338. rc = -EIO;
  1339. /* If there's no residue, simply send the last buffer */
  1340. } else if (common->residue == 0) {
  1341. bh->inreq->zero = 0;
  1342. if (!start_in_transfer(common, bh))
  1343. return -EIO;
  1344. common->next_buffhd_to_fill = bh->next;
  1345. /*
  1346. * For Bulk-only, mark the end of the data with a short
  1347. * packet. If we are allowed to stall, halt the bulk-in
  1348. * endpoint. (Note: This violates the Bulk-Only Transport
  1349. * specification, which requires us to pad the data if we
  1350. * don't halt the endpoint. Presumably nobody will mind.)
  1351. */
  1352. } else {
  1353. bh->inreq->zero = 1;
  1354. if (!start_in_transfer(common, bh))
  1355. rc = -EIO;
  1356. common->next_buffhd_to_fill = bh->next;
  1357. if (common->can_stall)
  1358. rc = halt_bulk_in_endpoint(common->fsg);
  1359. }
  1360. break;
  1361. /*
  1362. * We have processed all we want from the data the host has sent.
  1363. * There may still be outstanding bulk-out requests.
  1364. */
  1365. case DATA_DIR_FROM_HOST:
  1366. if (common->residue == 0) {
  1367. /* Nothing to receive */
  1368. /* Did the host stop sending unexpectedly early? */
  1369. } else if (common->short_packet_received) {
  1370. raise_exception(common, FSG_STATE_ABORT_BULK_OUT);
  1371. rc = -EINTR;
  1372. /*
  1373. * We haven't processed all the incoming data. Even though
  1374. * we may be allowed to stall, doing so would cause a race.
  1375. * The controller may already have ACK'ed all the remaining
  1376. * bulk-out packets, in which case the host wouldn't see a
  1377. * STALL. Not realizing the endpoint was halted, it wouldn't
  1378. * clear the halt -- leading to problems later on.
  1379. */
  1380. #if 0
  1381. } else if (common->can_stall) {
  1382. if (fsg_is_set(common))
  1383. fsg_set_halt(common->fsg,
  1384. common->fsg->bulk_out);
  1385. raise_exception(common, FSG_STATE_ABORT_BULK_OUT);
  1386. rc = -EINTR;
  1387. #endif
  1388. /*
  1389. * We can't stall. Read in the excess data and throw it
  1390. * all away.
  1391. */
  1392. } else {
  1393. rc = throw_away_data(common);
  1394. }
  1395. break;
  1396. }
  1397. return rc;
  1398. }
  1399. static int send_status(struct fsg_common *common)
  1400. {
  1401. struct fsg_lun *curlun = common->curlun;
  1402. struct fsg_buffhd *bh;
  1403. struct bulk_cs_wrap *csw;
  1404. int rc;
  1405. u8 status = US_BULK_STAT_OK;
  1406. u32 sd, sdinfo = 0;
  1407. /* Wait for the next buffer to become available */
  1408. bh = common->next_buffhd_to_fill;
  1409. while (bh->state != BUF_STATE_EMPTY) {
  1410. rc = sleep_thread(common);
  1411. if (rc)
  1412. return rc;
  1413. }
  1414. if (curlun) {
  1415. sd = curlun->sense_data;
  1416. sdinfo = curlun->sense_data_info;
  1417. } else if (common->bad_lun_okay)
  1418. sd = SS_NO_SENSE;
  1419. else
  1420. sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
  1421. if (common->phase_error) {
  1422. DBG(common, "sending phase-error status\n");
  1423. status = US_BULK_STAT_PHASE;
  1424. sd = SS_INVALID_COMMAND;
  1425. } else if (sd != SS_NO_SENSE) {
  1426. DBG(common, "sending command-failure status\n");
  1427. status = US_BULK_STAT_FAIL;
  1428. VDBG(common, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;"
  1429. " info x%x\n",
  1430. SK(sd), ASC(sd), ASCQ(sd), sdinfo);
  1431. }
  1432. /* Store and send the Bulk-only CSW */
  1433. csw = (void *)bh->buf;
  1434. csw->Signature = cpu_to_le32(US_BULK_CS_SIGN);
  1435. csw->Tag = common->tag;
  1436. csw->Residue = cpu_to_le32(common->residue);
  1437. csw->Status = status;
  1438. bh->inreq->length = US_BULK_CS_WRAP_LEN;
  1439. bh->inreq->zero = 0;
  1440. if (!start_in_transfer(common, bh))
  1441. /* Don't know what to do if common->fsg is NULL */
  1442. return -EIO;
  1443. common->next_buffhd_to_fill = bh->next;
  1444. return 0;
  1445. }
  1446. /*-------------------------------------------------------------------------*/
  1447. /*
  1448. * Check whether the command is properly formed and whether its data size
  1449. * and direction agree with the values we already have.
  1450. */
  1451. static int check_command(struct fsg_common *common, int cmnd_size,
  1452. enum data_direction data_dir, unsigned int mask,
  1453. int needs_medium, const char *name)
  1454. {
  1455. int i;
  1456. unsigned int lun = common->cmnd[1] >> 5;
  1457. static const char dirletter[4] = {'u', 'o', 'i', 'n'};
  1458. char hdlen[20];
  1459. struct fsg_lun *curlun;
  1460. hdlen[0] = 0;
  1461. if (common->data_dir != DATA_DIR_UNKNOWN)
  1462. sprintf(hdlen, ", H%c=%u", dirletter[(int) common->data_dir],
  1463. common->data_size);
  1464. VDBG(common, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n",
  1465. name, cmnd_size, dirletter[(int) data_dir],
  1466. common->data_size_from_cmnd, common->cmnd_size, hdlen);
  1467. /*
  1468. * We can't reply at all until we know the correct data direction
  1469. * and size.
  1470. */
  1471. if (common->data_size_from_cmnd == 0)
  1472. data_dir = DATA_DIR_NONE;
  1473. if (common->data_size < common->data_size_from_cmnd) {
  1474. /*
  1475. * Host data size < Device data size is a phase error.
  1476. * Carry out the command, but only transfer as much as
  1477. * we are allowed.
  1478. */
  1479. common->data_size_from_cmnd = common->data_size;
  1480. common->phase_error = 1;
  1481. }
  1482. common->residue = common->data_size;
  1483. common->usb_amount_left = common->data_size;
  1484. /* Conflicting data directions is a phase error */
  1485. if (common->data_dir != data_dir && common->data_size_from_cmnd > 0) {
  1486. common->phase_error = 1;
  1487. return -EINVAL;
  1488. }
  1489. /* Verify the length of the command itself */
  1490. if (cmnd_size != common->cmnd_size) {
  1491. /*
  1492. * Special case workaround: There are plenty of buggy SCSI
  1493. * implementations. Many have issues with cbw->Length
  1494. * field passing a wrong command size. For those cases we
  1495. * always try to work around the problem by using the length
  1496. * sent by the host side provided it is at least as large
  1497. * as the correct command length.
  1498. * Examples of such cases would be MS-Windows, which issues
  1499. * REQUEST SENSE with cbw->Length == 12 where it should
  1500. * be 6, and xbox360 issuing INQUIRY, TEST UNIT READY and
  1501. * REQUEST SENSE with cbw->Length == 10 where it should
  1502. * be 6 as well.
  1503. */
  1504. if (cmnd_size <= common->cmnd_size) {
  1505. DBG(common, "%s is buggy! Expected length %d "
  1506. "but we got %d\n", name,
  1507. cmnd_size, common->cmnd_size);
  1508. cmnd_size = common->cmnd_size;
  1509. } else {
  1510. common->phase_error = 1;
  1511. return -EINVAL;
  1512. }
  1513. }
  1514. /* Check that the LUN values are consistent */
  1515. if (common->lun != lun)
  1516. DBG(common, "using LUN %u from CBW, not LUN %u from CDB\n",
  1517. common->lun, lun);
  1518. /* Check the LUN */
  1519. curlun = common->curlun;
  1520. if (curlun) {
  1521. if (common->cmnd[0] != REQUEST_SENSE) {
  1522. curlun->sense_data = SS_NO_SENSE;
  1523. curlun->sense_data_info = 0;
  1524. curlun->info_valid = 0;
  1525. }
  1526. } else {
  1527. common->bad_lun_okay = 0;
  1528. /*
  1529. * INQUIRY and REQUEST SENSE commands are explicitly allowed
  1530. * to use unsupported LUNs; all others may not.
  1531. */
  1532. if (common->cmnd[0] != INQUIRY &&
  1533. common->cmnd[0] != REQUEST_SENSE) {
  1534. DBG(common, "unsupported LUN %u\n", common->lun);
  1535. return -EINVAL;
  1536. }
  1537. }
  1538. /*
  1539. * If a unit attention condition exists, only INQUIRY and
  1540. * REQUEST SENSE commands are allowed; anything else must fail.
  1541. */
  1542. if (curlun && curlun->unit_attention_data != SS_NO_SENSE &&
  1543. common->cmnd[0] != INQUIRY &&
  1544. common->cmnd[0] != REQUEST_SENSE) {
  1545. curlun->sense_data = curlun->unit_attention_data;
  1546. curlun->unit_attention_data = SS_NO_SENSE;
  1547. return -EINVAL;
  1548. }
  1549. /* Check that only command bytes listed in the mask are non-zero */
  1550. common->cmnd[1] &= 0x1f; /* Mask away the LUN */
  1551. for (i = 1; i < cmnd_size; ++i) {
  1552. if (common->cmnd[i] && !(mask & (1 << i))) {
  1553. if (curlun)
  1554. curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  1555. return -EINVAL;
  1556. }
  1557. }
  1558. /* If the medium isn't mounted and the command needs to access
  1559. * it, return an error. */
  1560. if (curlun && !fsg_lun_is_open(curlun) && needs_medium) {
  1561. curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
  1562. return -EINVAL;
  1563. }
  1564. return 0;
  1565. }
  1566. /* wrapper of check_command for data size in blocks handling */
  1567. static int check_command_size_in_blocks(struct fsg_common *common,
  1568. int cmnd_size, enum data_direction data_dir,
  1569. unsigned int mask, int needs_medium, const char *name)
  1570. {
  1571. if (common->curlun)
  1572. common->data_size_from_cmnd <<= common->curlun->blkbits;
  1573. return check_command(common, cmnd_size, data_dir,
  1574. mask, needs_medium, name);
  1575. }
  1576. static int do_scsi_command(struct fsg_common *common)
  1577. {
  1578. struct fsg_buffhd *bh;
  1579. int rc;
  1580. int reply = -EINVAL;
  1581. int i;
  1582. static char unknown[16];
  1583. dump_cdb(common);
  1584. /* Wait for the next buffer to become available for data or status */
  1585. bh = common->next_buffhd_to_fill;
  1586. common->next_buffhd_to_drain = bh;
  1587. while (bh->state != BUF_STATE_EMPTY) {
  1588. rc = sleep_thread(common);
  1589. if (rc)
  1590. return rc;
  1591. }
  1592. common->phase_error = 0;
  1593. common->short_packet_received = 0;
  1594. down_read(&common->filesem); /* We're using the backing file */
  1595. switch (common->cmnd[0]) {
  1596. case INQUIRY:
  1597. common->data_size_from_cmnd = common->cmnd[4];
  1598. reply = check_command(common, 6, DATA_DIR_TO_HOST,
  1599. (1<<4), 0,
  1600. "INQUIRY");
  1601. if (reply == 0)
  1602. reply = do_inquiry(common, bh);
  1603. break;
  1604. case MODE_SELECT:
  1605. common->data_size_from_cmnd = common->cmnd[4];
  1606. reply = check_command(common, 6, DATA_DIR_FROM_HOST,
  1607. (1<<1) | (1<<4), 0,
  1608. "MODE SELECT(6)");
  1609. if (reply == 0)
  1610. reply = do_mode_select(common, bh);
  1611. break;
  1612. case MODE_SELECT_10:
  1613. common->data_size_from_cmnd =
  1614. get_unaligned_be16(&common->cmnd[7]);
  1615. reply = check_command(common, 10, DATA_DIR_FROM_HOST,
  1616. (1<<1) | (3<<7), 0,
  1617. "MODE SELECT(10)");
  1618. if (reply == 0)
  1619. reply = do_mode_select(common, bh);
  1620. break;
  1621. case MODE_SENSE:
  1622. common->data_size_from_cmnd = common->cmnd[4];
  1623. reply = check_command(common, 6, DATA_DIR_TO_HOST,
  1624. (1<<1) | (1<<2) | (1<<4), 0,
  1625. "MODE SENSE(6)");
  1626. if (reply == 0)
  1627. reply = do_mode_sense(common, bh);
  1628. break;
  1629. case MODE_SENSE_10:
  1630. common->data_size_from_cmnd =
  1631. get_unaligned_be16(&common->cmnd[7]);
  1632. reply = check_command(common, 10, DATA_DIR_TO_HOST,
  1633. (1<<1) | (1<<2) | (3<<7), 0,
  1634. "MODE SENSE(10)");
  1635. if (reply == 0)
  1636. reply = do_mode_sense(common, bh);
  1637. break;
  1638. case ALLOW_MEDIUM_REMOVAL:
  1639. common->data_size_from_cmnd = 0;
  1640. reply = check_command(common, 6, DATA_DIR_NONE,
  1641. (1<<4), 0,
  1642. "PREVENT-ALLOW MEDIUM REMOVAL");
  1643. if (reply == 0)
  1644. reply = do_prevent_allow(common);
  1645. break;
  1646. case READ_6:
  1647. i = common->cmnd[4];
  1648. common->data_size_from_cmnd = (i == 0) ? 256 : i;
  1649. reply = check_command_size_in_blocks(common, 6,
  1650. DATA_DIR_TO_HOST,
  1651. (7<<1) | (1<<4), 1,
  1652. "READ(6)");
  1653. if (reply == 0)
  1654. reply = do_read(common);
  1655. break;
  1656. case READ_10:
  1657. common->data_size_from_cmnd =
  1658. get_unaligned_be16(&common->cmnd[7]);
  1659. reply = check_command_size_in_blocks(common, 10,
  1660. DATA_DIR_TO_HOST,
  1661. (1<<1) | (0xf<<2) | (3<<7), 1,
  1662. "READ(10)");
  1663. if (reply == 0)
  1664. reply = do_read(common);
  1665. break;
  1666. case READ_12:
  1667. common->data_size_from_cmnd =
  1668. get_unaligned_be32(&common->cmnd[6]);
  1669. reply = check_command_size_in_blocks(common, 12,
  1670. DATA_DIR_TO_HOST,
  1671. (1<<1) | (0xf<<2) | (0xf<<6), 1,
  1672. "READ(12)");
  1673. if (reply == 0)
  1674. reply = do_read(common);
  1675. break;
  1676. case READ_CAPACITY:
  1677. common->data_size_from_cmnd = 8;
  1678. reply = check_command(common, 10, DATA_DIR_TO_HOST,
  1679. (0xf<<2) | (1<<8), 1,
  1680. "READ CAPACITY");
  1681. if (reply == 0)
  1682. reply = do_read_capacity(common, bh);
  1683. break;
  1684. case READ_HEADER:
  1685. if (!common->curlun || !common->curlun->cdrom)
  1686. goto unknown_cmnd;
  1687. common->data_size_from_cmnd =
  1688. get_unaligned_be16(&common->cmnd[7]);
  1689. reply = check_command(common, 10, DATA_DIR_TO_HOST,
  1690. (3<<7) | (0x1f<<1), 1,
  1691. "READ HEADER");
  1692. if (reply == 0)
  1693. reply = do_read_header(common, bh);
  1694. break;
  1695. case READ_TOC:
  1696. if (!common->curlun || !common->curlun->cdrom)
  1697. goto unknown_cmnd;
  1698. common->data_size_from_cmnd =
  1699. get_unaligned_be16(&common->cmnd[7]);
  1700. reply = check_command(common, 10, DATA_DIR_TO_HOST,
  1701. (7<<6) | (1<<1), 1,
  1702. "READ TOC");
  1703. if (reply == 0)
  1704. reply = do_read_toc(common, bh);
  1705. break;
  1706. case READ_FORMAT_CAPACITIES:
  1707. common->data_size_from_cmnd =
  1708. get_unaligned_be16(&common->cmnd[7]);
  1709. reply = check_command(common, 10, DATA_DIR_TO_HOST,
  1710. (3<<7), 1,
  1711. "READ FORMAT CAPACITIES");
  1712. if (reply == 0)
  1713. reply = do_read_format_capacities(common, bh);
  1714. break;
  1715. case REQUEST_SENSE:
  1716. common->data_size_from_cmnd = common->cmnd[4];
  1717. reply = check_command(common, 6, DATA_DIR_TO_HOST,
  1718. (1<<4), 0,
  1719. "REQUEST SENSE");
  1720. if (reply == 0)
  1721. reply = do_request_sense(common, bh);
  1722. break;
  1723. case START_STOP:
  1724. common->data_size_from_cmnd = 0;
  1725. reply = check_command(common, 6, DATA_DIR_NONE,
  1726. (1<<1) | (1<<4), 0,
  1727. "START-STOP UNIT");
  1728. if (reply == 0)
  1729. reply = do_start_stop(common);
  1730. break;
  1731. case SYNCHRONIZE_CACHE:
  1732. common->data_size_from_cmnd = 0;
  1733. reply = check_command(common, 10, DATA_DIR_NONE,
  1734. (0xf<<2) | (3<<7), 1,
  1735. "SYNCHRONIZE CACHE");
  1736. if (reply == 0)
  1737. reply = do_synchronize_cache(common);
  1738. break;
  1739. case TEST_UNIT_READY:
  1740. common->data_size_from_cmnd = 0;
  1741. reply = check_command(common, 6, DATA_DIR_NONE,
  1742. 0, 1,
  1743. "TEST UNIT READY");
  1744. break;
  1745. /*
  1746. * Although optional, this command is used by MS-Windows. We
  1747. * support a minimal version: BytChk must be 0.
  1748. */
  1749. case VERIFY:
  1750. common->data_size_from_cmnd = 0;
  1751. reply = check_command(common, 10, DATA_DIR_NONE,
  1752. (1<<1) | (0xf<<2) | (3<<7), 1,
  1753. "VERIFY");
  1754. if (reply == 0)
  1755. reply = do_verify(common);
  1756. break;
  1757. case WRITE_6:
  1758. i = common->cmnd[4];
  1759. common->data_size_from_cmnd = (i == 0) ? 256 : i;
  1760. reply = check_command_size_in_blocks(common, 6,
  1761. DATA_DIR_FROM_HOST,
  1762. (7<<1) | (1<<4), 1,
  1763. "WRITE(6)");
  1764. if (reply == 0)
  1765. reply = do_write(common);
  1766. break;
  1767. case WRITE_10:
  1768. common->data_size_from_cmnd =
  1769. get_unaligned_be16(&common->cmnd[7]);
  1770. reply = check_command_size_in_blocks(common, 10,
  1771. DATA_DIR_FROM_HOST,
  1772. (1<<1) | (0xf<<2) | (3<<7), 1,
  1773. "WRITE(10)");
  1774. if (reply == 0)
  1775. reply = do_write(common);
  1776. break;
  1777. case WRITE_12:
  1778. common->data_size_from_cmnd =
  1779. get_unaligned_be32(&common->cmnd[6]);
  1780. reply = check_command_size_in_blocks(common, 12,
  1781. DATA_DIR_FROM_HOST,
  1782. (1<<1) | (0xf<<2) | (0xf<<6), 1,
  1783. "WRITE(12)");
  1784. if (reply == 0)
  1785. reply = do_write(common);
  1786. break;
  1787. /*
  1788. * Some mandatory commands that we recognize but don't implement.
  1789. * They don't mean much in this setting. It's left as an exercise
  1790. * for anyone interested to implement RESERVE and RELEASE in terms
  1791. * of Posix locks.
  1792. */
  1793. case FORMAT_UNIT:
  1794. case RELEASE:
  1795. case RESERVE:
  1796. case SEND_DIAGNOSTIC:
  1797. /* Fall through */
  1798. default:
  1799. unknown_cmnd:
  1800. common->data_size_from_cmnd = 0;
  1801. sprintf(unknown, "Unknown x%02x", common->cmnd[0]);
  1802. reply = check_command(common, common->cmnd_size,
  1803. DATA_DIR_UNKNOWN, ~0, 0, unknown);
  1804. if (reply == 0) {
  1805. common->curlun->sense_data = SS_INVALID_COMMAND;
  1806. reply = -EINVAL;
  1807. }
  1808. break;
  1809. }
  1810. up_read(&common->filesem);
  1811. if (reply == -EINTR || signal_pending(current))
  1812. return -EINTR;
  1813. /* Set up the single reply buffer for finish_reply() */
  1814. if (reply == -EINVAL)
  1815. reply = 0; /* Error reply length */
  1816. if (reply >= 0 && common->data_dir == DATA_DIR_TO_HOST) {
  1817. reply = min((u32)reply, common->data_size_from_cmnd);
  1818. bh->inreq->length = reply;
  1819. bh->state = BUF_STATE_FULL;
  1820. common->residue -= reply;
  1821. } /* Otherwise it's already set */
  1822. return 0;
  1823. }
  1824. /*-------------------------------------------------------------------------*/
  1825. static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  1826. {
  1827. struct usb_request *req = bh->outreq;
  1828. struct bulk_cb_wrap *cbw = req->buf;
  1829. struct fsg_common *common = fsg->common;
  1830. /* Was this a real packet? Should it be ignored? */
  1831. if (req->status || test_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
  1832. return -EINVAL;
  1833. /* Is the CBW valid? */
  1834. if (req->actual != US_BULK_CB_WRAP_LEN ||
  1835. cbw->Signature != cpu_to_le32(
  1836. US_BULK_CB_SIGN)) {
  1837. DBG(fsg, "invalid CBW: len %u sig 0x%x\n",
  1838. req->actual,
  1839. le32_to_cpu(cbw->Signature));
  1840. /*
  1841. * The Bulk-only spec says we MUST stall the IN endpoint
  1842. * (6.6.1), so it's unavoidable. It also says we must
  1843. * retain this state until the next reset, but there's
  1844. * no way to tell the controller driver it should ignore
  1845. * Clear-Feature(HALT) requests.
  1846. *
  1847. * We aren't required to halt the OUT endpoint; instead
  1848. * we can simply accept and discard any data received
  1849. * until the next reset.
  1850. */
  1851. wedge_bulk_in_endpoint(fsg);
  1852. set_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
  1853. return -EINVAL;
  1854. }
  1855. /* Is the CBW meaningful? */
  1856. if (cbw->Lun >= FSG_MAX_LUNS || cbw->Flags & ~US_BULK_FLAG_IN ||
  1857. cbw->Length <= 0 || cbw->Length > MAX_COMMAND_SIZE) {
  1858. DBG(fsg, "non-meaningful CBW: lun = %u, flags = 0x%x, "
  1859. "cmdlen %u\n",
  1860. cbw->Lun, cbw->Flags, cbw->Length);
  1861. /*
  1862. * We can do anything we want here, so let's stall the
  1863. * bulk pipes if we are allowed to.
  1864. */
  1865. if (common->can_stall) {
  1866. fsg_set_halt(fsg, fsg->bulk_out);
  1867. halt_bulk_in_endpoint(fsg);
  1868. }
  1869. return -EINVAL;
  1870. }
  1871. /* Save the command for later */
  1872. common->cmnd_size = cbw->Length;
  1873. memcpy(common->cmnd, cbw->CDB, common->cmnd_size);
  1874. if (cbw->Flags & US_BULK_FLAG_IN)
  1875. common->data_dir = DATA_DIR_TO_HOST;
  1876. else
  1877. common->data_dir = DATA_DIR_FROM_HOST;
  1878. common->data_size = le32_to_cpu(cbw->DataTransferLength);
  1879. if (common->data_size == 0)
  1880. common->data_dir = DATA_DIR_NONE;
  1881. common->lun = cbw->Lun;
  1882. if (common->lun < common->nluns)
  1883. common->curlun = common->luns[common->lun];
  1884. else
  1885. common->curlun = NULL;
  1886. common->tag = cbw->Tag;
  1887. return 0;
  1888. }
  1889. static int get_next_command(struct fsg_common *common)
  1890. {
  1891. struct fsg_buffhd *bh;
  1892. int rc = 0;
  1893. /* Wait for the next buffer to become available */
  1894. bh = common->next_buffhd_to_fill;
  1895. while (bh->state != BUF_STATE_EMPTY) {
  1896. rc = sleep_thread(common);
  1897. if (rc)
  1898. return rc;
  1899. }
  1900. /* Queue a request to read a Bulk-only CBW */
  1901. set_bulk_out_req_length(common, bh, US_BULK_CB_WRAP_LEN);
  1902. if (!start_out_transfer(common, bh))
  1903. /* Don't know what to do if common->fsg is NULL */
  1904. return -EIO;
  1905. /*
  1906. * We will drain the buffer in software, which means we
  1907. * can reuse it for the next filling. No need to advance
  1908. * next_buffhd_to_fill.
  1909. */
  1910. /* Wait for the CBW to arrive */
  1911. while (bh->state != BUF_STATE_FULL) {
  1912. rc = sleep_thread(common);
  1913. if (rc)
  1914. return rc;
  1915. }
  1916. smp_rmb();
  1917. rc = fsg_is_set(common) ? received_cbw(common->fsg, bh) : -EIO;
  1918. bh->state = BUF_STATE_EMPTY;
  1919. return rc;
  1920. }
  1921. /*-------------------------------------------------------------------------*/
  1922. static int alloc_request(struct fsg_common *common, struct usb_ep *ep,
  1923. struct usb_request **preq)
  1924. {
  1925. *preq = usb_ep_alloc_request(ep, GFP_ATOMIC);
  1926. if (*preq)
  1927. return 0;
  1928. ERROR(common, "can't allocate request for %s\n", ep->name);
  1929. return -ENOMEM;
  1930. }
  1931. /* Reset interface setting and re-init endpoint state (toggle etc). */
  1932. static int do_set_interface(struct fsg_common *common, struct fsg_dev *new_fsg)
  1933. {
  1934. struct fsg_dev *fsg;
  1935. int i, rc = 0;
  1936. if (common->running)
  1937. DBG(common, "reset interface\n");
  1938. reset:
  1939. /* Deallocate the requests */
  1940. if (common->fsg) {
  1941. fsg = common->fsg;
  1942. for (i = 0; i < common->fsg_num_buffers; ++i) {
  1943. struct fsg_buffhd *bh = &common->buffhds[i];
  1944. if (bh->inreq) {
  1945. usb_ep_free_request(fsg->bulk_in, bh->inreq);
  1946. bh->inreq = NULL;
  1947. }
  1948. if (bh->outreq) {
  1949. usb_ep_free_request(fsg->bulk_out, bh->outreq);
  1950. bh->outreq = NULL;
  1951. }
  1952. }
  1953. /* Disable the endpoints */
  1954. if (fsg->bulk_in_enabled) {
  1955. usb_ep_disable(fsg->bulk_in);
  1956. fsg->bulk_in->driver_data = NULL;
  1957. fsg->bulk_in_enabled = 0;
  1958. }
  1959. if (fsg->bulk_out_enabled) {
  1960. usb_ep_disable(fsg->bulk_out);
  1961. fsg->bulk_out->driver_data = NULL;
  1962. fsg->bulk_out_enabled = 0;
  1963. }
  1964. common->fsg = NULL;
  1965. wake_up(&common->fsg_wait);
  1966. }
  1967. common->running = 0;
  1968. if (!new_fsg || rc)
  1969. return rc;
  1970. common->fsg = new_fsg;
  1971. fsg = common->fsg;
  1972. /* Enable the endpoints */
  1973. rc = config_ep_by_speed(common->gadget, &(fsg->function), fsg->bulk_in);
  1974. if (rc)
  1975. goto reset;
  1976. rc = usb_ep_enable(fsg->bulk_in);
  1977. if (rc)
  1978. goto reset;
  1979. fsg->bulk_in->driver_data = common;
  1980. fsg->bulk_in_enabled = 1;
  1981. rc = config_ep_by_speed(common->gadget, &(fsg->function),
  1982. fsg->bulk_out);
  1983. if (rc)
  1984. goto reset;
  1985. rc = usb_ep_enable(fsg->bulk_out);
  1986. if (rc)
  1987. goto reset;
  1988. fsg->bulk_out->driver_data = common;
  1989. fsg->bulk_out_enabled = 1;
  1990. common->bulk_out_maxpacket = usb_endpoint_maxp(fsg->bulk_out->desc);
  1991. clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
  1992. /* Allocate the requests */
  1993. for (i = 0; i < common->fsg_num_buffers; ++i) {
  1994. struct fsg_buffhd *bh = &common->buffhds[i];
  1995. rc = alloc_request(common, fsg->bulk_in, &bh->inreq);
  1996. if (rc)
  1997. goto reset;
  1998. rc = alloc_request(common, fsg->bulk_out, &bh->outreq);
  1999. if (rc)
  2000. goto reset;
  2001. bh->inreq->buf = bh->outreq->buf = bh->buf;
  2002. bh->inreq->context = bh->outreq->context = bh;
  2003. bh->inreq->complete = bulk_in_complete;
  2004. bh->outreq->complete = bulk_out_complete;
  2005. }
  2006. common->running = 1;
  2007. for (i = 0; i < common->nluns; ++i)
  2008. if (common->luns[i])
  2009. common->luns[i]->unit_attention_data =
  2010. SS_RESET_OCCURRED;
  2011. return rc;
  2012. }
  2013. /****************************** ALT CONFIGS ******************************/
  2014. static int fsg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  2015. {
  2016. struct fsg_dev *fsg = fsg_from_func(f);
  2017. fsg->common->new_fsg = fsg;
  2018. raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE);
  2019. return USB_GADGET_DELAYED_STATUS;
  2020. }
  2021. static void fsg_disable(struct usb_function *f)
  2022. {
  2023. struct fsg_dev *fsg = fsg_from_func(f);
  2024. fsg->common->new_fsg = NULL;
  2025. raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE);
  2026. }
  2027. /*-------------------------------------------------------------------------*/
  2028. static void handle_exception(struct fsg_common *common)
  2029. {
  2030. siginfo_t info;
  2031. int i;
  2032. struct fsg_buffhd *bh;
  2033. enum fsg_state old_state;
  2034. struct fsg_lun *curlun;
  2035. unsigned int exception_req_tag;
  2036. /*
  2037. * Clear the existing signals. Anything but SIGUSR1 is converted
  2038. * into a high-priority EXIT exception.
  2039. */
  2040. for (;;) {
  2041. int sig =
  2042. dequeue_signal_lock(current, &current->blocked, &info);
  2043. if (!sig)
  2044. break;
  2045. if (sig != SIGUSR1) {
  2046. if (common->state < FSG_STATE_EXIT)
  2047. DBG(common, "Main thread exiting on signal\n");
  2048. raise_exception(common, FSG_STATE_EXIT);
  2049. }
  2050. }
  2051. /* Cancel all the pending transfers */
  2052. if (likely(common->fsg)) {
  2053. for (i = 0; i < common->fsg_num_buffers; ++i) {
  2054. bh = &common->buffhds[i];
  2055. if (bh->inreq_busy)
  2056. usb_ep_dequeue(common->fsg->bulk_in, bh->inreq);
  2057. if (bh->outreq_busy)
  2058. usb_ep_dequeue(common->fsg->bulk_out,
  2059. bh->outreq);
  2060. }
  2061. /* Wait until everything is idle */
  2062. for (;;) {
  2063. int num_active = 0;
  2064. for (i = 0; i < common->fsg_num_buffers; ++i) {
  2065. bh = &common->buffhds[i];
  2066. num_active += bh->inreq_busy + bh->outreq_busy;
  2067. }
  2068. if (num_active == 0)
  2069. break;
  2070. if (sleep_thread(common))
  2071. return;
  2072. }
  2073. /* Clear out the controller's fifos */
  2074. if (common->fsg->bulk_in_enabled)
  2075. usb_ep_fifo_flush(common->fsg->bulk_in);
  2076. if (common->fsg->bulk_out_enabled)
  2077. usb_ep_fifo_flush(common->fsg->bulk_out);
  2078. }
  2079. /*
  2080. * Reset the I/O buffer states and pointers, the SCSI
  2081. * state, and the exception. Then invoke the handler.
  2082. */
  2083. spin_lock_irq(&common->lock);
  2084. for (i = 0; i < common->fsg_num_buffers; ++i) {
  2085. bh = &common->buffhds[i];
  2086. bh->state = BUF_STATE_EMPTY;
  2087. }
  2088. common->next_buffhd_to_fill = &common->buffhds[0];
  2089. common->next_buffhd_to_drain = &common->buffhds[0];
  2090. exception_req_tag = common->exception_req_tag;
  2091. old_state = common->state;
  2092. if (old_state == FSG_STATE_ABORT_BULK_OUT)
  2093. common->state = FSG_STATE_STATUS_PHASE;
  2094. else {
  2095. for (i = 0; i < common->nluns; ++i) {
  2096. curlun = common->luns[i];
  2097. if (!curlun)
  2098. continue;
  2099. curlun->prevent_medium_removal = 0;
  2100. curlun->sense_data = SS_NO_SENSE;
  2101. curlun->unit_attention_data = SS_NO_SENSE;
  2102. curlun->sense_data_info = 0;
  2103. curlun->info_valid = 0;
  2104. }
  2105. common->state = FSG_STATE_IDLE;
  2106. }
  2107. spin_unlock_irq(&common->lock);
  2108. /* Carry out any extra actions required for the exception */
  2109. switch (old_state) {
  2110. case FSG_STATE_ABORT_BULK_OUT:
  2111. send_status(common);
  2112. spin_lock_irq(&common->lock);
  2113. if (common->state == FSG_STATE_STATUS_PHASE)
  2114. common->state = FSG_STATE_IDLE;
  2115. spin_unlock_irq(&common->lock);
  2116. break;
  2117. case FSG_STATE_RESET:
  2118. /*
  2119. * In case we were forced against our will to halt a
  2120. * bulk endpoint, clear the halt now. (The SuperH UDC
  2121. * requires this.)
  2122. */
  2123. if (!fsg_is_set(common))
  2124. break;
  2125. if (test_and_clear_bit(IGNORE_BULK_OUT,
  2126. &common->fsg->atomic_bitflags))
  2127. usb_ep_clear_halt(common->fsg->bulk_in);
  2128. if (common->ep0_req_tag == exception_req_tag)
  2129. ep0_queue(common); /* Complete the status stage */
  2130. /*
  2131. * Technically this should go here, but it would only be
  2132. * a waste of time. Ditto for the INTERFACE_CHANGE and
  2133. * CONFIG_CHANGE cases.
  2134. */
  2135. /* for (i = 0; i < common->nluns; ++i) */
  2136. /* if (common->luns[i]) */
  2137. /* common->luns[i]->unit_attention_data = */
  2138. /* SS_RESET_OCCURRED; */
  2139. break;
  2140. case FSG_STATE_CONFIG_CHANGE:
  2141. do_set_interface(common, common->new_fsg);
  2142. if (common->new_fsg)
  2143. usb_composite_setup_continue(common->cdev);
  2144. break;
  2145. case FSG_STATE_EXIT:
  2146. case FSG_STATE_TERMINATED:
  2147. do_set_interface(common, NULL); /* Free resources */
  2148. spin_lock_irq(&common->lock);
  2149. common->state = FSG_STATE_TERMINATED; /* Stop the thread */
  2150. spin_unlock_irq(&common->lock);
  2151. break;
  2152. case FSG_STATE_INTERFACE_CHANGE:
  2153. case FSG_STATE_DISCONNECT:
  2154. case FSG_STATE_COMMAND_PHASE:
  2155. case FSG_STATE_DATA_PHASE:
  2156. case FSG_STATE_STATUS_PHASE:
  2157. case FSG_STATE_IDLE:
  2158. break;
  2159. }
  2160. }
  2161. /*-------------------------------------------------------------------------*/
  2162. static int fsg_main_thread(void *common_)
  2163. {
  2164. struct fsg_common *common = common_;
  2165. /*
  2166. * Allow the thread to be killed by a signal, but set the signal mask
  2167. * to block everything but INT, TERM, KILL, and USR1.
  2168. */
  2169. allow_signal(SIGINT);
  2170. allow_signal(SIGTERM);
  2171. allow_signal(SIGKILL);
  2172. allow_signal(SIGUSR1);
  2173. /* Allow the thread to be frozen */
  2174. set_freezable();
  2175. /*
  2176. * Arrange for userspace references to be interpreted as kernel
  2177. * pointers. That way we can pass a kernel pointer to a routine
  2178. * that expects a __user pointer and it will work okay.
  2179. */
  2180. set_fs(get_ds());
  2181. /* The main loop */
  2182. while (common->state != FSG_STATE_TERMINATED) {
  2183. if (exception_in_progress(common) || signal_pending(current)) {
  2184. handle_exception(common);
  2185. continue;
  2186. }
  2187. if (!common->running) {
  2188. sleep_thread(common);
  2189. continue;
  2190. }
  2191. if (get_next_command(common))
  2192. continue;
  2193. spin_lock_irq(&common->lock);
  2194. if (!exception_in_progress(common))
  2195. common->state = FSG_STATE_DATA_PHASE;
  2196. spin_unlock_irq(&common->lock);
  2197. if (do_scsi_command(common) || finish_reply(common))
  2198. continue;
  2199. spin_lock_irq(&common->lock);
  2200. if (!exception_in_progress(common))
  2201. common->state = FSG_STATE_STATUS_PHASE;
  2202. spin_unlock_irq(&common->lock);
  2203. if (send_status(common))
  2204. continue;
  2205. spin_lock_irq(&common->lock);
  2206. if (!exception_in_progress(common))
  2207. common->state = FSG_STATE_IDLE;
  2208. spin_unlock_irq(&common->lock);
  2209. }
  2210. spin_lock_irq(&common->lock);
  2211. common->thread_task = NULL;
  2212. spin_unlock_irq(&common->lock);
  2213. if (!common->ops || !common->ops->thread_exits
  2214. || common->ops->thread_exits(common) < 0) {
  2215. struct fsg_lun **curlun_it = common->luns;
  2216. unsigned i = common->nluns;
  2217. down_write(&common->filesem);
  2218. for (; i--; ++curlun_it) {
  2219. struct fsg_lun *curlun = *curlun_it;
  2220. if (!curlun || !fsg_lun_is_open(curlun))
  2221. continue;
  2222. fsg_lun_close(curlun);
  2223. curlun->unit_attention_data = SS_MEDIUM_NOT_PRESENT;
  2224. }
  2225. up_write(&common->filesem);
  2226. }
  2227. /* Let fsg_unbind() know the thread has exited */
  2228. complete_and_exit(&common->thread_notifier, 0);
  2229. }
  2230. /*************************** DEVICE ATTRIBUTES ***************************/
  2231. static ssize_t ro_show(struct device *dev, struct device_attribute *attr, char *buf)
  2232. {
  2233. struct fsg_lun *curlun = fsg_lun_from_dev(dev);
  2234. return fsg_show_ro(curlun, buf);
  2235. }
  2236. static ssize_t nofua_show(struct device *dev, struct device_attribute *attr,
  2237. char *buf)
  2238. {
  2239. struct fsg_lun *curlun = fsg_lun_from_dev(dev);
  2240. return fsg_show_nofua(curlun, buf);
  2241. }
  2242. static ssize_t file_show(struct device *dev, struct device_attribute *attr,
  2243. char *buf)
  2244. {
  2245. struct fsg_lun *curlun = fsg_lun_from_dev(dev);
  2246. struct rw_semaphore *filesem = dev_get_drvdata(dev);
  2247. return fsg_show_file(curlun, filesem, buf);
  2248. }
  2249. static ssize_t ro_store(struct device *dev, struct device_attribute *attr,
  2250. const char *buf, size_t count)
  2251. {
  2252. struct fsg_lun *curlun = fsg_lun_from_dev(dev);
  2253. struct rw_semaphore *filesem = dev_get_drvdata(dev);
  2254. return fsg_store_ro(curlun, filesem, buf, count);
  2255. }
  2256. static ssize_t nofua_store(struct device *dev, struct device_attribute *attr,
  2257. const char *buf, size_t count)
  2258. {
  2259. struct fsg_lun *curlun = fsg_lun_from_dev(dev);
  2260. return fsg_store_nofua(curlun, buf, count);
  2261. }
  2262. static ssize_t file_store(struct device *dev, struct device_attribute *attr,
  2263. const char *buf, size_t count)
  2264. {
  2265. struct fsg_lun *curlun = fsg_lun_from_dev(dev);
  2266. struct rw_semaphore *filesem = dev_get_drvdata(dev);
  2267. return fsg_store_file(curlun, filesem, buf, count);
  2268. }
  2269. static DEVICE_ATTR_RW(ro);
  2270. static DEVICE_ATTR_RW(nofua);
  2271. static DEVICE_ATTR_RW(file);
  2272. static struct device_attribute dev_attr_ro_cdrom = __ATTR_RO(ro);
  2273. static struct device_attribute dev_attr_file_nonremovable = __ATTR_RO(file);
  2274. /****************************** FSG COMMON ******************************/
  2275. static void fsg_common_release(struct kref *ref);
  2276. static void fsg_lun_release(struct device *dev)
  2277. {
  2278. /* Nothing needs to be done */
  2279. }
  2280. void fsg_common_get(struct fsg_common *common)
  2281. {
  2282. kref_get(&common->ref);
  2283. }
  2284. EXPORT_SYMBOL_GPL(fsg_common_get);
  2285. void fsg_common_put(struct fsg_common *common)
  2286. {
  2287. kref_put(&common->ref, fsg_common_release);
  2288. }
  2289. EXPORT_SYMBOL_GPL(fsg_common_put);
  2290. /* check if fsg_num_buffers is within a valid range */
  2291. static inline int fsg_num_buffers_validate(unsigned int fsg_num_buffers)
  2292. {
  2293. if (fsg_num_buffers >= 2 && fsg_num_buffers <= 4)
  2294. return 0;
  2295. pr_err("fsg_num_buffers %u is out of range (%d to %d)\n",
  2296. fsg_num_buffers, 2, 4);
  2297. return -EINVAL;
  2298. }
  2299. static struct fsg_common *fsg_common_setup(struct fsg_common *common)
  2300. {
  2301. if (!common) {
  2302. common = kzalloc(sizeof(*common), GFP_KERNEL);
  2303. if (!common)
  2304. return ERR_PTR(-ENOMEM);
  2305. common->free_storage_on_release = 1;
  2306. } else {
  2307. common->free_storage_on_release = 0;
  2308. }
  2309. init_rwsem(&common->filesem);
  2310. spin_lock_init(&common->lock);
  2311. kref_init(&common->ref);
  2312. init_completion(&common->thread_notifier);
  2313. init_waitqueue_head(&common->fsg_wait);
  2314. common->state = FSG_STATE_TERMINATED;
  2315. return common;
  2316. }
  2317. void fsg_common_set_sysfs(struct fsg_common *common, bool sysfs)
  2318. {
  2319. common->sysfs = sysfs;
  2320. }
  2321. EXPORT_SYMBOL_GPL(fsg_common_set_sysfs);
  2322. static void _fsg_common_free_buffers(struct fsg_buffhd *buffhds, unsigned n)
  2323. {
  2324. if (buffhds) {
  2325. struct fsg_buffhd *bh = buffhds;
  2326. while (n--) {
  2327. kfree(bh->buf);
  2328. ++bh;
  2329. }
  2330. kfree(buffhds);
  2331. }
  2332. }
  2333. int fsg_common_set_num_buffers(struct fsg_common *common, unsigned int n)
  2334. {
  2335. struct fsg_buffhd *bh, *buffhds;
  2336. int i, rc;
  2337. rc = fsg_num_buffers_validate(n);
  2338. if (rc != 0)
  2339. return rc;
  2340. buffhds = kcalloc(n, sizeof(*buffhds), GFP_KERNEL);
  2341. if (!buffhds)
  2342. return -ENOMEM;
  2343. /* Data buffers cyclic list */
  2344. bh = buffhds;
  2345. i = n;
  2346. goto buffhds_first_it;
  2347. do {
  2348. bh->next = bh + 1;
  2349. ++bh;
  2350. buffhds_first_it:
  2351. bh->buf = kmalloc(FSG_BUFLEN, GFP_KERNEL);
  2352. if (unlikely(!bh->buf))
  2353. goto error_release;
  2354. } while (--i);
  2355. bh->next = buffhds;
  2356. _fsg_common_free_buffers(common->buffhds, common->fsg_num_buffers);
  2357. common->fsg_num_buffers = n;
  2358. common->buffhds = buffhds;
  2359. return 0;
  2360. error_release:
  2361. /*
  2362. * "buf"s pointed to by heads after n - i are NULL
  2363. * so releasing them won't hurt
  2364. */
  2365. _fsg_common_free_buffers(buffhds, n);
  2366. return -ENOMEM;
  2367. }
  2368. EXPORT_SYMBOL_GPL(fsg_common_set_num_buffers);
  2369. static inline void fsg_common_remove_sysfs(struct fsg_lun *lun)
  2370. {
  2371. device_remove_file(&lun->dev, &dev_attr_nofua);
  2372. /*
  2373. * device_remove_file() =>
  2374. *
  2375. * here the attr (e.g. dev_attr_ro) is only used to be passed to:
  2376. *
  2377. * sysfs_remove_file() =>
  2378. *
  2379. * here e.g. both dev_attr_ro_cdrom and dev_attr_ro are in
  2380. * the same namespace and
  2381. * from here only attr->name is passed to:
  2382. *
  2383. * sysfs_hash_and_remove()
  2384. *
  2385. * attr->name is the same for dev_attr_ro_cdrom and
  2386. * dev_attr_ro
  2387. * attr->name is the same for dev_attr_file and
  2388. * dev_attr_file_nonremovable
  2389. *
  2390. * so we don't differentiate between removing e.g. dev_attr_ro_cdrom
  2391. * and dev_attr_ro
  2392. */
  2393. device_remove_file(&lun->dev, &dev_attr_ro);
  2394. device_remove_file(&lun->dev, &dev_attr_file);
  2395. }
  2396. void fsg_common_remove_lun(struct fsg_lun *lun, bool sysfs)
  2397. {
  2398. if (sysfs) {
  2399. fsg_common_remove_sysfs(lun);
  2400. device_unregister(&lun->dev);
  2401. }
  2402. fsg_lun_close(lun);
  2403. kfree(lun);
  2404. }
  2405. EXPORT_SYMBOL_GPL(fsg_common_remove_lun);
  2406. static void _fsg_common_remove_luns(struct fsg_common *common, int n)
  2407. {
  2408. int i;
  2409. for (i = 0; i < n; ++i)
  2410. if (common->luns[i]) {
  2411. fsg_common_remove_lun(common->luns[i], common->sysfs);
  2412. common->luns[i] = NULL;
  2413. }
  2414. }
  2415. EXPORT_SYMBOL_GPL(fsg_common_remove_luns);
  2416. void fsg_common_remove_luns(struct fsg_common *common)
  2417. {
  2418. _fsg_common_remove_luns(common, common->nluns);
  2419. }
  2420. void fsg_common_free_luns(struct fsg_common *common)
  2421. {
  2422. fsg_common_remove_luns(common);
  2423. kfree(common->luns);
  2424. common->luns = NULL;
  2425. }
  2426. EXPORT_SYMBOL_GPL(fsg_common_free_luns);
  2427. int fsg_common_set_nluns(struct fsg_common *common, int nluns)
  2428. {
  2429. struct fsg_lun **curlun;
  2430. /* Find out how many LUNs there should be */
  2431. if (nluns < 1 || nluns > FSG_MAX_LUNS) {
  2432. pr_err("invalid number of LUNs: %u\n", nluns);
  2433. return -EINVAL;
  2434. }
  2435. curlun = kcalloc(nluns, sizeof(*curlun), GFP_KERNEL);
  2436. if (unlikely(!curlun))
  2437. return -ENOMEM;
  2438. if (common->luns)
  2439. fsg_common_free_luns(common);
  2440. common->luns = curlun;
  2441. common->nluns = nluns;
  2442. pr_info("Number of LUNs=%d\n", common->nluns);
  2443. return 0;
  2444. }
  2445. EXPORT_SYMBOL_GPL(fsg_common_set_nluns);
  2446. void fsg_common_set_ops(struct fsg_common *common,
  2447. const struct fsg_operations *ops)
  2448. {
  2449. common->ops = ops;
  2450. }
  2451. EXPORT_SYMBOL_GPL(fsg_common_set_ops);
  2452. void fsg_common_free_buffers(struct fsg_common *common)
  2453. {
  2454. _fsg_common_free_buffers(common->buffhds, common->fsg_num_buffers);
  2455. common->buffhds = NULL;
  2456. }
  2457. EXPORT_SYMBOL_GPL(fsg_common_free_buffers);
  2458. int fsg_common_set_cdev(struct fsg_common *common,
  2459. struct usb_composite_dev *cdev, bool can_stall)
  2460. {
  2461. struct usb_string *us;
  2462. common->gadget = cdev->gadget;
  2463. common->ep0 = cdev->gadget->ep0;
  2464. common->ep0req = cdev->req;
  2465. common->cdev = cdev;
  2466. us = usb_gstrings_attach(cdev, fsg_strings_array,
  2467. ARRAY_SIZE(fsg_strings));
  2468. if (IS_ERR(us))
  2469. return PTR_ERR(us);
  2470. fsg_intf_desc.iInterface = us[FSG_STRING_INTERFACE].id;
  2471. /*
  2472. * Some peripheral controllers are known not to be able to
  2473. * halt bulk endpoints correctly. If one of them is present,
  2474. * disable stalls.
  2475. */
  2476. common->can_stall = can_stall && !(gadget_is_at91(common->gadget));
  2477. return 0;
  2478. }
  2479. EXPORT_SYMBOL_GPL(fsg_common_set_cdev);
  2480. static inline int fsg_common_add_sysfs(struct fsg_common *common,
  2481. struct fsg_lun *lun)
  2482. {
  2483. int rc;
  2484. rc = device_register(&lun->dev);
  2485. if (rc) {
  2486. put_device(&lun->dev);
  2487. return rc;
  2488. }
  2489. rc = device_create_file(&lun->dev,
  2490. lun->cdrom
  2491. ? &dev_attr_ro_cdrom
  2492. : &dev_attr_ro);
  2493. if (rc)
  2494. goto error;
  2495. rc = device_create_file(&lun->dev,
  2496. lun->removable
  2497. ? &dev_attr_file
  2498. : &dev_attr_file_nonremovable);
  2499. if (rc)
  2500. goto error;
  2501. rc = device_create_file(&lun->dev, &dev_attr_nofua);
  2502. if (rc)
  2503. goto error;
  2504. return 0;
  2505. error:
  2506. /* removing nonexistent files is a no-op */
  2507. fsg_common_remove_sysfs(lun);
  2508. device_unregister(&lun->dev);
  2509. return rc;
  2510. }
  2511. int fsg_common_create_lun(struct fsg_common *common, struct fsg_lun_config *cfg,
  2512. unsigned int id, const char *name,
  2513. const char **name_pfx)
  2514. {
  2515. struct fsg_lun *lun;
  2516. char *pathbuf, *p;
  2517. int rc = -ENOMEM;
  2518. if (!common->nluns || !common->luns)
  2519. return -ENODEV;
  2520. if (common->luns[id])
  2521. return -EBUSY;
  2522. if (!cfg->filename && !cfg->removable) {
  2523. pr_err("no file given for LUN%d\n", id);
  2524. return -EINVAL;
  2525. }
  2526. lun = kzalloc(sizeof(*lun), GFP_KERNEL);
  2527. if (!lun)
  2528. return -ENOMEM;
  2529. lun->name_pfx = name_pfx;
  2530. lun->cdrom = !!cfg->cdrom;
  2531. lun->ro = cfg->cdrom || cfg->ro;
  2532. lun->initially_ro = lun->ro;
  2533. lun->removable = !!cfg->removable;
  2534. if (!common->sysfs) {
  2535. /* we DON'T own the name!*/
  2536. lun->name = name;
  2537. } else {
  2538. lun->dev.release = fsg_lun_release;
  2539. lun->dev.parent = &common->gadget->dev;
  2540. dev_set_drvdata(&lun->dev, &common->filesem);
  2541. dev_set_name(&lun->dev, "%s", name);
  2542. lun->name = dev_name(&lun->dev);
  2543. rc = fsg_common_add_sysfs(common, lun);
  2544. if (rc) {
  2545. pr_info("failed to register LUN%d: %d\n", id, rc);
  2546. goto error_sysfs;
  2547. }
  2548. }
  2549. common->luns[id] = lun;
  2550. if (cfg->filename) {
  2551. rc = fsg_lun_open(lun, cfg->filename);
  2552. if (rc)
  2553. goto error_lun;
  2554. }
  2555. pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
  2556. p = "(no medium)";
  2557. if (fsg_lun_is_open(lun)) {
  2558. p = "(error)";
  2559. if (pathbuf) {
  2560. p = d_path(&lun->filp->f_path, pathbuf, PATH_MAX);
  2561. if (IS_ERR(p))
  2562. p = "(error)";
  2563. }
  2564. }
  2565. pr_info("LUN: %s%s%sfile: %s\n",
  2566. lun->removable ? "removable " : "",
  2567. lun->ro ? "read only " : "",
  2568. lun->cdrom ? "CD-ROM " : "",
  2569. p);
  2570. kfree(pathbuf);
  2571. return 0;
  2572. error_lun:
  2573. if (common->sysfs) {
  2574. fsg_common_remove_sysfs(lun);
  2575. device_unregister(&lun->dev);
  2576. }
  2577. fsg_lun_close(lun);
  2578. common->luns[id] = NULL;
  2579. error_sysfs:
  2580. kfree(lun);
  2581. return rc;
  2582. }
  2583. EXPORT_SYMBOL_GPL(fsg_common_create_lun);
  2584. int fsg_common_create_luns(struct fsg_common *common, struct fsg_config *cfg)
  2585. {
  2586. char buf[8]; /* enough for 100000000 different numbers, decimal */
  2587. int i, rc;
  2588. for (i = 0; i < common->nluns; ++i) {
  2589. snprintf(buf, sizeof(buf), "lun%d", i);
  2590. rc = fsg_common_create_lun(common, &cfg->luns[i], i, buf, NULL);
  2591. if (rc)
  2592. goto fail;
  2593. }
  2594. pr_info("Number of LUNs=%d\n", common->nluns);
  2595. return 0;
  2596. fail:
  2597. _fsg_common_remove_luns(common, i);
  2598. return rc;
  2599. }
  2600. EXPORT_SYMBOL_GPL(fsg_common_create_luns);
  2601. void fsg_common_set_inquiry_string(struct fsg_common *common, const char *vn,
  2602. const char *pn)
  2603. {
  2604. int i;
  2605. /* Prepare inquiryString */
  2606. i = get_default_bcdDevice();
  2607. snprintf(common->inquiry_string, sizeof(common->inquiry_string),
  2608. "%-8s%-16s%04x", vn ?: "Linux",
  2609. /* Assume product name dependent on the first LUN */
  2610. pn ?: ((*common->luns)->cdrom
  2611. ? "File-CD Gadget"
  2612. : "File-Stor Gadget"),
  2613. i);
  2614. }
  2615. EXPORT_SYMBOL_GPL(fsg_common_set_inquiry_string);
  2616. int fsg_common_run_thread(struct fsg_common *common)
  2617. {
  2618. common->state = FSG_STATE_IDLE;
  2619. /* Tell the thread to start working */
  2620. common->thread_task =
  2621. kthread_create(fsg_main_thread, common, "file-storage");
  2622. if (IS_ERR(common->thread_task)) {
  2623. common->state = FSG_STATE_TERMINATED;
  2624. return PTR_ERR(common->thread_task);
  2625. }
  2626. DBG(common, "I/O thread pid: %d\n", task_pid_nr(common->thread_task));
  2627. wake_up_process(common->thread_task);
  2628. return 0;
  2629. }
  2630. EXPORT_SYMBOL_GPL(fsg_common_run_thread);
  2631. static void fsg_common_release(struct kref *ref)
  2632. {
  2633. struct fsg_common *common = container_of(ref, struct fsg_common, ref);
  2634. /* If the thread isn't already dead, tell it to exit now */
  2635. if (common->state != FSG_STATE_TERMINATED) {
  2636. raise_exception(common, FSG_STATE_EXIT);
  2637. wait_for_completion(&common->thread_notifier);
  2638. }
  2639. if (likely(common->luns)) {
  2640. struct fsg_lun **lun_it = common->luns;
  2641. unsigned i = common->nluns;
  2642. /* In error recovery common->nluns may be zero. */
  2643. for (; i; --i, ++lun_it) {
  2644. struct fsg_lun *lun = *lun_it;
  2645. if (!lun)
  2646. continue;
  2647. if (common->sysfs)
  2648. fsg_common_remove_sysfs(lun);
  2649. fsg_lun_close(lun);
  2650. if (common->sysfs)
  2651. device_unregister(&lun->dev);
  2652. kfree(lun);
  2653. }
  2654. kfree(common->luns);
  2655. }
  2656. _fsg_common_free_buffers(common->buffhds, common->fsg_num_buffers);
  2657. if (common->free_storage_on_release)
  2658. kfree(common);
  2659. }
  2660. /*-------------------------------------------------------------------------*/
  2661. static int fsg_bind(struct usb_configuration *c, struct usb_function *f)
  2662. {
  2663. struct fsg_dev *fsg = fsg_from_func(f);
  2664. struct usb_gadget *gadget = c->cdev->gadget;
  2665. int i;
  2666. struct usb_ep *ep;
  2667. unsigned max_burst;
  2668. int ret;
  2669. struct fsg_opts *opts;
  2670. opts = fsg_opts_from_func_inst(f->fi);
  2671. if (!opts->no_configfs) {
  2672. ret = fsg_common_set_cdev(fsg->common, c->cdev,
  2673. fsg->common->can_stall);
  2674. if (ret)
  2675. return ret;
  2676. fsg_common_set_inquiry_string(fsg->common, 0, 0);
  2677. ret = fsg_common_run_thread(fsg->common);
  2678. if (ret)
  2679. return ret;
  2680. }
  2681. fsg->gadget = gadget;
  2682. /* New interface */
  2683. i = usb_interface_id(c, f);
  2684. if (i < 0)
  2685. return i;
  2686. fsg_intf_desc.bInterfaceNumber = i;
  2687. fsg->interface_number = i;
  2688. /* Find all the endpoints we will use */
  2689. ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_in_desc);
  2690. if (!ep)
  2691. goto autoconf_fail;
  2692. ep->driver_data = fsg->common; /* claim the endpoint */
  2693. fsg->bulk_in = ep;
  2694. ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_out_desc);
  2695. if (!ep)
  2696. goto autoconf_fail;
  2697. ep->driver_data = fsg->common; /* claim the endpoint */
  2698. fsg->bulk_out = ep;
  2699. /* Assume endpoint addresses are the same for both speeds */
  2700. fsg_hs_bulk_in_desc.bEndpointAddress =
  2701. fsg_fs_bulk_in_desc.bEndpointAddress;
  2702. fsg_hs_bulk_out_desc.bEndpointAddress =
  2703. fsg_fs_bulk_out_desc.bEndpointAddress;
  2704. /* Calculate bMaxBurst, we know packet size is 1024 */
  2705. max_burst = min_t(unsigned, FSG_BUFLEN / 1024, 15);
  2706. fsg_ss_bulk_in_desc.bEndpointAddress =
  2707. fsg_fs_bulk_in_desc.bEndpointAddress;
  2708. fsg_ss_bulk_in_comp_desc.bMaxBurst = max_burst;
  2709. fsg_ss_bulk_out_desc.bEndpointAddress =
  2710. fsg_fs_bulk_out_desc.bEndpointAddress;
  2711. fsg_ss_bulk_out_comp_desc.bMaxBurst = max_burst;
  2712. ret = usb_assign_descriptors(f, fsg_fs_function, fsg_hs_function,
  2713. fsg_ss_function);
  2714. if (ret)
  2715. goto autoconf_fail;
  2716. return 0;
  2717. autoconf_fail:
  2718. ERROR(fsg, "unable to autoconfigure all endpoints\n");
  2719. return -ENOTSUPP;
  2720. }
  2721. /****************************** ALLOCATE FUNCTION *************************/
  2722. static void fsg_unbind(struct usb_configuration *c, struct usb_function *f)
  2723. {
  2724. struct fsg_dev *fsg = fsg_from_func(f);
  2725. struct fsg_common *common = fsg->common;
  2726. DBG(fsg, "unbind\n");
  2727. if (fsg->common->fsg == fsg) {
  2728. fsg->common->new_fsg = NULL;
  2729. raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE);
  2730. /* FIXME: make interruptible or killable somehow? */
  2731. wait_event(common->fsg_wait, common->fsg != fsg);
  2732. }
  2733. usb_free_all_descriptors(&fsg->function);
  2734. }
  2735. static inline struct fsg_lun_opts *to_fsg_lun_opts(struct config_item *item)
  2736. {
  2737. return container_of(to_config_group(item), struct fsg_lun_opts, group);
  2738. }
  2739. static inline struct fsg_opts *to_fsg_opts(struct config_item *item)
  2740. {
  2741. return container_of(to_config_group(item), struct fsg_opts,
  2742. func_inst.group);
  2743. }
  2744. CONFIGFS_ATTR_STRUCT(fsg_lun_opts);
  2745. CONFIGFS_ATTR_OPS(fsg_lun_opts);
  2746. static void fsg_lun_attr_release(struct config_item *item)
  2747. {
  2748. struct fsg_lun_opts *lun_opts;
  2749. lun_opts = to_fsg_lun_opts(item);
  2750. kfree(lun_opts);
  2751. }
  2752. static struct configfs_item_operations fsg_lun_item_ops = {
  2753. .release = fsg_lun_attr_release,
  2754. .show_attribute = fsg_lun_opts_attr_show,
  2755. .store_attribute = fsg_lun_opts_attr_store,
  2756. };
  2757. static ssize_t fsg_lun_opts_file_show(struct fsg_lun_opts *opts, char *page)
  2758. {
  2759. struct fsg_opts *fsg_opts;
  2760. fsg_opts = to_fsg_opts(opts->group.cg_item.ci_parent);
  2761. return fsg_show_file(opts->lun, &fsg_opts->common->filesem, page);
  2762. }
  2763. static ssize_t fsg_lun_opts_file_store(struct fsg_lun_opts *opts,
  2764. const char *page, size_t len)
  2765. {
  2766. struct fsg_opts *fsg_opts;
  2767. fsg_opts = to_fsg_opts(opts->group.cg_item.ci_parent);
  2768. return fsg_store_file(opts->lun, &fsg_opts->common->filesem, page, len);
  2769. }
  2770. static struct fsg_lun_opts_attribute fsg_lun_opts_file =
  2771. __CONFIGFS_ATTR(file, S_IRUGO | S_IWUSR, fsg_lun_opts_file_show,
  2772. fsg_lun_opts_file_store);
  2773. static ssize_t fsg_lun_opts_ro_show(struct fsg_lun_opts *opts, char *page)
  2774. {
  2775. return fsg_show_ro(opts->lun, page);
  2776. }
  2777. static ssize_t fsg_lun_opts_ro_store(struct fsg_lun_opts *opts,
  2778. const char *page, size_t len)
  2779. {
  2780. struct fsg_opts *fsg_opts;
  2781. fsg_opts = to_fsg_opts(opts->group.cg_item.ci_parent);
  2782. return fsg_store_ro(opts->lun, &fsg_opts->common->filesem, page, len);
  2783. }
  2784. static struct fsg_lun_opts_attribute fsg_lun_opts_ro =
  2785. __CONFIGFS_ATTR(ro, S_IRUGO | S_IWUSR, fsg_lun_opts_ro_show,
  2786. fsg_lun_opts_ro_store);
  2787. static ssize_t fsg_lun_opts_removable_show(struct fsg_lun_opts *opts,
  2788. char *page)
  2789. {
  2790. return fsg_show_removable(opts->lun, page);
  2791. }
  2792. static ssize_t fsg_lun_opts_removable_store(struct fsg_lun_opts *opts,
  2793. const char *page, size_t len)
  2794. {
  2795. return fsg_store_removable(opts->lun, page, len);
  2796. }
  2797. static struct fsg_lun_opts_attribute fsg_lun_opts_removable =
  2798. __CONFIGFS_ATTR(removable, S_IRUGO | S_IWUSR,
  2799. fsg_lun_opts_removable_show,
  2800. fsg_lun_opts_removable_store);
  2801. static ssize_t fsg_lun_opts_cdrom_show(struct fsg_lun_opts *opts, char *page)
  2802. {
  2803. return fsg_show_cdrom(opts->lun, page);
  2804. }
  2805. static ssize_t fsg_lun_opts_cdrom_store(struct fsg_lun_opts *opts,
  2806. const char *page, size_t len)
  2807. {
  2808. struct fsg_opts *fsg_opts;
  2809. fsg_opts = to_fsg_opts(opts->group.cg_item.ci_parent);
  2810. return fsg_store_cdrom(opts->lun, &fsg_opts->common->filesem, page,
  2811. len);
  2812. }
  2813. static struct fsg_lun_opts_attribute fsg_lun_opts_cdrom =
  2814. __CONFIGFS_ATTR(cdrom, S_IRUGO | S_IWUSR, fsg_lun_opts_cdrom_show,
  2815. fsg_lun_opts_cdrom_store);
  2816. static ssize_t fsg_lun_opts_nofua_show(struct fsg_lun_opts *opts, char *page)
  2817. {
  2818. return fsg_show_nofua(opts->lun, page);
  2819. }
  2820. static ssize_t fsg_lun_opts_nofua_store(struct fsg_lun_opts *opts,
  2821. const char *page, size_t len)
  2822. {
  2823. return fsg_store_nofua(opts->lun, page, len);
  2824. }
  2825. static struct fsg_lun_opts_attribute fsg_lun_opts_nofua =
  2826. __CONFIGFS_ATTR(nofua, S_IRUGO | S_IWUSR, fsg_lun_opts_nofua_show,
  2827. fsg_lun_opts_nofua_store);
  2828. static struct configfs_attribute *fsg_lun_attrs[] = {
  2829. &fsg_lun_opts_file.attr,
  2830. &fsg_lun_opts_ro.attr,
  2831. &fsg_lun_opts_removable.attr,
  2832. &fsg_lun_opts_cdrom.attr,
  2833. &fsg_lun_opts_nofua.attr,
  2834. NULL,
  2835. };
  2836. static struct config_item_type fsg_lun_type = {
  2837. .ct_item_ops = &fsg_lun_item_ops,
  2838. .ct_attrs = fsg_lun_attrs,
  2839. .ct_owner = THIS_MODULE,
  2840. };
  2841. static struct config_group *fsg_lun_make(struct config_group *group,
  2842. const char *name)
  2843. {
  2844. struct fsg_lun_opts *opts;
  2845. struct fsg_opts *fsg_opts;
  2846. struct fsg_lun_config config;
  2847. char *num_str;
  2848. u8 num;
  2849. int ret;
  2850. num_str = strchr(name, '.');
  2851. if (!num_str) {
  2852. pr_err("Unable to locate . in LUN.NUMBER\n");
  2853. return ERR_PTR(-EINVAL);
  2854. }
  2855. num_str++;
  2856. ret = kstrtou8(num_str, 0, &num);
  2857. if (ret)
  2858. return ERR_PTR(ret);
  2859. fsg_opts = to_fsg_opts(&group->cg_item);
  2860. if (num >= FSG_MAX_LUNS)
  2861. return ERR_PTR(-ERANGE);
  2862. mutex_lock(&fsg_opts->lock);
  2863. if (fsg_opts->refcnt || fsg_opts->common->luns[num]) {
  2864. ret = -EBUSY;
  2865. goto out;
  2866. }
  2867. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  2868. if (!opts) {
  2869. ret = -ENOMEM;
  2870. goto out;
  2871. }
  2872. memset(&config, 0, sizeof(config));
  2873. config.removable = true;
  2874. ret = fsg_common_create_lun(fsg_opts->common, &config, num, name,
  2875. (const char **)&group->cg_item.ci_name);
  2876. if (ret) {
  2877. kfree(opts);
  2878. goto out;
  2879. }
  2880. opts->lun = fsg_opts->common->luns[num];
  2881. opts->lun_id = num;
  2882. mutex_unlock(&fsg_opts->lock);
  2883. config_group_init_type_name(&opts->group, name, &fsg_lun_type);
  2884. return &opts->group;
  2885. out:
  2886. mutex_unlock(&fsg_opts->lock);
  2887. return ERR_PTR(ret);
  2888. }
  2889. static void fsg_lun_drop(struct config_group *group, struct config_item *item)
  2890. {
  2891. struct fsg_lun_opts *lun_opts;
  2892. struct fsg_opts *fsg_opts;
  2893. lun_opts = to_fsg_lun_opts(item);
  2894. fsg_opts = to_fsg_opts(&group->cg_item);
  2895. mutex_lock(&fsg_opts->lock);
  2896. if (fsg_opts->refcnt) {
  2897. struct config_item *gadget;
  2898. gadget = group->cg_item.ci_parent->ci_parent;
  2899. unregister_gadget_item(gadget);
  2900. }
  2901. fsg_common_remove_lun(lun_opts->lun, fsg_opts->common->sysfs);
  2902. fsg_opts->common->luns[lun_opts->lun_id] = NULL;
  2903. lun_opts->lun_id = 0;
  2904. mutex_unlock(&fsg_opts->lock);
  2905. config_item_put(item);
  2906. }
  2907. CONFIGFS_ATTR_STRUCT(fsg_opts);
  2908. CONFIGFS_ATTR_OPS(fsg_opts);
  2909. static void fsg_attr_release(struct config_item *item)
  2910. {
  2911. struct fsg_opts *opts = to_fsg_opts(item);
  2912. usb_put_function_instance(&opts->func_inst);
  2913. }
  2914. static struct configfs_item_operations fsg_item_ops = {
  2915. .release = fsg_attr_release,
  2916. .show_attribute = fsg_opts_attr_show,
  2917. .store_attribute = fsg_opts_attr_store,
  2918. };
  2919. static ssize_t fsg_opts_stall_show(struct fsg_opts *opts, char *page)
  2920. {
  2921. int result;
  2922. mutex_lock(&opts->lock);
  2923. result = sprintf(page, "%d", opts->common->can_stall);
  2924. mutex_unlock(&opts->lock);
  2925. return result;
  2926. }
  2927. static ssize_t fsg_opts_stall_store(struct fsg_opts *opts, const char *page,
  2928. size_t len)
  2929. {
  2930. int ret;
  2931. bool stall;
  2932. mutex_lock(&opts->lock);
  2933. if (opts->refcnt) {
  2934. mutex_unlock(&opts->lock);
  2935. return -EBUSY;
  2936. }
  2937. ret = strtobool(page, &stall);
  2938. if (!ret) {
  2939. opts->common->can_stall = stall;
  2940. ret = len;
  2941. }
  2942. mutex_unlock(&opts->lock);
  2943. return ret;
  2944. }
  2945. static struct fsg_opts_attribute fsg_opts_stall =
  2946. __CONFIGFS_ATTR(stall, S_IRUGO | S_IWUSR, fsg_opts_stall_show,
  2947. fsg_opts_stall_store);
  2948. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  2949. static ssize_t fsg_opts_num_buffers_show(struct fsg_opts *opts, char *page)
  2950. {
  2951. int result;
  2952. mutex_lock(&opts->lock);
  2953. result = sprintf(page, "%d", opts->common->fsg_num_buffers);
  2954. mutex_unlock(&opts->lock);
  2955. return result;
  2956. }
  2957. static ssize_t fsg_opts_num_buffers_store(struct fsg_opts *opts,
  2958. const char *page, size_t len)
  2959. {
  2960. int ret;
  2961. u8 num;
  2962. mutex_lock(&opts->lock);
  2963. if (opts->refcnt) {
  2964. ret = -EBUSY;
  2965. goto end;
  2966. }
  2967. ret = kstrtou8(page, 0, &num);
  2968. if (ret)
  2969. goto end;
  2970. ret = fsg_num_buffers_validate(num);
  2971. if (ret)
  2972. goto end;
  2973. fsg_common_set_num_buffers(opts->common, num);
  2974. ret = len;
  2975. end:
  2976. mutex_unlock(&opts->lock);
  2977. return ret;
  2978. }
  2979. static struct fsg_opts_attribute fsg_opts_num_buffers =
  2980. __CONFIGFS_ATTR(num_buffers, S_IRUGO | S_IWUSR,
  2981. fsg_opts_num_buffers_show,
  2982. fsg_opts_num_buffers_store);
  2983. #endif
  2984. static struct configfs_attribute *fsg_attrs[] = {
  2985. &fsg_opts_stall.attr,
  2986. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  2987. &fsg_opts_num_buffers.attr,
  2988. #endif
  2989. NULL,
  2990. };
  2991. static struct configfs_group_operations fsg_group_ops = {
  2992. .make_group = fsg_lun_make,
  2993. .drop_item = fsg_lun_drop,
  2994. };
  2995. static struct config_item_type fsg_func_type = {
  2996. .ct_item_ops = &fsg_item_ops,
  2997. .ct_group_ops = &fsg_group_ops,
  2998. .ct_attrs = fsg_attrs,
  2999. .ct_owner = THIS_MODULE,
  3000. };
  3001. static void fsg_free_inst(struct usb_function_instance *fi)
  3002. {
  3003. struct fsg_opts *opts;
  3004. opts = fsg_opts_from_func_inst(fi);
  3005. fsg_common_put(opts->common);
  3006. kfree(opts);
  3007. }
  3008. static struct usb_function_instance *fsg_alloc_inst(void)
  3009. {
  3010. struct fsg_opts *opts;
  3011. struct fsg_lun_config config;
  3012. int rc;
  3013. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  3014. if (!opts)
  3015. return ERR_PTR(-ENOMEM);
  3016. mutex_init(&opts->lock);
  3017. opts->func_inst.free_func_inst = fsg_free_inst;
  3018. opts->common = fsg_common_setup(opts->common);
  3019. if (IS_ERR(opts->common)) {
  3020. rc = PTR_ERR(opts->common);
  3021. goto release_opts;
  3022. }
  3023. rc = fsg_common_set_nluns(opts->common, FSG_MAX_LUNS);
  3024. if (rc)
  3025. goto release_opts;
  3026. rc = fsg_common_set_num_buffers(opts->common,
  3027. CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS);
  3028. if (rc)
  3029. goto release_luns;
  3030. pr_info(FSG_DRIVER_DESC ", version: " FSG_DRIVER_VERSION "\n");
  3031. memset(&config, 0, sizeof(config));
  3032. config.removable = true;
  3033. rc = fsg_common_create_lun(opts->common, &config, 0, "lun.0",
  3034. (const char **)&opts->func_inst.group.cg_item.ci_name);
  3035. opts->lun0.lun = opts->common->luns[0];
  3036. opts->lun0.lun_id = 0;
  3037. config_group_init_type_name(&opts->lun0.group, "lun.0", &fsg_lun_type);
  3038. opts->default_groups[0] = &opts->lun0.group;
  3039. opts->func_inst.group.default_groups = opts->default_groups;
  3040. config_group_init_type_name(&opts->func_inst.group, "", &fsg_func_type);
  3041. return &opts->func_inst;
  3042. release_luns:
  3043. kfree(opts->common->luns);
  3044. release_opts:
  3045. kfree(opts);
  3046. return ERR_PTR(rc);
  3047. }
  3048. static void fsg_free(struct usb_function *f)
  3049. {
  3050. struct fsg_dev *fsg;
  3051. struct fsg_opts *opts;
  3052. fsg = container_of(f, struct fsg_dev, function);
  3053. opts = container_of(f->fi, struct fsg_opts, func_inst);
  3054. mutex_lock(&opts->lock);
  3055. opts->refcnt--;
  3056. mutex_unlock(&opts->lock);
  3057. kfree(fsg);
  3058. }
  3059. static struct usb_function *fsg_alloc(struct usb_function_instance *fi)
  3060. {
  3061. struct fsg_opts *opts = fsg_opts_from_func_inst(fi);
  3062. struct fsg_common *common = opts->common;
  3063. struct fsg_dev *fsg;
  3064. fsg = kzalloc(sizeof(*fsg), GFP_KERNEL);
  3065. if (unlikely(!fsg))
  3066. return ERR_PTR(-ENOMEM);
  3067. mutex_lock(&opts->lock);
  3068. opts->refcnt++;
  3069. mutex_unlock(&opts->lock);
  3070. fsg->function.name = FSG_DRIVER_DESC;
  3071. fsg->function.bind = fsg_bind;
  3072. fsg->function.unbind = fsg_unbind;
  3073. fsg->function.setup = fsg_setup;
  3074. fsg->function.set_alt = fsg_set_alt;
  3075. fsg->function.disable = fsg_disable;
  3076. fsg->function.free_func = fsg_free;
  3077. fsg->common = common;
  3078. return &fsg->function;
  3079. }
  3080. DECLARE_USB_FUNCTION_INIT(mass_storage, fsg_alloc_inst, fsg_alloc);
  3081. MODULE_LICENSE("GPL");
  3082. MODULE_AUTHOR("Michal Nazarewicz");
  3083. /************************* Module parameters *************************/
  3084. void fsg_config_from_params(struct fsg_config *cfg,
  3085. const struct fsg_module_parameters *params,
  3086. unsigned int fsg_num_buffers)
  3087. {
  3088. struct fsg_lun_config *lun;
  3089. unsigned i;
  3090. /* Configure LUNs */
  3091. cfg->nluns =
  3092. min(params->luns ?: (params->file_count ?: 1u),
  3093. (unsigned)FSG_MAX_LUNS);
  3094. for (i = 0, lun = cfg->luns; i < cfg->nluns; ++i, ++lun) {
  3095. lun->ro = !!params->ro[i];
  3096. lun->cdrom = !!params->cdrom[i];
  3097. lun->removable = !!params->removable[i];
  3098. lun->filename =
  3099. params->file_count > i && params->file[i][0]
  3100. ? params->file[i]
  3101. : NULL;
  3102. }
  3103. /* Let MSF use defaults */
  3104. cfg->vendor_name = NULL;
  3105. cfg->product_name = NULL;
  3106. cfg->ops = NULL;
  3107. cfg->private_data = NULL;
  3108. /* Finalise */
  3109. cfg->can_stall = params->stall;
  3110. cfg->fsg_num_buffers = fsg_num_buffers;
  3111. }
  3112. EXPORT_SYMBOL_GPL(fsg_config_from_params);