1 // SPDX-License-Identifier: GPL-2.0
3 * Based on the same principle as kgdboe using the NETPOLL api, this
4 * driver uses a console polling api to implement a gdb serial inteface
5 * which is multiplexed on a console port.
7 * Maintainer: Jason Wessel <jason.wessel@windriver.com>
9 * 2007-2008 (c) Jason Wessel - Wind River Systems, Inc.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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 #include <linux/console.h>
20 #include <linux/vt_kern.h>
21 #include <linux/input.h>
22 #include <linux/module.h>
23 #include <linux/platform_device.h>
25 #define MAX_CONFIG_LEN 40
27 static struct kgdb_io kgdboc_io_ops;
29 /* -1 = init not run yet, 0 = unconfigured, 1 = configured. */
30 static int configured = -1;
31 static DEFINE_MUTEX(config_mutex);
33 static char config[MAX_CONFIG_LEN];
34 static struct kparam_string kps = {
36 .maxlen = MAX_CONFIG_LEN,
39 static int kgdboc_use_kms; /* 1 if we use kernel mode switching */
40 static struct tty_driver *kgdb_tty_driver;
41 static int kgdb_tty_line;
43 static struct platform_device *kgdboc_pdev;
45 #ifdef CONFIG_KDB_KEYBOARD
46 static int kgdboc_reset_connect(struct input_handler *handler,
47 struct input_dev *dev,
48 const struct input_device_id *id)
50 input_reset_device(dev);
52 /* Return an error - we do not want to bind, just to reset */
56 static void kgdboc_reset_disconnect(struct input_handle *handle)
58 /* We do not expect anyone to actually bind to us */
62 static const struct input_device_id kgdboc_reset_ids[] = {
64 .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
65 .evbit = { BIT_MASK(EV_KEY) },
70 static struct input_handler kgdboc_reset_handler = {
71 .connect = kgdboc_reset_connect,
72 .disconnect = kgdboc_reset_disconnect,
73 .name = "kgdboc_reset",
74 .id_table = kgdboc_reset_ids,
77 static DEFINE_MUTEX(kgdboc_reset_mutex);
79 static void kgdboc_restore_input_helper(struct work_struct *dummy)
82 * We need to take a mutex to prevent several instances of
83 * this work running on different CPUs so they don't try
84 * to register again already registered handler.
86 mutex_lock(&kgdboc_reset_mutex);
88 if (input_register_handler(&kgdboc_reset_handler) == 0)
89 input_unregister_handler(&kgdboc_reset_handler);
91 mutex_unlock(&kgdboc_reset_mutex);
94 static DECLARE_WORK(kgdboc_restore_input_work, kgdboc_restore_input_helper);
96 static void kgdboc_restore_input(void)
98 if (likely(system_state == SYSTEM_RUNNING))
99 schedule_work(&kgdboc_restore_input_work);
102 static int kgdboc_register_kbd(char **cptr)
104 if (strncmp(*cptr, "kbd", 3) == 0 ||
105 strncmp(*cptr, "kdb", 3) == 0) {
106 if (kdb_poll_idx < KDB_POLL_FUNC_MAX) {
107 kdb_poll_funcs[kdb_poll_idx] = kdb_get_kbd_char;
109 if (cptr[0][3] == ',')
118 static void kgdboc_unregister_kbd(void)
122 for (i = 0; i < kdb_poll_idx; i++) {
123 if (kdb_poll_funcs[i] == kdb_get_kbd_char) {
125 kdb_poll_funcs[i] = kdb_poll_funcs[kdb_poll_idx];
126 kdb_poll_funcs[kdb_poll_idx] = NULL;
130 flush_work(&kgdboc_restore_input_work);
132 #else /* ! CONFIG_KDB_KEYBOARD */
133 #define kgdboc_register_kbd(x) 0
134 #define kgdboc_unregister_kbd()
135 #define kgdboc_restore_input()
136 #endif /* ! CONFIG_KDB_KEYBOARD */
138 static void cleanup_kgdboc(void)
143 if (kgdb_unregister_nmi_console())
145 kgdboc_unregister_kbd();
146 kgdb_unregister_io_module(&kgdboc_io_ops);
149 static int configure_kgdboc(void)
151 struct tty_driver *p;
155 struct console *cons;
157 if (!strlen(config) || isspace(config[0])) {
162 kgdboc_io_ops.is_console = 0;
163 kgdb_tty_driver = NULL;
166 if (strncmp(cptr, "kms,", 4) == 0) {
171 if (kgdboc_register_kbd(&cptr))
174 p = tty_find_polling_driver(cptr, &tty_line);
178 cons = console_drivers;
181 if (cons->device && cons->device(cons, &idx) == p &&
183 kgdboc_io_ops.is_console = 1;
190 kgdb_tty_line = tty_line;
193 err = kgdb_register_io_module(&kgdboc_io_ops);
197 err = kgdb_register_nmi_console();
206 kgdb_unregister_io_module(&kgdboc_io_ops);
208 kgdboc_unregister_kbd();
214 static int kgdboc_probe(struct platform_device *pdev)
218 mutex_lock(&config_mutex);
219 if (configured != 1) {
220 ret = configure_kgdboc();
222 /* Convert "no device" to "defer" so we'll keep trying */
226 mutex_unlock(&config_mutex);
231 static struct platform_driver kgdboc_platform_driver = {
232 .probe = kgdboc_probe,
235 .suppress_bind_attrs = true,
239 static int __init init_kgdboc(void)
244 * kgdboc is a little bit of an odd "platform_driver". It can be
245 * up and running long before the platform_driver object is
246 * created and thus doesn't actually store anything in it. There's
247 * only one instance of kgdb so anything is stored as global state.
248 * The platform_driver is only created so that we can leverage the
249 * kernel's mechanisms (like -EPROBE_DEFER) to call us when our
250 * underlying tty is ready. Here we init our platform driver and
251 * then create the single kgdboc instance.
253 ret = platform_driver_register(&kgdboc_platform_driver);
257 kgdboc_pdev = platform_device_alloc("kgdboc", PLATFORM_DEVID_NONE);
260 goto err_did_register;
263 ret = platform_device_add(kgdboc_pdev);
267 platform_device_put(kgdboc_pdev);
270 platform_driver_unregister(&kgdboc_platform_driver);
274 static void exit_kgdboc(void)
276 mutex_lock(&config_mutex);
278 mutex_unlock(&config_mutex);
280 platform_device_unregister(kgdboc_pdev);
281 platform_driver_unregister(&kgdboc_platform_driver);
284 static int kgdboc_get_char(void)
286 if (!kgdb_tty_driver)
288 return kgdb_tty_driver->ops->poll_get_char(kgdb_tty_driver,
292 static void kgdboc_put_char(u8 chr)
294 if (!kgdb_tty_driver)
296 kgdb_tty_driver->ops->poll_put_char(kgdb_tty_driver,
300 static int param_set_kgdboc_var(const char *kmessage,
301 const struct kernel_param *kp)
303 size_t len = strlen(kmessage);
306 if (len >= MAX_CONFIG_LEN) {
307 pr_err("config string too long\n");
311 if (kgdb_connected) {
312 pr_err("Cannot reconfigure while KGDB is connected.\n");
316 mutex_lock(&config_mutex);
318 strcpy(config, kmessage);
319 /* Chop out \n char as a result of echo */
320 if (len && config[len - 1] == '\n')
321 config[len - 1] = '\0';
327 * Configure with the new params as long as init already ran.
328 * Note that we can get called before init if someone loads us
329 * with "modprobe kgdboc kgdboc=..." or if they happen to use the
330 * the odd syntax of "kgdboc.kgdboc=..." on the kernel command.
333 ret = configure_kgdboc();
336 * If we couldn't configure then clear out the config. Note that
337 * specifying an invalid config on the kernel command line vs.
338 * through sysfs have slightly different behaviors. If we fail
339 * to configure what was specified on the kernel command line
340 * we'll leave it in the 'config' and return -EPROBE_DEFER from
341 * our probe. When specified through sysfs userspace is
342 * responsible for loading the tty driver before setting up.
347 mutex_unlock(&config_mutex);
352 static int dbg_restore_graphics;
354 static void kgdboc_pre_exp_handler(void)
356 if (!dbg_restore_graphics && kgdboc_use_kms) {
357 dbg_restore_graphics = 1;
358 con_debug_enter(vc_cons[fg_console].d);
360 /* Increment the module count when the debugger is active */
362 try_module_get(THIS_MODULE);
364 atomic_inc(&ignore_console_lock_warning);
367 static void kgdboc_post_exp_handler(void)
369 atomic_dec(&ignore_console_lock_warning);
371 /* decrement the module count when the debugger detaches */
373 module_put(THIS_MODULE);
374 if (kgdboc_use_kms && dbg_restore_graphics) {
375 dbg_restore_graphics = 0;
378 kgdboc_restore_input();
381 static struct kgdb_io kgdboc_io_ops = {
383 .read_char = kgdboc_get_char,
384 .write_char = kgdboc_put_char,
385 .pre_exception = kgdboc_pre_exp_handler,
386 .post_exception = kgdboc_post_exp_handler,
389 #ifdef CONFIG_KGDB_SERIAL_CONSOLE
390 static int kgdboc_option_setup(char *opt)
393 pr_err("config string not provided\n");
397 if (strlen(opt) >= MAX_CONFIG_LEN) {
398 pr_err("config string too long\n");
406 __setup("kgdboc=", kgdboc_option_setup);
409 /* This is only available if kgdboc is a built in for early debugging */
410 static int __init kgdboc_early_init(char *opt)
412 kgdboc_option_setup(opt);
417 early_param("ekgdboc", kgdboc_early_init);
418 #endif /* CONFIG_KGDB_SERIAL_CONSOLE */
420 module_init(init_kgdboc);
421 module_exit(exit_kgdboc);
422 module_param_call(kgdboc, param_set_kgdboc_var, param_get_string, &kps, 0644);
423 MODULE_PARM_DESC(kgdboc, "<serial_device>[,baud]");
424 MODULE_DESCRIPTION("KGDB Console TTY Driver");
425 MODULE_LICENSE("GPL");