atakeyb.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:27k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * linux/atari/atakeyb.c
  3.  *
  4.  * Atari Keyboard driver for 680x0 Linux
  5.  *
  6.  * This file is subject to the terms and conditions of the GNU General Public
  7.  * License.  See the file COPYING in the main directory of this archive
  8.  * for more details.
  9.  */
  10. /*
  11.  * Atari support by Robert de Vries
  12.  * enhanced by Bjoern Brauel and Roman Hodek
  13.  */
  14. #include <linux/config.h>
  15. #include <linux/sched.h>
  16. #include <linux/kernel.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/errno.h>
  19. #include <linux/keyboard.h>
  20. #include <linux/delay.h>
  21. #include <linux/timer.h>
  22. #include <linux/kd.h>
  23. #include <linux/random.h>
  24. #include <linux/init.h>
  25. #include <linux/kbd_ll.h>
  26. #include <linux/kbd_kern.h>
  27. #include <asm/atariints.h>
  28. #include <asm/atarihw.h>
  29. #include <asm/atarikb.h>
  30. #include <asm/atari_joystick.h>
  31. #include <asm/irq.h>
  32. static void atakeyb_rep( unsigned long ignore );
  33. extern unsigned int keymap_count;
  34. /* Hook for MIDI serial driver */
  35. void (*atari_MIDI_interrupt_hook) (void);
  36. /* Hook for mouse driver */
  37. void (*atari_mouse_interrupt_hook) (char *);
  38. /* variables for IKBD self test: */
  39. /* state: 0: off; >0: in progress; >1: 0xf1 received */
  40. static volatile int ikbd_self_test;
  41. /* timestamp when last received a char */
  42. static volatile unsigned long self_test_last_rcv;
  43. /* bitmap of keys reported as broken */
  44. static unsigned long broken_keys[128/(sizeof(unsigned long)*8)] = { 0, };
  45. #define BREAK_MASK (0x80)
  46. /*
  47.  * ++roman: The following changes were applied manually:
  48.  *
  49.  *  - The Alt (= Meta) key works in combination with Shift and
  50.  *    Control, e.g. Alt+Shift+a sends Meta-A (0xc1), Alt+Control+A sends
  51.  *    Meta-Ctrl-A (0x81) ...
  52.  *
  53.  *  - The parentheses on the keypad send '(' and ')' with all
  54.  *    modifiers (as would do e.g. keypad '+'), but they cannot be used as
  55.  *    application keys (i.e. sending Esc O c).
  56.  *
  57.  *  - HELP and UNDO are mapped to be F21 and F24, resp, that send the
  58.  *    codes "E[M" and "E[P". (This is better than the old mapping to
  59.  *    F11 and F12, because these codes are on Shift+F1/2 anyway.) This
  60.  *    way, applications that allow their own keyboard mappings
  61.  *    (e.g. tcsh, X Windows) can be configured to use them in the way
  62.  *    the label suggests (providing help or undoing).
  63.  *
  64.  *  - Console switching is done with Alt+Fx (consoles 1..10) and
  65.  *    Shift+Alt+Fx (consoles 11..20).
  66.  *
  67.  *  - The misc. special function implemented in the kernel are mapped
  68.  *    to the following key combinations:
  69.  *
  70.  *      ClrHome          -> Home/Find
  71.  *      Shift + ClrHome  -> End/Select
  72.  *      Shift + Up       -> Page Up
  73.  *      Shift + Down     -> Page Down
  74.  *      Alt + Help       -> show system status
  75.  *      Shift + Help     -> show memory info
  76.  *      Ctrl + Help      -> show registers
  77.  *      Ctrl + Alt + Del -> Reboot
  78.  *      Alt + Undo       -> switch to last console
  79.  *      Shift + Undo     -> send interrupt
  80.  *      Alt + Insert     -> stop/start output (same as ^S/^Q)
  81.  *      Alt + Up         -> Scroll back console (if implemented)
  82.  *      Alt + Down       -> Scroll forward console (if implemented)
  83.  *      Alt + CapsLock   -> NumLock
  84.  *
  85.  * ++Andreas:
  86.  *
  87.  *  - Help mapped to K_HELP
  88.  *  - Undo mapped to K_UNDO (= K_F246)
  89.  *  - Keypad Left/Right Parenthesis mapped to new K_PPAREN[LR]
  90.  */
  91. static u_short ataplain_map[NR_KEYS] __initdata = {
  92. 0xf200, 0xf01b, 0xf031, 0xf032, 0xf033, 0xf034, 0xf035, 0xf036,
  93. 0xf037, 0xf038, 0xf039, 0xf030, 0xf02d, 0xf03d, 0xf008, 0xf009,
  94. 0xfb71, 0xfb77, 0xfb65, 0xfb72, 0xfb74, 0xfb79, 0xfb75, 0xfb69,
  95. 0xfb6f, 0xfb70, 0xf05b, 0xf05d, 0xf201, 0xf702, 0xfb61, 0xfb73,
  96. 0xfb64, 0xfb66, 0xfb67, 0xfb68, 0xfb6a, 0xfb6b, 0xfb6c, 0xf03b,
  97. 0xf027, 0xf060, 0xf700, 0xf05c, 0xfb7a, 0xfb78, 0xfb63, 0xfb76,
  98. 0xfb62, 0xfb6e, 0xfb6d, 0xf02c, 0xf02e, 0xf02f, 0xf700, 0xf200,
  99. 0xf703, 0xf020, 0xf207, 0xf100, 0xf101, 0xf102, 0xf103, 0xf104,
  100. 0xf105, 0xf106, 0xf107, 0xf108, 0xf109, 0xf200, 0xf200, 0xf114,
  101. 0xf603, 0xf200, 0xf30b, 0xf601, 0xf200, 0xf602, 0xf30a, 0xf200,
  102. 0xf600, 0xf200, 0xf115, 0xf07f, 0xf200, 0xf200, 0xf200, 0xf200,
  103. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  104. 0xf200, 0xf1ff, 0xf11b, 0xf312, 0xf313, 0xf30d, 0xf30c, 0xf307,
  105. 0xf308, 0xf309, 0xf304, 0xf305, 0xf306, 0xf301, 0xf302, 0xf303,
  106. 0xf300, 0xf310, 0xf30e, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  107. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200
  108. };
  109. static u_short atashift_map[NR_KEYS] __initdata = {
  110. 0xf200, 0xf01b, 0xf021, 0xf040, 0xf023, 0xf024, 0xf025, 0xf05e,
  111. 0xf026, 0xf02a, 0xf028, 0xf029, 0xf05f, 0xf02b, 0xf008, 0xf009,
  112. 0xfb51, 0xfb57, 0xfb45, 0xfb52, 0xfb54, 0xfb59, 0xfb55, 0xfb49,
  113. 0xfb4f, 0xfb50, 0xf07b, 0xf07d, 0xf201, 0xf702, 0xfb41, 0xfb53,
  114. 0xfb44, 0xfb46, 0xfb47, 0xfb48, 0xfb4a, 0xfb4b, 0xfb4c, 0xf03a,
  115. 0xf022, 0xf07e, 0xf700, 0xf07c, 0xfb5a, 0xfb58, 0xfb43, 0xfb56,
  116. 0xfb42, 0xfb4e, 0xfb4d, 0xf03c, 0xf03e, 0xf03f, 0xf700, 0xf200,
  117. 0xf703, 0xf020, 0xf207, 0xf10a, 0xf10b, 0xf10c, 0xf10d, 0xf10e,
  118. 0xf10f, 0xf110, 0xf111, 0xf112, 0xf113, 0xf200, 0xf200, 0xf117,
  119. 0xf118, 0xf200, 0xf30b, 0xf601, 0xf200, 0xf602, 0xf30a, 0xf200,
  120. 0xf119, 0xf200, 0xf115, 0xf07f, 0xf200, 0xf200, 0xf200, 0xf200,
  121. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  122. 0xf200, 0xf205, 0xf203, 0xf312, 0xf313, 0xf30d, 0xf30c, 0xf307,
  123. 0xf308, 0xf309, 0xf304, 0xf305, 0xf306, 0xf301, 0xf302, 0xf303,
  124. 0xf300, 0xf310, 0xf30e, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  125. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200
  126. };
  127. static u_short atactrl_map[NR_KEYS] __initdata = {
  128. 0xf200, 0xf200, 0xf200, 0xf000, 0xf01b, 0xf01c, 0xf01d, 0xf01e,
  129. 0xf01f, 0xf07f, 0xf200, 0xf200, 0xf01f, 0xf200, 0xf008, 0xf200,
  130. 0xf011, 0xf017, 0xf005, 0xf012, 0xf014, 0xf019, 0xf015, 0xf009,
  131. 0xf00f, 0xf010, 0xf01b, 0xf01d, 0xf201, 0xf702, 0xf001, 0xf013,
  132. 0xf004, 0xf006, 0xf007, 0xf008, 0xf00a, 0xf00b, 0xf00c, 0xf200,
  133. 0xf007, 0xf000, 0xf700, 0xf01c, 0xf01a, 0xf018, 0xf003, 0xf016,
  134. 0xf002, 0xf00e, 0xf00d, 0xf200, 0xf200, 0xf07f, 0xf700, 0xf200,
  135. 0xf703, 0xf000, 0xf207, 0xf100, 0xf101, 0xf102, 0xf103, 0xf104,
  136. 0xf105, 0xf106, 0xf107, 0xf108, 0xf109, 0xf200, 0xf200, 0xf114,
  137. 0xf603, 0xf200, 0xf30b, 0xf601, 0xf200, 0xf602, 0xf30a, 0xf200,
  138. 0xf600, 0xf200, 0xf115, 0xf07f, 0xf200, 0xf200, 0xf200, 0xf200,
  139. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  140. 0xf200, 0xf1ff, 0xf202, 0xf312, 0xf313, 0xf30d, 0xf30c, 0xf307,
  141. 0xf308, 0xf309, 0xf304, 0xf305, 0xf306, 0xf301, 0xf302, 0xf303,
  142. 0xf300, 0xf310, 0xf30e, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  143. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200
  144. };
  145. static u_short atashift_ctrl_map[NR_KEYS] __initdata = {
  146. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  147. 0xf200, 0xf200, 0xf200, 0xf200, 0xf01f, 0xf200, 0xf008, 0xf200,
  148. 0xf011, 0xf017, 0xf005, 0xf012, 0xf014, 0xf019, 0xf015, 0xf009,
  149. 0xf00f, 0xf010, 0xf200, 0xf200, 0xf201, 0xf702, 0xf001, 0xf013,
  150. 0xf004, 0xf006, 0xf007, 0xf008, 0xf00a, 0xf00b, 0xf00c, 0xf200,
  151. 0xf200, 0xf200, 0xf700, 0xf200, 0xf01a, 0xf018, 0xf003, 0xf016,
  152. 0xf002, 0xf00e, 0xf00d, 0xf200, 0xf200, 0xf07f, 0xf700, 0xf200,
  153. 0xf703, 0xf200, 0xf207, 0xf100, 0xf101, 0xf102, 0xf103, 0xf104,
  154. 0xf105, 0xf106, 0xf107, 0xf108, 0xf109, 0xf200, 0xf200, 0xf117,
  155. 0xf603, 0xf200, 0xf30b, 0xf601, 0xf200, 0xf602, 0xf30a, 0xf200,
  156. 0xf600, 0xf200, 0xf115, 0xf07f, 0xf200, 0xf200, 0xf200, 0xf200,
  157. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  158. 0xf200, 0xf1ff, 0xf11b, 0xf312, 0xf313, 0xf30d, 0xf30c, 0xf307,
  159. 0xf308, 0xf309, 0xf304, 0xf305, 0xf306, 0xf301, 0xf302, 0xf303,
  160. 0xf300, 0xf310, 0xf30e, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  161. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200
  162. };
  163. static u_short ataalt_map[NR_KEYS] __initdata = {
  164. 0xf200, 0xf81b, 0xf831, 0xf832, 0xf833, 0xf834, 0xf835, 0xf836,
  165. 0xf837, 0xf838, 0xf839, 0xf830, 0xf82d, 0xf83d, 0xf808, 0xf809,
  166. 0xf871, 0xf877, 0xf865, 0xf872, 0xf874, 0xf879, 0xf875, 0xf869,
  167. 0xf86f, 0xf870, 0xf85b, 0xf85d, 0xf80d, 0xf702, 0xf861, 0xf873,
  168. 0xf864, 0xf866, 0xf867, 0xf868, 0xf86a, 0xf86b, 0xf86c, 0xf83b,
  169. 0xf827, 0xf860, 0xf700, 0xf85c, 0xf87a, 0xf878, 0xf863, 0xf876,
  170. 0xf862, 0xf86e, 0xf86d, 0xf82c, 0xf82e, 0xf82f, 0xf700, 0xf200,
  171. 0xf703, 0xf820, 0xf208, 0xf500, 0xf501, 0xf502, 0xf503, 0xf504,
  172. 0xf505, 0xf506, 0xf507, 0xf508, 0xf509, 0xf200, 0xf200, 0xf114,
  173. 0xf20b, 0xf200, 0xf30b, 0xf601, 0xf200, 0xf602, 0xf30a, 0xf200,
  174. 0xf20a, 0xf200, 0xf209, 0xf87f, 0xf200, 0xf200, 0xf200, 0xf200,
  175. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  176. 0xf200, 0xf206, 0xf204, 0xf312, 0xf313, 0xf30d, 0xf30c, 0xf907,
  177. 0xf908, 0xf909, 0xf904, 0xf905, 0xf906, 0xf901, 0xf902, 0xf903,
  178. 0xf900, 0xf310, 0xf30e, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  179. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200
  180. };
  181. static u_short atashift_alt_map[NR_KEYS] __initdata = {
  182. 0xf200, 0xf81b, 0xf821, 0xf840, 0xf823, 0xf824, 0xf825, 0xf85e,
  183. 0xf826, 0xf82a, 0xf828, 0xf829, 0xf85f, 0xf82b, 0xf808, 0xf809,
  184. 0xf851, 0xf857, 0xf845, 0xf852, 0xf854, 0xf859, 0xf855, 0xf849,
  185. 0xf84f, 0xf850, 0xf87b, 0xf87d, 0xf201, 0xf702, 0xf841, 0xf853,
  186. 0xf844, 0xf846, 0xf847, 0xf848, 0xf84a, 0xf84b, 0xf84c, 0xf83a,
  187. 0xf822, 0xf87e, 0xf700, 0xf87c, 0xf85a, 0xf858, 0xf843, 0xf856,
  188. 0xf842, 0xf84e, 0xf84d, 0xf83c, 0xf83e, 0xf83f, 0xf700, 0xf200,
  189. 0xf703, 0xf820, 0xf207, 0xf50a, 0xf50b, 0xf50c, 0xf50d, 0xf50e,
  190. 0xf50f, 0xf510, 0xf511, 0xf512, 0xf513, 0xf200, 0xf200, 0xf117,
  191. 0xf118, 0xf200, 0xf30b, 0xf601, 0xf200, 0xf602, 0xf30a, 0xf200,
  192. 0xf119, 0xf200, 0xf115, 0xf87f, 0xf200, 0xf200, 0xf200, 0xf200,
  193. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  194. 0xf200, 0xf1ff, 0xf11b, 0xf312, 0xf313, 0xf30d, 0xf30c, 0xf307,
  195. 0xf308, 0xf309, 0xf304, 0xf305, 0xf306, 0xf301, 0xf302, 0xf303,
  196. 0xf300, 0xf310, 0xf30e, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  197. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200
  198. };
  199. static u_short atactrl_alt_map[NR_KEYS] __initdata = {
  200. 0xf200, 0xf200, 0xf200, 0xf800, 0xf81b, 0xf81c, 0xf81d, 0xf81e,
  201. 0xf81f, 0xf87f, 0xf200, 0xf200, 0xf81f, 0xf200, 0xf808, 0xf200,
  202. 0xf811, 0xf817, 0xf805, 0xf812, 0xf814, 0xf819, 0xf815, 0xf809,
  203. 0xf80f, 0xf810, 0xf81b, 0xf81d, 0xf201, 0xf702, 0xf801, 0xf813,
  204. 0xf804, 0xf806, 0xf807, 0xf808, 0xf80a, 0xf80b, 0xf80c, 0xf200,
  205. 0xf807, 0xf800, 0xf700, 0xf81c, 0xf81a, 0xf818, 0xf803, 0xf816,
  206. 0xf802, 0xf80e, 0xf80d, 0xf200, 0xf200, 0xf87f, 0xf700, 0xf200,
  207. 0xf703, 0xf800, 0xf207, 0xf100, 0xf101, 0xf102, 0xf103, 0xf104,
  208. 0xf105, 0xf106, 0xf107, 0xf108, 0xf109, 0xf200, 0xf200, 0xf114,
  209. 0xf603, 0xf200, 0xf30b, 0xf601, 0xf200, 0xf602, 0xf30a, 0xf200,
  210. 0xf600, 0xf200, 0xf115, 0xf87f, 0xf200, 0xf200, 0xf200, 0xf200,
  211. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  212. 0xf200, 0xf1ff, 0xf202, 0xf312, 0xf313, 0xf30d, 0xf30c, 0xf307,
  213. 0xf308, 0xf309, 0xf304, 0xf305, 0xf306, 0xf301, 0xf302, 0xf303,
  214. 0xf300, 0xf310, 0xf30e, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  215. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200
  216. };
  217. static u_short atashift_ctrl_alt_map[NR_KEYS] = {
  218. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  219. 0xf200, 0xf200, 0xf200, 0xf200, 0xf81f, 0xf200, 0xf808, 0xf200,
  220. 0xf811, 0xf817, 0xf805, 0xf812, 0xf814, 0xf819, 0xf815, 0xf809,
  221. 0xf80f, 0xf810, 0xf200, 0xf200, 0xf201, 0xf702, 0xf801, 0xf813,
  222. 0xf804, 0xf806, 0xf807, 0xf808, 0xf80a, 0xf80b, 0xf80c, 0xf200,
  223. 0xf200, 0xf200, 0xf700, 0xf200, 0xf81a, 0xf818, 0xf803, 0xf816,
  224. 0xf802, 0xf80e, 0xf80d, 0xf200, 0xf200, 0xf87f, 0xf700, 0xf200,
  225. 0xf703, 0xf200, 0xf207, 0xf100, 0xf101, 0xf102, 0xf103, 0xf104,
  226. 0xf105, 0xf106, 0xf107, 0xf108, 0xf109, 0xf200, 0xf200, 0xf117,
  227. 0xf603, 0xf200, 0xf30b, 0xf601, 0xf200, 0xf602, 0xf30a, 0xf200,
  228. 0xf600, 0xf200, 0xf115, 0xf87f, 0xf200, 0xf200, 0xf200, 0xf200,
  229. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  230. 0xf200, 0xf1ff, 0xf11b, 0xf312, 0xf313, 0xf30d, 0xf30c, 0xf307,
  231. 0xf308, 0xf309, 0xf304, 0xf305, 0xf306, 0xf301, 0xf302, 0xf303,
  232. 0xf300, 0xf310, 0xf30e, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
  233. 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200
  234. };
  235. typedef enum kb_state_t
  236. {
  237.     KEYBOARD, AMOUSE, RMOUSE, JOYSTICK, CLOCK, RESYNC
  238. } KB_STATE_T;
  239. #define IS_SYNC_CODE(sc) ((sc) >= 0x04 && (sc) <= 0xfb)
  240. typedef struct keyboard_state
  241. {
  242.     unsigned char  buf[6];
  243.     int     len;
  244.     KB_STATE_T    state;
  245. } KEYBOARD_STATE;
  246. KEYBOARD_STATE kb_state;
  247. #define DEFAULT_KEYB_REP_DELAY (HZ/4)
  248. #define DEFAULT_KEYB_REP_RATE (HZ/25)
  249. /* These could be settable by some ioctl() in future... */
  250. static unsigned int key_repeat_delay = DEFAULT_KEYB_REP_DELAY;
  251. static unsigned int key_repeat_rate  = DEFAULT_KEYB_REP_RATE;
  252. static unsigned char rep_scancode;
  253. static struct timer_list atakeyb_rep_timer = { function: atakeyb_rep };
  254. static void atakeyb_rep( unsigned long ignore )
  255. {
  256. kbd_pt_regs = NULL;
  257. /* Disable keyboard for the time we call handle_scancode(), else a race
  258.  * in the keyboard tty queue may happen */
  259. atari_disable_irq( IRQ_MFP_ACIA );
  260. del_timer( &atakeyb_rep_timer );
  261. /* A keyboard int may have come in before we disabled the irq, so
  262.  * double-check whether rep_scancode is still != 0 */
  263. if (rep_scancode) {
  264. init_timer(&atakeyb_rep_timer);
  265. atakeyb_rep_timer.expires = jiffies + key_repeat_rate;
  266. add_timer( &atakeyb_rep_timer );
  267. handle_scancode(rep_scancode, 1);
  268. }
  269. atari_enable_irq( IRQ_MFP_ACIA );
  270. }
  271. /* ++roman: If a keyboard overrun happened, we can't tell in general how much
  272.  * bytes have been lost and in which state of the packet structure we are now.
  273.  * This usually causes keyboards bytes to be interpreted as mouse movements
  274.  * and vice versa, which is very annoying. It seems better to throw away some
  275.  * bytes (that are usually mouse bytes) than to misinterpret them. Therefor I
  276.  * introduced the RESYNC state for IKBD data. In this state, the bytes up to
  277.  * one that really looks like a key event (0x04..0xf2) or the start of a mouse
  278.  * packet (0xf8..0xfb) are thrown away, but at most 2 bytes. This at least
  279.  * speeds up the resynchronization of the event structure, even if maybe a
  280.  * mouse movement is lost. However, nothing is perfect. For bytes 0x01..0x03,
  281.  * it's really hard to decide whether they're mouse or keyboard bytes. Since
  282.  * overruns usually occur when moving the Atari mouse rapidly, they're seen as
  283.  * mouse bytes here. If this is wrong, only a make code of the keyboard gets
  284.  * lost, which isn't too bad. Loosing a break code would be disastrous,
  285.  * because then the keyboard repeat strikes...
  286.  */
  287. static void keyboard_interrupt(int irq, void *dummy, struct pt_regs *fp)
  288. {
  289.   u_char acia_stat;
  290.   int scancode;
  291.   int break_flag;
  292.   /* save frame for register dump */
  293.   kbd_pt_regs = fp;
  294.  repeat:
  295.   if (acia.mid_ctrl & ACIA_IRQ)
  296. if (atari_MIDI_interrupt_hook)
  297. atari_MIDI_interrupt_hook();
  298.   acia_stat = acia.key_ctrl;
  299.   /* check out if the interrupt came from this ACIA */
  300.   if (!((acia_stat | acia.mid_ctrl) & ACIA_IRQ))
  301. return;
  302.     if (acia_stat & ACIA_OVRN)
  303.     {
  304. /* a very fast typist or a slow system, give a warning */
  305. /* ...happens often if interrupts were disabled for too long */
  306. printk( KERN_DEBUG "Keyboard overrunn" );
  307. scancode = acia.key_data;
  308. /* Turn off autorepeating in case a break code has been lost */
  309. del_timer( &atakeyb_rep_timer );
  310. rep_scancode = 0;
  311. if (ikbd_self_test)
  312.     /* During self test, don't do resyncing, just process the code */
  313.     goto interpret_scancode;
  314. else if (IS_SYNC_CODE(scancode)) {
  315.     /* This code seem already to be the start of a new packet or a
  316.      * single scancode */
  317.     kb_state.state = KEYBOARD;
  318.     goto interpret_scancode;
  319. }
  320. else {
  321.     /* Go to RESYNC state and skip this byte */
  322.     kb_state.state = RESYNC;
  323.     kb_state.len = 1; /* skip max. 1 another byte */
  324.     goto repeat;
  325. }
  326.     }
  327.     if (acia_stat & ACIA_RDRF) /* received a character */
  328.     {
  329. scancode = acia.key_data; /* get it or reset the ACIA, I'll get it! */
  330. tasklet_schedule(&keyboard_tasklet);
  331.       interpret_scancode:
  332. switch (kb_state.state)
  333. {
  334.   case KEYBOARD:
  335.     switch (scancode)
  336.     {
  337.       case 0xF7:
  338. kb_state.state = AMOUSE;
  339. kb_state.len = 0;
  340. break;
  341.       case 0xF8:
  342.       case 0xF9:
  343.             case 0xFA:
  344.       case 0xFB:
  345. kb_state.state = RMOUSE;
  346.      kb_state.len = 1;
  347. kb_state.buf[0] = scancode;
  348. break;
  349.       case 0xFC:
  350. kb_state.state = CLOCK;
  351. kb_state.len = 0;
  352. break;
  353.       case 0xFE:
  354.       case 0xFF:
  355. kb_state.state = JOYSTICK;
  356. kb_state.len = 1;
  357. kb_state.buf[0] = scancode;
  358. break;
  359.       case 0xF1:
  360. /* during self-test, note that 0xf1 received */
  361. if (ikbd_self_test) {
  362.     ++ikbd_self_test;
  363.     self_test_last_rcv = jiffies;
  364.     break;
  365. }
  366. /* FALL THROUGH */
  367.       default:
  368. break_flag = scancode & BREAK_MASK;
  369. scancode &= ~BREAK_MASK;
  370. if (ikbd_self_test) {
  371.     /* Scancodes sent during the self-test stand for broken
  372.      * keys (keys being down). The code *should* be a break
  373.      * code, but nevertheless some AT keyboard interfaces send
  374.      * make codes instead. Therefore, simply ignore
  375.      * break_flag...
  376.      * */
  377.     int keyval = plain_map[scancode], keytyp;
  378.     
  379.     set_bit( scancode, broken_keys );
  380.     self_test_last_rcv = jiffies;
  381.     keyval = plain_map[scancode];
  382.     keytyp = KTYP(keyval) - 0xf0;
  383.     keyval = KVAL(keyval);
  384.     printk( KERN_WARNING "Key with scancode %d ", scancode );
  385.     if (keytyp == KT_LATIN || keytyp == KT_LETTER) {
  386. if (keyval < ' ')
  387.     printk( "('^%c') ", keyval + '@' );
  388. else
  389.     printk( "('%c') ", keyval );
  390.     }
  391.     printk( "is broken -- will be ignored.n" );
  392.     break;
  393. }
  394. else if (test_bit( scancode, broken_keys ))
  395.     break;
  396. if (break_flag) {
  397.     del_timer( &atakeyb_rep_timer );
  398.     rep_scancode = 0;
  399. }
  400. else {
  401.     del_timer( &atakeyb_rep_timer );
  402.     rep_scancode = scancode;
  403.     atakeyb_rep_timer.expires = jiffies + key_repeat_delay;
  404.     add_timer( &atakeyb_rep_timer );
  405. }
  406. handle_scancode(scancode, !break_flag);
  407. break;
  408.     }
  409.     break;
  410.   case AMOUSE:
  411.     kb_state.buf[kb_state.len++] = scancode;
  412.     if (kb_state.len == 5)
  413.     {
  414. kb_state.state = KEYBOARD;
  415. /* not yet used */
  416. /* wake up someone waiting for this */
  417.     }
  418.     break;
  419.   case RMOUSE:
  420.     kb_state.buf[kb_state.len++] = scancode;
  421.     if (kb_state.len == 3)
  422.     {
  423. kb_state.state = KEYBOARD;
  424. if (atari_mouse_interrupt_hook)
  425. atari_mouse_interrupt_hook(kb_state.buf);
  426.     }
  427.     break;
  428.   case JOYSTICK:
  429.     kb_state.buf[1] = scancode;
  430.     kb_state.state = KEYBOARD;
  431.     atari_joystick_interrupt(kb_state.buf);
  432.     break;
  433.   case CLOCK:
  434.     kb_state.buf[kb_state.len++] = scancode;
  435.     if (kb_state.len == 6)
  436.     {
  437. kb_state.state = KEYBOARD;
  438. /* wake up someone waiting for this.
  439.    But will this ever be used, as Linux keeps its own time.
  440.    Perhaps for synchronization purposes? */
  441. /* wake_up_interruptible(&clock_wait); */
  442.     }
  443.     break;
  444.   case RESYNC:
  445.     if (kb_state.len <= 0 || IS_SYNC_CODE(scancode)) {
  446. kb_state.state = KEYBOARD;
  447. goto interpret_scancode;
  448.     }
  449.     kb_state.len--;
  450.     break;
  451. }
  452.     }
  453. #if 0
  454.     if (acia_stat & ACIA_CTS)
  455. /* cannot happen */;
  456. #endif
  457.     if (acia_stat & (ACIA_FE | ACIA_PE))
  458.     {
  459. printk("Error in keyboard communicationn");
  460.     }
  461.     /* handle_scancode() can take a lot of time, so check again if
  462.  * some character arrived
  463.  */
  464.     goto repeat;
  465. }
  466. /*
  467.  * I write to the keyboard without using interrupts, I poll instead.
  468.  * This takes for the maximum length string allowed (7) at 7812.5 baud
  469.  * 8 data 1 start 1 stop bit: 9.0 ms
  470.  * If this takes too long for normal operation, interrupt driven writing
  471.  * is the solution. (I made a feeble attempt in that direction but I
  472.  * kept it simple for now.)
  473.  */
  474. void ikbd_write(const char *str, int len)
  475. {
  476.     u_char acia_stat;
  477.     if ((len < 1) || (len > 7))
  478. panic("ikbd: maximum string length exceeded");
  479.     while (len)
  480.     {
  481. acia_stat = acia.key_ctrl;
  482. if (acia_stat & ACIA_TDRE)
  483. {
  484.     acia.key_data = *str++;
  485.     len--;
  486. }
  487.     }
  488. }
  489. /* Reset (without touching the clock) */
  490. void ikbd_reset(void)
  491. {
  492.     static const char cmd[2] = { 0x80, 0x01 };
  493.     
  494.     ikbd_write(cmd, 2);
  495.     /* if all's well code 0xF1 is returned, else the break codes of
  496.        all keys making contact */
  497. }
  498. /* Set mouse button action */
  499. void ikbd_mouse_button_action(int mode)
  500. {
  501.     char cmd[2] = { 0x07, mode };
  502.     ikbd_write(cmd, 2);
  503. }
  504. /* Set relative mouse position reporting */
  505. void ikbd_mouse_rel_pos(void)
  506. {
  507.     static const char cmd[1] = { 0x08 };
  508.     ikbd_write(cmd, 1);
  509. }
  510. /* Set absolute mouse position reporting */
  511. void ikbd_mouse_abs_pos(int xmax, int ymax)
  512. {
  513.     char cmd[5] = { 0x09, xmax>>8, xmax&0xFF, ymax>>8, ymax&0xFF };
  514.     ikbd_write(cmd, 5);
  515. }
  516. /* Set mouse keycode mode */
  517. void ikbd_mouse_kbd_mode(int dx, int dy)
  518. {
  519.     char cmd[3] = { 0x0A, dx, dy };
  520.     ikbd_write(cmd, 3);
  521. }
  522. /* Set mouse threshold */
  523. void ikbd_mouse_thresh(int x, int y)
  524. {
  525.     char cmd[3] = { 0x0B, x, y };
  526.     ikbd_write(cmd, 3);
  527. }
  528. /* Set mouse scale */
  529. void ikbd_mouse_scale(int x, int y)
  530. {
  531.     char cmd[3] = { 0x0C, x, y };
  532.     ikbd_write(cmd, 3);
  533. }
  534. /* Interrogate mouse position */
  535. void ikbd_mouse_pos_get(int *x, int *y)
  536. {
  537.     static const char cmd[1] = { 0x0D };
  538.     ikbd_write(cmd, 1);
  539.     /* wait for returning bytes */
  540. }
  541. /* Load mouse position */
  542. void ikbd_mouse_pos_set(int x, int y)
  543. {
  544.     char cmd[6] = { 0x0E, 0x00, x>>8, x&0xFF, y>>8, y&0xFF };
  545.     ikbd_write(cmd, 6);
  546. }
  547. /* Set Y=0 at bottom */
  548. void ikbd_mouse_y0_bot(void)
  549. {
  550.     static const char cmd[1] = { 0x0F };
  551.     ikbd_write(cmd, 1);
  552. }
  553. /* Set Y=0 at top */
  554. void ikbd_mouse_y0_top(void)
  555. {
  556.     static const char cmd[1] = { 0x10 };
  557.     ikbd_write(cmd, 1);
  558. }
  559. /* Resume */
  560. void ikbd_resume(void)
  561. {
  562.     static const char cmd[1] = { 0x11 };
  563.     ikbd_write(cmd, 1);
  564. }
  565. /* Disable mouse */
  566. void ikbd_mouse_disable(void)
  567. {
  568.     static const char cmd[1] = { 0x12 };
  569.     ikbd_write(cmd, 1);
  570. }
  571. /* Pause output */
  572. void ikbd_pause(void)
  573. {
  574.     static const char cmd[1] = { 0x13 };
  575.     ikbd_write(cmd, 1);
  576. }
  577. /* Set joystick event reporting */
  578. void ikbd_joystick_event_on(void)
  579. {
  580.     static const char cmd[1] = { 0x14 };
  581.     ikbd_write(cmd, 1);
  582. }
  583. /* Set joystick interrogation mode */
  584. void ikbd_joystick_event_off(void)
  585. {
  586.     static const char cmd[1] = { 0x15 };
  587.     ikbd_write(cmd, 1);
  588. }
  589. /* Joystick interrogation */
  590. void ikbd_joystick_get_state(void)
  591. {
  592.     static const char cmd[1] = { 0x16 };
  593.     ikbd_write(cmd, 1);
  594. }
  595. #if 0
  596. /* This disables all other ikbd activities !!!! */
  597. /* Set joystick monitoring */
  598. void ikbd_joystick_monitor(int rate)
  599. {
  600.     static const char cmd[2] = { 0x17, rate };
  601.     ikbd_write(cmd, 2);
  602.     kb_state.state = JOYSTICK_MONITOR;
  603. }
  604. #endif
  605. /* some joystick routines not in yet (0x18-0x19) */
  606. /* Disable joysticks */
  607. void ikbd_joystick_disable(void)
  608. {
  609.     static const char cmd[1] = { 0x1A };
  610.     ikbd_write(cmd, 1);
  611. }
  612. /* Time-of-day clock set */
  613. void ikbd_clock_set(int year, int month, int day, int hour, int minute, int second)
  614. {
  615.     char cmd[7] = { 0x1B, year, month, day, hour, minute, second };
  616.     ikbd_write(cmd, 7);
  617. }
  618. /* Interrogate time-of-day clock */
  619. void ikbd_clock_get(int *year, int *month, int *day, int *hour, int *minute, int second)
  620. {
  621.     static const char cmd[1] = { 0x1C };
  622.     ikbd_write(cmd, 1);
  623. }
  624. /* Memory load */
  625. void ikbd_mem_write(int address, int size, char *data)
  626. {
  627.     panic("Attempt to write data into keyboard memory");
  628. }
  629. /* Memory read */
  630. void ikbd_mem_read(int address, char data[6])
  631. {
  632.     char cmd[3] = { 0x21, address>>8, address&0xFF };
  633.     ikbd_write(cmd, 3);
  634.     /* receive data and put it in data */
  635. }
  636. /* Controller execute */
  637. void ikbd_exec(int address)
  638. {
  639.     char cmd[3] = { 0x22, address>>8, address&0xFF };
  640.     ikbd_write(cmd, 3);
  641. }
  642. /* Status inquiries (0x87-0x9A) not yet implemented */
  643. /* Set the state of the caps lock led. */
  644. void atari_kbd_leds (unsigned int leds)
  645. {
  646.     char cmd[6] = {32, 0, 4, 1, 254 + ((leds & 4) != 0), 0};
  647.     ikbd_write(cmd, 6);
  648. }
  649. /*
  650.  * The original code sometimes left the interrupt line of 
  651.  * the ACIAs low forever. I hope, it is fixed now.
  652.  *
  653.  * Martin Rogge, 20 Aug 1995
  654.  */
  655.  
  656. int __init atari_keyb_init(void)
  657. {
  658.     /* setup key map */
  659.     memcpy(key_maps[0], ataplain_map, sizeof(plain_map));
  660.     memcpy(key_maps[1], atashift_map, sizeof(plain_map));
  661.     memcpy(key_maps[4], atactrl_map, sizeof(plain_map));
  662.     memcpy(key_maps[5], atashift_ctrl_map, sizeof(plain_map));
  663.     memcpy(key_maps[8], ataalt_map, sizeof(plain_map));
  664.     /* Atari doesn't have an altgr_map, so we can reuse its memory for
  665.        atashift_alt_map */
  666.     memcpy(key_maps[2], atashift_alt_map, sizeof(plain_map));
  667.     key_maps[9]  = key_maps[2];
  668.     key_maps[2]  = 0; /* ataaltgr_map */
  669.     memcpy(key_maps[12], atactrl_alt_map, sizeof(plain_map));
  670.     key_maps[13] = atashift_ctrl_alt_map;
  671.     keymap_count = 8;
  672.     /* say that we don't have an AltGr key */
  673.     keyboard_type = KB_84;
  674.     kb_state.state = KEYBOARD;
  675.     kb_state.len = 0;
  676.     request_irq(IRQ_MFP_ACIA, keyboard_interrupt, IRQ_TYPE_SLOW,
  677.                 "keyboard/mouse/MIDI", keyboard_interrupt);
  678.     atari_turnoff_irq(IRQ_MFP_ACIA);
  679.     do {
  680. /* reset IKBD ACIA */
  681. acia.key_ctrl = ACIA_RESET |
  682. (atari_switches & ATARI_SWITCH_IKBD) ? ACIA_RHTID : 0;
  683. (void)acia.key_ctrl;
  684. (void)acia.key_data;
  685. /* reset MIDI ACIA */
  686. acia.mid_ctrl = ACIA_RESET |
  687. (atari_switches & ATARI_SWITCH_MIDI) ? ACIA_RHTID : 0;
  688. (void)acia.mid_ctrl;
  689. (void)acia.mid_data;
  690. /* divide 500kHz by 64 gives 7812.5 baud */
  691. /* 8 data no parity 1 start 1 stop bit */
  692. /* receive interrupt enabled */
  693. /* RTS low (except if switch selected), transmit interrupt disabled */
  694. acia.key_ctrl = (ACIA_DIV64|ACIA_D8N1S|ACIA_RIE) |
  695. ((atari_switches & ATARI_SWITCH_IKBD) ?
  696.  ACIA_RHTID : ACIA_RLTID);
  697.    
  698. acia.mid_ctrl = ACIA_DIV16 | ACIA_D8N1S |
  699. (atari_switches & ATARI_SWITCH_MIDI) ? ACIA_RHTID : 0;
  700.     }
  701.     /* make sure the interrupt line is up */
  702.     while ((mfp.par_dt_reg & 0x10) == 0);
  703.     /* enable ACIA Interrupts */ 
  704.     mfp.active_edge &= ~0x10;
  705.     atari_turnon_irq(IRQ_MFP_ACIA);
  706.     ikbd_self_test = 1;
  707.     ikbd_reset();
  708.     /* wait for a period of inactivity (here: 0.25s), then assume the IKBD's
  709.      * self-test is finished */
  710.     self_test_last_rcv = jiffies;
  711.     while (time_before(jiffies, self_test_last_rcv + HZ/4))
  712. barrier();
  713.     /* if not incremented: no 0xf1 received */
  714.     if (ikbd_self_test == 1)
  715. printk( KERN_ERR "WARNING: keyboard self test failed!n" );
  716.     ikbd_self_test = 0;
  717.     
  718.     ikbd_mouse_disable();
  719.     ikbd_joystick_disable();
  720.     atari_joystick_init();
  721.   
  722.     return 0;
  723. }
  724. int atari_kbdrate( struct kbd_repeat *k )
  725. {
  726. if (k->delay > 0) {
  727. /* convert from msec to jiffies */
  728. key_repeat_delay = (k->delay * HZ + 500) / 1000;
  729. if (key_repeat_delay < 1)
  730. key_repeat_delay = 1;
  731. }
  732. if (k->rate > 0) {
  733. key_repeat_rate = (k->rate * HZ + 500) / 1000;
  734. if (key_repeat_rate < 1)
  735. key_repeat_rate = 1;
  736. }
  737. k->delay = key_repeat_delay * 1000 / HZ;
  738. k->rate  = key_repeat_rate  * 1000 / HZ;
  739. return( 0 );
  740. }
  741. int atari_kbd_translate(unsigned char keycode, unsigned char *keycodep, char raw_mode)
  742. {
  743. #ifdef CONFIG_MAGIC_SYSRQ
  744.         /* ALT+HELP pressed? */
  745.         if ((keycode == 98) && ((shift_state & 0xff) == 8))
  746.                 *keycodep = 0xff;
  747.         else
  748. #endif
  749.                 *keycodep = keycode;
  750.         return 1;
  751. }