f_mass_storage.c 96 KB

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