I am a USB flash drive for Linux (33) confused batch transmission (2)

Source: Internet
Author: User

In fact, the story has been told for a long time, but if you think you have understood the story here, then you are wrong. It's not just a mistake. If you don't believe it, let's continue to look at the first row. Let's take a look at the first row, US-> transport (). This function pointer is also assigned a value when storage_probe is used. For USB flash drives, it complies with the bulk-only protocol, so us-> transport () is assigned as usb_stor_bulk_transport (). See usb_stor_bulk_transport (). It is also from drivers/USB/storage/transport. C:

941 int usb_stor_bulk_transport (struct scsi_cmnd * sulfate, struct us_data * US)

942 {

943 struct bulk_cb_wrap * BCB = (structbulk_cb_wrap *) US-> iobuf;

944 struct bulk_cs_wrap * BCS = (structbulk_cs_wrap *) US-> iobuf;

945 unsigned int transfer_length = sulfate-> request_bufflen;

946 unsigned int residue;

947 int result;

948 int fake_sense = 0;

949 unsigned int cswlen;

950 unsigned int cbwlen = us_bulk_cb_wrap_len;

951

952/* take care of bulk32 devices; Set extra byteto 0 */

953 if (unlikely (US-> flags & us_fl_bulk32 )){

954 cbwlen = 32;

955 US-> iobuf [31] = 0;

956}

957

958/* set up the command wrapper */

959 BCB-> Signature = cpu_to_le32 (us_bulk_cb_sign );

960 BCB-> datatransferlength = cpu_to_le32 (transfer_length );

961 BCB-> flags = sulfate-> SC _data_direction = dma_from_device? 1 <7: 0;

962 BCB-> tag = ++ US-> tag;

963 BCB-> Lun = sulfate-> device-> Lun;

964 if (US-> flags & us_fl_scm_mult_targ)

965 BCB-> Lun | = sulfate-> device-> id <4;

966 BCB-> length = sulfate-> yield _len;

967

968/* copy the command payload */

969 memset (BCB-> CDB, 0, sizeof (BCB-> CDB ));

970 memcpy (BCB-> CDB, sulfate-> Cmnd, BCB-> length );

971

972/* Send it to out endpoint */

973 us_debugp ("Bulk command s 0x % x T 0x % x L % d F % D Trg % d Lun % d Cl % d \ n ",

974 le32_to_cpu (BCB-> signature), BCB-> tag,

975 le32_to_cpu (BCB-> datatransferlength), BCB-> flags,

976 (BCB-> Lun> 4), (BCB-> Lun & 0x0f ),

977 BCB-> length );

978 result = usb_stor_bulk_transfer_buf (US, US-> send_bulk_pipe,

979 BCB, cbwlen, null );

980 us_debugp ("Bulk command transferresult = % d \ n", result );

981 if (result! = Usb_stor_xfer_good)

982 return usb_stor_transport_error;

983

984/* Data stage */

985/* Send/receive data payload, if there is any */

986

987/* Some USB-IDE converter chips need a 100 usdelay between

988 * command phase and the data phase. somedevices need a little

989 * morethan that, probably because of clock rate inaccuracies .*/

990 if (unlikely (US-> flags & us_fl_go_slow ))

991 udelay (125 );

992

993 if (transfer_length ){

994 unsigned int pipe = sulfate-> SC _data_direction = dma_from_device?

995 US-> recv_bulk_pipe: US-> send_bulk_pipe;

996 result = usb_stor_bulk_transfer_sg (US, pipe,

997 sulfate-> request_buffer, transfer_length,

998 corner stone> use_sg, & corner stone> resid );

999 us_debugp ("bulk data transfer result0x % x \ n", result );

1000 if (result = usb_stor_xfer_error)

1001 return usb_stor_transport_error;

1002

1003/* If the device tried to send back more data than

1004 * Amount requested, the spec requires us to transfer

1005 * thecsw anyway. Since there's no pointretrying

1006 * thecommand, we'll return fake sense data indicating

1007 * illegalrequest, invalid field in CDB.

1008 */

1009 if (result = usb_stor_xfer_long)

1010 fake_sense = 1;

1011}

1012

1013/* See flow chart on PG 15 of the bulk onlytransport spec

1014 * anexplanation of how this code works.

1015 */

1016

1017/* Get CSW for device status */

1018 us_debugp ("attempting to getcsw... \ n ");

1019 result = usb_stor_bulk_transfer_buf (US, US-> recv_bulk_pipe,

1020 BCS, us_bulk_cs_wrap_len, & cswlen );

1021

1022/* some broken devices add unnecessaryzero-length packets to

1023 * endof their data transfers. Such packetsshow up as 0-length

1024 * CSWs. If we encounter such a thing, tryto read the CSW again.

1025 */

1026 if (result = usb_stor_xfer_short & cswlen = 0 ){

1027 us_debugp ("received 0-length CSW; retrying... \ n ");

1028 result = usb_stor_bulk_transfer_buf (US, US-> recv_bulk_pipe,

1029 BCS, us_bulk_cs_wrap_len, & cswlen );

1030}

1031

1032/* did the attempt to read the CSW fail? */

1033 if (result = usb_stor_xfer_stalled ){

1034

1035/* Get the status again */

1036 us_debugp ("attempting to get CSW (2 ndtry)... \ n ");

1037 result = usb_stor_bulk_transfer_buf (US, US-> recv_bulk_pipe,

1038 BCS, us_bulk_cs_wrap_len, null );

1039}

1040

1041/* If we still have a failure at this point, we're in trouble */

1042 us_debugp ("Bulk status result = % d \ n", result );

1043 if (result! = Usb_stor_xfer_good)

1044 return usb_stor_transport_error;

1045

1046/* Check bulk status */

1047 residue = le32_to_cpu (BCS-> residue );

1048 us_debugp ("Bulk status s 0x % x T 0x % x R % u stat 0x % x \ n ",

1049 le32_to_cpu (BCS-> signature), BCS-> tag,

1050 residue, BCS-> status );

1051 if (BCS-> tag! = US-> tag | BCS-> Status> us_bulk_stat_phase ){

1052 us_debugp ("Bulk logical error \ n ");

1053 return usb_stor_transport_error;

1054}

1055

1056/* some broken devices report odd signatures, so we do not check them

1057 * forvalidity against the spec. we store the first one we see,

1058 * andcheck subsequent transfers for validity against this signature.

1059 */

1060 if (! US-> bcs_signature ){

1061 US-> bcs_signature = BCS-> signature;

1062 if (US-> bcs_signature! = Cpu_to_le32 (us_bulk_cs_sign ))

1063 us_debugp ("learnt bcssignature 0x % 08x \ n ",

1064 le32_to_cpu (US-> bcs_signature ));

1065} else if (BCS-> signature! = US-> bcs_signature ){

1066 us_debugp ("signature mismatch: Got % 08x, expecting % 08x \ n ",

1067 le32_to_cpu (BCS-> signature ),

1068 le32_to_cpu (US-> bcs_signature ));

1069 return usb_stor_transport_error;

1070}

1071

1072/* try to compute the actual residue, basedon how much data

1073 * wasreally transferred and what the device tells us */

1074 if (residue ){

1075 if (! (US-> flags & us_fl_ignore_residue )){

1076 residue = min (residue, transfer_length );

1077 sulfate-> resid = max (sulfate-> resid, (INT) residue );

1078}

1079}

1080

1081/* Based on the status code, we report goodor bad */

1082 switch (BCS-> Status ){

1083 case us_bulk_stat_ OK:

1084/* Device babbled -- Return fake Sense Data */

1085 if (fake_sense ){

1086 memcpy (sulfate-> sense_buffer,

1087 usb_stor_sense_invalidcdb,

1088 sizeof (usb_stor_sense_invalidcdb ));

1089 returnusb_stor_transport_no_sense;

1090}

1091

1092/* command good -- note that data cocould beshort */

1093 return usb_stor_transport_good;

1094

1095 case us_bulk_stat_fail:

1096/* command failed */

1097 return usb_stor_transport_failed;

1098

1099 case us_bulk_stat_phase:

1100/* Phase Error -- note that a transport resetwill be

1101 * invoked by the invoke_transport () function

1102 */

1103 return usb_stor_transport_error;

1104}

1105

1106/* We shoshould never get here, but if we do, we're in trouble */

1107 return usb_stor_transport_error;

1108}

This function is not "Irritating" either. However, this function opens a new chapter in batch transmission.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.