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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <sys/types.h>
36 #include "wine/windef16.h"
41 #include "wine/winaspi.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(aspi);
48 * 1) Residual byte length reporting not handled
49 * 2) Make this code re-entrant for multithreading
50 * 3) Only linux supported so far
55 #define PTR_TO_LIN(ptr,mode) \
56 ((mode) == ASPI_DOS ? ((void*)(((unsigned int)SELECTOROF(ptr) << 4) + OFFSETOF(ptr))) : MapSL(ptr))
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 */
64 ASPI_OpenDevice16(SRB_ExecSCSICmd16 *prb)
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) {
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);
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, device_str, &count )) device_str[0] = 0;
99 TRACE("Trying to open unlisted scsi device %s\n", idstr);
103 TRACE("Opening device %s=%s\n", idstr, device_str);
105 fd = open(device_str, O_RDWR);
107 int save_error = errno;
108 ERR("Error opening device %s, error '%s'\n", device_str, strerror(save_error));
112 /* device is now open */
113 curr = HeapAlloc( GetProcessHeap(), 0, sizeof(ASPI_DEVICE_INFO) );
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;
127 ASPI_DebugPrintCmd(SRB_ExecSCSICmd16 *prb, UINT16 mode)
132 BYTE *lpBuf = PTR_TO_LIN( prb->SRB_BufPointer, mode );
134 switch (prb->CDBByte[0]) {
137 TRACE("\tEVPD: %d\n", prb->CDBByte[1] & 1);
138 TRACE("\tLUN: %d\n", (prb->CDBByte[1] & 0xc) >> 1);
139 TRACE("\tPAGE CODE: %d\n", prb->CDBByte[2]);
140 TRACE("\tALLOCATION LENGTH: %d\n", prb->CDBByte[4]);
141 TRACE("\tCONTROL: %d\n", prb->CDBByte[5]);
145 TRACE("Transfer Length: %d\n", prb->CDBByte[4]);
149 TRACE("Host Adapter: %d\n", prb->SRB_HaId);
150 TRACE("Flags: %d\n", prb->SRB_Flags);
151 if (TARGET_TO_HOST(prb)) {
152 TRACE("\tData transfer: Target to host. Length checked.\n");
154 else if (HOST_TO_TARGET(prb)) {
155 TRACE("\tData transfer: Host to target. Length checked.\n");
157 else if (NO_DATA_TRANSFERRED(prb)) {
158 TRACE("\tData transfer: none\n");
161 WARN("\tTransfer by scsi cmd. Length not checked\n");
164 TRACE("\tResidual byte length reporting %s\n", prb->SRB_Flags & 0x4 ? "enabled" : "disabled");
165 TRACE("\tLinking %s\n", prb->SRB_Flags & 0x2 ? "enabled" : "disabled");
166 TRACE("\tPosting %s\n", prb->SRB_Flags & 0x1 ? "enabled" : "disabled");
167 TRACE("Target: %d\n", prb->SRB_Target);
168 TRACE("Lun: %d\n", prb->SRB_Lun);
169 TRACE("BufLen: %ld\n", prb->SRB_BufLen);
170 TRACE("SenseLen: %d\n", prb->SRB_SenseLen);
171 TRACE("BufPtr: %lx (%p)\n", prb->SRB_BufPointer, lpBuf);
172 TRACE("LinkPointer %lx\n", prb->SRB_Rsvd1);
173 TRACE("CDB Length: %d\n", prb->SRB_CDBLen);
174 TRACE("POST Proc: %lx\n", (DWORD) prb->SRB_PostProc);
175 cdb = &prb->CDBByte[0];
176 cmd = prb->CDBByte[0];
179 TRACE("CDB buffer[");
180 for (i = 0; i < prb->SRB_CDBLen; i++) {
181 if (i != 0) TRACE(",");
182 TRACE("%02x", *cdb++);
189 ASPI_PrintSenseArea16(SRB_ExecSCSICmd16 *prb)
196 cdb = &prb->CDBByte[0];
198 for (i = 0; i < prb->SRB_SenseLen; i++) {
200 TRACE("%02x", *cdb++);
207 ASPI_DebugPrintResult(SRB_ExecSCSICmd16 *prb, UINT16 mode)
209 BYTE *lpBuf = PTR_TO_LIN( prb->SRB_BufPointer, mode );
211 switch (prb->CDBByte[0]) {
213 TRACE("Vendor: '%s'\n", lpBuf + INQUIRY_VENDOR);
215 case CMD_TEST_UNIT_READY:
216 ASPI_PrintSenseArea16(prb);
222 ASPI_ExecScsiCmd(DWORD ptrPRB, UINT16 mode)
224 SRB_ExecSCSICmd16 *lpPRB = PTR_TO_LIN( ptrPRB, mode );
225 struct sg_header *sg_hd, *sg_reply_hdr;
232 ASPI_DebugPrintCmd(lpPRB, mode);
234 fd = ASPI_OpenDevice16(lpPRB);
236 WARN("Failed: could not open device. Device permissions !?\n");
237 lpPRB->SRB_Status = SS_ERR;
244 lpPRB->SRB_Status = SS_PENDING;
245 lpBuf = PTR_TO_LIN( lpPRB->SRB_BufPointer, mode );
247 if (!lpPRB->SRB_CDBLen) {
248 WARN("Failed: lpPRB->SRB_CDBLen = 0.\n");
249 lpPRB->SRB_Status = SS_ERR;
253 /* build up sg_header + scsi cmd */
254 if (HOST_TO_TARGET(lpPRB)) {
255 /* send header, command, and then data */
256 in_len = SCSI_OFF + lpPRB->SRB_CDBLen + lpPRB->SRB_BufLen;
257 sg_hd = (struct sg_header *) malloc(in_len);
258 memset(sg_hd, 0, SCSI_OFF);
259 memcpy(sg_hd + 1, &lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
260 if (lpPRB->SRB_BufLen) {
261 memcpy(((BYTE *) sg_hd) + SCSI_OFF + lpPRB->SRB_CDBLen, lpBuf, lpPRB->SRB_BufLen);
265 /* send header and command - no data */
266 in_len = SCSI_OFF + lpPRB->SRB_CDBLen;
267 sg_hd = (struct sg_header *) malloc(in_len);
268 memset(sg_hd, 0, SCSI_OFF);
269 memcpy(sg_hd + 1, &lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
272 if (TARGET_TO_HOST(lpPRB)) {
273 out_len = SCSI_OFF + lpPRB->SRB_BufLen;
274 sg_reply_hdr = (struct sg_header *) malloc(out_len);
275 memset(sg_reply_hdr, 0, SCSI_OFF);
276 sg_hd->reply_len = out_len;
280 sg_reply_hdr = (struct sg_header *) malloc(out_len);
281 memset(sg_reply_hdr, 0, SCSI_OFF);
282 sg_hd->reply_len = out_len;
285 status = write(fd, sg_hd, in_len);
286 if (status < 0 || status != in_len) {
287 int save_error = errno;
289 WARN("Not enough bytes written to scsi device bytes=%d .. %d\n", in_len, status);
291 if (save_error == ENOMEM) {
292 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");
294 WARN("error:= '%s'\n", strerror(save_error));
299 status = read(fd, sg_reply_hdr, out_len);
300 if (status < 0 || status != out_len) {
301 WARN("not enough bytes read from scsi device%d\n", status);
305 if (sg_reply_hdr->result != 0) {
306 error_code = sg_reply_hdr->result;
307 WARN("reply header error (%d)\n", sg_reply_hdr->result);
311 if (TARGET_TO_HOST(lpPRB) && lpPRB->SRB_BufLen) {
312 memcpy(lpBuf, sg_reply_hdr + 1, lpPRB->SRB_BufLen);
315 /* copy in sense buffer to amount that is available in client */
316 if (lpPRB->SRB_SenseLen) {
317 int sense_len = lpPRB->SRB_SenseLen;
318 if (lpPRB->SRB_SenseLen > 16)
320 memcpy(SENSE_BUFFER(lpPRB), &sg_reply_hdr->sense_buffer[0], sense_len);
324 lpPRB->SRB_Status = SS_COMP;
325 lpPRB->SRB_HaStat = HASTAT_OK;
326 lpPRB->SRB_TargStat = STATUS_GOOD;
330 if (ASPI_POSTING(lpPRB) && lpPRB->SRB_PostProc) {
331 TRACE("Post Routine (%lx) called\n", (DWORD) lpPRB->SRB_PostProc);
336 SEGPTR spPRB = MapLS(lpPRB);
338 WOWCallback16((DWORD)lpPRB->SRB_PostProc, spPRB);
343 WOWCallback16((DWORD)lpPRB->SRB_PostProc, ptrPRB);
350 ASPI_DebugPrintResult(lpPRB, mode);
354 if (error_code == EBUSY) {
355 lpPRB->SRB_Status = SS_ASPI_IS_BUSY;
356 TRACE("Device busy\n");
360 lpPRB->SRB_Status = SS_ERR;
363 /* I'm not sure exactly error codes work here
364 * We probably should set lpPRB->SRB_TargStat, SRB_HaStat ?
366 WARN("error_exit\n");
369 return lpPRB->SRB_Status;
374 /***********************************************************************
375 * GetASPISupportInfo (WINASPI.1)
378 WORD WINAPI GetASPISupportInfo16(void)
381 TRACE("GETASPISupportInfo16\n");
382 /* high byte SS_COMP - low byte number of host adapters */
383 return ((SS_COMP << 8) | HA_Count);
385 return ((SS_NO_ASPI << 8) | 0);
390 DWORD ASPI_SendASPICommand(DWORD ptrSRB, UINT16 mode)
393 LPSRB16 lpSRB = PTR_TO_LIN( ptrSRB, mode );
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);
401 lpSRB->inquiry.SRB_Status = SS_INVALID_SRB;
406 switch (lpSRB->common.SRB_Cmd) {
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 strcat(lpSRB->inquiry.HA_ManagerId, "Wine ASPI16"); /* max 15 chars */
416 strcat(lpSRB->inquiry.HA_Identifier, "Wine host"); /* FIXME: return host
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);
422 case SC_GET_DEV_TYPE:
423 FIXME("Not implemented SC_GET_DEV_TYPE\n");
425 case SC_EXEC_SCSI_CMD:
426 return ASPI_ExecScsiCmd((DWORD)ptrSRB, mode);
429 FIXME("Not implemented SC_RESET_DEV\n");
432 FIXME("Unknown command %d\n", lpSRB->common.SRB_Cmd);
435 return SS_INVALID_SRB;
439 /***********************************************************************
440 * SendASPICommand (WINASPI.2)
442 WORD WINAPI SendASPICommand16(SEGPTR segptr_srb)
445 return ASPI_SendASPICommand(segptr_srb, ASPI_WIN16);
452 /***********************************************************************
453 * InsertInASPIChain (WINASPI.3)
455 WORD WINAPI InsertInASPIChain16(BOOL16 remove, FARPROC16 pASPIChainFunc)
458 if (remove == TRUE) /* Remove */
460 if (ASPIChainFunc == pASPIChainFunc)
462 ASPIChainFunc = NULL;
467 if (remove == FALSE) /* Insert */
469 if (ASPIChainFunc == NULL)
471 ASPIChainFunc = pASPIChainFunc;
480 /***********************************************************************
481 * GETASPIDLLVERSION (WINASPI.4)
484 DWORD WINAPI GetASPIDLLVersion16(void)