1 /* $Id: pcikbd.c,v 1.61 2001/08/18 09:40:46 davem Exp $
2  * pcikbd.c: Ultra/AX PC keyboard support.
3  *
4  * Copyright (C) 1997  Eddie C. Dost  (ecd@skynet.be)
5  * JavaStation support by Pete A. Zaitcev.
6  *
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.
10  */
11 
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/slab.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/kbd_kern.h>
25 #include <linux/vt_kern.h>
26 #include <linux/delay.h>
27 #include <linux/spinlock.h>
28 #include <linux/smp_lock.h>
29 #include <linux/init.h>
30 
31 #include <asm/ebus.h>
32 #if (defined(CONFIG_USB) || defined(CONFIG_USB_MODULE)) && defined(CONFIG_SPARC64)
33 #include <asm/isa.h>
34 #endif
35 #include <asm/oplib.h>
36 #include <asm/irq.h>
37 #include <asm/io.h>
38 #include <asm/uaccess.h>
39 
40 /*
41  * Different platforms provide different permutations of names.
42  * AXi - kb_ps2, kdmouse.
43  * MrCoffee - keyboard, mouse.
44  * Espresso - keyboard, kdmouse.
45  */
46 #define	PCI_KB_NAME1	"kb_ps2"
47 #define PCI_KB_NAME2	"keyboard"
48 #define PCI_MS_NAME1	"kdmouse"
49 #define PCI_MS_NAME2	"mouse"
50 
51 #include "pcikbd.h"
52 #include "sunserial.h"
53 
54 #ifndef __sparc_v9__
55 static int pcikbd_mrcoffee = 0;
56 #else
57 #define pcikbd_mrcoffee 0
58 extern void (*prom_keyboard)(void);
59 #endif
60 
61 static unsigned long pcikbd_iobase = 0;
62 static unsigned int pcikbd_irq = 0;
63 
64 /* used only by send_data - set by keyboard_interrupt */
65 static volatile unsigned char reply_expected = 0;
66 static volatile unsigned char acknowledge = 0;
67 static volatile unsigned char resend = 0;
68 
69 static spinlock_t pcikbd_lock = SPIN_LOCK_UNLOCKED;
70 
71 static void pcikbd_write(int address, int data);
72 static int pcikbd_wait_for_input(void);
73 
74 unsigned char pckbd_read_mask = KBD_STAT_OBF;
75 
76 extern int pcikbd_init(void);
77 extern void pci_compute_shiftstate(void);
78 extern int pci_setkeycode(unsigned int, unsigned int);
79 extern int pci_getkeycode(unsigned int);
80 extern void pci_setledstate(struct kbd_struct *, unsigned int);
81 extern unsigned char pci_getledstate(void);
82 
83 #define pcikbd_inb(x)     inb(x)
84 #define pcikbd_outb(v,x)  outb(v,x)
85 
86 /* Wait for keyboard controller input buffer to drain.
87  * Must be invoked under the pcikbd_lock.
88  */
kb_wait(void)89 static void kb_wait(void)
90 {
91 	unsigned long timeout = 250;
92 
93 	do {
94 		if(!(pcikbd_inb(pcikbd_iobase + KBD_STATUS_REG) & KBD_STAT_IBF))
95 			return;
96 		mdelay(1);
97 	} while (--timeout);
98 }
99 
100 /*
101  * Translation of escaped scancodes to keycodes.
102  * This is now user-settable.
103  * The keycodes 1-88,96-111,119 are fairly standard, and
104  * should probably not be changed - changing might confuse X.
105  * X also interprets scancode 0x5d (KEY_Begin).
106  *
107  * For 1-88 keycode equals scancode.
108  */
109 
110 #define E0_KPENTER 96
111 #define E0_RCTRL   97
112 #define E0_KPSLASH 98
113 #define E0_PRSCR   99
114 #define E0_RALT    100
115 #define E0_BREAK   101  /* (control-pause) */
116 #define E0_HOME    102
117 #define E0_UP      103
118 #define E0_PGUP    104
119 #define E0_LEFT    105
120 #define E0_RIGHT   106
121 #define E0_END     107
122 #define E0_DOWN    108
123 #define E0_PGDN    109
124 #define E0_INS     110
125 #define E0_DEL     111
126 
127 #define E1_PAUSE   119
128 
129 /*
130  * The keycodes below are randomly located in 89-95,112-118,120-127.
131  * They could be thrown away (and all occurrences below replaced by 0),
132  * but that would force many users to use the `setkeycodes' utility, where
133  * they needed not before. It does not matter that there are duplicates, as
134  * long as no duplication occurs for any single keyboard.
135  */
136 #define SC_LIM 89
137 
138 #define FOCUS_PF1 85           /* actual code! */
139 #define FOCUS_PF2 89
140 #define FOCUS_PF3 90
141 #define FOCUS_PF4 91
142 #define FOCUS_PF5 92
143 #define FOCUS_PF6 93
144 #define FOCUS_PF7 94
145 #define FOCUS_PF8 95
146 #define FOCUS_PF9 120
147 #define FOCUS_PF10 121
148 #define FOCUS_PF11 122
149 #define FOCUS_PF12 123
150 
151 #define JAP_86     124
152 /* tfj@olivia.ping.dk:
153  * The four keys are located over the numeric keypad, and are
154  * labelled A1-A4. It's an rc930 keyboard, from
155  * Regnecentralen/RC International, Now ICL.
156  * Scancodes: 59, 5a, 5b, 5c.
157  */
158 #define RGN1 124
159 #define RGN2 125
160 #define RGN3 126
161 #define RGN4 127
162 
163 static unsigned char high_keys[128 - SC_LIM] = {
164   RGN1, RGN2, RGN3, RGN4, 0, 0, 0,                   /* 0x59-0x5f */
165   0, 0, 0, 0, 0, 0, 0, 0,                            /* 0x60-0x67 */
166   0, 0, 0, 0, 0, FOCUS_PF11, 0, FOCUS_PF12,          /* 0x68-0x6f */
167   0, 0, 0, FOCUS_PF2, FOCUS_PF9, 0, 0, FOCUS_PF3,    /* 0x70-0x77 */
168   FOCUS_PF4, FOCUS_PF5, FOCUS_PF6, FOCUS_PF7,        /* 0x78-0x7b */
169   FOCUS_PF8, JAP_86, FOCUS_PF10, 0                   /* 0x7c-0x7f */
170 };
171 
172 /* BTC */
173 #define E0_MACRO   112
174 /* LK450 */
175 #define E0_F13     113
176 #define E0_F14     114
177 #define E0_HELP    115
178 #define E0_DO      116
179 #define E0_F17     117
180 #define E0_KPMINPLUS 118
181 /*
182  * My OmniKey generates e0 4c for  the "OMNI" key and the
183  * right alt key does nada. [kkoller@nyx10.cs.du.edu]
184  */
185 #define E0_OK	124
186 /*
187  * New microsoft keyboard is rumoured to have
188  * e0 5b (left window button), e0 5c (right window button),
189  * e0 5d (menu button). [or: LBANNER, RBANNER, RMENU]
190  * [or: Windows_L, Windows_R, TaskMan]
191  */
192 #define E0_MSLW	125
193 #define E0_MSRW	126
194 #define E0_MSTM	127
195 
196 static unsigned char e0_keys[128] = {
197   0, 0, 0, 0, 0, 0, 0, 0,			      /* 0x00-0x07 */
198   0, 0, 0, 0, 0, 0, 0, 0,			      /* 0x08-0x0f */
199   0, 0, 0, 0, 0, 0, 0, 0,			      /* 0x10-0x17 */
200   0, 0, 0, 0, E0_KPENTER, E0_RCTRL, 0, 0,	      /* 0x18-0x1f */
201   0, 0, 0, 0, 0, 0, 0, 0,			      /* 0x20-0x27 */
202   0, 0, 0, 0, 0, 0, 0, 0,			      /* 0x28-0x2f */
203   0, 0, 0, 0, 0, E0_KPSLASH, 0, E0_PRSCR,	      /* 0x30-0x37 */
204   E0_RALT, 0, 0, 0, 0, E0_F13, E0_F14, E0_HELP,	      /* 0x38-0x3f */
205   E0_DO, E0_F17, 0, 0, 0, 0, E0_BREAK, E0_HOME,	      /* 0x40-0x47 */
206   E0_UP, E0_PGUP, 0, E0_LEFT, E0_OK, E0_RIGHT, E0_KPMINPLUS, E0_END,/* 0x48-0x4f */
207   E0_DOWN, E0_PGDN, E0_INS, E0_DEL, 0, 0, 0, 0,	      /* 0x50-0x57 */
208   0, 0, 0, E0_MSLW, E0_MSRW, E0_MSTM, 0, 0,	      /* 0x58-0x5f */
209   0, 0, 0, 0, 0, 0, 0, 0,			      /* 0x60-0x67 */
210   0, 0, 0, 0, 0, 0, 0, E0_MACRO,		      /* 0x68-0x6f */
211   0, 0, 0, 0, 0, 0, 0, 0,			      /* 0x70-0x77 */
212   0, 0, 0, 0, 0, 0, 0, 0			      /* 0x78-0x7f */
213 };
214 
215 /* Simple translation table for the SysRq keys */
216 
217 #ifdef CONFIG_MAGIC_SYSRQ
218 unsigned char pcikbd_sysrq_xlate[128] =
219 	"\000\0331234567890-=\177\t"			/* 0x00 - 0x0f */
220 	"qwertyuiop[]\r\000as"				/* 0x10 - 0x1f */
221 	"dfghjkl;'`\000\\zxcv"				/* 0x20 - 0x2f */
222 	"bnm,./\000*\000 \000\201\202\203\204\205"	/* 0x30 - 0x3f */
223 	"\206\207\210\211\212\000\000789-456+1"		/* 0x40 - 0x4f */
224 	"230\177\000\000\213\214\000\000\000\000\000\000\000\000\000\000" /* 0x50 - 0x5f */
225 	"\r\000/";					/* 0x60 - 0x6f */
226 #endif
227 
228 #define DEFAULT_KEYB_REP_DELAY	250
229 #define DEFAULT_KEYB_REP_RATE	30	/* cps */
230 
231 static struct kbd_repeat kbdrate = {
232 	DEFAULT_KEYB_REP_DELAY,
233 	DEFAULT_KEYB_REP_RATE
234 };
235 
236 static unsigned char parse_kbd_rate(struct kbd_repeat *r);
237 static int write_kbd_rate(unsigned char r);
238 
pcikbd_setkeycode(unsigned int scancode,unsigned int keycode)239 int pcikbd_setkeycode(unsigned int scancode, unsigned int keycode)
240 {
241 	if(scancode < SC_LIM || scancode > 255 || keycode > 127)
242 		return -EINVAL;
243 	if(scancode < 128)
244 		high_keys[scancode - SC_LIM] = keycode;
245 	else
246 		e0_keys[scancode - 128] = keycode;
247 	return 0;
248 }
249 
pcikbd_getkeycode(unsigned int scancode)250 int pcikbd_getkeycode(unsigned int scancode)
251 {
252 	return
253 		(scancode < SC_LIM || scancode > 255) ? -EINVAL :
254 		(scancode < 128) ? high_keys[scancode - SC_LIM] :
255 		e0_keys[scancode - 128];
256 }
257 
do_acknowledge(unsigned char scancode)258 static int do_acknowledge(unsigned char scancode)
259 {
260 	if(reply_expected) {
261 		if(scancode == KBD_REPLY_ACK) {
262 			acknowledge = 1;
263 			reply_expected = 0;
264 			return 0;
265 		} else if(scancode == KBD_REPLY_RESEND) {
266 			resend = 1;
267 			reply_expected = 0;
268 			return 0;
269 		}
270 	}
271 	return 1;
272 }
273 
274 #ifdef __sparc_v9__
pcikbd_enter_prom(void)275 static void pcikbd_enter_prom(void)
276 {
277 	pcikbd_write(KBD_DATA_REG, KBD_CMD_DISABLE);
278 	if(pcikbd_wait_for_input() != KBD_REPLY_ACK)
279 		printk("Prom Enter: Disable keyboard: no ACK\n");
280 
281 	/* Disable PC scancode translation */
282 	pcikbd_write(KBD_CNTL_REG, KBD_CCMD_WRITE_MODE);
283 	pcikbd_write(KBD_DATA_REG, KBD_MODE_SYS);
284 	pcikbd_write(KBD_DATA_REG, KBD_CMD_ENABLE);
285 	if (pcikbd_wait_for_input() != KBD_REPLY_ACK)
286 		printk("Prom Enter: Enable Keyboard: no ACK\n");
287 }
288 #endif
289 
ctrl_break(void)290 static void ctrl_break(void)
291 {
292 	extern int stop_a_enabled;
293 	unsigned long timeout;
294 	int status, data;
295 	int mode;
296 
297 	if (!stop_a_enabled)
298 		return;
299 
300 	pcikbd_write(KBD_DATA_REG, KBD_CMD_DISABLE);
301 	if(pcikbd_wait_for_input() != KBD_REPLY_ACK)
302 		printk("Prom Enter: Disable keyboard: no ACK\n");
303 
304 	/* Save current mode register settings */
305 	pcikbd_write(KBD_CNTL_REG, KBD_CCMD_READ_MODE);
306 	if ((mode = pcikbd_wait_for_input()) == -1)
307 		printk("Prom Enter: Read Mode: no ACK\n");
308 
309 	/* Disable PC scancode translation */
310 	pcikbd_write(KBD_CNTL_REG, KBD_CCMD_WRITE_MODE);
311 	pcikbd_write(KBD_DATA_REG, mode & ~(KBD_MODE_KCC));
312 	pcikbd_write(KBD_DATA_REG, KBD_CMD_ENABLE);
313 	if (pcikbd_wait_for_input() != KBD_REPLY_ACK)
314 		printk("Prom Enter: Enable Keyboard: no ACK\n");
315 
316 	/* Drop into OBP.
317 	 * Note that we must flush the user windows
318 	 * first before giving up control.
319 	 */
320         flush_user_windows();
321 	prom_cmdline();
322 
323 	/* Read prom's key up event (use short timeout) */
324 	do {
325 		timeout = 10;
326 		do {
327 			mdelay(1);
328 			status = pcikbd_inb(pcikbd_iobase + KBD_STATUS_REG);
329 			if (!(status & KBD_STAT_OBF))
330 				continue;
331 			data = pcikbd_inb(pcikbd_iobase + KBD_DATA_REG);
332 			if (status & (KBD_STAT_GTO | KBD_STAT_PERR))
333 				continue;
334 			break;
335 		} while (--timeout > 0);
336 	} while (timeout > 0);
337 
338 	/* Reenable PC scancode translation */
339 	pcikbd_write(KBD_DATA_REG, KBD_CMD_DISABLE);
340 	if(pcikbd_wait_for_input() != KBD_REPLY_ACK)
341 		printk("Prom Leave: Disable keyboard: no ACK\n");
342 
343 	pcikbd_write(KBD_CNTL_REG, KBD_CCMD_WRITE_MODE);
344 	pcikbd_write(KBD_DATA_REG, mode);
345 	pcikbd_write(KBD_DATA_REG, KBD_CMD_ENABLE);
346 	if (pcikbd_wait_for_input() != KBD_REPLY_ACK)
347 		printk("Prom Leave: Enable Keyboard: no ACK\n");
348 
349 	/* Reset keyboard rate */
350 	write_kbd_rate(parse_kbd_rate(&kbdrate));
351 }
352 
pcikbd_translate(unsigned char scancode,unsigned char * keycode,char raw_mode)353 int pcikbd_translate(unsigned char scancode, unsigned char *keycode,
354 		     char raw_mode)
355 {
356 	static int prev_scancode = 0;
357 	int down = scancode & 0x80 ? 0 : 1;
358 
359 	if (scancode == 0xe0 || scancode == 0xe1) {
360 		prev_scancode = scancode;
361 		return 0;
362 	}
363 	if (scancode == 0x00 || scancode == 0xff) {
364 		prev_scancode = 0;
365 		return 0;
366 	}
367 	scancode &= 0x7f;
368 	if(prev_scancode) {
369 		if(prev_scancode != 0xe0) {
370 			if(prev_scancode == 0xe1 && scancode == 0x1d) {
371 				prev_scancode = 0x100;
372 				return 0;
373 			} else if(prev_scancode == 0x100 && scancode == 0x45) {
374 				*keycode = E1_PAUSE;
375 				prev_scancode = 0;
376 			} else {
377 				prev_scancode = 0;
378 				return 0;
379 			}
380 		} else {
381 			prev_scancode = 0;
382 			if(scancode == 0x2a || scancode == 0x36)
383 				return 0;
384 			if(e0_keys[scancode])
385 				*keycode = e0_keys[scancode];
386 			else
387 				return 0;
388 		}
389 	} else if(scancode >= SC_LIM) {
390 		*keycode = high_keys[scancode - SC_LIM];
391 		if(!*keycode)
392 			return 0;
393 
394 	} else
395 		*keycode = scancode;
396 
397 	if (*keycode == E0_BREAK) {
398 		if (down)
399 			return 0;
400 
401 		/* Handle ctrl-break event */
402 		ctrl_break();
403 
404 		/* Send ctrl up event to the keyboard driver */
405 		*keycode = 0x1d;
406 	}
407 
408 	return 1;
409 }
410 
pcikbd_unexpected_up(unsigned char keycode)411 char pcikbd_unexpected_up(unsigned char keycode)
412 {
413 	if(keycode >= SC_LIM || keycode == 85)
414 		return 0;
415 	else
416 		return 0200;
417 }
418 
419 static void
pcikbd_interrupt(int irq,void * dev_id,struct pt_regs * regs)420 pcikbd_interrupt(int irq, void *dev_id, struct pt_regs *regs)
421 {
422 	unsigned long flags;
423 	unsigned char status;
424 
425 	spin_lock_irqsave(&pcikbd_lock, flags);
426 
427 	kbd_pt_regs = regs;
428 	status = pcikbd_inb(pcikbd_iobase + KBD_STATUS_REG);
429 	do {
430 		unsigned char scancode;
431 
432 		if(status & pckbd_read_mask & KBD_STAT_MOUSE_OBF)
433 			break;
434 		scancode = pcikbd_inb(pcikbd_iobase + KBD_DATA_REG);
435 		if((status & KBD_STAT_OBF) && do_acknowledge(scancode))
436 			handle_scancode(scancode, !(scancode & 0x80));
437 		status = pcikbd_inb(pcikbd_iobase + KBD_STATUS_REG);
438 	} while(status & KBD_STAT_OBF);
439 	tasklet_schedule(&keyboard_tasklet);
440 
441 	spin_unlock_irqrestore(&pcikbd_lock, flags);
442 }
443 
send_data(unsigned char data)444 static int send_data(unsigned char data)
445 {
446 	int retries = 3;
447 	unsigned long flags;
448 
449 	do {
450 		unsigned long timeout = 1000;
451 
452 		spin_lock_irqsave(&pcikbd_lock, flags);
453 
454 		kb_wait();
455 
456 		acknowledge = 0;
457 		resend = 0;
458 		reply_expected = 1;
459 
460 		pcikbd_outb(data, pcikbd_iobase + KBD_DATA_REG);
461 
462 		spin_unlock_irqrestore(&pcikbd_lock, flags);
463 
464 		do {
465 			if (acknowledge)
466 				return 1;
467 			if (resend)
468 				break;
469 			mdelay(1);
470 		} while (--timeout);
471 
472 		if (timeout == 0)
473 			break;
474 
475 	} while (retries-- > 0);
476 
477 	return 0;
478 }
479 
pcikbd_leds(unsigned char leds)480 void pcikbd_leds(unsigned char leds)
481 {
482 	if (!pcikbd_iobase)
483 		return;
484 	if (!send_data(KBD_CMD_SET_LEDS) || !send_data(leds))
485 		send_data(KBD_CMD_ENABLE);
486 }
487 
parse_kbd_rate(struct kbd_repeat * r)488 static unsigned char parse_kbd_rate(struct kbd_repeat *r)
489 {
490 	static struct r2v {
491 		int rate;
492 		unsigned char val;
493 	} kbd_rates[]={	{ 5,  0x14 },
494 			{ 7,  0x10 },
495 			{ 10, 0x0c },
496 			{ 15, 0x08 },
497 			{ 20, 0x04 },
498 			{ 25, 0x02 },
499 			{ 30, 0x00 } };
500 	static struct d2v {
501 		int delay;
502 		unsigned char val;
503 	} kbd_delays[]={ { 250,  0 },
504 			 { 500,  1 },
505 			 { 750,  2 },
506 			 { 1000, 3 } };
507 	int rate = 0, delay = 0;
508 
509 	if (r != NULL) {
510 		int i, new_rate = 30, new_delay = 250;
511 		if (r->rate <= 0)
512 			r->rate = kbdrate.rate;
513 		if (r->delay <= 0)
514 			r->delay = kbdrate.delay;
515 
516 		for (i = 0; i < sizeof(kbd_rates) / sizeof(struct r2v); i++) {
517 			if (kbd_rates[i].rate == r->rate) {
518 				new_rate = kbd_rates[i].rate;
519 				rate = kbd_rates[i].val;
520 				break;
521 			}
522 		}
523 		for (i=0; i < sizeof(kbd_delays) / sizeof(struct d2v); i++) {
524 			if (kbd_delays[i].delay == r->delay) {
525 				new_delay = kbd_delays[i].delay;
526 				delay = kbd_delays[i].val;
527 				break;
528 			}
529 		}
530 		r->rate = new_rate;
531 		r->delay = new_delay;
532 	}
533 	return (delay << 5) | rate;
534 }
535 
write_kbd_rate(unsigned char r)536 static int write_kbd_rate(unsigned char r)
537 {
538 	if (!send_data(KBD_CMD_SET_RATE) || !send_data(r)) {
539 		/* re-enable kbd if any errors */
540 		send_data(KBD_CMD_ENABLE);
541 		return 0;
542 	}
543 
544 	return 1;
545 }
546 
pcikbd_rate(struct kbd_repeat * rep)547 static int pcikbd_rate(struct kbd_repeat *rep)
548 {
549 	unsigned char r;
550 	struct kbd_repeat old_rep;
551 
552 	if (rep == NULL)
553 		return -EINVAL;
554 
555 	r = parse_kbd_rate(rep);
556 	memcpy(&old_rep, &kbdrate, sizeof(struct kbd_repeat));
557 	if (write_kbd_rate(r)) {
558 		memcpy(&kbdrate,rep,sizeof(struct kbd_repeat));
559 		memcpy(rep,&old_rep,sizeof(struct kbd_repeat));
560 		return 0;
561 	}
562 
563 	return -EIO;
564 }
565 
pcikbd_wait_for_input(void)566 static int pcikbd_wait_for_input(void)
567 {
568 	int status, data;
569 	unsigned long timeout = 1000;
570 
571 	do {
572 		mdelay(1);
573 
574 		status = pcikbd_inb(pcikbd_iobase + KBD_STATUS_REG);
575 		if (!(status & KBD_STAT_OBF))
576 			continue;
577 
578 		data = pcikbd_inb(pcikbd_iobase + KBD_DATA_REG);
579 		if (status & (KBD_STAT_GTO | KBD_STAT_PERR))
580 			continue;
581 
582 		return (data & 0xff);
583 
584 	} while (--timeout > 0);
585 
586 	return -1;
587 }
588 
pcikbd_write(int address,int data)589 static void pcikbd_write(int address, int data)
590 {
591 	int status;
592 
593 	do {
594 		status = pcikbd_inb(pcikbd_iobase + KBD_STATUS_REG);
595 	} while (status & KBD_STAT_IBF);
596 	pcikbd_outb(data, pcikbd_iobase + address);
597 }
598 
599 #ifdef __sparc_v9__
600 
601 static unsigned long pcibeep_iobase = 0;
602 
603 /* Timer routine to turn off the beep after the interval expires. */
pcikbd_kd_nosound(unsigned long __unused)604 static void pcikbd_kd_nosound(unsigned long __unused)
605 {
606 	if (pcibeep_iobase & 0x2UL)
607 		outb(0, pcibeep_iobase);
608 	else
609 		outl(0, pcibeep_iobase);
610 }
611 
612 /*
613  * Initiate a keyboard beep. If the frequency is zero, then we stop
614  * the beep. Any other frequency will start a monotone beep. The beep
615  * will be stopped by a timer after "ticks" jiffies. If ticks is 0,
616  * then we do not start a timer.
617  */
pcikbd_kd_mksound(unsigned int hz,unsigned int ticks)618 static void pcikbd_kd_mksound(unsigned int hz, unsigned int ticks)
619 {
620 	unsigned long flags;
621 	static struct timer_list sound_timer = { function: pcikbd_kd_nosound };
622 
623 	save_flags(flags); cli();
624 	del_timer(&sound_timer);
625 	if (hz) {
626 		if (pcibeep_iobase & 0x2UL)
627 			outb(1, pcibeep_iobase);
628 		else
629 			outl(1, pcibeep_iobase);
630 		if (ticks) {
631 			sound_timer.expires = jiffies + ticks;
632 			add_timer(&sound_timer);
633 		}
634 	} else {
635 		if (pcibeep_iobase & 0x2UL)
636 			outb(0, pcibeep_iobase);
637 		else
638 			outl(0, pcibeep_iobase);
639 	}
640 	restore_flags(flags);
641 }
642 
643 #if (defined(CONFIG_USB) || defined(CONFIG_USB_MODULE)) && defined(CONFIG_SPARC64)
isa_kd_nosound(unsigned long __unused)644 static void isa_kd_nosound(unsigned long __unused)
645 {
646 	/* disable counter 2 */
647 	outb(inb(pcibeep_iobase + 0x61)&0xFC, pcibeep_iobase + 0x61);
648 	return;
649 }
650 
isa_kd_mksound(unsigned int hz,unsigned int ticks)651 static void isa_kd_mksound(unsigned int hz, unsigned int ticks)
652 {
653 	static struct timer_list sound_timer = { function: isa_kd_nosound };
654 	unsigned int count = 0;
655 	unsigned long flags;
656 
657 	if (hz > 20 && hz < 32767)
658 		count = 1193180 / hz;
659 
660 	save_flags(flags);
661 	cli();
662 	del_timer(&sound_timer);
663 	if (count) {
664 		/* enable counter 2 */
665 		outb(inb(pcibeep_iobase + 0x61)|3, pcibeep_iobase + 0x61);
666 		/* set command for counter 2, 2 byte write */
667 		outb(0xB6, pcibeep_iobase + 0x43);
668 		/* select desired HZ */
669 		outb(count & 0xff, pcibeep_iobase + 0x42);
670 		outb((count >> 8) & 0xff, pcibeep_iobase + 0x42);
671 
672 		if (ticks) {
673 			sound_timer.expires = jiffies+ticks;
674 			add_timer(&sound_timer);
675 		}
676 	} else
677 		isa_kd_nosound(0);
678 	restore_flags(flags);
679 	return;
680 }
681 #endif
682 
683 #endif
684 
nop_kd_mksound(unsigned int hz,unsigned int ticks)685 static void nop_kd_mksound(unsigned int hz, unsigned int ticks)
686 {
687 }
688 
689 extern void (*kd_mksound)(unsigned int hz, unsigned int ticks);
690 
do_pcikbd_init_hw(void)691 static char * __init do_pcikbd_init_hw(void)
692 {
693 
694 	while(pcikbd_wait_for_input() != -1)
695 		;
696 
697 	pcikbd_write(KBD_CNTL_REG, KBD_CCMD_SELF_TEST);
698 	if(pcikbd_wait_for_input() != 0x55)
699 		return "Keyboard failed self test";
700 
701 	pcikbd_write(KBD_CNTL_REG, KBD_CCMD_KBD_TEST);
702 	if(pcikbd_wait_for_input() != 0x00)
703 		return "Keyboard interface failed self test";
704 
705 	pcikbd_write(KBD_CNTL_REG, KBD_CCMD_KBD_ENABLE);
706 	pcikbd_write(KBD_DATA_REG, KBD_CMD_RESET);
707 	if(pcikbd_wait_for_input() != KBD_REPLY_ACK)
708 		return "Keyboard reset failed, no ACK";
709 	if(pcikbd_wait_for_input() != KBD_REPLY_POR)
710 		return "Keyboard reset failed, no ACK";
711 
712 	pcikbd_write(KBD_DATA_REG, KBD_CMD_DISABLE);
713 	if(pcikbd_wait_for_input() != KBD_REPLY_ACK)
714 		return "Disable keyboard: no ACK";
715 
716 	pcikbd_write(KBD_CNTL_REG, KBD_CCMD_WRITE_MODE);
717 	pcikbd_write(KBD_DATA_REG,
718 		     (KBD_MODE_KBD_INT | KBD_MODE_SYS |
719 		      KBD_MODE_DISABLE_MOUSE | KBD_MODE_KCC));
720 	pcikbd_write(KBD_DATA_REG, KBD_CMD_ENABLE);
721 	if(pcikbd_wait_for_input() != KBD_REPLY_ACK)
722 		return "Enable keyboard: no ACK";
723 
724 	write_kbd_rate(parse_kbd_rate(&kbdrate));
725 
726 	return NULL; /* success */
727 }
728 
pcikbd_init_hw(void)729 void __init pcikbd_init_hw(void)
730 {
731 	struct linux_ebus *ebus;
732 	struct linux_ebus_device *edev;
733 	struct linux_ebus_child *child;
734 	char *msg;
735 
736 	if (pcikbd_mrcoffee) {
737 		if ((pcikbd_iobase = (unsigned long) ioremap(0x71300060, 8)) == 0) {
738 			prom_printf("pcikbd_init_hw: cannot map\n");
739 			return;
740 		}
741 		pcikbd_irq = 13 | 0x20;
742 		if (request_irq(pcikbd_irq, &pcikbd_interrupt,
743 				SA_SHIRQ, "keyboard", (void *)pcikbd_iobase)) {
744 			printk("8042: cannot register IRQ %x\n", pcikbd_irq);
745 			return;
746 		}
747 		printk("8042(kbd): iobase[%x] irq[%x]\n",
748 		    (unsigned)pcikbd_iobase, pcikbd_irq);
749 	} else {
750 		for_each_ebus(ebus) {
751 			for_each_ebusdev(edev, ebus) {
752 				if(!strcmp(edev->prom_name, "8042")) {
753 					for_each_edevchild(edev, child) {
754                                                 if (strcmp(child->prom_name, PCI_KB_NAME1) == 0 ||
755 						    strcmp(child->prom_name, PCI_KB_NAME2) == 0)
756 							goto found;
757 					}
758 				}
759 			}
760 		}
761 #if defined(CONFIG_USB) || defined(CONFIG_USB_MODULE)
762 		/* We are being called for the sake of USB keyboard
763 		 * state initialization.  So we should check for beeper
764 		 * device in this case.
765 		 */
766 		edev = 0;
767 		for_each_ebus(ebus) {
768 			for_each_ebusdev(edev, ebus) {
769 				if (!strcmp(edev->prom_name, "beep")) {
770 					pcibeep_iobase = edev->resource[0].start;
771 					kd_mksound = pcikbd_kd_mksound;
772 					printk("8042(speaker): iobase[%016lx]\n", pcibeep_iobase);
773 					return;
774 				}
775 			}
776 		}
777 
778 #ifdef CONFIG_SPARC64
779 		/* Maybe we have one inside the ALI southbridge? */
780 		{
781 			struct isa_bridge *isa_br;
782 			struct isa_device *isa_dev;
783 			for_each_isa(isa_br) {
784 				for_each_isadev(isa_dev, isa_br) {
785 					/* This is a hack, the 'dma' device node has
786 					 * the base of the I/O port space for that PBM
787 					 * as it's resource, so we use that. -DaveM
788 					 */
789 					if (!strcmp(isa_dev->prom_name, "dma")) {
790 						pcibeep_iobase = isa_dev->resource.start;
791 						kd_mksound = isa_kd_mksound;
792 						printk("isa(speaker): iobase[%016lx:%016lx]\n",
793 						       pcibeep_iobase + 0x42,
794 						       pcibeep_iobase + 0x61);
795 						return;
796 					}
797 				}
798 			}
799 		}
800 #endif
801 
802 		/* No beeper found, ok complain. */
803 #endif
804 		printk("pcikbd_init_hw: no 8042 found\n");
805 		return;
806 
807 found:
808 		pcikbd_iobase = child->resource[0].start;
809 		pcikbd_irq = child->irqs[0];
810 		if (request_irq(pcikbd_irq, &pcikbd_interrupt,
811 				SA_SHIRQ, "keyboard", (void *)pcikbd_iobase)) {
812 			printk("8042: cannot register IRQ %s\n",
813 			       __irq_itoa(pcikbd_irq));
814 			return;
815 		}
816 
817 		printk("8042(kbd) at 0x%lx (irq %s)\n", pcikbd_iobase,
818 		       __irq_itoa(pcikbd_irq));
819 	}
820 
821 	kd_mksound = nop_kd_mksound;
822 	kbd_rate = pcikbd_rate;
823 
824 #ifdef __sparc_v9__
825 	edev = 0;
826 	for_each_ebus(ebus) {
827 		for_each_ebusdev(edev, ebus) {
828 			if(!strcmp(edev->prom_name, "beeper"))
829 				goto ebus_done;
830 		}
831 	}
832 ebus_done:
833 
834 	/*
835 	 * XXX: my 3.1.3 PROM does not give me the beeper node for the audio
836 	 *      auxio register, though I know it is there... (ecd)
837 	 *
838 	 * JavaStations appear not to have beeper. --zaitcev
839 	 */
840 	if (!edev)
841 		pcibeep_iobase = (pcikbd_iobase & ~(0xffffff)) | 0x722000;
842 	else
843 		pcibeep_iobase = edev->resource[0].start;
844 
845 	kd_mksound = pcikbd_kd_mksound;
846 	printk("8042(speaker): iobase[%016lx]%s\n", pcibeep_iobase,
847 	       edev ? "" : " (forced)");
848 
849 	prom_keyboard = pcikbd_enter_prom;
850 #endif
851 
852 	disable_irq(pcikbd_irq);
853 	msg = do_pcikbd_init_hw();
854 	enable_irq(pcikbd_irq);
855 
856 	if(msg)
857 		printk("8042: keyboard init failure [%s]\n", msg);
858 }
859 
860 
861 /*
862  * Here begins the Mouse Driver.
863  */
864 
865 static unsigned long pcimouse_iobase = 0;
866 static unsigned int pcimouse_irq;
867 
868 #define AUX_BUF_SIZE	2048
869 
870 struct aux_queue {
871 	unsigned long head;
872 	unsigned long tail;
873 	wait_queue_head_t proc_list;
874 	struct fasync_struct *fasync;
875 	unsigned char buf[AUX_BUF_SIZE];
876 };
877 
878 static struct aux_queue *queue;
879 static int aux_count = 0;
880 static int aux_present = 0;
881 
882 #define pcimouse_inb(x)     inb(x)
883 #define pcimouse_outb(v,x)  outb(v,x)
884 
885 /*
886  *	Shared subroutines
887  */
888 
get_from_queue(void)889 static unsigned int get_from_queue(void)
890 {
891 	unsigned int result;
892 	unsigned long flags;
893 
894 	spin_lock_irqsave(&pcikbd_lock, flags);
895 	result = queue->buf[queue->tail];
896 	queue->tail = (queue->tail + 1) & (AUX_BUF_SIZE-1);
897 	spin_unlock_irqrestore(&pcikbd_lock, flags);
898 
899 	return result;
900 }
901 
902 
queue_empty(void)903 static inline int queue_empty(void)
904 {
905 	return queue->head == queue->tail;
906 }
907 
aux_fasync(int fd,struct file * filp,int on)908 static int aux_fasync(int fd, struct file *filp, int on)
909 {
910 	int retval;
911 
912 	retval = fasync_helper(fd, filp, on, &queue->fasync);
913 	if (retval < 0)
914 		return retval;
915 	return 0;
916 }
917 
918 /*
919  *	PS/2 Aux Device
920  */
921 
922 #define AUX_INTS_OFF	(KBD_MODE_KCC | KBD_MODE_DISABLE_MOUSE | \
923 			 KBD_MODE_SYS | KBD_MODE_KBD_INT)
924 
925 #define AUX_INTS_ON	(KBD_MODE_KCC | KBD_MODE_SYS | \
926 			 KBD_MODE_MOUSE_INT | KBD_MODE_KBD_INT)
927 
928 #define MAX_RETRIES	60		/* some aux operations take long time*/
929 
930 /*
931  *	Status polling
932  */
933 
poll_aux_status(void)934 static int poll_aux_status(void)
935 {
936 	int retries = 0;
937 
938 	while ((pcimouse_inb(pcimouse_iobase + KBD_STATUS_REG) &
939 		(KBD_STAT_IBF | KBD_STAT_OBF)) && retries < MAX_RETRIES) {
940  		if ((pcimouse_inb(pcimouse_iobase + KBD_STATUS_REG) & AUX_STAT_OBF)
941 		    == AUX_STAT_OBF)
942 			pcimouse_inb(pcimouse_iobase + KBD_DATA_REG);
943 		mdelay(5);
944 		retries++;
945 	}
946 
947 	return (retries < MAX_RETRIES);
948 }
949 
950 /*
951  * Write to aux device
952  */
953 
aux_write_dev(int val)954 static void aux_write_dev(int val)
955 {
956 	poll_aux_status();
957 	pcimouse_outb(KBD_CCMD_WRITE_MOUSE, pcimouse_iobase + KBD_CNTL_REG);/* Write magic cookie */
958 	poll_aux_status();
959 	pcimouse_outb(val, pcimouse_iobase + KBD_DATA_REG);		 /* Write data */
960 	udelay(1);
961 }
962 
963 /*
964  * Write to device & handle returned ack
965  */
966 
aux_write_ack(int val)967 static int __init aux_write_ack(int val)
968 {
969 	aux_write_dev(val);
970 	poll_aux_status();
971 
972 	if ((pcimouse_inb(pcimouse_iobase + KBD_STATUS_REG) & AUX_STAT_OBF) == AUX_STAT_OBF)
973 		return (pcimouse_inb(pcimouse_iobase + KBD_DATA_REG));
974 	return 0;
975 }
976 
977 /*
978  * Write aux device command
979  */
980 
aux_write_cmd(int val)981 static void aux_write_cmd(int val)
982 {
983 	poll_aux_status();
984 	pcimouse_outb(KBD_CCMD_WRITE_MODE, pcimouse_iobase + KBD_CNTL_REG);
985 	poll_aux_status();
986 	pcimouse_outb(val, pcimouse_iobase + KBD_DATA_REG);
987 }
988 
989 /*
990  * Interrupt from the auxiliary device: a character
991  * is waiting in the keyboard/aux controller.
992  */
993 
pcimouse_interrupt(int irq,void * dev_id,struct pt_regs * regs)994 void pcimouse_interrupt(int irq, void *dev_id, struct pt_regs *regs)
995 {
996 	unsigned long flags;
997 	int head, maxhead;
998 	unsigned char val;
999 
1000 	spin_lock_irqsave(&pcikbd_lock, flags);
1001 
1002 	head = queue->head;
1003 	maxhead = (queue->tail - 1) & (AUX_BUF_SIZE - 1);
1004 
1005 	if ((pcimouse_inb(pcimouse_iobase + KBD_STATUS_REG) & AUX_STAT_OBF) !=
1006 	    AUX_STAT_OBF) {
1007 		spin_unlock_irqrestore(&pcikbd_lock, flags);
1008 		return;
1009 	}
1010 
1011 	val = pcimouse_inb(pcimouse_iobase + KBD_DATA_REG);
1012 	queue->buf[head] = val;
1013 	add_mouse_randomness(val);
1014 	if (head != maxhead) {
1015 		head++;
1016 		head &= AUX_BUF_SIZE - 1;
1017 	}
1018 	queue->head = head;
1019 
1020 	spin_unlock_irqrestore(&pcikbd_lock, flags);
1021 
1022 	kill_fasync(&queue->fasync, SIGIO, POLL_IN);
1023 	wake_up_interruptible(&queue->proc_list);
1024 }
1025 
aux_release(struct inode * inode,struct file * file)1026 static int aux_release(struct inode * inode, struct file * file)
1027 {
1028 	unsigned long flags;
1029 
1030 	aux_fasync(-1, file, 0);
1031 
1032 	spin_lock_irqsave(&pcikbd_lock, flags);
1033 
1034 	if (--aux_count)
1035 		goto out;
1036 
1037 	/* Disable controller ints */
1038 	aux_write_cmd(AUX_INTS_OFF);
1039 	poll_aux_status();
1040 
1041 	/* Disable Aux device */
1042 	pcimouse_outb(KBD_CCMD_MOUSE_DISABLE, pcimouse_iobase + KBD_CNTL_REG);
1043 	poll_aux_status();
1044 
1045 out:
1046 	spin_unlock_irqrestore(&pcikbd_lock, flags);
1047 
1048 	return 0;
1049 }
1050 
1051 /*
1052  * Install interrupt handler.
1053  * Enable auxiliary device.
1054  */
1055 
aux_open(struct inode * inode,struct file * file)1056 static int aux_open(struct inode * inode, struct file * file)
1057 {
1058 	unsigned long flags;
1059 
1060 	if (!aux_present)
1061 		return -ENODEV;
1062 
1063 	spin_lock_irqsave(&pcikbd_lock, flags);
1064 
1065 	if (aux_count++) {
1066 		spin_unlock_irqrestore(&pcikbd_lock, flags);
1067 		return 0;
1068 	}
1069 
1070 	if (!poll_aux_status()) {
1071 		aux_count--;
1072 		spin_unlock_irqrestore(&pcikbd_lock, flags);
1073 		return -EBUSY;
1074 	}
1075 	queue->head = queue->tail = 0;		/* Flush input queue */
1076 
1077 	poll_aux_status();
1078 	pcimouse_outb(KBD_CCMD_MOUSE_ENABLE, pcimouse_iobase+KBD_CNTL_REG);    /* Enable Aux */
1079 	aux_write_dev(AUX_ENABLE_DEV);			    /* Enable aux device */
1080 	aux_write_cmd(AUX_INTS_ON);			    /* Enable controller ints */
1081 	poll_aux_status();
1082 
1083 	spin_unlock_irqrestore(&pcikbd_lock, flags);
1084 
1085 
1086 	return 0;
1087 }
1088 
1089 /*
1090  * Write to the aux device.
1091  */
1092 
aux_write(struct file * file,const char * buffer,size_t count,loff_t * ppos)1093 static ssize_t aux_write(struct file * file, const char * buffer,
1094 			 size_t count, loff_t *ppos)
1095 {
1096 	ssize_t retval = 0;
1097 	unsigned long flags;
1098 
1099 	if (count) {
1100 		ssize_t written = 0;
1101 
1102 		spin_lock_irqsave(&pcikbd_lock, flags);
1103 
1104 		do {
1105 			char c;
1106 
1107 			spin_unlock_irqrestore(&pcikbd_lock, flags);
1108 
1109 			get_user(c, buffer++);
1110 
1111 			spin_lock_irqsave(&pcikbd_lock, flags);
1112 
1113 			if (!poll_aux_status())
1114 				break;
1115 			pcimouse_outb(KBD_CCMD_WRITE_MOUSE,
1116 				      pcimouse_iobase + KBD_CNTL_REG);
1117 			if (!poll_aux_status())
1118 				break;
1119 
1120 			pcimouse_outb(c, pcimouse_iobase + KBD_DATA_REG);
1121 			written++;
1122 		} while (--count);
1123 
1124 		spin_unlock_irqrestore(&pcikbd_lock, flags);
1125 
1126 		retval = -EIO;
1127 		if (written) {
1128 			retval = written;
1129 			file->f_dentry->d_inode->i_mtime = CURRENT_TIME;
1130 		}
1131 	}
1132 
1133 	return retval;
1134 }
1135 
1136 /*
1137  *	Generic part continues...
1138  */
1139 
1140 /*
1141  * Put bytes from input queue to buffer.
1142  */
1143 
aux_read(struct file * file,char * buffer,size_t count,loff_t * ppos)1144 static ssize_t aux_read(struct file * file, char * buffer,
1145 		        size_t count, loff_t *ppos)
1146 {
1147 	DECLARE_WAITQUEUE(wait, current);
1148 	ssize_t i = count;
1149 	unsigned char c;
1150 
1151 	if (queue_empty()) {
1152 		if (file->f_flags & O_NONBLOCK)
1153 			return -EAGAIN;
1154 		add_wait_queue(&queue->proc_list, &wait);
1155 repeat:
1156 		set_current_state(TASK_INTERRUPTIBLE);
1157 		if (queue_empty() && !signal_pending(current)) {
1158 			schedule();
1159 			goto repeat;
1160 		}
1161 		current->state = TASK_RUNNING;
1162 		remove_wait_queue(&queue->proc_list, &wait);
1163 	}
1164 	while (i > 0 && !queue_empty()) {
1165 		c = get_from_queue();
1166 		put_user(c, buffer++);
1167 		i--;
1168 	}
1169 	if (count-i) {
1170 		file->f_dentry->d_inode->i_atime = CURRENT_TIME;
1171 		return count-i;
1172 	}
1173 	if (signal_pending(current))
1174 		return -ERESTARTSYS;
1175 	return 0;
1176 }
1177 
aux_poll(struct file * file,poll_table * wait)1178 static unsigned int aux_poll(struct file *file, poll_table * wait)
1179 {
1180 	poll_wait(file, &queue->proc_list, wait);
1181 	if (!queue_empty())
1182 		return POLLIN | POLLRDNORM;
1183 	return 0;
1184 }
1185 
1186 struct file_operations psaux_fops = {
1187 	owner:		THIS_MODULE,
1188 	read:		aux_read,
1189 	write:		aux_write,
1190 	poll:		aux_poll,
1191 	open:		aux_open,
1192 	release:	aux_release,
1193 	fasync:		aux_fasync,
1194 };
1195 
aux_no_open(struct inode * inode,struct file * file)1196 static int aux_no_open(struct inode *inode, struct file *file)
1197 {
1198 	return -ENODEV;
1199 }
1200 
1201 struct file_operations psaux_no_fops = {
1202 	owner:		THIS_MODULE,
1203 	open:		aux_no_open,
1204 };
1205 
1206 static struct miscdevice psaux_mouse = {
1207 	PSMOUSE_MINOR, "ps2aux", &psaux_fops
1208 };
1209 
1210 static struct miscdevice psaux_no_mouse = {
1211 	PSMOUSE_MINOR, "ps2aux", &psaux_no_fops
1212 };
1213 
pcimouse_init(void)1214 int __init pcimouse_init(void)
1215 {
1216 	struct linux_ebus *ebus;
1217 	struct linux_ebus_device *edev;
1218 	struct linux_ebus_child *child;
1219 
1220 	if (pcikbd_mrcoffee) {
1221 		if ((pcimouse_iobase = pcikbd_iobase) == 0) {
1222 			printk("pcimouse_init: no 8042 given\n");
1223 			goto do_enodev;
1224 		}
1225 		pcimouse_irq = pcikbd_irq;
1226 	} else {
1227 		for_each_ebus(ebus) {
1228 			for_each_ebusdev(edev, ebus) {
1229 				if(!strcmp(edev->prom_name, "8042")) {
1230 					for_each_edevchild(edev, child) {
1231 							if (strcmp(child->prom_name, PCI_MS_NAME1) == 0 ||
1232 							    strcmp(child->prom_name, PCI_MS_NAME2) == 0)
1233 							goto found;
1234 					}
1235 				}
1236 			}
1237 		}
1238 		printk("pcimouse_init: no 8042 found\n");
1239 		goto do_enodev;
1240 
1241 found:
1242 		pcimouse_iobase = child->resource[0].start;
1243 		pcimouse_irq = child->irqs[0];
1244 	}
1245 
1246 	queue = (struct aux_queue *) kmalloc(sizeof(*queue), GFP_KERNEL);
1247 	if (!queue) {
1248 		printk("pcimouse_init: kmalloc(aux_queue) failed.\n");
1249 		return -ENOMEM;
1250 	}
1251 	memset(queue, 0, sizeof(*queue));
1252 
1253 	init_waitqueue_head(&queue->proc_list);
1254 
1255 	if (request_irq(pcimouse_irq, &pcimouse_interrupt,
1256 		        SA_SHIRQ, "mouse", (void *)pcimouse_iobase)) {
1257 		printk("8042: Cannot register IRQ %s\n",
1258 		       __irq_itoa(pcimouse_irq));
1259 		goto do_enodev;
1260 	}
1261 
1262 	printk("8042(mouse) at %lx (irq %s)\n", pcimouse_iobase,
1263 	       __irq_itoa(pcimouse_irq));
1264 
1265 	printk("8042: PS/2 auxiliary pointing device detected.\n");
1266 	aux_present = 1;
1267 	pckbd_read_mask = AUX_STAT_OBF;
1268 
1269 	misc_register(&psaux_mouse);
1270 
1271 	spin_lock_irq(&pcikbd_lock);
1272 
1273 	pcimouse_outb(KBD_CCMD_MOUSE_ENABLE, pcimouse_iobase + KBD_CNTL_REG);
1274 	aux_write_ack(AUX_RESET);
1275 	aux_write_ack(AUX_SET_SAMPLE);
1276 	aux_write_ack(100);
1277 	aux_write_ack(AUX_SET_RES);
1278 	aux_write_ack(3);
1279 	aux_write_ack(AUX_SET_SCALE21);
1280 	poll_aux_status();
1281 	pcimouse_outb(KBD_CCMD_MOUSE_DISABLE, pcimouse_iobase + KBD_CNTL_REG);
1282 	poll_aux_status();
1283 	pcimouse_outb(KBD_CCMD_WRITE_MODE, pcimouse_iobase + KBD_CNTL_REG);
1284 	poll_aux_status();
1285 	pcimouse_outb(AUX_INTS_OFF, pcimouse_iobase + KBD_DATA_REG);
1286 	poll_aux_status();
1287 
1288 	spin_unlock_irq(&pcikbd_lock);
1289 
1290 	return 0;
1291 
1292 do_enodev:
1293 	misc_register(&psaux_no_mouse);
1294 	return -ENODEV;
1295 }
1296 
pcimouse_no_init(void)1297 int __init pcimouse_no_init(void)
1298 {
1299 	misc_register(&psaux_no_mouse);
1300 	return -ENODEV;
1301 }
1302 
ps2kbd_probe(void)1303 int __init ps2kbd_probe(void)
1304 {
1305 	int pnode, enode, node, dnode, xnode;
1306 	int kbnode = 0, msnode = 0, bnode = 0;
1307 	int devices = 0;
1308 	char prop[128];
1309 	int len;
1310 
1311 #ifndef __sparc_v9__
1312 	/*
1313 	 * MrCoffee has hardware but has no PROM nodes whatsoever.
1314 	 */
1315 	len = prom_getproperty(prom_root_node, "name", prop, sizeof(prop));
1316 	if (len < 0) {
1317 		printk("ps2kbd_probe: no name of root node\n");
1318 		goto do_enodev;
1319 	}
1320 	if (strncmp(prop, "SUNW,JavaStation-1", len) == 0) {
1321 		pcikbd_mrcoffee = 1;	/* Brain damage detected */
1322 		goto found;
1323 	}
1324 #endif
1325 	/*
1326 	 * Get the nodes for keyboard and mouse from aliases on normal systems.
1327 	 */
1328         node = prom_getchild(prom_root_node);
1329 	node = prom_searchsiblings(node, "aliases");
1330 	if (!node)
1331 		goto do_enodev;
1332 
1333 	len = prom_getproperty(node, "keyboard", prop, sizeof(prop));
1334 	if (len > 0) {
1335 		prop[len] = 0;
1336 		kbnode = prom_finddevice(prop);
1337 	}
1338 	if (!kbnode)
1339 		goto do_enodev;
1340 
1341 	len = prom_getproperty(node, "mouse", prop, sizeof(prop));
1342 	if (len > 0) {
1343 		prop[len] = 0;
1344 		msnode = prom_finddevice(prop);
1345 	}
1346 	if (!msnode)
1347 		goto do_enodev;
1348 
1349 	/*
1350 	 * Find matching EBus nodes...
1351 	 */
1352         node = prom_getchild(prom_root_node);
1353 	pnode = prom_searchsiblings(node, "pci");
1354 
1355 	/*
1356 	 * Check for SUNW,sabre on Ultra5/10/AXi.
1357 	 */
1358 	len = prom_getproperty(pnode, "model", prop, sizeof(prop));
1359 	if ((len > 0) && !strncmp(prop, "SUNW,sabre", len)) {
1360 		pnode = prom_getchild(pnode);
1361 		pnode = prom_searchsiblings(pnode, "pci");
1362 	}
1363 
1364 	/*
1365 	 * For each PCI bus...
1366 	 */
1367 	while (pnode) {
1368 		enode = prom_getchild(pnode);
1369 		enode = prom_searchsiblings(enode, "ebus");
1370 
1371 		/*
1372 		 * For each EBus on this PCI...
1373 		 */
1374 		while (enode) {
1375 			node = prom_getchild(enode);
1376 			bnode = prom_searchsiblings(node, "beeper");
1377 
1378 			node = prom_getchild(enode);
1379 			node = prom_searchsiblings(node, "8042");
1380 
1381 			/*
1382 			 * For each '8042' on this EBus...
1383 			 */
1384 			while (node) {
1385 				dnode = prom_getchild(node);
1386 
1387 				/*
1388 				 * Does it match?
1389 				 */
1390 				if ((xnode = prom_searchsiblings(dnode, PCI_KB_NAME1)) == kbnode) {
1391 					++devices;
1392 				} else if ((xnode = prom_searchsiblings(dnode, PCI_KB_NAME2)) == kbnode) {
1393 					++devices;
1394 				}
1395 
1396 				if ((xnode = prom_searchsiblings(dnode, PCI_MS_NAME1)) == msnode) {
1397 					++devices;
1398 				} else if ((xnode = prom_searchsiblings(dnode, PCI_MS_NAME2)) == msnode) {
1399 					++devices;
1400 				}
1401 
1402 				/*
1403 				 * Found everything we need?
1404 				 */
1405 				if (devices == 2)
1406 					goto found;
1407 
1408 				node = prom_getsibling(node);
1409 				node = prom_searchsiblings(node, "8042");
1410 			}
1411 			enode = prom_getsibling(enode);
1412 			enode = prom_searchsiblings(enode, "ebus");
1413 		}
1414 		pnode = prom_getsibling(pnode);
1415 		pnode = prom_searchsiblings(pnode, "pci");
1416 	}
1417 do_enodev:
1418 	sunkbd_setinitfunc(pcimouse_no_init);
1419 	return -ENODEV;
1420 
1421 found:
1422         sunkbd_setinitfunc(pcimouse_init);
1423         sunkbd_setinitfunc(pcikbd_init);
1424 	kbd_ops.compute_shiftstate = pci_compute_shiftstate;
1425 	kbd_ops.setledstate = pci_setledstate;
1426 	kbd_ops.getledstate = pci_getledstate;
1427 	kbd_ops.setkeycode = pci_setkeycode;
1428 	kbd_ops.getkeycode = pci_getkeycode;
1429 	return 0;
1430 }
1431