sunkbd.c revision 1.5.2.4 1 /* $NetBSD: sunkbd.c,v 1.5.2.4 2002/01/08 00:31:59 nathanw 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.5.2.4 2002/01/08 00:31:59 nathanw 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/lwp.h>
69 #include <sys/signal.h>
70 #include <sys/signalvar.h>
71 #include <sys/time.h>
72 #include <sys/select.h>
73 #include <sys/syslog.h>
74 #include <sys/fcntl.h>
75 #include <sys/tty.h>
76
77 #include <dev/cons.h>
78 #include <machine/vuid_event.h>
79 #include <machine/kbd.h>
80 #include <dev/sun/event_var.h>
81 #include <dev/sun/kbd_xlate.h>
82 #include <dev/sun/kbdvar.h>
83 #include <dev/sun/kbd_ms_ttyvar.h>
84
85 #include "kbd.h"
86 #if NKBD > 0
87
88 /****************************************************************
89 * Interface to the lower layer (ttycc)
90 ****************************************************************/
91
92 static int sunkbd_match(struct device *, struct cfdata *, void *);
93 static void sunkbd_attach(struct device *, struct device *, void *);
94 static void sunkbd_write_data(struct kbd_softc *, int);
95 static int sunkbdiopen(struct device *, int mode);
96
97 int sunkbdinput(int, struct tty *);
98 int sunkbdstart(struct tty *);
99
100 /* Default keyboard baud rate */
101 int sunkbd_bps = KBD_DEFAULT_BPS;
102
103 struct cfattach kbd_ca = {
104 sizeof(struct kbd_softc), sunkbd_match, sunkbd_attach
105 };
106
107 struct linesw sunkbd_disc =
108 { "sunkbd", 7, ttylopen, ttylclose, ttyerrio, ttyerrio, nullioctl,
109 sunkbdinput, sunkbdstart, nullmodem, ttpoll }; /* 7- SUNKBDDISC */
110
111 /*
112 * sunkbd_match: how is this tty channel configured?
113 */
114 int
115 sunkbd_match(parent, cf, aux)
116 struct device *parent;
117 struct cfdata *cf;
118 void *aux;
119 {
120 struct kbd_ms_tty_attach_args *args = aux;
121
122 if (strcmp(args->kmta_name, "keyboard") == 0)
123 return (1);
124
125 return 0;
126 }
127
128 void
129 sunkbd_attach(parent, self, aux)
130 struct device *parent, *self;
131 void *aux;
132
133 {
134 struct kbd_softc *k = (void *) self;
135 struct kbd_ms_tty_attach_args *args = aux;
136 struct cfdata *cf;
137 struct tty *tp = args->kmta_tp;
138 struct cons_channel *cc;
139 int kbd_unit;
140
141 /* Set up the proper line discipline. */
142 ttyldisc_init();
143 if (ttyldisc_add(&sunkbd_disc, -1) == -1)
144 panic("sunkbd_attach: sunkbd_disc");
145 tp->t_linesw = &sunkbd_disc;
146 tp->t_oflag &= ~OPOST;
147 tp->t_dev = args->kmta_dev;
148
149 /* link the structures together. */
150 k->k_priv = tp;
151 tp->t_sc = (void *)k;
152
153 cf = k->k_dev.dv_cfdata;
154 kbd_unit = k->k_dev.dv_unit;
155
156 k->k_deviopen = sunkbdiopen;
157 k->k_deviclose = NULL;
158 k->k_write_data = sunkbd_write_data;
159
160 if ((cc = malloc(sizeof *cc, M_DEVBUF, M_NOWAIT)) == NULL)
161 return;
162
163 cc->cc_dev = self;
164 cc->cc_iopen = kbd_cc_open;
165 cc->cc_iclose = kbd_cc_close;
166 cc->cc_upstream = NULL;
167 if (args->kmta_consdev) {
168 char magic[4];
169
170 /*
171 * Hookup ourselves as the console input channel
172 */
173 args->kmta_baud = sunkbd_bps;
174 args->kmta_cflag = CLOCAL|CS8;
175 cons_attach_input(cc, args->kmta_consdev);
176
177 /* Tell our parent what the console should be. */
178 args->kmta_consdev = cn_tab;
179 k->k_isconsole = 1;
180 printf(" (console input)");
181
182 /* Set magic to "L1-A" */
183 magic[0] = KBD_L1;
184 magic[1] = KBD_A;
185 magic[2] = 0;
186 cn_set_magic(magic);
187 } else {
188 extern void kd_attach_input(struct cons_channel *);
189
190 kd_attach_input(cc);
191 }
192 k->k_cc = cc;
193
194 printf("\n");
195
196 callout_init(&k->k_repeat_ch);
197
198 /* Do this before any calls to kbd_rint(). */
199 kbd_xlate_init(&k->k_state);
200
201 /* XXX - Do this in open? */
202 k->k_repeat_start = hz/2;
203 k->k_repeat_step = hz/20;
204
205 /* Magic sequence. */
206 k->k_magic1 = KBD_L1;
207 k->k_magic2 = KBD_A;
208 }
209
210 /*
211 * Internal open routine. This really should be inside com.c
212 * But I'm putting it here until we have a generic internal open
213 * mechanism.
214 */
215 int
216 sunkbdiopen(dev, flags)
217 struct device *dev;
218 int flags;
219 {
220 struct kbd_softc *k = (void *) dev;
221 struct tty *tp = (struct tty *)k->k_priv;
222 struct proc *p = curproc ? curproc->l_proc : &proc0;
223 struct termios t;
224 int maj;
225 int error;
226
227 maj = major(tp->t_dev);
228
229 /* Open the lower device */
230 if ((error = (*cdevsw[maj].d_open)(tp->t_dev, O_NONBLOCK|flags,
231 0/* ignored? */, p)) != 0)
232 return (error);
233
234 /* Now configure it for the console. */
235 tp->t_ospeed = 0;
236 t.c_ispeed = sunkbd_bps;
237 t.c_ospeed = sunkbd_bps;
238 t.c_cflag = CLOCAL|CS8;
239 (*tp->t_param)(tp, &t);
240
241 return (0);
242 }
243
244 /*
245 * TTY interface to handle input.
246 */
247 int
248 sunkbdinput(c, tp)
249 int c;
250 struct tty *tp;
251 {
252 struct kbd_softc *k = (void *) tp->t_sc;
253 u_char *cc;
254 int error;
255
256
257 cc = tp->t_cc;
258
259 /*
260 * Handle exceptional conditions (break, parity, framing).
261 */
262 if ((error = ((c & TTY_ERRORMASK))) != 0) {
263 /*
264 * After garbage, flush pending input, and
265 * send a reset to resync key translation.
266 */
267 log(LOG_ERR, "%s: input error (0x%x)\n",
268 k->k_dev.dv_xname, c);
269 c &= TTY_CHARMASK;
270 if (!k->k_txflags & K_TXBUSY) {
271 ttyflush(tp, FREAD | FWRITE);
272 goto send_reset;
273 }
274 }
275
276 /*
277 * Check for input buffer overflow
278 */
279 if (tp->t_rawq.c_cc + tp->t_canq.c_cc >= TTYHOG) {
280 log(LOG_ERR, "%s: input overrun\n",
281 k->k_dev.dv_xname);
282 goto send_reset;
283 }
284
285 /* Pass this up to the "middle" layer. */
286 kbd_input_raw(k, c);
287 return (-1);
288
289 send_reset:
290 /* Send a reset to resync translation. */
291 kbd_output(k, KBD_CMD_RESET);
292 return (ttstart(tp));
293
294 }
295
296 int
297 sunkbdstart(tp)
298 struct tty *tp;
299 {
300 struct kbd_softc *k = (void *) tp->t_sc;
301
302 /*
303 * Transmit done. Try to send more, or
304 * clear busy and wakeup drain waiters.
305 */
306 k->k_txflags &= ~K_TXBUSY;
307 kbd_start_tx(k);
308 ttstart(tp);
309 return (0);
310 }
311 /*
312 * used by kbd_start_tx();
313 */
314 void
315 sunkbd_write_data(k, c)
316 struct kbd_softc *k;
317 int c;
318 {
319 struct tty *tp = (struct tty *)k->k_priv;
320 int s;
321
322 s = spltty();
323 ttyoutput(c, tp);
324 ttstart(tp);
325 splx(s);
326 }
327
328 #endif
329