Linux那些事儿之我是U盘(42)我型我show--scsi命令集体show

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

372                 /* we've got a command, let's do it! */
    373                 else {
    374                         US_DEBUG(usb_stor_show_command(us->srb));
    375                         us->proto_handler(us->srb, us);
    376                 }

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

     55 void usb_stor_show_command(struct scsi_cmnd *srb)
     56 {
     57         char *what = NULL;
     58         int i;
     59
     60         switch (srb->cmnd[0]) {
     61         case TEST_UNIT_READY: what = "TEST_UNIT_READY"; break;
     62         case REZERO_UNIT: what = "REZERO_UNIT"; break;
     63         case REQUEST_SENSE: what = "REQUEST_SENSE"; break;
     64         case FORMAT_UNIT: what = "FORMAT_UNIT"; break;
     65         case READ_BLOCK_LIMITS: what = "READ_BLOCK_LIMITS"; break;
     66         case REASSIGN_BLOCKS: what = "REASSIGN_BLOCKS"; break;
     67         case READ_6: what = "READ_6"; break;
     68         case WRITE_6: what = "WRITE_6"; break;
     69         case SEEK_6: what = "SEEK_6"; break;
     70         case READ_REVERSE: what = "READ_REVERSE"; break;
     71         case WRITE_FILEMARKS: what = "WRITE_FILEMARKS"; break;
     72         case SPACE: what = "SPACE"; break;
     73         case INQUIRY: what = "INQUIRY"; break;
     74         case RECOVER_BUFFERED_DATA: what = "RECOVER_BUFFERED_DATA"; break;
     75         case MODE_SELECT: what = "MODE_SELECT"; break;
     76         case RESERVE: what = "RESERVE"; break;
     77         case RELEASE: what = "RELEASE"; break;
     78         case COPY: what = "COPY"; break;
     79         case ERASE: what = "ERASE"; break;
     80         case MODE_SENSE: what = "MODE_SENSE"; break;
     81         case START_STOP: what = "START_STOP"; break;
     82         case RECEIVE_DIAGNOSTIC: what = "RECEIVE_DIAGNOSTIC"; break;
     83         case SEND_DIAGNOSTIC: what = "SEND_DIAGNOSTIC"; break;
     84         case ALLOW_MEDIUM_REMOVAL: what = "ALLOW_MEDIUM_REMOVAL"; break;
     85         case SET_WINDOW: what = "SET_WINDOW"; break;
     86         case READ_CAPACITY: what = "READ_CAPACITY"; break;
     87         case READ_10: what = "READ_10"; break;
     88         case WRITE_10: what = "WRITE_10"; break;
     89         case SEEK_10: what = "SEEK_10"; break;
     90         case WRITE_VERIFY: what = "WRITE_VERIFY"; break;
     91         case VERIFY: what = "VERIFY"; break;
     92         case SEARCH_HIGH: what = "SEARCH_HIGH"; break;
     93         case SEARCH_EQUAL: what = "SEARCH_EQUAL"; break;
     94         case SEARCH_LOW: what = "SEARCH_LOW"; break;
     95         case SET_LIMITS: what = "SET_LIMITS"; break;
     96         case READ_POSITION: what = "READ_POSITION"; break;
     97         case SYNCHRONIZE_CACHE: what = "SYNCHRONIZE_CACHE"; break;
     98         case LOCK_UNLOCK_CACHE: what = "LOCK_UNLOCK_CACHE"; break;
     99         case READ_DEFECT_DATA: what = "READ_DEFECT_DATA"; break;
    100         case MEDIUM_SCAN: what = "MEDIUM_SCAN"; break;
    101         case COMPARE: what = "COMPARE"; break;
    102         case COPY_VERIFY: what = "COPY_VERIFY"; break;
    103         case WRITE_BUFFER: what = "WRITE_BUFFER"; break;
    104         case READ_BUFFER: what = "READ_BUFFER"; break;
    105         case UPDATE_BLOCK: what = "UPDATE_BLOCK"; break;
    106         case READ_LONG: what = "READ_LONG"; break;
    107         case WRITE_LONG: what = "WRITE_LONG"; break;
    108         case CHANGE_DEFINITION: what = "CHANGE_DEFINITION"; break;
    109         case WRITE_SAME: what = "WRITE_SAME"; break;
    110         case GPCMD_READ_SUBCHANNEL: what = "READ SUBCHANNEL"; break;
    111         case READ_TOC: what = "READ_TOC"; break;
    112         case GPCMD_READ_HEADER: what = "READ HEADER"; break;
    113         case GPCMD_PLAY_AUDIO_10: what = "PLAY AUDIO (10)"; break;
    114         case GPCMD_PLAY_AUDIO_MSF: what = "PLAY AUDIO MSF"; break;
    115         case GPCMD_GET_EVENT_STATUS_NOTIFICATION:
    116                 what = "GET EVENT/STATUS NOTIFICATION"; break;
    117         case GPCMD_PAUSE_RESUME: what = "PAUSE/RESUME"; break;
    118         case LOG_SELECT: what = "LOG_SELECT"; break;
    119         case LOG_SENSE: what = "LOG_SENSE"; break;
    120         case GPCMD_STOP_PLAY_SCAN: what = "STOP PLAY/SCAN"; break;
    121         case GPCMD_READ_DISC_INFO: what = "READ DISC INFORMATION"; break;
    122         case GPCMD_READ_TRACK_RZONE_INFO:
    123                 what = "READ TRACK INFORMATION"; break;
    124         case GPCMD_RESERVE_RZONE_TRACK: what = "RESERVE TRACK"; break;
    125         case GPCMD_SEND_OPC: what = "SEND OPC"; break;
    126         case MODE_SELECT_10: what = "MODE_SELECT_10"; break;
    127         case GPCMD_REPAIR_RZONE_TRACK: what = "REPAIR TRACK"; break;
    128         case 0x59: what = "READ MASTER CUE"; break;
    129         case MODE_SENSE_10: what = "MODE_SENSE_10"; break;
    130         case GPCMD_CLOSE_TRACK: what = "CLOSE TRACK/SESSION"; break;
    131         case 0x5C: what = "READ BUFFER CAPACITY"; break;
    132         case 0x5D: what = "SEND CUE SHEET"; break;
    133         case GPCMD_BLANK: what = "BLANK"; break;
    134         case MOVE_MEDIUM: what = "MOVE_MEDIUM or PLAY AUDIO (12)"; break;
    135         case READ_12: what = "READ_12"; break;
    136         case WRITE_12: what = "WRITE_12"; break;
    137         case WRITE_VERIFY_12: what = "WRITE_VERIFY_12"; break;
    138         case SEARCH_HIGH_12: what = "SEARCH_HIGH_12"; break;
    139         case SEARCH_EQUAL_12: what = "SEARCH_EQUAL_12"; break;
    140         case SEARCH_LOW_12: what = "SEARCH_LOW_12"; break;
    141         case SEND_VOLUME_TAG: what = "SEND_VOLUME_TAG"; break;
    142         case READ_ELEMENT_STATUS: what = "READ_ELEMENT_STATUS"; break;
    143         case GPCMD_READ_CD_MSF: what = "READ CD MSF"; break;
    144         case GPCMD_SCAN: what = "SCAN"; break;
    145         case GPCMD_SET_SPEED: what = "SET CD SPEED"; break;
    146         case GPCMD_MECHANISM_STATUS: what = "MECHANISM STATUS"; break;
    147         case GPCMD_READ_CD: what = "READ CD"; break;
    148         case 0xE1: what = "WRITE CONTINUE"; break;
    149         case WRITE_LONG_2: what = "WRITE_LONG_2"; break;
    150         default: what = "(unknown command)"; break;
    151         }
    152         US_DEBUGP("Command %s (%d bytes)/n", what, srb->cmd_len);
    153         US_DEBUGP("");
    154         for (i = 0; i < srb->cmd_len && i < 16; i++)
    155                 US_DEBUGPX(" %02x", srb->cmnd[i]);
    156         US_DEBUGPX("/n");
    157 }
  相信即使是天下无贼里边的傻根也能看懂这个函数,很简单,就是把要执行的scsi命令打印出来.列出这个函数没别的意思,让不熟悉scsi的同志们知道基本上会遇到些啥命令.显然,刚才说的那个INQUIRY也包含在其中的.

不过别看这个函数很easy,你要是不熟悉scsi协议的话,你还真的解释不了这个函数.比如你说srb->cmnd[]这个数组到底是什么内容?有什么格式?为啥函数一开始只判断cmnd[0]?实不相瞒,这里边还真有学问.首先,scsi的规范里边定义了一些命令,每个命令都有一定的格式,命令的字节数也有好几种,有的命令是六个字节的,有的命令是10个字节的,有的命令是12个字节的.你看,你看,下面就是摘自scsi规范里边的几张图,scsi命令就该是这个样子.

这是6字节的,

这是10个字节的,

这是12个字节的.

之所以有这好几种命令描述块,没什么特别的,也许只是想体现生物的多样性而已,又或许,印证了张爱玲的那句话,也许每一个男子全都有过这样的两个女人,至少两个.娶了红玫瑰,久而久之,红的变成了墙上的一抹蚊子血,白的还是窗前明月光;娶了白玫瑰,白的便是衣服上的一粒饭粘子,红的却是心口上的一颗朱砂痣.

江湖中人们把这样几个字节的命令称之为CDB, command descriptor block, 命令描述符块.而我们为CDB准备了一个字符数组,结构体struct scsi_cmnd中的unsigned char cmnd[16],你说最大就12个字节,干嘛不申请一个12个字节的数组?给你一个建议:我记得复旦并没有一门课程叫做可持续性发展,但是我记得我上海交大的同学有修过这样一门课程,有机会的话,去交大听一下吧.

Ok,既然这个CDB16个字节,那么为什么我们每次都判断cmnd[0]就够了?仔细看这三幅图,注意到那个Operation code了吗?没错,三幅图中的第一个字节都被称为Operation code,换言之,不管你是什么样子的命令, 你都必须在第一个字节里签上自己的名字,向世人说明你是谁.于是在include/scsi/scsi.h,定义了好多好多宏,比如#define INQUIRY 0x12,又比如#define READ_6 0x08,再比如#define FORMAT_UNIT 0x04,够了,实际上操作码就相当于scsi命令的序列号,scsi命令总共也就那么多,8位的操作码已经足够表示了,因此,我们只要用一个字节就可以判断出这是哪个命令了. 因为你的第一个字节就相当于你的眼睛,不管你埋藏的多深,你会发现最终总是你的眼睛背叛了你的心,这一点郑中基大概感受颇深吧.

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

你可能感兴趣的:(linux,command,search,buffer,audio,Descriptor)