transport.c 38 KB

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