首页 > 系统 > Linux >

《Linux那些事儿之我是USB》我是U盘(31)SCSI命令之我型我秀

2011-12-03

usb_stor_control_thread()基本讲完了,但是其中下面这几行,正是高潮中的高潮。所谓的批量传输,所谓的Bulk-Only协议。正是在这里体现出来的。371 /* we've got a command, let's do it! */37...

usb_stor_control_thread()基本讲完了,但是其中下面这几行,正是高潮中的高潮。所谓的批量传输,所谓的Bulk-Only协议。正是在这里体现出来的。

371 /* we've got a command, let's do it! */

372 else {

373 US_DEBUG(usb_stor_show_command(us->srb));

374 us->proto_handler(us->srb,us);

375 }

所谓的US_DEBUG,我们前面已经讲过,无非就是打印条是信息的。而眼下这句话就是执行usb_stor_show_command(us->srb)这个函数,鉴于这个函数是我们自己写的,而且有意义,所以也就列出来。这个函数定义于drivers/usb/storage/debug.c中:

56 voidusb_stor_show_command(struct scsi_cmnd *srb)

57 {

58 char*what = NULL;

59 inti;

60

61 switch(srb->cmnd[0]) {

62 caseTEST_UNIT_READY: what = "TEST_UNIT_READY"; break;

63 caseREZERO_UNIT: what = "REZERO_UNIT"; break;

64 caseREQUEST_SENSE: what = "REQUEST_SENSE"; break;

65 caseFORMAT_UNIT: what = "FORMAT_UNIT"; break;

66 caseREAD_BLOCK_LIMITS: what = "READ_BLOCK_LIMITS"; break;

67 caseREASSIGN_BLOCKS: what = "REASSIGN_BLOCKS"; break;

68 caseREAD_6: what = "READ_6"; break;

69 caseWRITE_6: what = "WRITE_6"; break;

70 caseSEEK_6: what = "SEEK_6"; break;

71 caseREAD_REVERSE: what = "READ_REVERSE"; break;

72 caseWRITE_FILEMARKS: what = "WRITE_FILEMARKS"; break;

73 caseSPACE: what = "SPACE"; break;

74 caseINQUIRY: what = "INQUIRY"; break;

75 caseRECOVER_BUFFERED_DATA: what ="RECOVER_BUFFERED_DATA"; break;

76 caseMODE_SELECT: what = "MODE_SELECT"; break;

77 caseRESERVE: what = "RESERVE"; break;

78 caseRELEASE: what = "RELEASE"; break;

79 caseCOPY: what = "COPY"; break;

80 caseERASE: what = "ERASE"; break;

81 caseMODE_SENSE: what = "MODE_SENSE"; break;

82 caseSTART_STOP: what = "START_STOP"; break;

83 caseRECEIVE_DIAGNOSTIC: what = "RECEIVE_DIAGNOSTIC"; break;

84 case SEND_DIAGNOSTIC: what ="SEND_DIAGNOSTIC"; break;

85 caseALLOW_MEDIUM_REMOVAL: what = "ALLOW_MEDIUM_REMOVAL"; break;

86 caseSET_WINDOW: what = "SET_WINDOW"; break;

87 caseREAD_CAPACITY: what = "READ_CAPACITY"; break;

88 caseREAD_10: what = "READ_10"; break;

89 caseWRITE_10: what = "WRITE_10"; break;

90 caseSEEK_10: what = "SEEK_10"; break;

91 caseWRITE_VERIFY: what = "WRITE_VERIFY"; break;

92 caseVERIFY: what = "VERIFY"; break;

93 caseSEARCH_HIGH: what = "SEARCH_HIGH"; break;

94 caseSEARCH_EQUAL: what = "SEARCH_EQUAL"; break;

95 caseSEARCH_LOW: what = "SEARCH_LOW"; break;

96 caseSET_LIMITS: what = "SET_LIMITS"; break;

97 caseREAD_POSITION: what = "READ_POSITION"; break;

98 case SYNCHRONIZE_CACHE: what ="SYNCHRONIZE_CACHE"; break;

99 case LOCK_UNLOCK_CACHE: what ="LOCK_UNLOCK_CACHE"; break;

100 case READ_DEFECT_DATA: what ="READ_DEFECT_DATA"; break;

101 case MEDIUM_SCAN: what ="MEDIUM_SCAN"; break;

102 case COMPARE: what = "COMPARE";break;

103 case COPY_VERIFY: what ="COPY_VERIFY"; break;

104 case WRITE_BUFFER: what ="WRITE_BUFFER"; break;

105 case READ_BUFFER: what ="READ_BUFFER"; break;

106 case UPDATE_BLOCK: what ="UPDATE_BLOCK"; break;

107 case READ_LONG: what = "READ_LONG";break;

108 case WRITE_LONG: what ="WRITE_LONG"; break;

109 case CHANGE_DEFINITION: what ="CHANGE_DEFINITION"; break;

110 case WRITE_SAME: what ="WRITE_SAME"; break;

111 case GPCMD_READ_SUBCHANNEL: what = "READSUBCHANNEL"; break;

112 case READ_TOC: what = "READ_TOC";break;

113 case GPCMD_READ_HEADER: what = "READHEADER"; break;

114 case GPCMD_PLAY_AUDIO_10: what = "PLAYAUDIO (10)"; break;

115 case GPCMD_PLAY_AUDIO_MSF: what = "PLAYAUDIO MSF"; break;

116 case GPCMD_GET_EVENT_STATUS_NOTIFICATION:

117 what = "GET EVENT/STATUSNOTIFICATION"; break;

118 case GPCMD_PAUSE_RESUME: what ="PAUSE/RESUME"; break;

119 case LOG_SELECT: what ="LOG_SELECT"; break;

120 case LOG_SENSE: what = "LOG_SENSE";break;

121 case GPCMD_STOP_PLAY_SCAN: what = "STOPPLAY/SCAN"; break;

122 case GPCMD_READ_DISC_INFO: what = "READDISC INFORMATION"; break;

123 case GPCMD_READ_TRACK_RZONE_INFO:

124 what = "READ TRACK INFORMATION";break;

125 case GPCMD_RESERVE_RZONE_TRACK: what ="RESERVE TRACK"; break;

126 case GPCMD_SEND_OPC: what = "SENDOPC"; break;

127 case MODE_SELECT_10: what ="MODE_SELECT_10"; break;

128 case GPCMD_REPAIR_RZONE_TRACK: what ="REPAIR TRACK"; break;

129 case 0x59: what = "READ MASTER CUE";break;

130 case MODE_SENSE_10: what ="MODE_SENSE_10"; break;

131 case GPCMD_CLOSE_TRACK: what = "CLOSETRACK/SESSION"; break;

132 case 0x5C: what = "READ BUFFER CAPACITY";break;

133 case 0x5D: what = "SEND CUESHEET"; break;

134 case GPCMD_BLANK: what = "BLANK";break;

135 case REPORT_LUNS: what = "REPORTLUNS"; break;

136 case MOVE_MEDIUM: what = "MOVE_MEDIUM orPLAY AUDIO (12)"; break;

137 case READ_12: what = "READ_12";break;

138 case WRITE_12: what = "WRITE_12";break;

139 case WRITE_VERIFY_12: what ="WRITE_VERIFY_12"; break;

140 case SEARCH_HIGH_12: what ="SEARCH_HIGH_12"; break;

141 case SEARCH_EQUAL_12: what ="SEARCH_EQUAL_12"; break;

142 case SEARCH_LOW_12: what ="SEARCH_LOW_12"; break;

143 case SEND_VOLUME_TAG: what ="SEND_VOLUME_TAG"; break;

144 case READ_ELEMENT_STATUS: what ="READ_ELEMENT_STATUS"; break;

145 case GPCMD_READ_CD_MSF: what = "READ CDMSF"; break;

146 case GPCMD_SCAN: what = "SCAN";break;

147 case GPCMD_SET_SPEED: what = "SET CDSPEED"; break;

148 case GPCMD_MECHANISM_STATUS: what ="MECHANISM STATUS"; break;

149 case GPCMD_READ_CD: what = "READCD"; break;

150 case 0xE1: what = "WRITE CONTINUE";break;

151 case WRITE_LONG_2: what ="WRITE_LONG_2"; break;

152 default: what = "(unknown command)";break;

153 }

154 US_DEBUGP("Command %s (%d Bytes)\n",what, srb->cmd_len);

155 US_DEBUGP("");

156 for (i = 0; i < srb->cmd_len &&i < 16; i++)

157 US_DEBUGPX(" %02x", srb->cmnd[i]);

158 US_DEBUGPX("\n");

159 }

这个函数,很简单,就是把要执行的SCSI命令打印出来。列出这个函数没别的意思,让不熟悉SCSI的读者知道基本上会遇到一些什么命令。显然,刚才说的那个INQUIRY也包含在其中。

不过别看这个函数很简单,你要是不熟悉SCSI协议的话,你还真的解释不了这个函数。比如你说srb->cmnd[]这个数组到底是什么内容?有什么格式?为什么函数一开始只判断cmnd[0]?实不相瞒,这里边还真有学问。首先,在SCSI的规范里边定义了一些命令,每个命令都有一定的格式,命令的字节数也有好几种,有的命令是6个字节的,有的命令是10个字节的,有的命令是12个字节的。如图4.33.1、图4.33.2和图4.33.3所示,SCSI命令就该是这个样子。

人们把这样几个字节的命令称之为CDB(Command DescriptorBlock,命令描述符块)。而我们为CDB准备了一个字符数组,结构体struct scsi_cmnd中的unsigned char cmnd[16],最大就12个字节,为什么不申请一个12个字节的数组?

既然这个CDB有16个字节,那么为什么我们每次都判断cmnd[0]就够了?仔细看这三幅图,注意到Operation code了吗?没错,三幅图中的第1个字节都被称为Operationcode,换而言之,不管是什么样子的命令,都必须在第1个字节里签上自己的名字,向世人说明你是谁。于是在include/scsi/scsi.h中,定义了好多宏,比如#define INQUIRY 0x12,#define READ_6 0x08,#define FORMAT_UNIT 0x04,实际上操作码就相当于SCSI命令的序列号,SCSI命令总共也就那么多,8位的操作码已经足够表示了,因此,我们只要用一个字节就可以判断出这是哪个命令了。

好了,命令说完了,开始进入真正处理命令的部分了。

相关文章
最新文章
热点推荐