19 #include "debugtools.h"
21 DEFAULT_DEBUG_CHANNEL(aspi
);
24 * 1) Residual byte length reporting not handled
25 * 2) Make this code re-entrant for multithreading
26 * -- Added CriticalSection to OpenDevices function
27 * 3) Only linux supported so far
28 * 4) Leaves sg devices open. This may or may not be okay. A better solution
29 * would be to close the file descriptors when the thread/process using
30 * them no longer needs them.
35 static ASPI_DEVICE_INFO
*ASPI_open_devices
= NULL
;
36 static CRITICAL_SECTION ASPI_CritSection
= CRITICAL_SECTION_INIT
;
38 #endif /* defined(linux) */
41 BOOL WINAPI
WNASPI32_LibMain(HINSTANCE hInstDLL
, DWORD fdwReason
, LPVOID fImpLoad
)
46 case DLL_PROCESS_ATTACH
:
49 case DLL_PROCESS_DETACH
:
50 DeleteCriticalSection( &ASPI_CritSection
);
52 case DLL_THREAD_ATTACH
:
53 case DLL_THREAD_DETACH
:
56 #endif /* defined(linux) */
64 ASPI_OpenDevice(SRB_ExecSCSICmd
*prb
)
68 ASPI_DEVICE_INFO
*curr
;
70 /* search list of devices to see if we've opened it already.
71 * There is not an explicit open/close in ASPI land, so hopefully
72 * keeping a device open won't be a problem.
75 EnterCriticalSection(&ASPI_CritSection
);
76 for (curr
= ASPI_open_devices
; curr
; curr
= curr
->next
) {
77 if (curr
->hostId
== prb
->SRB_HaId
&&
78 curr
->target
== prb
->SRB_Target
&&
79 curr
->lun
== prb
->SRB_Lun
) {
80 LeaveCriticalSection(&ASPI_CritSection
);
84 LeaveCriticalSection(&ASPI_CritSection
);
86 hc
= ASPI_GetHCforController( prb
->SRB_HaId
);
87 fd
= SCSI_OpenDevice( HIWORD(hc
), LOWORD(hc
), prb
->SRB_Target
, prb
->SRB_Lun
);
93 /* device is now open */
94 /* FIXME: Let users specify SCSI timeout in registry */
95 SCSI_LinuxSetTimeout( fd
, SCSI_DEFAULT_TIMEOUT
);
97 curr
= HeapAlloc( GetProcessHeap(), 0, sizeof(ASPI_DEVICE_INFO
) );
99 curr
->hostId
= prb
->SRB_HaId
;
100 curr
->target
= prb
->SRB_Target
;
101 curr
->lun
= prb
->SRB_Lun
;
103 /* insert new record at beginning of open device list */
104 EnterCriticalSection(&ASPI_CritSection
);
105 curr
->next
= ASPI_open_devices
;
106 ASPI_open_devices
= curr
;
107 LeaveCriticalSection(&ASPI_CritSection
);
113 ASPI_DebugPrintCmd(SRB_ExecSCSICmd
*prb
)
119 switch (prb
->CDBByte
[0]) {
121 TRACE("INQUIRY {\n");
122 TRACE("\tEVPD: %d\n", prb
->CDBByte
[1] & 1);
123 TRACE("\tLUN: %d\n", (prb
->CDBByte
[1] & 0xc) >> 1);
124 TRACE("\tPAGE CODE: %d\n", prb
->CDBByte
[2]);
125 TRACE("\tALLOCATION LENGTH: %d\n", prb
->CDBByte
[4]);
126 TRACE("\tCONTROL: %d\n", prb
->CDBByte
[5]);
130 TRACE("Transfer Length: %d\n", prb
->CDBByte
[4]);
134 TRACE("Host Adapter: %d\n", prb
->SRB_HaId
);
135 TRACE("Flags: %d\n", prb
->SRB_Flags
);
136 if (TARGET_TO_HOST(prb
)) {
137 TRACE("\tData transfer: Target to host. Length checked.\n");
139 else if (HOST_TO_TARGET(prb
)) {
140 TRACE("\tData transfer: Host to target. Length checked.\n");
142 else if (NO_DATA_TRANSFERED(prb
)) {
143 TRACE("\tData transfer: none\n");
146 WARN("\tTransfer by scsi cmd. Length not checked.\n");
149 TRACE("\tResidual byte length reporting %s\n", prb
->SRB_Flags
& 0x4 ? "enabled" : "disabled");
150 TRACE("\tLinking %s\n", prb
->SRB_Flags
& 0x2 ? "enabled" : "disabled");
151 TRACE("\tPosting %s\n", prb
->SRB_Flags
& 0x1 ? "enabled" : "disabled");
152 TRACE("Target: %d\n", prb
->SRB_Target
);
153 TRACE("Lun: %d\n", prb
->SRB_Lun
);
154 TRACE("BufLen: %ld\n", prb
->SRB_BufLen
);
155 TRACE("SenseLen: %d\n", prb
->SRB_SenseLen
);
156 TRACE("BufPtr: %p\n", prb
->SRB_BufPointer
);
157 TRACE("CDB Length: %d\n", prb
->SRB_CDBLen
);
158 TRACE("POST Proc: %lx\n", (DWORD
) prb
->SRB_PostProc
);
159 cdb
= &prb
->CDBByte
[0];
160 cmd
= prb
->CDBByte
[0];
161 if (TRACE_ON(aspi
)) {
162 DPRINTF("CDB buffer[");
163 for (i
= 0; i
< prb
->SRB_CDBLen
; i
++) {
164 if (i
!= 0) DPRINTF(",");
165 DPRINTF("%02x", *cdb
++);
172 ASPI_PrintCDBArea(SRB_ExecSCSICmd
*prb
)
178 for (i
= 0; i
< prb
->SRB_CDBLen
; i
++) {
180 DPRINTF("%02x", prb
->CDBByte
[i
]);
187 ASPI_PrintSenseArea(SRB_ExecSCSICmd
*prb
)
190 BYTE
*rqbuf
= prb
->SenseArea
;
194 DPRINTF("Request Sense reports:\n");
195 if ((rqbuf
[0]&0x7f)!=0x70) {
196 DPRINTF("\tInvalid sense header: 0x%02x instead of 0x70\n", rqbuf
[0]&0x7f);
199 DPRINTF("\tCurrent command read filemark: %s\n",(rqbuf
[2]&0x80)?"yes":"no");
200 DPRINTF("\tEarly warning passed: %s\n",(rqbuf
[2]&0x40)?"yes":"no");
201 DPRINTF("\tIncorrect blocklength: %s\n",(rqbuf
[2]&0x20)?"yes":"no");
202 DPRINTF("\tSense Key: %d\n",rqbuf
[2]&0xf);
204 DPRINTF("\tResidual Length: %d\n",rqbuf
[3]*0x1000000+rqbuf
[4]*0x10000+rqbuf
[5]*0x100+rqbuf
[6]);
205 DPRINTF("\tAdditional Sense Length: %d\n",rqbuf
[7]);
206 DPRINTF("\tAdditional Sense Code: %d\n",rqbuf
[12]);
207 DPRINTF("\tAdditional Sense Code Qualifier: %d\n",rqbuf
[13]);
208 if (rqbuf
[15]&0x80) {
209 DPRINTF("\tIllegal Param is in %s\n",(rqbuf
[15]&0x40)?"the CDB":"the Data Out Phase");
211 DPRINTF("Pointer at %d, bit %d\n",rqbuf
[16]*256+rqbuf
[17],rqbuf
[15]&0x7);
214 DPRINTF("SenseArea[");
215 for (i
= 0; i
< prb
->SRB_SenseLen
; i
++) {
217 DPRINTF("%02x", *rqbuf
++);
224 ASPI_DebugPrintResult(SRB_ExecSCSICmd
*prb
)
227 TRACE("SRB_Status: %x\n", prb
->SRB_Status
);
228 TRACE("SRB_HaStat: %x\n", prb
->SRB_HaStat
);
229 TRACE("SRB_TargStat: %x\n", prb
->SRB_TargStat
);
230 switch (prb
->CDBByte
[0]) {
232 TRACE("Vendor: '%s'\n", prb
->SRB_BufPointer
+ INQUIRY_VENDOR
);
234 case CMD_TEST_UNIT_READY
:
235 ASPI_PrintSenseArea(prb
);
240 /* Posting must be done in such a way that as soon as the SRB_Status is set
241 * we don't touch the SRB anymore because it could possibly be freed
242 * if the app is doing ASPI polling
245 WNASPI32_DoPosting( SRB_ExecSCSICmd
*lpPRB
, DWORD status
)
247 void (*SRB_PostProc
)() = lpPRB
->SRB_PostProc
;
248 BYTE SRB_Flags
= lpPRB
->SRB_Flags
;
249 if( status
== SS_PENDING
)
251 WARN("Tried posting SS_PENDING\n");
254 lpPRB
->SRB_Status
= status
;
255 /* lpPRB is NOT safe, it could be freed in another thread */
261 TRACE("Post Routine (%lx) called\n", (DWORD
) SRB_PostProc
);
262 /* Even though lpPRB could have been freed by
263 * the program.. that's unlikely if it planned
264 * to use it in the PostProc
266 (*SRB_PostProc
)(lpPRB
);
268 else if (SRB_Flags
& SRB_EVENT_NOTIFY
) {
269 TRACE("Setting event %04x\n", (HANDLE
)SRB_PostProc
);
270 SetEvent((HANDLE
)SRB_PostProc
);
277 ASPI_ExecScsiCmd(SRB_ExecSCSICmd
*lpPRB
)
279 struct sg_header
*sg_hd
, *sg_reply_hdr
;
286 /* FIXME: hackmode */
287 #define MAKE_TARGET_TO_HOST(lpPRB) \
288 if (!TARGET_TO_HOST(lpPRB)) { \
289 WARN("program was not sending target_to_host for cmd %x (flags=%x),correcting.\n",lpPRB->CDBByte[0],lpPRB->SRB_Flags); \
290 lpPRB->SRB_Flags |= 8; \
292 #define MAKE_HOST_TO_TARGET(lpPRB) \
293 if (!HOST_TO_TARGET(lpPRB)) { \
294 WARN("program was not sending host_to_target for cmd %x (flags=%x),correcting.\n",lpPRB->CDBByte[0],lpPRB->SRB_Flags); \
295 lpPRB->SRB_Flags |= 0x10; \
297 switch (lpPRB
->CDBByte
[0]) {
298 case 0x12: /* INQUIRY */
299 case 0x5a: /* MODE_SENSE_10 */
300 case 0xa4: /* REPORT_KEY (DVD) MMC-2 */
301 case 0xad: /* READ DVD STRUCTURE MMC-2 */
302 MAKE_TARGET_TO_HOST(lpPRB
)
304 case 0xa3: /* SEND KEY (DVD) MMC-2 */
305 MAKE_HOST_TO_TARGET(lpPRB
)
308 if ((((lpPRB
->SRB_Flags
& 0x18) == 0x00) ||
309 ((lpPRB
->SRB_Flags
& 0x18) == 0x18)
310 ) && lpPRB
->SRB_BufLen
312 FIXME("command 0x%02x, no data transfer specified, but buflen is %ld!!!\n",lpPRB
->CDBByte
[0],lpPRB
->SRB_BufLen
);
316 ASPI_DebugPrintCmd(lpPRB
);
317 fd
= ASPI_OpenDevice(lpPRB
);
319 return WNASPI32_DoPosting( lpPRB
, SS_NO_DEVICE
);
325 lpPRB
->SRB_Status
= SS_PENDING
;
327 if (!lpPRB
->SRB_CDBLen
) {
328 ERR("Failed: lpPRB->SRB_CDBLen = 0.\n");
329 return WNASPI32_DoPosting( lpPRB
, SS_INVALID_SRB
);
332 /* build up sg_header + scsi cmd */
333 if (HOST_TO_TARGET(lpPRB
)) {
334 /* send header, command, and then data */
335 in_len
= SCSI_OFF
+ lpPRB
->SRB_CDBLen
+ lpPRB
->SRB_BufLen
;
336 sg_hd
= (struct sg_header
*) HeapAlloc(GetProcessHeap(), 0, in_len
);
337 memset(sg_hd
, 0, SCSI_OFF
);
338 memcpy(sg_hd
+ 1, &lpPRB
->CDBByte
[0], lpPRB
->SRB_CDBLen
);
339 if (lpPRB
->SRB_BufLen
) {
340 memcpy(((BYTE
*) sg_hd
) + SCSI_OFF
+ lpPRB
->SRB_CDBLen
, lpPRB
->SRB_BufPointer
, lpPRB
->SRB_BufLen
);
344 /* send header and command - no data */
345 in_len
= SCSI_OFF
+ lpPRB
->SRB_CDBLen
;
346 sg_hd
= (struct sg_header
*) HeapAlloc(GetProcessHeap(), 0, in_len
);
347 memset(sg_hd
, 0, SCSI_OFF
);
348 memcpy(sg_hd
+ 1, &lpPRB
->CDBByte
[0], lpPRB
->SRB_CDBLen
);
351 if (TARGET_TO_HOST(lpPRB
)) {
352 out_len
= SCSI_OFF
+ lpPRB
->SRB_BufLen
;
353 sg_reply_hdr
= (struct sg_header
*) HeapAlloc(GetProcessHeap(), 0, out_len
);
354 memset(sg_reply_hdr
, 0, SCSI_OFF
);
355 sg_hd
->reply_len
= out_len
;
359 sg_reply_hdr
= (struct sg_header
*) HeapAlloc(GetProcessHeap(), 0, out_len
);
360 memset(sg_reply_hdr
, 0, SCSI_OFF
);
361 sg_hd
->reply_len
= out_len
;
364 SCSI_Fix_CMD_LEN(fd
, lpPRB
->CDBByte
[0], lpPRB
->SRB_CDBLen
);
366 if(!SCSI_LinuxDeviceIo( fd
,
368 sg_reply_hdr
, out_len
,
374 if (sg_reply_hdr
->result
!= 0) {
375 error_code
= sg_reply_hdr
->result
;
376 WARN("reply header error (%d)\n", sg_reply_hdr
->result
);
380 if (TARGET_TO_HOST(lpPRB
) && lpPRB
->SRB_BufLen
) {
381 memcpy(lpPRB
->SRB_BufPointer
, sg_reply_hdr
+ 1, lpPRB
->SRB_BufLen
);
384 /* copy in sense buffer to amount that is available in client */
385 if (lpPRB
->SRB_SenseLen
) {
386 int sense_len
= lpPRB
->SRB_SenseLen
;
387 if (lpPRB
->SRB_SenseLen
> 16)
390 /* CDB is fixed in WNASPI32 */
391 memcpy(lpPRB
->SenseArea
, &sg_reply_hdr
->sense_buffer
[0], sense_len
);
393 ASPI_PrintCDBArea(lpPRB
);
394 ASPI_PrintSenseArea(lpPRB
);
397 SRB_Status
= SS_COMP
;
398 lpPRB
->SRB_HaStat
= HASTAT_OK
;
399 lpPRB
->SRB_TargStat
= sg_reply_hdr
->target_status
<< 1;
401 HeapFree(GetProcessHeap(), 0, sg_reply_hdr
);
402 HeapFree(GetProcessHeap(), 0, sg_hd
);
404 /* FIXME: Should this be != 0 maybe? */
405 if( lpPRB
->SRB_TargStat
== 2 ) {
407 switch (lpPRB
->CDBByte
[0]) {
408 case 0xa4: /* REPORT_KEY (DVD) MMC-2 */
409 case 0xa3: /* SEND KEY (DVD) MMC-2 */
410 SRB_Status
= SS_COMP
;
411 lpPRB
->SRB_TargStat
= 0;
412 FIXME("Program wants to do DVD Region switching, but fails (non compliant DVD drive). Ignoring....\n");
417 ASPI_DebugPrintResult(lpPRB
);
419 return WNASPI32_DoPosting( lpPRB
, SRB_Status
);
420 /* In real WNASPI32 stuff really is always pending because ASPI does things
421 in the background, but we are not doing that (yet) */
425 if (error_code
== EBUSY
) {
426 WNASPI32_DoPosting( lpPRB
, SS_ASPI_IS_BUSY
);
427 TRACE("Device busy\n");
431 /* I'm not sure exactly error codes work here
432 * We probably should set lpPRB->SRB_TargStat, SRB_HaStat ?
434 WARN("error_exit\n");
435 HeapFree(GetProcessHeap(), 0, sg_reply_hdr
);
436 HeapFree(GetProcessHeap(), 0, sg_hd
);
437 WNASPI32_DoPosting( lpPRB
, SRB_Status
);
441 #endif /* defined(linux) */
444 /*******************************************************************
445 * GetASPI32SupportInfo [WNASPI32.0]
447 * Checks if the ASPI subsystem is initialized correctly.
451 * HIBYTE of LOWORD: status (SS_COMP or SS_FAILED_INIT)
452 * LOBYTE of LOWORD: # of host adapters.
454 DWORD __cdecl
GetASPI32SupportInfo(void)
456 return ((SS_COMP
<< 8) | ASPI_GetNumControllers());
460 /***********************************************************************
461 * SendASPI32Command (WNASPI32.1)
463 DWORD __cdecl
SendASPI32Command(LPSRB lpSRB
)
466 switch (lpSRB
->common
.SRB_Cmd
) {
468 lpSRB
->inquiry
.SRB_Status
= SS_COMP
; /* completed successfully */
469 lpSRB
->inquiry
.HA_Count
= 1; /* not always */
470 lpSRB
->inquiry
.HA_SCSI_ID
= 7; /* not always ID 7 */
471 strcpy(lpSRB
->inquiry
.HA_ManagerId
, "ASPI for WIN32"); /* max 15 chars, don't change */
472 strcpy(lpSRB
->inquiry
.HA_Identifier
, "Wine host"); /* FIXME: return host adapter name */
473 memset(lpSRB
->inquiry
.HA_Unique
, 0, 16); /* default HA_Unique content */
474 lpSRB
->inquiry
.HA_Unique
[6] = 0x02; /* Maximum Transfer Length (128K, Byte> 4-7) */
475 lpSRB
->inquiry
.HA_Unique
[3] = 0x08; /* Maximum number of SCSI targets */
476 FIXME("ASPI: Partially implemented SC_HA_INQUIRY for adapter %d.\n", lpSRB
->inquiry
.SRB_HaId
);
479 case SC_GET_DEV_TYPE
: {
480 /* FIXME: We should return SS_NO_DEVICE if the device is not configured */
481 /* FIXME: We should return SS_INVALID_HA if HostAdapter!=0 */
486 memset(&tmpsrb
,0,sizeof(tmpsrb
));
489 memcpy(&tmpsrb
.common
,&(lpSRB
->common
),sizeof(tmpsrb
.common
));
491 tmpsrb
.cmd
.SRB_Flags
|= 8; /* target to host */
492 tmpsrb
.cmd
.SRB_Cmd
= SC_EXEC_SCSI_CMD
;
493 tmpsrb
.cmd
.SRB_Target
= lpSRB
->devtype
.SRB_Target
;
494 tmpsrb
.cmd
.SRB_Lun
= lpSRB
->devtype
.SRB_Lun
;
495 tmpsrb
.cmd
.SRB_BufLen
= sizeof(inqbuf
);
496 tmpsrb
.cmd
.SRB_BufPointer
= inqbuf
;
497 tmpsrb
.cmd
.CDBByte
[0] = 0x12; /* INQUIRY */
498 /* FIXME: handle lun */
499 tmpsrb
.cmd
.CDBByte
[4] = sizeof(inqbuf
);
500 tmpsrb
.cmd
.SRB_CDBLen
= 6;
502 ret
= ASPI_ExecScsiCmd(&tmpsrb
.cmd
);
504 lpSRB
->devtype
.SRB_Status
= tmpsrb
.cmd
.SRB_Status
;
505 lpSRB
->devtype
.SRB_DeviceType
= inqbuf
[0]&0x1f;
507 TRACE("returning devicetype %d for target %d\n",inqbuf
[0]&0x1f,tmpsrb
.cmd
.SRB_Target
);
508 if (ret
!=SS_PENDING
) /* Any error is passed down directly */
510 /* FIXME: knows that the command is finished already, pass final Status */
511 return tmpsrb
.cmd
.SRB_Status
;
513 case SC_EXEC_SCSI_CMD
:
514 return ASPI_ExecScsiCmd(&lpSRB
->cmd
);
516 FIXME("Not implemented SC_ABORT_SRB\n");
519 FIXME("Not implemented SC_RESET_DEV\n");
521 #ifdef SC_GET_DISK_INFO
522 case SC_GET_DISK_INFO
:
523 /* NT Doesn't implement this either.. so don't feel bad */
524 FIXME("Not implemented SC_GET_DISK_INFO\n");
528 FIXME("Unknown command %d\n", lpSRB
->common
.SRB_Cmd
);
530 return SS_INVALID_SRB
;
532 return SS_INVALID_SRB
;
537 /***********************************************************************
538 * GetASPI32DLLVersion (WNASPI32.3)
540 DWORD __cdecl
GetASPI32DLLVersion(void)
543 TRACE("Returning version 1\n");
546 FIXME("Please add SCSI support for your operating system, returning 0\n");
551 /***********************************************************************
552 * GetASPI32Buffer (WNASPI32.@)
554 BOOL __cdecl
GetASPI32Buffer(LPVOID pab
) /* [???] FIXME: PASPI32BUFF */
556 FIXME("(%p), stub !\n", pab
);
560 /***********************************************************************
561 * FreeASPI32Buffer (WNASPI32.@)
563 BOOL __cdecl
FreeASPI32Buffer(LPVOID pab
) /* [???] FIXME: PASPI32BUFF */
565 FIXME("(%p), stub !\n", pab
);
569 /***********************************************************************
570 * TranslateASPI32Address (WNASPI32.@)
572 BOOL __cdecl
TranslateASPI32Address(LPDWORD pdwPath
, LPDWORD pdwDEVNODE
)
574 FIXME("(%p, %p), stub !\n", pdwPath
, pdwDEVNODE
);