~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/drivers/char/pc_keyb.c

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.