1 /* $NetBSD: sunkbd.c,v 1.26 2007/11/10 18:00:11 ad 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. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * @(#)kbd.c 8.2 (Berkeley) 10/30/93 41 */ 42 43 /* 44 * /dev/kbd lower layer for sun keyboard off a tty (line discipline). 45 * This driver uses kbdsun middle layer to hook up to /dev/kbd. 46 */ 47 48 /* 49 * Keyboard interface line discipline. 50 * 51 */ 52 53 #include <sys/cdefs.h> 54 __KERNEL_RCSID(0, "$NetBSD: sunkbd.c,v 1.26 2007/11/10 18:00:11 ad Exp $"); 55 56 #include <sys/param.h> 57 #include <sys/systm.h> 58 #include <sys/conf.h> 59 #include <sys/device.h> 60 #include <sys/kernel.h> 61 #include <sys/malloc.h> 62 #include <sys/proc.h> 63 #include <sys/signal.h> 64 #include <sys/signalvar.h> 65 #include <sys/time.h> 66 #include <sys/select.h> 67 #include <sys/syslog.h> 68 #include <sys/fcntl.h> 69 #include <sys/tty.h> 70 71 #include <dev/cons.h> 72 #include <machine/vuid_event.h> 73 #include <machine/kbd.h> 74 #include <dev/sun/event_var.h> 75 #include <dev/sun/kbd_xlate.h> 76 #include <dev/sun/kbdvar.h> 77 #include <dev/sun/kbdsunvar.h> 78 #include <dev/sun/kbd_ms_ttyvar.h> 79 80 /**************************************************************** 81 * Interface to the lower layer (ttycc) 82 ****************************************************************/ 83 84 static int sunkbd_match(struct device *, struct cfdata *, void *); 85 static void sunkbd_attach(struct device *, struct device *, void *); 86 static void sunkbd_write_data(struct kbd_sun_softc *, int); 87 static int sunkbdiopen(struct device *, int mode); 88 89 #if NWSKBD > 0 90 void kbd_wskbd_attach(struct kbd_softc *k, int isconsole); 91 #endif 92 93 int sunkbdinput(int, struct tty *); 94 int sunkbdstart(struct tty *); 95 96 /* Default keyboard baud rate */ 97 int sunkbd_bps = KBD_DEFAULT_BPS; 98 99 CFATTACH_DECL(kbd_tty, sizeof(struct kbd_sun_softc), 100 sunkbd_match, sunkbd_attach, NULL, NULL); 101 102 struct linesw sunkbd_disc = { 103 .l_name = "sunkbd", 104 .l_open = ttylopen, 105 .l_close = ttylclose, 106 .l_read = ttyerrio, 107 .l_write = ttyerrio, 108 .l_ioctl = ttynullioctl, 109 .l_rint = sunkbdinput, 110 .l_start = sunkbdstart, 111 .l_modem = nullmodem, 112 .l_poll = ttpoll 113 }; 114 115 116 /* 117 * sunkbd_match: how is this tty channel configured? 118 */ 119 int 120 sunkbd_match(parent, cf, aux) 121 struct device *parent; 122 struct cfdata *cf; 123 void *aux; 124 { 125 struct kbd_ms_tty_attach_args *args = aux; 126 127 if (strcmp(args->kmta_name, "keyboard") == 0) 128 return (1); 129 130 return 0; 131 } 132 133 void 134 sunkbd_attach(parent, self, aux) 135 struct device *parent, *self; 136 void *aux; 137 { 138 struct kbd_sun_softc *k = device_private(self); 139 struct kbd_ms_tty_attach_args *args = aux; 140 struct tty *tp = args->kmta_tp; 141 struct cons_channel *cc; 142 143 /* Set up the proper line discipline. */ 144 if (ttyldisc_attach(&sunkbd_disc) != 0) 145 panic("sunkbd_attach: sunkbd_disc"); 146 ttyldisc_release(tp->t_linesw); 147 tp->t_linesw = ttyldisc_lookup(sunkbd_disc.l_name); 148 KASSERT(tp->t_linesw == &sunkbd_disc); 149 tp->t_oflag &= ~OPOST; 150 tp->t_dev = args->kmta_dev; 151 152 /* link the structures together. */ 153 k->k_priv = tp; 154 tp->t_sc = (void *)k; 155 156 /* provide our middle layer with a link to the lower layer (i.e. us) */ 157 k->k_deviopen = sunkbdiopen; 158 k->k_deviclose = NULL; 159 k->k_write_data = sunkbd_write_data; 160 161 /* provide upper layer with a link to our middle layer */ 162 k->k_kbd.k_ops = &kbd_ops_sun; 163 164 /* alloc console input channel */ 165 if ((cc = kbd_cc_alloc(&k->k_kbd)) == NULL) 166 return; 167 168 if (args->kmta_consdev) { 169 char magic[4]; 170 171 /* 172 * Hookup ourselves as the console input channel 173 */ 174 args->kmta_baud = sunkbd_bps; 175 args->kmta_cflag = CLOCAL|CS8; 176 cons_attach_input(cc, args->kmta_consdev); 177 178 /* Tell our parent what the console should be. */ 179 args->kmta_consdev = cn_tab; 180 k->k_kbd.k_isconsole = 1; 181 printf(" (console input)"); 182 183 /* Set magic to "L1-A" */ 184 magic[0] = KBD_L1; 185 magic[1] = KBD_A; 186 magic[2] = 0; 187 cn_set_magic(magic); 188 } else { 189 extern void kd_attach_input(struct cons_channel *); 190 191 kd_attach_input(cc); 192 } 193 194 195 printf("\n"); 196 197 #if NWSKBD > 0 198 kbd_wskbd_attach(&k->k_kbd, args->kmta_consdev != NULL); 199 #endif 200 201 /* Do this before any calls to kbd_rint(). */ 202 kbd_xlate_init(&k->k_kbd.k_state); 203 204 /* Magic sequence. */ 205 k->k_magic1 = KBD_L1; 206 k->k_magic2 = KBD_A; 207 } 208 209 /* 210 * Internal open routine. This really should be inside com.c 211 * But I'm putting it here until we have a generic internal open 212 * mechanism. 213 */ 214 int 215 sunkbdiopen(dev, flags) 216 struct device *dev; 217 int flags; 218 { 219 struct kbd_sun_softc *k = (void *) dev; 220 struct tty *tp = (struct tty *)k->k_priv; 221 struct lwp *l = curlwp ? curlwp : &lwp0; 222 struct termios t; 223 int error; 224 225 /* Open the lower device */ 226 if ((error = cdev_open(tp->t_dev, O_NONBLOCK|flags, 227 0/* ignored? */, l)) != 0) 228 return (error); 229 230 /* Now configure it for the console. */ 231 tp->t_ospeed = 0; 232 t.c_ispeed = sunkbd_bps; 233 t.c_ospeed = sunkbd_bps; 234 t.c_cflag = CLOCAL|CS8; 235 (*tp->t_param)(tp, &t); 236 237 return (0); 238 } 239 240 /* 241 * TTY interface to handle input. 242 */ 243 int 244 sunkbdinput(c, tp) 245 int c; 246 struct tty *tp; 247 { 248 struct kbd_sun_softc *k = (void *) tp->t_sc; 249 u_char *cc; 250 int error; 251 252 253 cc = tp->t_cc; 254 255 /* 256 * Handle exceptional conditions (break, parity, framing). 257 */ 258 if ((error = ((c & TTY_ERRORMASK))) != 0) { 259 /* 260 * After garbage, flush pending input, and 261 * send a reset to resync key translation. 262 */ 263 log(LOG_ERR, "%s: input error (0x%x)\n", 264 k->k_kbd.k_dev.dv_xname, c); 265 c &= TTY_CHARMASK; 266 if (!k->k_txflags & K_TXBUSY) { 267 ttyflush(tp, FREAD | FWRITE); 268 goto send_reset; 269 } 270 } 271 272 /* 273 * Check for input buffer overflow 274 */ 275 if (tp->t_rawq.c_cc + tp->t_canq.c_cc >= TTYHOG) { 276 log(LOG_ERR, "%s: input overrun\n", 277 k->k_kbd.k_dev.dv_xname); 278 goto send_reset; 279 } 280 281 /* Pass this up to the "middle" layer. */ 282 return(kbd_sun_input(k, c)); 283 284 send_reset: 285 /* Send a reset to resync translation. */ 286 kbd_sun_output(k, KBD_CMD_RESET); 287 return (ttstart(tp)); 288 289 } 290 291 int 292 sunkbdstart(tp) 293 struct tty *tp; 294 { 295 struct kbd_sun_softc *k = (void *) tp->t_sc; 296 297 /* 298 * Transmit done. Try to send more, or 299 * clear busy and wakeup drain waiters. 300 */ 301 k->k_txflags &= ~K_TXBUSY; 302 kbd_sun_start_tx(k); 303 ttstart(tp); 304 return (0); 305 } 306 /* 307 * used by kbd_sun_start_tx(); 308 */ 309 void 310 sunkbd_write_data(k, c) 311 struct kbd_sun_softc *k; 312 int c; 313 { 314 struct tty *tp = (struct tty *)k->k_priv; 315 316 mutex_spin_enter(&tty_lock); 317 ttyoutput(c, tp); 318 ttstart(tp); 319 mutex_spin_exit(&tty_lock); 320 } 321