Debian Codegraph @

src/win32/ioports.cc

raw
1 /* Hey EMACS -*- win32-c -*- */
2 /* $Id$ */
3
4 /* libticables2 - link cable library, a part of the TiLP project
5 * Copyright (C) 1999-2005 Romain Lievin
6 * Copyright (c) 2002, Kevin Kofler for the __MINGW32__ & __GNUC__ extensions.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /*
24 * This unit manages direct low-level I/O operations depending on the
25 * platform type:
26 * - Windows 9x/Me: assembly routines,
27 * - Windows NT4/2000: assembly routines with a kernel driver for
28 * granting I/O permissions.
29 */
30
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34
35 #include <stdio.h>
36 #include <windows.h>
37
38 #include "../error.h"
39 #include "../logging.h"
40 #include "detect.h"
41 #include "ioports.h"
42
43 #include "../win32/dha.h"
44 #include "../win64/rwp.h"
45
46 /* Variables */
47
48 static int instance = 0; // Instance counter
49
50 /* Function pointers */
51
52 int (*io_rd) (unsigned int addr);
53 void (*io_wr) (unsigned int addr, int data);
54
55 /* Error helper */
56
57 #ifdef __WIN32__
58 static void print_last_error(const char *s)
59 {
60 LPTSTR lpMsgBuf;
61
62 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
63 FORMAT_MESSAGE_FROM_SYSTEM |
64 FORMAT_MESSAGE_IGNORE_INSERTS,
65 NULL, GetLastError(),
66 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
67 (LPTSTR) & lpMsgBuf, 0, NULL);
68
69 lpMsgBuf[strlen(lpMsgBuf)-2] = '\0';
70
71 ticables_info("%s (%lu -> %s)", s, (unsigned long)GetLastError(), lpMsgBuf);
72 }
73 #endif //__WIN32__
74
75 #ifndef _WIN64
76 /* I/O thru assembly code */
77
78 static int win32_read_io(unsigned int addr)
79 {
80 int c;
81
82 #ifdef __GNUC__
83 asm("movl $0,%%eax \n movw %1,%%dx \n inb %%dx,%%al \n movl %%eax,%0": "=g"(c): "g"(addr): "eax", "dx");
84 #else
85 __asm {
86 mov eax, 0
87 mov edx, addr
88 in al, dx
89 mov c, eax}
90 #endif
91 return c;
92 }
93
94 static void win32_write_io(unsigned int addr, int data)
95 {
96 #ifdef __GNUC__
97 asm("movw %0,%%dx \n movw %1,%%ax \n outb %%al,%%dx"::"g"(addr), "g"(data):"ax", "dx");
98 #else
99 __asm {
100 mov edx, addr
101 mov eax, data
102 out dx, al}
103 #endif
104 }
105 #endif
106
107 /* I/O thru device driver IOCTL calls */
108
109 static int win64_read_io(unsigned int addr)
110 {
111 unsigned char data;
112
113 rwp_read_byte((unsigned short)addr, &data);
114 return data;
115 }
116
117 static void win64_write_io(unsigned int addr, int data)
118 {
119 rwp_write_byte((unsigned short)addr, (unsigned char)data);
120 }
121
122 /* Functions used for initializing the I/O routines */
123
124 int io_open(unsigned long from)
125 {
126 int ret;
127
128 #ifndef _WIN64
129 if(win32_check_os() == WIN_9X)
130 {
131 io_rd = win32_read_io;
132 io_wr = win32_write_io;
133 }
134 else if(win32_check_os() == WIN_NT)
135 {
136 ret = dha_enable();
137 if(ret) return ERR_DHA_NOT_FOUND;
138
139 io_rd = win32_read_io;
140 io_wr = win32_write_io;
141
142 instance++;
143 }
144 else if(win32_check_os() == WIN_64)
145 #endif
146 {
147 ret = rwp_open();
148 if(ret) return ERR_DHA_NOT_FOUND;
149
150 io_rd = win64_read_io;
151 io_wr = win64_write_io;
152
153 instance++;
154 }
155
156 return 0;
157 }
158
159 int io_close(unsigned long from)
160 {
161 int ret;
162
163 #ifndef _WIN64
164 if(win32_check_os() == WIN_NT)
165 {
166 instance--;
167 if(!instance)
168 {
169 ret = dha_disable();
170 if(ret) return ERR_DHA_NOT_FOUND;
171 }
172 }
173 else if(win32_check_os() == WIN_64)
174 #endif
175 {
176 instance--;
177 if(!instance)
178 {
179 ret = rwp_close();
180 if(ret) return ERR_DHA_NOT_FOUND;
181 }
182 }
183
184 return 0;
185 }
186
187 /* Used by ser_link.c only (should be used by this module and tig_link.c) */
188
189 int win32_comport_open(char *comPort, PHANDLE hCom)
190 {
191 DCB dcb;
192 BOOL fSuccess;
193 COMMTIMEOUTS cto;
194
195 /* Open COM port */
196 *hCom = CreateFile(comPort, GENERIC_READ | GENERIC_WRITE, 0,
197 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
198 if (hCom == INVALID_HANDLE_VALUE)
199 {
200 ticables_info("CreateFile");
201 print_last_error("CreateFile");
202 return ERR_GRY_CREATEFILE;
203 }
204 // Setup buffer size
205 fSuccess = SetupComm(*hCom, 1024, 1024);
206 if (!fSuccess)
207 {
208 ticables_info("SetupComm");
209 print_last_error("SetupComm");
210 return ERR_GRY_SETUPCOMM;
211 }
212 // Retrieve config structure
213 fSuccess = GetCommState(*hCom, &dcb);
214 if (!fSuccess)
215 {
216 ticables_info("GetCommState");
217 print_last_error("GetCommState");
218 return ERR_GRY_GETCOMMSTATE;
219 }
220 // Fills the structure with config
221 dcb.BaudRate = CBR_9600; // 9600 bauds
222 dcb.fBinary = TRUE; // Binary mode
223 dcb.fParity = FALSE; // Parity checking disabled
224 dcb.fOutxCtsFlow = FALSE; // No output flow control
225 dcb.fOutxDsrFlow = FALSE; // Idem
226 dcb.fDtrControl = DTR_CONTROL_ENABLE; // Provide power supply
227 dcb.fDsrSensitivity = FALSE; // ignore DSR status
228 dcb.fOutX = FALSE; // no XON/XOFF flow control
229 dcb.fInX = FALSE; // idem
230 dcb.fErrorChar = FALSE; // no replacement
231 dcb.fNull = FALSE; // don't discard null chars
232 dcb.fRtsControl = RTS_CONTROL_ENABLE; // Provide power supply
233 dcb.fAbortOnError = FALSE; // do not report errors
234
235 dcb.ByteSize = 8; // 8 bits
236 dcb.Parity = NOPARITY; // no parity checking
237 dcb.StopBits = ONESTOPBIT; // 1 stop bit
238
239 // Config COM port
240 fSuccess = SetCommState(*hCom, &dcb);
241 if (!fSuccess)
242 {
243 ticables_info("SetCommState");
244 print_last_error("SetCommState");
245 return ERR_GRY_SETCOMMSTATE;
246 }
247
248 fSuccess = GetCommTimeouts(*hCom, &cto);
249 if (!fSuccess)
250 {
251 ticables_info("GetCommTimeouts");
252 print_last_error("GetCommTimeouts");
253 return ERR_GRY_GETCOMMTIMEOUT;
254 }
255
256 cto.ReadIntervalTimeout = MAXDWORD;
257 cto.ReadTotalTimeoutMultiplier = 0;
258 cto.ReadTotalTimeoutConstant = 100 * 10/*time_out*/;
259 cto.WriteTotalTimeoutMultiplier = 0;
260 cto.WriteTotalTimeoutConstant = 0; // 0 make non-blocking
261
262 fSuccess = SetCommTimeouts(*hCom, &cto);
263 if (!fSuccess)
264 {
265 ticables_info("SetCommTimeouts");
266 print_last_error("SetCommTimeouts");
267 return ERR_GRY_SETCOMMTIMEOUT;
268 }
269
270 return 0;
271 }
272
273 int win32_comport_close(PHANDLE hCom)
274 {
275 if (*hCom)
276 {
277 CloseHandle(*hCom);
278 *hCom = INVALID_HANDLE_VALUE;
279 }
280
281 return 0;
282 }
283