target-arm: Add support for AArch64 PMU register PMXEVTYPER_EL0
[qemu/ar7.git] / chardev / char-win.c
blobe4b6957ded20b132a4cce616d690c794d9400ab3
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 #include "qemu/osdep.h"
25 #include "qemu-common.h"
26 #include "qapi/error.h"
27 #include "char-win.h"
29 static void win_chr_readfile(Chardev *chr)
31 WinChardev *s = WIN_CHARDEV(chr);
33 int ret, err;
34 uint8_t buf[CHR_READ_BUF_LEN];
35 DWORD size;
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);
47 if (size > 0) {
48 qemu_chr_be_write(chr, buf, size);
52 static void win_chr_read(Chardev *chr)
54 WinChardev *s = WIN_CHARDEV(chr);
56 if (s->len > s->max_size) {
57 s->len = s->max_size;
59 if (s->len == 0) {
60 return;
63 win_chr_readfile(chr);
66 static int win_chr_read_poll(Chardev *chr)
68 WinChardev *s = WIN_CHARDEV(chr);
70 s->max_size = qemu_chr_be_can_write(chr);
71 return s->max_size;
74 static int win_chr_poll(void *opaque)
76 Chardev *chr = CHARDEV(opaque);
77 WinChardev *s = WIN_CHARDEV(opaque);
78 COMSTAT status;
79 DWORD comerr;
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;
88 return 0;
91 int win_chr_init(Chardev *chr, const char *filename, Error **errp)
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;
100 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
101 if (!s->hsend) {
102 error_setg(errp, "Failed CreateEvent");
103 goto fail;
105 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
106 if (!s->hrecv) {
107 error_setg(errp, "Failed CreateEvent");
108 goto fail;
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;
119 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
120 error_setg(errp, "Failed SetupComm");
121 goto fail;
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);
130 if (!SetCommState(s->hcom, &comcfg.dcb)) {
131 error_setg(errp, "Failed SetCommState");
132 goto fail;
135 if (!SetCommMask(s->hcom, EV_ERR)) {
136 error_setg(errp, "Failed SetCommMask");
137 goto fail;
140 cto.ReadIntervalTimeout = MAXDWORD;
141 if (!SetCommTimeouts(s->hcom, &cto)) {
142 error_setg(errp, "Failed SetCommTimeouts");
143 goto fail;
146 if (!ClearCommError(s->hcom, &err, &comstat)) {
147 error_setg(errp, "Failed ClearCommError");
148 goto fail;
150 qemu_add_polling_cb(win_chr_poll, chr);
151 return 0;
153 fail:
154 return -1;
157 int win_chr_pipe_poll(void *opaque)
159 Chardev *chr = CHARDEV(opaque);
160 WinChardev *s = WIN_CHARDEV(opaque);
161 DWORD size;
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;
170 return 0;
173 /* Called with chr_write_lock held. */
174 static int win_chr_write(Chardev *chr, const uint8_t *buf, int len1)
176 WinChardev *s = WIN_CHARDEV(chr);
177 DWORD len, ret, size, err;
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);
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;
198 } else {
199 break;
201 } else {
202 buf += size;
203 len -= size;
206 return len1 - len;
209 static void char_win_finalize(Object *obj)
211 Chardev *chr = CHARDEV(obj);
212 WinChardev *s = WIN_CHARDEV(chr);
214 if (s->skip_free) {
215 return;
218 if (s->hsend) {
219 CloseHandle(s->hsend);
221 if (s->hrecv) {
222 CloseHandle(s->hrecv);
224 if (s->hcom) {
225 CloseHandle(s->hcom);
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);
233 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
236 void qemu_chr_open_win_file(Chardev *chr, HANDLE fd_out)
238 WinChardev *s = WIN_CHARDEV(chr);
240 s->skip_free = true;
241 s->hcom = fd_out;
244 static void char_win_class_init(ObjectClass *oc, void *data)
246 ChardevClass *cc = CHARDEV_CLASS(oc);
248 cc->chr_write = win_chr_write;
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,
260 static void register_types(void)
262 type_register_static(&char_win_type_info);
265 type_init(register_types);