amikbd.c 6.4 KB

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