d3d9: Fix a few ok conditions in the visual tests.
[wine.git] / dlls / wnaspi32 / winaspi16.c
blobb8dcf27538bcee574ecad5f4bb97d3679939f060
1 /*
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
20 #include "config.h"
21 #include "wine/port.h"
23 #include <stdlib.h>
24 #include <sys/types.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #include <string.h>
31 #include <stdarg.h>
32 #include <stdio.h>
34 #include "windef.h"
35 #include "winbase.h"
36 #include "wine/windef16.h"
37 #include "winreg.h"
38 #include "wownt32.h"
39 #include "aspi.h"
40 #include "winescsi.h"
41 #include "wine/winaspi.h"
42 #include "wine/debug.h"
45 /* FIXME!
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 #ifdef linux
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 */
63 static int
64 ASPI_OpenDevice16(SRB_ExecSCSICmd16 *prb)
66 HKEY hkey;
67 int fd;
68 char idstr[50];
69 char device_str[50];
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) {
81 return curr->fd;
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);
89 device_str[0] = 0;
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;
94 RegCloseKey( hkey );
97 if (!device_str[0])
99 TRACE("Trying to open unlisted scsi device %s\n", idstr);
100 return -1;
103 TRACE("Opening device %s=%s\n", idstr, device_str);
105 fd = open(device_str, O_RDWR);
106 if (fd == -1) {
107 int save_error = errno;
108 ERR("Error opening device %s, error '%s'\n", device_str, strerror(save_error));
109 return -1;
112 /* device is now open */
113 curr = HeapAlloc( GetProcessHeap(), 0, sizeof(ASPI_DEVICE_INFO) );
114 curr->fd = fd;
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;
122 return fd;
126 static void
127 ASPI_DebugPrintCmd(SRB_ExecSCSICmd16 *prb, UINT16 mode)
129 BYTE cmd;
130 int i;
131 BYTE *cdb;
132 BYTE *lpBuf = PTR_TO_LIN( prb->SRB_BufPointer, mode );
134 switch (prb->CDBByte[0]) {
135 case CMD_INQUIRY:
136 TRACE("{\n");
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]);
142 TRACE("}\n");
143 break;
144 case CMD_SCAN_SCAN:
145 TRACE("Transfer Length: %d\n", prb->CDBByte[4]);
146 break;
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");
160 else {
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];
177 if (TRACE_ON(aspi))
179 TRACE("CDB buffer[");
180 for (i = 0; i < prb->SRB_CDBLen; i++) {
181 if (i != 0) TRACE(",");
182 TRACE("%02x", *cdb++);
184 TRACE("]\n");
188 static void
189 ASPI_PrintSenseArea16(SRB_ExecSCSICmd16 *prb)
191 int i;
192 BYTE *cdb;
194 if (TRACE_ON(aspi))
196 cdb = &prb->CDBByte[0];
197 TRACE("SenseArea[");
198 for (i = 0; i < prb->SRB_SenseLen; i++) {
199 if (i) TRACE(",");
200 TRACE("%02x", *cdb++);
202 TRACE("]\n");
206 static void
207 ASPI_DebugPrintResult(SRB_ExecSCSICmd16 *prb, UINT16 mode)
209 BYTE *lpBuf = PTR_TO_LIN( prb->SRB_BufPointer, mode );
211 switch (prb->CDBByte[0]) {
212 case CMD_INQUIRY:
213 TRACE("Vendor: '%s'\n", lpBuf + INQUIRY_VENDOR);
214 break;
215 case CMD_TEST_UNIT_READY:
216 ASPI_PrintSenseArea16(prb);
217 break;
221 static WORD
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;
226 int status;
227 BYTE *lpBuf = 0;
228 int in_len, out_len;
229 int error_code = 0;
230 int fd;
232 ASPI_DebugPrintCmd(lpPRB, mode);
234 fd = ASPI_OpenDevice16(lpPRB);
235 if (fd == -1) {
236 WARN("Failed: could not open device. Device permissions !?\n");
237 lpPRB->SRB_Status = SS_ERR;
238 return SS_ERR;
241 sg_hd = NULL;
242 sg_reply_hdr = NULL;
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;
250 return 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);
264 else {
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;
278 else {
279 out_len = SCSI_OFF;
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);
290 if (status < 0) {
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));
296 goto error_exit;
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);
302 goto error_exit;
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);
308 goto error_exit;
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)
319 sense_len = 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;
328 /* now do posting */
330 if (ASPI_POSTING(lpPRB) && lpPRB->SRB_PostProc) {
331 TRACE("Post Routine (%x) called\n", (DWORD) lpPRB->SRB_PostProc);
332 switch (mode)
334 case ASPI_DOS:
336 SEGPTR spPRB = MapLS(lpPRB);
338 WOWCallback16((DWORD)lpPRB->SRB_PostProc, spPRB);
339 UnMapLS(spPRB);
340 break;
342 case ASPI_WIN16:
343 WOWCallback16((DWORD)lpPRB->SRB_PostProc, ptrPRB);
344 break;
348 free(sg_reply_hdr);
349 free(sg_hd);
350 ASPI_DebugPrintResult(lpPRB, mode);
351 return SS_COMP;
353 error_exit:
354 if (error_code == EBUSY) {
355 lpPRB->SRB_Status = SS_ASPI_IS_BUSY;
356 TRACE("Device busy\n");
358 else {
359 WARN("Failed\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");
367 free(sg_reply_hdr);
368 free(sg_hd);
369 return lpPRB->SRB_Status;
371 #endif
374 /***********************************************************************
375 * GetASPISupportInfo (WINASPI.1)
378 WORD WINAPI GetASPISupportInfo16(void)
380 #ifdef linux
381 TRACE("GETASPISupportInfo16\n");
382 /* high byte SS_COMP - low byte number of host adapters */
383 return ((SS_COMP << 8) | HA_Count);
384 #else
385 return ((SS_NO_ASPI << 8) | 0);
386 #endif
390 DWORD ASPI_SendASPICommand(DWORD ptrSRB, UINT16 mode)
392 #ifdef linux
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);
401 if (ret)
403 lpSRB->inquiry.SRB_Status = SS_INVALID_SRB;
404 return ret;
408 switch (lpSRB->common.SRB_Cmd) {
409 case SC_HA_INQUIRY:
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
419 adapter name */
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);
423 return SS_COMP;
424 case SC_GET_DEV_TYPE:
425 FIXME("Not implemented SC_GET_DEV_TYPE\n");
426 break;
427 case SC_EXEC_SCSI_CMD:
428 return ASPI_ExecScsiCmd(ptrSRB, mode);
429 case SC_RESET_DEV:
430 FIXME("Not implemented SC_RESET_DEV\n");
431 break;
432 default:
433 FIXME("Unknown command %d\n", lpSRB->common.SRB_Cmd);
435 #endif
436 return SS_INVALID_SRB;
440 /***********************************************************************
441 * SendASPICommand (WINASPI.2)
443 WORD WINAPI SendASPICommand16(SEGPTR segptr_srb)
445 #ifdef linux
446 return ASPI_SendASPICommand(segptr_srb, ASPI_WIN16);
447 #else
448 return 0;
449 #endif
453 /***********************************************************************
454 * InsertInASPIChain (WINASPI.3)
456 WORD WINAPI InsertInASPIChain16(BOOL16 remove, FARPROC16 pASPIChainFunc)
458 #ifdef linux
459 if (remove) /* Remove */
461 if (ASPIChainFunc == pASPIChainFunc)
463 ASPIChainFunc = NULL;
464 return SS_COMP;
467 else /* Insert */
469 if (ASPIChainFunc == NULL)
471 ASPIChainFunc = pASPIChainFunc;
472 return SS_COMP;
475 #endif
476 return SS_ERR;
480 /***********************************************************************
481 * GETASPIDLLVERSION (WINASPI.4)
484 DWORD WINAPI GetASPIDLLVersion16(void)
486 #ifdef linux
487 return 2;
488 #else
489 return 0;
490 #endif