amikbd.c 6.6 KB

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