1 /* $NetBSD: sunkbd.c,v 1.7 2001/11/13 06:54:32 lukem Exp $ */ 2 3 /* 4 * Copyright (c) 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This software was developed by the Computer Systems Engineering group 8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 9 * contributed to Berkeley. 10 * 11 * All advertising materials mentioning features or use of this software 12 * must display the following acknowledgement: 13 * This product includes software developed by the University of 14 * California, Lawrence Berkeley Laboratory. 15 * 16 * Redistribution and use in source and binary forms, with or without 17 * modification, are permitted provided that the following conditions 18 * are met: 19 * 1. Redistributions of source code must retain the above copyright 20 * notice, this list of conditions and the following disclaimer. 21 * 2. Redistributions in binary form must reproduce the above copyright 22 * notice, this list of conditions and the following disclaimer in the 23 * documentation and/or other materials provided with the distribution. 24 * 3. All advertising materials mentioning features or use of this software 25 * must display the following acknowledgement: 26 * This product includes software developed by the University of 27 * California, Berkeley and its contributors. 28 * 4. Neither the name of the University nor the names of its contributors 29 * may be used to endorse or promote products derived from this software 30 * without specific prior written permission. 31 * 32 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 35 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 42 * SUCH DAMAGE. 43 * 44 * @(#)kbd.c 8.2 (Berkeley) 10/30/93 45 */ 46 47 /* 48 * Keyboard driver (/dev/kbd -- note that we do not have minor numbers 49 * [yet?]). Translates incoming bytes to ASCII or to `firm_events' and 50 * passes them up to the appropriate reader. 51 */ 52 53 /* 54 * Keyboard interface line discipline. 55 * 56 */ 57 58 #include <sys/cdefs.h> 59 __KERNEL_RCSID(0, "$NetBSD: sunkbd.c,v 1.7 2001/11/13 06:54:32 lukem Exp $"); 60 61 #include <sys/param.h> 62 #include <sys/systm.h> 63 #include <sys/conf.h> 64 #include <sys/device.h> 65 #include <sys/kernel.h> 66 #include <sys/malloc.h> 67 #include <sys/proc.h> 68 #include <sys/signal.h> 69 #include <sys/signalvar.h> 70 #include <sys/time.h> 71 #include <sys/select.h> 72 #include <sys/syslog.h> 73 #include <sys/fcntl.h> 74 #include <sys/tty.h> 75 76 #include <dev/cons.h> 77 #include <machine/vuid_event.h> 78 #include <machine/kbd.h> 79 #include <dev/sun/event_var.h> 80 #include <dev/sun/kbd_xlate.h> 81 #include <dev/sun/kbdvar.h> 82 #include <dev/sun/kbd_ms_ttyvar.h> 83 84 #include "kbd.h" 85 #if NKBD > 0 86 87 /**************************************************************** 88 * Interface to the lower layer (ttycc) 89 ****************************************************************/ 90 91 static int sunkbd_match(struct device *, struct cfdata *, void *); 92 static void sunkbd_attach(struct device *, struct device *, void *); 93 static void sunkbd_write_data(struct kbd_softc *, int); 94 static int sunkbdiopen(struct device *, int mode); 95 96 int sunkbdinput(int, struct tty *); 97 int sunkbdstart(struct tty *); 98 99 100 struct cfattach kbd_ca = { 101 sizeof(struct kbd_softc), sunkbd_match, sunkbd_attach 102 }; 103 104 struct linesw sunkbd_disc = 105 { "sunkbd", 7, ttylopen, ttylclose, ttyerrio, ttyerrio, nullioctl, 106 sunkbdinput, sunkbdstart, nullmodem, ttpoll }; /* 7- SUNKBDDISC */ 107 108 /* 109 * sunkbd_match: how is this tty channel configured? 110 */ 111 int 112 sunkbd_match(parent, cf, aux) 113 struct device *parent; 114 struct cfdata *cf; 115 void *aux; 116 { 117 struct kbd_ms_tty_attach_args *args = aux; 118 119 if (strcmp(args->kmta_name, "keyboard") == 0) 120 return (1); 121 122 return 0; 123 } 124 125 void 126 sunkbd_attach(parent, self, aux) 127 struct device *parent, *self; 128 void *aux; 129 130 { 131 struct kbd_softc *k = (void *) self; 132 struct kbd_ms_tty_attach_args *args = aux; 133 struct cfdata *cf; 134 struct tty *tp = args->kmta_tp; 135 struct cons_channel *cc; 136 int kbd_unit; 137 138 /* Set up the proper line discipline. */ 139 ttyldisc_init(); 140 if (ttyldisc_add(&sunkbd_disc, -1) == -1) 141 panic("sunkbd_attach: sunkbd_disc"); 142 tp->t_linesw = &sunkbd_disc; 143 tp->t_oflag &= ~OPOST; 144 tp->t_dev = args->kmta_dev; 145 146 /* link the structures together. */ 147 k->k_priv = tp; 148 tp->t_sc = (void *)k; 149 150 cf = k->k_dev.dv_cfdata; 151 kbd_unit = k->k_dev.dv_unit; 152 153 k->k_deviopen = sunkbdiopen; 154 k->k_deviclose = NULL; 155 k->k_write_data = sunkbd_write_data; 156 157 if ((cc = malloc(sizeof *cc, M_DEVBUF, M_NOWAIT)) == NULL) 158 return; 159 160 cc->cc_dev = self; 161 cc->cc_iopen = kbd_cc_open; 162 cc->cc_iclose = kbd_cc_close; 163 cc->cc_upstream = NULL; 164 if (args->kmta_consdev) { 165 char magic[4]; 166 167 /* 168 * Hookup ourselves as the console input channel 169 */ 170 args->kmta_baud = KBD_BPS; 171 args->kmta_cflag = CLOCAL|CS8; 172 cons_attach_input(cc, args->kmta_consdev); 173 174 /* Tell our parent what the console should be. */ 175 args->kmta_consdev = cn_tab; 176 k->k_isconsole = 1; 177 printf(" (console input)"); 178 179 /* Set magic to "L1-A" */ 180 magic[0] = KBD_L1; 181 magic[1] = KBD_A; 182 magic[2] = 0; 183 cn_set_magic(magic); 184 } else { 185 extern void kd_attach_input(struct cons_channel *); 186 187 kd_attach_input(cc); 188 } 189 k->k_cc = cc; 190 191 printf("\n"); 192 193 callout_init(&k->k_repeat_ch); 194 195 /* Do this before any calls to kbd_rint(). */ 196 kbd_xlate_init(&k->k_state); 197 198 /* XXX - Do this in open? */ 199 k->k_repeat_start = hz/2; 200 k->k_repeat_step = hz/20; 201 202 /* Magic sequence. */ 203 k->k_magic1 = KBD_L1; 204 k->k_magic2 = KBD_A; 205 } 206 207 /* 208 * Internal open routine. This really should be inside com.c 209 * But I'm putting it here until we have a generic internal open 210 * mechanism. 211 */ 212 int 213 sunkbdiopen(dev, flags) 214 struct device *dev; 215 int flags; 216 { 217 struct kbd_softc *k = (void *) dev; 218 struct tty *tp = (struct tty *)k->k_priv; 219 struct proc *p = curproc; 220 struct termios t; 221 int maj; 222 int error; 223 224 maj = major(tp->t_dev); 225 if (p == NULL) 226 p = &proc0; 227 228 /* Open the lower device */ 229 if ((error = (*cdevsw[maj].d_open)(tp->t_dev, O_NONBLOCK|flags, 230 0/* ignored? */, p)) != 0) 231 return (error); 232 233 /* Now configure it for the console. */ 234 tp->t_ospeed = 0; 235 t.c_ispeed = KBD_BPS; 236 t.c_ospeed = KBD_BPS; 237 t.c_cflag = CLOCAL|CS8; 238 (*tp->t_param)(tp, &t); 239 240 return (0); 241 } 242 243 /* 244 * TTY interface to handle input. 245 */ 246 int 247 sunkbdinput(c, tp) 248 int c; 249 struct tty *tp; 250 { 251 struct kbd_softc *k = (void *) tp->t_sc; 252 u_char *cc; 253 int error; 254 255 256 cc = tp->t_cc; 257 258 /* 259 * Handle exceptional conditions (break, parity, framing). 260 */ 261 if ((error = ((c & TTY_ERRORMASK))) != 0) { 262 /* 263 * After garbage, flush pending input, and 264 * send a reset to resync key translation. 265 */ 266 log(LOG_ERR, "%s: input error (0x%x)\n", 267 k->k_dev.dv_xname, c); 268 c &= TTY_CHARMASK; 269 if (!k->k_txflags & K_TXBUSY) { 270 ttyflush(tp, FREAD | FWRITE); 271 goto send_reset; 272 } 273 } 274 275 /* 276 * Check for input buffer overflow 277 */ 278 if (tp->t_rawq.c_cc + tp->t_canq.c_cc >= TTYHOG) { 279 log(LOG_ERR, "%s: input overrun\n", 280 k->k_dev.dv_xname); 281 goto send_reset; 282 } 283 284 /* Pass this up to the "middle" layer. */ 285 kbd_input_raw(k, c); 286 return (-1); 287 288 send_reset: 289 /* Send a reset to resync translation. */ 290 kbd_output(k, KBD_CMD_RESET); 291 return (ttstart(tp)); 292 293 } 294 295 int 296 sunkbdstart(tp) 297 struct tty *tp; 298 { 299 struct kbd_softc *k = (void *) tp->t_sc; 300 301 /* 302 * Transmit done. Try to send more, or 303 * clear busy and wakeup drain waiters. 304 */ 305 k->k_txflags &= ~K_TXBUSY; 306 kbd_start_tx(k); 307 ttstart(tp); 308 return (0); 309 } 310 /* 311 * used by kbd_start_tx(); 312 */ 313 void 314 sunkbd_write_data(k, c) 315 struct kbd_softc *k; 316 int c; 317 { 318 struct tty *tp = (struct tty *)k->k_priv; 319 int s; 320 321 s = spltty(); 322 ttyoutput(c, tp); 323 ttstart(tp); 324 splx(s); 325 } 326 327 #endif 328