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