Marc-André Lureau | 503ebef | 2016-12-12 17:38:21 +0300 | [diff] [blame] | 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 | */ |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 24 | |
Marc-André Lureau | 503ebef | 2016-12-12 17:38:21 +0300 | [diff] [blame] | 25 | #include "qemu/osdep.h" |
Markus Armbruster | db72581 | 2019-08-12 07:23:50 +0200 | [diff] [blame] | 26 | #include "qemu/main-loop.h" |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 27 | #include "qemu/module.h" |
Marc-André Lureau | 503ebef | 2016-12-12 17:38:21 +0300 | [diff] [blame] | 28 | #include "qapi/error.h" |
Marc-André Lureau | 8228e35 | 2017-01-26 17:19:46 +0400 | [diff] [blame] | 29 | #include "chardev/char-win.h" |
Marc-André Lureau | 503ebef | 2016-12-12 17:38:21 +0300 | [diff] [blame] | 30 | |
Marc-André Lureau | 6ce8e0e | 2017-01-04 20:03:08 +0100 | [diff] [blame] | 31 | static void win_chr_read(Chardev *chr, DWORD len) |
Marc-André Lureau | 503ebef | 2016-12-12 17:38:21 +0300 | [diff] [blame] | 32 | { |
| 33 | WinChardev *s = WIN_CHARDEV(chr); |
Marc-André Lureau | b88ee02 | 2017-01-04 19:37:19 +0100 | [diff] [blame] | 34 | int max_size = qemu_chr_be_can_write(chr); |
Marc-André Lureau | 503ebef | 2016-12-12 17:38:21 +0300 | [diff] [blame] | 35 | int ret, err; |
| 36 | uint8_t buf[CHR_READ_BUF_LEN]; |
| 37 | DWORD size; |
| 38 | |
Marc-André Lureau | 6ce8e0e | 2017-01-04 20:03:08 +0100 | [diff] [blame] | 39 | if (len > max_size) { |
| 40 | len = max_size; |
Marc-André Lureau | b88ee02 | 2017-01-04 19:37:19 +0100 | [diff] [blame] | 41 | } |
Marc-André Lureau | 6ce8e0e | 2017-01-04 20:03:08 +0100 | [diff] [blame] | 42 | if (len == 0) { |
Marc-André Lureau | b88ee02 | 2017-01-04 19:37:19 +0100 | [diff] [blame] | 43 | return; |
| 44 | } |
| 45 | |
Marc-André Lureau | 503ebef | 2016-12-12 17:38:21 +0300 | [diff] [blame] | 46 | ZeroMemory(&s->orecv, sizeof(s->orecv)); |
| 47 | s->orecv.hEvent = s->hrecv; |
Marc-André Lureau | ef0f272 | 2017-01-04 21:37:01 +0100 | [diff] [blame] | 48 | ret = ReadFile(s->file, buf, len, &size, &s->orecv); |
Marc-André Lureau | 503ebef | 2016-12-12 17:38:21 +0300 | [diff] [blame] | 49 | if (!ret) { |
| 50 | err = GetLastError(); |
| 51 | if (err == ERROR_IO_PENDING) { |
Marc-André Lureau | ef0f272 | 2017-01-04 21:37:01 +0100 | [diff] [blame] | 52 | ret = GetOverlappedResult(s->file, &s->orecv, &size, TRUE); |
Marc-André Lureau | 503ebef | 2016-12-12 17:38:21 +0300 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
| 56 | if (size > 0) { |
| 57 | qemu_chr_be_write(chr, buf, size); |
| 58 | } |
| 59 | } |
| 60 | |
Marc-André Lureau | 221e659 | 2017-01-04 19:51:46 +0100 | [diff] [blame] | 61 | static int win_chr_serial_poll(void *opaque) |
Marc-André Lureau | 503ebef | 2016-12-12 17:38:21 +0300 | [diff] [blame] | 62 | { |
| 63 | Chardev *chr = CHARDEV(opaque); |
| 64 | WinChardev *s = WIN_CHARDEV(opaque); |
| 65 | COMSTAT status; |
| 66 | DWORD comerr; |
| 67 | |
Marc-André Lureau | ef0f272 | 2017-01-04 21:37:01 +0100 | [diff] [blame] | 68 | ClearCommError(s->file, &comerr, &status); |
Marc-André Lureau | 503ebef | 2016-12-12 17:38:21 +0300 | [diff] [blame] | 69 | if (status.cbInQue > 0) { |
Marc-André Lureau | 6ce8e0e | 2017-01-04 20:03:08 +0100 | [diff] [blame] | 70 | win_chr_read(chr, status.cbInQue); |
Marc-André Lureau | 503ebef | 2016-12-12 17:38:21 +0300 | [diff] [blame] | 71 | return 1; |
| 72 | } |
| 73 | return 0; |
| 74 | } |
| 75 | |
Marc-André Lureau | 221e659 | 2017-01-04 19:51:46 +0100 | [diff] [blame] | 76 | int win_chr_serial_init(Chardev *chr, const char *filename, Error **errp) |
Marc-André Lureau | 503ebef | 2016-12-12 17:38:21 +0300 | [diff] [blame] | 77 | { |
| 78 | WinChardev *s = WIN_CHARDEV(chr); |
| 79 | COMMCONFIG comcfg; |
| 80 | COMMTIMEOUTS cto = { 0, 0, 0, 0, 0}; |
| 81 | COMSTAT comstat; |
| 82 | DWORD size; |
| 83 | DWORD err; |
| 84 | |
| 85 | s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 86 | if (!s->hsend) { |
| 87 | error_setg(errp, "Failed CreateEvent"); |
| 88 | goto fail; |
| 89 | } |
| 90 | s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 91 | if (!s->hrecv) { |
| 92 | error_setg(errp, "Failed CreateEvent"); |
| 93 | goto fail; |
| 94 | } |
| 95 | |
Marc-André Lureau | ef0f272 | 2017-01-04 21:37:01 +0100 | [diff] [blame] | 96 | s->file = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL, |
Marc-André Lureau | 503ebef | 2016-12-12 17:38:21 +0300 | [diff] [blame] | 97 | OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0); |
Marc-André Lureau | ef0f272 | 2017-01-04 21:37:01 +0100 | [diff] [blame] | 98 | if (s->file == INVALID_HANDLE_VALUE) { |
Philippe Mathieu-Daudé | 4c23519 | 2020-02-28 11:07:23 +0100 | [diff] [blame] | 99 | error_setg_win32(errp, GetLastError(), "Failed CreateFile"); |
Marc-André Lureau | ef0f272 | 2017-01-04 21:37:01 +0100 | [diff] [blame] | 100 | s->file = NULL; |
Marc-André Lureau | 503ebef | 2016-12-12 17:38:21 +0300 | [diff] [blame] | 101 | goto fail; |
| 102 | } |
| 103 | |
Marc-André Lureau | ef0f272 | 2017-01-04 21:37:01 +0100 | [diff] [blame] | 104 | if (!SetupComm(s->file, NRECVBUF, NSENDBUF)) { |
Marc-André Lureau | 503ebef | 2016-12-12 17:38:21 +0300 | [diff] [blame] | 105 | error_setg(errp, "Failed SetupComm"); |
| 106 | goto fail; |
| 107 | } |
| 108 | |
| 109 | ZeroMemory(&comcfg, sizeof(COMMCONFIG)); |
| 110 | size = sizeof(COMMCONFIG); |
| 111 | GetDefaultCommConfig(filename, &comcfg, &size); |
| 112 | comcfg.dcb.DCBlength = sizeof(DCB); |
| 113 | CommConfigDialog(filename, NULL, &comcfg); |
| 114 | |
Marc-André Lureau | ef0f272 | 2017-01-04 21:37:01 +0100 | [diff] [blame] | 115 | if (!SetCommState(s->file, &comcfg.dcb)) { |
Marc-André Lureau | 503ebef | 2016-12-12 17:38:21 +0300 | [diff] [blame] | 116 | error_setg(errp, "Failed SetCommState"); |
| 117 | goto fail; |
| 118 | } |
| 119 | |
Marc-André Lureau | ef0f272 | 2017-01-04 21:37:01 +0100 | [diff] [blame] | 120 | if (!SetCommMask(s->file, EV_ERR)) { |
Marc-André Lureau | 503ebef | 2016-12-12 17:38:21 +0300 | [diff] [blame] | 121 | error_setg(errp, "Failed SetCommMask"); |
| 122 | goto fail; |
| 123 | } |
| 124 | |
| 125 | cto.ReadIntervalTimeout = MAXDWORD; |
Marc-André Lureau | ef0f272 | 2017-01-04 21:37:01 +0100 | [diff] [blame] | 126 | if (!SetCommTimeouts(s->file, &cto)) { |
Marc-André Lureau | 503ebef | 2016-12-12 17:38:21 +0300 | [diff] [blame] | 127 | error_setg(errp, "Failed SetCommTimeouts"); |
| 128 | goto fail; |
| 129 | } |
| 130 | |
Marc-André Lureau | ef0f272 | 2017-01-04 21:37:01 +0100 | [diff] [blame] | 131 | if (!ClearCommError(s->file, &err, &comstat)) { |
Marc-André Lureau | 503ebef | 2016-12-12 17:38:21 +0300 | [diff] [blame] | 132 | error_setg(errp, "Failed ClearCommError"); |
| 133 | goto fail; |
| 134 | } |
Marc-André Lureau | 221e659 | 2017-01-04 19:51:46 +0100 | [diff] [blame] | 135 | qemu_add_polling_cb(win_chr_serial_poll, chr); |
Marc-André Lureau | 503ebef | 2016-12-12 17:38:21 +0300 | [diff] [blame] | 136 | return 0; |
| 137 | |
| 138 | fail: |
| 139 | return -1; |
| 140 | } |
| 141 | |
| 142 | int win_chr_pipe_poll(void *opaque) |
| 143 | { |
| 144 | Chardev *chr = CHARDEV(opaque); |
| 145 | WinChardev *s = WIN_CHARDEV(opaque); |
| 146 | DWORD size; |
| 147 | |
Marc-André Lureau | ef0f272 | 2017-01-04 21:37:01 +0100 | [diff] [blame] | 148 | PeekNamedPipe(s->file, NULL, 0, NULL, &size, NULL); |
Marc-André Lureau | 503ebef | 2016-12-12 17:38:21 +0300 | [diff] [blame] | 149 | if (size > 0) { |
Marc-André Lureau | 6ce8e0e | 2017-01-04 20:03:08 +0100 | [diff] [blame] | 150 | win_chr_read(chr, size); |
Marc-André Lureau | 503ebef | 2016-12-12 17:38:21 +0300 | [diff] [blame] | 151 | return 1; |
| 152 | } |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | /* Called with chr_write_lock held. */ |
| 157 | static int win_chr_write(Chardev *chr, const uint8_t *buf, int len1) |
| 158 | { |
| 159 | WinChardev *s = WIN_CHARDEV(chr); |
| 160 | DWORD len, ret, size, err; |
| 161 | |
| 162 | len = len1; |
| 163 | ZeroMemory(&s->osend, sizeof(s->osend)); |
| 164 | s->osend.hEvent = s->hsend; |
| 165 | while (len > 0) { |
| 166 | if (s->hsend) { |
Marc-André Lureau | ef0f272 | 2017-01-04 21:37:01 +0100 | [diff] [blame] | 167 | ret = WriteFile(s->file, buf, len, &size, &s->osend); |
Marc-André Lureau | 503ebef | 2016-12-12 17:38:21 +0300 | [diff] [blame] | 168 | } else { |
Marc-André Lureau | ef0f272 | 2017-01-04 21:37:01 +0100 | [diff] [blame] | 169 | ret = WriteFile(s->file, buf, len, &size, NULL); |
Marc-André Lureau | 503ebef | 2016-12-12 17:38:21 +0300 | [diff] [blame] | 170 | } |
| 171 | if (!ret) { |
| 172 | err = GetLastError(); |
| 173 | if (err == ERROR_IO_PENDING) { |
Marc-André Lureau | ef0f272 | 2017-01-04 21:37:01 +0100 | [diff] [blame] | 174 | ret = GetOverlappedResult(s->file, &s->osend, &size, TRUE); |
Marc-André Lureau | 503ebef | 2016-12-12 17:38:21 +0300 | [diff] [blame] | 175 | if (ret) { |
| 176 | buf += size; |
| 177 | len -= size; |
| 178 | } else { |
| 179 | break; |
| 180 | } |
| 181 | } else { |
| 182 | break; |
| 183 | } |
| 184 | } else { |
| 185 | buf += size; |
| 186 | len -= size; |
| 187 | } |
| 188 | } |
| 189 | return len1 - len; |
| 190 | } |
| 191 | |
| 192 | static void char_win_finalize(Object *obj) |
| 193 | { |
| 194 | Chardev *chr = CHARDEV(obj); |
| 195 | WinChardev *s = WIN_CHARDEV(chr); |
| 196 | |
Marc-André Lureau | 503ebef | 2016-12-12 17:38:21 +0300 | [diff] [blame] | 197 | if (s->hsend) { |
| 198 | CloseHandle(s->hsend); |
| 199 | } |
| 200 | if (s->hrecv) { |
| 201 | CloseHandle(s->hrecv); |
| 202 | } |
Marc-André Lureau | 541815f | 2017-01-04 21:34:42 +0100 | [diff] [blame] | 203 | if (!s->keep_open && s->file) { |
Marc-André Lureau | ef0f272 | 2017-01-04 21:37:01 +0100 | [diff] [blame] | 204 | CloseHandle(s->file); |
Marc-André Lureau | 503ebef | 2016-12-12 17:38:21 +0300 | [diff] [blame] | 205 | } |
| 206 | if (s->fpipe) { |
| 207 | qemu_del_polling_cb(win_chr_pipe_poll, chr); |
| 208 | } else { |
Marc-André Lureau | 221e659 | 2017-01-04 19:51:46 +0100 | [diff] [blame] | 209 | qemu_del_polling_cb(win_chr_serial_poll, chr); |
Marc-André Lureau | 503ebef | 2016-12-12 17:38:21 +0300 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
| 213 | } |
| 214 | |
Marc-André Lureau | 541815f | 2017-01-04 21:34:42 +0100 | [diff] [blame] | 215 | void win_chr_set_file(Chardev *chr, HANDLE file, bool keep_open) |
Marc-André Lureau | 503ebef | 2016-12-12 17:38:21 +0300 | [diff] [blame] | 216 | { |
| 217 | WinChardev *s = WIN_CHARDEV(chr); |
| 218 | |
Marc-André Lureau | 541815f | 2017-01-04 21:34:42 +0100 | [diff] [blame] | 219 | s->keep_open = keep_open; |
| 220 | s->file = file; |
Marc-André Lureau | 503ebef | 2016-12-12 17:38:21 +0300 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | static void char_win_class_init(ObjectClass *oc, void *data) |
| 224 | { |
| 225 | ChardevClass *cc = CHARDEV_CLASS(oc); |
| 226 | |
| 227 | cc->chr_write = win_chr_write; |
| 228 | } |
| 229 | |
| 230 | static const TypeInfo char_win_type_info = { |
| 231 | .name = TYPE_CHARDEV_WIN, |
| 232 | .parent = TYPE_CHARDEV, |
| 233 | .instance_size = sizeof(WinChardev), |
| 234 | .instance_finalize = char_win_finalize, |
| 235 | .class_init = char_win_class_init, |
| 236 | .abstract = true, |
| 237 | }; |
| 238 | |
| 239 | static void register_types(void) |
| 240 | { |
| 241 | type_register_static(&char_win_type_info); |
| 242 | } |
| 243 | |
| 244 | type_init(register_types); |