2 * Copyright 1997 Bruce Milner
3 * Copyright 1998 Andreas Mohr
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include <sys/types.h>
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(aspi
);
44 * 1) Residual byte length reporting not handled
45 * 2) Make this code re-entrant for multithreading
46 * -- Added CriticalSection to OpenDevices function
47 * 3) Only linux supported so far
48 * 4) Leaves sg devices open. This may or may not be okay. A better solution
49 * would be to close the file descriptors when the thread/process using
50 * them no longer needs them.
55 static ASPI_DEVICE_INFO
*ASPI_open_devices
= NULL
;
57 static CRITICAL_SECTION ASPI_CritSection
;
58 static CRITICAL_SECTION_DEBUG critsect_debug
=
60 0, 0, &ASPI_CritSection
,
61 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
62 0, 0, { (DWORD_PTR
)(__FILE__
": ASPI_CritSection") }
64 static CRITICAL_SECTION ASPI_CritSection
= { &critsect_debug
, -1, 0, 0, 0, 0 };
67 BOOL WINAPI
DllMain(HINSTANCE hInstDLL
, DWORD fdwReason
, LPVOID fImpLoad
)
71 case DLL_PROCESS_ATTACH
:
72 DisableThreadLibraryCalls(hInstDLL
);
75 case DLL_PROCESS_DETACH
:
77 DeleteCriticalSection( &ASPI_CritSection
);
85 ASPI_OpenDevice(SRB_ExecSCSICmd
*prb
)
89 ASPI_DEVICE_INFO
*curr
;
91 /* search list of devices to see if we've opened it already.
92 * There is not an explicit open/close in ASPI land, so hopefully
93 * keeping a device open won't be a problem.
96 EnterCriticalSection(&ASPI_CritSection
);
97 for (curr
= ASPI_open_devices
; curr
; curr
= curr
->next
) {
98 if (curr
->hostId
== prb
->SRB_HaId
&&
99 curr
->target
== prb
->SRB_Target
&&
100 curr
->lun
== prb
->SRB_Lun
) {
101 LeaveCriticalSection(&ASPI_CritSection
);
105 LeaveCriticalSection(&ASPI_CritSection
);
107 if (prb
->SRB_HaId
>= ASPI_GetNumControllers())
110 hc
= ASPI_GetHCforController( prb
->SRB_HaId
);
111 fd
= SCSI_OpenDevice( HIWORD(hc
), LOWORD(hc
), prb
->SRB_Target
, prb
->SRB_Lun
);
116 /* device is now open */
117 /* FIXME: Let users specify SCSI timeout in registry */
118 SCSI_LinuxSetTimeout( fd
, SCSI_DEFAULT_TIMEOUT
);
120 curr
= HeapAlloc( GetProcessHeap(), 0, sizeof(ASPI_DEVICE_INFO
) );
122 curr
->hostId
= prb
->SRB_HaId
;
123 curr
->target
= prb
->SRB_Target
;
124 curr
->lun
= prb
->SRB_Lun
;
126 /* insert new record at beginning of open device list */
127 EnterCriticalSection(&ASPI_CritSection
);
128 curr
->next
= ASPI_open_devices
;
129 ASPI_open_devices
= curr
;
130 LeaveCriticalSection(&ASPI_CritSection
);
136 ASPI_DebugPrintCmd(SRB_ExecSCSICmd
*prb
)
141 switch (prb
->CDBByte
[0]) {
143 TRACE("INQUIRY {\n");
144 TRACE("\tEVPD: %d\n", prb
->CDBByte
[1] & 1);
145 TRACE("\tLUN: %d\n", (prb
->CDBByte
[1] & 0xc) >> 1);
146 TRACE("\tPAGE CODE: %d\n", prb
->CDBByte
[2]);
147 TRACE("\tALLOCATION LENGTH: %d\n", prb
->CDBByte
[4]);
148 TRACE("\tCONTROL: %d\n", prb
->CDBByte
[5]);
152 TRACE("Transfer Length: %d\n", prb
->CDBByte
[4]);
156 TRACE("Host Adapter: %d\n", prb
->SRB_HaId
);
157 TRACE("Flags: %d\n", prb
->SRB_Flags
);
158 if (TARGET_TO_HOST(prb
)) {
159 TRACE("\tData transfer: Target to host. Length checked.\n");
161 else if (HOST_TO_TARGET(prb
)) {
162 TRACE("\tData transfer: Host to target. Length checked.\n");
164 else if (NO_DATA_TRANSFERRED(prb
)) {
165 TRACE("\tData transfer: none\n");
168 WARN("\tTransfer by scsi cmd. Length not checked.\n");
171 TRACE("\tResidual byte length reporting %s\n", prb
->SRB_Flags
& 0x4 ? "enabled" : "disabled");
172 TRACE("\tLinking %s\n", prb
->SRB_Flags
& 0x2 ? "enabled" : "disabled");
173 TRACE("\tPosting %s\n", prb
->SRB_Flags
& 0x1 ? "enabled" : "disabled");
174 TRACE("Target: %d\n", prb
->SRB_Target
);
175 TRACE("Lun: %d\n", prb
->SRB_Lun
);
176 TRACE("BufLen: %d\n", prb
->SRB_BufLen
);
177 TRACE("SenseLen: %d\n", prb
->SRB_SenseLen
);
178 TRACE("BufPtr: %p\n", prb
->SRB_BufPointer
);
179 TRACE("CDB Length: %d\n", prb
->SRB_CDBLen
);
180 TRACE("POST Proc: %p\n", prb
->SRB_PostProc
);
181 cdb
= &prb
->CDBByte
[0];
182 if (TRACE_ON(aspi
)) {
183 TRACE("CDB buffer[");
184 for (i
= 0; i
< prb
->SRB_CDBLen
; i
++) {
185 if (i
!= 0) TRACE(",");
186 TRACE("%02x", *cdb
++);
193 ASPI_PrintCDBArea(SRB_ExecSCSICmd
*prb
)
199 for (i
= 0; i
< prb
->SRB_CDBLen
; i
++) {
201 TRACE("%02x", prb
->CDBByte
[i
]);
208 ASPI_PrintSenseArea(SRB_ExecSCSICmd
*prb
)
211 BYTE
*rqbuf
= prb
->SenseArea
;
215 TRACE("Request Sense reports:\n");
216 if ((rqbuf
[0]&0x7f)!=0x70) {
217 TRACE("\tInvalid sense header: 0x%02x instead of 0x70\n", rqbuf
[0]&0x7f);
220 TRACE("\tCurrent command read filemark: %s\n",(rqbuf
[2]&0x80)?"yes":"no");
221 TRACE("\tEarly warning passed: %s\n",(rqbuf
[2]&0x40)?"yes":"no");
222 TRACE("\tIncorrect blocklength: %s\n",(rqbuf
[2]&0x20)?"yes":"no");
223 TRACE("\tSense Key: %d\n",rqbuf
[2]&0xf);
225 TRACE("\tResidual Length: %d\n",rqbuf
[3]*0x1000000+rqbuf
[4]*0x10000+rqbuf
[5]*0x100+rqbuf
[6]);
226 TRACE("\tAdditional Sense Length: %d\n",rqbuf
[7]);
227 TRACE("\tAdditional Sense Code: %d\n",rqbuf
[12]);
228 TRACE("\tAdditional Sense Code Qualifier: %d\n",rqbuf
[13]);
229 if (rqbuf
[15]&0x80) {
230 TRACE("\tIllegal Param is in %s\n",(rqbuf
[15]&0x40)?"the CDB":"the Data Out Phase");
232 TRACE("Pointer at %d, bit %d\n",rqbuf
[16]*256+rqbuf
[17],rqbuf
[15]&0x7);
236 for (i
= 0; i
< prb
->SRB_SenseLen
; i
++) {
238 TRACE("%02x", *rqbuf
++);
245 ASPI_DebugPrintResult(SRB_ExecSCSICmd
*prb
)
248 TRACE("SRB_Status: %x\n", prb
->SRB_Status
);
249 TRACE("SRB_HaStat: %x\n", prb
->SRB_HaStat
);
250 TRACE("SRB_TargStat: %x\n", prb
->SRB_TargStat
);
251 switch (prb
->CDBByte
[0]) {
253 TRACE("Vendor: '%s'\n", prb
->SRB_BufPointer
+ INQUIRY_VENDOR
);
255 case CMD_TEST_UNIT_READY
:
256 ASPI_PrintSenseArea(prb
);
261 /* Posting must be done in such a way that as soon as the SRB_Status is set
262 * we don't touch the SRB anymore because it could possibly be freed
263 * if the app is doing ASPI polling
266 WNASPI32_DoPosting( SRB_ExecSCSICmd
*lpPRB
, DWORD status
)
268 void (*SRB_PostProc
)(SRB_ExecSCSICmd
*) = lpPRB
->SRB_PostProc
;
269 BYTE SRB_Flags
= lpPRB
->SRB_Flags
;
270 if( status
== SS_PENDING
)
272 WARN("Tried posting SS_PENDING\n");
275 lpPRB
->SRB_Status
= status
;
276 /* lpPRB is NOT safe, it could be freed in another thread */
282 TRACE("Post Routine (%p) called\n", SRB_PostProc
);
283 /* Even though lpPRB could have been freed by
284 * the program.. that's unlikely if it planned
285 * to use it in the PostProc
287 (*SRB_PostProc
)(lpPRB
);
289 else if (SRB_Flags
& SRB_EVENT_NOTIFY
) {
290 TRACE("Setting event %p\n", SRB_PostProc
);
291 SetEvent(SRB_PostProc
);
298 ASPI_ExecScsiCmd(SRB_ExecSCSICmd
*lpPRB
)
300 struct sg_header
*sg_hd
, *sg_reply_hdr
;
304 int num_controllers
= 0;
309 num_controllers
= ASPI_GetNumControllers();
310 if (lpPRB
->SRB_HaId
>= num_controllers
) {
311 WARN("Failed: Wanted hostadapter with index %d, but we have only %d.\n",
312 lpPRB
->SRB_HaId
, num_controllers
314 return WNASPI32_DoPosting( lpPRB
, SS_INVALID_HA
);
316 fd
= ASPI_OpenDevice(lpPRB
);
318 return WNASPI32_DoPosting( lpPRB
, SS_NO_DEVICE
);
321 /* FIXME: hackmode */
322 #define MAKE_TARGET_TO_HOST(lpPRB) \
323 if (!TARGET_TO_HOST(lpPRB)) { \
324 WARN("program was not sending target_to_host for cmd %x (flags=%x),correcting.\n",lpPRB->CDBByte[0],lpPRB->SRB_Flags); \
325 lpPRB->SRB_Flags |= 0x08; \
327 #define MAKE_HOST_TO_TARGET(lpPRB) \
328 if (!HOST_TO_TARGET(lpPRB)) { \
329 WARN("program was not sending host_to_target for cmd %x (flags=%x),correcting.\n",lpPRB->CDBByte[0],lpPRB->SRB_Flags); \
330 lpPRB->SRB_Flags |= 0x10; \
332 switch (lpPRB
->CDBByte
[0]) {
333 case 0x12: /* INQUIRY */
334 case 0x5a: /* MODE_SENSE_10 */
335 case 0xa4: /* REPORT_KEY (DVD) MMC-2 */
336 case 0xad: /* READ DVD STRUCTURE MMC-2 */
337 MAKE_TARGET_TO_HOST(lpPRB
)
339 case 0xa3: /* SEND KEY (DVD) MMC-2 */
340 MAKE_HOST_TO_TARGET(lpPRB
)
343 if ((((lpPRB
->SRB_Flags
& 0x18) == 0x00) ||
344 ((lpPRB
->SRB_Flags
& 0x18) == 0x18)
345 ) && lpPRB
->SRB_BufLen
347 FIXME("command 0x%02x, no data transfer specified, but buflen is %d!!!\n",lpPRB
->CDBByte
[0],lpPRB
->SRB_BufLen
);
351 ASPI_DebugPrintCmd(lpPRB
);
356 lpPRB
->SRB_Status
= SS_PENDING
;
358 if (!lpPRB
->SRB_CDBLen
) {
359 ERR("Failed: lpPRB->SRB_CDBLen = 0.\n");
360 return WNASPI32_DoPosting( lpPRB
, SS_INVALID_SRB
);
363 /* build up sg_header + scsi cmd */
364 if (HOST_TO_TARGET(lpPRB
)) {
365 /* send header, command, and then data */
366 in_len
= SCSI_OFF
+ lpPRB
->SRB_CDBLen
+ lpPRB
->SRB_BufLen
;
367 sg_hd
= HeapAlloc(GetProcessHeap(), 0, in_len
);
368 memset(sg_hd
, 0, SCSI_OFF
);
369 memcpy(sg_hd
+ 1, &lpPRB
->CDBByte
[0], lpPRB
->SRB_CDBLen
);
370 if (lpPRB
->SRB_BufLen
) {
371 memcpy(((BYTE
*) sg_hd
) + SCSI_OFF
+ lpPRB
->SRB_CDBLen
, lpPRB
->SRB_BufPointer
, lpPRB
->SRB_BufLen
);
375 /* send header and command - no data */
376 in_len
= SCSI_OFF
+ lpPRB
->SRB_CDBLen
;
377 sg_hd
= HeapAlloc(GetProcessHeap(), 0, in_len
);
378 memset(sg_hd
, 0, SCSI_OFF
);
379 memcpy(sg_hd
+ 1, &lpPRB
->CDBByte
[0], lpPRB
->SRB_CDBLen
);
382 if (TARGET_TO_HOST(lpPRB
)) {
383 out_len
= SCSI_OFF
+ lpPRB
->SRB_BufLen
;
384 sg_reply_hdr
= HeapAlloc(GetProcessHeap(), 0, out_len
);
385 memset(sg_reply_hdr
, 0, SCSI_OFF
);
386 sg_hd
->reply_len
= out_len
;
390 sg_reply_hdr
= HeapAlloc(GetProcessHeap(), 0, out_len
);
391 memset(sg_reply_hdr
, 0, SCSI_OFF
);
392 sg_hd
->reply_len
= out_len
;
395 SCSI_Fix_CMD_LEN(fd
, lpPRB
->CDBByte
[0], lpPRB
->SRB_CDBLen
);
397 if(!SCSI_LinuxDeviceIo( fd
,
399 sg_reply_hdr
, out_len
,
405 if (sg_reply_hdr
->result
!= 0) {
406 error_code
= sg_reply_hdr
->result
;
407 WARN("reply header error (%d)\n", sg_reply_hdr
->result
);
411 if (TARGET_TO_HOST(lpPRB
) && lpPRB
->SRB_BufLen
) {
412 memcpy(lpPRB
->SRB_BufPointer
, sg_reply_hdr
+ 1, lpPRB
->SRB_BufLen
);
415 /* copy in sense buffer to amount that is available in client */
416 if (lpPRB
->SRB_SenseLen
) {
417 int sense_len
= lpPRB
->SRB_SenseLen
;
418 if (lpPRB
->SRB_SenseLen
> 16)
421 /* CDB is fixed in WNASPI32 */
422 memcpy(lpPRB
->SenseArea
, &sg_reply_hdr
->sense_buffer
[0], sense_len
);
424 ASPI_PrintCDBArea(lpPRB
);
425 ASPI_PrintSenseArea(lpPRB
);
428 SRB_Status
= SS_COMP
;
429 lpPRB
->SRB_HaStat
= HASTAT_OK
;
430 lpPRB
->SRB_TargStat
= sg_reply_hdr
->target_status
<< 1;
432 HeapFree(GetProcessHeap(), 0, sg_reply_hdr
);
433 HeapFree(GetProcessHeap(), 0, sg_hd
);
435 /* FIXME: Should this be != 0 maybe? */
436 if( lpPRB
->SRB_TargStat
== 2 ) {
438 switch (lpPRB
->CDBByte
[0]) {
439 case 0xa4: /* REPORT_KEY (DVD) MMC-2 */
440 case 0xa3: /* SEND KEY (DVD) MMC-2 */
441 SRB_Status
= SS_COMP
;
442 lpPRB
->SRB_TargStat
= 0;
443 FIXME("Program wants to do DVD Region switching, but fails (non compliant DVD drive). Ignoring....\n");
448 ASPI_DebugPrintResult(lpPRB
);
450 ret
= WNASPI32_DoPosting( lpPRB
, SRB_Status
);
452 switch (lpPRB
->CDBByte
[0]) {
454 if (SRB_Status
== SS_COMP
)
455 return SS_COMP
; /* some junk expects ss_comp here. */
461 /* In real WNASPI32 stuff really is always pending because ASPI does things
462 in the background, but we are not doing that (yet) */
468 if (error_code
== EBUSY
) {
469 WNASPI32_DoPosting( lpPRB
, SS_ASPI_IS_BUSY
);
470 TRACE("Device busy\n");
474 /* I'm not sure exactly error codes work here
475 * We probably should set lpPRB->SRB_TargStat, SRB_HaStat ?
477 WARN("error_exit\n");
478 HeapFree(GetProcessHeap(), 0, sg_reply_hdr
);
479 HeapFree(GetProcessHeap(), 0, sg_hd
);
480 WNASPI32_DoPosting( lpPRB
, SRB_Status
);
484 #endif /* defined(linux) */
487 /*******************************************************************
488 * GetASPI32SupportInfo [WNASPI32.1]
490 * Checks if the ASPI subsystem is initialized correctly.
494 * HIBYTE of LOWORD: status (SS_COMP or SS_FAILED_INIT)
495 * LOBYTE of LOWORD: # of host adapters.
497 DWORD __cdecl
GetASPI32SupportInfo(void)
499 DWORD controllers
= ASPI_GetNumControllers();
502 return SS_NO_ADAPTERS
<< 8;
503 return (SS_COMP
<< 8) | controllers
;
506 /***********************************************************************
507 * SendASPI32Command (WNASPI32.2)
509 DWORD __cdecl
SendASPI32Command(LPSRB lpSRB
)
512 static const char szId
[] = "ASPI for WIN32";
513 static const char szWh
[] = "Wine host";
514 switch (lpSRB
->common
.SRB_Cmd
) {
516 lpSRB
->inquiry
.SRB_Status
= SS_COMP
; /* completed successfully */
517 lpSRB
->inquiry
.HA_Count
= ASPI_GetNumControllers();
518 lpSRB
->inquiry
.HA_SCSI_ID
= 7; /* not always ID 7 */
519 memcpy(lpSRB
->inquiry
.HA_ManagerId
, szId
, sizeof szId
); /* max 15 chars, don't change */
520 memcpy(lpSRB
->inquiry
.HA_Identifier
, szWh
, sizeof szWh
); /* FIXME: return host adapter name */
521 memset(lpSRB
->inquiry
.HA_Unique
, 0, 16); /* default HA_Unique content */
522 lpSRB
->inquiry
.HA_Unique
[6] = 0x02; /* Maximum Transfer Length (128K, Byte> 4-7) */
523 lpSRB
->inquiry
.HA_Unique
[3] = 0x08; /* Maximum number of SCSI targets */
524 FIXME("ASPI: Partially implemented SC_HA_INQUIRY for adapter %d.\n", lpSRB
->inquiry
.SRB_HaId
);
527 case SC_GET_DEV_TYPE
: {
528 /* FIXME: We should return SS_NO_DEVICE if the device is not configured */
529 /* FIXME: We should return SS_INVALID_HA if HostAdapter!=0 */
531 unsigned char inqbuf
[200];
534 memset(&tmpsrb
,0,sizeof(tmpsrb
));
537 tmpsrb
.common
= lpSRB
->common
;
539 tmpsrb
.cmd
.SRB_Flags
|= 8; /* target to host */
540 tmpsrb
.cmd
.SRB_Cmd
= SC_EXEC_SCSI_CMD
;
541 tmpsrb
.cmd
.SRB_Target
= lpSRB
->devtype
.SRB_Target
;
542 tmpsrb
.cmd
.SRB_Lun
= lpSRB
->devtype
.SRB_Lun
;
543 tmpsrb
.cmd
.SRB_BufLen
= sizeof(inqbuf
);
544 tmpsrb
.cmd
.SRB_BufPointer
= inqbuf
;
545 tmpsrb
.cmd
.CDBByte
[0] = 0x12; /* INQUIRY */
546 /* FIXME: handle lun */
547 tmpsrb
.cmd
.CDBByte
[4] = sizeof(inqbuf
);
548 tmpsrb
.cmd
.SRB_CDBLen
= 6;
550 ret
= ASPI_ExecScsiCmd(&tmpsrb
.cmd
);
552 lpSRB
->devtype
.SRB_Status
= tmpsrb
.cmd
.SRB_Status
;
553 lpSRB
->devtype
.SRB_DeviceType
= inqbuf
[0]&0x1f;
555 TRACE("returning devicetype %d for target %d\n",inqbuf
[0]&0x1f,tmpsrb
.cmd
.SRB_Target
);
556 if (ret
!=SS_PENDING
) /* Any error is passed down directly */
558 /* FIXME: knows that the command is finished already, pass final Status */
559 return tmpsrb
.cmd
.SRB_Status
;
561 case SC_EXEC_SCSI_CMD
:
562 return ASPI_ExecScsiCmd(&lpSRB
->cmd
);
564 FIXME("Not implemented SC_ABORT_SRB\n");
567 FIXME("Not implemented SC_RESET_DEV\n");
569 case SC_GET_DISK_INFO
:
570 /* here we have to find out the int13 / bios association.
571 * We just say we do not have any.
573 FIXME("SC_GET_DISK_INFO always return 'int13 unassociated disk'.\n");
574 lpSRB
->diskinfo
.SRB_DriveFlags
= 0; /* disk is not int13 served */
577 FIXME("Unknown command %d\n", lpSRB
->common
.SRB_Cmd
);
579 return SS_INVALID_SRB
;
581 return SS_INVALID_SRB
;
586 /***********************************************************************
587 * GetASPI32DLLVersion (WNASPI32.4)
589 DWORD __cdecl
GetASPI32DLLVersion(void)
592 TRACE("Returning version 1\n");
595 FIXME("Please add SCSI support for your operating system, returning 0\n");
600 /***********************************************************************
601 * GetASPI32Buffer (WNASPI32.8)
602 * Supposed to return a DMA capable large SCSI buffer.
603 * Our implementation does not use those at all, all buffer stuff is
604 * done in the kernel SG device layer. So we just heapalloc the buffer.
606 BOOL __cdecl
GetASPI32Buffer(PASPI32BUFF pab
)
608 pab
->AB_BufPointer
= HeapAlloc(GetProcessHeap(),
609 pab
->AB_ZeroFill
?HEAP_ZERO_MEMORY
:0,
612 if (!pab
->AB_BufPointer
) return FALSE
;
616 /***********************************************************************
617 * FreeASPI32Buffer (WNASPI32.14)
619 BOOL __cdecl
FreeASPI32Buffer(PASPI32BUFF pab
)
621 HeapFree(GetProcessHeap(),0,pab
->AB_BufPointer
);
625 /***********************************************************************
626 * TranslateASPI32Address (WNASPI32.7)
628 BOOL __cdecl
TranslateASPI32Address(LPDWORD pdwPath
, LPDWORD pdwDEVNODE
)
630 FIXME("(%p, %p), stub !\n", pdwPath
, pdwDEVNODE
);