blob: 5b8c203e5e9308004f27522ae10c592f6b538906 [file] [log] [blame]
Patrik Stridvall96336321999-10-24 22:13:47 +00001#include "config.h"
2
Marcus Meissner8cd83a92000-03-24 19:45:47 +00003#include <assert.h>
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00004#include <stdlib.h>
Alexandre Julliard383da682000-02-10 22:15:21 +00005#include <stdio.h>
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00006#include <sys/types.h>
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00007#include <errno.h>
8#include <fcntl.h>
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00009#include <memory.h>
10#include <unistd.h>
Marcus Meissner317af321999-02-17 13:51:06 +000011
12#include "winbase.h"
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000013#include "aspi.h"
14#include "wnaspi32.h"
David Elliottc3bcd6c2000-03-08 19:41:49 +000015#include "winescsi.h"
Alexandre Julliarda099a551999-06-12 15:45:58 +000016#include "debugtools.h"
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000017
Alexandre Julliard383da682000-02-10 22:15:21 +000018DEFAULT_DEBUG_CHANNEL(aspi);
Patrik Stridvallb4b9fae1999-04-19 14:56:29 +000019
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000020/* FIXME!
21 * 1) Residual byte length reporting not handled
22 * 2) Make this code re-entrant for multithreading
David Elliott8b8a3432000-01-08 22:25:02 +000023 * -- Added CriticalSection to OpenDevices function
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000024 * 3) Only linux supported so far
David Elliott8b8a3432000-01-08 22:25:02 +000025 * 4) Leaves sg devices open. This may or may not be okay. A better solution
26 * would be to close the file descriptors when the thread/process using
27 * them no longer needs them.
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000028 */
29
30#ifdef linux
Patrik Stridvall1ed4ecf1999-06-26 14:58:24 +000031
32static ASPI_DEVICE_INFO *ASPI_open_devices = NULL;
Alexandre Julliard301df6b2001-08-16 18:12:56 +000033static CRITICAL_SECTION ASPI_CritSection = CRITICAL_SECTION_INIT("ASPI_CritSection");
David Elliott8b8a3432000-01-08 22:25:02 +000034
Patrik Stridvall7b0a0882000-01-09 21:42:42 +000035#endif /* defined(linux) */
36
37
David Elliott8b8a3432000-01-08 22:25:02 +000038BOOL WINAPI WNASPI32_LibMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID fImpLoad)
39{
Patrik Stridvall7b0a0882000-01-09 21:42:42 +000040#ifdef linux
David Elliott8b8a3432000-01-08 22:25:02 +000041 switch( fdwReason )
42 {
43 case DLL_PROCESS_ATTACH:
Alexandre Julliard06187462001-01-17 22:03:18 +000044 SCSI_Init();
45 break;
David Elliott8b8a3432000-01-08 22:25:02 +000046 case DLL_PROCESS_DETACH:
Alexandre Julliard06187462001-01-17 22:03:18 +000047 DeleteCriticalSection( &ASPI_CritSection );
48 break;
David Elliott8b8a3432000-01-08 22:25:02 +000049 case DLL_THREAD_ATTACH:
50 case DLL_THREAD_DETACH:
51 break;
52 }
Patrik Stridvall7b0a0882000-01-09 21:42:42 +000053#endif /* defined(linux) */
Alexandre Julliard06187462001-01-17 22:03:18 +000054 return TRUE;
David Elliott8b8a3432000-01-08 22:25:02 +000055}
Patrik Stridvall1ed4ecf1999-06-26 14:58:24 +000056
Marcus Meissner8cd83a92000-03-24 19:45:47 +000057
Patrik Stridvall7b0a0882000-01-09 21:42:42 +000058#ifdef linux
59
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000060static int
Alexandre Julliarda3960291999-02-26 11:11:13 +000061ASPI_OpenDevice(SRB_ExecSCSICmd *prb)
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000062{
63 int fd;
David Elliotta555ceb2000-03-09 18:44:02 +000064 DWORD hc;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000065 ASPI_DEVICE_INFO *curr;
66
67 /* search list of devices to see if we've opened it already.
68 * There is not an explicit open/close in ASPI land, so hopefully
69 * keeping a device open won't be a problem.
70 */
71
David Elliott8b8a3432000-01-08 22:25:02 +000072 EnterCriticalSection(&ASPI_CritSection);
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000073 for (curr = ASPI_open_devices; curr; curr = curr->next) {
74 if (curr->hostId == prb->SRB_HaId &&
75 curr->target == prb->SRB_Target &&
76 curr->lun == prb->SRB_Lun) {
David Elliott8b8a3432000-01-08 22:25:02 +000077 LeaveCriticalSection(&ASPI_CritSection);
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000078 return curr->fd;
79 }
80 }
David Elliott8b8a3432000-01-08 22:25:02 +000081 LeaveCriticalSection(&ASPI_CritSection);
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000082
Marcus Meissner485c3ef2001-06-06 20:19:07 +000083 if (prb->SRB_HaId > ASPI_GetNumControllers())
84 return -1;
85
David Elliotta555ceb2000-03-09 18:44:02 +000086 hc = ASPI_GetHCforController( prb->SRB_HaId );
87 fd = SCSI_OpenDevice( HIWORD(hc), LOWORD(hc), prb->SRB_Target, prb->SRB_Lun);
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000088
Marcus Meissner485c3ef2001-06-06 20:19:07 +000089 if (fd == -1)
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000090 return -1;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000091
92 /* device is now open */
David Elliotta555ceb2000-03-09 18:44:02 +000093 /* FIXME: Let users specify SCSI timeout in registry */
94 SCSI_LinuxSetTimeout( fd, SCSI_DEFAULT_TIMEOUT );
Patrik Stridvall3c0211f2001-09-11 00:32:32 +000095
Alexandre Julliard90476d62000-02-16 22:47:24 +000096 curr = HeapAlloc( GetProcessHeap(), 0, sizeof(ASPI_DEVICE_INFO) );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000097 curr->fd = fd;
98 curr->hostId = prb->SRB_HaId;
99 curr->target = prb->SRB_Target;
100 curr->lun = prb->SRB_Lun;
101
102 /* insert new record at beginning of open device list */
David Elliott8b8a3432000-01-08 22:25:02 +0000103 EnterCriticalSection(&ASPI_CritSection);
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000104 curr->next = ASPI_open_devices;
105 ASPI_open_devices = curr;
David Elliott8b8a3432000-01-08 22:25:02 +0000106 LeaveCriticalSection(&ASPI_CritSection);
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000107 return fd;
108}
109
110
111static void
Alexandre Julliarda3960291999-02-26 11:11:13 +0000112ASPI_DebugPrintCmd(SRB_ExecSCSICmd *prb)
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000113{
114 BYTE cmd;
115 int i;
116 BYTE *cdb;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000117
118 switch (prb->CDBByte[0]) {
119 case CMD_INQUIRY:
Marcus Meissner8cd83a92000-03-24 19:45:47 +0000120 TRACE("INQUIRY {\n");
Alexandre Julliarda099a551999-06-12 15:45:58 +0000121 TRACE("\tEVPD: %d\n", prb->CDBByte[1] & 1);
122 TRACE("\tLUN: %d\n", (prb->CDBByte[1] & 0xc) >> 1);
123 TRACE("\tPAGE CODE: %d\n", prb->CDBByte[2]);
124 TRACE("\tALLOCATION LENGTH: %d\n", prb->CDBByte[4]);
125 TRACE("\tCONTROL: %d\n", prb->CDBByte[5]);
126 TRACE("}\n");
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000127 break;
128 case CMD_SCAN_SCAN:
Alexandre Julliarda099a551999-06-12 15:45:58 +0000129 TRACE("Transfer Length: %d\n", prb->CDBByte[4]);
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000130 break;
131 }
132
Alexandre Julliarda099a551999-06-12 15:45:58 +0000133 TRACE("Host Adapter: %d\n", prb->SRB_HaId);
134 TRACE("Flags: %d\n", prb->SRB_Flags);
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000135 if (TARGET_TO_HOST(prb)) {
Alexandre Julliarda099a551999-06-12 15:45:58 +0000136 TRACE("\tData transfer: Target to host. Length checked.\n");
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000137 }
138 else if (HOST_TO_TARGET(prb)) {
Alexandre Julliarda099a551999-06-12 15:45:58 +0000139 TRACE("\tData transfer: Host to target. Length checked.\n");
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000140 }
141 else if (NO_DATA_TRANSFERED(prb)) {
Alexandre Julliarda099a551999-06-12 15:45:58 +0000142 TRACE("\tData transfer: none\n");
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000143 }
144 else {
Alexandre Julliarda099a551999-06-12 15:45:58 +0000145 WARN("\tTransfer by scsi cmd. Length not checked.\n");
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000146 }
147
Alexandre Julliarda099a551999-06-12 15:45:58 +0000148 TRACE("\tResidual byte length reporting %s\n", prb->SRB_Flags & 0x4 ? "enabled" : "disabled");
149 TRACE("\tLinking %s\n", prb->SRB_Flags & 0x2 ? "enabled" : "disabled");
150 TRACE("\tPosting %s\n", prb->SRB_Flags & 0x1 ? "enabled" : "disabled");
151 TRACE("Target: %d\n", prb->SRB_Target);
152 TRACE("Lun: %d\n", prb->SRB_Lun);
153 TRACE("BufLen: %ld\n", prb->SRB_BufLen);
154 TRACE("SenseLen: %d\n", prb->SRB_SenseLen);
155 TRACE("BufPtr: %p\n", prb->SRB_BufPointer);
156 TRACE("CDB Length: %d\n", prb->SRB_CDBLen);
157 TRACE("POST Proc: %lx\n", (DWORD) prb->SRB_PostProc);
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000158 cdb = &prb->CDBByte[0];
159 cmd = prb->CDBByte[0];
Marcus Meissner8cd83a92000-03-24 19:45:47 +0000160 if (TRACE_ON(aspi)) {
Alexandre Julliard15de6151999-08-04 12:22:42 +0000161 DPRINTF("CDB buffer[");
162 for (i = 0; i < prb->SRB_CDBLen; i++) {
163 if (i != 0) DPRINTF(",");
164 DPRINTF("%02x", *cdb++);
165 }
166 DPRINTF("]\n");
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000167 }
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000168}
169
170static void
Andreas Mohr482122a2000-12-09 03:06:18 +0000171ASPI_PrintCDBArea(SRB_ExecSCSICmd *prb)
172{
173 if (TRACE_ON(aspi))
174 {
175 int i;
176 DPRINTF("CDB[");
177 for (i = 0; i < prb->SRB_CDBLen; i++) {
178 if (i) DPRINTF(",");
179 DPRINTF("%02x", prb->CDBByte[i]);
180 }
181 DPRINTF("]\n");
182 }
183}
184
185static void
Alexandre Julliarda3960291999-02-26 11:11:13 +0000186ASPI_PrintSenseArea(SRB_ExecSCSICmd *prb)
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000187{
188 int i;
Alexandre Julliardab170a92000-09-27 00:25:24 +0000189 BYTE *rqbuf = prb->SenseArea;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000190
Alexandre Julliard15de6151999-08-04 12:22:42 +0000191 if (TRACE_ON(aspi))
192 {
Marcus Meissner8cd83a92000-03-24 19:45:47 +0000193 DPRINTF("Request Sense reports:\n");
194 if ((rqbuf[0]&0x7f)!=0x70) {
Andreas Mohr4d47ec22000-09-06 19:42:19 +0000195 DPRINTF("\tInvalid sense header: 0x%02x instead of 0x70\n", rqbuf[0]&0x7f);
Marcus Meissner8cd83a92000-03-24 19:45:47 +0000196 return;
197 }
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");
Andreas Mohr4d47ec22000-09-06 19:42:19 +0000200 DPRINTF("\tIncorrect blocklength: %s\n",(rqbuf[2]&0x20)?"yes":"no");
Marcus Meissner8cd83a92000-03-24 19:45:47 +0000201 DPRINTF("\tSense Key: %d\n",rqbuf[2]&0xf);
202 if (rqbuf[0]&0x80)
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");
209 if (rqbuf[15]&0x8) {
210 DPRINTF("Pointer at %d, bit %d\n",rqbuf[16]*256+rqbuf[17],rqbuf[15]&0x7);
211 }
212 }
Alexandre Julliard15de6151999-08-04 12:22:42 +0000213 DPRINTF("SenseArea[");
214 for (i = 0; i < prb->SRB_SenseLen; i++) {
Marcus Meissner8cd83a92000-03-24 19:45:47 +0000215 if (i) DPRINTF(",");
216 DPRINTF("%02x", *rqbuf++);
Alexandre Julliard15de6151999-08-04 12:22:42 +0000217 }
218 DPRINTF("]\n");
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000219 }
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000220}
221
222static void
Alexandre Julliarda3960291999-02-26 11:11:13 +0000223ASPI_DebugPrintResult(SRB_ExecSCSICmd *prb)
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000224{
225
David Elliott8b8a3432000-01-08 22:25:02 +0000226 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);
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000229 switch (prb->CDBByte[0]) {
230 case CMD_INQUIRY:
Alexandre Julliarda099a551999-06-12 15:45:58 +0000231 TRACE("Vendor: '%s'\n", prb->SRB_BufPointer + INQUIRY_VENDOR);
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000232 break;
233 case CMD_TEST_UNIT_READY:
Alexandre Julliarda3960291999-02-26 11:11:13 +0000234 ASPI_PrintSenseArea(prb);
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000235 break;
236 }
237}
238
David Elliotta555ceb2000-03-09 18:44:02 +0000239/* 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
242 */
243static DWORD
244WNASPI32_DoPosting( SRB_ExecSCSICmd *lpPRB, DWORD status )
245{
246 void (*SRB_PostProc)() = lpPRB->SRB_PostProc;
247 BYTE SRB_Flags = lpPRB->SRB_Flags;
248 if( status == SS_PENDING )
249 {
250 WARN("Tried posting SS_PENDING\n");
251 return SS_PENDING;
252 }
253 lpPRB->SRB_Status = status;
254 /* lpPRB is NOT safe, it could be freed in another thread */
255
256 if (SRB_PostProc)
257 {
258 if (SRB_Flags & 0x1)
259 {
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
264 */
265 (*SRB_PostProc)(lpPRB);
266 }
267 else if (SRB_Flags & SRB_EVENT_NOTIFY) {
268 TRACE("Setting event %04x\n", (HANDLE)SRB_PostProc);
269 SetEvent((HANDLE)SRB_PostProc);
270 }
271 }
272 return SS_PENDING;
273}
274
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000275static WORD
Alexandre Julliarda3960291999-02-26 11:11:13 +0000276ASPI_ExecScsiCmd(SRB_ExecSCSICmd *lpPRB)
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000277{
278 struct sg_header *sg_hd, *sg_reply_hdr;
Marcus Meissner485c3ef2001-06-06 20:19:07 +0000279 WORD ret;
David Elliotta555ceb2000-03-09 18:44:02 +0000280 DWORD status;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000281 int in_len, out_len;
282 int error_code = 0;
283 int fd;
David Elliotta555ceb2000-03-09 18:44:02 +0000284 DWORD SRB_Status;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000285
Marcus Meissner8cd83a92000-03-24 19:45:47 +0000286 /* 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); \
Marcus Meissner485c3ef2001-06-06 20:19:07 +0000290 lpPRB->SRB_Flags |= 0x08; \
Marcus Meissner8cd83a92000-03-24 19:45:47 +0000291 }
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; \
296 }
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
311 ) {
Patrik Stridvall3c0211f2001-09-11 00:32:32 +0000312 FIXME("command 0x%02x, no data transfer specified, but buflen is %ld!!!\n",lpPRB->CDBByte[0],lpPRB->SRB_BufLen);
Marcus Meissner8cd83a92000-03-24 19:45:47 +0000313 }
314 break;
315 }
Alexandre Julliarda3960291999-02-26 11:11:13 +0000316 ASPI_DebugPrintCmd(lpPRB);
Marcus Meissner485c3ef2001-06-06 20:19:07 +0000317 if (lpPRB->SRB_HaId > ASPI_GetNumControllers()) {
318 ERR("Failed: Wanted hostadapter %d, but we have only %d.\n",
319 lpPRB->SRB_HaId,ASPI_GetNumControllers()
320 );
321 return WNASPI32_DoPosting( lpPRB, SS_INVALID_HA );
322 }
Alexandre Julliarda3960291999-02-26 11:11:13 +0000323 fd = ASPI_OpenDevice(lpPRB);
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000324 if (fd == -1) {
David Elliotta555ceb2000-03-09 18:44:02 +0000325 return WNASPI32_DoPosting( lpPRB, SS_NO_DEVICE );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000326 }
327
328 sg_hd = NULL;
329 sg_reply_hdr = NULL;
330
331 lpPRB->SRB_Status = SS_PENDING;
332
333 if (!lpPRB->SRB_CDBLen) {
Marcus Meissner8cd83a92000-03-24 19:45:47 +0000334 ERR("Failed: lpPRB->SRB_CDBLen = 0.\n");
David Elliotta555ceb2000-03-09 18:44:02 +0000335 return WNASPI32_DoPosting( lpPRB, SS_INVALID_SRB );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000336 }
337
338 /* build up sg_header + scsi cmd */
339 if (HOST_TO_TARGET(lpPRB)) {
340 /* send header, command, and then data */
341 in_len = SCSI_OFF + lpPRB->SRB_CDBLen + lpPRB->SRB_BufLen;
David Elliott8b8a3432000-01-08 22:25:02 +0000342 sg_hd = (struct sg_header *) HeapAlloc(GetProcessHeap(), 0, in_len);
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000343 memset(sg_hd, 0, SCSI_OFF);
344 memcpy(sg_hd + 1, &lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
345 if (lpPRB->SRB_BufLen) {
346 memcpy(((BYTE *) sg_hd) + SCSI_OFF + lpPRB->SRB_CDBLen, lpPRB->SRB_BufPointer, lpPRB->SRB_BufLen);
347 }
348 }
349 else {
350 /* send header and command - no data */
351 in_len = SCSI_OFF + lpPRB->SRB_CDBLen;
David Elliott8b8a3432000-01-08 22:25:02 +0000352 sg_hd = (struct sg_header *) HeapAlloc(GetProcessHeap(), 0, in_len);
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000353 memset(sg_hd, 0, SCSI_OFF);
354 memcpy(sg_hd + 1, &lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
355 }
356
357 if (TARGET_TO_HOST(lpPRB)) {
358 out_len = SCSI_OFF + lpPRB->SRB_BufLen;
David Elliott8b8a3432000-01-08 22:25:02 +0000359 sg_reply_hdr = (struct sg_header *) HeapAlloc(GetProcessHeap(), 0, out_len);
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000360 memset(sg_reply_hdr, 0, SCSI_OFF);
361 sg_hd->reply_len = out_len;
362 }
363 else {
364 out_len = SCSI_OFF;
David Elliott8b8a3432000-01-08 22:25:02 +0000365 sg_reply_hdr = (struct sg_header *) HeapAlloc(GetProcessHeap(), 0, out_len);
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000366 memset(sg_reply_hdr, 0, SCSI_OFF);
367 sg_hd->reply_len = out_len;
368 }
369
David Elliott8466fc32000-07-15 15:15:31 +0000370 SCSI_Fix_CMD_LEN(fd, lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
371
David Elliotta555ceb2000-03-09 18:44:02 +0000372 if(!SCSI_LinuxDeviceIo( fd,
373 sg_hd, in_len,
374 sg_reply_hdr, out_len,
375 &status) )
376 {
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000377 goto error_exit;
378 }
379
380 if (sg_reply_hdr->result != 0) {
381 error_code = sg_reply_hdr->result;
Alexandre Julliarda099a551999-06-12 15:45:58 +0000382 WARN("reply header error (%d)\n", sg_reply_hdr->result);
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000383 goto error_exit;
384 }
385
386 if (TARGET_TO_HOST(lpPRB) && lpPRB->SRB_BufLen) {
387 memcpy(lpPRB->SRB_BufPointer, sg_reply_hdr + 1, lpPRB->SRB_BufLen);
388 }
389
390 /* copy in sense buffer to amount that is available in client */
391 if (lpPRB->SRB_SenseLen) {
392 int sense_len = lpPRB->SRB_SenseLen;
393 if (lpPRB->SRB_SenseLen > 16)
394 sense_len = 16;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000395
David Elliott8b8a3432000-01-08 22:25:02 +0000396 /* CDB is fixed in WNASPI32 */
Alexandre Julliardab170a92000-09-27 00:25:24 +0000397 memcpy(lpPRB->SenseArea, &sg_reply_hdr->sense_buffer[0], sense_len);
David Elliott8b8a3432000-01-08 22:25:02 +0000398
Andreas Mohr482122a2000-12-09 03:06:18 +0000399 ASPI_PrintCDBArea(lpPRB);
David Elliott8b8a3432000-01-08 22:25:02 +0000400 ASPI_PrintSenseArea(lpPRB);
401 }
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000402
David Elliotta555ceb2000-03-09 18:44:02 +0000403 SRB_Status = SS_COMP;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000404 lpPRB->SRB_HaStat = HASTAT_OK;
David Elliott8b8a3432000-01-08 22:25:02 +0000405 lpPRB->SRB_TargStat = sg_reply_hdr->target_status << 1;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000406
David Elliotta555ceb2000-03-09 18:44:02 +0000407 HeapFree(GetProcessHeap(), 0, sg_reply_hdr);
408 HeapFree(GetProcessHeap(), 0, sg_hd);
409
David Elliott8b8a3432000-01-08 22:25:02 +0000410 /* FIXME: Should this be != 0 maybe? */
Marcus Meissner8cd83a92000-03-24 19:45:47 +0000411 if( lpPRB->SRB_TargStat == 2 ) {
David Elliotta555ceb2000-03-09 18:44:02 +0000412 SRB_Status = SS_ERR;
Marcus Meissner8cd83a92000-03-24 19:45:47 +0000413 switch (lpPRB->CDBByte[0]) {
414 case 0xa4: /* REPORT_KEY (DVD) MMC-2 */
415 case 0xa3: /* SEND KEY (DVD) MMC-2 */
416 SRB_Status = SS_COMP;
417 lpPRB->SRB_TargStat = 0;
418 FIXME("Program wants to do DVD Region switching, but fails (non compliant DVD drive). Ignoring....\n");
419 break;
420 }
421 }
David Elliott8b8a3432000-01-08 22:25:02 +0000422
423 ASPI_DebugPrintResult(lpPRB);
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000424 /* now do posting */
Marcus Meissner485c3ef2001-06-06 20:19:07 +0000425 ret = WNASPI32_DoPosting( lpPRB, SRB_Status );
426
427 switch (lpPRB->CDBByte[0]) {
428 case CMD_INQUIRY:
429 if (SRB_Status == SS_COMP)
430 return SS_COMP; /* some junk expects ss_comp here. */
431 /*FALLTHROUGH*/
432 default:
Patrik Stridvall3c0211f2001-09-11 00:32:32 +0000433 break;
Marcus Meissner485c3ef2001-06-06 20:19:07 +0000434 }
435
David Elliott8b8a3432000-01-08 22:25:02 +0000436 /* In real WNASPI32 stuff really is always pending because ASPI does things
437 in the background, but we are not doing that (yet) */
Marcus Meissner485c3ef2001-06-06 20:19:07 +0000438
439 return ret;
Patrik Stridvall3c0211f2001-09-11 00:32:32 +0000440
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000441error_exit:
David Elliotta555ceb2000-03-09 18:44:02 +0000442 SRB_Status = SS_ERR;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000443 if (error_code == EBUSY) {
David Elliotta555ceb2000-03-09 18:44:02 +0000444 WNASPI32_DoPosting( lpPRB, SS_ASPI_IS_BUSY );
Alexandre Julliarda099a551999-06-12 15:45:58 +0000445 TRACE("Device busy\n");
Marcus Meissner8cd83a92000-03-24 19:45:47 +0000446 } else
447 FIXME("Failed\n");
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000448
449 /* I'm not sure exactly error codes work here
450 * We probably should set lpPRB->SRB_TargStat, SRB_HaStat ?
451 */
Alexandre Julliarda099a551999-06-12 15:45:58 +0000452 WARN("error_exit\n");
David Elliott8b8a3432000-01-08 22:25:02 +0000453 HeapFree(GetProcessHeap(), 0, sg_reply_hdr);
454 HeapFree(GetProcessHeap(), 0, sg_hd);
David Elliotta555ceb2000-03-09 18:44:02 +0000455 WNASPI32_DoPosting( lpPRB, SRB_Status );
456 return SS_PENDING;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000457}
Patrik Stridvall7b0a0882000-01-09 21:42:42 +0000458
459#endif /* defined(linux) */
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000460
461
462/*******************************************************************
Patrik Stridvall8b216b32001-06-19 18:20:47 +0000463 * GetASPI32SupportInfo [WNASPI32.1]
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000464 *
465 * Checks if the ASPI subsystem is initialized correctly.
466 *
467 * RETURNS
468 * HIWORD: 0.
469 * HIBYTE of LOWORD: status (SS_COMP or SS_FAILED_INIT)
Patrik Stridvall3c0211f2001-09-11 00:32:32 +0000470 * LOBYTE of LOWORD: # of host adapters.
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000471 */
Patrik Stridvallbf8943c2000-10-02 03:39:34 +0000472DWORD __cdecl GetASPI32SupportInfo(void)
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000473{
Marcus Meissner485c3ef2001-06-06 20:19:07 +0000474 DWORD controllers = ASPI_GetNumControllers();
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000475
Marcus Meissner485c3ef2001-06-06 20:19:07 +0000476 if (!controllers)
477 return SS_NO_ADAPTERS << 8;
478 return (SS_COMP << 8) | controllers ;
479}
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000480
481/***********************************************************************
Patrik Stridvall8b216b32001-06-19 18:20:47 +0000482 * SendASPI32Command (WNASPI32.2)
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000483 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000484DWORD __cdecl SendASPI32Command(LPSRB lpSRB)
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000485{
486#ifdef linux
487 switch (lpSRB->common.SRB_Cmd) {
488 case SC_HA_INQUIRY:
489 lpSRB->inquiry.SRB_Status = SS_COMP; /* completed successfully */
Marcus Meissner485c3ef2001-06-06 20:19:07 +0000490 lpSRB->inquiry.HA_Count = ASPI_GetNumControllers();
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000491 lpSRB->inquiry.HA_SCSI_ID = 7; /* not always ID 7 */
David Elliott8466fc32000-07-15 15:15:31 +0000492 strcpy(lpSRB->inquiry.HA_ManagerId, "ASPI for WIN32"); /* max 15 chars, don't change */
493 strcpy(lpSRB->inquiry.HA_Identifier, "Wine host"); /* FIXME: return host adapter name */
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000494 memset(lpSRB->inquiry.HA_Unique, 0, 16); /* default HA_Unique content */
495 lpSRB->inquiry.HA_Unique[6] = 0x02; /* Maximum Transfer Length (128K, Byte> 4-7) */
David Elliott8466fc32000-07-15 15:15:31 +0000496 lpSRB->inquiry.HA_Unique[3] = 0x08; /* Maximum number of SCSI targets */
Alexandre Julliarda099a551999-06-12 15:45:58 +0000497 FIXME("ASPI: Partially implemented SC_HA_INQUIRY for adapter %d.\n", lpSRB->inquiry.SRB_HaId);
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000498 return SS_COMP;
Marcus Meissner12085e92000-03-26 13:36:16 +0000499
Marcus Meissnereb71e501999-09-19 12:08:18 +0000500 case SC_GET_DEV_TYPE: {
David Elliott8b8a3432000-01-08 22:25:02 +0000501 /* FIXME: We should return SS_NO_DEVICE if the device is not configured */
502 /* FIXME: We should return SS_INVALID_HA if HostAdapter!=0 */
Marcus Meissnereb71e501999-09-19 12:08:18 +0000503 SRB tmpsrb;
504 char inqbuf[200];
Marcus Meissner8cd83a92000-03-24 19:45:47 +0000505 DWORD ret;
Marcus Meissnereb71e501999-09-19 12:08:18 +0000506
507 memset(&tmpsrb,0,sizeof(tmpsrb));
508
Marcus Meissner8cd83a92000-03-24 19:45:47 +0000509 /* Copy header */
510 memcpy(&tmpsrb.common,&(lpSRB->common),sizeof(tmpsrb.common));
Patrik Stridvall3c0211f2001-09-11 00:32:32 +0000511
Marcus Meissner8cd83a92000-03-24 19:45:47 +0000512 tmpsrb.cmd.SRB_Flags |= 8; /* target to host */
513 tmpsrb.cmd.SRB_Cmd = SC_EXEC_SCSI_CMD;
514 tmpsrb.cmd.SRB_Target = lpSRB->devtype.SRB_Target;
515 tmpsrb.cmd.SRB_Lun = lpSRB->devtype.SRB_Lun;
Marcus Meissnereb71e501999-09-19 12:08:18 +0000516 tmpsrb.cmd.SRB_BufLen = sizeof(inqbuf);
517 tmpsrb.cmd.SRB_BufPointer = inqbuf;
518 tmpsrb.cmd.CDBByte[0] = 0x12; /* INQUIRY */
Marcus Meissner8cd83a92000-03-24 19:45:47 +0000519 /* FIXME: handle lun */
Marcus Meissnereb71e501999-09-19 12:08:18 +0000520 tmpsrb.cmd.CDBByte[4] = sizeof(inqbuf);
521 tmpsrb.cmd.SRB_CDBLen = 6;
Marcus Meissner12085e92000-03-26 13:36:16 +0000522
Marcus Meissner8cd83a92000-03-24 19:45:47 +0000523 ret = ASPI_ExecScsiCmd(&tmpsrb.cmd);
Marcus Meissner12085e92000-03-26 13:36:16 +0000524
525 lpSRB->devtype.SRB_Status = tmpsrb.cmd.SRB_Status;
Marcus Meissner5b66f701999-12-20 04:02:48 +0000526 lpSRB->devtype.SRB_DeviceType = inqbuf[0]&0x1f;
Marcus Meissner12085e92000-03-26 13:36:16 +0000527
Marcus Meissner5b66f701999-12-20 04:02:48 +0000528 TRACE("returning devicetype %d for target %d\n",inqbuf[0]&0x1f,tmpsrb.cmd.SRB_Target);
Marcus Meissner12085e92000-03-26 13:36:16 +0000529 if (ret!=SS_PENDING) /* Any error is passed down directly */
530 return ret;
531 /* FIXME: knows that the command is finished already, pass final Status */
532 return tmpsrb.cmd.SRB_Status;
Marcus Meissnereb71e501999-09-19 12:08:18 +0000533 }
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000534 case SC_EXEC_SCSI_CMD:
Alexandre Julliarda3960291999-02-26 11:11:13 +0000535 return ASPI_ExecScsiCmd(&lpSRB->cmd);
David Elliott8b8a3432000-01-08 22:25:02 +0000536 case SC_ABORT_SRB:
537 FIXME("Not implemented SC_ABORT_SRB\n");
538 break;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000539 case SC_RESET_DEV:
Alexandre Julliarda099a551999-06-12 15:45:58 +0000540 FIXME("Not implemented SC_RESET_DEV\n");
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000541 break;
David Elliott8b8a3432000-01-08 22:25:02 +0000542 case SC_GET_DISK_INFO:
Patrik Stridvall3c0211f2001-09-11 00:32:32 +0000543 /* here we have to find out the int13 / bios association.
Marcus Meissner485c3ef2001-06-06 20:19:07 +0000544 * We just say we do not have any.
545 */
546 FIXME("SC_GET_DISK_INFO always return 'int13 unassociated disk'.\n");
547 lpSRB->diskinfo.SRB_DriveFlags = 0; /* disk is not int13 served */
548 return SS_COMP;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000549 default:
Marcus Meissner8cd83a92000-03-24 19:45:47 +0000550 FIXME("Unknown command %d\n", lpSRB->common.SRB_Cmd);
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000551 }
552 return SS_INVALID_SRB;
553#else
554 return SS_INVALID_SRB;
555#endif
556}
557
558
559/***********************************************************************
Patrik Stridvall8b216b32001-06-19 18:20:47 +0000560 * GetASPI32DLLVersion (WNASPI32.4)
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000561 */
Patrik Stridvallbf8943c2000-10-02 03:39:34 +0000562DWORD __cdecl GetASPI32DLLVersion(void)
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000563{
564#ifdef linux
David Elliott8466fc32000-07-15 15:15:31 +0000565 TRACE("Returning version 1\n");
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000566 return (DWORD)1;
567#else
David Elliott8466fc32000-07-15 15:15:31 +0000568 FIXME("Please add SCSI support for your operating system, returning 0\n");
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000569 return (DWORD)0;
570#endif
571}
572
Patrik Stridvallbf8943c2000-10-02 03:39:34 +0000573/***********************************************************************
Patrik Stridvall8b216b32001-06-19 18:20:47 +0000574 * GetASPI32Buffer (WNASPI32.8)
Marcus Meissner485c3ef2001-06-06 20:19:07 +0000575 * Supposed to return a DMA capable large SCSI buffer.
Patrik Stridvall3c0211f2001-09-11 00:32:32 +0000576 * Our implementation does not use those at all, all buffer stuff is
Marcus Meissner485c3ef2001-06-06 20:19:07 +0000577 * done in the kernel SG device layer. So we just heapalloc the buffer.
Patrik Stridvallbf8943c2000-10-02 03:39:34 +0000578 */
Patrik Stridvall3c0211f2001-09-11 00:32:32 +0000579BOOL __cdecl GetASPI32Buffer(PASPI32BUFF pab)
Andreas Mohr4d47ec22000-09-06 19:42:19 +0000580{
Marcus Meissner485c3ef2001-06-06 20:19:07 +0000581 pab->AB_BufPointer = HeapAlloc(GetProcessHeap(),
582 pab->AB_ZeroFill?HEAP_ZERO_MEMORY:0,
583 pab->AB_BufLen
584 );
585 if (!pab->AB_BufPointer) return FALSE;
Andreas Mohr4d47ec22000-09-06 19:42:19 +0000586 return TRUE;
587}
588
Patrik Stridvallbf8943c2000-10-02 03:39:34 +0000589/***********************************************************************
Patrik Stridvall8b216b32001-06-19 18:20:47 +0000590 * FreeASPI32Buffer (WNASPI32.14)
Patrik Stridvallbf8943c2000-10-02 03:39:34 +0000591 */
Marcus Meissner485c3ef2001-06-06 20:19:07 +0000592BOOL __cdecl FreeASPI32Buffer(PASPI32BUFF pab)
Andreas Mohr4d47ec22000-09-06 19:42:19 +0000593{
Marcus Meissner485c3ef2001-06-06 20:19:07 +0000594 HeapFree(GetProcessHeap(),0,pab->AB_BufPointer);
Andreas Mohr4d47ec22000-09-06 19:42:19 +0000595 return TRUE;
596}
597
Patrik Stridvallbf8943c2000-10-02 03:39:34 +0000598/***********************************************************************
Patrik Stridvall8b216b32001-06-19 18:20:47 +0000599 * TranslateASPI32Address (WNASPI32.7)
Patrik Stridvallbf8943c2000-10-02 03:39:34 +0000600 */
Andreas Mohr4d47ec22000-09-06 19:42:19 +0000601BOOL __cdecl TranslateASPI32Address(LPDWORD pdwPath, LPDWORD pdwDEVNODE)
602{
603 FIXME("(%p, %p), stub !\n", pdwPath, pdwDEVNODE);
604 return TRUE;
605}