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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <sys/types.h>
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(aspi
);
40 * 1) Residual byte length reporting not handled
41 * 2) Make this code re-entrant for multithreading
42 * -- Added CriticalSection to OpenDevices function
43 * 3) Only linux supported so far
44 * 4) Leaves sg devices open. This may or may not be okay. A better solution
45 * would be to close the file descriptors when the thread/process using
46 * them no longer needs them.
51 static ASPI_DEVICE_INFO
*ASPI_open_devices
= NULL
;
52 static CRITICAL_SECTION ASPI_CritSection
= CRITICAL_SECTION_INIT("ASPI_CritSection");
54 #endif /* defined(linux) */
57 BOOL WINAPI
WNASPI32_LibMain(HINSTANCE hInstDLL
, DWORD fdwReason
, LPVOID fImpLoad
)
62 case DLL_PROCESS_ATTACH
:
65 case DLL_PROCESS_DETACH
:
66 DeleteCriticalSection( &ASPI_CritSection
);
68 case DLL_THREAD_ATTACH
:
69 case DLL_THREAD_DETACH
:
72 #endif /* defined(linux) */
80 ASPI_OpenDevice(SRB_ExecSCSICmd
*prb
)
84 ASPI_DEVICE_INFO
*curr
;
86 /* search list of devices to see if we've opened it already.
87 * There is not an explicit open/close in ASPI land, so hopefully
88 * keeping a device open won't be a problem.
91 EnterCriticalSection(&ASPI_CritSection
);
92 for (curr
= ASPI_open_devices
; curr
; curr
= curr
->next
) {
93 if (curr
->hostId
== prb
->SRB_HaId
&&
94 curr
->target
== prb
->SRB_Target
&&
95 curr
->lun
== prb
->SRB_Lun
) {
96 LeaveCriticalSection(&ASPI_CritSection
);
100 LeaveCriticalSection(&ASPI_CritSection
);
102 if (prb
->SRB_HaId
> ASPI_GetNumControllers())
105 hc
= ASPI_GetHCforController( prb
->SRB_HaId
);
106 fd
= SCSI_OpenDevice( HIWORD(hc
), LOWORD(hc
), prb
->SRB_Target
, prb
->SRB_Lun
);
111 /* device is now open */
112 /* FIXME: Let users specify SCSI timeout in registry */
113 SCSI_LinuxSetTimeout( fd
, SCSI_DEFAULT_TIMEOUT
);
115 curr
= HeapAlloc( GetProcessHeap(), 0, sizeof(ASPI_DEVICE_INFO
) );
117 curr
->hostId
= prb
->SRB_HaId
;
118 curr
->target
= prb
->SRB_Target
;
119 curr
->lun
= prb
->SRB_Lun
;
121 /* insert new record at beginning of open device list */
122 EnterCriticalSection(&ASPI_CritSection
);
123 curr
->next
= ASPI_open_devices
;
124 ASPI_open_devices
= curr
;
125 LeaveCriticalSection(&ASPI_CritSection
);
131 ASPI_DebugPrintCmd(SRB_ExecSCSICmd
*prb
)
137 switch (prb
->CDBByte
[0]) {
139 TRACE("INQUIRY {\n");
140 TRACE("\tEVPD: %d\n", prb
->CDBByte
[1] & 1);
141 TRACE("\tLUN: %d\n", (prb
->CDBByte
[1] & 0xc) >> 1);
142 TRACE("\tPAGE CODE: %d\n", prb
->CDBByte
[2]);
143 TRACE("\tALLOCATION LENGTH: %d\n", prb
->CDBByte
[4]);
144 TRACE("\tCONTROL: %d\n", prb
->CDBByte
[5]);
148 TRACE("Transfer Length: %d\n", prb
->CDBByte
[4]);
152 TRACE("Host Adapter: %d\n", prb
->SRB_HaId
);
153 TRACE("Flags: %d\n", prb
->SRB_Flags
);
154 if (TARGET_TO_HOST(prb
)) {
155 TRACE("\tData transfer: Target to host. Length checked.\n");
157 else if (HOST_TO_TARGET(prb
)) {
158 TRACE("\tData transfer: Host to target. Length checked.\n");
160 else if (NO_DATA_TRANSFERED(prb
)) {
161 TRACE("\tData transfer: none\n");
164 WARN("\tTransfer by scsi cmd. Length not checked.\n");
167 TRACE("\tResidual byte length reporting %s\n", prb
->SRB_Flags
& 0x4 ? "enabled" : "disabled");
168 TRACE("\tLinking %s\n", prb
->SRB_Flags
& 0x2 ? "enabled" : "disabled");
169 TRACE("\tPosting %s\n", prb
->SRB_Flags
& 0x1 ? "enabled" : "disabled");
170 TRACE("Target: %d\n", prb
->SRB_Target
);
171 TRACE("Lun: %d\n", prb
->SRB_Lun
);
172 TRACE("BufLen: %ld\n", prb
->SRB_BufLen
);
173 TRACE("SenseLen: %d\n", prb
->SRB_SenseLen
);
174 TRACE("BufPtr: %p\n", prb
->SRB_BufPointer
);
175 TRACE("CDB Length: %d\n", prb
->SRB_CDBLen
);
176 TRACE("POST Proc: %lx\n", (DWORD
) prb
->SRB_PostProc
);
177 cdb
= &prb
->CDBByte
[0];
178 cmd
= prb
->CDBByte
[0];
179 if (TRACE_ON(aspi
)) {
180 DPRINTF("CDB buffer[");
181 for (i
= 0; i
< prb
->SRB_CDBLen
; i
++) {
182 if (i
!= 0) DPRINTF(",");
183 DPRINTF("%02x", *cdb
++);
190 ASPI_PrintCDBArea(SRB_ExecSCSICmd
*prb
)
196 for (i
= 0; i
< prb
->SRB_CDBLen
; i
++) {
198 DPRINTF("%02x", prb
->CDBByte
[i
]);
205 ASPI_PrintSenseArea(SRB_ExecSCSICmd
*prb
)
208 BYTE
*rqbuf
= prb
->SenseArea
;
212 DPRINTF("Request Sense reports:\n");
213 if ((rqbuf
[0]&0x7f)!=0x70) {
214 DPRINTF("\tInvalid sense header: 0x%02x instead of 0x70\n", rqbuf
[0]&0x7f);
217 DPRINTF("\tCurrent command read filemark: %s\n",(rqbuf
[2]&0x80)?"yes":"no");
218 DPRINTF("\tEarly warning passed: %s\n",(rqbuf
[2]&0x40)?"yes":"no");
219 DPRINTF("\tIncorrect blocklength: %s\n",(rqbuf
[2]&0x20)?"yes":"no");
220 DPRINTF("\tSense Key: %d\n",rqbuf
[2]&0xf);
222 DPRINTF("\tResidual Length: %d\n",rqbuf
[3]*0x1000000+rqbuf
[4]*0x10000+rqbuf
[5]*0x100+rqbuf
[6]);
223 DPRINTF("\tAdditional Sense Length: %d\n",rqbuf
[7]);
224 DPRINTF("\tAdditional Sense Code: %d\n",rqbuf
[12]);
225 DPRINTF("\tAdditional Sense Code Qualifier: %d\n",rqbuf
[13]);
226 if (rqbuf
[15]&0x80) {
227 DPRINTF("\tIllegal Param is in %s\n",(rqbuf
[15]&0x40)?"the CDB":"the Data Out Phase");
229 DPRINTF("Pointer at %d, bit %d\n",rqbuf
[16]*256+rqbuf
[17],rqbuf
[15]&0x7);
232 DPRINTF("SenseArea[");
233 for (i
= 0; i
< prb
->SRB_SenseLen
; i
++) {
235 DPRINTF("%02x", *rqbuf
++);
242 ASPI_DebugPrintResult(SRB_ExecSCSICmd
*prb
)
245 TRACE("SRB_Status: %x\n", prb
->SRB_Status
);
246 TRACE("SRB_HaStat: %x\n", prb
->SRB_HaStat
);
247 TRACE("SRB_TargStat: %x\n", prb
->SRB_TargStat
);
248 switch (prb
->CDBByte
[0]) {
250 TRACE("Vendor: '%s'\n", prb
->SRB_BufPointer
+ INQUIRY_VENDOR
);
252 case CMD_TEST_UNIT_READY
:
253 ASPI_PrintSenseArea(prb
);
258 /* Posting must be done in such a way that as soon as the SRB_Status is set
259 * we don't touch the SRB anymore because it could possibly be freed
260 * if the app is doing ASPI polling
263 WNASPI32_DoPosting( SRB_ExecSCSICmd
*lpPRB
, DWORD status
)
265 void (*SRB_PostProc
)() = lpPRB
->SRB_PostProc
;
266 BYTE SRB_Flags
= lpPRB
->SRB_Flags
;
267 if( status
== SS_PENDING
)
269 WARN("Tried posting SS_PENDING\n");
272 lpPRB
->SRB_Status
= status
;
273 /* lpPRB is NOT safe, it could be freed in another thread */
279 TRACE("Post Routine (%lx) called\n", (DWORD
) SRB_PostProc
);
280 /* Even though lpPRB could have been freed by
281 * the program.. that's unlikely if it planned
282 * to use it in the PostProc
284 (*SRB_PostProc
)(lpPRB
);
286 else if (SRB_Flags
& SRB_EVENT_NOTIFY
) {
287 TRACE("Setting event %04x\n", (HANDLE
)SRB_PostProc
);
288 SetEvent((HANDLE
)SRB_PostProc
);
295 ASPI_ExecScsiCmd(SRB_ExecSCSICmd
*lpPRB
)
297 struct sg_header
*sg_hd
, *sg_reply_hdr
;
305 /* FIXME: hackmode */
306 #define MAKE_TARGET_TO_HOST(lpPRB) \
307 if (!TARGET_TO_HOST(lpPRB)) { \
308 WARN("program was not sending target_to_host for cmd %x (flags=%x),correcting.\n",lpPRB->CDBByte[0],lpPRB->SRB_Flags); \
309 lpPRB->SRB_Flags |= 0x08; \
311 #define MAKE_HOST_TO_TARGET(lpPRB) \
312 if (!HOST_TO_TARGET(lpPRB)) { \
313 WARN("program was not sending host_to_target for cmd %x (flags=%x),correcting.\n",lpPRB->CDBByte[0],lpPRB->SRB_Flags); \
314 lpPRB->SRB_Flags |= 0x10; \
316 switch (lpPRB
->CDBByte
[0]) {
317 case 0x12: /* INQUIRY */
318 case 0x5a: /* MODE_SENSE_10 */
319 case 0xa4: /* REPORT_KEY (DVD) MMC-2 */
320 case 0xad: /* READ DVD STRUCTURE MMC-2 */
321 MAKE_TARGET_TO_HOST(lpPRB
)
323 case 0xa3: /* SEND KEY (DVD) MMC-2 */
324 MAKE_HOST_TO_TARGET(lpPRB
)
327 if ((((lpPRB
->SRB_Flags
& 0x18) == 0x00) ||
328 ((lpPRB
->SRB_Flags
& 0x18) == 0x18)
329 ) && lpPRB
->SRB_BufLen
331 FIXME("command 0x%02x, no data transfer specified, but buflen is %ld!!!\n",lpPRB
->CDBByte
[0],lpPRB
->SRB_BufLen
);
335 ASPI_DebugPrintCmd(lpPRB
);
336 if (lpPRB
->SRB_HaId
> ASPI_GetNumControllers()) {
337 ERR("Failed: Wanted hostadapter %d, but we have only %d.\n",
338 lpPRB
->SRB_HaId
,ASPI_GetNumControllers()
340 return WNASPI32_DoPosting( lpPRB
, SS_INVALID_HA
);
342 fd
= ASPI_OpenDevice(lpPRB
);
344 return WNASPI32_DoPosting( lpPRB
, SS_NO_DEVICE
);
350 lpPRB
->SRB_Status
= SS_PENDING
;
352 if (!lpPRB
->SRB_CDBLen
) {
353 ERR("Failed: lpPRB->SRB_CDBLen = 0.\n");
354 return WNASPI32_DoPosting( lpPRB
, SS_INVALID_SRB
);
357 /* build up sg_header + scsi cmd */
358 if (HOST_TO_TARGET(lpPRB
)) {
359 /* send header, command, and then data */
360 in_len
= SCSI_OFF
+ lpPRB
->SRB_CDBLen
+ lpPRB
->SRB_BufLen
;
361 sg_hd
= (struct sg_header
*) HeapAlloc(GetProcessHeap(), 0, in_len
);
362 memset(sg_hd
, 0, SCSI_OFF
);
363 memcpy(sg_hd
+ 1, &lpPRB
->CDBByte
[0], lpPRB
->SRB_CDBLen
);
364 if (lpPRB
->SRB_BufLen
) {
365 memcpy(((BYTE
*) sg_hd
) + SCSI_OFF
+ lpPRB
->SRB_CDBLen
, lpPRB
->SRB_BufPointer
, lpPRB
->SRB_BufLen
);
369 /* send header and command - no data */
370 in_len
= SCSI_OFF
+ lpPRB
->SRB_CDBLen
;
371 sg_hd
= (struct sg_header
*) HeapAlloc(GetProcessHeap(), 0, in_len
);
372 memset(sg_hd
, 0, SCSI_OFF
);
373 memcpy(sg_hd
+ 1, &lpPRB
->CDBByte
[0], lpPRB
->SRB_CDBLen
);
376 if (TARGET_TO_HOST(lpPRB
)) {
377 out_len
= SCSI_OFF
+ lpPRB
->SRB_BufLen
;
378 sg_reply_hdr
= (struct sg_header
*) HeapAlloc(GetProcessHeap(), 0, out_len
);
379 memset(sg_reply_hdr
, 0, SCSI_OFF
);
380 sg_hd
->reply_len
= out_len
;
384 sg_reply_hdr
= (struct sg_header
*) HeapAlloc(GetProcessHeap(), 0, out_len
);
385 memset(sg_reply_hdr
, 0, SCSI_OFF
);
386 sg_hd
->reply_len
= out_len
;
389 SCSI_Fix_CMD_LEN(fd
, lpPRB
->CDBByte
[0], lpPRB
->SRB_CDBLen
);
391 if(!SCSI_LinuxDeviceIo( fd
,
393 sg_reply_hdr
, out_len
,
399 if (sg_reply_hdr
->result
!= 0) {
400 error_code
= sg_reply_hdr
->result
;
401 WARN("reply header error (%d)\n", sg_reply_hdr
->result
);
405 if (TARGET_TO_HOST(lpPRB
) && lpPRB
->SRB_BufLen
) {
406 memcpy(lpPRB
->SRB_BufPointer
, sg_reply_hdr
+ 1, lpPRB
->SRB_BufLen
);
409 /* copy in sense buffer to amount that is available in client */
410 if (lpPRB
->SRB_SenseLen
) {
411 int sense_len
= lpPRB
->SRB_SenseLen
;
412 if (lpPRB
->SRB_SenseLen
> 16)
415 /* CDB is fixed in WNASPI32 */
416 memcpy(lpPRB
->SenseArea
, &sg_reply_hdr
->sense_buffer
[0], sense_len
);
418 ASPI_PrintCDBArea(lpPRB
);
419 ASPI_PrintSenseArea(lpPRB
);
422 SRB_Status
= SS_COMP
;
423 lpPRB
->SRB_HaStat
= HASTAT_OK
;
424 lpPRB
->SRB_TargStat
= sg_reply_hdr
->target_status
<< 1;
426 HeapFree(GetProcessHeap(), 0, sg_reply_hdr
);
427 HeapFree(GetProcessHeap(), 0, sg_hd
);
429 /* FIXME: Should this be != 0 maybe? */
430 if( lpPRB
->SRB_TargStat
== 2 ) {
432 switch (lpPRB
->CDBByte
[0]) {
433 case 0xa4: /* REPORT_KEY (DVD) MMC-2 */
434 case 0xa3: /* SEND KEY (DVD) MMC-2 */
435 SRB_Status
= SS_COMP
;
436 lpPRB
->SRB_TargStat
= 0;
437 FIXME("Program wants to do DVD Region switching, but fails (non compliant DVD drive). Ignoring....\n");
442 ASPI_DebugPrintResult(lpPRB
);
444 ret
= WNASPI32_DoPosting( lpPRB
, SRB_Status
);
446 switch (lpPRB
->CDBByte
[0]) {
448 if (SRB_Status
== SS_COMP
)
449 return SS_COMP
; /* some junk expects ss_comp here. */
455 /* In real WNASPI32 stuff really is always pending because ASPI does things
456 in the background, but we are not doing that (yet) */
462 if (error_code
== EBUSY
) {
463 WNASPI32_DoPosting( lpPRB
, SS_ASPI_IS_BUSY
);
464 TRACE("Device busy\n");
468 /* I'm not sure exactly error codes work here
469 * We probably should set lpPRB->SRB_TargStat, SRB_HaStat ?
471 WARN("error_exit\n");
472 HeapFree(GetProcessHeap(), 0, sg_reply_hdr
);
473 HeapFree(GetProcessHeap(), 0, sg_hd
);
474 WNASPI32_DoPosting( lpPRB
, SRB_Status
);
478 #endif /* defined(linux) */
481 /*******************************************************************
482 * GetASPI32SupportInfo [WNASPI32.1]
484 * Checks if the ASPI subsystem is initialized correctly.
488 * HIBYTE of LOWORD: status (SS_COMP or SS_FAILED_INIT)
489 * LOBYTE of LOWORD: # of host adapters.
491 DWORD __cdecl
GetASPI32SupportInfo(void)
493 DWORD controllers
= ASPI_GetNumControllers();
496 return SS_NO_ADAPTERS
<< 8;
497 return (SS_COMP
<< 8) | controllers
;
500 /***********************************************************************
501 * SendASPI32Command (WNASPI32.2)
503 DWORD __cdecl
SendASPI32Command(LPSRB lpSRB
)
506 switch (lpSRB
->common
.SRB_Cmd
) {
508 lpSRB
->inquiry
.SRB_Status
= SS_COMP
; /* completed successfully */
509 lpSRB
->inquiry
.HA_Count
= ASPI_GetNumControllers();
510 lpSRB
->inquiry
.HA_SCSI_ID
= 7; /* not always ID 7 */
511 strcpy(lpSRB
->inquiry
.HA_ManagerId
, "ASPI for WIN32"); /* max 15 chars, don't change */
512 strcpy(lpSRB
->inquiry
.HA_Identifier
, "Wine host"); /* FIXME: return host adapter name */
513 memset(lpSRB
->inquiry
.HA_Unique
, 0, 16); /* default HA_Unique content */
514 lpSRB
->inquiry
.HA_Unique
[6] = 0x02; /* Maximum Transfer Length (128K, Byte> 4-7) */
515 lpSRB
->inquiry
.HA_Unique
[3] = 0x08; /* Maximum number of SCSI targets */
516 FIXME("ASPI: Partially implemented SC_HA_INQUIRY for adapter %d.\n", lpSRB
->inquiry
.SRB_HaId
);
519 case SC_GET_DEV_TYPE
: {
520 /* FIXME: We should return SS_NO_DEVICE if the device is not configured */
521 /* FIXME: We should return SS_INVALID_HA if HostAdapter!=0 */
526 memset(&tmpsrb
,0,sizeof(tmpsrb
));
529 memcpy(&tmpsrb
.common
,&(lpSRB
->common
),sizeof(tmpsrb
.common
));
531 tmpsrb
.cmd
.SRB_Flags
|= 8; /* target to host */
532 tmpsrb
.cmd
.SRB_Cmd
= SC_EXEC_SCSI_CMD
;
533 tmpsrb
.cmd
.SRB_Target
= lpSRB
->devtype
.SRB_Target
;
534 tmpsrb
.cmd
.SRB_Lun
= lpSRB
->devtype
.SRB_Lun
;
535 tmpsrb
.cmd
.SRB_BufLen
= sizeof(inqbuf
);
536 tmpsrb
.cmd
.SRB_BufPointer
= inqbuf
;
537 tmpsrb
.cmd
.CDBByte
[0] = 0x12; /* INQUIRY */
538 /* FIXME: handle lun */
539 tmpsrb
.cmd
.CDBByte
[4] = sizeof(inqbuf
);
540 tmpsrb
.cmd
.SRB_CDBLen
= 6;
542 ret
= ASPI_ExecScsiCmd(&tmpsrb
.cmd
);
544 lpSRB
->devtype
.SRB_Status
= tmpsrb
.cmd
.SRB_Status
;
545 lpSRB
->devtype
.SRB_DeviceType
= inqbuf
[0]&0x1f;
547 TRACE("returning devicetype %d for target %d\n",inqbuf
[0]&0x1f,tmpsrb
.cmd
.SRB_Target
);
548 if (ret
!=SS_PENDING
) /* Any error is passed down directly */
550 /* FIXME: knows that the command is finished already, pass final Status */
551 return tmpsrb
.cmd
.SRB_Status
;
553 case SC_EXEC_SCSI_CMD
:
554 return ASPI_ExecScsiCmd(&lpSRB
->cmd
);
556 FIXME("Not implemented SC_ABORT_SRB\n");
559 FIXME("Not implemented SC_RESET_DEV\n");
561 case SC_GET_DISK_INFO
:
562 /* here we have to find out the int13 / bios association.
563 * We just say we do not have any.
565 FIXME("SC_GET_DISK_INFO always return 'int13 unassociated disk'.\n");
566 lpSRB
->diskinfo
.SRB_DriveFlags
= 0; /* disk is not int13 served */
569 FIXME("Unknown command %d\n", lpSRB
->common
.SRB_Cmd
);
571 return SS_INVALID_SRB
;
573 return SS_INVALID_SRB
;
578 /***********************************************************************
579 * GetASPI32DLLVersion (WNASPI32.4)
581 DWORD __cdecl
GetASPI32DLLVersion(void)
584 TRACE("Returning version 1\n");
587 FIXME("Please add SCSI support for your operating system, returning 0\n");
592 /***********************************************************************
593 * GetASPI32Buffer (WNASPI32.8)
594 * Supposed to return a DMA capable large SCSI buffer.
595 * Our implementation does not use those at all, all buffer stuff is
596 * done in the kernel SG device layer. So we just heapalloc the buffer.
598 BOOL __cdecl
GetASPI32Buffer(PASPI32BUFF pab
)
600 pab
->AB_BufPointer
= HeapAlloc(GetProcessHeap(),
601 pab
->AB_ZeroFill
?HEAP_ZERO_MEMORY
:0,
604 if (!pab
->AB_BufPointer
) return FALSE
;
608 /***********************************************************************
609 * FreeASPI32Buffer (WNASPI32.14)
611 BOOL __cdecl
FreeASPI32Buffer(PASPI32BUFF pab
)
613 HeapFree(GetProcessHeap(),0,pab
->AB_BufPointer
);
617 /***********************************************************************
618 * TranslateASPI32Address (WNASPI32.7)
620 BOOL __cdecl
TranslateASPI32Address(LPDWORD pdwPath
, LPDWORD pdwDEVNODE
)
622 FIXME("(%p, %p), stub !\n", pdwPath
, pdwDEVNODE
);