1 /* $NetBSD: arcbios_tty.c,v 1.19 2007/11/19 18:51:45 ad 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> 31 __KERNEL_RCSID(0, "$NetBSD: arcbios_tty.c,v 1.19 2007/11/19 18:51:45 ad Exp $"); 32 33 #include <sys/param.h> 34 #include <sys/user.h> 35 #include <sys/uio.h> 36 #include <sys/systm.h> 37 #include <sys/callout.h> 38 #include <sys/kernel.h> 39 #include <sys/conf.h> 40 #include <sys/proc.h> 41 #include <sys/tty.h> 42 #include <sys/termios.h> 43 #include <sys/kauth.h> 44 45 #include <dev/cons.h> 46 47 #include <dev/arcbios/arcbios.h> 48 #include <dev/arcbios/arcbiosvar.h> 49 50 callout_t arcbios_tty_ch; 51 bool arcbios_ch_init; 52 static struct tty *arcbios_tty[1]; 53 54 void arcbios_tty_start(struct tty *); 55 void arcbios_tty_poll(void *); 56 int arcbios_tty_param(struct tty *, struct termios *); 57 58 dev_type_open(arcbios_ttyopen); 59 dev_type_close(arcbios_ttyclose); 60 dev_type_read(arcbios_ttyread); 61 dev_type_write(arcbios_ttywrite); 62 dev_type_ioctl(arcbios_ttyioctl); 63 dev_type_stop(arcbios_ttystop); 64 dev_type_tty(arcbios_ttytty); 65 dev_type_poll(arcbios_ttypoll); 66 67 const struct cdevsw arcbios_cdevsw = { 68 arcbios_ttyopen, arcbios_ttyclose, arcbios_ttyread, arcbios_ttywrite, 69 arcbios_ttyioctl, arcbios_ttystop, arcbios_ttytty, arcbios_ttypoll, 70 nommap, ttykqfilter, D_TTY, 71 }; 72 73 int 74 arcbios_ttyopen(dev_t dev, int flag, int mode, struct lwp *l) 75 { 76 int unit = minor(dev); 77 struct tty *tp; 78 int s, error = 0, setuptimeout = 0; 79 80 if (!arcbios_ch_init) { 81 arcbios_ch_init = true; 82 callout_init(&arcbios_tty_ch, 0); 83 } 84 85 if (unit != 0) 86 return (ENODEV); 87 88 s = spltty(); 89 90 if (arcbios_tty[unit] == NULL) { 91 tp = arcbios_tty[unit] = ttymalloc(); 92 tty_attach(tp); 93 } else 94 tp = arcbios_tty[unit]; 95 96 tp->t_oproc = arcbios_tty_start; 97 tp->t_param = arcbios_tty_param; 98 tp->t_dev = dev; 99 100 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp)) { 101 splx(s); 102 return (EBUSY); 103 } 104 105 if ((tp->t_state & TS_ISOPEN) == 0) { 106 tp->t_state |= TS_CARR_ON; 107 ttychars(tp); 108 tp->t_iflag = TTYDEF_IFLAG; 109 tp->t_oflag = TTYDEF_OFLAG; 110 tp->t_cflag = TTYDEF_CFLAG | CLOCAL; 111 tp->t_lflag = TTYDEF_LFLAG; 112 tp->t_ispeed = tp->t_ospeed = 9600; 113 ttsetwater(tp); 114 115 setuptimeout = 1; 116 } 117 118 splx(s); 119 120 error = (*tp->t_linesw->l_open)(dev, tp); 121 if (error == 0 && setuptimeout) 122 callout_reset(&arcbios_tty_ch, 1, arcbios_tty_poll, tp); 123 124 return (error); 125 } 126 127 int 128 arcbios_ttyclose(dev_t dev, int flag, int mode, struct lwp *l) 129 { 130 int unit = minor(dev); 131 struct tty *tp = arcbios_tty[unit]; 132 133 callout_stop(&arcbios_tty_ch); 134 (*tp->t_linesw->l_close)(tp, flag); 135 ttyclose(tp); 136 return (0); 137 } 138 139 int 140 arcbios_ttyread(dev_t dev, struct uio *uio, int flag) 141 { 142 struct tty *tp = arcbios_tty[minor(dev)]; 143 144 return ((*tp->t_linesw->l_read)(tp, uio, flag)); 145 } 146 147 int 148 arcbios_ttywrite(dev_t dev, struct uio *uio, int flag) 149 { 150 struct tty *tp = arcbios_tty[minor(dev)]; 151 152 return ((*tp->t_linesw->l_write)(tp, uio, flag)); 153 } 154 155 int 156 arcbios_ttypoll(dev_t dev, int events, struct lwp *l) 157 { 158 struct tty *tp = arcbios_tty[minor(dev)]; 159 160 return ((*tp->t_linesw->l_poll)(tp, events, l)); 161 } 162 163 int 164 arcbios_ttyioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 165 { 166 int unit = minor(dev); 167 struct tty *tp = arcbios_tty[unit]; 168 int error; 169 170 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l); 171 if (error != EPASSTHROUGH) 172 return (error); 173 return (ttioctl(tp, cmd, data, flag, l)); 174 } 175 176 int 177 arcbios_tty_param(struct tty *tp, struct termios *t) 178 { 179 180 return (0); 181 } 182 183 void 184 arcbios_tty_start(struct tty *tp) 185 { 186 u_long count; 187 int s; 188 189 s = spltty(); 190 if (tp->t_state & (TS_TTSTOP | TS_BUSY)) 191 goto out; 192 ttypull(tp); 193 tp->t_state |= TS_BUSY; 194 while (tp->t_outq.c_cc != 0) { 195 (*ARCBIOS->Write)(ARCBIOS_STDOUT, tp->t_outq.c_cf, 196 ndqb(&tp->t_outq, 0), &count); 197 ndflush(&tp->t_outq, count); 198 } 199 tp->t_state &= ~TS_BUSY; 200 out: 201 splx(s); 202 } 203 204 void 205 arcbios_ttystop(struct tty *tp, int flag) 206 { 207 int s; 208 209 s = spltty(); 210 if (tp->t_state & TS_BUSY) 211 if ((tp->t_state & TS_TTSTOP) == 0) 212 tp->t_state |= TS_FLUSH; 213 splx(s); 214 } 215 216 static int 217 arcbios_tty_getchar(int *cp) 218 { 219 char c; 220 int32_t q; 221 u_long count; 222 223 q = ARCBIOS->GetReadStatus(ARCBIOS_STDIN); 224 225 if (q == 0) { 226 ARCBIOS->Read(ARCBIOS_STDIN, &c, 1, &count); 227 *cp = c; 228 229 return 1; 230 } 231 232 return 0; 233 } 234 235 void 236 arcbios_tty_poll(void *v) 237 { 238 struct tty *tp = v; 239 int c, l_r; 240 241 while (arcbios_tty_getchar(&c)) { 242 if (tp->t_state & TS_ISOPEN) 243 l_r = (*tp->t_linesw->l_rint)(c, tp); 244 } 245 callout_reset(&arcbios_tty_ch, 1, arcbios_tty_poll, tp); 246 } 247 248 struct tty * 249 arcbios_ttytty(dev_t dev) 250 { 251 252 if (minor(dev) != 0) 253 panic("arcbios_ttytty: bogus"); 254 255 return (arcbios_tty[0]); 256 } 257