135 lines
3.9 KiB
C
135 lines
3.9 KiB
C
|
|
#include "usb.h"
|
|
|
|
#include "public.h"
|
|
|
|
|
|
int usbWrite(int sg_fd, unsigned char * pData,unsigned int nLength,int nTimeOut)
|
|
{
|
|
unsigned char pCDB[12]={0};
|
|
int nCDBLen=12;
|
|
unsigned char sense_buffer[32];
|
|
sg_io_hdr_t io_hdr;
|
|
//int rv = 0;
|
|
|
|
pCDB[0]=0xfe;
|
|
pCDB[1]=1;
|
|
|
|
if(sg_fd < 0)
|
|
{
|
|
//perror(" fd invalid");
|
|
return -1;
|
|
}
|
|
|
|
memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
|
|
io_hdr.interface_id = 'S';
|
|
io_hdr.cmd_len = nCDBLen;
|
|
/* io_hdr.iovec_count = 0; */ /* memset takes care of this */
|
|
io_hdr.mx_sb_len = sizeof(sense_buffer); //max sense buffer length(一般用于错误信息返回)
|
|
io_hdr.dxfer_direction = SG_DXFER_TO_DEV; //写入标记
|
|
io_hdr.dxfer_len = nLength; //写入数据长度
|
|
io_hdr.dxferp = pData; //写入数据
|
|
io_hdr.cmdp = pCDB; //命令
|
|
io_hdr.sbp = sense_buffer; //存储错误返回信息
|
|
io_hdr.timeout = nTimeOut; /* 20000 millisecs == 20 seconds */
|
|
/* io_hdr.flags = 0; */ /* take defaults: indirect IO, etc */
|
|
/* io_hdr.pack_id = 0; */
|
|
/* io_hdr.usr_ptr = NULL; */
|
|
|
|
if (ioctl(sg_fd, SG_IO, &io_hdr) < 0)
|
|
{
|
|
perror("USB Write-> SG_IO ioctl error");
|
|
return -1;
|
|
}
|
|
|
|
/* now for the error processing */
|
|
if ((io_hdr.info & SG_INFO_OK_MASK) != SG_INFO_OK)
|
|
{
|
|
if (io_hdr.sb_len_wr > 0)
|
|
{
|
|
printf("USB Write-> sense data: ");
|
|
int k;
|
|
for (k = 0; k < io_hdr.sb_len_wr; ++k)
|
|
{
|
|
if ((k > 0) && (0 == (k % 10)))
|
|
printf("\n ");
|
|
printf("0x%02x ", sense_buffer[k]);
|
|
}
|
|
printf("\n");
|
|
}
|
|
if (io_hdr.masked_status)
|
|
printf("USB Write-> SCSI status=0x%x\n", io_hdr.status);
|
|
if (io_hdr.host_status)
|
|
printf("USB Write-> host_status=0x%x\n", io_hdr.host_status);
|
|
if (io_hdr.driver_status)
|
|
printf("USB Write-> driver_status=0x%x\n", io_hdr.driver_status);
|
|
|
|
return -1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
int usbRead(int sg_fd, unsigned char * pData,unsigned int nLength,int nTimeOut)
|
|
{
|
|
unsigned char pCDB[12]={0};
|
|
int nCDBLen=12;
|
|
//int rv = 0;
|
|
unsigned char sense_buffer[32];
|
|
sg_io_hdr_t io_hdr;
|
|
pCDB[0]=0xfe;
|
|
pCDB[1]=2;
|
|
if(sg_fd < 0)
|
|
{
|
|
perror(" fd invalid");
|
|
return -1;
|
|
}
|
|
memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
|
|
io_hdr.interface_id = 'S';
|
|
io_hdr.cmd_len = nCDBLen; //命令长度
|
|
/* io_hdr.iovec_count = 0; */ /* memset takes care of this */
|
|
io_hdr.mx_sb_len = sizeof(sense_buffer); //max sense buffer length(一般用于错误信息返回)
|
|
io_hdr.dxfer_direction = SG_DXFER_FROM_DEV; //读标记
|
|
io_hdr.dxfer_len = nLength; //读入数据长度
|
|
io_hdr.dxferp = pData; //读入数据
|
|
io_hdr.cmdp = pCDB; //命令
|
|
io_hdr.sbp = sense_buffer; //存储错误返回信息
|
|
io_hdr.timeout = nTimeOut; /* 20000 millisecs == 20 seconds */
|
|
/* io_hdr.flags = 0; */ /* take defaults: indirect IO, etc */
|
|
/* io_hdr.pack_id = 0; */
|
|
/* io_hdr.usr_ptr = NULL; */
|
|
|
|
if (ioctl(sg_fd, SG_IO, &io_hdr) < 0)
|
|
{
|
|
perror("USB Read-> SG_IO ioctl error");
|
|
return -1;
|
|
}
|
|
|
|
/* now for the error processing */
|
|
if ((io_hdr.info & SG_INFO_OK_MASK) != SG_INFO_OK)
|
|
{
|
|
if (io_hdr.sb_len_wr > 0)
|
|
{
|
|
printf("USB Read-> sense data: ");
|
|
int k;
|
|
for (k = 0; k < io_hdr.sb_len_wr; ++k)
|
|
{
|
|
if ((k > 0) && (0 == (k % 10)))
|
|
printf("\n ");
|
|
printf("0x%02x ", sense_buffer[k]);
|
|
}
|
|
printf("\n");
|
|
}
|
|
if (io_hdr.masked_status)
|
|
printf("USB Read SCSI status=0x%x\n", io_hdr.status);
|
|
if (io_hdr.host_status)
|
|
printf("USB Read host_status=0x%x\n", io_hdr.host_status);
|
|
if (io_hdr.driver_status)
|
|
printf("USB Read driver_status=0x%x\n", io_hdr.driver_status);
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
|