Debian Codegraph @

src/win32/dha.c

raw symbols SCIP
1 /* Hey EMACS -*- win32-c -*- */
2 /* $Id$ */
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 DhaHelper access routines for NT I/O port accesses.
27 */
28
29 #include <stdio.h>
30 #include <windows.h>
31 #include <winioctl.h>
32
33 #include "dha.h"
34 #include "./dha/dhahelper.h"
35
36 #define DHA_DRV_NAME "DhaHelper"
37
38 #if defined(__WIN32__) && !defined(_WIN64)
39
40 static void print_last_error(const char *s)
41 {
42 LPTSTR lpMsgBuf;
43
44 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
45 FORMAT_MESSAGE_FROM_SYSTEM |
46 FORMAT_MESSAGE_IGNORE_INSERTS,
47 NULL, GetLastError(),
48 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
49 (LPTSTR) & lpMsgBuf, 0, NULL);
50
51 lpMsgBuf[strlen(lpMsgBuf)-2] = '\0';
52
53 printf("%s (%lu -> %s)\n", s, (unsigned long)GetLastError(), lpMsgBuf);
54 }
55
56 int dha_detect(int* result)
57 {
58 HANDLE hDriver;
59 *result = 0;
60
61 hDriver = CreateFile("\\\\.\\DhaHelper", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
62 if(hDriver == INVALID_HANDLE_VALUE)
63 {
64 hDriver = CreateFile("\\\\.\\DhaHelper", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
65 if(hDriver != INVALID_HANDLE_VALUE)
66 {
67 CloseHandle(hDriver);
68 hDriver = INVALID_HANDLE_VALUE;
69 *result = 1;
70 }
71 }
72 else
73 {
74 CloseHandle(hDriver);
75 hDriver = INVALID_HANDLE_VALUE;
76 *result = 1;
77 }
78
79 printf(DHA_DRV_NAME "%sfound.\n", *result ? " " : " not ");
80 return 0;
81 }
82
83 int dha_enable(void)
84 {
85 HANDLE hDriver;
86 DWORD BytesReturned;
87 int iError;
88 int result = 0;
89
90 hDriver = CreateFile("\\\\.\\DhaHelper",
91 GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
92 if(hDriver == INVALID_HANDLE_VALUE)
93 {
94 printf("Couldn't access " DHA_DRV_NAME " driver; Please ensure driver is installed/loaded.");
95 return -1;
96 }
97
98 iError = DeviceIoControl(hDriver, IOCTL_DHAHELPER_ENABLEDIRECTIO, NULL, 0, NULL, 0, &BytesReturned, NULL);
99 if(!iError)
100 {
101 print_last_error("DhaHelper: error %d occurred in IOCTL_DHAHELPER_ENABLEDIRECTIO");
102 result = -1;
103 }
104 else
105 printf("I/O ports have been enabled.\n");
106
107 CloseHandle(hDriver);
108 hDriver = INVALID_HANDLE_VALUE;
109
110 return result;
111 }
112
113 int dha_disable(void)
114 {
115 HANDLE hDriver;
116 DWORD BytesReturned;
117 int iError;
118 int result = 0;
119
120 hDriver = CreateFile("\\\\.\\DhaHelper",
121 GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
122 if(hDriver == INVALID_HANDLE_VALUE)
123 {
124 printf("Couldn't access " DHA_DRV_NAME " driver; Please ensure driver is installed/loaded.");
125 return -1;
126 }
127
128 iError = DeviceIoControl(hDriver, IOCTL_DHAHELPER_DISABLEDIRECTIO, NULL, 0, NULL, 0, &BytesReturned, NULL);
129 if(!iError)
130 {
131 print_last_error("DhaHelper: error %d occurred in IOCTL_DHAHELPER_DISABLEDIRECTIO");
132 result = -1;
133 }
134 else
135 printf("I/O ports have been disabled.\n");
136
137 CloseHandle(hDriver);
138 hDriver = INVALID_HANDLE_VALUE;
139
140 return result;
141 }
142
143 #endif