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