transport.c 38 KB

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