📄 scsi-linux.c
字号:
return(SCSI_ERROR); } memset(buffer, 0, SCSI_OFF + DataBufferLength); status = read(pDev[DeviceFD].fd, buffer, SCSI_OFF + DataBufferLength); memset(pRequestSense, 0, RequestSenseLength); memcpy(pRequestSense, psg_header->sense_buffer, 16); if ( (status < 0) || (status != (ssize_t)(SCSI_OFF + DataBufferLength)) || (psg_header->result != 0)) { dbprintf(_("SCSI_ExecuteCommand error read \n")); dbprintf(_("Status %zd (%zd) %2X\n"), status, SCSI_OFF + DataBufferLength,psg_header->result ); SCSI_CloseDevice(DeviceFD); amfree(buffer); return(SCSI_ERROR); } if (DataBufferLength) { memcpy(DataBuffer, buffer + SCSI_OFF, DataBufferLength); } SCSI_CloseDevice(DeviceFD); amfree(buffer); return(SCSI_OK);}#elsestatic inline int min(int x, int y){ return (x < y ? x : y);}static inline int max(int x, int y){ return (x > y ? x : y);}int SCSI_OpenDevice(int ip){ int DeviceFD; int i; if (pDev[ip].inqdone == 0) { pDev[ip].inqdone = 1; if ((DeviceFD = open(pDev[ip].dev, O_RDWR)) >= 0) { pDev[ip].avail = 1; pDev[ip].fd = DeviceFD; pDev[ip].SCSI = 0; pDev[ip].inquiry = (SCSIInquiry_T *)malloc(INQUIRY_SIZE); dbprintf(_("SCSI_OpenDevice : use ioctl interface\n")); if (SCSI_Inquiry(ip, pDev[ip].inquiry, (u_char)INQUIRY_SIZE) == 0) { if (pDev[ip].inquiry->type == TYPE_TAPE || pDev[ip].inquiry->type == TYPE_CHANGER) { for (i=0;i < 16 && pDev[ip].inquiry->prod_ident[i] != ' ';i++) pDev[ip].ident[i] = pDev[ip].inquiry->prod_ident[i]; pDev[ip].ident[i] = '\0'; pDev[ip].SCSI = 1; PrintInquiry(pDev[ip].inquiry); return(1); } else { amfree(pDev[ip].inquiry); close(DeviceFD); return(0); } } else { close(DeviceFD); amfree(pDev[ip].inquiry); pDev[ip].inquiry = NULL; return(1); } } return(1); } else { if ((DeviceFD = open(pDev[ip].dev, O_RDWR)) >= 0) { pDev[ip].fd = DeviceFD; pDev[ip].devopen = 1; return(1); } else { pDev[ip].devopen = 0; return(0); } }}int SCSI_ExecuteCommand(int DeviceFD, Direction_T Direction, CDB_T CDB, int CDB_Length, void *DataBuffer, int DataBufferLength, RequestSense_T *pRequestSense, int RequestSenseLength){ unsigned char *Command; int Zero = 0, Result; if (pDev[DeviceFD].avail == 0) { return(SCSI_ERROR); } if (pDev[DeviceFD].devopen == 0) { if (SCSI_OpenDevice(DeviceFD) == 0) return(-1); } memset(pRequestSense, 0, RequestSenseLength); switch (Direction) { case Input: Command = (unsigned char *) malloc(8 + max(DataBufferLength, RequestSenseLength)); memcpy(&Command[0], &Zero, 4); memcpy(&Command[4], &DataBufferLength, 4); memcpy(&Command[8], CDB, CDB_Length); break; case Output: Command = (unsigned char *) malloc(8 + max(CDB_Length + DataBufferLength, RequestSenseLength)); memcpy(&Command[0], &DataBufferLength, 4); memcpy(&Command[4], &Zero, 4); memcpy(&Command[8], CDB, CDB_Length); memcpy(&Command[8 + CDB_Length], DataBuffer, DataBufferLength); break; } DecodeSCSI(CDB, "SCSI_ExecuteCommand : "); Result = ioctl(pDev[DeviceFD].fd, SCSI_IOCTL_SEND_COMMAND, Command); if (Result != 0) memcpy(pRequestSense, &Command[8], RequestSenseLength); else if (Direction == Input) memcpy(DataBuffer, &Command[8], DataBufferLength); amfree(Command); SCSI_CloseDevice(DeviceFD); switch(Result) { case 0: return(SCSI_OK); break; default: return(SCSI_SENSE); break; }}#endif/* * Send the command to the device with the * ioctl interface */int Tape_Ioctl( int DeviceFD, int command){ struct mtop mtop; int ret = 0; if (pDev[DeviceFD].devopen == 0) { if (SCSI_OpenDevice(DeviceFD) == 0) return(-1); } switch (command) { case IOCTL_EJECT: mtop.mt_op = MTOFFL; mtop.mt_count = 1; break; default: break; } if (ioctl(pDev[DeviceFD].fd , MTIOCTOP, &mtop) != 0) { dbprintf(_("Tape_Ioctl error ioctl %s\n"),strerror(errno)); SCSI_CloseDevice(DeviceFD); return(-1); } SCSI_CloseDevice(DeviceFD); return(ret); }int Tape_Status( int DeviceFD){ struct mtget mtget; int ret = 0; memset(&mtget, 0, SIZEOF(mtget)); if (pDev[DeviceFD].devopen == 0) { if (SCSI_OpenDevice(DeviceFD) == 0) return(-1); } if (ioctl(pDev[DeviceFD].fd , MTIOCGET, &mtget) != 0) { DebugPrint(DEBUG_ERROR, SECTION_TAPE,_("Tape_Status error ioctl %s\n"), strerror(errno)); SCSI_CloseDevice(DeviceFD); return(-1); } DebugPrint(DEBUG_INFO, SECTION_TAPE,_("ioctl -> mtget.mt_gstat %lX\n"),mtget.mt_gstat); if (GMT_ONLINE(mtget.mt_gstat)) { ret = TAPE_ONLINE; } if (GMT_BOT(mtget.mt_gstat)) { ret = ret | TAPE_BOT; } if (GMT_EOT(mtget.mt_gstat)) { ret = ret | TAPE_EOT; } if (GMT_WR_PROT(mtget.mt_gstat)) { ret = ret | TAPE_WR_PROT; } if (GMT_DR_OPEN(mtget.mt_gstat)) { ret = ret | TAPE_NOT_LOADED; } SCSI_CloseDevice(DeviceFD); return(ret); }/* * This functions scan all /dev/sg* devices * It opens the device an print the result of the inquiry * */int ScanBus(int print){ DIR *dir; struct dirent *dirent; int count = 0; if ((dir = opendir("/dev/")) == NULL) { dbprintf(_("/dev/ error: %s"), strerror(errno)); return 0; } while ((dirent = readdir(dir)) != NULL) { if (strstr(dirent->d_name, "sg") != NULL) { pDev[count].dev = malloc(10); pDev[count].inqdone = 0; g_snprintf(pDev[count].dev, SIZEOF(pDev[count].dev), "/dev/%s", dirent->d_name); if (OpenDevice(count,pDev[count].dev, "Scan", NULL )) { SCSI_CloseDevice(count); pDev[count].inqdone = 0; if (print) { g_printf(_("name /dev/%s "), dirent->d_name); switch (pDev[count].inquiry->type) { case TYPE_DISK: g_printf(_("Disk")); break; case TYPE_TAPE: g_printf(_("Tape")); break; case TYPE_PRINTER: g_printf(_("Printer")); break; case TYPE_PROCESSOR: g_printf(_("Processor")); break; case TYPE_WORM: g_printf(_("Worm")); break; case TYPE_CDROM: g_printf(_("Cdrom")); break; case TYPE_SCANNER: g_printf(_("Scanner")); break; case TYPE_OPTICAL: g_printf(_("Optical")); break; case TYPE_CHANGER: g_printf(_("Changer")); break; case TYPE_COMM: g_printf(_("Comm")); break; default: g_printf(_("unknown %d"),pDev[count].inquiry->type); break; } g_printf("\n"); } count++; g_printf(_("Count %d\n"),count); } else { amfree(pDev[count].dev); pDev[count].dev=NULL; } } } return 0;}/* * Local variables: * indent-tabs-mode: nil * c-file-style: gnu * End: */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -