使用ioctl向usb设备发送自定义scsi命令

使用ioctl向usb设备发送自定义scsi命令,usb,ioctl,scsi,Usb,Ioctl,Scsi,我在一个usb设备中实现了一些自定义的scsi命令。一个是:{0xB1,0xF5,0,0,0,0,0,0,0,0,0,0,0,1,0,0} 我使用以下代码将这个自定义命令发送到usb设备。操作系统是ubuntu 14.04 int main(int argc, char * argv[]) { int sg_fd, k, i; unsigned char readCmdBlk[READ_CMD_LEN] = { 0xB1, 0xF5, 0, 0,

我在一个usb设备中实现了一些自定义的
scsi
命令。一个是:
{0xB1,0xF5,0,0,0,0,0,0,0,0,0,0,0,1,0,0}

我使用以下代码将这个自定义命令发送到usb设备。操作系统是ubuntu 14.04

int main(int argc, char * argv[])
{
    int sg_fd, k, i;
    unsigned char readCmdBlk[READ_CMD_LEN] =
                { 0xB1, 0xF5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 };

unsigned char readBuff[READ_REPLY_LEN];
unsigned char sense_buffer[32];
sg_io_hdr_t io_hdr;

if (2 != argc) {
    printf("Usage: 'sg_simple0 <sg_device>'\n");
    return 1;
}
if ((sg_fd = open(argv[1], O_RDWR)) < 0) {
    perror("error opening given file name");
    return 1;
}
if ((ioctl(sg_fd, SG_GET_VERSION_NUM, &k) < 0) || (k < 30000)) {
    printf("%s is not an sg device, or old sg driver\n", argv[1]);
    return 1;
}
memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
io_hdr.interface_id = 'S';
io_hdr.cmd_len = sizeof(readCmdBlk);
io_hdr.mx_sb_len = sizeof(sense_buffer);
io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
io_hdr.dxfer_len = READ_REPLY_LEN;
io_hdr.dxferp = readBuff;
io_hdr.cmdp = readCmdBlk;
io_hdr.sbp = sense_buffer;
io_hdr.timeout = 20000;     /* 20000 millisecs == 20 seconds */

if (ioctl(sg_fd, SG_IO, &io_hdr) < 0) {
    perror("sg_simple0: Inquiry SG_IO ioctl error");
    return 1;
}
intmain(intargc,char*argv[])
{
int sg_fd,k,i;
未签名字符readCmdBlk[READ_CMD_LEN]=
{0xB1,0xF5,0,0,0,0,0,0,0,0,0,0,0,1,0,0};
未签名字符readBuff[READ_REPLY_LEN];
无符号字符检测缓冲区[32];
sg_io_hdr_t io_hdr;
如果(2!=argc){
printf(“用法:'sg_simple0'\n”);
返回1;
}
如果((sg_fd=open(argv[1],O_RDWR))<0){
perror(“打开给定文件名时出错”);
返回1;
}
if((ioctl(sg_fd,sg_GET_VERSION_NUM,&k)<0)|(k<30000)){
printf(“%s”不是sg设备或旧sg驱动程序,\n“,argv[1]);
返回1;
}
memset(&io_hdr,0,sizeof(sg_io_hdr_t));
io_hdr.interface_id='S';
io_hdr.cmd_len=sizeof(readCmdBlk);
io_hdr.mx_sb_len=sizeof(感测缓冲区);
io_hdr.dxfer_direction=SG_dxfer_FROM_DEV;
io_hdr.dxfer_len=读取_回复_len;
io_hdr.dxferp=readBuff;
io_hdr.cmdp=readCmdBlk;
io_hdr.sbp=感测缓冲区;
io_hdr.timeout=20000;/*20000毫秒==20秒*/
如果(ioctl(sg\U fd、sg\U IO和IO\U hdr)<0){
perror(“sg_simple0:查询sg_IO ioctl错误”);
返回1;
}
}

我使用
debugfs
捕获在usb总线上发出的命令,发现发出的命令是:
{0xB1、0x15、0、0、0、0、0、0、0、0、0、0、0、0、1、0、0}

第二个字节从
0xF5
更改为
0x15
。通过尝试其他一些命令,我发现第二个字节的前三位总是设置为零

为什么发出命令时,
ioctl
会更改命令内容

如何使
ioctl
发送与我定义的完全相同的命令