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.2 (Berkeley) 01/12/94 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 int cmd, flag; 105 caddr_t data; 106 struct proc *p; 107 { 108 int error; 109 110 if (cn_tab == NULL) 111 return (0); 112 /* 113 * Superuser can always use this to wrest control of console 114 * output from the "virtual" console. 115 */ 116 if (cmd == TIOCCONS && constty) { 117 error = suser(p->p_ucred, (u_short *) NULL); 118 if (error) 119 return (error); 120 constty = NULL; 121 return (0); 122 } 123 dev = cn_tab->cn_dev; 124 return ((*cdevsw[major(dev)].d_ioctl)(dev, cmd, data, flag, p)); 125 } 126 127 /*ARGSUSED*/ 128 cnselect(dev, rw, p) 129 dev_t dev; 130 int rw; 131 struct proc *p; 132 { 133 if (cn_tab == NULL) 134 return (1); 135 return (ttselect(cn_tab->cn_dev, rw, p)); 136 } 137 138 cngetc() 139 { 140 if (cn_tab == NULL) 141 return (0); 142 return ((*cn_tab->cn_getc)(cn_tab->cn_dev)); 143 } 144 145 cnputc(c) 146 register int c; 147 { 148 if (cn_tab == NULL) 149 return; 150 if (c) { 151 (*cn_tab->cn_putc)(cn_tab->cn_dev, c); 152 if (c == '\n') 153 (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r'); 154 } 155 } 156