I'm USB in Linux, I'm a USB flash drive (27) Legend of the other side (6)

Source: Internet
Author: User

Let's continue with the previous section. Fill_inquiry_response (), which is from drivers/usb/storage/usb. c.

 

266 void fill_inquiry_response (struct us_data * us, unsigned char * data,

 

267 unsigned int data_len)

 

268 {

 

269 if (data_len <36) // You lose.

 

270 return;

 

271

 

272 if (data [0] & 0x20) {/* USB device currently not connected. Return

 

273 peripheral qualifier 001b ("... however,

 

274 physical device is not currently connected

 

275 to this logical unit ") and leave vendor and

 

276 productidentification empty. ("If the target

 

277 does store some of the INQUIRY data on

 

278 device, it may return zeros or ASCII spaces

 

279 (20 h) in those fields until the data is

 

280 available from the device .").*/

 

281 me mset (data +, 28 );

 

282} else {

 

283 bcdDevice = le16_to_cpu (us-> pusb_dev-> descriptor. bcdDevice );

 

284 memcpy (data + 8, us-> unusual_dev-> vendorName,

 

285 strlen (us-> unusual_dev-> vendorName)> 8? 8:

 

286 strlen (us-> unusual_dev-> vendorName ));

 

287 memcpy (data + 16, us-> unusual_dev-> productName,

 

288 strlen (us-> unusual_dev-> productName)> 16? 16:

 

289 strlen (us-> unusual_dev-> productName ));

 

290 data [32] = 0x30 + (bcdDevice> 12) & 0x0F );

 

291 data [33] = 0x30 + (bcdDevice> 8) & 0x0F );

 

292 data [34] = 0x30 + (bcdDevice> 4) & 0x0F );

 

293 data [35] = 0x30 + (bcdDevice) & 0x0F );

 

294}

 

295

 

296 usb_stor_set_xfer_buf (data, data_len, us-> sulfate );

 

297}

 

The story is so sudden that an illusion may occur. Originally, the proto_handler () function we used to process SCSI commands is the proto_handler () that we will talk about later, but we can't think of starting to access SCSI commands here. The reason is that some PEG products like Sony are not doing well, even the most basic SCSI command INQUIRY is not supported, and then you want to use it in Linux, that's no way, so prepare a function to fix this problem. There is no doubt that this is a hardware Bug.

 

What is the INQUIRY command? As mentioned above, the INQUIRY command is the most basic SCSI command. For example, when a host detects a device for the first time, it needs to use the INQUIRY command to understand what the device is. If a device is inserted in a slot on the SCSI bus, the SCSI host will ask it, are you a SCSI disk, a SCSI tape, or a scsi cd rom? As a device, it must have a firmware program called firmware. It will give an answer after receiving the INQUIRY command of the host.

 

What should I do? Of course, it is based on the format specified in the SCSI protocol. This is not only the INQUIRY command, but also for every command. As long as the other party asks, "the King of Heaven is huge ". As a device, you should answer: "Baota Zhenghe demon ." This is actually like our couplet. This is all unwritten rules. The SCSI developers have written these rules into written rules. Specifically, when a device is queried by the INQUIRY command, it complies with the standard format specified in the SCSI protocol. The standard format specifies that the response data must contain at least 36 bytes. So for Row 3, if data_len is smaller than 36, don't go down and return.

 

If you are unfamiliar with the SCSI protocol and still do not understand what the INQUIRY command is, we recommend a tool for you to give it a try, so that you can have an intuitive impression, in fact, the INQUIRY command is to query some basic information about the device. From the software point of view, when the host is scanned, or enumeration, this command is sent to each device and an answer is obtained, the driver will then save the information, because this information may be used later or some of it will be used. The recommended tool here is sg_utils3, which is a software package that can be used in Linux. It is available everywhere. After installation, it contains an application sg_inq, this is actually used to send the INQUIRY command to the device. The usage is as follows:

 

[Root @ localhost ~] # Sg_inq-36/dev/sda

 

Standard INQUIRY:

 

PQual = 0 Device_type = 0 RMB = 1 version = 0x02 [SCSI-2]

 

[AERC = 0] [TrmTsk = 0] NormACA = 0 HiSUP = 0 Resp_data_format = 2

 

SCCS = 0 ACC = 0 TGPS = 0 3 PC = 0 Protect = 0 BQue = 0

 

EncServ = 0 MultiP = 0 [MChngr = 0] [ACKREQQ = 0] Addr16 = 0

 

[RelAdr = 0] WBus16 = 0 Sync = 0 Linked = 0 [TranDis = 0] semi que = 0

 

Length = 36 (0x24) Peripheral device type: disk

 

Vendor identification: Intel

 

Product identification: Flash Disk

 

Product revision level: 2.00

 

Here I am using a USB flash drive produced by Intel. The sg_inq command can be used to query the basic information about this USB flash drive. In fact, sg_inq can query information of all SCSI devices, because INQUIRY is a standard SCSI command. Of course, in the above information, we will use the approximate Vendor ID, Product ID, Product revision, and length, device type -- disk, as well as the SCSI-2 in the brackets, this indicates the SCSI version to be followed. The SCSI protocol has been developed for so many years, and of course there are different versions.

 

With an intuitive impression, let's continue to look at the code, line 1, and determine if data [0] is 20 h or 20 h. What's special? Of course. The SCSI protocol specifies that the standard INQUIRY data [0] has a total of 8 bits. Here, bit7 ~ Bi5 is called peripheral qualifier (three digits), while bit4 ~ Bit0 is called perpheral device type (five bits). They represent different meanings, but 20 h indicates that the peripheral device of peripheralqualifier is set to 001b, the peripheral device type of peripheral device type is 00 h. According to the SCSI protocol, the latter indicates that the device type is disk or directly accesses the device. The former indicates that the current LUN of the target device supports this type. However, the actual physical device is not connected to the current LUN. In data [36], the 28 bytes from data [8] to data [35] Save the information of the manufacturer and product. It is written in the SCSI protocol. If the information is stored in the device, it can return 0x20 h for the time being, because the latency should be minimized during the system poweron period or reset period, therefore, fill_inquiry_response () sets data [8] to data [35] to 0. Wait until the information stored on the device can be read and then read.

 

If it is not 20 h, for example, if the data [0] passed in here is 0, then the 8 bytes starting with data [8] Can Save vendor-related information, for us-> unusual_dev, we are no stranger. struct us_unusual_dev * unusual_dev, a member of the struct us_data struct, is stored in storage_probe () the corresponding element in the us_unusual_dev_list [] array is assigned to it, and us_unusua_dev_list [] and unusual_devs.h are pre-defined. So here we will copy the vendorName to the data array. However, if the vendorName contains more than 8 characters, it won't work. Just take the first 8 characters. Of course, this problem does not exist like Intel. There are only five characters, and most companies are within 8 characters. For example, if a longer name is Motorola, Samsung is fine. Similarly, productName is the same method. It is copied to the data array. The Protocol specifies that productName cannot exceed 16 characters from 16, so there is no problem with "Flash Disk. For more information about the standard INQUIRY data format, see SCSI Primary Command-4.

 

Then you can see Row 3, us-> pusb_dev-> descriptor. in bcdDevice and struct us_data, there is a member struct usb_device * pusb_dev, while in struct usb_device there is a member struct complete, while in structusb_device_descriptor, member _ bcdDevice indicates the version of the product specified by the manufacturer, the "channels" rule uses the version number, manufacturer id, and product id to mark a device. BcdDevice has a total of 16 bcddevices. It stores the information in bcd code. That is to say, every four bcddevices represent a decimal number. For example, 00110110 1001 represents 0111. In SCSI standard INQUIRY data, data [32] to data [35] is defined to save these four numbers, and must be saved in ASCII code. In the ASCII code, 48 corresponds to our daily 0, 49 corresponds to 1, 50 corresponds to 2, that is, we must add 48, or 0x30 on the basis of the existing number. This is the meaning of rows 290 to 293.

 

After everything is ready, we can send the 36-character data array to the location specified by the SCSI command, that is, the location specified by the Server Load balancer. This is exactly what usb_stor_set_xfer_buf does in Row 3.

 

Before talking about the Function usb_stor_set_xfer_buf in line 1, explain the first eight elements initialized when data_ptr [36] is defined. Their meanings correspond to those defined in the scsi protocol. Data_ptr [0] Needless to say, data_ptr [1] is assigned 0x80, which indicates that this device can be removed, data_ptr [2] is assigned 0x02 which indicates that the device complies with the SCSI-2 protocol, data_ptr [3] is assigned 0x02, indicating that the data format complies with the format specified by the International Organization for Standardization, data_ptr [4] is called additional length and the length of the additional parameter. In addition to the data response in such a standard format, more information may be returned. Here, 0x1F is set.

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.