f_mass_storage.c 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827
  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 <m.nazarewicz@samsung.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. * Function supports multiple logical units (LUNs). Backing storage
  47. * for each LUN is provided by a regular file or a block device.
  48. * Access for each LUN can be limited to read-only. Moreover, the
  49. * function can indicate that LUN is removable and/or CD-ROM. (The
  50. * later implies read-only access.)
  51. *
  52. * MSF is configured by specifying a fsg_config structure. It has the
  53. * following fields:
  54. *
  55. * nluns Number of LUNs function have (anywhere from 1
  56. * to FSG_MAX_LUNS which is 8).
  57. * luns An array of LUN configuration values. This
  58. * should be filled for each LUN that
  59. * function will include (ie. for "nluns"
  60. * LUNs). Each element of the array has
  61. * the following fields:
  62. * ->filename The path to the backing file for the LUN.
  63. * Required if LUN is not marked as
  64. * removable.
  65. * ->ro Flag specifying access to the LUN shall be
  66. * read-only. This is implied if CD-ROM
  67. * emulation is enabled as well as when
  68. * it was impossible to open "filename"
  69. * in R/W mode.
  70. * ->removable Flag specifying that LUN shall be indicated as
  71. * being removable.
  72. * ->cdrom Flag specifying that LUN shall be reported as
  73. * being a CD-ROM.
  74. *
  75. * lun_name_format A printf-like format for names of the LUN
  76. * devices. This determines how the
  77. * directory in sysfs will be named.
  78. * Unless you are using several MSFs in
  79. * a single gadget (as opposed to single
  80. * MSF in many configurations) you may
  81. * leave it as NULL (in which case
  82. * "lun%d" will be used). In the format
  83. * you can use "%d" to index LUNs for
  84. * MSF's with more than one LUN. (Beware
  85. * that there is only one integer given
  86. * as an argument for the format and
  87. * specifying invalid format may cause
  88. * unspecified behaviour.)
  89. * thread_name Name of the kernel thread process used by the
  90. * MSF. You can safely set it to NULL
  91. * (in which case default "file-storage"
  92. * will be used).
  93. *
  94. * vendor_name
  95. * product_name
  96. * release Information used as a reply to INQUIRY
  97. * request. To use default set to NULL,
  98. * NULL, 0xffff respectively. The first
  99. * field should be 8 and the second 16
  100. * characters or less.
  101. *
  102. * can_stall Set to permit function to halt bulk endpoints.
  103. * Disabled on some USB devices known not
  104. * to work correctly. You should set it
  105. * to true.
  106. *
  107. * If "removable" is not set for a LUN then a backing file must be
  108. * specified. If it is set, then NULL filename means the LUN's medium
  109. * is not loaded (an empty string as "filename" in the fsg_config
  110. * structure causes error). The CD-ROM emulation includes a single
  111. * data track and no audio tracks; hence there need be only one
  112. * backing file per LUN. Note also that the CD-ROM block length is
  113. * set to 512 rather than the more common value 2048.
  114. *
  115. *
  116. * MSF includes support for module parameters. If gadget using it
  117. * decides to use it, the following module parameters will be
  118. * available:
  119. *
  120. * file=filename[,filename...]
  121. * Names of the files or block devices used for
  122. * backing storage.
  123. * ro=b[,b...] Default false, boolean for read-only access.
  124. * removable=b[,b...]
  125. * Default true, boolean for removable media.
  126. * cdrom=b[,b...] Default false, boolean for whether to emulate
  127. * a CD-ROM drive.
  128. * luns=N Default N = number of filenames, number of
  129. * LUNs to support.
  130. * stall Default determined according to the type of
  131. * USB device controller (usually true),
  132. * boolean to permit the driver to halt
  133. * bulk endpoints.
  134. *
  135. * The module parameters may be prefixed with some string. You need
  136. * to consult gadget's documentation or source to verify whether it is
  137. * using those module parameters and if it does what are the prefixes
  138. * (look for FSG_MODULE_PARAMETERS() macro usage, what's inside it is
  139. * the prefix).
  140. *
  141. *
  142. * Requirements are modest; only a bulk-in and a bulk-out endpoint are
  143. * needed. The memory requirement amounts to two 16K buffers, size
  144. * configurable by a parameter. Support is included for both
  145. * full-speed and high-speed operation.
  146. *
  147. * Note that the driver is slightly non-portable in that it assumes a
  148. * single memory/DMA buffer will be useable for bulk-in, bulk-out, and
  149. * interrupt-in endpoints. With most device controllers this isn't an
  150. * issue, but there may be some with hardware restrictions that prevent
  151. * a buffer from being used by more than one endpoint.
  152. *
  153. *
  154. * The pathnames of the backing files and the ro settings are
  155. * available in the attribute files "file" and "ro" in the lun<n> (or
  156. * to be more precise in a directory which name comes from
  157. * "lun_name_format" option!) subdirectory of the gadget's sysfs
  158. * directory. If the "removable" option is set, writing to these
  159. * files will simulate ejecting/loading the medium (writing an empty
  160. * line means eject) and adjusting a write-enable tab. Changes to the
  161. * ro setting are not allowed when the medium is loaded or if CD-ROM
  162. * emulation is being used.
  163. *
  164. * When a LUN receive an "eject" SCSI request (Start/Stop Unit),
  165. * if the LUN is removable, the backing file is released to simulate
  166. * ejection.
  167. *
  168. *
  169. * This function is heavily based on "File-backed Storage Gadget" by
  170. * Alan Stern which in turn is heavily based on "Gadget Zero" by David
  171. * Brownell. The driver's SCSI command interface was based on the
  172. * "Information technology - Small Computer System Interface - 2"
  173. * document from X3T9.2 Project 375D, Revision 10L, 7-SEP-93,
  174. * available at <http://www.t10.org/ftp/t10/drafts/s2/s2-r10l.pdf>.
  175. * The single exception is opcode 0x23 (READ FORMAT CAPACITIES), which
  176. * was based on the "Universal Serial Bus Mass Storage Class UFI
  177. * Command Specification" document, Revision 1.0, December 14, 1998,
  178. * available at
  179. * <http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf>.
  180. */
  181. /*
  182. * Driver Design
  183. *
  184. * The MSF is fairly straightforward. There is a main kernel
  185. * thread that handles most of the work. Interrupt routines field
  186. * callbacks from the controller driver: bulk- and interrupt-request
  187. * completion notifications, endpoint-0 events, and disconnect events.
  188. * Completion events are passed to the main thread by wakeup calls. Many
  189. * ep0 requests are handled at interrupt time, but SetInterface,
  190. * SetConfiguration, and device reset requests are forwarded to the
  191. * thread in the form of "exceptions" using SIGUSR1 signals (since they
  192. * should interrupt any ongoing file I/O operations).
  193. *
  194. * The thread's main routine implements the standard command/data/status
  195. * parts of a SCSI interaction. It and its subroutines are full of tests
  196. * for pending signals/exceptions -- all this polling is necessary since
  197. * the kernel has no setjmp/longjmp equivalents. (Maybe this is an
  198. * indication that the driver really wants to be running in userspace.)
  199. * An important point is that so long as the thread is alive it keeps an
  200. * open reference to the backing file. This will prevent unmounting
  201. * the backing file's underlying filesystem and could cause problems
  202. * during system shutdown, for example. To prevent such problems, the
  203. * thread catches INT, TERM, and KILL signals and converts them into
  204. * an EXIT exception.
  205. *
  206. * In normal operation the main thread is started during the gadget's
  207. * fsg_bind() callback and stopped during fsg_unbind(). But it can
  208. * also exit when it receives a signal, and there's no point leaving
  209. * the gadget running when the thread is dead. At of this moment, MSF
  210. * provides no way to deregister the gadget when thread dies -- maybe
  211. * a callback functions is needed.
  212. *
  213. * To provide maximum throughput, the driver uses a circular pipeline of
  214. * buffer heads (struct fsg_buffhd). In principle the pipeline can be
  215. * arbitrarily long; in practice the benefits don't justify having more
  216. * than 2 stages (i.e., double buffering). But it helps to think of the
  217. * pipeline as being a long one. Each buffer head contains a bulk-in and
  218. * a bulk-out request pointer (since the buffer can be used for both
  219. * output and input -- directions always are given from the host's
  220. * point of view) as well as a pointer to the buffer and various state
  221. * variables.
  222. *
  223. * Use of the pipeline follows a simple protocol. There is a variable
  224. * (fsg->next_buffhd_to_fill) that points to the next buffer head to use.
  225. * At any time that buffer head may still be in use from an earlier
  226. * request, so each buffer head has a state variable indicating whether
  227. * it is EMPTY, FULL, or BUSY. Typical use involves waiting for the
  228. * buffer head to be EMPTY, filling the buffer either by file I/O or by
  229. * USB I/O (during which the buffer head is BUSY), and marking the buffer
  230. * head FULL when the I/O is complete. Then the buffer will be emptied
  231. * (again possibly by USB I/O, during which it is marked BUSY) and
  232. * finally marked EMPTY again (possibly by a completion routine).
  233. *
  234. * A module parameter tells the driver to avoid stalling the bulk
  235. * endpoints wherever the transport specification allows. This is
  236. * necessary for some UDCs like the SuperH, which cannot reliably clear a
  237. * halt on a bulk endpoint. However, under certain circumstances the
  238. * Bulk-only specification requires a stall. In such cases the driver
  239. * will halt the endpoint and set a flag indicating that it should clear
  240. * the halt in software during the next device reset. Hopefully this
  241. * will permit everything to work correctly. Furthermore, although the
  242. * specification allows the bulk-out endpoint to halt when the host sends
  243. * too much data, implementing this would cause an unavoidable race.
  244. * The driver will always use the "no-stall" approach for OUT transfers.
  245. *
  246. * One subtle point concerns sending status-stage responses for ep0
  247. * requests. Some of these requests, such as device reset, can involve
  248. * interrupting an ongoing file I/O operation, which might take an
  249. * arbitrarily long time. During that delay the host might give up on
  250. * the original ep0 request and issue a new one. When that happens the
  251. * driver should not notify the host about completion of the original
  252. * request, as the host will no longer be waiting for it. So the driver
  253. * assigns to each ep0 request a unique tag, and it keeps track of the
  254. * tag value of the request associated with a long-running exception
  255. * (device-reset, interface-change, or configuration-change). When the
  256. * exception handler is finished, the status-stage response is submitted
  257. * only if the current ep0 request tag is equal to the exception request
  258. * tag. Thus only the most recently received ep0 request will get a
  259. * status-stage response.
  260. *
  261. * Warning: This driver source file is too long. It ought to be split up
  262. * into a header file plus about 3 separate .c files, to handle the details
  263. * of the Gadget, USB Mass Storage, and SCSI protocols.
  264. */
  265. /* #define VERBOSE_DEBUG */
  266. /* #define DUMP_MSGS */
  267. #include <config.h>
  268. #include <malloc.h>
  269. #include <common.h>
  270. #include <linux/err.h>
  271. #include <linux/usb/ch9.h>
  272. #include <linux/usb/gadget.h>
  273. #include <usb_mass_storage.h>
  274. #include <asm/unaligned.h>
  275. #include <linux/usb/gadget.h>
  276. #include <linux/usb/gadget.h>
  277. #include <linux/usb/composite.h>
  278. #include <usb/lin_gadget_compat.h>
  279. /*------------------------------------------------------------------------*/
  280. #define FSG_DRIVER_DESC "Mass Storage Function"
  281. #define FSG_DRIVER_VERSION "2012/06/5"
  282. static const char fsg_string_interface[] = "Mass Storage";
  283. #define FSG_NO_INTR_EP 1
  284. #define FSG_NO_DEVICE_STRINGS 1
  285. #define FSG_NO_OTG 1
  286. #define FSG_NO_INTR_EP 1
  287. #include "storage_common.c"
  288. /*-------------------------------------------------------------------------*/
  289. #define GFP_ATOMIC ((gfp_t) 0)
  290. #define PAGE_CACHE_SHIFT 12
  291. #define PAGE_CACHE_SIZE (1 << PAGE_CACHE_SHIFT)
  292. #define kthread_create(...) __builtin_return_address(0)
  293. #define wait_for_completion(...) do {} while (0)
  294. struct kref {int x; };
  295. struct completion {int x; };
  296. inline void set_bit(int nr, volatile void *addr)
  297. {
  298. int mask;
  299. unsigned int *a = (unsigned int *) addr;
  300. a += nr >> 5;
  301. mask = 1 << (nr & 0x1f);
  302. *a |= mask;
  303. }
  304. inline void clear_bit(int nr, volatile void *addr)
  305. {
  306. int mask;
  307. unsigned int *a = (unsigned int *) addr;
  308. a += nr >> 5;
  309. mask = 1 << (nr & 0x1f);
  310. *a &= ~mask;
  311. }
  312. struct fsg_dev;
  313. struct fsg_common;
  314. /* Data shared by all the FSG instances. */
  315. struct fsg_common {
  316. struct usb_gadget *gadget;
  317. struct fsg_dev *fsg, *new_fsg;
  318. struct usb_ep *ep0; /* Copy of gadget->ep0 */
  319. struct usb_request *ep0req; /* Copy of cdev->req */
  320. unsigned int ep0_req_tag;
  321. struct fsg_buffhd *next_buffhd_to_fill;
  322. struct fsg_buffhd *next_buffhd_to_drain;
  323. struct fsg_buffhd buffhds[FSG_NUM_BUFFERS];
  324. int cmnd_size;
  325. u8 cmnd[MAX_COMMAND_SIZE];
  326. unsigned int nluns;
  327. unsigned int lun;
  328. struct fsg_lun luns[FSG_MAX_LUNS];
  329. unsigned int bulk_out_maxpacket;
  330. enum fsg_state state; /* For exception handling */
  331. unsigned int exception_req_tag;
  332. enum data_direction data_dir;
  333. u32 data_size;
  334. u32 data_size_from_cmnd;
  335. u32 tag;
  336. u32 residue;
  337. u32 usb_amount_left;
  338. unsigned int can_stall:1;
  339. unsigned int free_storage_on_release:1;
  340. unsigned int phase_error:1;
  341. unsigned int short_packet_received:1;
  342. unsigned int bad_lun_okay:1;
  343. unsigned int running:1;
  344. int thread_wakeup_needed;
  345. struct completion thread_notifier;
  346. struct task_struct *thread_task;
  347. /* Callback functions. */
  348. const struct fsg_operations *ops;
  349. /* Gadget's private data. */
  350. void *private_data;
  351. const char *vendor_name; /* 8 characters or less */
  352. const char *product_name; /* 16 characters or less */
  353. u16 release;
  354. /* Vendor (8 chars), product (16 chars), release (4
  355. * hexadecimal digits) and NUL byte */
  356. char inquiry_string[8 + 16 + 4 + 1];
  357. struct kref ref;
  358. };
  359. struct fsg_config {
  360. unsigned nluns;
  361. struct fsg_lun_config {
  362. const char *filename;
  363. char ro;
  364. char removable;
  365. char cdrom;
  366. char nofua;
  367. } luns[FSG_MAX_LUNS];
  368. /* Callback functions. */
  369. const struct fsg_operations *ops;
  370. /* Gadget's private data. */
  371. void *private_data;
  372. const char *vendor_name; /* 8 characters or less */
  373. const char *product_name; /* 16 characters or less */
  374. char can_stall;
  375. };
  376. struct fsg_dev {
  377. struct usb_function function;
  378. struct usb_gadget *gadget; /* Copy of cdev->gadget */
  379. struct fsg_common *common;
  380. u16 interface_number;
  381. unsigned int bulk_in_enabled:1;
  382. unsigned int bulk_out_enabled:1;
  383. unsigned long atomic_bitflags;
  384. #define IGNORE_BULK_OUT 0
  385. struct usb_ep *bulk_in;
  386. struct usb_ep *bulk_out;
  387. #ifdef CONFIG_FSL_UTP
  388. void *utp;
  389. #endif
  390. };
  391. #ifdef CONFIG_FSL_UTP
  392. #include "fsl_updater.h"
  393. #endif
  394. static inline int __fsg_is_set(struct fsg_common *common,
  395. const char *func, unsigned line)
  396. {
  397. if (common->fsg)
  398. return 1;
  399. ERROR(common, "common->fsg is NULL in %s at %u\n", func, line);
  400. WARN_ON(1);
  401. return 0;
  402. }
  403. #define fsg_is_set(common) likely(__fsg_is_set(common, __func__, __LINE__))
  404. static inline struct fsg_dev *fsg_from_func(struct usb_function *f)
  405. {
  406. return container_of(f, struct fsg_dev, function);
  407. }
  408. typedef void (*fsg_routine_t)(struct fsg_dev *);
  409. static int exception_in_progress(struct fsg_common *common)
  410. {
  411. return common->state > FSG_STATE_IDLE;
  412. }
  413. /* Make bulk-out requests be divisible by the maxpacket size */
  414. static void set_bulk_out_req_length(struct fsg_common *common,
  415. struct fsg_buffhd *bh, unsigned int length)
  416. {
  417. unsigned int rem;
  418. bh->bulk_out_intended_length = length;
  419. rem = length % common->bulk_out_maxpacket;
  420. if (rem > 0)
  421. length += common->bulk_out_maxpacket - rem;
  422. bh->outreq->length = length;
  423. }
  424. /*-------------------------------------------------------------------------*/
  425. struct ums_board_info *ums_info;
  426. struct fsg_common *the_fsg_common;
  427. static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep)
  428. {
  429. const char *name;
  430. if (ep == fsg->bulk_in)
  431. name = "bulk-in";
  432. else if (ep == fsg->bulk_out)
  433. name = "bulk-out";
  434. else
  435. name = ep->name;
  436. DBG(fsg, "%s set halt\n", name);
  437. return usb_ep_set_halt(ep);
  438. }
  439. /*-------------------------------------------------------------------------*/
  440. /* These routines may be called in process context or in_irq */
  441. /* Caller must hold fsg->lock */
  442. static void wakeup_thread(struct fsg_common *common)
  443. {
  444. common->thread_wakeup_needed = 1;
  445. }
  446. static void raise_exception(struct fsg_common *common, enum fsg_state new_state)
  447. {
  448. /* Do nothing if a higher-priority exception is already in progress.
  449. * If a lower-or-equal priority exception is in progress, preempt it
  450. * and notify the main thread by sending it a signal. */
  451. if (common->state <= new_state) {
  452. common->exception_req_tag = common->ep0_req_tag;
  453. common->state = new_state;
  454. common->thread_wakeup_needed = 1;
  455. }
  456. }
  457. /*-------------------------------------------------------------------------*/
  458. static int ep0_queue(struct fsg_common *common)
  459. {
  460. int rc;
  461. rc = usb_ep_queue(common->ep0, common->ep0req, GFP_ATOMIC);
  462. common->ep0->driver_data = common;
  463. if (rc != 0 && rc != -ESHUTDOWN) {
  464. /* We can't do much more than wait for a reset */
  465. WARNING(common, "error in submission: %s --> %d\n",
  466. common->ep0->name, rc);
  467. }
  468. return rc;
  469. }
  470. /*-------------------------------------------------------------------------*/
  471. /* Bulk and interrupt endpoint completion handlers.
  472. * These always run in_irq. */
  473. static void bulk_in_complete(struct usb_ep *ep, struct usb_request *req)
  474. {
  475. struct fsg_common *common = ep->driver_data;
  476. struct fsg_buffhd *bh = req->context;
  477. if (req->status || req->actual != req->length)
  478. DBG(common, "%s --> %d, %u/%u\n", __func__,
  479. req->status, req->actual, req->length);
  480. if (req->status == -ECONNRESET) /* Request was cancelled */
  481. usb_ep_fifo_flush(ep);
  482. /* Hold the lock while we update the request and buffer states */
  483. bh->inreq_busy = 0;
  484. bh->state = BUF_STATE_EMPTY;
  485. wakeup_thread(common);
  486. }
  487. static void bulk_out_complete(struct usb_ep *ep, struct usb_request *req)
  488. {
  489. struct fsg_common *common = ep->driver_data;
  490. struct fsg_buffhd *bh = req->context;
  491. dump_msg(common, "bulk-out", req->buf, req->actual);
  492. if (req->status || req->actual != bh->bulk_out_intended_length)
  493. DBG(common, "%s --> %d, %u/%u\n", __func__,
  494. req->status, req->actual,
  495. bh->bulk_out_intended_length);
  496. if (req->status == -ECONNRESET) /* Request was cancelled */
  497. usb_ep_fifo_flush(ep);
  498. /* Hold the lock while we update the request and buffer states */
  499. bh->outreq_busy = 0;
  500. bh->state = BUF_STATE_FULL;
  501. wakeup_thread(common);
  502. }
  503. /*-------------------------------------------------------------------------*/
  504. /* Ep0 class-specific handlers. These always run in_irq. */
  505. static int fsg_setup(struct usb_function *f,
  506. const struct usb_ctrlrequest *ctrl)
  507. {
  508. struct fsg_dev *fsg = fsg_from_func(f);
  509. struct usb_request *req = fsg->common->ep0req;
  510. u16 w_index = get_unaligned_le16(&ctrl->wIndex);
  511. u16 w_value = get_unaligned_le16(&ctrl->wValue);
  512. u16 w_length = get_unaligned_le16(&ctrl->wLength);
  513. if (!fsg_is_set(fsg->common))
  514. return -EOPNOTSUPP;
  515. switch (ctrl->bRequest) {
  516. case USB_BULK_RESET_REQUEST:
  517. if (ctrl->bRequestType !=
  518. (USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE))
  519. break;
  520. if (w_index != fsg->interface_number || w_value != 0)
  521. return -EDOM;
  522. /* Raise an exception to stop the current operation
  523. * and reinitialize our state. */
  524. DBG(fsg, "bulk reset request\n");
  525. raise_exception(fsg->common, FSG_STATE_RESET);
  526. return DELAYED_STATUS;
  527. case USB_BULK_GET_MAX_LUN_REQUEST:
  528. if (ctrl->bRequestType !=
  529. (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE))
  530. break;
  531. if (w_index != fsg->interface_number || w_value != 0)
  532. return -EDOM;
  533. VDBG(fsg, "get max LUN\n");
  534. *(u8 *) req->buf = fsg->common->nluns - 1;
  535. /* Respond with data/status */
  536. req->length = min((u16)1, w_length);
  537. return ep0_queue(fsg->common);
  538. }
  539. VDBG(fsg,
  540. "unknown class-specific control req "
  541. "%02x.%02x v%04x i%04x l%u\n",
  542. ctrl->bRequestType, ctrl->bRequest,
  543. get_unaligned_le16(&ctrl->wValue), w_index, w_length);
  544. return -EOPNOTSUPP;
  545. }
  546. /*-------------------------------------------------------------------------*/
  547. /* All the following routines run in process context */
  548. /* Use this for bulk or interrupt transfers, not ep0 */
  549. static void start_transfer(struct fsg_dev *fsg, struct usb_ep *ep,
  550. struct usb_request *req, int *pbusy,
  551. enum fsg_buffer_state *state)
  552. {
  553. int rc;
  554. if (ep == fsg->bulk_in)
  555. dump_msg(fsg, "bulk-in", req->buf, req->length);
  556. *pbusy = 1;
  557. *state = BUF_STATE_BUSY;
  558. rc = usb_ep_queue(ep, req, GFP_KERNEL);
  559. if (rc != 0) {
  560. *pbusy = 0;
  561. *state = BUF_STATE_EMPTY;
  562. /* We can't do much more than wait for a reset */
  563. /* Note: currently the net2280 driver fails zero-length
  564. * submissions if DMA is enabled. */
  565. if (rc != -ESHUTDOWN && !(rc == -EOPNOTSUPP &&
  566. req->length == 0))
  567. WARNING(fsg, "error in submission: %s --> %d\n",
  568. ep->name, rc);
  569. }
  570. }
  571. #define START_TRANSFER_OR(common, ep_name, req, pbusy, state) \
  572. if (fsg_is_set(common)) \
  573. start_transfer((common)->fsg, (common)->fsg->ep_name, \
  574. req, pbusy, state); \
  575. else
  576. #define START_TRANSFER(common, ep_name, req, pbusy, state) \
  577. START_TRANSFER_OR(common, ep_name, req, pbusy, state) (void)0
  578. static void busy_indicator(void)
  579. {
  580. static int state;
  581. switch (state) {
  582. case 0:
  583. puts("\r|"); break;
  584. case 1:
  585. puts("\r/"); break;
  586. case 2:
  587. puts("\r-"); break;
  588. case 3:
  589. puts("\r\\"); break;
  590. case 4:
  591. puts("\r|"); break;
  592. case 5:
  593. puts("\r/"); break;
  594. case 6:
  595. puts("\r-"); break;
  596. case 7:
  597. puts("\r\\"); break;
  598. default:
  599. state = 0;
  600. }
  601. if (state++ == 8)
  602. state = 0;
  603. }
  604. static int sleep_thread(struct fsg_common *common)
  605. {
  606. int rc = 0;
  607. int i = 0, k = 0;
  608. /* Wait until a signal arrives or we are woken up */
  609. for (;;) {
  610. if (common->thread_wakeup_needed)
  611. break;
  612. if (++i == 50000) {
  613. busy_indicator();
  614. i = 0;
  615. k++;
  616. }
  617. usb_gadget_handle_interrupts();
  618. }
  619. common->thread_wakeup_needed = 0;
  620. return rc;
  621. }
  622. /*-------------------------------------------------------------------------*/
  623. static int do_read(struct fsg_common *common)
  624. {
  625. struct fsg_lun *curlun = &common->luns[common->lun];
  626. u32 lba;
  627. struct fsg_buffhd *bh;
  628. int rc;
  629. u32 amount_left;
  630. loff_t file_offset;
  631. unsigned int amount;
  632. unsigned int partial_page;
  633. ssize_t nread;
  634. /* Get the starting Logical Block Address and check that it's
  635. * not too big */
  636. if (common->cmnd[0] == SC_READ_6)
  637. lba = get_unaligned_be24(&common->cmnd[1]);
  638. else {
  639. lba = get_unaligned_be32(&common->cmnd[2]);
  640. /* We allow DPO (Disable Page Out = don't save data in the
  641. * cache) and FUA (Force Unit Access = don't read from the
  642. * cache), but we don't implement them. */
  643. if ((common->cmnd[1] & ~0x18) != 0) {
  644. curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  645. return -EINVAL;
  646. }
  647. }
  648. if (lba >= curlun->num_sectors) {
  649. curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  650. return -EINVAL;
  651. }
  652. file_offset = ((loff_t) lba) << 9;
  653. /* Carry out the file reads */
  654. amount_left = common->data_size_from_cmnd;
  655. if (unlikely(amount_left == 0))
  656. return -EIO; /* No default reply */
  657. for (;;) {
  658. /* Figure out how much we need to read:
  659. * Try to read the remaining amount.
  660. * But don't read more than the buffer size.
  661. * And don't try to read past the end of the file.
  662. * Finally, if we're not at a page boundary, don't read past
  663. * the next page.
  664. * If this means reading 0 then we were asked to read past
  665. * the end of file. */
  666. amount = min(amount_left, FSG_BUFLEN);
  667. partial_page = file_offset & (PAGE_CACHE_SIZE - 1);
  668. if (partial_page > 0)
  669. amount = min(amount, (unsigned int) PAGE_CACHE_SIZE -
  670. partial_page);
  671. /* Wait for the next buffer to become available */
  672. bh = common->next_buffhd_to_fill;
  673. while (bh->state != BUF_STATE_EMPTY) {
  674. rc = sleep_thread(common);
  675. if (rc)
  676. return rc;
  677. }
  678. /* If we were asked to read past the end of file,
  679. * end with an empty buffer. */
  680. if (amount == 0) {
  681. curlun->sense_data =
  682. SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  683. curlun->info_valid = 1;
  684. bh->inreq->length = 0;
  685. bh->state = BUF_STATE_FULL;
  686. break;
  687. }
  688. /* Perform the read */
  689. nread = 0;
  690. rc = ums_info->read_sector(&(ums_info->ums_dev),
  691. file_offset / SECTOR_SIZE,
  692. amount / SECTOR_SIZE,
  693. (char __user *)bh->buf);
  694. if (rc)
  695. return -EIO;
  696. nread = amount;
  697. VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
  698. (unsigned long long) file_offset,
  699. (int) nread);
  700. if (nread < 0) {
  701. LDBG(curlun, "error in file read: %d\n",
  702. (int) nread);
  703. nread = 0;
  704. } else if (nread < amount) {
  705. LDBG(curlun, "partial file read: %d/%u\n",
  706. (int) nread, amount);
  707. nread -= (nread & 511); /* Round down to a block */
  708. }
  709. file_offset += nread;
  710. amount_left -= nread;
  711. common->residue -= nread;
  712. bh->inreq->length = nread;
  713. bh->state = BUF_STATE_FULL;
  714. /* If an error occurred, report it and its position */
  715. if (nread < amount) {
  716. curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
  717. curlun->info_valid = 1;
  718. break;
  719. }
  720. if (amount_left == 0)
  721. break; /* No more left to read */
  722. /* Send this buffer and go read some more */
  723. bh->inreq->zero = 0;
  724. START_TRANSFER_OR(common, bulk_in, bh->inreq,
  725. &bh->inreq_busy, &bh->state)
  726. /* Don't know what to do if
  727. * common->fsg is NULL */
  728. return -EIO;
  729. common->next_buffhd_to_fill = bh->next;
  730. }
  731. return -EIO; /* No default reply */
  732. }
  733. /*-------------------------------------------------------------------------*/
  734. static int do_write(struct fsg_common *common)
  735. {
  736. struct fsg_lun *curlun = &common->luns[common->lun];
  737. u32 lba;
  738. struct fsg_buffhd *bh;
  739. int get_some_more;
  740. u32 amount_left_to_req, amount_left_to_write;
  741. loff_t usb_offset, file_offset;
  742. unsigned int amount;
  743. unsigned int partial_page;
  744. ssize_t nwritten;
  745. int rc;
  746. if (curlun->ro) {
  747. curlun->sense_data = SS_WRITE_PROTECTED;
  748. return -EINVAL;
  749. }
  750. /* Get the starting Logical Block Address and check that it's
  751. * not too big */
  752. if (common->cmnd[0] == SC_WRITE_6)
  753. lba = get_unaligned_be24(&common->cmnd[1]);
  754. else {
  755. lba = get_unaligned_be32(&common->cmnd[2]);
  756. /* We allow DPO (Disable Page Out = don't save data in the
  757. * cache) and FUA (Force Unit Access = write directly to the
  758. * medium). We don't implement DPO; we implement FUA by
  759. * performing synchronous output. */
  760. if (common->cmnd[1] & ~0x18) {
  761. curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  762. return -EINVAL;
  763. }
  764. }
  765. if (lba >= curlun->num_sectors) {
  766. curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  767. return -EINVAL;
  768. }
  769. /* Carry out the file writes */
  770. get_some_more = 1;
  771. file_offset = usb_offset = ((loff_t) lba) << 9;
  772. amount_left_to_req = common->data_size_from_cmnd;
  773. amount_left_to_write = common->data_size_from_cmnd;
  774. while (amount_left_to_write > 0) {
  775. /* Queue a request for more data from the host */
  776. bh = common->next_buffhd_to_fill;
  777. if (bh->state == BUF_STATE_EMPTY && get_some_more) {
  778. /* Figure out how much we want to get:
  779. * Try to get the remaining amount.
  780. * But don't get more than the buffer size.
  781. * And don't try to go past the end of the file.
  782. * If we're not at a page boundary,
  783. * don't go past the next page.
  784. * If this means getting 0, then we were asked
  785. * to write past the end of file.
  786. * Finally, round down to a block boundary. */
  787. amount = min(amount_left_to_req, FSG_BUFLEN);
  788. partial_page = usb_offset & (PAGE_CACHE_SIZE - 1);
  789. if (partial_page > 0)
  790. amount = min(amount,
  791. (unsigned int) PAGE_CACHE_SIZE - partial_page);
  792. if (amount == 0) {
  793. get_some_more = 0;
  794. curlun->sense_data =
  795. SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  796. curlun->info_valid = 1;
  797. continue;
  798. }
  799. amount -= (amount & 511);
  800. if (amount == 0) {
  801. /* Why were we were asked to transfer a
  802. * partial block? */
  803. get_some_more = 0;
  804. continue;
  805. }
  806. /* Get the next buffer */
  807. usb_offset += amount;
  808. common->usb_amount_left -= amount;
  809. amount_left_to_req -= amount;
  810. if (amount_left_to_req == 0)
  811. get_some_more = 0;
  812. /* amount is always divisible by 512, hence by
  813. * the bulk-out maxpacket size */
  814. bh->outreq->length = amount;
  815. bh->bulk_out_intended_length = amount;
  816. bh->outreq->short_not_ok = 1;
  817. START_TRANSFER_OR(common, bulk_out, bh->outreq,
  818. &bh->outreq_busy, &bh->state)
  819. /* Don't know what to do if
  820. * common->fsg is NULL */
  821. return -EIO;
  822. common->next_buffhd_to_fill = bh->next;
  823. continue;
  824. }
  825. /* Write the received data to the backing file */
  826. bh = common->next_buffhd_to_drain;
  827. if (bh->state == BUF_STATE_EMPTY && !get_some_more)
  828. break; /* We stopped early */
  829. if (bh->state == BUF_STATE_FULL) {
  830. common->next_buffhd_to_drain = bh->next;
  831. bh->state = BUF_STATE_EMPTY;
  832. /* Did something go wrong with the transfer? */
  833. if (bh->outreq->status != 0) {
  834. curlun->sense_data = SS_COMMUNICATION_FAILURE;
  835. curlun->info_valid = 1;
  836. break;
  837. }
  838. amount = bh->outreq->actual;
  839. /* Perform the write */
  840. rc = ums_info->write_sector(&(ums_info->ums_dev),
  841. file_offset / SECTOR_SIZE,
  842. amount / SECTOR_SIZE,
  843. (char __user *)bh->buf);
  844. if (rc)
  845. return -EIO;
  846. nwritten = amount;
  847. VLDBG(curlun, "file write %u @ %llu -> %d\n", amount,
  848. (unsigned long long) file_offset,
  849. (int) nwritten);
  850. if (nwritten < 0) {
  851. LDBG(curlun, "error in file write: %d\n",
  852. (int) nwritten);
  853. nwritten = 0;
  854. } else if (nwritten < amount) {
  855. LDBG(curlun, "partial file write: %d/%u\n",
  856. (int) nwritten, amount);
  857. nwritten -= (nwritten & 511);
  858. /* Round down to a block */
  859. }
  860. file_offset += nwritten;
  861. amount_left_to_write -= nwritten;
  862. common->residue -= nwritten;
  863. /* If an error occurred, report it and its position */
  864. if (nwritten < amount) {
  865. curlun->sense_data = SS_WRITE_ERROR;
  866. curlun->info_valid = 1;
  867. break;
  868. }
  869. /* Did the host decide to stop early? */
  870. if (bh->outreq->actual != bh->outreq->length) {
  871. common->short_packet_received = 1;
  872. break;
  873. }
  874. continue;
  875. }
  876. /* Wait for something to happen */
  877. rc = sleep_thread(common);
  878. if (rc)
  879. return rc;
  880. }
  881. return -EIO; /* No default reply */
  882. }
  883. /*-------------------------------------------------------------------------*/
  884. static int do_synchronize_cache(struct fsg_common *common)
  885. {
  886. return 0;
  887. }
  888. /*-------------------------------------------------------------------------*/
  889. static int do_verify(struct fsg_common *common)
  890. {
  891. struct fsg_lun *curlun = &common->luns[common->lun];
  892. u32 lba;
  893. u32 verification_length;
  894. struct fsg_buffhd *bh = common->next_buffhd_to_fill;
  895. loff_t file_offset;
  896. u32 amount_left;
  897. unsigned int amount;
  898. ssize_t nread;
  899. int rc;
  900. /* Get the starting Logical Block Address and check that it's
  901. * not too big */
  902. lba = get_unaligned_be32(&common->cmnd[2]);
  903. if (lba >= curlun->num_sectors) {
  904. curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  905. return -EINVAL;
  906. }
  907. /* We allow DPO (Disable Page Out = don't save data in the
  908. * cache) but we don't implement it. */
  909. if (common->cmnd[1] & ~0x10) {
  910. curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  911. return -EINVAL;
  912. }
  913. verification_length = get_unaligned_be16(&common->cmnd[7]);
  914. if (unlikely(verification_length == 0))
  915. return -EIO; /* No default reply */
  916. /* Prepare to carry out the file verify */
  917. amount_left = verification_length << 9;
  918. file_offset = ((loff_t) lba) << 9;
  919. /* Write out all the dirty buffers before invalidating them */
  920. /* Just try to read the requested blocks */
  921. while (amount_left > 0) {
  922. /* Figure out how much we need to read:
  923. * Try to read the remaining amount, but not more than
  924. * the buffer size.
  925. * And don't try to read past the end of the file.
  926. * If this means reading 0 then we were asked to read
  927. * past the end of file. */
  928. amount = min(amount_left, FSG_BUFLEN);
  929. if (amount == 0) {
  930. curlun->sense_data =
  931. SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  932. curlun->info_valid = 1;
  933. break;
  934. }
  935. /* Perform the read */
  936. nread = 0;
  937. rc = ums_info->read_sector(&(ums_info->ums_dev),
  938. file_offset / SECTOR_SIZE,
  939. amount / SECTOR_SIZE,
  940. (char __user *)bh->buf);
  941. if (rc)
  942. return -EIO;
  943. nread = amount;
  944. VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
  945. (unsigned long long) file_offset,
  946. (int) nread);
  947. if (nread < 0) {
  948. LDBG(curlun, "error in file verify: %d\n",
  949. (int) nread);
  950. nread = 0;
  951. } else if (nread < amount) {
  952. LDBG(curlun, "partial file verify: %d/%u\n",
  953. (int) nread, amount);
  954. nread -= (nread & 511); /* Round down to a sector */
  955. }
  956. if (nread == 0) {
  957. curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
  958. curlun->info_valid = 1;
  959. break;
  960. }
  961. file_offset += nread;
  962. amount_left -= nread;
  963. }
  964. return 0;
  965. }
  966. /*-------------------------------------------------------------------------*/
  967. static int do_inquiry(struct fsg_common *common, struct fsg_buffhd *bh)
  968. {
  969. struct fsg_lun *curlun = &common->luns[common->lun];
  970. static const char vendor_id[] = "Linux ";
  971. u8 *buf = (u8 *) bh->buf;
  972. if (!curlun) { /* Unsupported LUNs are okay */
  973. common->bad_lun_okay = 1;
  974. memset(buf, 0, 36);
  975. buf[0] = 0x7f; /* Unsupported, no device-type */
  976. buf[4] = 31; /* Additional length */
  977. return 36;
  978. }
  979. memset(buf, 0, 8);
  980. buf[0] = TYPE_DISK;
  981. buf[2] = 2; /* ANSI SCSI level 2 */
  982. buf[3] = 2; /* SCSI-2 INQUIRY data format */
  983. buf[4] = 31; /* Additional length */
  984. /* No special options */
  985. sprintf((char *) (buf + 8), "%-8s%-16s%04x", (char*) vendor_id ,
  986. ums_info->name, (u16) 0xffff);
  987. return 36;
  988. }
  989. static int do_request_sense(struct fsg_common *common, struct fsg_buffhd *bh)
  990. {
  991. struct fsg_lun *curlun = &common->luns[common->lun];
  992. u8 *buf = (u8 *) bh->buf;
  993. u32 sd, sdinfo;
  994. int valid;
  995. /*
  996. * From the SCSI-2 spec., section 7.9 (Unit attention condition):
  997. *
  998. * If a REQUEST SENSE command is received from an initiator
  999. * with a pending unit attention condition (before the target
  1000. * generates the contingent allegiance condition), then the
  1001. * target shall either:
  1002. * a) report any pending sense data and preserve the unit
  1003. * attention condition on the logical unit, or,
  1004. * b) report the unit attention condition, may discard any
  1005. * pending sense data, and clear the unit attention
  1006. * condition on the logical unit for that initiator.
  1007. *
  1008. * FSG normally uses option a); enable this code to use option b).
  1009. */
  1010. #if 0
  1011. if (curlun && curlun->unit_attention_data != SS_NO_SENSE) {
  1012. curlun->sense_data = curlun->unit_attention_data;
  1013. curlun->unit_attention_data = SS_NO_SENSE;
  1014. }
  1015. #endif
  1016. #ifdef CONFIG_FSL_UTP
  1017. if (utp_get_sense(fsg) == 0) { /* got the sense from the UTP */
  1018. sd = UTP_CTX(fsg)->sd;
  1019. sdinfo = UTP_CTX(fsg)->sdinfo;
  1020. valid = 0;
  1021. } else
  1022. #endif
  1023. if (!curlun) { /* Unsupported LUNs are okay */
  1024. common->bad_lun_okay = 1;
  1025. sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
  1026. sdinfo = 0;
  1027. valid = 0;
  1028. } else {
  1029. sd = curlun->sense_data;
  1030. valid = curlun->info_valid << 7;
  1031. curlun->sense_data = SS_NO_SENSE;
  1032. curlun->info_valid = 0;
  1033. }
  1034. memset(buf, 0, 18);
  1035. buf[0] = valid | 0x70; /* Valid, current error */
  1036. buf[2] = SK(sd);
  1037. put_unaligned_be32(sdinfo, &buf[3]); /* Sense information */
  1038. buf[7] = 18 - 8; /* Additional sense length */
  1039. buf[12] = ASC(sd);
  1040. buf[13] = ASCQ(sd);
  1041. #ifdef CONFIG_FSL_UTP
  1042. put_unaligned_be32(UTP_CTX(fsg)->sdinfo_h, &buf[8]);
  1043. #endif
  1044. return 18;
  1045. }
  1046. static int do_read_capacity(struct fsg_common *common, struct fsg_buffhd *bh)
  1047. {
  1048. struct fsg_lun *curlun = &common->luns[common->lun];
  1049. u32 lba = get_unaligned_be32(&common->cmnd[2]);
  1050. int pmi = common->cmnd[8];
  1051. u8 *buf = (u8 *) bh->buf;
  1052. /* Check the PMI and LBA fields */
  1053. if (pmi > 1 || (pmi == 0 && lba != 0)) {
  1054. curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  1055. return -EINVAL;
  1056. }
  1057. put_unaligned_be32(curlun->num_sectors - 1, &buf[0]);
  1058. /* Max logical block */
  1059. put_unaligned_be32(512, &buf[4]); /* Block length */
  1060. return 8;
  1061. }
  1062. static int do_read_header(struct fsg_common *common, struct fsg_buffhd *bh)
  1063. {
  1064. struct fsg_lun *curlun = &common->luns[common->lun];
  1065. int msf = common->cmnd[1] & 0x02;
  1066. u32 lba = get_unaligned_be32(&common->cmnd[2]);
  1067. u8 *buf = (u8 *) bh->buf;
  1068. if (common->cmnd[1] & ~0x02) { /* Mask away MSF */
  1069. curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  1070. return -EINVAL;
  1071. }
  1072. if (lba >= curlun->num_sectors) {
  1073. curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  1074. return -EINVAL;
  1075. }
  1076. memset(buf, 0, 8);
  1077. buf[0] = 0x01; /* 2048 bytes of user data, rest is EC */
  1078. store_cdrom_address(&buf[4], msf, lba);
  1079. return 8;
  1080. }
  1081. static int do_read_toc(struct fsg_common *common, struct fsg_buffhd *bh)
  1082. {
  1083. struct fsg_lun *curlun = &common->luns[common->lun];
  1084. int msf = common->cmnd[1] & 0x02;
  1085. int start_track = common->cmnd[6];
  1086. u8 *buf = (u8 *) bh->buf;
  1087. if ((common->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */
  1088. start_track > 1) {
  1089. curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  1090. return -EINVAL;
  1091. }
  1092. memset(buf, 0, 20);
  1093. buf[1] = (20-2); /* TOC data length */
  1094. buf[2] = 1; /* First track number */
  1095. buf[3] = 1; /* Last track number */
  1096. buf[5] = 0x16; /* Data track, copying allowed */
  1097. buf[6] = 0x01; /* Only track is number 1 */
  1098. store_cdrom_address(&buf[8], msf, 0);
  1099. buf[13] = 0x16; /* Lead-out track is data */
  1100. buf[14] = 0xAA; /* Lead-out track number */
  1101. store_cdrom_address(&buf[16], msf, curlun->num_sectors);
  1102. return 20;
  1103. }
  1104. static int do_mode_sense(struct fsg_common *common, struct fsg_buffhd *bh)
  1105. {
  1106. struct fsg_lun *curlun = &common->luns[common->lun];
  1107. int mscmnd = common->cmnd[0];
  1108. u8 *buf = (u8 *) bh->buf;
  1109. u8 *buf0 = buf;
  1110. int pc, page_code;
  1111. int changeable_values, all_pages;
  1112. int valid_page = 0;
  1113. int len, limit;
  1114. if ((common->cmnd[1] & ~0x08) != 0) { /* Mask away DBD */
  1115. curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  1116. return -EINVAL;
  1117. }
  1118. pc = common->cmnd[2] >> 6;
  1119. page_code = common->cmnd[2] & 0x3f;
  1120. if (pc == 3) {
  1121. curlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
  1122. return -EINVAL;
  1123. }
  1124. changeable_values = (pc == 1);
  1125. all_pages = (page_code == 0x3f);
  1126. /* Write the mode parameter header. Fixed values are: default
  1127. * medium type, no cache control (DPOFUA), and no block descriptors.
  1128. * The only variable value is the WriteProtect bit. We will fill in
  1129. * the mode data length later. */
  1130. memset(buf, 0, 8);
  1131. if (mscmnd == SC_MODE_SENSE_6) {
  1132. buf[2] = (curlun->ro ? 0x80 : 0x00); /* WP, DPOFUA */
  1133. buf += 4;
  1134. limit = 255;
  1135. } else { /* SC_MODE_SENSE_10 */
  1136. buf[3] = (curlun->ro ? 0x80 : 0x00); /* WP, DPOFUA */
  1137. buf += 8;
  1138. limit = 65535; /* Should really be FSG_BUFLEN */
  1139. }
  1140. /* No block descriptors */
  1141. /* The mode pages, in numerical order. The only page we support
  1142. * is the Caching page. */
  1143. if (page_code == 0x08 || all_pages) {
  1144. valid_page = 1;
  1145. buf[0] = 0x08; /* Page code */
  1146. buf[1] = 10; /* Page length */
  1147. memset(buf+2, 0, 10); /* None of the fields are changeable */
  1148. if (!changeable_values) {
  1149. buf[2] = 0x04; /* Write cache enable, */
  1150. /* Read cache not disabled */
  1151. /* No cache retention priorities */
  1152. put_unaligned_be16(0xffff, &buf[4]);
  1153. /* Don't disable prefetch */
  1154. /* Minimum prefetch = 0 */
  1155. put_unaligned_be16(0xffff, &buf[8]);
  1156. /* Maximum prefetch */
  1157. put_unaligned_be16(0xffff, &buf[10]);
  1158. /* Maximum prefetch ceiling */
  1159. }
  1160. buf += 12;
  1161. }
  1162. /* Check that a valid page was requested and the mode data length
  1163. * isn't too long. */
  1164. len = buf - buf0;
  1165. if (!valid_page || len > limit) {
  1166. curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  1167. return -EINVAL;
  1168. }
  1169. /* Store the mode data length */
  1170. if (mscmnd == SC_MODE_SENSE_6)
  1171. buf0[0] = len - 1;
  1172. else
  1173. put_unaligned_be16(len - 2, buf0);
  1174. return len;
  1175. }
  1176. static int do_start_stop(struct fsg_common *common)
  1177. {
  1178. struct fsg_lun *curlun = &common->luns[common->lun];
  1179. if (!curlun) {
  1180. return -EINVAL;
  1181. } else if (!curlun->removable) {
  1182. curlun->sense_data = SS_INVALID_COMMAND;
  1183. return -EINVAL;
  1184. }
  1185. return 0;
  1186. }
  1187. static int do_prevent_allow(struct fsg_common *common)
  1188. {
  1189. struct fsg_lun *curlun = &common->luns[common->lun];
  1190. int prevent;
  1191. if (!curlun->removable) {
  1192. curlun->sense_data = SS_INVALID_COMMAND;
  1193. return -EINVAL;
  1194. }
  1195. prevent = common->cmnd[4] & 0x01;
  1196. if ((common->cmnd[4] & ~0x01) != 0) { /* Mask away Prevent */
  1197. curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  1198. return -EINVAL;
  1199. }
  1200. if (curlun->prevent_medium_removal && !prevent)
  1201. fsg_lun_fsync_sub(curlun);
  1202. curlun->prevent_medium_removal = prevent;
  1203. return 0;
  1204. }
  1205. static int do_read_format_capacities(struct fsg_common *common,
  1206. struct fsg_buffhd *bh)
  1207. {
  1208. struct fsg_lun *curlun = &common->luns[common->lun];
  1209. u8 *buf = (u8 *) bh->buf;
  1210. buf[0] = buf[1] = buf[2] = 0;
  1211. buf[3] = 8; /* Only the Current/Maximum Capacity Descriptor */
  1212. buf += 4;
  1213. put_unaligned_be32(curlun->num_sectors, &buf[0]);
  1214. /* Number of blocks */
  1215. put_unaligned_be32(512, &buf[4]); /* Block length */
  1216. buf[4] = 0x02; /* Current capacity */
  1217. return 12;
  1218. }
  1219. static int do_mode_select(struct fsg_common *common, struct fsg_buffhd *bh)
  1220. {
  1221. struct fsg_lun *curlun = &common->luns[common->lun];
  1222. /* We don't support MODE SELECT */
  1223. if (curlun)
  1224. curlun->sense_data = SS_INVALID_COMMAND;
  1225. return -EINVAL;
  1226. }
  1227. /*-------------------------------------------------------------------------*/
  1228. static int halt_bulk_in_endpoint(struct fsg_dev *fsg)
  1229. {
  1230. int rc;
  1231. rc = fsg_set_halt(fsg, fsg->bulk_in);
  1232. if (rc == -EAGAIN)
  1233. VDBG(fsg, "delayed bulk-in endpoint halt\n");
  1234. while (rc != 0) {
  1235. if (rc != -EAGAIN) {
  1236. WARNING(fsg, "usb_ep_set_halt -> %d\n", rc);
  1237. rc = 0;
  1238. break;
  1239. }
  1240. rc = usb_ep_set_halt(fsg->bulk_in);
  1241. }
  1242. return rc;
  1243. }
  1244. static int wedge_bulk_in_endpoint(struct fsg_dev *fsg)
  1245. {
  1246. int rc;
  1247. DBG(fsg, "bulk-in set wedge\n");
  1248. rc = 0; /* usb_ep_set_wedge(fsg->bulk_in); */
  1249. if (rc == -EAGAIN)
  1250. VDBG(fsg, "delayed bulk-in endpoint wedge\n");
  1251. while (rc != 0) {
  1252. if (rc != -EAGAIN) {
  1253. WARNING(fsg, "usb_ep_set_wedge -> %d\n", rc);
  1254. rc = 0;
  1255. break;
  1256. }
  1257. }
  1258. return rc;
  1259. }
  1260. static int pad_with_zeros(struct fsg_dev *fsg)
  1261. {
  1262. struct fsg_buffhd *bh = fsg->common->next_buffhd_to_fill;
  1263. u32 nkeep = bh->inreq->length;
  1264. u32 nsend;
  1265. int rc;
  1266. bh->state = BUF_STATE_EMPTY; /* For the first iteration */
  1267. fsg->common->usb_amount_left = nkeep + fsg->common->residue;
  1268. while (fsg->common->usb_amount_left > 0) {
  1269. /* Wait for the next buffer to be free */
  1270. while (bh->state != BUF_STATE_EMPTY) {
  1271. rc = sleep_thread(fsg->common);
  1272. if (rc)
  1273. return rc;
  1274. }
  1275. nsend = min(fsg->common->usb_amount_left, FSG_BUFLEN);
  1276. memset(bh->buf + nkeep, 0, nsend - nkeep);
  1277. bh->inreq->length = nsend;
  1278. bh->inreq->zero = 0;
  1279. start_transfer(fsg, fsg->bulk_in, bh->inreq,
  1280. &bh->inreq_busy, &bh->state);
  1281. bh = fsg->common->next_buffhd_to_fill = bh->next;
  1282. fsg->common->usb_amount_left -= nsend;
  1283. nkeep = 0;
  1284. }
  1285. return 0;
  1286. }
  1287. static int throw_away_data(struct fsg_common *common)
  1288. {
  1289. struct fsg_buffhd *bh;
  1290. u32 amount;
  1291. int rc;
  1292. for (bh = common->next_buffhd_to_drain;
  1293. bh->state != BUF_STATE_EMPTY || common->usb_amount_left > 0;
  1294. bh = common->next_buffhd_to_drain) {
  1295. /* Throw away the data in a filled buffer */
  1296. if (bh->state == BUF_STATE_FULL) {
  1297. bh->state = BUF_STATE_EMPTY;
  1298. common->next_buffhd_to_drain = bh->next;
  1299. /* A short packet or an error ends everything */
  1300. if (bh->outreq->actual != bh->outreq->length ||
  1301. bh->outreq->status != 0) {
  1302. raise_exception(common,
  1303. FSG_STATE_ABORT_BULK_OUT);
  1304. return -EINTR;
  1305. }
  1306. continue;
  1307. }
  1308. /* Try to submit another request if we need one */
  1309. bh = common->next_buffhd_to_fill;
  1310. if (bh->state == BUF_STATE_EMPTY
  1311. && common->usb_amount_left > 0) {
  1312. amount = min(common->usb_amount_left, FSG_BUFLEN);
  1313. /* amount is always divisible by 512, hence by
  1314. * the bulk-out maxpacket size */
  1315. bh->outreq->length = amount;
  1316. bh->bulk_out_intended_length = amount;
  1317. bh->outreq->short_not_ok = 1;
  1318. START_TRANSFER_OR(common, bulk_out, bh->outreq,
  1319. &bh->outreq_busy, &bh->state)
  1320. /* Don't know what to do if
  1321. * common->fsg is NULL */
  1322. return -EIO;
  1323. common->next_buffhd_to_fill = bh->next;
  1324. common->usb_amount_left -= amount;
  1325. continue;
  1326. }
  1327. /* Otherwise wait for something to happen */
  1328. rc = sleep_thread(common);
  1329. if (rc)
  1330. return rc;
  1331. }
  1332. return 0;
  1333. }
  1334. static int finish_reply(struct fsg_common *common)
  1335. {
  1336. struct fsg_buffhd *bh = common->next_buffhd_to_fill;
  1337. int rc = 0;
  1338. switch (common->data_dir) {
  1339. case DATA_DIR_NONE:
  1340. break; /* Nothing to send */
  1341. /* If we don't know whether the host wants to read or write,
  1342. * this must be CB or CBI with an unknown command. We mustn't
  1343. * try to send or receive any data. So stall both bulk pipes
  1344. * if we can and wait for a reset. */
  1345. case DATA_DIR_UNKNOWN:
  1346. if (!common->can_stall) {
  1347. /* Nothing */
  1348. } else if (fsg_is_set(common)) {
  1349. fsg_set_halt(common->fsg, common->fsg->bulk_out);
  1350. rc = halt_bulk_in_endpoint(common->fsg);
  1351. } else {
  1352. /* Don't know what to do if common->fsg is NULL */
  1353. rc = -EIO;
  1354. }
  1355. break;
  1356. /* All but the last buffer of data must have already been sent */
  1357. case DATA_DIR_TO_HOST:
  1358. if (common->data_size == 0) {
  1359. /* Nothing to send */
  1360. /* If there's no residue, simply send the last buffer */
  1361. } else if (common->residue == 0) {
  1362. bh->inreq->zero = 0;
  1363. START_TRANSFER_OR(common, bulk_in, bh->inreq,
  1364. &bh->inreq_busy, &bh->state)
  1365. return -EIO;
  1366. common->next_buffhd_to_fill = bh->next;
  1367. /* For Bulk-only, if we're allowed to stall then send the
  1368. * short packet and halt the bulk-in endpoint. If we can't
  1369. * stall, pad out the remaining data with 0's. */
  1370. } else if (common->can_stall) {
  1371. bh->inreq->zero = 1;
  1372. START_TRANSFER_OR(common, bulk_in, bh->inreq,
  1373. &bh->inreq_busy, &bh->state)
  1374. /* Don't know what to do if
  1375. * common->fsg is NULL */
  1376. rc = -EIO;
  1377. common->next_buffhd_to_fill = bh->next;
  1378. if (common->fsg)
  1379. rc = halt_bulk_in_endpoint(common->fsg);
  1380. } else if (fsg_is_set(common)) {
  1381. rc = pad_with_zeros(common->fsg);
  1382. } else {
  1383. /* Don't know what to do if common->fsg is NULL */
  1384. rc = -EIO;
  1385. }
  1386. break;
  1387. /* We have processed all we want from the data the host has sent.
  1388. * There may still be outstanding bulk-out requests. */
  1389. case DATA_DIR_FROM_HOST:
  1390. if (common->residue == 0) {
  1391. /* Nothing to receive */
  1392. /* Did the host stop sending unexpectedly early? */
  1393. } else if (common->short_packet_received) {
  1394. raise_exception(common, FSG_STATE_ABORT_BULK_OUT);
  1395. rc = -EINTR;
  1396. /* We haven't processed all the incoming data. Even though
  1397. * we may be allowed to stall, doing so would cause a race.
  1398. * The controller may already have ACK'ed all the remaining
  1399. * bulk-out packets, in which case the host wouldn't see a
  1400. * STALL. Not realizing the endpoint was halted, it wouldn't
  1401. * clear the halt -- leading to problems later on. */
  1402. #if 0
  1403. } else if (common->can_stall) {
  1404. if (fsg_is_set(common))
  1405. fsg_set_halt(common->fsg,
  1406. common->fsg->bulk_out);
  1407. raise_exception(common, FSG_STATE_ABORT_BULK_OUT);
  1408. rc = -EINTR;
  1409. #endif
  1410. /* We can't stall. Read in the excess data and throw it
  1411. * all away. */
  1412. } else {
  1413. rc = throw_away_data(common);
  1414. }
  1415. break;
  1416. }
  1417. return rc;
  1418. }
  1419. static int send_status(struct fsg_common *common)
  1420. {
  1421. struct fsg_lun *curlun = &common->luns[common->lun];
  1422. struct fsg_buffhd *bh;
  1423. struct bulk_cs_wrap *csw;
  1424. int rc;
  1425. u8 status = USB_STATUS_PASS;
  1426. u32 sd, sdinfo = 0;
  1427. /* Wait for the next buffer to become available */
  1428. bh = common->next_buffhd_to_fill;
  1429. while (bh->state != BUF_STATE_EMPTY) {
  1430. rc = sleep_thread(common);
  1431. if (rc)
  1432. return rc;
  1433. }
  1434. if (curlun)
  1435. sd = curlun->sense_data;
  1436. else if (common->bad_lun_okay)
  1437. sd = SS_NO_SENSE;
  1438. else
  1439. sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
  1440. if (common->phase_error) {
  1441. DBG(common, "sending phase-error status\n");
  1442. status = USB_STATUS_PHASE_ERROR;
  1443. sd = SS_INVALID_COMMAND;
  1444. } else if (sd != SS_NO_SENSE) {
  1445. DBG(common, "sending command-failure status\n");
  1446. status = USB_STATUS_FAIL;
  1447. VDBG(common, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;"
  1448. " info x%x\n",
  1449. SK(sd), ASC(sd), ASCQ(sd), sdinfo);
  1450. }
  1451. /* Store and send the Bulk-only CSW */
  1452. csw = (void *)bh->buf;
  1453. csw->Signature = cpu_to_le32(USB_BULK_CS_SIG);
  1454. csw->Tag = common->tag;
  1455. csw->Residue = cpu_to_le32(common->residue);
  1456. csw->Status = status;
  1457. bh->inreq->length = USB_BULK_CS_WRAP_LEN;
  1458. bh->inreq->zero = 0;
  1459. START_TRANSFER_OR(common, bulk_in, bh->inreq,
  1460. &bh->inreq_busy, &bh->state)
  1461. /* Don't know what to do if common->fsg is NULL */
  1462. return -EIO;
  1463. common->next_buffhd_to_fill = bh->next;
  1464. return 0;
  1465. }
  1466. /*-------------------------------------------------------------------------*/
  1467. /* Check whether the command is properly formed and whether its data size
  1468. * and direction agree with the values we already have. */
  1469. static int check_command(struct fsg_common *common, int cmnd_size,
  1470. enum data_direction data_dir, unsigned int mask,
  1471. int needs_medium, const char *name)
  1472. {
  1473. int i;
  1474. int lun = common->cmnd[1] >> 5;
  1475. static const char dirletter[4] = {'u', 'o', 'i', 'n'};
  1476. char hdlen[20];
  1477. struct fsg_lun *curlun;
  1478. hdlen[0] = 0;
  1479. if (common->data_dir != DATA_DIR_UNKNOWN)
  1480. sprintf(hdlen, ", H%c=%u", dirletter[(int) common->data_dir],
  1481. common->data_size);
  1482. VDBG(common, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n",
  1483. name, cmnd_size, dirletter[(int) data_dir],
  1484. common->data_size_from_cmnd, common->cmnd_size, hdlen);
  1485. /* We can't reply at all until we know the correct data direction
  1486. * and size. */
  1487. if (common->data_size_from_cmnd == 0)
  1488. data_dir = DATA_DIR_NONE;
  1489. if (common->data_size < common->data_size_from_cmnd) {
  1490. /* Host data size < Device data size is a phase error.
  1491. * Carry out the command, but only transfer as much as
  1492. * we are allowed. */
  1493. common->data_size_from_cmnd = common->data_size;
  1494. common->phase_error = 1;
  1495. }
  1496. common->residue = common->data_size;
  1497. common->usb_amount_left = common->data_size;
  1498. /* Conflicting data directions is a phase error */
  1499. if (common->data_dir != data_dir
  1500. && common->data_size_from_cmnd > 0) {
  1501. common->phase_error = 1;
  1502. return -EINVAL;
  1503. }
  1504. /* Verify the length of the command itself */
  1505. if (cmnd_size != common->cmnd_size) {
  1506. /* Special case workaround: There are plenty of buggy SCSI
  1507. * implementations. Many have issues with cbw->Length
  1508. * field passing a wrong command size. For those cases we
  1509. * always try to work around the problem by using the length
  1510. * sent by the host side provided it is at least as large
  1511. * as the correct command length.
  1512. * Examples of such cases would be MS-Windows, which issues
  1513. * REQUEST SENSE with cbw->Length == 12 where it should
  1514. * be 6, and xbox360 issuing INQUIRY, TEST UNIT READY and
  1515. * REQUEST SENSE with cbw->Length == 10 where it should
  1516. * be 6 as well.
  1517. */
  1518. if (cmnd_size <= common->cmnd_size) {
  1519. DBG(common, "%s is buggy! Expected length %d "
  1520. "but we got %d\n", name,
  1521. cmnd_size, common->cmnd_size);
  1522. cmnd_size = common->cmnd_size;
  1523. } else {
  1524. common->phase_error = 1;
  1525. return -EINVAL;
  1526. }
  1527. }
  1528. /* Check that the LUN values are consistent */
  1529. if (common->lun != lun)
  1530. DBG(common, "using LUN %d from CBW, not LUN %d from CDB\n",
  1531. common->lun, lun);
  1532. /* Check the LUN */
  1533. if (common->lun >= 0 && common->lun < common->nluns) {
  1534. curlun = &common->luns[common->lun];
  1535. if (common->cmnd[0] != SC_REQUEST_SENSE) {
  1536. curlun->sense_data = SS_NO_SENSE;
  1537. curlun->info_valid = 0;
  1538. }
  1539. } else {
  1540. curlun = NULL;
  1541. common->bad_lun_okay = 0;
  1542. /* INQUIRY and REQUEST SENSE commands are explicitly allowed
  1543. * to use unsupported LUNs; all others may not. */
  1544. if (common->cmnd[0] != SC_INQUIRY &&
  1545. common->cmnd[0] != SC_REQUEST_SENSE) {
  1546. DBG(common, "unsupported LUN %d\n", common->lun);
  1547. return -EINVAL;
  1548. }
  1549. }
  1550. #if 0
  1551. /* If a unit attention condition exists, only INQUIRY and
  1552. * REQUEST SENSE commands are allowed; anything else must fail. */
  1553. if (curlun && curlun->unit_attention_data != SS_NO_SENSE &&
  1554. common->cmnd[0] != SC_INQUIRY &&
  1555. common->cmnd[0] != SC_REQUEST_SENSE) {
  1556. curlun->sense_data = curlun->unit_attention_data;
  1557. curlun->unit_attention_data = SS_NO_SENSE;
  1558. return -EINVAL;
  1559. }
  1560. #endif
  1561. /* Check that only command bytes listed in the mask are non-zero */
  1562. common->cmnd[1] &= 0x1f; /* Mask away the LUN */
  1563. for (i = 1; i < cmnd_size; ++i) {
  1564. if (common->cmnd[i] && !(mask & (1 << i))) {
  1565. if (curlun)
  1566. curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  1567. return -EINVAL;
  1568. }
  1569. }
  1570. return 0;
  1571. }
  1572. static int do_scsi_command(struct fsg_common *common)
  1573. {
  1574. struct fsg_buffhd *bh;
  1575. int rc;
  1576. int reply = -EINVAL;
  1577. int i;
  1578. static char unknown[16];
  1579. struct fsg_lun *curlun = &common->luns[common->lun];
  1580. dump_cdb(common);
  1581. /* Wait for the next buffer to become available for data or status */
  1582. bh = common->next_buffhd_to_fill;
  1583. common->next_buffhd_to_drain = bh;
  1584. while (bh->state != BUF_STATE_EMPTY) {
  1585. rc = sleep_thread(common);
  1586. if (rc)
  1587. return rc;
  1588. }
  1589. common->phase_error = 0;
  1590. common->short_packet_received = 0;
  1591. #ifdef CONFIG_FSL_UTP
  1592. reply = utp_handle_message(fsg, fsg->cmnd, reply);
  1593. if (reply != -EINVAL)
  1594. return reply;
  1595. #endif
  1596. down_read(&common->filesem); /* We're using the backing file */
  1597. switch (common->cmnd[0]) {
  1598. case SC_INQUIRY:
  1599. common->data_size_from_cmnd = common->cmnd[4];
  1600. reply = check_command(common, 6, DATA_DIR_TO_HOST,
  1601. (1<<4), 0,
  1602. "INQUIRY");
  1603. if (reply == 0)
  1604. reply = do_inquiry(common, bh);
  1605. break;
  1606. case SC_MODE_SELECT_6:
  1607. common->data_size_from_cmnd = common->cmnd[4];
  1608. reply = check_command(common, 6, DATA_DIR_FROM_HOST,
  1609. (1<<1) | (1<<4), 0,
  1610. "MODE SELECT(6)");
  1611. if (reply == 0)
  1612. reply = do_mode_select(common, bh);
  1613. break;
  1614. case SC_MODE_SELECT_10:
  1615. common->data_size_from_cmnd =
  1616. get_unaligned_be16(&common->cmnd[7]);
  1617. reply = check_command(common, 10, DATA_DIR_FROM_HOST,
  1618. (1<<1) | (3<<7), 0,
  1619. "MODE SELECT(10)");
  1620. if (reply == 0)
  1621. reply = do_mode_select(common, bh);
  1622. break;
  1623. case SC_MODE_SENSE_6:
  1624. common->data_size_from_cmnd = common->cmnd[4];
  1625. reply = check_command(common, 6, DATA_DIR_TO_HOST,
  1626. (1<<1) | (1<<2) | (1<<4), 0,
  1627. "MODE SENSE(6)");
  1628. if (reply == 0)
  1629. reply = do_mode_sense(common, bh);
  1630. break;
  1631. case SC_MODE_SENSE_10:
  1632. common->data_size_from_cmnd =
  1633. get_unaligned_be16(&common->cmnd[7]);
  1634. reply = check_command(common, 10, DATA_DIR_TO_HOST,
  1635. (1<<1) | (1<<2) | (3<<7), 0,
  1636. "MODE SENSE(10)");
  1637. if (reply == 0)
  1638. reply = do_mode_sense(common, bh);
  1639. break;
  1640. case SC_PREVENT_ALLOW_MEDIUM_REMOVAL:
  1641. common->data_size_from_cmnd = 0;
  1642. reply = check_command(common, 6, DATA_DIR_NONE,
  1643. (1<<4), 0,
  1644. "PREVENT-ALLOW MEDIUM REMOVAL");
  1645. if (reply == 0)
  1646. reply = do_prevent_allow(common);
  1647. break;
  1648. case SC_READ_6:
  1649. i = common->cmnd[4];
  1650. common->data_size_from_cmnd = (i == 0 ? 256 : i) << 9;
  1651. reply = check_command(common, 6, DATA_DIR_TO_HOST,
  1652. (7<<1) | (1<<4), 1,
  1653. "READ(6)");
  1654. if (reply == 0)
  1655. reply = do_read(common);
  1656. break;
  1657. case SC_READ_10:
  1658. common->data_size_from_cmnd =
  1659. get_unaligned_be16(&common->cmnd[7]) << 9;
  1660. reply = check_command(common, 10, DATA_DIR_TO_HOST,
  1661. (1<<1) | (0xf<<2) | (3<<7), 1,
  1662. "READ(10)");
  1663. if (reply == 0)
  1664. reply = do_read(common);
  1665. break;
  1666. case SC_READ_12:
  1667. common->data_size_from_cmnd =
  1668. get_unaligned_be32(&common->cmnd[6]) << 9;
  1669. reply = check_command(common, 12, DATA_DIR_TO_HOST,
  1670. (1<<1) | (0xf<<2) | (0xf<<6), 1,
  1671. "READ(12)");
  1672. if (reply == 0)
  1673. reply = do_read(common);
  1674. break;
  1675. case SC_READ_CAPACITY:
  1676. common->data_size_from_cmnd = 8;
  1677. reply = check_command(common, 10, DATA_DIR_TO_HOST,
  1678. (0xf<<2) | (1<<8), 1,
  1679. "READ CAPACITY");
  1680. if (reply == 0)
  1681. reply = do_read_capacity(common, bh);
  1682. break;
  1683. case SC_READ_HEADER:
  1684. if (!common->luns[common->lun].cdrom)
  1685. goto unknown_cmnd;
  1686. common->data_size_from_cmnd =
  1687. get_unaligned_be16(&common->cmnd[7]);
  1688. reply = check_command(common, 10, DATA_DIR_TO_HOST,
  1689. (3<<7) | (0x1f<<1), 1,
  1690. "READ HEADER");
  1691. if (reply == 0)
  1692. reply = do_read_header(common, bh);
  1693. break;
  1694. case SC_READ_TOC:
  1695. if (!common->luns[common->lun].cdrom)
  1696. goto unknown_cmnd;
  1697. common->data_size_from_cmnd =
  1698. get_unaligned_be16(&common->cmnd[7]);
  1699. reply = check_command(common, 10, DATA_DIR_TO_HOST,
  1700. (7<<6) | (1<<1), 1,
  1701. "READ TOC");
  1702. if (reply == 0)
  1703. reply = do_read_toc(common, bh);
  1704. break;
  1705. case SC_READ_FORMAT_CAPACITIES:
  1706. common->data_size_from_cmnd =
  1707. get_unaligned_be16(&common->cmnd[7]);
  1708. reply = check_command(common, 10, DATA_DIR_TO_HOST,
  1709. (3<<7), 1,
  1710. "READ FORMAT CAPACITIES");
  1711. if (reply == 0)
  1712. reply = do_read_format_capacities(common, bh);
  1713. break;
  1714. case SC_REQUEST_SENSE:
  1715. common->data_size_from_cmnd = common->cmnd[4];
  1716. reply = check_command(common, 6, DATA_DIR_TO_HOST,
  1717. (1<<4), 0,
  1718. "REQUEST SENSE");
  1719. if (reply == 0)
  1720. reply = do_request_sense(common, bh);
  1721. break;
  1722. case SC_START_STOP_UNIT:
  1723. common->data_size_from_cmnd = 0;
  1724. reply = check_command(common, 6, DATA_DIR_NONE,
  1725. (1<<1) | (1<<4), 0,
  1726. "START-STOP UNIT");
  1727. if (reply == 0)
  1728. reply = do_start_stop(common);
  1729. break;
  1730. case SC_SYNCHRONIZE_CACHE:
  1731. common->data_size_from_cmnd = 0;
  1732. reply = check_command(common, 10, DATA_DIR_NONE,
  1733. (0xf<<2) | (3<<7), 1,
  1734. "SYNCHRONIZE CACHE");
  1735. if (reply == 0)
  1736. reply = do_synchronize_cache(common);
  1737. break;
  1738. case SC_TEST_UNIT_READY:
  1739. common->data_size_from_cmnd = 0;
  1740. reply = check_command(common, 6, DATA_DIR_NONE,
  1741. 0, 1,
  1742. "TEST UNIT READY");
  1743. break;
  1744. /* Although optional, this command is used by MS-Windows. We
  1745. * support a minimal version: BytChk must be 0. */
  1746. case SC_VERIFY:
  1747. common->data_size_from_cmnd = 0;
  1748. reply = check_command(common, 10, DATA_DIR_NONE,
  1749. (1<<1) | (0xf<<2) | (3<<7), 1,
  1750. "VERIFY");
  1751. if (reply == 0)
  1752. reply = do_verify(common);
  1753. break;
  1754. case SC_WRITE_6:
  1755. i = common->cmnd[4];
  1756. common->data_size_from_cmnd = (i == 0 ? 256 : i) << 9;
  1757. reply = check_command(common, 6, DATA_DIR_FROM_HOST,
  1758. (7<<1) | (1<<4), 1,
  1759. "WRITE(6)");
  1760. if (reply == 0)
  1761. reply = do_write(common);
  1762. break;
  1763. case SC_WRITE_10:
  1764. common->data_size_from_cmnd =
  1765. get_unaligned_be16(&common->cmnd[7]) << 9;
  1766. reply = check_command(common, 10, DATA_DIR_FROM_HOST,
  1767. (1<<1) | (0xf<<2) | (3<<7), 1,
  1768. "WRITE(10)");
  1769. if (reply == 0)
  1770. reply = do_write(common);
  1771. break;
  1772. case SC_WRITE_12:
  1773. common->data_size_from_cmnd =
  1774. get_unaligned_be32(&common->cmnd[6]) << 9;
  1775. reply = check_command(common, 12, DATA_DIR_FROM_HOST,
  1776. (1<<1) | (0xf<<2) | (0xf<<6), 1,
  1777. "WRITE(12)");
  1778. if (reply == 0)
  1779. reply = do_write(common);
  1780. break;
  1781. /* Some mandatory commands that we recognize but don't implement.
  1782. * They don't mean much in this setting. It's left as an exercise
  1783. * for anyone interested to implement RESERVE and RELEASE in terms
  1784. * of Posix locks. */
  1785. case SC_FORMAT_UNIT:
  1786. case SC_RELEASE:
  1787. case SC_RESERVE:
  1788. case SC_SEND_DIAGNOSTIC:
  1789. /* Fall through */
  1790. default:
  1791. unknown_cmnd:
  1792. common->data_size_from_cmnd = 0;
  1793. sprintf(unknown, "Unknown x%02x", common->cmnd[0]);
  1794. reply = check_command(common, common->cmnd_size,
  1795. DATA_DIR_UNKNOWN, 0xff, 0, unknown);
  1796. if (reply == 0) {
  1797. curlun->sense_data = SS_INVALID_COMMAND;
  1798. reply = -EINVAL;
  1799. }
  1800. break;
  1801. }
  1802. up_read(&common->filesem);
  1803. if (reply == -EINTR)
  1804. return -EINTR;
  1805. /* Set up the single reply buffer for finish_reply() */
  1806. if (reply == -EINVAL)
  1807. reply = 0; /* Error reply length */
  1808. if (reply >= 0 && common->data_dir == DATA_DIR_TO_HOST) {
  1809. reply = min((u32) reply, common->data_size_from_cmnd);
  1810. bh->inreq->length = reply;
  1811. bh->state = BUF_STATE_FULL;
  1812. common->residue -= reply;
  1813. } /* Otherwise it's already set */
  1814. return 0;
  1815. }
  1816. /*-------------------------------------------------------------------------*/
  1817. static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  1818. {
  1819. struct usb_request *req = bh->outreq;
  1820. struct fsg_bulk_cb_wrap *cbw = req->buf;
  1821. struct fsg_common *common = fsg->common;
  1822. /* Was this a real packet? Should it be ignored? */
  1823. if (req->status || test_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
  1824. return -EINVAL;
  1825. /* Is the CBW valid? */
  1826. if (req->actual != USB_BULK_CB_WRAP_LEN ||
  1827. cbw->Signature != cpu_to_le32(
  1828. USB_BULK_CB_SIG)) {
  1829. DBG(fsg, "invalid CBW: len %u sig 0x%x\n",
  1830. req->actual,
  1831. le32_to_cpu(cbw->Signature));
  1832. /* The Bulk-only spec says we MUST stall the IN endpoint
  1833. * (6.6.1), so it's unavoidable. It also says we must
  1834. * retain this state until the next reset, but there's
  1835. * no way to tell the controller driver it should ignore
  1836. * Clear-Feature(HALT) requests.
  1837. *
  1838. * We aren't required to halt the OUT endpoint; instead
  1839. * we can simply accept and discard any data received
  1840. * until the next reset. */
  1841. wedge_bulk_in_endpoint(fsg);
  1842. set_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
  1843. return -EINVAL;
  1844. }
  1845. /* Is the CBW meaningful? */
  1846. if (cbw->Lun >= FSG_MAX_LUNS || cbw->Flags & ~USB_BULK_IN_FLAG ||
  1847. cbw->Length <= 0 || cbw->Length > MAX_COMMAND_SIZE) {
  1848. DBG(fsg, "non-meaningful CBW: lun = %u, flags = 0x%x, "
  1849. "cmdlen %u\n",
  1850. cbw->Lun, cbw->Flags, cbw->Length);
  1851. /* We can do anything we want here, so let's stall the
  1852. * bulk pipes if we are allowed to. */
  1853. if (common->can_stall) {
  1854. fsg_set_halt(fsg, fsg->bulk_out);
  1855. halt_bulk_in_endpoint(fsg);
  1856. }
  1857. return -EINVAL;
  1858. }
  1859. /* Save the command for later */
  1860. common->cmnd_size = cbw->Length;
  1861. memcpy(common->cmnd, cbw->CDB, common->cmnd_size);
  1862. if (cbw->Flags & USB_BULK_IN_FLAG)
  1863. common->data_dir = DATA_DIR_TO_HOST;
  1864. else
  1865. common->data_dir = DATA_DIR_FROM_HOST;
  1866. common->data_size = le32_to_cpu(cbw->DataTransferLength);
  1867. if (common->data_size == 0)
  1868. common->data_dir = DATA_DIR_NONE;
  1869. common->lun = cbw->Lun;
  1870. common->tag = cbw->Tag;
  1871. return 0;
  1872. }
  1873. static int get_next_command(struct fsg_common *common)
  1874. {
  1875. struct fsg_buffhd *bh;
  1876. int rc = 0;
  1877. /* Wait for the next buffer to become available */
  1878. bh = common->next_buffhd_to_fill;
  1879. while (bh->state != BUF_STATE_EMPTY) {
  1880. rc = sleep_thread(common);
  1881. if (rc)
  1882. return rc;
  1883. }
  1884. /* Queue a request to read a Bulk-only CBW */
  1885. set_bulk_out_req_length(common, bh, USB_BULK_CB_WRAP_LEN);
  1886. bh->outreq->short_not_ok = 1;
  1887. START_TRANSFER_OR(common, bulk_out, bh->outreq,
  1888. &bh->outreq_busy, &bh->state)
  1889. /* Don't know what to do if common->fsg is NULL */
  1890. return -EIO;
  1891. /* We will drain the buffer in software, which means we
  1892. * can reuse it for the next filling. No need to advance
  1893. * next_buffhd_to_fill. */
  1894. /* Wait for the CBW to arrive */
  1895. while (bh->state != BUF_STATE_FULL) {
  1896. rc = sleep_thread(common);
  1897. if (rc)
  1898. return rc;
  1899. }
  1900. rc = fsg_is_set(common) ? received_cbw(common->fsg, bh) : -EIO;
  1901. bh->state = BUF_STATE_EMPTY;
  1902. return rc;
  1903. }
  1904. /*-------------------------------------------------------------------------*/
  1905. static int enable_endpoint(struct fsg_common *common, struct usb_ep *ep,
  1906. const struct usb_endpoint_descriptor *d)
  1907. {
  1908. int rc;
  1909. ep->driver_data = common;
  1910. rc = usb_ep_enable(ep, d);
  1911. if (rc)
  1912. ERROR(common, "can't enable %s, result %d\n", ep->name, rc);
  1913. return rc;
  1914. }
  1915. static int alloc_request(struct fsg_common *common, struct usb_ep *ep,
  1916. struct usb_request **preq)
  1917. {
  1918. *preq = usb_ep_alloc_request(ep, GFP_ATOMIC);
  1919. if (*preq)
  1920. return 0;
  1921. ERROR(common, "can't allocate request for %s\n", ep->name);
  1922. return -ENOMEM;
  1923. }
  1924. /* Reset interface setting and re-init endpoint state (toggle etc). */
  1925. static int do_set_interface(struct fsg_common *common, struct fsg_dev *new_fsg)
  1926. {
  1927. const struct usb_endpoint_descriptor *d;
  1928. struct fsg_dev *fsg;
  1929. int i, rc = 0;
  1930. if (common->running)
  1931. DBG(common, "reset interface\n");
  1932. reset:
  1933. /* Deallocate the requests */
  1934. if (common->fsg) {
  1935. fsg = common->fsg;
  1936. for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
  1937. struct fsg_buffhd *bh = &common->buffhds[i];
  1938. if (bh->inreq) {
  1939. usb_ep_free_request(fsg->bulk_in, bh->inreq);
  1940. bh->inreq = NULL;
  1941. }
  1942. if (bh->outreq) {
  1943. usb_ep_free_request(fsg->bulk_out, bh->outreq);
  1944. bh->outreq = NULL;
  1945. }
  1946. }
  1947. /* Disable the endpoints */
  1948. if (fsg->bulk_in_enabled) {
  1949. usb_ep_disable(fsg->bulk_in);
  1950. fsg->bulk_in_enabled = 0;
  1951. }
  1952. if (fsg->bulk_out_enabled) {
  1953. usb_ep_disable(fsg->bulk_out);
  1954. fsg->bulk_out_enabled = 0;
  1955. }
  1956. common->fsg = NULL;
  1957. /* wake_up(&common->fsg_wait); */
  1958. }
  1959. common->running = 0;
  1960. if (!new_fsg || rc)
  1961. return rc;
  1962. common->fsg = new_fsg;
  1963. fsg = common->fsg;
  1964. /* Enable the endpoints */
  1965. d = fsg_ep_desc(common->gadget,
  1966. &fsg_fs_bulk_in_desc, &fsg_hs_bulk_in_desc);
  1967. rc = enable_endpoint(common, fsg->bulk_in, d);
  1968. if (rc)
  1969. goto reset;
  1970. fsg->bulk_in_enabled = 1;
  1971. d = fsg_ep_desc(common->gadget,
  1972. &fsg_fs_bulk_out_desc, &fsg_hs_bulk_out_desc);
  1973. rc = enable_endpoint(common, fsg->bulk_out, d);
  1974. if (rc)
  1975. goto reset;
  1976. fsg->bulk_out_enabled = 1;
  1977. common->bulk_out_maxpacket =
  1978. le16_to_cpu(get_unaligned(&d->wMaxPacketSize));
  1979. clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
  1980. /* Allocate the requests */
  1981. for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
  1982. struct fsg_buffhd *bh = &common->buffhds[i];
  1983. rc = alloc_request(common, fsg->bulk_in, &bh->inreq);
  1984. if (rc)
  1985. goto reset;
  1986. rc = alloc_request(common, fsg->bulk_out, &bh->outreq);
  1987. if (rc)
  1988. goto reset;
  1989. bh->inreq->buf = bh->outreq->buf = bh->buf;
  1990. bh->inreq->context = bh->outreq->context = bh;
  1991. bh->inreq->complete = bulk_in_complete;
  1992. bh->outreq->complete = bulk_out_complete;
  1993. }
  1994. common->running = 1;
  1995. return rc;
  1996. }
  1997. /****************************** ALT CONFIGS ******************************/
  1998. static int fsg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  1999. {
  2000. struct fsg_dev *fsg = fsg_from_func(f);
  2001. fsg->common->new_fsg = fsg;
  2002. raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE);
  2003. return 0;
  2004. }
  2005. static void fsg_disable(struct usb_function *f)
  2006. {
  2007. struct fsg_dev *fsg = fsg_from_func(f);
  2008. fsg->common->new_fsg = NULL;
  2009. raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE);
  2010. }
  2011. /*-------------------------------------------------------------------------*/
  2012. static void handle_exception(struct fsg_common *common)
  2013. {
  2014. int i;
  2015. struct fsg_buffhd *bh;
  2016. enum fsg_state old_state;
  2017. struct fsg_lun *curlun;
  2018. unsigned int exception_req_tag;
  2019. /* Cancel all the pending transfers */
  2020. if (common->fsg) {
  2021. for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
  2022. bh = &common->buffhds[i];
  2023. if (bh->inreq_busy)
  2024. usb_ep_dequeue(common->fsg->bulk_in, bh->inreq);
  2025. if (bh->outreq_busy)
  2026. usb_ep_dequeue(common->fsg->bulk_out,
  2027. bh->outreq);
  2028. }
  2029. /* Wait until everything is idle */
  2030. for (;;) {
  2031. int num_active = 0;
  2032. for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
  2033. bh = &common->buffhds[i];
  2034. num_active += bh->inreq_busy + bh->outreq_busy;
  2035. }
  2036. if (num_active == 0)
  2037. break;
  2038. if (sleep_thread(common))
  2039. return;
  2040. }
  2041. /* Clear out the controller's fifos */
  2042. if (common->fsg->bulk_in_enabled)
  2043. usb_ep_fifo_flush(common->fsg->bulk_in);
  2044. if (common->fsg->bulk_out_enabled)
  2045. usb_ep_fifo_flush(common->fsg->bulk_out);
  2046. }
  2047. /* Reset the I/O buffer states and pointers, the SCSI
  2048. * state, and the exception. Then invoke the handler. */
  2049. for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
  2050. bh = &common->buffhds[i];
  2051. bh->state = BUF_STATE_EMPTY;
  2052. }
  2053. common->next_buffhd_to_fill = &common->buffhds[0];
  2054. common->next_buffhd_to_drain = &common->buffhds[0];
  2055. exception_req_tag = common->exception_req_tag;
  2056. old_state = common->state;
  2057. if (old_state == FSG_STATE_ABORT_BULK_OUT)
  2058. common->state = FSG_STATE_STATUS_PHASE;
  2059. else {
  2060. for (i = 0; i < common->nluns; ++i) {
  2061. curlun = &common->luns[i];
  2062. curlun->sense_data = SS_NO_SENSE;
  2063. curlun->info_valid = 0;
  2064. }
  2065. common->state = FSG_STATE_IDLE;
  2066. }
  2067. /* Carry out any extra actions required for the exception */
  2068. switch (old_state) {
  2069. case FSG_STATE_ABORT_BULK_OUT:
  2070. send_status(common);
  2071. if (common->state == FSG_STATE_STATUS_PHASE)
  2072. common->state = FSG_STATE_IDLE;
  2073. break;
  2074. case FSG_STATE_RESET:
  2075. /* In case we were forced against our will to halt a
  2076. * bulk endpoint, clear the halt now. (The SuperH UDC
  2077. * requires this.) */
  2078. if (!fsg_is_set(common))
  2079. break;
  2080. if (test_and_clear_bit(IGNORE_BULK_OUT,
  2081. &common->fsg->atomic_bitflags))
  2082. usb_ep_clear_halt(common->fsg->bulk_in);
  2083. if (common->ep0_req_tag == exception_req_tag)
  2084. ep0_queue(common); /* Complete the status stage */
  2085. break;
  2086. case FSG_STATE_CONFIG_CHANGE:
  2087. do_set_interface(common, common->new_fsg);
  2088. break;
  2089. case FSG_STATE_EXIT:
  2090. case FSG_STATE_TERMINATED:
  2091. do_set_interface(common, NULL); /* Free resources */
  2092. common->state = FSG_STATE_TERMINATED; /* Stop the thread */
  2093. break;
  2094. case FSG_STATE_INTERFACE_CHANGE:
  2095. case FSG_STATE_DISCONNECT:
  2096. case FSG_STATE_COMMAND_PHASE:
  2097. case FSG_STATE_DATA_PHASE:
  2098. case FSG_STATE_STATUS_PHASE:
  2099. case FSG_STATE_IDLE:
  2100. break;
  2101. }
  2102. }
  2103. /*-------------------------------------------------------------------------*/
  2104. int fsg_main_thread(void *common_)
  2105. {
  2106. struct fsg_common *common = the_fsg_common;
  2107. /* The main loop */
  2108. do {
  2109. if (exception_in_progress(common)) {
  2110. handle_exception(common);
  2111. continue;
  2112. }
  2113. if (!common->running) {
  2114. sleep_thread(common);
  2115. continue;
  2116. }
  2117. if (get_next_command(common))
  2118. continue;
  2119. if (!exception_in_progress(common))
  2120. common->state = FSG_STATE_DATA_PHASE;
  2121. if (do_scsi_command(common) || finish_reply(common))
  2122. continue;
  2123. if (!exception_in_progress(common))
  2124. common->state = FSG_STATE_STATUS_PHASE;
  2125. if (send_status(common))
  2126. continue;
  2127. if (!exception_in_progress(common))
  2128. common->state = FSG_STATE_IDLE;
  2129. } while (0);
  2130. common->thread_task = NULL;
  2131. return 0;
  2132. }
  2133. static void fsg_common_release(struct kref *ref);
  2134. static struct fsg_common *fsg_common_init(struct fsg_common *common,
  2135. struct usb_composite_dev *cdev)
  2136. {
  2137. struct usb_gadget *gadget = cdev->gadget;
  2138. struct fsg_buffhd *bh;
  2139. struct fsg_lun *curlun;
  2140. int nluns, i, rc;
  2141. /* Find out how many LUNs there should be */
  2142. nluns = 1;
  2143. if (nluns < 1 || nluns > FSG_MAX_LUNS) {
  2144. printf("invalid number of LUNs: %u\n", nluns);
  2145. return ERR_PTR(-EINVAL);
  2146. }
  2147. /* Allocate? */
  2148. if (!common) {
  2149. common = calloc(sizeof *common, 1);
  2150. if (!common)
  2151. return ERR_PTR(-ENOMEM);
  2152. common->free_storage_on_release = 1;
  2153. } else {
  2154. memset(common, 0, sizeof common);
  2155. common->free_storage_on_release = 0;
  2156. }
  2157. common->ops = NULL;
  2158. common->private_data = NULL;
  2159. common->gadget = gadget;
  2160. common->ep0 = gadget->ep0;
  2161. common->ep0req = cdev->req;
  2162. /* Maybe allocate device-global string IDs, and patch descriptors */
  2163. if (fsg_strings[FSG_STRING_INTERFACE].id == 0) {
  2164. rc = usb_string_id(cdev);
  2165. if (unlikely(rc < 0))
  2166. goto error_release;
  2167. fsg_strings[FSG_STRING_INTERFACE].id = rc;
  2168. fsg_intf_desc.iInterface = rc;
  2169. }
  2170. /* Create the LUNs, open their backing files, and register the
  2171. * LUN devices in sysfs. */
  2172. curlun = calloc(nluns, sizeof *curlun);
  2173. if (!curlun) {
  2174. rc = -ENOMEM;
  2175. goto error_release;
  2176. }
  2177. common->nluns = nluns;
  2178. for (i = 0; i < nluns; i++) {
  2179. common->luns[i].removable = 1;
  2180. #ifndef CONFIG_FSL_UTP
  2181. rc = fsg_lun_open(&common->luns[i], "");
  2182. if (rc)
  2183. goto error_luns;
  2184. #endif
  2185. }
  2186. common->lun = 0;
  2187. /* Data buffers cyclic list */
  2188. bh = common->buffhds;
  2189. i = FSG_NUM_BUFFERS;
  2190. goto buffhds_first_it;
  2191. do {
  2192. bh->next = bh + 1;
  2193. ++bh;
  2194. buffhds_first_it:
  2195. bh->inreq_busy = 0;
  2196. bh->outreq_busy = 0;
  2197. bh->buf = kmalloc(FSG_BUFLEN, GFP_KERNEL);
  2198. if (unlikely(!bh->buf)) {
  2199. rc = -ENOMEM;
  2200. goto error_release;
  2201. }
  2202. } while (--i);
  2203. bh->next = common->buffhds;
  2204. snprintf(common->inquiry_string, sizeof common->inquiry_string,
  2205. "%-8s%-16s%04x",
  2206. "Linux ",
  2207. "File-Store Gadget",
  2208. 0xffff);
  2209. /* Some peripheral controllers are known not to be able to
  2210. * halt bulk endpoints correctly. If one of them is present,
  2211. * disable stalls.
  2212. */
  2213. /* Tell the thread to start working */
  2214. common->thread_task =
  2215. kthread_create(fsg_main_thread, common,
  2216. OR(cfg->thread_name, "file-storage"));
  2217. if (IS_ERR(common->thread_task)) {
  2218. rc = PTR_ERR(common->thread_task);
  2219. goto error_release;
  2220. }
  2221. #undef OR
  2222. /* Information */
  2223. INFO(common, FSG_DRIVER_DESC ", version: " FSG_DRIVER_VERSION "\n");
  2224. INFO(common, "Number of LUNs=%d\n", common->nluns);
  2225. return common;
  2226. error_luns:
  2227. common->nluns = i + 1;
  2228. error_release:
  2229. common->state = FSG_STATE_TERMINATED; /* The thread is dead */
  2230. /* Call fsg_common_release() directly, ref might be not
  2231. * initialised */
  2232. fsg_common_release(&common->ref);
  2233. return ERR_PTR(rc);
  2234. }
  2235. static void fsg_common_release(struct kref *ref)
  2236. {
  2237. struct fsg_common *common = container_of(ref, struct fsg_common, ref);
  2238. /* If the thread isn't already dead, tell it to exit now */
  2239. if (common->state != FSG_STATE_TERMINATED) {
  2240. raise_exception(common, FSG_STATE_EXIT);
  2241. wait_for_completion(&common->thread_notifier);
  2242. }
  2243. if (likely(common->luns)) {
  2244. struct fsg_lun *lun = common->luns;
  2245. unsigned i = common->nluns;
  2246. /* In error recovery common->nluns may be zero. */
  2247. for (; i; --i, ++lun)
  2248. fsg_lun_close(lun);
  2249. kfree(common->luns);
  2250. }
  2251. {
  2252. struct fsg_buffhd *bh = common->buffhds;
  2253. unsigned i = FSG_NUM_BUFFERS;
  2254. do {
  2255. kfree(bh->buf);
  2256. } while (++bh, --i);
  2257. }
  2258. if (common->free_storage_on_release)
  2259. kfree(common);
  2260. }
  2261. /*-------------------------------------------------------------------------*/
  2262. /**
  2263. * usb_copy_descriptors - copy a vector of USB descriptors
  2264. * @src: null-terminated vector to copy
  2265. * Context: initialization code, which may sleep
  2266. *
  2267. * This makes a copy of a vector of USB descriptors. Its primary use
  2268. * is to support usb_function objects which can have multiple copies,
  2269. * each needing different descriptors. Functions may have static
  2270. * tables of descriptors, which are used as templates and customized
  2271. * with identifiers (for interfaces, strings, endpoints, and more)
  2272. * as needed by a given function instance.
  2273. */
  2274. struct usb_descriptor_header **
  2275. usb_copy_descriptors(struct usb_descriptor_header **src)
  2276. {
  2277. struct usb_descriptor_header **tmp;
  2278. unsigned bytes;
  2279. unsigned n_desc;
  2280. void *mem;
  2281. struct usb_descriptor_header **ret;
  2282. /* count descriptors and their sizes; then add vector size */
  2283. for (bytes = 0, n_desc = 0, tmp = src; *tmp; tmp++, n_desc++)
  2284. bytes += (*tmp)->bLength;
  2285. bytes += (n_desc + 1) * sizeof(*tmp);
  2286. mem = kmalloc(bytes, GFP_KERNEL);
  2287. if (!mem)
  2288. return NULL;
  2289. /* fill in pointers starting at "tmp",
  2290. * to descriptors copied starting at "mem";
  2291. * and return "ret"
  2292. */
  2293. tmp = mem;
  2294. ret = mem;
  2295. mem += (n_desc + 1) * sizeof(*tmp);
  2296. while (*src) {
  2297. memcpy(mem, *src, (*src)->bLength);
  2298. *tmp = mem;
  2299. tmp++;
  2300. mem += (*src)->bLength;
  2301. src++;
  2302. }
  2303. *tmp = NULL;
  2304. return ret;
  2305. }
  2306. static void fsg_unbind(struct usb_configuration *c, struct usb_function *f)
  2307. {
  2308. struct fsg_dev *fsg = fsg_from_func(f);
  2309. DBG(fsg, "unbind\n");
  2310. if (fsg->common->fsg == fsg) {
  2311. fsg->common->new_fsg = NULL;
  2312. raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE);
  2313. }
  2314. #ifdef CONFIG_FSL_UTP
  2315. utp_exit(fsg);
  2316. #endif
  2317. free(fsg->function.descriptors);
  2318. free(fsg->function.hs_descriptors);
  2319. kfree(fsg);
  2320. }
  2321. #ifdef CONFIG_FSL_UTP
  2322. #include "fsl_updater.c"
  2323. #endif
  2324. static int fsg_bind(struct usb_configuration *c, struct usb_function *f)
  2325. {
  2326. struct fsg_dev *fsg = fsg_from_func(f);
  2327. struct usb_gadget *gadget = c->cdev->gadget;
  2328. int i;
  2329. struct usb_ep *ep;
  2330. fsg->gadget = gadget;
  2331. /* New interface */
  2332. i = usb_interface_id(c, f);
  2333. if (i < 0)
  2334. return i;
  2335. fsg_intf_desc.bInterfaceNumber = i;
  2336. fsg->interface_number = i;
  2337. #ifdef CONFIG_FSL_UTP
  2338. utp_init(fsg);
  2339. #endif
  2340. /* Find all the endpoints we will use */
  2341. ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_in_desc);
  2342. if (!ep)
  2343. goto autoconf_fail;
  2344. ep->driver_data = fsg->common; /* claim the endpoint */
  2345. fsg->bulk_in = ep;
  2346. ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_out_desc);
  2347. if (!ep)
  2348. goto autoconf_fail;
  2349. ep->driver_data = fsg->common; /* claim the endpoint */
  2350. fsg->bulk_out = ep;
  2351. /* Copy descriptors */
  2352. f->descriptors = usb_copy_descriptors(fsg_fs_function);
  2353. if (unlikely(!f->descriptors))
  2354. return -ENOMEM;
  2355. if (gadget_is_dualspeed(gadget)) {
  2356. /* Assume endpoint addresses are the same for both speeds */
  2357. fsg_hs_bulk_in_desc.bEndpointAddress =
  2358. fsg_fs_bulk_in_desc.bEndpointAddress;
  2359. fsg_hs_bulk_out_desc.bEndpointAddress =
  2360. fsg_fs_bulk_out_desc.bEndpointAddress;
  2361. f->hs_descriptors = usb_copy_descriptors(fsg_hs_function);
  2362. if (unlikely(!f->hs_descriptors)) {
  2363. free(f->descriptors);
  2364. return -ENOMEM;
  2365. }
  2366. }
  2367. return 0;
  2368. autoconf_fail:
  2369. ERROR(fsg, "unable to autoconfigure all endpoints\n");
  2370. return -ENOTSUPP;
  2371. }
  2372. /****************************** ADD FUNCTION ******************************/
  2373. static struct usb_gadget_strings *fsg_strings_array[] = {
  2374. &fsg_stringtab,
  2375. NULL,
  2376. };
  2377. static int fsg_bind_config(struct usb_composite_dev *cdev,
  2378. struct usb_configuration *c,
  2379. struct fsg_common *common)
  2380. {
  2381. struct fsg_dev *fsg;
  2382. int rc;
  2383. fsg = calloc(1, sizeof *fsg);
  2384. if (!fsg)
  2385. return -ENOMEM;
  2386. fsg->function.name = FSG_DRIVER_DESC;
  2387. fsg->function.strings = fsg_strings_array;
  2388. fsg->function.bind = fsg_bind;
  2389. fsg->function.unbind = fsg_unbind;
  2390. fsg->function.setup = fsg_setup;
  2391. fsg->function.set_alt = fsg_set_alt;
  2392. fsg->function.disable = fsg_disable;
  2393. fsg->common = common;
  2394. common->fsg = fsg;
  2395. /* Our caller holds a reference to common structure so we
  2396. * don't have to be worry about it being freed until we return
  2397. * from this function. So instead of incrementing counter now
  2398. * and decrement in error recovery we increment it only when
  2399. * call to usb_add_function() was successful. */
  2400. rc = usb_add_function(c, &fsg->function);
  2401. if (rc)
  2402. kfree(fsg);
  2403. return rc;
  2404. }
  2405. int fsg_add(struct usb_configuration *c)
  2406. {
  2407. struct fsg_common *fsg_common;
  2408. fsg_common = fsg_common_init(NULL, c->cdev);
  2409. fsg_common->vendor_name = 0;
  2410. fsg_common->product_name = 0;
  2411. fsg_common->release = 0xffff;
  2412. fsg_common->ops = NULL;
  2413. fsg_common->private_data = NULL;
  2414. the_fsg_common = fsg_common;
  2415. return fsg_bind_config(c->cdev, c, fsg_common);
  2416. }
  2417. int fsg_init(struct ums_board_info *ums)
  2418. {
  2419. ums_info = ums;
  2420. return 0;
  2421. }