atakeyb.c 16 KB

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