transport.c 37 KB

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