ntdll/tests: Remove duplicated entry.
[wine.git] / dlls / winaspi.dll16 / winaspi.c
blob0755b4dc10957e299aa2a7f1a16a74ac39528c40
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 "wine/winbase16.h"
38 #include "winreg.h"
39 #include "wownt32.h"
40 #include "aspi.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 /* Copy of info from 2.2.x kernel */
54 #define SG_MAX_SENSE 16 /* too little, unlikely to change in 2.2.x */
56 struct sg_header
58 int pack_len; /* [o] reply_len (ie useless), ignored as input */
59 int reply_len; /* [i] max length of expected reply (inc. sg_header) */
60 int pack_id; /* [io] id number of packet (use ints >= 0) */
61 int result; /* [o] 0==ok, else (+ve) Unix errno (best ignored) */
62 unsigned int twelve_byte:1;
63 /* [i] Force 12 byte command length for group 6 & 7 commands */
64 unsigned int target_status:5; /* [o] scsi status from target */
65 unsigned int host_status:8; /* [o] host status (see "DID" codes) */
66 unsigned int driver_status:8; /* [o] driver status+suggestion */
67 unsigned int other_flags:10; /* unused */
68 unsigned char sense_buffer[SG_MAX_SENSE]; /* [o] Output in 3 cases:
69 when target_status is CHECK_CONDITION or
70 when target_status is COMMAND_TERMINATED or
71 when (driver_status & DRIVER_SENSE) is true. */
72 }; /* This structure is 36 bytes long on i386 */
74 #define SCSI_OFF sizeof(struct sg_header)
76 #define PTR_TO_LIN(ptr,mode) \
77 ((mode) == ASPI_DOS ? ((void*)(((unsigned int)SELECTOROF(ptr) << 4) + OFFSETOF(ptr))) : MapSL(ptr))
79 WINE_DEFAULT_DEBUG_CHANNEL(aspi);
81 /* Just a container for seeing what devices are open */
82 struct ASPI_DEVICE_INFO {
83 struct ASPI_DEVICE_INFO * next;
84 int fd;
85 int hostId;
86 int target;
87 int lun;
90 typedef struct ASPI_DEVICE_INFO ASPI_DEVICE_INFO;
92 static ASPI_DEVICE_INFO *ASPI_open_devices = NULL;
94 static FARPROC16 ASPIChainFunc = NULL;
95 static WORD HA_Count = 1; /* host adapter count; FIXME: detect it */
97 static int
98 ASPI_OpenDevice16(SRB_ExecSCSICmd16 *prb)
100 HKEY hkey;
101 int fd;
102 char idstr[50];
103 char device_str[50];
104 ASPI_DEVICE_INFO *curr;
106 /* search list of devices to see if we've opened it already.
107 * There is not an explicit open/close in ASPI land, so hopefully
108 * keeping a device open won't be a problem.
111 for (curr = ASPI_open_devices; curr; curr = curr->next) {
112 if (curr->hostId == prb->SRB_HaId &&
113 curr->target == prb->SRB_Target &&
114 curr->lun == prb->SRB_Lun) {
115 return curr->fd;
119 /* device wasn't cached, go ahead and open it */
120 sprintf( idstr, "Software\\Wine\\Wine\\Config\\scsi c%1dt%1dd%1d",
121 prb->SRB_HaId, prb->SRB_Target, prb->SRB_Lun);
123 device_str[0] = 0;
124 if (!RegOpenKeyExA( HKEY_LOCAL_MACHINE, idstr, 0, KEY_ALL_ACCESS, &hkey ))
126 DWORD type, count = sizeof(device_str);
127 if (RegQueryValueExA( hkey, "Device", 0, &type, (LPBYTE)device_str, &count )) device_str[0] = 0;
128 RegCloseKey( hkey );
131 if (!device_str[0])
133 TRACE("Trying to open unlisted scsi device %s\n", idstr);
134 return -1;
137 TRACE("Opening device %s=%s\n", idstr, device_str);
139 fd = open(device_str, O_RDWR);
140 if (fd == -1) {
141 int save_error = errno;
142 ERR("Error opening device %s, error '%s'\n", device_str, strerror(save_error));
143 return -1;
146 /* device is now open */
147 curr = HeapAlloc( GetProcessHeap(), 0, sizeof(ASPI_DEVICE_INFO) );
148 curr->fd = fd;
149 curr->hostId = prb->SRB_HaId;
150 curr->target = prb->SRB_Target;
151 curr->lun = prb->SRB_Lun;
153 /* insert new record at beginning of open device list */
154 curr->next = ASPI_open_devices;
155 ASPI_open_devices = curr;
156 return fd;
160 static void
161 ASPI_DebugPrintCmd(SRB_ExecSCSICmd16 *prb, UINT16 mode)
163 int i;
164 BYTE *cdb;
165 BYTE *lpBuf = PTR_TO_LIN( prb->SRB_BufPointer, mode );
167 switch (prb->CDBByte[0]) {
168 case CMD_INQUIRY:
169 TRACE("{\n");
170 TRACE("\tEVPD: %d\n", prb->CDBByte[1] & 1);
171 TRACE("\tLUN: %d\n", (prb->CDBByte[1] & 0xc) >> 1);
172 TRACE("\tPAGE CODE: %d\n", prb->CDBByte[2]);
173 TRACE("\tALLOCATION LENGTH: %d\n", prb->CDBByte[4]);
174 TRACE("\tCONTROL: %d\n", prb->CDBByte[5]);
175 TRACE("}\n");
176 break;
177 case CMD_SCAN_SCAN:
178 TRACE("Transfer Length: %d\n", prb->CDBByte[4]);
179 break;
182 TRACE("Host Adapter: %d\n", prb->SRB_HaId);
183 TRACE("Flags: %d\n", prb->SRB_Flags);
184 if (TARGET_TO_HOST(prb)) {
185 TRACE("\tData transfer: Target to host. Length checked.\n");
187 else if (HOST_TO_TARGET(prb)) {
188 TRACE("\tData transfer: Host to target. Length checked.\n");
190 else if (NO_DATA_TRANSFERRED(prb)) {
191 TRACE("\tData transfer: none\n");
193 else {
194 WARN("\tTransfer by scsi cmd. Length not checked\n");
197 TRACE("\tResidual byte length reporting %s\n", prb->SRB_Flags & 0x4 ? "enabled" : "disabled");
198 TRACE("\tLinking %s\n", prb->SRB_Flags & 0x2 ? "enabled" : "disabled");
199 TRACE("\tPosting %s\n", prb->SRB_Flags & 0x1 ? "enabled" : "disabled");
200 TRACE("Target: %d\n", prb->SRB_Target);
201 TRACE("Lun: %d\n", prb->SRB_Lun);
202 TRACE("BufLen: %d\n", prb->SRB_BufLen);
203 TRACE("SenseLen: %d\n", prb->SRB_SenseLen);
204 TRACE("BufPtr: %x (%p)\n", prb->SRB_BufPointer, lpBuf);
205 TRACE("LinkPointer %x\n", prb->SRB_Rsvd1);
206 TRACE("CDB Length: %d\n", prb->SRB_CDBLen);
207 TRACE("POST Proc: %x\n", (DWORD) prb->SRB_PostProc);
208 cdb = prb->CDBByte;
209 if (TRACE_ON(aspi))
211 TRACE("CDB buffer[");
212 for (i = 0; i < prb->SRB_CDBLen; i++) {
213 if (i != 0) TRACE(",");
214 TRACE("%02x", *cdb++);
216 TRACE("]\n");
220 static void
221 ASPI_PrintSenseArea16(SRB_ExecSCSICmd16 *prb)
223 int i;
224 BYTE *cdb;
226 if (TRACE_ON(aspi))
228 cdb = prb->CDBByte;
229 TRACE("SenseArea[");
230 for (i = 0; i < prb->SRB_SenseLen; i++) {
231 if (i) TRACE(",");
232 TRACE("%02x", *cdb++);
234 TRACE("]\n");
238 static void
239 ASPI_DebugPrintResult(SRB_ExecSCSICmd16 *prb, UINT16 mode)
241 BYTE *lpBuf = PTR_TO_LIN( prb->SRB_BufPointer, mode );
243 switch (prb->CDBByte[0]) {
244 case CMD_INQUIRY:
245 TRACE("Vendor: '%s'\n", lpBuf + INQUIRY_VENDOR);
246 break;
247 case CMD_TEST_UNIT_READY:
248 ASPI_PrintSenseArea16(prb);
249 break;
253 static WORD
254 ASPI_ExecScsiCmd(DWORD ptrPRB, UINT16 mode)
256 SRB_ExecSCSICmd16 *lpPRB = PTR_TO_LIN( ptrPRB, mode );
257 struct sg_header *sg_hd, *sg_reply_hdr;
258 int status;
259 BYTE *lpBuf = 0;
260 int in_len, out_len;
261 int error_code = 0;
262 int fd;
264 ASPI_DebugPrintCmd(lpPRB, mode);
266 fd = ASPI_OpenDevice16(lpPRB);
267 if (fd == -1) {
268 WARN("Failed: could not open device. Device permissions !?\n");
269 lpPRB->SRB_Status = SS_ERR;
270 return SS_ERR;
273 sg_hd = NULL;
274 sg_reply_hdr = NULL;
276 lpPRB->SRB_Status = SS_PENDING;
277 lpBuf = PTR_TO_LIN( lpPRB->SRB_BufPointer, mode );
279 if (!lpPRB->SRB_CDBLen) {
280 WARN("Failed: lpPRB->SRB_CDBLen = 0.\n");
281 lpPRB->SRB_Status = SS_ERR;
282 return SS_ERR;
285 /* build up sg_header + scsi cmd */
286 if (HOST_TO_TARGET(lpPRB)) {
287 /* send header, command, and then data */
288 in_len = SCSI_OFF + lpPRB->SRB_CDBLen + lpPRB->SRB_BufLen;
289 sg_hd = HeapAlloc(GetProcessHeap(), 0, in_len);
290 memset(sg_hd, 0, SCSI_OFF);
291 memcpy(sg_hd + 1, lpPRB->CDBByte, lpPRB->SRB_CDBLen);
292 if (lpPRB->SRB_BufLen) {
293 memcpy(((BYTE *) sg_hd) + SCSI_OFF + lpPRB->SRB_CDBLen, lpBuf, lpPRB->SRB_BufLen);
296 else {
297 /* send header and command - no data */
298 in_len = SCSI_OFF + lpPRB->SRB_CDBLen;
299 sg_hd = HeapAlloc(GetProcessHeap(), 0, in_len);
300 memset(sg_hd, 0, SCSI_OFF);
301 memcpy(sg_hd + 1, lpPRB->CDBByte, lpPRB->SRB_CDBLen);
304 if (TARGET_TO_HOST(lpPRB)) {
305 out_len = SCSI_OFF + lpPRB->SRB_BufLen;
306 sg_reply_hdr = HeapAlloc(GetProcessHeap(), 0, out_len);
307 memset(sg_reply_hdr, 0, SCSI_OFF);
308 sg_hd->reply_len = out_len;
310 else {
311 out_len = SCSI_OFF;
312 sg_reply_hdr = HeapAlloc(GetProcessHeap(), 0, out_len);
313 memset(sg_reply_hdr, 0, SCSI_OFF);
314 sg_hd->reply_len = out_len;
317 status = write(fd, sg_hd, in_len);
318 if (status < 0 || status != in_len) {
319 int save_error = errno;
321 WARN("Not enough bytes written to scsi device bytes=%d .. %d\n", in_len, status);
322 if (status < 0) {
323 if (save_error == ENOMEM) {
324 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");
326 WARN("error:= '%s'\n", strerror(save_error));
328 goto error_exit;
331 status = read(fd, sg_reply_hdr, out_len);
332 if (status < 0 || status != out_len) {
333 WARN("not enough bytes read from scsi device%d\n", status);
334 goto error_exit;
337 if (sg_reply_hdr->result != 0) {
338 error_code = sg_reply_hdr->result;
339 WARN("reply header error (%d)\n", sg_reply_hdr->result);
340 goto error_exit;
343 if (TARGET_TO_HOST(lpPRB) && lpPRB->SRB_BufLen) {
344 memcpy(lpBuf, sg_reply_hdr + 1, lpPRB->SRB_BufLen);
347 /* copy in sense buffer to amount that is available in client */
348 if (lpPRB->SRB_SenseLen) {
349 int sense_len = lpPRB->SRB_SenseLen;
350 if (lpPRB->SRB_SenseLen > 16)
351 sense_len = 16;
352 memcpy(SENSE_BUFFER(lpPRB), sg_reply_hdr->sense_buffer, sense_len);
356 lpPRB->SRB_Status = SS_COMP;
357 lpPRB->SRB_HaStat = HASTAT_OK;
358 lpPRB->SRB_TargStat = STATUS_GOOD;
360 /* now do posting */
362 if (ASPI_POSTING(lpPRB) && lpPRB->SRB_PostProc) {
363 TRACE("Post Routine (%x) called\n", (DWORD) lpPRB->SRB_PostProc);
364 switch (mode)
366 case ASPI_DOS:
368 SEGPTR spPRB = MapLS(lpPRB);
370 WOWCallback16((DWORD)lpPRB->SRB_PostProc, spPRB);
371 UnMapLS(spPRB);
372 break;
374 case ASPI_WIN16:
375 WOWCallback16((DWORD)lpPRB->SRB_PostProc, ptrPRB);
376 break;
380 HeapFree(GetProcessHeap(), 0, sg_reply_hdr);
381 HeapFree(GetProcessHeap(), 0, sg_hd);
382 ASPI_DebugPrintResult(lpPRB, mode);
383 return SS_COMP;
385 error_exit:
386 if (error_code == EBUSY) {
387 lpPRB->SRB_Status = SS_ASPI_IS_BUSY;
388 TRACE("Device busy\n");
390 else {
391 WARN("Failed\n");
392 lpPRB->SRB_Status = SS_ERR;
395 /* I'm not sure exactly error codes work here
396 * We probably should set lpPRB->SRB_TargStat, SRB_HaStat ?
398 WARN("error_exit\n");
399 HeapFree(GetProcessHeap(), 0, sg_reply_hdr);
400 HeapFree(GetProcessHeap(), 0, sg_hd);
401 return lpPRB->SRB_Status;
403 #endif
406 /***********************************************************************
407 * GetASPISupportInfo (WINASPI.1)
410 WORD WINAPI GetASPISupportInfo16(void)
412 #ifdef linux
413 TRACE("GETASPISupportInfo16\n");
414 /* high byte SS_COMP - low byte number of host adapters */
415 return ((SS_COMP << 8) | HA_Count);
416 #else
417 return ((SS_NO_ASPI << 8) | 0);
418 #endif
422 static DWORD ASPI_SendASPICommand(DWORD ptrSRB, UINT16 mode)
424 #ifdef linux
425 LPSRB16 lpSRB = PTR_TO_LIN( ptrSRB, mode );
426 static const char szId[] = "Wine ASPI16";
427 static const char szWh[] = "Wine host";
429 if (mode == ASPI_WIN16 && ASPIChainFunc)
431 /* This is not the post proc, it's the chain proc this time */
432 DWORD ret = WOWCallback16((DWORD)ASPIChainFunc, ptrSRB);
433 if (ret)
435 lpSRB->inquiry.SRB_Status = SS_INVALID_SRB;
436 return ret;
440 switch (lpSRB->common.SRB_Cmd) {
441 case SC_HA_INQUIRY:
442 lpSRB->inquiry.SRB_Status = SS_COMP; /* completed successfully */
443 if (lpSRB->inquiry.SRB_55AASignature == 0x55aa) {
444 TRACE("Extended request detected (Adaptec's ASPIxDOS).\nWe don't support it at the moment.\n");
446 lpSRB->inquiry.SRB_ExtBufferSize = 0x2000; /* bogus value */
447 lpSRB->inquiry.HA_Count = HA_Count;
448 lpSRB->inquiry.HA_SCSI_ID = 7; /* not always ID 7 */
449 memcpy(lpSRB->inquiry.HA_ManagerId, szId, sizeof szId); /* max 15 chars */
450 memcpy(lpSRB->inquiry.HA_Identifier, szWh, sizeof szWh); /* FIXME: return host
451 adapter name */
452 memset(lpSRB->inquiry.HA_Unique, 0, 16); /* default HA_Unique content */
453 lpSRB->inquiry.HA_Unique[6] = 0x02; /* Maximum Transfer Length (128K, Byte> 4-7) */
454 FIXME("ASPI: Partially implemented SC_HA_INQUIRY for adapter %d.\n", lpSRB->inquiry.SRB_HaId);
455 return SS_COMP;
456 case SC_GET_DEV_TYPE:
457 FIXME("Not implemented SC_GET_DEV_TYPE\n");
458 break;
459 case SC_EXEC_SCSI_CMD:
460 return ASPI_ExecScsiCmd(ptrSRB, mode);
461 case SC_RESET_DEV:
462 FIXME("Not implemented SC_RESET_DEV\n");
463 break;
464 default:
465 FIXME("Unknown command %d\n", lpSRB->common.SRB_Cmd);
467 #endif
468 return SS_INVALID_SRB;
472 /***********************************************************************
473 * SendASPICommand (WINASPI.2)
475 WORD WINAPI SendASPICommand16(SEGPTR segptr_srb)
477 return ASPI_SendASPICommand(segptr_srb, ASPI_WIN16);
481 /***********************************************************************
482 * InsertInASPIChain (WINASPI.3)
484 WORD WINAPI InsertInASPIChain16(BOOL16 remove, FARPROC16 pASPIChainFunc)
486 #ifdef linux
487 if (remove) /* Remove */
489 if (ASPIChainFunc == pASPIChainFunc)
491 ASPIChainFunc = NULL;
492 return SS_COMP;
495 else /* Insert */
497 if (ASPIChainFunc == NULL)
499 ASPIChainFunc = pASPIChainFunc;
500 return SS_COMP;
503 #endif
504 return SS_ERR;
508 /***********************************************************************
509 * GETASPIDLLVERSION (WINASPI.4)
512 DWORD WINAPI GetASPIDLLVersion16(void)
514 #ifdef linux
515 return 2;
516 #else
517 return 0;
518 #endif