atakeyb.c 16 KB

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