transport.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410
  1. /* Driver for USB Mass Storage compliant devices
  2. *
  3. * Current development and maintenance by:
  4. * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
  5. *
  6. * Developed with the assistance of:
  7. * (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org)
  8. * (c) 2000 Stephen J. Gowdy (SGowdy@lbl.gov)
  9. * (c) 2002 Alan Stern <stern@rowland.org>
  10. *
  11. * Initial work by:
  12. * (c) 1999 Michael Gee (michael@linuxspecific.com)
  13. *
  14. * This driver is based on the 'USB Mass Storage Class' document. This
  15. * describes in detail the protocol used to communicate with such
  16. * devices. Clearly, the designers had SCSI and ATAPI commands in
  17. * mind when they created this document. The commands are all very
  18. * similar to commands in the SCSI-II and ATAPI specifications.
  19. *
  20. * It is important to note that in a number of cases this class
  21. * exhibits class-specific exemptions from the USB specification.
  22. * Notably the usage of NAK, STALL and ACK differs from the norm, in
  23. * that they are used to communicate wait, failed and OK on commands.
  24. *
  25. * Also, for certain devices, the interrupt endpoint is used to convey
  26. * status of a command.
  27. *
  28. * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
  29. * information about this driver.
  30. *
  31. * This program is free software; you can redistribute it and/or modify it
  32. * under the terms of the GNU General Public License as published by the
  33. * Free Software Foundation; either version 2, or (at your option) any
  34. * later version.
  35. *
  36. * This program is distributed in the hope that it will be useful, but
  37. * WITHOUT ANY WARRANTY; without even the implied warranty of
  38. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  39. * General Public License for more details.
  40. *
  41. * You should have received a copy of the GNU General Public License along
  42. * with this program; if not, write to the Free Software Foundation, Inc.,
  43. * 675 Mass Ave, Cambridge, MA 02139, USA.
  44. */
  45. #include <linux/sched.h>
  46. #include <linux/gfp.h>
  47. #include <linux/errno.h>
  48. #include <linux/export.h>
  49. #include <linux/usb/quirks.h>
  50. #include <scsi/scsi.h>
  51. #include <scsi/scsi_eh.h>
  52. #include <scsi/scsi_device.h>
  53. #include "usb.h"
  54. #include "transport.h"
  55. #include "protocol.h"
  56. #include "scsiglue.h"
  57. #include "debug.h"
  58. #include <linux/blkdev.h>
  59. #include "../../scsi/sd.h"
  60. /***********************************************************************
  61. * Data transfer routines
  62. ***********************************************************************/
  63. /*
  64. * This is subtle, so pay attention:
  65. * ---------------------------------
  66. * We're very concerned about races with a command abort. Hanging this code
  67. * is a sure fire way to hang the kernel. (Note that this discussion applies
  68. * only to transactions resulting from a scsi queued-command, since only
  69. * these transactions are subject to a scsi abort. Other transactions, such
  70. * as those occurring during device-specific initialization, must be handled
  71. * by a separate code path.)
  72. *
  73. * The abort function (usb_storage_command_abort() in scsiglue.c) first
  74. * sets the machine state and the ABORTING bit in us->dflags to prevent
  75. * new URBs from being submitted. It then calls usb_stor_stop_transport()
  76. * below, which atomically tests-and-clears the URB_ACTIVE bit in us->dflags
  77. * to see if the current_urb needs to be stopped. Likewise, the SG_ACTIVE
  78. * bit is tested to see if the current_sg scatter-gather request needs to be
  79. * stopped. The timeout callback routine does much the same thing.
  80. *
  81. * When a disconnect occurs, the DISCONNECTING bit in us->dflags is set to
  82. * prevent new URBs from being submitted, and usb_stor_stop_transport() is
  83. * called to stop any ongoing requests.
  84. *
  85. * The submit function first verifies that the submitting is allowed
  86. * (neither ABORTING nor DISCONNECTING bits are set) and that the submit
  87. * completes without errors, and only then sets the URB_ACTIVE bit. This
  88. * prevents the stop_transport() function from trying to cancel the URB
  89. * while the submit call is underway. Next, the submit function must test
  90. * the flags to see if an abort or disconnect occurred during the submission
  91. * or before the URB_ACTIVE bit was set. If so, it's essential to cancel
  92. * the URB if it hasn't been cancelled already (i.e., if the URB_ACTIVE bit
  93. * is still set). Either way, the function must then wait for the URB to
  94. * finish. Note that the URB can still be in progress even after a call to
  95. * usb_unlink_urb() returns.
  96. *
  97. * The idea is that (1) once the ABORTING or DISCONNECTING bit is set,
  98. * either the stop_transport() function or the submitting function
  99. * is guaranteed to call usb_unlink_urb() for an active URB,
  100. * and (2) test_and_clear_bit() prevents usb_unlink_urb() from being
  101. * called more than once or from being called during usb_submit_urb().
  102. */
  103. /* This is the completion handler which will wake us up when an URB
  104. * completes.
  105. */
  106. static void usb_stor_blocking_completion(struct urb *urb)
  107. {
  108. struct completion *urb_done_ptr = urb->context;
  109. complete(urb_done_ptr);
  110. }
  111. /* This is the common part of the URB message submission code
  112. *
  113. * All URBs from the usb-storage driver involved in handling a queued scsi
  114. * command _must_ pass through this function (or something like it) for the
  115. * abort mechanisms to work properly.
  116. */
  117. static int usb_stor_msg_common(struct us_data *us, int timeout)
  118. {
  119. struct completion urb_done;
  120. long timeleft;
  121. int status;
  122. /* don't submit URBs during abort processing */
  123. if (test_bit(US_FLIDX_ABORTING, &us->dflags))
  124. return -EIO;
  125. /* set up data structures for the wakeup system */
  126. init_completion(&urb_done);
  127. /* fill the common fields in the URB */
  128. us->current_urb->context = &urb_done;
  129. us->current_urb->transfer_flags = 0;
  130. /* we assume that if transfer_buffer isn't us->iobuf then it
  131. * hasn't been mapped for DMA. Yes, this is clunky, but it's
  132. * easier than always having the caller tell us whether the
  133. * transfer buffer has already been mapped. */
  134. if (us->current_urb->transfer_buffer == us->iobuf)
  135. us->current_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  136. us->current_urb->transfer_dma = us->iobuf_dma;
  137. /* submit the URB */
  138. status = usb_submit_urb(us->current_urb, GFP_NOIO);
  139. if (status) {
  140. /* something went wrong */
  141. return status;
  142. }
  143. /* since the URB has been submitted successfully, it's now okay
  144. * to cancel it */
  145. set_bit(US_FLIDX_URB_ACTIVE, &us->dflags);
  146. /* did an abort occur during the submission? */
  147. if (test_bit(US_FLIDX_ABORTING, &us->dflags)) {
  148. /* cancel the URB, if it hasn't been cancelled already */
  149. if (test_and_clear_bit(US_FLIDX_URB_ACTIVE, &us->dflags)) {
  150. US_DEBUGP("-- cancelling URB\n");
  151. usb_unlink_urb(us->current_urb);
  152. }
  153. }
  154. /* wait for the completion of the URB */
  155. timeleft = wait_for_completion_interruptible_timeout(
  156. &urb_done, timeout ? : MAX_SCHEDULE_TIMEOUT);
  157. clear_bit(US_FLIDX_URB_ACTIVE, &us->dflags);
  158. if (timeleft <= 0) {
  159. US_DEBUGP("%s -- cancelling URB\n",
  160. timeleft == 0 ? "Timeout" : "Signal");
  161. usb_kill_urb(us->current_urb);
  162. }
  163. /* return the URB status */
  164. return us->current_urb->status;
  165. }
  166. /*
  167. * Transfer one control message, with timeouts, and allowing early
  168. * termination. Return codes are usual -Exxx, *not* USB_STOR_XFER_xxx.
  169. */
  170. int usb_stor_control_msg(struct us_data *us, unsigned int pipe,
  171. u8 request, u8 requesttype, u16 value, u16 index,
  172. void *data, u16 size, int timeout)
  173. {
  174. int status;
  175. US_DEBUGP("%s: rq=%02x rqtype=%02x value=%04x index=%02x len=%u\n",
  176. __func__, request, requesttype,
  177. value, index, size);
  178. /* fill in the devrequest structure */
  179. us->cr->bRequestType = requesttype;
  180. us->cr->bRequest = request;
  181. us->cr->wValue = cpu_to_le16(value);
  182. us->cr->wIndex = cpu_to_le16(index);
  183. us->cr->wLength = cpu_to_le16(size);
  184. /* fill and submit the URB */
  185. usb_fill_control_urb(us->current_urb, us->pusb_dev, pipe,
  186. (unsigned char*) us->cr, data, size,
  187. usb_stor_blocking_completion, NULL);
  188. status = usb_stor_msg_common(us, timeout);
  189. /* return the actual length of the data transferred if no error */
  190. if (status == 0)
  191. status = us->current_urb->actual_length;
  192. return status;
  193. }
  194. EXPORT_SYMBOL_GPL(usb_stor_control_msg);
  195. /* This is a version of usb_clear_halt() that allows early termination and
  196. * doesn't read the status from the device -- this is because some devices
  197. * crash their internal firmware when the status is requested after a halt.
  198. *
  199. * A definitive list of these 'bad' devices is too difficult to maintain or
  200. * make complete enough to be useful. This problem was first observed on the
  201. * Hagiwara FlashGate DUAL unit. However, bus traces reveal that neither
  202. * MacOS nor Windows checks the status after clearing a halt.
  203. *
  204. * Since many vendors in this space limit their testing to interoperability
  205. * with these two OSes, specification violations like this one are common.
  206. */
  207. int usb_stor_clear_halt(struct us_data *us, unsigned int pipe)
  208. {
  209. int result;
  210. int endp = usb_pipeendpoint(pipe);
  211. if (usb_pipein (pipe))
  212. endp |= USB_DIR_IN;
  213. result = usb_stor_control_msg(us, us->send_ctrl_pipe,
  214. USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
  215. USB_ENDPOINT_HALT, endp,
  216. NULL, 0, 3*HZ);
  217. if (result >= 0)
  218. usb_reset_endpoint(us->pusb_dev, endp);
  219. US_DEBUGP("%s: result = %d\n", __func__, result);
  220. return result;
  221. }
  222. EXPORT_SYMBOL_GPL(usb_stor_clear_halt);
  223. /*
  224. * Interpret the results of a URB transfer
  225. *
  226. * This function prints appropriate debugging messages, clears halts on
  227. * non-control endpoints, and translates the status to the corresponding
  228. * USB_STOR_XFER_xxx return code.
  229. */
  230. static int interpret_urb_result(struct us_data *us, unsigned int pipe,
  231. unsigned int length, int result, unsigned int partial)
  232. {
  233. US_DEBUGP("Status code %d; transferred %u/%u\n",
  234. result, partial, length);
  235. switch (result) {
  236. /* no error code; did we send all the data? */
  237. case 0:
  238. if (partial != length) {
  239. US_DEBUGP("-- short transfer\n");
  240. return USB_STOR_XFER_SHORT;
  241. }
  242. US_DEBUGP("-- transfer complete\n");
  243. return USB_STOR_XFER_GOOD;
  244. /* stalled */
  245. case -EPIPE:
  246. /* for control endpoints, (used by CB[I]) a stall indicates
  247. * a failed command */
  248. if (usb_pipecontrol(pipe)) {
  249. US_DEBUGP("-- stall on control pipe\n");
  250. #ifdef CONFIG_USB_DEBUG_DETEAILED_LOG
  251. printk(KERN_ERR "usb storage -- stall on control pipe\n");
  252. #endif
  253. return USB_STOR_XFER_STALLED;
  254. }
  255. /* for other sorts of endpoint, clear the stall */
  256. US_DEBUGP("clearing endpoint halt for pipe 0x%x\n", pipe);
  257. if (usb_stor_clear_halt(us, pipe) < 0)
  258. return USB_STOR_XFER_ERROR;
  259. return USB_STOR_XFER_STALLED;
  260. /* babble - the device tried to send more than we wanted to read */
  261. case -EOVERFLOW:
  262. US_DEBUGP("-- babble\n");
  263. #ifdef CONFIG_USB_DEBUG_DETEAILED_LOG
  264. printk(KERN_ERR "usb storage -- babble\n");
  265. #endif
  266. return USB_STOR_XFER_LONG;
  267. /* the transfer was cancelled by abort, disconnect, or timeout */
  268. case -ECONNRESET:
  269. US_DEBUGP("-- transfer cancelled\n");
  270. #ifdef CONFIG_USB_DEBUG_DETEAILED_LOG
  271. printk(KERN_ERR "usb storage -- transfer cancelled\n");
  272. #endif
  273. return USB_STOR_XFER_ERROR;
  274. /* short scatter-gather read transfer */
  275. case -EREMOTEIO:
  276. US_DEBUGP("-- short read transfer\n");
  277. #ifdef CONFIG_USB_DEBUG_DETEAILED_LOG
  278. printk(KERN_ERR "usb storage -- short read transfer\n");
  279. #endif
  280. return USB_STOR_XFER_SHORT;
  281. /* abort or disconnect in progress */
  282. case -EIO:
  283. US_DEBUGP("-- abort or disconnect in progress\n");
  284. #ifdef CONFIG_USB_DEBUG_DETEAILED_LOG
  285. printk(KERN_ERR "usb storage -- abort or disconnect in progress\n");
  286. #endif
  287. return USB_STOR_XFER_ERROR;
  288. /* the catch-all error case */
  289. default:
  290. US_DEBUGP("-- unknown error\n");
  291. #ifdef CONFIG_USB_DEBUG_DETEAILED_LOG
  292. printk(KERN_ERR "usb storage -- unknown error %d\n", result);
  293. #endif
  294. return USB_STOR_XFER_ERROR;
  295. }
  296. }
  297. /*
  298. * Transfer one control message, without timeouts, but allowing early
  299. * termination. Return codes are USB_STOR_XFER_xxx.
  300. */
  301. int usb_stor_ctrl_transfer(struct us_data *us, unsigned int pipe,
  302. u8 request, u8 requesttype, u16 value, u16 index,
  303. void *data, u16 size)
  304. {
  305. int result;
  306. US_DEBUGP("%s: rq=%02x rqtype=%02x value=%04x index=%02x len=%u\n",
  307. __func__, request, requesttype,
  308. value, index, size);
  309. /* fill in the devrequest structure */
  310. us->cr->bRequestType = requesttype;
  311. us->cr->bRequest = request;
  312. us->cr->wValue = cpu_to_le16(value);
  313. us->cr->wIndex = cpu_to_le16(index);
  314. us->cr->wLength = cpu_to_le16(size);
  315. /* fill and submit the URB */
  316. usb_fill_control_urb(us->current_urb, us->pusb_dev, pipe,
  317. (unsigned char*) us->cr, data, size,
  318. usb_stor_blocking_completion, NULL);
  319. result = usb_stor_msg_common(us, 0);
  320. return interpret_urb_result(us, pipe, size, result,
  321. us->current_urb->actual_length);
  322. }
  323. EXPORT_SYMBOL_GPL(usb_stor_ctrl_transfer);
  324. /*
  325. * Receive one interrupt buffer, without timeouts, but allowing early
  326. * termination. Return codes are USB_STOR_XFER_xxx.
  327. *
  328. * This routine always uses us->recv_intr_pipe as the pipe and
  329. * us->ep_bInterval as the interrupt interval.
  330. */
  331. static int usb_stor_intr_transfer(struct us_data *us, void *buf,
  332. unsigned int length)
  333. {
  334. int result;
  335. unsigned int pipe = us->recv_intr_pipe;
  336. unsigned int maxp;
  337. US_DEBUGP("%s: xfer %u bytes\n", __func__, length);
  338. /* calculate the max packet size */
  339. maxp = usb_maxpacket(us->pusb_dev, pipe, usb_pipeout(pipe));
  340. if (maxp > length)
  341. maxp = length;
  342. /* fill and submit the URB */
  343. usb_fill_int_urb(us->current_urb, us->pusb_dev, pipe, buf,
  344. maxp, usb_stor_blocking_completion, NULL,
  345. us->ep_bInterval);
  346. result = usb_stor_msg_common(us, 0);
  347. return interpret_urb_result(us, pipe, length, result,
  348. us->current_urb->actual_length);
  349. }
  350. /*
  351. * Transfer one buffer via bulk pipe, without timeouts, but allowing early
  352. * termination. Return codes are USB_STOR_XFER_xxx. If the bulk pipe
  353. * stalls during the transfer, the halt is automatically cleared.
  354. */
  355. int usb_stor_bulk_transfer_buf(struct us_data *us, unsigned int pipe,
  356. void *buf, unsigned int length, unsigned int *act_len)
  357. {
  358. int result;
  359. US_DEBUGP("%s: xfer %u bytes\n", __func__, length);
  360. /* fill and submit the URB */
  361. usb_fill_bulk_urb(us->current_urb, us->pusb_dev, pipe, buf, length,
  362. usb_stor_blocking_completion, NULL);
  363. result = usb_stor_msg_common(us, 0);
  364. /* store the actual length of the data transferred */
  365. if (act_len)
  366. *act_len = us->current_urb->actual_length;
  367. return interpret_urb_result(us, pipe, length, result,
  368. us->current_urb->actual_length);
  369. }
  370. EXPORT_SYMBOL_GPL(usb_stor_bulk_transfer_buf);
  371. /*
  372. * Transfer a scatter-gather list via bulk transfer
  373. *
  374. * This function does basically the same thing as usb_stor_bulk_transfer_buf()
  375. * above, but it uses the usbcore scatter-gather library.
  376. */
  377. static int usb_stor_bulk_transfer_sglist(struct us_data *us, unsigned int pipe,
  378. struct scatterlist *sg, int num_sg, unsigned int length,
  379. unsigned int *act_len)
  380. {
  381. int result;
  382. /* don't submit s-g requests during abort processing */
  383. if (test_bit(US_FLIDX_ABORTING, &us->dflags))
  384. return USB_STOR_XFER_ERROR;
  385. /* initialize the scatter-gather request block */
  386. US_DEBUGP("%s: xfer %u bytes, %d entries\n", __func__,
  387. length, num_sg);
  388. result = usb_sg_init(&us->current_sg, us->pusb_dev, pipe, 0,
  389. sg, num_sg, length, GFP_NOIO);
  390. if (result) {
  391. US_DEBUGP("usb_sg_init returned %d\n", result);
  392. return USB_STOR_XFER_ERROR;
  393. }
  394. /* since the block has been initialized successfully, it's now
  395. * okay to cancel it */
  396. set_bit(US_FLIDX_SG_ACTIVE, &us->dflags);
  397. /* did an abort occur during the submission? */
  398. if (test_bit(US_FLIDX_ABORTING, &us->dflags)) {
  399. /* cancel the request, if it hasn't been cancelled already */
  400. if (test_and_clear_bit(US_FLIDX_SG_ACTIVE, &us->dflags)) {
  401. US_DEBUGP("-- cancelling sg request\n");
  402. usb_sg_cancel(&us->current_sg);
  403. }
  404. }
  405. /* wait for the completion of the transfer */
  406. usb_sg_wait(&us->current_sg);
  407. clear_bit(US_FLIDX_SG_ACTIVE, &us->dflags);
  408. result = us->current_sg.status;
  409. if (act_len)
  410. *act_len = us->current_sg.bytes;
  411. return interpret_urb_result(us, pipe, length, result,
  412. us->current_sg.bytes);
  413. }
  414. /*
  415. * Common used function. Transfer a complete command
  416. * via usb_stor_bulk_transfer_sglist() above. Set cmnd resid
  417. */
  418. int usb_stor_bulk_srb(struct us_data* us, unsigned int pipe,
  419. struct scsi_cmnd* srb)
  420. {
  421. unsigned int partial;
  422. int result = usb_stor_bulk_transfer_sglist(us, pipe, scsi_sglist(srb),
  423. scsi_sg_count(srb), scsi_bufflen(srb),
  424. &partial);
  425. scsi_set_resid(srb, scsi_bufflen(srb) - partial);
  426. return result;
  427. }
  428. EXPORT_SYMBOL_GPL(usb_stor_bulk_srb);
  429. /*
  430. * Transfer an entire SCSI command's worth of data payload over the bulk
  431. * pipe.
  432. *
  433. * Note that this uses usb_stor_bulk_transfer_buf() and
  434. * usb_stor_bulk_transfer_sglist() to achieve its goals --
  435. * this function simply determines whether we're going to use
  436. * scatter-gather or not, and acts appropriately.
  437. */
  438. int usb_stor_bulk_transfer_sg(struct us_data* us, unsigned int pipe,
  439. void *buf, unsigned int length_left, int use_sg, int *residual)
  440. {
  441. int result;
  442. unsigned int partial;
  443. /* are we scatter-gathering? */
  444. if (use_sg) {
  445. /* use the usb core scatter-gather primitives */
  446. result = usb_stor_bulk_transfer_sglist(us, pipe,
  447. (struct scatterlist *) buf, use_sg,
  448. length_left, &partial);
  449. length_left -= partial;
  450. } else {
  451. /* no scatter-gather, just make the request */
  452. result = usb_stor_bulk_transfer_buf(us, pipe, buf,
  453. length_left, &partial);
  454. length_left -= partial;
  455. }
  456. /* store the residual and return the error code */
  457. if (residual)
  458. *residual = length_left;
  459. return result;
  460. }
  461. EXPORT_SYMBOL_GPL(usb_stor_bulk_transfer_sg);
  462. /***********************************************************************
  463. * Transport routines
  464. ***********************************************************************/
  465. /* There are so many devices that report the capacity incorrectly,
  466. * this routine was written to counteract some of the resulting
  467. * problems.
  468. */
  469. static void last_sector_hacks(struct us_data *us, struct scsi_cmnd *srb)
  470. {
  471. struct gendisk *disk;
  472. struct scsi_disk *sdkp;
  473. u32 sector;
  474. /* To Report "Medium Error: Record Not Found */
  475. static unsigned char record_not_found[18] = {
  476. [0] = 0x70, /* current error */
  477. [2] = MEDIUM_ERROR, /* = 0x03 */
  478. [7] = 0x0a, /* additional length */
  479. [12] = 0x14 /* Record Not Found */
  480. };
  481. /* If last-sector problems can't occur, whether because the
  482. * capacity was already decremented or because the device is
  483. * known to report the correct capacity, then we don't need
  484. * to do anything.
  485. */
  486. if (!us->use_last_sector_hacks)
  487. return;
  488. /* Was this command a READ(10) or a WRITE(10)? */
  489. if (srb->cmnd[0] != READ_10 && srb->cmnd[0] != WRITE_10)
  490. goto done;
  491. /* Did this command access the last sector? */
  492. sector = (srb->cmnd[2] << 24) | (srb->cmnd[3] << 16) |
  493. (srb->cmnd[4] << 8) | (srb->cmnd[5]);
  494. disk = srb->request->rq_disk;
  495. if (!disk)
  496. goto done;
  497. sdkp = scsi_disk(disk);
  498. if (!sdkp)
  499. goto done;
  500. if (sector + 1 != sdkp->capacity)
  501. goto done;
  502. if (srb->result == SAM_STAT_GOOD && scsi_get_resid(srb) == 0) {
  503. /* The command succeeded. We know this device doesn't
  504. * have the last-sector bug, so stop checking it.
  505. */
  506. us->use_last_sector_hacks = 0;
  507. } else {
  508. /* The command failed. Allow up to 3 retries in case this
  509. * is some normal sort of failure. After that, assume the
  510. * capacity is wrong and we're trying to access the sector
  511. * beyond the end. Replace the result code and sense data
  512. * with values that will cause the SCSI core to fail the
  513. * command immediately, instead of going into an infinite
  514. * (or even just a very long) retry loop.
  515. */
  516. if (++us->last_sector_retries < 3)
  517. return;
  518. srb->result = SAM_STAT_CHECK_CONDITION;
  519. memcpy(srb->sense_buffer, record_not_found,
  520. sizeof(record_not_found));
  521. }
  522. done:
  523. /* Don't reset the retry counter for TEST UNIT READY commands,
  524. * because they get issued after device resets which might be
  525. * caused by a failed last-sector access.
  526. */
  527. if (srb->cmnd[0] != TEST_UNIT_READY)
  528. us->last_sector_retries = 0;
  529. }
  530. /* Invoke the transport and basic error-handling/recovery methods
  531. *
  532. * This is used by the protocol layers to actually send the message to
  533. * the device and receive the response.
  534. */
  535. void usb_stor_invoke_transport(struct scsi_cmnd *srb, struct us_data *us)
  536. {
  537. int need_auto_sense;
  538. int result;
  539. /* send the command to the transport layer */
  540. scsi_set_resid(srb, 0);
  541. result = us->transport(srb, us);
  542. /* if the command gets aborted by the higher layers, we need to
  543. * short-circuit all other processing
  544. */
  545. if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
  546. US_DEBUGP("-- command was aborted\n");
  547. #ifdef CONFIG_USB_DEBUG_DETEAILED_LOG
  548. printk(KERN_ERR "usb storage -- command was aborted\n");
  549. #endif
  550. srb->result = DID_ABORT << 16;
  551. goto Handle_Errors;
  552. }
  553. /* if there is a transport error, reset and don't auto-sense */
  554. if (result == USB_STOR_TRANSPORT_ERROR) {
  555. US_DEBUGP("-- transport indicates error, resetting\n");
  556. #ifdef CONFIG_USB_DEBUG_DETEAILED_LOG
  557. printk(KERN_ERR "usb storage -- transport indicates error, resetting\n");
  558. #endif
  559. srb->result = DID_ERROR << 16;
  560. goto Handle_Errors;
  561. }
  562. /* if the transport provided its own sense data, don't auto-sense */
  563. if (result == USB_STOR_TRANSPORT_NO_SENSE) {
  564. srb->result = SAM_STAT_CHECK_CONDITION;
  565. last_sector_hacks(us, srb);
  566. return;
  567. }
  568. srb->result = SAM_STAT_GOOD;
  569. /* Determine if we need to auto-sense
  570. *
  571. * I normally don't use a flag like this, but it's almost impossible
  572. * to understand what's going on here if I don't.
  573. */
  574. need_auto_sense = 0;
  575. /*
  576. * If we're running the CB transport, which is incapable
  577. * of determining status on its own, we will auto-sense
  578. * unless the operation involved a data-in transfer. Devices
  579. * can signal most data-in errors by stalling the bulk-in pipe.
  580. */
  581. if ((us->protocol == USB_PR_CB || us->protocol == USB_PR_DPCM_USB) &&
  582. srb->sc_data_direction != DMA_FROM_DEVICE) {
  583. US_DEBUGP("-- CB transport device requiring auto-sense\n");
  584. need_auto_sense = 1;
  585. }
  586. /*
  587. * If we have a failure, we're going to do a REQUEST_SENSE
  588. * automatically. Note that we differentiate between a command
  589. * "failure" and an "error" in the transport mechanism.
  590. */
  591. if (result == USB_STOR_TRANSPORT_FAILED) {
  592. US_DEBUGP("-- transport indicates command failure\n");
  593. need_auto_sense = 1;
  594. }
  595. /*
  596. * Determine if this device is SAT by seeing if the
  597. * command executed successfully. Otherwise we'll have
  598. * to wait for at least one CHECK_CONDITION to determine
  599. * SANE_SENSE support
  600. */
  601. if (unlikely((srb->cmnd[0] == ATA_16 || srb->cmnd[0] == ATA_12) &&
  602. result == USB_STOR_TRANSPORT_GOOD &&
  603. !(us->fflags & US_FL_SANE_SENSE) &&
  604. !(us->fflags & US_FL_BAD_SENSE) &&
  605. !(srb->cmnd[2] & 0x20))) {
  606. US_DEBUGP("-- SAT supported, increasing auto-sense\n");
  607. us->fflags |= US_FL_SANE_SENSE;
  608. }
  609. /*
  610. * A short transfer on a command where we don't expect it
  611. * is unusual, but it doesn't mean we need to auto-sense.
  612. */
  613. if ((scsi_get_resid(srb) > 0) &&
  614. !((srb->cmnd[0] == REQUEST_SENSE) ||
  615. (srb->cmnd[0] == INQUIRY) ||
  616. (srb->cmnd[0] == MODE_SENSE) ||
  617. (srb->cmnd[0] == LOG_SENSE) ||
  618. (srb->cmnd[0] == MODE_SENSE_10))) {
  619. US_DEBUGP("-- unexpectedly short transfer\n");
  620. }
  621. /* Now, if we need to do the auto-sense, let's do it */
  622. if (need_auto_sense) {
  623. int temp_result;
  624. struct scsi_eh_save ses;
  625. int sense_size = US_SENSE_SIZE;
  626. struct scsi_sense_hdr sshdr;
  627. const u8 *scdd;
  628. u8 fm_ili;
  629. /* device supports and needs bigger sense buffer */
  630. if (us->fflags & US_FL_SANE_SENSE)
  631. sense_size = ~0;
  632. Retry_Sense:
  633. US_DEBUGP("Issuing auto-REQUEST_SENSE\n");
  634. scsi_eh_prep_cmnd(srb, &ses, NULL, 0, sense_size);
  635. /* FIXME: we must do the protocol translation here */
  636. if (us->subclass == USB_SC_RBC || us->subclass == USB_SC_SCSI ||
  637. us->subclass == USB_SC_CYP_ATACB)
  638. srb->cmd_len = 6;
  639. else
  640. srb->cmd_len = 12;
  641. /* issue the auto-sense command */
  642. scsi_set_resid(srb, 0);
  643. temp_result = us->transport(us->srb, us);
  644. /* let's clean up right away */
  645. scsi_eh_restore_cmnd(srb, &ses);
  646. if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
  647. US_DEBUGP("-- auto-sense aborted\n");
  648. #ifdef CONFIG_USB_DEBUG_DETEAILED_LOG
  649. printk(KERN_ERR "usb storage -- auto-sense aborted\n");
  650. #endif
  651. srb->result = DID_ABORT << 16;
  652. /* If SANE_SENSE caused this problem, disable it */
  653. if (sense_size != US_SENSE_SIZE) {
  654. us->fflags &= ~US_FL_SANE_SENSE;
  655. us->fflags |= US_FL_BAD_SENSE;
  656. }
  657. goto Handle_Errors;
  658. }
  659. /* Some devices claim to support larger sense but fail when
  660. * trying to request it. When a transport failure happens
  661. * using US_FS_SANE_SENSE, we always retry with a standard
  662. * (small) sense request. This fixes some USB GSM modems
  663. */
  664. if (temp_result == USB_STOR_TRANSPORT_FAILED &&
  665. sense_size != US_SENSE_SIZE) {
  666. US_DEBUGP("-- auto-sense failure, retry small sense\n");
  667. sense_size = US_SENSE_SIZE;
  668. us->fflags &= ~US_FL_SANE_SENSE;
  669. us->fflags |= US_FL_BAD_SENSE;
  670. goto Retry_Sense;
  671. }
  672. /* Other failures */
  673. if (temp_result != USB_STOR_TRANSPORT_GOOD) {
  674. US_DEBUGP("-- auto-sense failure\n");
  675. #ifdef CONFIG_USB_DEBUG_DETEAILED_LOG
  676. printk(KERN_ERR "usb storage -- auto-sense failure\n");
  677. #endif
  678. /* we skip the reset if this happens to be a
  679. * multi-target device, since failure of an
  680. * auto-sense is perfectly valid
  681. */
  682. srb->result = DID_ERROR << 16;
  683. if (!(us->fflags & US_FL_SCM_MULT_TARG))
  684. goto Handle_Errors;
  685. return;
  686. }
  687. /* If the sense data returned is larger than 18-bytes then we
  688. * assume this device supports requesting more in the future.
  689. * The response code must be 70h through 73h inclusive.
  690. */
  691. if (srb->sense_buffer[7] > (US_SENSE_SIZE - 8) &&
  692. !(us->fflags & US_FL_SANE_SENSE) &&
  693. !(us->fflags & US_FL_BAD_SENSE) &&
  694. (srb->sense_buffer[0] & 0x7C) == 0x70) {
  695. US_DEBUGP("-- SANE_SENSE support enabled\n");
  696. us->fflags |= US_FL_SANE_SENSE;
  697. /* Indicate to the user that we truncated their sense
  698. * because we didn't know it supported larger sense.
  699. */
  700. US_DEBUGP("-- Sense data truncated to %i from %i\n",
  701. US_SENSE_SIZE,
  702. srb->sense_buffer[7] + 8);
  703. srb->sense_buffer[7] = (US_SENSE_SIZE - 8);
  704. }
  705. scsi_normalize_sense(srb->sense_buffer, SCSI_SENSE_BUFFERSIZE,
  706. &sshdr);
  707. US_DEBUGP("-- Result from auto-sense is %d\n", temp_result);
  708. US_DEBUGP("-- code: 0x%x, key: 0x%x, ASC: 0x%x, ASCQ: 0x%x\n",
  709. sshdr.response_code, sshdr.sense_key,
  710. sshdr.asc, sshdr.ascq);
  711. #ifdef CONFIG_USB_STORAGE_DEBUG
  712. usb_stor_show_sense(sshdr.sense_key, sshdr.asc, sshdr.ascq);
  713. #endif
  714. /* set the result so the higher layers expect this data */
  715. srb->result = SAM_STAT_CHECK_CONDITION;
  716. scdd = scsi_sense_desc_find(srb->sense_buffer,
  717. SCSI_SENSE_BUFFERSIZE, 4);
  718. fm_ili = (scdd ? scdd[3] : srb->sense_buffer[2]) & 0xA0;
  719. /* We often get empty sense data. This could indicate that
  720. * everything worked or that there was an unspecified
  721. * problem. We have to decide which.
  722. */
  723. if (sshdr.sense_key == 0 && sshdr.asc == 0 && sshdr.ascq == 0 &&
  724. fm_ili == 0) {
  725. /* If things are really okay, then let's show that.
  726. * Zero out the sense buffer so the higher layers
  727. * won't realize we did an unsolicited auto-sense.
  728. */
  729. if (result == USB_STOR_TRANSPORT_GOOD) {
  730. srb->result = SAM_STAT_GOOD;
  731. srb->sense_buffer[0] = 0x0;
  732. /* If there was a problem, report an unspecified
  733. * hardware error to prevent the higher layers from
  734. * entering an infinite retry loop.
  735. */
  736. } else {
  737. srb->result = DID_ERROR << 16;
  738. if ((sshdr.response_code & 0x72) == 0x72)
  739. srb->sense_buffer[1] = HARDWARE_ERROR;
  740. else
  741. srb->sense_buffer[2] = HARDWARE_ERROR;
  742. }
  743. }
  744. }
  745. /*
  746. * Some devices don't work or return incorrect data the first
  747. * time they get a READ(10) command, or for the first READ(10)
  748. * after a media change. If the INITIAL_READ10 flag is set,
  749. * keep track of whether READ(10) commands succeed. If the
  750. * previous one succeeded and this one failed, set the REDO_READ10
  751. * flag to force a retry.
  752. */
  753. if (unlikely((us->fflags & US_FL_INITIAL_READ10) &&
  754. srb->cmnd[0] == READ_10)) {
  755. if (srb->result == SAM_STAT_GOOD) {
  756. set_bit(US_FLIDX_READ10_WORKED, &us->dflags);
  757. } else if (test_bit(US_FLIDX_READ10_WORKED, &us->dflags)) {
  758. clear_bit(US_FLIDX_READ10_WORKED, &us->dflags);
  759. set_bit(US_FLIDX_REDO_READ10, &us->dflags);
  760. }
  761. /*
  762. * Next, if the REDO_READ10 flag is set, return a result
  763. * code that will cause the SCSI core to retry the READ(10)
  764. * command immediately.
  765. */
  766. if (test_bit(US_FLIDX_REDO_READ10, &us->dflags)) {
  767. clear_bit(US_FLIDX_REDO_READ10, &us->dflags);
  768. srb->result = DID_IMM_RETRY << 16;
  769. srb->sense_buffer[0] = 0;
  770. }
  771. }
  772. /* Did we transfer less than the minimum amount required? */
  773. if ((srb->result == SAM_STAT_GOOD || srb->sense_buffer[2] == 0) &&
  774. scsi_bufflen(srb) - scsi_get_resid(srb) < srb->underflow)
  775. srb->result = DID_ERROR << 16;
  776. last_sector_hacks(us, srb);
  777. return;
  778. /* Error and abort processing: try to resynchronize with the device
  779. * by issuing a port reset. If that fails, try a class-specific
  780. * device reset. */
  781. Handle_Errors:
  782. /* Set the RESETTING bit, and clear the ABORTING bit so that
  783. * the reset may proceed. */
  784. scsi_lock(us_to_host(us));
  785. set_bit(US_FLIDX_RESETTING, &us->dflags);
  786. clear_bit(US_FLIDX_ABORTING, &us->dflags);
  787. scsi_unlock(us_to_host(us));
  788. /* We must release the device lock because the pre_reset routine
  789. * will want to acquire it. */
  790. mutex_unlock(&us->dev_mutex);
  791. result = usb_stor_port_reset(us);
  792. mutex_lock(&us->dev_mutex);
  793. if (result < 0) {
  794. scsi_lock(us_to_host(us));
  795. usb_stor_report_device_reset(us);
  796. scsi_unlock(us_to_host(us));
  797. us->transport_reset(us);
  798. }
  799. clear_bit(US_FLIDX_RESETTING, &us->dflags);
  800. last_sector_hacks(us, srb);
  801. }
  802. /* Stop the current URB transfer */
  803. void usb_stor_stop_transport(struct us_data *us)
  804. {
  805. US_DEBUGP("%s called\n", __func__);
  806. /* If the state machine is blocked waiting for an URB,
  807. * let's wake it up. The test_and_clear_bit() call
  808. * guarantees that if a URB has just been submitted,
  809. * it won't be cancelled more than once. */
  810. if (test_and_clear_bit(US_FLIDX_URB_ACTIVE, &us->dflags)) {
  811. US_DEBUGP("-- cancelling URB\n");
  812. usb_unlink_urb(us->current_urb);
  813. }
  814. /* If we are waiting for a scatter-gather operation, cancel it. */
  815. if (test_and_clear_bit(US_FLIDX_SG_ACTIVE, &us->dflags)) {
  816. US_DEBUGP("-- cancelling sg request\n");
  817. usb_sg_cancel(&us->current_sg);
  818. }
  819. }
  820. /*
  821. * Control/Bulk and Control/Bulk/Interrupt transport
  822. */
  823. int usb_stor_CB_transport(struct scsi_cmnd *srb, struct us_data *us)
  824. {
  825. unsigned int transfer_length = scsi_bufflen(srb);
  826. unsigned int pipe = 0;
  827. int result;
  828. /* COMMAND STAGE */
  829. /* let's send the command via the control pipe */
  830. result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
  831. US_CBI_ADSC,
  832. USB_TYPE_CLASS | USB_RECIP_INTERFACE, 0,
  833. us->ifnum, srb->cmnd, srb->cmd_len);
  834. /* check the return code for the command */
  835. US_DEBUGP("Call to usb_stor_ctrl_transfer() returned %d\n", result);
  836. /* if we stalled the command, it means command failed */
  837. if (result == USB_STOR_XFER_STALLED) {
  838. return USB_STOR_TRANSPORT_FAILED;
  839. }
  840. /* Uh oh... serious problem here */
  841. if (result != USB_STOR_XFER_GOOD) {
  842. return USB_STOR_TRANSPORT_ERROR;
  843. }
  844. /* DATA STAGE */
  845. /* transfer the data payload for this command, if one exists*/
  846. if (transfer_length) {
  847. pipe = srb->sc_data_direction == DMA_FROM_DEVICE ?
  848. us->recv_bulk_pipe : us->send_bulk_pipe;
  849. result = usb_stor_bulk_srb(us, pipe, srb);
  850. US_DEBUGP("CBI data stage result is 0x%x\n", result);
  851. /* if we stalled the data transfer it means command failed */
  852. if (result == USB_STOR_XFER_STALLED)
  853. return USB_STOR_TRANSPORT_FAILED;
  854. if (result > USB_STOR_XFER_STALLED)
  855. return USB_STOR_TRANSPORT_ERROR;
  856. }
  857. /* STATUS STAGE */
  858. /* NOTE: CB does not have a status stage. Silly, I know. So
  859. * we have to catch this at a higher level.
  860. */
  861. if (us->protocol != USB_PR_CBI)
  862. return USB_STOR_TRANSPORT_GOOD;
  863. result = usb_stor_intr_transfer(us, us->iobuf, 2);
  864. US_DEBUGP("Got interrupt data (0x%x, 0x%x)\n",
  865. us->iobuf[0], us->iobuf[1]);
  866. if (result != USB_STOR_XFER_GOOD)
  867. return USB_STOR_TRANSPORT_ERROR;
  868. /* UFI gives us ASC and ASCQ, like a request sense
  869. *
  870. * REQUEST_SENSE and INQUIRY don't affect the sense data on UFI
  871. * devices, so we ignore the information for those commands. Note
  872. * that this means we could be ignoring a real error on these
  873. * commands, but that can't be helped.
  874. */
  875. if (us->subclass == USB_SC_UFI) {
  876. if (srb->cmnd[0] == REQUEST_SENSE ||
  877. srb->cmnd[0] == INQUIRY)
  878. return USB_STOR_TRANSPORT_GOOD;
  879. if (us->iobuf[0])
  880. goto Failed;
  881. return USB_STOR_TRANSPORT_GOOD;
  882. }
  883. /* If not UFI, we interpret the data as a result code
  884. * The first byte should always be a 0x0.
  885. *
  886. * Some bogus devices don't follow that rule. They stuff the ASC
  887. * into the first byte -- so if it's non-zero, call it a failure.
  888. */
  889. if (us->iobuf[0]) {
  890. US_DEBUGP("CBI IRQ data showed reserved bType 0x%x\n",
  891. us->iobuf[0]);
  892. goto Failed;
  893. }
  894. /* The second byte & 0x0F should be 0x0 for good, otherwise error */
  895. switch (us->iobuf[1] & 0x0F) {
  896. case 0x00:
  897. return USB_STOR_TRANSPORT_GOOD;
  898. case 0x01:
  899. goto Failed;
  900. }
  901. return USB_STOR_TRANSPORT_ERROR;
  902. /* the CBI spec requires that the bulk pipe must be cleared
  903. * following any data-in/out command failure (section 2.4.3.1.3)
  904. */
  905. Failed:
  906. if (pipe)
  907. usb_stor_clear_halt(us, pipe);
  908. return USB_STOR_TRANSPORT_FAILED;
  909. }
  910. EXPORT_SYMBOL_GPL(usb_stor_CB_transport);
  911. /*
  912. * Bulk only transport
  913. */
  914. /* Determine what the maximum LUN supported is */
  915. int usb_stor_Bulk_max_lun(struct us_data *us)
  916. {
  917. int result;
  918. /* issue the command */
  919. us->iobuf[0] = 0;
  920. result = usb_stor_control_msg(us, us->recv_ctrl_pipe,
  921. US_BULK_GET_MAX_LUN,
  922. USB_DIR_IN | USB_TYPE_CLASS |
  923. USB_RECIP_INTERFACE,
  924. 0, us->ifnum, us->iobuf, 1, 10*HZ);
  925. US_DEBUGP("GetMaxLUN command result is %d, data is %d\n",
  926. result, us->iobuf[0]);
  927. /* if we have a successful request, return the result */
  928. if (result > 0)
  929. return us->iobuf[0];
  930. /*
  931. * Some devices don't like GetMaxLUN. They may STALL the control
  932. * pipe, they may return a zero-length result, they may do nothing at
  933. * all and timeout, or they may fail in even more bizarrely creative
  934. * ways. In these cases the best approach is to use the default
  935. * value: only one LUN.
  936. */
  937. return 0;
  938. }
  939. int usb_stor_Bulk_transport(struct scsi_cmnd *srb, struct us_data *us)
  940. {
  941. struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
  942. struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
  943. unsigned int transfer_length = scsi_bufflen(srb);
  944. unsigned int residue;
  945. int result;
  946. int fake_sense = 0;
  947. unsigned int cswlen;
  948. unsigned int cbwlen = US_BULK_CB_WRAP_LEN;
  949. /* Take care of BULK32 devices; set extra byte to 0 */
  950. if (unlikely(us->fflags & US_FL_BULK32)) {
  951. cbwlen = 32;
  952. us->iobuf[31] = 0;
  953. }
  954. /* set up the command wrapper */
  955. bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
  956. bcb->DataTransferLength = cpu_to_le32(transfer_length);
  957. bcb->Flags = srb->sc_data_direction == DMA_FROM_DEVICE ?
  958. US_BULK_FLAG_IN : 0;
  959. bcb->Tag = ++us->tag;
  960. bcb->Lun = srb->device->lun;
  961. if (us->fflags & US_FL_SCM_MULT_TARG)
  962. bcb->Lun |= srb->device->id << 4;
  963. bcb->Length = srb->cmd_len;
  964. /* copy the command payload */
  965. memset(bcb->CDB, 0, sizeof(bcb->CDB));
  966. memcpy(bcb->CDB, srb->cmnd, bcb->Length);
  967. /* send it to out endpoint */
  968. US_DEBUGP("Bulk Command S 0x%x T 0x%x L %d F %d Trg %d LUN %d CL %d\n",
  969. le32_to_cpu(bcb->Signature), bcb->Tag,
  970. le32_to_cpu(bcb->DataTransferLength), bcb->Flags,
  971. (bcb->Lun >> 4), (bcb->Lun & 0x0F),
  972. bcb->Length);
  973. result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  974. bcb, cbwlen, NULL);
  975. US_DEBUGP("Bulk command transfer result=%d\n", result);
  976. if (result != USB_STOR_XFER_GOOD)
  977. return USB_STOR_TRANSPORT_ERROR;
  978. /* DATA STAGE */
  979. /* send/receive data payload, if there is any */
  980. /* Some USB-IDE converter chips need a 100us delay between the
  981. * command phase and the data phase. Some devices need a little
  982. * more than that, probably because of clock rate inaccuracies. */
  983. if (unlikely(us->fflags & US_FL_GO_SLOW))
  984. udelay(125);
  985. if (transfer_length) {
  986. unsigned int pipe = srb->sc_data_direction == DMA_FROM_DEVICE ?
  987. us->recv_bulk_pipe : us->send_bulk_pipe;
  988. result = usb_stor_bulk_srb(us, pipe, srb);
  989. US_DEBUGP("Bulk data transfer result 0x%x\n", result);
  990. if (result == USB_STOR_XFER_ERROR)
  991. return USB_STOR_TRANSPORT_ERROR;
  992. /* If the device tried to send back more data than the
  993. * amount requested, the spec requires us to transfer
  994. * the CSW anyway. Since there's no point retrying the
  995. * the command, we'll return fake sense data indicating
  996. * Illegal Request, Invalid Field in CDB.
  997. */
  998. if (result == USB_STOR_XFER_LONG)
  999. fake_sense = 1;
  1000. /*
  1001. * Sometimes a device will mistakenly skip the data phase
  1002. * and go directly to the status phase without sending a
  1003. * zero-length packet. If we get a 13-byte response here,
  1004. * check whether it really is a CSW.
  1005. */
  1006. if (result == USB_STOR_XFER_SHORT &&
  1007. srb->sc_data_direction == DMA_FROM_DEVICE &&
  1008. transfer_length - scsi_get_resid(srb) ==
  1009. US_BULK_CS_WRAP_LEN) {
  1010. struct scatterlist *sg = NULL;
  1011. unsigned int offset = 0;
  1012. if (usb_stor_access_xfer_buf((unsigned char *) bcs,
  1013. US_BULK_CS_WRAP_LEN, srb, &sg,
  1014. &offset, FROM_XFER_BUF) ==
  1015. US_BULK_CS_WRAP_LEN &&
  1016. bcs->Signature ==
  1017. cpu_to_le32(US_BULK_CS_SIGN)) {
  1018. US_DEBUGP("Device skipped data phase\n");
  1019. scsi_set_resid(srb, transfer_length);
  1020. goto skipped_data_phase;
  1021. }
  1022. }
  1023. }
  1024. /* See flow chart on pg 15 of the Bulk Only Transport spec for
  1025. * an explanation of how this code works.
  1026. */
  1027. /* get CSW for device status */
  1028. US_DEBUGP("Attempting to get CSW...\n");
  1029. result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  1030. bcs, US_BULK_CS_WRAP_LEN, &cswlen);
  1031. /* Some broken devices add unnecessary zero-length packets to the
  1032. * end of their data transfers. Such packets show up as 0-length
  1033. * CSWs. If we encounter such a thing, try to read the CSW again.
  1034. */
  1035. if (result == USB_STOR_XFER_SHORT && cswlen == 0) {
  1036. US_DEBUGP("Received 0-length CSW; retrying...\n");
  1037. result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  1038. bcs, US_BULK_CS_WRAP_LEN, &cswlen);
  1039. }
  1040. /* did the attempt to read the CSW fail? */
  1041. if (result == USB_STOR_XFER_STALLED) {
  1042. /* get the status again */
  1043. US_DEBUGP("Attempting to get CSW (2nd try)...\n");
  1044. result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  1045. bcs, US_BULK_CS_WRAP_LEN, NULL);
  1046. }
  1047. /* if we still have a failure at this point, we're in trouble */
  1048. US_DEBUGP("Bulk status result = %d\n", result);
  1049. if (result != USB_STOR_XFER_GOOD)
  1050. return USB_STOR_TRANSPORT_ERROR;
  1051. skipped_data_phase:
  1052. /* check bulk status */
  1053. residue = le32_to_cpu(bcs->Residue);
  1054. US_DEBUGP("Bulk Status S 0x%x T 0x%x R %u Stat 0x%x\n",
  1055. le32_to_cpu(bcs->Signature), bcs->Tag,
  1056. residue, bcs->Status);
  1057. if (!(bcs->Tag == us->tag || (us->fflags & US_FL_BULK_IGNORE_TAG)) ||
  1058. bcs->Status > US_BULK_STAT_PHASE) {
  1059. US_DEBUGP("Bulk logical error\n");
  1060. return USB_STOR_TRANSPORT_ERROR;
  1061. }
  1062. /* Some broken devices report odd signatures, so we do not check them
  1063. * for validity against the spec. We store the first one we see,
  1064. * and check subsequent transfers for validity against this signature.
  1065. */
  1066. if (!us->bcs_signature) {
  1067. us->bcs_signature = bcs->Signature;
  1068. if (us->bcs_signature != cpu_to_le32(US_BULK_CS_SIGN))
  1069. US_DEBUGP("Learnt BCS signature 0x%08X\n",
  1070. le32_to_cpu(us->bcs_signature));
  1071. } else if (bcs->Signature != us->bcs_signature) {
  1072. US_DEBUGP("Signature mismatch: got %08X, expecting %08X\n",
  1073. le32_to_cpu(bcs->Signature),
  1074. le32_to_cpu(us->bcs_signature));
  1075. return USB_STOR_TRANSPORT_ERROR;
  1076. }
  1077. /* try to compute the actual residue, based on how much data
  1078. * was really transferred and what the device tells us */
  1079. if (residue && !(us->fflags & US_FL_IGNORE_RESIDUE)) {
  1080. /* Heuristically detect devices that generate bogus residues
  1081. * by seeing what happens with INQUIRY and READ CAPACITY
  1082. * commands.
  1083. */
  1084. if (bcs->Status == US_BULK_STAT_OK &&
  1085. scsi_get_resid(srb) == 0 &&
  1086. ((srb->cmnd[0] == INQUIRY &&
  1087. transfer_length == 36) ||
  1088. (srb->cmnd[0] == READ_CAPACITY &&
  1089. transfer_length == 8))) {
  1090. us->fflags |= US_FL_IGNORE_RESIDUE;
  1091. } else {
  1092. residue = min(residue, transfer_length);
  1093. scsi_set_resid(srb, max(scsi_get_resid(srb),
  1094. (int) residue));
  1095. }
  1096. }
  1097. /* based on the status code, we report good or bad */
  1098. switch (bcs->Status) {
  1099. case US_BULK_STAT_OK:
  1100. /* device babbled -- return fake sense data */
  1101. if (fake_sense) {
  1102. memcpy(srb->sense_buffer,
  1103. usb_stor_sense_invalidCDB,
  1104. sizeof(usb_stor_sense_invalidCDB));
  1105. return USB_STOR_TRANSPORT_NO_SENSE;
  1106. }
  1107. /* command good -- note that data could be short */
  1108. return USB_STOR_TRANSPORT_GOOD;
  1109. case US_BULK_STAT_FAIL:
  1110. /* command failed */
  1111. return USB_STOR_TRANSPORT_FAILED;
  1112. case US_BULK_STAT_PHASE:
  1113. /* phase error -- note that a transport reset will be
  1114. * invoked by the invoke_transport() function
  1115. */
  1116. return USB_STOR_TRANSPORT_ERROR;
  1117. }
  1118. /* we should never get here, but if we do, we're in trouble */
  1119. return USB_STOR_TRANSPORT_ERROR;
  1120. }
  1121. EXPORT_SYMBOL_GPL(usb_stor_Bulk_transport);
  1122. /***********************************************************************
  1123. * Reset routines
  1124. ***********************************************************************/
  1125. /* This is the common part of the device reset code.
  1126. *
  1127. * It's handy that every transport mechanism uses the control endpoint for
  1128. * resets.
  1129. *
  1130. * Basically, we send a reset with a 5-second timeout, so we don't get
  1131. * jammed attempting to do the reset.
  1132. */
  1133. static int usb_stor_reset_common(struct us_data *us,
  1134. u8 request, u8 requesttype,
  1135. u16 value, u16 index, void *data, u16 size)
  1136. {
  1137. int result;
  1138. int result2;
  1139. if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
  1140. US_DEBUGP("No reset during disconnect\n");
  1141. return -EIO;
  1142. }
  1143. result = usb_stor_control_msg(us, us->send_ctrl_pipe,
  1144. request, requesttype, value, index, data, size,
  1145. 5*HZ);
  1146. if (result < 0) {
  1147. US_DEBUGP("Soft reset failed: %d\n", result);
  1148. return result;
  1149. }
  1150. /* Give the device some time to recover from the reset,
  1151. * but don't delay disconnect processing. */
  1152. wait_event_interruptible_timeout(us->delay_wait,
  1153. test_bit(US_FLIDX_DISCONNECTING, &us->dflags),
  1154. HZ*6);
  1155. if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
  1156. US_DEBUGP("Reset interrupted by disconnect\n");
  1157. return -EIO;
  1158. }
  1159. US_DEBUGP("Soft reset: clearing bulk-in endpoint halt\n");
  1160. result = usb_stor_clear_halt(us, us->recv_bulk_pipe);
  1161. US_DEBUGP("Soft reset: clearing bulk-out endpoint halt\n");
  1162. result2 = usb_stor_clear_halt(us, us->send_bulk_pipe);
  1163. /* return a result code based on the result of the clear-halts */
  1164. if (result >= 0)
  1165. result = result2;
  1166. if (result < 0)
  1167. US_DEBUGP("Soft reset failed\n");
  1168. else
  1169. US_DEBUGP("Soft reset done\n");
  1170. return result;
  1171. }
  1172. /* This issues a CB[I] Reset to the device in question
  1173. */
  1174. #define CB_RESET_CMD_SIZE 12
  1175. int usb_stor_CB_reset(struct us_data *us)
  1176. {
  1177. US_DEBUGP("%s called\n", __func__);
  1178. memset(us->iobuf, 0xFF, CB_RESET_CMD_SIZE);
  1179. us->iobuf[0] = SEND_DIAGNOSTIC;
  1180. us->iobuf[1] = 4;
  1181. return usb_stor_reset_common(us, US_CBI_ADSC,
  1182. USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  1183. 0, us->ifnum, us->iobuf, CB_RESET_CMD_SIZE);
  1184. }
  1185. EXPORT_SYMBOL_GPL(usb_stor_CB_reset);
  1186. /* This issues a Bulk-only Reset to the device in question, including
  1187. * clearing the subsequent endpoint halts that may occur.
  1188. */
  1189. int usb_stor_Bulk_reset(struct us_data *us)
  1190. {
  1191. US_DEBUGP("%s called\n", __func__);
  1192. return usb_stor_reset_common(us, US_BULK_RESET_REQUEST,
  1193. USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  1194. 0, us->ifnum, NULL, 0);
  1195. }
  1196. EXPORT_SYMBOL_GPL(usb_stor_Bulk_reset);
  1197. /* Issue a USB port reset to the device. The caller must not hold
  1198. * us->dev_mutex.
  1199. */
  1200. int usb_stor_port_reset(struct us_data *us)
  1201. {
  1202. int result;
  1203. /*for these devices we must use the class specific method */
  1204. if (us->pusb_dev->quirks & USB_QUIRK_RESET_MORPHS)
  1205. return -EPERM;
  1206. result = usb_lock_device_for_reset(us->pusb_dev, us->pusb_intf);
  1207. if (result < 0)
  1208. US_DEBUGP("unable to lock device for reset: %d\n", result);
  1209. else {
  1210. /* Were we disconnected while waiting for the lock? */
  1211. if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
  1212. result = -EIO;
  1213. US_DEBUGP("No reset during disconnect\n");
  1214. } else {
  1215. result = usb_reset_device(us->pusb_dev);
  1216. US_DEBUGP("usb_reset_device returns %d\n",
  1217. result);
  1218. }
  1219. usb_unlock_device(us->pusb_dev);
  1220. }
  1221. return result;
  1222. }