19 #include "debugtools.h"
21 DEFAULT_DEBUG_CHANNEL(aspi);
24 * 1) Residual byte length reporting not handled
25 * 2) Make this code re-entrant for multithreading
26 * -- Added CriticalSection to OpenDevices function
27 * 3) Only linux supported so far
28 * 4) Leaves sg devices open. This may or may not be okay. A better solution
29 * would be to close the file descriptors when the thread/process using
30 * them no longer needs them.
35 static ASPI_DEVICE_INFO *ASPI_open_devices = NULL;
36 static CRITICAL_SECTION ASPI_CritSection;
38 #endif /* defined(linux) */
41 BOOL WINAPI WNASPI32_LibMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID fImpLoad)
44 static BOOL bInitDone=FALSE;
46 TRACE("0x%x 0x%1x %p\n", hInstDLL, fdwReason, fImpLoad);
50 case DLL_PROCESS_ATTACH:
51 /* Create instance data */
55 /* Initialize global stuff just once */
56 InitializeCriticalSection(&ASPI_CritSection);
60 case DLL_PROCESS_DETACH:
61 /* Destroy instance data */
63 case DLL_THREAD_ATTACH:
64 case DLL_THREAD_DETACH:
68 #else /* defined(linux) */
70 #endif /* defined(linux) */
77 ASPI_OpenDevice(SRB_ExecSCSICmd *prb)
81 ASPI_DEVICE_INFO *curr;
83 /* search list of devices to see if we've opened it already.
84 * There is not an explicit open/close in ASPI land, so hopefully
85 * keeping a device open won't be a problem.
88 EnterCriticalSection(&ASPI_CritSection);
89 for (curr = ASPI_open_devices; curr; curr = curr->next) {
90 if (curr->hostId == prb->SRB_HaId &&
91 curr->target == prb->SRB_Target &&
92 curr->lun == prb->SRB_Lun) {
93 LeaveCriticalSection(&ASPI_CritSection);
97 LeaveCriticalSection(&ASPI_CritSection);
99 hc = ASPI_GetHCforController( prb->SRB_HaId );
100 fd = SCSI_OpenDevice( HIWORD(hc), LOWORD(hc), prb->SRB_Target, prb->SRB_Lun);
106 /* device is now open */
107 /* FIXME: Let users specify SCSI timeout in registry */
108 SCSI_LinuxSetTimeout( fd, SCSI_DEFAULT_TIMEOUT );
110 curr = HeapAlloc( GetProcessHeap(), 0, sizeof(ASPI_DEVICE_INFO) );
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 EnterCriticalSection(&ASPI_CritSection);
118 curr->next = ASPI_open_devices;
119 ASPI_open_devices = curr;
120 LeaveCriticalSection(&ASPI_CritSection);
126 ASPI_DebugPrintCmd(SRB_ExecSCSICmd *prb)
132 switch (prb->CDBByte[0]) {
134 TRACE("INQUIRY {\n");
135 TRACE("\tEVPD: %d\n", prb->CDBByte[1] & 1);
136 TRACE("\tLUN: %d\n", (prb->CDBByte[1] & 0xc) >> 1);
137 TRACE("\tPAGE CODE: %d\n", prb->CDBByte[2]);
138 TRACE("\tALLOCATION LENGTH: %d\n", prb->CDBByte[4]);
139 TRACE("\tCONTROL: %d\n", prb->CDBByte[5]);
143 TRACE("Transfer Length: %d\n", prb->CDBByte[4]);
147 TRACE("Host Adapter: %d\n", prb->SRB_HaId);
148 TRACE("Flags: %d\n", prb->SRB_Flags);
149 if (TARGET_TO_HOST(prb)) {
150 TRACE("\tData transfer: Target to host. Length checked.\n");
152 else if (HOST_TO_TARGET(prb)) {
153 TRACE("\tData transfer: Host to target. Length checked.\n");
155 else if (NO_DATA_TRANSFERED(prb)) {
156 TRACE("\tData transfer: none\n");
159 WARN("\tTransfer by scsi cmd. Length not checked.\n");
162 TRACE("\tResidual byte length reporting %s\n", prb->SRB_Flags & 0x4 ? "enabled" : "disabled");
163 TRACE("\tLinking %s\n", prb->SRB_Flags & 0x2 ? "enabled" : "disabled");
164 TRACE("\tPosting %s\n", prb->SRB_Flags & 0x1 ? "enabled" : "disabled");
165 TRACE("Target: %d\n", prb->SRB_Target);
166 TRACE("Lun: %d\n", prb->SRB_Lun);
167 TRACE("BufLen: %ld\n", prb->SRB_BufLen);
168 TRACE("SenseLen: %d\n", prb->SRB_SenseLen);
169 TRACE("BufPtr: %p\n", prb->SRB_BufPointer);
170 TRACE("CDB Length: %d\n", prb->SRB_CDBLen);
171 TRACE("POST Proc: %lx\n", (DWORD) prb->SRB_PostProc);
172 cdb = &prb->CDBByte[0];
173 cmd = prb->CDBByte[0];
174 if (TRACE_ON(aspi)) {
175 DPRINTF("CDB buffer[");
176 for (i = 0; i < prb->SRB_CDBLen; i++) {
177 if (i != 0) DPRINTF(",");
178 DPRINTF("%02x", *cdb++);
185 ASPI_PrintCDBArea(SRB_ExecSCSICmd *prb)
191 for (i = 0; i < prb->SRB_CDBLen; i++) {
193 DPRINTF("%02x", prb->CDBByte[i]);
200 ASPI_PrintSenseArea(SRB_ExecSCSICmd *prb)
203 BYTE *rqbuf = prb->SenseArea;
207 DPRINTF("Request Sense reports:\n");
208 if ((rqbuf[0]&0x7f)!=0x70) {
209 DPRINTF("\tInvalid sense header: 0x%02x instead of 0x70\n", rqbuf[0]&0x7f);
212 DPRINTF("\tCurrent command read filemark: %s\n",(rqbuf[2]&0x80)?"yes":"no");
213 DPRINTF("\tEarly warning passed: %s\n",(rqbuf[2]&0x40)?"yes":"no");
214 DPRINTF("\tIncorrect blocklength: %s\n",(rqbuf[2]&0x20)?"yes":"no");
215 DPRINTF("\tSense Key: %d\n",rqbuf[2]&0xf);
217 DPRINTF("\tResidual Length: %d\n",rqbuf[3]*0x1000000+rqbuf[4]*0x10000+rqbuf[5]*0x100+rqbuf[6]);
218 DPRINTF("\tAdditional Sense Length: %d\n",rqbuf[7]);
219 DPRINTF("\tAdditional Sense Code: %d\n",rqbuf[12]);
220 DPRINTF("\tAdditional Sense Code Qualifier: %d\n",rqbuf[13]);
221 if (rqbuf[15]&0x80) {
222 DPRINTF("\tIllegal Param is in %s\n",(rqbuf[15]&0x40)?"the CDB":"the Data Out Phase");
224 DPRINTF("Pointer at %d, bit %d\n",rqbuf[16]*256+rqbuf[17],rqbuf[15]&0x7);
227 DPRINTF("SenseArea[");
228 for (i = 0; i < prb->SRB_SenseLen; i++) {
230 DPRINTF("%02x", *rqbuf++);
237 ASPI_DebugPrintResult(SRB_ExecSCSICmd *prb)
240 TRACE("SRB_Status: %x\n", prb->SRB_Status);
241 TRACE("SRB_HaStat: %x\n", prb->SRB_HaStat);
242 TRACE("SRB_TargStat: %x\n", prb->SRB_TargStat);
243 switch (prb->CDBByte[0]) {
245 TRACE("Vendor: '%s'\n", prb->SRB_BufPointer + INQUIRY_VENDOR);
247 case CMD_TEST_UNIT_READY:
248 ASPI_PrintSenseArea(prb);
253 /* Posting must be done in such a way that as soon as the SRB_Status is set
254 * we don't touch the SRB anymore because it could possibly be freed
255 * if the app is doing ASPI polling
258 WNASPI32_DoPosting( SRB_ExecSCSICmd *lpPRB, DWORD status )
260 void (*SRB_PostProc)() = lpPRB->SRB_PostProc;
261 BYTE SRB_Flags = lpPRB->SRB_Flags;
262 if( status == SS_PENDING )
264 WARN("Tried posting SS_PENDING\n");
267 lpPRB->SRB_Status = status;
268 /* lpPRB is NOT safe, it could be freed in another thread */
274 TRACE("Post Routine (%lx) called\n", (DWORD) SRB_PostProc);
275 /* Even though lpPRB could have been freed by
276 * the program.. that's unlikely if it planned
277 * to use it in the PostProc
279 (*SRB_PostProc)(lpPRB);
281 else if (SRB_Flags & SRB_EVENT_NOTIFY) {
282 TRACE("Setting event %04x\n", (HANDLE)SRB_PostProc);
283 SetEvent((HANDLE)SRB_PostProc);
290 ASPI_ExecScsiCmd(SRB_ExecSCSICmd *lpPRB)
292 struct sg_header *sg_hd, *sg_reply_hdr;
299 /* FIXME: hackmode */
300 #define MAKE_TARGET_TO_HOST(lpPRB) \
301 if (!TARGET_TO_HOST(lpPRB)) { \
302 WARN("program was not sending target_to_host for cmd %x (flags=%x),correcting.\n",lpPRB->CDBByte[0],lpPRB->SRB_Flags); \
303 lpPRB->SRB_Flags |= 8; \
305 #define MAKE_HOST_TO_TARGET(lpPRB) \
306 if (!HOST_TO_TARGET(lpPRB)) { \
307 WARN("program was not sending host_to_target for cmd %x (flags=%x),correcting.\n",lpPRB->CDBByte[0],lpPRB->SRB_Flags); \
308 lpPRB->SRB_Flags |= 0x10; \
310 switch (lpPRB->CDBByte[0]) {
311 case 0x12: /* INQUIRY */
312 case 0x5a: /* MODE_SENSE_10 */
313 case 0xa4: /* REPORT_KEY (DVD) MMC-2 */
314 case 0xad: /* READ DVD STRUCTURE MMC-2 */
315 MAKE_TARGET_TO_HOST(lpPRB)
317 case 0xa3: /* SEND KEY (DVD) MMC-2 */
318 MAKE_HOST_TO_TARGET(lpPRB)
321 if ((((lpPRB->SRB_Flags & 0x18) == 0x00) ||
322 ((lpPRB->SRB_Flags & 0x18) == 0x18)
323 ) && lpPRB->SRB_BufLen
325 FIXME("command 0x%02x, no data transfer specified, but buflen is %ld!!!\n",lpPRB->CDBByte[0],lpPRB->SRB_BufLen);
329 ASPI_DebugPrintCmd(lpPRB);
330 fd = ASPI_OpenDevice(lpPRB);
332 return WNASPI32_DoPosting( lpPRB, SS_NO_DEVICE );
338 lpPRB->SRB_Status = SS_PENDING;
340 if (!lpPRB->SRB_CDBLen) {
341 ERR("Failed: lpPRB->SRB_CDBLen = 0.\n");
342 return WNASPI32_DoPosting( lpPRB, SS_INVALID_SRB );
345 /* build up sg_header + scsi cmd */
346 if (HOST_TO_TARGET(lpPRB)) {
347 /* send header, command, and then data */
348 in_len = SCSI_OFF + lpPRB->SRB_CDBLen + lpPRB->SRB_BufLen;
349 sg_hd = (struct sg_header *) HeapAlloc(GetProcessHeap(), 0, in_len);
350 memset(sg_hd, 0, SCSI_OFF);
351 memcpy(sg_hd + 1, &lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
352 if (lpPRB->SRB_BufLen) {
353 memcpy(((BYTE *) sg_hd) + SCSI_OFF + lpPRB->SRB_CDBLen, lpPRB->SRB_BufPointer, lpPRB->SRB_BufLen);
357 /* send header and command - no data */
358 in_len = SCSI_OFF + lpPRB->SRB_CDBLen;
359 sg_hd = (struct sg_header *) HeapAlloc(GetProcessHeap(), 0, in_len);
360 memset(sg_hd, 0, SCSI_OFF);
361 memcpy(sg_hd + 1, &lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
364 if (TARGET_TO_HOST(lpPRB)) {
365 out_len = SCSI_OFF + lpPRB->SRB_BufLen;
366 sg_reply_hdr = (struct sg_header *) HeapAlloc(GetProcessHeap(), 0, out_len);
367 memset(sg_reply_hdr, 0, SCSI_OFF);
368 sg_hd->reply_len = out_len;
372 sg_reply_hdr = (struct sg_header *) HeapAlloc(GetProcessHeap(), 0, out_len);
373 memset(sg_reply_hdr, 0, SCSI_OFF);
374 sg_hd->reply_len = out_len;
377 SCSI_Fix_CMD_LEN(fd, lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
379 if(!SCSI_LinuxDeviceIo( fd,
381 sg_reply_hdr, out_len,
387 if (sg_reply_hdr->result != 0) {
388 error_code = sg_reply_hdr->result;
389 WARN("reply header error (%d)\n", sg_reply_hdr->result);
393 if (TARGET_TO_HOST(lpPRB) && lpPRB->SRB_BufLen) {
394 memcpy(lpPRB->SRB_BufPointer, sg_reply_hdr + 1, lpPRB->SRB_BufLen);
397 /* copy in sense buffer to amount that is available in client */
398 if (lpPRB->SRB_SenseLen) {
399 int sense_len = lpPRB->SRB_SenseLen;
400 if (lpPRB->SRB_SenseLen > 16)
403 /* CDB is fixed in WNASPI32 */
404 memcpy(lpPRB->SenseArea, &sg_reply_hdr->sense_buffer[0], sense_len);
406 ASPI_PrintCDBArea(lpPRB);
407 ASPI_PrintSenseArea(lpPRB);
410 SRB_Status = SS_COMP;
411 lpPRB->SRB_HaStat = HASTAT_OK;
412 lpPRB->SRB_TargStat = sg_reply_hdr->target_status << 1;
414 HeapFree(GetProcessHeap(), 0, sg_reply_hdr);
415 HeapFree(GetProcessHeap(), 0, sg_hd);
417 /* FIXME: Should this be != 0 maybe? */
418 if( lpPRB->SRB_TargStat == 2 ) {
420 switch (lpPRB->CDBByte[0]) {
421 case 0xa4: /* REPORT_KEY (DVD) MMC-2 */
422 case 0xa3: /* SEND KEY (DVD) MMC-2 */
423 SRB_Status = SS_COMP;
424 lpPRB->SRB_TargStat = 0;
425 FIXME("Program wants to do DVD Region switching, but fails (non compliant DVD drive). Ignoring....\n");
430 ASPI_DebugPrintResult(lpPRB);
432 return WNASPI32_DoPosting( lpPRB, SRB_Status );
433 /* In real WNASPI32 stuff really is always pending because ASPI does things
434 in the background, but we are not doing that (yet) */
438 if (error_code == EBUSY) {
439 WNASPI32_DoPosting( lpPRB, SS_ASPI_IS_BUSY );
440 TRACE("Device busy\n");
444 /* I'm not sure exactly error codes work here
445 * We probably should set lpPRB->SRB_TargStat, SRB_HaStat ?
447 WARN("error_exit\n");
448 HeapFree(GetProcessHeap(), 0, sg_reply_hdr);
449 HeapFree(GetProcessHeap(), 0, sg_hd);
450 WNASPI32_DoPosting( lpPRB, SRB_Status );
454 #endif /* defined(linux) */
457 /*******************************************************************
458 * GetASPI32SupportInfo [WNASPI32.0]
460 * Checks if the ASPI subsystem is initialized correctly.
464 * HIBYTE of LOWORD: status (SS_COMP or SS_FAILED_INIT)
465 * LOBYTE of LOWORD: # of host adapters.
467 DWORD __cdecl GetASPI32SupportInfo(void)
469 return ((SS_COMP << 8) | ASPI_GetNumControllers());
473 /***********************************************************************
474 * SendASPI32Command (WNASPI32.1)
476 DWORD __cdecl SendASPI32Command(LPSRB lpSRB)
479 switch (lpSRB->common.SRB_Cmd) {
481 lpSRB->inquiry.SRB_Status = SS_COMP; /* completed successfully */
482 lpSRB->inquiry.HA_Count = 1; /* not always */
483 lpSRB->inquiry.HA_SCSI_ID = 7; /* not always ID 7 */
484 strcpy(lpSRB->inquiry.HA_ManagerId, "ASPI for WIN32"); /* max 15 chars, don't change */
485 strcpy(lpSRB->inquiry.HA_Identifier, "Wine host"); /* FIXME: return host adapter name */
486 memset(lpSRB->inquiry.HA_Unique, 0, 16); /* default HA_Unique content */
487 lpSRB->inquiry.HA_Unique[6] = 0x02; /* Maximum Transfer Length (128K, Byte> 4-7) */
488 lpSRB->inquiry.HA_Unique[3] = 0x08; /* Maximum number of SCSI targets */
489 FIXME("ASPI: Partially implemented SC_HA_INQUIRY for adapter %d.\n", lpSRB->inquiry.SRB_HaId);
492 case SC_GET_DEV_TYPE: {
493 /* FIXME: We should return SS_NO_DEVICE if the device is not configured */
494 /* FIXME: We should return SS_INVALID_HA if HostAdapter!=0 */
499 memset(&tmpsrb,0,sizeof(tmpsrb));
502 memcpy(&tmpsrb.common,&(lpSRB->common),sizeof(tmpsrb.common));
504 tmpsrb.cmd.SRB_Flags |= 8; /* target to host */
505 tmpsrb.cmd.SRB_Cmd = SC_EXEC_SCSI_CMD;
506 tmpsrb.cmd.SRB_Target = lpSRB->devtype.SRB_Target;
507 tmpsrb.cmd.SRB_Lun = lpSRB->devtype.SRB_Lun;
508 tmpsrb.cmd.SRB_BufLen = sizeof(inqbuf);
509 tmpsrb.cmd.SRB_BufPointer = inqbuf;
510 tmpsrb.cmd.CDBByte[0] = 0x12; /* INQUIRY */
511 /* FIXME: handle lun */
512 tmpsrb.cmd.CDBByte[4] = sizeof(inqbuf);
513 tmpsrb.cmd.SRB_CDBLen = 6;
515 ret = ASPI_ExecScsiCmd(&tmpsrb.cmd);
517 lpSRB->devtype.SRB_Status = tmpsrb.cmd.SRB_Status;
518 lpSRB->devtype.SRB_DeviceType = inqbuf[0]&0x1f;
520 TRACE("returning devicetype %d for target %d\n",inqbuf[0]&0x1f,tmpsrb.cmd.SRB_Target);
521 if (ret!=SS_PENDING) /* Any error is passed down directly */
523 /* FIXME: knows that the command is finished already, pass final Status */
524 return tmpsrb.cmd.SRB_Status;
526 case SC_EXEC_SCSI_CMD:
527 return ASPI_ExecScsiCmd(&lpSRB->cmd);
529 FIXME("Not implemented SC_ABORT_SRB\n");
532 FIXME("Not implemented SC_RESET_DEV\n");
534 #ifdef SC_GET_DISK_INFO
535 case SC_GET_DISK_INFO:
536 /* NT Doesn't implement this either.. so don't feel bad */
537 FIXME("Not implemented SC_GET_DISK_INFO\n");
541 FIXME("Unknown command %d\n", lpSRB->common.SRB_Cmd);
543 return SS_INVALID_SRB;
545 return SS_INVALID_SRB;
550 /***********************************************************************
551 * GetASPI32DLLVersion (WNASPI32.3)
553 DWORD __cdecl GetASPI32DLLVersion(void)
556 TRACE("Returning version 1\n");
559 FIXME("Please add SCSI support for your operating system, returning 0\n");
564 /***********************************************************************
565 * GetASPI32Buffer (WNASPI32.@)
567 BOOL __cdecl GetASPI32Buffer(LPVOID pab) /* [???] FIXME: PASPI32BUFF */
569 FIXME("(%p), stub !\n", pab);
573 /***********************************************************************
574 * FreeASPI32Buffer (WNASPI32.@)
576 BOOL __cdecl FreeASPI32Buffer(LPVOID pab) /* [???] FIXME: PASPI32BUFF */
578 FIXME("(%p), stub !\n", pab);
582 /***********************************************************************
583 * TranslateASPI32Address (WNASPI32.@)
585 BOOL __cdecl TranslateASPI32Address(LPDWORD pdwPath, LPDWORD pdwDEVNODE)
587 FIXME("(%p, %p), stub !\n", pdwPath, pdwDEVNODE);