Release 980628
[wine] / misc / aspi.c
1 #include <stdlib.h>
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <errno.h>
5 #include <fcntl.h>
6 #include <ldt.h>
7 #include <memory.h>
8 #include <unistd.h>
9 #include <callback.h>
10 #include "windows.h"
11 #include "aspi.h"
12 #include "options.h"
13 #include "heap.h"
14 #include "debug.h"
15 #include "selectors.h"
16 #include "module.h"
17 #include "miscemu.h"
18
19
20 /* FIXME!
21  * 1) Residual byte length reporting not handled
22  * 2) Make this code re-entrant for multithreading
23  * 3) Only linux supported so far
24  */
25
26 #ifdef linux
27
28 /* This is a duplicate of the sg_header from /usr/src/linux/include/scsi/sg.h
29  * kernel 2.0.30
30  * This will probably break at some point, but for those who don't have
31  * kernels installed, I think this should still work.
32  *
33  */
34
35 struct sg_header
36  {
37   int pack_len;    /* length of incoming packet <4096 (including header) */
38   int reply_len;   /* maximum length <4096 of expected reply */
39   int pack_id;     /* id number of packet */
40   int result;      /* 0==ok, otherwise refer to errno codes */
41   unsigned int twelve_byte:1; /* Force 12 byte command length for group 6 & 7 commands  */
42   unsigned int other_flags:31;                  /* for future use */
43   unsigned char sense_buffer[16]; /* used only by reads */
44   /* command follows then data for command */
45  };
46
47 #define SCSI_OFF sizeof(struct sg_header)
48 #endif
49
50 #define ASPI_POSTING(prb) (prb->SRB_Flags & 0x1)
51
52 #define HOST_TO_TARGET(prb) (((prb->SRB_Flags>>3) & 0x3) == 0x2)
53 #define TARGET_TO_HOST(prb) (((prb->SRB_Flags>>3) & 0x3) == 0x1)
54 #define NO_DATA_TRANSFERED(prb) (((prb->SRB_Flags>>3) & 0x3) == 0x3)
55
56 #define SRB_ENABLE_RESIDUAL_COUNT 0x4
57
58 #define INQUIRY_VENDOR          8
59
60 #define MUSTEK_SCSI_AREA_AND_WINDOWS 0x04
61 #define MUSTEK_SCSI_READ_SCANNED_DATA 0x08
62 #define MUSTEK_SCSI_GET_IMAGE_STATUS 0x0f
63 #define MUSTEK_SCSI_ADF_AND_BACKTRACE 0x10
64 #define MUSTEK_SCSI_CCD_DISTANCE 0x11
65 #define MUSTEK_SCSI_START_STOP 0x1b
66
67 #define CMD_TEST_UNIT_READY 0x00
68 #define CMD_REQUEST_SENSE 0x03
69 #define CMD_INQUIRY 0x12
70
71 /* scanner commands - just for debug */
72 #define CMD_SCAN_GET_DATA_BUFFER_STATUS 0x34
73 #define CMD_SCAN_GET_WINDOW 0x25
74 #define CMD_SCAN_OBJECT_POSITION 0x31
75 #define CMD_SCAN_READ 0x28
76 #define CMD_SCAN_RELEASE_UNIT 0x17
77 #define CMD_SCAN_RESERVE_UNIT 0x16
78 #define CMD_SCAN_SCAN 0x1b
79 #define CMD_SCAN_SEND 0x2a
80 #define CMD_SCAN_CHANGE_DEFINITION 0x40
81
82 #define INQURIY_CMDLEN 6
83 #define INQURIY_REPLY_LEN 96
84 #define INQUIRY_VENDOR 8
85
86 #define SENSE_BUFFER(prb) (&prb->CDBByte[prb->SRB_CDBLen])
87
88
89 /* Just a container for seeing what devices are open */
90 struct ASPI_DEVICE_INFO {
91     struct ASPI_DEVICE_INFO *   next;
92     int                         fd;
93     int                         hostId;
94     int                         target;
95     int                         lun;
96 };
97
98 typedef struct ASPI_DEVICE_INFO ASPI_DEVICE_INFO;
99 static ASPI_DEVICE_INFO *ASPI_open_devices = NULL;
100
101 static BOOL16 DOSASPI = FALSE;
102
103 #ifdef linux
104 static int
105 ASPI_OpenDevice16(SRB_ExecSCSICmd16 *prb)
106 {
107     int fd;
108     char        idstr[20];
109     char        device_str[50];
110     ASPI_DEVICE_INFO *curr;
111
112     /* search list of devices to see if we've opened it already.
113      * There is not an explicit open/close in ASPI land, so hopefully
114      * keeping a device open won't be a problem.
115      */
116
117     for (curr = ASPI_open_devices; curr; curr = curr->next) {
118         if (curr->hostId == prb->SRB_HaId &&
119             curr->target == prb->SRB_Target &&
120             curr->lun == prb->SRB_Lun) {
121             return curr->fd;
122         }
123     }
124
125     /* device wasn't cached, go ahead and open it */
126     sprintf(idstr, "scsi c%1dt%1dd%1d", prb->SRB_HaId, prb->SRB_Target, prb->SRB_Lun);
127
128     if (!PROFILE_GetWineIniString(idstr, "Device", "", device_str, sizeof(device_str))) {
129         TRACE(aspi, "Trying to open unlisted scsi device %s\n", idstr);
130         return -1;
131     }
132
133     TRACE(aspi, "Opening device %s=%s\n", idstr, device_str);
134
135     fd = open(device_str, O_RDWR);
136     if (fd == -1) {
137         int save_error = errno;
138         WARN(aspi, "Error opening device errno=%d\n", save_error);
139         return -1;
140     }
141
142     /* device is now open */
143     curr = HeapAlloc( SystemHeap, 0, sizeof(ASPI_DEVICE_INFO) );
144     curr->fd = fd;
145     curr->hostId = prb->SRB_HaId;
146     curr->target = prb->SRB_Target;
147     curr->lun = prb->SRB_Lun;
148
149     /* insert new record at beginning of open device list */
150     curr->next = ASPI_open_devices;
151     ASPI_open_devices = curr;
152     return fd;
153 }
154
155
156 static void
157 ASPI_DebugPrintCmd16(SRB_ExecSCSICmd16 *prb)
158 {
159   BYTE  cmd;
160   int   i;
161   BYTE *cdb;
162   BYTE *lpBuf;
163   dbg_decl_str(aspi, 512);
164
165   if ((DOSASPI) && (prb->SRB_BufPointer)) /* translate real mode address */
166     lpBuf = (BYTE *)DOSMEM_MapRealToLinear((UINT32)prb->SRB_BufPointer);
167   else
168     lpBuf = PTR_SEG_TO_LIN(prb->SRB_BufPointer);
169
170   switch (prb->CDBByte[0]) {
171   case CMD_INQUIRY:
172     TRACE(aspi, "{\n");
173     TRACE(aspi, "\tEVPD: %d\n", prb->CDBByte[1] & 1);
174     TRACE(aspi, "\tLUN: %d\n", (prb->CDBByte[1] & 0xc) >> 1);
175     TRACE(aspi, "\tPAGE CODE: %d\n", prb->CDBByte[2]);
176     TRACE(aspi, "\tALLOCATION LENGTH: %d\n", prb->CDBByte[4]);
177     TRACE(aspi, "\tCONTROL: %d\n", prb->CDBByte[5]);
178     TRACE(aspi, "}\n");
179     break;
180   case CMD_SCAN_SCAN:
181     TRACE(aspi, "Transfer Length: %d\n", prb->CDBByte[4]);
182     break;
183   }
184
185   TRACE(aspi, "Host Adapter: %d\n", prb->SRB_HaId);
186   TRACE(aspi, "Flags: %d\n", prb->SRB_Flags);
187   if (TARGET_TO_HOST(prb)) {
188     TRACE(aspi, "\tData transfer: Target to host. Length checked.\n");
189   }
190   else if (HOST_TO_TARGET(prb)) {
191     TRACE(aspi, "\tData transfer: Host to target. Length checked.\n");
192   }
193   else if (NO_DATA_TRANSFERED(prb)) {
194     TRACE(aspi, "\tData transfer: none\n");
195   }
196   else {
197     WARN(aspi, "\tTransfer by scsi cmd. Length not checked\n");
198   }
199
200   TRACE(aspi, "\tResidual byte length reporting %s\n", prb->SRB_Flags & 0x4 ? "enabled" : "disabled");
201   TRACE(aspi, "\tLinking %s\n", prb->SRB_Flags & 0x2 ? "enabled" : "disabled");
202   TRACE(aspi, "\tPosting %s\n", prb->SRB_Flags & 0x1 ? "enabled" : "disabled");
203   TRACE(aspi, "Target: %d\n", prb->SRB_Target);
204   TRACE(aspi, "Lun: %d\n", prb->SRB_Lun);
205   TRACE(aspi, "BufLen: %ld\n", prb->SRB_BufLen);
206   TRACE(aspi, "SenseLen: %d\n", prb->SRB_SenseLen);
207   TRACE(aspi, "BufPtr: %lx (%p)\n", prb->SRB_BufPointer, lpBuf);
208   TRACE(aspi, "LinkPointer %lx\n", prb->SRB_Rsvd1);
209   TRACE(aspi, "CDB Length: %d\n", prb->SRB_CDBLen);
210   TRACE(aspi, "POST Proc: %lx\n", (DWORD) prb->SRB_PostProc);
211   cdb = &prb->CDBByte[0];
212   cmd = prb->CDBByte[0];
213   for (i = 0; i < prb->SRB_CDBLen; i++) {
214     if (i != 0) dsprintf(aspi, ",");
215     dsprintf(aspi, "%02x", *cdb++);
216   }
217   TRACE(aspi, "CDB buffer[%s]\n", dbg_str(aspi));
218 }
219
220 static void
221 PrintSenseArea16(SRB_ExecSCSICmd16 *prb)
222 {
223   int   i;
224   BYTE *cdb;
225   dbg_decl_str(aspi, 512);
226
227   cdb = &prb->CDBByte[0];
228   for (i = 0; i < prb->SRB_SenseLen; i++) {
229     if (i) dsprintf(aspi, ",");
230     dsprintf(aspi, "%02x", *cdb++);
231   }
232   TRACE(aspi, "SenseArea[%s]\n", dbg_str(aspi));
233 }
234
235 static void
236 ASPI_DebugPrintResult16(SRB_ExecSCSICmd16 *prb)
237 {
238   BYTE *lpBuf;
239
240   if ((DOSASPI) && (prb->SRB_BufPointer)) /* translate real mode address */
241     lpBuf = (BYTE *)DOSMEM_MapRealToLinear((UINT32)prb->SRB_BufPointer);
242   else
243     lpBuf = PTR_SEG_TO_LIN(prb->SRB_BufPointer);
244
245   switch (prb->CDBByte[0]) {
246   case CMD_INQUIRY:
247     TRACE(aspi, "Vendor: %s\n", lpBuf + INQUIRY_VENDOR);
248     break;
249   case CMD_TEST_UNIT_READY:
250     PrintSenseArea16(prb);
251     break;
252   }
253 }
254
255 static WORD
256 ASPI_ExecScsiCmd16(SRB_ExecSCSICmd16 *prb, SEGPTR segptr_prb)
257 {
258   struct sg_header *sg_hd, *sg_reply_hdr;
259   int   status;
260   BYTE *lpBuf;
261   int   in_len, out_len;
262   int   error_code = 0;
263   int   fd;
264
265   ASPI_DebugPrintCmd16(prb);
266
267   fd = ASPI_OpenDevice16(prb);
268   if (fd == -1) {
269       WARN(aspi, "ASPI_ExecScsiCmd16 failed: could not open device.\n");
270       prb->SRB_Status = SS_ERR;
271       return SS_ERR;
272   }
273
274   sg_hd = NULL;
275   sg_reply_hdr = NULL;
276
277   prb->SRB_Status = SS_PENDING;
278   if ((DOSASPI) && (prb->SRB_BufPointer)) /* translate real mode address */
279     lpBuf = (BYTE *)DOSMEM_MapRealToLinear((UINT32)prb->SRB_BufPointer);
280   else
281     lpBuf = PTR_SEG_TO_LIN(prb->SRB_BufPointer);
282
283   if (!prb->SRB_CDBLen) {
284       WARN(aspi, "ASPI_ExecScsiCmd16 failed: prb->SRB_CDBLen = 0.\n");
285       prb->SRB_Status = SS_ERR;
286       return SS_ERR;
287   }
288
289   /* build up sg_header + scsi cmd */
290   if (HOST_TO_TARGET(prb)) {
291     /* send header, command, and then data */
292     in_len = SCSI_OFF + prb->SRB_CDBLen + prb->SRB_BufLen;
293     sg_hd = (struct sg_header *) malloc(in_len);
294     memset(sg_hd, 0, SCSI_OFF);
295     memcpy(sg_hd + 1, &prb->CDBByte[0], prb->SRB_CDBLen);
296     if (prb->SRB_BufLen) {
297       memcpy(((BYTE *) sg_hd) + SCSI_OFF + prb->SRB_CDBLen, lpBuf, prb->SRB_BufLen);
298     }
299   }
300   else {
301     /* send header and command - no data */
302     in_len = SCSI_OFF + prb->SRB_CDBLen;
303     sg_hd = (struct sg_header *) malloc(in_len);
304     memset(sg_hd, 0, SCSI_OFF);
305     memcpy(sg_hd + 1, &prb->CDBByte[0], prb->SRB_CDBLen);
306   }
307
308   if (TARGET_TO_HOST(prb)) {
309     out_len = SCSI_OFF + prb->SRB_BufLen;
310     sg_reply_hdr = (struct sg_header *) malloc(out_len);
311     memset(sg_reply_hdr, 0, SCSI_OFF);
312     sg_hd->reply_len = out_len;
313   }
314   else {
315     out_len = SCSI_OFF;
316     sg_reply_hdr = (struct sg_header *) malloc(out_len);
317     memset(sg_reply_hdr, 0, SCSI_OFF);
318     sg_hd->reply_len = out_len;
319   }
320
321   status = write(fd, sg_hd, in_len);
322   if (status < 0 || status != in_len) {
323       int myerror = errno;
324
325     WARN(aspi, "Not enough bytes written to scsi device bytes=%d .. %d\n", in_len, status);
326     if (status < 0) {
327         if (myerror == ENOMEM) {
328             MSG("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");
329         }
330         WARN(aspi, "errno: = %d\n", myerror);
331     }
332     goto error_exit;
333   }
334
335   status = read(fd, sg_reply_hdr, out_len);
336   if (status < 0 || status != out_len) {
337     WARN(aspi, "not enough bytes read from scsi device%d\n", status);
338     goto error_exit;
339   }
340
341   if (sg_reply_hdr->result != 0) {
342     error_code = sg_reply_hdr->result;
343     WARN(aspi, "reply header error (%d)\n", sg_reply_hdr->result);
344     goto error_exit;
345   }
346
347   if (TARGET_TO_HOST(prb) && prb->SRB_BufLen) {
348     memcpy(lpBuf, sg_reply_hdr + 1, prb->SRB_BufLen);
349   }
350
351   /* copy in sense buffer to amount that is available in client */
352   if (prb->SRB_SenseLen) {
353     int sense_len = prb->SRB_SenseLen;
354     if (prb->SRB_SenseLen > 16)
355       sense_len = 16;
356     memcpy(SENSE_BUFFER(prb), &sg_reply_hdr->sense_buffer[0], sense_len);
357   }
358
359
360   prb->SRB_Status = SS_COMP;
361   prb->SRB_HaStat = HASTAT_OK;
362   prb->SRB_TargStat = STATUS_GOOD;
363
364   /* now do  posting */
365
366   if (ASPI_POSTING(prb) && prb->SRB_PostProc) {
367     TRACE(aspi, "Post Routine (%lx) called\n", (DWORD) prb->SRB_PostProc);
368     Callbacks->CallASPIPostProc(prb->SRB_PostProc, segptr_prb);
369   }
370
371   free(sg_reply_hdr);
372   free(sg_hd);
373   ASPI_DebugPrintResult16(prb);
374   return SS_COMP;
375   
376 error_exit:
377   if (error_code == EBUSY) {
378       prb->SRB_Status = SS_ASPI_IS_BUSY;
379       TRACE(aspi, "Device busy\n");
380   }
381   else {
382       WARN(aspi, "ASPI_GenericHandleScsiCmd failed\n");
383       prb->SRB_Status = SS_ERR;
384   }
385
386   /* I'm not sure exactly error codes work here
387    * We probably should set prb->SRB_TargStat, SRB_HaStat ?
388    */
389   WARN(aspi, "ASPI_GenericHandleScsiCmd: error_exit\n");
390   free(sg_reply_hdr);
391   free(sg_hd);
392   return prb->SRB_Status;
393 }
394 #endif
395
396 /***********************************************************************
397  *             GetASPISupportInfo16   (WINASPI.1)
398  */
399
400 WORD WINAPI GetASPISupportInfo16()
401 {
402 #ifdef linux
403     TRACE(aspi, "GETASPISupportInfo\n");
404     /* high byte SS_COMP - low byte number of host adapters.
405      * FIXME!!! The number of host adapters is incorrect.
406      * I'm not sure how to determine this under linux etc.
407      */
408     return ((SS_COMP << 8) | 0x1);
409 #else
410     return ((SS_COMP << 8) | 0x0);
411 #endif
412 }
413
414 /***********************************************************************
415  *             SendASPICommand16   (WINASPI.2)
416  */
417
418 WORD WINAPI SendASPICommand16(SEGPTR segptr_srb)
419 {
420 #ifdef linux
421   LPSRB16 lpSRB = PTR_SEG_TO_LIN(segptr_srb);
422
423   switch (lpSRB->common.SRB_cmd) {
424   case SC_HA_INQUIRY:
425     lpSRB->inquiry.SRB_Status = 0x1;           /* completed successfully */
426     lpSRB->inquiry.SRB_HaId = 1;               /* bogus value */
427     lpSRB->inquiry.HA_Count = 1;               /* not always */
428     lpSRB->inquiry.HA_SCSI_ID = 7;             /* not always ID 7 */
429     strcat(lpSRB->inquiry.HA_ManagerId, "Wine ASPI"); /* max 15 chars */
430     lpSRB->inquiry.SRB_55AASignature = 0x55aa; /* correct ??? */
431     lpSRB->inquiry.SRB_ExtBufferSize = 0x2000; /* bogus value */
432     FIXME(aspi, "ASPI: Partially implemented SC_HA_INQUIRY\n");
433     break;
434   case SC_GET_DEV_TYPE:
435     FIXME(aspi, "Not implemented SC_GET_DEV_TYPE\n");
436     break;
437   case SC_EXEC_SCSI_CMD:
438     return ASPI_ExecScsiCmd16(&lpSRB->cmd, segptr_srb);
439     break;
440   case SC_RESET_DEV:
441     FIXME(aspi, "Not implemented SC_RESET_DEV\n");
442     break;
443   default:
444     WARN(aspi, "Unknown command %d\n", lpSRB->common.SRB_cmd);
445   }
446   return SS_INVALID_SRB;
447 #else
448   return SS_INVALID_SRB;
449 #endif
450 }
451
452 /***********************************************************************
453  *             GetASPIDLLVersion   (WINASPI.4)
454  */
455
456 DWORD WINAPI GetASPIDLLVersion()
457 {
458 #ifdef linux
459         return (DWORD)2;
460 #else
461         return (DWORD)0;
462 #endif
463 }
464
465
466 void WINAPI ASPI_DOS_func(DWORD srb)
467 {
468        LPSRB16 lpSRB = (LPSRB16)DOSMEM_MapRealToLinear(srb);
469        SEGPTR spSRB = MapLS(lpSRB);
470
471        TRACE(aspi, "DOSASPI: function #%d\n", lpSRB->common.SRB_cmd);
472        DOSASPI = TRUE;
473        SendASPICommand16(spSRB);
474        DOSASPI = FALSE;
475        UnMapLS(spSRB);
476 }
477
478 void ASPI_DOS_HandleInt(CONTEXT *context)
479 /* returns a real mode call address to ASPI_DOS_func() */
480 {
481 #ifdef linux
482        FARPROC16 DOS_func;
483        DWORD dos;
484        LPBYTE dosptr;
485
486        DOS_func = MODULE_GetWndProcEntry16("ASPI_DOS_func");
487        dos = GlobalDOSAlloc(5);
488        dosptr = (BYTE *)PTR_SEG_OFF_TO_LIN(LOWORD(dos), 0);
489        *dosptr++ = 0xea; /* ljmp */
490        *(FARPROC16 *)dosptr = DOS_func;
491
492        *(DWORD *)PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context))
493                = MAKELONG(0, HIWORD(dos)); /* real mode address */
494        RESET_CFLAG(context);
495        AX_reg(context) = CX_reg(context);
496 #else
497        SET_CFLAG(context);
498 #endif
499 }