transport.c 40 KB

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