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