1 /* 2 * Copyright (c) 1988 University of Utah. 3 * Copyright (c) 1990 The Regents of the University of California. 4 * All rights reserved. 5 * 6 * This code is derived from software contributed to Berkeley by 7 * the Systems Programming Group of the University of Utah Computer 8 * Science Department. 9 * 10 * %sccs.include.redist.c% 11 * 12 * from: Utah $Hdr: cons.c 1.7 92/01/21$ 13 * 14 * @(#)cons.c 7.7 (Berkeley) 06/05/92 15 */ 16 17 #include "sys/param.h" 18 #include "sys/proc.h" 19 #include "sys/systm.h" 20 #include "sys/buf.h" 21 #include "sys/ioctl.h" 22 #include "sys/tty.h" 23 #include "sys/file.h" 24 #include "sys/conf.h" 25 26 #include "cons.h" 27 28 struct tty *constty; /* virtual console output device */ 29 struct consdev *cn_tab; /* physical console device info */ 30 struct tty *cn_tty; /* XXX: console tty struct for tprintf */ 31 32 cninit() 33 { 34 register struct consdev *cp; 35 36 /* 37 * Collect information about all possible consoles 38 * and find the one with highest priority 39 */ 40 for (cp = constab; cp->cn_probe; cp++) { 41 (*cp->cn_probe)(cp); 42 if (cp->cn_pri > CN_DEAD && 43 (cn_tab == NULL || cp->cn_pri > cn_tab->cn_pri)) 44 cn_tab = cp; 45 } 46 /* 47 * No console, we can handle it 48 */ 49 if ((cp = cn_tab) == NULL) 50 return; 51 /* 52 * Turn on console 53 */ 54 cn_tty = cp->cn_tp; 55 (*cp->cn_init)(cp); 56 } 57 58 cnopen(dev, flag, mode, p) 59 dev_t dev; 60 int flag, mode; 61 struct proc *p; 62 { 63 if (cn_tab == NULL) 64 return (0); 65 dev = cn_tab->cn_dev; 66 return ((*cdevsw[major(dev)].d_open)(dev, flag, mode, p)); 67 } 68 69 cnclose(dev, flag, mode, p) 70 dev_t dev; 71 int flag, mode; 72 struct proc *p; 73 { 74 if (cn_tab == NULL) 75 return (0); 76 dev = cn_tab->cn_dev; 77 return ((*cdevsw[major(dev)].d_close)(dev, flag, mode, p)); 78 } 79 80 cnread(dev, uio, flag) 81 dev_t dev; 82 struct uio *uio; 83 { 84 if (cn_tab == NULL) 85 return (0); 86 dev = cn_tab->cn_dev; 87 return ((*cdevsw[major(dev)].d_read)(dev, uio, flag)); 88 } 89 90 cnwrite(dev, uio, flag) 91 dev_t dev; 92 struct uio *uio; 93 { 94 if (cn_tab == NULL) 95 return (0); 96 dev = cn_tab->cn_dev; 97 return ((*cdevsw[major(dev)].d_write)(dev, uio, flag)); 98 } 99 100 cnioctl(dev, cmd, data, flag, p) 101 dev_t dev; 102 caddr_t data; 103 struct proc *p; 104 { 105 int error; 106 107 if (cn_tab == NULL) 108 return (0); 109 /* 110 * Superuser can always use this to wrest control of console 111 * output from the "virtual" console. 112 */ 113 if (cmd == TIOCCONS && constty) { 114 error = suser(p->p_ucred, (u_short *) NULL); 115 if (error) 116 return (error); 117 constty = NULL; 118 return (0); 119 } 120 dev = cn_tab->cn_dev; 121 return ((*cdevsw[major(dev)].d_ioctl)(dev, cmd, data, flag, p)); 122 } 123 124 /*ARGSUSED*/ 125 cnselect(dev, rw, p) 126 dev_t dev; 127 int rw; 128 struct proc *p; 129 { 130 if (cn_tab == NULL) 131 return (1); 132 return (ttselect(cn_tab->cn_dev, rw, p)); 133 } 134 135 cngetc() 136 { 137 if (cn_tab == NULL) 138 return (0); 139 return ((*cn_tab->cn_getc)(cn_tab->cn_dev)); 140 } 141 142 cnputc(c) 143 register int c; 144 { 145 if (cn_tab == NULL) 146 return; 147 if (c) { 148 (*cn_tab->cn_putc)(cn_tab->cn_dev, c); 149 if (c == '\n') 150 (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r'); 151 } 152 } 153