blob: 69c16bbf1f48612132fdbf83cdbdb95bb48d69d4 [file] [log] [blame]
Adrian Bunk88278ca2008-05-19 16:53:02 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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 Torvalds1da177e2005-04-16 15:20:36 -070013#include <asm/oplib.h>
14#include <asm/system.h>
15#include <linux/string.h>
16
17extern void restore_current(void);
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019/* 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. Millere62cac12010-11-30 14:33:29 -080022static int prom_nbgetchar(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070023{
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 unsigned long flags;
David S. Millere62cac12010-11-30 14:33:29 -080025 int i = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27 spin_lock_irqsave(&prom_lock, flags);
28 switch(prom_vers) {
29 case PROM_V0:
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 i = (*(romvec->pv_nbgetchar))();
David S. Millere62cac12010-11-30 14:33:29 -080031 if (i != -1) {
32 *buf = i;
33 i = 0;
34 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 break;
36 case PROM_V2:
37 case PROM_V3:
David S. Millere62cac12010-11-30 14:33:29 -080038 if ((*(romvec->pv_v2devops).v2_dev_read)(*romvec->pv_v2bootargs.fd_stdin,
39 buf, 0x1) == 1)
40 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 break;
42 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 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. Millere62cac12010-11-30 14:33:29 -080053static int prom_nbputchar(const char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 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. Millere62cac12010-11-30 14:33:29 -080061 i = (*(romvec->pv_nbputchar))(*buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 break;
63 case PROM_V2:
64 case PROM_V3:
David S. Millere62cac12010-11-30 14:33:29 -080065 if ((*(romvec->pv_v2devops).v2_dev_write)(*romvec->pv_v2bootargs.fd_stdout,
66 buf, 0x1) == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 break;
69 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 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. Millere62cac12010-11-30 14:33:29 -080078void prom_getchar(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
David S. Millere62cac12010-11-30 14:33:29 -080080 while (1) {
81 int err = prom_nbgetchar(buf);
82 if (!err)
83 break;
84 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
87/* Blocking version of put character routine above. */
David S. Millere62cac12010-11-30 14:33:29 -080088void prom_putchar(const char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
David S. Millere62cac12010-11-30 14:33:29 -080090 while (1) {
91 int err = prom_nbputchar(buf);
92 if (!err)
93 break;
94 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}