xref: /netbsd-src/sys/dev/hpc/hpf1275a_tty.c (revision f7d6597db0cc705cceb80b78e7ed03156c74a9b3)
1 /*	$NetBSD: hpf1275a_tty.c,v 1.13 2006/05/29 23:01:08 uwe Exp $ */
2 
3 /*
4  * Copyright (c) 2004 Valeriy E. Ushakov
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: hpf1275a_tty.c,v 1.13 2006/05/29 23:01:08 uwe Exp $");
32 
33 #include "opt_wsdisplay_compat.h"
34 
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/conf.h>
38 #include <sys/device.h>
39 #include <sys/tty.h>
40 #include <sys/fcntl.h>
41 #include <sys/proc.h>
42 #include <sys/systm.h>
43 #include <sys/kauth.h>
44 #ifdef GPROF
45 #include <sys/gmon.h>
46 #endif
47 
48 #include <dev/wscons/wsconsio.h>
49 #include <dev/wscons/wskbdvar.h>
50 #include <dev/wscons/wsksymdef.h>
51 #include <dev/wscons/wsksymvar.h>
52 
53 #include <dev/pckbport/wskbdmap_mfii.h>
54 #ifdef WSDISPLAY_COMPAT_RAWKBD
55 #include <dev/hpc/pckbd_encode.h>
56 #endif
57 
58 
59 extern struct cfdriver hpf1275a_cd;
60 
61 struct hpf1275a_softc {
62 	struct device sc_dev;
63 
64 	struct tty *sc_tp;		/* back reference to the tty */
65 	struct device *sc_wskbd;	/* wskbd child */
66 	int sc_enabled;
67 #ifdef WSDISPLAY_COMPAT_RAWKBD
68 	int sc_rawkbd;
69 #endif
70 };
71 
72 
73 /* pseudo-device initialization */
74 extern void	hpf1275aattach(int);
75 
76 /* line discipline methods */
77 static int	hpf1275a_open(dev_t, struct tty *);
78 static int	hpf1275a_close(struct tty *, int);
79 static int	hpf1275a_input(int, struct tty *);
80 
81 /* autoconf(9) methods */
82 static int	hpf1275a_match(struct device *, struct cfdata *, void *);
83 static void	hpf1275a_attach(struct device *, struct device *, void *);
84 static int	hpf1275a_detach(struct device *, int);
85 
86 /* wskbd(4) accessops */
87 static int	hpf1275a_wskbd_enable(void *, int);
88 static void	hpf1275a_wskbd_set_leds(void *, int);
89 static int	hpf1275a_wskbd_ioctl(void *, u_long, caddr_t, int,
90 				     struct lwp *);
91 
92 
93 /*
94  * It doesn't need to be exported, as only hpf1275aattach() uses it,
95  * but there's no "official" way to make it static.
96  */
97 CFATTACH_DECL(hpf1275a, sizeof(struct hpf1275a_softc),
98     hpf1275a_match, hpf1275a_attach, hpf1275a_detach, NULL);
99 
100 
101 static struct linesw hpf1275a_disc = {
102 	.l_name = "hpf1275a",
103 	.l_open = hpf1275a_open,
104 	.l_close = hpf1275a_close,
105 	.l_read = ttyerrio,
106 	.l_write = ttyerrio,
107 	.l_ioctl = ttynullioctl,
108 	.l_rint = hpf1275a_input,
109 	.l_start = ttstart,
110 	.l_modem = nullmodem,
111 	.l_poll = ttpoll
112 };
113 
114 
115 static const struct wskbd_accessops hpf1275a_wskbd_accessops = {
116 	hpf1275a_wskbd_enable,
117 	hpf1275a_wskbd_set_leds,
118 	hpf1275a_wskbd_ioctl
119 };
120 
121 
122 static const struct wskbd_mapdata hpf1275a_wskbd_keymapdata = {
123 	pckbd_keydesctab, KB_US
124 };
125 
126 
127 /* F1275A scancodes -> XT scancodes so that we can use pckbd_keydesctab. */
128 static const uint8_t hpf1275a_to_xtscan[128] = {
129 	[0x04] = 30,		/* a */
130 	[0x05] = 48,		/* b */
131 	[0x06] = 46,		/* c */
132 	[0x07] = 32,		/* d */
133 	[0x08] = 18,		/* e */
134 	[0x09] = 33,		/* f */
135 	[0x0a] = 34,		/* g */
136 	[0x0b] = 35,		/* h */
137 	[0x0c] = 23,		/* i */
138 	[0x0d] = 36,		/* j */
139 	[0x0e] = 37,		/* k */
140 	[0x0f] = 38,		/* l */
141 	[0x10] = 50,		/* m */
142 	[0x11] = 49,		/* n */
143 	[0x12] = 24,		/* o */
144 	[0x13] = 25,		/* p */
145 	[0x14] = 16,		/* q */
146 	[0x15] = 19,		/* r */
147 	[0x16] = 31,		/* s */
148 	[0x17] = 20,		/* t */
149 	[0x18] = 22,		/* u */
150 	[0x19] = 47,		/* v */
151 	[0x1a] = 17,		/* w */
152 	[0x1b] = 45,		/* x */
153 	[0x1c] = 21,		/* y */
154 	[0x1d] = 44,		/* z */
155 
156 	[0x1e] = 2,		/* 1 */
157 	[0x1f] = 3,		/* 2 */
158 	[0x20] = 4,		/* 3 */
159 	[0x21] = 5,		/* 4 */
160 	[0x22] = 6,		/* 5 */
161 	[0x23] = 7,		/* 6 */
162 	[0x24] = 8,		/* 7 */
163 	[0x25] = 9,		/* 8 */
164 	[0x26] = 10,		/* 9 */
165 	[0x27] = 11,		/* 0 */
166 
167 	[0x28] = 28,		/* Enter */
168 
169 	[0x29] = 1,		/* ESC */
170 	[0x2a] = 14,		/* Backspace */
171 	[0x2b] = 15,		/* Tab */
172 	[0x2c] = 57,		/* Space */
173 
174 	[0x2d] = 12,		/* - */
175 	[0x2e] = 13,		/* = */
176 	[0x2f] = 26,		/* [ */
177 	[0x30] = 27,		/* ] */
178 	[0x31] = 43,		/* \ */
179 
180 	[0x33] = 39,		/* ; */
181 	[0x34] = 40,		/* ' */
182 	[0x35] = 41,		/* ` */
183 	[0x36] = 51,		/* , */
184 	[0x37] = 52,		/* . */
185 	[0x38] = 53,		/* / */
186 
187 	[0x3a] = 59,		/* F1 */
188 	[0x3b] = 60,		/* F2 */
189 	[0x3c] = 61,		/* F3 */
190 	[0x3d] = 62,		/* F4 */
191 	[0x3e] = 63,		/* F5 */
192 	[0x3f] = 64,		/* F6 */
193 	[0x40] = 65,		/* F7 */
194 	[0x41] = 66,		/* F8 */
195 
196 	[0x42] = 68,		/* "OK" -> F10 */
197 	[0x43] = 87,		/* "Cancel" -> F11 */
198 
199 	[0x4c] = 211,		/* Del */
200 
201 	[0x4f] = 205,		/* Right */
202 	[0x50] = 203,		/* Left  */
203 	[0x51] = 208,		/* Down  */
204 	[0x52] = 200,		/* Up    */
205 
206 	[0x53] = 67,		/* "task switch" -> F9 */
207 
208 	[0x65] = 221,		/* windows */
209 	[0x66] = 88,		/* "keyboard" -> F12 */
210 
211 	[0x74] = 42,		/* Shift (left) */
212 	[0x75] = 54,		/* Shift (right) */
213 	[0x76] = 56,		/* Alt (left) */
214 	[0x77] = 184,		/* Fn -> AltGr == Mode Switch */
215 	[0x78] = 29,		/* Control (left) */
216 };
217 
218 
219 /*
220  * Pseudo-device initialization routine called from main().
221  */
222 void
223 hpf1275aattach(int n)
224 {
225 	int error;
226 
227 	error = ttyldisc_attach(&hpf1275a_disc);
228 	if (error) {
229 		printf("%s: unable to register line discipline, error = %d\n",
230 		       hpf1275a_cd.cd_name, error);
231 		return;
232 	}
233 
234 	error = config_cfattach_attach(hpf1275a_cd.cd_name, &hpf1275a_ca);
235 	if (error) {
236 		printf("%s: unable to register cfattach, error = %d\n",
237 		       hpf1275a_cd.cd_name, error);
238 		config_cfdriver_detach(&hpf1275a_cd);
239 		(void) ttyldisc_detach(&hpf1275a_disc);
240 	}
241 }
242 
243 
244 /*
245  * Autoconf match routine.
246  *
247  * XXX: unused: config_attach_pseudo(9) does not call ca_match.
248  */
249 static int
250 hpf1275a_match(struct device *self, struct cfdata *cfdata, void *arg)
251 {
252 
253 	/* pseudo-device; always present */
254 	return (1);
255 }
256 
257 
258 /*
259  * Autoconf attach routine.  Called by config_attach_pseudo(9) when we
260  * open the line discipline.
261  */
262 static void
263 hpf1275a_attach(struct device *parent, struct device *self, void *aux)
264 {
265 	struct hpf1275a_softc *sc = device_private(self);
266 	struct wskbddev_attach_args wska;
267 
268 	wska.console = 0;
269 	wska.keymap = &hpf1275a_wskbd_keymapdata;
270 	wska.accessops = &hpf1275a_wskbd_accessops;
271 	wska.accesscookie = sc;
272 
273 	sc->sc_enabled = 0;
274 #ifdef WSDISPLAY_COMPAT_RAWKBD
275 	sc->sc_rawkbd = 0;
276 #endif
277 	sc->sc_wskbd = config_found(self, &wska, wskbddevprint);
278 }
279 
280 
281 /*
282  * Autoconf detach routine.  Called when we close the line discipline.
283  */
284 static int
285 hpf1275a_detach(struct device *self, int flags)
286 {
287 	struct hpf1275a_softc *sc = device_private(self);
288 	int error;
289 
290 	if (sc->sc_wskbd == NULL)
291 		return (0);
292 
293 	error = config_detach(sc->sc_wskbd, 0);
294 
295 	return (error);
296 }
297 
298 
299 /*
300  * Line discipline open routine.
301  */
302 static int
303 hpf1275a_open(dev_t dev, struct tty *tp)
304 {
305 	static struct cfdata hpf1275a_cfdata = {
306 		.cf_name = "hpf1275a",
307 		.cf_atname = "hpf1275a",
308 		.cf_unit = DVUNIT_ANY,
309 		.cf_fstate = FSTATE_STAR,
310 	};
311 	struct proc *p = curproc;		/* XXX */
312 	struct hpf1275a_softc *sc;
313 	int error, s;
314 
315 	error = kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER,
316 					&p->p_acflag);
317 	if (error != 0)
318 	    return (error);
319 
320 	s = spltty();
321 
322 	sc = (struct hpf1275a_softc *)config_attach_pseudo(&hpf1275a_cfdata);
323 	if (sc == NULL) {
324 		splx(s);
325 		return (EIO);
326 	}
327 
328 	tp->t_sc = sc;
329 	sc->sc_tp = tp;
330 
331 	splx(s);
332 	return (0);
333 }
334 
335 
336 /*
337  * Line discipline close routine.
338  */
339 static int
340 hpf1275a_close(struct tty *tp, int flag)
341 {
342 	struct hpf1275a_softc *sc = tp->t_sc;
343 	int s;
344 
345 	s = spltty();
346 	ttyflush(tp, FREAD | FWRITE);
347 	ttyldisc_release(tp->t_linesw);
348 	tp->t_linesw = ttyldisc_default();
349 	if (sc != NULL) {
350 		tp->t_sc = NULL;
351 		if (sc->sc_tp == tp)
352 			config_detach(&sc->sc_dev, 0);
353 	}
354 	splx(s);
355 	return (0);
356 }
357 
358 
359 /*
360  * Feed input from the keyboard to wskbd(4).
361  */
362 static int
363 hpf1275a_input(int c, struct tty *tp)
364 {
365 	struct hpf1275a_softc *sc = tp->t_sc;
366 	int code;
367 	u_int type;
368 	int xtscan;
369 
370 	if (!sc->sc_enabled)
371 		return (0);
372 
373 	if (c & TTY_ERRORMASK)
374 		return (0);	/* TODO? */
375 
376 	code = c & TTY_CHARMASK;
377 	if (code & 0x80) {
378 		type = WSCONS_EVENT_KEY_UP;
379 		code &= ~0x80;
380 	} else
381 		type = WSCONS_EVENT_KEY_DOWN;
382 
383 	xtscan = hpf1275a_to_xtscan[code];
384 	if (xtscan == 0) {
385 		printf("%s: unknown code 0x%x\n", sc->sc_dev.dv_xname, code);
386 		return (0);
387 	}
388 
389 	KASSERT(sc->sc_wskbd != NULL);
390 
391 #ifdef WSDISPLAY_COMPAT_RAWKBD
392 	if (sc->sc_rawkbd) {
393 		u_char data[16];
394 		int n;
395 
396 		n = pckbd_encode(type, xtscan, data);
397 		wskbd_rawinput(sc->sc_wskbd, data, n);
398 	} else
399 #endif
400 		wskbd_input(sc->sc_wskbd, type, xtscan);
401 
402 	return (0);
403 }
404 
405 
406 static int
407 hpf1275a_wskbd_enable(void *self, int on)
408 {
409 	struct hpf1275a_softc *sc = self;
410 
411 	sc->sc_enabled = on;
412 	return (0);
413 }
414 
415 
416 /* ARGSUSED */
417 static void
418 hpf1275a_wskbd_set_leds(void *self, int leds)
419 {
420 
421 	/* this keyboard has no leds; nothing to do */
422 	return;
423 }
424 
425 
426 static int
427 hpf1275a_wskbd_ioctl(void *self, u_long cmd, caddr_t data, int flag,
428 		     struct lwp *l)
429 {
430 #ifdef WSDISPLAY_COMPAT_RAWKBD
431 	struct hpf1275a_softc *sc = self;
432 #endif
433 
434 	switch (cmd) {
435 	case WSKBDIO_GTYPE:
436 		*(int *)data = WSKBD_TYPE_HPC_KBD; /* may be use new type? */
437 		return (0);
438 
439 	case WSKBDIO_GETLEDS:
440 		*(int *)data = 0; /* this keyboard has no leds */
441 		return (0);
442 
443 #ifdef WSDISPLAY_COMPAT_RAWKBD
444 	case WSKBDIO_SETMODE:
445 		sc->sc_rawkbd = (*(int *)data == WSKBD_RAW);
446 		return (0);
447 #endif
448 
449 	default:
450 		return (EPASSTHROUGH);
451 	}
452 }
453