cmd: Improve 'attrib' builtin to handle at least setting/clearing single attributes.
[wine/multimedia.git] / dlls / winaspi.dll16 / winaspi.c
blob9ef9974e5be06314abb4eea700238860fe801d29
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 "wine/winaspi.h"
41 #include "wine/debug.h"
44 /* FIXME!
45 * 1) Residual byte length reporting not handled
46 * 2) Make this code re-entrant for multithreading
47 * 3) Only linux supported so far
50 #ifdef linux
52 /* Copy of info from 2.2.x kernel */
53 #define SG_MAX_SENSE 16 /* too little, unlikely to change in 2.2.x */
55 struct sg_header
57 int pack_len; /* [o] reply_len (ie useless), ignored as input */
58 int reply_len; /* [i] max length of expected reply (inc. sg_header) */
59 int pack_id; /* [io] id number of packet (use ints >= 0) */
60 int result; /* [o] 0==ok, else (+ve) Unix errno (best ignored) */
61 unsigned int twelve_byte:1;
62 /* [i] Force 12 byte command length for group 6 & 7 commands */
63 unsigned int target_status:5; /* [o] scsi status from target */
64 unsigned int host_status:8; /* [o] host status (see "DID" codes) */
65 unsigned int driver_status:8; /* [o] driver status+suggestion */
66 unsigned int other_flags:10; /* unused */
67 unsigned char sense_buffer[SG_MAX_SENSE]; /* [o] Output in 3 cases:
68 when target_status is CHECK_CONDITION or
69 when target_status is COMMAND_TERMINATED or
70 when (driver_status & DRIVER_SENSE) is true. */
71 }; /* This structure is 36 bytes long on i386 */
73 #define SCSI_OFF sizeof(struct sg_header)
75 #define PTR_TO_LIN(ptr,mode) \
76 ((mode) == ASPI_DOS ? ((void*)(((unsigned int)SELECTOROF(ptr) << 4) + OFFSETOF(ptr))) : MapSL(ptr))
78 WINE_DEFAULT_DEBUG_CHANNEL(aspi);
80 /* Just a container for seeing what devices are open */
81 struct ASPI_DEVICE_INFO {
82 struct ASPI_DEVICE_INFO * next;
83 int fd;
84 int hostId;
85 int target;
86 int lun;
89 typedef struct ASPI_DEVICE_INFO ASPI_DEVICE_INFO;
91 static ASPI_DEVICE_INFO *ASPI_open_devices = NULL;
93 static FARPROC16 ASPIChainFunc = NULL;
94 static WORD HA_Count = 1; /* host adapter count; FIXME: detect it */
96 static int
97 ASPI_OpenDevice16(SRB_ExecSCSICmd16 *prb)
99 HKEY hkey;
100 int fd;
101 char idstr[50];
102 char device_str[50];
103 ASPI_DEVICE_INFO *curr;
105 /* search list of devices to see if we've opened it already.
106 * There is not an explicit open/close in ASPI land, so hopefully
107 * keeping a device open won't be a problem.
110 for (curr = ASPI_open_devices; curr; curr = curr->next) {
111 if (curr->hostId == prb->SRB_HaId &&
112 curr->target == prb->SRB_Target &&
113 curr->lun == prb->SRB_Lun) {
114 return curr->fd;
118 /* device wasn't cached, go ahead and open it */
119 sprintf( idstr, "Software\\Wine\\Wine\\Config\\scsi c%1dt%1dd%1d",
120 prb->SRB_HaId, prb->SRB_Target, prb->SRB_Lun);
122 device_str[0] = 0;
123 if (!RegOpenKeyExA( HKEY_LOCAL_MACHINE, idstr, 0, KEY_ALL_ACCESS, &hkey ))
125 DWORD type, count = sizeof(device_str);
126 if (RegQueryValueExA( hkey, "Device", 0, &type, (LPBYTE)device_str, &count )) device_str[0] = 0;
127 RegCloseKey( hkey );
130 if (!device_str[0])
132 TRACE("Trying to open unlisted scsi device %s\n", idstr);
133 return -1;
136 TRACE("Opening device %s=%s\n", idstr, device_str);
138 fd = open(device_str, O_RDWR);
139 if (fd == -1) {
140 int save_error = errno;
141 ERR("Error opening device %s, error '%s'\n", device_str, strerror(save_error));
142 return -1;
145 /* device is now open */
146 curr = HeapAlloc( GetProcessHeap(), 0, sizeof(ASPI_DEVICE_INFO) );
147 curr->fd = fd;
148 curr->hostId = prb->SRB_HaId;
149 curr->target = prb->SRB_Target;
150 curr->lun = prb->SRB_Lun;
152 /* insert new record at beginning of open device list */
153 curr->next = ASPI_open_devices;
154 ASPI_open_devices = curr;
155 return fd;
159 static void
160 ASPI_DebugPrintCmd(SRB_ExecSCSICmd16 *prb, UINT16 mode)
162 int i;
163 BYTE *cdb;
164 BYTE *lpBuf = PTR_TO_LIN( prb->SRB_BufPointer, mode );
166 switch (prb->CDBByte[0]) {
167 case CMD_INQUIRY:
168 TRACE("{\n");
169 TRACE("\tEVPD: %d\n", prb->CDBByte[1] & 1);
170 TRACE("\tLUN: %d\n", (prb->CDBByte[1] & 0xc) >> 1);
171 TRACE("\tPAGE CODE: %d\n", prb->CDBByte[2]);
172 TRACE("\tALLOCATION LENGTH: %d\n", prb->CDBByte[4]);
173 TRACE("\tCONTROL: %d\n", prb->CDBByte[5]);
174 TRACE("}\n");
175 break;
176 case CMD_SCAN_SCAN:
177 TRACE("Transfer Length: %d\n", prb->CDBByte[4]);
178 break;
181 TRACE("Host Adapter: %d\n", prb->SRB_HaId);
182 TRACE("Flags: %d\n", prb->SRB_Flags);
183 if (TARGET_TO_HOST(prb)) {
184 TRACE("\tData transfer: Target to host. Length checked.\n");
186 else if (HOST_TO_TARGET(prb)) {
187 TRACE("\tData transfer: Host to target. Length checked.\n");
189 else if (NO_DATA_TRANSFERRED(prb)) {
190 TRACE("\tData transfer: none\n");
192 else {
193 WARN("\tTransfer by scsi cmd. Length not checked\n");
196 TRACE("\tResidual byte length reporting %s\n", prb->SRB_Flags & 0x4 ? "enabled" : "disabled");
197 TRACE("\tLinking %s\n", prb->SRB_Flags & 0x2 ? "enabled" : "disabled");
198 TRACE("\tPosting %s\n", prb->SRB_Flags & 0x1 ? "enabled" : "disabled");
199 TRACE("Target: %d\n", prb->SRB_Target);
200 TRACE("Lun: %d\n", prb->SRB_Lun);
201 TRACE("BufLen: %d\n", prb->SRB_BufLen);
202 TRACE("SenseLen: %d\n", prb->SRB_SenseLen);
203 TRACE("BufPtr: %x (%p)\n", prb->SRB_BufPointer, lpBuf);
204 TRACE("LinkPointer %x\n", prb->SRB_Rsvd1);
205 TRACE("CDB Length: %d\n", prb->SRB_CDBLen);
206 TRACE("POST Proc: %x\n", (DWORD) prb->SRB_PostProc);
207 cdb = &prb->CDBByte[0];
208 if (TRACE_ON(aspi))
210 TRACE("CDB buffer[");
211 for (i = 0; i < prb->SRB_CDBLen; i++) {
212 if (i != 0) TRACE(",");
213 TRACE("%02x", *cdb++);
215 TRACE("]\n");
219 static void
220 ASPI_PrintSenseArea16(SRB_ExecSCSICmd16 *prb)
222 int i;
223 BYTE *cdb;
225 if (TRACE_ON(aspi))
227 cdb = &prb->CDBByte[0];
228 TRACE("SenseArea[");
229 for (i = 0; i < prb->SRB_SenseLen; i++) {
230 if (i) TRACE(",");
231 TRACE("%02x", *cdb++);
233 TRACE("]\n");
237 static void
238 ASPI_DebugPrintResult(SRB_ExecSCSICmd16 *prb, UINT16 mode)
240 BYTE *lpBuf = PTR_TO_LIN( prb->SRB_BufPointer, mode );
242 switch (prb->CDBByte[0]) {
243 case CMD_INQUIRY:
244 TRACE("Vendor: '%s'\n", lpBuf + INQUIRY_VENDOR);
245 break;
246 case CMD_TEST_UNIT_READY:
247 ASPI_PrintSenseArea16(prb);
248 break;
252 static WORD
253 ASPI_ExecScsiCmd(DWORD ptrPRB, UINT16 mode)
255 SRB_ExecSCSICmd16 *lpPRB = PTR_TO_LIN( ptrPRB, mode );
256 struct sg_header *sg_hd, *sg_reply_hdr;
257 int status;
258 BYTE *lpBuf = 0;
259 int in_len, out_len;
260 int error_code = 0;
261 int fd;
263 ASPI_DebugPrintCmd(lpPRB, mode);
265 fd = ASPI_OpenDevice16(lpPRB);
266 if (fd == -1) {
267 WARN("Failed: could not open device. Device permissions !?\n");
268 lpPRB->SRB_Status = SS_ERR;
269 return SS_ERR;
272 sg_hd = NULL;
273 sg_reply_hdr = NULL;
275 lpPRB->SRB_Status = SS_PENDING;
276 lpBuf = PTR_TO_LIN( lpPRB->SRB_BufPointer, mode );
278 if (!lpPRB->SRB_CDBLen) {
279 WARN("Failed: lpPRB->SRB_CDBLen = 0.\n");
280 lpPRB->SRB_Status = SS_ERR;
281 return SS_ERR;
284 /* build up sg_header + scsi cmd */
285 if (HOST_TO_TARGET(lpPRB)) {
286 /* send header, command, and then data */
287 in_len = SCSI_OFF + lpPRB->SRB_CDBLen + lpPRB->SRB_BufLen;
288 sg_hd = HeapAlloc(GetProcessHeap(), 0, in_len);
289 memset(sg_hd, 0, SCSI_OFF);
290 memcpy(sg_hd + 1, &lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
291 if (lpPRB->SRB_BufLen) {
292 memcpy(((BYTE *) sg_hd) + SCSI_OFF + lpPRB->SRB_CDBLen, lpBuf, lpPRB->SRB_BufLen);
295 else {
296 /* send header and command - no data */
297 in_len = SCSI_OFF + lpPRB->SRB_CDBLen;
298 sg_hd = HeapAlloc(GetProcessHeap(), 0, in_len);
299 memset(sg_hd, 0, SCSI_OFF);
300 memcpy(sg_hd + 1, &lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
303 if (TARGET_TO_HOST(lpPRB)) {
304 out_len = SCSI_OFF + lpPRB->SRB_BufLen;
305 sg_reply_hdr = HeapAlloc(GetProcessHeap(), 0, out_len);
306 memset(sg_reply_hdr, 0, SCSI_OFF);
307 sg_hd->reply_len = out_len;
309 else {
310 out_len = SCSI_OFF;
311 sg_reply_hdr = HeapAlloc(GetProcessHeap(), 0, out_len);
312 memset(sg_reply_hdr, 0, SCSI_OFF);
313 sg_hd->reply_len = out_len;
316 status = write(fd, sg_hd, in_len);
317 if (status < 0 || status != in_len) {
318 int save_error = errno;
320 WARN("Not enough bytes written to scsi device bytes=%d .. %d\n", in_len, status);
321 if (status < 0) {
322 if (save_error == ENOMEM) {
323 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");
325 WARN("error:= '%s'\n", strerror(save_error));
327 goto error_exit;
330 status = read(fd, sg_reply_hdr, out_len);
331 if (status < 0 || status != out_len) {
332 WARN("not enough bytes read from scsi device%d\n", status);
333 goto error_exit;
336 if (sg_reply_hdr->result != 0) {
337 error_code = sg_reply_hdr->result;
338 WARN("reply header error (%d)\n", sg_reply_hdr->result);
339 goto error_exit;
342 if (TARGET_TO_HOST(lpPRB) && lpPRB->SRB_BufLen) {
343 memcpy(lpBuf, sg_reply_hdr + 1, lpPRB->SRB_BufLen);
346 /* copy in sense buffer to amount that is available in client */
347 if (lpPRB->SRB_SenseLen) {
348 int sense_len = lpPRB->SRB_SenseLen;
349 if (lpPRB->SRB_SenseLen > 16)
350 sense_len = 16;
351 memcpy(SENSE_BUFFER(lpPRB), &sg_reply_hdr->sense_buffer[0], sense_len);
355 lpPRB->SRB_Status = SS_COMP;
356 lpPRB->SRB_HaStat = HASTAT_OK;
357 lpPRB->SRB_TargStat = STATUS_GOOD;
359 /* now do posting */
361 if (ASPI_POSTING(lpPRB) && lpPRB->SRB_PostProc) {
362 TRACE("Post Routine (%x) called\n", (DWORD) lpPRB->SRB_PostProc);
363 switch (mode)
365 case ASPI_DOS:
367 SEGPTR spPRB = MapLS(lpPRB);
369 WOWCallback16((DWORD)lpPRB->SRB_PostProc, spPRB);
370 UnMapLS(spPRB);
371 break;
373 case ASPI_WIN16:
374 WOWCallback16((DWORD)lpPRB->SRB_PostProc, ptrPRB);
375 break;
379 HeapFree(GetProcessHeap(), 0, sg_reply_hdr);
380 HeapFree(GetProcessHeap(), 0, sg_hd);
381 ASPI_DebugPrintResult(lpPRB, mode);
382 return SS_COMP;
384 error_exit:
385 if (error_code == EBUSY) {
386 lpPRB->SRB_Status = SS_ASPI_IS_BUSY;
387 TRACE("Device busy\n");
389 else {
390 WARN("Failed\n");
391 lpPRB->SRB_Status = SS_ERR;
394 /* I'm not sure exactly error codes work here
395 * We probably should set lpPRB->SRB_TargStat, SRB_HaStat ?
397 WARN("error_exit\n");
398 HeapFree(GetProcessHeap(), 0, sg_reply_hdr);
399 HeapFree(GetProcessHeap(), 0, sg_hd);
400 return lpPRB->SRB_Status;
402 #endif
405 /***********************************************************************
406 * GetASPISupportInfo (WINASPI.1)
409 WORD WINAPI GetASPISupportInfo16(void)
411 #ifdef linux
412 TRACE("GETASPISupportInfo16\n");
413 /* high byte SS_COMP - low byte number of host adapters */
414 return ((SS_COMP << 8) | HA_Count);
415 #else
416 return ((SS_NO_ASPI << 8) | 0);
417 #endif
421 static DWORD ASPI_SendASPICommand(DWORD ptrSRB, UINT16 mode)
423 #ifdef linux
424 LPSRB16 lpSRB = PTR_TO_LIN( ptrSRB, mode );
425 static const char szId[] = "Wine ASPI16";
426 static const char szWh[] = "Wine host";
428 if (mode == ASPI_WIN16 && ASPIChainFunc)
430 /* This is not the post proc, it's the chain proc this time */
431 DWORD ret = WOWCallback16((DWORD)ASPIChainFunc, ptrSRB);
432 if (ret)
434 lpSRB->inquiry.SRB_Status = SS_INVALID_SRB;
435 return ret;
439 switch (lpSRB->common.SRB_Cmd) {
440 case SC_HA_INQUIRY:
441 lpSRB->inquiry.SRB_Status = SS_COMP; /* completed successfully */
442 if (lpSRB->inquiry.SRB_55AASignature == 0x55aa) {
443 TRACE("Extended request detected (Adaptec's ASPIxDOS).\nWe don't support it at the moment.\n");
445 lpSRB->inquiry.SRB_ExtBufferSize = 0x2000; /* bogus value */
446 lpSRB->inquiry.HA_Count = HA_Count;
447 lpSRB->inquiry.HA_SCSI_ID = 7; /* not always ID 7 */
448 memcpy(lpSRB->inquiry.HA_ManagerId, szId, sizeof szId); /* max 15 chars */
449 memcpy(lpSRB->inquiry.HA_Identifier, szWh, sizeof szWh); /* FIXME: return host
450 adapter name */
451 memset(lpSRB->inquiry.HA_Unique, 0, 16); /* default HA_Unique content */
452 lpSRB->inquiry.HA_Unique[6] = 0x02; /* Maximum Transfer Length (128K, Byte> 4-7) */
453 FIXME("ASPI: Partially implemented SC_HA_INQUIRY for adapter %d.\n", lpSRB->inquiry.SRB_HaId);
454 return SS_COMP;
455 case SC_GET_DEV_TYPE:
456 FIXME("Not implemented SC_GET_DEV_TYPE\n");
457 break;
458 case SC_EXEC_SCSI_CMD:
459 return ASPI_ExecScsiCmd(ptrSRB, mode);
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 * SendASPICommand (WINASPI.2)
474 WORD WINAPI SendASPICommand16(SEGPTR segptr_srb)
476 return ASPI_SendASPICommand(segptr_srb, ASPI_WIN16);
480 /***********************************************************************
481 * InsertInASPIChain (WINASPI.3)
483 WORD WINAPI InsertInASPIChain16(BOOL16 remove, FARPROC16 pASPIChainFunc)
485 #ifdef linux
486 if (remove) /* Remove */
488 if (ASPIChainFunc == pASPIChainFunc)
490 ASPIChainFunc = NULL;
491 return SS_COMP;
494 else /* Insert */
496 if (ASPIChainFunc == NULL)
498 ASPIChainFunc = pASPIChainFunc;
499 return SS_COMP;
502 #endif
503 return SS_ERR;
507 /***********************************************************************
508 * GETASPIDLLVERSION (WINASPI.4)
511 DWORD WINAPI GetASPIDLLVersion16(void)
513 #ifdef linux
514 return 2;
515 #else
516 return 0;
517 #endif