f_mass_storage.c 87 KB

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