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
23 #include <sys/types.h>
35 #include "wine/windef16.h"
40 #include "wine/winaspi.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(aspi
);
47 * 1) Residual byte length reporting not handled
48 * 2) Make this code re-entrant for multithreading
49 * 3) Only linux supported so far
54 #define PTR_TO_LIN(ptr,mode) \
55 ((mode) == ASPI_DOS ? ((void*)(((unsigned int)SELECTOROF(ptr) << 4) + OFFSETOF(ptr))) : MapSL(ptr))
57 static ASPI_DEVICE_INFO
*ASPI_open_devices
= NULL
;
59 static FARPROC16 ASPIChainFunc
= NULL
;
60 static WORD HA_Count
= 1; /* host adapter count; FIXME: detect it */
63 ASPI_OpenDevice16(SRB_ExecSCSICmd16
*prb
)
69 ASPI_DEVICE_INFO
*curr
;
71 /* search list of devices to see if we've opened it already.
72 * There is not an explicit open/close in ASPI land, so hopefully
73 * keeping a device open won't be a problem.
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
) {
84 /* device wasn't cached, go ahead and open it */
85 sprintf( idstr
, "Software\\Wine\\Wine\\Config\\scsi c%1dt%1dd%1d",
86 prb
->SRB_HaId
, prb
->SRB_Target
, prb
->SRB_Lun
);
89 if (!RegOpenKeyExA( HKEY_LOCAL_MACHINE
, idstr
, 0, KEY_ALL_ACCESS
, &hkey
))
91 DWORD type
, count
= sizeof(device_str
);
92 if (RegQueryValueExA( hkey
, "Device", 0, &type
, device_str
, &count
)) device_str
[0] = 0;
98 TRACE("Trying to open unlisted scsi device %s\n", idstr
);
102 TRACE("Opening device %s=%s\n", idstr
, device_str
);
104 fd
= open(device_str
, O_RDWR
);
106 int save_error
= errno
;
107 ERR("Error opening device %s, error '%s'\n", device_str
, strerror(save_error
));
111 /* device is now open */
112 curr
= HeapAlloc( GetProcessHeap(), 0, sizeof(ASPI_DEVICE_INFO
) );
114 curr
->hostId
= prb
->SRB_HaId
;
115 curr
->target
= prb
->SRB_Target
;
116 curr
->lun
= prb
->SRB_Lun
;
118 /* insert new record at beginning of open device list */
119 curr
->next
= ASPI_open_devices
;
120 ASPI_open_devices
= curr
;
126 ASPI_DebugPrintCmd(SRB_ExecSCSICmd16
*prb
, UINT16 mode
)
131 BYTE
*lpBuf
= PTR_TO_LIN( prb
->SRB_BufPointer
, mode
);
133 switch (prb
->CDBByte
[0]) {
136 TRACE("\tEVPD: %d\n", prb
->CDBByte
[1] & 1);
137 TRACE("\tLUN: %d\n", (prb
->CDBByte
[1] & 0xc) >> 1);
138 TRACE("\tPAGE CODE: %d\n", prb
->CDBByte
[2]);
139 TRACE("\tALLOCATION LENGTH: %d\n", prb
->CDBByte
[4]);
140 TRACE("\tCONTROL: %d\n", prb
->CDBByte
[5]);
144 TRACE("Transfer Length: %d\n", prb
->CDBByte
[4]);
148 TRACE("Host Adapter: %d\n", prb
->SRB_HaId
);
149 TRACE("Flags: %d\n", prb
->SRB_Flags
);
150 if (TARGET_TO_HOST(prb
)) {
151 TRACE("\tData transfer: Target to host. Length checked.\n");
153 else if (HOST_TO_TARGET(prb
)) {
154 TRACE("\tData transfer: Host to target. Length checked.\n");
156 else if (NO_DATA_TRANSFERRED(prb
)) {
157 TRACE("\tData transfer: none\n");
160 WARN("\tTransfer by scsi cmd. Length not checked\n");
163 TRACE("\tResidual byte length reporting %s\n", prb
->SRB_Flags
& 0x4 ? "enabled" : "disabled");
164 TRACE("\tLinking %s\n", prb
->SRB_Flags
& 0x2 ? "enabled" : "disabled");
165 TRACE("\tPosting %s\n", prb
->SRB_Flags
& 0x1 ? "enabled" : "disabled");
166 TRACE("Target: %d\n", prb
->SRB_Target
);
167 TRACE("Lun: %d\n", prb
->SRB_Lun
);
168 TRACE("BufLen: %ld\n", prb
->SRB_BufLen
);
169 TRACE("SenseLen: %d\n", prb
->SRB_SenseLen
);
170 TRACE("BufPtr: %lx (%p)\n", prb
->SRB_BufPointer
, lpBuf
);
171 TRACE("LinkPointer %lx\n", prb
->SRB_Rsvd1
);
172 TRACE("CDB Length: %d\n", prb
->SRB_CDBLen
);
173 TRACE("POST Proc: %lx\n", (DWORD
) prb
->SRB_PostProc
);
174 cdb
= &prb
->CDBByte
[0];
175 cmd
= prb
->CDBByte
[0];
178 TRACE("CDB buffer[");
179 for (i
= 0; i
< prb
->SRB_CDBLen
; i
++) {
180 if (i
!= 0) TRACE(",");
181 TRACE("%02x", *cdb
++);
188 ASPI_PrintSenseArea16(SRB_ExecSCSICmd16
*prb
)
195 cdb
= &prb
->CDBByte
[0];
197 for (i
= 0; i
< prb
->SRB_SenseLen
; i
++) {
199 TRACE("%02x", *cdb
++);
206 ASPI_DebugPrintResult(SRB_ExecSCSICmd16
*prb
, UINT16 mode
)
208 BYTE
*lpBuf
= PTR_TO_LIN( prb
->SRB_BufPointer
, mode
);
210 switch (prb
->CDBByte
[0]) {
212 TRACE("Vendor: '%s'\n", lpBuf
+ INQUIRY_VENDOR
);
214 case CMD_TEST_UNIT_READY
:
215 ASPI_PrintSenseArea16(prb
);
221 ASPI_ExecScsiCmd(DWORD ptrPRB
, UINT16 mode
)
223 SRB_ExecSCSICmd16
*lpPRB
= PTR_TO_LIN( ptrPRB
, mode
);
224 struct sg_header
*sg_hd
, *sg_reply_hdr
;
231 ASPI_DebugPrintCmd(lpPRB
, mode
);
233 fd
= ASPI_OpenDevice16(lpPRB
);
235 WARN("Failed: could not open device. Device permissions !?\n");
236 lpPRB
->SRB_Status
= SS_ERR
;
243 lpPRB
->SRB_Status
= SS_PENDING
;
244 lpBuf
= PTR_TO_LIN( lpPRB
->SRB_BufPointer
, mode
);
246 if (!lpPRB
->SRB_CDBLen
) {
247 WARN("Failed: lpPRB->SRB_CDBLen = 0.\n");
248 lpPRB
->SRB_Status
= SS_ERR
;
252 /* build up sg_header + scsi cmd */
253 if (HOST_TO_TARGET(lpPRB
)) {
254 /* send header, command, and then data */
255 in_len
= SCSI_OFF
+ lpPRB
->SRB_CDBLen
+ lpPRB
->SRB_BufLen
;
256 sg_hd
= (struct sg_header
*) malloc(in_len
);
257 memset(sg_hd
, 0, SCSI_OFF
);
258 memcpy(sg_hd
+ 1, &lpPRB
->CDBByte
[0], lpPRB
->SRB_CDBLen
);
259 if (lpPRB
->SRB_BufLen
) {
260 memcpy(((BYTE
*) sg_hd
) + SCSI_OFF
+ lpPRB
->SRB_CDBLen
, lpBuf
, lpPRB
->SRB_BufLen
);
264 /* send header and command - no data */
265 in_len
= SCSI_OFF
+ lpPRB
->SRB_CDBLen
;
266 sg_hd
= (struct sg_header
*) malloc(in_len
);
267 memset(sg_hd
, 0, SCSI_OFF
);
268 memcpy(sg_hd
+ 1, &lpPRB
->CDBByte
[0], lpPRB
->SRB_CDBLen
);
271 if (TARGET_TO_HOST(lpPRB
)) {
272 out_len
= SCSI_OFF
+ lpPRB
->SRB_BufLen
;
273 sg_reply_hdr
= (struct sg_header
*) malloc(out_len
);
274 memset(sg_reply_hdr
, 0, SCSI_OFF
);
275 sg_hd
->reply_len
= out_len
;
279 sg_reply_hdr
= (struct sg_header
*) malloc(out_len
);
280 memset(sg_reply_hdr
, 0, SCSI_OFF
);
281 sg_hd
->reply_len
= out_len
;
284 status
= write(fd
, sg_hd
, in_len
);
285 if (status
< 0 || status
!= in_len
) {
286 int save_error
= errno
;
288 WARN("Not enough bytes written to scsi device bytes=%d .. %d\n", in_len
, status
);
290 if (save_error
== ENOMEM
) {
291 MESSAGE("ASPI: Linux generic scsi driver\n You probably need to re-compile your kernel with a larger SG_BIG_BUFF value (sg.h)\n Suggest 130560\n");
293 WARN("error:= '%s'\n", strerror(save_error
));
298 status
= read(fd
, sg_reply_hdr
, out_len
);
299 if (status
< 0 || status
!= out_len
) {
300 WARN("not enough bytes read from scsi device%d\n", status
);
304 if (sg_reply_hdr
->result
!= 0) {
305 error_code
= sg_reply_hdr
->result
;
306 WARN("reply header error (%d)\n", sg_reply_hdr
->result
);
310 if (TARGET_TO_HOST(lpPRB
) && lpPRB
->SRB_BufLen
) {
311 memcpy(lpBuf
, sg_reply_hdr
+ 1, lpPRB
->SRB_BufLen
);
314 /* copy in sense buffer to amount that is available in client */
315 if (lpPRB
->SRB_SenseLen
) {
316 int sense_len
= lpPRB
->SRB_SenseLen
;
317 if (lpPRB
->SRB_SenseLen
> 16)
319 memcpy(SENSE_BUFFER(lpPRB
), &sg_reply_hdr
->sense_buffer
[0], sense_len
);
323 lpPRB
->SRB_Status
= SS_COMP
;
324 lpPRB
->SRB_HaStat
= HASTAT_OK
;
325 lpPRB
->SRB_TargStat
= STATUS_GOOD
;
329 if (ASPI_POSTING(lpPRB
) && lpPRB
->SRB_PostProc
) {
330 TRACE("Post Routine (%lx) called\n", (DWORD
) lpPRB
->SRB_PostProc
);
335 SEGPTR spPRB
= MapLS(lpPRB
);
337 WOWCallback16((DWORD
)lpPRB
->SRB_PostProc
, spPRB
);
342 WOWCallback16((DWORD
)lpPRB
->SRB_PostProc
, ptrPRB
);
349 ASPI_DebugPrintResult(lpPRB
, mode
);
353 if (error_code
== EBUSY
) {
354 lpPRB
->SRB_Status
= SS_ASPI_IS_BUSY
;
355 TRACE("Device busy\n");
359 lpPRB
->SRB_Status
= SS_ERR
;
362 /* I'm not sure exactly error codes work here
363 * We probably should set lpPRB->SRB_TargStat, SRB_HaStat ?
365 WARN("error_exit\n");
368 return lpPRB
->SRB_Status
;
373 /***********************************************************************
374 * GetASPISupportInfo (WINASPI.1)
377 WORD WINAPI
GetASPISupportInfo16(void)
380 TRACE("GETASPISupportInfo16\n");
381 /* high byte SS_COMP - low byte number of host adapters */
382 return ((SS_COMP
<< 8) | HA_Count
);
384 return ((SS_NO_ASPI
<< 8) | 0);
389 DWORD
ASPI_SendASPICommand(DWORD ptrSRB
, UINT16 mode
)
392 LPSRB16 lpSRB
= PTR_TO_LIN( ptrSRB
, mode
);
394 if (mode
== ASPI_WIN16
&& ASPIChainFunc
)
396 /* This is not the post proc, it's the chain proc this time */
397 DWORD ret
= WOWCallback16((DWORD
)ASPIChainFunc
, ptrSRB
);
400 lpSRB
->inquiry
.SRB_Status
= SS_INVALID_SRB
;
405 switch (lpSRB
->common
.SRB_Cmd
) {
407 lpSRB
->inquiry
.SRB_Status
= SS_COMP
; /* completed successfully */
408 if (lpSRB
->inquiry
.SRB_55AASignature
== 0x55aa) {
409 TRACE("Extended request detected (Adaptec's ASPIxDOS).\nWe don't support it at the moment.\n");
411 lpSRB
->inquiry
.SRB_ExtBufferSize
= 0x2000; /* bogus value */
412 lpSRB
->inquiry
.HA_Count
= HA_Count
;
413 lpSRB
->inquiry
.HA_SCSI_ID
= 7; /* not always ID 7 */
414 strcat(lpSRB
->inquiry
.HA_ManagerId
, "Wine ASPI16"); /* max 15 chars */
415 strcat(lpSRB
->inquiry
.HA_Identifier
, "Wine host"); /* FIXME: return host
417 memset(lpSRB
->inquiry
.HA_Unique
, 0, 16); /* default HA_Unique content */
418 lpSRB
->inquiry
.HA_Unique
[6] = 0x02; /* Maximum Transfer Length (128K, Byte> 4-7) */
419 FIXME("ASPI: Partially implemented SC_HA_INQUIRY for adapter %d.\n", lpSRB
->inquiry
.SRB_HaId
);
421 case SC_GET_DEV_TYPE
:
422 FIXME("Not implemented SC_GET_DEV_TYPE\n");
424 case SC_EXEC_SCSI_CMD
:
425 return ASPI_ExecScsiCmd((DWORD
)ptrSRB
, mode
);
427 FIXME("Not implemented SC_RESET_DEV\n");
430 FIXME("Unknown command %d\n", lpSRB
->common
.SRB_Cmd
);
433 return SS_INVALID_SRB
;
437 /***********************************************************************
438 * SendASPICommand (WINASPI.2)
440 WORD WINAPI
SendASPICommand16(SEGPTR segptr_srb
)
443 return ASPI_SendASPICommand(segptr_srb
, ASPI_WIN16
);
450 /***********************************************************************
451 * InsertInASPIChain (WINASPI.3)
453 WORD WINAPI
InsertInASPIChain16(BOOL16 remove
, FARPROC16 pASPIChainFunc
)
456 if (remove
) /* Remove */
458 if (ASPIChainFunc
== pASPIChainFunc
)
460 ASPIChainFunc
= NULL
;
466 if (ASPIChainFunc
== NULL
)
468 ASPIChainFunc
= pASPIChainFunc
;
477 /***********************************************************************
478 * GETASPIDLLVERSION (WINASPI.4)
481 DWORD WINAPI
GetASPIDLLVersion16(void)