Import 2.3.18pre1
[davej-history.git] / drivers / sbus / char / pcikbd.c
blobdc34fe4cd24540b8d10c15c1d875df1cedb7dc30
1 /* $Id: pcikbd.c,v 1.35 1999/09/01 08:09:26 davem Exp $
2 * pcikbd.c: Ultra/AX PC keyboard support.
4 * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
5 * JavaStation support by Pete A. Zaitcev.
7 * This code is mainly put together from various places in
8 * drivers/char, please refer to these sources for credits
9 * to the original authors.
12 #include <linux/config.h>
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/sched.h>
16 #include <linux/interrupt.h>
17 #include <linux/ioport.h>
18 #include <linux/poll.h>
19 #include <linux/malloc.h>
20 #include <linux/errno.h>
21 #include <linux/random.h>
22 #include <linux/miscdevice.h>
23 #include <linux/kbd_ll.h>
24 #include <linux/delay.h>
25 #include <linux/init.h>
27 #include <asm/ebus.h>
28 #include <asm/oplib.h>
29 #include <asm/irq.h>
30 #include <asm/io.h>
31 #include <asm/uaccess.h>
34 * Different platforms provide different permutations of names.
35 * AXi - kb_ps2, kdmouse.
36 * MrCoffee - keyboard, mouse.
37 * Espresso - keyboard, kdmouse.
39 #define PCI_KB_NAME1 "kb_ps2"
40 #define PCI_KB_NAME2 "keyboard"
41 #define PCI_MS_NAME1 "kdmouse"
42 #define PCI_MS_NAME2 "mouse"
44 #include "pcikbd.h"
45 #include "sunserial.h"
47 #ifndef __sparc_v9__
48 static int pcikbd_mrcoffee = 0;
49 #else
50 #define pcikbd_mrcoffee 0
51 #endif
53 static unsigned long pcikbd_iobase = 0;
54 static unsigned int pcikbd_irq = 0;
56 /* used only by send_data - set by keyboard_interrupt */
57 static volatile unsigned char reply_expected = 0;
58 static volatile unsigned char acknowledge = 0;
59 static volatile unsigned char resend = 0;
61 unsigned char pckbd_read_mask = KBD_STAT_OBF;
63 extern int pcikbd_init(void);
64 extern void pci_compute_shiftstate(void);
65 extern int pci_setkeycode(unsigned int, unsigned int);
66 extern int pci_getkeycode(unsigned int);
67 extern void pci_setledstate(struct kbd_struct *, unsigned int);
68 extern unsigned char pci_getledstate(void);
70 #ifdef __sparc_v9__
72 static __inline__ unsigned char pcikbd_inb(unsigned long port)
74 return inb(port);
77 static __inline__ void pcikbd_outb(unsigned char val, unsigned long port)
79 outb(val, port);
82 #else
84 static __inline__ unsigned char pcikbd_inb(unsigned long port)
86 return *(volatile unsigned char *)port;
89 static __inline__ void pcikbd_outb(unsigned char val, unsigned long port)
91 *(volatile unsigned char *)port = val;
94 #endif
96 static inline void kb_wait(void)
98 unsigned long start = jiffies;
100 do {
101 if(!(pcikbd_inb(pcikbd_iobase + KBD_STATUS_REG) & KBD_STAT_IBF))
102 return;
103 } while (jiffies - start < KBC_TIMEOUT);
107 * Translation of escaped scancodes to keycodes.
108 * This is now user-settable.
109 * The keycodes 1-88,96-111,119 are fairly standard, and
110 * should probably not be changed - changing might confuse X.
111 * X also interprets scancode 0x5d (KEY_Begin).
113 * For 1-88 keycode equals scancode.
116 #define E0_KPENTER 96
117 #define E0_RCTRL 97
118 #define E0_KPSLASH 98
119 #define E0_PRSCR 99
120 #define E0_RALT 100
121 #define E0_BREAK 101 /* (control-pause) */
122 #define E0_HOME 102
123 #define E0_UP 103
124 #define E0_PGUP 104
125 #define E0_LEFT 105
126 #define E0_RIGHT 106
127 #define E0_END 107
128 #define E0_DOWN 108
129 #define E0_PGDN 109
130 #define E0_INS 110
131 #define E0_DEL 111
133 #define E1_PAUSE 119
136 * The keycodes below are randomly located in 89-95,112-118,120-127.
137 * They could be thrown away (and all occurrences below replaced by 0),
138 * but that would force many users to use the `setkeycodes' utility, where
139 * they needed not before. It does not matter that there are duplicates, as
140 * long as no duplication occurs for any single keyboard.
142 #define SC_LIM 89
144 #define FOCUS_PF1 85 /* actual code! */
145 #define FOCUS_PF2 89
146 #define FOCUS_PF3 90
147 #define FOCUS_PF4 91
148 #define FOCUS_PF5 92
149 #define FOCUS_PF6 93
150 #define FOCUS_PF7 94
151 #define FOCUS_PF8 95
152 #define FOCUS_PF9 120
153 #define FOCUS_PF10 121
154 #define FOCUS_PF11 122
155 #define FOCUS_PF12 123
157 #define JAP_86 124
158 /* tfj@olivia.ping.dk:
159 * The four keys are located over the numeric keypad, and are
160 * labelled A1-A4. It's an rc930 keyboard, from
161 * Regnecentralen/RC International, Now ICL.
162 * Scancodes: 59, 5a, 5b, 5c.
164 #define RGN1 124
165 #define RGN2 125
166 #define RGN3 126
167 #define RGN4 127
169 static unsigned char high_keys[128 - SC_LIM] = {
170 RGN1, RGN2, RGN3, RGN4, 0, 0, 0, /* 0x59-0x5f */
171 0, 0, 0, 0, 0, 0, 0, 0, /* 0x60-0x67 */
172 0, 0, 0, 0, 0, FOCUS_PF11, 0, FOCUS_PF12, /* 0x68-0x6f */
173 0, 0, 0, FOCUS_PF2, FOCUS_PF9, 0, 0, FOCUS_PF3, /* 0x70-0x77 */
174 FOCUS_PF4, FOCUS_PF5, FOCUS_PF6, FOCUS_PF7, /* 0x78-0x7b */
175 FOCUS_PF8, JAP_86, FOCUS_PF10, 0 /* 0x7c-0x7f */
178 /* BTC */
179 #define E0_MACRO 112
180 /* LK450 */
181 #define E0_F13 113
182 #define E0_F14 114
183 #define E0_HELP 115
184 #define E0_DO 116
185 #define E0_F17 117
186 #define E0_KPMINPLUS 118
188 * My OmniKey generates e0 4c for the "OMNI" key and the
189 * right alt key does nada. [kkoller@nyx10.cs.du.edu]
191 #define E0_OK 124
193 * New microsoft keyboard is rumoured to have
194 * e0 5b (left window button), e0 5c (right window button),
195 * e0 5d (menu button). [or: LBANNER, RBANNER, RMENU]
196 * [or: Windows_L, Windows_R, TaskMan]
198 #define E0_MSLW 125
199 #define E0_MSRW 126
200 #define E0_MSTM 127
202 static unsigned char e0_keys[128] = {
203 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00-0x07 */
204 0, 0, 0, 0, 0, 0, 0, 0, /* 0x08-0x0f */
205 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10-0x17 */
206 0, 0, 0, 0, E0_KPENTER, E0_RCTRL, 0, 0, /* 0x18-0x1f */
207 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20-0x27 */
208 0, 0, 0, 0, 0, 0, 0, 0, /* 0x28-0x2f */
209 0, 0, 0, 0, 0, E0_KPSLASH, 0, E0_PRSCR, /* 0x30-0x37 */
210 E0_RALT, 0, 0, 0, 0, E0_F13, E0_F14, E0_HELP, /* 0x38-0x3f */
211 E0_DO, E0_F17, 0, 0, 0, 0, E0_BREAK, E0_HOME, /* 0x40-0x47 */
212 E0_UP, E0_PGUP, 0, E0_LEFT, E0_OK, E0_RIGHT, E0_KPMINPLUS, E0_END,/* 0x48-0x4f */
213 E0_DOWN, E0_PGDN, E0_INS, E0_DEL, 0, 0, 0, 0, /* 0x50-0x57 */
214 0, 0, 0, E0_MSLW, E0_MSRW, E0_MSTM, 0, 0, /* 0x58-0x5f */
215 0, 0, 0, 0, 0, 0, 0, 0, /* 0x60-0x67 */
216 0, 0, 0, 0, 0, 0, 0, E0_MACRO, /* 0x68-0x6f */
217 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70-0x77 */
218 0, 0, 0, 0, 0, 0, 0, 0 /* 0x78-0x7f */
221 /* Simple translation table for the SysRq keys */
223 #ifdef CONFIG_MAGIC_SYSRQ
224 unsigned char pcikbd_sysrq_xlate[128] =
225 "\000\0331234567890-=\177\t" /* 0x00 - 0x0f */
226 "qwertyuiop[]\r\000as" /* 0x10 - 0x1f */
227 "dfghjkl;'`\000\\zxcv" /* 0x20 - 0x2f */
228 "bnm,./\000*\000 \000\201\202\203\204\205" /* 0x30 - 0x3f */
229 "\206\207\210\211\212\000\000789-456+1" /* 0x40 - 0x4f */
230 "230\177\000\000\213\214\000\000\000\000\000\000\000\000\000\000" /* 0x50 - 0x5f */
231 "\r\000/"; /* 0x60 - 0x6f */
232 #endif
234 int pcikbd_setkeycode(unsigned int scancode, unsigned int keycode)
236 if(scancode < SC_LIM || scancode > 255 || keycode > 127)
237 return -EINVAL;
238 if(scancode < 128)
239 high_keys[scancode - SC_LIM] = keycode;
240 else
241 e0_keys[scancode - 128] = keycode;
242 return 0;
245 int pcikbd_getkeycode(unsigned int scancode)
247 return
248 (scancode < SC_LIM || scancode > 255) ? -EINVAL :
249 (scancode < 128) ? high_keys[scancode - SC_LIM] :
250 e0_keys[scancode - 128];
253 int do_acknowledge(unsigned char scancode)
255 if(reply_expected) {
256 if(scancode == KBD_REPLY_ACK) {
257 acknowledge = 1;
258 reply_expected = 0;
259 return 0;
260 } else if(scancode == KBD_REPLY_RESEND) {
261 resend = 1;
262 reply_expected = 0;
263 return 0;
266 return 1;
269 int pcikbd_translate(unsigned char scancode, unsigned char *keycode,
270 char raw_mode)
272 static int prev_scancode = 0;
274 if (scancode == 0xe0 || scancode == 0xe1) {
275 prev_scancode = scancode;
276 return 0;
278 if (scancode == 0x00 || scancode == 0xff) {
279 prev_scancode = 0;
280 return 0;
282 scancode &= 0x7f;
283 if(prev_scancode) {
284 if(prev_scancode != 0xe0) {
285 if(prev_scancode == 0xe1 && scancode == 0x1d) {
286 prev_scancode = 0x100;
287 return 0;
288 } else if(prev_scancode == 0x100 && scancode == 0x45) {
289 *keycode = E1_PAUSE;
290 prev_scancode = 0;
291 } else {
292 prev_scancode = 0;
293 return 0;
295 } else {
296 prev_scancode = 0;
297 if(scancode == 0x2a || scancode == 0x36)
298 return 0;
299 if(e0_keys[scancode])
300 *keycode = e0_keys[scancode];
301 else
302 return 0;
304 } else if(scancode >= SC_LIM) {
305 *keycode = high_keys[scancode - SC_LIM];
306 if(!*keycode)
307 return 0;
309 } else
310 *keycode = scancode;
311 return 1;
314 char pcikbd_unexpected_up(unsigned char keycode)
316 if(keycode >= SC_LIM || keycode == 85)
317 return 0;
318 else
319 return 0200;
322 static void
323 pcikbd_interrupt(int irq, void *dev_id, struct pt_regs *regs)
325 unsigned char status;
327 kbd_pt_regs = regs;
328 status = pcikbd_inb(pcikbd_iobase + KBD_STATUS_REG);
329 do {
330 unsigned char scancode;
332 if(status & pckbd_read_mask & KBD_STAT_MOUSE_OBF)
333 break;
334 scancode = pcikbd_inb(pcikbd_iobase + KBD_DATA_REG);
335 if((status & KBD_STAT_OBF) && do_acknowledge(scancode))
336 handle_scancode(scancode, !(scancode & 0x80));
337 status = pcikbd_inb(pcikbd_iobase + KBD_STATUS_REG);
338 } while(status & KBD_STAT_OBF);
339 mark_bh(KEYBOARD_BH);
342 static int send_data(unsigned char data)
344 int retries = 3;
345 unsigned long start;
347 do {
348 kb_wait();
349 acknowledge = resend = 0;
350 reply_expected = 1;
351 pcikbd_outb(data, pcikbd_iobase + KBD_DATA_REG);
352 start = jiffies;
353 do {
354 if(acknowledge)
355 return 1;
356 if(jiffies - start >= KBD_TIMEOUT)
357 return 0;
358 } while(!resend);
359 } while(retries-- > 0);
360 return 0;
363 void pcikbd_leds(unsigned char leds)
365 if(!send_data(KBD_CMD_SET_LEDS) || !send_data(leds))
366 send_data(KBD_CMD_ENABLE);
370 static int __init pcikbd_wait_for_input(void)
372 int status, data;
373 unsigned long start = jiffies;
375 do {
376 status = pcikbd_inb(pcikbd_iobase + KBD_STATUS_REG);
377 if(!(status & KBD_STAT_OBF))
378 continue;
379 data = pcikbd_inb(pcikbd_iobase + KBD_DATA_REG);
380 if(status & (KBD_STAT_GTO | KBD_STAT_PERR))
381 continue;
382 return (data & 0xff);
383 } while(jiffies - start < KBD_INIT_TIMEOUT);
384 return -1;
387 static void __init pcikbd_write(int address, int data)
389 int status;
391 do {
392 status = pcikbd_inb(pcikbd_iobase + KBD_STATUS_REG);
393 } while (status & KBD_STAT_IBF);
394 pcikbd_outb(data, pcikbd_iobase + address);
397 #ifdef __sparc_v9__
399 static unsigned long pcibeep_iobase = 0;
401 /* Timer routine to turn off the beep after the interval expires. */
402 static void pcikbd_kd_nosound(unsigned long __unused)
404 outl(0, pcibeep_iobase);
408 * Initiate a keyboard beep. If the frequency is zero, then we stop
409 * the beep. Any other frequency will start a monotone beep. The beep
410 * will be stopped by a timer after "ticks" jiffies. If ticks is 0,
411 * then we do not start a timer.
413 static void pcikbd_kd_mksound(unsigned int hz, unsigned int ticks)
415 unsigned long flags;
416 static struct timer_list sound_timer = { NULL, NULL, 0, 0,
417 pcikbd_kd_nosound };
419 save_flags(flags); cli();
420 del_timer(&sound_timer);
421 if (hz) {
422 outl(1, pcibeep_iobase);
423 if (ticks) {
424 sound_timer.expires = jiffies + ticks;
425 add_timer(&sound_timer);
427 } else
428 outl(0, pcibeep_iobase);
429 restore_flags(flags);
431 #endif
433 static void nop_kd_mksound(unsigned int hz, unsigned int ticks)
437 extern void (*kd_mksound)(unsigned int hz, unsigned int ticks);
439 static char * __init do_pcikbd_init_hw(void)
442 while(pcikbd_wait_for_input() != -1)
445 pcikbd_write(KBD_CNTL_REG, KBD_CCMD_SELF_TEST);
446 if(pcikbd_wait_for_input() != 0x55)
447 return "Keyboard failed self test";
449 pcikbd_write(KBD_CNTL_REG, KBD_CCMD_KBD_TEST);
450 if(pcikbd_wait_for_input() != 0x00)
451 return "Keyboard interface failed self test";
453 pcikbd_write(KBD_CNTL_REG, KBD_CCMD_KBD_ENABLE);
454 pcikbd_write(KBD_DATA_REG, KBD_CMD_RESET);
455 if(pcikbd_wait_for_input() != KBD_REPLY_ACK)
456 return "Keyboard reset failed, no ACK";
457 if(pcikbd_wait_for_input() != KBD_REPLY_POR)
458 return "Keyboard reset failed, no ACK";
460 pcikbd_write(KBD_DATA_REG, KBD_CMD_DISABLE);
461 if(pcikbd_wait_for_input() != KBD_REPLY_ACK)
462 return "Disable keyboard: no ACK";
464 pcikbd_write(KBD_CNTL_REG, KBD_CCMD_WRITE_MODE);
465 pcikbd_write(KBD_DATA_REG,
466 (KBD_MODE_KBD_INT | KBD_MODE_SYS |
467 KBD_MODE_DISABLE_MOUSE | KBD_MODE_KCC));
468 pcikbd_write(KBD_DATA_REG, KBD_CMD_ENABLE);
469 if(pcikbd_wait_for_input() != KBD_REPLY_ACK)
470 return "Enable keyboard: no ACK";
472 pcikbd_write(KBD_DATA_REG, KBD_CMD_SET_RATE);
473 if(pcikbd_wait_for_input() != KBD_REPLY_ACK)
474 return "Set rate: no ACK";
475 pcikbd_write(KBD_DATA_REG, 0x00);
476 if(pcikbd_wait_for_input() != KBD_REPLY_ACK)
477 return "Set rate: no ACK";
479 return NULL; /* success */
482 void __init pcikbd_init_hw(void)
484 struct linux_ebus *ebus;
485 struct linux_ebus_device *edev;
486 struct linux_ebus_child *child;
487 char *msg;
489 if (pcikbd_mrcoffee) {
490 if ((pcikbd_iobase = (unsigned long) sparc_alloc_io(0x71300060,
491 0, 8, "ps2kbd-regs", 0x0, 0)) == 0) {
492 prom_printf("pcikbd_init_hw: cannot map\n");
493 return;
495 pcikbd_irq = 13 | 0x20;
496 if (request_irq(pcikbd_irq, &pcikbd_interrupt,
497 SA_SHIRQ, "keyboard", (void *)pcikbd_iobase)) {
498 printk("8042: cannot register IRQ %x\n", pcikbd_irq);
499 return;
501 printk("8042(kbd): iobase[%08x] irq[%x]\n",
502 (unsigned)pcikbd_iobase, pcikbd_irq);
503 } else {
504 for_each_ebus(ebus) {
505 for_each_ebusdev(edev, ebus) {
506 if(!strcmp(edev->prom_name, "8042")) {
507 for_each_edevchild(edev, child) {
508 if (strcmp(child->prom_name, PCI_KB_NAME1) == 0 ||
509 strcmp(child->prom_name, PCI_KB_NAME2) == 0)
510 goto found;
515 printk("pcikbd_init_hw: no 8042 found\n");
516 return;
518 found:
519 pcikbd_iobase = child->resource[0].start;
520 pcikbd_irq = child->irqs[0];
521 if (request_irq(pcikbd_irq, &pcikbd_interrupt,
522 SA_SHIRQ, "keyboard", (void *)pcikbd_iobase)) {
523 printk("8042: cannot register IRQ %s\n",
524 __irq_itoa(pcikbd_irq));
525 return;
528 printk("8042(kbd) at 0x%lx (irq %s)\n", pcikbd_iobase,
529 __irq_itoa(pcikbd_irq));
532 kd_mksound = nop_kd_mksound;
534 #ifdef __sparc_v9__
535 edev = 0;
536 for_each_ebus(ebus) {
537 for_each_ebusdev(edev, ebus) {
538 if(!strcmp(edev->prom_name, "beeper"))
539 goto ebus_done;
542 ebus_done:
545 * XXX: my 3.1.3 PROM does not give me the beeper node for the audio
546 * auxio register, though I know it is there... (ecd)
548 * JavaStations appear not to have beeper. --zaitcev
550 if (!edev)
551 pcibeep_iobase = (pcikbd_iobase & ~(0xffffff)) | 0x722000;
552 else
553 pcibeep_iobase = edev->resource[0].start;
555 kd_mksound = pcikbd_kd_mksound;
556 printk("8042(speaker): iobase[%016lx]%s\n", pcibeep_iobase,
557 edev ? "" : " (forced)");
558 #endif
560 disable_irq(pcikbd_irq);
561 msg = do_pcikbd_init_hw();
562 enable_irq(pcikbd_irq);
564 if(msg)
565 printk("8042: keyboard init failure [%s]\n", msg);
570 * Here begins the Mouse Driver.
573 static unsigned long pcimouse_iobase = 0;
574 static unsigned int pcimouse_irq;
576 #define AUX_BUF_SIZE 2048
578 struct aux_queue {
579 unsigned long head;
580 unsigned long tail;
581 wait_queue_head_t proc_list;
582 struct fasync_struct *fasync;
583 unsigned char buf[AUX_BUF_SIZE];
586 static struct aux_queue *queue;
587 static int aux_ready = 0;
588 static int aux_count = 0;
589 static int aux_present = 0;
591 #ifdef __sparc_v9__
593 static __inline__ unsigned char pcimouse_inb(unsigned long port)
595 return inb(port);
598 static __inline__ void pcimouse_outb(unsigned char val, unsigned long port)
600 outb(val, port);
603 #else
605 static __inline__ unsigned char pcimouse_inb(unsigned long port)
607 return *(volatile unsigned char *)port;
610 static __inline__ void pcimouse_outb(unsigned char val, unsigned long port)
612 *(volatile unsigned char *)port = val;
615 #endif
618 * Shared subroutines
621 static unsigned int get_from_queue(void)
623 unsigned int result;
624 unsigned long flags;
626 save_flags(flags);
627 cli();
628 result = queue->buf[queue->tail];
629 queue->tail = (queue->tail + 1) & (AUX_BUF_SIZE-1);
630 restore_flags(flags);
631 return result;
635 static inline int queue_empty(void)
637 return queue->head == queue->tail;
640 static int aux_fasync(int fd, struct file *filp, int on)
642 int retval;
644 retval = fasync_helper(fd, filp, on, &queue->fasync);
645 if (retval < 0)
646 return retval;
647 return 0;
651 * PS/2 Aux Device
654 #define AUX_INTS_OFF (KBD_MODE_KCC | KBD_MODE_DISABLE_MOUSE | \
655 KBD_MODE_SYS | KBD_MODE_KBD_INT)
657 #define AUX_INTS_ON (KBD_MODE_KCC | KBD_MODE_SYS | \
658 KBD_MODE_MOUSE_INT | KBD_MODE_KBD_INT)
660 #define MAX_RETRIES 60 /* some aux operations take long time*/
663 * Status polling
666 static int poll_aux_status(void)
668 int retries=0;
670 while ((pcimouse_inb(pcimouse_iobase + KBD_STATUS_REG) &
671 (KBD_STAT_IBF | KBD_STAT_OBF)) && retries < MAX_RETRIES) {
672 if ((pcimouse_inb(pcimouse_iobase + KBD_STATUS_REG) & AUX_STAT_OBF)
673 == AUX_STAT_OBF)
674 pcimouse_inb(pcimouse_iobase + KBD_DATA_REG);
675 current->state = TASK_INTERRUPTIBLE;
676 schedule_timeout((5*HZ + 99) / 100);
677 retries++;
679 return (retries < MAX_RETRIES);
683 * Write to aux device
686 static void aux_write_dev(int val)
688 poll_aux_status();
689 pcimouse_outb(KBD_CCMD_WRITE_MOUSE, pcimouse_iobase + KBD_CNTL_REG);/* Write magic cookie */
690 poll_aux_status();
691 pcimouse_outb(val, pcimouse_iobase + KBD_DATA_REG); /* Write data */
692 udelay(1);
696 * Write to device & handle returned ack
699 static int __init aux_write_ack(int val)
701 aux_write_dev(val);
702 poll_aux_status();
704 if ((pcimouse_inb(pcimouse_iobase + KBD_STATUS_REG) & AUX_STAT_OBF) == AUX_STAT_OBF)
705 return (pcimouse_inb(pcimouse_iobase + KBD_DATA_REG));
706 return 0;
710 * Write aux device command
713 static void aux_write_cmd(int val)
715 poll_aux_status();
716 pcimouse_outb(KBD_CCMD_WRITE_MODE, pcimouse_iobase + KBD_CNTL_REG);
717 poll_aux_status();
718 pcimouse_outb(val, pcimouse_iobase + KBD_DATA_REG);
722 * AUX handler critical section start and end.
724 * Only one process can be in the critical section and all keyboard sends are
725 * deferred as long as we're inside. This is necessary as we may sleep when
726 * waiting for the keyboard controller and other processes / BH's can
727 * preempt us. Please note that the input buffer must be flushed when
728 * aux_end_atomic() is called and the interrupt is no longer enabled as not
729 * doing so might cause the keyboard driver to ignore all incoming keystrokes.
732 static DECLARE_MUTEX(aux_sema4);
734 static inline void aux_start_atomic(void)
736 down(&aux_sema4);
737 disable_bh(KEYBOARD_BH);
740 static inline void aux_end_atomic(void)
742 enable_bh(KEYBOARD_BH);
743 up(&aux_sema4);
747 * Interrupt from the auxiliary device: a character
748 * is waiting in the keyboard/aux controller.
751 void pcimouse_interrupt(int irq, void *dev_id, struct pt_regs *regs)
753 int head = queue->head;
754 int maxhead = (queue->tail-1) & (AUX_BUF_SIZE-1);
756 if ((pcimouse_inb(pcimouse_iobase + KBD_STATUS_REG) & AUX_STAT_OBF) != AUX_STAT_OBF)
757 return;
759 add_mouse_randomness(queue->buf[head] = pcimouse_inb(pcimouse_iobase + KBD_DATA_REG));
760 if (head != maxhead) {
761 head++;
762 head &= AUX_BUF_SIZE-1;
764 queue->head = head;
765 aux_ready = 1;
766 if (queue->fasync)
767 kill_fasync(queue->fasync, SIGIO);
768 wake_up_interruptible(&queue->proc_list);
771 static int aux_release(struct inode * inode, struct file * file)
773 aux_fasync(-1, file, 0);
774 if (--aux_count)
775 return 0;
776 aux_start_atomic();
778 /* Disable controller ints */
779 aux_write_cmd(AUX_INTS_OFF);
780 poll_aux_status();
782 /* Disable Aux device */
783 pcimouse_outb(KBD_CCMD_MOUSE_DISABLE, pcimouse_iobase + KBD_CNTL_REG);
784 poll_aux_status();
785 aux_end_atomic();
787 MOD_DEC_USE_COUNT;
788 return 0;
792 * Install interrupt handler.
793 * Enable auxiliary device.
796 static int aux_open(struct inode * inode, struct file * file)
798 if (!aux_present)
799 return -ENODEV;
801 aux_start_atomic();
802 if (aux_count++) {
803 aux_end_atomic();
804 return 0;
806 if (!poll_aux_status()) { /* FIXME: Race condition */
807 aux_count--;
808 aux_end_atomic();
809 return -EBUSY;
811 queue->head = queue->tail = 0; /* Flush input queue */
813 MOD_INC_USE_COUNT;
815 poll_aux_status();
816 pcimouse_outb(KBD_CCMD_MOUSE_ENABLE, pcimouse_iobase+KBD_CNTL_REG); /* Enable Aux */
817 aux_write_dev(AUX_ENABLE_DEV); /* Enable aux device */
818 aux_write_cmd(AUX_INTS_ON); /* Enable controller ints */
819 poll_aux_status();
820 aux_end_atomic();
822 aux_ready = 0;
823 return 0;
827 * Write to the aux device.
830 static ssize_t aux_write(struct file * file, const char * buffer,
831 size_t count, loff_t *ppos)
833 ssize_t retval = 0;
835 if (count) {
836 ssize_t written = 0;
838 aux_start_atomic();
839 do {
840 char c;
841 if (!poll_aux_status())
842 break;
843 pcimouse_outb(KBD_CCMD_WRITE_MOUSE, pcimouse_iobase + KBD_CNTL_REG);
844 if (!poll_aux_status())
845 break;
846 get_user(c, buffer++);
847 pcimouse_outb(c, pcimouse_iobase + KBD_DATA_REG);
848 written++;
849 } while (--count);
850 aux_end_atomic();
851 retval = -EIO;
852 if (written) {
853 retval = written;
854 file->f_dentry->d_inode->i_mtime = CURRENT_TIME;
858 return retval;
862 * Generic part continues...
866 * Put bytes from input queue to buffer.
869 static ssize_t aux_read(struct file * file, char * buffer,
870 size_t count, loff_t *ppos)
872 DECLARE_WAITQUEUE(wait, current);
873 ssize_t i = count;
874 unsigned char c;
876 if (queue_empty()) {
877 if (file->f_flags & O_NONBLOCK)
878 return -EAGAIN;
879 add_wait_queue(&queue->proc_list, &wait);
880 repeat:
881 set_current_state(TASK_INTERRUPTIBLE);
882 if (queue_empty() && !signal_pending(current)) {
883 schedule();
884 goto repeat;
886 current->state = TASK_RUNNING;
887 remove_wait_queue(&queue->proc_list, &wait);
889 while (i > 0 && !queue_empty()) {
890 c = get_from_queue();
891 put_user(c, buffer++);
892 i--;
894 aux_ready = !queue_empty();
895 if (count-i) {
896 file->f_dentry->d_inode->i_atime = CURRENT_TIME;
897 return count-i;
899 if (signal_pending(current))
900 return -ERESTARTSYS;
901 return 0;
904 static unsigned int aux_poll(struct file *file, poll_table * wait)
906 poll_wait(file, &queue->proc_list, wait);
907 if (aux_ready)
908 return POLLIN | POLLRDNORM;
909 return 0;
912 struct file_operations psaux_fops = {
913 NULL, /* seek */
914 aux_read,
915 aux_write,
916 NULL, /* readdir */
917 aux_poll,
918 NULL, /* ioctl */
919 NULL, /* mmap */
920 aux_open,
921 NULL, /* flush */
922 aux_release,
923 NULL,
924 aux_fasync,
927 static struct miscdevice psaux_mouse = {
928 PSMOUSE_MINOR, "ps2aux", &psaux_fops
931 int __init pcimouse_init(void)
933 struct linux_ebus *ebus;
934 struct linux_ebus_device *edev;
935 struct linux_ebus_child *child;
937 if (pcikbd_mrcoffee) {
938 if ((pcimouse_iobase = pcikbd_iobase) == 0) {
939 printk("pcimouse_init: no 8042 given\n");
940 return -ENODEV;
942 pcimouse_irq = pcikbd_irq;
943 } else {
944 for_each_ebus(ebus) {
945 for_each_ebusdev(edev, ebus) {
946 if(!strcmp(edev->prom_name, "8042")) {
947 for_each_edevchild(edev, child) {
948 if (strcmp(child->prom_name, PCI_MS_NAME1) == 0 ||
949 strcmp(child->prom_name, PCI_MS_NAME2) == 0)
950 goto found;
955 printk("pcimouse_init: no 8042 found\n");
956 return -ENODEV;
958 found:
959 pcimouse_iobase = child->resource[0].start;
960 pcimouse_irq = child->irqs[0];
963 queue = (struct aux_queue *) kmalloc(sizeof(*queue), GFP_KERNEL);
964 if (!queue) {
965 printk("pcimouse_init: kmalloc(aux_queue) failed.\n");
966 return -ENOMEM;
968 memset(queue, 0, sizeof(*queue));
970 init_waitqueue_head(&queue->proc_list);
972 if (request_irq(pcimouse_irq, &pcimouse_interrupt,
973 SA_SHIRQ, "mouse", (void *)pcimouse_iobase)) {
974 printk("8042: Cannot register IRQ %s\n",
975 __irq_itoa(pcimouse_irq));
976 return -ENODEV;
979 printk("8042(mouse) at %lx (irq %s)\n", pcimouse_iobase,
980 __irq_itoa(pcimouse_irq));
982 printk("8042: PS/2 auxiliary pointing device detected.\n");
983 aux_present = 1;
984 pckbd_read_mask = AUX_STAT_OBF;
986 misc_register(&psaux_mouse);
987 aux_start_atomic();
988 pcimouse_outb(KBD_CCMD_MOUSE_ENABLE, pcimouse_iobase + KBD_CNTL_REG);
989 aux_write_ack(AUX_RESET);
990 aux_write_ack(AUX_SET_SAMPLE);
991 aux_write_ack(100);
992 aux_write_ack(AUX_SET_RES);
993 aux_write_ack(3);
994 aux_write_ack(AUX_SET_SCALE21);
995 poll_aux_status();
996 pcimouse_outb(KBD_CCMD_MOUSE_DISABLE, pcimouse_iobase + KBD_CNTL_REG);
997 poll_aux_status();
998 pcimouse_outb(KBD_CCMD_WRITE_MODE, pcimouse_iobase + KBD_CNTL_REG);
999 poll_aux_status();
1000 pcimouse_outb(AUX_INTS_OFF, pcimouse_iobase + KBD_DATA_REG);
1001 poll_aux_status();
1002 aux_end_atomic();
1004 return 0;
1008 int __init ps2kbd_probe(unsigned long *memory_start)
1010 int pnode, enode, node, dnode, xnode;
1011 int kbnode = 0, msnode = 0, bnode = 0;
1012 int devices = 0;
1013 char prop[128];
1014 int len;
1016 #ifndef __sparc_v9__
1018 * MrCoffee has hardware but has no PROM nodes whatsoever.
1020 len = prom_getproperty(prom_root_node, "name", prop, sizeof(prop));
1021 if (len < 0) {
1022 printk("ps2kbd_probe: no name of root node\n");
1023 return -ENODEV;
1025 if (strncmp(prop, "SUNW,JavaStation-1", len) == 0) {
1026 pcikbd_mrcoffee = 1; /* Brain damage detected */
1027 goto found;
1029 #endif
1031 * Get the nodes for keyboard and mouse from aliases on normal systems.
1033 node = prom_getchild(prom_root_node);
1034 node = prom_searchsiblings(node, "aliases");
1035 if (!node)
1036 return -ENODEV;
1038 len = prom_getproperty(node, "keyboard", prop, sizeof(prop));
1039 if (len > 0) {
1040 prop[len] = 0;
1041 kbnode = prom_finddevice(prop);
1043 if (!kbnode)
1044 return -ENODEV;
1046 len = prom_getproperty(node, "mouse", prop, sizeof(prop));
1047 if (len > 0) {
1048 prop[len] = 0;
1049 msnode = prom_finddevice(prop);
1051 if (!msnode)
1052 return -ENODEV;
1055 * Find matching EBus nodes...
1057 node = prom_getchild(prom_root_node);
1058 pnode = prom_searchsiblings(node, "pci");
1061 * Check for SUNW,sabre on Ultra5/10/AXi.
1063 len = prom_getproperty(pnode, "model", prop, sizeof(prop));
1064 if ((len > 0) && !strncmp(prop, "SUNW,sabre", len)) {
1065 pnode = prom_getchild(pnode);
1066 pnode = prom_searchsiblings(pnode, "pci");
1070 * For each PCI bus...
1072 while (pnode) {
1073 enode = prom_getchild(pnode);
1074 enode = prom_searchsiblings(enode, "ebus");
1077 * For each EBus on this PCI...
1079 while (enode) {
1080 node = prom_getchild(enode);
1081 bnode = prom_searchsiblings(node, "beeper");
1083 node = prom_getchild(enode);
1084 node = prom_searchsiblings(node, "8042");
1087 * For each '8042' on this EBus...
1089 while (node) {
1090 dnode = prom_getchild(node);
1093 * Does it match?
1095 if ((xnode = prom_searchsiblings(dnode, PCI_KB_NAME1)) == kbnode) {
1096 ++devices;
1097 } else if ((xnode = prom_searchsiblings(dnode, PCI_KB_NAME2)) == kbnode) {
1098 ++devices;
1101 if ((xnode = prom_searchsiblings(dnode, PCI_MS_NAME1)) == msnode) {
1102 ++devices;
1103 } else if ((xnode = prom_searchsiblings(dnode, PCI_MS_NAME2)) == msnode) {
1104 ++devices;
1108 * Found everything we need?
1110 if (devices == 2)
1111 goto found;
1113 node = prom_getsibling(node);
1114 node = prom_searchsiblings(node, "8042");
1116 enode = prom_getsibling(enode);
1117 enode = prom_searchsiblings(enode, "ebus");
1119 pnode = prom_getsibling(pnode);
1120 pnode = prom_searchsiblings(pnode, "pci");
1122 return -ENODEV;
1124 found:
1125 sunkbd_setinitfunc(memory_start, pcimouse_init);
1126 sunkbd_setinitfunc(memory_start, pcikbd_init);
1127 kbd_ops.compute_shiftstate = pci_compute_shiftstate;
1128 kbd_ops.setledstate = pci_setledstate;
1129 kbd_ops.getledstate = pci_getledstate;
1130 kbd_ops.setkeycode = pci_setkeycode;
1131 kbd_ops.getkeycode = pci_getkeycode;
1132 return 0;