/*1* Atari Keyboard driver for 680x0 Linux2*3* This file is subject to the terms and conditions of the GNU General Public4* License. See the file COPYING in the main directory of this archive5* for more details.6*/78/*9* Atari support by Robert de Vries10* enhanced by Bjoern Brauel and Roman Hodek11*12* 2.6 and input cleanup (removed autorepeat stuff) for 2.6.2113* 06/07 Michael Schmitz14*/1516#include <linux/module.h>17#include <linux/sched.h>18#include <linux/kernel.h>19#include <linux/interrupt.h>20#include <linux/errno.h>21#include <linux/keyboard.h>22#include <linux/delay.h>23#include <linux/timer.h>24#include <linux/kd.h>25#include <linux/random.h>26#include <linux/init.h>27#include <linux/kbd_kern.h>2829#include <asm/atariints.h>30#include <asm/atarihw.h>31#include <asm/atarikb.h>32#include <asm/atari_joystick.h>33#include <asm/irq.h>343536/* Hook for MIDI serial driver */37void (*atari_MIDI_interrupt_hook) (void);38/* Hook for keyboard inputdev driver */39void (*atari_input_keyboard_interrupt_hook) (unsigned char, char);40/* Hook for mouse inputdev driver */41void (*atari_input_mouse_interrupt_hook) (char *);42EXPORT_SYMBOL(atari_input_keyboard_interrupt_hook);43EXPORT_SYMBOL(atari_input_mouse_interrupt_hook);4445/* variables for IKBD self test: */4647/* state: 0: off; >0: in progress; >1: 0xf1 received */48static volatile int ikbd_self_test;49/* timestamp when last received a char */50static volatile unsigned long self_test_last_rcv;51/* bitmap of keys reported as broken */52static unsigned long broken_keys[128/(sizeof(unsigned long)*8)] = { 0, };5354#define BREAK_MASK (0x80)5556/*57* ++roman: The following changes were applied manually:58*59* - The Alt (= Meta) key works in combination with Shift and60* Control, e.g. Alt+Shift+a sends Meta-A (0xc1), Alt+Control+A sends61* Meta-Ctrl-A (0x81) ...62*63* - The parentheses on the keypad send '(' and ')' with all64* modifiers (as would do e.g. keypad '+'), but they cannot be used as65* application keys (i.e. sending Esc O c).66*67* - HELP and UNDO are mapped to be F21 and F24, resp, that send the68* codes "\E[M" and "\E[P". (This is better than the old mapping to69* F11 and F12, because these codes are on Shift+F1/2 anyway.) This70* way, applications that allow their own keyboard mappings71* (e.g. tcsh, X Windows) can be configured to use them in the way72* the label suggests (providing help or undoing).73*74* - Console switching is done with Alt+Fx (consoles 1..10) and75* Shift+Alt+Fx (consoles 11..20).76*77* - The misc. special function implemented in the kernel are mapped78* to the following key combinations:79*80* ClrHome -> Home/Find81* Shift + ClrHome -> End/Select82* Shift + Up -> Page Up83* Shift + Down -> Page Down84* Alt + Help -> show system status85* Shift + Help -> show memory info86* Ctrl + Help -> show registers87* Ctrl + Alt + Del -> Reboot88* Alt + Undo -> switch to last console89* Shift + Undo -> send interrupt90* Alt + Insert -> stop/start output (same as ^S/^Q)91* Alt + Up -> Scroll back console (if implemented)92* Alt + Down -> Scroll forward console (if implemented)93* Alt + CapsLock -> NumLock94*95* ++Andreas:96*97* - Help mapped to K_HELP98* - Undo mapped to K_UNDO (= K_F246)99* - Keypad Left/Right Parenthesis mapped to new K_PPAREN[LR]100*/101102typedef enum kb_state_t {103KEYBOARD, AMOUSE, RMOUSE, JOYSTICK, CLOCK, RESYNC104} KB_STATE_T;105106#define IS_SYNC_CODE(sc) ((sc) >= 0x04 && (sc) <= 0xfb)107108typedef struct keyboard_state {109unsigned char buf[6];110int len;111KB_STATE_T state;112} KEYBOARD_STATE;113114KEYBOARD_STATE kb_state;115116/* ++roman: If a keyboard overrun happened, we can't tell in general how much117* bytes have been lost and in which state of the packet structure we are now.118* This usually causes keyboards bytes to be interpreted as mouse movements119* and vice versa, which is very annoying. It seems better to throw away some120* bytes (that are usually mouse bytes) than to misinterpret them. Therefore I121* introduced the RESYNC state for IKBD data. In this state, the bytes up to122* one that really looks like a key event (0x04..0xf2) or the start of a mouse123* packet (0xf8..0xfb) are thrown away, but at most 2 bytes. This at least124* speeds up the resynchronization of the event structure, even if maybe a125* mouse movement is lost. However, nothing is perfect. For bytes 0x01..0x03,126* it's really hard to decide whether they're mouse or keyboard bytes. Since127* overruns usually occur when moving the Atari mouse rapidly, they're seen as128* mouse bytes here. If this is wrong, only a make code of the keyboard gets129* lost, which isn't too bad. Losing a break code would be disastrous,130* because then the keyboard repeat strikes...131*/132133static irqreturn_t atari_keyboard_interrupt(int irq, void *dummy)134{135u_char acia_stat;136int scancode;137int break_flag;138139repeat:140if (acia.mid_ctrl & ACIA_IRQ)141if (atari_MIDI_interrupt_hook)142atari_MIDI_interrupt_hook();143acia_stat = acia.key_ctrl;144/* check out if the interrupt came from this ACIA */145if (!((acia_stat | acia.mid_ctrl) & ACIA_IRQ))146return IRQ_HANDLED;147148if (acia_stat & ACIA_OVRN) {149/* a very fast typist or a slow system, give a warning */150/* ...happens often if interrupts were disabled for too long */151printk(KERN_DEBUG "Keyboard overrun\n");152scancode = acia.key_data;153if (ikbd_self_test)154/* During self test, don't do resyncing, just process the code */155goto interpret_scancode;156else if (IS_SYNC_CODE(scancode)) {157/* This code seem already to be the start of a new packet or a158* single scancode */159kb_state.state = KEYBOARD;160goto interpret_scancode;161} else {162/* Go to RESYNC state and skip this byte */163kb_state.state = RESYNC;164kb_state.len = 1; /* skip max. 1 another byte */165goto repeat;166}167}168169if (acia_stat & ACIA_RDRF) {170/* received a character */171scancode = acia.key_data; /* get it or reset the ACIA, I'll get it! */172tasklet_schedule(&keyboard_tasklet);173interpret_scancode:174switch (kb_state.state) {175case KEYBOARD:176switch (scancode) {177case 0xF7:178kb_state.state = AMOUSE;179kb_state.len = 0;180break;181182case 0xF8:183case 0xF9:184case 0xFA:185case 0xFB:186kb_state.state = RMOUSE;187kb_state.len = 1;188kb_state.buf[0] = scancode;189break;190191case 0xFC:192kb_state.state = CLOCK;193kb_state.len = 0;194break;195196case 0xFE:197case 0xFF:198kb_state.state = JOYSTICK;199kb_state.len = 1;200kb_state.buf[0] = scancode;201break;202203case 0xF1:204/* during self-test, note that 0xf1 received */205if (ikbd_self_test) {206++ikbd_self_test;207self_test_last_rcv = jiffies;208break;209}210/* FALL THROUGH */211212default:213break_flag = scancode & BREAK_MASK;214scancode &= ~BREAK_MASK;215if (ikbd_self_test) {216/* Scancodes sent during the self-test stand for broken217* keys (keys being down). The code *should* be a break218* code, but nevertheless some AT keyboard interfaces send219* make codes instead. Therefore, simply ignore220* break_flag...221*/222int keyval, keytyp;223224set_bit(scancode, broken_keys);225self_test_last_rcv = jiffies;226/* new Linux scancodes; approx. */227keyval = scancode;228keytyp = KTYP(keyval) - 0xf0;229keyval = KVAL(keyval);230231printk(KERN_WARNING "Key with scancode %d ", scancode);232if (keytyp == KT_LATIN || keytyp == KT_LETTER) {233if (keyval < ' ')234printk("('^%c') ", keyval + '@');235else236printk("('%c') ", keyval);237}238printk("is broken -- will be ignored.\n");239break;240} else if (test_bit(scancode, broken_keys))241break;242243if (atari_input_keyboard_interrupt_hook)244atari_input_keyboard_interrupt_hook((unsigned char)scancode, !break_flag);245break;246}247break;248249case AMOUSE:250kb_state.buf[kb_state.len++] = scancode;251if (kb_state.len == 5) {252kb_state.state = KEYBOARD;253/* not yet used */254/* wake up someone waiting for this */255}256break;257258case RMOUSE:259kb_state.buf[kb_state.len++] = scancode;260if (kb_state.len == 3) {261kb_state.state = KEYBOARD;262if (atari_input_mouse_interrupt_hook)263atari_input_mouse_interrupt_hook(kb_state.buf);264}265break;266267case JOYSTICK:268kb_state.buf[1] = scancode;269kb_state.state = KEYBOARD;270#ifdef FIXED_ATARI_JOYSTICK271atari_joystick_interrupt(kb_state.buf);272#endif273break;274275case CLOCK:276kb_state.buf[kb_state.len++] = scancode;277if (kb_state.len == 6) {278kb_state.state = KEYBOARD;279/* wake up someone waiting for this.280But will this ever be used, as Linux keeps its own time.281Perhaps for synchronization purposes? */282/* wake_up_interruptible(&clock_wait); */283}284break;285286case RESYNC:287if (kb_state.len <= 0 || IS_SYNC_CODE(scancode)) {288kb_state.state = KEYBOARD;289goto interpret_scancode;290}291kb_state.len--;292break;293}294}295296#if 0297if (acia_stat & ACIA_CTS)298/* cannot happen */;299#endif300301if (acia_stat & (ACIA_FE | ACIA_PE)) {302printk("Error in keyboard communication\n");303}304305/* handle_scancode() can take a lot of time, so check again if306* some character arrived307*/308goto repeat;309}310311/*312* I write to the keyboard without using interrupts, I poll instead.313* This takes for the maximum length string allowed (7) at 7812.5 baud314* 8 data 1 start 1 stop bit: 9.0 ms315* If this takes too long for normal operation, interrupt driven writing316* is the solution. (I made a feeble attempt in that direction but I317* kept it simple for now.)318*/319void ikbd_write(const char *str, int len)320{321u_char acia_stat;322323if ((len < 1) || (len > 7))324panic("ikbd: maximum string length exceeded");325while (len) {326acia_stat = acia.key_ctrl;327if (acia_stat & ACIA_TDRE) {328acia.key_data = *str++;329len--;330}331}332}333334/* Reset (without touching the clock) */335void ikbd_reset(void)336{337static const char cmd[2] = { 0x80, 0x01 };338339ikbd_write(cmd, 2);340341/*342* if all's well code 0xF1 is returned, else the break codes of343* all keys making contact344*/345}346347/* Set mouse button action */348void ikbd_mouse_button_action(int mode)349{350char cmd[2] = { 0x07, mode };351352ikbd_write(cmd, 2);353}354355/* Set relative mouse position reporting */356void ikbd_mouse_rel_pos(void)357{358static const char cmd[1] = { 0x08 };359360ikbd_write(cmd, 1);361}362EXPORT_SYMBOL(ikbd_mouse_rel_pos);363364/* Set absolute mouse position reporting */365void ikbd_mouse_abs_pos(int xmax, int ymax)366{367char cmd[5] = { 0x09, xmax>>8, xmax&0xFF, ymax>>8, ymax&0xFF };368369ikbd_write(cmd, 5);370}371372/* Set mouse keycode mode */373void ikbd_mouse_kbd_mode(int dx, int dy)374{375char cmd[3] = { 0x0A, dx, dy };376377ikbd_write(cmd, 3);378}379380/* Set mouse threshold */381void ikbd_mouse_thresh(int x, int y)382{383char cmd[3] = { 0x0B, x, y };384385ikbd_write(cmd, 3);386}387EXPORT_SYMBOL(ikbd_mouse_thresh);388389/* Set mouse scale */390void ikbd_mouse_scale(int x, int y)391{392char cmd[3] = { 0x0C, x, y };393394ikbd_write(cmd, 3);395}396397/* Interrogate mouse position */398void ikbd_mouse_pos_get(int *x, int *y)399{400static const char cmd[1] = { 0x0D };401402ikbd_write(cmd, 1);403404/* wait for returning bytes */405}406407/* Load mouse position */408void ikbd_mouse_pos_set(int x, int y)409{410char cmd[6] = { 0x0E, 0x00, x>>8, x&0xFF, y>>8, y&0xFF };411412ikbd_write(cmd, 6);413}414415/* Set Y=0 at bottom */416void ikbd_mouse_y0_bot(void)417{418static const char cmd[1] = { 0x0F };419420ikbd_write(cmd, 1);421}422423/* Set Y=0 at top */424void ikbd_mouse_y0_top(void)425{426static const char cmd[1] = { 0x10 };427428ikbd_write(cmd, 1);429}430EXPORT_SYMBOL(ikbd_mouse_y0_top);431432/* Resume */433void ikbd_resume(void)434{435static const char cmd[1] = { 0x11 };436437ikbd_write(cmd, 1);438}439440/* Disable mouse */441void ikbd_mouse_disable(void)442{443static const char cmd[1] = { 0x12 };444445ikbd_write(cmd, 1);446}447EXPORT_SYMBOL(ikbd_mouse_disable);448449/* Pause output */450void ikbd_pause(void)451{452static const char cmd[1] = { 0x13 };453454ikbd_write(cmd, 1);455}456457/* Set joystick event reporting */458void ikbd_joystick_event_on(void)459{460static const char cmd[1] = { 0x14 };461462ikbd_write(cmd, 1);463}464465/* Set joystick interrogation mode */466void ikbd_joystick_event_off(void)467{468static const char cmd[1] = { 0x15 };469470ikbd_write(cmd, 1);471}472473/* Joystick interrogation */474void ikbd_joystick_get_state(void)475{476static const char cmd[1] = { 0x16 };477478ikbd_write(cmd, 1);479}480481#if 0482/* This disables all other ikbd activities !!!! */483/* Set joystick monitoring */484void ikbd_joystick_monitor(int rate)485{486static const char cmd[2] = { 0x17, rate };487488ikbd_write(cmd, 2);489490kb_state.state = JOYSTICK_MONITOR;491}492#endif493494/* some joystick routines not in yet (0x18-0x19) */495496/* Disable joysticks */497void ikbd_joystick_disable(void)498{499static const char cmd[1] = { 0x1A };500501ikbd_write(cmd, 1);502}503504/* Time-of-day clock set */505void ikbd_clock_set(int year, int month, int day, int hour, int minute, int second)506{507char cmd[7] = { 0x1B, year, month, day, hour, minute, second };508509ikbd_write(cmd, 7);510}511512/* Interrogate time-of-day clock */513void ikbd_clock_get(int *year, int *month, int *day, int *hour, int *minute, int second)514{515static const char cmd[1] = { 0x1C };516517ikbd_write(cmd, 1);518}519520/* Memory load */521void ikbd_mem_write(int address, int size, char *data)522{523panic("Attempt to write data into keyboard memory");524}525526/* Memory read */527void ikbd_mem_read(int address, char data[6])528{529char cmd[3] = { 0x21, address>>8, address&0xFF };530531ikbd_write(cmd, 3);532533/* receive data and put it in data */534}535536/* Controller execute */537void ikbd_exec(int address)538{539char cmd[3] = { 0x22, address>>8, address&0xFF };540541ikbd_write(cmd, 3);542}543544/* Status inquiries (0x87-0x9A) not yet implemented */545546/* Set the state of the caps lock led. */547void atari_kbd_leds(unsigned int leds)548{549char cmd[6] = {32, 0, 4, 1, 254 + ((leds & 4) != 0), 0};550551ikbd_write(cmd, 6);552}553554/*555* The original code sometimes left the interrupt line of556* the ACIAs low forever. I hope, it is fixed now.557*558* Martin Rogge, 20 Aug 1995559*/560561static int atari_keyb_done = 0;562563int atari_keyb_init(void)564{565int error;566567if (atari_keyb_done)568return 0;569570kb_state.state = KEYBOARD;571kb_state.len = 0;572573error = request_irq(IRQ_MFP_ACIA, atari_keyboard_interrupt,574IRQ_TYPE_SLOW, "keyboard,mouse,MIDI",575atari_keyboard_interrupt);576if (error)577return error;578579atari_turnoff_irq(IRQ_MFP_ACIA);580do {581/* reset IKBD ACIA */582acia.key_ctrl = ACIA_RESET |583((atari_switches & ATARI_SWITCH_IKBD) ?584ACIA_RHTID : 0);585(void)acia.key_ctrl;586(void)acia.key_data;587588/* reset MIDI ACIA */589acia.mid_ctrl = ACIA_RESET |590((atari_switches & ATARI_SWITCH_MIDI) ?591ACIA_RHTID : 0);592(void)acia.mid_ctrl;593(void)acia.mid_data;594595/* divide 500kHz by 64 gives 7812.5 baud */596/* 8 data no parity 1 start 1 stop bit */597/* receive interrupt enabled */598/* RTS low (except if switch selected), transmit interrupt disabled */599acia.key_ctrl = (ACIA_DIV64|ACIA_D8N1S|ACIA_RIE) |600((atari_switches & ATARI_SWITCH_IKBD) ?601ACIA_RHTID : ACIA_RLTID);602603acia.mid_ctrl = ACIA_DIV16 | ACIA_D8N1S |604((atari_switches & ATARI_SWITCH_MIDI) ?605ACIA_RHTID : 0);606607/* make sure the interrupt line is up */608} while ((st_mfp.par_dt_reg & 0x10) == 0);609610/* enable ACIA Interrupts */611st_mfp.active_edge &= ~0x10;612atari_turnon_irq(IRQ_MFP_ACIA);613614ikbd_self_test = 1;615ikbd_reset();616/* wait for a period of inactivity (here: 0.25s), then assume the IKBD's617* self-test is finished */618self_test_last_rcv = jiffies;619while (time_before(jiffies, self_test_last_rcv + HZ/4))620barrier();621/* if not incremented: no 0xf1 received */622if (ikbd_self_test == 1)623printk(KERN_ERR "WARNING: keyboard self test failed!\n");624ikbd_self_test = 0;625626ikbd_mouse_disable();627ikbd_joystick_disable();628629#ifdef FIXED_ATARI_JOYSTICK630atari_joystick_init();631#endif632633// flag init done634atari_keyb_done = 1;635return 0;636}637EXPORT_SYMBOL_GPL(atari_keyb_init);638639640