transport.c 42 KB

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