4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
24 #include "qemu/osdep.h"
25 #include "qemu/sockets.h"
26 #include "io/channel-file.h"
27 #include "qapi/error.h"
30 #include "chardev/char-win.h"
32 #include <sys/ioctl.h>
34 #include "chardev/char-fd.h"
37 #include "chardev/char-serial.h"
41 static void qmp_chardev_open_serial(Chardev *chr,
42 ChardevBackend *backend,
46 ChardevHostdev *serial = backend->u.serial.data;
48 win_chr_serial_init(chr, serial->device, errp);
51 #elif defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
52 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
55 static void tty_serial_init(int fd, int speed,
56 int parity, int data_bits, int stop_bits)
62 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
63 speed, parity, data_bits, stop_bits);
67 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
68 speed = speed * 10 / 11;
85 /* Non-Posix values follow. They may be unsupported on some systems. */
104 check_speed(1000000);
107 check_speed(1152000);
110 check_speed(1500000);
113 check_speed(2000000);
116 check_speed(2500000);
119 check_speed(3000000);
122 check_speed(3500000);
125 check_speed(4000000);
130 cfsetispeed(&tty, spd);
131 cfsetospeed(&tty, spd);
133 tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
134 | INLCR | IGNCR | ICRNL | IXON);
135 tty.c_oflag |= OPOST;
136 tty.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG);
137 tty.c_cflag &= ~(CSIZE | PARENB | PARODD | CRTSCTS | CSTOPB);
158 tty.c_cflag |= PARENB;
161 tty.c_cflag |= PARENB | PARODD;
164 if (stop_bits == 2) {
165 tty.c_cflag |= CSTOPB;
168 tcsetattr(fd, TCSANOW, &tty);
171 static int tty_serial_ioctl(Chardev *chr, int cmd, void *arg)
173 FDChardev *s = FD_CHARDEV(chr);
174 QIOChannelFile *fioc = QIO_CHANNEL_FILE(s->ioc_in);
177 case CHR_IOCTL_SERIAL_SET_PARAMS:
179 QEMUSerialSetParams *ssp = arg;
180 tty_serial_init(fioc->fd,
181 ssp->speed, ssp->parity,
182 ssp->data_bits, ssp->stop_bits);
185 case CHR_IOCTL_SERIAL_SET_BREAK:
187 int enable = *(int *)arg;
189 tcsendbreak(fioc->fd, 1);
193 case CHR_IOCTL_SERIAL_GET_TIOCM:
196 int *targ = (int *)arg;
197 ioctl(fioc->fd, TIOCMGET, &sarg);
199 if (sarg & TIOCM_CTS) {
200 *targ |= CHR_TIOCM_CTS;
202 if (sarg & TIOCM_CAR) {
203 *targ |= CHR_TIOCM_CAR;
205 if (sarg & TIOCM_DSR) {
206 *targ |= CHR_TIOCM_DSR;
208 if (sarg & TIOCM_RI) {
209 *targ |= CHR_TIOCM_RI;
211 if (sarg & TIOCM_DTR) {
212 *targ |= CHR_TIOCM_DTR;
214 if (sarg & TIOCM_RTS) {
215 *targ |= CHR_TIOCM_RTS;
219 case CHR_IOCTL_SERIAL_SET_TIOCM:
221 int sarg = *(int *)arg;
223 ioctl(fioc->fd, TIOCMGET, &targ);
224 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
225 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
226 if (sarg & CHR_TIOCM_CTS) {
229 if (sarg & CHR_TIOCM_CAR) {
232 if (sarg & CHR_TIOCM_DSR) {
235 if (sarg & CHR_TIOCM_RI) {
238 if (sarg & CHR_TIOCM_DTR) {
241 if (sarg & CHR_TIOCM_RTS) {
244 ioctl(fioc->fd, TIOCMSET, &targ);
253 static void qmp_chardev_open_serial(Chardev *chr,
254 ChardevBackend *backend,
258 ChardevHostdev *serial = backend->u.serial.data;
261 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
265 qemu_set_nonblock(fd);
266 tty_serial_init(fd, 115200, 'N', 8, 1);
268 qemu_chr_open_fd(chr, fd, fd);
270 #endif /* __linux__ || __sun__ */
272 #ifdef HAVE_CHARDEV_SERIAL
273 static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend,
276 const char *device = qemu_opt_get(opts, "path");
277 ChardevHostdev *serial;
279 if (device == NULL) {
280 error_setg(errp, "chardev: serial/tty: no device path given");
283 backend->type = CHARDEV_BACKEND_KIND_SERIAL;
284 serial = backend->u.serial.data = g_new0(ChardevHostdev, 1);
285 qemu_chr_parse_common(opts, qapi_ChardevHostdev_base(serial));
286 serial->device = g_strdup(device);
289 static void char_serial_class_init(ObjectClass *oc, void *data)
291 ChardevClass *cc = CHARDEV_CLASS(oc);
293 cc->parse = qemu_chr_parse_serial;
294 cc->open = qmp_chardev_open_serial;
296 cc->chr_ioctl = tty_serial_ioctl;
301 static const TypeInfo char_serial_type_info = {
302 .name = TYPE_CHARDEV_SERIAL,
304 .parent = TYPE_CHARDEV_WIN,
306 .parent = TYPE_CHARDEV_FD,
308 .class_init = char_serial_class_init,
311 static void register_types(void)
313 type_register_static(&char_serial_type_info);
316 type_init(register_types);