transport.c 44 KB

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