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