Fixed winebuild operation in Darwin.
[wine/multimedia.git] / dlls / winaspi / winaspi16.c
blobd7528eacc628b5108a0edd2771e3ceef157b80a3
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 #ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
29 #include <string.h>
30 #include <stdarg.h>
31 #include <stdio.h>
33 #include "windef.h"
34 #include "winbase.h"
35 #include "wine/windef16.h"
36 #include "winreg.h"
37 #include "wownt32.h"
38 #include "aspi.h"
39 #include "winescsi.h"
40 #include "wine/winaspi.h"
41 #include "wine/debug.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 #define PTR_TO_LIN(ptr,mode) \
55 ((mode) == ASPI_DOS ? ((void*)(((unsigned int)SELECTOROF(ptr) << 4) + OFFSETOF(ptr))) : MapSL(ptr))
57 static ASPI_DEVICE_INFO *ASPI_open_devices = NULL;
59 static FARPROC16 ASPIChainFunc = NULL;
60 static WORD HA_Count = 1; /* host adapter count; FIXME: detect it */
62 static int
63 ASPI_OpenDevice16(SRB_ExecSCSICmd16 *prb)
65 HKEY hkey;
66 int fd;
67 char idstr[50];
68 char device_str[50];
69 ASPI_DEVICE_INFO *curr;
71 /* search list of devices to see if we've opened it already.
72 * There is not an explicit open/close in ASPI land, so hopefully
73 * keeping a device open won't be a problem.
76 for (curr = ASPI_open_devices; curr; curr = curr->next) {
77 if (curr->hostId == prb->SRB_HaId &&
78 curr->target == prb->SRB_Target &&
79 curr->lun == prb->SRB_Lun) {
80 return curr->fd;
84 /* device wasn't cached, go ahead and open it */
85 sprintf( idstr, "Software\\Wine\\Wine\\Config\\scsi c%1dt%1dd%1d",
86 prb->SRB_HaId, prb->SRB_Target, prb->SRB_Lun);
88 device_str[0] = 0;
89 if (!RegOpenKeyExA( HKEY_LOCAL_MACHINE, idstr, 0, KEY_ALL_ACCESS, &hkey ))
91 DWORD type, count = sizeof(device_str);
92 if (RegQueryValueExA( hkey, "Device", 0, &type, device_str, &count )) device_str[0] = 0;
93 RegCloseKey( hkey );
96 if (!device_str[0])
98 TRACE("Trying to open unlisted scsi device %s\n", idstr);
99 return -1;
102 TRACE("Opening device %s=%s\n", idstr, device_str);
104 fd = open(device_str, O_RDWR);
105 if (fd == -1) {
106 int save_error = errno;
107 ERR("Error opening device %s, error '%s'\n", device_str, strerror(save_error));
108 return -1;
111 /* device is now open */
112 curr = HeapAlloc( GetProcessHeap(), 0, sizeof(ASPI_DEVICE_INFO) );
113 curr->fd = fd;
114 curr->hostId = prb->SRB_HaId;
115 curr->target = prb->SRB_Target;
116 curr->lun = prb->SRB_Lun;
118 /* insert new record at beginning of open device list */
119 curr->next = ASPI_open_devices;
120 ASPI_open_devices = curr;
121 return fd;
125 static void
126 ASPI_DebugPrintCmd(SRB_ExecSCSICmd16 *prb, UINT16 mode)
128 BYTE cmd;
129 int i;
130 BYTE *cdb;
131 BYTE *lpBuf = PTR_TO_LIN( prb->SRB_BufPointer, mode );
133 switch (prb->CDBByte[0]) {
134 case CMD_INQUIRY:
135 TRACE("{\n");
136 TRACE("\tEVPD: %d\n", prb->CDBByte[1] & 1);
137 TRACE("\tLUN: %d\n", (prb->CDBByte[1] & 0xc) >> 1);
138 TRACE("\tPAGE CODE: %d\n", prb->CDBByte[2]);
139 TRACE("\tALLOCATION LENGTH: %d\n", prb->CDBByte[4]);
140 TRACE("\tCONTROL: %d\n", prb->CDBByte[5]);
141 TRACE("}\n");
142 break;
143 case CMD_SCAN_SCAN:
144 TRACE("Transfer Length: %d\n", prb->CDBByte[4]);
145 break;
148 TRACE("Host Adapter: %d\n", prb->SRB_HaId);
149 TRACE("Flags: %d\n", prb->SRB_Flags);
150 if (TARGET_TO_HOST(prb)) {
151 TRACE("\tData transfer: Target to host. Length checked.\n");
153 else if (HOST_TO_TARGET(prb)) {
154 TRACE("\tData transfer: Host to target. Length checked.\n");
156 else if (NO_DATA_TRANSFERRED(prb)) {
157 TRACE("\tData transfer: none\n");
159 else {
160 WARN("\tTransfer by scsi cmd. Length not checked\n");
163 TRACE("\tResidual byte length reporting %s\n", prb->SRB_Flags & 0x4 ? "enabled" : "disabled");
164 TRACE("\tLinking %s\n", prb->SRB_Flags & 0x2 ? "enabled" : "disabled");
165 TRACE("\tPosting %s\n", prb->SRB_Flags & 0x1 ? "enabled" : "disabled");
166 TRACE("Target: %d\n", prb->SRB_Target);
167 TRACE("Lun: %d\n", prb->SRB_Lun);
168 TRACE("BufLen: %ld\n", prb->SRB_BufLen);
169 TRACE("SenseLen: %d\n", prb->SRB_SenseLen);
170 TRACE("BufPtr: %lx (%p)\n", prb->SRB_BufPointer, lpBuf);
171 TRACE("LinkPointer %lx\n", prb->SRB_Rsvd1);
172 TRACE("CDB Length: %d\n", prb->SRB_CDBLen);
173 TRACE("POST Proc: %lx\n", (DWORD) prb->SRB_PostProc);
174 cdb = &prb->CDBByte[0];
175 cmd = prb->CDBByte[0];
176 if (TRACE_ON(aspi))
178 TRACE("CDB buffer[");
179 for (i = 0; i < prb->SRB_CDBLen; i++) {
180 if (i != 0) TRACE(",");
181 TRACE("%02x", *cdb++);
183 TRACE("]\n");
187 static void
188 ASPI_PrintSenseArea16(SRB_ExecSCSICmd16 *prb)
190 int i;
191 BYTE *cdb;
193 if (TRACE_ON(aspi))
195 cdb = &prb->CDBByte[0];
196 TRACE("SenseArea[");
197 for (i = 0; i < prb->SRB_SenseLen; i++) {
198 if (i) TRACE(",");
199 TRACE("%02x", *cdb++);
201 TRACE("]\n");
205 static void
206 ASPI_DebugPrintResult(SRB_ExecSCSICmd16 *prb, UINT16 mode)
208 BYTE *lpBuf = PTR_TO_LIN( prb->SRB_BufPointer, mode );
210 switch (prb->CDBByte[0]) {
211 case CMD_INQUIRY:
212 TRACE("Vendor: '%s'\n", lpBuf + INQUIRY_VENDOR);
213 break;
214 case CMD_TEST_UNIT_READY:
215 ASPI_PrintSenseArea16(prb);
216 break;
220 static WORD
221 ASPI_ExecScsiCmd(DWORD ptrPRB, UINT16 mode)
223 SRB_ExecSCSICmd16 *lpPRB = PTR_TO_LIN( ptrPRB, mode );
224 struct sg_header *sg_hd, *sg_reply_hdr;
225 int status;
226 BYTE *lpBuf = 0;
227 int in_len, out_len;
228 int error_code = 0;
229 int fd;
231 ASPI_DebugPrintCmd(lpPRB, mode);
233 fd = ASPI_OpenDevice16(lpPRB);
234 if (fd == -1) {
235 WARN("Failed: could not open device. Device permissions !?\n");
236 lpPRB->SRB_Status = SS_ERR;
237 return SS_ERR;
240 sg_hd = NULL;
241 sg_reply_hdr = NULL;
243 lpPRB->SRB_Status = SS_PENDING;
244 lpBuf = PTR_TO_LIN( lpPRB->SRB_BufPointer, mode );
246 if (!lpPRB->SRB_CDBLen) {
247 WARN("Failed: lpPRB->SRB_CDBLen = 0.\n");
248 lpPRB->SRB_Status = SS_ERR;
249 return SS_ERR;
252 /* build up sg_header + scsi cmd */
253 if (HOST_TO_TARGET(lpPRB)) {
254 /* send header, command, and then data */
255 in_len = SCSI_OFF + lpPRB->SRB_CDBLen + lpPRB->SRB_BufLen;
256 sg_hd = (struct sg_header *) malloc(in_len);
257 memset(sg_hd, 0, SCSI_OFF);
258 memcpy(sg_hd + 1, &lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
259 if (lpPRB->SRB_BufLen) {
260 memcpy(((BYTE *) sg_hd) + SCSI_OFF + lpPRB->SRB_CDBLen, lpBuf, lpPRB->SRB_BufLen);
263 else {
264 /* send header and command - no data */
265 in_len = SCSI_OFF + lpPRB->SRB_CDBLen;
266 sg_hd = (struct sg_header *) malloc(in_len);
267 memset(sg_hd, 0, SCSI_OFF);
268 memcpy(sg_hd + 1, &lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
271 if (TARGET_TO_HOST(lpPRB)) {
272 out_len = SCSI_OFF + lpPRB->SRB_BufLen;
273 sg_reply_hdr = (struct sg_header *) malloc(out_len);
274 memset(sg_reply_hdr, 0, SCSI_OFF);
275 sg_hd->reply_len = out_len;
277 else {
278 out_len = SCSI_OFF;
279 sg_reply_hdr = (struct sg_header *) malloc(out_len);
280 memset(sg_reply_hdr, 0, SCSI_OFF);
281 sg_hd->reply_len = out_len;
284 status = write(fd, sg_hd, in_len);
285 if (status < 0 || status != in_len) {
286 int save_error = errno;
288 WARN("Not enough bytes written to scsi device bytes=%d .. %d\n", in_len, status);
289 if (status < 0) {
290 if (save_error == ENOMEM) {
291 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");
293 WARN("error:= '%s'\n", strerror(save_error));
295 goto error_exit;
298 status = read(fd, sg_reply_hdr, out_len);
299 if (status < 0 || status != out_len) {
300 WARN("not enough bytes read from scsi device%d\n", status);
301 goto error_exit;
304 if (sg_reply_hdr->result != 0) {
305 error_code = sg_reply_hdr->result;
306 WARN("reply header error (%d)\n", sg_reply_hdr->result);
307 goto error_exit;
310 if (TARGET_TO_HOST(lpPRB) && lpPRB->SRB_BufLen) {
311 memcpy(lpBuf, sg_reply_hdr + 1, lpPRB->SRB_BufLen);
314 /* copy in sense buffer to amount that is available in client */
315 if (lpPRB->SRB_SenseLen) {
316 int sense_len = lpPRB->SRB_SenseLen;
317 if (lpPRB->SRB_SenseLen > 16)
318 sense_len = 16;
319 memcpy(SENSE_BUFFER(lpPRB), &sg_reply_hdr->sense_buffer[0], sense_len);
323 lpPRB->SRB_Status = SS_COMP;
324 lpPRB->SRB_HaStat = HASTAT_OK;
325 lpPRB->SRB_TargStat = STATUS_GOOD;
327 /* now do posting */
329 if (ASPI_POSTING(lpPRB) && lpPRB->SRB_PostProc) {
330 TRACE("Post Routine (%lx) called\n", (DWORD) lpPRB->SRB_PostProc);
331 switch (mode)
333 case ASPI_DOS:
335 SEGPTR spPRB = MapLS(lpPRB);
337 WOWCallback16((DWORD)lpPRB->SRB_PostProc, spPRB);
338 UnMapLS(spPRB);
339 break;
341 case ASPI_WIN16:
342 WOWCallback16((DWORD)lpPRB->SRB_PostProc, ptrPRB);
343 break;
347 free(sg_reply_hdr);
348 free(sg_hd);
349 ASPI_DebugPrintResult(lpPRB, mode);
350 return SS_COMP;
352 error_exit:
353 if (error_code == EBUSY) {
354 lpPRB->SRB_Status = SS_ASPI_IS_BUSY;
355 TRACE("Device busy\n");
357 else {
358 WARN("Failed\n");
359 lpPRB->SRB_Status = SS_ERR;
362 /* I'm not sure exactly error codes work here
363 * We probably should set lpPRB->SRB_TargStat, SRB_HaStat ?
365 WARN("error_exit\n");
366 free(sg_reply_hdr);
367 free(sg_hd);
368 return lpPRB->SRB_Status;
370 #endif
373 /***********************************************************************
374 * GetASPISupportInfo (WINASPI.1)
377 WORD WINAPI GetASPISupportInfo16(void)
379 #ifdef linux
380 TRACE("GETASPISupportInfo16\n");
381 /* high byte SS_COMP - low byte number of host adapters */
382 return ((SS_COMP << 8) | HA_Count);
383 #else
384 return ((SS_NO_ASPI << 8) | 0);
385 #endif
389 DWORD ASPI_SendASPICommand(DWORD ptrSRB, UINT16 mode)
391 #ifdef linux
392 LPSRB16 lpSRB = PTR_TO_LIN( ptrSRB, mode );
394 if (mode == ASPI_WIN16 && ASPIChainFunc)
396 /* This is not the post proc, it's the chain proc this time */
397 DWORD ret = WOWCallback16((DWORD)ASPIChainFunc, ptrSRB);
398 if (ret)
400 lpSRB->inquiry.SRB_Status = SS_INVALID_SRB;
401 return ret;
405 switch (lpSRB->common.SRB_Cmd) {
406 case SC_HA_INQUIRY:
407 lpSRB->inquiry.SRB_Status = SS_COMP; /* completed successfully */
408 if (lpSRB->inquiry.SRB_55AASignature == 0x55aa) {
409 TRACE("Extended request detected (Adaptec's ASPIxDOS).\nWe don't support it at the moment.\n");
411 lpSRB->inquiry.SRB_ExtBufferSize = 0x2000; /* bogus value */
412 lpSRB->inquiry.HA_Count = HA_Count;
413 lpSRB->inquiry.HA_SCSI_ID = 7; /* not always ID 7 */
414 strcat(lpSRB->inquiry.HA_ManagerId, "Wine ASPI16"); /* max 15 chars */
415 strcat(lpSRB->inquiry.HA_Identifier, "Wine host"); /* FIXME: return host
416 adapter name */
417 memset(lpSRB->inquiry.HA_Unique, 0, 16); /* default HA_Unique content */
418 lpSRB->inquiry.HA_Unique[6] = 0x02; /* Maximum Transfer Length (128K, Byte> 4-7) */
419 FIXME("ASPI: Partially implemented SC_HA_INQUIRY for adapter %d.\n", lpSRB->inquiry.SRB_HaId);
420 return SS_COMP;
421 case SC_GET_DEV_TYPE:
422 FIXME("Not implemented SC_GET_DEV_TYPE\n");
423 break;
424 case SC_EXEC_SCSI_CMD:
425 return ASPI_ExecScsiCmd((DWORD)ptrSRB, mode);
426 case SC_RESET_DEV:
427 FIXME("Not implemented SC_RESET_DEV\n");
428 break;
429 default:
430 FIXME("Unknown command %d\n", lpSRB->common.SRB_Cmd);
432 #endif
433 return SS_INVALID_SRB;
437 /***********************************************************************
438 * SendASPICommand (WINASPI.2)
440 WORD WINAPI SendASPICommand16(SEGPTR segptr_srb)
442 #ifdef linux
443 return ASPI_SendASPICommand(segptr_srb, ASPI_WIN16);
444 #else
445 return 0;
446 #endif
450 /***********************************************************************
451 * InsertInASPIChain (WINASPI.3)
453 WORD WINAPI InsertInASPIChain16(BOOL16 remove, FARPROC16 pASPIChainFunc)
455 #ifdef linux
456 if (remove) /* Remove */
458 if (ASPIChainFunc == pASPIChainFunc)
460 ASPIChainFunc = NULL;
461 return SS_COMP;
464 else /* Insert */
466 if (ASPIChainFunc == NULL)
468 ASPIChainFunc = pASPIChainFunc;
469 return SS_COMP;
472 #endif
473 return SS_ERR;
477 /***********************************************************************
478 * GETASPIDLLVERSION (WINASPI.4)
481 DWORD WINAPI GetASPIDLLVersion16(void)
483 #ifdef linux
484 return 2;
485 #else
486 return 0;
487 #endif