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
21 #include "wine/port.h"
24 #include <sys/types.h>
36 #include "wine/windef16.h"
41 #include "wine/winaspi.h"
42 #include "wine/debug.h"
46 * 1) Residual byte length reporting not handled
47 * 2) Make this code re-entrant for multithreading
48 * 3) Only linux supported so far
53 #define PTR_TO_LIN(ptr,mode) \
54 ((mode) == ASPI_DOS ? ((void*)(((unsigned int)SELECTOROF(ptr) << 4) + OFFSETOF(ptr))) : MapSL(ptr))
56 WINE_DEFAULT_DEBUG_CHANNEL(aspi);
58 static ASPI_DEVICE_INFO *ASPI_open_devices = NULL;
60 static FARPROC16 ASPIChainFunc = NULL;
61 static WORD HA_Count = 1; /* host adapter count; FIXME: detect it */
64 ASPI_OpenDevice16(SRB_ExecSCSICmd16 *prb)
70 ASPI_DEVICE_INFO *curr;
72 /* search list of devices to see if we've opened it already.
73 * There is not an explicit open/close in ASPI land, so hopefully
74 * keeping a device open won't be a problem.
77 for (curr = ASPI_open_devices; curr; curr = curr->next) {
78 if (curr->hostId == prb->SRB_HaId &&
79 curr->target == prb->SRB_Target &&
80 curr->lun == prb->SRB_Lun) {
85 /* device wasn't cached, go ahead and open it */
86 sprintf( idstr, "Software\\Wine\\Wine\\Config\\scsi c%1dt%1dd%1d",
87 prb->SRB_HaId, prb->SRB_Target, prb->SRB_Lun);
90 if (!RegOpenKeyExA( HKEY_LOCAL_MACHINE, idstr, 0, KEY_ALL_ACCESS, &hkey ))
92 DWORD type, count = sizeof(device_str);
93 if (RegQueryValueExA( hkey, "Device", 0, &type, (LPBYTE)device_str, &count )) device_str[0] = 0;
99 TRACE("Trying to open unlisted scsi device %s\n", idstr);
103 TRACE("Opening device %s=%s\n", idstr, device_str);
105 fd = open(device_str, O_RDWR);
107 int save_error = errno;
108 ERR("Error opening device %s, error '%s'\n", device_str, strerror(save_error));
112 /* device is now open */
113 curr = HeapAlloc( GetProcessHeap(), 0, sizeof(ASPI_DEVICE_INFO) );
115 curr->hostId = prb->SRB_HaId;
116 curr->target = prb->SRB_Target;
117 curr->lun = prb->SRB_Lun;
119 /* insert new record at beginning of open device list */
120 curr->next = ASPI_open_devices;
121 ASPI_open_devices = curr;
127 ASPI_DebugPrintCmd(SRB_ExecSCSICmd16 *prb, UINT16 mode)
132 BYTE *lpBuf = PTR_TO_LIN( prb->SRB_BufPointer, mode );
134 switch (prb->CDBByte[0]) {
137 TRACE("\tEVPD: %d\n", prb->CDBByte[1] & 1);
138 TRACE("\tLUN: %d\n", (prb->CDBByte[1] & 0xc) >> 1);
139 TRACE("\tPAGE CODE: %d\n", prb->CDBByte[2]);
140 TRACE("\tALLOCATION LENGTH: %d\n", prb->CDBByte[4]);
141 TRACE("\tCONTROL: %d\n", prb->CDBByte[5]);
145 TRACE("Transfer Length: %d\n", prb->CDBByte[4]);
149 TRACE("Host Adapter: %d\n", prb->SRB_HaId);
150 TRACE("Flags: %d\n", prb->SRB_Flags);
151 if (TARGET_TO_HOST(prb)) {
152 TRACE("\tData transfer: Target to host. Length checked.\n");
154 else if (HOST_TO_TARGET(prb)) {
155 TRACE("\tData transfer: Host to target. Length checked.\n");
157 else if (NO_DATA_TRANSFERRED(prb)) {
158 TRACE("\tData transfer: none\n");
161 WARN("\tTransfer by scsi cmd. Length not checked\n");
164 TRACE("\tResidual byte length reporting %s\n", prb->SRB_Flags & 0x4 ? "enabled" : "disabled");
165 TRACE("\tLinking %s\n", prb->SRB_Flags & 0x2 ? "enabled" : "disabled");
166 TRACE("\tPosting %s\n", prb->SRB_Flags & 0x1 ? "enabled" : "disabled");
167 TRACE("Target: %d\n", prb->SRB_Target);
168 TRACE("Lun: %d\n", prb->SRB_Lun);
169 TRACE("BufLen: %d\n", prb->SRB_BufLen);
170 TRACE("SenseLen: %d\n", prb->SRB_SenseLen);
171 TRACE("BufPtr: %x (%p)\n", prb->SRB_BufPointer, lpBuf);
172 TRACE("LinkPointer %x\n", prb->SRB_Rsvd1);
173 TRACE("CDB Length: %d\n", prb->SRB_CDBLen);
174 TRACE("POST Proc: %x\n", (DWORD) prb->SRB_PostProc);
175 cdb = &prb->CDBByte[0];
176 cmd = prb->CDBByte[0];
179 TRACE("CDB buffer[");
180 for (i = 0; i < prb->SRB_CDBLen; i++) {
181 if (i != 0) TRACE(",");
182 TRACE("%02x", *cdb++);
189 ASPI_PrintSenseArea16(SRB_ExecSCSICmd16 *prb)
196 cdb = &prb->CDBByte[0];
198 for (i = 0; i < prb->SRB_SenseLen; i++) {
200 TRACE("%02x", *cdb++);
207 ASPI_DebugPrintResult(SRB_ExecSCSICmd16 *prb, UINT16 mode)
209 BYTE *lpBuf = PTR_TO_LIN( prb->SRB_BufPointer, mode );
211 switch (prb->CDBByte[0]) {
213 TRACE("Vendor: '%s'\n", lpBuf + INQUIRY_VENDOR);
215 case CMD_TEST_UNIT_READY:
216 ASPI_PrintSenseArea16(prb);
222 ASPI_ExecScsiCmd(DWORD ptrPRB, UINT16 mode)
224 SRB_ExecSCSICmd16 *lpPRB = PTR_TO_LIN( ptrPRB, mode );
225 struct sg_header *sg_hd, *sg_reply_hdr;
232 ASPI_DebugPrintCmd(lpPRB, mode);
234 fd = ASPI_OpenDevice16(lpPRB);
236 WARN("Failed: could not open device. Device permissions !?\n");
237 lpPRB->SRB_Status = SS_ERR;
244 lpPRB->SRB_Status = SS_PENDING;
245 lpBuf = PTR_TO_LIN( lpPRB->SRB_BufPointer, mode );
247 if (!lpPRB->SRB_CDBLen) {
248 WARN("Failed: lpPRB->SRB_CDBLen = 0.\n");
249 lpPRB->SRB_Status = SS_ERR;
253 /* build up sg_header + scsi cmd */
254 if (HOST_TO_TARGET(lpPRB)) {
255 /* send header, command, and then data */
256 in_len = SCSI_OFF + lpPRB->SRB_CDBLen + lpPRB->SRB_BufLen;
257 sg_hd = (struct sg_header *) malloc(in_len);
258 memset(sg_hd, 0, SCSI_OFF);
259 memcpy(sg_hd + 1, &lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
260 if (lpPRB->SRB_BufLen) {
261 memcpy(((BYTE *) sg_hd) + SCSI_OFF + lpPRB->SRB_CDBLen, lpBuf, lpPRB->SRB_BufLen);
265 /* send header and command - no data */
266 in_len = SCSI_OFF + lpPRB->SRB_CDBLen;
267 sg_hd = (struct sg_header *) malloc(in_len);
268 memset(sg_hd, 0, SCSI_OFF);
269 memcpy(sg_hd + 1, &lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
272 if (TARGET_TO_HOST(lpPRB)) {
273 out_len = SCSI_OFF + lpPRB->SRB_BufLen;
274 sg_reply_hdr = (struct sg_header *) malloc(out_len);
275 memset(sg_reply_hdr, 0, SCSI_OFF);
276 sg_hd->reply_len = out_len;
280 sg_reply_hdr = (struct sg_header *) malloc(out_len);
281 memset(sg_reply_hdr, 0, SCSI_OFF);
282 sg_hd->reply_len = out_len;
285 status = write(fd, sg_hd, in_len);
286 if (status < 0 || status != in_len) {
287 int save_error = errno;
289 WARN("Not enough bytes written to scsi device bytes=%d .. %d\n", in_len, status);
291 if (save_error == ENOMEM) {
292 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");
294 WARN("error:= '%s'\n", strerror(save_error));
299 status = read(fd, sg_reply_hdr, out_len);
300 if (status < 0 || status != out_len) {
301 WARN("not enough bytes read from scsi device%d\n", status);
305 if (sg_reply_hdr->result != 0) {
306 error_code = sg_reply_hdr->result;
307 WARN("reply header error (%d)\n", sg_reply_hdr->result);
311 if (TARGET_TO_HOST(lpPRB) && lpPRB->SRB_BufLen) {
312 memcpy(lpBuf, sg_reply_hdr + 1, lpPRB->SRB_BufLen);
315 /* copy in sense buffer to amount that is available in client */
316 if (lpPRB->SRB_SenseLen) {
317 int sense_len = lpPRB->SRB_SenseLen;
318 if (lpPRB->SRB_SenseLen > 16)
320 memcpy(SENSE_BUFFER(lpPRB), &sg_reply_hdr->sense_buffer[0], sense_len);
324 lpPRB->SRB_Status = SS_COMP;
325 lpPRB->SRB_HaStat = HASTAT_OK;
326 lpPRB->SRB_TargStat = STATUS_GOOD;
330 if (ASPI_POSTING(lpPRB) && lpPRB->SRB_PostProc) {
331 TRACE("Post Routine (%x) called\n", (DWORD) lpPRB->SRB_PostProc);
336 SEGPTR spPRB = MapLS(lpPRB);
338 WOWCallback16((DWORD)lpPRB->SRB_PostProc, spPRB);
343 WOWCallback16((DWORD)lpPRB->SRB_PostProc, ptrPRB);
350 ASPI_DebugPrintResult(lpPRB, mode);
354 if (error_code == EBUSY) {
355 lpPRB->SRB_Status = SS_ASPI_IS_BUSY;
356 TRACE("Device busy\n");
360 lpPRB->SRB_Status = SS_ERR;
363 /* I'm not sure exactly error codes work here
364 * We probably should set lpPRB->SRB_TargStat, SRB_HaStat ?
366 WARN("error_exit\n");
369 return lpPRB->SRB_Status;
374 /***********************************************************************
375 * GetASPISupportInfo (WINASPI.1)
378 WORD WINAPI GetASPISupportInfo16(void)
381 TRACE("GETASPISupportInfo16\n");
382 /* high byte SS_COMP - low byte number of host adapters */
383 return ((SS_COMP << 8) | HA_Count);
385 return ((SS_NO_ASPI << 8) | 0);
390 DWORD ASPI_SendASPICommand(DWORD ptrSRB, UINT16 mode)
393 LPSRB16 lpSRB = PTR_TO_LIN( ptrSRB, mode );
394 static const char szId[] = "Wine ASPI16";
395 static const char szWh[] = "Wine host";
397 if (mode == ASPI_WIN16 && ASPIChainFunc)
399 /* This is not the post proc, it's the chain proc this time */
400 DWORD ret = WOWCallback16((DWORD)ASPIChainFunc, ptrSRB);
403 lpSRB->inquiry.SRB_Status = SS_INVALID_SRB;
408 switch (lpSRB->common.SRB_Cmd) {
410 lpSRB->inquiry.SRB_Status = SS_COMP; /* completed successfully */
411 if (lpSRB->inquiry.SRB_55AASignature == 0x55aa) {
412 TRACE("Extended request detected (Adaptec's ASPIxDOS).\nWe don't support it at the moment.\n");
414 lpSRB->inquiry.SRB_ExtBufferSize = 0x2000; /* bogus value */
415 lpSRB->inquiry.HA_Count = HA_Count;
416 lpSRB->inquiry.HA_SCSI_ID = 7; /* not always ID 7 */
417 memcpy(lpSRB->inquiry.HA_ManagerId, szId, sizeof szId); /* max 15 chars */
418 memcpy(lpSRB->inquiry.HA_Identifier, szWh, sizeof szWh); /* FIXME: return host
420 memset(lpSRB->inquiry.HA_Unique, 0, 16); /* default HA_Unique content */
421 lpSRB->inquiry.HA_Unique[6] = 0x02; /* Maximum Transfer Length (128K, Byte> 4-7) */
422 FIXME("ASPI: Partially implemented SC_HA_INQUIRY for adapter %d.\n", lpSRB->inquiry.SRB_HaId);
424 case SC_GET_DEV_TYPE:
425 FIXME("Not implemented SC_GET_DEV_TYPE\n");
427 case SC_EXEC_SCSI_CMD:
428 return ASPI_ExecScsiCmd(ptrSRB, mode);
430 FIXME("Not implemented SC_RESET_DEV\n");
433 FIXME("Unknown command %d\n", lpSRB->common.SRB_Cmd);
436 return SS_INVALID_SRB;
440 /***********************************************************************
441 * SendASPICommand (WINASPI.2)
443 WORD WINAPI SendASPICommand16(SEGPTR segptr_srb)
446 return ASPI_SendASPICommand(segptr_srb, ASPI_WIN16);
453 /***********************************************************************
454 * InsertInASPIChain (WINASPI.3)
456 WORD WINAPI InsertInASPIChain16(BOOL16 remove, FARPROC16 pASPIChainFunc)
459 if (remove) /* Remove */
461 if (ASPIChainFunc == pASPIChainFunc)
463 ASPIChainFunc = NULL;
469 if (ASPIChainFunc == NULL)
471 ASPIChainFunc = pASPIChainFunc;
480 /***********************************************************************
481 * GETASPIDLLVERSION (WINASPI.4)
484 DWORD WINAPI GetASPIDLLVersion16(void)