transport.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:37k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* Driver for USB Mass Storage compliant devices
  2.  *
  3.  * $Id: transport.c,v 1.44 2002/02/25 00:43:41 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 "transport.h"
  49. #include "protocol.h"
  50. #include "usb.h"
  51. #include "debug.h"
  52. #include <linux/sched.h>
  53. #include <linux/errno.h>
  54. #include <linux/slab.h>
  55. /***********************************************************************
  56.  * Helper routines
  57.  ***********************************************************************/
  58. /* Calculate the length of the data transfer (not the command) for any
  59.  * given SCSI command
  60.  */
  61. unsigned int usb_stor_transfer_length(Scsi_Cmnd *srb)
  62. {
  63.        int i;
  64. int doDefault = 0;
  65. unsigned int len = 0;
  66. unsigned int total = 0;
  67. struct scatterlist *sg;
  68. /* This table tells us:
  69.    X = command not supported
  70.    L = return length in cmnd[4] (8 bits).
  71.    M = return length in cmnd[8] (8 bits).
  72.    G = return length in cmnd[3] and cmnd[4] (16 bits)
  73.    H = return length in cmnd[7] and cmnd[8] (16 bits)
  74.    I = return length in cmnd[8] and cmnd[9] (16 bits)
  75.    C = return length in cmnd[2] to cmnd[5] (32 bits)
  76.    D = return length in cmnd[6] to cmnd[9] (32 bits)
  77.    B = return length in blocksize so we use buff_len
  78.    R = return length in cmnd[2] to cmnd[4] (24 bits)
  79.    S = return length in cmnd[3] to cmnd[5] (24 bits)
  80.    T = return length in cmnd[6] to cmnd[8] (24 bits)
  81.    U = return length in cmnd[7] to cmnd[9] (24 bits)
  82.    0-9 = fixed return length
  83.    V = 20 bytes
  84.    W = 24 bytes
  85.    Z = return length is mode dependant or not in command, use buff_len
  86. */
  87. static char *lengths =
  88.       /* 0123456789ABCDEF   0123456789ABCDEF */
  89. "00XLZ6XZBXBBXXXB" "00LBBLG0R0L0GG0X"  /* 00-1F */
  90. "XXXXT8XXB4B0BBBB" "ZZZ0B00HCSSZTBHH"  /* 20-3F */
  91. "M0HHB0X000H0HH0X" "XHH0HHXX0TH0H0XX"  /* 40-5F */
  92. "XXXXXXXXXXXXXXXX" "XXXXXXXXXXXXXXXX"  /* 60-7F */
  93. "XXXXXXXXXXXXXXXX" "XXXXXXXXXXXXXXXX"  /* 80-9F */
  94. "X0XXX00XB0BXBXBB" "ZZZ0XUIDU000XHBX"  /* A0-BF */
  95. "XXXXXXXXXXXXXXXX" "XXXXXXXXXXXXXXXX"  /* C0-DF */
  96. "XDXXXXXXXXXXXXXX" "XXW00HXXXXXXXXXX"; /* E0-FF */
  97. /* Commands checked in table:
  98.    CHANGE_DEFINITION 40
  99.    COMPARE 39
  100.    COPY 18
  101.    COPY_AND_VERIFY 3a
  102.    ERASE 19
  103.    ERASE_10 2c
  104.    ERASE_12 ac
  105.    EXCHANGE_MEDIUM a6
  106.    FORMAT_UNIT 04
  107.    GET_DATA_BUFFER_STATUS 34
  108.    GET_MESSAGE_10 28
  109.    GET_MESSAGE_12 a8
  110.    GET_WINDOW 25   !!! Has more data than READ_CAPACITY, need to fix table
  111.    INITIALIZE_ELEMENT_STATUS 07 !!! REASSIGN_BLOCKS luckily uses buff_len
  112.    INQUIRY 12
  113.    LOAD_UNLOAD 1b
  114.    LOCATE 2b
  115.    LOCK_UNLOCK_CACHE 36
  116.    LOG_SELECT 4c
  117.    LOG_SENSE 4d
  118.    MEDIUM_SCAN 38     !!! This was M
  119.    MODE_SELECT6 15
  120.    MODE_SELECT_10 55
  121.    MODE_SENSE_6 1a
  122.    MODE_SENSE_10 5a
  123.    MOVE_MEDIUM a5
  124.    OBJECT_POSITION 31  !!! Same as SEARCH_DATA_EQUAL
  125.    PAUSE_RESUME 4b
  126.    PLAY_AUDIO_10 45
  127.    PLAY_AUDIO_12 a5
  128.    PLAY_AUDIO_MSF 47
  129.    PLAY_AUDIO_TRACK_INDEX 48
  130.        PLAY_AUDIO_TRACK_RELATIVE_10 49
  131.    PLAY_AUDIO_TRACK_RELATIVE_12 a9
  132.    POSITION_TO_ELEMENT 2b
  133.           PRE-FETCH 34
  134.    PREVENT_ALLOW_MEDIUM_REMOVAL 1e
  135.    PRINT 0a             !!! Same as WRITE_6 but is always in bytes
  136.    READ_6 08
  137.    READ_10 28
  138.    READ_12 a8
  139.    READ_BLOCK_LIMITS 05
  140.    READ_BUFFER 3c
  141.    READ_CAPACITY 25
  142.    READ_CDROM_CAPACITY 25
  143.    READ_DEFECT_DATA 37
  144.    READ_DEFECT_DATA_12 b7
  145.    READ_ELEMENT_STATUS b8 !!! Think this is in bytes
  146.    READ_GENERATION 29 !!! Could also be M?
  147.    READ_HEADER 44     !!! This was L
  148.    READ_LONG 3e
  149.    READ_POSITION 34   !!! This should be V but conflicts with PRE-FETCH
  150.    READ_REVERSE 0f
  151.    READ_SUB-CHANNEL 42 !!! Is this in bytes?
  152.    READ_TOC 43         !!! Is this in bytes?
  153.    READ_UPDATED_BLOCK 2d
  154.    REASSIGN_BLOCKS 07
  155.    RECEIVE 08        !!! Same as READ_6 probably in bytes though
  156.    RECEIVE_DIAGNOSTIC_RESULTS 1c
  157.    RECOVER_BUFFERED_DATA 14 !!! For PRINTERs this is bytes
  158.    RELEASE_UNIT 17
  159.    REQUEST_SENSE 03
  160.    REQUEST_VOLUME_ELEMENT_ADDRESS b5 !!! Think this is in bytes
  161.    RESERVE_UNIT 16
  162.    REWIND 01
  163.    REZERO_UNIT 01
  164.    SCAN 1b          !!! Conflicts with various commands, should be L
  165.    SEARCH_DATA_EQUAL 31
  166.    SEARCH_DATA_EQUAL_12 b1
  167.    SEARCH_DATA_LOW 30
  168.    SEARCH_DATA_LOW_12 b0
  169.    SEARCH_DATA_HIGH 32
  170.    SEARCH_DATA_HIGH_12 b2
  171.    SEEK_6 0b         !!! Conflicts with SLEW_AND_PRINT
  172.    SEEK_10 2b
  173.    SEND 0a           !!! Same as WRITE_6, probably in bytes though
  174.    SEND 2a           !!! Similar to WRITE_10 but for scanners
  175.    SEND_DIAGNOSTIC 1d
  176.    SEND_MESSAGE_6 0a   !!! Same as WRITE_6 - is in bytes
  177.    SEND_MESSAGE_10 2a  !!! Same as WRITE_10 - is in bytes
  178.    SEND_MESSAGE_12 aa  !!! Same as WRITE_12 - is in bytes
  179.    SEND_OPC 54
  180.    SEND_VOLUME_TAG b6 !!! Think this is in bytes
  181.    SET_LIMITS 33
  182.    SET_LIMITS_12 b3
  183.    SET_WINDOW 24
  184.    SLEW_AND_PRINT 0b !!! Conflicts with SEEK_6
  185.    SPACE 11
  186.    START_STOP_UNIT 1b
  187.    STOP_PRINT 1b
  188.    SYNCHRONIZE_BUFFER 10
  189.    SYNCHRONIZE_CACHE 35
  190.    TEST_UNIT_READY 00
  191.    UPDATE_BLOCK 3d
  192.    VERIFY 13
  193.    VERIFY 2f
  194.    VERIFY_12 af
  195.    WRITE_6 0a
  196.    WRITE_10 2a
  197.    WRITE_12 aa
  198.    WRITE_AND_VERIFY 2e
  199.    WRITE_AND_VERIFY_12 ae
  200.    WRITE_BUFFER 3b
  201.    WRITE_FILEMARKS 10
  202.    WRITE_LONG 3f
  203.    WRITE_SAME 41
  204. */
  205. if (srb->sc_data_direction == SCSI_DATA_WRITE) {
  206. doDefault = 1;
  207. }
  208. else
  209. switch (lengths[srb->cmnd[0]]) {
  210. case 'L':
  211. len = srb->cmnd[4];
  212. break;
  213. case 'M':
  214. len = srb->cmnd[8];
  215. break;
  216. case '0':
  217. case '1':
  218. case '2':
  219. case '3':
  220. case '4':
  221. case '5':
  222. case '6':
  223. case '7':
  224. case '8':
  225. case '9':
  226. len = lengths[srb->cmnd[0]]-'0';
  227. break;
  228. case 'G':
  229. len = (((unsigned int)srb->cmnd[3])<<8) |
  230. srb->cmnd[4];
  231. break;
  232. case 'H':
  233. len = (((unsigned int)srb->cmnd[7])<<8) |
  234. srb->cmnd[8];
  235. break;
  236. case 'I':
  237. len = (((unsigned int)srb->cmnd[8])<<8) |
  238. srb->cmnd[9];
  239. break;
  240. case 'R':
  241. len = (((unsigned int)srb->cmnd[2])<<16) |
  242. (((unsigned int)srb->cmnd[3])<<8) |
  243. srb->cmnd[4];
  244. break;
  245. case 'S':
  246. len = (((unsigned int)srb->cmnd[3])<<16) |
  247. (((unsigned int)srb->cmnd[4])<<8) |
  248. srb->cmnd[5];
  249. break;
  250. case 'T':
  251. len = (((unsigned int)srb->cmnd[6])<<16) |
  252. (((unsigned int)srb->cmnd[7])<<8) |
  253. srb->cmnd[8];
  254. break;
  255. case 'U':
  256. len = (((unsigned int)srb->cmnd[7])<<16) |
  257. (((unsigned int)srb->cmnd[8])<<8) |
  258. srb->cmnd[9];
  259. break;
  260. case 'C':
  261. len = (((unsigned int)srb->cmnd[2])<<24) |
  262. (((unsigned int)srb->cmnd[3])<<16) |
  263. (((unsigned int)srb->cmnd[4])<<8) |
  264. srb->cmnd[5];
  265. break;
  266. case 'D':
  267. len = (((unsigned int)srb->cmnd[6])<<24) |
  268. (((unsigned int)srb->cmnd[7])<<16) |
  269. (((unsigned int)srb->cmnd[8])<<8) |
  270. srb->cmnd[9];
  271. break;
  272. case 'V':
  273. len = 20;
  274. break;
  275. case 'W':
  276. len = 24;
  277. break;
  278. case 'B':
  279. /* Use buffer size due to different block sizes */
  280. doDefault = 1;
  281. break;
  282. case 'X':
  283. US_DEBUGP("Error: UNSUPPORTED COMMAND %02Xn",
  284. srb->cmnd[0]);
  285. doDefault = 1;
  286. break;
  287. case 'Z':
  288. /* Use buffer size due to mode dependence */
  289. doDefault = 1;
  290. break;
  291. default:
  292. US_DEBUGP("Error: COMMAND %02X out of range or table inconsistent (%c).n",
  293. srb->cmnd[0], lengths[srb->cmnd[0]] );
  294. doDefault = 1;
  295. }
  296.    
  297.    if ( doDefault == 1 ) {
  298.    /* Are we going to scatter gather? */
  299.    if (srb->use_sg) {
  300.    /* Add up the sizes of all the sg segments */
  301.    sg = (struct scatterlist *) srb->request_buffer;
  302.    for (i = 0; i < srb->use_sg; i++)
  303.    total += sg[i].length;
  304.    len = total;
  305.    }
  306.    else
  307.    /* Just return the length of the buffer */
  308.    len = srb->request_bufflen;
  309.    }
  310. return len;
  311. }
  312. /***********************************************************************
  313.  * Data transfer routines
  314.  ***********************************************************************/
  315. /* This is the completion handler which will wake us up when an URB
  316.  * completes.
  317.  */
  318. static void usb_stor_blocking_completion(struct urb *urb)
  319. {
  320. struct completion *urb_done_ptr = (struct completion *)urb->context;
  321. complete(urb_done_ptr);
  322. }
  323. /* This is our function to emulate usb_control_msg() but give us enough
  324.  * access to make aborts/resets work
  325.  */
  326. int usb_stor_control_msg(struct us_data *us, unsigned int pipe,
  327.  u8 request, u8 requesttype, u16 value, u16 index, 
  328.  void *data, u16 size)
  329. {
  330. int status;
  331. struct usb_ctrlrequest *dr;
  332. /* allocate the device request structure */
  333. dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO);
  334. if (!dr)
  335. return -ENOMEM;
  336. /* fill in the structure */
  337. dr->bRequestType = requesttype;
  338. dr->bRequest = request;
  339. dr->wValue = cpu_to_le16(value);
  340. dr->wIndex = cpu_to_le16(index);
  341. dr->wLength = cpu_to_le16(size);
  342. /* set up data structures for the wakeup system */
  343. init_completion(&us->current_done);
  344. /* lock the URB */
  345. down(&(us->current_urb_sem));
  346. /* fill the URB */
  347. FILL_CONTROL_URB(us->current_urb, us->pusb_dev, pipe, 
  348.  (unsigned char*) dr, data, size, 
  349.  usb_stor_blocking_completion, &us->current_done);
  350. us->current_urb->actual_length = 0;
  351. us->current_urb->error_count = 0;
  352. us->current_urb->transfer_flags = USB_ASYNC_UNLINK;
  353. /* submit the URB */
  354. status = usb_submit_urb(us->current_urb);
  355. if (status) {
  356. /* something went wrong */
  357. up(&(us->current_urb_sem));
  358. kfree(dr);
  359. return status;
  360. }
  361. /* wait for the completion of the URB */
  362. up(&(us->current_urb_sem));
  363. wait_for_completion(&us->current_done);
  364. down(&(us->current_urb_sem));
  365. /* return the actual length of the data transferred if no error*/
  366. status = us->current_urb->status;
  367. if (status >= 0)
  368. status = us->current_urb->actual_length;
  369. /* release the lock and return status */
  370. up(&(us->current_urb_sem));
  371. kfree(dr);
  372.    return status;
  373. }
  374. /* This is our function to emulate usb_bulk_msg() but give us enough
  375.  * access to make aborts/resets work
  376.  */
  377. int usb_stor_bulk_msg(struct us_data *us, void *data, int pipe,
  378.       unsigned int len, unsigned int *act_len)
  379. {
  380. int status;
  381. /* set up data structures for the wakeup system */
  382. init_completion(&us->current_done);
  383. /* lock the URB */
  384. down(&(us->current_urb_sem));
  385. /* fill the URB */
  386. FILL_BULK_URB(us->current_urb, us->pusb_dev, pipe, data, len,
  387.       usb_stor_blocking_completion, &us->current_done);
  388. us->current_urb->actual_length = 0;
  389. us->current_urb->error_count = 0;
  390. us->current_urb->transfer_flags = USB_ASYNC_UNLINK;
  391. /* submit the URB */
  392. status = usb_submit_urb(us->current_urb);
  393. if (status) {
  394. /* something went wrong */
  395. up(&(us->current_urb_sem));
  396. return status;
  397. }
  398. /* wait for the completion of the URB */
  399. up(&(us->current_urb_sem));
  400. wait_for_completion(&us->current_done);
  401. down(&(us->current_urb_sem));
  402. /* return the actual length of the data transferred */
  403. *act_len = us->current_urb->actual_length;
  404. /* release the lock and return status */
  405. up(&(us->current_urb_sem));
  406. return us->current_urb->status;
  407. }
  408. /* This is a version of usb_clear_halt() that doesn't read the status from
  409.  * the device -- this is because some devices crash their internal firmware
  410.  * when the status is requested after a halt
  411.  */
  412. int usb_stor_clear_halt(struct us_data *us, int pipe)
  413. {
  414. int result;
  415. int endp = usb_pipeendpoint(pipe) | (usb_pipein(pipe) << 7);
  416. result = usb_stor_control_msg(us,
  417. usb_sndctrlpipe(us->pusb_dev, 0),
  418. USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT, 0,
  419. endp, NULL, 0); /* note: no 3*HZ timeout */
  420. US_DEBUGP("usb_stor_clear_halt: result=%dn", result);
  421. /* this is a failure case */
  422. if (result < 0)
  423. return result;
  424. /* reset the toggles and endpoint flags */
  425. usb_endpoint_running(us->pusb_dev, usb_pipeendpoint(pipe),
  426. usb_pipeout(pipe));
  427. usb_settoggle(us->pusb_dev, usb_pipeendpoint(pipe),
  428. usb_pipeout(pipe), 0);
  429. return 0;
  430. }
  431. /*
  432.  * Transfer one SCSI scatter-gather buffer via bulk transfer
  433.  *
  434.  * Note that this function is necessary because we want the ability to
  435.  * use scatter-gather memory.  Good performance is achieved by a combination
  436.  * of scatter-gather and clustering (which makes each chunk bigger).
  437.  *
  438.  * Note that the lower layer will always retry when a NAK occurs, up to the
  439.  * timeout limit.  Thus we don't have to worry about it for individual
  440.  * packets.
  441.  */
  442. int usb_stor_transfer_partial(struct us_data *us, char *buf, int length)
  443. {
  444. int result;
  445. int partial;
  446. int pipe;
  447. /* calculate the appropriate pipe information */
  448. if (us->srb->sc_data_direction == SCSI_DATA_READ)
  449. pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
  450. else
  451. pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
  452. /* transfer the data */
  453. US_DEBUGP("usb_stor_transfer_partial(): xfer %d bytesn", length);
  454. result = usb_stor_bulk_msg(us, buf, pipe, length, &partial);
  455. US_DEBUGP("usb_stor_bulk_msg() returned %d xferred %d/%dn",
  456.   result, partial, length);
  457. /* if we stall, we need to clear it before we go on */
  458. if (result == -EPIPE) {
  459. US_DEBUGP("clearing endpoint halt for pipe 0x%xn", pipe);
  460. usb_stor_clear_halt(us, pipe);
  461. }
  462. /* did we abort this command? */
  463. if (result == -ENOENT) {
  464. US_DEBUGP("usb_stor_transfer_partial(): transfer abortedn");
  465. return US_BULK_TRANSFER_ABORTED;
  466. }
  467. /* did we send all the data? */
  468. if (partial == length) {
  469. US_DEBUGP("usb_stor_transfer_partial(): transfer completen");
  470. return US_BULK_TRANSFER_GOOD;
  471. }
  472. /* NAK - that means we've retried a few times already */
  473. if (result == -ETIMEDOUT) {
  474. US_DEBUGP("usb_stor_transfer_partial(): device NAKedn");
  475. return US_BULK_TRANSFER_FAILED;
  476. }
  477. /* the catch-all error case */
  478. if (result) {
  479. US_DEBUGP("usb_stor_transfer_partial(): unknown errorn");
  480. return US_BULK_TRANSFER_FAILED;
  481. }
  482. /* no error code, so we must have transferred some data, 
  483.  * just not all of it */
  484. return US_BULK_TRANSFER_SHORT;
  485. }
  486. /*
  487.  * Transfer an entire SCSI command's worth of data payload over the bulk
  488.  * pipe.
  489.  *
  490.  * Note that this uses usb_stor_transfer_partial to achieve its goals -- this
  491.  * function simply determines if we're going to use scatter-gather or not,
  492.  * and acts appropriately.  For now, it also re-interprets the error codes.
  493.  */
  494. void usb_stor_transfer(Scsi_Cmnd *srb, struct us_data* us)
  495. {
  496. int i;
  497. int result = -1;
  498. struct scatterlist *sg;
  499. unsigned int total_transferred = 0;
  500. unsigned int transfer_amount;
  501. /* calculate how much we want to transfer */
  502. transfer_amount = usb_stor_transfer_length(srb);
  503. /* was someone foolish enough to request more data than available
  504.  * buffer space? */
  505. if (transfer_amount > srb->request_bufflen)
  506. transfer_amount = srb->request_bufflen;
  507. /* are we scatter-gathering? */
  508. if (srb->use_sg) {
  509. /* loop over all the scatter gather structures and 
  510.  * make the appropriate requests for each, until done
  511.  */
  512. sg = (struct scatterlist *) srb->request_buffer;
  513. for (i = 0; i < srb->use_sg; i++) {
  514. /* transfer the lesser of the next buffer or the
  515.  * remaining data */
  516. if (transfer_amount - total_transferred >= 
  517. sg[i].length) {
  518. result = usb_stor_transfer_partial(us,
  519. sg[i].address, sg[i].length);
  520. total_transferred += sg[i].length;
  521. } else
  522. result = usb_stor_transfer_partial(us,
  523. sg[i].address,
  524. transfer_amount - total_transferred);
  525. /* if we get an error, end the loop here */
  526. if (result)
  527. break;
  528. }
  529. }
  530. else
  531. /* no scatter-gather, just make the request */
  532. result = usb_stor_transfer_partial(us, srb->request_buffer, 
  533.      transfer_amount);
  534. /* return the result in the data structure itself */
  535. srb->result = result;
  536. }
  537. /***********************************************************************
  538.  * Transport routines
  539.  ***********************************************************************/
  540. /* Invoke the transport and basic error-handling/recovery methods
  541.  *
  542.  * This is used by the protocol layers to actually send the message to
  543.  * the device and receive the response.
  544.  */
  545. void usb_stor_invoke_transport(Scsi_Cmnd *srb, struct us_data *us)
  546. {
  547. int need_auto_sense;
  548. int result;
  549. /* send the command to the transport layer */
  550. result = us->transport(srb, us);
  551. /* if the command gets aborted by the higher layers, we need to
  552.  * short-circuit all other processing
  553.  */
  554. if (result == USB_STOR_TRANSPORT_ABORTED) {
  555. US_DEBUGP("-- transport indicates command was abortedn");
  556. srb->result = DID_ABORT << 16;
  557. return;
  558. }
  559. /* if there is a transport error, reset and don't auto-sense */
  560. if (result == USB_STOR_TRANSPORT_ERROR) {
  561. US_DEBUGP("-- transport indicates error, resettingn");
  562. us->transport_reset(us);
  563. srb->result = DID_ERROR << 16;
  564. return;
  565. }
  566. /* Determine if we need to auto-sense
  567.  *
  568.  * I normally don't use a flag like this, but it's almost impossible
  569.  * to understand what's going on here if I don't.
  570.  */
  571. need_auto_sense = 0;
  572. /*
  573.  * If we're running the CB transport, which is incapable
  574.  * of determining status on it's own, we need to auto-sense almost
  575.  * every time.
  576.  */
  577. if (us->protocol == US_PR_CB || us->protocol == US_PR_DPCM_USB) {
  578. US_DEBUGP("-- CB transport device requiring auto-sensen");
  579. need_auto_sense = 1;
  580. /* There are some exceptions to this.  Notably, if this is
  581.  * a UFI device and the command is REQUEST_SENSE or INQUIRY,
  582.  * then it is impossible to truly determine status.
  583.  */
  584. if (us->subclass == US_SC_UFI &&
  585.     ((srb->cmnd[0] == REQUEST_SENSE) ||
  586.      (srb->cmnd[0] == INQUIRY))) {
  587. US_DEBUGP("** no auto-sense for a special commandn");
  588. need_auto_sense = 0;
  589. }
  590. }
  591. /*
  592.  * If we have a failure, we're going to do a REQUEST_SENSE 
  593.  * automatically.  Note that we differentiate between a command
  594.  * "failure" and an "error" in the transport mechanism.
  595.  */
  596. if (result == USB_STOR_TRANSPORT_FAILED) {
  597. US_DEBUGP("-- transport indicates command failuren");
  598. need_auto_sense = 1;
  599. }
  600. /*
  601.  * Also, if we have a short transfer on a command that can't have
  602.  * a short transfer, we're going to do this.
  603.  */
  604. if ((srb->result == US_BULK_TRANSFER_SHORT) &&
  605.     !((srb->cmnd[0] == REQUEST_SENSE) ||
  606.       (srb->cmnd[0] == INQUIRY) ||
  607.       (srb->cmnd[0] == MODE_SENSE) ||
  608.       (srb->cmnd[0] == LOG_SENSE) ||
  609.       (srb->cmnd[0] == MODE_SENSE_10))) {
  610. US_DEBUGP("-- unexpectedly short transfern");
  611. need_auto_sense = 1;
  612. }
  613. /* Now, if we need to do the auto-sense, let's do it */
  614. if (need_auto_sense) {
  615. int temp_result;
  616. void* old_request_buffer;
  617. unsigned short old_sg;
  618. unsigned old_request_bufflen;
  619. unsigned char old_sc_data_direction;
  620. unsigned char old_cmnd[MAX_COMMAND_SIZE];
  621. US_DEBUGP("Issuing auto-REQUEST_SENSEn");
  622. /* save the old command */
  623. memcpy(old_cmnd, srb->cmnd, MAX_COMMAND_SIZE);
  624. /* set the command and the LUN */
  625. srb->cmnd[0] = REQUEST_SENSE;
  626. srb->cmnd[1] = old_cmnd[1] & 0xE0;
  627. srb->cmnd[2] = 0;
  628. srb->cmnd[3] = 0;
  629. srb->cmnd[4] = 18;
  630. srb->cmnd[5] = 0;
  631. /* set the transfer direction */
  632. old_sc_data_direction = srb->sc_data_direction;
  633. srb->sc_data_direction = SCSI_DATA_READ;
  634. /* use the new buffer we have */
  635. old_request_buffer = srb->request_buffer;
  636. srb->request_buffer = srb->sense_buffer;
  637. /* set the buffer length for transfer */
  638. old_request_bufflen = srb->request_bufflen;
  639. srb->request_bufflen = 18;
  640. /* set up for no scatter-gather use */
  641. old_sg = srb->use_sg;
  642. srb->use_sg = 0;
  643. /* issue the auto-sense command */
  644. temp_result = us->transport(us->srb, us);
  645. /* let's clean up right away */
  646. srb->request_buffer = old_request_buffer;
  647. srb->request_bufflen = old_request_bufflen;
  648. srb->use_sg = old_sg;
  649. srb->sc_data_direction = old_sc_data_direction;
  650. memcpy(srb->cmnd, old_cmnd, MAX_COMMAND_SIZE);
  651. if (temp_result == USB_STOR_TRANSPORT_ABORTED) {
  652. US_DEBUGP("-- auto-sense abortedn");
  653. srb->result = DID_ABORT << 16;
  654. return;
  655. }
  656. if (temp_result != USB_STOR_TRANSPORT_GOOD) {
  657. US_DEBUGP("-- auto-sense failuren");
  658. /* we skip the reset if this happens to be a
  659.  * multi-target device, since failure of an
  660.  * auto-sense is perfectly valid
  661.  */
  662. if (!(us->flags & US_FL_SCM_MULT_TARG)) {
  663. us->transport_reset(us);
  664. }
  665. srb->result = DID_ERROR << 16;
  666. return;
  667. }
  668. US_DEBUGP("-- Result from auto-sense is %dn", temp_result);
  669. US_DEBUGP("-- code: 0x%x, key: 0x%x, ASC: 0x%x, ASCQ: 0x%xn",
  670.   srb->sense_buffer[0],
  671.   srb->sense_buffer[2] & 0xf,
  672.   srb->sense_buffer[12], 
  673.   srb->sense_buffer[13]);
  674. #ifdef CONFIG_USB_STORAGE_DEBUG
  675. usb_stor_show_sense(
  676.   srb->sense_buffer[2] & 0xf,
  677.   srb->sense_buffer[12], 
  678.   srb->sense_buffer[13]);
  679. #endif
  680. /* set the result so the higher layers expect this data */
  681. srb->result = CHECK_CONDITION << 1;
  682. /* If things are really okay, then let's show that */
  683. if ((srb->sense_buffer[2] & 0xf) == 0x0)
  684. srb->result = GOOD << 1;
  685. } else /* if (need_auto_sense) */
  686. srb->result = GOOD << 1;
  687. /* Regardless of auto-sense, if we _know_ we have an error
  688.  * condition, show that in the result code
  689.  */
  690. if (result == USB_STOR_TRANSPORT_FAILED)
  691. srb->result = CHECK_CONDITION << 1;
  692. /* If we think we're good, then make sure the sense data shows it.
  693.  * This is necessary because the auto-sense for some devices always
  694.  * sets byte 0 == 0x70, even if there is no error
  695.  */
  696. if ((us->protocol == US_PR_CB || us->protocol == US_PR_DPCM_USB) && 
  697.     (result == USB_STOR_TRANSPORT_GOOD) &&
  698.     ((srb->sense_buffer[2] & 0xf) == 0x0))
  699. srb->sense_buffer[0] = 0x0;
  700. }
  701. /*
  702.  * Control/Bulk/Interrupt transport
  703.  */
  704. /* The interrupt handler for CBI devices */
  705. void usb_stor_CBI_irq(struct urb *urb)
  706. {
  707. struct us_data *us = (struct us_data *)urb->context;
  708. US_DEBUGP("USB IRQ received for device on host %dn", us->host_no);
  709. US_DEBUGP("-- IRQ data length is %dn", urb->actual_length);
  710. US_DEBUGP("-- IRQ state is %dn", urb->status);
  711. US_DEBUGP("-- Interrupt Status (0x%x, 0x%x)n",
  712. us->irqbuf[0], us->irqbuf[1]);
  713. /* reject improper IRQs */
  714. if (urb->actual_length != 2) {
  715. US_DEBUGP("-- IRQ too shortn");
  716. return;
  717. }
  718. /* is the device removed? */
  719. if (urb->status == -ENOENT) {
  720. US_DEBUGP("-- device has been removedn");
  721. return;
  722. }
  723. /* was this a command-completion interrupt? */
  724. if (us->irqbuf[0] && (us->subclass != US_SC_UFI)) {
  725. US_DEBUGP("-- not a command-completion IRQn");
  726. return;
  727. }
  728. /* was this a wanted interrupt? */
  729. if (!atomic_read(us->ip_wanted)) {
  730. US_DEBUGP("ERROR: Unwanted interrupt received!n");
  731. return;
  732. }
  733. /* adjust the flag */
  734. atomic_set(us->ip_wanted, 0);
  735. /* copy the valid data */
  736. us->irqdata[0] = us->irqbuf[0];
  737. us->irqdata[1] = us->irqbuf[1];
  738. /* wake up the command thread */
  739. US_DEBUGP("-- Current value of ip_waitq is: %dn",
  740. atomic_read(&us->ip_waitq.count));
  741. up(&(us->ip_waitq));
  742. }
  743. int usb_stor_CBI_transport(Scsi_Cmnd *srb, struct us_data *us)
  744. {
  745. int result;
  746. /* Set up for status notification */
  747. atomic_set(us->ip_wanted, 1);
  748. /* re-initialize the mutex so that we avoid any races with
  749.  * early/late IRQs from previous commands */
  750. init_MUTEX_LOCKED(&(us->ip_waitq));
  751. /* COMMAND STAGE */
  752. /* let's send the command via the control pipe */
  753. result = usb_stor_control_msg(us, usb_sndctrlpipe(us->pusb_dev,0),
  754.       US_CBI_ADSC, 
  755.       USB_TYPE_CLASS | USB_RECIP_INTERFACE, 0, 
  756.       us->ifnum, srb->cmnd, srb->cmd_len);
  757. /* check the return code for the command */
  758. US_DEBUGP("Call to usb_stor_control_msg() returned %dn", result);
  759. if (result < 0) {
  760. /* Reset flag for status notification */
  761. atomic_set(us->ip_wanted, 0);
  762. }
  763. /* if the command was aborted, indicate that */
  764. if (result == -ENOENT)
  765. return USB_STOR_TRANSPORT_ABORTED;
  766. /* STALL must be cleared when it is detected */
  767. if (result == -EPIPE) {
  768. US_DEBUGP("-- Stall on control pipe. Clearingn");
  769. result = usb_stor_clear_halt(us,
  770. usb_sndctrlpipe(us->pusb_dev, 0));
  771. /* if the command was aborted, indicate that */
  772. if (result == -ENOENT)
  773. return USB_STOR_TRANSPORT_ABORTED;
  774. return USB_STOR_TRANSPORT_FAILED;
  775. }
  776. if (result < 0) {
  777. /* Uh oh... serious problem here */
  778. return USB_STOR_TRANSPORT_ERROR;
  779. }
  780. /* DATA STAGE */
  781. /* transfer the data payload for this command, if one exists*/
  782. if (usb_stor_transfer_length(srb)) {
  783. usb_stor_transfer(srb, us);
  784. result = srb->result;
  785. US_DEBUGP("CBI data stage result is 0x%xn", result);
  786. /* report any errors */
  787. if (result == US_BULK_TRANSFER_ABORTED) {
  788. atomic_set(us->ip_wanted, 0);
  789. return USB_STOR_TRANSPORT_ABORTED;
  790. }
  791. if (result == US_BULK_TRANSFER_FAILED) {
  792. atomic_set(us->ip_wanted, 0);
  793. return USB_STOR_TRANSPORT_FAILED;
  794. }
  795. }
  796. /* STATUS STAGE */
  797. /* go to sleep until we get this interrupt */
  798. US_DEBUGP("Current value of ip_waitq is: %dn", atomic_read(&us->ip_waitq.count));
  799. down(&(us->ip_waitq));
  800. /* if we were woken up by an abort instead of the actual interrupt */
  801. if (atomic_read(us->ip_wanted)) {
  802. US_DEBUGP("Did not get interrupt on CBIn");
  803. atomic_set(us->ip_wanted, 0);
  804. return USB_STOR_TRANSPORT_ABORTED;
  805. }
  806. US_DEBUGP("Got interrupt data (0x%x, 0x%x)n", 
  807. us->irqdata[0], us->irqdata[1]);
  808. /* UFI gives us ASC and ASCQ, like a request sense
  809.  *
  810.  * REQUEST_SENSE and INQUIRY don't affect the sense data on UFI
  811.  * devices, so we ignore the information for those commands.  Note
  812.  * that this means we could be ignoring a real error on these
  813.  * commands, but that can't be helped.
  814.  */
  815. if (us->subclass == US_SC_UFI) {
  816. if (srb->cmnd[0] == REQUEST_SENSE ||
  817.     srb->cmnd[0] == INQUIRY)
  818. return USB_STOR_TRANSPORT_GOOD;
  819. else
  820. if (((unsigned char*)us->irq_urb->transfer_buffer)[0])
  821. return USB_STOR_TRANSPORT_FAILED;
  822. else
  823. return USB_STOR_TRANSPORT_GOOD;
  824. }
  825. /* If not UFI, we interpret the data as a result code 
  826.  * The first byte should always be a 0x0
  827.  * The second byte & 0x0F should be 0x0 for good, otherwise error 
  828.  */
  829. if (us->irqdata[0]) {
  830. US_DEBUGP("CBI IRQ data showed reserved bType %dn",
  831. us->irqdata[0]);
  832. return USB_STOR_TRANSPORT_ERROR;
  833. }
  834. switch (us->irqdata[1] & 0x0F) {
  835. case 0x00: 
  836. return USB_STOR_TRANSPORT_GOOD;
  837. case 0x01: 
  838. return USB_STOR_TRANSPORT_FAILED;
  839. default: 
  840. return USB_STOR_TRANSPORT_ERROR;
  841. }
  842. /* we should never get here, but if we do, we're in trouble */
  843. return USB_STOR_TRANSPORT_ERROR;
  844. }
  845. /*
  846.  * Control/Bulk transport
  847.  */
  848. int usb_stor_CB_transport(Scsi_Cmnd *srb, struct us_data *us)
  849. {
  850. int result;
  851. /* COMMAND STAGE */
  852. /* let's send the command via the control pipe */
  853. result = usb_stor_control_msg(us, usb_sndctrlpipe(us->pusb_dev,0),
  854.       US_CBI_ADSC, 
  855.       USB_TYPE_CLASS | USB_RECIP_INTERFACE, 0, 
  856.       us->ifnum, srb->cmnd, srb->cmd_len);
  857. /* check the return code for the command */
  858. US_DEBUGP("Call to usb_stor_control_msg() returned %dn", result);
  859. if (result < 0) {
  860. /* if the command was aborted, indicate that */
  861. if (result == -ENOENT)
  862. return USB_STOR_TRANSPORT_ABORTED;
  863. /* a stall is a fatal condition from the device */
  864. if (result == -EPIPE) {
  865. US_DEBUGP("-- Stall on control pipe. Clearingn");
  866. result = usb_stor_clear_halt(us,
  867. usb_sndctrlpipe(us->pusb_dev, 0));
  868. /* if the command was aborted, indicate that */
  869. if (result == -ENOENT)
  870. return USB_STOR_TRANSPORT_ABORTED;
  871. return USB_STOR_TRANSPORT_FAILED;
  872. }
  873. /* Uh oh... serious problem here */
  874. return USB_STOR_TRANSPORT_ERROR;
  875. }
  876. /* DATA STAGE */
  877. /* transfer the data payload for this command, if one exists*/
  878. if (usb_stor_transfer_length(srb)) {
  879. usb_stor_transfer(srb, us);
  880. result = srb->result;
  881. US_DEBUGP("CB data stage result is 0x%xn", result);
  882. /* report any errors */
  883. if (result == US_BULK_TRANSFER_ABORTED) {
  884. return USB_STOR_TRANSPORT_ABORTED;
  885. }
  886. if (result == US_BULK_TRANSFER_FAILED) {
  887. return USB_STOR_TRANSPORT_FAILED;
  888. }
  889. }
  890. /* STATUS STAGE */
  891. /* NOTE: CB does not have a status stage.  Silly, I know.  So
  892.  * we have to catch this at a higher level.
  893.  */
  894. return USB_STOR_TRANSPORT_GOOD;
  895. }
  896. /*
  897.  * Bulk only transport
  898.  */
  899. /* Determine what the maximum LUN supported is */
  900. int usb_stor_Bulk_max_lun(struct us_data *us)
  901. {
  902. unsigned char *data;
  903. int result;
  904. int pipe;
  905. data = kmalloc(sizeof *data, GFP_KERNEL);
  906. if (!data) {
  907. return 0;
  908. }
  909. /* issue the command -- use usb_control_msg() because
  910.  *  the state machine is not yet alive */
  911. pipe = usb_rcvctrlpipe(us->pusb_dev, 0);
  912. result = usb_control_msg(us->pusb_dev, pipe,
  913.  US_BULK_GET_MAX_LUN, 
  914.  USB_DIR_IN | USB_TYPE_CLASS | 
  915.  USB_RECIP_INTERFACE,
  916.  0, us->ifnum, data, sizeof(data), HZ);
  917. US_DEBUGP("GetMaxLUN command result is %d, data is %dn", 
  918.   result, *data);
  919. /* if we have a successful request, return the result */
  920. if (result == 1) {
  921. result = *data;
  922. kfree(data);
  923. return result;
  924. } else {
  925. kfree(data);
  926. }
  927. /* if we get a STALL, clear the stall */
  928. if (result == -EPIPE) {
  929. US_DEBUGP("clearing endpoint halt for pipe 0x%xn", pipe);
  930. /* Use usb_clear_halt() because the state machine
  931.  *  is not yet alive */
  932. usb_clear_halt(us->pusb_dev, pipe);
  933. }
  934. /* return the default -- no LUNs */
  935. return 0;
  936. }
  937. int usb_stor_Bulk_reset(struct us_data *us);
  938. int usb_stor_Bulk_transport(Scsi_Cmnd *srb, struct us_data *us)
  939. {
  940. struct bulk_cb_wrap *bcb;
  941. struct bulk_cs_wrap *bcs;
  942. int result;
  943. int pipe;
  944. int partial;
  945. int ret = USB_STOR_TRANSPORT_ERROR;
  946. bcb = kmalloc(sizeof *bcb, in_interrupt() ? GFP_ATOMIC : GFP_NOIO);
  947. if (!bcb) {
  948. return USB_STOR_TRANSPORT_ERROR;
  949. }
  950. bcs = kmalloc(sizeof *bcs, in_interrupt() ? GFP_ATOMIC : GFP_NOIO);
  951. if (!bcs) {
  952. kfree(bcb);
  953. return USB_STOR_TRANSPORT_ERROR;
  954. }
  955. /* set up the command wrapper */
  956. bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
  957. bcb->DataTransferLength = cpu_to_le32(usb_stor_transfer_length(srb));
  958. bcb->Flags = srb->sc_data_direction == SCSI_DATA_READ ? 1 << 7 : 0;
  959. bcb->Tag = srb->serial_number;
  960. bcb->Lun = srb->cmnd[1] >> 5;
  961. if (us->flags & US_FL_SCM_MULT_TARG)
  962. bcb->Lun |= srb->target << 4;
  963. bcb->Length = srb->cmd_len;
  964. /* construct the pipe handle */
  965. pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
  966. /* copy the command payload */
  967. memset(bcb->CDB, 0, sizeof(bcb->CDB));
  968. memcpy(bcb->CDB, srb->cmnd, bcb->Length);
  969. /* send it to out endpoint */
  970. US_DEBUGP("Bulk command S 0x%x T 0x%x Trg %d LUN %d L %d F %d CL %dn",
  971.   le32_to_cpu(bcb->Signature), bcb->Tag,
  972.   (bcb->Lun >> 4), (bcb->Lun & 0x0F), 
  973.   bcb->DataTransferLength, bcb->Flags, bcb->Length);
  974. result = usb_stor_bulk_msg(us, bcb, pipe, US_BULK_CB_WRAP_LEN, 
  975.    &partial);
  976. US_DEBUGP("Bulk command transfer result=%dn", result);
  977. /* if the command was aborted, indicate that */
  978. if (result == -ENOENT) {
  979. ret = USB_STOR_TRANSPORT_ABORTED;
  980. goto out;
  981. }
  982. /* if we stall, we need to clear it before we go on */
  983. if (result == -EPIPE) {
  984. US_DEBUGP("clearing endpoint halt for pipe 0x%xn", pipe);
  985. result = usb_stor_clear_halt(us, pipe);
  986. /* if the command was aborted, indicate that */
  987. if (result == -ENOENT) {
  988. ret = USB_STOR_TRANSPORT_ABORTED;
  989. goto out;
  990. }
  991. result = -EPIPE;
  992. } else if (result) {
  993. /* unknown error -- we've got a problem */
  994. ret = USB_STOR_TRANSPORT_ERROR;
  995. goto out;
  996. }
  997. /* if the command transfered well, then we go to the data stage */
  998. if (result == 0) {
  999. /* send/receive data payload, if there is any */
  1000. if (bcb->DataTransferLength) {
  1001. usb_stor_transfer(srb, us);
  1002. result = srb->result;
  1003. US_DEBUGP("Bulk data transfer result 0x%xn", result);
  1004. /* if it was aborted, we need to indicate that */
  1005. if (result == US_BULK_TRANSFER_ABORTED) {
  1006. ret = USB_STOR_TRANSPORT_ABORTED;
  1007. goto out;
  1008. }
  1009. }
  1010. }
  1011. /* See flow chart on pg 15 of the Bulk Only Transport spec for
  1012.  * an explanation of how this code works.
  1013.  */
  1014. /* construct the pipe handle */
  1015. pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
  1016. /* get CSW for device status */
  1017. US_DEBUGP("Attempting to get CSW...n");
  1018. result = usb_stor_bulk_msg(us, bcs, pipe, US_BULK_CS_WRAP_LEN, 
  1019.    &partial);
  1020. /* if the command was aborted, indicate that */
  1021. if (result == -ENOENT) {
  1022. ret = USB_STOR_TRANSPORT_ABORTED;
  1023. goto out;
  1024. }
  1025. /* did the attempt to read the CSW fail? */
  1026. if (result == -EPIPE) {
  1027. US_DEBUGP("clearing endpoint halt for pipe 0x%xn", pipe);
  1028. result = usb_stor_clear_halt(us, pipe);
  1029. /* if the command was aborted, indicate that */
  1030. if (result == -ENOENT) {
  1031. ret = USB_STOR_TRANSPORT_ABORTED;
  1032. goto out;
  1033. }
  1034. /* get the status again */
  1035. US_DEBUGP("Attempting to get CSW (2nd try)...n");
  1036. result = usb_stor_bulk_msg(us, bcs, pipe,
  1037.    US_BULK_CS_WRAP_LEN, &partial);
  1038. /* if the command was aborted, indicate that */
  1039. if (result == -ENOENT) {
  1040. ret = USB_STOR_TRANSPORT_ABORTED;
  1041. goto out;
  1042. }
  1043. /* if it fails again, we need a reset and return an error*/
  1044. if (result == -EPIPE) {
  1045. US_DEBUGP("clearing halt for pipe 0x%xn", pipe);
  1046. result = usb_stor_clear_halt(us, pipe);
  1047. /* if the command was aborted, indicate that */
  1048. if (result == -ENOENT) {
  1049. ret = USB_STOR_TRANSPORT_ABORTED;
  1050. } else {
  1051. ret = USB_STOR_TRANSPORT_ERROR;
  1052. }
  1053. goto out;
  1054. }
  1055. }
  1056. /* if we still have a failure at this point, we're in trouble */
  1057. US_DEBUGP("Bulk status result = %dn", result);
  1058. if (result) {
  1059. ret = USB_STOR_TRANSPORT_ERROR;
  1060. goto out;
  1061. }
  1062. /* check bulk status */
  1063. US_DEBUGP("Bulk status Sig 0x%x T 0x%x R %d Stat 0x%xn",
  1064.   le32_to_cpu(bcs->Signature), bcs->Tag, 
  1065.   bcs->Residue, bcs->Status);
  1066. if (bcs->Signature != cpu_to_le32(US_BULK_CS_SIGN) || 
  1067.     bcs->Tag != bcb->Tag || 
  1068.     bcs->Status > US_BULK_STAT_PHASE || partial != 13) {
  1069. US_DEBUGP("Bulk logical errorn");
  1070. ret = USB_STOR_TRANSPORT_ERROR;
  1071. goto out;
  1072. }
  1073. /* based on the status code, we report good or bad */
  1074. switch (bcs->Status) {
  1075. case US_BULK_STAT_OK:
  1076. /* command good -- note that data could be short */
  1077. ret = USB_STOR_TRANSPORT_GOOD;
  1078. goto out;
  1079. case US_BULK_STAT_FAIL:
  1080. /* command failed */
  1081. ret = USB_STOR_TRANSPORT_FAILED;
  1082. goto out;
  1083. case US_BULK_STAT_PHASE:
  1084. /* phase error -- note that a transport reset will be
  1085.  * invoked by the invoke_transport() function
  1086.  */
  1087. ret = USB_STOR_TRANSPORT_ERROR;
  1088. goto out;
  1089. }
  1090. /* we should never get here, but if we do, we're in trouble */
  1091.  out:
  1092. kfree(bcb);
  1093. kfree(bcs);
  1094. return ret;
  1095. }
  1096. /***********************************************************************
  1097.  * Reset routines
  1098.  ***********************************************************************/
  1099. /* This issues a CB[I] Reset to the device in question
  1100.  */
  1101. int usb_stor_CB_reset(struct us_data *us)
  1102. {
  1103. unsigned char cmd[12];
  1104. int result;
  1105. US_DEBUGP("CB_reset() calledn");
  1106. /* if the device was removed, then we're already reset */
  1107. if (!us->pusb_dev)
  1108. return SUCCESS;
  1109. memset(cmd, 0xFF, sizeof(cmd));
  1110. cmd[0] = SEND_DIAGNOSTIC;
  1111. cmd[1] = 4;
  1112. result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0),
  1113.  US_CBI_ADSC, 
  1114.  USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  1115.  0, us->ifnum, cmd, sizeof(cmd), HZ*5);
  1116. if (result < 0) {
  1117. US_DEBUGP("CB[I] soft reset failed %dn", result);
  1118. return FAILED;
  1119. }
  1120. /* long wait for reset */
  1121. set_current_state(TASK_UNINTERRUPTIBLE);
  1122. schedule_timeout(HZ*6);
  1123. set_current_state(TASK_RUNNING);
  1124. US_DEBUGP("CB_reset: clearing endpoint haltn");
  1125. usb_stor_clear_halt(us,
  1126. usb_rcvbulkpipe(us->pusb_dev, us->ep_in));
  1127. usb_stor_clear_halt(us,
  1128. usb_sndbulkpipe(us->pusb_dev, us->ep_out));
  1129. US_DEBUGP("CB_reset donen");
  1130. /* return a result code based on the result of the control message */
  1131. return SUCCESS;
  1132. }
  1133. /* This issues a Bulk-only Reset to the device in question, including
  1134.  * clearing the subsequent endpoint halts that may occur.
  1135.  */
  1136. int usb_stor_Bulk_reset(struct us_data *us)
  1137. {
  1138. int result;
  1139. US_DEBUGP("Bulk reset requestedn");
  1140. /* if the device was removed, then we're already reset */
  1141. if (!us->pusb_dev)
  1142. return SUCCESS;
  1143. result = usb_control_msg(us->pusb_dev, 
  1144.  usb_sndctrlpipe(us->pusb_dev,0), 
  1145.  US_BULK_RESET_REQUEST, 
  1146.  USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  1147.  0, us->ifnum, NULL, 0, HZ*5);
  1148. if (result < 0) {
  1149. US_DEBUGP("Bulk soft reset failed %dn", result);
  1150. return FAILED;
  1151. }
  1152. /* long wait for reset */
  1153. set_current_state(TASK_UNINTERRUPTIBLE);
  1154. schedule_timeout(HZ*6);
  1155. set_current_state(TASK_RUNNING);
  1156. usb_stor_clear_halt(us,
  1157. usb_rcvbulkpipe(us->pusb_dev, us->ep_in));
  1158. usb_stor_clear_halt(us,
  1159. usb_sndbulkpipe(us->pusb_dev, us->ep_out));
  1160. US_DEBUGP("Bulk soft reset completedn");
  1161. return SUCCESS;
  1162. }