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