transport.c 42 KB

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