1 /* $NetBSD: biconsdev.c,v 1.21 2014/03/16 05:20:27 dholland Exp $ */ 2 3 /*- 4 * Copyright (c) 1999-2001 5 * Shin Takemura and PocketBSD Project. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the PocketBSD project 18 * and its contributors. 19 * 4. Neither the name of the project nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 */ 36 /* 37 * Copyright (c) 1995 38 * The Regents of the University of California. All rights reserved. 39 * 40 * This code is derived from software contributed to Berkeley by 41 * Ted Lemon. 42 * 43 * Redistribution and use in source and binary forms, with or without 44 * modification, are permitted provided that the following conditions 45 * are met: 46 * 1. Redistributions of source code must retain the above copyright 47 * notice, this list of conditions and the following disclaimer. 48 * 2. Redistributions in binary form must reproduce the above copyright 49 * notice, this list of conditions and the following disclaimer in the 50 * documentation and/or other materials provided with the distribution. 51 * 3. Neither the name of the University nor the names of its contributors 52 * may be used to endorse or promote products derived from this software 53 * without specific prior written permission. 54 * 55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 65 * SUCH DAMAGE. 66 * 67 */ 68 69 #include <sys/cdefs.h> 70 __KERNEL_RCSID(0, "$NetBSD: biconsdev.c,v 1.21 2014/03/16 05:20:27 dholland Exp $"); 71 72 #include "biconsdev.h" 73 #include <sys/param.h> 74 #include <sys/proc.h> 75 #include <sys/systm.h> 76 #include <sys/buf.h> 77 #include <sys/ioctl.h> 78 #include <sys/tty.h> 79 #include <sys/conf.h> 80 #include <sys/kauth.h> 81 82 #include <dev/cons.h> 83 #include <dev/hpc/bicons.h> 84 #include <dev/hpc/biconsvar.h> 85 86 struct tty biconsdev_tty[NBICONSDEV]; 87 void biconsdevattach(int); 88 static void biconsdev_output(struct tty *); 89 90 dev_type_open(biconsdevopen); 91 dev_type_close(biconsdevclose); 92 dev_type_read(biconsdevread); 93 dev_type_write(biconsdevwrite); 94 dev_type_ioctl(biconsdevioctl); 95 dev_type_tty(biconsdevtty); 96 dev_type_poll(biconsdevpoll); 97 98 const struct cdevsw biconsdev_cdevsw = { 99 .d_open = biconsdevopen, 100 .d_close = biconsdevclose, 101 .d_read = biconsdevread, 102 .d_write = biconsdevwrite, 103 .d_ioctl = biconsdevioctl, 104 .d_stop = nostop, 105 .d_tty = biconsdevtty, 106 .d_poll = biconsdevpoll, 107 .d_mmap = nommap, 108 .d_kqfilter = ttykqfilter, 109 .d_flag = D_TTY 110 }; 111 112 void 113 biconsdevattach(int n) 114 { 115 struct tty *tp = &biconsdev_tty[0]; 116 int maj; 117 118 /* locate the major number */ 119 maj = cdevsw_lookup_major(&biconsdev_cdevsw); 120 121 /* Set up the tty queues now... */ 122 clalloc(&tp->t_rawq, 1024, 1); 123 clalloc(&tp->t_canq, 1024, 1); 124 /* output queue doesn't need quoting */ 125 clalloc(&tp->t_outq, 1024, 0); 126 /* Set default line discipline. */ 127 tp->t_linesw = ttyldisc_default(); 128 129 130 tp->t_dev = makedev(maj, 0); 131 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; 132 tp->t_param = (int (*)(struct tty *, struct termios *))nullop; 133 tp->t_winsize.ws_row = bicons_height; 134 tp->t_winsize.ws_col = bicons_width; 135 tp->t_winsize.ws_xpixel = bicons_xpixel; 136 tp->t_winsize.ws_ypixel = bicons_ypixel; 137 tp->t_oproc = biconsdev_output; 138 } 139 140 141 static void 142 biconsdev_output(struct tty *tp) 143 { 144 int s, n; 145 char buf[OBUFSIZ]; 146 147 s = spltty(); 148 if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) { 149 splx(s); 150 return; 151 } 152 tp->t_state |= TS_BUSY; 153 splx(s); 154 n = q_to_b(&tp->t_outq, buf, sizeof(buf)); 155 bicons_putn(buf, n); 156 157 s = spltty(); 158 tp->t_state &= ~TS_BUSY; 159 /* Come back if there's more to do */ 160 if (ttypull(tp)) { 161 tp->t_state |= TS_TIMEOUT; 162 callout_schedule(&tp->t_rstrt_ch, 1); 163 } 164 splx(s); 165 } 166 167 168 int 169 biconsdevopen(dev_t dev, int flag, int mode, struct lwp *l) 170 { 171 struct tty *tp = &biconsdev_tty[0]; 172 int status; 173 174 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp)) 175 return (EBUSY); 176 177 if ((tp->t_state & TS_ISOPEN) == 0) { 178 /* 179 * Leave baud rate alone! 180 */ 181 ttychars(tp); 182 tp->t_iflag = TTYDEF_IFLAG; 183 tp->t_oflag = TTYDEF_OFLAG; 184 tp->t_lflag = TTYDEF_LFLAG; 185 tp->t_cflag = TTYDEF_CFLAG; 186 tp->t_state = TS_ISOPEN | TS_CARR_ON; 187 (void)(*tp->t_param)(tp, &tp->t_termios); 188 ttsetwater(tp); 189 } 190 191 status = (*tp->t_linesw->l_open)(dev, tp); 192 return status; 193 } 194 195 196 int 197 biconsdevclose(dev_t dev, int flag, int mode, struct lwp *l) 198 { 199 struct tty *tp = &biconsdev_tty[0]; 200 201 (*tp->t_linesw->l_close)(tp, flag); 202 ttyclose(tp); 203 204 return (0); 205 } 206 207 208 int 209 biconsdevread(dev_t dev, struct uio *uio, int flag) 210 { 211 struct tty *tp = &biconsdev_tty[0]; 212 213 return ((*tp->t_linesw->l_read)(tp, uio, flag)); 214 } 215 216 217 int 218 biconsdevwrite(dev_t dev, struct uio *uio, int flag) 219 { 220 struct tty *tp = &biconsdev_tty[0]; 221 222 return ((*tp->t_linesw->l_write)(tp, uio, flag)); 223 } 224 225 226 int 227 biconsdevpoll(dev_t dev, int events, struct lwp *l) 228 { 229 struct tty *tp = &biconsdev_tty[0]; 230 231 return ((*tp->t_linesw->l_poll)(tp, events, l)); 232 } 233 234 235 struct tty * 236 biconsdevtty(dev_t dev) 237 { 238 struct tty *tp = &biconsdev_tty[0]; 239 240 return (tp); 241 } 242 243 int 244 biconsdevioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 245 { 246 struct tty *tp = &biconsdev_tty[0]; 247 int error; 248 249 if ((error = tp->t_linesw->l_ioctl(tp, cmd, data, flag, l)) != 250 EPASSTHROUGH) 251 return (error); 252 return (ttioctl(tp, cmd, data, flag, l)); 253 } 254