1 /* $NetBSD: promcons.c,v 1.35 2009/11/21 05:35:40 rmind Exp $ */ 2 3 /* 4 * Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University. 5 * All rights reserved. 6 * 7 * Author: Chris G. Demetriou 8 * 9 * Permission to use, copy, modify and distribute this software and 10 * its documentation is hereby granted, provided that both the copyright 11 * notice and this permission notice appear in all copies of the 12 * software, derivative works or modified versions, and any portions 13 * thereof, and that both notices appear in supporting documentation. 14 * 15 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 16 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 17 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 18 * 19 * Carnegie Mellon requests users of this software to return to 20 * 21 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 22 * School of Computer Science 23 * Carnegie Mellon University 24 * Pittsburgh PA 15213-3890 25 * 26 * any improvements or extensions that they make and grant Carnegie the 27 * rights to redistribute these changes. 28 */ 29 30 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */ 31 32 __KERNEL_RCSID(0, "$NetBSD: promcons.c,v 1.35 2009/11/21 05:35:40 rmind Exp $"); 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/ioctl.h> 37 #include <sys/select.h> 38 #include <sys/tty.h> 39 #include <sys/proc.h> 40 #include <sys/file.h> 41 #include <sys/uio.h> 42 #include <sys/kernel.h> 43 #include <sys/syslog.h> 44 #include <sys/types.h> 45 #include <sys/device.h> 46 #include <sys/conf.h> 47 #include <sys/kauth.h> 48 49 #include <uvm/uvm_extern.h> 50 51 #include <machine/cpuconf.h> 52 #include <machine/prom.h> 53 54 #ifdef _PMAP_MAY_USE_PROM_CONSOLE 55 56 dev_type_open(promopen); 57 dev_type_close(promclose); 58 dev_type_read(promread); 59 dev_type_write(promwrite); 60 dev_type_ioctl(promioctl); 61 dev_type_stop(promstop); 62 dev_type_tty(promtty); 63 dev_type_poll(prompoll); 64 65 const struct cdevsw prom_cdevsw = { 66 promopen, promclose, promread, promwrite, promioctl, 67 promstop, promtty, prompoll, nommap, ttykqfilter, D_TTY 68 }; 69 70 #define PROM_POLL_HZ 50 71 72 static struct tty *prom_tty[1]; 73 static int polltime; 74 75 void promstart(struct tty *); 76 void promtimeout(void *); 77 int promparam(struct tty *, struct termios *); 78 79 struct callout prom_ch; 80 81 int 82 promopen(dev_t dev, int flag, int mode, struct lwp *l) 83 { 84 int unit = minor(dev); 85 struct tty *tp; 86 int s; 87 int error = 0, setuptimeout = 0; 88 static bool callo; 89 90 if (!callo) { 91 callout_init(&prom_ch, 0); 92 callo = true; 93 } 94 95 if (!pmap_uses_prom_console() || unit >= 1) 96 return ENXIO; 97 98 s = spltty(); 99 100 if (!prom_tty[unit]) { 101 tp = prom_tty[unit] = ttymalloc(); 102 tty_attach(tp); 103 } else 104 tp = prom_tty[unit]; 105 106 tp->t_oproc = promstart; 107 tp->t_param = promparam; 108 tp->t_dev = dev; 109 110 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp)) { 111 splx(s); 112 return (EBUSY); 113 } 114 115 if ((tp->t_state & TS_ISOPEN) == 0) { 116 tp->t_state |= TS_CARR_ON; 117 ttychars(tp); 118 tp->t_iflag = TTYDEF_IFLAG; 119 tp->t_oflag = TTYDEF_OFLAG; 120 tp->t_cflag = TTYDEF_CFLAG|CLOCAL; 121 tp->t_lflag = TTYDEF_LFLAG; 122 tp->t_ispeed = tp->t_ospeed = 9600; 123 ttsetwater(tp); 124 125 setuptimeout = 1; 126 } 127 128 splx(s); 129 130 error = (*tp->t_linesw->l_open)(dev, tp); 131 if (error == 0 && setuptimeout) { 132 polltime = hz / PROM_POLL_HZ; 133 if (polltime < 1) 134 polltime = 1; 135 callout_reset(&prom_ch, polltime, promtimeout, tp); 136 } 137 return error; 138 } 139 140 int 141 promclose(dev_t dev, int flag, int mode, struct lwp *l) 142 { 143 int unit = minor(dev); 144 struct tty *tp = prom_tty[unit]; 145 146 callout_stop(&prom_ch); 147 (*tp->t_linesw->l_close)(tp, flag); 148 ttyclose(tp); 149 return 0; 150 } 151 152 int 153 promread(dev_t dev, struct uio *uio, int flag) 154 { 155 struct tty *tp = prom_tty[minor(dev)]; 156 157 return ((*tp->t_linesw->l_read)(tp, uio, flag)); 158 } 159 160 int 161 promwrite(dev_t dev, struct uio *uio, int flag) 162 { 163 struct tty *tp = prom_tty[minor(dev)]; 164 165 return ((*tp->t_linesw->l_write)(tp, uio, flag)); 166 } 167 168 int 169 prompoll(dev_t dev, int events, struct lwp *l) 170 { 171 struct tty *tp = prom_tty[minor(dev)]; 172 173 return ((*tp->t_linesw->l_poll)(tp, events, l)); 174 } 175 176 int 177 promioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 178 { 179 int unit = minor(dev); 180 struct tty *tp = prom_tty[unit]; 181 int error; 182 183 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l); 184 if (error != EPASSTHROUGH) 185 return error; 186 return ttioctl(tp, cmd, data, flag, l); 187 } 188 189 int 190 promparam(struct tty *tp, struct termios *t) 191 { 192 193 return 0; 194 } 195 196 void 197 promstart(struct tty *tp) 198 { 199 int s; 200 201 s = spltty(); 202 if (tp->t_state & (TS_TTSTOP | TS_BUSY)) 203 goto out; 204 ttypull(tp); 205 tp->t_state |= TS_BUSY; 206 while (tp->t_outq.c_cc != 0) 207 promcnputc(tp->t_dev, getc(&tp->t_outq)); 208 tp->t_state &= ~TS_BUSY; 209 out: 210 splx(s); 211 } 212 213 /* 214 * Stop output on a line. 215 */ 216 void 217 promstop(struct tty *tp, int flag) 218 { 219 int s; 220 221 s = spltty(); 222 if (tp->t_state & TS_BUSY) 223 if ((tp->t_state & TS_TTSTOP) == 0) 224 tp->t_state |= TS_FLUSH; 225 splx(s); 226 } 227 228 void 229 promtimeout(void *v) 230 { 231 struct tty *tp = v; 232 u_char c; 233 234 while (promcnlookc(tp->t_dev, &c)) { 235 if (tp->t_state & TS_ISOPEN) 236 (*tp->t_linesw->l_rint)(c, tp); 237 } 238 callout_reset(&prom_ch, polltime, promtimeout, tp); 239 } 240 241 struct tty * 242 promtty(dev_t dev) 243 { 244 245 if (minor(dev) != 0) 246 panic("promtty: bogus"); 247 248 return prom_tty[0]; 249 } 250 251 #else /* _PMAP_MAY_USE_PROM_CONSOLE */ 252 253 /* 254 * If not defined _PMAP_MAY_USE_PROM_CONSOLE, 255 * this fake prom_cdevsw is attached to the kernel. 256 * NEVER REMOVE! 257 */ 258 const struct cdevsw prom_cdevsw = { 259 noopen, noclose, noread, nowrite, noioctl, 260 nostop, notty, nopoll, nommap, 261 }; 262 263 #endif /* _PMAP_MAY_USE_PROM_CONSOLE */ 264