f_mass_storage.c 100 KB

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