2 * Emulation of processor ioports.
4 * Copyright 1995 Morten Welinder
8 - only a few ports are emulated.
9 - real-time clock in "cmos" is bogus. A nifty alarm() setup could
22 static BYTE cmosaddress
;
24 static BYTE cmosimage
[64] =
26 0x27, 0x34, 0x31, 0x47, 0x16, 0x15, 0x00, 0x01,
27 0x04, 0x94, 0x26, 0x02, 0x50, 0x80, 0x00, 0x00,
28 0x40, 0xb1, 0x00, 0x9c, 0x01, 0x80, 0x02, 0x00,
29 0x1c, 0x00, 0x00, 0xad, 0x02, 0x10, 0x00, 0x00,
30 0x08, 0x00, 0x00, 0x26, 0x00, 0x00, 0x00, 0x00,
31 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x03, 0x58,
32 0x00, 0x1c, 0x19, 0x81, 0x00, 0x0e, 0x00, 0x80,
33 0x1b, 0x7b, 0x21, 0x00, 0x00, 0x00, 0x05, 0x5f
36 #if defined(linux) && defined(__i386__)
37 # define DIRECT_IO_ACCESS
39 # undef DIRECT_IO_ACCESS
40 #endif /* linux && __i386__ */
42 #ifdef DIRECT_IO_ACCESS
44 extern int iopl(int level
);
46 static char do_direct_port_access
= 0;
47 static char port_permissions
[0x10000];
52 #endif /* DIRECT_IO_ACCESS */
54 /**********************************************************************
58 /* set_IO_permissions(int val1, int val)
59 * Helper function for IO_port_init
61 #ifdef DIRECT_IO_ACCESS
62 static void set_IO_permissions(int val1
, int val
, char rw
)
66 if (val
== -1) val
= 0x3ff;
67 for (j
= val1
; j
<= val
; j
++)
68 port_permissions
[j
] |= rw
;
70 do_direct_port_access
= 1;
73 } else if (val
!= -1) {
74 do_direct_port_access
= 1;
76 port_permissions
[val
] |= rw
;
81 /* do_IO_port_init_read_or_write(char* temp, char rw)
82 * Helper function for IO_port_init
85 static void do_IO_port_init_read_or_write(char* temp
, char rw
)
87 int val
, val1
, i
, len
;
88 if (!strcasecmp(temp
, "all")) {
89 MSG("Warning!!! Granting FULL IO port access to"
90 " windoze programs!\nWarning!!! "
91 "*** THIS IS NOT AT ALL "
92 "RECOMMENDED!!! ***\n");
93 for (i
=0; i
< sizeof(port_permissions
); i
++)
94 port_permissions
[i
] |= rw
;
96 } else if (!(!strcmp(temp
, "*") || *temp
== '\0')) {
100 for (i
= 0; i
< len
; i
++) {
103 if (temp
[i
+1] == 'x' || temp
[i
+1] == 'X') {
104 sscanf(temp
+i
, "%x", &val
);
107 sscanf(temp
+i
, "%d", &val
);
109 while (isxdigit(temp
[i
]))
116 set_IO_permissions(val1
, val
, rw
);
121 if (val1
== -1) val1
= 0;
124 if (temp
[i
] >= '0' && temp
[i
] <= '9') {
125 sscanf(temp
+i
, "%d", &val
);
126 while (isdigit(temp
[i
]))
131 set_IO_permissions(val1
, val
, rw
);
135 static __inline__ BYTE
inb( WORD port
)
138 __asm__
__volatile__( "inb %w1,%0" : "=a" (b
) : "d" (port
) );
142 static __inline__ WORD
inw( WORD port
)
145 __asm__
__volatile__( "inw %w1,%0" : "=a" (w
) : "d" (port
) );
149 static __inline__ DWORD
inl( WORD port
)
152 __asm__
__volatile__( "inl %w1,%0" : "=a" (dw
) : "d" (port
) );
156 static __inline__
void outb( BYTE value
, WORD port
)
158 __asm__
__volatile__( "outb %b0,%w1" : : "a" (value
), "d" (port
) );
161 static __inline__
void outw( WORD value
, WORD port
)
163 __asm__
__volatile__( "outw %w0,%w1" : : "a" (value
), "d" (port
) );
166 static __inline__
void outl( DWORD value
, WORD port
)
168 __asm__
__volatile__( "outl %0,%w1" : : "a" (value
), "d" (port
) );
171 #endif /* DIRECT_IO_ACCESS */
175 #ifdef DIRECT_IO_ACCESS
178 /* Can we do that? */
182 PROFILE_GetWineIniString( "ports", "read", "*",
183 temp
, sizeof(temp
) );
184 do_IO_port_init_read_or_write(temp
, IO_READ
);
185 PROFILE_GetWineIniString( "ports", "write", "*",
186 temp
, sizeof(temp
) );
187 do_IO_port_init_read_or_write(temp
, IO_WRITE
);
189 #endif /* DIRECT_IO_ACCESS */
193 /**********************************************************************
196 DWORD
IO_inport( int port
, int count
)
201 #ifdef DIRECT_IO_ACCESS
202 if (do_direct_port_access
)
204 /* Make sure we have access to the whole range */
206 for (i
= 0; i
< count
; i
++)
207 if (!(port_permissions
[port
+i
] & IO_READ
)) break;
213 case 1: res
= inb( port
); break;
214 case 2: res
= inw( port
); break;
215 case 4: res
= inl( port
); break;
217 ERR(int, "invalid count %d\n", count
);
225 TRACE(int, "%d bytes from port 0x%02x\n", count
, port
);
235 b
= cmosimage
[cmosaddress
& 0x3f];
238 WARN( int, "Direct I/O read attempted from port %x\n", port
);
243 res
= (res
<< 8) | b
;
245 TRACE(int, " returning ( 0x%lx )\n", res
);
250 /**********************************************************************
253 void IO_outport( int port
, int count
, DWORD value
)
257 TRACE(int, "IO: 0x%lx (%d bytes) to port 0x%02x\n",
258 value
, count
, port
);
260 #ifdef DIRECT_IO_ACCESS
261 if (do_direct_port_access
)
263 /* Make sure we have access to the whole range */
265 for (i
= 0; i
< count
; i
++)
266 if (!(port_permissions
[port
+i
] & IO_WRITE
)) break;
272 case 1: outb( LOBYTE(value
), port
); break;
273 case 2: outw( LOWORD(value
), port
); break;
274 case 4: outl( value
, port
); break;
276 WARN(int, "Invalid count %d\n", count
);
291 cmosaddress
= b
& 0x7f;
294 cmosimage
[cmosaddress
& 0x3f] = b
;
297 WARN(int, "Direct I/O write attempted to port %x\n", port
);