Release 980201
[wine/multimedia.git] / misc / aspi.c
blob993cd4ad3fe697b628d28cd9f70822280f8b9f76
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 #include "stddebug.h"
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
24 #ifdef linux
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.
33 struct sg_header
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 */
45 #define SCSI_OFF sizeof(struct sg_header)
46 #endif
48 #define ASPI_POSTING(prb) (prb->SRB_Flags & 0x1)
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)
54 #define SRB_ENABLE_RESIDUAL_COUNT 0x4
56 #define INQUIRY_VENDOR 8
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
65 #define CMD_TEST_UNIT_READY 0x00
66 #define CMD_REQUEST_SENSE 0x03
67 #define CMD_INQUIRY 0x12
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
80 #define INQURIY_CMDLEN 6
81 #define INQURIY_REPLY_LEN 96
82 #define INQUIRY_VENDOR 8
84 #define SENSE_BUFFER(prb) (&prb->CDBByte[prb->SRB_CDBLen])
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;
96 typedef struct ASPI_DEVICE_INFO ASPI_DEVICE_INFO;
97 static ASPI_DEVICE_INFO *ASPI_open_devices = NULL;
99 #ifdef linux
100 static int
101 ASPI_OpenDevice16(SRB_ExecSCSICmd16 *prb)
103 int fd;
104 char idstr[20];
105 char device_str[50];
106 ASPI_DEVICE_INFO *curr;
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.
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;
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);
124 if (!PROFILE_GetWineIniString(idstr, "Device", "", device_str, sizeof(device_str))) {
125 dprintf_aspi(stddeb, "Trying to open unlisted scsi device %s\n", idstr);
126 return -1;
129 dprintf_aspi(stddeb, "Opening device %s=%s\n", idstr, device_str);
131 fd = open(device_str, O_RDWR);
132 if (fd == -1) {
133 int save_error = errno;
134 dprintf_aspi(stddeb, "Error opening device errno=%d\n", save_error);
135 return -1;
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;
145 /* insert new record at beginning of open device list */
146 curr->next = ASPI_open_devices;
147 ASPI_open_devices = curr;
148 return fd;
152 static void
153 ASPI_DebugPrintCmd16(SRB_ExecSCSICmd16 *prb)
155 BYTE cmd;
156 int i;
157 BYTE *cdb;
158 BYTE *lpBuf;
160 lpBuf = PTR_SEG_TO_LIN(prb->SRB_BufPointer);
162 switch (prb->CDBByte[0]) {
163 case CMD_INQUIRY:
164 dprintf_aspi(stddeb, "{\n");
165 dprintf_aspi(stddeb, "\tEVPD: %d\n", prb->CDBByte[1] & 1);
166 dprintf_aspi(stddeb, "\tLUN: %d\n", (prb->CDBByte[1] & 0xc) >> 1);
167 dprintf_aspi(stddeb, "\tPAGE CODE: %d\n", prb->CDBByte[2]);
168 dprintf_aspi(stddeb, "\tALLOCATION LENGTH: %d\n", prb->CDBByte[4]);
169 dprintf_aspi(stddeb, "\tCONTROL: %d\n", prb->CDBByte[5]);
170 dprintf_aspi(stddeb, "}\n");
171 break;
172 case CMD_SCAN_SCAN:
173 dprintf_aspi(stddeb, "Transfer Length: %d\n", prb->CDBByte[4]);
174 break;
177 dprintf_aspi(stddeb, "Host Adapter: %d\n", prb->SRB_HaId);
178 dprintf_aspi(stddeb, "Flags: %d\n", prb->SRB_Flags);
179 if (TARGET_TO_HOST(prb)) {
180 dprintf_aspi(stddeb, "\tData transfer: Target to host. Length checked.\n");
182 else if (HOST_TO_TARGET(prb)) {
183 dprintf_aspi(stddeb, "\tData transfer: Host to target. Length checked.\n");
185 else if (NO_DATA_TRANSFERED(prb)) {
186 dprintf_aspi(stddeb, "\tData transfer: none\n");
188 else {
189 dprintf_aspi(stddeb, "\tTransfer by scsi cmd. Length not checked\n");
192 dprintf_aspi(stddeb, "\tResidual byte length reporting %s\n", prb->SRB_Flags & 0x4 ? "enabled" : "disabled");
193 dprintf_aspi(stddeb, "\tLinking %s\n", prb->SRB_Flags & 0x2 ? "enabled" : "disabled");
194 dprintf_aspi(stddeb, "\tPosting %s\n", prb->SRB_Flags & 0x1 ? "enabled" : "disabled");
195 dprintf_aspi(stddeb, "Target: %d\n", prb->SRB_Target);
196 dprintf_aspi(stddeb, "Lun: %d\n", prb->SRB_Lun);
197 dprintf_aspi(stddeb, "BufLen: %ld\n", prb->SRB_BufLen);
198 dprintf_aspi(stddeb, "SenseLen: %d\n", prb->SRB_SenseLen);
199 dprintf_aspi(stddeb, "BufPtr: %lx (%p)\n", prb->SRB_BufPointer, lpBuf);
200 dprintf_aspi(stddeb, "LinkPointer %lx\n", prb->SRB_Rsvd1);
201 dprintf_aspi(stddeb, "CDB Length: %d\n", prb->SRB_CDBLen);
202 dprintf_aspi(stddeb, "POST Proc: %lx\n", (DWORD) prb->SRB_PostProc);
203 cdb = &prb->CDBByte[0];
204 dprintf_aspi(stddeb, "CDB buffer[");
205 cmd = prb->CDBByte[0];
206 for (i = 0; i < prb->SRB_CDBLen; i++) {
207 if (i != 0)
208 dprintf_aspi(stddeb, ",");
209 dprintf_aspi(stddeb, "%02x", *cdb++);
211 dprintf_aspi(stddeb, "]\n");
214 static void
215 PrintSenseArea16(SRB_ExecSCSICmd16 *prb)
217 int i;
218 BYTE *cdb;
220 cdb = &prb->CDBByte[0];
221 dprintf_aspi(stddeb, "SenseArea[");
222 for (i = 0; i < prb->SRB_SenseLen; i++) {
223 if (i)
224 dprintf_aspi(stddeb, ",");
225 dprintf_aspi(stddeb, "%02x", *cdb++);
227 dprintf_aspi(stddeb, "]\n");
230 static void
231 ASPI_DebugPrintResult16(SRB_ExecSCSICmd16 *prb)
233 BYTE *lpBuf;
235 lpBuf = PTR_SEG_TO_LIN(prb->SRB_BufPointer);
237 switch (prb->CDBByte[0]) {
238 case CMD_INQUIRY:
239 dprintf_aspi(stddeb, "Vendor: %s\n", lpBuf + INQUIRY_VENDOR);
240 break;
241 case CMD_TEST_UNIT_READY:
242 PrintSenseArea16(prb);
243 break;
247 static WORD
248 ASPI_ExecScsiCmd16(SRB_ExecSCSICmd16 *prb, SEGPTR segptr_prb)
250 struct sg_header *sg_hd, *sg_reply_hdr;
251 int status;
252 BYTE *lpBuf;
253 int in_len, out_len;
254 int error_code = 0;
255 int fd;
257 ASPI_DebugPrintCmd16(prb);
259 fd = ASPI_OpenDevice16(prb);
260 if (fd == -1) {
261 prb->SRB_Status = SS_ERR;
262 return SS_ERR;
265 sg_hd = NULL;
266 sg_reply_hdr = NULL;
268 prb->SRB_Status = SS_PENDING;
269 lpBuf = PTR_SEG_TO_LIN(prb->SRB_BufPointer);
271 if (!prb->SRB_CDBLen) {
272 prb->SRB_Status = SS_ERR;
273 return SS_ERR;
276 /* build up sg_header + scsi cmd */
277 if (HOST_TO_TARGET(prb)) {
278 /* send header, command, and then data */
279 in_len = SCSI_OFF + prb->SRB_CDBLen + prb->SRB_BufLen;
280 sg_hd = (struct sg_header *) malloc(in_len);
281 memset(sg_hd, 0, SCSI_OFF);
282 memcpy(sg_hd + 1, &prb->CDBByte[0], prb->SRB_CDBLen);
283 if (prb->SRB_BufLen) {
284 memcpy(((BYTE *) sg_hd) + SCSI_OFF + prb->SRB_CDBLen, lpBuf, prb->SRB_BufLen);
287 else {
288 /* send header and command - no data */
289 in_len = SCSI_OFF + prb->SRB_CDBLen;
290 sg_hd = (struct sg_header *) malloc(in_len);
291 memset(sg_hd, 0, SCSI_OFF);
292 memcpy(sg_hd + 1, &prb->CDBByte[0], prb->SRB_CDBLen);
295 if (TARGET_TO_HOST(prb)) {
296 out_len = SCSI_OFF + prb->SRB_BufLen;
297 sg_reply_hdr = (struct sg_header *) malloc(out_len);
298 memset(sg_reply_hdr, 0, SCSI_OFF);
299 sg_hd->reply_len = out_len;
301 else {
302 out_len = SCSI_OFF;
303 sg_reply_hdr = (struct sg_header *) malloc(out_len);
304 memset(sg_reply_hdr, 0, SCSI_OFF);
305 sg_hd->reply_len = out_len;
308 status = write(fd, sg_hd, in_len);
309 if (status < 0 || status != in_len) {
310 int myerror = errno;
312 fprintf(stderr, "not enough bytes written to scsi device bytes=%d .. %d\n", in_len, status);
313 if (status < 0) {
314 if (myerror == ENOMEM) {
315 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");
317 dprintf_aspi(stddeb, "errno: = %d\n", myerror);
319 goto error_exit;
322 status = read(fd, sg_reply_hdr, out_len);
323 if (status < 0 || status != out_len) {
324 dprintf_aspi(stddeb, "not enough bytes read from scsi device%d\n", status);
325 goto error_exit;
328 if (sg_reply_hdr->result != 0) {
329 error_code = sg_reply_hdr->result;
330 dprintf_aspi(stddeb, "reply header error (%d)\n", sg_reply_hdr->result);
331 goto error_exit;
334 if (TARGET_TO_HOST(prb) && prb->SRB_BufLen) {
335 memcpy(lpBuf, sg_reply_hdr + 1, prb->SRB_BufLen);
338 /* copy in sense buffer to amount that is available in client */
339 if (prb->SRB_SenseLen) {
340 int sense_len = prb->SRB_SenseLen;
341 if (prb->SRB_SenseLen > 16)
342 sense_len = 16;
343 memcpy(SENSE_BUFFER(prb), &sg_reply_hdr->sense_buffer[0], sense_len);
347 prb->SRB_Status = SS_COMP;
348 prb->SRB_HaStat = HASTAT_OK;
349 prb->SRB_TargStat = STATUS_GOOD;
351 /* now do posting */
353 if (ASPI_POSTING(prb) && prb->SRB_PostProc) {
354 dprintf_aspi(stddeb, "ASPI: Post Routine (%lx) called\n", (DWORD) prb->SRB_PostProc);
355 Callbacks->CallASPIPostProc(prb->SRB_PostProc, segptr_prb);
358 free(sg_reply_hdr);
359 free(sg_hd);
360 ASPI_DebugPrintResult16(prb);
361 return SS_COMP;
363 error_exit:
364 if (error_code == EBUSY) {
365 prb->SRB_Status = SS_ASPI_IS_BUSY;
366 dprintf_aspi(stddeb, "ASPI: Device busy\n");
368 else {
369 dprintf_aspi(stddeb, "ASPI_GenericHandleScsiCmd failed\n");
370 prb->SRB_Status = SS_ERR;
373 /* I'm not sure exactly error codes work here
374 * We probably should set prb->SRB_TargStat, SRB_HaStat ?
376 dprintf_aspi(stddeb, "ASPI_GenericHandleScsiCmd: error_exit\n");
377 free(sg_reply_hdr);
378 free(sg_hd);
379 return prb->SRB_Status;
381 #endif
383 /***********************************************************************
384 * GetASPISupportInfo16 (WINASPI.1)
387 WORD WINAPI GetASPISupportInfo16()
389 #ifdef linux
390 dprintf_aspi(stddeb, "GETASPISupportInfo\n");
391 /* high byte SS_COMP - low byte number of host adapters.
392 * FIXME!!! The number of host adapters is incorrect.
393 * I'm not sure how to determine this under linux etc.
395 return ((SS_COMP << 8) | 0x1);
396 #else
397 return ((SS_COMP << 8) | 0x0);
398 #endif
401 /***********************************************************************
402 * SendASPICommand16 (WINASPI.2)
405 WORD WINAPI SendASPICommand16(SEGPTR segptr_srb)
407 #ifdef linux
408 LPSRB16 lpSRB = PTR_SEG_TO_LIN(segptr_srb);
410 switch (lpSRB->common.SRB_cmd) {
411 case SC_HA_INQUIRY:
412 dprintf_aspi(stddeb, "ASPI: Not implemented SC_HA_INQUIRY\n");
413 break;
414 case SC_GET_DEV_TYPE:
415 dprintf_aspi(stddeb, "ASPI: Not implemented SC_GET_DEV_TYPE\n");
416 break;
417 case SC_EXEC_SCSI_CMD:
418 return ASPI_ExecScsiCmd16(&lpSRB->cmd, segptr_srb);
419 break;
420 case SC_RESET_DEV:
421 dprintf_aspi(stddeb, "ASPI: Not implemented SC_RESET_DEV\n");
422 break;
423 default:
424 dprintf_aspi(stddeb, "ASPI: Unknown command %d\n", lpSRB->common.SRB_cmd);
426 return SS_INVALID_SRB;
427 #else
428 return SS_INVALID_SRB;
429 #endif
432 /***********************************************************************
433 * GetASPIDLLVersion (WINASPI.4)
436 DWORD WINAPI GetASPIDLLVersion()
438 #ifdef linux
439 return (DWORD)2;
440 #else
441 return (DWORD)0;
442 #endif