📄 pc_keyb.c
字号:
do { /* * "handle_kbd_event()" will handle any incoming events * while we wait - keypresses or mouse movement. */ unsigned char status = handle_kbd_event(); if (! (status & KBD_STAT_IBF)) return; mdelay(1); timeout--; } while (timeout);#ifdef KBD_REPORT_TIMEOUTS printk( "Keyboard timed out[1]\n");#endif}/* * This reads the keyboard status port, and does the * appropriate action. * * It requires that we hold the keyboard controller * spinlock. */static unsigned char handle_kbd_event(void){ unsigned char status = kbd_read_status(); unsigned int work = 10000; while (status & KBD_STAT_OBF) { unsigned char scancode; scancode = kbd_read_input(); if (status & KBD_STAT_MOUSE_OBF) {#if 0 handle_mouse_event(scancode);#endif } else { if (do_acknowledge(scancode)) handle_scancode(scancode, !(scancode & 0x80)); mark_bh(KEYBOARD_BH); } status = kbd_read_status(); if(!work--) { printk( "pc_keyb: controller jammed (0x%02X).\n", status); break; } return status; } /* * the commands to set the leds for some reason, returns 0x14, 0x16 * and I am intepreting as an ACK, because the original code from * Linux was timeing out here... */ acknowledge = 1; reply_expected = 0; resend = 0; return status;}void keyboard_interrupt( void ){ handle_kbd_event();}/* * send_data sends a character to the keyboard and waits * for an acknowledge, possibly retrying if asked to. Returns * the success status. * * Don't use 'jiffies', so that we don't depend on interrupts */static int send_data(unsigned char data){ int retries = 3; do { unsigned long timeout = KBD_TIMEOUT; acknowledge = 0; /* Set by interrupt routine on receipt of ACK. */ resend = 0; reply_expected = 1; kbd_write_output_w(data); for (;;) { if (acknowledge) return 1; if (resend) break; mdelay(1); if (!--timeout) {#ifdef KBD_REPORT_TIMEOUTS printk("Keyboard timeout[2]\n");#endif return 0; } } } while (retries-- > 0);#ifdef KBD_REPORT_TIMEOUTS printk( "keyboard: Too many NACKs -- noisy kbd cable?\n");#endif return 0;}void pckbd_leds(unsigned char leds){ if (!send_data(KBD_CMD_SET_LEDS) || !send_data(leds)) send_data(KBD_CMD_ENABLE); /* re-enable kbd if any errors */}/* * In case we run on a non-x86 hardware we need to initialize both the * keyboard controller and the keyboard. On a x86, the BIOS will * already have initialized them. * * Some x86 BIOSes do not correctly initialize the keyboard, so the * "kbd-reset" command line options can be given to force a reset. * [Ranger] */#ifdef __i386__ int kbd_startup_reset = 0;#else int kbd_startup_reset = 1;#endif/* for "kbd-reset" cmdline param */void kbd_reset_setup(char *str, int *ints){ kbd_startup_reset = 1;}#define KBD_NO_DATA (-1) /* No data */#define KBD_BAD_DATA (-2) /* Parity or other error */static int kbd_read_data(void){ int retval = KBD_NO_DATA; unsigned char status; status = kbd_read_status(); if (status & KBD_STAT_OBF) { unsigned char data = kbd_read_input(); retval = data; if (status & (KBD_STAT_GTO | KBD_STAT_PERR)) retval = KBD_BAD_DATA; } return retval;}static void kbd_clear_input(void){ int maxread = 100; /* Random number */ do { if (kbd_read_data() == KBD_NO_DATA) break; } while (--maxread);}static int kbd_wait_for_input(void){ long timeout = KBD_INIT_TIMEOUT; do { int retval = kbd_read_data(); if (retval >= 0) return retval; mdelay(1); } while (--timeout); return -1;}static void kbd_write_command_w(int data){ kb_wait(); kbd_write_command(data);}static void kbd_write_output_w(int data){ kb_wait(); kbd_write_output(data);}static char * initialize_kbd(void){ int status; /* * Test the keyboard interface. * This seems to be the only way to get it going. * If the test is successful a x55 is placed in the input buffer. */ kbd_write_command_w(KBD_CCMD_SELF_TEST); if (kbd_wait_for_input() != 0x55) return "Keyboard failed self test"; /* * Perform a keyboard interface test. This causes the controller * to test the keyboard clock and data lines. The results of the * test are placed in the input buffer. */ kbd_write_command_w(KBD_CCMD_KBD_TEST); if (kbd_wait_for_input() != 0x00) return "Keyboard interface failed self test"; /* * Enable the keyboard by allowing the keyboard clock to run. */ kbd_write_command_w(KBD_CCMD_KBD_ENABLE); /* * Reset keyboard. If the read times out * then the assumption is that no keyboard is * plugged into the machine. * This defaults the keyboard to scan-code set 2. * * Set up to try again if the keyboard asks for RESEND. */ do { kbd_write_output_w(KBD_CMD_RESET); status = kbd_wait_for_input(); if (status == KBD_REPLY_ACK) break; if (status != KBD_REPLY_RESEND) return "Keyboard reset failed, no ACK"; } while (1); if (kbd_wait_for_input() != KBD_REPLY_POR) return "Keyboard reset failed, no POR"; /* * Set keyboard controller mode. During this, the keyboard should be * in the disabled state. * * Set up to try again if the keyboard asks for RESEND. */ do { kbd_write_output_w(KBD_CMD_DISABLE); status = kbd_wait_for_input(); if (status == KBD_REPLY_ACK) break; if (status != KBD_REPLY_RESEND) return "Disable keyboard: no ACK"; } while (1); kbd_write_command_w(KBD_CCMD_WRITE_MODE); kbd_write_output_w(KBD_MODE_KBD_INT | KBD_MODE_SYS | KBD_MODE_DISABLE_MOUSE | KBD_MODE_KCC); /* ibm powerpc portables need this to use scan-code set 1 -- Cort */ kbd_write_command_w(KBD_CCMD_READ_MODE); if (!(kbd_wait_for_input() & KBD_MODE_KCC)) { /* * If the controller does not support conversion, * Set the keyboard to scan-code set 1. */ kbd_write_output_w(0xF0); kbd_wait_for_input(); kbd_write_output_w(0x01); kbd_wait_for_input(); } kbd_write_output_w(KBD_CMD_ENABLE); if (kbd_wait_for_input() != KBD_REPLY_ACK) return "Enable keyboard: no ACK"; /* * Finally, set the typematic rate to maximum. */ kbd_write_output_w(KBD_CMD_SET_RATE); if (kbd_wait_for_input() != KBD_REPLY_ACK) return "Set rate: no ACK"; kbd_write_output_w(0x00); if (kbd_wait_for_input() != KBD_REPLY_ACK) return "Set rate: no ACK"; return NULL;}void pckbd_init_hw(void){ /* kbd_request_region(); */ /* Flush any pending input. */ kbd_clear_input(); if (kbd_startup_reset) { char *msg = initialize_kbd(); if (msg) printk( "initialize_kbd: %s\n", msg); }#if defined CONFIG_PSMOUSE psaux_init();#endif /* Ok, finally allocate the IRQ, and off we go.. */#if 0 kbd_request_irq( keyboard_interrupt );#endif}/*char BSP_wait_polled_input( void ){ int c,level; _CPU_ISR_Disable(level); while ( ( c= kbd_wait_for_input() ) < 0 ) continue; _CPU_ISR_Enable (level); return c;}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -