atakeyb.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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. printk(KERN_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. tasklet_schedule(&keyboard_tasklet);
  156. interpret_scancode:
  157. switch (kb_state.state) {
  158. case KEYBOARD:
  159. switch (scancode) {
  160. case 0xF7:
  161. kb_state.state = AMOUSE;
  162. kb_state.len = 0;
  163. break;
  164. case 0xF8:
  165. case 0xF9:
  166. case 0xFA:
  167. case 0xFB:
  168. kb_state.state = RMOUSE;
  169. kb_state.len = 1;
  170. kb_state.buf[0] = scancode;
  171. break;
  172. case 0xFC:
  173. kb_state.state = CLOCK;
  174. kb_state.len = 0;
  175. break;
  176. case 0xFE:
  177. case 0xFF:
  178. kb_state.state = JOYSTICK;
  179. kb_state.len = 1;
  180. kb_state.buf[0] = scancode;
  181. break;
  182. case 0xF1:
  183. /* during self-test, note that 0xf1 received */
  184. if (ikbd_self_test) {
  185. ++ikbd_self_test;
  186. self_test_last_rcv = jiffies;
  187. break;
  188. }
  189. /* FALL THROUGH */
  190. default:
  191. break_flag = scancode & BREAK_MASK;
  192. scancode &= ~BREAK_MASK;
  193. if (ikbd_self_test) {
  194. /* Scancodes sent during the self-test stand for broken
  195. * keys (keys being down). The code *should* be a break
  196. * code, but nevertheless some AT keyboard interfaces send
  197. * make codes instead. Therefore, simply ignore
  198. * break_flag...
  199. */
  200. int keyval, keytyp;
  201. set_bit(scancode, broken_keys);
  202. self_test_last_rcv = jiffies;
  203. /* new Linux scancodes; approx. */
  204. keyval = scancode;
  205. keytyp = KTYP(keyval) - 0xf0;
  206. keyval = KVAL(keyval);
  207. printk(KERN_WARNING "Key with scancode %d ", scancode);
  208. if (keytyp == KT_LATIN || keytyp == KT_LETTER) {
  209. if (keyval < ' ')
  210. printk("('^%c') ", keyval + '@');
  211. else
  212. printk("('%c') ", keyval);
  213. }
  214. printk("is broken -- will be ignored.\n");
  215. break;
  216. } else if (test_bit(scancode, broken_keys))
  217. break;
  218. if (atari_input_keyboard_interrupt_hook)
  219. atari_input_keyboard_interrupt_hook((unsigned char)scancode, !break_flag);
  220. break;
  221. }
  222. break;
  223. case AMOUSE:
  224. kb_state.buf[kb_state.len++] = scancode;
  225. if (kb_state.len == 5) {
  226. kb_state.state = KEYBOARD;
  227. /* not yet used */
  228. /* wake up someone waiting for this */
  229. }
  230. break;
  231. case RMOUSE:
  232. kb_state.buf[kb_state.len++] = scancode;
  233. if (kb_state.len == 3) {
  234. kb_state.state = KEYBOARD;
  235. if (atari_input_mouse_interrupt_hook)
  236. atari_input_mouse_interrupt_hook(kb_state.buf);
  237. }
  238. break;
  239. case JOYSTICK:
  240. kb_state.buf[1] = scancode;
  241. kb_state.state = KEYBOARD;
  242. #ifdef FIXED_ATARI_JOYSTICK
  243. atari_joystick_interrupt(kb_state.buf);
  244. #endif
  245. break;
  246. case CLOCK:
  247. kb_state.buf[kb_state.len++] = scancode;
  248. if (kb_state.len == 6) {
  249. kb_state.state = KEYBOARD;
  250. /* wake up someone waiting for this.
  251. But will this ever be used, as Linux keeps its own time.
  252. Perhaps for synchronization purposes? */
  253. /* wake_up_interruptible(&clock_wait); */
  254. }
  255. break;
  256. case RESYNC:
  257. if (kb_state.len <= 0 || IS_SYNC_CODE(scancode)) {
  258. kb_state.state = KEYBOARD;
  259. goto interpret_scancode;
  260. }
  261. kb_state.len--;
  262. break;
  263. }
  264. }
  265. #if 0
  266. if (acia_stat & ACIA_CTS)
  267. /* cannot happen */;
  268. #endif
  269. if (acia_stat & (ACIA_FE | ACIA_PE)) {
  270. printk("Error in keyboard communication\n");
  271. }
  272. /* handle_scancode() can take a lot of time, so check again if
  273. * some character arrived
  274. */
  275. goto repeat;
  276. }
  277. /*
  278. * I write to the keyboard without using interrupts, I poll instead.
  279. * This takes for the maximum length string allowed (7) at 7812.5 baud
  280. * 8 data 1 start 1 stop bit: 9.0 ms
  281. * If this takes too long for normal operation, interrupt driven writing
  282. * is the solution. (I made a feeble attempt in that direction but I
  283. * kept it simple for now.)
  284. */
  285. void ikbd_write(const char *str, int len)
  286. {
  287. u_char acia_stat;
  288. if ((len < 1) || (len > 7))
  289. panic("ikbd: maximum string length exceeded");
  290. while (len) {
  291. acia_stat = acia.key_ctrl;
  292. if (acia_stat & ACIA_TDRE) {
  293. acia.key_data = *str++;
  294. len--;
  295. }
  296. }
  297. }
  298. /* Reset (without touching the clock) */
  299. void ikbd_reset(void)
  300. {
  301. static const char cmd[2] = { 0x80, 0x01 };
  302. ikbd_write(cmd, 2);
  303. /*
  304. * if all's well code 0xF1 is returned, else the break codes of
  305. * all keys making contact
  306. */
  307. }
  308. /* Set mouse button action */
  309. void ikbd_mouse_button_action(int mode)
  310. {
  311. char cmd[2] = { 0x07, mode };
  312. ikbd_write(cmd, 2);
  313. }
  314. /* Set relative mouse position reporting */
  315. void ikbd_mouse_rel_pos(void)
  316. {
  317. static const char cmd[1] = { 0x08 };
  318. ikbd_write(cmd, 1);
  319. }
  320. EXPORT_SYMBOL(ikbd_mouse_rel_pos);
  321. /* Set absolute mouse position reporting */
  322. void ikbd_mouse_abs_pos(int xmax, int ymax)
  323. {
  324. char cmd[5] = { 0x09, xmax>>8, xmax&0xFF, ymax>>8, ymax&0xFF };
  325. ikbd_write(cmd, 5);
  326. }
  327. /* Set mouse keycode mode */
  328. void ikbd_mouse_kbd_mode(int dx, int dy)
  329. {
  330. char cmd[3] = { 0x0A, dx, dy };
  331. ikbd_write(cmd, 3);
  332. }
  333. /* Set mouse threshold */
  334. void ikbd_mouse_thresh(int x, int y)
  335. {
  336. char cmd[3] = { 0x0B, x, y };
  337. ikbd_write(cmd, 3);
  338. }
  339. EXPORT_SYMBOL(ikbd_mouse_thresh);
  340. /* Set mouse scale */
  341. void ikbd_mouse_scale(int x, int y)
  342. {
  343. char cmd[3] = { 0x0C, x, y };
  344. ikbd_write(cmd, 3);
  345. }
  346. /* Interrogate mouse position */
  347. void ikbd_mouse_pos_get(int *x, int *y)
  348. {
  349. static const char cmd[1] = { 0x0D };
  350. ikbd_write(cmd, 1);
  351. /* wait for returning bytes */
  352. }
  353. /* Load mouse position */
  354. void ikbd_mouse_pos_set(int x, int y)
  355. {
  356. char cmd[6] = { 0x0E, 0x00, x>>8, x&0xFF, y>>8, y&0xFF };
  357. ikbd_write(cmd, 6);
  358. }
  359. /* Set Y=0 at bottom */
  360. void ikbd_mouse_y0_bot(void)
  361. {
  362. static const char cmd[1] = { 0x0F };
  363. ikbd_write(cmd, 1);
  364. }
  365. /* Set Y=0 at top */
  366. void ikbd_mouse_y0_top(void)
  367. {
  368. static const char cmd[1] = { 0x10 };
  369. ikbd_write(cmd, 1);
  370. }
  371. EXPORT_SYMBOL(ikbd_mouse_y0_top);
  372. /* Resume */
  373. void ikbd_resume(void)
  374. {
  375. static const char cmd[1] = { 0x11 };
  376. ikbd_write(cmd, 1);
  377. }
  378. /* Disable mouse */
  379. void ikbd_mouse_disable(void)
  380. {
  381. static const char cmd[1] = { 0x12 };
  382. ikbd_write(cmd, 1);
  383. }
  384. EXPORT_SYMBOL(ikbd_mouse_disable);
  385. /* Pause output */
  386. void ikbd_pause(void)
  387. {
  388. static const char cmd[1] = { 0x13 };
  389. ikbd_write(cmd, 1);
  390. }
  391. /* Set joystick event reporting */
  392. void ikbd_joystick_event_on(void)
  393. {
  394. static const char cmd[1] = { 0x14 };
  395. ikbd_write(cmd, 1);
  396. }
  397. /* Set joystick interrogation mode */
  398. void ikbd_joystick_event_off(void)
  399. {
  400. static const char cmd[1] = { 0x15 };
  401. ikbd_write(cmd, 1);
  402. }
  403. /* Joystick interrogation */
  404. void ikbd_joystick_get_state(void)
  405. {
  406. static const char cmd[1] = { 0x16 };
  407. ikbd_write(cmd, 1);
  408. }
  409. #if 0
  410. /* This disables all other ikbd activities !!!! */
  411. /* Set joystick monitoring */
  412. void ikbd_joystick_monitor(int rate)
  413. {
  414. static const char cmd[2] = { 0x17, rate };
  415. ikbd_write(cmd, 2);
  416. kb_state.state = JOYSTICK_MONITOR;
  417. }
  418. #endif
  419. /* some joystick routines not in yet (0x18-0x19) */
  420. /* Disable joysticks */
  421. void ikbd_joystick_disable(void)
  422. {
  423. static const char cmd[1] = { 0x1A };
  424. ikbd_write(cmd, 1);
  425. }
  426. /* Time-of-day clock set */
  427. void ikbd_clock_set(int year, int month, int day, int hour, int minute, int second)
  428. {
  429. char cmd[7] = { 0x1B, year, month, day, hour, minute, second };
  430. ikbd_write(cmd, 7);
  431. }
  432. /* Interrogate time-of-day clock */
  433. void ikbd_clock_get(int *year, int *month, int *day, int *hour, int *minute, int second)
  434. {
  435. static const char cmd[1] = { 0x1C };
  436. ikbd_write(cmd, 1);
  437. }
  438. /* Memory load */
  439. void ikbd_mem_write(int address, int size, char *data)
  440. {
  441. panic("Attempt to write data into keyboard memory");
  442. }
  443. /* Memory read */
  444. void ikbd_mem_read(int address, char data[6])
  445. {
  446. char cmd[3] = { 0x21, address>>8, address&0xFF };
  447. ikbd_write(cmd, 3);
  448. /* receive data and put it in data */
  449. }
  450. /* Controller execute */
  451. void ikbd_exec(int address)
  452. {
  453. char cmd[3] = { 0x22, address>>8, address&0xFF };
  454. ikbd_write(cmd, 3);
  455. }
  456. /* Status inquiries (0x87-0x9A) not yet implemented */
  457. /* Set the state of the caps lock led. */
  458. void atari_kbd_leds(unsigned int leds)
  459. {
  460. char cmd[6] = {32, 0, 4, 1, 254 + ((leds & 4) != 0), 0};
  461. ikbd_write(cmd, 6);
  462. }
  463. /*
  464. * The original code sometimes left the interrupt line of
  465. * the ACIAs low forever. I hope, it is fixed now.
  466. *
  467. * Martin Rogge, 20 Aug 1995
  468. */
  469. static int atari_keyb_done = 0;
  470. int atari_keyb_init(void)
  471. {
  472. int error;
  473. if (atari_keyb_done)
  474. return 0;
  475. kb_state.state = KEYBOARD;
  476. kb_state.len = 0;
  477. error = request_irq(IRQ_MFP_ACIA, atari_keyboard_interrupt,
  478. IRQ_TYPE_SLOW, "keyboard,mouse,MIDI",
  479. atari_keyboard_interrupt);
  480. if (error)
  481. return error;
  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 ((st_mfp.par_dt_reg & 0x10) == 0);
  508. /* enable ACIA Interrupts */
  509. st_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);