Added patch headers.
[linux-flexiantxendom0-3.2.10.git] / arch / ia64 / kdb / kdba_io.c
1 /*
2  * Kernel Debugger Architecture Dependent Console I/O handler
3  *
4  * This file is subject to the terms and conditions of the GNU General Public
5  * License.  See the file "COPYING" in the main directory of this archive
6  * for more details.
7  *
8  * Copyright (c) 1999-2006 Silicon Graphics, Inc.  All Rights Reserved.
9  */
10
11 #include <linux/kernel.h>
12 #include <asm/io.h>
13 #include <linux/delay.h>
14 #include <linux/console.h>
15 #include <linux/ctype.h>
16 #include <linux/keyboard.h>
17 #include <linux/serial.h>
18 #include <linux/serial_reg.h>
19
20 #include <linux/kdb.h>
21 #include <linux/kdbprivate.h>
22
23 #if defined(CONFIG_SERIAL_8250_CONSOLE) || defined(CONFIG_SERIAL_SGI_L1_CONSOLE)
24 #define HAVE_KDBA_SERIAL_CONSOLE
25 #endif
26
27 /* from include/linux/pc_keyb.h on 2.4 */
28 #define KBD_STATUS_REG          0x64    /* Status register (R) */
29 #define KBD_DATA_REG            0x60    /* Keyboard data register (R/W) */
30 #define KBD_CMD_SET_LEDS        0xED    /* Set keyboard leds */
31 #define KBD_STAT_OBF            0x01    /* Keyboard output buffer full */
32 #define KBD_STAT_IBF            0x02    /* Keyboard input buffer full */
33 #define KBD_STAT_MOUSE_OBF      0x20    /* Mouse output buffer full */
34
35 #ifdef  CONFIG_VT_CONSOLE
36 #define KDB_BLINK_LED 1
37 #else
38 #undef  KDB_BLINK_LED
39 #endif
40
41 #ifdef CONFIG_KDB_USB
42
43 /* support up to 8 USB keyboards (probably excessive, but...) */
44 #define KDB_USB_NUM_KEYBOARDS   8
45 struct kdb_usb_kbd_info kdb_usb_kbds[KDB_USB_NUM_KEYBOARDS];
46 EXPORT_SYMBOL(kdb_usb_kbds);
47
48 extern int kdb_no_usb;
49
50 static unsigned char kdb_usb_keycode[256] = {
51           0,  0,  0,  0, 30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38,
52          50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44,  2,  3,
53           4,  5,  6,  7,  8,  9, 10, 11, 28,  1, 14, 15, 57, 12, 13, 26,
54          27, 43, 84, 39, 40, 41, 51, 52, 53, 58, 59, 60, 61, 62, 63, 64,
55          65, 66, 67, 68, 87, 88, 99, 70,119,110,102,104,111,107,109,106,
56         105,108,103, 69, 98, 55, 74, 78, 96, 79, 80, 81, 75, 76, 77, 71,
57          72, 73, 82, 83, 86,127,116,117, 85, 89, 90, 91, 92, 93, 94, 95,
58         120,121,122,123,134,138,130,132,128,129,131,137,133,135,136,113,
59         115,114,  0,  0,  0,124,  0,181,182,183,184,185,186,187,188,189,
60         190,191,192,193,194,195,196,197,198,  0,  0,  0,  0,  0,  0,  0,
61           0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
62           0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
63           0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
64           0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
65          29, 42, 56,125, 97, 54,100,126,164,166,165,163,161,115,114,113,
66         150,158,159,128,136,177,178,176,142,152,173,140
67 };
68
69 /*
70  * kdb_usb_keyboard_attach()
71  * Attach a USB keyboard to kdb.
72  */
73 int
74 kdb_usb_keyboard_attach(struct urb *urb, unsigned char *buffer,
75                         void *poll_func, void *compl_func,
76                         kdb_hc_keyboard_attach_t kdb_hc_keyboard_attach,
77                         kdb_hc_keyboard_detach_t kdb_hc_keyboard_detach,
78                         unsigned int bufsize, struct urb *hid_urb)
79 {
80         int     i;
81         int     rc = -1;
82
83         if (kdb_no_usb)
84                 return 0;
85
86         /*
87          * Search through the array of KDB USB keyboards (kdb_usb_kbds)
88          * looking for a free index. If found, assign the keyboard to
89          * the array index.
90          */
91
92         for (i = 0; i < KDB_USB_NUM_KEYBOARDS; i++) {
93                 if (kdb_usb_kbds[i].urb) /* index is already assigned */
94                         continue;
95
96                 /* found a free array index */
97                 kdb_usb_kbds[i].urb = urb;
98                 kdb_usb_kbds[i].buffer = buffer;
99                 kdb_usb_kbds[i].poll_func = poll_func;
100
101                 kdb_usb_kbds[i].kdb_hc_urb_complete = compl_func;
102                 kdb_usb_kbds[i].kdb_hc_keyboard_attach = kdb_hc_keyboard_attach;
103                 kdb_usb_kbds[i].kdb_hc_keyboard_detach = kdb_hc_keyboard_detach;
104
105                 /* USB Host Controller specific Keyboadr attach callback.
106                  * Currently only UHCI has this callback.
107                  */
108                 if (kdb_usb_kbds[i].kdb_hc_keyboard_attach)
109                         kdb_usb_kbds[i].kdb_hc_keyboard_attach(i, bufsize);
110
111
112                 rc = 0; /* success */
113
114                 break;
115         }
116
117         return rc;
118 }
119 EXPORT_SYMBOL_GPL (kdb_usb_keyboard_attach);
120
121 /*
122  * kdb_usb_keyboard_detach()
123  * Detach a USB keyboard from kdb.
124  */
125 int
126 kdb_usb_keyboard_detach(struct urb *urb)
127 {
128         int     i;
129         int     rc = -1;
130
131         if (kdb_no_usb)
132                 return 0;
133
134         /*
135          * Search through the array of KDB USB keyboards (kdb_usb_kbds)
136          * looking for the index with the matching URB. If found,
137          * clear the array index.
138          */
139
140         for (i = 0; i < KDB_USB_NUM_KEYBOARDS; i++) {
141                 if (kdb_usb_kbds[i].urb != urb)
142                         continue;
143
144                 /* USB Host Controller specific Keyboard detach callback.
145                  * Currently only UHCI has this callback.
146                  */
147                 if (kdb_usb_kbds[i].kdb_hc_keyboard_detach)
148                         kdb_usb_kbds[i].kdb_hc_keyboard_detach(urb, i);
149
150
151                 /* found it, clear the index */
152                 kdb_usb_kbds[i].urb = NULL;
153                 kdb_usb_kbds[i].buffer = NULL;
154                 kdb_usb_kbds[i].poll_func = NULL;
155                 kdb_usb_kbds[i].caps_lock = 0;
156                 kdb_usb_kbds[i].hid_urb = NULL;
157
158                 rc = 0; /* success */
159
160                 break;
161         }
162
163         return rc;
164 }
165 EXPORT_SYMBOL_GPL (kdb_usb_keyboard_detach);
166
167 /*
168  * get_usb_char
169  * This function drives the USB attached keyboards.
170  * Fetch the USB scancode and decode it.
171  */
172 static int
173 get_usb_char(void)
174 {
175         int     i;
176         int     ret;
177         unsigned char keycode, spec;
178         extern u_short plain_map[], shift_map[], ctrl_map[];
179
180         if (kdb_no_usb)
181                 return -1;
182
183         /*
184          * Loop through all the USB keyboard(s) and return
185          * the first character obtained from them.
186          */
187
188         for (i = 0; i < KDB_USB_NUM_KEYBOARDS; i++) {
189                 /* skip uninitialized keyboard array entries */
190                 if (!kdb_usb_kbds[i].urb || !kdb_usb_kbds[i].buffer ||
191                     !kdb_usb_kbds[i].poll_func)
192                         continue;
193
194                 /* Transfer char */
195                 ret = (*kdb_usb_kbds[i].poll_func)(kdb_usb_kbds[i].urb);
196
197                 if (ret == -EBUSY && kdb_usb_kbds[i].poll_ret != -EBUSY)
198                         kdb_printf("NOTICE: USB HC driver BUSY. "
199                             "USB keyboard has been disabled.\n");
200
201                 kdb_usb_kbds[i].poll_ret = ret;
202
203                 if (ret < 0) /* error or no characters, try the next kbd */
204                         continue;
205
206                 spec = kdb_usb_kbds[i].buffer[0];
207                 keycode = kdb_usb_kbds[i].buffer[2];
208                 kdb_usb_kbds[i].buffer[0] = (char)0;
209                 kdb_usb_kbds[i].buffer[2] = (char)0;
210
211                 if(kdb_usb_kbds[i].buffer[3]) {
212                         kdb_usb_kbds[i].buffer[3] = (char)0;
213                         continue;
214                 }
215
216                 /* A normal key is pressed, decode it */
217                 if(keycode)
218                         keycode = kdb_usb_keycode[keycode];
219
220                 /* 2 Keys pressed at one time ? */
221                 if (spec && keycode) {
222                         switch(spec)
223                         {
224                         case 0x2:
225                         case 0x20: /* Shift */
226                                 return shift_map[keycode];
227                         case 0x1:
228                         case 0x10: /* Ctrl */
229                                 return ctrl_map[keycode];
230                         case 0x4:
231                         case 0x40: /* Alt */
232                                 break;
233                         }
234                 } else if (keycode) { /* If only one key pressed */
235                         switch(keycode)
236                         {
237                         case 0x1C: /* Enter */
238                                 return 13;
239
240                         case 0x3A: /* Capslock */
241                                 kdb_usb_kbds[i].caps_lock = !(kdb_usb_kbds[i].caps_lock);
242                                 break;
243                         case 0x0E: /* Backspace */
244                                 return 8;
245                         case 0x0F: /* TAB */
246                                 return 9;
247                         case 0x77: /* Pause */
248                                 break ;
249                         default:
250                                 if(!kdb_usb_kbds[i].caps_lock) {
251                                         return plain_map[keycode];
252                                 }
253                                 else {
254                                         return shift_map[keycode];
255                                 }
256                         }
257                 }
258         }
259
260         /* no chars were returned from any of the USB keyboards */
261
262         return -1;
263 }
264 #endif  /* CONFIG_KDB_USB */
265
266 /*
267  * This module contains code to read characters from the keyboard or a serial
268  * port.
269  *
270  * It is used by the kernel debugger, and is polled, not interrupt driven.
271  *
272  */
273
274 #ifdef  KDB_BLINK_LED
275 /*
276  * send:  Send a byte to the keyboard controller.  Used primarily to
277  *        alter LED settings.
278  */
279
280 static void
281 kdb_kbdsend(unsigned char byte)
282 {
283         int timeout;
284         for (timeout = 200 * 1000; timeout && (inb(KBD_STATUS_REG) & KBD_STAT_IBF); timeout--);
285         outb(byte, KBD_DATA_REG);
286         udelay(40);
287         for (timeout = 200 * 1000; timeout && (~inb(KBD_STATUS_REG) & KBD_STAT_OBF); timeout--);
288         inb(KBD_DATA_REG);
289         udelay(40);
290 }
291
292 static void
293 kdb_toggleled(int led)
294 {
295         static int leds;
296
297         leds ^= led;
298
299         kdb_kbdsend(KBD_CMD_SET_LEDS);
300         kdb_kbdsend((unsigned char)leds);
301 }
302 #endif  /* KDB_BLINK_LED */
303
304 #ifdef  HAVE_KDBA_SERIAL_CONSOLE
305
306 struct kdb_serial kdb_serial;
307 enum kdba_serial_console kdba_serial_console;
308 static int get_serial_char(void);
309
310 /* There must be a serial_inp_xxx() and get_serial_char_xxx() for each type
311  * of console.  See enum kdba_serial_console in include/asm-$(ARCH)/kdbprivate.h.
312  */
313
314 #ifdef  CONFIG_SERIAL_8250_CONSOLE
315
316 static unsigned int
317 serial_inp_standard(const struct kdb_serial *kdb_serial, int offset)
318 {
319         offset <<= kdb_serial->ioreg_shift;
320
321         switch (kdb_serial->io_type) {
322         case SERIAL_IO_MEM:
323                 return readb((void __iomem *)(kdb_serial->iobase + offset));
324                 break;
325         default:
326                 return inb(kdb_serial->iobase + offset);
327                 break;
328         }
329 }
330
331 /* Check if there is a byte ready at the serial port */
332 static int
333 get_serial_char_standard(void)
334 {
335         unsigned char ch;
336         static unsigned long fifon;
337         if (fifon == 0) {
338                 /* try to set the FIFO */
339                 fifon = kdb_serial.iobase +
340                         (UART_FCR << kdb_serial.ioreg_shift);
341                 switch (kdb_serial.io_type) {
342                 case SERIAL_IO_MEM:
343                         writeb((UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR |
344                                 UART_FCR_CLEAR_XMIT), (void __iomem *)fifon);
345                         break;
346                 default:
347                         outb((UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR |
348                                 UART_FCR_CLEAR_XMIT), fifon);
349                         break;
350                 }
351         }
352
353         if (kdb_serial.iobase == 0)
354                 return -1;
355
356         if (serial_inp_standard(&kdb_serial, UART_LSR) & UART_LSR_DR) {
357                 ch = serial_inp_standard(&kdb_serial, UART_RX);
358                 if (ch == 0x7f)
359                         ch = 8;
360                 return ch;
361         }
362         return -1;
363 }
364
365 #else   /* !CONFIG_SERIAL_8250_CONSOLE */
366
367 #define get_serial_char_standard() -1
368
369 #endif  /* CONFIG_SERIAL_8250_CONSOLE */
370
371 #ifdef CONFIG_SERIAL_SGI_L1_CONSOLE
372
373 extern u64 master_node_bedrock_address;
374
375 /* UART registers on the Bedrock start at 0x80 */
376
377 extern int l1_serial_in_polled(void);
378 extern int l1_control_in_polled(int);
379
380 /* Read a byte from the L1 port.  kdb_serial is ignored */
381 static unsigned int
382 serial_inp_sgi_l1(const struct kdb_serial *kdb_serial, int offset)
383 {
384         if (offset & 0x80) {
385                 int counter = 10000;
386                 unsigned int value;
387                 while ( counter-- ) {
388                         value = l1_serial_in_polled();
389                         /* Gobble up the 0's */
390                         if ( value )
391                                 return(value);
392                 }
393                 return(0);
394         }
395         else {
396                 return l1_control_in_polled(offset);
397         }
398 }
399
400 /* Check if there is a byte ready at the L1 port. */
401 static int
402 get_serial_char_sgi_l1(void)
403 {
404         unsigned char ch;
405         int status;
406
407         if ((status = serial_inp_sgi_l1(&kdb_serial, UART_LSR)) & UART_LSR_DR) {
408                 ch = serial_inp_sgi_l1(&kdb_serial, UART_RX | 0x80);    /* bedrock offset */
409                 if (ch == 0x7f)
410                         ch = 8;
411                 return ch;
412         }
413         return -1;
414 }
415
416 #else   /* !CONFIG_SERIAL_SGI_L1_CONSOLE */
417
418 #define get_serial_char_sgi_l1() -1
419
420 #endif  /* CONFIG_SERIAL_SGI_L1_CONSOLE */
421
422 /* Select the serial console input at run time, to handle generic kernels */
423
424 static int
425 get_serial_char(void)
426 {
427         switch (kdba_serial_console) {
428         case KDBA_SC_NONE:
429                 return -1;
430         case KDBA_SC_STANDARD:
431                 return get_serial_char_standard();
432         case KDBA_SC_SGI_L1:
433                 return get_serial_char_sgi_l1();
434         }
435         /* gcc is not smart enough to realize that all paths return before here :( */
436         return -1;
437 }
438
439 #endif /* HAVE_KDBA_SERIAL_CONSOLE */
440
441 #ifdef  CONFIG_VT_CONSOLE
442
443 static int kbd_exists;
444
445 /*
446  * Check if the keyboard controller has a keypress for us.
447  * Some parts (Enter Release, LED change) are still blocking polled here,
448  * but hopefully they are all short.
449  */
450 static int get_kbd_char(void)
451 {
452         int scancode, scanstatus;
453         static int shift_lock;  /* CAPS LOCK state (0-off, 1-on) */
454         static int shift_key;   /* Shift next keypress */
455         static int ctrl_key;
456         u_short keychar;
457         extern u_short plain_map[], shift_map[], ctrl_map[];
458
459         if (KDB_FLAG(NO_I8042) || KDB_FLAG(NO_VT_CONSOLE) ||
460             (inb(KBD_STATUS_REG) == 0xff && inb(KBD_DATA_REG) == 0xff)) {
461                 kbd_exists = 0;
462                 return -1;
463         }
464         kbd_exists = 1;
465
466         if ((inb(KBD_STATUS_REG) & KBD_STAT_OBF) == 0)
467                 return -1;
468
469         /*
470          * Fetch the scancode
471          */
472         scancode = inb(KBD_DATA_REG);
473         scanstatus = inb(KBD_STATUS_REG);
474
475         /*
476          * Ignore mouse events.
477          */
478         if (scanstatus & KBD_STAT_MOUSE_OBF)
479                 return -1;
480
481         /*
482          * Ignore release, trigger on make
483          * (except for shift keys, where we want to
484          *  keep the shift state so long as the key is
485          *  held down).
486          */
487
488         if (((scancode&0x7f) == 0x2a) || ((scancode&0x7f) == 0x36)) {
489                 /*
490                  * Next key may use shift table
491                  */
492                 if ((scancode & 0x80) == 0) {
493                         shift_key=1;
494                 } else {
495                         shift_key=0;
496                 }
497                 return -1;
498         }
499
500         if ((scancode&0x7f) == 0x1d) {
501                 /*
502                  * Left ctrl key
503                  */
504                 if ((scancode & 0x80) == 0) {
505                         ctrl_key = 1;
506                 } else {
507                         ctrl_key = 0;
508                 }
509                 return -1;
510         }
511
512         if ((scancode & 0x80) != 0)
513                 return -1;
514
515         scancode &= 0x7f;
516
517         /*
518          * Translate scancode
519          */
520
521         if (scancode == 0x3a) {
522                 /*
523                  * Toggle caps lock
524                  */
525                 shift_lock ^= 1;
526
527 #ifdef  KDB_BLINK_LED
528                 kdb_toggleled(0x4);
529 #endif
530                 return -1;
531         }
532
533         if (scancode == 0x0e) {
534                 /*
535                  * Backspace
536                  */
537                 return 8;
538         }
539
540         /* Special Key */
541         switch (scancode) {
542         case 0xF: /* Tab */
543                 return 9;
544         case 0x53: /* Del */
545                 return 4;
546         case 0x47: /* Home */
547                 return 1;
548         case 0x4F: /* End */
549                 return 5;
550         case 0x4B: /* Left */
551                 return 2;
552         case 0x48: /* Up */
553                 return 16;
554         case 0x50: /* Down */
555                 return 14;
556         case 0x4D: /* Right */
557                 return 6;
558         }
559
560         if (scancode == 0xe0) {
561                 return -1;
562         }
563
564         /*
565          * For Japanese 86/106 keyboards
566          *      See comment in drivers/char/pc_keyb.c.
567          *      - Masahiro Adegawa
568          */
569         if (scancode == 0x73) {
570                 scancode = 0x59;
571         } else if (scancode == 0x7d) {
572                 scancode = 0x7c;
573         }
574
575         if (!shift_lock && !shift_key && !ctrl_key) {
576                 keychar = plain_map[scancode];
577         } else if (shift_lock || shift_key) {
578                 keychar = shift_map[scancode];
579         } else if (ctrl_key) {
580                 keychar = ctrl_map[scancode];
581         } else {
582                 keychar = 0x0020;
583                 kdb_printf("Unknown state/scancode (%d)\n", scancode);
584         }
585         keychar &= 0x0fff;
586         switch (KTYP(keychar)) {
587         case KT_LETTER:
588         case KT_LATIN:
589                 if (isprint(keychar))
590                         break;          /* printable characters */
591                 /* drop through */
592         case KT_SPEC:
593                 if (keychar == K_ENTER)
594                         break;
595                 /* drop through */
596         default:
597                 return(-1);     /* ignore unprintables */
598         }
599
600         if ((scancode & 0x7f) == 0x1c) {
601                 /*
602                  * enter key.  All done.  Absorb the release scancode.
603                  */
604                 while ((inb(KBD_STATUS_REG) & KBD_STAT_OBF) == 0)
605                         ;
606
607                 /*
608                  * Fetch the scancode
609                  */
610                 scancode = inb(KBD_DATA_REG);
611                 scanstatus = inb(KBD_STATUS_REG);
612
613                 while (scanstatus & KBD_STAT_MOUSE_OBF) {
614                         scancode = inb(KBD_DATA_REG);
615                         scanstatus = inb(KBD_STATUS_REG);
616                 }
617
618                 if (scancode != 0x9c) {
619                         /*
620                          * Wasn't an enter-release,  why not?
621                          */
622                         kdb_printf("kdb: expected enter got 0x%x status 0x%x\n",
623                                scancode, scanstatus);
624                 }
625
626                 kdb_printf("\n");
627                 return 13;
628         }
629
630         return keychar & 0xff;
631 }
632 #endif  /* CONFIG_VT_CONSOLE */
633
634 #ifdef KDB_BLINK_LED
635
636 /* Leave numlock alone, setting it messes up laptop keyboards with the keypad
637  * mapped over normal keys.
638  */
639 static int kdba_blink_mask = 0x1 | 0x4;
640
641 #ifdef CONFIG_SMP
642 #define BOGOMIPS (local_cpu_data->loops_per_jiffy/(500000/HZ))
643 #else
644 #define BOGOMIPS (loops_per_jiffy/(500000/HZ))
645 #endif
646 static int blink_led(void)
647 {
648         static long delay;
649
650         if (kbd_exists == 0)
651                 return -1;
652
653         if (--delay < 0) {
654                 if (BOGOMIPS == 0)      /* early kdb */
655                         delay = 150000000/1000;     /* arbitrary bogomips */
656                 else
657                         delay = 150000000/BOGOMIPS; /* Roughly 1 second when polling */
658                 kdb_toggleled(kdba_blink_mask);
659         }
660         return -1;
661 }
662 #endif
663
664 get_char_func poll_funcs[] = {
665 #if defined(CONFIG_VT_CONSOLE)
666         get_kbd_char,
667 #endif
668 #ifdef  HAVE_KDBA_SERIAL_CONSOLE
669         get_serial_char,
670 #endif
671 #ifdef KDB_BLINK_LED
672         blink_led,
673 #endif
674 #ifdef CONFIG_KDB_USB
675         get_usb_char,
676 #endif
677         NULL
678 };
679
680 /* Dummy versions of kdba_local_arch_setup, kdba_local_arch_cleanup.
681  * FIXME: ia64 with legacy keyboard might need the same code as i386.
682  */
683
684 void kdba_local_arch_setup(void) {}
685 void kdba_local_arch_cleanup(void) {}