pc_keyb.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:31k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * linux/drivers/char/pc_keyb.c
  3.  *
  4.  * Separation of the PC low-level part by Geert Uytterhoeven, May 1997
  5.  * See keyboard.c for the whole history.
  6.  *
  7.  * Major cleanup by Martin Mares, May 1997
  8.  *
  9.  * Combined the keyboard and PS/2 mouse handling into one file,
  10.  * because they share the same hardware.
  11.  * Johan Myreen <jem@iki.fi> 1998-10-08.
  12.  *
  13.  * Code fixes to handle mouse ACKs properly.
  14.  * C. Scott Ananian <cananian@alumni.princeton.edu> 1999-01-29.
  15.  *
  16.  */
  17. #include <linux/config.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/sched.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/tty.h>
  22. #include <linux/mm.h>
  23. #include <linux/signal.h>
  24. #include <linux/init.h>
  25. #include <linux/kbd_ll.h>
  26. #include <linux/delay.h>
  27. #include <linux/random.h>
  28. #include <linux/poll.h>
  29. #include <linux/miscdevice.h>
  30. #include <linux/slab.h>
  31. #include <linux/kbd_kern.h>
  32. #include <linux/vt_kern.h>
  33. #include <linux/smp_lock.h>
  34. #include <linux/kd.h>
  35. #include <linux/pm.h>
  36. #include <asm/keyboard.h>
  37. #include <asm/bitops.h>
  38. #include <asm/uaccess.h>
  39. #include <asm/irq.h>
  40. #include <asm/system.h>
  41. #include <asm/io.h>
  42. /* Some configuration switches are present in the include file... */
  43. #include <linux/pc_keyb.h>
  44. /* Simple translation table for the SysRq keys */
  45. #ifdef CONFIG_MAGIC_SYSRQ
  46. unsigned char pckbd_sysrq_xlate[128] =
  47. "00331234567890-=177t" /* 0x00 - 0x0f */
  48. "qwertyuiop[]r00as" /* 0x10 - 0x1f */
  49. "dfghjkl;'`00\zxcv" /* 0x20 - 0x2f */
  50. "bnm,./00*00 00201202203204205" /* 0x30 - 0x3f */
  51. "2062072102112120000789-456+1" /* 0x40 - 0x4f */
  52. "230177000021321400000000000000000000" /* 0x50 - 0x5f */
  53. "r00/"; /* 0x60 - 0x6f */
  54. #endif
  55. static void kbd_write_command_w(int data);
  56. static void kbd_write_output_w(int data);
  57. #ifdef CONFIG_PSMOUSE
  58. static void aux_write_ack(int val);
  59. static void __aux_write_ack(int val);
  60. static int aux_reconnect = 0;
  61. #endif
  62. #ifndef kbd_controller_present
  63. #define kbd_controller_present() 1
  64. #endif
  65. static spinlock_t kbd_controller_lock = SPIN_LOCK_UNLOCKED;
  66. static unsigned char handle_kbd_event(void);
  67. /* used only by send_data - set by keyboard_interrupt */
  68. static volatile unsigned char reply_expected;
  69. static volatile unsigned char acknowledge;
  70. static volatile unsigned char resend;
  71. #if defined CONFIG_PSMOUSE
  72. /*
  73.  * PS/2 Auxiliary Device
  74.  */
  75. static int __init psaux_init(void);
  76. #define AUX_RECONNECT1 0xaa /* scancode1 when ps2 device is plugged (back) in */
  77. #define AUX_RECONNECT2 0x00 /* scancode2 when ps2 device is plugged (back) in */
  78.  
  79. static struct aux_queue *queue; /* Mouse data buffer. */
  80. static int aux_count;
  81. /* used when we send commands to the mouse that expect an ACK. */
  82. static unsigned char mouse_reply_expected;
  83. #define AUX_INTS_OFF (KBD_MODE_KCC | KBD_MODE_DISABLE_MOUSE | KBD_MODE_SYS | KBD_MODE_KBD_INT)
  84. #define AUX_INTS_ON  (KBD_MODE_KCC | KBD_MODE_SYS | KBD_MODE_MOUSE_INT | KBD_MODE_KBD_INT)
  85. #define MAX_RETRIES 60 /* some aux operations take long time*/
  86. #endif /* CONFIG_PSMOUSE */
  87. /*
  88.  * Wait for keyboard controller input buffer to drain.
  89.  *
  90.  * Don't use 'jiffies' so that we don't depend on
  91.  * interrupts..
  92.  *
  93.  * Quote from PS/2 System Reference Manual:
  94.  *
  95.  * "Address hex 0060 and address hex 0064 should be written only when
  96.  * the input-buffer-full bit and output-buffer-full bit in the
  97.  * Controller Status register are set 0."
  98.  */
  99. static void kb_wait(void)
  100. {
  101. unsigned long timeout = KBC_TIMEOUT;
  102. do {
  103. /*
  104.  * "handle_kbd_event()" will handle any incoming events
  105.  * while we wait - keypresses or mouse movement.
  106.  */
  107. unsigned char status = handle_kbd_event();
  108. if (! (status & KBD_STAT_IBF))
  109. return;
  110. mdelay(1);
  111. timeout--;
  112. } while (timeout);
  113. #ifdef KBD_REPORT_TIMEOUTS
  114. printk(KERN_WARNING "Keyboard timed out[1]n");
  115. #endif
  116. }
  117. /*
  118.  * Translation of escaped scancodes to keycodes.
  119.  * This is now user-settable.
  120.  * The keycodes 1-88,96-111,119 are fairly standard, and
  121.  * should probably not be changed - changing might confuse X.
  122.  * X also interprets scancode 0x5d (KEY_Begin).
  123.  *
  124.  * For 1-88 keycode equals scancode.
  125.  */
  126. #define E0_KPENTER 96
  127. #define E0_RCTRL   97
  128. #define E0_KPSLASH 98
  129. #define E0_PRSCR   99
  130. #define E0_RALT    100
  131. #define E0_BREAK   101  /* (control-pause) */
  132. #define E0_HOME    102
  133. #define E0_UP      103
  134. #define E0_PGUP    104
  135. #define E0_LEFT    105
  136. #define E0_RIGHT   106
  137. #define E0_END     107
  138. #define E0_DOWN    108
  139. #define E0_PGDN    109
  140. #define E0_INS     110
  141. #define E0_DEL     111
  142. #define E1_PAUSE   119
  143. /*
  144.  * The keycodes below are randomly located in 89-95,112-118,120-127.
  145.  * They could be thrown away (and all occurrences below replaced by 0),
  146.  * but that would force many users to use the `setkeycodes' utility, where
  147.  * they needed not before. It does not matter that there are duplicates, as
  148.  * long as no duplication occurs for any single keyboard.
  149.  */
  150. #define SC_LIM 89
  151. #define FOCUS_PF1 85           /* actual code! */
  152. #define FOCUS_PF2 89
  153. #define FOCUS_PF3 90
  154. #define FOCUS_PF4 91
  155. #define FOCUS_PF5 92
  156. #define FOCUS_PF6 93
  157. #define FOCUS_PF7 94
  158. #define FOCUS_PF8 95
  159. #define FOCUS_PF9 120
  160. #define FOCUS_PF10 121
  161. #define FOCUS_PF11 122
  162. #define FOCUS_PF12 123
  163. #define JAP_86     124
  164. /* tfj@olivia.ping.dk:
  165.  * The four keys are located over the numeric keypad, and are
  166.  * labelled A1-A4. It's an rc930 keyboard, from
  167.  * Regnecentralen/RC International, Now ICL.
  168.  * Scancodes: 59, 5a, 5b, 5c.
  169.  */
  170. #define RGN1 124
  171. #define RGN2 125
  172. #define RGN3 126
  173. #define RGN4 127
  174. static unsigned char high_keys[128 - SC_LIM] = {
  175.   RGN1, RGN2, RGN3, RGN4, 0, 0, 0,                   /* 0x59-0x5f */
  176.   0, 0, 0, 0, 0, 0, 0, 0,                            /* 0x60-0x67 */
  177.   0, 0, 0, 0, 0, FOCUS_PF11, 0, FOCUS_PF12,          /* 0x68-0x6f */
  178.   0, 0, 0, FOCUS_PF2, FOCUS_PF9, 0, 0, FOCUS_PF3,    /* 0x70-0x77 */
  179.   FOCUS_PF4, FOCUS_PF5, FOCUS_PF6, FOCUS_PF7,        /* 0x78-0x7b */
  180.   FOCUS_PF8, JAP_86, FOCUS_PF10, 0                   /* 0x7c-0x7f */
  181. };
  182. /* BTC */
  183. #define E0_MACRO   112
  184. /* LK450 */
  185. #define E0_F13     113
  186. #define E0_F14     114
  187. #define E0_HELP    115
  188. #define E0_DO      116
  189. #define E0_F17     117
  190. #define E0_KPMINPLUS 118
  191. /*
  192.  * My OmniKey generates e0 4c for  the "OMNI" key and the
  193.  * right alt key does nada. [kkoller@nyx10.cs.du.edu]
  194.  */
  195. #define E0_OK 124
  196. /*
  197.  * New microsoft keyboard is rumoured to have
  198.  * e0 5b (left window button), e0 5c (right window button),
  199.  * e0 5d (menu button). [or: LBANNER, RBANNER, RMENU]
  200.  * [or: Windows_L, Windows_R, TaskMan]
  201.  */
  202. #define E0_MSLW 125
  203. #define E0_MSRW 126
  204. #define E0_MSTM 127
  205. static unsigned char e0_keys[128] = {
  206.   0, 0, 0, 0, 0, 0, 0, 0,       /* 0x00-0x07 */
  207.   0, 0, 0, 0, 0, 0, 0, 0,       /* 0x08-0x0f */
  208.   0, 0, 0, 0, 0, 0, 0, 0,       /* 0x10-0x17 */
  209.   0, 0, 0, 0, E0_KPENTER, E0_RCTRL, 0, 0,       /* 0x18-0x1f */
  210.   0, 0, 0, 0, 0, 0, 0, 0,       /* 0x20-0x27 */
  211.   0, 0, 0, 0, 0, 0, 0, 0,       /* 0x28-0x2f */
  212.   0, 0, 0, 0, 0, E0_KPSLASH, 0, E0_PRSCR,       /* 0x30-0x37 */
  213.   E0_RALT, 0, 0, 0, 0, E0_F13, E0_F14, E0_HELP,       /* 0x38-0x3f */
  214.   E0_DO, E0_F17, 0, 0, 0, 0, E0_BREAK, E0_HOME,       /* 0x40-0x47 */
  215.   E0_UP, E0_PGUP, 0, E0_LEFT, E0_OK, E0_RIGHT, E0_KPMINPLUS, E0_END,/* 0x48-0x4f */
  216.   E0_DOWN, E0_PGDN, E0_INS, E0_DEL, 0, 0, 0, 0,       /* 0x50-0x57 */
  217.   0, 0, 0, E0_MSLW, E0_MSRW, E0_MSTM, 0, 0,       /* 0x58-0x5f */
  218.   0, 0, 0, 0, 0, 0, 0, 0,       /* 0x60-0x67 */
  219.   0, 0, 0, 0, 0, 0, 0, E0_MACRO,       /* 0x68-0x6f */
  220.   0, 0, 0, 0, 0, 0, 0, 0,       /* 0x70-0x77 */
  221.   0, 0, 0, 0, 0, 0, 0, 0       /* 0x78-0x7f */
  222. };
  223. int pckbd_setkeycode(unsigned int scancode, unsigned int keycode)
  224. {
  225. if (scancode < SC_LIM || scancode > 255 || keycode > 127)
  226.   return -EINVAL;
  227. if (scancode < 128)
  228.   high_keys[scancode - SC_LIM] = keycode;
  229. else
  230.   e0_keys[scancode - 128] = keycode;
  231. return 0;
  232. }
  233. int pckbd_getkeycode(unsigned int scancode)
  234. {
  235. return
  236.   (scancode < SC_LIM || scancode > 255) ? -EINVAL :
  237.   (scancode < 128) ? high_keys[scancode - SC_LIM] :
  238.     e0_keys[scancode - 128];
  239. }
  240. static int do_acknowledge(unsigned char scancode)
  241. {
  242. if (reply_expected) {
  243.   /* Unfortunately, we must recognise these codes only if we know they
  244.    * are known to be valid (i.e., after sending a command), because there
  245.    * are some brain-damaged keyboards (yes, FOCUS 9000 again) which have
  246.    * keys with such codes :(
  247.    */
  248. if (scancode == KBD_REPLY_ACK) {
  249. acknowledge = 1;
  250. reply_expected = 0;
  251. return 0;
  252. } else if (scancode == KBD_REPLY_RESEND) {
  253. resend = 1;
  254. reply_expected = 0;
  255. return 0;
  256. }
  257. /* Should not happen... */
  258. #if 0
  259. printk(KERN_DEBUG "keyboard reply expected - got %02xn",
  260.        scancode);
  261. #endif
  262. }
  263. return 1;
  264. }
  265. int pckbd_translate(unsigned char scancode, unsigned char *keycode,
  266.     char raw_mode)
  267. {
  268. static int prev_scancode;
  269. /* special prefix scancodes.. */
  270. if (scancode == 0xe0 || scancode == 0xe1) {
  271. prev_scancode = scancode;
  272. return 0;
  273. }
  274. /* 0xFF is sent by a few keyboards, ignore it. 0x00 is error */
  275. if (scancode == 0x00 || scancode == 0xff) {
  276. prev_scancode = 0;
  277. return 0;
  278. }
  279. scancode &= 0x7f;
  280. if (prev_scancode) {
  281.   /*
  282.    * usually it will be 0xe0, but a Pause key generates
  283.    * e1 1d 45 e1 9d c5 when pressed, and nothing when released
  284.    */
  285.   if (prev_scancode != 0xe0) {
  286.       if (prev_scancode == 0xe1 && scancode == 0x1d) {
  287.   prev_scancode = 0x100;
  288.   return 0;
  289.       } else if (prev_scancode == 0x100 && scancode == 0x45) {
  290.   *keycode = E1_PAUSE;
  291.   prev_scancode = 0;
  292.       } else {
  293. #ifdef KBD_REPORT_UNKN
  294.   if (!raw_mode)
  295.     printk(KERN_INFO "keyboard: unknown e1 escape sequencen");
  296. #endif
  297.   prev_scancode = 0;
  298.   return 0;
  299.       }
  300.   } else {
  301.       prev_scancode = 0;
  302.       /*
  303.        *  The keyboard maintains its own internal caps lock and
  304.        *  num lock statuses. In caps lock mode E0 AA precedes make
  305.        *  code and E0 2A follows break code. In num lock mode,
  306.        *  E0 2A precedes make code and E0 AA follows break code.
  307.        *  We do our own book-keeping, so we will just ignore these.
  308.        */
  309.       /*
  310.        *  For my keyboard there is no caps lock mode, but there are
  311.        *  both Shift-L and Shift-R modes. The former mode generates
  312.        *  E0 2A / E0 AA pairs, the latter E0 B6 / E0 36 pairs.
  313.        *  So, we should also ignore the latter. - aeb@cwi.nl
  314.        */
  315.       if (scancode == 0x2a || scancode == 0x36)
  316. return 0;
  317.       if (e0_keys[scancode])
  318. *keycode = e0_keys[scancode];
  319.       else {
  320. #ifdef KBD_REPORT_UNKN
  321.   if (!raw_mode)
  322.     printk(KERN_INFO "keyboard: unknown scancode e0 %02xn",
  323.    scancode);
  324. #endif
  325.   return 0;
  326.       }
  327.   }
  328. } else if (scancode >= SC_LIM) {
  329.     /* This happens with the FOCUS 9000 keyboard
  330.        Its keys PF1..PF12 are reported to generate
  331.        55 73 77 78 79 7a 7b 7c 74 7e 6d 6f
  332.        Moreover, unless repeated, they do not generate
  333.        key-down events, so we have to zero up_flag below */
  334.     /* Also, Japanese 86/106 keyboards are reported to
  335.        generate 0x73 and 0x7d for  - and  | respectively. */
  336.     /* Also, some Brazilian keyboard is reported to produce
  337.        0x73 and 0x7e for  ? and KP-dot, respectively. */
  338.   *keycode = high_keys[scancode - SC_LIM];
  339.   if (!*keycode) {
  340.       if (!raw_mode) {
  341. #ifdef KBD_REPORT_UNKN
  342.   printk(KERN_INFO "keyboard: unrecognized scancode (%02x)"
  343.  " - ignoredn", scancode);
  344. #endif
  345.       }
  346.       return 0;
  347.   }
  348.   } else
  349.   *keycode = scancode;
  350.   return 1;
  351. }
  352. char pckbd_unexpected_up(unsigned char keycode)
  353. {
  354. /* unexpected, but this can happen: maybe this was a key release for a
  355.    FOCUS 9000 PF key; if we want to see it, we have to clear up_flag */
  356. if (keycode >= SC_LIM || keycode == 85)
  357.     return 0;
  358. else
  359.     return 0200;
  360. }
  361. int pckbd_pm_resume(struct pm_dev *dev, pm_request_t rqst, void *data) 
  362. {
  363. #if defined CONFIG_PSMOUSE
  364.        unsigned long flags;
  365.        if (rqst == PM_RESUME) {
  366.                if (queue) {                    /* Aux port detected */
  367.                        if (aux_count == 0) {   /* Mouse not in use */ 
  368.                                spin_lock_irqsave(&kbd_controller_lock, flags);
  369.        /*
  370. * Dell Lat. C600 A06 enables mouse after resume.
  371. * When user touches the pad, it posts IRQ 12
  372. * (which we do not process), thus holding keyboard.
  373. */
  374.        kbd_write_command(KBD_CCMD_MOUSE_DISABLE);
  375.        /* kbd_write_cmd(AUX_INTS_OFF); */ /* Config & lock */
  376.        kb_wait();
  377.        kbd_write_command(KBD_CCMD_WRITE_MODE);
  378.        kb_wait();
  379.        kbd_write_output(AUX_INTS_OFF);
  380.        spin_unlock_irqrestore(&kbd_controller_lock, flags);
  381.        }
  382.        }
  383.        }
  384. #endif
  385.        return 0;
  386. }
  387. static inline void handle_mouse_event(unsigned char scancode)
  388. {
  389. #ifdef CONFIG_PSMOUSE
  390. static unsigned char prev_code;
  391. if (mouse_reply_expected) {
  392. if (scancode == AUX_ACK) {
  393. mouse_reply_expected--;
  394. return;
  395. }
  396. mouse_reply_expected = 0;
  397. }
  398. else if(scancode == AUX_RECONNECT2 && prev_code == AUX_RECONNECT1
  399. && aux_reconnect) {
  400. printk (KERN_INFO "PS/2 mouse reconnect detectedn");
  401. queue->head = queue->tail = 0; /* Flush input queue */
  402. __aux_write_ack(AUX_ENABLE_DEV);  /* ping the mouse :) */
  403. return;
  404. }
  405. prev_code = scancode;
  406. add_mouse_randomness(scancode);
  407. if (aux_count) {
  408. int head = queue->head;
  409. queue->buf[head] = scancode;
  410. head = (head + 1) & (AUX_BUF_SIZE-1);
  411. if (head != queue->tail) {
  412. queue->head = head;
  413. kill_fasync(&queue->fasync, SIGIO, POLL_IN);
  414. wake_up_interruptible(&queue->proc_list);
  415. }
  416. }
  417. #endif
  418. }
  419. static unsigned char kbd_exists = 1;
  420. static inline void handle_keyboard_event(unsigned char scancode)
  421. {
  422. #ifdef CONFIG_VT
  423. kbd_exists = 1;
  424. if (do_acknowledge(scancode))
  425. handle_scancode(scancode, !(scancode & 0x80));
  426. #endif
  427. tasklet_schedule(&keyboard_tasklet);
  428. }
  429. /*
  430.  * This reads the keyboard status port, and does the
  431.  * appropriate action.
  432.  *
  433.  * It requires that we hold the keyboard controller
  434.  * spinlock.
  435.  */
  436. static unsigned char handle_kbd_event(void)
  437. {
  438. unsigned char status = kbd_read_status();
  439. unsigned int work = 10000;
  440. while ((--work > 0) && (status & KBD_STAT_OBF)) {
  441. unsigned char scancode;
  442. scancode = kbd_read_input();
  443. /* Error bytes must be ignored to make the 
  444.    Synaptics touchpads compaq use work */
  445. #if 1
  446. /* Ignore error bytes */
  447. if (!(status & (KBD_STAT_GTO | KBD_STAT_PERR)))
  448. #endif
  449. {
  450. if (status & KBD_STAT_MOUSE_OBF)
  451. handle_mouse_event(scancode);
  452. else
  453. handle_keyboard_event(scancode);
  454. }
  455. status = kbd_read_status();
  456. }
  457. if (!work)
  458. printk(KERN_ERR "pc_keyb: controller jammed (0x%02X).n", status);
  459. return status;
  460. }
  461. static void keyboard_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  462. {
  463. #ifdef CONFIG_VT
  464. kbd_pt_regs = regs;
  465. #endif
  466. spin_lock_irq(&kbd_controller_lock);
  467. handle_kbd_event();
  468. spin_unlock_irq(&kbd_controller_lock);
  469. }
  470. /*
  471.  * send_data sends a character to the keyboard and waits
  472.  * for an acknowledge, possibly retrying if asked to. Returns
  473.  * the success status.
  474.  *
  475.  * Don't use 'jiffies', so that we don't depend on interrupts
  476.  */
  477. static int send_data(unsigned char data)
  478. {
  479. int retries = 3;
  480. do {
  481. unsigned long timeout = KBD_TIMEOUT;
  482. acknowledge = 0; /* Set by interrupt routine on receipt of ACK. */
  483. resend = 0;
  484. reply_expected = 1;
  485. kbd_write_output_w(data);
  486. for (;;) {
  487. if (acknowledge)
  488. return 1;
  489. if (resend)
  490. break;
  491. mdelay(1);
  492. if (!--timeout) {
  493. #ifdef KBD_REPORT_TIMEOUTS
  494. printk(KERN_WARNING "keyboard: Timeout - AT keyboard not present?(%02x)n", data);
  495. #endif
  496. return 0;
  497. }
  498. }
  499. } while (retries-- > 0);
  500. #ifdef KBD_REPORT_TIMEOUTS
  501. printk(KERN_WARNING "keyboard: Too many NACKs -- noisy kbd cable?n");
  502. #endif
  503. return 0;
  504. }
  505. void pckbd_leds(unsigned char leds)
  506. {
  507. if (kbd_exists && (!send_data(KBD_CMD_SET_LEDS) || !send_data(leds))) {
  508. send_data(KBD_CMD_ENABLE); /* re-enable kbd if any errors */
  509. kbd_exists = 0;
  510. }
  511. }
  512. #define DEFAULT_KEYB_REP_DELAY 250
  513. #define DEFAULT_KEYB_REP_RATE 30 /* cps */
  514. static struct kbd_repeat kbdrate={
  515. DEFAULT_KEYB_REP_DELAY,
  516. DEFAULT_KEYB_REP_RATE
  517. };
  518. static unsigned char parse_kbd_rate(struct kbd_repeat *r)
  519. {
  520. static struct r2v{
  521. int rate;
  522. unsigned char val;
  523. } kbd_rates[]={ {5,0x14},
  524. {7,0x10},
  525. {10,0x0c},
  526. {15,0x08},
  527. {20,0x04},
  528. {25,0x02},
  529. {30,0x00}
  530. };
  531. static struct d2v{
  532. int delay;
  533. unsigned char val;
  534. } kbd_delays[]={{250,0},
  535. {500,1},
  536. {750,2},
  537. {1000,3}
  538. };
  539. int rate=0,delay=0;
  540. if (r != NULL){
  541. int i,new_rate=30,new_delay=250;
  542. if (r->rate <= 0)
  543. r->rate=kbdrate.rate;
  544. if (r->delay <= 0)
  545. r->delay=kbdrate.delay;
  546. for (i=0; i < sizeof(kbd_rates)/sizeof(struct r2v); i++)
  547. if (kbd_rates[i].rate == r->rate){
  548. new_rate=kbd_rates[i].rate;
  549. rate=kbd_rates[i].val;
  550. break;
  551. }
  552. for (i=0; i < sizeof(kbd_delays)/sizeof(struct d2v); i++)
  553. if (kbd_delays[i].delay == r->delay){
  554. new_delay=kbd_delays[i].delay;
  555. delay=kbd_delays[i].val;
  556. break;
  557. }
  558. r->rate=new_rate;
  559. r->delay=new_delay;
  560. }
  561. return (delay << 5) | rate;
  562. }
  563. static int write_kbd_rate(unsigned char r)
  564. {
  565. if (!send_data(KBD_CMD_SET_RATE) || !send_data(r)){
  566. send_data(KBD_CMD_ENABLE);  /* re-enable kbd if any errors */
  567. return 0;
  568. }else
  569. return 1;
  570. }
  571. static int pckbd_rate(struct kbd_repeat *rep)
  572. {
  573. if (rep == NULL)
  574. return -EINVAL;
  575. else{
  576. unsigned char r=parse_kbd_rate(rep);
  577. struct kbd_repeat old_rep;
  578. memcpy(&old_rep,&kbdrate,sizeof(struct kbd_repeat));
  579. if (write_kbd_rate(r)){
  580. memcpy(&kbdrate,rep,sizeof(struct kbd_repeat));
  581. memcpy(rep,&old_rep,sizeof(struct kbd_repeat));
  582. return 0;
  583. }
  584. }
  585. return -EIO;
  586. }
  587. /*
  588.  * In case we run on a non-x86 hardware we need to initialize both the
  589.  * keyboard controller and the keyboard.  On a x86, the BIOS will
  590.  * already have initialized them.
  591.  *
  592.  * Some x86 BIOSes do not correctly initialize the keyboard, so the
  593.  * "kbd-reset" command line options can be given to force a reset.
  594.  * [Ranger]
  595.  */
  596. #ifdef __i386__
  597.  int kbd_startup_reset __initdata = 0;
  598. #else
  599.  int kbd_startup_reset __initdata = 1;
  600. #endif
  601. /* for "kbd-reset" cmdline param */
  602. static int __init kbd_reset_setup(char *str)
  603. {
  604. kbd_startup_reset = 1;
  605. return 1;
  606. }
  607. __setup("kbd-reset", kbd_reset_setup);
  608. #define KBD_NO_DATA (-1) /* No data */
  609. #define KBD_BAD_DATA (-2) /* Parity or other error */
  610. static int __init kbd_read_data(void)
  611. {
  612. int retval = KBD_NO_DATA;
  613. unsigned char status;
  614. status = kbd_read_status();
  615. if (status & KBD_STAT_OBF) {
  616. unsigned char data = kbd_read_input();
  617. retval = data;
  618. if (status & (KBD_STAT_GTO | KBD_STAT_PERR))
  619. retval = KBD_BAD_DATA;
  620. }
  621. return retval;
  622. }
  623. static void __init kbd_clear_input(void)
  624. {
  625. int maxread = 100; /* Random number */
  626. do {
  627. if (kbd_read_data() == KBD_NO_DATA)
  628. break;
  629. } while (--maxread);
  630. }
  631. static int __init kbd_wait_for_input(void)
  632. {
  633. long timeout = KBD_INIT_TIMEOUT;
  634. do {
  635. int retval = kbd_read_data();
  636. if (retval >= 0)
  637. return retval;
  638. mdelay(1);
  639. } while (--timeout);
  640. return -1;
  641. }
  642. static void kbd_write_command_w(int data)
  643. {
  644. unsigned long flags;
  645. spin_lock_irqsave(&kbd_controller_lock, flags);
  646. kb_wait();
  647. kbd_write_command(data);
  648. spin_unlock_irqrestore(&kbd_controller_lock, flags);
  649. }
  650. static void kbd_write_output_w(int data)
  651. {
  652. unsigned long flags;
  653. spin_lock_irqsave(&kbd_controller_lock, flags);
  654. kb_wait();
  655. kbd_write_output(data);
  656. spin_unlock_irqrestore(&kbd_controller_lock, flags);
  657. }
  658. #if defined(__alpha__)
  659. /*
  660.  * Some Alphas cannot mask some/all interrupts, so we have to
  661.  * make sure not to allow interrupts AT ALL when polling for
  662.  * specific return values from the keyboard.
  663.  *
  664.  * I think this should work on any architecture, but for now, only Alpha.
  665.  */
  666. static int kbd_write_command_w_and_wait(int data)
  667. {
  668. unsigned long flags;
  669. int input;
  670. spin_lock_irqsave(&kbd_controller_lock, flags);
  671. kb_wait();
  672. kbd_write_command(data);
  673. input = kbd_wait_for_input();
  674. spin_unlock_irqrestore(&kbd_controller_lock, flags);
  675. return input;
  676. }
  677. static int kbd_write_output_w_and_wait(int data)
  678. {
  679. unsigned long flags;
  680. int input;
  681. spin_lock_irqsave(&kbd_controller_lock, flags);
  682. kb_wait();
  683. kbd_write_output(data);
  684. input = kbd_wait_for_input();
  685. spin_unlock_irqrestore(&kbd_controller_lock, flags);
  686. return input;
  687. }
  688. #else
  689. static int kbd_write_command_w_and_wait(int data)
  690. {
  691. kbd_write_command_w(data);
  692. return kbd_wait_for_input();
  693. }
  694. static int kbd_write_output_w_and_wait(int data)
  695. {
  696. kbd_write_output_w(data);
  697. return kbd_wait_for_input();
  698. }
  699. #endif /* __alpha__ */
  700. #if defined CONFIG_PSMOUSE
  701. static void kbd_write_cmd(int cmd)
  702. {
  703. unsigned long flags;
  704. spin_lock_irqsave(&kbd_controller_lock, flags);
  705. kb_wait();
  706. kbd_write_command(KBD_CCMD_WRITE_MODE);
  707. kb_wait();
  708. kbd_write_output(cmd);
  709. spin_unlock_irqrestore(&kbd_controller_lock, flags);
  710. }
  711. #endif /* CONFIG_PSMOUSE */
  712. static char * __init initialize_kbd(void)
  713. {
  714. int status;
  715. /*
  716.  * Test the keyboard interface.
  717.  * This seems to be the only way to get it going.
  718.  * If the test is successful a x55 is placed in the input buffer.
  719.  */
  720. kbd_write_command_w(KBD_CCMD_SELF_TEST);
  721. if (kbd_wait_for_input() != 0x55)
  722. return "Keyboard failed self test";
  723. /*
  724.  * Perform a keyboard interface test.  This causes the controller
  725.  * to test the keyboard clock and data lines.  The results of the
  726.  * test are placed in the input buffer.
  727.  */
  728. kbd_write_command_w(KBD_CCMD_KBD_TEST);
  729. if (kbd_wait_for_input() != 0x00)
  730. return "Keyboard interface failed self test";
  731. /*
  732.  * Enable the keyboard by allowing the keyboard clock to run.
  733.  */
  734. kbd_write_command_w(KBD_CCMD_KBD_ENABLE);
  735. /*
  736.  * Reset keyboard. If the read times out
  737.  * then the assumption is that no keyboard is
  738.  * plugged into the machine.
  739.  * This defaults the keyboard to scan-code set 2.
  740.  *
  741.  * Set up to try again if the keyboard asks for RESEND.
  742.  */
  743. do {
  744. kbd_write_output_w(KBD_CMD_RESET);
  745. status = kbd_wait_for_input();
  746. if (status == KBD_REPLY_ACK)
  747. break;
  748. if (status != KBD_REPLY_RESEND)
  749. return "Keyboard reset failed, no ACK";
  750. } while (1);
  751. if (kbd_wait_for_input() != KBD_REPLY_POR)
  752. return "Keyboard reset failed, no POR";
  753. /*
  754.  * Set keyboard controller mode. During this, the keyboard should be
  755.  * in the disabled state.
  756.  *
  757.  * Set up to try again if the keyboard asks for RESEND.
  758.  */
  759. do {
  760. kbd_write_output_w(KBD_CMD_DISABLE);
  761. status = kbd_wait_for_input();
  762. if (status == KBD_REPLY_ACK)
  763. break;
  764. if (status != KBD_REPLY_RESEND)
  765. return "Disable keyboard: no ACK";
  766. } while (1);
  767. kbd_write_command_w(KBD_CCMD_WRITE_MODE);
  768. kbd_write_output_w(KBD_MODE_KBD_INT
  769.       | KBD_MODE_SYS
  770.       | KBD_MODE_DISABLE_MOUSE
  771.       | KBD_MODE_KCC);
  772. /* ibm powerpc portables need this to use scan-code set 1 -- Cort */
  773. if (!(kbd_write_command_w_and_wait(KBD_CCMD_READ_MODE) & KBD_MODE_KCC))
  774. {
  775. /*
  776.  * If the controller does not support conversion,
  777.  * Set the keyboard to scan-code set 1.
  778.  */
  779. kbd_write_output_w(0xF0);
  780. kbd_wait_for_input();
  781. kbd_write_output_w(0x01);
  782. kbd_wait_for_input();
  783. }
  784. if (kbd_write_output_w_and_wait(KBD_CMD_ENABLE) != KBD_REPLY_ACK)
  785. return "Enable keyboard: no ACK";
  786. /*
  787.  * Finally, set the typematic rate to maximum.
  788.  */
  789. if (kbd_write_output_w_and_wait(KBD_CMD_SET_RATE) != KBD_REPLY_ACK)
  790. return "Set rate: no ACK";
  791. if (kbd_write_output_w_and_wait(0x00) != KBD_REPLY_ACK)
  792. return "Set rate: no 2nd ACK";
  793. return NULL;
  794. }
  795. void __init pckbd_init_hw(void)
  796. {
  797. if (!kbd_controller_present()) {
  798. kbd_exists = 0;
  799. return;
  800. }
  801. kbd_request_region();
  802. /* Flush any pending input. */
  803. kbd_clear_input();
  804. if (kbd_startup_reset) {
  805. char *msg = initialize_kbd();
  806. if (msg)
  807. printk(KERN_WARNING "initialize_kbd: %sn", msg);
  808. }
  809. #if defined CONFIG_PSMOUSE
  810. psaux_init();
  811. #endif
  812. kbd_rate = pckbd_rate;
  813. /* Ok, finally allocate the IRQ, and off we go.. */
  814. kbd_request_irq(keyboard_interrupt);
  815. }
  816. #if defined CONFIG_PSMOUSE
  817. static int __init aux_reconnect_setup (char *str)
  818. {
  819. aux_reconnect = 1;
  820. return 1;
  821. }
  822. __setup("psaux-reconnect", aux_reconnect_setup);
  823. /*
  824.  * Check if this is a dual port controller.
  825.  */
  826. static int __init detect_auxiliary_port(void)
  827. {
  828. unsigned long flags;
  829. int loops = 10;
  830. int retval = 0;
  831. /* Check if the BIOS detected a device on the auxiliary port. */
  832. if (aux_device_present == 0xaa)
  833. return 1;
  834. spin_lock_irqsave(&kbd_controller_lock, flags);
  835. /* Put the value 0x5A in the output buffer using the "Write
  836.  * Auxiliary Device Output Buffer" command (0xD3). Poll the
  837.  * Status Register for a while to see if the value really
  838.  * turns up in the Data Register. If the KBD_STAT_MOUSE_OBF
  839.  * bit is also set to 1 in the Status Register, we assume this
  840.  * controller has an Auxiliary Port (a.k.a. Mouse Port).
  841.  */
  842. kb_wait();
  843. kbd_write_command(KBD_CCMD_WRITE_AUX_OBUF);
  844. kb_wait();
  845. kbd_write_output(0x5a); /* 0x5a is a random dummy value. */
  846. do {
  847. unsigned char status = kbd_read_status();
  848. if (status & KBD_STAT_OBF) {
  849. (void) kbd_read_input();
  850. if (status & KBD_STAT_MOUSE_OBF) {
  851. printk(KERN_INFO "Detected PS/2 Mouse Port.n");
  852. retval = 1;
  853. }
  854. break;
  855. }
  856. mdelay(1);
  857. } while (--loops);
  858. spin_unlock_irqrestore(&kbd_controller_lock, flags);
  859. return retval;
  860. }
  861. /*
  862.  * Send a byte to the mouse.
  863.  */
  864. static void aux_write_dev(int val)
  865. {
  866. unsigned long flags;
  867. spin_lock_irqsave(&kbd_controller_lock, flags);
  868. kb_wait();
  869. kbd_write_command(KBD_CCMD_WRITE_MOUSE);
  870. kb_wait();
  871. kbd_write_output(val);
  872. spin_unlock_irqrestore(&kbd_controller_lock, flags);
  873. }
  874. /*
  875.  * Send a byte to the mouse & handle returned ack
  876.  */
  877. static void __aux_write_ack(int val)
  878. {
  879. kb_wait();
  880. kbd_write_command(KBD_CCMD_WRITE_MOUSE);
  881. kb_wait();
  882. kbd_write_output(val);
  883. /* we expect an ACK in response. */
  884. mouse_reply_expected++;
  885. kb_wait();
  886. }
  887. static void aux_write_ack(int val)
  888. {
  889. unsigned long flags;
  890. spin_lock_irqsave(&kbd_controller_lock, flags);
  891. __aux_write_ack(val);
  892. spin_unlock_irqrestore(&kbd_controller_lock, flags);
  893. }
  894. static unsigned char get_from_queue(void)
  895. {
  896. unsigned char result;
  897. unsigned long flags;
  898. spin_lock_irqsave(&kbd_controller_lock, flags);
  899. result = queue->buf[queue->tail];
  900. queue->tail = (queue->tail + 1) & (AUX_BUF_SIZE-1);
  901. spin_unlock_irqrestore(&kbd_controller_lock, flags);
  902. return result;
  903. }
  904. static inline int queue_empty(void)
  905. {
  906. return queue->head == queue->tail;
  907. }
  908. static int fasync_aux(int fd, struct file *filp, int on)
  909. {
  910. int retval;
  911. retval = fasync_helper(fd, filp, on, &queue->fasync);
  912. if (retval < 0)
  913. return retval;
  914. return 0;
  915. }
  916. /*
  917.  * Random magic cookie for the aux device
  918.  */
  919. #define AUX_DEV ((void *)queue)
  920. static int release_aux(struct inode * inode, struct file * file)
  921. {
  922. lock_kernel();
  923. fasync_aux(-1, file, 0);
  924. if (--aux_count) {
  925. unlock_kernel();
  926. return 0;
  927. }
  928. kbd_write_cmd(AUX_INTS_OFF);     /* Disable controller ints */
  929. kbd_write_command_w(KBD_CCMD_MOUSE_DISABLE);
  930. aux_free_irq(AUX_DEV);
  931. unlock_kernel();
  932. return 0;
  933. }
  934. /*
  935.  * Install interrupt handler.
  936.  * Enable auxiliary device.
  937.  */
  938. static int open_aux(struct inode * inode, struct file * file)
  939. {
  940. if (aux_count++) {
  941. return 0;
  942. }
  943. queue->head = queue->tail = 0; /* Flush input queue */
  944. if (aux_request_irq(keyboard_interrupt, AUX_DEV)) {
  945. aux_count--;
  946. return -EBUSY;
  947. }
  948. kbd_write_command_w(KBD_CCMD_MOUSE_ENABLE); /* Enable the
  949.    auxiliary port on
  950.    controller. */
  951. aux_write_ack(AUX_ENABLE_DEV); /* Enable aux device */
  952. kbd_write_cmd(AUX_INTS_ON); /* Enable controller ints */
  953. mdelay(2); /* Ensure we follow the kbc access delay rules.. */
  954. send_data(KBD_CMD_ENABLE); /* try to workaround toshiba4030cdt problem */
  955. return 0;
  956. }
  957. /*
  958.  * Put bytes from input queue to buffer.
  959.  */
  960. static ssize_t read_aux(struct file * file, char * buffer,
  961. size_t count, loff_t *ppos)
  962. {
  963. DECLARE_WAITQUEUE(wait, current);
  964. ssize_t i = count;
  965. unsigned char c;
  966. if (queue_empty()) {
  967. if (file->f_flags & O_NONBLOCK)
  968. return -EAGAIN;
  969. add_wait_queue(&queue->proc_list, &wait);
  970. repeat:
  971. set_current_state(TASK_INTERRUPTIBLE);
  972. if (queue_empty() && !signal_pending(current)) {
  973. schedule();
  974. goto repeat;
  975. }
  976. current->state = TASK_RUNNING;
  977. remove_wait_queue(&queue->proc_list, &wait);
  978. }
  979. while (i > 0 && !queue_empty()) {
  980. c = get_from_queue();
  981. put_user(c, buffer++);
  982. i--;
  983. }
  984. if (count-i) {
  985. file->f_dentry->d_inode->i_atime = CURRENT_TIME;
  986. return count-i;
  987. }
  988. if (signal_pending(current))
  989. return -ERESTARTSYS;
  990. return 0;
  991. }
  992. /*
  993.  * Write to the aux device.
  994.  */
  995. static ssize_t write_aux(struct file * file, const char * buffer,
  996.  size_t count, loff_t *ppos)
  997. {
  998. ssize_t retval = 0;
  999. if (count) {
  1000. ssize_t written = 0;
  1001. if (count > 32)
  1002. count = 32; /* Limit to 32 bytes. */
  1003. do {
  1004. char c;
  1005. get_user(c, buffer++);
  1006. aux_write_dev(c);
  1007. written++;
  1008. } while (--count);
  1009. retval = -EIO;
  1010. if (written) {
  1011. retval = written;
  1012. file->f_dentry->d_inode->i_mtime = CURRENT_TIME;
  1013. }
  1014. }
  1015. return retval;
  1016. }
  1017. /* No kernel lock held - fine */
  1018. static unsigned int aux_poll(struct file *file, poll_table * wait)
  1019. {
  1020. poll_wait(file, &queue->proc_list, wait);
  1021. if (!queue_empty())
  1022. return POLLIN | POLLRDNORM;
  1023. return 0;
  1024. }
  1025. struct file_operations psaux_fops = {
  1026. read: read_aux,
  1027. write: write_aux,
  1028. poll: aux_poll,
  1029. open: open_aux,
  1030. release: release_aux,
  1031. fasync: fasync_aux,
  1032. };
  1033. /*
  1034.  * Initialize driver.
  1035.  */
  1036. static struct miscdevice psaux_mouse = {
  1037. PSMOUSE_MINOR, "psaux", &psaux_fops
  1038. };
  1039. static int __init psaux_init(void)
  1040. {
  1041. int retval;
  1042. if (!detect_auxiliary_port())
  1043. return -EIO;
  1044. if ((retval = misc_register(&psaux_mouse)))
  1045. return retval;
  1046. queue = (struct aux_queue *) kmalloc(sizeof(*queue), GFP_KERNEL);
  1047. if (queue == NULL) {
  1048. printk(KERN_ERR "psaux_init(): out of memoryn");
  1049. misc_deregister(&psaux_mouse);
  1050. return -ENOMEM;
  1051. }
  1052. memset(queue, 0, sizeof(*queue));
  1053. queue->head = queue->tail = 0;
  1054. init_waitqueue_head(&queue->proc_list);
  1055. #ifdef INITIALIZE_MOUSE
  1056. kbd_write_command_w(KBD_CCMD_MOUSE_ENABLE); /* Enable Aux. */
  1057. aux_write_ack(AUX_SET_SAMPLE);
  1058. aux_write_ack(100); /* 100 samples/sec */
  1059. aux_write_ack(AUX_SET_RES);
  1060. aux_write_ack(3); /* 8 counts per mm */
  1061. aux_write_ack(AUX_SET_SCALE21); /* 2:1 scaling */
  1062. #endif /* INITIALIZE_MOUSE */
  1063. kbd_write_command(KBD_CCMD_MOUSE_DISABLE); /* Disable aux device. */
  1064. kbd_write_cmd(AUX_INTS_OFF); /* Disable controller ints. */
  1065. return 0;
  1066. }
  1067. #endif /* CONFIG_PSMOUSE */
  1068. static int blink_frequency = HZ/2;
  1069. /* Tell the user who may be running in X and not see the console that we have 
  1070.    panic'ed. This is to distingush panics from "real" lockups. 
  1071.    Could in theory send the panic message as morse, but that is left as an
  1072.    exercise for the reader.  */ 
  1073. void panic_blink(void)
  1074. static unsigned long last_jiffie;
  1075. static char led;
  1076. /* Roughly 1/2s frequency. KDB uses about 1s. Make sure it is 
  1077.    different. */
  1078. if (!blink_frequency) 
  1079. return;
  1080. if (jiffies - last_jiffie > blink_frequency) {
  1081. led ^= 0x01 | 0x04;
  1082. while (kbd_read_status() & KBD_STAT_IBF) mdelay(1); 
  1083. kbd_write_output(KBD_CMD_SET_LEDS);
  1084. mdelay(1); 
  1085. while (kbd_read_status() & KBD_STAT_IBF) mdelay(1); 
  1086. mdelay(1); 
  1087. kbd_write_output(led);
  1088. last_jiffie = jiffies;
  1089. }
  1090. }  
  1091. static int __init panicblink_setup(char *str)
  1092. {
  1093.     int par;
  1094.     if (get_option(&str,&par)) 
  1095.     blink_frequency = par*(1000/HZ);
  1096.     return 1;
  1097. }
  1098. /* panicblink=0 disables the blinking as it caused problems with some console
  1099.    switches. otherwise argument is ms of a blink period. */
  1100. __setup("panicblink=", panicblink_setup);