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 ioctl(s->fd_in, TIOCMGET, &targ);
1069 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1070 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1071 if (sarg & CHR_TIOCM_CTS)
1073 if (sarg & CHR_TIOCM_CAR)
1075 if (sarg & CHR_TIOCM_DSR)
1077 if (sarg & CHR_TIOCM_RI)
1079 if (sarg & CHR_TIOCM_DTR)
1081 if (sarg & CHR_TIOCM_RTS)
1083 ioctl(s->fd_in, TIOCMSET, &targ);
1092 static CharDriverState *qemu_chr_open_tty(const char *filename)
1094 CharDriverState *chr;
1097 TFR(fd = open(filename, O_RDWR | O_NONBLOCK));
1098 tty_serial_init(fd, 115200, 'N', 8, 1);
1099 chr = qemu_chr_open_fd(fd, fd);
1104 chr->chr_ioctl = tty_serial_ioctl;
1105 qemu_chr_reset(chr);
1108 #else /* ! __linux__ && ! __sun__ */
1109 static CharDriverState *qemu_chr_open_pty(void)
1113 #endif /* __linux__ || __sun__ */
1115 #if defined(__linux__)
1119 } ParallelCharDriver;
1121 static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1123 if (s->mode != mode) {
1125 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1132 static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1134 ParallelCharDriver *drv = chr->opaque;
1139 case CHR_IOCTL_PP_READ_DATA:
1140 if (ioctl(fd, PPRDATA, &b) < 0)
1142 *(uint8_t *)arg = b;
1144 case CHR_IOCTL_PP_WRITE_DATA:
1145 b = *(uint8_t *)arg;
1146 if (ioctl(fd, PPWDATA, &b) < 0)
1149 case CHR_IOCTL_PP_READ_CONTROL:
1150 if (ioctl(fd, PPRCONTROL, &b) < 0)
1152 /* Linux gives only the lowest bits, and no way to know data
1153 direction! For better compatibility set the fixed upper
1155 *(uint8_t *)arg = b | 0xc0;
1157 case CHR_IOCTL_PP_WRITE_CONTROL:
1158 b = *(uint8_t *)arg;
1159 if (ioctl(fd, PPWCONTROL, &b) < 0)
1162 case CHR_IOCTL_PP_READ_STATUS:
1163 if (ioctl(fd, PPRSTATUS, &b) < 0)
1165 *(uint8_t *)arg = b;
1167 case CHR_IOCTL_PP_DATA_DIR:
1168 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1171 case CHR_IOCTL_PP_EPP_READ_ADDR:
1172 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1173 struct ParallelIOArg *parg = arg;
1174 int n = read(fd, parg->buffer, parg->count);
1175 if (n != parg->count) {
1180 case CHR_IOCTL_PP_EPP_READ:
1181 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
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_WRITE_ADDR:
1190 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1191 struct ParallelIOArg *parg = arg;
1192 int n = write(fd, parg->buffer, parg->count);
1193 if (n != parg->count) {
1198 case CHR_IOCTL_PP_EPP_WRITE:
1199 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1200 struct ParallelIOArg *parg = arg;
1201 int n = write(fd, parg->buffer, parg->count);
1202 if (n != parg->count) {
1213 static void pp_close(CharDriverState *chr)
1215 ParallelCharDriver *drv = chr->opaque;
1218 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1219 ioctl(fd, PPRELEASE);
1224 static CharDriverState *qemu_chr_open_pp(const char *filename)
1226 CharDriverState *chr;
1227 ParallelCharDriver *drv;
1230 TFR(fd = open(filename, O_RDWR));
1234 if (ioctl(fd, PPCLAIM) < 0) {
1239 drv = qemu_mallocz(sizeof(ParallelCharDriver));
1241 drv->mode = IEEE1284_MODE_COMPAT;
1243 chr = qemu_mallocz(sizeof(CharDriverState));
1244 chr->chr_write = null_chr_write;
1245 chr->chr_ioctl = pp_ioctl;
1246 chr->chr_close = pp_close;
1249 qemu_chr_reset(chr);
1253 #endif /* __linux__ */
1255 #if defined(__FreeBSD__)
1256 static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1258 int fd = (int)chr->opaque;
1262 case CHR_IOCTL_PP_READ_DATA:
1263 if (ioctl(fd, PPIGDATA, &b) < 0)
1265 *(uint8_t *)arg = b;
1267 case CHR_IOCTL_PP_WRITE_DATA:
1268 b = *(uint8_t *)arg;
1269 if (ioctl(fd, PPISDATA, &b) < 0)
1272 case CHR_IOCTL_PP_READ_CONTROL:
1273 if (ioctl(fd, PPIGCTRL, &b) < 0)
1275 *(uint8_t *)arg = b;
1277 case CHR_IOCTL_PP_WRITE_CONTROL:
1278 b = *(uint8_t *)arg;
1279 if (ioctl(fd, PPISCTRL, &b) < 0)
1282 case CHR_IOCTL_PP_READ_STATUS:
1283 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1285 *(uint8_t *)arg = b;
1293 static CharDriverState *qemu_chr_open_pp(const char *filename)
1295 CharDriverState *chr;
1298 fd = open(filename, O_RDWR);
1302 chr = qemu_mallocz(sizeof(CharDriverState));
1303 chr->opaque = (void *)fd;
1304 chr->chr_write = null_chr_write;
1305 chr->chr_ioctl = pp_ioctl;
1314 HANDLE hcom, hrecv, hsend;
1315 OVERLAPPED orecv, osend;
1320 #define NSENDBUF 2048
1321 #define NRECVBUF 2048
1322 #define MAXCONNECT 1
1323 #define NTIMEOUT 5000
1325 static int win_chr_poll(void *opaque);
1326 static int win_chr_pipe_poll(void *opaque);
1328 static void win_chr_close(CharDriverState *chr)
1330 WinCharState *s = chr->opaque;
1333 CloseHandle(s->hsend);
1337 CloseHandle(s->hrecv);
1341 CloseHandle(s->hcom);
1345 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1347 qemu_del_polling_cb(win_chr_poll, chr);
1350 static int win_chr_init(CharDriverState *chr, const char *filename)
1352 WinCharState *s = chr->opaque;
1354 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1359 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1361 fprintf(stderr, "Failed CreateEvent\n");
1364 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1366 fprintf(stderr, "Failed CreateEvent\n");
1370 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1371 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1372 if (s->hcom == INVALID_HANDLE_VALUE) {
1373 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1378 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1379 fprintf(stderr, "Failed SetupComm\n");
1383 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1384 size = sizeof(COMMCONFIG);
1385 GetDefaultCommConfig(filename, &comcfg, &size);
1386 comcfg.dcb.DCBlength = sizeof(DCB);
1387 CommConfigDialog(filename, NULL, &comcfg);
1389 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1390 fprintf(stderr, "Failed SetCommState\n");
1394 if (!SetCommMask(s->hcom, EV_ERR)) {
1395 fprintf(stderr, "Failed SetCommMask\n");
1399 cto.ReadIntervalTimeout = MAXDWORD;
1400 if (!SetCommTimeouts(s->hcom, &cto)) {
1401 fprintf(stderr, "Failed SetCommTimeouts\n");
1405 if (!ClearCommError(s->hcom, &err, &comstat)) {
1406 fprintf(stderr, "Failed ClearCommError\n");
1409 qemu_add_polling_cb(win_chr_poll, chr);
1417 static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1419 WinCharState *s = chr->opaque;
1420 DWORD len, ret, size, err;
1423 ZeroMemory(&s->osend, sizeof(s->osend));
1424 s->osend.hEvent = s->hsend;
1427 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1429 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1431 err = GetLastError();
1432 if (err == ERROR_IO_PENDING) {
1433 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1451 static int win_chr_read_poll(CharDriverState *chr)
1453 WinCharState *s = chr->opaque;
1455 s->max_size = qemu_chr_can_read(chr);
1459 static void win_chr_readfile(CharDriverState *chr)
1461 WinCharState *s = chr->opaque;
1466 ZeroMemory(&s->orecv, sizeof(s->orecv));
1467 s->orecv.hEvent = s->hrecv;
1468 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1470 err = GetLastError();
1471 if (err == ERROR_IO_PENDING) {
1472 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1477 qemu_chr_read(chr, buf, size);
1481 static void win_chr_read(CharDriverState *chr)
1483 WinCharState *s = chr->opaque;
1485 if (s->len > s->max_size)
1486 s->len = s->max_size;
1490 win_chr_readfile(chr);
1493 static int win_chr_poll(void *opaque)
1495 CharDriverState *chr = opaque;
1496 WinCharState *s = chr->opaque;
1500 ClearCommError(s->hcom, &comerr, &status);
1501 if (status.cbInQue > 0) {
1502 s->len = status.cbInQue;
1503 win_chr_read_poll(chr);
1510 static CharDriverState *qemu_chr_open_win(const char *filename)
1512 CharDriverState *chr;
1515 chr = qemu_mallocz(sizeof(CharDriverState));
1516 s = qemu_mallocz(sizeof(WinCharState));
1518 chr->chr_write = win_chr_write;
1519 chr->chr_close = win_chr_close;
1521 if (win_chr_init(chr, filename) < 0) {
1526 qemu_chr_reset(chr);
1530 static int win_chr_pipe_poll(void *opaque)
1532 CharDriverState *chr = opaque;
1533 WinCharState *s = chr->opaque;
1536 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1539 win_chr_read_poll(chr);
1546 static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1548 WinCharState *s = chr->opaque;
1556 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1558 fprintf(stderr, "Failed CreateEvent\n");
1561 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1563 fprintf(stderr, "Failed CreateEvent\n");
1567 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1568 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1569 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1571 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1572 if (s->hcom == INVALID_HANDLE_VALUE) {
1573 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1578 ZeroMemory(&ov, sizeof(ov));
1579 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1580 ret = ConnectNamedPipe(s->hcom, &ov);
1582 fprintf(stderr, "Failed ConnectNamedPipe\n");
1586 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1588 fprintf(stderr, "Failed GetOverlappedResult\n");
1590 CloseHandle(ov.hEvent);
1597 CloseHandle(ov.hEvent);
1600 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1609 static CharDriverState *qemu_chr_open_win_pipe(const char *filename)
1611 CharDriverState *chr;
1614 chr = qemu_mallocz(sizeof(CharDriverState));
1615 s = qemu_mallocz(sizeof(WinCharState));
1617 chr->chr_write = win_chr_write;
1618 chr->chr_close = win_chr_close;
1620 if (win_chr_pipe_init(chr, filename) < 0) {
1625 qemu_chr_reset(chr);
1629 static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
1631 CharDriverState *chr;
1634 chr = qemu_mallocz(sizeof(CharDriverState));
1635 s = qemu_mallocz(sizeof(WinCharState));
1638 chr->chr_write = win_chr_write;
1639 qemu_chr_reset(chr);
1643 static CharDriverState *qemu_chr_open_win_con(const char *filename)
1645 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
1648 static CharDriverState *qemu_chr_open_win_file_out(const char *file_out)
1652 fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
1653 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1654 if (fd_out == INVALID_HANDLE_VALUE)
1657 return qemu_chr_open_win_file(fd_out);
1659 #endif /* !_WIN32 */
1661 /***********************************************************/
1662 /* UDP Net console */
1666 struct sockaddr_in daddr;
1673 static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1675 NetCharDriver *s = chr->opaque;
1677 return sendto(s->fd, buf, len, 0,
1678 (struct sockaddr *)&s->daddr, sizeof(struct sockaddr_in));
1681 static int udp_chr_read_poll(void *opaque)
1683 CharDriverState *chr = opaque;
1684 NetCharDriver *s = chr->opaque;
1686 s->max_size = qemu_chr_can_read(chr);
1688 /* If there were any stray characters in the queue process them
1691 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1692 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
1694 s->max_size = qemu_chr_can_read(chr);
1699 static void udp_chr_read(void *opaque)
1701 CharDriverState *chr = opaque;
1702 NetCharDriver *s = chr->opaque;
1704 if (s->max_size == 0)
1706 s->bufcnt = recv(s->fd, s->buf, sizeof(s->buf), 0);
1707 s->bufptr = s->bufcnt;
1712 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1713 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
1715 s->max_size = qemu_chr_can_read(chr);
1719 static void udp_chr_update_read_handler(CharDriverState *chr)
1721 NetCharDriver *s = chr->opaque;
1724 qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
1725 udp_chr_read, NULL, chr);
1729 static CharDriverState *qemu_chr_open_udp(const char *def)
1731 CharDriverState *chr = NULL;
1732 NetCharDriver *s = NULL;
1734 struct sockaddr_in saddr;
1736 chr = qemu_mallocz(sizeof(CharDriverState));
1737 s = qemu_mallocz(sizeof(NetCharDriver));
1739 fd = socket(PF_INET, SOCK_DGRAM, 0);
1741 perror("socket(PF_INET, SOCK_DGRAM)");
1745 if (parse_host_src_port(&s->daddr, &saddr, def) < 0) {
1746 printf("Could not parse: %s\n", def);
1750 if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
1760 chr->chr_write = udp_chr_write;
1761 chr->chr_update_read_handler = udp_chr_update_read_handler;
1774 /***********************************************************/
1775 /* TCP Net console */
1786 static void tcp_chr_accept(void *opaque);
1788 static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1790 TCPCharDriver *s = chr->opaque;
1792 return send_all(s->fd, buf, len);
1794 /* XXX: indicate an error ? */
1799 static int tcp_chr_read_poll(void *opaque)
1801 CharDriverState *chr = opaque;
1802 TCPCharDriver *s = chr->opaque;
1805 s->max_size = qemu_chr_can_read(chr);
1810 #define IAC_BREAK 243
1811 static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
1813 uint8_t *buf, int *size)
1815 /* Handle any telnet client's basic IAC options to satisfy char by
1816 * char mode with no echo. All IAC options will be removed from
1817 * the buf and the do_telnetopt variable will be used to track the
1818 * state of the width of the IAC information.
1820 * IAC commands come in sets of 3 bytes with the exception of the
1821 * "IAC BREAK" command and the double IAC.
1827 for (i = 0; i < *size; i++) {
1828 if (s->do_telnetopt > 1) {
1829 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
1830 /* Double IAC means send an IAC */
1834 s->do_telnetopt = 1;
1836 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
1837 /* Handle IAC break commands by sending a serial break */
1838 qemu_chr_event(chr, CHR_EVENT_BREAK);
1843 if (s->do_telnetopt >= 4) {
1844 s->do_telnetopt = 1;
1847 if ((unsigned char)buf[i] == IAC) {
1848 s->do_telnetopt = 2;
1859 static void tcp_chr_read(void *opaque)
1861 CharDriverState *chr = opaque;
1862 TCPCharDriver *s = chr->opaque;
1866 if (!s->connected || s->max_size <= 0)
1869 if (len > s->max_size)
1871 size = recv(s->fd, buf, len, 0);
1873 /* connection closed */
1875 if (s->listen_fd >= 0) {
1876 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
1878 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1881 } else if (size > 0) {
1882 if (s->do_telnetopt)
1883 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
1885 qemu_chr_read(chr, buf, size);
1889 static void tcp_chr_connect(void *opaque)
1891 CharDriverState *chr = opaque;
1892 TCPCharDriver *s = chr->opaque;
1895 qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
1896 tcp_chr_read, NULL, chr);
1897 qemu_chr_reset(chr);
1900 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
1901 static void tcp_chr_telnet_init(int fd)
1904 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
1905 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
1906 send(fd, (char *)buf, 3, 0);
1907 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
1908 send(fd, (char *)buf, 3, 0);
1909 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
1910 send(fd, (char *)buf, 3, 0);
1911 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
1912 send(fd, (char *)buf, 3, 0);
1915 static void socket_set_nodelay(int fd)
1918 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
1921 static void tcp_chr_accept(void *opaque)
1923 CharDriverState *chr = opaque;
1924 TCPCharDriver *s = chr->opaque;
1925 struct sockaddr_in saddr;
1927 struct sockaddr_un uaddr;
1929 struct sockaddr *addr;
1936 len = sizeof(uaddr);
1937 addr = (struct sockaddr *)&uaddr;
1941 len = sizeof(saddr);
1942 addr = (struct sockaddr *)&saddr;
1944 fd = accept(s->listen_fd, addr, &len);
1945 if (fd < 0 && errno != EINTR) {
1947 } else if (fd >= 0) {
1948 if (s->do_telnetopt)
1949 tcp_chr_telnet_init(fd);
1953 socket_set_nonblock(fd);
1955 socket_set_nodelay(fd);
1957 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
1958 tcp_chr_connect(chr);
1961 static void tcp_chr_close(CharDriverState *chr)
1963 TCPCharDriver *s = chr->opaque;
1966 if (s->listen_fd >= 0)
1967 closesocket(s->listen_fd);
1971 static CharDriverState *qemu_chr_open_tcp(const char *host_str,
1975 CharDriverState *chr = NULL;
1976 TCPCharDriver *s = NULL;
1977 int fd = -1, offset = 0;
1979 int is_waitconnect = 1;
1984 while((ptr = strchr(ptr,','))) {
1986 if (!strncmp(ptr,"server",6)) {
1988 } else if (!strncmp(ptr,"nowait",6)) {
1990 } else if (!strncmp(ptr,"nodelay",6)) {
1992 } else if (!strncmp(ptr,"to=",3)) {
1993 /* nothing, inet_listen() parses this one */;
1995 printf("Unknown option: %s\n", ptr);
2002 chr = qemu_mallocz(sizeof(CharDriverState));
2003 s = qemu_mallocz(sizeof(TCPCharDriver));
2006 chr->filename = qemu_malloc(256);
2008 pstrcpy(chr->filename, 256, "unix:");
2009 } else if (is_telnet) {
2010 pstrcpy(chr->filename, 256, "telnet:");
2012 pstrcpy(chr->filename, 256, "tcp:");
2014 offset = strlen(chr->filename);
2018 fd = unix_listen(host_str, chr->filename + offset, 256 - offset);
2020 fd = unix_connect(host_str);
2024 fd = inet_listen(host_str, chr->filename + offset, 256 - offset,
2027 fd = inet_connect(host_str, SOCK_STREAM);
2033 if (!is_waitconnect)
2034 socket_set_nonblock(fd);
2039 s->is_unix = is_unix;
2040 s->do_nodelay = do_nodelay && !is_unix;
2043 chr->chr_write = tcp_chr_write;
2044 chr->chr_close = tcp_chr_close;
2048 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
2050 s->do_telnetopt = 1;
2054 socket_set_nodelay(fd);
2055 tcp_chr_connect(chr);
2058 if (is_listen && is_waitconnect) {
2059 printf("QEMU waiting for connection on: %s\n",
2060 chr->filename ? chr->filename : host_str);
2061 tcp_chr_accept(chr);
2062 socket_set_nonblock(s->listen_fd);
2074 static TAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs
2075 = TAILQ_HEAD_INITIALIZER(chardevs);
2077 CharDriverState *qemu_chr_open(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
2080 CharDriverState *chr;
2082 if (!strcmp(filename, "vc")) {
2083 chr = text_console_init(0);
2085 if (strstart(filename, "vc:", &p)) {
2086 chr = text_console_init(p);
2088 if (!strcmp(filename, "null")) {
2089 chr = qemu_chr_open_null();
2091 if (strstart(filename, "tcp:", &p)) {
2092 chr = qemu_chr_open_tcp(p, 0, 0);
2094 if (strstart(filename, "telnet:", &p)) {
2095 chr = qemu_chr_open_tcp(p, 1, 0);
2097 if (strstart(filename, "udp:", &p)) {
2098 chr = qemu_chr_open_udp(p);
2100 if (strstart(filename, "mon:", &p)) {
2101 chr = qemu_chr_open(label, p, NULL);
2103 chr = qemu_chr_open_mux(chr);
2104 monitor_init(chr, !nographic);
2106 printf("Unable to open driver: %s\n", p);
2110 if (strstart(filename, "unix:", &p)) {
2111 chr = qemu_chr_open_tcp(p, 0, 1);
2112 } else if (strstart(filename, "file:", &p)) {
2113 chr = qemu_chr_open_file_out(p);
2114 } else if (strstart(filename, "pipe:", &p)) {
2115 chr = qemu_chr_open_pipe(p);
2116 } else if (!strcmp(filename, "pty")) {
2117 chr = qemu_chr_open_pty();
2118 } else if (!strcmp(filename, "stdio")) {
2119 chr = qemu_chr_open_stdio();
2121 #if defined(__linux__)
2122 if (strstart(filename, "/dev/parport", NULL)) {
2123 chr = qemu_chr_open_pp(filename);
2125 #elif defined(__FreeBSD__)
2126 if (strstart(filename, "/dev/ppi", NULL)) {
2127 chr = qemu_chr_open_pp(filename);
2130 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2131 || defined(__NetBSD__) || defined(__OpenBSD__)
2132 if (strstart(filename, "/dev/", NULL)) {
2133 chr = qemu_chr_open_tty(filename);
2137 if (strstart(filename, "COM", NULL)) {
2138 chr = qemu_chr_open_win(filename);
2140 if (strstart(filename, "pipe:", &p)) {
2141 chr = qemu_chr_open_win_pipe(p);
2143 if (strstart(filename, "con:", NULL)) {
2144 chr = qemu_chr_open_win_con(filename);
2146 if (strstart(filename, "file:", &p)) {
2147 chr = qemu_chr_open_win_file_out(p);
2150 #ifdef CONFIG_BRLAPI
2151 if (!strcmp(filename, "braille")) {
2152 chr = chr_baum_init();
2161 chr->filename = qemu_strdup(filename);
2163 chr->label = qemu_strdup(label);
2164 TAILQ_INSERT_TAIL(&chardevs, chr, next);
2169 void qemu_chr_close(CharDriverState *chr)
2171 TAILQ_REMOVE(&chardevs, chr, next);
2173 chr->chr_close(chr);
2174 qemu_free(chr->filename);
2175 qemu_free(chr->label);
2179 void qemu_chr_info(void)
2181 CharDriverState *chr;
2183 TAILQ_FOREACH(chr, &chardevs, next) {
2184 term_printf("%s: filename=%s\n", chr->label, chr->filename);