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));
196 chr->chr_write = null_chr_write;
200 /* MUX driver for serial I/O splitting */
201 static int term_timestamps;
202 static int64_t term_timestamps_start;
204 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
205 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
207 IOCanRWHandler *chr_can_read[MAX_MUX];
208 IOReadHandler *chr_read[MAX_MUX];
209 IOEventHandler *chr_event[MAX_MUX];
210 void *ext_opaque[MAX_MUX];
211 CharDriverState *drv;
212 unsigned char buffer[MUX_BUFFER_SIZE];
221 static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
223 MuxDriver *d = chr->opaque;
225 if (!term_timestamps) {
226 ret = d->drv->chr_write(d->drv, buf, len);
231 for(i = 0; i < len; i++) {
232 ret += d->drv->chr_write(d->drv, buf+i, 1);
233 if (buf[i] == '\n') {
238 ti = qemu_get_clock(rt_clock);
239 if (term_timestamps_start == -1)
240 term_timestamps_start = ti;
241 ti -= term_timestamps_start;
243 snprintf(buf1, sizeof(buf1),
244 "[%02d:%02d:%02d.%03d] ",
249 d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
256 static const char * const mux_help[] = {
257 "% h print this help\n\r",
258 "% x exit emulator\n\r",
259 "% s save disk data back to file (if -snapshot)\n\r",
260 "% t toggle console timestamps\n\r"
261 "% b send break (magic sysrq)\n\r",
262 "% c switch between console and monitor\n\r",
267 int term_escape_char = 0x01; /* ctrl-a is used for escape */
268 static void mux_print_help(CharDriverState *chr)
271 char ebuf[15] = "Escape-Char";
272 char cbuf[50] = "\n\r";
274 if (term_escape_char > 0 && term_escape_char < 26) {
275 snprintf(cbuf, sizeof(cbuf), "\n\r");
276 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
278 snprintf(cbuf, sizeof(cbuf),
279 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
282 chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
283 for (i = 0; mux_help[i] != NULL; i++) {
284 for (j=0; mux_help[i][j] != '\0'; j++) {
285 if (mux_help[i][j] == '%')
286 chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
288 chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
293 static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
295 if (d->term_got_escape) {
296 d->term_got_escape = 0;
297 if (ch == term_escape_char)
306 const char *term = "QEMU: Terminated\n\r";
307 chr->chr_write(chr,(uint8_t *)term,strlen(term));
314 for (i = 0; i < nb_drives; i++) {
315 bdrv_commit(drives_table[i].bdrv);
320 qemu_chr_event(chr, CHR_EVENT_BREAK);
323 /* Switch to the next registered device */
325 if (chr->focus >= d->mux_cnt)
329 term_timestamps = !term_timestamps;
330 term_timestamps_start = -1;
333 } else if (ch == term_escape_char) {
334 d->term_got_escape = 1;
342 static void mux_chr_accept_input(CharDriverState *chr)
345 MuxDriver *d = chr->opaque;
347 while (d->prod != d->cons &&
348 d->chr_can_read[m] &&
349 d->chr_can_read[m](d->ext_opaque[m])) {
350 d->chr_read[m](d->ext_opaque[m],
351 &d->buffer[d->cons++ & MUX_BUFFER_MASK], 1);
355 static int mux_chr_can_read(void *opaque)
357 CharDriverState *chr = opaque;
358 MuxDriver *d = chr->opaque;
360 if ((d->prod - d->cons) < MUX_BUFFER_SIZE)
362 if (d->chr_can_read[chr->focus])
363 return d->chr_can_read[chr->focus](d->ext_opaque[chr->focus]);
367 static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
369 CharDriverState *chr = opaque;
370 MuxDriver *d = chr->opaque;
374 mux_chr_accept_input (opaque);
376 for(i = 0; i < size; i++)
377 if (mux_proc_byte(chr, d, buf[i])) {
378 if (d->prod == d->cons &&
379 d->chr_can_read[m] &&
380 d->chr_can_read[m](d->ext_opaque[m]))
381 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
383 d->buffer[d->prod++ & MUX_BUFFER_MASK] = buf[i];
387 static void mux_chr_event(void *opaque, int event)
389 CharDriverState *chr = opaque;
390 MuxDriver *d = chr->opaque;
393 /* Send the event to all registered listeners */
394 for (i = 0; i < d->mux_cnt; i++)
396 d->chr_event[i](d->ext_opaque[i], event);
399 static void mux_chr_update_read_handler(CharDriverState *chr)
401 MuxDriver *d = chr->opaque;
403 if (d->mux_cnt >= MAX_MUX) {
404 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
407 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
408 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
409 d->chr_read[d->mux_cnt] = chr->chr_read;
410 d->chr_event[d->mux_cnt] = chr->chr_event;
411 /* Fix up the real driver with mux routines */
412 if (d->mux_cnt == 0) {
413 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
416 chr->focus = d->mux_cnt;
420 static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
422 CharDriverState *chr;
425 chr = qemu_mallocz(sizeof(CharDriverState));
426 d = qemu_mallocz(sizeof(MuxDriver));
431 chr->chr_write = mux_chr_write;
432 chr->chr_update_read_handler = mux_chr_update_read_handler;
433 chr->chr_accept_input = mux_chr_accept_input;
439 int send_all(int fd, const void *buf, int len1)
445 ret = send(fd, buf, len, 0);
447 errno = WSAGetLastError();
448 if (errno != WSAEWOULDBLOCK) {
451 } else if (ret == 0) {
463 static int unix_write(int fd, const uint8_t *buf, int len1)
469 ret = write(fd, buf, len);
471 if (errno != EINTR && errno != EAGAIN)
473 } else if (ret == 0) {
483 int send_all(int fd, const void *buf, int len1)
485 return unix_write(fd, buf, len1);
496 #define STDIO_MAX_CLIENTS 1
497 static int stdio_nb_clients = 0;
499 static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
501 FDCharDriver *s = chr->opaque;
502 return send_all(s->fd_out, buf, len);
505 static int fd_chr_read_poll(void *opaque)
507 CharDriverState *chr = opaque;
508 FDCharDriver *s = chr->opaque;
510 s->max_size = qemu_chr_can_read(chr);
514 static void fd_chr_read(void *opaque)
516 CharDriverState *chr = opaque;
517 FDCharDriver *s = chr->opaque;
522 if (len > s->max_size)
526 size = read(s->fd_in, buf, len);
528 /* FD has been closed. Remove it from the active list. */
529 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
533 qemu_chr_read(chr, buf, size);
537 static void fd_chr_update_read_handler(CharDriverState *chr)
539 FDCharDriver *s = chr->opaque;
542 if (nographic && s->fd_in == 0) {
544 qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll,
545 fd_chr_read, NULL, chr);
550 static void fd_chr_close(struct CharDriverState *chr)
552 FDCharDriver *s = chr->opaque;
555 if (nographic && s->fd_in == 0) {
557 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
564 /* open a character device to a unix fd */
565 static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
567 CharDriverState *chr;
570 chr = qemu_mallocz(sizeof(CharDriverState));
571 s = qemu_mallocz(sizeof(FDCharDriver));
575 chr->chr_write = fd_chr_write;
576 chr->chr_update_read_handler = fd_chr_update_read_handler;
577 chr->chr_close = fd_chr_close;
584 static CharDriverState *qemu_chr_open_file_out(const char *file_out)
588 TFR(fd_out = open(file_out, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
591 return qemu_chr_open_fd(-1, fd_out);
594 static CharDriverState *qemu_chr_open_pipe(const char *filename)
597 char filename_in[256], filename_out[256];
599 snprintf(filename_in, 256, "%s.in", filename);
600 snprintf(filename_out, 256, "%s.out", filename);
601 TFR(fd_in = open(filename_in, O_RDWR | O_BINARY));
602 TFR(fd_out = open(filename_out, O_RDWR | O_BINARY));
603 if (fd_in < 0 || fd_out < 0) {
608 TFR(fd_in = fd_out = open(filename, O_RDWR | O_BINARY));
612 return qemu_chr_open_fd(fd_in, fd_out);
616 /* for STDIO, we handle the case where several clients use it
619 #define TERM_FIFO_MAX_SIZE 1
621 static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
622 static int term_fifo_size;
624 static int stdio_read_poll(void *opaque)
626 CharDriverState *chr = opaque;
628 /* try to flush the queue if needed */
629 if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) {
630 qemu_chr_read(chr, term_fifo, 1);
633 /* see if we can absorb more chars */
634 if (term_fifo_size == 0)
640 static void stdio_read(void *opaque)
644 CharDriverState *chr = opaque;
646 size = read(0, buf, 1);
648 /* stdin has been closed. Remove it from the active list. */
649 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
653 if (qemu_chr_can_read(chr) > 0) {
654 qemu_chr_read(chr, buf, 1);
655 } else if (term_fifo_size == 0) {
656 term_fifo[term_fifo_size++] = buf[0];
661 /* init terminal so that we can grab keys */
662 static struct termios oldtty;
663 static int old_fd0_flags;
664 static int term_atexit_done;
666 static void term_exit(void)
668 tcsetattr (0, TCSANOW, &oldtty);
669 fcntl(0, F_SETFL, old_fd0_flags);
672 static void term_init(void)
678 old_fd0_flags = fcntl(0, F_GETFL);
680 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
681 |INLCR|IGNCR|ICRNL|IXON);
682 tty.c_oflag |= OPOST;
683 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
684 /* if graphical mode, we allow Ctrl-C handling */
686 tty.c_lflag &= ~ISIG;
687 tty.c_cflag &= ~(CSIZE|PARENB);
692 tcsetattr (0, TCSANOW, &tty);
694 if (!term_atexit_done++)
697 fcntl(0, F_SETFL, O_NONBLOCK);
700 static void qemu_chr_close_stdio(struct CharDriverState *chr)
704 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
708 static CharDriverState *qemu_chr_open_stdio(void)
710 CharDriverState *chr;
712 if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
714 chr = qemu_chr_open_fd(0, 1);
715 chr->chr_close = qemu_chr_close_stdio;
716 qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr);
724 /* Once Solaris has openpty(), this is going to be removed. */
725 int openpty(int *amaster, int *aslave, char *name,
726 struct termios *termp, struct winsize *winp)
729 int mfd = -1, sfd = -1;
731 *amaster = *aslave = -1;
733 mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
737 if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
740 if ((slave = ptsname(mfd)) == NULL)
743 if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
746 if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
747 (termp != NULL && tcgetattr(sfd, termp) < 0))
755 ioctl(sfd, TIOCSWINSZ, winp);
766 void cfmakeraw (struct termios *termios_p)
768 termios_p->c_iflag &=
769 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
770 termios_p->c_oflag &= ~OPOST;
771 termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
772 termios_p->c_cflag &= ~(CSIZE|PARENB);
773 termios_p->c_cflag |= CS8;
775 termios_p->c_cc[VMIN] = 0;
776 termios_p->c_cc[VTIME] = 0;
780 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
781 || defined(__NetBSD__) || defined(__OpenBSD__)
791 static void pty_chr_update_read_handler(CharDriverState *chr);
792 static void pty_chr_state(CharDriverState *chr, int connected);
794 static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
796 PtyCharDriver *s = chr->opaque;
799 /* guest sends data, check for (re-)connect */
800 pty_chr_update_read_handler(chr);
803 return send_all(s->fd, buf, len);
806 static int pty_chr_read_poll(void *opaque)
808 CharDriverState *chr = opaque;
809 PtyCharDriver *s = chr->opaque;
811 s->read_bytes = qemu_chr_can_read(chr);
812 return s->read_bytes;
815 static void pty_chr_read(void *opaque)
817 CharDriverState *chr = opaque;
818 PtyCharDriver *s = chr->opaque;
823 if (len > s->read_bytes)
827 size = read(s->fd, buf, len);
828 if ((size == -1 && errno == EIO) ||
830 pty_chr_state(chr, 0);
834 pty_chr_state(chr, 1);
835 qemu_chr_read(chr, buf, size);
839 static void pty_chr_update_read_handler(CharDriverState *chr)
841 PtyCharDriver *s = chr->opaque;
843 qemu_set_fd_handler2(s->fd, pty_chr_read_poll,
844 pty_chr_read, NULL, chr);
847 * Short timeout here: just need wait long enougth that qemu makes
848 * it through the poll loop once. When reconnected we want a
849 * short timeout so we notice it almost instantly. Otherwise
850 * read() gives us -EIO instantly, making pty_chr_state() reset the
851 * timeout to the normal (much longer) poll interval before the
854 qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 10);
857 static void pty_chr_state(CharDriverState *chr, int connected)
859 PtyCharDriver *s = chr->opaque;
862 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
865 /* (re-)connect poll interval for idle guests: once per second.
866 * We check more frequently in case the guests sends data to
867 * the virtual device linked to our pty. */
868 qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 1000);
876 static void pty_chr_timer(void *opaque)
878 struct CharDriverState *chr = opaque;
879 PtyCharDriver *s = chr->opaque;
884 /* If we arrive here without polling being cleared due
885 * read returning -EIO, then we are (re-)connected */
886 pty_chr_state(chr, 1);
891 pty_chr_update_read_handler(chr);
894 static void pty_chr_close(struct CharDriverState *chr)
896 PtyCharDriver *s = chr->opaque;
898 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
903 static CharDriverState *qemu_chr_open_pty(void)
905 CharDriverState *chr;
909 #if defined(__OpenBSD__)
910 char pty_name[PATH_MAX];
911 #define q_ptsname(x) pty_name
913 char *pty_name = NULL;
914 #define q_ptsname(x) ptsname(x)
917 chr = qemu_mallocz(sizeof(CharDriverState));
918 s = qemu_mallocz(sizeof(PtyCharDriver));
920 if (openpty(&s->fd, &slave_fd, pty_name, NULL, NULL) < 0) {
924 /* Set raw attributes on the pty. */
925 tcgetattr(slave_fd, &tty);
927 tcsetattr(slave_fd, TCSAFLUSH, &tty);
930 len = strlen(q_ptsname(s->fd)) + 5;
931 chr->filename = qemu_malloc(len);
932 snprintf(chr->filename, len, "pty:%s", q_ptsname(s->fd));
933 fprintf(stderr, "char device redirected to %s\n", q_ptsname(s->fd));
936 chr->chr_write = pty_chr_write;
937 chr->chr_update_read_handler = pty_chr_update_read_handler;
938 chr->chr_close = pty_chr_close;
940 s->timer = qemu_new_timer(rt_clock, pty_chr_timer, chr);
945 static void tty_serial_init(int fd, int speed,
946 int parity, int data_bits, int stop_bits)
952 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
953 speed, parity, data_bits, stop_bits);
955 tcgetattr (fd, &tty);
958 if (speed <= 50 * MARGIN)
960 else if (speed <= 75 * MARGIN)
962 else if (speed <= 300 * MARGIN)
964 else if (speed <= 600 * MARGIN)
966 else if (speed <= 1200 * MARGIN)
968 else if (speed <= 2400 * MARGIN)
970 else if (speed <= 4800 * MARGIN)
972 else if (speed <= 9600 * MARGIN)
974 else if (speed <= 19200 * MARGIN)
976 else if (speed <= 38400 * MARGIN)
978 else if (speed <= 57600 * MARGIN)
980 else if (speed <= 115200 * MARGIN)
985 cfsetispeed(&tty, spd);
986 cfsetospeed(&tty, spd);
988 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
989 |INLCR|IGNCR|ICRNL|IXON);
990 tty.c_oflag |= OPOST;
991 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
992 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1013 tty.c_cflag |= PARENB;
1016 tty.c_cflag |= PARENB | PARODD;
1020 tty.c_cflag |= CSTOPB;
1022 tcsetattr (fd, TCSANOW, &tty);
1025 static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1027 FDCharDriver *s = chr->opaque;
1030 case CHR_IOCTL_SERIAL_SET_PARAMS:
1032 QEMUSerialSetParams *ssp = arg;
1033 tty_serial_init(s->fd_in, ssp->speed, ssp->parity,
1034 ssp->data_bits, ssp->stop_bits);
1037 case CHR_IOCTL_SERIAL_SET_BREAK:
1039 int enable = *(int *)arg;
1041 tcsendbreak(s->fd_in, 1);
1044 case CHR_IOCTL_SERIAL_GET_TIOCM:
1047 int *targ = (int *)arg;
1048 ioctl(s->fd_in, TIOCMGET, &sarg);
1050 if (sarg | TIOCM_CTS)
1051 *targ |= CHR_TIOCM_CTS;
1052 if (sarg | TIOCM_CAR)
1053 *targ |= CHR_TIOCM_CAR;
1054 if (sarg | TIOCM_DSR)
1055 *targ |= CHR_TIOCM_DSR;
1056 if (sarg | TIOCM_RI)
1057 *targ |= CHR_TIOCM_RI;
1058 if (sarg | TIOCM_DTR)
1059 *targ |= CHR_TIOCM_DTR;
1060 if (sarg | TIOCM_RTS)
1061 *targ |= CHR_TIOCM_RTS;
1064 case CHR_IOCTL_SERIAL_SET_TIOCM:
1066 int sarg = *(int *)arg;
1068 if (sarg | CHR_TIOCM_DTR)
1070 if (sarg | CHR_TIOCM_RTS)
1072 ioctl(s->fd_in, TIOCMSET, &targ);
1081 static CharDriverState *qemu_chr_open_tty(const char *filename)
1083 CharDriverState *chr;
1086 TFR(fd = open(filename, O_RDWR | O_NONBLOCK));
1087 tty_serial_init(fd, 115200, 'N', 8, 1);
1088 chr = qemu_chr_open_fd(fd, fd);
1093 chr->chr_ioctl = tty_serial_ioctl;
1094 qemu_chr_reset(chr);
1097 #else /* ! __linux__ && ! __sun__ */
1098 static CharDriverState *qemu_chr_open_pty(void)
1102 #endif /* __linux__ || __sun__ */
1104 #if defined(__linux__)
1108 } ParallelCharDriver;
1110 static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1112 if (s->mode != mode) {
1114 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1121 static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1123 ParallelCharDriver *drv = chr->opaque;
1128 case CHR_IOCTL_PP_READ_DATA:
1129 if (ioctl(fd, PPRDATA, &b) < 0)
1131 *(uint8_t *)arg = b;
1133 case CHR_IOCTL_PP_WRITE_DATA:
1134 b = *(uint8_t *)arg;
1135 if (ioctl(fd, PPWDATA, &b) < 0)
1138 case CHR_IOCTL_PP_READ_CONTROL:
1139 if (ioctl(fd, PPRCONTROL, &b) < 0)
1141 /* Linux gives only the lowest bits, and no way to know data
1142 direction! For better compatibility set the fixed upper
1144 *(uint8_t *)arg = b | 0xc0;
1146 case CHR_IOCTL_PP_WRITE_CONTROL:
1147 b = *(uint8_t *)arg;
1148 if (ioctl(fd, PPWCONTROL, &b) < 0)
1151 case CHR_IOCTL_PP_READ_STATUS:
1152 if (ioctl(fd, PPRSTATUS, &b) < 0)
1154 *(uint8_t *)arg = b;
1156 case CHR_IOCTL_PP_DATA_DIR:
1157 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1160 case CHR_IOCTL_PP_EPP_READ_ADDR:
1161 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1162 struct ParallelIOArg *parg = arg;
1163 int n = read(fd, parg->buffer, parg->count);
1164 if (n != parg->count) {
1169 case CHR_IOCTL_PP_EPP_READ:
1170 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1171 struct ParallelIOArg *parg = arg;
1172 int n = read(fd, parg->buffer, parg->count);
1173 if (n != parg->count) {
1178 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1179 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1180 struct ParallelIOArg *parg = arg;
1181 int n = write(fd, parg->buffer, parg->count);
1182 if (n != parg->count) {
1187 case CHR_IOCTL_PP_EPP_WRITE:
1188 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1189 struct ParallelIOArg *parg = arg;
1190 int n = write(fd, parg->buffer, parg->count);
1191 if (n != parg->count) {
1202 static void pp_close(CharDriverState *chr)
1204 ParallelCharDriver *drv = chr->opaque;
1207 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1208 ioctl(fd, PPRELEASE);
1213 static CharDriverState *qemu_chr_open_pp(const char *filename)
1215 CharDriverState *chr;
1216 ParallelCharDriver *drv;
1219 TFR(fd = open(filename, O_RDWR));
1223 if (ioctl(fd, PPCLAIM) < 0) {
1228 drv = qemu_mallocz(sizeof(ParallelCharDriver));
1230 drv->mode = IEEE1284_MODE_COMPAT;
1232 chr = qemu_mallocz(sizeof(CharDriverState));
1233 chr->chr_write = null_chr_write;
1234 chr->chr_ioctl = pp_ioctl;
1235 chr->chr_close = pp_close;
1238 qemu_chr_reset(chr);
1242 #endif /* __linux__ */
1244 #if defined(__FreeBSD__)
1245 static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1247 int fd = (int)chr->opaque;
1251 case CHR_IOCTL_PP_READ_DATA:
1252 if (ioctl(fd, PPIGDATA, &b) < 0)
1254 *(uint8_t *)arg = b;
1256 case CHR_IOCTL_PP_WRITE_DATA:
1257 b = *(uint8_t *)arg;
1258 if (ioctl(fd, PPISDATA, &b) < 0)
1261 case CHR_IOCTL_PP_READ_CONTROL:
1262 if (ioctl(fd, PPIGCTRL, &b) < 0)
1264 *(uint8_t *)arg = b;
1266 case CHR_IOCTL_PP_WRITE_CONTROL:
1267 b = *(uint8_t *)arg;
1268 if (ioctl(fd, PPISCTRL, &b) < 0)
1271 case CHR_IOCTL_PP_READ_STATUS:
1272 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1274 *(uint8_t *)arg = b;
1282 static CharDriverState *qemu_chr_open_pp(const char *filename)
1284 CharDriverState *chr;
1287 fd = open(filename, O_RDWR);
1291 chr = qemu_mallocz(sizeof(CharDriverState));
1292 chr->opaque = (void *)fd;
1293 chr->chr_write = null_chr_write;
1294 chr->chr_ioctl = pp_ioctl;
1303 HANDLE hcom, hrecv, hsend;
1304 OVERLAPPED orecv, osend;
1309 #define NSENDBUF 2048
1310 #define NRECVBUF 2048
1311 #define MAXCONNECT 1
1312 #define NTIMEOUT 5000
1314 static int win_chr_poll(void *opaque);
1315 static int win_chr_pipe_poll(void *opaque);
1317 static void win_chr_close(CharDriverState *chr)
1319 WinCharState *s = chr->opaque;
1322 CloseHandle(s->hsend);
1326 CloseHandle(s->hrecv);
1330 CloseHandle(s->hcom);
1334 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1336 qemu_del_polling_cb(win_chr_poll, chr);
1339 static int win_chr_init(CharDriverState *chr, const char *filename)
1341 WinCharState *s = chr->opaque;
1343 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1348 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1350 fprintf(stderr, "Failed CreateEvent\n");
1353 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1355 fprintf(stderr, "Failed CreateEvent\n");
1359 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1360 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1361 if (s->hcom == INVALID_HANDLE_VALUE) {
1362 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1367 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1368 fprintf(stderr, "Failed SetupComm\n");
1372 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1373 size = sizeof(COMMCONFIG);
1374 GetDefaultCommConfig(filename, &comcfg, &size);
1375 comcfg.dcb.DCBlength = sizeof(DCB);
1376 CommConfigDialog(filename, NULL, &comcfg);
1378 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1379 fprintf(stderr, "Failed SetCommState\n");
1383 if (!SetCommMask(s->hcom, EV_ERR)) {
1384 fprintf(stderr, "Failed SetCommMask\n");
1388 cto.ReadIntervalTimeout = MAXDWORD;
1389 if (!SetCommTimeouts(s->hcom, &cto)) {
1390 fprintf(stderr, "Failed SetCommTimeouts\n");
1394 if (!ClearCommError(s->hcom, &err, &comstat)) {
1395 fprintf(stderr, "Failed ClearCommError\n");
1398 qemu_add_polling_cb(win_chr_poll, chr);
1406 static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1408 WinCharState *s = chr->opaque;
1409 DWORD len, ret, size, err;
1412 ZeroMemory(&s->osend, sizeof(s->osend));
1413 s->osend.hEvent = s->hsend;
1416 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1418 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1420 err = GetLastError();
1421 if (err == ERROR_IO_PENDING) {
1422 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1440 static int win_chr_read_poll(CharDriverState *chr)
1442 WinCharState *s = chr->opaque;
1444 s->max_size = qemu_chr_can_read(chr);
1448 static void win_chr_readfile(CharDriverState *chr)
1450 WinCharState *s = chr->opaque;
1455 ZeroMemory(&s->orecv, sizeof(s->orecv));
1456 s->orecv.hEvent = s->hrecv;
1457 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1459 err = GetLastError();
1460 if (err == ERROR_IO_PENDING) {
1461 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1466 qemu_chr_read(chr, buf, size);
1470 static void win_chr_read(CharDriverState *chr)
1472 WinCharState *s = chr->opaque;
1474 if (s->len > s->max_size)
1475 s->len = s->max_size;
1479 win_chr_readfile(chr);
1482 static int win_chr_poll(void *opaque)
1484 CharDriverState *chr = opaque;
1485 WinCharState *s = chr->opaque;
1489 ClearCommError(s->hcom, &comerr, &status);
1490 if (status.cbInQue > 0) {
1491 s->len = status.cbInQue;
1492 win_chr_read_poll(chr);
1499 static CharDriverState *qemu_chr_open_win(const char *filename)
1501 CharDriverState *chr;
1504 chr = qemu_mallocz(sizeof(CharDriverState));
1505 s = qemu_mallocz(sizeof(WinCharState));
1507 chr->chr_write = win_chr_write;
1508 chr->chr_close = win_chr_close;
1510 if (win_chr_init(chr, filename) < 0) {
1515 qemu_chr_reset(chr);
1519 static int win_chr_pipe_poll(void *opaque)
1521 CharDriverState *chr = opaque;
1522 WinCharState *s = chr->opaque;
1525 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1528 win_chr_read_poll(chr);
1535 static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1537 WinCharState *s = chr->opaque;
1545 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1547 fprintf(stderr, "Failed CreateEvent\n");
1550 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1552 fprintf(stderr, "Failed CreateEvent\n");
1556 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1557 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1558 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1560 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1561 if (s->hcom == INVALID_HANDLE_VALUE) {
1562 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1567 ZeroMemory(&ov, sizeof(ov));
1568 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1569 ret = ConnectNamedPipe(s->hcom, &ov);
1571 fprintf(stderr, "Failed ConnectNamedPipe\n");
1575 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1577 fprintf(stderr, "Failed GetOverlappedResult\n");
1579 CloseHandle(ov.hEvent);
1586 CloseHandle(ov.hEvent);
1589 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1598 static CharDriverState *qemu_chr_open_win_pipe(const char *filename)
1600 CharDriverState *chr;
1603 chr = qemu_mallocz(sizeof(CharDriverState));
1604 s = qemu_mallocz(sizeof(WinCharState));
1606 chr->chr_write = win_chr_write;
1607 chr->chr_close = win_chr_close;
1609 if (win_chr_pipe_init(chr, filename) < 0) {
1614 qemu_chr_reset(chr);
1618 static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
1620 CharDriverState *chr;
1623 chr = qemu_mallocz(sizeof(CharDriverState));
1624 s = qemu_mallocz(sizeof(WinCharState));
1627 chr->chr_write = win_chr_write;
1628 qemu_chr_reset(chr);
1632 static CharDriverState *qemu_chr_open_win_con(const char *filename)
1634 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
1637 static CharDriverState *qemu_chr_open_win_file_out(const char *file_out)
1641 fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
1642 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1643 if (fd_out == INVALID_HANDLE_VALUE)
1646 return qemu_chr_open_win_file(fd_out);
1648 #endif /* !_WIN32 */
1650 /***********************************************************/
1651 /* UDP Net console */
1655 struct sockaddr_in daddr;
1662 static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1664 NetCharDriver *s = chr->opaque;
1666 return sendto(s->fd, buf, len, 0,
1667 (struct sockaddr *)&s->daddr, sizeof(struct sockaddr_in));
1670 static int udp_chr_read_poll(void *opaque)
1672 CharDriverState *chr = opaque;
1673 NetCharDriver *s = chr->opaque;
1675 s->max_size = qemu_chr_can_read(chr);
1677 /* If there were any stray characters in the queue process them
1680 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1681 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
1683 s->max_size = qemu_chr_can_read(chr);
1688 static void udp_chr_read(void *opaque)
1690 CharDriverState *chr = opaque;
1691 NetCharDriver *s = chr->opaque;
1693 if (s->max_size == 0)
1695 s->bufcnt = recv(s->fd, s->buf, sizeof(s->buf), 0);
1696 s->bufptr = s->bufcnt;
1701 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1702 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
1704 s->max_size = qemu_chr_can_read(chr);
1708 static void udp_chr_update_read_handler(CharDriverState *chr)
1710 NetCharDriver *s = chr->opaque;
1713 qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
1714 udp_chr_read, NULL, chr);
1718 static CharDriverState *qemu_chr_open_udp(const char *def)
1720 CharDriverState *chr = NULL;
1721 NetCharDriver *s = NULL;
1723 struct sockaddr_in saddr;
1725 chr = qemu_mallocz(sizeof(CharDriverState));
1726 s = qemu_mallocz(sizeof(NetCharDriver));
1728 fd = socket(PF_INET, SOCK_DGRAM, 0);
1730 perror("socket(PF_INET, SOCK_DGRAM)");
1734 if (parse_host_src_port(&s->daddr, &saddr, def) < 0) {
1735 printf("Could not parse: %s\n", def);
1739 if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
1749 chr->chr_write = udp_chr_write;
1750 chr->chr_update_read_handler = udp_chr_update_read_handler;
1763 /***********************************************************/
1764 /* TCP Net console */
1775 static void tcp_chr_accept(void *opaque);
1777 static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1779 TCPCharDriver *s = chr->opaque;
1781 return send_all(s->fd, buf, len);
1783 /* XXX: indicate an error ? */
1788 static int tcp_chr_read_poll(void *opaque)
1790 CharDriverState *chr = opaque;
1791 TCPCharDriver *s = chr->opaque;
1794 s->max_size = qemu_chr_can_read(chr);
1799 #define IAC_BREAK 243
1800 static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
1802 uint8_t *buf, int *size)
1804 /* Handle any telnet client's basic IAC options to satisfy char by
1805 * char mode with no echo. All IAC options will be removed from
1806 * the buf and the do_telnetopt variable will be used to track the
1807 * state of the width of the IAC information.
1809 * IAC commands come in sets of 3 bytes with the exception of the
1810 * "IAC BREAK" command and the double IAC.
1816 for (i = 0; i < *size; i++) {
1817 if (s->do_telnetopt > 1) {
1818 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
1819 /* Double IAC means send an IAC */
1823 s->do_telnetopt = 1;
1825 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
1826 /* Handle IAC break commands by sending a serial break */
1827 qemu_chr_event(chr, CHR_EVENT_BREAK);
1832 if (s->do_telnetopt >= 4) {
1833 s->do_telnetopt = 1;
1836 if ((unsigned char)buf[i] == IAC) {
1837 s->do_telnetopt = 2;
1848 static void tcp_chr_read(void *opaque)
1850 CharDriverState *chr = opaque;
1851 TCPCharDriver *s = chr->opaque;
1855 if (!s->connected || s->max_size <= 0)
1858 if (len > s->max_size)
1860 size = recv(s->fd, buf, len, 0);
1862 /* connection closed */
1864 if (s->listen_fd >= 0) {
1865 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
1867 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1870 } else if (size > 0) {
1871 if (s->do_telnetopt)
1872 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
1874 qemu_chr_read(chr, buf, size);
1878 static void tcp_chr_connect(void *opaque)
1880 CharDriverState *chr = opaque;
1881 TCPCharDriver *s = chr->opaque;
1884 qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
1885 tcp_chr_read, NULL, chr);
1886 qemu_chr_reset(chr);
1889 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
1890 static void tcp_chr_telnet_init(int fd)
1893 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
1894 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
1895 send(fd, (char *)buf, 3, 0);
1896 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
1897 send(fd, (char *)buf, 3, 0);
1898 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
1899 send(fd, (char *)buf, 3, 0);
1900 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
1901 send(fd, (char *)buf, 3, 0);
1904 static void socket_set_nodelay(int fd)
1907 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
1910 static void tcp_chr_accept(void *opaque)
1912 CharDriverState *chr = opaque;
1913 TCPCharDriver *s = chr->opaque;
1914 struct sockaddr_in saddr;
1916 struct sockaddr_un uaddr;
1918 struct sockaddr *addr;
1925 len = sizeof(uaddr);
1926 addr = (struct sockaddr *)&uaddr;
1930 len = sizeof(saddr);
1931 addr = (struct sockaddr *)&saddr;
1933 fd = accept(s->listen_fd, addr, &len);
1934 if (fd < 0 && errno != EINTR) {
1936 } else if (fd >= 0) {
1937 if (s->do_telnetopt)
1938 tcp_chr_telnet_init(fd);
1942 socket_set_nonblock(fd);
1944 socket_set_nodelay(fd);
1946 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
1947 tcp_chr_connect(chr);
1950 static void tcp_chr_close(CharDriverState *chr)
1952 TCPCharDriver *s = chr->opaque;
1955 if (s->listen_fd >= 0)
1956 closesocket(s->listen_fd);
1960 static CharDriverState *qemu_chr_open_tcp(const char *host_str,
1964 CharDriverState *chr = NULL;
1965 TCPCharDriver *s = NULL;
1966 int fd = -1, offset = 0;
1968 int is_waitconnect = 1;
1973 while((ptr = strchr(ptr,','))) {
1975 if (!strncmp(ptr,"server",6)) {
1977 } else if (!strncmp(ptr,"nowait",6)) {
1979 } else if (!strncmp(ptr,"nodelay",6)) {
1981 } else if (!strncmp(ptr,"to=",3)) {
1982 /* nothing, inet_listen() parses this one */;
1984 printf("Unknown option: %s\n", ptr);
1991 chr = qemu_mallocz(sizeof(CharDriverState));
1992 s = qemu_mallocz(sizeof(TCPCharDriver));
1995 chr->filename = qemu_malloc(256);
1997 pstrcpy(chr->filename, 256, "unix:");
1998 } else if (is_telnet) {
1999 pstrcpy(chr->filename, 256, "telnet:");
2001 pstrcpy(chr->filename, 256, "tcp:");
2003 offset = strlen(chr->filename);
2007 fd = unix_listen(host_str, chr->filename + offset, 256 - offset);
2009 fd = unix_connect(host_str);
2013 fd = inet_listen(host_str, chr->filename + offset, 256 - offset,
2016 fd = inet_connect(host_str, SOCK_STREAM);
2022 if (!is_waitconnect)
2023 socket_set_nonblock(fd);
2028 s->is_unix = is_unix;
2029 s->do_nodelay = do_nodelay && !is_unix;
2032 chr->chr_write = tcp_chr_write;
2033 chr->chr_close = tcp_chr_close;
2037 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
2039 s->do_telnetopt = 1;
2043 socket_set_nodelay(fd);
2044 tcp_chr_connect(chr);
2047 if (is_listen && is_waitconnect) {
2048 printf("QEMU waiting for connection on: %s\n",
2049 chr->filename ? chr->filename : host_str);
2050 tcp_chr_accept(chr);
2051 socket_set_nonblock(s->listen_fd);
2063 static TAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs
2064 = TAILQ_HEAD_INITIALIZER(chardevs);
2066 CharDriverState *qemu_chr_open(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
2069 CharDriverState *chr;
2071 if (!strcmp(filename, "vc")) {
2072 chr = text_console_init(0);
2074 if (strstart(filename, "vc:", &p)) {
2075 chr = text_console_init(p);
2077 if (!strcmp(filename, "null")) {
2078 chr = qemu_chr_open_null();
2080 if (strstart(filename, "tcp:", &p)) {
2081 chr = qemu_chr_open_tcp(p, 0, 0);
2083 if (strstart(filename, "telnet:", &p)) {
2084 chr = qemu_chr_open_tcp(p, 1, 0);
2086 if (strstart(filename, "udp:", &p)) {
2087 chr = qemu_chr_open_udp(p);
2089 if (strstart(filename, "mon:", &p)) {
2090 chr = qemu_chr_open(label, p, NULL);
2092 chr = qemu_chr_open_mux(chr);
2093 monitor_init(chr, !nographic);
2095 printf("Unable to open driver: %s\n", p);
2099 if (strstart(filename, "unix:", &p)) {
2100 chr = qemu_chr_open_tcp(p, 0, 1);
2101 } else if (strstart(filename, "file:", &p)) {
2102 chr = qemu_chr_open_file_out(p);
2103 } else if (strstart(filename, "pipe:", &p)) {
2104 chr = qemu_chr_open_pipe(p);
2105 } else if (!strcmp(filename, "pty")) {
2106 chr = qemu_chr_open_pty();
2107 } else if (!strcmp(filename, "stdio")) {
2108 chr = qemu_chr_open_stdio();
2110 #if defined(__linux__)
2111 if (strstart(filename, "/dev/parport", NULL)) {
2112 chr = qemu_chr_open_pp(filename);
2114 #elif defined(__FreeBSD__)
2115 if (strstart(filename, "/dev/ppi", NULL)) {
2116 chr = qemu_chr_open_pp(filename);
2119 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2120 || defined(__NetBSD__) || defined(__OpenBSD__)
2121 if (strstart(filename, "/dev/", NULL)) {
2122 chr = qemu_chr_open_tty(filename);
2126 if (strstart(filename, "COM", NULL)) {
2127 chr = qemu_chr_open_win(filename);
2129 if (strstart(filename, "pipe:", &p)) {
2130 chr = qemu_chr_open_win_pipe(p);
2132 if (strstart(filename, "con:", NULL)) {
2133 chr = qemu_chr_open_win_con(filename);
2135 if (strstart(filename, "file:", &p)) {
2136 chr = qemu_chr_open_win_file_out(p);
2139 #ifdef CONFIG_BRLAPI
2140 if (!strcmp(filename, "braille")) {
2141 chr = chr_baum_init();
2150 chr->filename = qemu_strdup(filename);
2152 chr->label = qemu_strdup(label);
2153 TAILQ_INSERT_TAIL(&chardevs, chr, next);
2158 void qemu_chr_close(CharDriverState *chr)
2160 TAILQ_REMOVE(&chardevs, chr, next);
2162 chr->chr_close(chr);
2163 qemu_free(chr->filename);
2164 qemu_free(chr->label);
2168 void qemu_chr_info(void)
2170 CharDriverState *chr;
2172 TAILQ_FOREACH(chr, &chardevs, next) {
2173 term_printf("%s: filename=%s\n", chr->label, chr->filename);