msvcrt/tests: Remove a space before a '\n'.
[wine/gsoc-2012-control.git] / dlls / wnaspi32 / winaspi16.c
blob17d6a6c948fe29d7c8783cc121fdfd9efe3a1677
1 /*
2 * Copyright 1997 Bruce Milner
3 * Copyright 1998 Andreas Mohr
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "config.h"
21 #include "wine/port.h"
23 #include <stdlib.h>
24 #include <sys/types.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #include <string.h>
31 #include <stdarg.h>
32 #include <stdio.h>
34 #include "windef.h"
35 #include "winbase.h"
36 #include "wine/windef16.h"
37 #include "winreg.h"
38 #include "wownt32.h"
39 #include "aspi.h"
40 #include "winescsi.h"
41 #include "wine/winaspi.h"
42 #include "wine/debug.h"
45 /* FIXME!
46 * 1) Residual byte length reporting not handled
47 * 2) Make this code re-entrant for multithreading
48 * 3) Only linux supported so far
51 #ifdef linux
53 #define PTR_TO_LIN(ptr,mode) \
54 ((mode) == ASPI_DOS ? ((void*)(((unsigned int)SELECTOROF(ptr) << 4) + OFFSETOF(ptr))) : MapSL(ptr))
56 WINE_DEFAULT_DEBUG_CHANNEL(aspi);
58 static ASPI_DEVICE_INFO *ASPI_open_devices = NULL;
60 static FARPROC16 ASPIChainFunc = NULL;
61 static WORD HA_Count = 1; /* host adapter count; FIXME: detect it */
63 static int
64 ASPI_OpenDevice16(SRB_ExecSCSICmd16 *prb)
66 HKEY hkey;
67 int fd;
68 char idstr[50];
69 char device_str[50];
70 ASPI_DEVICE_INFO *curr;
72 /* search list of devices to see if we've opened it already.
73 * There is not an explicit open/close in ASPI land, so hopefully
74 * keeping a device open won't be a problem.
77 for (curr = ASPI_open_devices; curr; curr = curr->next) {
78 if (curr->hostId == prb->SRB_HaId &&
79 curr->target == prb->SRB_Target &&
80 curr->lun == prb->SRB_Lun) {
81 return curr->fd;
85 /* device wasn't cached, go ahead and open it */
86 sprintf( idstr, "Software\\Wine\\Wine\\Config\\scsi c%1dt%1dd%1d",
87 prb->SRB_HaId, prb->SRB_Target, prb->SRB_Lun);
89 device_str[0] = 0;
90 if (!RegOpenKeyExA( HKEY_LOCAL_MACHINE, idstr, 0, KEY_ALL_ACCESS, &hkey ))
92 DWORD type, count = sizeof(device_str);
93 if (RegQueryValueExA( hkey, "Device", 0, &type, (LPBYTE)device_str, &count )) device_str[0] = 0;
94 RegCloseKey( hkey );
97 if (!device_str[0])
99 TRACE("Trying to open unlisted scsi device %s\n", idstr);
100 return -1;
103 TRACE("Opening device %s=%s\n", idstr, device_str);
105 fd = open(device_str, O_RDWR);
106 if (fd == -1) {
107 int save_error = errno;
108 ERR("Error opening device %s, error '%s'\n", device_str, strerror(save_error));
109 return -1;
112 /* device is now open */
113 curr = HeapAlloc( GetProcessHeap(), 0, sizeof(ASPI_DEVICE_INFO) );
114 curr->fd = fd;
115 curr->hostId = prb->SRB_HaId;
116 curr->target = prb->SRB_Target;
117 curr->lun = prb->SRB_Lun;
119 /* insert new record at beginning of open device list */
120 curr->next = ASPI_open_devices;
121 ASPI_open_devices = curr;
122 return fd;
126 static void
127 ASPI_DebugPrintCmd(SRB_ExecSCSICmd16 *prb, UINT16 mode)
129 int i;
130 BYTE *cdb;
131 BYTE *lpBuf = PTR_TO_LIN( prb->SRB_BufPointer, mode );
133 switch (prb->CDBByte[0]) {
134 case CMD_INQUIRY:
135 TRACE("{\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]);
141 TRACE("}\n");
142 break;
143 case CMD_SCAN_SCAN:
144 TRACE("Transfer Length: %d\n", prb->CDBByte[4]);
145 break;
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_TRANSFERRED(prb)) {
157 TRACE("\tData transfer: none\n");
159 else {
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: %d\n", prb->SRB_BufLen);
169 TRACE("SenseLen: %d\n", prb->SRB_SenseLen);
170 TRACE("BufPtr: %x (%p)\n", prb->SRB_BufPointer, lpBuf);
171 TRACE("LinkPointer %x\n", prb->SRB_Rsvd1);
172 TRACE("CDB Length: %d\n", prb->SRB_CDBLen);
173 TRACE("POST Proc: %x\n", (DWORD) prb->SRB_PostProc);
174 cdb = &prb->CDBByte[0];
175 if (TRACE_ON(aspi))
177 TRACE("CDB buffer[");
178 for (i = 0; i < prb->SRB_CDBLen; i++) {
179 if (i != 0) TRACE(",");
180 TRACE("%02x", *cdb++);
182 TRACE("]\n");
186 static void
187 ASPI_PrintSenseArea16(SRB_ExecSCSICmd16 *prb)
189 int i;
190 BYTE *cdb;
192 if (TRACE_ON(aspi))
194 cdb = &prb->CDBByte[0];
195 TRACE("SenseArea[");
196 for (i = 0; i < prb->SRB_SenseLen; i++) {
197 if (i) TRACE(",");
198 TRACE("%02x", *cdb++);
200 TRACE("]\n");
204 static void
205 ASPI_DebugPrintResult(SRB_ExecSCSICmd16 *prb, UINT16 mode)
207 BYTE *lpBuf = PTR_TO_LIN( prb->SRB_BufPointer, mode );
209 switch (prb->CDBByte[0]) {
210 case CMD_INQUIRY:
211 TRACE("Vendor: '%s'\n", lpBuf + INQUIRY_VENDOR);
212 break;
213 case CMD_TEST_UNIT_READY:
214 ASPI_PrintSenseArea16(prb);
215 break;
219 static WORD
220 ASPI_ExecScsiCmd(DWORD ptrPRB, UINT16 mode)
222 SRB_ExecSCSICmd16 *lpPRB = PTR_TO_LIN( ptrPRB, mode );
223 struct sg_header *sg_hd, *sg_reply_hdr;
224 int status;
225 BYTE *lpBuf = 0;
226 int in_len, out_len;
227 int error_code = 0;
228 int fd;
230 ASPI_DebugPrintCmd(lpPRB, mode);
232 fd = ASPI_OpenDevice16(lpPRB);
233 if (fd == -1) {
234 WARN("Failed: could not open device. Device permissions !?\n");
235 lpPRB->SRB_Status = SS_ERR;
236 return SS_ERR;
239 sg_hd = NULL;
240 sg_reply_hdr = NULL;
242 lpPRB->SRB_Status = SS_PENDING;
243 lpBuf = PTR_TO_LIN( lpPRB->SRB_BufPointer, mode );
245 if (!lpPRB->SRB_CDBLen) {
246 WARN("Failed: lpPRB->SRB_CDBLen = 0.\n");
247 lpPRB->SRB_Status = SS_ERR;
248 return SS_ERR;
251 /* build up sg_header + scsi cmd */
252 if (HOST_TO_TARGET(lpPRB)) {
253 /* send header, command, and then data */
254 in_len = SCSI_OFF + lpPRB->SRB_CDBLen + lpPRB->SRB_BufLen;
255 sg_hd = HeapAlloc(GetProcessHeap(), 0, in_len);
256 memset(sg_hd, 0, SCSI_OFF);
257 memcpy(sg_hd + 1, &lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
258 if (lpPRB->SRB_BufLen) {
259 memcpy(((BYTE *) sg_hd) + SCSI_OFF + lpPRB->SRB_CDBLen, lpBuf, lpPRB->SRB_BufLen);
262 else {
263 /* send header and command - no data */
264 in_len = SCSI_OFF + lpPRB->SRB_CDBLen;
265 sg_hd = HeapAlloc(GetProcessHeap(), 0, in_len);
266 memset(sg_hd, 0, SCSI_OFF);
267 memcpy(sg_hd + 1, &lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
270 if (TARGET_TO_HOST(lpPRB)) {
271 out_len = SCSI_OFF + lpPRB->SRB_BufLen;
272 sg_reply_hdr = HeapAlloc(GetProcessHeap(), 0, out_len);
273 memset(sg_reply_hdr, 0, SCSI_OFF);
274 sg_hd->reply_len = out_len;
276 else {
277 out_len = SCSI_OFF;
278 sg_reply_hdr = HeapAlloc(GetProcessHeap(), 0, out_len);
279 memset(sg_reply_hdr, 0, SCSI_OFF);
280 sg_hd->reply_len = out_len;
283 status = write(fd, sg_hd, in_len);
284 if (status < 0 || status != in_len) {
285 int save_error = errno;
287 WARN("Not enough bytes written to scsi device bytes=%d .. %d\n", in_len, status);
288 if (status < 0) {
289 if (save_error == ENOMEM) {
290 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");
292 WARN("error:= '%s'\n", strerror(save_error));
294 goto error_exit;
297 status = read(fd, sg_reply_hdr, out_len);
298 if (status < 0 || status != out_len) {
299 WARN("not enough bytes read from scsi device%d\n", status);
300 goto error_exit;
303 if (sg_reply_hdr->result != 0) {
304 error_code = sg_reply_hdr->result;
305 WARN("reply header error (%d)\n", sg_reply_hdr->result);
306 goto error_exit;
309 if (TARGET_TO_HOST(lpPRB) && lpPRB->SRB_BufLen) {
310 memcpy(lpBuf, sg_reply_hdr + 1, lpPRB->SRB_BufLen);
313 /* copy in sense buffer to amount that is available in client */
314 if (lpPRB->SRB_SenseLen) {
315 int sense_len = lpPRB->SRB_SenseLen;
316 if (lpPRB->SRB_SenseLen > 16)
317 sense_len = 16;
318 memcpy(SENSE_BUFFER(lpPRB), &sg_reply_hdr->sense_buffer[0], sense_len);
322 lpPRB->SRB_Status = SS_COMP;
323 lpPRB->SRB_HaStat = HASTAT_OK;
324 lpPRB->SRB_TargStat = STATUS_GOOD;
326 /* now do posting */
328 if (ASPI_POSTING(lpPRB) && lpPRB->SRB_PostProc) {
329 TRACE("Post Routine (%x) called\n", (DWORD) lpPRB->SRB_PostProc);
330 switch (mode)
332 case ASPI_DOS:
334 SEGPTR spPRB = MapLS(lpPRB);
336 WOWCallback16((DWORD)lpPRB->SRB_PostProc, spPRB);
337 UnMapLS(spPRB);
338 break;
340 case ASPI_WIN16:
341 WOWCallback16((DWORD)lpPRB->SRB_PostProc, ptrPRB);
342 break;
346 HeapFree(GetProcessHeap(), 0, sg_reply_hdr);
347 HeapFree(GetProcessHeap(), 0, sg_hd);
348 ASPI_DebugPrintResult(lpPRB, mode);
349 return SS_COMP;
351 error_exit:
352 if (error_code == EBUSY) {
353 lpPRB->SRB_Status = SS_ASPI_IS_BUSY;
354 TRACE("Device busy\n");
356 else {
357 WARN("Failed\n");
358 lpPRB->SRB_Status = SS_ERR;
361 /* I'm not sure exactly error codes work here
362 * We probably should set lpPRB->SRB_TargStat, SRB_HaStat ?
364 WARN("error_exit\n");
365 HeapFree(GetProcessHeap(), 0, sg_reply_hdr);
366 HeapFree(GetProcessHeap(), 0, sg_hd);
367 return lpPRB->SRB_Status;
369 #endif
372 /***********************************************************************
373 * GetASPISupportInfo (WINASPI.1)
376 WORD WINAPI GetASPISupportInfo16(void)
378 #ifdef linux
379 TRACE("GETASPISupportInfo16\n");
380 /* high byte SS_COMP - low byte number of host adapters */
381 return ((SS_COMP << 8) | HA_Count);
382 #else
383 return ((SS_NO_ASPI << 8) | 0);
384 #endif
388 DWORD ASPI_SendASPICommand(DWORD ptrSRB, UINT16 mode)
390 #ifdef linux
391 LPSRB16 lpSRB = PTR_TO_LIN( ptrSRB, mode );
392 static const char szId[] = "Wine ASPI16";
393 static const char szWh[] = "Wine host";
395 if (mode == ASPI_WIN16 && ASPIChainFunc)
397 /* This is not the post proc, it's the chain proc this time */
398 DWORD ret = WOWCallback16((DWORD)ASPIChainFunc, ptrSRB);
399 if (ret)
401 lpSRB->inquiry.SRB_Status = SS_INVALID_SRB;
402 return ret;
406 switch (lpSRB->common.SRB_Cmd) {
407 case SC_HA_INQUIRY:
408 lpSRB->inquiry.SRB_Status = SS_COMP; /* completed successfully */
409 if (lpSRB->inquiry.SRB_55AASignature == 0x55aa) {
410 TRACE("Extended request detected (Adaptec's ASPIxDOS).\nWe don't support it at the moment.\n");
412 lpSRB->inquiry.SRB_ExtBufferSize = 0x2000; /* bogus value */
413 lpSRB->inquiry.HA_Count = HA_Count;
414 lpSRB->inquiry.HA_SCSI_ID = 7; /* not always ID 7 */
415 memcpy(lpSRB->inquiry.HA_ManagerId, szId, sizeof szId); /* max 15 chars */
416 memcpy(lpSRB->inquiry.HA_Identifier, szWh, sizeof szWh); /* FIXME: return host
417 adapter name */
418 memset(lpSRB->inquiry.HA_Unique, 0, 16); /* default HA_Unique content */
419 lpSRB->inquiry.HA_Unique[6] = 0x02; /* Maximum Transfer Length (128K, Byte> 4-7) */
420 FIXME("ASPI: Partially implemented SC_HA_INQUIRY for adapter %d.\n", lpSRB->inquiry.SRB_HaId);
421 return SS_COMP;
422 case SC_GET_DEV_TYPE:
423 FIXME("Not implemented SC_GET_DEV_TYPE\n");
424 break;
425 case SC_EXEC_SCSI_CMD:
426 return ASPI_ExecScsiCmd(ptrSRB, mode);
427 case SC_RESET_DEV:
428 FIXME("Not implemented SC_RESET_DEV\n");
429 break;
430 default:
431 FIXME("Unknown command %d\n", lpSRB->common.SRB_Cmd);
433 #endif
434 return SS_INVALID_SRB;
438 /***********************************************************************
439 * SendASPICommand (WINASPI.2)
441 WORD WINAPI SendASPICommand16(SEGPTR segptr_srb)
443 #ifdef linux
444 return ASPI_SendASPICommand(segptr_srb, ASPI_WIN16);
445 #else
446 return 0;
447 #endif
451 /***********************************************************************
452 * InsertInASPIChain (WINASPI.3)
454 WORD WINAPI InsertInASPIChain16(BOOL16 remove, FARPROC16 pASPIChainFunc)
456 #ifdef linux
457 if (remove) /* Remove */
459 if (ASPIChainFunc == pASPIChainFunc)
461 ASPIChainFunc = NULL;
462 return SS_COMP;
465 else /* Insert */
467 if (ASPIChainFunc == NULL)
469 ASPIChainFunc = pASPIChainFunc;
470 return SS_COMP;
473 #endif
474 return SS_ERR;
478 /***********************************************************************
479 * GETASPIDLLVERSION (WINASPI.4)
482 DWORD WINAPI GetASPIDLLVersion16(void)
484 #ifdef linux
485 return 2;
486 #else
487 return 0;
488 #endif