transport.c 41 KB

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