1 /* $NetBSD: promcons.c,v 1.21 2002/10/23 09:10:29 jdolecek 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.21 2002/10/23 09:10:29 jdolecek 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/user.h> 41 #include <sys/file.h> 42 #include <sys/uio.h> 43 #include <sys/kernel.h> 44 #include <sys/syslog.h> 45 #include <sys/types.h> 46 #include <sys/device.h> 47 #include <sys/conf.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 = CALLOUT_INITIALIZER; 80 81 int 82 promopen(dev_t dev, int flag, int mode, struct proc *p) 83 { 84 int unit = minor(dev); 85 struct tty *tp; 86 int s; 87 int error = 0, setuptimeout = 0; 88 89 if (!pmap_uses_prom_console() || unit >= 1) 90 return ENXIO; 91 92 s = spltty(); 93 94 if (!prom_tty[unit]) { 95 tp = prom_tty[unit] = ttymalloc(); 96 tty_attach(tp); 97 } else 98 tp = prom_tty[unit]; 99 100 tp->t_oproc = promstart; 101 tp->t_param = promparam; 102 tp->t_dev = dev; 103 if ((tp->t_state & TS_ISOPEN) == 0) { 104 tp->t_state |= TS_CARR_ON; 105 ttychars(tp); 106 tp->t_iflag = TTYDEF_IFLAG; 107 tp->t_oflag = TTYDEF_OFLAG; 108 tp->t_cflag = TTYDEF_CFLAG|CLOCAL; 109 tp->t_lflag = TTYDEF_LFLAG; 110 tp->t_ispeed = tp->t_ospeed = 9600; 111 ttsetwater(tp); 112 113 setuptimeout = 1; 114 } else if (tp->t_state&TS_XCLUDE && p->p_ucred->cr_uid != 0) { 115 splx(s); 116 return EBUSY; 117 } 118 119 splx(s); 120 121 error = (*tp->t_linesw->l_open)(dev, tp); 122 if (error == 0 && setuptimeout) { 123 polltime = hz / PROM_POLL_HZ; 124 if (polltime < 1) 125 polltime = 1; 126 callout_reset(&prom_ch, polltime, promtimeout, tp); 127 } 128 return error; 129 } 130 131 int 132 promclose(dev_t dev, int flag, int mode, struct proc *p) 133 { 134 int unit = minor(dev); 135 struct tty *tp = prom_tty[unit]; 136 137 callout_stop(&prom_ch); 138 (*tp->t_linesw->l_close)(tp, flag); 139 ttyclose(tp); 140 return 0; 141 } 142 143 int 144 promread(dev_t dev, struct uio *uio, int flag) 145 { 146 struct tty *tp = prom_tty[minor(dev)]; 147 148 return ((*tp->t_linesw->l_read)(tp, uio, flag)); 149 } 150 151 int 152 promwrite(dev_t dev, struct uio *uio, int flag) 153 { 154 struct tty *tp = prom_tty[minor(dev)]; 155 156 return ((*tp->t_linesw->l_write)(tp, uio, flag)); 157 } 158 159 int 160 prompoll(dev, events, p) 161 dev_t dev; 162 int events; 163 struct proc *p; 164 { 165 struct tty *tp = prom_tty[minor(dev)]; 166 167 return ((*tp->t_linesw->l_poll)(tp, events, p)); 168 } 169 170 int 171 promioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p) 172 { 173 int unit = minor(dev); 174 struct tty *tp = prom_tty[unit]; 175 int error; 176 177 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p); 178 if (error != EPASSTHROUGH) 179 return error; 180 return ttioctl(tp, cmd, data, flag, p); 181 } 182 183 int 184 promparam(struct tty *tp, struct termios *t) 185 { 186 187 return 0; 188 } 189 190 void 191 promstart(struct tty *tp) 192 { 193 int s; 194 195 s = spltty(); 196 if (tp->t_state & (TS_TTSTOP | TS_BUSY)) 197 goto out; 198 if (tp->t_outq.c_cc <= tp->t_lowat) { 199 if (tp->t_state & TS_ASLEEP) { 200 tp->t_state &= ~TS_ASLEEP; 201 wakeup((caddr_t)&tp->t_outq); 202 } 203 selwakeup(&tp->t_wsel); 204 } 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