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