fdopen: don't rewind the file after creating the FILE* handle. Added
[wine] / dlls / winaspi / winaspi16.c
1 /*
2  * Copyright 1997 Bruce Milner
3  * Copyright 1998 Andreas Mohr
4  *
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.
9  *
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.
14  *
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
18  */
19
20 #include "config.h"
21
22 #include <stdlib.h>
23 #include <sys/types.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <memory.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #include <string.h>
31 #include <stdio.h>
32
33 #include "winbase.h"
34 #include "wine/windef16.h"
35 #include "winreg.h"
36 #include "aspi.h"
37 #include "winescsi.h"
38 #include "wine/winaspi.h"
39 #include "wine/debug.h"
40 #include "miscemu.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(aspi);
43
44
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
49  */
50
51 /* ### start build ### */
52 extern LONG CALLBACK ASPI_CallTo16_long_l(FARPROC16,SEGPTR);
53 /* ### stop build ### */
54
55 #ifdef linux
56
57 static ASPI_DEVICE_INFO *ASPI_open_devices = NULL;
58
59 static FARPROC16 ASPIChainFunc = NULL;
60 static WORD HA_Count = 1; /* host adapter count; FIXME: detect it */
61
62 static int
63 ASPI_OpenDevice16(SRB_ExecSCSICmd16 *prb)
64 {
65     HKEY hkey;
66     int fd;
67     char        idstr[50];
68     char        device_str[50];
69     ASPI_DEVICE_INFO *curr;
70
71     /* search list of devices to see if we've opened it already.
72      * There is not an explicit open/close in ASPI land, so hopefully
73      * keeping a device open won't be a problem.
74      */
75
76     for (curr = ASPI_open_devices; curr; curr = curr->next) {
77         if (curr->hostId == prb->SRB_HaId &&
78             curr->target == prb->SRB_Target &&
79             curr->lun == prb->SRB_Lun) {
80             return curr->fd;
81         }
82     }
83
84     /* device wasn't cached, go ahead and open it */
85     sprintf( idstr, "Software\\Wine\\Wine\\Config\\scsi c%1dt%1dd%1d",
86              prb->SRB_HaId, prb->SRB_Target, prb->SRB_Lun);
87
88     device_str[0] = 0;
89     if (!RegOpenKeyExA( HKEY_LOCAL_MACHINE, idstr, 0, KEY_ALL_ACCESS, &hkey ))
90     {
91         DWORD type, count = sizeof(device_str);
92         if (RegQueryValueExA( hkey, "Device", 0, &type, device_str, &count )) device_str[0] = 0;
93         RegCloseKey( hkey );
94     }
95
96     if (!device_str[0])
97     {
98         TRACE("Trying to open unlisted scsi device %s\n", idstr);
99         return -1;
100     }
101
102     TRACE("Opening device %s=%s\n", idstr, device_str);
103
104     fd = open(device_str, O_RDWR);
105     if (fd == -1) {
106         int save_error = errno;
107         ERR("Error opening device %s, error '%s'\n", device_str, strerror(save_error));
108         return -1;
109     }
110
111     /* device is now open */
112     curr = HeapAlloc( GetProcessHeap(), 0, sizeof(ASPI_DEVICE_INFO) );
113     curr->fd = fd;
114     curr->hostId = prb->SRB_HaId;
115     curr->target = prb->SRB_Target;
116     curr->lun = prb->SRB_Lun;
117
118     /* insert new record at beginning of open device list */
119     curr->next = ASPI_open_devices;
120     ASPI_open_devices = curr;
121     return fd;
122 }
123
124
125 static void
126 ASPI_DebugPrintCmd(SRB_ExecSCSICmd16 *prb, UINT16 mode)
127 {
128   BYTE  cmd;
129   int   i;
130   BYTE *cdb;
131   BYTE *lpBuf = 0;
132
133   switch (mode)
134   {
135       case ASPI_DOS:
136         /* translate real mode address */
137         if (prb->SRB_BufPointer)
138             lpBuf = PTR_REAL_TO_LIN( SELECTOROF(prb->SRB_BufPointer), OFFSETOF(prb->SRB_BufPointer));
139         break;
140       case ASPI_WIN16:
141         lpBuf = MapSL(prb->SRB_BufPointer);
142         break;
143   }
144
145   switch (prb->CDBByte[0]) {
146   case CMD_INQUIRY:
147     TRACE("{\n");
148     TRACE("\tEVPD: %d\n", prb->CDBByte[1] & 1);
149     TRACE("\tLUN: %d\n", (prb->CDBByte[1] & 0xc) >> 1);
150     TRACE("\tPAGE CODE: %d\n", prb->CDBByte[2]);
151     TRACE("\tALLOCATION LENGTH: %d\n", prb->CDBByte[4]);
152     TRACE("\tCONTROL: %d\n", prb->CDBByte[5]);
153     TRACE("}\n");
154     break;
155   case CMD_SCAN_SCAN:
156     TRACE("Transfer Length: %d\n", prb->CDBByte[4]);
157     break;
158   }
159
160   TRACE("Host Adapter: %d\n", prb->SRB_HaId);
161   TRACE("Flags: %d\n", prb->SRB_Flags);
162   if (TARGET_TO_HOST(prb)) {
163     TRACE("\tData transfer: Target to host. Length checked.\n");
164   }
165   else if (HOST_TO_TARGET(prb)) {
166     TRACE("\tData transfer: Host to target. Length checked.\n");
167   }
168   else if (NO_DATA_TRANSFERED(prb)) {
169     TRACE("\tData transfer: none\n");
170   }
171   else {
172     WARN("\tTransfer by scsi cmd. Length not checked\n");
173   }
174
175   TRACE("\tResidual byte length reporting %s\n", prb->SRB_Flags & 0x4 ? "enabled" : "disabled");
176   TRACE("\tLinking %s\n", prb->SRB_Flags & 0x2 ? "enabled" : "disabled");
177   TRACE("\tPosting %s\n", prb->SRB_Flags & 0x1 ? "enabled" : "disabled");
178   TRACE("Target: %d\n", prb->SRB_Target);
179   TRACE("Lun: %d\n", prb->SRB_Lun);
180   TRACE("BufLen: %ld\n", prb->SRB_BufLen);
181   TRACE("SenseLen: %d\n", prb->SRB_SenseLen);
182   TRACE("BufPtr: %lx (%p)\n", prb->SRB_BufPointer, lpBuf);
183   TRACE("LinkPointer %lx\n", prb->SRB_Rsvd1);
184   TRACE("CDB Length: %d\n", prb->SRB_CDBLen);
185   TRACE("POST Proc: %lx\n", (DWORD) prb->SRB_PostProc);
186   cdb = &prb->CDBByte[0];
187   cmd = prb->CDBByte[0];
188   if (TRACE_ON(aspi))
189   {
190       DPRINTF("CDB buffer[");
191       for (i = 0; i < prb->SRB_CDBLen; i++) {
192           if (i != 0) DPRINTF(",");
193           DPRINTF("%02x", *cdb++);
194       }
195       DPRINTF("]\n");
196   }
197 }
198
199 static void
200 ASPI_PrintSenseArea16(SRB_ExecSCSICmd16 *prb)
201 {
202   int   i;
203   BYTE *cdb;
204
205   if (TRACE_ON(aspi))
206   {
207       cdb = &prb->CDBByte[0];
208       DPRINTF("SenseArea[");
209       for (i = 0; i < prb->SRB_SenseLen; i++) {
210           if (i) DPRINTF(",");
211           DPRINTF("%02x", *cdb++);
212       }
213       DPRINTF("]\n");
214   }
215 }
216
217 static void
218 ASPI_DebugPrintResult(SRB_ExecSCSICmd16 *prb, UINT16 mode)
219 {
220   BYTE *lpBuf = 0;
221
222   switch (mode)
223   {
224       case ASPI_DOS:
225         /* translate real mode address */
226         if (prb->SRB_BufPointer)
227             lpBuf = PTR_REAL_TO_LIN( SELECTOROF(prb->SRB_BufPointer), OFFSETOF(prb->SRB_BufPointer));
228         break;
229       case ASPI_WIN16:
230         lpBuf = MapSL(prb->SRB_BufPointer);
231         break;
232   }
233
234   switch (prb->CDBByte[0]) {
235   case CMD_INQUIRY:
236     TRACE("Vendor: '%s'\n", lpBuf + INQUIRY_VENDOR);
237     break;
238   case CMD_TEST_UNIT_READY:
239     ASPI_PrintSenseArea16(prb);
240     break;
241   }
242 }
243
244 static WORD
245 ASPI_ExecScsiCmd(DWORD ptrPRB, UINT16 mode)
246 {
247   SRB_ExecSCSICmd16 *lpPRB = 0;
248   struct sg_header *sg_hd, *sg_reply_hdr;
249   int   status;
250   BYTE *lpBuf = 0;
251   int   in_len, out_len;
252   int   error_code = 0;
253   int   fd;
254
255   switch (mode)
256   {
257       case ASPI_DOS:
258         if (ptrPRB)
259             lpPRB = PTR_REAL_TO_LIN( SELECTOROF(ptrPRB), OFFSETOF(ptrPRB));
260         break;
261       case ASPI_WIN16:
262         lpPRB = MapSL(ptrPRB);
263         break;
264   }
265
266   ASPI_DebugPrintCmd(lpPRB, mode);
267
268   fd = ASPI_OpenDevice16(lpPRB);
269   if (fd == -1) {
270       WARN("Failed: could not open device. Device permissions !?\n");
271       lpPRB->SRB_Status = SS_ERR;
272       return SS_ERR;
273   }
274
275   sg_hd = NULL;
276   sg_reply_hdr = NULL;
277
278   lpPRB->SRB_Status = SS_PENDING;
279
280   switch (mode)
281   {
282       case ASPI_DOS:
283         /* translate real mode address */
284         if (ptrPRB)
285             lpBuf = PTR_REAL_TO_LIN( SELECTOROF(lpPRB->SRB_BufPointer),
286                                      OFFSETOF(lpPRB->SRB_BufPointer));
287         break;
288       case ASPI_WIN16:
289         lpBuf = MapSL(lpPRB->SRB_BufPointer);
290         break;
291   }
292
293   if (!lpPRB->SRB_CDBLen) {
294       WARN("Failed: lpPRB->SRB_CDBLen = 0.\n");
295       lpPRB->SRB_Status = SS_ERR;
296       return SS_ERR;
297   }
298
299   /* build up sg_header + scsi cmd */
300   if (HOST_TO_TARGET(lpPRB)) {
301     /* send header, command, and then data */
302     in_len = SCSI_OFF + lpPRB->SRB_CDBLen + lpPRB->SRB_BufLen;
303     sg_hd = (struct sg_header *) malloc(in_len);
304     memset(sg_hd, 0, SCSI_OFF);
305     memcpy(sg_hd + 1, &lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
306     if (lpPRB->SRB_BufLen) {
307       memcpy(((BYTE *) sg_hd) + SCSI_OFF + lpPRB->SRB_CDBLen, lpBuf, lpPRB->SRB_BufLen);
308     }
309   }
310   else {
311     /* send header and command - no data */
312     in_len = SCSI_OFF + lpPRB->SRB_CDBLen;
313     sg_hd = (struct sg_header *) malloc(in_len);
314     memset(sg_hd, 0, SCSI_OFF);
315     memcpy(sg_hd + 1, &lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
316   }
317
318   if (TARGET_TO_HOST(lpPRB)) {
319     out_len = SCSI_OFF + lpPRB->SRB_BufLen;
320     sg_reply_hdr = (struct sg_header *) malloc(out_len);
321     memset(sg_reply_hdr, 0, SCSI_OFF);
322     sg_hd->reply_len = out_len;
323   }
324   else {
325     out_len = SCSI_OFF;
326     sg_reply_hdr = (struct sg_header *) malloc(out_len);
327     memset(sg_reply_hdr, 0, SCSI_OFF);
328     sg_hd->reply_len = out_len;
329   }
330
331   status = write(fd, sg_hd, in_len);
332   if (status < 0 || status != in_len) {
333       int save_error = errno;
334
335     WARN("Not enough bytes written to scsi device bytes=%d .. %d\n", in_len, status);
336     if (status < 0) {
337                 if (save_error == ENOMEM) {
338             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");
339         }
340                 WARN("error:= '%s'\n", strerror(save_error));
341     }
342     goto error_exit;
343   }
344
345   status = read(fd, sg_reply_hdr, out_len);
346   if (status < 0 || status != out_len) {
347     WARN("not enough bytes read from scsi device%d\n", status);
348     goto error_exit;
349   }
350
351   if (sg_reply_hdr->result != 0) {
352     error_code = sg_reply_hdr->result;
353     WARN("reply header error (%d)\n", sg_reply_hdr->result);
354     goto error_exit;
355   }
356
357   if (TARGET_TO_HOST(lpPRB) && lpPRB->SRB_BufLen) {
358     memcpy(lpBuf, sg_reply_hdr + 1, lpPRB->SRB_BufLen);
359   }
360
361   /* copy in sense buffer to amount that is available in client */
362   if (lpPRB->SRB_SenseLen) {
363     int sense_len = lpPRB->SRB_SenseLen;
364     if (lpPRB->SRB_SenseLen > 16)
365       sense_len = 16;
366     memcpy(SENSE_BUFFER(lpPRB), &sg_reply_hdr->sense_buffer[0], sense_len);
367   }
368
369
370   lpPRB->SRB_Status = SS_COMP;
371   lpPRB->SRB_HaStat = HASTAT_OK;
372   lpPRB->SRB_TargStat = STATUS_GOOD;
373
374   /* now do posting */
375
376   if (ASPI_POSTING(lpPRB) && lpPRB->SRB_PostProc) {
377     TRACE("Post Routine (%lx) called\n", (DWORD) lpPRB->SRB_PostProc);
378     switch (mode)
379     {
380       case ASPI_DOS:
381       {
382         SEGPTR spPRB = MapLS(lpPRB);
383
384         ASPI_CallTo16_long_l(lpPRB->SRB_PostProc, spPRB);
385         UnMapLS(spPRB);
386         break;
387       }
388       case ASPI_WIN16:
389         ASPI_CallTo16_long_l(lpPRB->SRB_PostProc, ptrPRB);
390         break;
391     }
392   }
393
394   free(sg_reply_hdr);
395   free(sg_hd);
396   ASPI_DebugPrintResult(lpPRB, mode);
397   return SS_COMP;
398
399 error_exit:
400   if (error_code == EBUSY) {
401       lpPRB->SRB_Status = SS_ASPI_IS_BUSY;
402       TRACE("Device busy\n");
403   }
404   else {
405       WARN("Failed\n");
406       lpPRB->SRB_Status = SS_ERR;
407   }
408
409   /* I'm not sure exactly error codes work here
410    * We probably should set lpPRB->SRB_TargStat, SRB_HaStat ?
411    */
412   WARN("error_exit\n");
413   free(sg_reply_hdr);
414   free(sg_hd);
415   return lpPRB->SRB_Status;
416 }
417 #endif
418
419
420 /***********************************************************************
421  *             GetASPISupportInfo   (WINASPI.1)
422  */
423
424 WORD WINAPI GetASPISupportInfo16(void)
425 {
426 #ifdef linux
427     TRACE("GETASPISupportInfo16\n");
428     /* high byte SS_COMP - low byte number of host adapters */
429     return ((SS_COMP << 8) | HA_Count);
430 #else
431     return ((SS_NO_ASPI << 8) | 0);
432 #endif
433 }
434
435
436 DWORD ASPI_SendASPICommand(DWORD ptrSRB, UINT16 mode)
437 {
438 #ifdef linux
439   LPSRB16 lpSRB = 0;
440
441   switch (mode)
442   {
443       case ASPI_DOS:
444         if (ptrSRB)
445             lpSRB = PTR_REAL_TO_LIN( SELECTOROF(ptrSRB), OFFSETOF(ptrSRB));
446         break;
447       case ASPI_WIN16:
448         lpSRB = MapSL(ptrSRB);
449         if (ASPIChainFunc)
450         {
451             /* This is not the post proc, it's the chain proc this time */
452             DWORD ret = ASPI_CallTo16_long_l(ASPIChainFunc, ptrSRB);
453             if (ret)
454             {
455                 lpSRB->inquiry.SRB_Status = SS_INVALID_SRB;
456                 return ret;
457             }
458         }
459         break;
460   }
461
462   switch (lpSRB->common.SRB_Cmd) {
463   case SC_HA_INQUIRY:
464     lpSRB->inquiry.SRB_Status = SS_COMP;       /* completed successfully */
465     if (lpSRB->inquiry.SRB_55AASignature == 0x55aa) {
466         TRACE("Extended request detected (Adaptec's ASPIxDOS).\nWe don't support it at the moment.\n");
467     }
468     lpSRB->inquiry.SRB_ExtBufferSize = 0x2000; /* bogus value */
469     lpSRB->inquiry.HA_Count = HA_Count;
470     lpSRB->inquiry.HA_SCSI_ID = 7;             /* not always ID 7 */
471     strcat(lpSRB->inquiry.HA_ManagerId, "Wine ASPI16"); /* max 15 chars */
472     strcat(lpSRB->inquiry.HA_Identifier, "Wine host"); /* FIXME: return host
473 adapter name */
474     memset(lpSRB->inquiry.HA_Unique, 0, 16); /* default HA_Unique content */
475     lpSRB->inquiry.HA_Unique[6] = 0x02; /* Maximum Transfer Length (128K, Byte> 4-7) */
476     FIXME("ASPI: Partially implemented SC_HA_INQUIRY for adapter %d.\n", lpSRB->inquiry.SRB_HaId);
477     return SS_COMP;
478   case SC_GET_DEV_TYPE:
479     FIXME("Not implemented SC_GET_DEV_TYPE\n");
480     break;
481   case SC_EXEC_SCSI_CMD:
482     return ASPI_ExecScsiCmd((DWORD)ptrSRB, mode);
483     break;
484   case SC_RESET_DEV:
485     FIXME("Not implemented SC_RESET_DEV\n");
486     break;
487   default:
488     FIXME("Unknown command %d\n", lpSRB->common.SRB_Cmd);
489   }
490 #endif
491   return SS_INVALID_SRB;
492 }
493
494
495 /***********************************************************************
496  *             SendASPICommand   (WINASPI.2)
497  */
498 WORD WINAPI SendASPICommand16(SEGPTR segptr_srb)
499 {
500 #ifdef linux
501     return ASPI_SendASPICommand(segptr_srb, ASPI_WIN16);
502 #else
503     return 0;
504 #endif
505 }
506
507
508 /***********************************************************************
509  *             InsertInASPIChain   (WINASPI.3)
510  */
511 WORD WINAPI InsertInASPIChain16(BOOL16 remove, FARPROC16 pASPIChainFunc)
512 {
513 #ifdef linux
514     if (remove == TRUE) /* Remove */
515     {
516         if (ASPIChainFunc == pASPIChainFunc)
517         {
518             ASPIChainFunc = NULL;
519             return SS_COMP;
520         }
521     }
522     else
523     if (remove == FALSE) /* Insert */
524     {
525         if (ASPIChainFunc == NULL)
526         {
527             ASPIChainFunc = pASPIChainFunc;
528             return SS_COMP;
529         }
530     }
531 #endif
532     return SS_ERR;
533 }
534
535
536 /***********************************************************************
537  *             GETASPIDLLVERSION   (WINASPI.4)
538  */
539
540 DWORD WINAPI GetASPIDLLVersion16()
541 {
542 #ifdef linux
543         return 2;
544 #else
545         return 0;
546 #endif
547 }
548