transport.c 37 KB

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