_open_osfhandle is expected to take the absence of either _O_TEXT or
[wine/gsoc_dplay.git] / dlls / winaspi / winaspi16.c
blob200748db82e5e0a169bb848f8ff465132b9d5720
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 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #include <string.h>
31 #include <stdio.h>
33 #include "winbase.h"
34 #include "wine/windef16.h"
35 #include "winreg.h"
36 #include "wownt32.h"
37 #include "aspi.h"
38 #include "winescsi.h"
39 #include "wine/winaspi.h"
40 #include "wine/debug.h"
41 #include "miscemu.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(aspi);
46 /* FIXME!
47 * 1) Residual byte length reporting not handled
48 * 2) Make this code re-entrant for multithreading
49 * 3) Only linux supported so far
52 #ifdef linux
54 static ASPI_DEVICE_INFO *ASPI_open_devices = NULL;
56 static FARPROC16 ASPIChainFunc = NULL;
57 static WORD HA_Count = 1; /* host adapter count; FIXME: detect it */
59 static int
60 ASPI_OpenDevice16(SRB_ExecSCSICmd16 *prb)
62 HKEY hkey;
63 int fd;
64 char idstr[50];
65 char device_str[50];
66 ASPI_DEVICE_INFO *curr;
68 /* search list of devices to see if we've opened it already.
69 * There is not an explicit open/close in ASPI land, so hopefully
70 * keeping a device open won't be a problem.
73 for (curr = ASPI_open_devices; curr; curr = curr->next) {
74 if (curr->hostId == prb->SRB_HaId &&
75 curr->target == prb->SRB_Target &&
76 curr->lun == prb->SRB_Lun) {
77 return curr->fd;
81 /* device wasn't cached, go ahead and open it */
82 sprintf( idstr, "Software\\Wine\\Wine\\Config\\scsi c%1dt%1dd%1d",
83 prb->SRB_HaId, prb->SRB_Target, prb->SRB_Lun);
85 device_str[0] = 0;
86 if (!RegOpenKeyExA( HKEY_LOCAL_MACHINE, idstr, 0, KEY_ALL_ACCESS, &hkey ))
88 DWORD type, count = sizeof(device_str);
89 if (RegQueryValueExA( hkey, "Device", 0, &type, device_str, &count )) device_str[0] = 0;
90 RegCloseKey( hkey );
93 if (!device_str[0])
95 TRACE("Trying to open unlisted scsi device %s\n", idstr);
96 return -1;
99 TRACE("Opening device %s=%s\n", idstr, device_str);
101 fd = open(device_str, O_RDWR);
102 if (fd == -1) {
103 int save_error = errno;
104 ERR("Error opening device %s, error '%s'\n", device_str, strerror(save_error));
105 return -1;
108 /* device is now open */
109 curr = HeapAlloc( GetProcessHeap(), 0, sizeof(ASPI_DEVICE_INFO) );
110 curr->fd = fd;
111 curr->hostId = prb->SRB_HaId;
112 curr->target = prb->SRB_Target;
113 curr->lun = prb->SRB_Lun;
115 /* insert new record at beginning of open device list */
116 curr->next = ASPI_open_devices;
117 ASPI_open_devices = curr;
118 return fd;
122 static void
123 ASPI_DebugPrintCmd(SRB_ExecSCSICmd16 *prb, UINT16 mode)
125 BYTE cmd;
126 int i;
127 BYTE *cdb;
128 BYTE *lpBuf = 0;
130 switch (mode)
132 case ASPI_DOS:
133 /* translate real mode address */
134 if (prb->SRB_BufPointer)
135 lpBuf = PTR_REAL_TO_LIN( SELECTOROF(prb->SRB_BufPointer), OFFSETOF(prb->SRB_BufPointer));
136 break;
137 case ASPI_WIN16:
138 lpBuf = MapSL(prb->SRB_BufPointer);
139 break;
142 switch (prb->CDBByte[0]) {
143 case CMD_INQUIRY:
144 TRACE("{\n");
145 TRACE("\tEVPD: %d\n", prb->CDBByte[1] & 1);
146 TRACE("\tLUN: %d\n", (prb->CDBByte[1] & 0xc) >> 1);
147 TRACE("\tPAGE CODE: %d\n", prb->CDBByte[2]);
148 TRACE("\tALLOCATION LENGTH: %d\n", prb->CDBByte[4]);
149 TRACE("\tCONTROL: %d\n", prb->CDBByte[5]);
150 TRACE("}\n");
151 break;
152 case CMD_SCAN_SCAN:
153 TRACE("Transfer Length: %d\n", prb->CDBByte[4]);
154 break;
157 TRACE("Host Adapter: %d\n", prb->SRB_HaId);
158 TRACE("Flags: %d\n", prb->SRB_Flags);
159 if (TARGET_TO_HOST(prb)) {
160 TRACE("\tData transfer: Target to host. Length checked.\n");
162 else if (HOST_TO_TARGET(prb)) {
163 TRACE("\tData transfer: Host to target. Length checked.\n");
165 else if (NO_DATA_TRANSFERED(prb)) {
166 TRACE("\tData transfer: none\n");
168 else {
169 WARN("\tTransfer by scsi cmd. Length not checked\n");
172 TRACE("\tResidual byte length reporting %s\n", prb->SRB_Flags & 0x4 ? "enabled" : "disabled");
173 TRACE("\tLinking %s\n", prb->SRB_Flags & 0x2 ? "enabled" : "disabled");
174 TRACE("\tPosting %s\n", prb->SRB_Flags & 0x1 ? "enabled" : "disabled");
175 TRACE("Target: %d\n", prb->SRB_Target);
176 TRACE("Lun: %d\n", prb->SRB_Lun);
177 TRACE("BufLen: %ld\n", prb->SRB_BufLen);
178 TRACE("SenseLen: %d\n", prb->SRB_SenseLen);
179 TRACE("BufPtr: %lx (%p)\n", prb->SRB_BufPointer, lpBuf);
180 TRACE("LinkPointer %lx\n", prb->SRB_Rsvd1);
181 TRACE("CDB Length: %d\n", prb->SRB_CDBLen);
182 TRACE("POST Proc: %lx\n", (DWORD) prb->SRB_PostProc);
183 cdb = &prb->CDBByte[0];
184 cmd = prb->CDBByte[0];
185 if (TRACE_ON(aspi))
187 TRACE("CDB buffer[");
188 for (i = 0; i < prb->SRB_CDBLen; i++) {
189 if (i != 0) TRACE(",");
190 TRACE("%02x", *cdb++);
192 TRACE("]\n");
196 static void
197 ASPI_PrintSenseArea16(SRB_ExecSCSICmd16 *prb)
199 int i;
200 BYTE *cdb;
202 if (TRACE_ON(aspi))
204 cdb = &prb->CDBByte[0];
205 TRACE("SenseArea[");
206 for (i = 0; i < prb->SRB_SenseLen; i++) {
207 if (i) TRACE(",");
208 TRACE("%02x", *cdb++);
210 TRACE("]\n");
214 static void
215 ASPI_DebugPrintResult(SRB_ExecSCSICmd16 *prb, UINT16 mode)
217 BYTE *lpBuf = 0;
219 switch (mode)
221 case ASPI_DOS:
222 /* translate real mode address */
223 if (prb->SRB_BufPointer)
224 lpBuf = PTR_REAL_TO_LIN( SELECTOROF(prb->SRB_BufPointer), OFFSETOF(prb->SRB_BufPointer));
225 break;
226 case ASPI_WIN16:
227 lpBuf = MapSL(prb->SRB_BufPointer);
228 break;
231 switch (prb->CDBByte[0]) {
232 case CMD_INQUIRY:
233 TRACE("Vendor: '%s'\n", lpBuf + INQUIRY_VENDOR);
234 break;
235 case CMD_TEST_UNIT_READY:
236 ASPI_PrintSenseArea16(prb);
237 break;
241 static WORD
242 ASPI_ExecScsiCmd(DWORD ptrPRB, UINT16 mode)
244 SRB_ExecSCSICmd16 *lpPRB = 0;
245 struct sg_header *sg_hd, *sg_reply_hdr;
246 int status;
247 BYTE *lpBuf = 0;
248 int in_len, out_len;
249 int error_code = 0;
250 int fd;
252 switch (mode)
254 case ASPI_DOS:
255 if (ptrPRB)
256 lpPRB = PTR_REAL_TO_LIN( SELECTOROF(ptrPRB), OFFSETOF(ptrPRB));
257 break;
258 case ASPI_WIN16:
259 lpPRB = MapSL(ptrPRB);
260 break;
263 ASPI_DebugPrintCmd(lpPRB, mode);
265 fd = ASPI_OpenDevice16(lpPRB);
266 if (fd == -1) {
267 WARN("Failed: could not open device. Device permissions !?\n");
268 lpPRB->SRB_Status = SS_ERR;
269 return SS_ERR;
272 sg_hd = NULL;
273 sg_reply_hdr = NULL;
275 lpPRB->SRB_Status = SS_PENDING;
277 switch (mode)
279 case ASPI_DOS:
280 /* translate real mode address */
281 if (ptrPRB)
282 lpBuf = PTR_REAL_TO_LIN( SELECTOROF(lpPRB->SRB_BufPointer),
283 OFFSETOF(lpPRB->SRB_BufPointer));
284 break;
285 case ASPI_WIN16:
286 lpBuf = MapSL(lpPRB->SRB_BufPointer);
287 break;
290 if (!lpPRB->SRB_CDBLen) {
291 WARN("Failed: lpPRB->SRB_CDBLen = 0.\n");
292 lpPRB->SRB_Status = SS_ERR;
293 return SS_ERR;
296 /* build up sg_header + scsi cmd */
297 if (HOST_TO_TARGET(lpPRB)) {
298 /* send header, command, and then data */
299 in_len = SCSI_OFF + lpPRB->SRB_CDBLen + lpPRB->SRB_BufLen;
300 sg_hd = (struct sg_header *) malloc(in_len);
301 memset(sg_hd, 0, SCSI_OFF);
302 memcpy(sg_hd + 1, &lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
303 if (lpPRB->SRB_BufLen) {
304 memcpy(((BYTE *) sg_hd) + SCSI_OFF + lpPRB->SRB_CDBLen, lpBuf, lpPRB->SRB_BufLen);
307 else {
308 /* send header and command - no data */
309 in_len = SCSI_OFF + lpPRB->SRB_CDBLen;
310 sg_hd = (struct sg_header *) malloc(in_len);
311 memset(sg_hd, 0, SCSI_OFF);
312 memcpy(sg_hd + 1, &lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
315 if (TARGET_TO_HOST(lpPRB)) {
316 out_len = SCSI_OFF + lpPRB->SRB_BufLen;
317 sg_reply_hdr = (struct sg_header *) malloc(out_len);
318 memset(sg_reply_hdr, 0, SCSI_OFF);
319 sg_hd->reply_len = out_len;
321 else {
322 out_len = SCSI_OFF;
323 sg_reply_hdr = (struct sg_header *) malloc(out_len);
324 memset(sg_reply_hdr, 0, SCSI_OFF);
325 sg_hd->reply_len = out_len;
328 status = write(fd, sg_hd, in_len);
329 if (status < 0 || status != in_len) {
330 int save_error = errno;
332 WARN("Not enough bytes written to scsi device bytes=%d .. %d\n", in_len, status);
333 if (status < 0) {
334 if (save_error == ENOMEM) {
335 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");
337 WARN("error:= '%s'\n", strerror(save_error));
339 goto error_exit;
342 status = read(fd, sg_reply_hdr, out_len);
343 if (status < 0 || status != out_len) {
344 WARN("not enough bytes read from scsi device%d\n", status);
345 goto error_exit;
348 if (sg_reply_hdr->result != 0) {
349 error_code = sg_reply_hdr->result;
350 WARN("reply header error (%d)\n", sg_reply_hdr->result);
351 goto error_exit;
354 if (TARGET_TO_HOST(lpPRB) && lpPRB->SRB_BufLen) {
355 memcpy(lpBuf, sg_reply_hdr + 1, lpPRB->SRB_BufLen);
358 /* copy in sense buffer to amount that is available in client */
359 if (lpPRB->SRB_SenseLen) {
360 int sense_len = lpPRB->SRB_SenseLen;
361 if (lpPRB->SRB_SenseLen > 16)
362 sense_len = 16;
363 memcpy(SENSE_BUFFER(lpPRB), &sg_reply_hdr->sense_buffer[0], sense_len);
367 lpPRB->SRB_Status = SS_COMP;
368 lpPRB->SRB_HaStat = HASTAT_OK;
369 lpPRB->SRB_TargStat = STATUS_GOOD;
371 /* now do posting */
373 if (ASPI_POSTING(lpPRB) && lpPRB->SRB_PostProc) {
374 TRACE("Post Routine (%lx) called\n", (DWORD) lpPRB->SRB_PostProc);
375 switch (mode)
377 case ASPI_DOS:
379 SEGPTR spPRB = MapLS(lpPRB);
381 WOWCallback16((DWORD)lpPRB->SRB_PostProc, spPRB);
382 UnMapLS(spPRB);
383 break;
385 case ASPI_WIN16:
386 WOWCallback16((DWORD)lpPRB->SRB_PostProc, ptrPRB);
387 break;
391 free(sg_reply_hdr);
392 free(sg_hd);
393 ASPI_DebugPrintResult(lpPRB, mode);
394 return SS_COMP;
396 error_exit:
397 if (error_code == EBUSY) {
398 lpPRB->SRB_Status = SS_ASPI_IS_BUSY;
399 TRACE("Device busy\n");
401 else {
402 WARN("Failed\n");
403 lpPRB->SRB_Status = SS_ERR;
406 /* I'm not sure exactly error codes work here
407 * We probably should set lpPRB->SRB_TargStat, SRB_HaStat ?
409 WARN("error_exit\n");
410 free(sg_reply_hdr);
411 free(sg_hd);
412 return lpPRB->SRB_Status;
414 #endif
417 /***********************************************************************
418 * GetASPISupportInfo (WINASPI.1)
421 WORD WINAPI GetASPISupportInfo16(void)
423 #ifdef linux
424 TRACE("GETASPISupportInfo16\n");
425 /* high byte SS_COMP - low byte number of host adapters */
426 return ((SS_COMP << 8) | HA_Count);
427 #else
428 return ((SS_NO_ASPI << 8) | 0);
429 #endif
433 DWORD ASPI_SendASPICommand(DWORD ptrSRB, UINT16 mode)
435 #ifdef linux
436 LPSRB16 lpSRB = 0;
438 switch (mode)
440 case ASPI_DOS:
441 if (ptrSRB)
442 lpSRB = PTR_REAL_TO_LIN( SELECTOROF(ptrSRB), OFFSETOF(ptrSRB));
443 break;
444 case ASPI_WIN16:
445 lpSRB = MapSL(ptrSRB);
446 if (ASPIChainFunc)
448 /* This is not the post proc, it's the chain proc this time */
449 DWORD ret = WOWCallback16((DWORD)ASPIChainFunc, ptrSRB);
450 if (ret)
452 lpSRB->inquiry.SRB_Status = SS_INVALID_SRB;
453 return ret;
456 break;
459 switch (lpSRB->common.SRB_Cmd) {
460 case SC_HA_INQUIRY:
461 lpSRB->inquiry.SRB_Status = SS_COMP; /* completed successfully */
462 if (lpSRB->inquiry.SRB_55AASignature == 0x55aa) {
463 TRACE("Extended request detected (Adaptec's ASPIxDOS).\nWe don't support it at the moment.\n");
465 lpSRB->inquiry.SRB_ExtBufferSize = 0x2000; /* bogus value */
466 lpSRB->inquiry.HA_Count = HA_Count;
467 lpSRB->inquiry.HA_SCSI_ID = 7; /* not always ID 7 */
468 strcat(lpSRB->inquiry.HA_ManagerId, "Wine ASPI16"); /* max 15 chars */
469 strcat(lpSRB->inquiry.HA_Identifier, "Wine host"); /* FIXME: return host
470 adapter name */
471 memset(lpSRB->inquiry.HA_Unique, 0, 16); /* default HA_Unique content */
472 lpSRB->inquiry.HA_Unique[6] = 0x02; /* Maximum Transfer Length (128K, Byte> 4-7) */
473 FIXME("ASPI: Partially implemented SC_HA_INQUIRY for adapter %d.\n", lpSRB->inquiry.SRB_HaId);
474 return SS_COMP;
475 case SC_GET_DEV_TYPE:
476 FIXME("Not implemented SC_GET_DEV_TYPE\n");
477 break;
478 case SC_EXEC_SCSI_CMD:
479 return ASPI_ExecScsiCmd((DWORD)ptrSRB, mode);
480 break;
481 case SC_RESET_DEV:
482 FIXME("Not implemented SC_RESET_DEV\n");
483 break;
484 default:
485 FIXME("Unknown command %d\n", lpSRB->common.SRB_Cmd);
487 #endif
488 return SS_INVALID_SRB;
492 /***********************************************************************
493 * SendASPICommand (WINASPI.2)
495 WORD WINAPI SendASPICommand16(SEGPTR segptr_srb)
497 #ifdef linux
498 return ASPI_SendASPICommand(segptr_srb, ASPI_WIN16);
499 #else
500 return 0;
501 #endif
505 /***********************************************************************
506 * InsertInASPIChain (WINASPI.3)
508 WORD WINAPI InsertInASPIChain16(BOOL16 remove, FARPROC16 pASPIChainFunc)
510 #ifdef linux
511 if (remove == TRUE) /* Remove */
513 if (ASPIChainFunc == pASPIChainFunc)
515 ASPIChainFunc = NULL;
516 return SS_COMP;
519 else
520 if (remove == FALSE) /* Insert */
522 if (ASPIChainFunc == NULL)
524 ASPIChainFunc = pASPIChainFunc;
525 return SS_COMP;
528 #endif
529 return SS_ERR;
533 /***********************************************************************
534 * GETASPIDLLVERSION (WINASPI.4)
537 DWORD WINAPI GetASPIDLLVersion16()
539 #ifdef linux
540 return 2;
541 #else
542 return 0;
543 #endif