11a59d1b8SThomas Gleixner// SPDX-License-Identifier: GPL-2.0-or-later
2c04cb856SMichael Schmitz/*
3c04cb856SMichael Schmitz *  atakbd.c
4c04cb856SMichael Schmitz *
5c04cb856SMichael Schmitz *  Copyright (c) 2005 Michael Schmitz
6c04cb856SMichael Schmitz *
7c04cb856SMichael Schmitz * Based on amikbd.c, which is
8c04cb856SMichael Schmitz *
9c04cb856SMichael Schmitz *  Copyright (c) 2000-2001 Vojtech Pavlik
10c04cb856SMichael Schmitz *
11c04cb856SMichael Schmitz *  Based on the work of:
12c04cb856SMichael Schmitz *	Hamish Macdonald
13c04cb856SMichael Schmitz */
14c04cb856SMichael Schmitz
15c04cb856SMichael Schmitz/*
16c04cb856SMichael Schmitz * Atari keyboard driver for Linux/m68k
17c04cb856SMichael Schmitz *
18c04cb856SMichael Schmitz * The low level init and interrupt stuff is handled in arch/mm68k/atari/atakeyb.c
19c04cb856SMichael Schmitz * (the keyboard ACIA also handles the mouse and joystick data, and the keyboard
20c04cb856SMichael Schmitz * interrupt is shared with the MIDI ACIA so MIDI data also get handled there).
21c04cb856SMichael Schmitz * This driver only deals with handing key events off to the input layer.
22c04cb856SMichael Schmitz */
23c04cb856SMichael Schmitz
24c04cb856SMichael Schmitz/*
25c04cb856SMichael Schmitz */
26c04cb856SMichael Schmitz
27c04cb856SMichael Schmitz#include <linux/module.h>
28c04cb856SMichael Schmitz#include <linux/init.h>
29c04cb856SMichael Schmitz#include <linux/input.h>
30c04cb856SMichael Schmitz#include <linux/delay.h>
31c04cb856SMichael Schmitz#include <linux/interrupt.h>
32c04cb856SMichael Schmitz
33c04cb856SMichael Schmitz#include <asm/atariints.h>
34c04cb856SMichael Schmitz#include <asm/atarihw.h>
35c04cb856SMichael Schmitz#include <asm/atarikb.h>
36c04cb856SMichael Schmitz#include <asm/irq.h>
37c04cb856SMichael Schmitz
38c04cb856SMichael SchmitzMODULE_AUTHOR("Michael Schmitz <schmitz@biophys.uni-duesseldorf.de>");
39c04cb856SMichael SchmitzMODULE_DESCRIPTION("Atari keyboard driver");
40c04cb856SMichael SchmitzMODULE_LICENSE("GPL");
41c04cb856SMichael Schmitz
426615c5b2SGeert Uytterhoeven/*
436615c5b2SGeert Uytterhoeven 0x47: KP_7     71
446615c5b2SGeert Uytterhoeven 0x48: KP_8     72
456615c5b2SGeert Uytterhoeven 0x49: KP_9     73
466615c5b2SGeert Uytterhoeven 0x62: KP_/     98
476615c5b2SGeert Uytterhoeven 0x4b: KP_4     75
486615c5b2SGeert Uytterhoeven 0x4c: KP_5     76
496615c5b2SGeert Uytterhoeven 0x4d: KP_6     77
506615c5b2SGeert Uytterhoeven 0x37: KP_*     55
516615c5b2SGeert Uytterhoeven 0x4f: KP_1     79
526615c5b2SGeert Uytterhoeven 0x50: KP_2     80
536615c5b2SGeert Uytterhoeven 0x51: KP_3     81
546615c5b2SGeert Uytterhoeven 0x4a: KP_-     74
556615c5b2SGeert Uytterhoeven 0x52: KP_0     82
566615c5b2SGeert Uytterhoeven 0x53: KP_.     83
576615c5b2SGeert Uytterhoeven 0x4e: KP_+     78
586615c5b2SGeert Uytterhoeven
596615c5b2SGeert Uytterhoeven 0x67: Up       103
606615c5b2SGeert Uytterhoeven 0x6c: Down     108
616615c5b2SGeert Uytterhoeven 0x69: Left     105
626615c5b2SGeert Uytterhoeven 0x6a: Right    106
636615c5b2SGeert Uytterhoeven */
646615c5b2SGeert Uytterhoeven
656615c5b2SGeert Uytterhoeven
669e62df51SAndreas Schwabstatic unsigned char atakbd_keycode[0x73] = {	/* American layout */
676615c5b2SGeert Uytterhoeven	[1]	 = KEY_ESC,
686615c5b2SGeert Uytterhoeven	[2]	 = KEY_1,
696615c5b2SGeert Uytterhoeven	[3]	 = KEY_2,
706615c5b2SGeert Uytterhoeven	[4]	 = KEY_3,
716615c5b2SGeert Uytterhoeven	[5]	 = KEY_4,
726615c5b2SGeert Uytterhoeven	[6]	 = KEY_5,
736615c5b2SGeert Uytterhoeven	[7]	 = KEY_6,
746615c5b2SGeert Uytterhoeven	[8]	 = KEY_7,
756615c5b2SGeert Uytterhoeven	[9]	 = KEY_8,
766615c5b2SGeert Uytterhoeven	[10]	 = KEY_9,
776615c5b2SGeert Uytterhoeven	[11]	 = KEY_0,
786615c5b2SGeert Uytterhoeven	[12]	 = KEY_MINUS,
796615c5b2SGeert Uytterhoeven	[13]	 = KEY_EQUAL,
806615c5b2SGeert Uytterhoeven	[14]	 = KEY_BACKSPACE,
816615c5b2SGeert Uytterhoeven	[15]	 = KEY_TAB,
826615c5b2SGeert Uytterhoeven	[16]	 = KEY_Q,
836615c5b2SGeert Uytterhoeven	[17]	 = KEY_W,
846615c5b2SGeert Uytterhoeven	[18]	 = KEY_E,
856615c5b2SGeert Uytterhoeven	[19]	 = KEY_R,
866615c5b2SGeert Uytterhoeven	[20]	 = KEY_T,
876615c5b2SGeert Uytterhoeven	[21]	 = KEY_Y,
886615c5b2SGeert Uytterhoeven	[22]	 = KEY_U,
896615c5b2SGeert Uytterhoeven	[23]	 = KEY_I,
906615c5b2SGeert Uytterhoeven	[24]	 = KEY_O,
916615c5b2SGeert Uytterhoeven	[25]	 = KEY_P,
926615c5b2SGeert Uytterhoeven	[26]	 = KEY_LEFTBRACE,
936615c5b2SGeert Uytterhoeven	[27]	 = KEY_RIGHTBRACE,
946615c5b2SGeert Uytterhoeven	[28]	 = KEY_ENTER,
956615c5b2SGeert Uytterhoeven	[29]	 = KEY_LEFTCTRL,
966615c5b2SGeert Uytterhoeven	[30]	 = KEY_A,
976615c5b2SGeert Uytterhoeven	[31]	 = KEY_S,
986615c5b2SGeert Uytterhoeven	[32]	 = KEY_D,
996615c5b2SGeert Uytterhoeven	[33]	 = KEY_F,
1006615c5b2SGeert Uytterhoeven	[34]	 = KEY_G,
1016615c5b2SGeert Uytterhoeven	[35]	 = KEY_H,
1026615c5b2SGeert Uytterhoeven	[36]	 = KEY_J,
1036615c5b2SGeert Uytterhoeven	[37]	 = KEY_K,
1046615c5b2SGeert Uytterhoeven	[38]	 = KEY_L,
1056615c5b2SGeert Uytterhoeven	[39]	 = KEY_SEMICOLON,
1066615c5b2SGeert Uytterhoeven	[40]	 = KEY_APOSTROPHE,
1079e62df51SAndreas Schwab	[41]	 = KEY_GRAVE,
1086615c5b2SGeert Uytterhoeven	[42]	 = KEY_LEFTSHIFT,
1099e62df51SAndreas Schwab	[43]	 = KEY_BACKSLASH,
1106615c5b2SGeert Uytterhoeven	[44]	 = KEY_Z,
1116615c5b2SGeert Uytterhoeven	[45]	 = KEY_X,
1126615c5b2SGeert Uytterhoeven	[46]	 = KEY_C,
1136615c5b2SGeert Uytterhoeven	[47]	 = KEY_V,
1146615c5b2SGeert Uytterhoeven	[48]	 = KEY_B,
1156615c5b2SGeert Uytterhoeven	[49]	 = KEY_N,
1166615c5b2SGeert Uytterhoeven	[50]	 = KEY_M,
1176615c5b2SGeert Uytterhoeven	[51]	 = KEY_COMMA,
1186615c5b2SGeert Uytterhoeven	[52]	 = KEY_DOT,
1196615c5b2SGeert Uytterhoeven	[53]	 = KEY_SLASH,
1206615c5b2SGeert Uytterhoeven	[54]	 = KEY_RIGHTSHIFT,
1216615c5b2SGeert Uytterhoeven	[55]	 = KEY_KPASTERISK,
1226615c5b2SGeert Uytterhoeven	[56]	 = KEY_LEFTALT,
1236615c5b2SGeert Uytterhoeven	[57]	 = KEY_SPACE,
1246615c5b2SGeert Uytterhoeven	[58]	 = KEY_CAPSLOCK,
1256615c5b2SGeert Uytterhoeven	[59]	 = KEY_F1,
1266615c5b2SGeert Uytterhoeven	[60]	 = KEY_F2,
1276615c5b2SGeert Uytterhoeven	[61]	 = KEY_F3,
1286615c5b2SGeert Uytterhoeven	[62]	 = KEY_F4,
1296615c5b2SGeert Uytterhoeven	[63]	 = KEY_F5,
1306615c5b2SGeert Uytterhoeven	[64]	 = KEY_F6,
1316615c5b2SGeert Uytterhoeven	[65]	 = KEY_F7,
1326615c5b2SGeert Uytterhoeven	[66]	 = KEY_F8,
1336615c5b2SGeert Uytterhoeven	[67]	 = KEY_F9,
1346615c5b2SGeert Uytterhoeven	[68]	 = KEY_F10,
1359e62df51SAndreas Schwab	[71]	 = KEY_HOME,
1369e62df51SAndreas Schwab	[72]	 = KEY_UP,
1376615c5b2SGeert Uytterhoeven	[74]	 = KEY_KPMINUS,
1389e62df51SAndreas Schwab	[75]	 = KEY_LEFT,
1399e62df51SAndreas Schwab	[77]	 = KEY_RIGHT,
1406615c5b2SGeert Uytterhoeven	[78]	 = KEY_KPPLUS,
1419e62df51SAndreas Schwab	[80]	 = KEY_DOWN,
1429e62df51SAndreas Schwab	[82]	 = KEY_INSERT,
1439e62df51SAndreas Schwab	[83]	 = KEY_DELETE,
1449923d858SMichael Karcher	[96]	 = KEY_102ND,
1459e62df51SAndreas Schwab	[97]	 = KEY_UNDO,
1469e62df51SAndreas Schwab	[98]	 = KEY_HELP,
1476615c5b2SGeert Uytterhoeven	[99]	 = KEY_KPLEFTPAREN,
1486615c5b2SGeert Uytterhoeven	[100]	 = KEY_KPRIGHTPAREN,
1496615c5b2SGeert Uytterhoeven	[101]	 = KEY_KPSLASH,
1506615c5b2SGeert Uytterhoeven	[102]	 = KEY_KPASTERISK,
1519e62df51SAndreas Schwab	[103]	 = KEY_KP7,
1529e62df51SAndreas Schwab	[104]	 = KEY_KP8,
1539e62df51SAndreas Schwab	[105]	 = KEY_KP9,
1549e62df51SAndreas Schwab	[106]	 = KEY_KP4,
1559e62df51SAndreas Schwab	[107]	 = KEY_KP5,
1569e62df51SAndreas Schwab	[108]	 = KEY_KP6,
1579e62df51SAndreas Schwab	[109]	 = KEY_KP1,
1589e62df51SAndreas Schwab	[110]	 = KEY_KP2,
1599e62df51SAndreas Schwab	[111]	 = KEY_KP3,
1609e62df51SAndreas Schwab	[112]	 = KEY_KP0,
1619e62df51SAndreas Schwab	[113]	 = KEY_KPDOT,
1629e62df51SAndreas Schwab	[114]	 = KEY_KPENTER,
1636615c5b2SGeert Uytterhoeven};
164c04cb856SMichael Schmitz
165c04cb856SMichael Schmitzstatic struct input_dev *atakbd_dev;
166c04cb856SMichael Schmitz
167c04cb856SMichael Schmitzstatic void atakbd_interrupt(unsigned char scancode, char down)
168c04cb856SMichael Schmitz{
169c04cb856SMichael Schmitz
1709e62df51SAndreas Schwab	if (scancode < 0x73) {		/* scancodes < 0xf3 are keys */
171c04cb856SMichael Schmitz
172c04cb856SMichael Schmitz		// report raw events here?
173c04cb856SMichael Schmitz
174c04cb856SMichael Schmitz		scancode = atakbd_keycode[scancode];
175c04cb856SMichael Schmitz
17652d2c7bfSMichael Schmitz		input_report_key(atakbd_dev, scancode, down);
17752d2c7bfSMichael Schmitz		input_sync(atakbd_dev);
1789e62df51SAndreas Schwab	} else				/* scancodes >= 0xf3 are mouse data, most likely */
179c04cb856SMichael Schmitz		printk(KERN_INFO "atakbd: unhandled scancode %x\n", scancode);
180c04cb856SMichael Schmitz
181c04cb856SMichael Schmitz	return;
182c04cb856SMichael Schmitz}
183c04cb856SMichael Schmitz
184c04cb856SMichael Schmitzstatic int __init atakbd_init(void)
185c04cb856SMichael Schmitz{
18663bd8c48SGeert Uytterhoeven	int i, error;
187c04cb856SMichael Schmitz
1886615c5b2SGeert Uytterhoeven	if (!MACH_IS_ATARI || !ATARIHW_PRESENT(ST_MFP))
189e945b568SGeert Uytterhoeven		return -ENODEV;
190c04cb856SMichael Schmitz
191c04cb856SMichael Schmitz	// need to init core driver if not already done so
192186f200aSMichael Schmitz	error = atari_keyb_init();
193186f200aSMichael Schmitz	if (error)
194186f200aSMichael Schmitz		return error;
195c04cb856SMichael Schmitz
1966615c5b2SGeert Uytterhoeven	atakbd_dev = input_allocate_device();
1976615c5b2SGeert Uytterhoeven	if (!atakbd_dev)
1986615c5b2SGeert Uytterhoeven		return -ENOMEM;
1996615c5b2SGeert Uytterhoeven
200c04cb856SMichael Schmitz	atakbd_dev->name = "Atari Keyboard";
201c04cb856SMichael Schmitz	atakbd_dev->phys = "atakbd/input0";
2026615c5b2SGeert Uytterhoeven	atakbd_dev->id.bustype = BUS_HOST;
203c04cb856SMichael Schmitz	atakbd_dev->id.vendor = 0x0001;
204c04cb856SMichael Schmitz	atakbd_dev->id.product = 0x0001;
205c04cb856SMichael Schmitz	atakbd_dev->id.version = 0x0100;
206c04cb856SMichael Schmitz
2077b19ada2SJiri Slaby	atakbd_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
208c04cb856SMichael Schmitz	atakbd_dev->keycode = atakbd_keycode;
209c04cb856SMichael Schmitz	atakbd_dev->keycodesize = sizeof(unsigned char);
210c04cb856SMichael Schmitz	atakbd_dev->keycodemax = ARRAY_SIZE(atakbd_keycode);
211c04cb856SMichael Schmitz
212c04cb856SMichael Schmitz	for (i = 1; i < 0x72; i++) {
213c04cb856SMichael Schmitz		set_bit(atakbd_keycode[i], atakbd_dev->keybit);
214c04cb856SMichael Schmitz	}
215c04cb856SMichael Schmitz
2166615c5b2SGeert Uytterhoeven	/* error check */
21763bd8c48SGeert Uytterhoeven	error = input_register_device(atakbd_dev);
21863bd8c48SGeert Uytterhoeven	if (error) {
2196615c5b2SGeert Uytterhoeven		input_free_device(atakbd_dev);
22063bd8c48SGeert Uytterhoeven		return error;
2216615c5b2SGeert Uytterhoeven	}
222c04cb856SMichael Schmitz
223c04cb856SMichael Schmitz	atari_input_keyboard_interrupt_hook = atakbd_interrupt;
224c04cb856SMichael Schmitz
225c04cb856SMichael Schmitz	return 0;
226c04cb856SMichael Schmitz}
227c04cb856SMichael Schmitz
228c04cb856SMichael Schmitzstatic void __exit atakbd_exit(void)
229c04cb856SMichael Schmitz{
230c04cb856SMichael Schmitz	atari_input_keyboard_interrupt_hook = NULL;
231c04cb856SMichael Schmitz	input_unregister_device(atakbd_dev);
232c04cb856SMichael Schmitz}
233c04cb856SMichael Schmitz
234c04cb856SMichael Schmitzmodule_init(atakbd_init);
235c04cb856SMichael Schmitzmodule_exit(atakbd_exit);
236