Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / arch / m68k / atari / atakeyb.c
blob8a2a53b33616708a7bbe6815bfd9d58dce42bf98
1 /*
2 * Atari Keyboard driver for 680x0 Linux
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 */
9 /*
10 * Atari support by Robert de Vries
11 * enhanced by Bjoern Brauel and Roman Hodek
13 * 2.6 and input cleanup (removed autorepeat stuff) for 2.6.21
14 * 06/07 Michael Schmitz
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>
30 #include <asm/atariints.h>
31 #include <asm/atarihw.h>
32 #include <asm/atarikb.h>
33 #include <asm/atari_joystick.h>
34 #include <asm/irq.h>
36 extern unsigned int keymap_count;
38 /* Hook for MIDI serial driver */
39 void (*atari_MIDI_interrupt_hook) (void);
40 /* Hook for mouse driver */
41 void (*atari_mouse_interrupt_hook) (char *);
42 /* Hook for keyboard inputdev driver */
43 void (*atari_input_keyboard_interrupt_hook) (unsigned char, char);
44 /* Hook for mouse inputdev driver */
45 void (*atari_input_mouse_interrupt_hook) (char *);
46 EXPORT_SYMBOL(atari_mouse_interrupt_hook);
47 EXPORT_SYMBOL(atari_input_keyboard_interrupt_hook);
48 EXPORT_SYMBOL(atari_input_mouse_interrupt_hook);
50 /* variables for IKBD self test: */
52 /* state: 0: off; >0: in progress; >1: 0xf1 received */
53 static volatile int ikbd_self_test;
54 /* timestamp when last received a char */
55 static volatile unsigned long self_test_last_rcv;
56 /* bitmap of keys reported as broken */
57 static unsigned long broken_keys[128/(sizeof(unsigned long)*8)] = { 0, };
59 #define BREAK_MASK (0x80)
62 * ++roman: The following changes were applied manually:
64 * - The Alt (= Meta) key works in combination with Shift and
65 * Control, e.g. Alt+Shift+a sends Meta-A (0xc1), Alt+Control+A sends
66 * Meta-Ctrl-A (0x81) ...
68 * - The parentheses on the keypad send '(' and ')' with all
69 * modifiers (as would do e.g. keypad '+'), but they cannot be used as
70 * application keys (i.e. sending Esc O c).
72 * - HELP and UNDO are mapped to be F21 and F24, resp, that send the
73 * codes "\E[M" and "\E[P". (This is better than the old mapping to
74 * F11 and F12, because these codes are on Shift+F1/2 anyway.) This
75 * way, applications that allow their own keyboard mappings
76 * (e.g. tcsh, X Windows) can be configured to use them in the way
77 * the label suggests (providing help or undoing).
79 * - Console switching is done with Alt+Fx (consoles 1..10) and
80 * Shift+Alt+Fx (consoles 11..20).
82 * - The misc. special function implemented in the kernel are mapped
83 * to the following key combinations:
85 * ClrHome -> Home/Find
86 * Shift + ClrHome -> End/Select
87 * Shift + Up -> Page Up
88 * Shift + Down -> Page Down
89 * Alt + Help -> show system status
90 * Shift + Help -> show memory info
91 * Ctrl + Help -> show registers
92 * Ctrl + Alt + Del -> Reboot
93 * Alt + Undo -> switch to last console
94 * Shift + Undo -> send interrupt
95 * Alt + Insert -> stop/start output (same as ^S/^Q)
96 * Alt + Up -> Scroll back console (if implemented)
97 * Alt + Down -> Scroll forward console (if implemented)
98 * Alt + CapsLock -> NumLock
100 * ++Andreas:
102 * - Help mapped to K_HELP
103 * - Undo mapped to K_UNDO (= K_F246)
104 * - Keypad Left/Right Parenthesis mapped to new K_PPAREN[LR]
107 typedef enum kb_state_t {
108 KEYBOARD, AMOUSE, RMOUSE, JOYSTICK, CLOCK, RESYNC
109 } KB_STATE_T;
111 #define IS_SYNC_CODE(sc) ((sc) >= 0x04 && (sc) <= 0xfb)
113 typedef struct keyboard_state {
114 unsigned char buf[6];
115 int len;
116 KB_STATE_T state;
117 } KEYBOARD_STATE;
119 KEYBOARD_STATE kb_state;
121 /* ++roman: If a keyboard overrun happened, we can't tell in general how much
122 * bytes have been lost and in which state of the packet structure we are now.
123 * This usually causes keyboards bytes to be interpreted as mouse movements
124 * and vice versa, which is very annoying. It seems better to throw away some
125 * bytes (that are usually mouse bytes) than to misinterpret them. Therefor I
126 * introduced the RESYNC state for IKBD data. In this state, the bytes up to
127 * one that really looks like a key event (0x04..0xf2) or the start of a mouse
128 * packet (0xf8..0xfb) are thrown away, but at most 2 bytes. This at least
129 * speeds up the resynchronization of the event structure, even if maybe a
130 * mouse movement is lost. However, nothing is perfect. For bytes 0x01..0x03,
131 * it's really hard to decide whether they're mouse or keyboard bytes. Since
132 * overruns usually occur when moving the Atari mouse rapidly, they're seen as
133 * mouse bytes here. If this is wrong, only a make code of the keyboard gets
134 * lost, which isn't too bad. Loosing a break code would be disastrous,
135 * because then the keyboard repeat strikes...
138 static irqreturn_t atari_keyboard_interrupt(int irq, void *dummy)
140 u_char acia_stat;
141 int scancode;
142 int break_flag;
144 repeat:
145 if (acia.mid_ctrl & ACIA_IRQ)
146 if (atari_MIDI_interrupt_hook)
147 atari_MIDI_interrupt_hook();
148 acia_stat = acia.key_ctrl;
149 /* check out if the interrupt came from this ACIA */
150 if (!((acia_stat | acia.mid_ctrl) & ACIA_IRQ))
151 return IRQ_HANDLED;
153 if (acia_stat & ACIA_OVRN) {
154 /* a very fast typist or a slow system, give a warning */
155 /* ...happens often if interrupts were disabled for too long */
156 printk(KERN_DEBUG "Keyboard overrun\n");
157 scancode = acia.key_data;
158 if (ikbd_self_test)
159 /* During self test, don't do resyncing, just process the code */
160 goto interpret_scancode;
161 else if (IS_SYNC_CODE(scancode)) {
162 /* This code seem already to be the start of a new packet or a
163 * single scancode */
164 kb_state.state = KEYBOARD;
165 goto interpret_scancode;
166 } else {
167 /* Go to RESYNC state and skip this byte */
168 kb_state.state = RESYNC;
169 kb_state.len = 1; /* skip max. 1 another byte */
170 goto repeat;
174 if (acia_stat & ACIA_RDRF) {
175 /* received a character */
176 scancode = acia.key_data; /* get it or reset the ACIA, I'll get it! */
177 tasklet_schedule(&keyboard_tasklet);
178 interpret_scancode:
179 switch (kb_state.state) {
180 case KEYBOARD:
181 switch (scancode) {
182 case 0xF7:
183 kb_state.state = AMOUSE;
184 kb_state.len = 0;
185 break;
187 case 0xF8:
188 case 0xF9:
189 case 0xFA:
190 case 0xFB:
191 kb_state.state = RMOUSE;
192 kb_state.len = 1;
193 kb_state.buf[0] = scancode;
194 break;
196 case 0xFC:
197 kb_state.state = CLOCK;
198 kb_state.len = 0;
199 break;
201 case 0xFE:
202 case 0xFF:
203 kb_state.state = JOYSTICK;
204 kb_state.len = 1;
205 kb_state.buf[0] = scancode;
206 break;
208 case 0xF1:
209 /* during self-test, note that 0xf1 received */
210 if (ikbd_self_test) {
211 ++ikbd_self_test;
212 self_test_last_rcv = jiffies;
213 break;
215 /* FALL THROUGH */
217 default:
218 break_flag = scancode & BREAK_MASK;
219 scancode &= ~BREAK_MASK;
220 if (ikbd_self_test) {
221 /* Scancodes sent during the self-test stand for broken
222 * keys (keys being down). The code *should* be a break
223 * code, but nevertheless some AT keyboard interfaces send
224 * make codes instead. Therefore, simply ignore
225 * break_flag...
227 int keyval, keytyp;
229 set_bit(scancode, broken_keys);
230 self_test_last_rcv = jiffies;
231 /* new Linux scancodes; approx. */
232 keyval = scancode;
233 keytyp = KTYP(keyval) - 0xf0;
234 keyval = KVAL(keyval);
236 printk(KERN_WARNING "Key with scancode %d ", scancode);
237 if (keytyp == KT_LATIN || keytyp == KT_LETTER) {
238 if (keyval < ' ')
239 printk("('^%c') ", keyval + '@');
240 else
241 printk("('%c') ", keyval);
243 printk("is broken -- will be ignored.\n");
244 break;
245 } else if (test_bit(scancode, broken_keys))
246 break;
248 if (atari_input_keyboard_interrupt_hook)
249 atari_input_keyboard_interrupt_hook((unsigned char)scancode, !break_flag);
250 break;
252 break;
254 case AMOUSE:
255 kb_state.buf[kb_state.len++] = scancode;
256 if (kb_state.len == 5) {
257 kb_state.state = KEYBOARD;
258 /* not yet used */
259 /* wake up someone waiting for this */
261 break;
263 case RMOUSE:
264 kb_state.buf[kb_state.len++] = scancode;
265 if (kb_state.len == 3) {
266 kb_state.state = KEYBOARD;
267 if (atari_mouse_interrupt_hook)
268 atari_mouse_interrupt_hook(kb_state.buf);
270 break;
272 case JOYSTICK:
273 kb_state.buf[1] = scancode;
274 kb_state.state = KEYBOARD;
275 #ifdef FIXED_ATARI_JOYSTICK
276 atari_joystick_interrupt(kb_state.buf);
277 #endif
278 break;
280 case CLOCK:
281 kb_state.buf[kb_state.len++] = scancode;
282 if (kb_state.len == 6) {
283 kb_state.state = KEYBOARD;
284 /* wake up someone waiting for this.
285 But will this ever be used, as Linux keeps its own time.
286 Perhaps for synchronization purposes? */
287 /* wake_up_interruptible(&clock_wait); */
289 break;
291 case RESYNC:
292 if (kb_state.len <= 0 || IS_SYNC_CODE(scancode)) {
293 kb_state.state = KEYBOARD;
294 goto interpret_scancode;
296 kb_state.len--;
297 break;
301 #if 0
302 if (acia_stat & ACIA_CTS)
303 /* cannot happen */;
304 #endif
306 if (acia_stat & (ACIA_FE | ACIA_PE)) {
307 printk("Error in keyboard communication\n");
310 /* handle_scancode() can take a lot of time, so check again if
311 * some character arrived
313 goto repeat;
317 * I write to the keyboard without using interrupts, I poll instead.
318 * This takes for the maximum length string allowed (7) at 7812.5 baud
319 * 8 data 1 start 1 stop bit: 9.0 ms
320 * If this takes too long for normal operation, interrupt driven writing
321 * is the solution. (I made a feeble attempt in that direction but I
322 * kept it simple for now.)
324 void ikbd_write(const char *str, int len)
326 u_char acia_stat;
328 if ((len < 1) || (len > 7))
329 panic("ikbd: maximum string length exceeded");
330 while (len) {
331 acia_stat = acia.key_ctrl;
332 if (acia_stat & ACIA_TDRE) {
333 acia.key_data = *str++;
334 len--;
339 /* Reset (without touching the clock) */
340 void ikbd_reset(void)
342 static const char cmd[2] = { 0x80, 0x01 };
344 ikbd_write(cmd, 2);
347 * if all's well code 0xF1 is returned, else the break codes of
348 * all keys making contact
352 /* Set mouse button action */
353 void ikbd_mouse_button_action(int mode)
355 char cmd[2] = { 0x07, mode };
357 ikbd_write(cmd, 2);
360 /* Set relative mouse position reporting */
361 void ikbd_mouse_rel_pos(void)
363 static const char cmd[1] = { 0x08 };
365 ikbd_write(cmd, 1);
367 EXPORT_SYMBOL(ikbd_mouse_rel_pos);
369 /* Set absolute mouse position reporting */
370 void ikbd_mouse_abs_pos(int xmax, int ymax)
372 char cmd[5] = { 0x09, xmax>>8, xmax&0xFF, ymax>>8, ymax&0xFF };
374 ikbd_write(cmd, 5);
377 /* Set mouse keycode mode */
378 void ikbd_mouse_kbd_mode(int dx, int dy)
380 char cmd[3] = { 0x0A, dx, dy };
382 ikbd_write(cmd, 3);
385 /* Set mouse threshold */
386 void ikbd_mouse_thresh(int x, int y)
388 char cmd[3] = { 0x0B, x, y };
390 ikbd_write(cmd, 3);
392 EXPORT_SYMBOL(ikbd_mouse_thresh);
394 /* Set mouse scale */
395 void ikbd_mouse_scale(int x, int y)
397 char cmd[3] = { 0x0C, x, y };
399 ikbd_write(cmd, 3);
402 /* Interrogate mouse position */
403 void ikbd_mouse_pos_get(int *x, int *y)
405 static const char cmd[1] = { 0x0D };
407 ikbd_write(cmd, 1);
409 /* wait for returning bytes */
412 /* Load mouse position */
413 void ikbd_mouse_pos_set(int x, int y)
415 char cmd[6] = { 0x0E, 0x00, x>>8, x&0xFF, y>>8, y&0xFF };
417 ikbd_write(cmd, 6);
420 /* Set Y=0 at bottom */
421 void ikbd_mouse_y0_bot(void)
423 static const char cmd[1] = { 0x0F };
425 ikbd_write(cmd, 1);
428 /* Set Y=0 at top */
429 void ikbd_mouse_y0_top(void)
431 static const char cmd[1] = { 0x10 };
433 ikbd_write(cmd, 1);
435 EXPORT_SYMBOL(ikbd_mouse_y0_top);
437 /* Resume */
438 void ikbd_resume(void)
440 static const char cmd[1] = { 0x11 };
442 ikbd_write(cmd, 1);
445 /* Disable mouse */
446 void ikbd_mouse_disable(void)
448 static const char cmd[1] = { 0x12 };
450 ikbd_write(cmd, 1);
452 EXPORT_SYMBOL(ikbd_mouse_disable);
454 /* Pause output */
455 void ikbd_pause(void)
457 static const char cmd[1] = { 0x13 };
459 ikbd_write(cmd, 1);
462 /* Set joystick event reporting */
463 void ikbd_joystick_event_on(void)
465 static const char cmd[1] = { 0x14 };
467 ikbd_write(cmd, 1);
470 /* Set joystick interrogation mode */
471 void ikbd_joystick_event_off(void)
473 static const char cmd[1] = { 0x15 };
475 ikbd_write(cmd, 1);
478 /* Joystick interrogation */
479 void ikbd_joystick_get_state(void)
481 static const char cmd[1] = { 0x16 };
483 ikbd_write(cmd, 1);
486 #if 0
487 /* This disables all other ikbd activities !!!! */
488 /* Set joystick monitoring */
489 void ikbd_joystick_monitor(int rate)
491 static const char cmd[2] = { 0x17, rate };
493 ikbd_write(cmd, 2);
495 kb_state.state = JOYSTICK_MONITOR;
497 #endif
499 /* some joystick routines not in yet (0x18-0x19) */
501 /* Disable joysticks */
502 void ikbd_joystick_disable(void)
504 static const char cmd[1] = { 0x1A };
506 ikbd_write(cmd, 1);
509 /* Time-of-day clock set */
510 void ikbd_clock_set(int year, int month, int day, int hour, int minute, int second)
512 char cmd[7] = { 0x1B, year, month, day, hour, minute, second };
514 ikbd_write(cmd, 7);
517 /* Interrogate time-of-day clock */
518 void ikbd_clock_get(int *year, int *month, int *day, int *hour, int *minute, int second)
520 static const char cmd[1] = { 0x1C };
522 ikbd_write(cmd, 1);
525 /* Memory load */
526 void ikbd_mem_write(int address, int size, char *data)
528 panic("Attempt to write data into keyboard memory");
531 /* Memory read */
532 void ikbd_mem_read(int address, char data[6])
534 char cmd[3] = { 0x21, address>>8, address&0xFF };
536 ikbd_write(cmd, 3);
538 /* receive data and put it in data */
541 /* Controller execute */
542 void ikbd_exec(int address)
544 char cmd[3] = { 0x22, address>>8, address&0xFF };
546 ikbd_write(cmd, 3);
549 /* Status inquiries (0x87-0x9A) not yet implemented */
551 /* Set the state of the caps lock led. */
552 void atari_kbd_leds(unsigned int leds)
554 char cmd[6] = {32, 0, 4, 1, 254 + ((leds & 4) != 0), 0};
556 ikbd_write(cmd, 6);
560 * The original code sometimes left the interrupt line of
561 * the ACIAs low forever. I hope, it is fixed now.
563 * Martin Rogge, 20 Aug 1995
566 static int atari_keyb_done = 0;
568 int atari_keyb_init(void)
570 if (atari_keyb_done)
571 return 0;
573 kb_state.state = KEYBOARD;
574 kb_state.len = 0;
576 request_irq(IRQ_MFP_ACIA, atari_keyboard_interrupt, IRQ_TYPE_SLOW,
577 "keyboard/mouse/MIDI", atari_keyboard_interrupt);
579 atari_turnoff_irq(IRQ_MFP_ACIA);
580 do {
581 /* reset IKBD ACIA */
582 acia.key_ctrl = ACIA_RESET |
583 (atari_switches & ATARI_SWITCH_IKBD) ? ACIA_RHTID : 0;
584 (void)acia.key_ctrl;
585 (void)acia.key_data;
587 /* reset MIDI ACIA */
588 acia.mid_ctrl = ACIA_RESET |
589 (atari_switches & ATARI_SWITCH_MIDI) ? ACIA_RHTID : 0;
590 (void)acia.mid_ctrl;
591 (void)acia.mid_data;
593 /* divide 500kHz by 64 gives 7812.5 baud */
594 /* 8 data no parity 1 start 1 stop bit */
595 /* receive interrupt enabled */
596 /* RTS low (except if switch selected), transmit interrupt disabled */
597 acia.key_ctrl = (ACIA_DIV64|ACIA_D8N1S|ACIA_RIE) |
598 ((atari_switches & ATARI_SWITCH_IKBD) ?
599 ACIA_RHTID : ACIA_RLTID);
601 acia.mid_ctrl = ACIA_DIV16 | ACIA_D8N1S |
602 (atari_switches & ATARI_SWITCH_MIDI) ? ACIA_RHTID : 0;
604 /* make sure the interrupt line is up */
605 } while ((mfp.par_dt_reg & 0x10) == 0);
607 /* enable ACIA Interrupts */
608 mfp.active_edge &= ~0x10;
609 atari_turnon_irq(IRQ_MFP_ACIA);
611 ikbd_self_test = 1;
612 ikbd_reset();
613 /* wait for a period of inactivity (here: 0.25s), then assume the IKBD's
614 * self-test is finished */
615 self_test_last_rcv = jiffies;
616 while (time_before(jiffies, self_test_last_rcv + HZ/4))
617 barrier();
618 /* if not incremented: no 0xf1 received */
619 if (ikbd_self_test == 1)
620 printk(KERN_ERR "WARNING: keyboard self test failed!\n");
621 ikbd_self_test = 0;
623 ikbd_mouse_disable();
624 ikbd_joystick_disable();
626 #ifdef FIXED_ATARI_JOYSTICK
627 atari_joystick_init();
628 #endif
630 // flag init done
631 atari_keyb_done = 1;
632 return 0;
634 EXPORT_SYMBOL_GPL(atari_keyb_init);
636 int atari_kbd_translate(unsigned char keycode, unsigned char *keycodep, char raw_mode)
638 #ifdef CONFIG_MAGIC_SYSRQ
639 /* ALT+HELP pressed? */
640 if ((keycode == 98) && ((shift_state & 0xff) == 8))
641 *keycodep = 0xff;
642 else
643 #endif
644 *keycodep = keycode;
645 return 1;