2
0

char-serial.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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/module.h"
  26. #include "qemu/option.h"
  27. #include "qemu/sockets.h"
  28. #include "io/channel-file.h"
  29. #include "qapi/error.h"
  30. #ifdef _WIN32
  31. #include "chardev/char-win.h"
  32. #else
  33. #include <sys/ioctl.h>
  34. #include <termios.h>
  35. #include "chardev/char-fd.h"
  36. #endif
  37. #include "chardev/char-serial.h"
  38. #ifdef _WIN32
  39. static void qmp_chardev_open_serial(Chardev *chr,
  40. ChardevBackend *backend,
  41. bool *be_opened,
  42. Error **errp)
  43. {
  44. ChardevHostdev *serial = backend->u.serial.data;
  45. win_chr_serial_init(chr, serial->device, errp);
  46. }
  47. #elif defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
  48. || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
  49. || defined(__GLIBC__) || defined(__APPLE__)
  50. static void tty_serial_init(int fd, int speed,
  51. int parity, int data_bits, int stop_bits)
  52. {
  53. struct termios tty = {0};
  54. speed_t spd;
  55. #if 0
  56. printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
  57. speed, parity, data_bits, stop_bits);
  58. #endif
  59. tcgetattr(fd, &tty);
  60. #define check_speed(val) \
  61. if (speed <= val) { \
  62. spd = B##val; \
  63. goto done; \
  64. }
  65. speed = speed * 10 / 11;
  66. check_speed(50);
  67. check_speed(75);
  68. check_speed(110);
  69. check_speed(134);
  70. check_speed(150);
  71. check_speed(200);
  72. check_speed(300);
  73. check_speed(600);
  74. check_speed(1200);
  75. check_speed(1800);
  76. check_speed(2400);
  77. check_speed(4800);
  78. check_speed(9600);
  79. check_speed(19200);
  80. check_speed(38400);
  81. /* Non-Posix values follow. They may be unsupported on some systems. */
  82. check_speed(57600);
  83. check_speed(115200);
  84. #ifdef B230400
  85. check_speed(230400);
  86. #endif
  87. #ifdef B460800
  88. check_speed(460800);
  89. #endif
  90. #ifdef B500000
  91. check_speed(500000);
  92. #endif
  93. #ifdef B576000
  94. check_speed(576000);
  95. #endif
  96. #ifdef B921600
  97. check_speed(921600);
  98. #endif
  99. #ifdef B1000000
  100. check_speed(1000000);
  101. #endif
  102. #ifdef B1152000
  103. check_speed(1152000);
  104. #endif
  105. #ifdef B1500000
  106. check_speed(1500000);
  107. #endif
  108. #ifdef B2000000
  109. check_speed(2000000);
  110. #endif
  111. #ifdef B2500000
  112. check_speed(2500000);
  113. #endif
  114. #ifdef B3000000
  115. check_speed(3000000);
  116. #endif
  117. #ifdef B3500000
  118. check_speed(3500000);
  119. #endif
  120. #ifdef B4000000
  121. check_speed(4000000);
  122. #endif
  123. spd = B115200;
  124. #undef check_speed
  125. done:
  126. cfsetispeed(&tty, spd);
  127. cfsetospeed(&tty, spd);
  128. tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
  129. | INLCR | IGNCR | ICRNL | IXON);
  130. tty.c_oflag &= ~OPOST;
  131. tty.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG);
  132. tty.c_cflag &= ~(CSIZE | PARENB | PARODD | CRTSCTS | CSTOPB);
  133. switch (data_bits) {
  134. default:
  135. case 8:
  136. tty.c_cflag |= CS8;
  137. break;
  138. case 7:
  139. tty.c_cflag |= CS7;
  140. break;
  141. case 6:
  142. tty.c_cflag |= CS6;
  143. break;
  144. case 5:
  145. tty.c_cflag |= CS5;
  146. break;
  147. }
  148. switch (parity) {
  149. default:
  150. case 'N':
  151. break;
  152. case 'E':
  153. tty.c_cflag |= PARENB;
  154. break;
  155. case 'O':
  156. tty.c_cflag |= PARENB | PARODD;
  157. break;
  158. }
  159. if (stop_bits == 2) {
  160. tty.c_cflag |= CSTOPB;
  161. }
  162. tcsetattr(fd, TCSANOW, &tty);
  163. }
  164. static int tty_serial_ioctl(Chardev *chr, int cmd, void *arg)
  165. {
  166. FDChardev *s = FD_CHARDEV(chr);
  167. QIOChannelFile *fioc = QIO_CHANNEL_FILE(s->ioc_in);
  168. switch (cmd) {
  169. case CHR_IOCTL_SERIAL_SET_PARAMS:
  170. {
  171. QEMUSerialSetParams *ssp = arg;
  172. tty_serial_init(fioc->fd,
  173. ssp->speed, ssp->parity,
  174. ssp->data_bits, ssp->stop_bits);
  175. }
  176. break;
  177. case CHR_IOCTL_SERIAL_SET_BREAK:
  178. {
  179. int enable = *(int *)arg;
  180. if (enable) {
  181. tcsendbreak(fioc->fd, 1);
  182. }
  183. }
  184. break;
  185. case CHR_IOCTL_SERIAL_GET_TIOCM:
  186. {
  187. int sarg = 0;
  188. int *targ = (int *)arg;
  189. ioctl(fioc->fd, TIOCMGET, &sarg);
  190. *targ = 0;
  191. if (sarg & TIOCM_CTS) {
  192. *targ |= CHR_TIOCM_CTS;
  193. }
  194. if (sarg & TIOCM_CAR) {
  195. *targ |= CHR_TIOCM_CAR;
  196. }
  197. if (sarg & TIOCM_DSR) {
  198. *targ |= CHR_TIOCM_DSR;
  199. }
  200. if (sarg & TIOCM_RI) {
  201. *targ |= CHR_TIOCM_RI;
  202. }
  203. if (sarg & TIOCM_DTR) {
  204. *targ |= CHR_TIOCM_DTR;
  205. }
  206. if (sarg & TIOCM_RTS) {
  207. *targ |= CHR_TIOCM_RTS;
  208. }
  209. }
  210. break;
  211. case CHR_IOCTL_SERIAL_SET_TIOCM:
  212. {
  213. int sarg = *(int *)arg;
  214. int targ = 0;
  215. ioctl(fioc->fd, TIOCMGET, &targ);
  216. targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
  217. | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
  218. if (sarg & CHR_TIOCM_CTS) {
  219. targ |= TIOCM_CTS;
  220. }
  221. if (sarg & CHR_TIOCM_CAR) {
  222. targ |= TIOCM_CAR;
  223. }
  224. if (sarg & CHR_TIOCM_DSR) {
  225. targ |= TIOCM_DSR;
  226. }
  227. if (sarg & CHR_TIOCM_RI) {
  228. targ |= TIOCM_RI;
  229. }
  230. if (sarg & CHR_TIOCM_DTR) {
  231. targ |= TIOCM_DTR;
  232. }
  233. if (sarg & CHR_TIOCM_RTS) {
  234. targ |= TIOCM_RTS;
  235. }
  236. ioctl(fioc->fd, TIOCMSET, &targ);
  237. }
  238. break;
  239. default:
  240. return -ENOTSUP;
  241. }
  242. return 0;
  243. }
  244. static void qmp_chardev_open_serial(Chardev *chr,
  245. ChardevBackend *backend,
  246. bool *be_opened,
  247. Error **errp)
  248. {
  249. ChardevHostdev *serial = backend->u.serial.data;
  250. int fd;
  251. fd = qmp_chardev_open_file_source(serial->device, O_RDWR | O_NONBLOCK,
  252. errp);
  253. if (fd < 0) {
  254. return;
  255. }
  256. if (!g_unix_set_fd_nonblocking(fd, true, NULL)) {
  257. error_setg_errno(errp, errno, "Failed to set FD nonblocking");
  258. return;
  259. }
  260. tty_serial_init(fd, 115200, 'N', 8, 1);
  261. qemu_chr_open_fd(chr, fd, fd);
  262. }
  263. #endif /* __linux__ || __sun__ */
  264. #ifdef HAVE_CHARDEV_SERIAL
  265. static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend,
  266. Error **errp)
  267. {
  268. const char *device = qemu_opt_get(opts, "path");
  269. ChardevHostdev *serial;
  270. if (device == NULL) {
  271. error_setg(errp, "chardev: serial/tty: no device path given");
  272. return;
  273. }
  274. backend->type = CHARDEV_BACKEND_KIND_SERIAL;
  275. serial = backend->u.serial.data = g_new0(ChardevHostdev, 1);
  276. qemu_chr_parse_common(opts, qapi_ChardevHostdev_base(serial));
  277. serial->device = g_strdup(device);
  278. }
  279. static void char_serial_class_init(ObjectClass *oc, void *data)
  280. {
  281. ChardevClass *cc = CHARDEV_CLASS(oc);
  282. cc->parse = qemu_chr_parse_serial;
  283. cc->open = qmp_chardev_open_serial;
  284. #ifndef _WIN32
  285. cc->chr_ioctl = tty_serial_ioctl;
  286. #endif
  287. }
  288. static const TypeInfo char_serial_type_info = {
  289. .name = TYPE_CHARDEV_SERIAL,
  290. #ifdef _WIN32
  291. .parent = TYPE_CHARDEV_WIN,
  292. #else
  293. .parent = TYPE_CHARDEV_FD,
  294. #endif
  295. .class_init = char_serial_class_init,
  296. };
  297. static void register_types(void)
  298. {
  299. type_register_static(&char_serial_type_info);
  300. }
  301. type_init(register_types);
  302. #endif