I am USB in Linux, I am a USB flash drive (26) Legend of the other side (5)

Source: Internet
Author: User

The following describes the usb_stor_control_thread () function. What wakes up is the up (& (US-> SEMA) from the queuecommand, and us-> sulfate is assigned to the Server Load balancer, in contrast, the Server Load balancer is a parameter passed in when the SCSI core layer calls the queuecommand. Focus on usb_stor_control_thread (). Row 3: As mentioned earlier, we have to look at the dev_mutex lock from a higher perspective after reading the entire module.

Line 3: If us_fw.x_disconnecting is set, you don't need to say much about this. It is to determine whether the device has been pulled out. If your USB flash drive is inserted, it will never be pulled out, you can delete all the Code related to the flag. Of course, in fact, it is impossible for you to pull it out. Hot swapping is a major feature of USB devices.

Row 3 and host are also locks, so we can check the lock at the end.

Lines 326 to 330 are used to determine whether another flag has been set. The meaning of the Flag us_fldx_timed_out is also as literal, that is, timeout. The concept of timeout is everywhere in the computer world. However, for this flag, the function for setting it is command_abort. This function is also provided by us. It is called by the SCSI core layer, where it is responsible for timing. when it expires, it calls command_abort. We will see it later. Don't worry.

Row 3: Judge SC _data_direction, a member of the Server Load balancer instance. Check the dma_bidirectional macro first. This macro is defined in include/Linux/dma-mapping.h:

7/* These definitions mirror those in PCI. H, sothey can be used

8 * interchangeably with their PCI _ counterparts */

9 Enum dma_data_direction {

10 dma_bidirectional = 0,

11 dma_to_device = 1,

12 dma_from_device = 2,

13 dma_none = 3,

14 };

These codes are used to indicate the direction of data transmission in the data phase. Dma_to_device indicates that the device is from the primary storage to the device, and dma_from_device indicates that the device is from the primary storage to the primary storage. It is rumored that dma_none is only used for debugging and cannot be used. Otherwise, the kernel may crash. More accurately, however, the USB Mass Storage protocol stipulates that bidirectional transmission is illegal, while a command is valid to transmit zero data. For example, the test_unit_ready command does not need to transmit data.

Dma_bidirectional indicates that both directions are possible. In other words, it means you do not know which direction it is. Similarly, when the SC _data_direction of the Server Load balancer instance is dma_bidirectional in Row 3, it is regarded as an error. Data cannot be transmitted because of uncertainty.

Row 3: the flag us_fl_scm_mult_targ indicates that the device supports multiple targets. The meaning here is obvious. For devices that do not support multiple targets, its US-> sulfate-> device-> ID must be 0; otherwise, the problem may occur. In the struct us_data struct, the struct scsi_cmnd * sulfate is a member, and the struct scsi_device * device is a member in the struct scsi_cmnd struct. struct scsi_device, as its name implies, describes a SCSI device, just like the previous struct usb_device used to describe a USB device. Struct
Scsi_device from include/SCSI/scsi_device.h:

49 struct scsi_device {

50 struct scsi_host * Host;

51 struct request_queue * request_queue;

52

53/* the next two are protected by thehost-> host_lock */

54 struct list_head siblings;/* List of all devices on this host */

55 struct list_head same_target_siblings;

56

57/* This is now protected by therequest_queue-> queue_lock */

58 unsigned int device_busy;/* commands actually active on

59 * low-level. Protected by queue_lock .*/

60 spinlock_t list_lock;

61 struct list_head into _list;/* queue of in usescsi command structures */

62 struct list_head starved_entry;

63 struct scsi_cmnd * current_cmnd;/* currentlyactive command */

64 unsigned short queue_depth;/* how deep of aqueue we want */

65 unsigned short last_queue_full_depth;/* thesetwo are used */

66 unsigned short last_queue_full_count;/* scsi_track_queue_full ()*/

67 unsigned long last_queue_full_time;/* Don't let queue_fulls on the same

68 jiffie count on our counter, they

69 cocould all be from the same event .*/

70

71 unsigned int ID, Lun, channel;

72

73 unsigned int manufacturer;/* manufacturerof device, for using

74 * vendor-specific CMD's */

75 unsigned sector_size;/* size in bytes */

76

77 void * hostdata;/* available to low-level driver */

78 char type;

79 char scsi_level;

80 char inq_periph_qual;/* PQ from inquiry data */

81 unsigned char inquiry_len;/* Valid bytesin 'inquiry '*/

82 unsigned char * inquiry;/* inquiry response data */

83 const char * vendor;/* [back_compat] point into 'inquiry '...*/

84 const char * model;/*... after scan; point to static string */

85 const char * Rev;/*... "nullnullnullnull" before scan */

86 unsigned char current_tag;/* Current tag */

87 struct scsi_target * sdev_target;/* used only forsingle_lun */

88

89 unsigned int sdev_bflags;/* black/white flags as also foundin

90 * scsi_devinfo. [HC]. For now used only

91 * pass settings from slave_alloc to SCSI

92 * CORE .*/

93 unsigned writeable: 1;

94 unsigned removable: 1;

95 unsigned changed: 1;/* data invalid due to media change */

96 unsigned busy: 1;/* used to prevent races */

97 unsigned lockable: 1;/* able to prevent media removal */

98 unsigned locked: 1;/* Media removal disabled */

99 unsigned borken: 1;/* tell the Seagate driver to be

100 * painfully slow on this device */

101 unsigned disconnect: 1;/* can disconnect */

102 unsignedsoft_reset: 1;/* uses soft resetoption */

103 unsigned sdtr: 1;/* Device supports sdtr messages */

104 unsignedwdtr: 1;/* Device supports wdtrmessages */

105 unsigned PPR: 1;/* Device supports PPR messages */

106 unsignedtagged_supported: 1;/* supports SCSI-II tagged queuing */

107 unsignedsimple_tags: 1;/* simple queue tag messages are enabled */

108 unsignedordered_tags: 1;/* ordered queue tag messages are enabled */

109 unsigned single_lun: 1;/* Indicates we shoshould only allow I/O

110 * One of the Luns for the device at

111 * time .*/

112 unsignedwas_reset: 1;/* There was ABUS reset on the bus

113 * this device */

114 unsigned expecting_cc_ua: 1;/* expecting acheck_condition/unit_attn

115 * because we did a bus reset .*/

116 unsigneduse_10_for_rw: 1;/* First try 10-byte read/write */

117 unsigneduse_10_for _ MS: 1;/* First try 10-byte mode sense/select */

118 unsignedskip _ ms_page_8: 1;/* do not use mode sense page 0x08 */

119 unsignedskip _ ms_page_3f: 1;/* do not use mode sense page 0x3f */

120 unsigneduse_192_bytes_for_3f: 1;/* ask for 192 bytes from page 0x3f */

121 unsigned no_start_on_add: 1;/* do not issue starton add */

122 unsignedallow_restart: 1;/* issue start_unit in error handler */

123 unsignedmanage_start_stop: 1;/* Let HLD (SD) manage start/stop */

124 unsigned no_uld_attach: 1;/* disable connecting toupper level drivers */

125 unsigned select_no_atn: 1;

126 unsignedfix_capacity: 1;/* read_capacity is too high by 1 */

127 unsignedguess_capacity: 1;/* read_capacity might be too high by 1 */

128 unsignedretry_hwerror: 1;/* retry hardware_error */

129

130 unsigned int device_blocked;/* Device returnedqueue_full .*/

131

132 unsigned int max_device_blocked;

133 # define scsi_default_device_blocked 3

134

135 atomic_t iorequest_cnt;

136 atomic_t iodone_cnt;

137 atomic_tierr_cnt;

138

139 inttimeout;

140

141 struct device sdev_gendev;

142 structclass_device sdev_classdev;

143

144 struct execute_work EW;/* used to getprocess context on put */

145

146 enumscsi_device_state sdev_state;

147 unsigned long sdev_data [0];

148} _ attribute _ (aligned (sizeof (unsignedlong ))));

This struct will be mentioned many times later. Of course, at this moment, we only need to note the unsigned int ID, Lun, and channel members. This is exactly the three members necessary to locate a SCSI device, the devices controlled by a scsicard are divided into several layers. There are several channels, and each channel has several targets. Each target is represented by a target ID, then a target can have several Luns, and the target ID is determined here. For devices that do not support multiple targets, the value must be 0. For most USB mass storage devices, their target
The ID must be 0. Some equipment manufacturers want to be unconventional. They want devices to support multiple targets, so they can set a flag such as us_fl_scm_mult_targ, for example, we can see the following definitions in drivers/USB/storage/unusual_devs.h:

416 unusual_dev (0x04e6, 0x0002, 0x0100, 0x0100,

417 "Shuttle ",

418 "euscsi bridge ",

419 us_ SC _device, us_pr_device, usb_stor_euscsi_init,

420 us_fl_scm_mult_targ ),

Then, in Row 3, the number of "us"> "ports"> "device"> "Lun" should not be greater than that of "us"> "max_lun". What are the differences between the two? US-> max_lun is the maximum Lun obtained from the USB Mass Storage Device by calling the usb_stor_scan_thread () function in usb_stor_bulk_max_lun () in the early stage. For example, Max Lun is 3, the device supports four Luns, namely, 0, 1, 2, and 3. However, the "us"> "sulfate"> "device"> "Lun" parameter can be any of the four values. You can see who is accessing the passed command. But it obviously cannot exceed Max
Lun.

Then there are 358 rows. Seeing such a flag-US_FL_FIX_INQUIRY, this is one of the many flags in US-> flags, which we have introduced before, some devices defined in drivers/USB/storage/unusal_devs.h have this flag. In fact, the vendor name and Product Name of most devices are usually obtained through the inquiry command, and this flag indicates that, the vendor name and product name of these devices do not need to be queried, or cannot be queried at all. Their vendor name and product name are defined directly and set in unusal_devs.h. In row 3, What Is Cmnd [0? Struct
Scsi_cmnd has such a member,

65 # define max_command_size 16

66 unsigned charcmnd [max_command_size];

The array contains 16 elements, which are the SCSI commands. To understand this condition, you must first look at the following fill_inquiry_response () function call.

Finally, "Paste" several devices with the us_fl_fix_inquiry flag. These devices are Sony's peg memory sticks or memory cards, which can be used in PDAs. Drivers/USB/storage/unusual_devs.h:

635/* submitted by Nathan babb <nathan@lexi.com> */

636 unusual_dev (0x054c, 0x006d, 0x0000, 0x9999,

637 "Sony ",

638 "pegmass storage ",

639 us_ SC _device, us_pr_device, null,

640 us_fl_fix_inquiry ),

641

642/* submitted by Mike alborn <malborn@deandra.homeip.net> */

643 unusual_dev (0x054c, 0x016a, 0x0000, 0x9999,

644 "Sony ",

645 "Peg mass storage ",

646 us_ SC _device, us_pr_device, null,

647 us_fl_fix_inquiry ),

648

649/* submitted by Frank Engel <frankie@cse.unsw.edu.au> */

650 unusual_dev (0x054c, 0x0099, 0x0000, 0x9999,

651 "Sony ",

652 "Peg mass storage ",

653 us_ SC _device, us_pr_device, null,

654 us_fl_fix_inquiry ),

655

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.