| 1 |
/** |
| 2 |
* 円盤複写屋 - EnbanKensa |
| 3 |
* Copyright (C) 2005 Kagetani Hideto |
| 4 |
* scan.c - 光ディスク系ドライブ検索 |
| 5 |
* $Date: 2005/12/11 15:21:45 $ |
| 6 |
* $Revision: 1.5 $ |
| 7 |
*/ |
| 8 |
#include <stdio.h> |
| 9 |
#include <string.h> |
| 10 |
#include <stdlib.h> |
| 11 |
|
| 12 |
#include "aspi.h" |
| 13 |
#include "cmd.h" |
| 14 |
#include "scan.h" |
| 15 |
|
| 16 |
|
| 17 |
#define MAX_SCAN 16*6 |
| 18 |
|
| 19 |
extern void DebugLog(char *fmt, ...); |
| 20 |
|
| 21 |
#if !defined(MACOSX) |
| 22 |
static BOOL IsWriterDrive(CMDDRIVE *drive) |
| 23 |
{ |
| 24 |
int ret; |
| 25 |
struct _MODEPAGE2A *mp2a; |
| 26 |
|
| 27 |
ret = SendModeSense(drive, MSPC_CURRENT, 0x2a); |
| 28 |
if(ret!=RET_OK) |
| 29 |
return FALSE; |
| 30 |
mp2a = (struct _MODEPAGE2A *) |
| 31 |
(drive->data_buf + (drive->cmd_type==CMDDRVCTYPE_ATAPI ? 8:16)); |
| 32 |
if(mp2a->cd_r_write || mp2a->cd_rw_write || mp2a->dvd_r_write) |
| 33 |
return TRUE; |
| 34 |
|
| 35 |
/* GetConfig.も見ないとダメかも */ |
| 36 |
return FALSE; |
| 37 |
} |
| 38 |
#endif |
| 39 |
|
| 40 |
|
| 41 |
SCAN_t *ScanDrive(BOOL bWriter) |
| 42 |
{ |
| 43 |
#if !defined(MACOSX) |
| 44 |
const BYTE inqcmd[12]={CMD_INQUIRY, 0, 0, 0, 96, 0, 0, 0, 0, 0, 0, 0}; |
| 45 |
DWORD timeout=0; |
| 46 |
BYTE device_type; |
| 47 |
int ret; |
| 48 |
#endif |
| 49 |
int num_drive=0; |
| 50 |
int hid, tid, max_hid, max_tid; |
| 51 |
SCAN_t *drives; |
| 52 |
CMDDRIVE drive; |
| 53 |
|
| 54 |
drives = (SCAN_t *)malloc(sizeof(SCAN_t)*MAX_SCAN); |
| 55 |
|
| 56 |
#if defined(WIN32) || defined(linux) |
| 57 |
max_hid = GetHostAdapterCount(); |
| 58 |
max_tid = GetMaxTarget(); |
| 59 |
#else |
| 60 |
/* MacOSX */ |
| 61 |
max_hid = 1; |
| 62 |
max_tid = GetDriveCount(); |
| 63 |
#endif |
| 64 |
|
| 65 |
memset(&drive, 0, sizeof(CMDDRIVE)); |
| 66 |
InitializeCmdDrive(&drive, 0x10000); |
| 67 |
|
| 68 |
for(hid=0; hid<max_hid; hid++){ |
| 69 |
for(tid=0; tid<max_tid; tid++){ |
| 70 |
if(num_drive>=MAX_SCAN-1){ |
| 71 |
break; |
| 72 |
} |
| 73 |
DebugLog("[%d:%d]\n", hid, tid); |
| 74 |
#ifdef MACOSX |
| 75 |
if(GetInquiryString(tid, drive.data_buf, drive.bufsize)){ |
| 76 |
continue; |
| 77 |
} |
| 78 |
sprintf(drives[num_drive].inqdata, "[%d:%d] ", hid, tid); |
| 79 |
strcpy(drives[num_drive].inqdata+strlen(drives[num_drive].inqdata), |
| 80 |
drive.data_buf); |
| 81 |
#else |
| 82 |
DebugLog("[%d:%d] SetAspiSetting Start\n", hid, tid); |
| 83 |
if(SetDriveAspiSetting(&drive, &hid, &tid, &timeout)!=RET_OK){ |
| 84 |
DebugLog("[%d:%d] select error\n", hid, tid); |
| 85 |
continue; |
| 86 |
} |
| 87 |
DebugLog("[%d:%d] SetAspiSetting End\n", hid, tid); |
| 88 |
# ifdef WIN32 |
| 89 |
if(CheckDiscDevice(&drive.drive)==0){ |
| 90 |
DebugLog("[%d:%d] not a disc device\n", hid, tid); |
| 91 |
continue; |
| 92 |
} |
| 93 |
# endif |
| 94 |
memset(drive.data_buf, 0, 96*2); |
| 95 |
ret = SendCmd(&drive, (BYTE *)inqcmd, 96, REQ_DATAIN); |
| 96 |
if(ret!=RET_OK){ |
| 97 |
/*DebugLog("[%d:%d] inquiry command error(%d:%d)\n", hid, tid, ret, drive.srb->SRB_Status);*/ |
| 98 |
continue; |
| 99 |
} |
| 100 |
#if 0 |
| 101 |
if(!memcmp(drive.data_buf, drive.data_buf+96, 96)){ |
| 102 |
DebugLog("[%d:%d] no inquiry data\n", hid, tid); |
| 103 |
continue; |
| 104 |
} |
| 105 |
/* 何かデータを受信していればOKとする */ |
| 106 |
#endif |
| 107 |
device_type = drive.data_buf[0]&0x1f; |
| 108 |
if(device_type!=4 && device_type!=5){ |
| 109 |
DebugLog("[%d:%d] device=%d\n", hid, tid, device_type); |
| 110 |
continue; |
| 111 |
} |
| 112 |
DebugLog("[%d:%d] OK\n", hid, tid); |
| 113 |
#if defined(WIN32) |
| 114 |
if(UsingSPTI()) |
| 115 |
sprintf(drives[num_drive].inqdata, "[%c:] ", tid+'C'); |
| 116 |
else |
| 117 |
#endif |
| 118 |
sprintf(drives[num_drive].inqdata, "[%d:%d] ", hid, tid); |
| 119 |
drives[num_drive].inqdata[strlen(drives[num_drive].inqdata)+40]='\0'; |
| 120 |
strncpy(drives[num_drive].inqdata+strlen(drives[num_drive].inqdata), |
| 121 |
drive.data_buf+8, 40); |
| 122 |
#endif |
| 123 |
drives[num_drive].hid = hid; |
| 124 |
drives[num_drive].tid = tid; |
| 125 |
if(bWriter){ |
| 126 |
#ifdef MACOSX |
| 127 |
if(MMCIsWriterDrive(tid, drive.data_buf, drive.bufsize)==FALSE){ |
| 128 |
continue; |
| 129 |
} |
| 130 |
#else |
| 131 |
if(IsWriterDrive(&drive)==FALSE){ |
| 132 |
continue; |
| 133 |
} |
| 134 |
#endif |
| 135 |
} |
| 136 |
num_drive++; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
drives[num_drive].inqdata[0] = '\0'; |
| 141 |
|
| 142 |
FreeCmdDrive(&drive); |
| 143 |
return drives; |
| 144 |
} |
| 145 |
|