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>
34 #include "wine/windef16.h"
38 #include "wine/winaspi.h"
39 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(aspi
);
46 * 1) Residual byte length reporting not handled
47 * 2) Make this code re-entrant for multithreading
48 * 3) Only linux supported so far
51 /* ### start build ### */
52 extern LONG CALLBACK
ASPI_CallTo16_long_l(FARPROC16
,SEGPTR
);
53 /* ### stop build ### */
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
)
136 /* translate real mode address */
137 if (prb
->SRB_BufPointer
)
138 lpBuf
= PTR_REAL_TO_LIN( SELECTOROF(prb
->SRB_BufPointer
), OFFSETOF(prb
->SRB_BufPointer
));
141 lpBuf
= MapSL(prb
->SRB_BufPointer
);
145 switch (prb
->CDBByte
[0]) {
148 TRACE("\tEVPD: %d\n", prb
->CDBByte
[1] & 1);
149 TRACE("\tLUN: %d\n", (prb
->CDBByte
[1] & 0xc) >> 1);
150 TRACE("\tPAGE CODE: %d\n", prb
->CDBByte
[2]);
151 TRACE("\tALLOCATION LENGTH: %d\n", prb
->CDBByte
[4]);
152 TRACE("\tCONTROL: %d\n", prb
->CDBByte
[5]);
156 TRACE("Transfer Length: %d\n", prb
->CDBByte
[4]);
160 TRACE("Host Adapter: %d\n", prb
->SRB_HaId
);
161 TRACE("Flags: %d\n", prb
->SRB_Flags
);
162 if (TARGET_TO_HOST(prb
)) {
163 TRACE("\tData transfer: Target to host. Length checked.\n");
165 else if (HOST_TO_TARGET(prb
)) {
166 TRACE("\tData transfer: Host to target. Length checked.\n");
168 else if (NO_DATA_TRANSFERED(prb
)) {
169 TRACE("\tData transfer: none\n");
172 WARN("\tTransfer by scsi cmd. Length not checked\n");
175 TRACE("\tResidual byte length reporting %s\n", prb
->SRB_Flags
& 0x4 ? "enabled" : "disabled");
176 TRACE("\tLinking %s\n", prb
->SRB_Flags
& 0x2 ? "enabled" : "disabled");
177 TRACE("\tPosting %s\n", prb
->SRB_Flags
& 0x1 ? "enabled" : "disabled");
178 TRACE("Target: %d\n", prb
->SRB_Target
);
179 TRACE("Lun: %d\n", prb
->SRB_Lun
);
180 TRACE("BufLen: %ld\n", prb
->SRB_BufLen
);
181 TRACE("SenseLen: %d\n", prb
->SRB_SenseLen
);
182 TRACE("BufPtr: %lx (%p)\n", prb
->SRB_BufPointer
, lpBuf
);
183 TRACE("LinkPointer %lx\n", prb
->SRB_Rsvd1
);
184 TRACE("CDB Length: %d\n", prb
->SRB_CDBLen
);
185 TRACE("POST Proc: %lx\n", (DWORD
) prb
->SRB_PostProc
);
186 cdb
= &prb
->CDBByte
[0];
187 cmd
= prb
->CDBByte
[0];
190 DPRINTF("CDB buffer[");
191 for (i
= 0; i
< prb
->SRB_CDBLen
; i
++) {
192 if (i
!= 0) DPRINTF(",");
193 DPRINTF("%02x", *cdb
++);
200 ASPI_PrintSenseArea16(SRB_ExecSCSICmd16
*prb
)
207 cdb
= &prb
->CDBByte
[0];
208 DPRINTF("SenseArea[");
209 for (i
= 0; i
< prb
->SRB_SenseLen
; i
++) {
211 DPRINTF("%02x", *cdb
++);
218 ASPI_DebugPrintResult(SRB_ExecSCSICmd16
*prb
, UINT16 mode
)
225 /* translate real mode address */
226 if (prb
->SRB_BufPointer
)
227 lpBuf
= PTR_REAL_TO_LIN( SELECTOROF(prb
->SRB_BufPointer
), OFFSETOF(prb
->SRB_BufPointer
));
230 lpBuf
= MapSL(prb
->SRB_BufPointer
);
234 switch (prb
->CDBByte
[0]) {
236 TRACE("Vendor: '%s'\n", lpBuf
+ INQUIRY_VENDOR
);
238 case CMD_TEST_UNIT_READY
:
239 ASPI_PrintSenseArea16(prb
);
245 ASPI_ExecScsiCmd(DWORD ptrPRB
, UINT16 mode
)
247 SRB_ExecSCSICmd16
*lpPRB
= 0;
248 struct sg_header
*sg_hd
, *sg_reply_hdr
;
259 lpPRB
= PTR_REAL_TO_LIN( SELECTOROF(ptrPRB
), OFFSETOF(ptrPRB
));
262 lpPRB
= MapSL(ptrPRB
);
266 ASPI_DebugPrintCmd(lpPRB
, mode
);
268 fd
= ASPI_OpenDevice16(lpPRB
);
270 WARN("Failed: could not open device. Device permissions !?\n");
271 lpPRB
->SRB_Status
= SS_ERR
;
278 lpPRB
->SRB_Status
= SS_PENDING
;
283 /* translate real mode address */
285 lpBuf
= PTR_REAL_TO_LIN( SELECTOROF(lpPRB
->SRB_BufPointer
),
286 OFFSETOF(lpPRB
->SRB_BufPointer
));
289 lpBuf
= MapSL(lpPRB
->SRB_BufPointer
);
293 if (!lpPRB
->SRB_CDBLen
) {
294 WARN("Failed: lpPRB->SRB_CDBLen = 0.\n");
295 lpPRB
->SRB_Status
= SS_ERR
;
299 /* build up sg_header + scsi cmd */
300 if (HOST_TO_TARGET(lpPRB
)) {
301 /* send header, command, and then data */
302 in_len
= SCSI_OFF
+ lpPRB
->SRB_CDBLen
+ lpPRB
->SRB_BufLen
;
303 sg_hd
= (struct sg_header
*) malloc(in_len
);
304 memset(sg_hd
, 0, SCSI_OFF
);
305 memcpy(sg_hd
+ 1, &lpPRB
->CDBByte
[0], lpPRB
->SRB_CDBLen
);
306 if (lpPRB
->SRB_BufLen
) {
307 memcpy(((BYTE
*) sg_hd
) + SCSI_OFF
+ lpPRB
->SRB_CDBLen
, lpBuf
, lpPRB
->SRB_BufLen
);
311 /* send header and command - no data */
312 in_len
= SCSI_OFF
+ lpPRB
->SRB_CDBLen
;
313 sg_hd
= (struct sg_header
*) malloc(in_len
);
314 memset(sg_hd
, 0, SCSI_OFF
);
315 memcpy(sg_hd
+ 1, &lpPRB
->CDBByte
[0], lpPRB
->SRB_CDBLen
);
318 if (TARGET_TO_HOST(lpPRB
)) {
319 out_len
= SCSI_OFF
+ lpPRB
->SRB_BufLen
;
320 sg_reply_hdr
= (struct sg_header
*) malloc(out_len
);
321 memset(sg_reply_hdr
, 0, SCSI_OFF
);
322 sg_hd
->reply_len
= out_len
;
326 sg_reply_hdr
= (struct sg_header
*) malloc(out_len
);
327 memset(sg_reply_hdr
, 0, SCSI_OFF
);
328 sg_hd
->reply_len
= out_len
;
331 status
= write(fd
, sg_hd
, in_len
);
332 if (status
< 0 || status
!= in_len
) {
333 int save_error
= errno
;
335 WARN("Not enough bytes written to scsi device bytes=%d .. %d\n", in_len
, status
);
337 if (save_error
== ENOMEM
) {
338 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");
340 WARN("error:= '%s'\n", strerror(save_error
));
345 status
= read(fd
, sg_reply_hdr
, out_len
);
346 if (status
< 0 || status
!= out_len
) {
347 WARN("not enough bytes read from scsi device%d\n", status
);
351 if (sg_reply_hdr
->result
!= 0) {
352 error_code
= sg_reply_hdr
->result
;
353 WARN("reply header error (%d)\n", sg_reply_hdr
->result
);
357 if (TARGET_TO_HOST(lpPRB
) && lpPRB
->SRB_BufLen
) {
358 memcpy(lpBuf
, sg_reply_hdr
+ 1, lpPRB
->SRB_BufLen
);
361 /* copy in sense buffer to amount that is available in client */
362 if (lpPRB
->SRB_SenseLen
) {
363 int sense_len
= lpPRB
->SRB_SenseLen
;
364 if (lpPRB
->SRB_SenseLen
> 16)
366 memcpy(SENSE_BUFFER(lpPRB
), &sg_reply_hdr
->sense_buffer
[0], sense_len
);
370 lpPRB
->SRB_Status
= SS_COMP
;
371 lpPRB
->SRB_HaStat
= HASTAT_OK
;
372 lpPRB
->SRB_TargStat
= STATUS_GOOD
;
376 if (ASPI_POSTING(lpPRB
) && lpPRB
->SRB_PostProc
) {
377 TRACE("Post Routine (%lx) called\n", (DWORD
) lpPRB
->SRB_PostProc
);
382 SEGPTR spPRB
= MapLS(lpPRB
);
384 ASPI_CallTo16_long_l(lpPRB
->SRB_PostProc
, spPRB
);
389 ASPI_CallTo16_long_l(lpPRB
->SRB_PostProc
, ptrPRB
);
396 ASPI_DebugPrintResult(lpPRB
, mode
);
400 if (error_code
== EBUSY
) {
401 lpPRB
->SRB_Status
= SS_ASPI_IS_BUSY
;
402 TRACE("Device busy\n");
406 lpPRB
->SRB_Status
= SS_ERR
;
409 /* I'm not sure exactly error codes work here
410 * We probably should set lpPRB->SRB_TargStat, SRB_HaStat ?
412 WARN("error_exit\n");
415 return lpPRB
->SRB_Status
;
420 /***********************************************************************
421 * GetASPISupportInfo (WINASPI.1)
424 WORD WINAPI
GetASPISupportInfo16(void)
427 TRACE("GETASPISupportInfo16\n");
428 /* high byte SS_COMP - low byte number of host adapters */
429 return ((SS_COMP
<< 8) | HA_Count
);
431 return ((SS_NO_ASPI
<< 8) | 0);
436 DWORD
ASPI_SendASPICommand(DWORD ptrSRB
, UINT16 mode
)
445 lpSRB
= PTR_REAL_TO_LIN( SELECTOROF(ptrSRB
), OFFSETOF(ptrSRB
));
448 lpSRB
= MapSL(ptrSRB
);
451 /* This is not the post proc, it's the chain proc this time */
452 DWORD ret
= ASPI_CallTo16_long_l(ASPIChainFunc
, ptrSRB
);
455 lpSRB
->inquiry
.SRB_Status
= SS_INVALID_SRB
;
462 switch (lpSRB
->common
.SRB_Cmd
) {
464 lpSRB
->inquiry
.SRB_Status
= SS_COMP
; /* completed successfully */
465 if (lpSRB
->inquiry
.SRB_55AASignature
== 0x55aa) {
466 TRACE("Extended request detected (Adaptec's ASPIxDOS).\nWe don't support it at the moment.\n");
468 lpSRB
->inquiry
.SRB_ExtBufferSize
= 0x2000; /* bogus value */
469 lpSRB
->inquiry
.HA_Count
= HA_Count
;
470 lpSRB
->inquiry
.HA_SCSI_ID
= 7; /* not always ID 7 */
471 strcat(lpSRB
->inquiry
.HA_ManagerId
, "Wine ASPI16"); /* max 15 chars */
472 strcat(lpSRB
->inquiry
.HA_Identifier
, "Wine host"); /* FIXME: return host
474 memset(lpSRB
->inquiry
.HA_Unique
, 0, 16); /* default HA_Unique content */
475 lpSRB
->inquiry
.HA_Unique
[6] = 0x02; /* Maximum Transfer Length (128K, Byte> 4-7) */
476 FIXME("ASPI: Partially implemented SC_HA_INQUIRY for adapter %d.\n", lpSRB
->inquiry
.SRB_HaId
);
478 case SC_GET_DEV_TYPE
:
479 FIXME("Not implemented SC_GET_DEV_TYPE\n");
481 case SC_EXEC_SCSI_CMD
:
482 return ASPI_ExecScsiCmd((DWORD
)ptrSRB
, mode
);
485 FIXME("Not implemented SC_RESET_DEV\n");
488 FIXME("Unknown command %d\n", lpSRB
->common
.SRB_Cmd
);
491 return SS_INVALID_SRB
;
495 /***********************************************************************
496 * SendASPICommand (WINASPI.2)
498 WORD WINAPI
SendASPICommand16(SEGPTR segptr_srb
)
501 return ASPI_SendASPICommand(segptr_srb
, ASPI_WIN16
);
508 /***********************************************************************
509 * InsertInASPIChain (WINASPI.3)
511 WORD WINAPI
InsertInASPIChain16(BOOL16 remove
, FARPROC16 pASPIChainFunc
)
514 if (remove
== TRUE
) /* Remove */
516 if (ASPIChainFunc
== pASPIChainFunc
)
518 ASPIChainFunc
= NULL
;
523 if (remove
== FALSE
) /* Insert */
525 if (ASPIChainFunc
== NULL
)
527 ASPIChainFunc
= pASPIChainFunc
;
536 /***********************************************************************
537 * GETASPIDLLVERSION (WINASPI.4)
540 DWORD WINAPI
GetASPIDLLVersion16()