slirp: VMStatify sbuf
[qemu.git] / chardev / char-win.c
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 #include "qemu/osdep.h"
25 #include "qemu-common.h"
26 #include "qapi/error.h"
27 #include "char-win.h"
28
29 static void win_chr_readfile(Chardev *chr)
30 {
31 WinChardev *s = WIN_CHARDEV(chr);
32
33 int ret, err;
34 uint8_t buf[CHR_READ_BUF_LEN];
35 DWORD size;
36
37 ZeroMemory(&s->orecv, sizeof(s->orecv));
38 s->orecv.hEvent = s->hrecv;
39 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
40 if (!ret) {
41 err = GetLastError();
42 if (err == ERROR_IO_PENDING) {
43 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
44 }
45 }
46
47 if (size > 0) {
48 qemu_chr_be_write(chr, buf, size);
49 }
50 }
51
52 static void win_chr_read(Chardev *chr)
53 {
54 WinChardev *s = WIN_CHARDEV(chr);
55
56 if (s->len > s->max_size) {
57 s->len = s->max_size;
58 }
59 if (s->len == 0) {
60 return;
61 }
62
63 win_chr_readfile(chr);
64 }
65
66 static int win_chr_read_poll(Chardev *chr)
67 {
68 WinChardev *s = WIN_CHARDEV(chr);
69
70 s->max_size = qemu_chr_be_can_write(chr);
71 return s->max_size;
72 }
73
74 static int win_chr_poll(void *opaque)
75 {
76 Chardev *chr = CHARDEV(opaque);
77 WinChardev *s = WIN_CHARDEV(opaque);
78 COMSTAT status;
79 DWORD comerr;
80
81 ClearCommError(s->hcom, &comerr, &status);
82 if (status.cbInQue > 0) {
83 s->len = status.cbInQue;
84 win_chr_read_poll(chr);
85 win_chr_read(chr);
86 return 1;
87 }
88 return 0;
89 }
90
91 int win_chr_init(Chardev *chr, const char *filename, Error **errp)
92 {
93 WinChardev *s = WIN_CHARDEV(chr);
94 COMMCONFIG comcfg;
95 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
96 COMSTAT comstat;
97 DWORD size;
98 DWORD err;
99
100 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
101 if (!s->hsend) {
102 error_setg(errp, "Failed CreateEvent");
103 goto fail;
104 }
105 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
106 if (!s->hrecv) {
107 error_setg(errp, "Failed CreateEvent");
108 goto fail;
109 }
110
111 s->hcom = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
112 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
113 if (s->hcom == INVALID_HANDLE_VALUE) {
114 error_setg(errp, "Failed CreateFile (%lu)", GetLastError());
115 s->hcom = NULL;
116 goto fail;
117 }
118
119 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
120 error_setg(errp, "Failed SetupComm");
121 goto fail;
122 }
123
124 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
125 size = sizeof(COMMCONFIG);
126 GetDefaultCommConfig(filename, &comcfg, &size);
127 comcfg.dcb.DCBlength = sizeof(DCB);
128 CommConfigDialog(filename, NULL, &comcfg);
129
130 if (!SetCommState(s->hcom, &comcfg.dcb)) {
131 error_setg(errp, "Failed SetCommState");
132 goto fail;
133 }
134
135 if (!SetCommMask(s->hcom, EV_ERR)) {
136 error_setg(errp, "Failed SetCommMask");
137 goto fail;
138 }
139
140 cto.ReadIntervalTimeout = MAXDWORD;
141 if (!SetCommTimeouts(s->hcom, &cto)) {
142 error_setg(errp, "Failed SetCommTimeouts");
143 goto fail;
144 }
145
146 if (!ClearCommError(s->hcom, &err, &comstat)) {
147 error_setg(errp, "Failed ClearCommError");
148 goto fail;
149 }
150 qemu_add_polling_cb(win_chr_poll, chr);
151 return 0;
152
153 fail:
154 return -1;
155 }
156
157 int win_chr_pipe_poll(void *opaque)
158 {
159 Chardev *chr = CHARDEV(opaque);
160 WinChardev *s = WIN_CHARDEV(opaque);
161 DWORD size;
162
163 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
164 if (size > 0) {
165 s->len = size;
166 win_chr_read_poll(chr);
167 win_chr_read(chr);
168 return 1;
169 }
170 return 0;
171 }
172
173 /* Called with chr_write_lock held. */
174 static int win_chr_write(Chardev *chr, const uint8_t *buf, int len1)
175 {
176 WinChardev *s = WIN_CHARDEV(chr);
177 DWORD len, ret, size, err;
178
179 len = len1;
180 ZeroMemory(&s->osend, sizeof(s->osend));
181 s->osend.hEvent = s->hsend;
182 while (len > 0) {
183 if (s->hsend) {
184 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
185 } else {
186 ret = WriteFile(s->hcom, buf, len, &size, NULL);
187 }
188 if (!ret) {
189 err = GetLastError();
190 if (err == ERROR_IO_PENDING) {
191 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
192 if (ret) {
193 buf += size;
194 len -= size;
195 } else {
196 break;
197 }
198 } else {
199 break;
200 }
201 } else {
202 buf += size;
203 len -= size;
204 }
205 }
206 return len1 - len;
207 }
208
209 static void char_win_finalize(Object *obj)
210 {
211 Chardev *chr = CHARDEV(obj);
212 WinChardev *s = WIN_CHARDEV(chr);
213
214 if (s->skip_free) {
215 return;
216 }
217
218 if (s->hsend) {
219 CloseHandle(s->hsend);
220 }
221 if (s->hrecv) {
222 CloseHandle(s->hrecv);
223 }
224 if (s->hcom) {
225 CloseHandle(s->hcom);
226 }
227 if (s->fpipe) {
228 qemu_del_polling_cb(win_chr_pipe_poll, chr);
229 } else {
230 qemu_del_polling_cb(win_chr_poll, chr);
231 }
232
233 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
234 }
235
236 void qemu_chr_open_win_file(Chardev *chr, HANDLE fd_out)
237 {
238 WinChardev *s = WIN_CHARDEV(chr);
239
240 s->skip_free = true;
241 s->hcom = fd_out;
242 }
243
244 static void char_win_class_init(ObjectClass *oc, void *data)
245 {
246 ChardevClass *cc = CHARDEV_CLASS(oc);
247
248 cc->chr_write = win_chr_write;
249 }
250
251 static const TypeInfo char_win_type_info = {
252 .name = TYPE_CHARDEV_WIN,
253 .parent = TYPE_CHARDEV,
254 .instance_size = sizeof(WinChardev),
255 .instance_finalize = char_win_finalize,
256 .class_init = char_win_class_init,
257 .abstract = true,
258 };
259
260 static void register_types(void)
261 {
262 type_register_static(&char_win_type_info);
263 }
264
265 type_init(register_types);