15 #include "selectors.h"
21 * 1) Residual byte length reporting not handled
22 * 2) Make this code re-entrant for multithreading
23 * 3) Only linux supported so far
28 /* This is a duplicate of the sg_header from /usr/src/linux/include/scsi/sg.h
30 * This will probably break at some point, but for those who don't have
31 * kernels installed, I think this should still work.
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)
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
;
98 typedef struct ASPI_DEVICE_INFO ASPI_DEVICE_INFO
;
99 static ASPI_DEVICE_INFO
*ASPI_open_devices
= NULL
;
101 static BOOL16 DOSASPI
= FALSE
;
105 ASPI_OpenDevice16(SRB_ExecSCSICmd16
*prb
)
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
) {
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
);
133 TRACE(aspi
, "Opening device %s=%s\n", idstr
, device_str
);
135 fd
= open(device_str
, O_RDWR
);
137 int save_error
= errno
;
138 WARN(aspi
, "Error opening device errno=%d\n", save_error
);
142 /* device is now open */
143 curr
= HeapAlloc( SystemHeap
, 0, sizeof(ASPI_DEVICE_INFO
) );
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
;
157 ASPI_DebugPrintCmd16(SRB_ExecSCSICmd16
*prb
)
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
);
168 lpBuf
= PTR_SEG_TO_LIN(prb
->SRB_BufPointer
);
170 switch (prb
->CDBByte
[0]) {
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]);
181 TRACE(aspi
, "Transfer Length: %d\n", prb
->CDBByte
[4]);
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");
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
));
221 PrintSenseArea16(SRB_ExecSCSICmd16
*prb
)
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
));
236 ASPI_DebugPrintResult16(SRB_ExecSCSICmd16
*prb
)
240 if ((DOSASPI
) && (prb
->SRB_BufPointer
)) /* translate real mode address */
241 lpBuf
= (BYTE
*)DOSMEM_MapRealToLinear((UINT32
)prb
->SRB_BufPointer
);
243 lpBuf
= PTR_SEG_TO_LIN(prb
->SRB_BufPointer
);
245 switch (prb
->CDBByte
[0]) {
247 TRACE(aspi
, "Vendor: %s\n", lpBuf
+ INQUIRY_VENDOR
);
249 case CMD_TEST_UNIT_READY
:
250 PrintSenseArea16(prb
);
256 ASPI_ExecScsiCmd16(SRB_ExecSCSICmd16
*prb
, SEGPTR segptr_prb
)
258 struct sg_header
*sg_hd
, *sg_reply_hdr
;
265 ASPI_DebugPrintCmd16(prb
);
267 fd
= ASPI_OpenDevice16(prb
);
269 WARN(aspi
, "ASPI_ExecScsiCmd16 failed: could not open device.\n");
270 prb
->SRB_Status
= SS_ERR
;
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
);
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
;
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
);
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
;
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
) {
325 WARN(aspi
, "Not enough bytes written to scsi device bytes=%d .. %d\n", in_len
, status
);
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
);
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
);
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
);
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)
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
;
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
);
373 ASPI_DebugPrintResult16(prb
);
377 if (error_code
== EBUSY
) {
378 prb
->SRB_Status
= SS_ASPI_IS_BUSY
;
379 TRACE(aspi
, "Device busy\n");
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");
392 return prb
->SRB_Status
;
396 /***********************************************************************
397 * GetASPISupportInfo16 (WINASPI.1)
400 WORD WINAPI
GetASPISupportInfo16()
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);
410 return ((SS_COMP
<< 8) | 0x0);
414 /***********************************************************************
415 * SendASPICommand16 (WINASPI.2)
418 WORD WINAPI
SendASPICommand16(SEGPTR segptr_srb
)
421 LPSRB16 lpSRB
= PTR_SEG_TO_LIN(segptr_srb
);
423 switch (lpSRB
->common
.SRB_cmd
) {
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");
434 case SC_GET_DEV_TYPE
:
435 FIXME(aspi
, "Not implemented SC_GET_DEV_TYPE\n");
437 case SC_EXEC_SCSI_CMD
:
438 return ASPI_ExecScsiCmd16(&lpSRB
->cmd
, segptr_srb
);
441 FIXME(aspi
, "Not implemented SC_RESET_DEV\n");
444 WARN(aspi
, "Unknown command %d\n", lpSRB
->common
.SRB_cmd
);
446 return SS_INVALID_SRB
;
448 return SS_INVALID_SRB
;
452 /***********************************************************************
453 * GetASPIDLLVersion (WINASPI.4)
456 DWORD WINAPI
GetASPIDLLVersion()
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
);
473 SendASPICommand16(spSRB
);
478 void ASPI_DOS_HandleInt(CONTEXT
*context
)
479 /* returns a real mode call address to ASPI_DOS_func() */
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
;
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
);