1 1.2 pooka /* $NetBSD: tipsy.c,v 1.2 2010/02/18 16:14:55 pooka Exp $ */ 2 1.1 pooka 3 1.1 pooka /* 4 1.1 pooka * Copyright (c) 2009 Antti Kantee. All Rights Reserved. 5 1.1 pooka * 6 1.1 pooka * Redistribution and use in source and binary forms, with or without 7 1.1 pooka * modification, are permitted provided that the following conditions 8 1.1 pooka * are met: 9 1.1 pooka * 1. Redistributions of source code must retain the above copyright 10 1.1 pooka * notice, this list of conditions and the following disclaimer. 11 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright 12 1.1 pooka * notice, this list of conditions and the following disclaimer in the 13 1.1 pooka * documentation and/or other materials provided with the distribution. 14 1.1 pooka * 15 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 16 1.1 pooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 1.1 pooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 1.1 pooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 1.1 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 1.1 pooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 1.1 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 1.1 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 1.1 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 1.1 pooka * SUCH DAMAGE. 26 1.1 pooka */ 27 1.1 pooka 28 1.1 pooka /* 29 1.1 pooka * Experimental proof-of-concept program: 30 1.1 pooka * 31 1.1 pooka * tip-on-booze. Uses rump kernel for ucom driver + underlying 32 1.1 pooka * hardware. Just shovels bits back and forth between the user 33 1.1 pooka * terminal and the ucom device. 34 1.1 pooka * 35 1.1 pooka * Seems to drop an occasional character for output here and there. 36 1.1 pooka * Haven't pinpointed the problem yet. It happens commonly for certain 37 1.1 pooka * patterns such as ctrl-U followed by immediate typing. Tipsy is quite 38 1.1 pooka * sober despite this "feature". 39 1.1 pooka */ 40 1.1 pooka 41 1.1 pooka #include <sys/types.h> 42 1.1 pooka #include <sys/ioctl.h> 43 1.1 pooka 44 1.1 pooka #include <rump/rump.h> 45 1.1 pooka #include <rump/rump_syscalls.h> 46 1.1 pooka 47 1.1 pooka #include <err.h> 48 1.1 pooka #include <errno.h> 49 1.1 pooka #include <fcntl.h> 50 1.1 pooka #include <pthread.h> 51 1.1 pooka #include <stdio.h> 52 1.1 pooka #include <stdlib.h> 53 1.1 pooka #include <string.h> 54 1.1 pooka #include <unistd.h> 55 1.1 pooka #include <termios.h> 56 1.1 pooka 57 1.1 pooka /* 58 1.1 pooka * We use a shovel thread to get the bits from the rump kernel and 59 1.1 pooka * print them on the terminal. The reason for a thread instead of 60 1.1 pooka * polling is that we currently have no way to poll two fd's when 61 1.1 pooka * they are in different kernels (stdin in the host, ucom in rump). 62 1.1 pooka */ 63 1.1 pooka static void * 64 1.1 pooka shovel(void *arg) 65 1.1 pooka { 66 1.1 pooka char buf[64]; 67 1.1 pooka ssize_t n; 68 1.1 pooka int fd = (int)(intptr_t)arg; 69 1.1 pooka 70 1.1 pooka for (;;) { 71 1.1 pooka n = rump_sys_read(fd, buf, sizeof(buf)); 72 1.1 pooka if (__predict_false(n <= 0)) { 73 1.1 pooka if (n == 0) 74 1.1 pooka errx(1, "ucom EOF"); 75 1.1 pooka if (n == -1) 76 1.1 pooka err(1, "ucom read"); 77 1.1 pooka } 78 1.1 pooka if (write(STDOUT_FILENO, buf, n) != n) 79 1.1 pooka err(1, "write to console"); 80 1.1 pooka } 81 1.1 pooka } 82 1.1 pooka 83 1.1 pooka int 84 1.1 pooka main(int argc, char *argv[]) 85 1.1 pooka { 86 1.1 pooka pthread_t pt; 87 1.1 pooka struct termios tios; 88 1.1 pooka int probeonly = 0; 89 1.1 pooka int com; 90 1.1 pooka 91 1.1 pooka if (argc > 1) { 92 1.1 pooka if (argc == 2 && strcmp(argv[1], "probe") == 0) { 93 1.1 pooka probeonly = 1; 94 1.1 pooka } else { 95 1.1 pooka fprintf(stderr, "mind the usage\n"); 96 1.1 pooka exit(1); 97 1.1 pooka } 98 1.1 pooka } 99 1.1 pooka 100 1.1 pooka if (probeonly) 101 1.1 pooka rump_boot_sethowto(RUMP_AB_VERBOSE); 102 1.1 pooka rump_init(); 103 1.2 pooka if (probeonly) { 104 1.2 pooka pause(); 105 1.1 pooka exit(0); 106 1.2 pooka } 107 1.1 pooka 108 1.1 pooka com = rump_sys_open("/dev/dtyU0", O_RDWR); 109 1.1 pooka if (com == -1) 110 1.1 pooka err(1, "rump ucom open failed"); 111 1.1 pooka 112 1.1 pooka /* 113 1.1 pooka * Setup the com port. You might need to tweak this. 114 1.1 pooka */ 115 1.1 pooka if (rump_sys_ioctl(com, TIOCGETA, &tios) == -1) 116 1.1 pooka err(1, "rump get term"); 117 1.1 pooka tios.c_cflag &= ~(CSIZE|PARENB); 118 1.1 pooka tios.c_cflag |= CS8; 119 1.1 pooka tios.c_cflag |= CLOCAL; 120 1.1 pooka tios.c_iflag &= ~(ISTRIP|ICRNL); 121 1.1 pooka tios.c_oflag &= ~OPOST; 122 1.1 pooka tios.c_lflag &= ~(ICANON|ISIG|IEXTEN|ECHO); 123 1.1 pooka tios.c_cc[VMIN] = 1; 124 1.1 pooka tios.c_cc[VTIME] = 0; 125 1.1 pooka if (rump_sys_ioctl(com, TIOCSETA, &tios) == -1) 126 1.1 pooka err(1, "rump set term"); 127 1.1 pooka 128 1.1 pooka /* setup stdin */ 129 1.1 pooka if (tcgetattr(STDIN_FILENO, &tios) == -1) 130 1.1 pooka err(1, "host get term"); 131 1.1 pooka tios.c_oflag &= ~OPOST; 132 1.1 pooka tios.c_iflag &= ~(INPCK|ICRNL); 133 1.1 pooka tios.c_lflag &= ~(ICANON|IEXTEN|ECHO); 134 1.1 pooka tios.c_cc[VINTR] = tios.c_cc[VQUIT] = tios.c_cc[VSUSP] 135 1.1 pooka = tios.c_cc[VDSUSP] = tios.c_cc[VDISCARD] = tios.c_cc[VREPRINT] 136 1.1 pooka = tios.c_cc[VLNEXT] = _POSIX_VDISABLE; 137 1.1 pooka tios.c_cc[VMIN] = 1; 138 1.1 pooka tios.c_cc[VTIME] = 0; 139 1.1 pooka if (tcsetattr(STDIN_FILENO, TCSADRAIN, &tios) == -1) 140 1.1 pooka err(1, "host set term"); 141 1.1 pooka 142 1.1 pooka if (pthread_create(&pt, NULL, shovel, (void *)(intptr_t)com) == -1) 143 1.1 pooka err(1, "pthread create"); 144 1.1 pooka 145 1.1 pooka /* read stdin, feed that into the rump kernel */ 146 1.1 pooka for (;;) { 147 1.1 pooka char ch; 148 1.1 pooka 149 1.1 pooka ch = getchar(); 150 1.1 pooka if (rump_sys_write(com, &ch, 1) != 1) 151 1.1 pooka err(1, "rump write"); 152 1.1 pooka } 153 1.1 pooka } 154