- Added remaining DirectX 3D3 6&7 3D interfaces.
[wine.git] / dlls / winaspi / winaspi32.c
blobb63a70e69521a7970df35347350240eaa747ee09
1 #include "config.h"
3 #include <assert.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <memory.h>
11 #include <unistd.h>
13 #include "winbase.h"
14 #include "aspi.h"
15 #include "wnaspi32.h"
16 #include "winescsi.h"
17 #include "options.h"
18 #include "heap.h"
19 #include "debugtools.h"
20 #include "ldt.h"
21 #include "callback.h"
23 DEFAULT_DEBUG_CHANNEL(aspi);
25 /* FIXME!
26 * 1) Residual byte length reporting not handled
27 * 2) Make this code re-entrant for multithreading
28 * -- Added CriticalSection to OpenDevices function
29 * 3) Only linux supported so far
30 * 4) Leaves sg devices open. This may or may not be okay. A better solution
31 * would be to close the file descriptors when the thread/process using
32 * them no longer needs them.
35 #ifdef linux
37 static ASPI_DEVICE_INFO *ASPI_open_devices = NULL;
38 static CRITICAL_SECTION ASPI_CritSection;
40 #endif /* defined(linux) */
43 BOOL WINAPI WNASPI32_LibMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID fImpLoad)
45 #ifdef linux
46 static BOOL bInitDone=FALSE;
47 #if 0
48 TRACE("0x%x 0x%1x %p\n", hInstDLL, fdwReason, fImpLoad);
49 #endif
50 switch( fdwReason )
52 case DLL_PROCESS_ATTACH:
53 /* Create instance data */
54 if(!bInitDone)
56 bInitDone=TRUE;
57 /* Initialize global stuff just once */
58 InitializeCriticalSection(&ASPI_CritSection);
59 SCSI_Init();
61 break;
62 case DLL_PROCESS_DETACH:
63 /* Destroy instance data */
64 break;
65 case DLL_THREAD_ATTACH:
66 case DLL_THREAD_DETACH:
67 break;
69 return TRUE;
70 #else /* defined(linux) */
71 return TRUE;
72 #endif /* defined(linux) */
76 #ifdef linux
78 static int
79 ASPI_OpenDevice(SRB_ExecSCSICmd *prb)
81 int fd;
82 DWORD hc;
83 ASPI_DEVICE_INFO *curr;
85 /* search list of devices to see if we've opened it already.
86 * There is not an explicit open/close in ASPI land, so hopefully
87 * keeping a device open won't be a problem.
90 EnterCriticalSection(&ASPI_CritSection);
91 for (curr = ASPI_open_devices; curr; curr = curr->next) {
92 if (curr->hostId == prb->SRB_HaId &&
93 curr->target == prb->SRB_Target &&
94 curr->lun == prb->SRB_Lun) {
95 LeaveCriticalSection(&ASPI_CritSection);
96 return curr->fd;
99 LeaveCriticalSection(&ASPI_CritSection);
101 hc = ASPI_GetHCforController( prb->SRB_HaId );
102 fd = SCSI_OpenDevice( HIWORD(hc), LOWORD(hc), prb->SRB_Target, prb->SRB_Lun);
104 if (fd == -1) {
105 return -1;
108 /* device is now open */
109 /* FIXME: Let users specify SCSI timeout in registry */
110 SCSI_LinuxSetTimeout( fd, SCSI_DEFAULT_TIMEOUT );
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 EnterCriticalSection(&ASPI_CritSection);
120 curr->next = ASPI_open_devices;
121 ASPI_open_devices = curr;
122 LeaveCriticalSection(&ASPI_CritSection);
123 return fd;
127 static void
128 ASPI_DebugPrintCmd(SRB_ExecSCSICmd *prb)
130 BYTE cmd;
131 int i;
132 BYTE *cdb;
134 switch (prb->CDBByte[0]) {
135 case CMD_INQUIRY:
136 TRACE("INQUIRY {\n");
137 TRACE("\tEVPD: %d\n", prb->CDBByte[1] & 1);
138 TRACE("\tLUN: %d\n", (prb->CDBByte[1] & 0xc) >> 1);
139 TRACE("\tPAGE CODE: %d\n", prb->CDBByte[2]);
140 TRACE("\tALLOCATION LENGTH: %d\n", prb->CDBByte[4]);
141 TRACE("\tCONTROL: %d\n", prb->CDBByte[5]);
142 TRACE("}\n");
143 break;
144 case CMD_SCAN_SCAN:
145 TRACE("Transfer Length: %d\n", prb->CDBByte[4]);
146 break;
149 TRACE("Host Adapter: %d\n", prb->SRB_HaId);
150 TRACE("Flags: %d\n", prb->SRB_Flags);
151 if (TARGET_TO_HOST(prb)) {
152 TRACE("\tData transfer: Target to host. Length checked.\n");
154 else if (HOST_TO_TARGET(prb)) {
155 TRACE("\tData transfer: Host to target. Length checked.\n");
157 else if (NO_DATA_TRANSFERED(prb)) {
158 TRACE("\tData transfer: none\n");
160 else {
161 WARN("\tTransfer by scsi cmd. Length not checked.\n");
164 TRACE("\tResidual byte length reporting %s\n", prb->SRB_Flags & 0x4 ? "enabled" : "disabled");
165 TRACE("\tLinking %s\n", prb->SRB_Flags & 0x2 ? "enabled" : "disabled");
166 TRACE("\tPosting %s\n", prb->SRB_Flags & 0x1 ? "enabled" : "disabled");
167 TRACE("Target: %d\n", prb->SRB_Target);
168 TRACE("Lun: %d\n", prb->SRB_Lun);
169 TRACE("BufLen: %ld\n", prb->SRB_BufLen);
170 TRACE("SenseLen: %d\n", prb->SRB_SenseLen);
171 TRACE("BufPtr: %p\n", prb->SRB_BufPointer);
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)) {
177 DPRINTF("CDB buffer[");
178 for (i = 0; i < prb->SRB_CDBLen; i++) {
179 if (i != 0) DPRINTF(",");
180 DPRINTF("%02x", *cdb++);
182 DPRINTF("]\n");
186 static void
187 ASPI_PrintSenseArea(SRB_ExecSCSICmd *prb)
189 int i;
190 BYTE *rqbuf = &prb->CDBByte[16];
192 if (TRACE_ON(aspi))
194 DPRINTF("Request Sense reports:\n");
195 if ((rqbuf[0]&0x7f)!=0x70) {
196 DPRINTF("\tInvalid sense header.\n");
197 return;
199 DPRINTF("\tCurrent command read filemark: %s\n",(rqbuf[2]&0x80)?"yes":"no");
200 DPRINTF("\tEarly warning passed: %s\n",(rqbuf[2]&0x40)?"yes":"no");
201 DPRINTF("\tIncorrect blocklengt: %s\n",(rqbuf[2]&0x20)?"yes":"no");
202 DPRINTF("\tSense Key: %d\n",rqbuf[2]&0xf);
203 if (rqbuf[0]&0x80)
204 DPRINTF("\tResidual Length: %d\n",rqbuf[3]*0x1000000+rqbuf[4]*0x10000+rqbuf[5]*0x100+rqbuf[6]);
205 DPRINTF("\tAdditional Sense Length: %d\n",rqbuf[7]);
206 DPRINTF("\tAdditional Sense Code: %d\n",rqbuf[12]);
207 DPRINTF("\tAdditional Sense Code Qualifier: %d\n",rqbuf[13]);
208 if (rqbuf[15]&0x80) {
209 DPRINTF("\tIllegal Param is in %s\n",(rqbuf[15]&0x40)?"the CDB":"the Data Out Phase");
210 if (rqbuf[15]&0x8) {
211 DPRINTF("Pointer at %d, bit %d\n",rqbuf[16]*256+rqbuf[17],rqbuf[15]&0x7);
214 DPRINTF("SenseArea[");
215 for (i = 0; i < prb->SRB_SenseLen; i++) {
216 if (i) DPRINTF(",");
217 DPRINTF("%02x", *rqbuf++);
219 DPRINTF("]\n");
223 static void
224 ASPI_DebugPrintResult(SRB_ExecSCSICmd *prb)
227 TRACE("SRB_Status: %x\n", prb->SRB_Status);
228 TRACE("SRB_HaStat: %x\n", prb->SRB_HaStat);
229 TRACE("SRB_TargStat: %x\n", prb->SRB_TargStat);
230 switch (prb->CDBByte[0]) {
231 case CMD_INQUIRY:
232 TRACE("Vendor: '%s'\n", prb->SRB_BufPointer + INQUIRY_VENDOR);
233 break;
234 case CMD_TEST_UNIT_READY:
235 ASPI_PrintSenseArea(prb);
236 break;
240 /* Posting must be done in such a way that as soon as the SRB_Status is set
241 * we don't touch the SRB anymore because it could possibly be freed
242 * if the app is doing ASPI polling
244 static DWORD
245 WNASPI32_DoPosting( SRB_ExecSCSICmd *lpPRB, DWORD status )
247 void (*SRB_PostProc)() = lpPRB->SRB_PostProc;
248 BYTE SRB_Flags = lpPRB->SRB_Flags;
249 if( status == SS_PENDING )
251 WARN("Tried posting SS_PENDING\n");
252 return SS_PENDING;
254 lpPRB->SRB_Status = status;
255 /* lpPRB is NOT safe, it could be freed in another thread */
257 if (SRB_PostProc)
259 if (SRB_Flags & 0x1)
261 TRACE("Post Routine (%lx) called\n", (DWORD) SRB_PostProc);
262 /* Even though lpPRB could have been freed by
263 * the program.. that's unlikely if it planned
264 * to use it in the PostProc
266 (*SRB_PostProc)(lpPRB);
268 else if (SRB_Flags & SRB_EVENT_NOTIFY) {
269 TRACE("Setting event %04x\n", (HANDLE)SRB_PostProc);
270 SetEvent((HANDLE)SRB_PostProc);
273 return SS_PENDING;
276 static WORD
277 ASPI_ExecScsiCmd(SRB_ExecSCSICmd *lpPRB)
279 struct sg_header *sg_hd, *sg_reply_hdr;
280 DWORD status;
281 int in_len, out_len;
282 int error_code = 0;
283 int fd;
284 DWORD SRB_Status;
286 /* FIXME: hackmode */
287 #define MAKE_TARGET_TO_HOST(lpPRB) \
288 if (!TARGET_TO_HOST(lpPRB)) { \
289 WARN("program was not sending target_to_host for cmd %x (flags=%x),correcting.\n",lpPRB->CDBByte[0],lpPRB->SRB_Flags); \
290 lpPRB->SRB_Flags |= 8; \
292 #define MAKE_HOST_TO_TARGET(lpPRB) \
293 if (!HOST_TO_TARGET(lpPRB)) { \
294 WARN("program was not sending host_to_target for cmd %x (flags=%x),correcting.\n",lpPRB->CDBByte[0],lpPRB->SRB_Flags); \
295 lpPRB->SRB_Flags |= 0x10; \
297 switch (lpPRB->CDBByte[0]) {
298 case 0x12: /* INQUIRY */
299 case 0x5a: /* MODE_SENSE_10 */
300 case 0xa4: /* REPORT_KEY (DVD) MMC-2 */
301 case 0xad: /* READ DVD STRUCTURE MMC-2 */
302 MAKE_TARGET_TO_HOST(lpPRB)
303 break;
304 case 0xa3: /* SEND KEY (DVD) MMC-2 */
305 MAKE_HOST_TO_TARGET(lpPRB)
306 break;
307 default:
308 if ((((lpPRB->SRB_Flags & 0x18) == 0x00) ||
309 ((lpPRB->SRB_Flags & 0x18) == 0x18)
310 ) && lpPRB->SRB_BufLen
312 FIXME("command 0x%02x, no data transfer specified, but buflen is %ld!!!\n",lpPRB->CDBByte[0],lpPRB->SRB_BufLen);
314 break;
316 ASPI_DebugPrintCmd(lpPRB);
317 fd = ASPI_OpenDevice(lpPRB);
318 if (fd == -1) {
319 TRACE("Failed: could not open device c%01dt%01dd%01d. Device permissions !?\n",
320 lpPRB->SRB_HaId,lpPRB->SRB_Target,lpPRB->SRB_Lun);
321 return WNASPI32_DoPosting( lpPRB, SS_NO_DEVICE );
324 sg_hd = NULL;
325 sg_reply_hdr = NULL;
327 lpPRB->SRB_Status = SS_PENDING;
329 if (!lpPRB->SRB_CDBLen) {
330 ERR("Failed: lpPRB->SRB_CDBLen = 0.\n");
331 return WNASPI32_DoPosting( lpPRB, SS_INVALID_SRB );
334 /* build up sg_header + scsi cmd */
335 if (HOST_TO_TARGET(lpPRB)) {
336 /* send header, command, and then data */
337 in_len = SCSI_OFF + lpPRB->SRB_CDBLen + lpPRB->SRB_BufLen;
338 sg_hd = (struct sg_header *) HeapAlloc(GetProcessHeap(), 0, in_len);
339 memset(sg_hd, 0, SCSI_OFF);
340 memcpy(sg_hd + 1, &lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
341 if (lpPRB->SRB_BufLen) {
342 memcpy(((BYTE *) sg_hd) + SCSI_OFF + lpPRB->SRB_CDBLen, lpPRB->SRB_BufPointer, lpPRB->SRB_BufLen);
345 else {
346 /* send header and command - no data */
347 in_len = SCSI_OFF + lpPRB->SRB_CDBLen;
348 sg_hd = (struct sg_header *) HeapAlloc(GetProcessHeap(), 0, in_len);
349 memset(sg_hd, 0, SCSI_OFF);
350 memcpy(sg_hd + 1, &lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
353 if (TARGET_TO_HOST(lpPRB)) {
354 out_len = SCSI_OFF + lpPRB->SRB_BufLen;
355 sg_reply_hdr = (struct sg_header *) HeapAlloc(GetProcessHeap(), 0, out_len);
356 memset(sg_reply_hdr, 0, SCSI_OFF);
357 sg_hd->reply_len = out_len;
359 else {
360 out_len = SCSI_OFF;
361 sg_reply_hdr = (struct sg_header *) HeapAlloc(GetProcessHeap(), 0, out_len);
362 memset(sg_reply_hdr, 0, SCSI_OFF);
363 sg_hd->reply_len = out_len;
366 if(!SCSI_LinuxDeviceIo( fd,
367 sg_hd, in_len,
368 sg_reply_hdr, out_len,
369 &status) )
371 goto error_exit;
374 if (sg_reply_hdr->result != 0) {
375 error_code = sg_reply_hdr->result;
376 WARN("reply header error (%d)\n", sg_reply_hdr->result);
377 goto error_exit;
380 if (TARGET_TO_HOST(lpPRB) && lpPRB->SRB_BufLen) {
381 memcpy(lpPRB->SRB_BufPointer, sg_reply_hdr + 1, lpPRB->SRB_BufLen);
384 /* copy in sense buffer to amount that is available in client */
385 if (lpPRB->SRB_SenseLen) {
386 int sense_len = lpPRB->SRB_SenseLen;
387 if (lpPRB->SRB_SenseLen > 16)
388 sense_len = 16;
390 /* CDB is fixed in WNASPI32 */
391 memcpy(&lpPRB->CDBByte[16], &sg_reply_hdr->sense_buffer[0], sense_len);
393 TRACE("CDB is %d bytes long\n", lpPRB->SRB_CDBLen );
394 ASPI_PrintSenseArea(lpPRB);
397 SRB_Status = SS_COMP;
398 lpPRB->SRB_HaStat = HASTAT_OK;
399 lpPRB->SRB_TargStat = sg_reply_hdr->target_status << 1;
401 HeapFree(GetProcessHeap(), 0, sg_reply_hdr);
402 HeapFree(GetProcessHeap(), 0, sg_hd);
404 /* FIXME: Should this be != 0 maybe? */
405 if( lpPRB->SRB_TargStat == 2 ) {
406 SRB_Status = SS_ERR;
407 switch (lpPRB->CDBByte[0]) {
408 case 0xa4: /* REPORT_KEY (DVD) MMC-2 */
409 case 0xa3: /* SEND KEY (DVD) MMC-2 */
410 SRB_Status = SS_COMP;
411 lpPRB->SRB_TargStat = 0;
412 FIXME("Program wants to do DVD Region switching, but fails (non compliant DVD drive). Ignoring....\n");
413 break;
417 ASPI_DebugPrintResult(lpPRB);
418 /* now do posting */
419 return WNASPI32_DoPosting( lpPRB, SRB_Status );
420 /* In real WNASPI32 stuff really is always pending because ASPI does things
421 in the background, but we are not doing that (yet) */
423 error_exit:
424 SRB_Status = SS_ERR;
425 if (error_code == EBUSY) {
426 WNASPI32_DoPosting( lpPRB, SS_ASPI_IS_BUSY );
427 TRACE("Device busy\n");
428 } else
429 FIXME("Failed\n");
431 /* I'm not sure exactly error codes work here
432 * We probably should set lpPRB->SRB_TargStat, SRB_HaStat ?
434 WARN("error_exit\n");
435 HeapFree(GetProcessHeap(), 0, sg_reply_hdr);
436 HeapFree(GetProcessHeap(), 0, sg_hd);
437 WNASPI32_DoPosting( lpPRB, SRB_Status );
438 return SS_PENDING;
441 #endif /* defined(linux) */
444 /*******************************************************************
445 * GetASPI32SupportInfo [WNASPI32.0]
447 * Checks if the ASPI subsystem is initialized correctly.
449 * RETURNS
450 * HIWORD: 0.
451 * HIBYTE of LOWORD: status (SS_COMP or SS_FAILED_INIT)
452 * LOBYTE of LOWORD: # of host adapters.
454 DWORD WINAPI GetASPI32SupportInfo()
456 return ((SS_COMP << 8) | 1); /* FIXME: get # of host adapters installed */
460 /***********************************************************************
461 * SendASPI32Command (WNASPI32.1)
463 DWORD __cdecl SendASPI32Command(LPSRB lpSRB)
465 #ifdef linux
466 switch (lpSRB->common.SRB_Cmd) {
467 case SC_HA_INQUIRY:
468 lpSRB->inquiry.SRB_Status = SS_COMP; /* completed successfully */
469 lpSRB->inquiry.HA_Count = 1; /* not always */
470 lpSRB->inquiry.HA_SCSI_ID = 7; /* not always ID 7 */
471 strcat(lpSRB->inquiry.HA_ManagerId, "ASPI for WIN32"); /* max 15 chars, don't change */
472 strcat(lpSRB->inquiry.HA_Identifier, "Wine host"); /* FIXME: return host adapter name */
473 memset(lpSRB->inquiry.HA_Unique, 0, 16); /* default HA_Unique content */
474 lpSRB->inquiry.HA_Unique[6] = 0x02; /* Maximum Transfer Length (128K, Byte> 4-7) */
475 FIXME("ASPI: Partially implemented SC_HA_INQUIRY for adapter %d.\n", lpSRB->inquiry.SRB_HaId);
476 return SS_COMP;
478 case SC_GET_DEV_TYPE: {
479 /* FIXME: We should return SS_NO_DEVICE if the device is not configured */
480 /* FIXME: We should return SS_INVALID_HA if HostAdapter!=0 */
481 SRB tmpsrb;
482 char inqbuf[200];
483 DWORD ret;
485 memset(&tmpsrb,0,sizeof(tmpsrb));
487 /* Copy header */
488 memcpy(&tmpsrb.common,&(lpSRB->common),sizeof(tmpsrb.common));
490 tmpsrb.cmd.SRB_Flags |= 8; /* target to host */
491 tmpsrb.cmd.SRB_Cmd = SC_EXEC_SCSI_CMD;
492 tmpsrb.cmd.SRB_Target = lpSRB->devtype.SRB_Target;
493 tmpsrb.cmd.SRB_Lun = lpSRB->devtype.SRB_Lun;
494 tmpsrb.cmd.SRB_BufLen = sizeof(inqbuf);
495 tmpsrb.cmd.SRB_BufPointer = inqbuf;
496 tmpsrb.cmd.CDBByte[0] = 0x12; /* INQUIRY */
497 /* FIXME: handle lun */
498 tmpsrb.cmd.CDBByte[4] = sizeof(inqbuf);
499 tmpsrb.cmd.SRB_CDBLen = 6;
501 ret = ASPI_ExecScsiCmd(&tmpsrb.cmd);
503 lpSRB->devtype.SRB_Status = tmpsrb.cmd.SRB_Status;
504 lpSRB->devtype.SRB_DeviceType = inqbuf[0]&0x1f;
506 TRACE("returning devicetype %d for target %d\n",inqbuf[0]&0x1f,tmpsrb.cmd.SRB_Target);
507 if (ret!=SS_PENDING) /* Any error is passed down directly */
508 return ret;
509 /* FIXME: knows that the command is finished already, pass final Status */
510 return tmpsrb.cmd.SRB_Status;
512 case SC_EXEC_SCSI_CMD:
513 return ASPI_ExecScsiCmd(&lpSRB->cmd);
514 case SC_ABORT_SRB:
515 FIXME("Not implemented SC_ABORT_SRB\n");
516 break;
517 case SC_RESET_DEV:
518 FIXME("Not implemented SC_RESET_DEV\n");
519 break;
520 #ifdef SC_GET_DISK_INFO
521 case SC_GET_DISK_INFO:
522 /* NT Doesn't implement this either.. so don't feel bad */
523 FIXME("Not implemented SC_GET_DISK_INFO\n");
524 break;
525 #endif
526 default:
527 FIXME("Unknown command %d\n", lpSRB->common.SRB_Cmd);
529 return SS_INVALID_SRB;
530 #else
531 return SS_INVALID_SRB;
532 #endif
536 /***********************************************************************
537 * GetASPI32DLLVersion (WNASPI32.3)
540 DWORD WINAPI GetASPI32DLLVersion()
542 #ifdef linux
543 return (DWORD)1;
544 #else
545 return (DWORD)0;
546 #endif