1 /* 2 * Copyright (c) 1988 University of Utah. 3 * Copyright (c) 1990, 1993 4 * The Regents of the University of California. 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 8.3 (Berkeley) 01/09/95 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 <hp/dev/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 int flag; 84 { 85 if (cn_tab == NULL) 86 return (0); 87 dev = cn_tab->cn_dev; 88 return ((*cdevsw[major(dev)].d_read)(dev, uio, flag)); 89 } 90 91 cnwrite(dev, uio, flag) 92 dev_t dev; 93 struct uio *uio; 94 int flag; 95 { 96 if (cn_tab == NULL) 97 return (0); 98 dev = cn_tab->cn_dev; 99 return ((*cdevsw[major(dev)].d_write)(dev, uio, flag)); 100 } 101 102 cnioctl(dev, cmd, data, flag, p) 103 dev_t dev; 104 u_long cmd; 105 caddr_t data; 106 int flag; 107 struct proc *p; 108 { 109 int error; 110 111 if (cn_tab == NULL) 112 return (0); 113 /* 114 * Superuser can always use this to wrest control of console 115 * output from the "virtual" console. 116 */ 117 if (cmd == TIOCCONS && constty) { 118 error = suser(p->p_ucred, (u_short *) NULL); 119 if (error) 120 return (error); 121 constty = NULL; 122 return (0); 123 } 124 dev = cn_tab->cn_dev; 125 return ((*cdevsw[major(dev)].d_ioctl)(dev, cmd, data, flag, p)); 126 } 127 128 /*ARGSUSED*/ 129 cnselect(dev, rw, p) 130 dev_t dev; 131 int rw; 132 struct proc *p; 133 { 134 if (cn_tab == NULL) 135 return (1); 136 return (ttselect(cn_tab->cn_dev, rw, p)); 137 } 138 139 cngetc() 140 { 141 if (cn_tab == NULL) 142 return (0); 143 return ((*cn_tab->cn_getc)(cn_tab->cn_dev)); 144 } 145 146 cnputc(c) 147 register int c; 148 { 149 if (cn_tab == NULL) 150 return; 151 if (c) { 152 (*cn_tab->cn_putc)(cn_tab->cn_dev, c); 153 if (c == '\n') 154 (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r'); 155 } 156 } 157