transport.c 38 KB

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