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"
39 #include "wine/winaspi.h"
40 #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 static ASPI_DEVICE_INFO
*ASPI_open_devices
= NULL
;
56 static FARPROC16 ASPIChainFunc
= NULL
;
57 static WORD HA_Count
= 1; /* host adapter count; FIXME: detect it */
60 ASPI_OpenDevice16(SRB_ExecSCSICmd16
*prb
)
66 ASPI_DEVICE_INFO
*curr
;
68 /* search list of devices to see if we've opened it already.
69 * There is not an explicit open/close in ASPI land, so hopefully
70 * keeping a device open won't be a problem.
73 for (curr
= ASPI_open_devices
; curr
; curr
= curr
->next
) {
74 if (curr
->hostId
== prb
->SRB_HaId
&&
75 curr
->target
== prb
->SRB_Target
&&
76 curr
->lun
== prb
->SRB_Lun
) {
81 /* device wasn't cached, go ahead and open it */
82 sprintf( idstr
, "Software\\Wine\\Wine\\Config\\scsi c%1dt%1dd%1d",
83 prb
->SRB_HaId
, prb
->SRB_Target
, prb
->SRB_Lun
);
86 if (!RegOpenKeyExA( HKEY_LOCAL_MACHINE
, idstr
, 0, KEY_ALL_ACCESS
, &hkey
))
88 DWORD type
, count
= sizeof(device_str
);
89 if (RegQueryValueExA( hkey
, "Device", 0, &type
, device_str
, &count
)) device_str
[0] = 0;
95 TRACE("Trying to open unlisted scsi device %s\n", idstr
);
99 TRACE("Opening device %s=%s\n", idstr
, device_str
);
101 fd
= open(device_str
, O_RDWR
);
103 int save_error
= errno
;
104 ERR("Error opening device %s, error '%s'\n", device_str
, strerror(save_error
));
108 /* device is now open */
109 curr
= HeapAlloc( GetProcessHeap(), 0, sizeof(ASPI_DEVICE_INFO
) );
111 curr
->hostId
= prb
->SRB_HaId
;
112 curr
->target
= prb
->SRB_Target
;
113 curr
->lun
= prb
->SRB_Lun
;
115 /* insert new record at beginning of open device list */
116 curr
->next
= ASPI_open_devices
;
117 ASPI_open_devices
= curr
;
123 ASPI_DebugPrintCmd(SRB_ExecSCSICmd16
*prb
, UINT16 mode
)
133 /* translate real mode address */
134 if (prb
->SRB_BufPointer
)
135 lpBuf
= PTR_REAL_TO_LIN( SELECTOROF(prb
->SRB_BufPointer
), OFFSETOF(prb
->SRB_BufPointer
));
138 lpBuf
= MapSL(prb
->SRB_BufPointer
);
142 switch (prb
->CDBByte
[0]) {
145 TRACE("\tEVPD: %d\n", prb
->CDBByte
[1] & 1);
146 TRACE("\tLUN: %d\n", (prb
->CDBByte
[1] & 0xc) >> 1);
147 TRACE("\tPAGE CODE: %d\n", prb
->CDBByte
[2]);
148 TRACE("\tALLOCATION LENGTH: %d\n", prb
->CDBByte
[4]);
149 TRACE("\tCONTROL: %d\n", prb
->CDBByte
[5]);
153 TRACE("Transfer Length: %d\n", prb
->CDBByte
[4]);
157 TRACE("Host Adapter: %d\n", prb
->SRB_HaId
);
158 TRACE("Flags: %d\n", prb
->SRB_Flags
);
159 if (TARGET_TO_HOST(prb
)) {
160 TRACE("\tData transfer: Target to host. Length checked.\n");
162 else if (HOST_TO_TARGET(prb
)) {
163 TRACE("\tData transfer: Host to target. Length checked.\n");
165 else if (NO_DATA_TRANSFERED(prb
)) {
166 TRACE("\tData transfer: none\n");
169 WARN("\tTransfer by scsi cmd. Length not checked\n");
172 TRACE("\tResidual byte length reporting %s\n", prb
->SRB_Flags
& 0x4 ? "enabled" : "disabled");
173 TRACE("\tLinking %s\n", prb
->SRB_Flags
& 0x2 ? "enabled" : "disabled");
174 TRACE("\tPosting %s\n", prb
->SRB_Flags
& 0x1 ? "enabled" : "disabled");
175 TRACE("Target: %d\n", prb
->SRB_Target
);
176 TRACE("Lun: %d\n", prb
->SRB_Lun
);
177 TRACE("BufLen: %ld\n", prb
->SRB_BufLen
);
178 TRACE("SenseLen: %d\n", prb
->SRB_SenseLen
);
179 TRACE("BufPtr: %lx (%p)\n", prb
->SRB_BufPointer
, lpBuf
);
180 TRACE("LinkPointer %lx\n", prb
->SRB_Rsvd1
);
181 TRACE("CDB Length: %d\n", prb
->SRB_CDBLen
);
182 TRACE("POST Proc: %lx\n", (DWORD
) prb
->SRB_PostProc
);
183 cdb
= &prb
->CDBByte
[0];
184 cmd
= prb
->CDBByte
[0];
187 TRACE("CDB buffer[");
188 for (i
= 0; i
< prb
->SRB_CDBLen
; i
++) {
189 if (i
!= 0) TRACE(",");
190 TRACE("%02x", *cdb
++);
197 ASPI_PrintSenseArea16(SRB_ExecSCSICmd16
*prb
)
204 cdb
= &prb
->CDBByte
[0];
206 for (i
= 0; i
< prb
->SRB_SenseLen
; i
++) {
208 TRACE("%02x", *cdb
++);
215 ASPI_DebugPrintResult(SRB_ExecSCSICmd16
*prb
, UINT16 mode
)
222 /* translate real mode address */
223 if (prb
->SRB_BufPointer
)
224 lpBuf
= PTR_REAL_TO_LIN( SELECTOROF(prb
->SRB_BufPointer
), OFFSETOF(prb
->SRB_BufPointer
));
227 lpBuf
= MapSL(prb
->SRB_BufPointer
);
231 switch (prb
->CDBByte
[0]) {
233 TRACE("Vendor: '%s'\n", lpBuf
+ INQUIRY_VENDOR
);
235 case CMD_TEST_UNIT_READY
:
236 ASPI_PrintSenseArea16(prb
);
242 ASPI_ExecScsiCmd(DWORD ptrPRB
, UINT16 mode
)
244 SRB_ExecSCSICmd16
*lpPRB
= 0;
245 struct sg_header
*sg_hd
, *sg_reply_hdr
;
256 lpPRB
= PTR_REAL_TO_LIN( SELECTOROF(ptrPRB
), OFFSETOF(ptrPRB
));
259 lpPRB
= MapSL(ptrPRB
);
263 ASPI_DebugPrintCmd(lpPRB
, mode
);
265 fd
= ASPI_OpenDevice16(lpPRB
);
267 WARN("Failed: could not open device. Device permissions !?\n");
268 lpPRB
->SRB_Status
= SS_ERR
;
275 lpPRB
->SRB_Status
= SS_PENDING
;
280 /* translate real mode address */
282 lpBuf
= PTR_REAL_TO_LIN( SELECTOROF(lpPRB
->SRB_BufPointer
),
283 OFFSETOF(lpPRB
->SRB_BufPointer
));
286 lpBuf
= MapSL(lpPRB
->SRB_BufPointer
);
290 if (!lpPRB
->SRB_CDBLen
) {
291 WARN("Failed: lpPRB->SRB_CDBLen = 0.\n");
292 lpPRB
->SRB_Status
= SS_ERR
;
296 /* build up sg_header + scsi cmd */
297 if (HOST_TO_TARGET(lpPRB
)) {
298 /* send header, command, and then data */
299 in_len
= SCSI_OFF
+ lpPRB
->SRB_CDBLen
+ lpPRB
->SRB_BufLen
;
300 sg_hd
= (struct sg_header
*) malloc(in_len
);
301 memset(sg_hd
, 0, SCSI_OFF
);
302 memcpy(sg_hd
+ 1, &lpPRB
->CDBByte
[0], lpPRB
->SRB_CDBLen
);
303 if (lpPRB
->SRB_BufLen
) {
304 memcpy(((BYTE
*) sg_hd
) + SCSI_OFF
+ lpPRB
->SRB_CDBLen
, lpBuf
, lpPRB
->SRB_BufLen
);
308 /* send header and command - no data */
309 in_len
= SCSI_OFF
+ lpPRB
->SRB_CDBLen
;
310 sg_hd
= (struct sg_header
*) malloc(in_len
);
311 memset(sg_hd
, 0, SCSI_OFF
);
312 memcpy(sg_hd
+ 1, &lpPRB
->CDBByte
[0], lpPRB
->SRB_CDBLen
);
315 if (TARGET_TO_HOST(lpPRB
)) {
316 out_len
= SCSI_OFF
+ lpPRB
->SRB_BufLen
;
317 sg_reply_hdr
= (struct sg_header
*) malloc(out_len
);
318 memset(sg_reply_hdr
, 0, SCSI_OFF
);
319 sg_hd
->reply_len
= out_len
;
323 sg_reply_hdr
= (struct sg_header
*) malloc(out_len
);
324 memset(sg_reply_hdr
, 0, SCSI_OFF
);
325 sg_hd
->reply_len
= out_len
;
328 status
= write(fd
, sg_hd
, in_len
);
329 if (status
< 0 || status
!= in_len
) {
330 int save_error
= errno
;
332 WARN("Not enough bytes written to scsi device bytes=%d .. %d\n", in_len
, status
);
334 if (save_error
== ENOMEM
) {
335 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");
337 WARN("error:= '%s'\n", strerror(save_error
));
342 status
= read(fd
, sg_reply_hdr
, out_len
);
343 if (status
< 0 || status
!= out_len
) {
344 WARN("not enough bytes read from scsi device%d\n", status
);
348 if (sg_reply_hdr
->result
!= 0) {
349 error_code
= sg_reply_hdr
->result
;
350 WARN("reply header error (%d)\n", sg_reply_hdr
->result
);
354 if (TARGET_TO_HOST(lpPRB
) && lpPRB
->SRB_BufLen
) {
355 memcpy(lpBuf
, sg_reply_hdr
+ 1, lpPRB
->SRB_BufLen
);
358 /* copy in sense buffer to amount that is available in client */
359 if (lpPRB
->SRB_SenseLen
) {
360 int sense_len
= lpPRB
->SRB_SenseLen
;
361 if (lpPRB
->SRB_SenseLen
> 16)
363 memcpy(SENSE_BUFFER(lpPRB
), &sg_reply_hdr
->sense_buffer
[0], sense_len
);
367 lpPRB
->SRB_Status
= SS_COMP
;
368 lpPRB
->SRB_HaStat
= HASTAT_OK
;
369 lpPRB
->SRB_TargStat
= STATUS_GOOD
;
373 if (ASPI_POSTING(lpPRB
) && lpPRB
->SRB_PostProc
) {
374 TRACE("Post Routine (%lx) called\n", (DWORD
) lpPRB
->SRB_PostProc
);
379 SEGPTR spPRB
= MapLS(lpPRB
);
381 WOWCallback16((DWORD
)lpPRB
->SRB_PostProc
, spPRB
);
386 WOWCallback16((DWORD
)lpPRB
->SRB_PostProc
, ptrPRB
);
393 ASPI_DebugPrintResult(lpPRB
, mode
);
397 if (error_code
== EBUSY
) {
398 lpPRB
->SRB_Status
= SS_ASPI_IS_BUSY
;
399 TRACE("Device busy\n");
403 lpPRB
->SRB_Status
= SS_ERR
;
406 /* I'm not sure exactly error codes work here
407 * We probably should set lpPRB->SRB_TargStat, SRB_HaStat ?
409 WARN("error_exit\n");
412 return lpPRB
->SRB_Status
;
417 /***********************************************************************
418 * GetASPISupportInfo (WINASPI.1)
421 WORD WINAPI
GetASPISupportInfo16(void)
424 TRACE("GETASPISupportInfo16\n");
425 /* high byte SS_COMP - low byte number of host adapters */
426 return ((SS_COMP
<< 8) | HA_Count
);
428 return ((SS_NO_ASPI
<< 8) | 0);
433 DWORD
ASPI_SendASPICommand(DWORD ptrSRB
, UINT16 mode
)
442 lpSRB
= PTR_REAL_TO_LIN( SELECTOROF(ptrSRB
), OFFSETOF(ptrSRB
));
445 lpSRB
= MapSL(ptrSRB
);
448 /* This is not the post proc, it's the chain proc this time */
449 DWORD ret
= WOWCallback16((DWORD
)ASPIChainFunc
, ptrSRB
);
452 lpSRB
->inquiry
.SRB_Status
= SS_INVALID_SRB
;
459 switch (lpSRB
->common
.SRB_Cmd
) {
461 lpSRB
->inquiry
.SRB_Status
= SS_COMP
; /* completed successfully */
462 if (lpSRB
->inquiry
.SRB_55AASignature
== 0x55aa) {
463 TRACE("Extended request detected (Adaptec's ASPIxDOS).\nWe don't support it at the moment.\n");
465 lpSRB
->inquiry
.SRB_ExtBufferSize
= 0x2000; /* bogus value */
466 lpSRB
->inquiry
.HA_Count
= HA_Count
;
467 lpSRB
->inquiry
.HA_SCSI_ID
= 7; /* not always ID 7 */
468 strcat(lpSRB
->inquiry
.HA_ManagerId
, "Wine ASPI16"); /* max 15 chars */
469 strcat(lpSRB
->inquiry
.HA_Identifier
, "Wine host"); /* FIXME: return host
471 memset(lpSRB
->inquiry
.HA_Unique
, 0, 16); /* default HA_Unique content */
472 lpSRB
->inquiry
.HA_Unique
[6] = 0x02; /* Maximum Transfer Length (128K, Byte> 4-7) */
473 FIXME("ASPI: Partially implemented SC_HA_INQUIRY for adapter %d.\n", lpSRB
->inquiry
.SRB_HaId
);
475 case SC_GET_DEV_TYPE
:
476 FIXME("Not implemented SC_GET_DEV_TYPE\n");
478 case SC_EXEC_SCSI_CMD
:
479 return ASPI_ExecScsiCmd((DWORD
)ptrSRB
, mode
);
482 FIXME("Not implemented SC_RESET_DEV\n");
485 FIXME("Unknown command %d\n", lpSRB
->common
.SRB_Cmd
);
488 return SS_INVALID_SRB
;
492 /***********************************************************************
493 * SendASPICommand (WINASPI.2)
495 WORD WINAPI
SendASPICommand16(SEGPTR segptr_srb
)
498 return ASPI_SendASPICommand(segptr_srb
, ASPI_WIN16
);
505 /***********************************************************************
506 * InsertInASPIChain (WINASPI.3)
508 WORD WINAPI
InsertInASPIChain16(BOOL16 remove
, FARPROC16 pASPIChainFunc
)
511 if (remove
== TRUE
) /* Remove */
513 if (ASPIChainFunc
== pASPIChainFunc
)
515 ASPIChainFunc
= NULL
;
520 if (remove
== FALSE
) /* Insert */
522 if (ASPIChainFunc
== NULL
)
524 ASPIChainFunc
= pASPIChainFunc
;
533 /***********************************************************************
534 * GETASPIDLLVERSION (WINASPI.4)
537 DWORD WINAPI
GetASPIDLLVersion16()