blob: ecef6e1a599a5d3ddb560925942302876db31740 [file] [log] [blame]
Jason Wesself2d937f2008-04-17 20:05:37 +02001/*
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>
Jason Wesselada64e42010-05-20 21:04:24 -050017#include <linux/kdb.h>
Jason Wesself2d937f2008-04-17 20:05:37 +020018#include <linux/tty.h>
19
20#define MAX_CONFIG_LEN 40
21
22static struct kgdb_io kgdboc_io_ops;
23
24/* -1 = init not run yet, 0 = unconfigured, 1 = configured. */
25static int configured = -1;
26
27static char config[MAX_CONFIG_LEN];
28static struct kparam_string kps = {
29 .string = config,
30 .maxlen = MAX_CONFIG_LEN,
31};
32
33static struct tty_driver *kgdb_tty_driver;
34static int kgdb_tty_line;
35
Jason Wesselada64e42010-05-20 21:04:24 -050036#ifdef CONFIG_KDB_KEYBOARD
37static 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
52static 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
Jason Wesself2d937f2008-04-17 20:05:37 +020070static 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
Jason Wesselada64e42010-05-20 21:04:24 -050083static void cleanup_kgdboc(void)
84{
85 kgdboc_unregister_kbd();
86 if (configured == 1)
87 kgdb_unregister_io_module(&kgdboc_io_ops);
88}
89
Jason Wesself2d937f2008-04-17 20:05:37 +020090static int configure_kgdboc(void)
91{
92 struct tty_driver *p;
93 int tty_line = 0;
94 int err;
Jason Wesselada64e42010-05-20 21:04:24 -050095 char *cptr = config;
Jason Wesself2d937f2008-04-17 20:05:37 +020096
97 err = kgdboc_option_setup(config);
98 if (err || !strlen(config) || isspace(config[0]))
99 goto noconfig;
100
101 err = -ENODEV;
Jason Wesselada64e42010-05-20 21:04:24 -0500102 kgdb_tty_driver = NULL;
Jason Wesself2d937f2008-04-17 20:05:37 +0200103
Jason Wesselada64e42010-05-20 21:04:24 -0500104 if (kgdboc_register_kbd(&cptr))
105 goto do_register;
106
107 p = tty_find_polling_driver(cptr, &tty_line);
Jason Wesself2d937f2008-04-17 20:05:37 +0200108 if (!p)
109 goto noconfig;
110
111 kgdb_tty_driver = p;
112 kgdb_tty_line = tty_line;
113
Jason Wesselada64e42010-05-20 21:04:24 -0500114do_register:
Jason Wesself2d937f2008-04-17 20:05:37 +0200115 err = kgdb_register_io_module(&kgdboc_io_ops);
116 if (err)
117 goto noconfig;
118
119 configured = 1;
120
121 return 0;
122
123noconfig:
124 config[0] = 0;
125 configured = 0;
Jason Wesselada64e42010-05-20 21:04:24 -0500126 cleanup_kgdboc();
Jason Wesself2d937f2008-04-17 20:05:37 +0200127
128 return err;
129}
130
131static int __init init_kgdboc(void)
132{
133 /* Already configured? */
134 if (configured == 1)
135 return 0;
136
137 return configure_kgdboc();
138}
139
Jason Wesself2d937f2008-04-17 20:05:37 +0200140static int kgdboc_get_char(void)
141{
Jason Wesselada64e42010-05-20 21:04:24 -0500142 if (!kgdb_tty_driver)
143 return -1;
Alan Coxf34d7a52008-04-30 00:54:13 -0700144 return kgdb_tty_driver->ops->poll_get_char(kgdb_tty_driver,
145 kgdb_tty_line);
Jason Wesself2d937f2008-04-17 20:05:37 +0200146}
147
148static void kgdboc_put_char(u8 chr)
149{
Jason Wesselada64e42010-05-20 21:04:24 -0500150 if (!kgdb_tty_driver)
151 return;
Alan Coxf34d7a52008-04-30 00:54:13 -0700152 kgdb_tty_driver->ops->poll_put_char(kgdb_tty_driver,
153 kgdb_tty_line, chr);
Jason Wesself2d937f2008-04-17 20:05:37 +0200154}
155
156static int param_set_kgdboc_var(const char *kmessage, struct kernel_param *kp)
157{
Jason Wesselc191e5a2008-02-15 14:55:52 -0600158 int len = strlen(kmessage);
159
160 if (len >= MAX_CONFIG_LEN) {
Jason Wesself2d937f2008-04-17 20:05:37 +0200161 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);
Jason Wesselc191e5a2008-02-15 14:55:52 -0600179 /* Chop out \n char as a result of echo */
180 if (config[len - 1] == '\n')
181 config[len - 1] = '\0';
Jason Wesself2d937f2008-04-17 20:05:37 +0200182
183 if (configured == 1)
184 cleanup_kgdboc();
185
186 /* Go and configure with the new params. */
187 return configure_kgdboc();
188}
189
190static 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
197static 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
204static 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
212module_init(init_kgdboc);
213module_exit(cleanup_kgdboc);
214module_param_call(kgdboc, param_set_kgdboc_var, param_get_string, &kps, 0644);
215MODULE_PARM_DESC(kgdboc, "<serial_device>[,baud]");
216MODULE_DESCRIPTION("KGDB Console TTY Driver");
217MODULE_LICENSE("GPL");