ecef6e1a599a5d3ddb560925942302876db31740
[sfrench/cifs-2.6.git] / drivers / serial / kgdboc.c
1 /*
2  * Based on the same principle as kgdboe using the NETPOLL api, this
3  * driver uses a console polling api to implement a gdb serial inteface
4  * which is multiplexed on a console port.
5  *
6  * Maintainer: Jason Wessel <jason.wessel@windriver.com>
7  *
8  * 2007-2008 (c) Jason Wessel - Wind River Systems, Inc.
9  *
10  * This file is licensed under the terms of the GNU General Public
11  * License version 2. This program is licensed "as is" without any
12  * warranty of any kind, whether express or implied.
13  */
14 #include <linux/kernel.h>
15 #include <linux/ctype.h>
16 #include <linux/kgdb.h>
17 #include <linux/kdb.h>
18 #include <linux/tty.h>
19
20 #define MAX_CONFIG_LEN          40
21
22 static struct kgdb_io           kgdboc_io_ops;
23
24 /* -1 = init not run yet, 0 = unconfigured, 1 = configured. */
25 static int configured           = -1;
26
27 static char config[MAX_CONFIG_LEN];
28 static struct kparam_string kps = {
29         .string                 = config,
30         .maxlen                 = MAX_CONFIG_LEN,
31 };
32
33 static struct tty_driver        *kgdb_tty_driver;
34 static int                      kgdb_tty_line;
35
36 #ifdef CONFIG_KDB_KEYBOARD
37 static int kgdboc_register_kbd(char **cptr)
38 {
39         if (strncmp(*cptr, "kbd", 3) == 0) {
40                 if (kdb_poll_idx < KDB_POLL_FUNC_MAX) {
41                         kdb_poll_funcs[kdb_poll_idx] = kdb_get_kbd_char;
42                         kdb_poll_idx++;
43                         if (cptr[0][3] == ',')
44                                 *cptr += 4;
45                         else
46                                 return 1;
47                 }
48         }
49         return 0;
50 }
51
52 static void kgdboc_unregister_kbd(void)
53 {
54         int i;
55
56         for (i = 0; i < kdb_poll_idx; i++) {
57                 if (kdb_poll_funcs[i] == kdb_get_kbd_char) {
58                         kdb_poll_idx--;
59                         kdb_poll_funcs[i] = kdb_poll_funcs[kdb_poll_idx];
60                         kdb_poll_funcs[kdb_poll_idx] = NULL;
61                         i--;
62                 }
63         }
64 }
65 #else /* ! CONFIG_KDB_KEYBOARD */
66 #define kgdboc_register_kbd(x) 0
67 #define kgdboc_unregister_kbd()
68 #endif /* ! CONFIG_KDB_KEYBOARD */
69
70 static int kgdboc_option_setup(char *opt)
71 {
72         if (strlen(opt) > MAX_CONFIG_LEN) {
73                 printk(KERN_ERR "kgdboc: config string too long\n");
74                 return -ENOSPC;
75         }
76         strcpy(config, opt);
77
78         return 0;
79 }
80
81 __setup("kgdboc=", kgdboc_option_setup);
82
83 static void cleanup_kgdboc(void)
84 {
85         kgdboc_unregister_kbd();
86         if (configured == 1)
87                 kgdb_unregister_io_module(&kgdboc_io_ops);
88 }
89
90 static int configure_kgdboc(void)
91 {
92         struct tty_driver *p;
93         int tty_line = 0;
94         int err;
95         char *cptr = config;
96
97         err = kgdboc_option_setup(config);
98         if (err || !strlen(config) || isspace(config[0]))
99                 goto noconfig;
100
101         err = -ENODEV;
102         kgdb_tty_driver = NULL;
103
104         if (kgdboc_register_kbd(&cptr))
105                 goto do_register;
106
107         p = tty_find_polling_driver(cptr, &tty_line);
108         if (!p)
109                 goto noconfig;
110
111         kgdb_tty_driver = p;
112         kgdb_tty_line = tty_line;
113
114 do_register:
115         err = kgdb_register_io_module(&kgdboc_io_ops);
116         if (err)
117                 goto noconfig;
118
119         configured = 1;
120
121         return 0;
122
123 noconfig:
124         config[0] = 0;
125         configured = 0;
126         cleanup_kgdboc();
127
128         return err;
129 }
130
131 static int __init init_kgdboc(void)
132 {
133         /* Already configured? */
134         if (configured == 1)
135                 return 0;
136
137         return configure_kgdboc();
138 }
139
140 static int kgdboc_get_char(void)
141 {
142         if (!kgdb_tty_driver)
143                 return -1;
144         return kgdb_tty_driver->ops->poll_get_char(kgdb_tty_driver,
145                                                 kgdb_tty_line);
146 }
147
148 static void kgdboc_put_char(u8 chr)
149 {
150         if (!kgdb_tty_driver)
151                 return;
152         kgdb_tty_driver->ops->poll_put_char(kgdb_tty_driver,
153                                         kgdb_tty_line, chr);
154 }
155
156 static int param_set_kgdboc_var(const char *kmessage, struct kernel_param *kp)
157 {
158         int len = strlen(kmessage);
159
160         if (len >= MAX_CONFIG_LEN) {
161                 printk(KERN_ERR "kgdboc: config string too long\n");
162                 return -ENOSPC;
163         }
164
165         /* Only copy in the string if the init function has not run yet */
166         if (configured < 0) {
167                 strcpy(config, kmessage);
168                 return 0;
169         }
170
171         if (kgdb_connected) {
172                 printk(KERN_ERR
173                        "kgdboc: Cannot reconfigure while KGDB is connected.\n");
174
175                 return -EBUSY;
176         }
177
178         strcpy(config, kmessage);
179         /* Chop out \n char as a result of echo */
180         if (config[len - 1] == '\n')
181                 config[len - 1] = '\0';
182
183         if (configured == 1)
184                 cleanup_kgdboc();
185
186         /* Go and configure with the new params. */
187         return configure_kgdboc();
188 }
189
190 static void kgdboc_pre_exp_handler(void)
191 {
192         /* Increment the module count when the debugger is active */
193         if (!kgdb_connected)
194                 try_module_get(THIS_MODULE);
195 }
196
197 static void kgdboc_post_exp_handler(void)
198 {
199         /* decrement the module count when the debugger detaches */
200         if (!kgdb_connected)
201                 module_put(THIS_MODULE);
202 }
203
204 static struct kgdb_io kgdboc_io_ops = {
205         .name                   = "kgdboc",
206         .read_char              = kgdboc_get_char,
207         .write_char             = kgdboc_put_char,
208         .pre_exception          = kgdboc_pre_exp_handler,
209         .post_exception         = kgdboc_post_exp_handler,
210 };
211
212 module_init(init_kgdboc);
213 module_exit(cleanup_kgdboc);
214 module_param_call(kgdboc, param_set_kgdboc_var, param_get_string, &kps, 0644);
215 MODULE_PARM_DESC(kgdboc, "<serial_device>[,baud]");
216 MODULE_DESCRIPTION("KGDB Console TTY Driver");
217 MODULE_LICENSE("GPL");