18 #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) */
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]) {
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];
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)
193 cdb = &prb->CDBByte[16];
194 DPRINTF("SenseArea[");
195 for (i = 0; i < prb->SRB_SenseLen; i++) {
197 DPRINTF("%02x", *cdb++);
204 ASPI_DebugPrintResult(SRB_ExecSCSICmd *prb)
207 TRACE("SRB_Status: %x\n", prb->SRB_Status);
208 TRACE("SRB_HaStat: %x\n", prb->SRB_HaStat);
209 TRACE("SRB_TargStat: %x\n", prb->SRB_TargStat);
210 switch (prb->CDBByte[0]) {
212 TRACE("Vendor: '%s'\n", prb->SRB_BufPointer + INQUIRY_VENDOR);
214 case CMD_TEST_UNIT_READY:
215 ASPI_PrintSenseArea(prb);
220 /* Posting must be done in such a way that as soon as the SRB_Status is set
221 * we don't touch the SRB anymore because it could possibly be freed
222 * if the app is doing ASPI polling
225 WNASPI32_DoPosting( SRB_ExecSCSICmd *lpPRB, DWORD status )
227 void (*SRB_PostProc)() = lpPRB->SRB_PostProc;
228 BYTE SRB_Flags = lpPRB->SRB_Flags;
229 if( status == SS_PENDING )
231 WARN("Tried posting SS_PENDING\n");
234 lpPRB->SRB_Status = status;
235 /* lpPRB is NOT safe, it could be freed in another thread */
241 TRACE("Post Routine (%lx) called\n", (DWORD) SRB_PostProc);
242 /* Even though lpPRB could have been freed by
243 * the program.. that's unlikely if it planned
244 * to use it in the PostProc
246 (*SRB_PostProc)(lpPRB);
248 else if (SRB_Flags & SRB_EVENT_NOTIFY) {
249 TRACE("Setting event %04x\n", (HANDLE)SRB_PostProc);
250 SetEvent((HANDLE)SRB_PostProc);
257 ASPI_ExecScsiCmd(SRB_ExecSCSICmd *lpPRB)
259 struct sg_header *sg_hd, *sg_reply_hdr;
266 ASPI_DebugPrintCmd(lpPRB);
268 fd = ASPI_OpenDevice(lpPRB);
270 ERR("Failed: could not open device c%01dt%01dd%01d. Device permissions !?\n",
271 lpPRB->SRB_HaId,lpPRB->SRB_Target,lpPRB->SRB_Lun);
272 return WNASPI32_DoPosting( lpPRB, SS_NO_DEVICE );
278 lpPRB->SRB_Status = SS_PENDING;
280 if (!lpPRB->SRB_CDBLen) {
281 WARN("Failed: lpPRB->SRB_CDBLen = 0.\n");
282 return WNASPI32_DoPosting( lpPRB, SS_INVALID_SRB );
285 /* build up sg_header + scsi cmd */
286 if (HOST_TO_TARGET(lpPRB)) {
287 /* send header, command, and then data */
288 in_len = SCSI_OFF + lpPRB->SRB_CDBLen + lpPRB->SRB_BufLen;
289 sg_hd = (struct sg_header *) HeapAlloc(GetProcessHeap(), 0, in_len);
290 memset(sg_hd, 0, SCSI_OFF);
291 memcpy(sg_hd + 1, &lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
292 if (lpPRB->SRB_BufLen) {
293 memcpy(((BYTE *) sg_hd) + SCSI_OFF + lpPRB->SRB_CDBLen, lpPRB->SRB_BufPointer, lpPRB->SRB_BufLen);
297 /* send header and command - no data */
298 in_len = SCSI_OFF + lpPRB->SRB_CDBLen;
299 sg_hd = (struct sg_header *) HeapAlloc(GetProcessHeap(), 0, in_len);
300 memset(sg_hd, 0, SCSI_OFF);
301 memcpy(sg_hd + 1, &lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
304 if (TARGET_TO_HOST(lpPRB)) {
305 out_len = SCSI_OFF + lpPRB->SRB_BufLen;
306 sg_reply_hdr = (struct sg_header *) HeapAlloc(GetProcessHeap(), 0, out_len);
307 memset(sg_reply_hdr, 0, SCSI_OFF);
308 sg_hd->reply_len = out_len;
312 sg_reply_hdr = (struct sg_header *) HeapAlloc(GetProcessHeap(), 0, out_len);
313 memset(sg_reply_hdr, 0, SCSI_OFF);
314 sg_hd->reply_len = out_len;
317 if(!SCSI_LinuxDeviceIo( fd,
319 sg_reply_hdr, out_len,
325 if (sg_reply_hdr->result != 0) {
326 error_code = sg_reply_hdr->result;
327 WARN("reply header error (%d)\n", sg_reply_hdr->result);
331 if (TARGET_TO_HOST(lpPRB) && lpPRB->SRB_BufLen) {
332 memcpy(lpPRB->SRB_BufPointer, sg_reply_hdr + 1, lpPRB->SRB_BufLen);
335 /* copy in sense buffer to amount that is available in client */
336 if (lpPRB->SRB_SenseLen) {
337 int sense_len = lpPRB->SRB_SenseLen;
338 if (lpPRB->SRB_SenseLen > 16)
341 /* CDB is fixed in WNASPI32 */
342 memcpy(&lpPRB->CDBByte[16], &sg_reply_hdr->sense_buffer[0], sense_len);
344 TRACE("CDB is %d bytes long\n", lpPRB->SRB_CDBLen );
345 ASPI_PrintSenseArea(lpPRB);
348 SRB_Status = SS_COMP;
349 lpPRB->SRB_HaStat = HASTAT_OK;
350 lpPRB->SRB_TargStat = sg_reply_hdr->target_status << 1;
352 HeapFree(GetProcessHeap(), 0, sg_reply_hdr);
353 HeapFree(GetProcessHeap(), 0, sg_hd);
355 /* FIXME: Should this be != 0 maybe? */
356 if( lpPRB->SRB_TargStat == 2 )
359 ASPI_DebugPrintResult(lpPRB);
361 return WNASPI32_DoPosting( lpPRB, SRB_Status );
362 /* In real WNASPI32 stuff really is always pending because ASPI does things
363 in the background, but we are not doing that (yet) */
367 if (error_code == EBUSY) {
368 WNASPI32_DoPosting( lpPRB, SS_ASPI_IS_BUSY );
369 TRACE("Device busy\n");
375 /* I'm not sure exactly error codes work here
376 * We probably should set lpPRB->SRB_TargStat, SRB_HaStat ?
378 WARN("error_exit\n");
379 HeapFree(GetProcessHeap(), 0, sg_reply_hdr);
380 HeapFree(GetProcessHeap(), 0, sg_hd);
381 WNASPI32_DoPosting( lpPRB, SRB_Status );
385 #endif /* defined(linux) */
388 /*******************************************************************
389 * GetASPI32SupportInfo32 [WNASPI32.0]
391 * Checks if the ASPI subsystem is initialized correctly.
395 * HIBYTE of LOWORD: status (SS_COMP or SS_FAILED_INIT)
396 * LOBYTE of LOWORD: # of host adapters.
398 DWORD WINAPI GetASPI32SupportInfo()
400 return ((SS_COMP << 8) | 1); /* FIXME: get # of host adapters installed */
404 /***********************************************************************
405 * SendASPI32Command32 (WNASPI32.1)
407 DWORD __cdecl SendASPI32Command(LPSRB lpSRB)
410 switch (lpSRB->common.SRB_Cmd) {
412 lpSRB->inquiry.SRB_Status = SS_COMP; /* completed successfully */
413 lpSRB->inquiry.HA_Count = 1; /* not always */
414 lpSRB->inquiry.HA_SCSI_ID = 7; /* not always ID 7 */
415 strcat(lpSRB->inquiry.HA_ManagerId, "ASPI for WIN32"); /* max 15 chars, don't change */
416 strcat(lpSRB->inquiry.HA_Identifier, "Wine host"); /* FIXME: return host 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);
421 case SC_GET_DEV_TYPE: {
422 /* FIXME: We should return SS_NO_DEVICE if the device is not configured */
423 /* FIXME: We should return SS_INVALID_HA if HostAdapter!=0 */
427 memset(&tmpsrb,0,sizeof(tmpsrb));
429 tmpsrb.cmd.SRB_Cmd = SC_EXEC_SCSI_CMD;
430 #define X(x) tmpsrb.cmd.SRB_##x = lpSRB->devtype.SRB_##x
431 X(Status);X(HaId);X(Flags);X(Target);X(Lun);
433 tmpsrb.cmd.SRB_BufLen = sizeof(inqbuf);
434 tmpsrb.cmd.SRB_Flags = 8;/*target->host data. FIXME: anything more?*/
435 tmpsrb.cmd.SRB_BufPointer = inqbuf;
436 tmpsrb.cmd.CDBByte[0] = 0x12; /* INQUIRY */
437 tmpsrb.cmd.CDBByte[4] = sizeof(inqbuf);
438 tmpsrb.cmd.SRB_CDBLen = 6;
439 ASPI_ExecScsiCmd(&tmpsrb.cmd);
440 #define X(x) lpSRB->devtype.SRB_##x = tmpsrb.cmd.SRB_##x
443 lpSRB->devtype.SRB_DeviceType = inqbuf[0]&0x1f;
444 TRACE("returning devicetype %d for target %d\n",inqbuf[0]&0x1f,tmpsrb.cmd.SRB_Target);
447 case SC_EXEC_SCSI_CMD:
448 return ASPI_ExecScsiCmd(&lpSRB->cmd);
451 FIXME("Not implemented SC_ABORT_SRB\n");
454 FIXME("Not implemented SC_RESET_DEV\n");
456 #ifdef SC_GET_DISK_INFO
457 case SC_GET_DISK_INFO:
458 /* NT Doesn't implement this either.. so don't feel bad */
459 WARN("Not implemented SC_GET_DISK_INFO\n");
463 WARN("Unknown command %d\n", lpSRB->common.SRB_Cmd);
465 return SS_INVALID_SRB;
467 return SS_INVALID_SRB;
472 /***********************************************************************
473 * GetASPI32DLLVersion32 (WNASPI32.3)
476 DWORD WINAPI GetASPI32DLLVersion()