Added PCF_TOTALTIMEOUTS to comm properties.
[wine/wine64.git] / dlls / winaspi / winaspi16.c
blob740f69a8e680d7f90a1940599dc2cae529aa0122
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "config.h"
22 #include <stdlib.h>
23 #include <sys/types.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <memory.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <stdio.h>
31 #include "winbase.h"
32 #include "wine/windef16.h"
33 #include "winreg.h"
34 #include "aspi.h"
35 #include "winescsi.h"
36 #include "wine/winaspi.h"
37 #include "wine/debug.h"
38 #include "miscemu.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(aspi);
43 /* FIXME!
44 * 1) Residual byte length reporting not handled
45 * 2) Make this code re-entrant for multithreading
46 * 3) Only linux supported so far
49 /* ### start build ### */
50 extern LONG CALLBACK ASPI_CallTo16_long_l(FARPROC16,SEGPTR);
51 /* ### stop build ### */
53 #ifdef linux
55 static ASPI_DEVICE_INFO *ASPI_open_devices = NULL;
57 static FARPROC16 ASPIChainFunc = NULL;
58 static WORD HA_Count = 1; /* host adapter count; FIXME: detect it */
60 static int
61 ASPI_OpenDevice16(SRB_ExecSCSICmd16 *prb)
63 HKEY hkey;
64 int fd;
65 char idstr[50];
66 char device_str[50];
67 ASPI_DEVICE_INFO *curr;
69 /* search list of devices to see if we've opened it already.
70 * There is not an explicit open/close in ASPI land, so hopefully
71 * keeping a device open won't be a problem.
74 for (curr = ASPI_open_devices; curr; curr = curr->next) {
75 if (curr->hostId == prb->SRB_HaId &&
76 curr->target == prb->SRB_Target &&
77 curr->lun == prb->SRB_Lun) {
78 return curr->fd;
82 /* device wasn't cached, go ahead and open it */
83 sprintf( idstr, "Software\\Wine\\Wine\\Config\\scsi c%1dt%1dd%1d",
84 prb->SRB_HaId, prb->SRB_Target, prb->SRB_Lun);
86 device_str[0] = 0;
87 if (!RegOpenKeyExA( HKEY_LOCAL_MACHINE, idstr, 0, KEY_ALL_ACCESS, &hkey ))
89 DWORD type, count = sizeof(device_str);
90 if (RegQueryValueExA( hkey, "Device", 0, &type, device_str, &count )) device_str[0] = 0;
91 RegCloseKey( hkey );
94 if (!device_str[0])
96 TRACE("Trying to open unlisted scsi device %s\n", idstr);
97 return -1;
100 TRACE("Opening device %s=%s\n", idstr, device_str);
102 fd = open(device_str, O_RDWR);
103 if (fd == -1) {
104 int save_error = errno;
105 ERR("Error opening device %s, error '%s'\n", device_str, strerror(save_error));
106 return -1;
109 /* device is now open */
110 curr = HeapAlloc( GetProcessHeap(), 0, sizeof(ASPI_DEVICE_INFO) );
111 curr->fd = fd;
112 curr->hostId = prb->SRB_HaId;
113 curr->target = prb->SRB_Target;
114 curr->lun = prb->SRB_Lun;
116 /* insert new record at beginning of open device list */
117 curr->next = ASPI_open_devices;
118 ASPI_open_devices = curr;
119 return fd;
123 static void
124 ASPI_DebugPrintCmd(SRB_ExecSCSICmd16 *prb, UINT16 mode)
126 BYTE cmd;
127 int i;
128 BYTE *cdb;
129 BYTE *lpBuf = 0;
131 switch (mode)
133 case ASPI_DOS:
134 /* translate real mode address */
135 if (prb->SRB_BufPointer)
136 lpBuf = PTR_REAL_TO_LIN( SELECTOROF(prb->SRB_BufPointer), OFFSETOF(prb->SRB_BufPointer));
137 break;
138 case ASPI_WIN16:
139 lpBuf = MapSL(prb->SRB_BufPointer);
140 break;
143 switch (prb->CDBByte[0]) {
144 case CMD_INQUIRY:
145 TRACE("{\n");
146 TRACE("\tEVPD: %d\n", prb->CDBByte[1] & 1);
147 TRACE("\tLUN: %d\n", (prb->CDBByte[1] & 0xc) >> 1);
148 TRACE("\tPAGE CODE: %d\n", prb->CDBByte[2]);
149 TRACE("\tALLOCATION LENGTH: %d\n", prb->CDBByte[4]);
150 TRACE("\tCONTROL: %d\n", prb->CDBByte[5]);
151 TRACE("}\n");
152 break;
153 case CMD_SCAN_SCAN:
154 TRACE("Transfer Length: %d\n", prb->CDBByte[4]);
155 break;
158 TRACE("Host Adapter: %d\n", prb->SRB_HaId);
159 TRACE("Flags: %d\n", prb->SRB_Flags);
160 if (TARGET_TO_HOST(prb)) {
161 TRACE("\tData transfer: Target to host. Length checked.\n");
163 else if (HOST_TO_TARGET(prb)) {
164 TRACE("\tData transfer: Host to target. Length checked.\n");
166 else if (NO_DATA_TRANSFERED(prb)) {
167 TRACE("\tData transfer: none\n");
169 else {
170 WARN("\tTransfer by scsi cmd. Length not checked\n");
173 TRACE("\tResidual byte length reporting %s\n", prb->SRB_Flags & 0x4 ? "enabled" : "disabled");
174 TRACE("\tLinking %s\n", prb->SRB_Flags & 0x2 ? "enabled" : "disabled");
175 TRACE("\tPosting %s\n", prb->SRB_Flags & 0x1 ? "enabled" : "disabled");
176 TRACE("Target: %d\n", prb->SRB_Target);
177 TRACE("Lun: %d\n", prb->SRB_Lun);
178 TRACE("BufLen: %ld\n", prb->SRB_BufLen);
179 TRACE("SenseLen: %d\n", prb->SRB_SenseLen);
180 TRACE("BufPtr: %lx (%p)\n", prb->SRB_BufPointer, lpBuf);
181 TRACE("LinkPointer %lx\n", prb->SRB_Rsvd1);
182 TRACE("CDB Length: %d\n", prb->SRB_CDBLen);
183 TRACE("POST Proc: %lx\n", (DWORD) prb->SRB_PostProc);
184 cdb = &prb->CDBByte[0];
185 cmd = prb->CDBByte[0];
186 if (TRACE_ON(aspi))
188 DPRINTF("CDB buffer[");
189 for (i = 0; i < prb->SRB_CDBLen; i++) {
190 if (i != 0) DPRINTF(",");
191 DPRINTF("%02x", *cdb++);
193 DPRINTF("]\n");
197 static void
198 ASPI_PrintSenseArea16(SRB_ExecSCSICmd16 *prb)
200 int i;
201 BYTE *cdb;
203 if (TRACE_ON(aspi))
205 cdb = &prb->CDBByte[0];
206 DPRINTF("SenseArea[");
207 for (i = 0; i < prb->SRB_SenseLen; i++) {
208 if (i) DPRINTF(",");
209 DPRINTF("%02x", *cdb++);
211 DPRINTF("]\n");
215 static void
216 ASPI_DebugPrintResult(SRB_ExecSCSICmd16 *prb, UINT16 mode)
218 BYTE *lpBuf = 0;
220 switch (mode)
222 case ASPI_DOS:
223 /* translate real mode address */
224 if (prb->SRB_BufPointer)
225 lpBuf = PTR_REAL_TO_LIN( SELECTOROF(prb->SRB_BufPointer), OFFSETOF(prb->SRB_BufPointer));
226 break;
227 case ASPI_WIN16:
228 lpBuf = MapSL(prb->SRB_BufPointer);
229 break;
232 switch (prb->CDBByte[0]) {
233 case CMD_INQUIRY:
234 TRACE("Vendor: '%s'\n", lpBuf + INQUIRY_VENDOR);
235 break;
236 case CMD_TEST_UNIT_READY:
237 ASPI_PrintSenseArea16(prb);
238 break;
242 static WORD
243 ASPI_ExecScsiCmd(DWORD ptrPRB, UINT16 mode)
245 SRB_ExecSCSICmd16 *lpPRB = 0;
246 struct sg_header *sg_hd, *sg_reply_hdr;
247 int status;
248 BYTE *lpBuf = 0;
249 int in_len, out_len;
250 int error_code = 0;
251 int fd;
253 switch (mode)
255 case ASPI_DOS:
256 if (ptrPRB)
257 lpPRB = PTR_REAL_TO_LIN( SELECTOROF(ptrPRB), OFFSETOF(ptrPRB));
258 break;
259 case ASPI_WIN16:
260 lpPRB = MapSL(ptrPRB);
261 break;
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;
278 switch (mode)
280 case ASPI_DOS:
281 /* translate real mode address */
282 if (ptrPRB)
283 lpBuf = PTR_REAL_TO_LIN( SELECTOROF(lpPRB->SRB_BufPointer),
284 OFFSETOF(lpPRB->SRB_BufPointer));
285 break;
286 case ASPI_WIN16:
287 lpBuf = MapSL(lpPRB->SRB_BufPointer);
288 break;
291 if (!lpPRB->SRB_CDBLen) {
292 WARN("Failed: lpPRB->SRB_CDBLen = 0.\n");
293 lpPRB->SRB_Status = SS_ERR;
294 return SS_ERR;
297 /* build up sg_header + scsi cmd */
298 if (HOST_TO_TARGET(lpPRB)) {
299 /* send header, command, and then data */
300 in_len = SCSI_OFF + lpPRB->SRB_CDBLen + lpPRB->SRB_BufLen;
301 sg_hd = (struct sg_header *) malloc(in_len);
302 memset(sg_hd, 0, SCSI_OFF);
303 memcpy(sg_hd + 1, &lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
304 if (lpPRB->SRB_BufLen) {
305 memcpy(((BYTE *) sg_hd) + SCSI_OFF + lpPRB->SRB_CDBLen, lpBuf, lpPRB->SRB_BufLen);
308 else {
309 /* send header and command - no data */
310 in_len = SCSI_OFF + lpPRB->SRB_CDBLen;
311 sg_hd = (struct sg_header *) malloc(in_len);
312 memset(sg_hd, 0, SCSI_OFF);
313 memcpy(sg_hd + 1, &lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
316 if (TARGET_TO_HOST(lpPRB)) {
317 out_len = SCSI_OFF + lpPRB->SRB_BufLen;
318 sg_reply_hdr = (struct sg_header *) malloc(out_len);
319 memset(sg_reply_hdr, 0, SCSI_OFF);
320 sg_hd->reply_len = out_len;
322 else {
323 out_len = SCSI_OFF;
324 sg_reply_hdr = (struct sg_header *) malloc(out_len);
325 memset(sg_reply_hdr, 0, SCSI_OFF);
326 sg_hd->reply_len = out_len;
329 status = write(fd, sg_hd, in_len);
330 if (status < 0 || status != in_len) {
331 int save_error = errno;
333 WARN("Not enough bytes written to scsi device bytes=%d .. %d\n", in_len, status);
334 if (status < 0) {
335 if (save_error == ENOMEM) {
336 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");
338 WARN("error:= '%s'\n", strerror(save_error));
340 goto error_exit;
343 status = read(fd, sg_reply_hdr, out_len);
344 if (status < 0 || status != out_len) {
345 WARN("not enough bytes read from scsi device%d\n", status);
346 goto error_exit;
349 if (sg_reply_hdr->result != 0) {
350 error_code = sg_reply_hdr->result;
351 WARN("reply header error (%d)\n", sg_reply_hdr->result);
352 goto error_exit;
355 if (TARGET_TO_HOST(lpPRB) && lpPRB->SRB_BufLen) {
356 memcpy(lpBuf, sg_reply_hdr + 1, lpPRB->SRB_BufLen);
359 /* copy in sense buffer to amount that is available in client */
360 if (lpPRB->SRB_SenseLen) {
361 int sense_len = lpPRB->SRB_SenseLen;
362 if (lpPRB->SRB_SenseLen > 16)
363 sense_len = 16;
364 memcpy(SENSE_BUFFER(lpPRB), &sg_reply_hdr->sense_buffer[0], sense_len);
368 lpPRB->SRB_Status = SS_COMP;
369 lpPRB->SRB_HaStat = HASTAT_OK;
370 lpPRB->SRB_TargStat = STATUS_GOOD;
372 /* now do posting */
374 if (ASPI_POSTING(lpPRB) && lpPRB->SRB_PostProc) {
375 TRACE("Post Routine (%lx) called\n", (DWORD) lpPRB->SRB_PostProc);
376 switch (mode)
378 case ASPI_DOS:
380 SEGPTR spPRB = MapLS(lpPRB);
382 ASPI_CallTo16_long_l(lpPRB->SRB_PostProc, spPRB);
383 UnMapLS(spPRB);
384 break;
386 case ASPI_WIN16:
387 ASPI_CallTo16_long_l(lpPRB->SRB_PostProc, ptrPRB);
388 break;
392 free(sg_reply_hdr);
393 free(sg_hd);
394 ASPI_DebugPrintResult(lpPRB, mode);
395 return SS_COMP;
397 error_exit:
398 if (error_code == EBUSY) {
399 lpPRB->SRB_Status = SS_ASPI_IS_BUSY;
400 TRACE("Device busy\n");
402 else {
403 WARN("Failed\n");
404 lpPRB->SRB_Status = SS_ERR;
407 /* I'm not sure exactly error codes work here
408 * We probably should set lpPRB->SRB_TargStat, SRB_HaStat ?
410 WARN("error_exit\n");
411 free(sg_reply_hdr);
412 free(sg_hd);
413 return lpPRB->SRB_Status;
415 #endif
418 /***********************************************************************
419 * GetASPISupportInfo (WINASPI.1)
422 WORD WINAPI GetASPISupportInfo16(void)
424 #ifdef linux
425 TRACE("GETASPISupportInfo16\n");
426 /* high byte SS_COMP - low byte number of host adapters */
427 return ((SS_COMP << 8) | HA_Count);
428 #else
429 return ((SS_NO_ASPI << 8) | 0);
430 #endif
434 DWORD ASPI_SendASPICommand(DWORD ptrSRB, UINT16 mode)
436 #ifdef linux
437 LPSRB16 lpSRB = 0;
439 switch (mode)
441 case ASPI_DOS:
442 if (ptrSRB)
443 lpSRB = PTR_REAL_TO_LIN( SELECTOROF(ptrSRB), OFFSETOF(ptrSRB));
444 break;
445 case ASPI_WIN16:
446 lpSRB = MapSL(ptrSRB);
447 if (ASPIChainFunc)
449 /* This is not the post proc, it's the chain proc this time */
450 DWORD ret = ASPI_CallTo16_long_l(ASPIChainFunc, ptrSRB);
451 if (ret)
453 lpSRB->inquiry.SRB_Status = SS_INVALID_SRB;
454 return ret;
457 break;
460 switch (lpSRB->common.SRB_Cmd) {
461 case SC_HA_INQUIRY:
462 lpSRB->inquiry.SRB_Status = SS_COMP; /* completed successfully */
463 if (lpSRB->inquiry.SRB_55AASignature == 0x55aa) {
464 TRACE("Extended request detected (Adaptec's ASPIxDOS).\nWe don't support it at the moment.\n");
466 lpSRB->inquiry.SRB_ExtBufferSize = 0x2000; /* bogus value */
467 lpSRB->inquiry.HA_Count = HA_Count;
468 lpSRB->inquiry.HA_SCSI_ID = 7; /* not always ID 7 */
469 strcat(lpSRB->inquiry.HA_ManagerId, "Wine ASPI16"); /* max 15 chars */
470 strcat(lpSRB->inquiry.HA_Identifier, "Wine host"); /* FIXME: return host
471 adapter name */
472 memset(lpSRB->inquiry.HA_Unique, 0, 16); /* default HA_Unique content */
473 lpSRB->inquiry.HA_Unique[6] = 0x02; /* Maximum Transfer Length (128K, Byte> 4-7) */
474 FIXME("ASPI: Partially implemented SC_HA_INQUIRY for adapter %d.\n", lpSRB->inquiry.SRB_HaId);
475 return SS_COMP;
476 case SC_GET_DEV_TYPE:
477 FIXME("Not implemented SC_GET_DEV_TYPE\n");
478 break;
479 case SC_EXEC_SCSI_CMD:
480 return ASPI_ExecScsiCmd((DWORD)ptrSRB, mode);
481 break;
482 case SC_RESET_DEV:
483 FIXME("Not implemented SC_RESET_DEV\n");
484 break;
485 default:
486 FIXME("Unknown command %d\n", lpSRB->common.SRB_Cmd);
488 #endif
489 return SS_INVALID_SRB;
493 /***********************************************************************
494 * SendASPICommand (WINASPI.2)
496 WORD WINAPI SendASPICommand16(SEGPTR segptr_srb)
498 #ifdef linux
499 return ASPI_SendASPICommand(segptr_srb, ASPI_WIN16);
500 #else
501 return 0;
502 #endif
506 /***********************************************************************
507 * InsertInASPIChain (WINASPI.3)
509 WORD WINAPI InsertInASPIChain16(BOOL16 remove, FARPROC16 pASPIChainFunc)
511 #ifdef linux
512 if (remove == TRUE) /* Remove */
514 if (ASPIChainFunc == pASPIChainFunc)
516 ASPIChainFunc = NULL;
517 return SS_COMP;
520 else
521 if (remove == FALSE) /* Insert */
523 if (ASPIChainFunc == NULL)
525 ASPIChainFunc = pASPIChainFunc;
526 return SS_COMP;
529 #endif
530 return SS_ERR;
534 /***********************************************************************
535 * GETASPIDLLVERSION (WINASPI.4)
538 DWORD WINAPI GetASPIDLLVersion16()
540 #ifdef linux
541 return 2;
542 #else
543 return 0;
544 #endif