transport.c 41 KB

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