f_mass_storage.c 85 KB

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