master
c 248 lines 6.43 KB
Raw
1 /*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25 #include "qemu/osdep.h"
26 #include "qemu/main-loop.h"
27 #include "qemu/module.h"
28 #include "qapi/error.h"
29 #include "chardev/char-win.h"
30
31 static int win_chr_read(Chardev *chr, DWORD len)
32 {
33 WinChardev *s = WIN_CHARDEV(chr);
34 int max_size = qemu_chr_be_can_write(chr);
35 int ret, err;
36 uint8_t buf[CHR_READ_BUF_LEN];
37 DWORD size;
38
39 if (len > max_size) {
40 len = max_size;
41 }
42 if (len == 0) {
43 return 0;
44 }
45
46 ZeroMemory(&s->orecv, sizeof(s->orecv));
47 s->orecv.hEvent = s->hrecv;
48 ret = ReadFile(s->file, buf, len, &size, &s->orecv);
49 if (!ret) {
50 err = GetLastError();
51 if (err == ERROR_IO_PENDING) {
52 ret = GetOverlappedResult(s->file, &s->orecv, &size, TRUE);
53 }
54 }
55
56 if (size > 0) {
57 qemu_chr_be_write(chr, buf, size);
58 }
59
60 return size > 0 ? 1 : 0;
61 }
62
63 static int win_chr_serial_poll(void *opaque)
64 {
65 Chardev *chr = CHARDEV(opaque);
66 WinChardev *s = WIN_CHARDEV(opaque);
67 COMSTAT status;
68 DWORD comerr;
69
70 ClearCommError(s->file, &comerr, &status);
71 if (status.cbInQue > 0) {
72 if (win_chr_read(chr, status.cbInQue)) {
73 return 1;
74 }
75 }
76 return 0;
77 }
78
79 int win_chr_serial_init(Chardev *chr, const char *filename, Error **errp)
80 {
81 WinChardev *s = WIN_CHARDEV(chr);
82 COMMCONFIG comcfg;
83 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
84 COMSTAT comstat;
85 DWORD size;
86 DWORD err;
87
88 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
89 if (!s->hsend) {
90 error_setg(errp, "Failed CreateEvent");
91 goto fail;
92 }
93 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
94 if (!s->hrecv) {
95 error_setg(errp, "Failed CreateEvent");
96 goto fail;
97 }
98
99 s->file = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
100 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
101 if (s->file == INVALID_HANDLE_VALUE) {
102 error_setg_win32(errp, GetLastError(), "Failed CreateFile");
103 s->file = NULL;
104 goto fail;
105 }
106
107 if (!SetupComm(s->file, NRECVBUF, NSENDBUF)) {
108 error_setg(errp, "Failed SetupComm");
109 goto fail;
110 }
111
112 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
113 size = sizeof(COMMCONFIG);
114 GetDefaultCommConfig(filename, &comcfg, &size);
115 comcfg.dcb.DCBlength = sizeof(DCB);
116 CommConfigDialog(filename, NULL, &comcfg);
117
118 if (!SetCommState(s->file, &comcfg.dcb)) {
119 error_setg(errp, "Failed SetCommState");
120 goto fail;
121 }
122
123 if (!SetCommMask(s->file, EV_ERR)) {
124 error_setg(errp, "Failed SetCommMask");
125 goto fail;
126 }
127
128 cto.ReadIntervalTimeout = MAXDWORD;
129 if (!SetCommTimeouts(s->file, &cto)) {
130 error_setg(errp, "Failed SetCommTimeouts");
131 goto fail;
132 }
133
134 if (!ClearCommError(s->file, &err, &comstat)) {
135 error_setg(errp, "Failed ClearCommError");
136 goto fail;
137 }
138 qemu_add_polling_cb(win_chr_serial_poll, chr);
139 return 0;
140
141 fail:
142 return -1;
143 }
144
145 int win_chr_pipe_poll(void *opaque)
146 {
147 Chardev *chr = CHARDEV(opaque);
148 WinChardev *s = WIN_CHARDEV(opaque);
149 DWORD size;
150
151 PeekNamedPipe(s->file, NULL, 0, NULL, &size, NULL);
152 if (size > 0) {
153 if (win_chr_read(chr, size)) {
154 return 1;
155 }
156 }
157 return 0;
158 }
159
160 /* Called with chr_write_lock held. */
161 static int win_chr_write(Chardev *chr, const uint8_t *buf, int len1)
162 {
163 WinChardev *s = WIN_CHARDEV(chr);
164 DWORD len, ret, size, err;
165
166 len = len1;
167 ZeroMemory(&s->osend, sizeof(s->osend));
168 s->osend.hEvent = s->hsend;
169 while (len > 0) {
170 if (s->hsend) {
171 ret = WriteFile(s->file, buf, len, &size, &s->osend);
172 } else {
173 ret = WriteFile(s->file, buf, len, &size, NULL);
174 }
175 if (!ret) {
176 err = GetLastError();
177 if (err == ERROR_IO_PENDING) {
178 ret = GetOverlappedResult(s->file, &s->osend, &size, TRUE);
179 if (ret) {
180 buf += size;
181 len -= size;
182 } else {
183 break;
184 }
185 } else {
186 break;
187 }
188 } else {
189 buf += size;
190 len -= size;
191 }
192 }
193 return len1 - len;
194 }
195
196 static void char_win_finalize(Object *obj)
197 {
198 Chardev *chr = CHARDEV(obj);
199 WinChardev *s = WIN_CHARDEV(chr);
200
201 if (s->hsend) {
202 CloseHandle(s->hsend);
203 }
204 if (s->hrecv) {
205 CloseHandle(s->hrecv);
206 }
207 if (!s->keep_open && s->file) {
208 CloseHandle(s->file);
209 }
210 if (s->fpipe) {
211 qemu_del_polling_cb(win_chr_pipe_poll, chr);
212 } else {
213 qemu_del_polling_cb(win_chr_serial_poll, chr);
214 }
215
216 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
217 }
218
219 void win_chr_set_file(Chardev *chr, HANDLE file, bool keep_open)
220 {
221 WinChardev *s = WIN_CHARDEV(chr);
222
223 s->keep_open = keep_open;
224 s->file = file;
225 }
226
227 static void char_win_class_init(ObjectClass *oc, const void *data)
228 {
229 ChardevClass *cc = CHARDEV_CLASS(oc);
230
231 cc->chr_write = win_chr_write;
232 }
233
234 static const TypeInfo char_win_type_info = {
235 .name = TYPE_CHARDEV_WIN,
236 .parent = TYPE_CHARDEV,
237 .instance_size = sizeof(WinChardev),
238 .instance_finalize = char_win_finalize,
239 .class_init = char_win_class_init,
240 .abstract = true,
241 };
242
243 static void register_types(void)
244 {
245 type_register_static(&char_win_type_info);
246 }
247
248 type_init(register_types);