transport.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  1. /* Driver for USB Mass Storage compliant devices
  2. *
  3. * $Id: transport.c,v 1.47 2002/04/22 03:39:43 mdharm Exp $
  4. *
  5. * Current development and maintenance by:
  6. * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
  7. *
  8. * Developed with the assistance of:
  9. * (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org)
  10. * (c) 2000 Stephen J. Gowdy (SGowdy@lbl.gov)
  11. * (c) 2002 Alan Stern <stern@rowland.org>
  12. *
  13. * Initial work by:
  14. * (c) 1999 Michael Gee (michael@linuxspecific.com)
  15. *
  16. * This driver is based on the 'USB Mass Storage Class' document. This
  17. * describes in detail the protocol used to communicate with such
  18. * devices. Clearly, the designers had SCSI and ATAPI commands in
  19. * mind when they created this document. The commands are all very
  20. * similar to commands in the SCSI-II and ATAPI specifications.
  21. *
  22. * It is important to note that in a number of cases this class
  23. * exhibits class-specific exemptions from the USB specification.
  24. * Notably the usage of NAK, STALL and ACK differs from the norm, in
  25. * that they are used to communicate wait, failed and OK on commands.
  26. *
  27. * Also, for certain devices, the interrupt endpoint is used to convey
  28. * status of a command.
  29. *
  30. * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
  31. * information about this driver.
  32. *
  33. * This program is free software; you can redistribute it and/or modify it
  34. * under the terms of the GNU General Public License as published by the
  35. * Free Software Foundation; either version 2, or (at your option) any
  36. * later version.
  37. *
  38. * This program is distributed in the hope that it will be useful, but
  39. * WITHOUT ANY WARRANTY; without even the implied warranty of
  40. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  41. * General Public License for more details.
  42. *
  43. * You should have received a copy of the GNU General Public License along
  44. * with this program; if not, write to the Free Software Foundation, Inc.,
  45. * 675 Mass Ave, Cambridge, MA 02139, USA.
  46. */
  47. #include <linux/config.h>
  48. #include <linux/sched.h>
  49. #include <linux/errno.h>
  50. #include <linux/slab.h>
  51. #include <scsi/scsi.h>
  52. #include <scsi/scsi_cmnd.h>
  53. #include <scsi/scsi_device.h>
  54. #include "usb.h"
  55. #include "transport.h"
  56. #include "protocol.h"
  57. #include "scsiglue.h"
  58. #include "debug.h"
  59. /***********************************************************************
  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->flags 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->flags
  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->flags 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 because the URB_ASYNC_UNLINK flag is set, the URB can
  94. * still be in progress even after a call to 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, struct pt_regs *regs)
  106. {
  107. struct completion *urb_done_ptr = (struct completion *)urb->context;
  108. complete(urb_done_ptr);
  109. }
  110. /* This is the timeout handler which will cancel an URB when its timeout
  111. * expires.
  112. */
  113. static void timeout_handler(unsigned long us_)
  114. {
  115. struct us_data *us = (struct us_data *) us_;
  116. if (test_and_clear_bit(US_FLIDX_URB_ACTIVE, &us->flags)) {
  117. US_DEBUGP("Timeout -- cancelling URB\n");
  118. usb_unlink_urb(us->current_urb);
  119. }
  120. }
  121. /* This is the common part of the URB message submission code
  122. *
  123. * All URBs from the usb-storage driver involved in handling a queued scsi
  124. * command _must_ pass through this function (or something like it) for the
  125. * abort mechanisms to work properly.
  126. */
  127. static int usb_stor_msg_common(struct us_data *us, int timeout)
  128. {
  129. struct completion urb_done;
  130. struct timer_list to_timer;
  131. int status;
  132. /* don't submit URBs during abort/disconnect processing */
  133. if (us->flags & ABORTING_OR_DISCONNECTING)
  134. return -EIO;
  135. /* set up data structures for the wakeup system */
  136. init_completion(&urb_done);
  137. /* fill the common fields in the URB */
  138. us->current_urb->context = &urb_done;
  139. us->current_urb->actual_length = 0;
  140. us->current_urb->error_count = 0;
  141. us->current_urb->status = 0;
  142. /* we assume that if transfer_buffer isn't us->iobuf then it
  143. * hasn't been mapped for DMA. Yes, this is clunky, but it's
  144. * easier than always having the caller tell us whether the
  145. * transfer buffer has already been mapped. */
  146. us->current_urb->transfer_flags =
  147. URB_ASYNC_UNLINK | URB_NO_SETUP_DMA_MAP;
  148. if (us->current_urb->transfer_buffer == us->iobuf)
  149. us->current_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  150. us->current_urb->transfer_dma = us->iobuf_dma;
  151. us->current_urb->setup_dma = us->cr_dma;
  152. /* submit the URB */
  153. status = usb_submit_urb(us->current_urb, GFP_NOIO);
  154. if (status) {
  155. /* something went wrong */
  156. return status;
  157. }
  158. /* since the URB has been submitted successfully, it's now okay
  159. * to cancel it */
  160. set_bit(US_FLIDX_URB_ACTIVE, &us->flags);
  161. /* did an abort/disconnect occur during the submission? */
  162. if (us->flags & ABORTING_OR_DISCONNECTING) {
  163. /* cancel the URB, if it hasn't been cancelled already */
  164. if (test_and_clear_bit(US_FLIDX_URB_ACTIVE, &us->flags)) {
  165. US_DEBUGP("-- cancelling URB\n");
  166. usb_unlink_urb(us->current_urb);
  167. }
  168. }
  169. /* submit the timeout timer, if a timeout was requested */
  170. if (timeout > 0) {
  171. init_timer(&to_timer);
  172. to_timer.expires = jiffies + timeout;
  173. to_timer.function = timeout_handler;
  174. to_timer.data = (unsigned long) us;
  175. add_timer(&to_timer);
  176. }
  177. /* wait for the completion of the URB */
  178. wait_for_completion(&urb_done);
  179. clear_bit(US_FLIDX_URB_ACTIVE, &us->flags);
  180. /* clean up the timeout timer */
  181. if (timeout > 0)
  182. del_timer_sync(&to_timer);
  183. /* return the URB status */
  184. return us->current_urb->status;
  185. }
  186. /*
  187. * Transfer one control message, with timeouts, and allowing early
  188. * termination. Return codes are usual -Exxx, *not* USB_STOR_XFER_xxx.
  189. */
  190. int usb_stor_control_msg(struct us_data *us, unsigned int pipe,
  191. u8 request, u8 requesttype, u16 value, u16 index,
  192. void *data, u16 size, int timeout)
  193. {
  194. int status;
  195. US_DEBUGP("%s: rq=%02x rqtype=%02x value=%04x index=%02x len=%u\n",
  196. __FUNCTION__, request, requesttype,
  197. value, index, size);
  198. /* fill in the devrequest structure */
  199. us->cr->bRequestType = requesttype;
  200. us->cr->bRequest = request;
  201. us->cr->wValue = cpu_to_le16(value);
  202. us->cr->wIndex = cpu_to_le16(index);
  203. us->cr->wLength = cpu_to_le16(size);
  204. /* fill and submit the URB */
  205. usb_fill_control_urb(us->current_urb, us->pusb_dev, pipe,
  206. (unsigned char*) us->cr, data, size,
  207. usb_stor_blocking_completion, NULL);
  208. status = usb_stor_msg_common(us, timeout);
  209. /* return the actual length of the data transferred if no error */
  210. if (status == 0)
  211. status = us->current_urb->actual_length;
  212. return status;
  213. }
  214. /* This is a version of usb_clear_halt() that allows early termination and
  215. * doesn't read the status from the device -- this is because some devices
  216. * crash their internal firmware when the status is requested after a halt.
  217. *
  218. * A definitive list of these 'bad' devices is too difficult to maintain or
  219. * make complete enough to be useful. This problem was first observed on the
  220. * Hagiwara FlashGate DUAL unit. However, bus traces reveal that neither
  221. * MacOS nor Windows checks the status after clearing a halt.
  222. *
  223. * Since many vendors in this space limit their testing to interoperability
  224. * with these two OSes, specification violations like this one are common.
  225. */
  226. int usb_stor_clear_halt(struct us_data *us, unsigned int pipe)
  227. {
  228. int result;
  229. int endp = usb_pipeendpoint(pipe);
  230. if (usb_pipein (pipe))
  231. endp |= USB_DIR_IN;
  232. result = usb_stor_control_msg(us, us->send_ctrl_pipe,
  233. USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
  234. USB_ENDPOINT_HALT, endp,
  235. NULL, 0, 3*HZ);
  236. /* reset the endpoint toggle */
  237. if (result >= 0)
  238. usb_settoggle(us->pusb_dev, usb_pipeendpoint(pipe),
  239. usb_pipeout(pipe), 0);
  240. US_DEBUGP("%s: result = %d\n", __FUNCTION__, result);
  241. return result;
  242. }
  243. /*
  244. * Interpret the results of a URB transfer
  245. *
  246. * This function prints appropriate debugging messages, clears halts on
  247. * non-control endpoints, and translates the status to the corresponding
  248. * USB_STOR_XFER_xxx return code.
  249. */
  250. static int interpret_urb_result(struct us_data *us, unsigned int pipe,
  251. unsigned int length, int result, unsigned int partial)
  252. {
  253. US_DEBUGP("Status code %d; transferred %u/%u\n",
  254. result, partial, length);
  255. switch (result) {
  256. /* no error code; did we send all the data? */
  257. case 0:
  258. if (partial != length) {
  259. US_DEBUGP("-- short transfer\n");
  260. return USB_STOR_XFER_SHORT;
  261. }
  262. US_DEBUGP("-- transfer complete\n");
  263. return USB_STOR_XFER_GOOD;
  264. /* stalled */
  265. case -EPIPE:
  266. /* for control endpoints, (used by CB[I]) a stall indicates
  267. * a failed command */
  268. if (usb_pipecontrol(pipe)) {
  269. US_DEBUGP("-- stall on control pipe\n");
  270. return USB_STOR_XFER_STALLED;
  271. }
  272. /* for other sorts of endpoint, clear the stall */
  273. US_DEBUGP("clearing endpoint halt for pipe 0x%x\n", pipe);
  274. if (usb_stor_clear_halt(us, pipe) < 0)
  275. return USB_STOR_XFER_ERROR;
  276. return USB_STOR_XFER_STALLED;
  277. /* timeout or excessively long NAK */
  278. case -ETIMEDOUT:
  279. US_DEBUGP("-- timeout or NAK\n");
  280. return USB_STOR_XFER_ERROR;
  281. /* babble - the device tried to send more than we wanted to read */
  282. case -EOVERFLOW:
  283. US_DEBUGP("-- babble\n");
  284. return USB_STOR_XFER_LONG;
  285. /* the transfer was cancelled by abort, disconnect, or timeout */
  286. case -ECONNRESET:
  287. US_DEBUGP("-- transfer cancelled\n");
  288. return USB_STOR_XFER_ERROR;
  289. /* short scatter-gather read transfer */
  290. case -EREMOTEIO:
  291. US_DEBUGP("-- short read transfer\n");
  292. return USB_STOR_XFER_SHORT;
  293. /* abort or disconnect in progress */
  294. case -EIO:
  295. US_DEBUGP("-- abort or disconnect in progress\n");
  296. return USB_STOR_XFER_ERROR;
  297. /* the catch-all error case */
  298. default:
  299. US_DEBUGP("-- unknown error\n");
  300. return USB_STOR_XFER_ERROR;
  301. }
  302. }
  303. /*
  304. * Transfer one control message, without timeouts, but allowing early
  305. * termination. Return codes are USB_STOR_XFER_xxx.
  306. */
  307. int usb_stor_ctrl_transfer(struct us_data *us, unsigned int pipe,
  308. u8 request, u8 requesttype, u16 value, u16 index,
  309. void *data, u16 size)
  310. {
  311. int result;
  312. US_DEBUGP("%s: rq=%02x rqtype=%02x value=%04x index=%02x len=%u\n",
  313. __FUNCTION__, request, requesttype,
  314. value, index, size);
  315. /* fill in the devrequest structure */
  316. us->cr->bRequestType = requesttype;
  317. us->cr->bRequest = request;
  318. us->cr->wValue = cpu_to_le16(value);
  319. us->cr->wIndex = cpu_to_le16(index);
  320. us->cr->wLength = cpu_to_le16(size);
  321. /* fill and submit the URB */
  322. usb_fill_control_urb(us->current_urb, us->pusb_dev, pipe,
  323. (unsigned char*) us->cr, data, size,
  324. usb_stor_blocking_completion, NULL);
  325. result = usb_stor_msg_common(us, 0);
  326. return interpret_urb_result(us, pipe, size, result,
  327. us->current_urb->actual_length);
  328. }
  329. /*
  330. * Receive one interrupt buffer, without timeouts, but allowing early
  331. * termination. Return codes are USB_STOR_XFER_xxx.
  332. *
  333. * This routine always uses us->recv_intr_pipe as the pipe and
  334. * us->ep_bInterval as the interrupt interval.
  335. */
  336. static int usb_stor_intr_transfer(struct us_data *us, void *buf,
  337. unsigned int length)
  338. {
  339. int result;
  340. unsigned int pipe = us->recv_intr_pipe;
  341. unsigned int maxp;
  342. US_DEBUGP("%s: xfer %u bytes\n", __FUNCTION__, length);
  343. /* calculate the max packet size */
  344. maxp = usb_maxpacket(us->pusb_dev, pipe, usb_pipeout(pipe));
  345. if (maxp > length)
  346. maxp = length;
  347. /* fill and submit the URB */
  348. usb_fill_int_urb(us->current_urb, us->pusb_dev, pipe, buf,
  349. maxp, usb_stor_blocking_completion, NULL,
  350. us->ep_bInterval);
  351. result = usb_stor_msg_common(us, 0);
  352. return interpret_urb_result(us, pipe, length, result,
  353. us->current_urb->actual_length);
  354. }
  355. /*
  356. * Transfer one buffer via bulk pipe, without timeouts, but allowing early
  357. * termination. Return codes are USB_STOR_XFER_xxx. If the bulk pipe
  358. * stalls during the transfer, the halt is automatically cleared.
  359. */
  360. int usb_stor_bulk_transfer_buf(struct us_data *us, unsigned int pipe,
  361. void *buf, unsigned int length, unsigned int *act_len)
  362. {
  363. int result;
  364. US_DEBUGP("%s: xfer %u bytes\n", __FUNCTION__, length);
  365. /* fill and submit the URB */
  366. usb_fill_bulk_urb(us->current_urb, us->pusb_dev, pipe, buf, length,
  367. usb_stor_blocking_completion, NULL);
  368. result = usb_stor_msg_common(us, 0);
  369. /* store the actual length of the data transferred */
  370. if (act_len)
  371. *act_len = us->current_urb->actual_length;
  372. return interpret_urb_result(us, pipe, length, result,
  373. us->current_urb->actual_length);
  374. }
  375. /*
  376. * Transfer a scatter-gather list via bulk transfer
  377. *
  378. * This function does basically the same thing as usb_stor_bulk_transfer_buf()
  379. * above, but it uses the usbcore scatter-gather library.
  380. */
  381. static int usb_stor_bulk_transfer_sglist(struct us_data *us, unsigned int pipe,
  382. struct scatterlist *sg, int num_sg, unsigned int length,
  383. unsigned int *act_len)
  384. {
  385. int result;
  386. /* don't submit s-g requests during abort/disconnect processing */
  387. if (us->flags & ABORTING_OR_DISCONNECTING)
  388. return USB_STOR_XFER_ERROR;
  389. /* initialize the scatter-gather request block */
  390. US_DEBUGP("%s: xfer %u bytes, %d entries\n", __FUNCTION__,
  391. length, num_sg);
  392. result = usb_sg_init(&us->current_sg, us->pusb_dev, pipe, 0,
  393. sg, num_sg, length, SLAB_NOIO);
  394. if (result) {
  395. US_DEBUGP("usb_sg_init returned %d\n", result);
  396. return USB_STOR_XFER_ERROR;
  397. }
  398. /* since the block has been initialized successfully, it's now
  399. * okay to cancel it */
  400. set_bit(US_FLIDX_SG_ACTIVE, &us->flags);
  401. /* did an abort/disconnect occur during the submission? */
  402. if (us->flags & ABORTING_OR_DISCONNECTING) {
  403. /* cancel the request, if it hasn't been cancelled already */
  404. if (test_and_clear_bit(US_FLIDX_SG_ACTIVE, &us->flags)) {
  405. US_DEBUGP("-- cancelling sg request\n");
  406. usb_sg_cancel(&us->current_sg);
  407. }
  408. }
  409. /* wait for the completion of the transfer */
  410. usb_sg_wait(&us->current_sg);
  411. clear_bit(US_FLIDX_SG_ACTIVE, &us->flags);
  412. result = us->current_sg.status;
  413. if (act_len)
  414. *act_len = us->current_sg.bytes;
  415. return interpret_urb_result(us, pipe, length, result,
  416. us->current_sg.bytes);
  417. }
  418. /*
  419. * Transfer an entire SCSI command's worth of data payload over the bulk
  420. * pipe.
  421. *
  422. * Note that this uses usb_stor_bulk_transfer_buf() and
  423. * usb_stor_bulk_transfer_sglist() to achieve its goals --
  424. * this function simply determines whether we're going to use
  425. * scatter-gather or not, and acts appropriately.
  426. */
  427. int usb_stor_bulk_transfer_sg(struct us_data* us, unsigned int pipe,
  428. void *buf, unsigned int length_left, int use_sg, int *residual)
  429. {
  430. int result;
  431. unsigned int partial;
  432. /* are we scatter-gathering? */
  433. if (use_sg) {
  434. /* use the usb core scatter-gather primitives */
  435. result = usb_stor_bulk_transfer_sglist(us, pipe,
  436. (struct scatterlist *) buf, use_sg,
  437. length_left, &partial);
  438. length_left -= partial;
  439. } else {
  440. /* no scatter-gather, just make the request */
  441. result = usb_stor_bulk_transfer_buf(us, pipe, buf,
  442. length_left, &partial);
  443. length_left -= partial;
  444. }
  445. /* store the residual and return the error code */
  446. if (residual)
  447. *residual = length_left;
  448. return result;
  449. }
  450. /***********************************************************************
  451. * Transport routines
  452. ***********************************************************************/
  453. /* Invoke the transport and basic error-handling/recovery methods
  454. *
  455. * This is used by the protocol layers to actually send the message to
  456. * the device and receive the response.
  457. */
  458. void usb_stor_invoke_transport(struct scsi_cmnd *srb, struct us_data *us)
  459. {
  460. int need_auto_sense;
  461. int result;
  462. /* send the command to the transport layer */
  463. srb->resid = 0;
  464. result = us->transport(srb, us);
  465. /* if the command gets aborted by the higher layers, we need to
  466. * short-circuit all other processing
  467. */
  468. if (test_bit(US_FLIDX_TIMED_OUT, &us->flags)) {
  469. US_DEBUGP("-- command was aborted\n");
  470. srb->result = DID_ABORT << 16;
  471. goto Handle_Errors;
  472. }
  473. /* if there is a transport error, reset and don't auto-sense */
  474. if (result == USB_STOR_TRANSPORT_ERROR) {
  475. US_DEBUGP("-- transport indicates error, resetting\n");
  476. srb->result = DID_ERROR << 16;
  477. goto Handle_Errors;
  478. }
  479. /* if the transport provided its own sense data, don't auto-sense */
  480. if (result == USB_STOR_TRANSPORT_NO_SENSE) {
  481. srb->result = SAM_STAT_CHECK_CONDITION;
  482. return;
  483. }
  484. srb->result = SAM_STAT_GOOD;
  485. /* Determine if we need to auto-sense
  486. *
  487. * I normally don't use a flag like this, but it's almost impossible
  488. * to understand what's going on here if I don't.
  489. */
  490. need_auto_sense = 0;
  491. /*
  492. * If we're running the CB transport, which is incapable
  493. * of determining status on its own, we will auto-sense
  494. * unless the operation involved a data-in transfer. Devices
  495. * can signal most data-in errors by stalling the bulk-in pipe.
  496. */
  497. if ((us->protocol == US_PR_CB || us->protocol == US_PR_DPCM_USB) &&
  498. srb->sc_data_direction != DMA_FROM_DEVICE) {
  499. US_DEBUGP("-- CB transport device requiring auto-sense\n");
  500. need_auto_sense = 1;
  501. }
  502. /*
  503. * If we have a failure, we're going to do a REQUEST_SENSE
  504. * automatically. Note that we differentiate between a command
  505. * "failure" and an "error" in the transport mechanism.
  506. */
  507. if (result == USB_STOR_TRANSPORT_FAILED) {
  508. US_DEBUGP("-- transport indicates command failure\n");
  509. need_auto_sense = 1;
  510. }
  511. /*
  512. * A short transfer on a command where we don't expect it
  513. * is unusual, but it doesn't mean we need to auto-sense.
  514. */
  515. if ((srb->resid > 0) &&
  516. !((srb->cmnd[0] == REQUEST_SENSE) ||
  517. (srb->cmnd[0] == INQUIRY) ||
  518. (srb->cmnd[0] == MODE_SENSE) ||
  519. (srb->cmnd[0] == LOG_SENSE) ||
  520. (srb->cmnd[0] == MODE_SENSE_10))) {
  521. US_DEBUGP("-- unexpectedly short transfer\n");
  522. }
  523. /* Now, if we need to do the auto-sense, let's do it */
  524. if (need_auto_sense) {
  525. int temp_result;
  526. void* old_request_buffer;
  527. unsigned short old_sg;
  528. unsigned old_request_bufflen;
  529. unsigned char old_sc_data_direction;
  530. unsigned char old_cmd_len;
  531. unsigned char old_cmnd[MAX_COMMAND_SIZE];
  532. unsigned long old_serial_number;
  533. int old_resid;
  534. US_DEBUGP("Issuing auto-REQUEST_SENSE\n");
  535. /* save the old command */
  536. memcpy(old_cmnd, srb->cmnd, MAX_COMMAND_SIZE);
  537. old_cmd_len = srb->cmd_len;
  538. /* set the command and the LUN */
  539. memset(srb->cmnd, 0, MAX_COMMAND_SIZE);
  540. srb->cmnd[0] = REQUEST_SENSE;
  541. srb->cmnd[1] = old_cmnd[1] & 0xE0;
  542. srb->cmnd[4] = 18;
  543. /* FIXME: we must do the protocol translation here */
  544. if (us->subclass == US_SC_RBC || us->subclass == US_SC_SCSI)
  545. srb->cmd_len = 6;
  546. else
  547. srb->cmd_len = 12;
  548. /* set the transfer direction */
  549. old_sc_data_direction = srb->sc_data_direction;
  550. srb->sc_data_direction = DMA_FROM_DEVICE;
  551. /* use the new buffer we have */
  552. old_request_buffer = srb->request_buffer;
  553. srb->request_buffer = srb->sense_buffer;
  554. /* set the buffer length for transfer */
  555. old_request_bufflen = srb->request_bufflen;
  556. srb->request_bufflen = 18;
  557. /* set up for no scatter-gather use */
  558. old_sg = srb->use_sg;
  559. srb->use_sg = 0;
  560. /* change the serial number -- toggle the high bit*/
  561. old_serial_number = srb->serial_number;
  562. srb->serial_number ^= 0x80000000;
  563. /* issue the auto-sense command */
  564. old_resid = srb->resid;
  565. srb->resid = 0;
  566. temp_result = us->transport(us->srb, us);
  567. /* let's clean up right away */
  568. srb->resid = old_resid;
  569. srb->request_buffer = old_request_buffer;
  570. srb->request_bufflen = old_request_bufflen;
  571. srb->use_sg = old_sg;
  572. srb->serial_number = old_serial_number;
  573. srb->sc_data_direction = old_sc_data_direction;
  574. srb->cmd_len = old_cmd_len;
  575. memcpy(srb->cmnd, old_cmnd, MAX_COMMAND_SIZE);
  576. if (test_bit(US_FLIDX_TIMED_OUT, &us->flags)) {
  577. US_DEBUGP("-- auto-sense aborted\n");
  578. srb->result = DID_ABORT << 16;
  579. goto Handle_Errors;
  580. }
  581. if (temp_result != USB_STOR_TRANSPORT_GOOD) {
  582. US_DEBUGP("-- auto-sense failure\n");
  583. /* we skip the reset if this happens to be a
  584. * multi-target device, since failure of an
  585. * auto-sense is perfectly valid
  586. */
  587. srb->result = DID_ERROR << 16;
  588. if (!(us->flags & US_FL_SCM_MULT_TARG))
  589. goto Handle_Errors;
  590. return;
  591. }
  592. US_DEBUGP("-- Result from auto-sense is %d\n", temp_result);
  593. US_DEBUGP("-- code: 0x%x, key: 0x%x, ASC: 0x%x, ASCQ: 0x%x\n",
  594. srb->sense_buffer[0],
  595. srb->sense_buffer[2] & 0xf,
  596. srb->sense_buffer[12],
  597. srb->sense_buffer[13]);
  598. #ifdef CONFIG_USB_STORAGE_DEBUG
  599. usb_stor_show_sense(
  600. srb->sense_buffer[2] & 0xf,
  601. srb->sense_buffer[12],
  602. srb->sense_buffer[13]);
  603. #endif
  604. /* set the result so the higher layers expect this data */
  605. srb->result = SAM_STAT_CHECK_CONDITION;
  606. /* If things are really okay, then let's show that. Zero
  607. * out the sense buffer so the higher layers won't realize
  608. * we did an unsolicited auto-sense. */
  609. if (result == USB_STOR_TRANSPORT_GOOD &&
  610. /* Filemark 0, ignore EOM, ILI 0, no sense */
  611. (srb->sense_buffer[2] & 0xaf) == 0 &&
  612. /* No ASC or ASCQ */
  613. srb->sense_buffer[12] == 0 &&
  614. srb->sense_buffer[13] == 0) {
  615. srb->result = SAM_STAT_GOOD;
  616. srb->sense_buffer[0] = 0x0;
  617. }
  618. }
  619. /* Did we transfer less than the minimum amount required? */
  620. if (srb->result == SAM_STAT_GOOD &&
  621. srb->request_bufflen - srb->resid < srb->underflow)
  622. srb->result = (DID_ERROR << 16) | (SUGGEST_RETRY << 24);
  623. return;
  624. /* Error and abort processing: try to resynchronize with the device
  625. * by issuing a port reset. If that fails, try a class-specific
  626. * device reset. */
  627. Handle_Errors:
  628. /* Let the SCSI layer know we are doing a reset, set the
  629. * RESETTING bit, and clear the ABORTING bit so that the reset
  630. * may proceed. */
  631. scsi_lock(us_to_host(us));
  632. usb_stor_report_bus_reset(us);
  633. set_bit(US_FLIDX_RESETTING, &us->flags);
  634. clear_bit(US_FLIDX_ABORTING, &us->flags);
  635. scsi_unlock(us_to_host(us));
  636. result = usb_stor_port_reset(us);
  637. if (result < 0) {
  638. scsi_lock(us_to_host(us));
  639. usb_stor_report_device_reset(us);
  640. scsi_unlock(us_to_host(us));
  641. us->transport_reset(us);
  642. }
  643. clear_bit(US_FLIDX_RESETTING, &us->flags);
  644. }
  645. /* Stop the current URB transfer */
  646. void usb_stor_stop_transport(struct us_data *us)
  647. {
  648. US_DEBUGP("%s called\n", __FUNCTION__);
  649. /* If the state machine is blocked waiting for an URB,
  650. * let's wake it up. The test_and_clear_bit() call
  651. * guarantees that if a URB has just been submitted,
  652. * it won't be cancelled more than once. */
  653. if (test_and_clear_bit(US_FLIDX_URB_ACTIVE, &us->flags)) {
  654. US_DEBUGP("-- cancelling URB\n");
  655. usb_unlink_urb(us->current_urb);
  656. }
  657. /* If we are waiting for a scatter-gather operation, cancel it. */
  658. if (test_and_clear_bit(US_FLIDX_SG_ACTIVE, &us->flags)) {
  659. US_DEBUGP("-- cancelling sg request\n");
  660. usb_sg_cancel(&us->current_sg);
  661. }
  662. }
  663. /*
  664. * Control/Bulk/Interrupt transport
  665. */
  666. int usb_stor_CBI_transport(struct scsi_cmnd *srb, struct us_data *us)
  667. {
  668. unsigned int transfer_length = srb->request_bufflen;
  669. unsigned int pipe = 0;
  670. int result;
  671. /* COMMAND STAGE */
  672. /* let's send the command via the control pipe */
  673. result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
  674. US_CBI_ADSC,
  675. USB_TYPE_CLASS | USB_RECIP_INTERFACE, 0,
  676. us->ifnum, srb->cmnd, srb->cmd_len);
  677. /* check the return code for the command */
  678. US_DEBUGP("Call to usb_stor_ctrl_transfer() returned %d\n", result);
  679. /* if we stalled the command, it means command failed */
  680. if (result == USB_STOR_XFER_STALLED) {
  681. return USB_STOR_TRANSPORT_FAILED;
  682. }
  683. /* Uh oh... serious problem here */
  684. if (result != USB_STOR_XFER_GOOD) {
  685. return USB_STOR_TRANSPORT_ERROR;
  686. }
  687. /* DATA STAGE */
  688. /* transfer the data payload for this command, if one exists*/
  689. if (transfer_length) {
  690. pipe = srb->sc_data_direction == DMA_FROM_DEVICE ?
  691. us->recv_bulk_pipe : us->send_bulk_pipe;
  692. result = usb_stor_bulk_transfer_sg(us, pipe,
  693. srb->request_buffer, transfer_length,
  694. srb->use_sg, &srb->resid);
  695. US_DEBUGP("CBI data stage result is 0x%x\n", result);
  696. /* if we stalled the data transfer it means command failed */
  697. if (result == USB_STOR_XFER_STALLED)
  698. return USB_STOR_TRANSPORT_FAILED;
  699. if (result > USB_STOR_XFER_STALLED)
  700. return USB_STOR_TRANSPORT_ERROR;
  701. }
  702. /* STATUS STAGE */
  703. result = usb_stor_intr_transfer(us, us->iobuf, 2);
  704. US_DEBUGP("Got interrupt data (0x%x, 0x%x)\n",
  705. us->iobuf[0], us->iobuf[1]);
  706. if (result != USB_STOR_XFER_GOOD)
  707. return USB_STOR_TRANSPORT_ERROR;
  708. /* UFI gives us ASC and ASCQ, like a request sense
  709. *
  710. * REQUEST_SENSE and INQUIRY don't affect the sense data on UFI
  711. * devices, so we ignore the information for those commands. Note
  712. * that this means we could be ignoring a real error on these
  713. * commands, but that can't be helped.
  714. */
  715. if (us->subclass == US_SC_UFI) {
  716. if (srb->cmnd[0] == REQUEST_SENSE ||
  717. srb->cmnd[0] == INQUIRY)
  718. return USB_STOR_TRANSPORT_GOOD;
  719. if (us->iobuf[0])
  720. goto Failed;
  721. return USB_STOR_TRANSPORT_GOOD;
  722. }
  723. /* If not UFI, we interpret the data as a result code
  724. * The first byte should always be a 0x0.
  725. *
  726. * Some bogus devices don't follow that rule. They stuff the ASC
  727. * into the first byte -- so if it's non-zero, call it a failure.
  728. */
  729. if (us->iobuf[0]) {
  730. US_DEBUGP("CBI IRQ data showed reserved bType 0x%x\n",
  731. us->iobuf[0]);
  732. goto Failed;
  733. }
  734. /* The second byte & 0x0F should be 0x0 for good, otherwise error */
  735. switch (us->iobuf[1] & 0x0F) {
  736. case 0x00:
  737. return USB_STOR_TRANSPORT_GOOD;
  738. case 0x01:
  739. goto Failed;
  740. }
  741. return USB_STOR_TRANSPORT_ERROR;
  742. /* the CBI spec requires that the bulk pipe must be cleared
  743. * following any data-in/out command failure (section 2.4.3.1.3)
  744. */
  745. Failed:
  746. if (pipe)
  747. usb_stor_clear_halt(us, pipe);
  748. return USB_STOR_TRANSPORT_FAILED;
  749. }
  750. /*
  751. * Control/Bulk transport
  752. */
  753. int usb_stor_CB_transport(struct scsi_cmnd *srb, struct us_data *us)
  754. {
  755. unsigned int transfer_length = srb->request_bufflen;
  756. int result;
  757. /* COMMAND STAGE */
  758. /* let's send the command via the control pipe */
  759. result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
  760. US_CBI_ADSC,
  761. USB_TYPE_CLASS | USB_RECIP_INTERFACE, 0,
  762. us->ifnum, srb->cmnd, srb->cmd_len);
  763. /* check the return code for the command */
  764. US_DEBUGP("Call to usb_stor_ctrl_transfer() returned %d\n", result);
  765. /* if we stalled the command, it means command failed */
  766. if (result == USB_STOR_XFER_STALLED) {
  767. return USB_STOR_TRANSPORT_FAILED;
  768. }
  769. /* Uh oh... serious problem here */
  770. if (result != USB_STOR_XFER_GOOD) {
  771. return USB_STOR_TRANSPORT_ERROR;
  772. }
  773. /* DATA STAGE */
  774. /* transfer the data payload for this command, if one exists*/
  775. if (transfer_length) {
  776. unsigned int pipe = srb->sc_data_direction == DMA_FROM_DEVICE ?
  777. us->recv_bulk_pipe : us->send_bulk_pipe;
  778. result = usb_stor_bulk_transfer_sg(us, pipe,
  779. srb->request_buffer, transfer_length,
  780. srb->use_sg, &srb->resid);
  781. US_DEBUGP("CB data stage result is 0x%x\n", result);
  782. /* if we stalled the data transfer it means command failed */
  783. if (result == USB_STOR_XFER_STALLED)
  784. return USB_STOR_TRANSPORT_FAILED;
  785. if (result > USB_STOR_XFER_STALLED)
  786. return USB_STOR_TRANSPORT_ERROR;
  787. }
  788. /* STATUS STAGE */
  789. /* NOTE: CB does not have a status stage. Silly, I know. So
  790. * we have to catch this at a higher level.
  791. */
  792. return USB_STOR_TRANSPORT_GOOD;
  793. }
  794. /*
  795. * Bulk only transport
  796. */
  797. /* Determine what the maximum LUN supported is */
  798. int usb_stor_Bulk_max_lun(struct us_data *us)
  799. {
  800. int result;
  801. /* issue the command */
  802. result = usb_stor_control_msg(us, us->recv_ctrl_pipe,
  803. US_BULK_GET_MAX_LUN,
  804. USB_DIR_IN | USB_TYPE_CLASS |
  805. USB_RECIP_INTERFACE,
  806. 0, us->ifnum, us->iobuf, 1, HZ);
  807. US_DEBUGP("GetMaxLUN command result is %d, data is %d\n",
  808. result, us->iobuf[0]);
  809. /* if we have a successful request, return the result */
  810. if (result > 0)
  811. return us->iobuf[0];
  812. /*
  813. * Some devices (i.e. Iomega Zip100) need this -- apparently
  814. * the bulk pipes get STALLed when the GetMaxLUN request is
  815. * processed. This is, in theory, harmless to all other devices
  816. * (regardless of if they stall or not).
  817. */
  818. if (result == -EPIPE) {
  819. usb_stor_clear_halt(us, us->recv_bulk_pipe);
  820. usb_stor_clear_halt(us, us->send_bulk_pipe);
  821. }
  822. /*
  823. * Some devices don't like GetMaxLUN. They may STALL the control
  824. * pipe, they may return a zero-length result, they may do nothing at
  825. * all and timeout, or they may fail in even more bizarrely creative
  826. * ways. In these cases the best approach is to use the default
  827. * value: only one LUN.
  828. */
  829. return 0;
  830. }
  831. int usb_stor_Bulk_transport(struct scsi_cmnd *srb, struct us_data *us)
  832. {
  833. struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
  834. struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
  835. unsigned int transfer_length = srb->request_bufflen;
  836. unsigned int residue;
  837. int result;
  838. int fake_sense = 0;
  839. unsigned int cswlen;
  840. unsigned int cbwlen = US_BULK_CB_WRAP_LEN;
  841. /* Take care of BULK32 devices; set extra byte to 0 */
  842. if ( unlikely(us->flags & US_FL_BULK32)) {
  843. cbwlen = 32;
  844. us->iobuf[31] = 0;
  845. }
  846. /* set up the command wrapper */
  847. bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
  848. bcb->DataTransferLength = cpu_to_le32(transfer_length);
  849. bcb->Flags = srb->sc_data_direction == DMA_FROM_DEVICE ? 1 << 7 : 0;
  850. bcb->Tag = srb->serial_number;
  851. bcb->Lun = srb->device->lun;
  852. if (us->flags & US_FL_SCM_MULT_TARG)
  853. bcb->Lun |= srb->device->id << 4;
  854. bcb->Length = srb->cmd_len;
  855. /* copy the command payload */
  856. memset(bcb->CDB, 0, sizeof(bcb->CDB));
  857. memcpy(bcb->CDB, srb->cmnd, bcb->Length);
  858. /* send it to out endpoint */
  859. US_DEBUGP("Bulk Command S 0x%x T 0x%x L %d F %d Trg %d LUN %d CL %d\n",
  860. le32_to_cpu(bcb->Signature), bcb->Tag,
  861. le32_to_cpu(bcb->DataTransferLength), bcb->Flags,
  862. (bcb->Lun >> 4), (bcb->Lun & 0x0F),
  863. bcb->Length);
  864. result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  865. bcb, cbwlen, NULL);
  866. US_DEBUGP("Bulk command transfer result=%d\n", result);
  867. if (result != USB_STOR_XFER_GOOD)
  868. return USB_STOR_TRANSPORT_ERROR;
  869. /* DATA STAGE */
  870. /* send/receive data payload, if there is any */
  871. /* Some USB-IDE converter chips need a 100us delay between the
  872. * command phase and the data phase. Some devices need a little
  873. * more than that, probably because of clock rate inaccuracies. */
  874. if (unlikely(us->flags & US_FL_GO_SLOW))
  875. udelay(125);
  876. if (transfer_length) {
  877. unsigned int pipe = srb->sc_data_direction == DMA_FROM_DEVICE ?
  878. us->recv_bulk_pipe : us->send_bulk_pipe;
  879. result = usb_stor_bulk_transfer_sg(us, pipe,
  880. srb->request_buffer, transfer_length,
  881. srb->use_sg, &srb->resid);
  882. US_DEBUGP("Bulk data transfer result 0x%x\n", result);
  883. if (result == USB_STOR_XFER_ERROR)
  884. return USB_STOR_TRANSPORT_ERROR;
  885. /* If the device tried to send back more data than the
  886. * amount requested, the spec requires us to transfer
  887. * the CSW anyway. Since there's no point retrying the
  888. * the command, we'll return fake sense data indicating
  889. * Illegal Request, Invalid Field in CDB.
  890. */
  891. if (result == USB_STOR_XFER_LONG)
  892. fake_sense = 1;
  893. }
  894. /* See flow chart on pg 15 of the Bulk Only Transport spec for
  895. * an explanation of how this code works.
  896. */
  897. /* get CSW for device status */
  898. US_DEBUGP("Attempting to get CSW...\n");
  899. result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  900. bcs, US_BULK_CS_WRAP_LEN, &cswlen);
  901. /* Some broken devices add unnecessary zero-length packets to the
  902. * end of their data transfers. Such packets show up as 0-length
  903. * CSWs. If we encounter such a thing, try to read the CSW again.
  904. */
  905. if (result == USB_STOR_XFER_SHORT && cswlen == 0) {
  906. US_DEBUGP("Received 0-length CSW; retrying...\n");
  907. result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  908. bcs, US_BULK_CS_WRAP_LEN, &cswlen);
  909. }
  910. /* did the attempt to read the CSW fail? */
  911. if (result == USB_STOR_XFER_STALLED) {
  912. /* get the status again */
  913. US_DEBUGP("Attempting to get CSW (2nd try)...\n");
  914. result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  915. bcs, US_BULK_CS_WRAP_LEN, NULL);
  916. }
  917. /* if we still have a failure at this point, we're in trouble */
  918. US_DEBUGP("Bulk status result = %d\n", result);
  919. if (result != USB_STOR_XFER_GOOD)
  920. return USB_STOR_TRANSPORT_ERROR;
  921. /* check bulk status */
  922. residue = le32_to_cpu(bcs->Residue);
  923. US_DEBUGP("Bulk Status S 0x%x T 0x%x R %u Stat 0x%x\n",
  924. le32_to_cpu(bcs->Signature), bcs->Tag,
  925. residue, bcs->Status);
  926. if (bcs->Tag != srb->serial_number || bcs->Status > US_BULK_STAT_PHASE) {
  927. US_DEBUGP("Bulk logical error\n");
  928. return USB_STOR_TRANSPORT_ERROR;
  929. }
  930. /* Some broken devices report odd signatures, so we do not check them
  931. * for validity against the spec. We store the first one we see,
  932. * and check subsequent transfers for validity against this signature.
  933. */
  934. if (!us->bcs_signature) {
  935. us->bcs_signature = bcs->Signature;
  936. if (us->bcs_signature != cpu_to_le32(US_BULK_CS_SIGN))
  937. US_DEBUGP("Learnt BCS signature 0x%08X\n",
  938. le32_to_cpu(us->bcs_signature));
  939. } else if (bcs->Signature != us->bcs_signature) {
  940. US_DEBUGP("Signature mismatch: got %08X, expecting %08X\n",
  941. le32_to_cpu(bcs->Signature),
  942. le32_to_cpu(us->bcs_signature));
  943. return USB_STOR_TRANSPORT_ERROR;
  944. }
  945. /* try to compute the actual residue, based on how much data
  946. * was really transferred and what the device tells us */
  947. if (residue) {
  948. if (!(us->flags & US_FL_IGNORE_RESIDUE)) {
  949. residue = min(residue, transfer_length);
  950. srb->resid = max(srb->resid, (int) residue);
  951. }
  952. }
  953. /* based on the status code, we report good or bad */
  954. switch (bcs->Status) {
  955. case US_BULK_STAT_OK:
  956. /* device babbled -- return fake sense data */
  957. if (fake_sense) {
  958. memcpy(srb->sense_buffer,
  959. usb_stor_sense_invalidCDB,
  960. sizeof(usb_stor_sense_invalidCDB));
  961. return USB_STOR_TRANSPORT_NO_SENSE;
  962. }
  963. /* command good -- note that data could be short */
  964. return USB_STOR_TRANSPORT_GOOD;
  965. case US_BULK_STAT_FAIL:
  966. /* command failed */
  967. return USB_STOR_TRANSPORT_FAILED;
  968. case US_BULK_STAT_PHASE:
  969. /* phase error -- note that a transport reset will be
  970. * invoked by the invoke_transport() function
  971. */
  972. return USB_STOR_TRANSPORT_ERROR;
  973. }
  974. /* we should never get here, but if we do, we're in trouble */
  975. return USB_STOR_TRANSPORT_ERROR;
  976. }
  977. /***********************************************************************
  978. * Reset routines
  979. ***********************************************************************/
  980. /* This is the common part of the device reset code.
  981. *
  982. * It's handy that every transport mechanism uses the control endpoint for
  983. * resets.
  984. *
  985. * Basically, we send a reset with a 5-second timeout, so we don't get
  986. * jammed attempting to do the reset.
  987. */
  988. static int usb_stor_reset_common(struct us_data *us,
  989. u8 request, u8 requesttype,
  990. u16 value, u16 index, void *data, u16 size)
  991. {
  992. int result;
  993. int result2;
  994. if (test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {
  995. US_DEBUGP("No reset during disconnect\n");
  996. return -EIO;
  997. }
  998. result = usb_stor_control_msg(us, us->send_ctrl_pipe,
  999. request, requesttype, value, index, data, size,
  1000. 5*HZ);
  1001. if (result < 0) {
  1002. US_DEBUGP("Soft reset failed: %d\n", result);
  1003. return result;
  1004. }
  1005. /* Give the device some time to recover from the reset,
  1006. * but don't delay disconnect processing. */
  1007. wait_event_interruptible_timeout(us->delay_wait,
  1008. test_bit(US_FLIDX_DISCONNECTING, &us->flags),
  1009. HZ*6);
  1010. if (test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {
  1011. US_DEBUGP("Reset interrupted by disconnect\n");
  1012. return -EIO;
  1013. }
  1014. US_DEBUGP("Soft reset: clearing bulk-in endpoint halt\n");
  1015. result = usb_stor_clear_halt(us, us->recv_bulk_pipe);
  1016. US_DEBUGP("Soft reset: clearing bulk-out endpoint halt\n");
  1017. result2 = usb_stor_clear_halt(us, us->send_bulk_pipe);
  1018. /* return a result code based on the result of the clear-halts */
  1019. if (result >= 0)
  1020. result = result2;
  1021. if (result < 0)
  1022. US_DEBUGP("Soft reset failed\n");
  1023. else
  1024. US_DEBUGP("Soft reset done\n");
  1025. return result;
  1026. }
  1027. /* This issues a CB[I] Reset to the device in question
  1028. */
  1029. #define CB_RESET_CMD_SIZE 12
  1030. int usb_stor_CB_reset(struct us_data *us)
  1031. {
  1032. US_DEBUGP("%s called\n", __FUNCTION__);
  1033. memset(us->iobuf, 0xFF, CB_RESET_CMD_SIZE);
  1034. us->iobuf[0] = SEND_DIAGNOSTIC;
  1035. us->iobuf[1] = 4;
  1036. return usb_stor_reset_common(us, US_CBI_ADSC,
  1037. USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  1038. 0, us->ifnum, us->iobuf, CB_RESET_CMD_SIZE);
  1039. }
  1040. /* This issues a Bulk-only Reset to the device in question, including
  1041. * clearing the subsequent endpoint halts that may occur.
  1042. */
  1043. int usb_stor_Bulk_reset(struct us_data *us)
  1044. {
  1045. US_DEBUGP("%s called\n", __FUNCTION__);
  1046. return usb_stor_reset_common(us, US_BULK_RESET_REQUEST,
  1047. USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  1048. 0, us->ifnum, NULL, 0);
  1049. }
  1050. /* Issue a USB port reset to the device. But don't do anything if
  1051. * there's more than one interface in the device, so that other users
  1052. * are not affected. */
  1053. int usb_stor_port_reset(struct us_data *us)
  1054. {
  1055. int result, rc;
  1056. if (test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {
  1057. result = -EIO;
  1058. US_DEBUGP("No reset during disconnect\n");
  1059. } else if (us->pusb_dev->actconfig->desc.bNumInterfaces != 1) {
  1060. result = -EBUSY;
  1061. US_DEBUGP("Refusing to reset a multi-interface device\n");
  1062. } else {
  1063. result = rc =
  1064. usb_lock_device_for_reset(us->pusb_dev, us->pusb_intf);
  1065. if (result < 0) {
  1066. US_DEBUGP("unable to lock device for reset: %d\n",
  1067. result);
  1068. } else {
  1069. result = usb_reset_device(us->pusb_dev);
  1070. if (rc)
  1071. usb_unlock_device(us->pusb_dev);
  1072. US_DEBUGP("usb_reset_device returns %d\n", result);
  1073. }
  1074. }
  1075. return result;
  1076. }