xref: /openbmc/linux/drivers/input/keyboard/xtkbd.c (revision 09c434b8)
1 /*
2  *  Copyright (c) 1999-2001 Vojtech Pavlik
3  */
4 
5 /*
6  * XT keyboard driver for Linux
7  */
8 
9 /*
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  */
24 
25 #include <linux/slab.h>
26 #include <linux/module.h>
27 #include <linux/input.h>
28 #include <linux/serio.h>
29 
30 #define DRIVER_DESC	"XT keyboard driver"
31 
32 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
33 MODULE_DESCRIPTION(DRIVER_DESC);
34 MODULE_LICENSE("GPL");
35 
36 #define XTKBD_EMUL0	0xe0
37 #define XTKBD_EMUL1	0xe1
38 #define XTKBD_KEY	0x7f
39 #define XTKBD_RELEASE	0x80
40 
41 static unsigned char xtkbd_keycode[256] = {
42 	  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
43 	 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
44 	 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
45 	 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
46 	 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
47 	 80, 81, 82, 83,  0,  0,  0, 87, 88,  0,  0,  0,  0,  0,  0,  0,
48 	  0,  0,  0,  0,  0, 87, 88,  0,  0,  0,  0,110,111,103,108,105,
49 	106
50 };
51 
52 struct xtkbd {
53 	unsigned char keycode[256];
54 	struct input_dev *dev;
55 	struct serio *serio;
56 	char phys[32];
57 };
58 
59 static irqreturn_t xtkbd_interrupt(struct serio *serio,
60 	unsigned char data, unsigned int flags)
61 {
62 	struct xtkbd *xtkbd = serio_get_drvdata(serio);
63 
64 	switch (data) {
65 		case XTKBD_EMUL0:
66 		case XTKBD_EMUL1:
67 			break;
68 		default:
69 
70 			if (xtkbd->keycode[data & XTKBD_KEY]) {
71 				input_report_key(xtkbd->dev, xtkbd->keycode[data & XTKBD_KEY], !(data & XTKBD_RELEASE));
72 				input_sync(xtkbd->dev);
73 			} else {
74 				printk(KERN_WARNING "xtkbd.c: Unknown key (scancode %#x) %s.\n",
75 					data & XTKBD_KEY, data & XTKBD_RELEASE ? "released" : "pressed");
76 			}
77 	}
78 	return IRQ_HANDLED;
79 }
80 
81 static int xtkbd_connect(struct serio *serio, struct serio_driver *drv)
82 {
83 	struct xtkbd *xtkbd;
84 	struct input_dev *input_dev;
85 	int err = -ENOMEM;
86 	int i;
87 
88 	xtkbd = kmalloc(sizeof(struct xtkbd), GFP_KERNEL);
89 	input_dev = input_allocate_device();
90 	if (!xtkbd || !input_dev)
91 		goto fail1;
92 
93 	xtkbd->serio = serio;
94 	xtkbd->dev = input_dev;
95 	snprintf(xtkbd->phys, sizeof(xtkbd->phys), "%s/input0", serio->phys);
96 	memcpy(xtkbd->keycode, xtkbd_keycode, sizeof(xtkbd->keycode));
97 
98 	input_dev->name = "XT Keyboard";
99 	input_dev->phys = xtkbd->phys;
100 	input_dev->id.bustype = BUS_XTKBD;
101 	input_dev->id.vendor  = 0x0001;
102 	input_dev->id.product = 0x0001;
103 	input_dev->id.version = 0x0100;
104 	input_dev->dev.parent = &serio->dev;
105 
106 	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
107 	input_dev->keycode = xtkbd->keycode;
108 	input_dev->keycodesize = sizeof(unsigned char);
109 	input_dev->keycodemax = ARRAY_SIZE(xtkbd_keycode);
110 
111 	for (i = 0; i < 255; i++)
112 		set_bit(xtkbd->keycode[i], input_dev->keybit);
113 	clear_bit(0, input_dev->keybit);
114 
115 	serio_set_drvdata(serio, xtkbd);
116 
117 	err = serio_open(serio, drv);
118 	if (err)
119 		goto fail2;
120 
121 	err = input_register_device(xtkbd->dev);
122 	if (err)
123 		goto fail3;
124 
125 	return 0;
126 
127  fail3:	serio_close(serio);
128  fail2:	serio_set_drvdata(serio, NULL);
129  fail1:	input_free_device(input_dev);
130 	kfree(xtkbd);
131 	return err;
132 }
133 
134 static void xtkbd_disconnect(struct serio *serio)
135 {
136 	struct xtkbd *xtkbd = serio_get_drvdata(serio);
137 
138 	serio_close(serio);
139 	serio_set_drvdata(serio, NULL);
140 	input_unregister_device(xtkbd->dev);
141 	kfree(xtkbd);
142 }
143 
144 static const struct serio_device_id xtkbd_serio_ids[] = {
145 	{
146 		.type	= SERIO_XT,
147 		.proto	= SERIO_ANY,
148 		.id	= SERIO_ANY,
149 		.extra	= SERIO_ANY,
150 	},
151 	{ 0 }
152 };
153 
154 MODULE_DEVICE_TABLE(serio, xtkbd_serio_ids);
155 
156 static struct serio_driver xtkbd_drv = {
157 	.driver		= {
158 		.name	= "xtkbd",
159 	},
160 	.description	= DRIVER_DESC,
161 	.id_table	= xtkbd_serio_ids,
162 	.interrupt	= xtkbd_interrupt,
163 	.connect	= xtkbd_connect,
164 	.disconnect	= xtkbd_disconnect,
165 };
166 
167 module_serio_driver(xtkbd_drv);
168