1 /*
2  *  Copyright (c) 2000-2001 Vojtech Pavlik
3  *
4  *  Based on the work of:
5  *	Hamish Macdonald
6  */
7 
8 /*
9  * Amiga keyboard driver for Linux/m68k
10  */
11 
12 /*
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26  */
27 
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/input.h>
31 #include <linux/delay.h>
32 #include <linux/interrupt.h>
33 #include <linux/keyboard.h>
34 #include <linux/platform_device.h>
35 
36 #include <asm/amigaints.h>
37 #include <asm/amigahw.h>
38 #include <asm/irq.h>
39 
40 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
41 MODULE_DESCRIPTION("Amiga keyboard driver");
42 MODULE_LICENSE("GPL");
43 
44 #ifdef CONFIG_HW_CONSOLE
45 static unsigned char amikbd_keycode[0x78] __initdata = {
46 	[0]	 = KEY_GRAVE,
47 	[1]	 = KEY_1,
48 	[2]	 = KEY_2,
49 	[3]	 = KEY_3,
50 	[4]	 = KEY_4,
51 	[5]	 = KEY_5,
52 	[6]	 = KEY_6,
53 	[7]	 = KEY_7,
54 	[8]	 = KEY_8,
55 	[9]	 = KEY_9,
56 	[10]	 = KEY_0,
57 	[11]	 = KEY_MINUS,
58 	[12]	 = KEY_EQUAL,
59 	[13]	 = KEY_BACKSLASH,
60 	[15]	 = KEY_KP0,
61 	[16]	 = KEY_Q,
62 	[17]	 = KEY_W,
63 	[18]	 = KEY_E,
64 	[19]	 = KEY_R,
65 	[20]	 = KEY_T,
66 	[21]	 = KEY_Y,
67 	[22]	 = KEY_U,
68 	[23]	 = KEY_I,
69 	[24]	 = KEY_O,
70 	[25]	 = KEY_P,
71 	[26]	 = KEY_LEFTBRACE,
72 	[27]	 = KEY_RIGHTBRACE,
73 	[29]	 = KEY_KP1,
74 	[30]	 = KEY_KP2,
75 	[31]	 = KEY_KP3,
76 	[32]	 = KEY_A,
77 	[33]	 = KEY_S,
78 	[34]	 = KEY_D,
79 	[35]	 = KEY_F,
80 	[36]	 = KEY_G,
81 	[37]	 = KEY_H,
82 	[38]	 = KEY_J,
83 	[39]	 = KEY_K,
84 	[40]	 = KEY_L,
85 	[41]	 = KEY_SEMICOLON,
86 	[42]	 = KEY_APOSTROPHE,
87 	[43]	 = KEY_BACKSLASH,
88 	[45]	 = KEY_KP4,
89 	[46]	 = KEY_KP5,
90 	[47]	 = KEY_KP6,
91 	[48]	 = KEY_102ND,
92 	[49]	 = KEY_Z,
93 	[50]	 = KEY_X,
94 	[51]	 = KEY_C,
95 	[52]	 = KEY_V,
96 	[53]	 = KEY_B,
97 	[54]	 = KEY_N,
98 	[55]	 = KEY_M,
99 	[56]	 = KEY_COMMA,
100 	[57]	 = KEY_DOT,
101 	[58]	 = KEY_SLASH,
102 	[60]	 = KEY_KPDOT,
103 	[61]	 = KEY_KP7,
104 	[62]	 = KEY_KP8,
105 	[63]	 = KEY_KP9,
106 	[64]	 = KEY_SPACE,
107 	[65]	 = KEY_BACKSPACE,
108 	[66]	 = KEY_TAB,
109 	[67]	 = KEY_KPENTER,
110 	[68]	 = KEY_ENTER,
111 	[69]	 = KEY_ESC,
112 	[70]	 = KEY_DELETE,
113 	[74]	 = KEY_KPMINUS,
114 	[76]	 = KEY_UP,
115 	[77]	 = KEY_DOWN,
116 	[78]	 = KEY_RIGHT,
117 	[79]	 = KEY_LEFT,
118 	[80]	 = KEY_F1,
119 	[81]	 = KEY_F2,
120 	[82]	 = KEY_F3,
121 	[83]	 = KEY_F4,
122 	[84]	 = KEY_F5,
123 	[85]	 = KEY_F6,
124 	[86]	 = KEY_F7,
125 	[87]	 = KEY_F8,
126 	[88]	 = KEY_F9,
127 	[89]	 = KEY_F10,
128 	[90]	 = KEY_KPLEFTPAREN,
129 	[91]	 = KEY_KPRIGHTPAREN,
130 	[92]	 = KEY_KPSLASH,
131 	[93]	 = KEY_KPASTERISK,
132 	[94]	 = KEY_KPPLUS,
133 	[95]	 = KEY_HELP,
134 	[96]	 = KEY_LEFTSHIFT,
135 	[97]	 = KEY_RIGHTSHIFT,
136 	[98]	 = KEY_CAPSLOCK,
137 	[99]	 = KEY_LEFTCTRL,
138 	[100]	 = KEY_LEFTALT,
139 	[101]	 = KEY_RIGHTALT,
140 	[102]	 = KEY_LEFTMETA,
141 	[103]	 = KEY_RIGHTMETA
142 };
143 
amikbd_init_console_keymaps(void)144 static void __init amikbd_init_console_keymaps(void)
145 {
146 	/* We can spare 512 bytes on stack for temp_map in init path. */
147 	unsigned short temp_map[NR_KEYS];
148 	int i, j;
149 
150 	for (i = 0; i < MAX_NR_KEYMAPS; i++) {
151 		if (!key_maps[i])
152 			continue;
153 		memset(temp_map, 0, sizeof(temp_map));
154 		for (j = 0; j < 0x78; j++) {
155 			if (!amikbd_keycode[j])
156 				continue;
157 			temp_map[j] = key_maps[i][amikbd_keycode[j]];
158 		}
159 		for (j = 0; j < NR_KEYS; j++) {
160 			if (!temp_map[j])
161 				temp_map[j] = 0xf200;
162 		}
163 		memcpy(key_maps[i], temp_map, sizeof(temp_map));
164 	}
165 }
166 #else /* !CONFIG_HW_CONSOLE */
amikbd_init_console_keymaps(void)167 static inline void amikbd_init_console_keymaps(void) {}
168 #endif /* !CONFIG_HW_CONSOLE */
169 
170 static const char *amikbd_messages[8] = {
171 	[0] = KERN_ALERT "amikbd: Ctrl-Amiga-Amiga reset warning!!\n",
172 	[1] = KERN_WARNING "amikbd: keyboard lost sync\n",
173 	[2] = KERN_WARNING "amikbd: keyboard buffer overflow\n",
174 	[3] = KERN_WARNING "amikbd: keyboard controller failure\n",
175 	[4] = KERN_ERR "amikbd: keyboard selftest failure\n",
176 	[5] = KERN_INFO "amikbd: initiate power-up key stream\n",
177 	[6] = KERN_INFO "amikbd: terminate power-up key stream\n",
178 	[7] = KERN_WARNING "amikbd: keyboard interrupt\n"
179 };
180 
amikbd_interrupt(int irq,void * data)181 static irqreturn_t amikbd_interrupt(int irq, void *data)
182 {
183 	struct input_dev *dev = data;
184 	unsigned char scancode, down;
185 
186 	scancode = ~ciaa.sdr;		/* get and invert scancode (keyboard is active low) */
187 	ciaa.cra |= 0x40;		/* switch SP pin to output for handshake */
188 	udelay(85);			/* wait until 85 us have expired */
189 	ciaa.cra &= ~0x40;		/* switch CIA serial port to input mode */
190 
191 	down = !(scancode & 1);		/* lowest bit is release bit */
192 	scancode >>= 1;
193 
194 	if (scancode < 0x78) {		/* scancodes < 0x78 are keys */
195 		if (scancode == 98) {	/* CapsLock is a toggle switch key on Amiga */
196 			input_report_key(dev, scancode, 1);
197 			input_report_key(dev, scancode, 0);
198 		} else {
199 			input_report_key(dev, scancode, down);
200 		}
201 
202 		input_sync(dev);
203 	} else				/* scancodes >= 0x78 are error codes */
204 		printk(amikbd_messages[scancode - 0x78]);
205 
206 	return IRQ_HANDLED;
207 }
208 
amikbd_probe(struct platform_device * pdev)209 static int __init amikbd_probe(struct platform_device *pdev)
210 {
211 	struct input_dev *dev;
212 	int i, err;
213 
214 	dev = input_allocate_device();
215 	if (!dev) {
216 		dev_err(&pdev->dev, "Not enough memory for input device\n");
217 		return -ENOMEM;
218 	}
219 
220 	dev->name = pdev->name;
221 	dev->phys = "amikbd/input0";
222 	dev->id.bustype = BUS_AMIGA;
223 	dev->id.vendor = 0x0001;
224 	dev->id.product = 0x0001;
225 	dev->id.version = 0x0100;
226 	dev->dev.parent = &pdev->dev;
227 
228 	dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
229 
230 	for (i = 0; i < 0x78; i++)
231 		set_bit(i, dev->keybit);
232 
233 	amikbd_init_console_keymaps();
234 
235 	ciaa.cra &= ~0x41;	 /* serial data in, turn off TA */
236 	err = request_irq(IRQ_AMIGA_CIAA_SP, amikbd_interrupt, 0, "amikbd",
237 			  dev);
238 	if (err)
239 		goto fail2;
240 
241 	err = input_register_device(dev);
242 	if (err)
243 		goto fail3;
244 
245 	platform_set_drvdata(pdev, dev);
246 
247 	return 0;
248 
249  fail3:	free_irq(IRQ_AMIGA_CIAA_SP, dev);
250  fail2:	input_free_device(dev);
251 	return err;
252 }
253 
amikbd_remove(struct platform_device * pdev)254 static int __exit amikbd_remove(struct platform_device *pdev)
255 {
256 	struct input_dev *dev = platform_get_drvdata(pdev);
257 
258 	free_irq(IRQ_AMIGA_CIAA_SP, dev);
259 	input_unregister_device(dev);
260 	return 0;
261 }
262 
263 static struct platform_driver amikbd_driver = {
264 	.remove = __exit_p(amikbd_remove),
265 	.driver   = {
266 		.name	= "amiga-keyboard",
267 	},
268 };
269 
270 module_platform_driver_probe(amikbd_driver, amikbd_probe);
271 
272 MODULE_ALIAS("platform:amiga-keyboard");
273