19 #include "debugtools.h"
22 DEFAULT_DEBUG_CHANNEL(aspi);
25 * 1) Residual byte length reporting not handled
26 * 2) Make this code re-entrant for multithreading
27 * -- Added CriticalSection to OpenDevices function
28 * 3) Only linux supported so far
29 * 4) Leaves sg devices open. This may or may not be okay. A better solution
30 * would be to close the file descriptors when the thread/process using
31 * them no longer needs them.
36 static ASPI_DEVICE_INFO *ASPI_open_devices = NULL;
37 static CRITICAL_SECTION ASPI_CritSection;
39 #endif /* defined(linux) */
42 BOOL WINAPI WNASPI32_LibMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID fImpLoad)
45 static BOOL bInitDone=FALSE;
47 TRACE("0x%x 0x%1x %p\n", hInstDLL, fdwReason, fImpLoad);
51 case DLL_PROCESS_ATTACH:
52 /* Create instance data */
56 /* Initialize global stuff just once */
57 InitializeCriticalSection(&ASPI_CritSection);
61 case DLL_PROCESS_DETACH:
62 /* Destroy instance data */
64 case DLL_THREAD_ATTACH:
65 case DLL_THREAD_DETACH:
69 #else /* defined(linux) */
71 #endif /* defined(linux) */
78 ASPI_OpenDevice(SRB_ExecSCSICmd *prb)
82 ASPI_DEVICE_INFO *curr;
84 /* search list of devices to see if we've opened it already.
85 * There is not an explicit open/close in ASPI land, so hopefully
86 * keeping a device open won't be a problem.
89 EnterCriticalSection(&ASPI_CritSection);
90 for (curr = ASPI_open_devices; curr; curr = curr->next) {
91 if (curr->hostId == prb->SRB_HaId &&
92 curr->target == prb->SRB_Target &&
93 curr->lun == prb->SRB_Lun) {
94 LeaveCriticalSection(&ASPI_CritSection);
98 LeaveCriticalSection(&ASPI_CritSection);
100 hc = ASPI_GetHCforController( prb->SRB_HaId );
101 fd = SCSI_OpenDevice( HIWORD(hc), LOWORD(hc), prb->SRB_Target, prb->SRB_Lun);
107 /* device is now open */
108 /* FIXME: Let users specify SCSI timeout in registry */
109 SCSI_LinuxSetTimeout( fd, SCSI_DEFAULT_TIMEOUT );
111 curr = HeapAlloc( GetProcessHeap(), 0, sizeof(ASPI_DEVICE_INFO) );
113 curr->hostId = prb->SRB_HaId;
114 curr->target = prb->SRB_Target;
115 curr->lun = prb->SRB_Lun;
117 /* insert new record at beginning of open device list */
118 EnterCriticalSection(&ASPI_CritSection);
119 curr->next = ASPI_open_devices;
120 ASPI_open_devices = curr;
121 LeaveCriticalSection(&ASPI_CritSection);
127 ASPI_DebugPrintCmd(SRB_ExecSCSICmd *prb)
133 switch (prb->CDBByte[0]) {
135 TRACE("INQUIRY {\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]);
144 TRACE("Transfer Length: %d\n", prb->CDBByte[4]);
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_TRANSFERED(prb)) {
157 TRACE("\tData transfer: none\n");
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: %p\n", prb->SRB_BufPointer);
171 TRACE("CDB Length: %d\n", prb->SRB_CDBLen);
172 TRACE("POST Proc: %lx\n", (DWORD) prb->SRB_PostProc);
173 cdb = &prb->CDBByte[0];
174 cmd = prb->CDBByte[0];
175 if (TRACE_ON(aspi)) {
176 DPRINTF("CDB buffer[");
177 for (i = 0; i < prb->SRB_CDBLen; i++) {
178 if (i != 0) DPRINTF(",");
179 DPRINTF("%02x", *cdb++);
186 ASPI_PrintSenseArea(SRB_ExecSCSICmd *prb)
189 BYTE *rqbuf = &prb->CDBByte[16];
193 DPRINTF("Request Sense reports:\n");
194 if ((rqbuf[0]&0x7f)!=0x70) {
195 DPRINTF("\tInvalid sense header: 0x%02x instead of 0x70\n", rqbuf[0]&0x7f);
198 DPRINTF("\tCurrent command read filemark: %s\n",(rqbuf[2]&0x80)?"yes":"no");
199 DPRINTF("\tEarly warning passed: %s\n",(rqbuf[2]&0x40)?"yes":"no");
200 DPRINTF("\tIncorrect blocklength: %s\n",(rqbuf[2]&0x20)?"yes":"no");
201 DPRINTF("\tSense Key: %d\n",rqbuf[2]&0xf);
203 DPRINTF("\tResidual Length: %d\n",rqbuf[3]*0x1000000+rqbuf[4]*0x10000+rqbuf[5]*0x100+rqbuf[6]);
204 DPRINTF("\tAdditional Sense Length: %d\n",rqbuf[7]);
205 DPRINTF("\tAdditional Sense Code: %d\n",rqbuf[12]);
206 DPRINTF("\tAdditional Sense Code Qualifier: %d\n",rqbuf[13]);
207 if (rqbuf[15]&0x80) {
208 DPRINTF("\tIllegal Param is in %s\n",(rqbuf[15]&0x40)?"the CDB":"the Data Out Phase");
210 DPRINTF("Pointer at %d, bit %d\n",rqbuf[16]*256+rqbuf[17],rqbuf[15]&0x7);
213 DPRINTF("SenseArea[");
214 for (i = 0; i < prb->SRB_SenseLen; i++) {
216 DPRINTF("%02x", *rqbuf++);
223 ASPI_DebugPrintResult(SRB_ExecSCSICmd *prb)
226 TRACE("SRB_Status: %x\n", prb->SRB_Status);
227 TRACE("SRB_HaStat: %x\n", prb->SRB_HaStat);
228 TRACE("SRB_TargStat: %x\n", prb->SRB_TargStat);
229 switch (prb->CDBByte[0]) {
231 TRACE("Vendor: '%s'\n", prb->SRB_BufPointer + INQUIRY_VENDOR);
233 case CMD_TEST_UNIT_READY:
234 ASPI_PrintSenseArea(prb);
239 /* Posting must be done in such a way that as soon as the SRB_Status is set
240 * we don't touch the SRB anymore because it could possibly be freed
241 * if the app is doing ASPI polling
244 WNASPI32_DoPosting( SRB_ExecSCSICmd *lpPRB, DWORD status )
246 void (*SRB_PostProc)() = lpPRB->SRB_PostProc;
247 BYTE SRB_Flags = lpPRB->SRB_Flags;
248 if( status == SS_PENDING )
250 WARN("Tried posting SS_PENDING\n");
253 lpPRB->SRB_Status = status;
254 /* lpPRB is NOT safe, it could be freed in another thread */
260 TRACE("Post Routine (%lx) called\n", (DWORD) SRB_PostProc);
261 /* Even though lpPRB could have been freed by
262 * the program.. that's unlikely if it planned
263 * to use it in the PostProc
265 (*SRB_PostProc)(lpPRB);
267 else if (SRB_Flags & SRB_EVENT_NOTIFY) {
268 TRACE("Setting event %04x\n", (HANDLE)SRB_PostProc);
269 SetEvent((HANDLE)SRB_PostProc);
276 ASPI_ExecScsiCmd(SRB_ExecSCSICmd *lpPRB)
278 struct sg_header *sg_hd, *sg_reply_hdr;
285 /* FIXME: hackmode */
286 #define MAKE_TARGET_TO_HOST(lpPRB) \
287 if (!TARGET_TO_HOST(lpPRB)) { \
288 WARN("program was not sending target_to_host for cmd %x (flags=%x),correcting.\n",lpPRB->CDBByte[0],lpPRB->SRB_Flags); \
289 lpPRB->SRB_Flags |= 8; \
291 #define MAKE_HOST_TO_TARGET(lpPRB) \
292 if (!HOST_TO_TARGET(lpPRB)) { \
293 WARN("program was not sending host_to_target for cmd %x (flags=%x),correcting.\n",lpPRB->CDBByte[0],lpPRB->SRB_Flags); \
294 lpPRB->SRB_Flags |= 0x10; \
296 switch (lpPRB->CDBByte[0]) {
297 case 0x12: /* INQUIRY */
298 case 0x5a: /* MODE_SENSE_10 */
299 case 0xa4: /* REPORT_KEY (DVD) MMC-2 */
300 case 0xad: /* READ DVD STRUCTURE MMC-2 */
301 MAKE_TARGET_TO_HOST(lpPRB)
303 case 0xa3: /* SEND KEY (DVD) MMC-2 */
304 MAKE_HOST_TO_TARGET(lpPRB)
307 if ((((lpPRB->SRB_Flags & 0x18) == 0x00) ||
308 ((lpPRB->SRB_Flags & 0x18) == 0x18)
309 ) && lpPRB->SRB_BufLen
311 FIXME("command 0x%02x, no data transfer specified, but buflen is %ld!!!\n",lpPRB->CDBByte[0],lpPRB->SRB_BufLen);
315 ASPI_DebugPrintCmd(lpPRB);
316 fd = ASPI_OpenDevice(lpPRB);
318 return WNASPI32_DoPosting( lpPRB, SS_NO_DEVICE );
324 lpPRB->SRB_Status = SS_PENDING;
326 if (!lpPRB->SRB_CDBLen) {
327 ERR("Failed: lpPRB->SRB_CDBLen = 0.\n");
328 return WNASPI32_DoPosting( lpPRB, SS_INVALID_SRB );
331 /* build up sg_header + scsi cmd */
332 if (HOST_TO_TARGET(lpPRB)) {
333 /* send header, command, and then data */
334 in_len = SCSI_OFF + lpPRB->SRB_CDBLen + lpPRB->SRB_BufLen;
335 sg_hd = (struct sg_header *) HeapAlloc(GetProcessHeap(), 0, in_len);
336 memset(sg_hd, 0, SCSI_OFF);
337 memcpy(sg_hd + 1, &lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
338 if (lpPRB->SRB_BufLen) {
339 memcpy(((BYTE *) sg_hd) + SCSI_OFF + lpPRB->SRB_CDBLen, lpPRB->SRB_BufPointer, lpPRB->SRB_BufLen);
343 /* send header and command - no data */
344 in_len = SCSI_OFF + lpPRB->SRB_CDBLen;
345 sg_hd = (struct sg_header *) HeapAlloc(GetProcessHeap(), 0, in_len);
346 memset(sg_hd, 0, SCSI_OFF);
347 memcpy(sg_hd + 1, &lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
350 if (TARGET_TO_HOST(lpPRB)) {
351 out_len = SCSI_OFF + lpPRB->SRB_BufLen;
352 sg_reply_hdr = (struct sg_header *) HeapAlloc(GetProcessHeap(), 0, out_len);
353 memset(sg_reply_hdr, 0, SCSI_OFF);
354 sg_hd->reply_len = out_len;
358 sg_reply_hdr = (struct sg_header *) HeapAlloc(GetProcessHeap(), 0, out_len);
359 memset(sg_reply_hdr, 0, SCSI_OFF);
360 sg_hd->reply_len = out_len;
363 SCSI_Fix_CMD_LEN(fd, lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
365 if(!SCSI_LinuxDeviceIo( fd,
367 sg_reply_hdr, out_len,
373 if (sg_reply_hdr->result != 0) {
374 error_code = sg_reply_hdr->result;
375 WARN("reply header error (%d)\n", sg_reply_hdr->result);
379 if (TARGET_TO_HOST(lpPRB) && lpPRB->SRB_BufLen) {
380 memcpy(lpPRB->SRB_BufPointer, sg_reply_hdr + 1, lpPRB->SRB_BufLen);
383 /* copy in sense buffer to amount that is available in client */
384 if (lpPRB->SRB_SenseLen) {
385 int sense_len = lpPRB->SRB_SenseLen;
386 if (lpPRB->SRB_SenseLen > 16)
389 /* CDB is fixed in WNASPI32 */
390 memcpy(&lpPRB->CDBByte[16], &sg_reply_hdr->sense_buffer[0], sense_len);
392 TRACE("CDB is %d bytes long\n", lpPRB->SRB_CDBLen );
393 ASPI_PrintSenseArea(lpPRB);
396 SRB_Status = SS_COMP;
397 lpPRB->SRB_HaStat = HASTAT_OK;
398 lpPRB->SRB_TargStat = sg_reply_hdr->target_status << 1;
400 HeapFree(GetProcessHeap(), 0, sg_reply_hdr);
401 HeapFree(GetProcessHeap(), 0, sg_hd);
403 /* FIXME: Should this be != 0 maybe? */
404 if( lpPRB->SRB_TargStat == 2 ) {
406 switch (lpPRB->CDBByte[0]) {
407 case 0xa4: /* REPORT_KEY (DVD) MMC-2 */
408 case 0xa3: /* SEND KEY (DVD) MMC-2 */
409 SRB_Status = SS_COMP;
410 lpPRB->SRB_TargStat = 0;
411 FIXME("Program wants to do DVD Region switching, but fails (non compliant DVD drive). Ignoring....\n");
416 ASPI_DebugPrintResult(lpPRB);
418 return WNASPI32_DoPosting( lpPRB, SRB_Status );
419 /* In real WNASPI32 stuff really is always pending because ASPI does things
420 in the background, but we are not doing that (yet) */
424 if (error_code == EBUSY) {
425 WNASPI32_DoPosting( lpPRB, SS_ASPI_IS_BUSY );
426 TRACE("Device busy\n");
430 /* I'm not sure exactly error codes work here
431 * We probably should set lpPRB->SRB_TargStat, SRB_HaStat ?
433 WARN("error_exit\n");
434 HeapFree(GetProcessHeap(), 0, sg_reply_hdr);
435 HeapFree(GetProcessHeap(), 0, sg_hd);
436 WNASPI32_DoPosting( lpPRB, SRB_Status );
440 #endif /* defined(linux) */
443 /*******************************************************************
444 * GetASPI32SupportInfo [WNASPI32.0]
446 * Checks if the ASPI subsystem is initialized correctly.
450 * HIBYTE of LOWORD: status (SS_COMP or SS_FAILED_INIT)
451 * LOBYTE of LOWORD: # of host adapters.
453 DWORD WINAPI GetASPI32SupportInfo()
455 return ((SS_COMP << 8) | ASPI_GetNumControllers());
459 /***********************************************************************
460 * SendASPI32Command (WNASPI32.1)
462 DWORD __cdecl SendASPI32Command(LPSRB lpSRB)
465 switch (lpSRB->common.SRB_Cmd) {
467 lpSRB->inquiry.SRB_Status = SS_COMP; /* completed successfully */
468 lpSRB->inquiry.HA_Count = 1; /* not always */
469 lpSRB->inquiry.HA_SCSI_ID = 7; /* not always ID 7 */
470 strcpy(lpSRB->inquiry.HA_ManagerId, "ASPI for WIN32"); /* max 15 chars, don't change */
471 strcpy(lpSRB->inquiry.HA_Identifier, "Wine host"); /* FIXME: return host adapter name */
472 memset(lpSRB->inquiry.HA_Unique, 0, 16); /* default HA_Unique content */
473 lpSRB->inquiry.HA_Unique[6] = 0x02; /* Maximum Transfer Length (128K, Byte> 4-7) */
474 lpSRB->inquiry.HA_Unique[3] = 0x08; /* Maximum number of SCSI targets */
475 FIXME("ASPI: Partially implemented SC_HA_INQUIRY for adapter %d.\n", lpSRB->inquiry.SRB_HaId);
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 */
485 memset(&tmpsrb,0,sizeof(tmpsrb));
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 */
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);
515 FIXME("Not implemented SC_ABORT_SRB\n");
518 FIXME("Not implemented SC_RESET_DEV\n");
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");
527 FIXME("Unknown command %d\n", lpSRB->common.SRB_Cmd);
529 return SS_INVALID_SRB;
531 return SS_INVALID_SRB;
536 /***********************************************************************
537 * GetASPI32DLLVersion (WNASPI32.3)
540 DWORD WINAPI GetASPI32DLLVersion()
543 TRACE("Returning version 1\n");
546 FIXME("Please add SCSI support for your operating system, returning 0\n");
551 BOOL __cdecl GetASPI32Buffer(/*PASPI32BUFF*/LPVOID pab)
553 FIXME("(%p), stub !\n", pab);
557 BOOL __cdecl FreeASPI32Buffer(/*PASPI32BUFF*/LPVOID pab)
559 FIXME("(%p), stub !\n", pab);
563 BOOL __cdecl TranslateASPI32Address(LPDWORD pdwPath, LPDWORD pdwDEVNODE)
565 FIXME("(%p, %p), stub !\n", pdwPath, pdwDEVNODE);