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