Debian Codegraph @

src/win64/rwp.c

raw symbols SCIP
1 /* Hey EMACS -*- win32-c -*- */
2 /* $Id: dha.c 3701 2007-08-07 18:03:36Z roms $ */
3
4 /* libticables2 - link cable library, a part of the TiLP project
5 * Copyright (C) 2007 Romain Lievin
6 *
7 * Some parts from dhasetup.c source code <http://svn.mplayerhq.hu/mplayer/trunk/vidix/dhahelperwin/>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24
25 /*
26 This unit provides RwPorts access routines for NT-64 I/O port accesses.
27 */
28
29 #include <stdio.h>
30 #include <windows.h>
31 #include <winioctl.h>
32
33 #include "rwp.h"
34 #include "./rwp/rwports.h"
35
36 #define RWP_DRV_NAME "RwPorts"
37
38 #if defined(__WIN32__) || defined(_WIN64)
39
40 #if !defined(__MINGW32__) || defined(_WIN64)
41 /* Under MinGW, both dha.c and rwp.c are included into detect.c, so avoid the
42 duplicate definition. */
43 static void print_last_error(const char *s)
44 {
45 LPTSTR lpMsgBuf;
46
47 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
48 FORMAT_MESSAGE_FROM_SYSTEM |
49 FORMAT_MESSAGE_IGNORE_INSERTS,
50 NULL, GetLastError(),
51 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
52 (LPTSTR) & lpMsgBuf, 0, NULL);
53
54 lpMsgBuf[strlen(lpMsgBuf)-2] = '\0';
55
56 printf("%s (%lu -> %s)\n", s, (unsigned long)GetLastError(), lpMsgBuf);
57 }
58 #endif
59
60 int rwp_detect(int* result)
61 {
62 HANDLE hDriver;
63 *result = 0;
64
65 hDriver = CreateFile("\\\\.\\RwPorts", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
66 if(hDriver == INVALID_HANDLE_VALUE)
67 {
68 hDriver = CreateFile("\\\\.\\RwPorts", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
69 if(hDriver != INVALID_HANDLE_VALUE)
70 {
71 CloseHandle(hDriver);
72 hDriver = INVALID_HANDLE_VALUE;
73 *result = 1;
74 }
75 }
76 else
77 {
78 CloseHandle(hDriver);
79 hDriver = INVALID_HANDLE_VALUE;
80 *result = 1;
81 }
82
83 printf(RWP_DRV_NAME "%sfound.\n", *result ? " " : " not ");
84 return 0;
85 }
86
87 static HANDLE hDriver = INVALID_HANDLE_VALUE;
88
89 int rwp_open(void)
90 {
91 hDriver = CreateFile("\\\\.\\RwPorts",
92 GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
93 if(hDriver == INVALID_HANDLE_VALUE)
94 {
95 printf("Couldn't access " RWP_DRV_NAME " driver; Please ensure driver is installed/loaded.");
96 return -1;
97 }
98
99 return 0;
100 }
101
102 int rwp_close(void)
103 {
104 CloseHandle(hDriver);
105 hDriver = INVALID_HANDLE_VALUE;
106
107 return 0;
108 }
109
110 int rwp_read_byte(unsigned short address, unsigned char *data)
111 {
112 DWORD BytesReturned;
113 int iError;
114 int result = 0;
115 unsigned char buf[3];
116
117 *((unsigned short *)buf) = (unsigned short)address;
118 iError = DeviceIoControl(hDriver, IOCTL_RWPORTS_READ_BYTE, &buf, 2, &buf, 1, &BytesReturned, NULL);
119 if(!iError)
120 {
121 print_last_error("RwPorts: error occurred in IOCTL_RWPORTS_READ_BYTE");
122 result = -1;
123 }
124 else
125 {
126 *data = buf[0];
127 //printf("I/O ports read at 0x%04x: %02x\n", address, *data);
128 }
129
130 return result;
131 }
132
133 int rwp_write_byte(unsigned short address, unsigned char data)
134 {
135 DWORD BytesReturned;
136 int iError;
137 int result = 0;
138 unsigned char buf[3];
139
140 *((unsigned short *)buf) = (unsigned short)address; buf[2] = data;
141 iError = DeviceIoControl(hDriver, IOCTL_RWPORTS_WRITE_BYTE, &buf, 3, &buf, 1, &BytesReturned, NULL);
142 if(!iError)
143 {
144 print_last_error("RwPorts: error occurred in IOCTL_RWPORTS_WRITE_BYTE");
145 result = -1;
146 }
147 //else printf("I/O ports write at 0x%04x.\n", address);
148
149 return result;
150 }
151
152 #endif