transport.c 37 KB

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