transport.c 40 KB

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