f_mass_storage.c 85 KB

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