1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2000-2001 Vojtech Pavlik
5 * Based on the work of:
10 * Amiga keyboard driver for Linux/m68k
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/input.h>
19 #include <linux/delay.h>
20 #include <linux/interrupt.h>
21 #include <linux/keyboard.h>
22 #include <linux/platform_device.h>
24 #include <asm/amigaints.h>
25 #include <asm/amigahw.h>
28 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
29 MODULE_DESCRIPTION("Amiga keyboard driver");
30 MODULE_LICENSE("GPL");
32 #ifdef CONFIG_HW_CONSOLE
33 static unsigned char amikbd_keycode[0x78] __initdata = {
60 [27] = KEY_RIGHTBRACE,
74 [42] = KEY_APOSTROPHE,
116 [90] = KEY_KPLEFTPAREN,
117 [91] = KEY_KPRIGHTPAREN,
119 [93] = KEY_KPASTERISK,
122 [96] = KEY_LEFTSHIFT,
123 [97] = KEY_RIGHTSHIFT,
127 [101] = KEY_RIGHTALT,
128 [102] = KEY_LEFTMETA,
129 [103] = KEY_RIGHTMETA
132 static void __init amikbd_init_console_keymaps(void)
134 /* We can spare 512 bytes on stack for temp_map in init path. */
135 unsigned short temp_map[NR_KEYS];
138 for (i = 0; i < MAX_NR_KEYMAPS; i++) {
141 memset(temp_map, 0, sizeof(temp_map));
142 for (j = 0; j < 0x78; j++) {
143 if (!amikbd_keycode[j])
145 temp_map[j] = key_maps[i][amikbd_keycode[j]];
147 for (j = 0; j < NR_KEYS; j++) {
149 temp_map[j] = 0xf200;
151 memcpy(key_maps[i], temp_map, sizeof(temp_map));
154 #else /* !CONFIG_HW_CONSOLE */
155 static inline void amikbd_init_console_keymaps(void) {}
156 #endif /* !CONFIG_HW_CONSOLE */
158 static const char *amikbd_messages[8] = {
159 [0] = KERN_ALERT "amikbd: Ctrl-Amiga-Amiga reset warning!!\n",
160 [1] = KERN_WARNING "amikbd: keyboard lost sync\n",
161 [2] = KERN_WARNING "amikbd: keyboard buffer overflow\n",
162 [3] = KERN_WARNING "amikbd: keyboard controller failure\n",
163 [4] = KERN_ERR "amikbd: keyboard selftest failure\n",
164 [5] = KERN_INFO "amikbd: initiate power-up key stream\n",
165 [6] = KERN_INFO "amikbd: terminate power-up key stream\n",
166 [7] = KERN_WARNING "amikbd: keyboard interrupt\n"
169 static irqreturn_t amikbd_interrupt(int irq, void *data)
171 struct input_dev *dev = data;
172 unsigned char scancode, down;
174 scancode = ~ciaa.sdr; /* get and invert scancode (keyboard is active low) */
175 ciaa.cra |= 0x40; /* switch SP pin to output for handshake */
176 udelay(85); /* wait until 85 us have expired */
177 ciaa.cra &= ~0x40; /* switch CIA serial port to input mode */
179 down = !(scancode & 1); /* lowest bit is release bit */
182 if (scancode < 0x78) { /* scancodes < 0x78 are keys */
183 if (scancode == 98) { /* CapsLock is a toggle switch key on Amiga */
184 input_report_key(dev, scancode, 1);
185 input_report_key(dev, scancode, 0);
187 input_report_key(dev, scancode, down);
191 } else /* scancodes >= 0x78 are error codes */
192 printk(amikbd_messages[scancode - 0x78]);
197 static int __init amikbd_probe(struct platform_device *pdev)
199 struct input_dev *dev;
202 dev = input_allocate_device();
204 dev_err(&pdev->dev, "Not enough memory for input device\n");
208 dev->name = pdev->name;
209 dev->phys = "amikbd/input0";
210 dev->id.bustype = BUS_AMIGA;
211 dev->id.vendor = 0x0001;
212 dev->id.product = 0x0001;
213 dev->id.version = 0x0100;
214 dev->dev.parent = &pdev->dev;
216 dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
218 for (i = 0; i < 0x78; i++)
219 set_bit(i, dev->keybit);
221 amikbd_init_console_keymaps();
223 ciaa.cra &= ~0x41; /* serial data in, turn off TA */
224 err = request_irq(IRQ_AMIGA_CIAA_SP, amikbd_interrupt, 0, "amikbd",
229 err = input_register_device(dev);
233 platform_set_drvdata(pdev, dev);
237 fail3: free_irq(IRQ_AMIGA_CIAA_SP, dev);
238 fail2: input_free_device(dev);
242 static int __exit amikbd_remove(struct platform_device *pdev)
244 struct input_dev *dev = platform_get_drvdata(pdev);
246 free_irq(IRQ_AMIGA_CIAA_SP, dev);
247 input_unregister_device(dev);
251 static struct platform_driver amikbd_driver = {
252 .remove = __exit_p(amikbd_remove),
254 .name = "amiga-keyboard",
258 module_platform_driver_probe(amikbd_driver, amikbd_probe);
260 MODULE_ALIAS("platform:amiga-keyboard");