Release 980726
[wine/multimedia.git] / misc / aspi.c
blobe4592a932e3530156c1d548ac79e127bdd60948f
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"
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
26 #ifdef linux
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.
35 struct sg_header
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 */
47 #define SCSI_OFF sizeof(struct sg_header)
48 #endif
50 #define ASPI_POSTING(prb) (prb->SRB_Flags & 0x1)
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)
56 #define SRB_ENABLE_RESIDUAL_COUNT 0x4
58 #define INQUIRY_VENDOR 8
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
67 #define CMD_TEST_UNIT_READY 0x00
68 #define CMD_REQUEST_SENSE 0x03
69 #define CMD_INQUIRY 0x12
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
82 #define INQURIY_CMDLEN 6
83 #define INQURIY_REPLY_LEN 96
84 #define INQUIRY_VENDOR 8
86 #define SENSE_BUFFER(prb) (&prb->CDBByte[prb->SRB_CDBLen])
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;
98 typedef struct ASPI_DEVICE_INFO ASPI_DEVICE_INFO;
99 static ASPI_DEVICE_INFO *ASPI_open_devices = NULL;
101 static BOOL16 DOSASPI = FALSE;
103 #ifdef linux
104 static int
105 ASPI_OpenDevice16(SRB_ExecSCSICmd16 *prb)
107 int fd;
108 char idstr[20];
109 char device_str[50];
110 ASPI_DEVICE_INFO *curr;
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.
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;
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);
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;
133 TRACE(aspi, "Opening device %s=%s\n", idstr, device_str);
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;
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;
149 /* insert new record at beginning of open device list */
150 curr->next = ASPI_open_devices;
151 ASPI_open_devices = curr;
152 return fd;
156 static void
157 ASPI_DebugPrintCmd16(SRB_ExecSCSICmd16 *prb)
159 BYTE cmd;
160 int i;
161 BYTE *cdb;
162 BYTE *lpBuf;
163 dbg_decl_str(aspi, 512);
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);
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;
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");
190 else if (HOST_TO_TARGET(prb)) {
191 TRACE(aspi, "\tData transfer: Host to target. Length checked.\n");
193 else if (NO_DATA_TRANSFERED(prb)) {
194 TRACE(aspi, "\tData transfer: none\n");
196 else {
197 WARN(aspi, "\tTransfer by scsi cmd. Length not checked\n");
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++);
217 TRACE(aspi, "CDB buffer[%s]\n", dbg_str(aspi));
220 static void
221 PrintSenseArea16(SRB_ExecSCSICmd16 *prb)
223 int i;
224 BYTE *cdb;
225 dbg_decl_str(aspi, 512);
227 cdb = &prb->CDBByte[0];
228 for (i = 0; i < prb->SRB_SenseLen; i++) {
229 if (i) dsprintf(aspi, ",");
230 dsprintf(aspi, "%02x", *cdb++);
232 TRACE(aspi, "SenseArea[%s]\n", dbg_str(aspi));
235 static void
236 ASPI_DebugPrintResult16(SRB_ExecSCSICmd16 *prb)
238 BYTE *lpBuf;
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);
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;
255 static WORD
256 ASPI_ExecScsiCmd16(SRB_ExecSCSICmd16 *prb, SEGPTR segptr_prb)
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;
265 ASPI_DebugPrintCmd16(prb);
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;
274 sg_hd = NULL;
275 sg_reply_hdr = NULL;
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);
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;
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);
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);
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;
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;
321 status = write(fd, sg_hd, in_len);
322 if (status < 0 || status != in_len) {
323 int myerror = errno;
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");
330 WARN(aspi, "errno: = %d\n", myerror);
332 goto error_exit;
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;
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;
347 if (TARGET_TO_HOST(prb) && prb->SRB_BufLen) {
348 memcpy(lpBuf, sg_reply_hdr + 1, prb->SRB_BufLen);
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);
360 prb->SRB_Status = SS_COMP;
361 prb->SRB_HaStat = HASTAT_OK;
362 prb->SRB_TargStat = STATUS_GOOD;
364 /* now do posting */
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);
371 free(sg_reply_hdr);
372 free(sg_hd);
373 ASPI_DebugPrintResult16(prb);
374 return SS_COMP;
376 error_exit:
377 if (error_code == EBUSY) {
378 prb->SRB_Status = SS_ASPI_IS_BUSY;
379 TRACE(aspi, "Device busy\n");
381 else {
382 WARN(aspi, "ASPI_GenericHandleScsiCmd failed\n");
383 prb->SRB_Status = SS_ERR;
386 /* I'm not sure exactly error codes work here
387 * We probably should set prb->SRB_TargStat, SRB_HaStat ?
389 WARN(aspi, "ASPI_GenericHandleScsiCmd: error_exit\n");
390 free(sg_reply_hdr);
391 free(sg_hd);
392 return prb->SRB_Status;
394 #endif
396 /***********************************************************************
397 * GetASPISupportInfo16 (WINASPI.1)
400 WORD WINAPI GetASPISupportInfo16()
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.
408 return ((SS_COMP << 8) | 0x1);
409 #else
410 return ((SS_COMP << 8) | 0x0);
411 #endif
414 /***********************************************************************
415 * SendASPICommand16 (WINASPI.2)
418 WORD WINAPI SendASPICommand16(SEGPTR segptr_srb)
420 #ifdef linux
421 LPSRB16 lpSRB = PTR_SEG_TO_LIN(segptr_srb);
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);
446 return SS_INVALID_SRB;
447 #else
448 return SS_INVALID_SRB;
449 #endif
452 /***********************************************************************
453 * GetASPIDLLVersion (WINASPI.4)
456 DWORD WINAPI GetASPIDLLVersion()
458 #ifdef linux
459 return (DWORD)2;
460 #else
461 return (DWORD)0;
462 #endif
466 void WINAPI ASPI_DOS_func(DWORD srb)
468 LPSRB16 lpSRB = (LPSRB16)DOSMEM_MapRealToLinear(srb);
469 SEGPTR spSRB = MapLS(lpSRB);
471 TRACE(aspi, "DOSASPI: function #%d\n", lpSRB->common.SRB_cmd);
472 DOSASPI = TRUE;
473 SendASPICommand16(spSRB);
474 DOSASPI = FALSE;
475 UnMapLS(spSRB);
479 /* returns a real mode call address to ASPI_DOS_func() */
480 void ASPI_DOS_HandleInt(CONTEXT *context)
482 #ifdef linux
483 FARPROC16 DOS_func;
484 DWORD dos;
485 LPBYTE dosptr;
487 DOS_func = MODULE_GetWndProcEntry16("ASPI_DOS_func");
488 dos = GlobalDOSAlloc(5);
489 dosptr = (BYTE *)PTR_SEG_OFF_TO_LIN(LOWORD(dos), 0);
490 *dosptr++ = 0xea; /* ljmp */
491 *(FARPROC16 *)dosptr = DOS_func;
493 *(DWORD *)PTR_SEG_OFF_TO_LIN(DS_reg(context), DX_reg(context))
494 = MAKELONG(0, HIWORD(dos)); /* real mode address */
495 RESET_CFLAG(context);
496 AX_reg(context) = CX_reg(context);
497 #else
498 SET_CFLAG(context);
499 #endif
502 /*******************************************************************
503 * GetASPI32SupportInfo [WNASPI32.0]
505 * Checks if the ASPI subsystem is initialized correctly.
507 * RETURNS
508 * HIWORD: 0.
509 * HIBYTE of LOWORD: status (SS_COMP or SS_FAILED_INIT)
510 * LOBYTE of LOWORD: # of host adapters.
512 DWORD WINAPI GetASPI32SupportInfo()
514 return (SS_COMP << 8) | 1; /* FIXME: get # of host adapters installed */