Release 20000326.
[wine/gsoc-2012-control.git] / dlls / winaspi / winaspi16.c
blob7bbae1f2c6766b0dd6c479adcbb21d4479b3b8ca
1 #include "config.h"
3 #include <stdlib.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <memory.h>
9 #include <unistd.h>
10 #include <string.h>
11 #include <stdio.h>
13 #include "winbase.h"
14 #include "aspi.h"
15 #include "winescsi.h"
16 #include "winaspi.h"
17 #include "winescsi.h"
18 #include "options.h"
19 #include "heap.h"
20 #include "debugtools.h"
21 #include "selectors.h"
22 #include "miscemu.h"
23 #include "ldt.h"
24 #include "callback.h"
26 DEFAULT_DEBUG_CHANNEL(aspi)
29 /* FIXME!
30 * 1) Residual byte length reporting not handled
31 * 2) Make this code re-entrant for multithreading
32 * 3) Only linux supported so far
35 #ifdef linux
37 static ASPI_DEVICE_INFO *ASPI_open_devices = NULL;
39 static FARPROC16 ASPIChainFunc = NULL;
40 static WORD HA_Count = 1; /* host adapter count; FIXME: detect it */
42 static int
43 ASPI_OpenDevice16(SRB_ExecSCSICmd16 *prb)
45 int fd;
46 char idstr[20];
47 char device_str[50];
48 ASPI_DEVICE_INFO *curr;
50 /* search list of devices to see if we've opened it already.
51 * There is not an explicit open/close in ASPI land, so hopefully
52 * keeping a device open won't be a problem.
55 for (curr = ASPI_open_devices; curr; curr = curr->next) {
56 if (curr->hostId == prb->SRB_HaId &&
57 curr->target == prb->SRB_Target &&
58 curr->lun == prb->SRB_Lun) {
59 return curr->fd;
63 /* device wasn't cached, go ahead and open it */
64 sprintf(idstr, "scsi c%1dt%1dd%1d", prb->SRB_HaId, prb->SRB_Target, prb->SRB_Lun);
66 if (!PROFILE_GetWineIniString(idstr, "Device", "", device_str, sizeof(device_str))) {
67 TRACE("Trying to open unlisted scsi device %s\n", idstr);
68 return -1;
71 TRACE("Opening device %s=%s\n", idstr, device_str);
73 fd = open(device_str, O_RDWR);
74 if (fd == -1) {
75 int save_error = errno;
76 #ifdef HAVE_STRERROR
77 ERR("Error opening device %s, error '%s'\n", device_str, strerror(save_error));
78 #else
79 ERR("Error opening device %s, error %d\n", device_str, save_error);
80 #endif
81 return -1;
84 /* device is now open */
85 curr = HeapAlloc( GetProcessHeap(), 0, sizeof(ASPI_DEVICE_INFO) );
86 curr->fd = fd;
87 curr->hostId = prb->SRB_HaId;
88 curr->target = prb->SRB_Target;
89 curr->lun = prb->SRB_Lun;
91 /* insert new record at beginning of open device list */
92 curr->next = ASPI_open_devices;
93 ASPI_open_devices = curr;
94 return fd;
98 static void
99 ASPI_DebugPrintCmd(SRB_ExecSCSICmd16 *prb, UINT16 mode)
101 BYTE cmd;
102 int i;
103 BYTE *cdb;
104 BYTE *lpBuf = 0;
106 switch (mode)
108 case ASPI_DOS:
109 /* translate real mode address */
110 if (prb->SRB_BufPointer)
111 lpBuf = (BYTE *)DOSMEM_MapRealToLinear((UINT)prb->SRB_BufPointer);
112 break;
113 case ASPI_WIN16:
114 lpBuf = PTR_SEG_TO_LIN(prb->SRB_BufPointer);
115 break;
118 switch (prb->CDBByte[0]) {
119 case CMD_INQUIRY:
120 TRACE("{\n");
121 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");
127 break;
128 case CMD_SCAN_SCAN:
129 TRACE("Transfer Length: %d\n", prb->CDBByte[4]);
130 break;
133 TRACE("Host Adapter: %d\n", prb->SRB_HaId);
134 TRACE("Flags: %d\n", prb->SRB_Flags);
135 if (TARGET_TO_HOST(prb)) {
136 TRACE("\tData transfer: Target to host. Length checked.\n");
138 else if (HOST_TO_TARGET(prb)) {
139 TRACE("\tData transfer: Host to target. Length checked.\n");
141 else if (NO_DATA_TRANSFERED(prb)) {
142 TRACE("\tData transfer: none\n");
144 else {
145 WARN("\tTransfer by scsi cmd. Length not checked\n");
148 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: %lx (%p)\n", prb->SRB_BufPointer, lpBuf);
156 TRACE("LinkPointer %lx\n", prb->SRB_Rsvd1);
157 TRACE("CDB Length: %d\n", prb->SRB_CDBLen);
158 TRACE("POST Proc: %lx\n", (DWORD) prb->SRB_PostProc);
159 cdb = &prb->CDBByte[0];
160 cmd = prb->CDBByte[0];
161 if (TRACE_ON(aspi))
163 DPRINTF("CDB buffer[");
164 for (i = 0; i < prb->SRB_CDBLen; i++) {
165 if (i != 0) DPRINTF(",");
166 DPRINTF("%02x", *cdb++);
168 DPRINTF("]\n");
172 static void
173 ASPI_PrintSenseArea16(SRB_ExecSCSICmd16 *prb)
175 int i;
176 BYTE *cdb;
178 if (TRACE_ON(aspi))
180 cdb = &prb->CDBByte[0];
181 DPRINTF("SenseArea[");
182 for (i = 0; i < prb->SRB_SenseLen; i++) {
183 if (i) DPRINTF(",");
184 DPRINTF("%02x", *cdb++);
186 DPRINTF("]\n");
190 static void
191 ASPI_DebugPrintResult(SRB_ExecSCSICmd16 *prb, UINT16 mode)
193 BYTE *lpBuf = 0;
195 switch (mode)
197 case ASPI_DOS:
198 /* translate real mode address */
199 if (prb->SRB_BufPointer)
200 lpBuf = (BYTE *)DOSMEM_MapRealToLinear((UINT)prb->SRB_BufPointer);
201 break;
202 case ASPI_WIN16:
203 lpBuf = PTR_SEG_TO_LIN(prb->SRB_BufPointer);
204 break;
207 switch (prb->CDBByte[0]) {
208 case CMD_INQUIRY:
209 TRACE("Vendor: '%s'\n", lpBuf + INQUIRY_VENDOR);
210 break;
211 case CMD_TEST_UNIT_READY:
212 ASPI_PrintSenseArea16(prb);
213 break;
217 static WORD
218 ASPI_ExecScsiCmd(DWORD ptrPRB, UINT16 mode)
220 SRB_ExecSCSICmd16 *lpPRB = 0;
221 struct sg_header *sg_hd, *sg_reply_hdr;
222 int status;
223 BYTE *lpBuf = 0;
224 int in_len, out_len;
225 int error_code = 0;
226 int fd;
228 switch (mode)
230 case ASPI_DOS:
231 if (ptrPRB)
232 lpPRB = (SRB_ExecSCSICmd16 *)DOSMEM_MapRealToLinear(ptrPRB);
233 break;
234 case ASPI_WIN16:
235 lpPRB = PTR_SEG_TO_LIN(ptrPRB);
236 break;
239 ASPI_DebugPrintCmd(lpPRB, mode);
241 fd = ASPI_OpenDevice16(lpPRB);
242 if (fd == -1) {
243 WARN("Failed: could not open device. Device permissions !?\n");
244 lpPRB->SRB_Status = SS_ERR;
245 return SS_ERR;
248 sg_hd = NULL;
249 sg_reply_hdr = NULL;
251 lpPRB->SRB_Status = SS_PENDING;
253 switch (mode)
255 case ASPI_DOS:
256 /* translate real mode address */
257 if (ptrPRB)
258 lpBuf = (BYTE *)DOSMEM_MapRealToLinear((UINT)lpPRB->SRB_BufPointer);
259 break;
260 case ASPI_WIN16:
261 lpBuf = PTR_SEG_TO_LIN(lpPRB->SRB_BufPointer);
262 break;
265 if (!lpPRB->SRB_CDBLen) {
266 WARN("Failed: lpPRB->SRB_CDBLen = 0.\n");
267 lpPRB->SRB_Status = SS_ERR;
268 return SS_ERR;
271 /* build up sg_header + scsi cmd */
272 if (HOST_TO_TARGET(lpPRB)) {
273 /* send header, command, and then data */
274 in_len = SCSI_OFF + lpPRB->SRB_CDBLen + lpPRB->SRB_BufLen;
275 sg_hd = (struct sg_header *) malloc(in_len);
276 memset(sg_hd, 0, SCSI_OFF);
277 memcpy(sg_hd + 1, &lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
278 if (lpPRB->SRB_BufLen) {
279 memcpy(((BYTE *) sg_hd) + SCSI_OFF + lpPRB->SRB_CDBLen, lpBuf, lpPRB->SRB_BufLen);
282 else {
283 /* send header and command - no data */
284 in_len = SCSI_OFF + lpPRB->SRB_CDBLen;
285 sg_hd = (struct sg_header *) malloc(in_len);
286 memset(sg_hd, 0, SCSI_OFF);
287 memcpy(sg_hd + 1, &lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
290 if (TARGET_TO_HOST(lpPRB)) {
291 out_len = SCSI_OFF + lpPRB->SRB_BufLen;
292 sg_reply_hdr = (struct sg_header *) malloc(out_len);
293 memset(sg_reply_hdr, 0, SCSI_OFF);
294 sg_hd->reply_len = out_len;
296 else {
297 out_len = SCSI_OFF;
298 sg_reply_hdr = (struct sg_header *) malloc(out_len);
299 memset(sg_reply_hdr, 0, SCSI_OFF);
300 sg_hd->reply_len = out_len;
303 status = write(fd, sg_hd, in_len);
304 if (status < 0 || status != in_len) {
305 int save_error = errno;
307 WARN("Not enough bytes written to scsi device bytes=%d .. %d\n", in_len, status);
308 if (status < 0) {
309 if (save_error == ENOMEM) {
310 MESSAGE("ASPI: Linux generic scsi driver\n You probably need to re-compile your kernel with a larger SG_BIG_BUFF value (sg.h)\n Suggest 130560\n");
312 #ifdef HAVE_STRERROR
313 WARN("error:= '%s'\n", strerror(save_error));
314 #else
315 WARN("error:= %d\n", save_error);
316 #endif
318 goto error_exit;
321 status = read(fd, sg_reply_hdr, out_len);
322 if (status < 0 || status != out_len) {
323 WARN("not enough bytes read from scsi device%d\n", status);
324 goto error_exit;
327 if (sg_reply_hdr->result != 0) {
328 error_code = sg_reply_hdr->result;
329 WARN("reply header error (%d)\n", sg_reply_hdr->result);
330 goto error_exit;
333 if (TARGET_TO_HOST(lpPRB) && lpPRB->SRB_BufLen) {
334 memcpy(lpBuf, sg_reply_hdr + 1, lpPRB->SRB_BufLen);
337 /* copy in sense buffer to amount that is available in client */
338 if (lpPRB->SRB_SenseLen) {
339 int sense_len = lpPRB->SRB_SenseLen;
340 if (lpPRB->SRB_SenseLen > 16)
341 sense_len = 16;
342 memcpy(SENSE_BUFFER(lpPRB), &sg_reply_hdr->sense_buffer[0], sense_len);
346 lpPRB->SRB_Status = SS_COMP;
347 lpPRB->SRB_HaStat = HASTAT_OK;
348 lpPRB->SRB_TargStat = STATUS_GOOD;
350 /* now do posting */
352 if (ASPI_POSTING(lpPRB) && lpPRB->SRB_PostProc) {
353 TRACE("Post Routine (%lx) called\n", (DWORD) lpPRB->SRB_PostProc);
354 switch (mode)
356 case ASPI_DOS:
358 SEGPTR spPRB = MapLS(lpPRB);
360 Callbacks->CallASPIPostProc(lpPRB->SRB_PostProc, spPRB);
361 UnMapLS(spPRB);
362 break;
364 case ASPI_WIN16:
365 Callbacks->CallASPIPostProc(lpPRB->SRB_PostProc, ptrPRB);
366 break;
370 free(sg_reply_hdr);
371 free(sg_hd);
372 ASPI_DebugPrintResult(lpPRB, mode);
373 return SS_COMP;
375 error_exit:
376 if (error_code == EBUSY) {
377 lpPRB->SRB_Status = SS_ASPI_IS_BUSY;
378 TRACE("Device busy\n");
380 else {
381 WARN("Failed\n");
382 lpPRB->SRB_Status = SS_ERR;
385 /* I'm not sure exactly error codes work here
386 * We probably should set lpPRB->SRB_TargStat, SRB_HaStat ?
388 WARN("error_exit\n");
389 free(sg_reply_hdr);
390 free(sg_hd);
391 return lpPRB->SRB_Status;
393 #endif
396 /***********************************************************************
397 * GetASPISupportInfo16 (WINASPI.1)
400 WORD WINAPI GetASPISupportInfo16()
402 #ifdef linux
403 TRACE("GETASPISupportInfo16\n");
404 /* high byte SS_COMP - low byte number of host adapters */
405 return ((SS_COMP << 8) | HA_Count);
406 #else
407 return ((SS_NO_ASPI << 8) | 0);
408 #endif
412 DWORD ASPI_SendASPICommand(DWORD ptrSRB, UINT16 mode)
414 #ifdef linux
415 LPSRB16 lpSRB = 0;
417 switch (mode)
419 case ASPI_DOS:
420 if (ptrSRB)
421 lpSRB = (LPSRB16)DOSMEM_MapRealToLinear(ptrSRB);
422 break;
423 case ASPI_WIN16:
424 lpSRB = PTR_SEG_TO_LIN(ptrSRB);
425 if (ASPIChainFunc)
427 /* This is not the post proc, it's the chain proc this time */
428 DWORD ret = Callbacks->CallASPIPostProc(ASPIChainFunc, ptrSRB);
429 if (ret)
431 lpSRB->inquiry.SRB_Status = SS_INVALID_SRB;
432 return ret;
435 break;
438 switch (lpSRB->common.SRB_Cmd) {
439 case SC_HA_INQUIRY:
440 lpSRB->inquiry.SRB_Status = SS_COMP; /* completed successfully */
441 if (lpSRB->inquiry.SRB_55AASignature == 0x55aa) {
442 TRACE("Extended request detected (Adaptec's ASPIxDOS).\nWe don't support it at the moment.\n");
444 lpSRB->inquiry.SRB_ExtBufferSize = 0x2000; /* bogus value */
445 lpSRB->inquiry.HA_Count = HA_Count;
446 lpSRB->inquiry.HA_SCSI_ID = 7; /* not always ID 7 */
447 strcat(lpSRB->inquiry.HA_ManagerId, "Wine ASPI16"); /* max 15 chars */
448 strcat(lpSRB->inquiry.HA_Identifier, "Wine host"); /* FIXME: return host
449 adapter name */
450 memset(lpSRB->inquiry.HA_Unique, 0, 16); /* default HA_Unique content */
451 lpSRB->inquiry.HA_Unique[6] = 0x02; /* Maximum Transfer Length (128K, Byte> 4-7) */
452 FIXME("ASPI: Partially implemented SC_HA_INQUIRY for adapter %d.\n", lpSRB->inquiry.SRB_HaId);
453 return SS_COMP;
454 case SC_GET_DEV_TYPE:
455 FIXME("Not implemented SC_GET_DEV_TYPE\n");
456 break;
457 case SC_EXEC_SCSI_CMD:
458 return ASPI_ExecScsiCmd((DWORD)ptrSRB, mode);
459 break;
460 case SC_RESET_DEV:
461 FIXME("Not implemented SC_RESET_DEV\n");
462 break;
463 default:
464 FIXME("Unknown command %d\n", lpSRB->common.SRB_Cmd);
466 #endif
467 return SS_INVALID_SRB;
471 /***********************************************************************
472 * SendASPICommand16 (WINASPI.2)
474 WORD WINAPI SendASPICommand16(SEGPTR segptr_srb)
476 #ifdef linux
477 return ASPI_SendASPICommand(segptr_srb, ASPI_WIN16);
478 #else
479 return 0;
480 #endif
484 /***********************************************************************
485 * InsertInASPIChain16 (WINASPI.3)
487 WORD WINAPI InsertInASPIChain16(BOOL16 remove, FARPROC16 pASPIChainFunc)
489 #ifdef linux
490 if (remove == TRUE) /* Remove */
492 if (ASPIChainFunc == pASPIChainFunc)
494 ASPIChainFunc = NULL;
495 return SS_COMP;
498 else
499 if (remove == FALSE) /* Insert */
501 if (ASPIChainFunc == NULL)
503 ASPIChainFunc = pASPIChainFunc;
504 return SS_COMP;
507 #endif
508 return SS_ERR;
512 /***********************************************************************
513 * GetASPIDLLVersion16 (WINASPI.4)
516 DWORD WINAPI GetASPIDLLVersion16()
518 #ifdef linux
519 return 2;
520 #else
521 return 0;
522 #endif