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