char-win.c 6.4 KB

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