Replaced PTR_SEG_TO_LIN macro by exported MapSL function.
[wine.git] / dlls / winaspi / winaspi16.c
blobdfad8ea96c7314959f014eaf3e3a0ac52bf13c1d
1 #include "config.h"
3 #include <stdlib.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <memory.h>
9 #include <unistd.h>
10 #include <string.h>
11 #include <stdio.h>
13 #include "winbase.h"
14 #include "wine/windef16.h"
15 #include "winreg.h"
16 #include "aspi.h"
17 #include "winescsi.h"
18 #include "wine/winaspi.h"
19 #include "heap.h"
20 #include "debugtools.h"
21 #include "miscemu.h"
23 DEFAULT_DEBUG_CHANNEL(aspi);
26 /* FIXME!
27 * 1) Residual byte length reporting not handled
28 * 2) Make this code re-entrant for multithreading
29 * 3) Only linux supported so far
32 /* ### start build ### */
33 extern LONG CALLBACK ASPI_CallTo16_long_l(FARPROC16,SEGPTR);
34 /* ### stop build ### */
36 #ifdef linux
38 static ASPI_DEVICE_INFO *ASPI_open_devices = NULL;
40 static FARPROC16 ASPIChainFunc = NULL;
41 static WORD HA_Count = 1; /* host adapter count; FIXME: detect it */
43 static int
44 ASPI_OpenDevice16(SRB_ExecSCSICmd16 *prb)
46 HKEY hkey;
47 int fd;
48 char idstr[50];
49 char device_str[50];
50 ASPI_DEVICE_INFO *curr;
52 /* search list of devices to see if we've opened it already.
53 * There is not an explicit open/close in ASPI land, so hopefully
54 * keeping a device open won't be a problem.
57 for (curr = ASPI_open_devices; curr; curr = curr->next) {
58 if (curr->hostId == prb->SRB_HaId &&
59 curr->target == prb->SRB_Target &&
60 curr->lun == prb->SRB_Lun) {
61 return curr->fd;
65 /* device wasn't cached, go ahead and open it */
66 sprintf( idstr, "Software\\Wine\\Wine\\Config\\scsi c%1dt%1dd%1d",
67 prb->SRB_HaId, prb->SRB_Target, prb->SRB_Lun);
69 device_str[0] = 0;
70 if (!RegOpenKeyExA( HKEY_LOCAL_MACHINE, idstr, 0, KEY_ALL_ACCESS, &hkey ))
72 DWORD type, count = sizeof(device_str);
73 if (RegQueryValueExA( hkey, "Device", 0, &type, device_str, &count )) device_str[0] = 0;
74 RegCloseKey( hkey );
77 if (!device_str[0])
79 TRACE("Trying to open unlisted scsi device %s\n", idstr);
80 return -1;
83 TRACE("Opening device %s=%s\n", idstr, device_str);
85 fd = open(device_str, O_RDWR);
86 if (fd == -1) {
87 int save_error = errno;
88 ERR("Error opening device %s, error '%s'\n", device_str, strerror(save_error));
89 return -1;
92 /* device is now open */
93 curr = HeapAlloc( GetProcessHeap(), 0, sizeof(ASPI_DEVICE_INFO) );
94 curr->fd = fd;
95 curr->hostId = prb->SRB_HaId;
96 curr->target = prb->SRB_Target;
97 curr->lun = prb->SRB_Lun;
99 /* insert new record at beginning of open device list */
100 curr->next = ASPI_open_devices;
101 ASPI_open_devices = curr;
102 return fd;
106 static void
107 ASPI_DebugPrintCmd(SRB_ExecSCSICmd16 *prb, UINT16 mode)
109 BYTE cmd;
110 int i;
111 BYTE *cdb;
112 BYTE *lpBuf = 0;
114 switch (mode)
116 case ASPI_DOS:
117 /* translate real mode address */
118 if (prb->SRB_BufPointer)
119 lpBuf = PTR_REAL_TO_LIN( SELECTOROF(prb->SRB_BufPointer), OFFSETOF(prb->SRB_BufPointer));
120 break;
121 case ASPI_WIN16:
122 lpBuf = MapSL(prb->SRB_BufPointer);
123 break;
126 switch (prb->CDBByte[0]) {
127 case CMD_INQUIRY:
128 TRACE("{\n");
129 TRACE("\tEVPD: %d\n", prb->CDBByte[1] & 1);
130 TRACE("\tLUN: %d\n", (prb->CDBByte[1] & 0xc) >> 1);
131 TRACE("\tPAGE CODE: %d\n", prb->CDBByte[2]);
132 TRACE("\tALLOCATION LENGTH: %d\n", prb->CDBByte[4]);
133 TRACE("\tCONTROL: %d\n", prb->CDBByte[5]);
134 TRACE("}\n");
135 break;
136 case CMD_SCAN_SCAN:
137 TRACE("Transfer Length: %d\n", prb->CDBByte[4]);
138 break;
141 TRACE("Host Adapter: %d\n", prb->SRB_HaId);
142 TRACE("Flags: %d\n", prb->SRB_Flags);
143 if (TARGET_TO_HOST(prb)) {
144 TRACE("\tData transfer: Target to host. Length checked.\n");
146 else if (HOST_TO_TARGET(prb)) {
147 TRACE("\tData transfer: Host to target. Length checked.\n");
149 else if (NO_DATA_TRANSFERED(prb)) {
150 TRACE("\tData transfer: none\n");
152 else {
153 WARN("\tTransfer by scsi cmd. Length not checked\n");
156 TRACE("\tResidual byte length reporting %s\n", prb->SRB_Flags & 0x4 ? "enabled" : "disabled");
157 TRACE("\tLinking %s\n", prb->SRB_Flags & 0x2 ? "enabled" : "disabled");
158 TRACE("\tPosting %s\n", prb->SRB_Flags & 0x1 ? "enabled" : "disabled");
159 TRACE("Target: %d\n", prb->SRB_Target);
160 TRACE("Lun: %d\n", prb->SRB_Lun);
161 TRACE("BufLen: %ld\n", prb->SRB_BufLen);
162 TRACE("SenseLen: %d\n", prb->SRB_SenseLen);
163 TRACE("BufPtr: %lx (%p)\n", prb->SRB_BufPointer, lpBuf);
164 TRACE("LinkPointer %lx\n", prb->SRB_Rsvd1);
165 TRACE("CDB Length: %d\n", prb->SRB_CDBLen);
166 TRACE("POST Proc: %lx\n", (DWORD) prb->SRB_PostProc);
167 cdb = &prb->CDBByte[0];
168 cmd = prb->CDBByte[0];
169 if (TRACE_ON(aspi))
171 DPRINTF("CDB buffer[");
172 for (i = 0; i < prb->SRB_CDBLen; i++) {
173 if (i != 0) DPRINTF(",");
174 DPRINTF("%02x", *cdb++);
176 DPRINTF("]\n");
180 static void
181 ASPI_PrintSenseArea16(SRB_ExecSCSICmd16 *prb)
183 int i;
184 BYTE *cdb;
186 if (TRACE_ON(aspi))
188 cdb = &prb->CDBByte[0];
189 DPRINTF("SenseArea[");
190 for (i = 0; i < prb->SRB_SenseLen; i++) {
191 if (i) DPRINTF(",");
192 DPRINTF("%02x", *cdb++);
194 DPRINTF("]\n");
198 static void
199 ASPI_DebugPrintResult(SRB_ExecSCSICmd16 *prb, UINT16 mode)
201 BYTE *lpBuf = 0;
203 switch (mode)
205 case ASPI_DOS:
206 /* translate real mode address */
207 if (prb->SRB_BufPointer)
208 lpBuf = PTR_REAL_TO_LIN( SELECTOROF(prb->SRB_BufPointer), OFFSETOF(prb->SRB_BufPointer));
209 break;
210 case ASPI_WIN16:
211 lpBuf = MapSL(prb->SRB_BufPointer);
212 break;
215 switch (prb->CDBByte[0]) {
216 case CMD_INQUIRY:
217 TRACE("Vendor: '%s'\n", lpBuf + INQUIRY_VENDOR);
218 break;
219 case CMD_TEST_UNIT_READY:
220 ASPI_PrintSenseArea16(prb);
221 break;
225 static WORD
226 ASPI_ExecScsiCmd(DWORD ptrPRB, UINT16 mode)
228 SRB_ExecSCSICmd16 *lpPRB = 0;
229 struct sg_header *sg_hd, *sg_reply_hdr;
230 int status;
231 BYTE *lpBuf = 0;
232 int in_len, out_len;
233 int error_code = 0;
234 int fd;
236 switch (mode)
238 case ASPI_DOS:
239 if (ptrPRB)
240 lpPRB = PTR_REAL_TO_LIN( SELECTOROF(ptrPRB), OFFSETOF(ptrPRB));
241 break;
242 case ASPI_WIN16:
243 lpPRB = MapSL(ptrPRB);
244 break;
247 ASPI_DebugPrintCmd(lpPRB, mode);
249 fd = ASPI_OpenDevice16(lpPRB);
250 if (fd == -1) {
251 WARN("Failed: could not open device. Device permissions !?\n");
252 lpPRB->SRB_Status = SS_ERR;
253 return SS_ERR;
256 sg_hd = NULL;
257 sg_reply_hdr = NULL;
259 lpPRB->SRB_Status = SS_PENDING;
261 switch (mode)
263 case ASPI_DOS:
264 /* translate real mode address */
265 if (ptrPRB)
266 lpBuf = PTR_REAL_TO_LIN( SELECTOROF(lpPRB->SRB_BufPointer),
267 OFFSETOF(lpPRB->SRB_BufPointer));
268 break;
269 case ASPI_WIN16:
270 lpBuf = MapSL(lpPRB->SRB_BufPointer);
271 break;
274 if (!lpPRB->SRB_CDBLen) {
275 WARN("Failed: lpPRB->SRB_CDBLen = 0.\n");
276 lpPRB->SRB_Status = SS_ERR;
277 return SS_ERR;
280 /* build up sg_header + scsi cmd */
281 if (HOST_TO_TARGET(lpPRB)) {
282 /* send header, command, and then data */
283 in_len = SCSI_OFF + lpPRB->SRB_CDBLen + lpPRB->SRB_BufLen;
284 sg_hd = (struct sg_header *) malloc(in_len);
285 memset(sg_hd, 0, SCSI_OFF);
286 memcpy(sg_hd + 1, &lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
287 if (lpPRB->SRB_BufLen) {
288 memcpy(((BYTE *) sg_hd) + SCSI_OFF + lpPRB->SRB_CDBLen, lpBuf, lpPRB->SRB_BufLen);
291 else {
292 /* send header and command - no data */
293 in_len = SCSI_OFF + lpPRB->SRB_CDBLen;
294 sg_hd = (struct sg_header *) malloc(in_len);
295 memset(sg_hd, 0, SCSI_OFF);
296 memcpy(sg_hd + 1, &lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
299 if (TARGET_TO_HOST(lpPRB)) {
300 out_len = SCSI_OFF + lpPRB->SRB_BufLen;
301 sg_reply_hdr = (struct sg_header *) malloc(out_len);
302 memset(sg_reply_hdr, 0, SCSI_OFF);
303 sg_hd->reply_len = out_len;
305 else {
306 out_len = SCSI_OFF;
307 sg_reply_hdr = (struct sg_header *) malloc(out_len);
308 memset(sg_reply_hdr, 0, SCSI_OFF);
309 sg_hd->reply_len = out_len;
312 status = write(fd, sg_hd, in_len);
313 if (status < 0 || status != in_len) {
314 int save_error = errno;
316 WARN("Not enough bytes written to scsi device bytes=%d .. %d\n", in_len, status);
317 if (status < 0) {
318 if (save_error == ENOMEM) {
319 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");
321 WARN("error:= '%s'\n", strerror(save_error));
323 goto error_exit;
326 status = read(fd, sg_reply_hdr, out_len);
327 if (status < 0 || status != out_len) {
328 WARN("not enough bytes read from scsi device%d\n", status);
329 goto error_exit;
332 if (sg_reply_hdr->result != 0) {
333 error_code = sg_reply_hdr->result;
334 WARN("reply header error (%d)\n", sg_reply_hdr->result);
335 goto error_exit;
338 if (TARGET_TO_HOST(lpPRB) && lpPRB->SRB_BufLen) {
339 memcpy(lpBuf, sg_reply_hdr + 1, lpPRB->SRB_BufLen);
342 /* copy in sense buffer to amount that is available in client */
343 if (lpPRB->SRB_SenseLen) {
344 int sense_len = lpPRB->SRB_SenseLen;
345 if (lpPRB->SRB_SenseLen > 16)
346 sense_len = 16;
347 memcpy(SENSE_BUFFER(lpPRB), &sg_reply_hdr->sense_buffer[0], sense_len);
351 lpPRB->SRB_Status = SS_COMP;
352 lpPRB->SRB_HaStat = HASTAT_OK;
353 lpPRB->SRB_TargStat = STATUS_GOOD;
355 /* now do posting */
357 if (ASPI_POSTING(lpPRB) && lpPRB->SRB_PostProc) {
358 TRACE("Post Routine (%lx) called\n", (DWORD) lpPRB->SRB_PostProc);
359 switch (mode)
361 case ASPI_DOS:
363 SEGPTR spPRB = MapLS(lpPRB);
365 ASPI_CallTo16_long_l(lpPRB->SRB_PostProc, spPRB);
366 UnMapLS(spPRB);
367 break;
369 case ASPI_WIN16:
370 ASPI_CallTo16_long_l(lpPRB->SRB_PostProc, ptrPRB);
371 break;
375 free(sg_reply_hdr);
376 free(sg_hd);
377 ASPI_DebugPrintResult(lpPRB, mode);
378 return SS_COMP;
380 error_exit:
381 if (error_code == EBUSY) {
382 lpPRB->SRB_Status = SS_ASPI_IS_BUSY;
383 TRACE("Device busy\n");
385 else {
386 WARN("Failed\n");
387 lpPRB->SRB_Status = SS_ERR;
390 /* I'm not sure exactly error codes work here
391 * We probably should set lpPRB->SRB_TargStat, SRB_HaStat ?
393 WARN("error_exit\n");
394 free(sg_reply_hdr);
395 free(sg_hd);
396 return lpPRB->SRB_Status;
398 #endif
401 /***********************************************************************
402 * GetASPISupportInfo16 (WINASPI.1)
405 WORD WINAPI GetASPISupportInfo16(void)
407 #ifdef linux
408 TRACE("GETASPISupportInfo16\n");
409 /* high byte SS_COMP - low byte number of host adapters */
410 return ((SS_COMP << 8) | HA_Count);
411 #else
412 return ((SS_NO_ASPI << 8) | 0);
413 #endif
417 DWORD ASPI_SendASPICommand(DWORD ptrSRB, UINT16 mode)
419 #ifdef linux
420 LPSRB16 lpSRB = 0;
422 switch (mode)
424 case ASPI_DOS:
425 if (ptrSRB)
426 lpSRB = PTR_REAL_TO_LIN( SELECTOROF(ptrSRB), OFFSETOF(ptrSRB));
427 break;
428 case ASPI_WIN16:
429 lpSRB = MapSL(ptrSRB);
430 if (ASPIChainFunc)
432 /* This is not the post proc, it's the chain proc this time */
433 DWORD ret = ASPI_CallTo16_long_l(ASPIChainFunc, ptrSRB);
434 if (ret)
436 lpSRB->inquiry.SRB_Status = SS_INVALID_SRB;
437 return ret;
440 break;
443 switch (lpSRB->common.SRB_Cmd) {
444 case SC_HA_INQUIRY:
445 lpSRB->inquiry.SRB_Status = SS_COMP; /* completed successfully */
446 if (lpSRB->inquiry.SRB_55AASignature == 0x55aa) {
447 TRACE("Extended request detected (Adaptec's ASPIxDOS).\nWe don't support it at the moment.\n");
449 lpSRB->inquiry.SRB_ExtBufferSize = 0x2000; /* bogus value */
450 lpSRB->inquiry.HA_Count = HA_Count;
451 lpSRB->inquiry.HA_SCSI_ID = 7; /* not always ID 7 */
452 strcat(lpSRB->inquiry.HA_ManagerId, "Wine ASPI16"); /* max 15 chars */
453 strcat(lpSRB->inquiry.HA_Identifier, "Wine host"); /* FIXME: return host
454 adapter name */
455 memset(lpSRB->inquiry.HA_Unique, 0, 16); /* default HA_Unique content */
456 lpSRB->inquiry.HA_Unique[6] = 0x02; /* Maximum Transfer Length (128K, Byte> 4-7) */
457 FIXME("ASPI: Partially implemented SC_HA_INQUIRY for adapter %d.\n", lpSRB->inquiry.SRB_HaId);
458 return SS_COMP;
459 case SC_GET_DEV_TYPE:
460 FIXME("Not implemented SC_GET_DEV_TYPE\n");
461 break;
462 case SC_EXEC_SCSI_CMD:
463 return ASPI_ExecScsiCmd((DWORD)ptrSRB, mode);
464 break;
465 case SC_RESET_DEV:
466 FIXME("Not implemented SC_RESET_DEV\n");
467 break;
468 default:
469 FIXME("Unknown command %d\n", lpSRB->common.SRB_Cmd);
471 #endif
472 return SS_INVALID_SRB;
476 /***********************************************************************
477 * SendASPICommand16 (WINASPI.2)
479 WORD WINAPI SendASPICommand16(SEGPTR segptr_srb)
481 #ifdef linux
482 return ASPI_SendASPICommand(segptr_srb, ASPI_WIN16);
483 #else
484 return 0;
485 #endif
489 /***********************************************************************
490 * InsertInASPIChain16 (WINASPI.3)
492 WORD WINAPI InsertInASPIChain16(BOOL16 remove, FARPROC16 pASPIChainFunc)
494 #ifdef linux
495 if (remove == TRUE) /* Remove */
497 if (ASPIChainFunc == pASPIChainFunc)
499 ASPIChainFunc = NULL;
500 return SS_COMP;
503 else
504 if (remove == FALSE) /* Insert */
506 if (ASPIChainFunc == NULL)
508 ASPIChainFunc = pASPIChainFunc;
509 return SS_COMP;
512 #endif
513 return SS_ERR;
517 /***********************************************************************
518 * GetASPIDLLVersion16 (WINASPI.4)
521 DWORD WINAPI GetASPIDLLVersion16()
523 #ifdef linux
524 return 2;
525 #else
526 return 0;
527 #endif