atakeyb.c 16 KB

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