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-common.h"
28 #include "qemu-timer.h"
29 #include "qemu-char.h"
43 #include <sys/times.h>
47 #include <sys/ioctl.h>
48 #include <sys/resource.h>
49 #include <sys/socket.h>
50 #include <netinet/in.h>
53 #include <net/if_tap.h>
56 #include <linux/if_tun.h>
58 #include <arpa/inet.h>
61 #include <sys/select.h>
66 #include <dev/ppbus/ppi.h>
67 #include <dev/ppbus/ppbconf.h>
71 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
72 #include <freebsd/stdlib.h>
77 #include <linux/ppdev.h>
78 #include <linux/parport.h>
82 #include <sys/ethernet.h>
83 #include <sys/sockio.h>
84 #include <netinet/arp.h>
85 #include <netinet/in.h>
86 #include <netinet/in_systm.h>
87 #include <netinet/ip.h>
88 #include <netinet/ip_icmp.h> // must come after ip.h
89 #include <netinet/udp.h>
90 #include <netinet/tcp.h>
98 #include "qemu_socket.h"
100 /***********************************************************/
101 /* character device */
103 static void qemu_chr_event(CharDriverState *s, int event)
107 s->chr_event(s->handler_opaque, event);
110 static void qemu_chr_reset_bh(void *opaque)
112 CharDriverState *s = opaque;
113 qemu_chr_event(s, CHR_EVENT_RESET);
114 qemu_bh_delete(s->bh);
118 void qemu_chr_reset(CharDriverState *s)
121 s->bh = qemu_bh_new(qemu_chr_reset_bh, s);
122 qemu_bh_schedule(s->bh);
126 int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len)
128 return s->chr_write(s, buf, len);
131 int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg)
135 return s->chr_ioctl(s, cmd, arg);
138 int qemu_chr_can_read(CharDriverState *s)
140 if (!s->chr_can_read)
142 return s->chr_can_read(s->handler_opaque);
145 void qemu_chr_read(CharDriverState *s, uint8_t *buf, int len)
147 s->chr_read(s->handler_opaque, buf, len);
150 void qemu_chr_accept_input(CharDriverState *s)
152 if (s->chr_accept_input)
153 s->chr_accept_input(s);
156 void qemu_chr_printf(CharDriverState *s, const char *fmt, ...)
161 vsnprintf(buf, sizeof(buf), fmt, ap);
162 qemu_chr_write(s, (uint8_t *)buf, strlen(buf));
166 void qemu_chr_send_event(CharDriverState *s, int event)
168 if (s->chr_send_event)
169 s->chr_send_event(s, event);
172 void qemu_chr_add_handlers(CharDriverState *s,
173 IOCanRWHandler *fd_can_read,
174 IOReadHandler *fd_read,
175 IOEventHandler *fd_event,
178 s->chr_can_read = fd_can_read;
179 s->chr_read = fd_read;
180 s->chr_event = fd_event;
181 s->handler_opaque = opaque;
182 if (s->chr_update_read_handler)
183 s->chr_update_read_handler(s);
186 static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
191 static CharDriverState *qemu_chr_open_null(void)
193 CharDriverState *chr;
195 chr = qemu_mallocz(sizeof(CharDriverState));
198 chr->chr_write = null_chr_write;
202 /* MUX driver for serial I/O splitting */
203 static int term_timestamps;
204 static int64_t term_timestamps_start;
206 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
207 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
209 IOCanRWHandler *chr_can_read[MAX_MUX];
210 IOReadHandler *chr_read[MAX_MUX];
211 IOEventHandler *chr_event[MAX_MUX];
212 void *ext_opaque[MAX_MUX];
213 CharDriverState *drv;
214 unsigned char buffer[MUX_BUFFER_SIZE];
223 static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
225 MuxDriver *d = chr->opaque;
227 if (!term_timestamps) {
228 ret = d->drv->chr_write(d->drv, buf, len);
233 for(i = 0; i < len; i++) {
234 ret += d->drv->chr_write(d->drv, buf+i, 1);
235 if (buf[i] == '\n') {
240 ti = qemu_get_clock(rt_clock);
241 if (term_timestamps_start == -1)
242 term_timestamps_start = ti;
243 ti -= term_timestamps_start;
244 secs = ti / 1000000000;
245 snprintf(buf1, sizeof(buf1),
246 "[%02d:%02d:%02d.%03d] ",
250 (int)((ti / 1000000) % 1000));
251 d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
258 static const char * const mux_help[] = {
259 "% h print this help\n\r",
260 "% x exit emulator\n\r",
261 "% s save disk data back to file (if -snapshot)\n\r",
262 "% t toggle console timestamps\n\r"
263 "% b send break (magic sysrq)\n\r",
264 "% c switch between console and monitor\n\r",
269 int term_escape_char = 0x01; /* ctrl-a is used for escape */
270 static void mux_print_help(CharDriverState *chr)
273 char ebuf[15] = "Escape-Char";
274 char cbuf[50] = "\n\r";
276 if (term_escape_char > 0 && term_escape_char < 26) {
277 snprintf(cbuf, sizeof(cbuf), "\n\r");
278 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
280 snprintf(cbuf, sizeof(cbuf),
281 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
284 chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
285 for (i = 0; mux_help[i] != NULL; i++) {
286 for (j=0; mux_help[i][j] != '\0'; j++) {
287 if (mux_help[i][j] == '%')
288 chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
290 chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
295 static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
297 if (d->term_got_escape) {
298 d->term_got_escape = 0;
299 if (ch == term_escape_char)
308 const char *term = "QEMU: Terminated\n\r";
309 chr->chr_write(chr,(uint8_t *)term,strlen(term));
316 for (i = 0; i < nb_drives; i++) {
317 bdrv_commit(drives_table[i].bdrv);
322 qemu_chr_event(chr, CHR_EVENT_BREAK);
325 /* Switch to the next registered device */
327 if (chr->focus >= d->mux_cnt)
331 term_timestamps = !term_timestamps;
332 term_timestamps_start = -1;
335 } else if (ch == term_escape_char) {
336 d->term_got_escape = 1;
344 static void mux_chr_accept_input(CharDriverState *chr)
347 MuxDriver *d = chr->opaque;
349 while (d->prod != d->cons &&
350 d->chr_can_read[m] &&
351 d->chr_can_read[m](d->ext_opaque[m])) {
352 d->chr_read[m](d->ext_opaque[m],
353 &d->buffer[d->cons++ & MUX_BUFFER_MASK], 1);
357 static int mux_chr_can_read(void *opaque)
359 CharDriverState *chr = opaque;
360 MuxDriver *d = chr->opaque;
362 if ((d->prod - d->cons) < MUX_BUFFER_SIZE)
364 if (d->chr_can_read[chr->focus])
365 return d->chr_can_read[chr->focus](d->ext_opaque[chr->focus]);
369 static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
371 CharDriverState *chr = opaque;
372 MuxDriver *d = chr->opaque;
376 mux_chr_accept_input (opaque);
378 for(i = 0; i < size; i++)
379 if (mux_proc_byte(chr, d, buf[i])) {
380 if (d->prod == d->cons &&
381 d->chr_can_read[m] &&
382 d->chr_can_read[m](d->ext_opaque[m]))
383 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
385 d->buffer[d->prod++ & MUX_BUFFER_MASK] = buf[i];
389 static void mux_chr_event(void *opaque, int event)
391 CharDriverState *chr = opaque;
392 MuxDriver *d = chr->opaque;
395 /* Send the event to all registered listeners */
396 for (i = 0; i < d->mux_cnt; i++)
398 d->chr_event[i](d->ext_opaque[i], event);
401 static void mux_chr_update_read_handler(CharDriverState *chr)
403 MuxDriver *d = chr->opaque;
405 if (d->mux_cnt >= MAX_MUX) {
406 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
409 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
410 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
411 d->chr_read[d->mux_cnt] = chr->chr_read;
412 d->chr_event[d->mux_cnt] = chr->chr_event;
413 /* Fix up the real driver with mux routines */
414 if (d->mux_cnt == 0) {
415 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
418 chr->focus = d->mux_cnt;
422 static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
424 CharDriverState *chr;
427 chr = qemu_mallocz(sizeof(CharDriverState));
430 d = qemu_mallocz(sizeof(MuxDriver));
439 chr->chr_write = mux_chr_write;
440 chr->chr_update_read_handler = mux_chr_update_read_handler;
441 chr->chr_accept_input = mux_chr_accept_input;
447 int send_all(int fd, const void *buf, int len1)
453 ret = send(fd, buf, len, 0);
455 errno = WSAGetLastError();
456 if (errno != WSAEWOULDBLOCK) {
459 } else if (ret == 0) {
471 static int unix_write(int fd, const uint8_t *buf, int len1)
477 ret = write(fd, buf, len);
479 if (errno != EINTR && errno != EAGAIN)
481 } else if (ret == 0) {
491 int send_all(int fd, const void *buf, int len1)
493 return unix_write(fd, buf, len1);
504 #define STDIO_MAX_CLIENTS 1
505 static int stdio_nb_clients = 0;
507 static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
509 FDCharDriver *s = chr->opaque;
510 return send_all(s->fd_out, buf, len);
513 static int fd_chr_read_poll(void *opaque)
515 CharDriverState *chr = opaque;
516 FDCharDriver *s = chr->opaque;
518 s->max_size = qemu_chr_can_read(chr);
522 static void fd_chr_read(void *opaque)
524 CharDriverState *chr = opaque;
525 FDCharDriver *s = chr->opaque;
530 if (len > s->max_size)
534 size = read(s->fd_in, buf, len);
536 /* FD has been closed. Remove it from the active list. */
537 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
541 qemu_chr_read(chr, buf, size);
545 static void fd_chr_update_read_handler(CharDriverState *chr)
547 FDCharDriver *s = chr->opaque;
550 if (nographic && s->fd_in == 0) {
552 qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll,
553 fd_chr_read, NULL, chr);
558 static void fd_chr_close(struct CharDriverState *chr)
560 FDCharDriver *s = chr->opaque;
563 if (nographic && s->fd_in == 0) {
565 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
572 /* open a character device to a unix fd */
573 static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
575 CharDriverState *chr;
578 chr = qemu_mallocz(sizeof(CharDriverState));
581 s = qemu_mallocz(sizeof(FDCharDriver));
589 chr->chr_write = fd_chr_write;
590 chr->chr_update_read_handler = fd_chr_update_read_handler;
591 chr->chr_close = fd_chr_close;
598 static CharDriverState *qemu_chr_open_file_out(const char *file_out)
602 TFR(fd_out = open(file_out, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
605 return qemu_chr_open_fd(-1, fd_out);
608 static CharDriverState *qemu_chr_open_pipe(const char *filename)
611 char filename_in[256], filename_out[256];
613 snprintf(filename_in, 256, "%s.in", filename);
614 snprintf(filename_out, 256, "%s.out", filename);
615 TFR(fd_in = open(filename_in, O_RDWR | O_BINARY));
616 TFR(fd_out = open(filename_out, O_RDWR | O_BINARY));
617 if (fd_in < 0 || fd_out < 0) {
622 TFR(fd_in = fd_out = open(filename, O_RDWR | O_BINARY));
626 return qemu_chr_open_fd(fd_in, fd_out);
630 /* for STDIO, we handle the case where several clients use it
633 #define TERM_FIFO_MAX_SIZE 1
635 static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
636 static int term_fifo_size;
638 static int stdio_read_poll(void *opaque)
640 CharDriverState *chr = opaque;
642 /* try to flush the queue if needed */
643 if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) {
644 qemu_chr_read(chr, term_fifo, 1);
647 /* see if we can absorb more chars */
648 if (term_fifo_size == 0)
654 static void stdio_read(void *opaque)
658 CharDriverState *chr = opaque;
660 size = read(0, buf, 1);
662 /* stdin has been closed. Remove it from the active list. */
663 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
667 if (qemu_chr_can_read(chr) > 0) {
668 qemu_chr_read(chr, buf, 1);
669 } else if (term_fifo_size == 0) {
670 term_fifo[term_fifo_size++] = buf[0];
675 /* init terminal so that we can grab keys */
676 static struct termios oldtty;
677 static int old_fd0_flags;
678 static int term_atexit_done;
680 static void term_exit(void)
682 tcsetattr (0, TCSANOW, &oldtty);
683 fcntl(0, F_SETFL, old_fd0_flags);
686 static void term_init(void)
692 old_fd0_flags = fcntl(0, F_GETFL);
694 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
695 |INLCR|IGNCR|ICRNL|IXON);
696 tty.c_oflag |= OPOST;
697 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
698 /* if graphical mode, we allow Ctrl-C handling */
700 tty.c_lflag &= ~ISIG;
701 tty.c_cflag &= ~(CSIZE|PARENB);
706 tcsetattr (0, TCSANOW, &tty);
708 if (!term_atexit_done++)
711 fcntl(0, F_SETFL, O_NONBLOCK);
714 static void qemu_chr_close_stdio(struct CharDriverState *chr)
718 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
722 static CharDriverState *qemu_chr_open_stdio(void)
724 CharDriverState *chr;
726 if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
728 chr = qemu_chr_open_fd(0, 1);
729 chr->chr_close = qemu_chr_close_stdio;
730 qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr);
738 /* Once Solaris has openpty(), this is going to be removed. */
739 int openpty(int *amaster, int *aslave, char *name,
740 struct termios *termp, struct winsize *winp)
743 int mfd = -1, sfd = -1;
745 *amaster = *aslave = -1;
747 mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
751 if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
754 if ((slave = ptsname(mfd)) == NULL)
757 if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
760 if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
761 (termp != NULL && tcgetattr(sfd, termp) < 0))
769 ioctl(sfd, TIOCSWINSZ, winp);
780 void cfmakeraw (struct termios *termios_p)
782 termios_p->c_iflag &=
783 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
784 termios_p->c_oflag &= ~OPOST;
785 termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
786 termios_p->c_cflag &= ~(CSIZE|PARENB);
787 termios_p->c_cflag |= CS8;
789 termios_p->c_cc[VMIN] = 0;
790 termios_p->c_cc[VTIME] = 0;
794 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
795 || defined(__NetBSD__) || defined(__OpenBSD__)
805 static void pty_chr_update_read_handler(CharDriverState *chr);
806 static void pty_chr_state(CharDriverState *chr, int connected);
808 static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
810 PtyCharDriver *s = chr->opaque;
813 /* guest sends data, check for (re-)connect */
814 pty_chr_update_read_handler(chr);
817 return send_all(s->fd, buf, len);
820 static int pty_chr_read_poll(void *opaque)
822 CharDriverState *chr = opaque;
823 PtyCharDriver *s = chr->opaque;
825 s->read_bytes = qemu_chr_can_read(chr);
826 return s->read_bytes;
829 static void pty_chr_read(void *opaque)
831 CharDriverState *chr = opaque;
832 PtyCharDriver *s = chr->opaque;
837 if (len > s->read_bytes)
841 size = read(s->fd, buf, len);
842 if ((size == -1 && errno == EIO) ||
844 pty_chr_state(chr, 0);
848 pty_chr_state(chr, 1);
849 qemu_chr_read(chr, buf, size);
853 static void pty_chr_update_read_handler(CharDriverState *chr)
855 PtyCharDriver *s = chr->opaque;
857 qemu_set_fd_handler2(s->fd, pty_chr_read_poll,
858 pty_chr_read, NULL, chr);
861 * Short timeout here: just need wait long enougth that qemu makes
862 * it through the poll loop once. When reconnected we want a
863 * short timeout so we notice it almost instantly. Otherwise
864 * read() gives us -EIO instantly, making pty_chr_state() reset the
865 * timeout to the normal (much longer) poll interval before the
868 qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 10);
871 static void pty_chr_state(CharDriverState *chr, int connected)
873 PtyCharDriver *s = chr->opaque;
876 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
879 /* (re-)connect poll interval for idle guests: once per second.
880 * We check more frequently in case the guests sends data to
881 * the virtual device linked to our pty. */
882 qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 1000);
890 static void pty_chr_timer(void *opaque)
892 struct CharDriverState *chr = opaque;
893 PtyCharDriver *s = chr->opaque;
898 /* If we arrive here without polling being cleared due
899 * read returning -EIO, then we are (re-)connected */
900 pty_chr_state(chr, 1);
905 pty_chr_update_read_handler(chr);
908 static void pty_chr_close(struct CharDriverState *chr)
910 PtyCharDriver *s = chr->opaque;
912 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
917 static CharDriverState *qemu_chr_open_pty(void)
919 CharDriverState *chr;
923 #if defined(__OpenBSD__)
924 char pty_name[PATH_MAX];
925 #define q_ptsname(x) pty_name
927 char *pty_name = NULL;
928 #define q_ptsname(x) ptsname(x)
931 chr = qemu_mallocz(sizeof(CharDriverState));
934 s = qemu_mallocz(sizeof(PtyCharDriver));
940 if (openpty(&s->fd, &slave_fd, pty_name, NULL, NULL) < 0) {
944 /* Set raw attributes on the pty. */
945 tcgetattr(slave_fd, &tty);
947 tcsetattr(slave_fd, TCSAFLUSH, &tty);
950 len = strlen(q_ptsname(s->fd)) + 5;
951 chr->filename = qemu_malloc(len);
952 snprintf(chr->filename, len, "pty:%s", q_ptsname(s->fd));
953 fprintf(stderr, "char device redirected to %s\n", q_ptsname(s->fd));
956 chr->chr_write = pty_chr_write;
957 chr->chr_update_read_handler = pty_chr_update_read_handler;
958 chr->chr_close = pty_chr_close;
960 s->timer = qemu_new_timer(rt_clock, pty_chr_timer, chr);
965 static void tty_serial_init(int fd, int speed,
966 int parity, int data_bits, int stop_bits)
972 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
973 speed, parity, data_bits, stop_bits);
975 tcgetattr (fd, &tty);
978 if (speed <= 50 * MARGIN)
980 else if (speed <= 75 * MARGIN)
982 else if (speed <= 300 * MARGIN)
984 else if (speed <= 600 * MARGIN)
986 else if (speed <= 1200 * MARGIN)
988 else if (speed <= 2400 * MARGIN)
990 else if (speed <= 4800 * MARGIN)
992 else if (speed <= 9600 * MARGIN)
994 else if (speed <= 19200 * MARGIN)
996 else if (speed <= 38400 * MARGIN)
998 else if (speed <= 57600 * MARGIN)
1000 else if (speed <= 115200 * MARGIN)
1005 cfsetispeed(&tty, spd);
1006 cfsetospeed(&tty, spd);
1008 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1009 |INLCR|IGNCR|ICRNL|IXON);
1010 tty.c_oflag |= OPOST;
1011 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1012 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1033 tty.c_cflag |= PARENB;
1036 tty.c_cflag |= PARENB | PARODD;
1040 tty.c_cflag |= CSTOPB;
1042 tcsetattr (fd, TCSANOW, &tty);
1045 static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1047 FDCharDriver *s = chr->opaque;
1050 case CHR_IOCTL_SERIAL_SET_PARAMS:
1052 QEMUSerialSetParams *ssp = arg;
1053 tty_serial_init(s->fd_in, ssp->speed, ssp->parity,
1054 ssp->data_bits, ssp->stop_bits);
1057 case CHR_IOCTL_SERIAL_SET_BREAK:
1059 int enable = *(int *)arg;
1061 tcsendbreak(s->fd_in, 1);
1064 case CHR_IOCTL_SERIAL_GET_TIOCM:
1067 int *targ = (int *)arg;
1068 ioctl(s->fd_in, TIOCMGET, &sarg);
1070 if (sarg | TIOCM_CTS)
1071 *targ |= CHR_TIOCM_CTS;
1072 if (sarg | TIOCM_CAR)
1073 *targ |= CHR_TIOCM_CAR;
1074 if (sarg | TIOCM_DSR)
1075 *targ |= CHR_TIOCM_DSR;
1076 if (sarg | TIOCM_RI)
1077 *targ |= CHR_TIOCM_RI;
1078 if (sarg | TIOCM_DTR)
1079 *targ |= CHR_TIOCM_DTR;
1080 if (sarg | TIOCM_RTS)
1081 *targ |= CHR_TIOCM_RTS;
1084 case CHR_IOCTL_SERIAL_SET_TIOCM:
1086 int sarg = *(int *)arg;
1088 if (sarg | CHR_TIOCM_DTR)
1090 if (sarg | CHR_TIOCM_RTS)
1092 ioctl(s->fd_in, TIOCMSET, &targ);
1101 static CharDriverState *qemu_chr_open_tty(const char *filename)
1103 CharDriverState *chr;
1106 TFR(fd = open(filename, O_RDWR | O_NONBLOCK));
1107 tty_serial_init(fd, 115200, 'N', 8, 1);
1108 chr = qemu_chr_open_fd(fd, fd);
1113 chr->chr_ioctl = tty_serial_ioctl;
1114 qemu_chr_reset(chr);
1117 #else /* ! __linux__ && ! __sun__ */
1118 static CharDriverState *qemu_chr_open_pty(void)
1122 #endif /* __linux__ || __sun__ */
1124 #if defined(__linux__)
1128 } ParallelCharDriver;
1130 static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1132 if (s->mode != mode) {
1134 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1141 static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1143 ParallelCharDriver *drv = chr->opaque;
1148 case CHR_IOCTL_PP_READ_DATA:
1149 if (ioctl(fd, PPRDATA, &b) < 0)
1151 *(uint8_t *)arg = b;
1153 case CHR_IOCTL_PP_WRITE_DATA:
1154 b = *(uint8_t *)arg;
1155 if (ioctl(fd, PPWDATA, &b) < 0)
1158 case CHR_IOCTL_PP_READ_CONTROL:
1159 if (ioctl(fd, PPRCONTROL, &b) < 0)
1161 /* Linux gives only the lowest bits, and no way to know data
1162 direction! For better compatibility set the fixed upper
1164 *(uint8_t *)arg = b | 0xc0;
1166 case CHR_IOCTL_PP_WRITE_CONTROL:
1167 b = *(uint8_t *)arg;
1168 if (ioctl(fd, PPWCONTROL, &b) < 0)
1171 case CHR_IOCTL_PP_READ_STATUS:
1172 if (ioctl(fd, PPRSTATUS, &b) < 0)
1174 *(uint8_t *)arg = b;
1176 case CHR_IOCTL_PP_DATA_DIR:
1177 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1180 case CHR_IOCTL_PP_EPP_READ_ADDR:
1181 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1182 struct ParallelIOArg *parg = arg;
1183 int n = read(fd, parg->buffer, parg->count);
1184 if (n != parg->count) {
1189 case CHR_IOCTL_PP_EPP_READ:
1190 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1191 struct ParallelIOArg *parg = arg;
1192 int n = read(fd, parg->buffer, parg->count);
1193 if (n != parg->count) {
1198 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1199 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1200 struct ParallelIOArg *parg = arg;
1201 int n = write(fd, parg->buffer, parg->count);
1202 if (n != parg->count) {
1207 case CHR_IOCTL_PP_EPP_WRITE:
1208 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1209 struct ParallelIOArg *parg = arg;
1210 int n = write(fd, parg->buffer, parg->count);
1211 if (n != parg->count) {
1222 static void pp_close(CharDriverState *chr)
1224 ParallelCharDriver *drv = chr->opaque;
1227 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1228 ioctl(fd, PPRELEASE);
1233 static CharDriverState *qemu_chr_open_pp(const char *filename)
1235 CharDriverState *chr;
1236 ParallelCharDriver *drv;
1239 TFR(fd = open(filename, O_RDWR));
1243 if (ioctl(fd, PPCLAIM) < 0) {
1248 drv = qemu_mallocz(sizeof(ParallelCharDriver));
1254 drv->mode = IEEE1284_MODE_COMPAT;
1256 chr = qemu_mallocz(sizeof(CharDriverState));
1262 chr->chr_write = null_chr_write;
1263 chr->chr_ioctl = pp_ioctl;
1264 chr->chr_close = pp_close;
1267 qemu_chr_reset(chr);
1271 #endif /* __linux__ */
1273 #if defined(__FreeBSD__)
1274 static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1276 int fd = (int)chr->opaque;
1280 case CHR_IOCTL_PP_READ_DATA:
1281 if (ioctl(fd, PPIGDATA, &b) < 0)
1283 *(uint8_t *)arg = b;
1285 case CHR_IOCTL_PP_WRITE_DATA:
1286 b = *(uint8_t *)arg;
1287 if (ioctl(fd, PPISDATA, &b) < 0)
1290 case CHR_IOCTL_PP_READ_CONTROL:
1291 if (ioctl(fd, PPIGCTRL, &b) < 0)
1293 *(uint8_t *)arg = b;
1295 case CHR_IOCTL_PP_WRITE_CONTROL:
1296 b = *(uint8_t *)arg;
1297 if (ioctl(fd, PPISCTRL, &b) < 0)
1300 case CHR_IOCTL_PP_READ_STATUS:
1301 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1303 *(uint8_t *)arg = b;
1311 static CharDriverState *qemu_chr_open_pp(const char *filename)
1313 CharDriverState *chr;
1316 fd = open(filename, O_RDWR);
1320 chr = qemu_mallocz(sizeof(CharDriverState));
1325 chr->opaque = (void *)fd;
1326 chr->chr_write = null_chr_write;
1327 chr->chr_ioctl = pp_ioctl;
1336 HANDLE hcom, hrecv, hsend;
1337 OVERLAPPED orecv, osend;
1342 #define NSENDBUF 2048
1343 #define NRECVBUF 2048
1344 #define MAXCONNECT 1
1345 #define NTIMEOUT 5000
1347 static int win_chr_poll(void *opaque);
1348 static int win_chr_pipe_poll(void *opaque);
1350 static void win_chr_close(CharDriverState *chr)
1352 WinCharState *s = chr->opaque;
1355 CloseHandle(s->hsend);
1359 CloseHandle(s->hrecv);
1363 CloseHandle(s->hcom);
1367 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1369 qemu_del_polling_cb(win_chr_poll, chr);
1372 static int win_chr_init(CharDriverState *chr, const char *filename)
1374 WinCharState *s = chr->opaque;
1376 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1381 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1383 fprintf(stderr, "Failed CreateEvent\n");
1386 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1388 fprintf(stderr, "Failed CreateEvent\n");
1392 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1393 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1394 if (s->hcom == INVALID_HANDLE_VALUE) {
1395 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1400 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1401 fprintf(stderr, "Failed SetupComm\n");
1405 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1406 size = sizeof(COMMCONFIG);
1407 GetDefaultCommConfig(filename, &comcfg, &size);
1408 comcfg.dcb.DCBlength = sizeof(DCB);
1409 CommConfigDialog(filename, NULL, &comcfg);
1411 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1412 fprintf(stderr, "Failed SetCommState\n");
1416 if (!SetCommMask(s->hcom, EV_ERR)) {
1417 fprintf(stderr, "Failed SetCommMask\n");
1421 cto.ReadIntervalTimeout = MAXDWORD;
1422 if (!SetCommTimeouts(s->hcom, &cto)) {
1423 fprintf(stderr, "Failed SetCommTimeouts\n");
1427 if (!ClearCommError(s->hcom, &err, &comstat)) {
1428 fprintf(stderr, "Failed ClearCommError\n");
1431 qemu_add_polling_cb(win_chr_poll, chr);
1439 static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1441 WinCharState *s = chr->opaque;
1442 DWORD len, ret, size, err;
1445 ZeroMemory(&s->osend, sizeof(s->osend));
1446 s->osend.hEvent = s->hsend;
1449 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1451 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1453 err = GetLastError();
1454 if (err == ERROR_IO_PENDING) {
1455 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1473 static int win_chr_read_poll(CharDriverState *chr)
1475 WinCharState *s = chr->opaque;
1477 s->max_size = qemu_chr_can_read(chr);
1481 static void win_chr_readfile(CharDriverState *chr)
1483 WinCharState *s = chr->opaque;
1488 ZeroMemory(&s->orecv, sizeof(s->orecv));
1489 s->orecv.hEvent = s->hrecv;
1490 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1492 err = GetLastError();
1493 if (err == ERROR_IO_PENDING) {
1494 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1499 qemu_chr_read(chr, buf, size);
1503 static void win_chr_read(CharDriverState *chr)
1505 WinCharState *s = chr->opaque;
1507 if (s->len > s->max_size)
1508 s->len = s->max_size;
1512 win_chr_readfile(chr);
1515 static int win_chr_poll(void *opaque)
1517 CharDriverState *chr = opaque;
1518 WinCharState *s = chr->opaque;
1522 ClearCommError(s->hcom, &comerr, &status);
1523 if (status.cbInQue > 0) {
1524 s->len = status.cbInQue;
1525 win_chr_read_poll(chr);
1532 static CharDriverState *qemu_chr_open_win(const char *filename)
1534 CharDriverState *chr;
1537 chr = qemu_mallocz(sizeof(CharDriverState));
1540 s = qemu_mallocz(sizeof(WinCharState));
1546 chr->chr_write = win_chr_write;
1547 chr->chr_close = win_chr_close;
1549 if (win_chr_init(chr, filename) < 0) {
1554 qemu_chr_reset(chr);
1558 static int win_chr_pipe_poll(void *opaque)
1560 CharDriverState *chr = opaque;
1561 WinCharState *s = chr->opaque;
1564 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1567 win_chr_read_poll(chr);
1574 static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1576 WinCharState *s = chr->opaque;
1584 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1586 fprintf(stderr, "Failed CreateEvent\n");
1589 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1591 fprintf(stderr, "Failed CreateEvent\n");
1595 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1596 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1597 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1599 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1600 if (s->hcom == INVALID_HANDLE_VALUE) {
1601 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1606 ZeroMemory(&ov, sizeof(ov));
1607 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1608 ret = ConnectNamedPipe(s->hcom, &ov);
1610 fprintf(stderr, "Failed ConnectNamedPipe\n");
1614 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1616 fprintf(stderr, "Failed GetOverlappedResult\n");
1618 CloseHandle(ov.hEvent);
1625 CloseHandle(ov.hEvent);
1628 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1637 static CharDriverState *qemu_chr_open_win_pipe(const char *filename)
1639 CharDriverState *chr;
1642 chr = qemu_mallocz(sizeof(CharDriverState));
1645 s = qemu_mallocz(sizeof(WinCharState));
1651 chr->chr_write = win_chr_write;
1652 chr->chr_close = win_chr_close;
1654 if (win_chr_pipe_init(chr, filename) < 0) {
1659 qemu_chr_reset(chr);
1663 static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
1665 CharDriverState *chr;
1668 chr = qemu_mallocz(sizeof(CharDriverState));
1671 s = qemu_mallocz(sizeof(WinCharState));
1678 chr->chr_write = win_chr_write;
1679 qemu_chr_reset(chr);
1683 static CharDriverState *qemu_chr_open_win_con(const char *filename)
1685 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
1688 static CharDriverState *qemu_chr_open_win_file_out(const char *file_out)
1692 fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
1693 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1694 if (fd_out == INVALID_HANDLE_VALUE)
1697 return qemu_chr_open_win_file(fd_out);
1699 #endif /* !_WIN32 */
1701 /***********************************************************/
1702 /* UDP Net console */
1706 struct sockaddr_in daddr;
1713 static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1715 NetCharDriver *s = chr->opaque;
1717 return sendto(s->fd, buf, len, 0,
1718 (struct sockaddr *)&s->daddr, sizeof(struct sockaddr_in));
1721 static int udp_chr_read_poll(void *opaque)
1723 CharDriverState *chr = opaque;
1724 NetCharDriver *s = chr->opaque;
1726 s->max_size = qemu_chr_can_read(chr);
1728 /* If there were any stray characters in the queue process them
1731 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1732 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
1734 s->max_size = qemu_chr_can_read(chr);
1739 static void udp_chr_read(void *opaque)
1741 CharDriverState *chr = opaque;
1742 NetCharDriver *s = chr->opaque;
1744 if (s->max_size == 0)
1746 s->bufcnt = recv(s->fd, s->buf, sizeof(s->buf), 0);
1747 s->bufptr = s->bufcnt;
1752 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1753 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
1755 s->max_size = qemu_chr_can_read(chr);
1759 static void udp_chr_update_read_handler(CharDriverState *chr)
1761 NetCharDriver *s = chr->opaque;
1764 qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
1765 udp_chr_read, NULL, chr);
1769 static CharDriverState *qemu_chr_open_udp(const char *def)
1771 CharDriverState *chr = NULL;
1772 NetCharDriver *s = NULL;
1774 struct sockaddr_in saddr;
1776 chr = qemu_mallocz(sizeof(CharDriverState));
1779 s = qemu_mallocz(sizeof(NetCharDriver));
1783 fd = socket(PF_INET, SOCK_DGRAM, 0);
1785 perror("socket(PF_INET, SOCK_DGRAM)");
1789 if (parse_host_src_port(&s->daddr, &saddr, def) < 0) {
1790 printf("Could not parse: %s\n", def);
1794 if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
1804 chr->chr_write = udp_chr_write;
1805 chr->chr_update_read_handler = udp_chr_update_read_handler;
1818 /***********************************************************/
1819 /* TCP Net console */
1830 static void tcp_chr_accept(void *opaque);
1832 static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1834 TCPCharDriver *s = chr->opaque;
1836 return send_all(s->fd, buf, len);
1838 /* XXX: indicate an error ? */
1843 static int tcp_chr_read_poll(void *opaque)
1845 CharDriverState *chr = opaque;
1846 TCPCharDriver *s = chr->opaque;
1849 s->max_size = qemu_chr_can_read(chr);
1854 #define IAC_BREAK 243
1855 static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
1857 uint8_t *buf, int *size)
1859 /* Handle any telnet client's basic IAC options to satisfy char by
1860 * char mode with no echo. All IAC options will be removed from
1861 * the buf and the do_telnetopt variable will be used to track the
1862 * state of the width of the IAC information.
1864 * IAC commands come in sets of 3 bytes with the exception of the
1865 * "IAC BREAK" command and the double IAC.
1871 for (i = 0; i < *size; i++) {
1872 if (s->do_telnetopt > 1) {
1873 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
1874 /* Double IAC means send an IAC */
1878 s->do_telnetopt = 1;
1880 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
1881 /* Handle IAC break commands by sending a serial break */
1882 qemu_chr_event(chr, CHR_EVENT_BREAK);
1887 if (s->do_telnetopt >= 4) {
1888 s->do_telnetopt = 1;
1891 if ((unsigned char)buf[i] == IAC) {
1892 s->do_telnetopt = 2;
1903 static void tcp_chr_read(void *opaque)
1905 CharDriverState *chr = opaque;
1906 TCPCharDriver *s = chr->opaque;
1910 if (!s->connected || s->max_size <= 0)
1913 if (len > s->max_size)
1915 size = recv(s->fd, buf, len, 0);
1917 /* connection closed */
1919 if (s->listen_fd >= 0) {
1920 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
1922 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1925 } else if (size > 0) {
1926 if (s->do_telnetopt)
1927 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
1929 qemu_chr_read(chr, buf, size);
1933 static void tcp_chr_connect(void *opaque)
1935 CharDriverState *chr = opaque;
1936 TCPCharDriver *s = chr->opaque;
1939 qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
1940 tcp_chr_read, NULL, chr);
1941 qemu_chr_reset(chr);
1944 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
1945 static void tcp_chr_telnet_init(int fd)
1948 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
1949 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
1950 send(fd, (char *)buf, 3, 0);
1951 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
1952 send(fd, (char *)buf, 3, 0);
1953 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
1954 send(fd, (char *)buf, 3, 0);
1955 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
1956 send(fd, (char *)buf, 3, 0);
1959 static void socket_set_nodelay(int fd)
1962 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
1965 static void tcp_chr_accept(void *opaque)
1967 CharDriverState *chr = opaque;
1968 TCPCharDriver *s = chr->opaque;
1969 struct sockaddr_in saddr;
1971 struct sockaddr_un uaddr;
1973 struct sockaddr *addr;
1980 len = sizeof(uaddr);
1981 addr = (struct sockaddr *)&uaddr;
1985 len = sizeof(saddr);
1986 addr = (struct sockaddr *)&saddr;
1988 fd = accept(s->listen_fd, addr, &len);
1989 if (fd < 0 && errno != EINTR) {
1991 } else if (fd >= 0) {
1992 if (s->do_telnetopt)
1993 tcp_chr_telnet_init(fd);
1997 socket_set_nonblock(fd);
1999 socket_set_nodelay(fd);
2001 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
2002 tcp_chr_connect(chr);
2005 static void tcp_chr_close(CharDriverState *chr)
2007 TCPCharDriver *s = chr->opaque;
2010 if (s->listen_fd >= 0)
2011 closesocket(s->listen_fd);
2015 static CharDriverState *qemu_chr_open_tcp(const char *host_str,
2019 CharDriverState *chr = NULL;
2020 TCPCharDriver *s = NULL;
2021 int fd = -1, offset = 0;
2023 int is_waitconnect = 1;
2028 while((ptr = strchr(ptr,','))) {
2030 if (!strncmp(ptr,"server",6)) {
2032 } else if (!strncmp(ptr,"nowait",6)) {
2034 } else if (!strncmp(ptr,"nodelay",6)) {
2036 } else if (!strncmp(ptr,"to=",3)) {
2037 /* nothing, inet_listen() parses this one */;
2039 printf("Unknown option: %s\n", ptr);
2046 chr = qemu_mallocz(sizeof(CharDriverState));
2049 s = qemu_mallocz(sizeof(TCPCharDriver));
2054 chr->filename = qemu_malloc(256);
2056 pstrcpy(chr->filename, 256, "unix:");
2057 } else if (is_telnet) {
2058 pstrcpy(chr->filename, 256, "telnet:");
2060 pstrcpy(chr->filename, 256, "tcp:");
2062 offset = strlen(chr->filename);
2066 fd = unix_listen(host_str, chr->filename + offset, 256 - offset);
2068 fd = unix_connect(host_str);
2072 fd = inet_listen(host_str, chr->filename + offset, 256 - offset,
2075 fd = inet_connect(host_str, SOCK_STREAM);
2081 if (!is_waitconnect)
2082 socket_set_nonblock(fd);
2087 s->is_unix = is_unix;
2088 s->do_nodelay = do_nodelay && !is_unix;
2091 chr->chr_write = tcp_chr_write;
2092 chr->chr_close = tcp_chr_close;
2096 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
2098 s->do_telnetopt = 1;
2102 socket_set_nodelay(fd);
2103 tcp_chr_connect(chr);
2106 if (is_listen && is_waitconnect) {
2107 printf("QEMU waiting for connection on: %s\n",
2108 chr->filename ? chr->filename : host_str);
2109 tcp_chr_accept(chr);
2110 socket_set_nonblock(s->listen_fd);
2122 static TAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs
2123 = TAILQ_HEAD_INITIALIZER(chardevs);
2125 CharDriverState *qemu_chr_open(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
2128 CharDriverState *chr;
2130 if (!strcmp(filename, "vc")) {
2131 chr = text_console_init(0);
2133 if (strstart(filename, "vc:", &p)) {
2134 chr = text_console_init(p);
2136 if (!strcmp(filename, "null")) {
2137 chr = qemu_chr_open_null();
2139 if (strstart(filename, "tcp:", &p)) {
2140 chr = qemu_chr_open_tcp(p, 0, 0);
2142 if (strstart(filename, "telnet:", &p)) {
2143 chr = qemu_chr_open_tcp(p, 1, 0);
2145 if (strstart(filename, "udp:", &p)) {
2146 chr = qemu_chr_open_udp(p);
2148 if (strstart(filename, "mon:", &p)) {
2149 chr = qemu_chr_open(label, p, NULL);
2151 chr = qemu_chr_open_mux(chr);
2152 monitor_init(chr, !nographic);
2154 printf("Unable to open driver: %s\n", p);
2158 if (strstart(filename, "unix:", &p)) {
2159 chr = qemu_chr_open_tcp(p, 0, 1);
2160 } else if (strstart(filename, "file:", &p)) {
2161 chr = qemu_chr_open_file_out(p);
2162 } else if (strstart(filename, "pipe:", &p)) {
2163 chr = qemu_chr_open_pipe(p);
2164 } else if (!strcmp(filename, "pty")) {
2165 chr = qemu_chr_open_pty();
2166 } else if (!strcmp(filename, "stdio")) {
2167 chr = qemu_chr_open_stdio();
2169 #if defined(__linux__)
2170 if (strstart(filename, "/dev/parport", NULL)) {
2171 chr = qemu_chr_open_pp(filename);
2173 #elif defined(__FreeBSD__)
2174 if (strstart(filename, "/dev/ppi", NULL)) {
2175 chr = qemu_chr_open_pp(filename);
2178 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2179 || defined(__NetBSD__) || defined(__OpenBSD__)
2180 if (strstart(filename, "/dev/", NULL)) {
2181 chr = qemu_chr_open_tty(filename);
2185 if (strstart(filename, "COM", NULL)) {
2186 chr = qemu_chr_open_win(filename);
2188 if (strstart(filename, "pipe:", &p)) {
2189 chr = qemu_chr_open_win_pipe(p);
2191 if (strstart(filename, "con:", NULL)) {
2192 chr = qemu_chr_open_win_con(filename);
2194 if (strstart(filename, "file:", &p)) {
2195 chr = qemu_chr_open_win_file_out(p);
2198 #ifdef CONFIG_BRLAPI
2199 if (!strcmp(filename, "braille")) {
2200 chr = chr_baum_init();
2209 chr->filename = qemu_strdup(filename);
2211 chr->label = qemu_strdup(label);
2212 TAILQ_INSERT_TAIL(&chardevs, chr, next);
2217 void qemu_chr_close(CharDriverState *chr)
2219 TAILQ_REMOVE(&chardevs, chr, next);
2221 chr->chr_close(chr);
2222 qemu_free(chr->filename);
2223 qemu_free(chr->label);
2227 void qemu_chr_info(void)
2229 CharDriverState *chr;
2231 TAILQ_FOREACH(chr, &chardevs, next) {
2232 term_printf("%s: filename=%s\n", chr->label, chr->filename);