Merge branch 'upstream'
[pandora-kernel.git] / drivers / char / hvc_rtas.c
1 /*
2  * IBM RTAS driver interface to hvc_console.c
3  *
4  * (C) Copyright IBM Corporation 2001-2005
5  * (C) Copyright Red Hat, Inc. 2005
6  *
7  * Author(s): Maximino Augilar <IBM STI Design Center>
8  *          : Ryan S. Arnold <rsa@us.ibm.com>
9  *          : Utz Bacher <utz.bacher@de.ibm.com>
10  *          : David Woodhouse <dwmw2@infradead.org>
11  *
12  *    inspired by drivers/char/hvc_console.c
13  *    written by Anton Blanchard and Paul Mackerras
14  *
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 2 of the License, or
18  * (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
28  */
29
30 #include <linux/console.h>
31 #include <linux/delay.h>
32 #include <linux/err.h>
33 #include <linux/init.h>
34 #include <linux/moduleparam.h>
35 #include <linux/types.h>
36
37 #include <asm/irq.h>
38 #include <asm/rtas.h>
39 #include "hvc_console.h"
40
41 #define hvc_rtas_cookie 0x67781e15
42 struct hvc_struct *hvc_rtas_dev;
43
44 #define RTASCONS_PUT_ATTEMPTS  16
45
46 static int rtascons_put_char_token = RTAS_UNKNOWN_SERVICE;
47 static int rtascons_get_char_token = RTAS_UNKNOWN_SERVICE;
48 static int rtascons_put_delay = 100;
49 module_param_named(put_delay, rtascons_put_delay, int, 0644);
50
51 static inline int hvc_rtas_write_console(uint32_t vtermno, const char *buf, int count)
52 {
53         int done;
54
55         /* if there is more than one character to be displayed, wait a bit */
56         for (done = 0; done < count; done++) {
57                 int result;
58                 result = rtas_call(rtascons_put_char_token, 1, 1, NULL, buf[done]);
59                 if (result)
60                         break;
61         }
62         /* the calling routine expects to receive the number of bytes sent */
63         return done;
64 }
65
66 static int hvc_rtas_read_console(uint32_t vtermno, char *buf, int count)
67 {
68         int i;
69
70         for (i = 0; i < count; i++) {
71                 int c, err;
72
73                 err = rtas_call(rtascons_get_char_token, 0, 2, &c);
74                 if (err)
75                         break;
76
77                 buf[i] = c;
78         }
79
80         return i;
81 }
82
83 static struct hv_ops hvc_rtas_get_put_ops = {
84         .get_chars = hvc_rtas_read_console,
85         .put_chars = hvc_rtas_write_console,
86 };
87
88 static int hvc_rtas_init(void)
89 {
90         struct hvc_struct *hp;
91
92         if (rtascons_put_char_token == RTAS_UNKNOWN_SERVICE)
93                 rtascons_put_char_token = rtas_token("put-term-char");
94         if (rtascons_put_char_token == RTAS_UNKNOWN_SERVICE)
95                 return -EIO;
96
97         if (rtascons_get_char_token == RTAS_UNKNOWN_SERVICE)
98                 rtascons_get_char_token = rtas_token("get-term-char");
99         if (rtascons_get_char_token == RTAS_UNKNOWN_SERVICE)
100                 return -EIO;
101
102         BUG_ON(hvc_rtas_dev);
103
104         /* Allocate an hvc_struct for the console device we instantiated
105          * earlier.  Save off hp so that we can return it on exit */
106         hp = hvc_alloc(hvc_rtas_cookie, NO_IRQ, &hvc_rtas_get_put_ops);
107         if (IS_ERR(hp))
108                 return PTR_ERR(hp);
109         hvc_rtas_dev = hp;
110         return 0;
111 }
112 module_init(hvc_rtas_init);
113
114 /* This will tear down the tty portion of the driver */
115 static void __exit hvc_rtas_exit(void)
116 {
117         /* Really the fun isn't over until the worker thread breaks down and the
118          * tty cleans up */
119         if (hvc_rtas_dev)
120                 hvc_remove(hvc_rtas_dev);
121 }
122 module_exit(hvc_rtas_exit);
123
124 /* This will happen prior to module init.  There is no tty at this time? */
125 static int hvc_rtas_console_init(void)
126 {
127         rtascons_put_char_token = rtas_token("put-term-char");
128         if (rtascons_put_char_token == RTAS_UNKNOWN_SERVICE)
129                 return -EIO;
130         rtascons_get_char_token = rtas_token("get-term-char");
131         if (rtascons_get_char_token == RTAS_UNKNOWN_SERVICE)
132                 return -EIO;
133
134         hvc_instantiate(hvc_rtas_cookie, 0, &hvc_rtas_get_put_ops );
135         add_preferred_console("hvc", 0, NULL);
136         return 0;
137 }
138 console_initcall(hvc_rtas_console_init);