char-win.c 6.3 KB

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