atakeyb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. /*
  2. * Atari Keyboard driver for 680x0 Linux
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License. See the file COPYING in the main directory of this archive
  6. * for more details.
  7. */
  8. /*
  9. * Atari support by Robert de Vries
  10. * enhanced by Bjoern Brauel and Roman Hodek
  11. *
  12. * 2.6 and input cleanup (removed autorepeat stuff) for 2.6.21
  13. * 06/07 Michael Schmitz
  14. */
  15. #include <linux/module.h>
  16. #include <linux/sched.h>
  17. #include <linux/kernel.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/errno.h>
  20. #include <linux/keyboard.h>
  21. #include <linux/delay.h>
  22. #include <linux/timer.h>
  23. #include <linux/kd.h>
  24. #include <linux/random.h>
  25. #include <linux/init.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. /* Hook for MIDI serial driver */
  33. void (*atari_MIDI_interrupt_hook) (void);
  34. /* Hook for keyboard inputdev driver */
  35. void (*atari_input_keyboard_interrupt_hook) (unsigned char, char);
  36. /* Hook for mouse inputdev driver */
  37. void (*atari_input_mouse_interrupt_hook) (char *);
  38. EXPORT_SYMBOL(atari_input_keyboard_interrupt_hook);
  39. EXPORT_SYMBOL(atari_input_mouse_interrupt_hook);
  40. /* variables for IKBD self test: */
  41. /* state: 0: off; >0: in progress; >1: 0xf1 received */
  42. static volatile int ikbd_self_test;
  43. /* timestamp when last received a char */
  44. static volatile unsigned long self_test_last_rcv;
  45. /* bitmap of keys reported as broken */
  46. static unsigned long broken_keys[128/(sizeof(unsigned long)*8)] = { 0, };
  47. #define BREAK_MASK (0x80)
  48. /*
  49. * ++roman: The following changes were applied manually:
  50. *
  51. * - The Alt (= Meta) key works in combination with Shift and
  52. * Control, e.g. Alt+Shift+a sends Meta-A (0xc1), Alt+Control+A sends
  53. * Meta-Ctrl-A (0x81) ...
  54. *
  55. * - The parentheses on the keypad send '(' and ')' with all
  56. * modifiers (as would do e.g. keypad '+'), but they cannot be used as
  57. * application keys (i.e. sending Esc O c).
  58. *
  59. * - HELP and UNDO are mapped to be F21 and F24, resp, that send the
  60. * codes "\E[M" and "\E[P". (This is better than the old mapping to
  61. * F11 and F12, because these codes are on Shift+F1/2 anyway.) This
  62. * way, applications that allow their own keyboard mappings
  63. * (e.g. tcsh, X Windows) can be configured to use them in the way
  64. * the label suggests (providing help or undoing).
  65. *
  66. * - Console switching is done with Alt+Fx (consoles 1..10) and
  67. * Shift+Alt+Fx (consoles 11..20).
  68. *
  69. * - The misc. special function implemented in the kernel are mapped
  70. * to the following key combinations:
  71. *
  72. * ClrHome -> Home/Find
  73. * Shift + ClrHome -> End/Select
  74. * Shift + Up -> Page Up
  75. * Shift + Down -> Page Down
  76. * Alt + Help -> show system status
  77. * Shift + Help -> show memory info
  78. * Ctrl + Help -> show registers
  79. * Ctrl + Alt + Del -> Reboot
  80. * Alt + Undo -> switch to last console
  81. * Shift + Undo -> send interrupt
  82. * Alt + Insert -> stop/start output (same as ^S/^Q)
  83. * Alt + Up -> Scroll back console (if implemented)
  84. * Alt + Down -> Scroll forward console (if implemented)
  85. * Alt + CapsLock -> NumLock
  86. *
  87. * ++Andreas:
  88. *
  89. * - Help mapped to K_HELP
  90. * - Undo mapped to K_UNDO (= K_F246)
  91. * - Keypad Left/Right Parenthesis mapped to new K_PPAREN[LR]
  92. */
  93. typedef enum kb_state_t {
  94. KEYBOARD, AMOUSE, RMOUSE, JOYSTICK, CLOCK, RESYNC
  95. } KB_STATE_T;
  96. #define IS_SYNC_CODE(sc) ((sc) >= 0x04 && (sc) <= 0xfb)
  97. typedef struct keyboard_state {
  98. unsigned char buf[6];
  99. int len;
  100. KB_STATE_T state;
  101. } KEYBOARD_STATE;
  102. KEYBOARD_STATE kb_state;
  103. /* ++roman: If a keyboard overrun happened, we can't tell in general how much
  104. * bytes have been lost and in which state of the packet structure we are now.
  105. * This usually causes keyboards bytes to be interpreted as mouse movements
  106. * and vice versa, which is very annoying. It seems better to throw away some
  107. * bytes (that are usually mouse bytes) than to misinterpret them. Therefore I
  108. * introduced the RESYNC state for IKBD data. In this state, the bytes up to
  109. * one that really looks like a key event (0x04..0xf2) or the start of a mouse
  110. * packet (0xf8..0xfb) are thrown away, but at most 2 bytes. This at least
  111. * speeds up the resynchronization of the event structure, even if maybe a
  112. * mouse movement is lost. However, nothing is perfect. For bytes 0x01..0x03,
  113. * it's really hard to decide whether they're mouse or keyboard bytes. Since
  114. * overruns usually occur when moving the Atari mouse rapidly, they're seen as
  115. * mouse bytes here. If this is wrong, only a make code of the keyboard gets
  116. * lost, which isn't too bad. Losing a break code would be disastrous,
  117. * because then the keyboard repeat strikes...
  118. */
  119. static irqreturn_t atari_keyboard_interrupt(int irq, void *dummy)
  120. {
  121. u_char acia_stat;
  122. int scancode;
  123. int break_flag;
  124. repeat:
  125. if (acia.mid_ctrl & ACIA_IRQ)
  126. if (atari_MIDI_interrupt_hook)
  127. atari_MIDI_interrupt_hook();
  128. acia_stat = acia.key_ctrl;
  129. /* check out if the interrupt came from this ACIA */
  130. if (!((acia_stat | acia.mid_ctrl) & ACIA_IRQ))
  131. return IRQ_HANDLED;
  132. if (acia_stat & ACIA_OVRN) {
  133. /* a very fast typist or a slow system, give a warning */
  134. /* ...happens often if interrupts were disabled for too long */
  135. pr_debug("Keyboard overrun\n");
  136. scancode = acia.key_data;
  137. if (ikbd_self_test)
  138. /* During self test, don't do resyncing, just process the code */
  139. goto interpret_scancode;
  140. else if (IS_SYNC_CODE(scancode)) {
  141. /* This code seem already to be the start of a new packet or a
  142. * single scancode */
  143. kb_state.state = KEYBOARD;
  144. goto interpret_scancode;
  145. } else {
  146. /* Go to RESYNC state and skip this byte */
  147. kb_state.state = RESYNC;
  148. kb_state.len = 1; /* skip max. 1 another byte */
  149. goto repeat;
  150. }
  151. }
  152. if (acia_stat & ACIA_RDRF) {
  153. /* received a character */
  154. scancode = acia.key_data; /* get it or reset the ACIA, I'll get it! */
  155. interpret_scancode:
  156. switch (kb_state.state) {
  157. case KEYBOARD:
  158. switch (scancode) {
  159. case 0xF7:
  160. kb_state.state = AMOUSE;
  161. kb_state.len = 0;
  162. break;
  163. case 0xF8:
  164. case 0xF9:
  165. case 0xFA:
  166. case 0xFB:
  167. kb_state.state = RMOUSE;
  168. kb_state.len = 1;
  169. kb_state.buf[0] = scancode;
  170. break;
  171. case 0xFC:
  172. kb_state.state = CLOCK;
  173. kb_state.len = 0;
  174. break;
  175. case 0xFE:
  176. case 0xFF:
  177. kb_state.state = JOYSTICK;
  178. kb_state.len = 1;
  179. kb_state.buf[0] = scancode;
  180. break;
  181. case 0xF1:
  182. /* during self-test, note that 0xf1 received */
  183. if (ikbd_self_test) {
  184. ++ikbd_self_test;
  185. self_test_last_rcv = jiffies;
  186. break;
  187. }
  188. /* FALL THROUGH */
  189. default:
  190. break_flag = scancode & BREAK_MASK;
  191. scancode &= ~BREAK_MASK;
  192. if (ikbd_self_test) {
  193. /* Scancodes sent during the self-test stand for broken
  194. * keys (keys being down). The code *should* be a break
  195. * code, but nevertheless some AT keyboard interfaces send
  196. * make codes instead. Therefore, simply ignore
  197. * break_flag...
  198. */
  199. int keyval, keytyp;
  200. set_bit(scancode, broken_keys);
  201. self_test_last_rcv = jiffies;
  202. /* new Linux scancodes; approx. */
  203. keyval = scancode;
  204. keytyp = KTYP(keyval) - 0xf0;
  205. keyval = KVAL(keyval);
  206. pr_warn("Key with scancode %d ", scancode);
  207. if (keytyp == KT_LATIN || keytyp == KT_LETTER) {
  208. if (keyval < ' ')
  209. pr_cont("('^%c') ", keyval + '@');
  210. else
  211. pr_cont("('%c') ", keyval);
  212. }
  213. pr_cont("is broken -- will be ignored.\n");
  214. break;
  215. } else if (test_bit(scancode, broken_keys))
  216. break;
  217. if (atari_input_keyboard_interrupt_hook)
  218. atari_input_keyboard_interrupt_hook((unsigned char)scancode, !break_flag);
  219. break;
  220. }
  221. break;
  222. case AMOUSE:
  223. kb_state.buf[kb_state.len++] = scancode;
  224. if (kb_state.len == 5) {
  225. kb_state.state = KEYBOARD;
  226. /* not yet used */
  227. /* wake up someone waiting for this */
  228. }
  229. break;
  230. case RMOUSE:
  231. kb_state.buf[kb_state.len++] = scancode;
  232. if (kb_state.len == 3) {
  233. kb_state.state = KEYBOARD;
  234. if (atari_input_mouse_interrupt_hook)
  235. atari_input_mouse_interrupt_hook(kb_state.buf);
  236. }
  237. break;
  238. case JOYSTICK:
  239. kb_state.buf[1] = scancode;
  240. kb_state.state = KEYBOARD;
  241. #ifdef FIXED_ATARI_JOYSTICK
  242. atari_joystick_interrupt(kb_state.buf);
  243. #endif
  244. break;
  245. case CLOCK:
  246. kb_state.buf[kb_state.len++] = scancode;
  247. if (kb_state.len == 6) {
  248. kb_state.state = KEYBOARD;
  249. /* wake up someone waiting for this.
  250. But will this ever be used, as Linux keeps its own time.
  251. Perhaps for synchronization purposes? */
  252. /* wake_up_interruptible(&clock_wait); */
  253. }
  254. break;
  255. case RESYNC:
  256. if (kb_state.len <= 0 || IS_SYNC_CODE(scancode)) {
  257. kb_state.state = KEYBOARD;
  258. goto interpret_scancode;
  259. }
  260. kb_state.len--;
  261. break;
  262. }
  263. }
  264. #if 0
  265. if (acia_stat & ACIA_CTS)
  266. /* cannot happen */;
  267. #endif
  268. if (acia_stat & (ACIA_FE | ACIA_PE)) {
  269. pr_err("Error in keyboard communication\n");
  270. }
  271. /* handle_scancode() can take a lot of time, so check again if
  272. * some character arrived
  273. */
  274. goto repeat;
  275. }
  276. /*
  277. * I write to the keyboard without using interrupts, I poll instead.
  278. * This takes for the maximum length string allowed (7) at 7812.5 baud
  279. * 8 data 1 start 1 stop bit: 9.0 ms
  280. * If this takes too long for normal operation, interrupt driven writing
  281. * is the solution. (I made a feeble attempt in that direction but I
  282. * kept it simple for now.)
  283. */
  284. void ikbd_write(const char *str, int len)
  285. {
  286. u_char acia_stat;
  287. if ((len < 1) || (len > 7))
  288. panic("ikbd: maximum string length exceeded");
  289. while (len) {
  290. acia_stat = acia.key_ctrl;
  291. if (acia_stat & ACIA_TDRE) {
  292. acia.key_data = *str++;
  293. len--;
  294. }
  295. }
  296. }
  297. /* Reset (without touching the clock) */
  298. void ikbd_reset(void)
  299. {
  300. static const char cmd[2] = { 0x80, 0x01 };
  301. ikbd_write(cmd, 2);
  302. /*
  303. * if all's well code 0xF1 is returned, else the break codes of
  304. * all keys making contact
  305. */
  306. }
  307. /* Set mouse button action */
  308. void ikbd_mouse_button_action(int mode)
  309. {
  310. char cmd[2] = { 0x07, mode };
  311. ikbd_write(cmd, 2);
  312. }
  313. /* Set relative mouse position reporting */
  314. void ikbd_mouse_rel_pos(void)
  315. {
  316. static const char cmd[1] = { 0x08 };
  317. ikbd_write(cmd, 1);
  318. }
  319. EXPORT_SYMBOL(ikbd_mouse_rel_pos);
  320. /* Set absolute mouse position reporting */
  321. void ikbd_mouse_abs_pos(int xmax, int ymax)
  322. {
  323. char cmd[5] = { 0x09, xmax>>8, xmax&0xFF, ymax>>8, ymax&0xFF };
  324. ikbd_write(cmd, 5);
  325. }
  326. /* Set mouse keycode mode */
  327. void ikbd_mouse_kbd_mode(int dx, int dy)
  328. {
  329. char cmd[3] = { 0x0A, dx, dy };
  330. ikbd_write(cmd, 3);
  331. }
  332. /* Set mouse threshold */
  333. void ikbd_mouse_thresh(int x, int y)
  334. {
  335. char cmd[3] = { 0x0B, x, y };
  336. ikbd_write(cmd, 3);
  337. }
  338. EXPORT_SYMBOL(ikbd_mouse_thresh);
  339. /* Set mouse scale */
  340. void ikbd_mouse_scale(int x, int y)
  341. {
  342. char cmd[3] = { 0x0C, x, y };
  343. ikbd_write(cmd, 3);
  344. }
  345. /* Interrogate mouse position */
  346. void ikbd_mouse_pos_get(int *x, int *y)
  347. {
  348. static const char cmd[1] = { 0x0D };
  349. ikbd_write(cmd, 1);
  350. /* wait for returning bytes */
  351. }
  352. /* Load mouse position */
  353. void ikbd_mouse_pos_set(int x, int y)
  354. {
  355. char cmd[6] = { 0x0E, 0x00, x>>8, x&0xFF, y>>8, y&0xFF };
  356. ikbd_write(cmd, 6);
  357. }
  358. /* Set Y=0 at bottom */
  359. void ikbd_mouse_y0_bot(void)
  360. {
  361. static const char cmd[1] = { 0x0F };
  362. ikbd_write(cmd, 1);
  363. }
  364. /* Set Y=0 at top */
  365. void ikbd_mouse_y0_top(void)
  366. {
  367. static const char cmd[1] = { 0x10 };
  368. ikbd_write(cmd, 1);
  369. }
  370. EXPORT_SYMBOL(ikbd_mouse_y0_top);
  371. /* Disable mouse */
  372. void ikbd_mouse_disable(void)
  373. {
  374. static const char cmd[1] = { 0x12 };
  375. ikbd_write(cmd, 1);
  376. }
  377. EXPORT_SYMBOL(ikbd_mouse_disable);
  378. /* Set joystick event reporting */
  379. void ikbd_joystick_event_on(void)
  380. {
  381. static const char cmd[1] = { 0x14 };
  382. ikbd_write(cmd, 1);
  383. }
  384. /* Set joystick interrogation mode */
  385. void ikbd_joystick_event_off(void)
  386. {
  387. static const char cmd[1] = { 0x15 };
  388. ikbd_write(cmd, 1);
  389. }
  390. /* Joystick interrogation */
  391. void ikbd_joystick_get_state(void)
  392. {
  393. static const char cmd[1] = { 0x16 };
  394. ikbd_write(cmd, 1);
  395. }
  396. #if 0
  397. /* This disables all other ikbd activities !!!! */
  398. /* Set joystick monitoring */
  399. void ikbd_joystick_monitor(int rate)
  400. {
  401. static const char cmd[2] = { 0x17, rate };
  402. ikbd_write(cmd, 2);
  403. kb_state.state = JOYSTICK_MONITOR;
  404. }
  405. #endif
  406. /* some joystick routines not in yet (0x18-0x19) */
  407. /* Disable joysticks */
  408. void ikbd_joystick_disable(void)
  409. {
  410. static const char cmd[1] = { 0x1A };
  411. ikbd_write(cmd, 1);
  412. }
  413. /*
  414. * The original code sometimes left the interrupt line of
  415. * the ACIAs low forever. I hope, it is fixed now.
  416. *
  417. * Martin Rogge, 20 Aug 1995
  418. */
  419. static int atari_keyb_done = 0;
  420. int atari_keyb_init(void)
  421. {
  422. int error;
  423. if (atari_keyb_done)
  424. return 0;
  425. kb_state.state = KEYBOARD;
  426. kb_state.len = 0;
  427. error = request_irq(IRQ_MFP_ACIA, atari_keyboard_interrupt, 0,
  428. "keyboard,mouse,MIDI", atari_keyboard_interrupt);
  429. if (error)
  430. return error;
  431. atari_turnoff_irq(IRQ_MFP_ACIA);
  432. do {
  433. /* reset IKBD ACIA */
  434. acia.key_ctrl = ACIA_RESET |
  435. ((atari_switches & ATARI_SWITCH_IKBD) ?
  436. ACIA_RHTID : 0);
  437. (void)acia.key_ctrl;
  438. (void)acia.key_data;
  439. /* reset MIDI ACIA */
  440. acia.mid_ctrl = ACIA_RESET |
  441. ((atari_switches & ATARI_SWITCH_MIDI) ?
  442. ACIA_RHTID : 0);
  443. (void)acia.mid_ctrl;
  444. (void)acia.mid_data;
  445. /* divide 500kHz by 64 gives 7812.5 baud */
  446. /* 8 data no parity 1 start 1 stop bit */
  447. /* receive interrupt enabled */
  448. /* RTS low (except if switch selected), transmit interrupt disabled */
  449. acia.key_ctrl = (ACIA_DIV64|ACIA_D8N1S|ACIA_RIE) |
  450. ((atari_switches & ATARI_SWITCH_IKBD) ?
  451. ACIA_RHTID : ACIA_RLTID);
  452. acia.mid_ctrl = ACIA_DIV16 | ACIA_D8N1S |
  453. ((atari_switches & ATARI_SWITCH_MIDI) ?
  454. ACIA_RHTID : 0);
  455. /* make sure the interrupt line is up */
  456. } while ((st_mfp.par_dt_reg & 0x10) == 0);
  457. /* enable ACIA Interrupts */
  458. st_mfp.active_edge &= ~0x10;
  459. atari_turnon_irq(IRQ_MFP_ACIA);
  460. ikbd_self_test = 1;
  461. ikbd_reset();
  462. /* wait for a period of inactivity (here: 0.25s), then assume the IKBD's
  463. * self-test is finished */
  464. self_test_last_rcv = jiffies;
  465. while (time_before(jiffies, self_test_last_rcv + HZ/4))
  466. barrier();
  467. /* if not incremented: no 0xf1 received */
  468. if (ikbd_self_test == 1)
  469. pr_err("Keyboard self test failed!\n");
  470. ikbd_self_test = 0;
  471. ikbd_mouse_disable();
  472. ikbd_joystick_disable();
  473. #ifdef FIXED_ATARI_JOYSTICK
  474. atari_joystick_init();
  475. #endif
  476. // flag init done
  477. atari_keyb_done = 1;
  478. return 0;
  479. }
  480. EXPORT_SYMBOL_GPL(atari_keyb_init);