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 for_each_console(cons
) {
180 if (cons
->device
&& cons
->device(cons
, &idx
) == p
&&
182 kgdboc_io_ops
.is_console
= 1;
188 kgdb_tty_line
= tty_line
;
191 err
= kgdb_register_io_module(&kgdboc_io_ops
);
195 err
= kgdb_register_nmi_console();
204 kgdb_unregister_io_module(&kgdboc_io_ops
);
206 kgdboc_unregister_kbd();
212 static int kgdboc_probe(struct platform_device
*pdev
)
216 mutex_lock(&config_mutex
);
217 if (configured
!= 1) {
218 ret
= configure_kgdboc();
220 /* Convert "no device" to "defer" so we'll keep trying */
224 mutex_unlock(&config_mutex
);
229 static struct platform_driver kgdboc_platform_driver
= {
230 .probe
= kgdboc_probe
,
233 .suppress_bind_attrs
= true,
237 static int __init
init_kgdboc(void)
242 * kgdboc is a little bit of an odd "platform_driver". It can be
243 * up and running long before the platform_driver object is
244 * created and thus doesn't actually store anything in it. There's
245 * only one instance of kgdb so anything is stored as global state.
246 * The platform_driver is only created so that we can leverage the
247 * kernel's mechanisms (like -EPROBE_DEFER) to call us when our
248 * underlying tty is ready. Here we init our platform driver and
249 * then create the single kgdboc instance.
251 ret
= platform_driver_register(&kgdboc_platform_driver
);
255 kgdboc_pdev
= platform_device_alloc("kgdboc", PLATFORM_DEVID_NONE
);
258 goto err_did_register
;
261 ret
= platform_device_add(kgdboc_pdev
);
265 platform_device_put(kgdboc_pdev
);
268 platform_driver_unregister(&kgdboc_platform_driver
);
272 static void exit_kgdboc(void)
274 mutex_lock(&config_mutex
);
276 mutex_unlock(&config_mutex
);
278 platform_device_unregister(kgdboc_pdev
);
279 platform_driver_unregister(&kgdboc_platform_driver
);
282 static int kgdboc_get_char(void)
284 if (!kgdb_tty_driver
)
286 return kgdb_tty_driver
->ops
->poll_get_char(kgdb_tty_driver
,
290 static void kgdboc_put_char(u8 chr
)
292 if (!kgdb_tty_driver
)
294 kgdb_tty_driver
->ops
->poll_put_char(kgdb_tty_driver
,
298 static int param_set_kgdboc_var(const char *kmessage
,
299 const struct kernel_param
*kp
)
301 size_t len
= strlen(kmessage
);
304 if (len
>= MAX_CONFIG_LEN
) {
305 pr_err("config string too long\n");
309 if (kgdb_connected
) {
310 pr_err("Cannot reconfigure while KGDB is connected.\n");
314 mutex_lock(&config_mutex
);
316 strcpy(config
, kmessage
);
317 /* Chop out \n char as a result of echo */
318 if (len
&& config
[len
- 1] == '\n')
319 config
[len
- 1] = '\0';
325 * Configure with the new params as long as init already ran.
326 * Note that we can get called before init if someone loads us
327 * with "modprobe kgdboc kgdboc=..." or if they happen to use the
328 * the odd syntax of "kgdboc.kgdboc=..." on the kernel command.
331 ret
= configure_kgdboc();
334 * If we couldn't configure then clear out the config. Note that
335 * specifying an invalid config on the kernel command line vs.
336 * through sysfs have slightly different behaviors. If we fail
337 * to configure what was specified on the kernel command line
338 * we'll leave it in the 'config' and return -EPROBE_DEFER from
339 * our probe. When specified through sysfs userspace is
340 * responsible for loading the tty driver before setting up.
345 mutex_unlock(&config_mutex
);
350 static int dbg_restore_graphics
;
352 static void kgdboc_pre_exp_handler(void)
354 if (!dbg_restore_graphics
&& kgdboc_use_kms
) {
355 dbg_restore_graphics
= 1;
356 con_debug_enter(vc_cons
[fg_console
].d
);
358 /* Increment the module count when the debugger is active */
360 try_module_get(THIS_MODULE
);
362 atomic_inc(&ignore_console_lock_warning
);
365 static void kgdboc_post_exp_handler(void)
367 atomic_dec(&ignore_console_lock_warning
);
369 /* decrement the module count when the debugger detaches */
371 module_put(THIS_MODULE
);
372 if (kgdboc_use_kms
&& dbg_restore_graphics
) {
373 dbg_restore_graphics
= 0;
376 kgdboc_restore_input();
379 static struct kgdb_io kgdboc_io_ops
= {
381 .read_char
= kgdboc_get_char
,
382 .write_char
= kgdboc_put_char
,
383 .pre_exception
= kgdboc_pre_exp_handler
,
384 .post_exception
= kgdboc_post_exp_handler
,
387 #ifdef CONFIG_KGDB_SERIAL_CONSOLE
388 static int kgdboc_option_setup(char *opt
)
391 pr_err("config string not provided\n");
395 if (strlen(opt
) >= MAX_CONFIG_LEN
) {
396 pr_err("config string too long\n");
404 __setup("kgdboc=", kgdboc_option_setup
);
407 /* This is only available if kgdboc is a built in for early debugging */
408 static int __init
kgdboc_early_init(char *opt
)
410 kgdboc_option_setup(opt
);
415 early_param("ekgdboc", kgdboc_early_init
);
416 #endif /* CONFIG_KGDB_SERIAL_CONSOLE */
418 module_init(init_kgdboc
);
419 module_exit(exit_kgdboc
);
420 module_param_call(kgdboc
, param_set_kgdboc_var
, param_get_string
, &kps
, 0644);
421 MODULE_PARM_DESC(kgdboc
, "<serial_device>[,baud]");
422 MODULE_DESCRIPTION("KGDB Console TTY Driver");
423 MODULE_LICENSE("GPL");