Adrian Bunk | 88278ca | 2008-05-19 16:53:02 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * console.c: Routines that deal with sending and receiving IO |
| 3 | * to/from the current console device using the PROM. |
| 4 | * |
| 5 | * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu) |
| 6 | * Copyright (C) 1998 Pete Zaitcev <zaitcev@yahoo.com> |
| 7 | */ |
| 8 | |
| 9 | #include <linux/types.h> |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/sched.h> |
| 12 | #include <asm/openprom.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <asm/oplib.h> |
| 14 | #include <asm/system.h> |
| 15 | #include <linux/string.h> |
| 16 | |
| 17 | extern void restore_current(void); |
| 18 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | /* Non blocking get character from console input device, returns -1 |
| 20 | * if no input was taken. This can be used for polling. |
| 21 | */ |
David S. Miller | e62cac1 | 2010-11-30 14:33:29 -0800 | [diff] [blame^] | 22 | static int prom_nbgetchar(char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | unsigned long flags; |
David S. Miller | e62cac1 | 2010-11-30 14:33:29 -0800 | [diff] [blame^] | 25 | int i = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 27 | spin_lock_irqsave(&prom_lock, flags); |
| 28 | switch(prom_vers) { |
| 29 | case PROM_V0: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | i = (*(romvec->pv_nbgetchar))(); |
David S. Miller | e62cac1 | 2010-11-30 14:33:29 -0800 | [diff] [blame^] | 31 | if (i != -1) { |
| 32 | *buf = i; |
| 33 | i = 0; |
| 34 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | break; |
| 36 | case PROM_V2: |
| 37 | case PROM_V3: |
David S. Miller | e62cac1 | 2010-11-30 14:33:29 -0800 | [diff] [blame^] | 38 | if ((*(romvec->pv_v2devops).v2_dev_read)(*romvec->pv_v2bootargs.fd_stdin, |
| 39 | buf, 0x1) == 1) |
| 40 | i = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | break; |
| 42 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | break; |
| 44 | }; |
| 45 | restore_current(); |
| 46 | spin_unlock_irqrestore(&prom_lock, flags); |
| 47 | return i; /* Ugh, we could spin forever on unsupported proms ;( */ |
| 48 | } |
| 49 | |
| 50 | /* Non blocking put character to console device, returns -1 if |
| 51 | * unsuccessful. |
| 52 | */ |
David S. Miller | e62cac1 | 2010-11-30 14:33:29 -0800 | [diff] [blame^] | 53 | static int prom_nbputchar(const char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | unsigned long flags; |
| 56 | int i = -1; |
| 57 | |
| 58 | spin_lock_irqsave(&prom_lock, flags); |
| 59 | switch(prom_vers) { |
| 60 | case PROM_V0: |
David S. Miller | e62cac1 | 2010-11-30 14:33:29 -0800 | [diff] [blame^] | 61 | i = (*(romvec->pv_nbputchar))(*buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | break; |
| 63 | case PROM_V2: |
| 64 | case PROM_V3: |
David S. Miller | e62cac1 | 2010-11-30 14:33:29 -0800 | [diff] [blame^] | 65 | if ((*(romvec->pv_v2devops).v2_dev_write)(*romvec->pv_v2bootargs.fd_stdout, |
| 66 | buf, 0x1) == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | i = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | break; |
| 69 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | break; |
| 71 | }; |
| 72 | restore_current(); |
| 73 | spin_unlock_irqrestore(&prom_lock, flags); |
| 74 | return i; /* Ugh, we could spin forever on unsupported proms ;( */ |
| 75 | } |
| 76 | |
| 77 | /* Blocking version of get character routine above. */ |
David S. Miller | e62cac1 | 2010-11-30 14:33:29 -0800 | [diff] [blame^] | 78 | void prom_getchar(char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | { |
David S. Miller | e62cac1 | 2010-11-30 14:33:29 -0800 | [diff] [blame^] | 80 | while (1) { |
| 81 | int err = prom_nbgetchar(buf); |
| 82 | if (!err) |
| 83 | break; |
| 84 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /* Blocking version of put character routine above. */ |
David S. Miller | e62cac1 | 2010-11-30 14:33:29 -0800 | [diff] [blame^] | 88 | void prom_putchar(const char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | { |
David S. Miller | e62cac1 | 2010-11-30 14:33:29 -0800 | [diff] [blame^] | 90 | while (1) { |
| 91 | int err = prom_nbputchar(buf); |
| 92 | if (!err) |
| 93 | break; |
| 94 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | } |