Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / tty / serial / kgdboc.c
blob49d0c7f2b29b8f927915327025a84a555a73a7bd
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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>
24 #include <linux/serial_core.h>
26 #define MAX_CONFIG_LEN 40
28 static struct kgdb_io kgdboc_io_ops;
30 /* -1 = init not run yet, 0 = unconfigured, 1 = configured. */
31 static int configured = -1;
32 static DEFINE_MUTEX(config_mutex);
34 static char config[MAX_CONFIG_LEN];
35 static struct kparam_string kps = {
36 .string = config,
37 .maxlen = MAX_CONFIG_LEN,
40 static int kgdboc_use_kms; /* 1 if we use kernel mode switching */
41 static struct tty_driver *kgdb_tty_driver;
42 static int kgdb_tty_line;
44 static struct platform_device *kgdboc_pdev;
46 #if IS_BUILTIN(CONFIG_KGDB_SERIAL_CONSOLE)
47 static struct kgdb_io kgdboc_earlycon_io_ops;
48 static int (*earlycon_orig_exit)(struct console *con);
49 #endif /* IS_BUILTIN(CONFIG_KGDB_SERIAL_CONSOLE) */
51 #ifdef CONFIG_KDB_KEYBOARD
52 static int kgdboc_reset_connect(struct input_handler *handler,
53 struct input_dev *dev,
54 const struct input_device_id *id)
56 input_reset_device(dev);
58 /* Return an error - we do not want to bind, just to reset */
59 return -ENODEV;
62 static void kgdboc_reset_disconnect(struct input_handle *handle)
64 /* We do not expect anyone to actually bind to us */
65 BUG();
68 static const struct input_device_id kgdboc_reset_ids[] = {
70 .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
71 .evbit = { BIT_MASK(EV_KEY) },
73 { }
76 static struct input_handler kgdboc_reset_handler = {
77 .connect = kgdboc_reset_connect,
78 .disconnect = kgdboc_reset_disconnect,
79 .name = "kgdboc_reset",
80 .id_table = kgdboc_reset_ids,
83 static DEFINE_MUTEX(kgdboc_reset_mutex);
85 static void kgdboc_restore_input_helper(struct work_struct *dummy)
88 * We need to take a mutex to prevent several instances of
89 * this work running on different CPUs so they don't try
90 * to register again already registered handler.
92 mutex_lock(&kgdboc_reset_mutex);
94 if (input_register_handler(&kgdboc_reset_handler) == 0)
95 input_unregister_handler(&kgdboc_reset_handler);
97 mutex_unlock(&kgdboc_reset_mutex);
100 static DECLARE_WORK(kgdboc_restore_input_work, kgdboc_restore_input_helper);
102 static void kgdboc_restore_input(void)
104 if (likely(system_state == SYSTEM_RUNNING))
105 schedule_work(&kgdboc_restore_input_work);
108 static int kgdboc_register_kbd(char **cptr)
110 if (strncmp(*cptr, "kbd", 3) == 0 ||
111 strncmp(*cptr, "kdb", 3) == 0) {
112 if (kdb_poll_idx < KDB_POLL_FUNC_MAX) {
113 kdb_poll_funcs[kdb_poll_idx] = kdb_get_kbd_char;
114 kdb_poll_idx++;
115 if (cptr[0][3] == ',')
116 *cptr += 4;
117 else
118 return 1;
121 return 0;
124 static void kgdboc_unregister_kbd(void)
126 int i;
128 for (i = 0; i < kdb_poll_idx; i++) {
129 if (kdb_poll_funcs[i] == kdb_get_kbd_char) {
130 kdb_poll_idx--;
131 kdb_poll_funcs[i] = kdb_poll_funcs[kdb_poll_idx];
132 kdb_poll_funcs[kdb_poll_idx] = NULL;
133 i--;
136 flush_work(&kgdboc_restore_input_work);
138 #else /* ! CONFIG_KDB_KEYBOARD */
139 #define kgdboc_register_kbd(x) 0
140 #define kgdboc_unregister_kbd()
141 #define kgdboc_restore_input()
142 #endif /* ! CONFIG_KDB_KEYBOARD */
144 #if IS_BUILTIN(CONFIG_KGDB_SERIAL_CONSOLE)
145 static void cleanup_earlycon(void)
147 if (kgdboc_earlycon_io_ops.cons)
148 kgdb_unregister_io_module(&kgdboc_earlycon_io_ops);
150 #else /* !IS_BUILTIN(CONFIG_KGDB_SERIAL_CONSOLE) */
151 static inline void cleanup_earlycon(void) { }
152 #endif /* !IS_BUILTIN(CONFIG_KGDB_SERIAL_CONSOLE) */
154 static void cleanup_kgdboc(void)
156 cleanup_earlycon();
158 if (configured != 1)
159 return;
161 if (kgdb_unregister_nmi_console())
162 return;
163 kgdboc_unregister_kbd();
164 kgdb_unregister_io_module(&kgdboc_io_ops);
167 static int configure_kgdboc(void)
169 struct tty_driver *p;
170 int tty_line = 0;
171 int err = -ENODEV;
172 char *cptr = config;
173 struct console *cons;
175 if (!strlen(config) || isspace(config[0])) {
176 err = 0;
177 goto noconfig;
180 kgdboc_io_ops.cons = NULL;
181 kgdb_tty_driver = NULL;
183 kgdboc_use_kms = 0;
184 if (strncmp(cptr, "kms,", 4) == 0) {
185 cptr += 4;
186 kgdboc_use_kms = 1;
189 if (kgdboc_register_kbd(&cptr))
190 goto do_register;
192 p = tty_find_polling_driver(cptr, &tty_line);
193 if (!p)
194 goto noconfig;
196 for_each_console(cons) {
197 int idx;
198 if (cons->device && cons->device(cons, &idx) == p &&
199 idx == tty_line) {
200 kgdboc_io_ops.cons = cons;
201 break;
205 kgdb_tty_driver = p;
206 kgdb_tty_line = tty_line;
208 do_register:
209 err = kgdb_register_io_module(&kgdboc_io_ops);
210 if (err)
211 goto noconfig;
213 err = kgdb_register_nmi_console();
214 if (err)
215 goto nmi_con_failed;
217 configured = 1;
219 return 0;
221 nmi_con_failed:
222 kgdb_unregister_io_module(&kgdboc_io_ops);
223 noconfig:
224 kgdboc_unregister_kbd();
225 configured = 0;
227 return err;
230 static int kgdboc_probe(struct platform_device *pdev)
232 int ret = 0;
234 mutex_lock(&config_mutex);
235 if (configured != 1) {
236 ret = configure_kgdboc();
238 /* Convert "no device" to "defer" so we'll keep trying */
239 if (ret == -ENODEV)
240 ret = -EPROBE_DEFER;
242 mutex_unlock(&config_mutex);
244 return ret;
247 static struct platform_driver kgdboc_platform_driver = {
248 .probe = kgdboc_probe,
249 .driver = {
250 .name = "kgdboc",
251 .suppress_bind_attrs = true,
255 static int __init init_kgdboc(void)
257 int ret;
260 * kgdboc is a little bit of an odd "platform_driver". It can be
261 * up and running long before the platform_driver object is
262 * created and thus doesn't actually store anything in it. There's
263 * only one instance of kgdb so anything is stored as global state.
264 * The platform_driver is only created so that we can leverage the
265 * kernel's mechanisms (like -EPROBE_DEFER) to call us when our
266 * underlying tty is ready. Here we init our platform driver and
267 * then create the single kgdboc instance.
269 ret = platform_driver_register(&kgdboc_platform_driver);
270 if (ret)
271 return ret;
273 kgdboc_pdev = platform_device_alloc("kgdboc", PLATFORM_DEVID_NONE);
274 if (!kgdboc_pdev) {
275 ret = -ENOMEM;
276 goto err_did_register;
279 ret = platform_device_add(kgdboc_pdev);
280 if (!ret)
281 return 0;
283 platform_device_put(kgdboc_pdev);
285 err_did_register:
286 platform_driver_unregister(&kgdboc_platform_driver);
287 return ret;
290 static void exit_kgdboc(void)
292 mutex_lock(&config_mutex);
293 cleanup_kgdboc();
294 mutex_unlock(&config_mutex);
296 platform_device_unregister(kgdboc_pdev);
297 platform_driver_unregister(&kgdboc_platform_driver);
300 static int kgdboc_get_char(void)
302 if (!kgdb_tty_driver)
303 return -1;
304 return kgdb_tty_driver->ops->poll_get_char(kgdb_tty_driver,
305 kgdb_tty_line);
308 static void kgdboc_put_char(u8 chr)
310 if (!kgdb_tty_driver)
311 return;
312 kgdb_tty_driver->ops->poll_put_char(kgdb_tty_driver,
313 kgdb_tty_line, chr);
316 static int param_set_kgdboc_var(const char *kmessage,
317 const struct kernel_param *kp)
319 size_t len = strlen(kmessage);
320 int ret = 0;
322 if (len >= MAX_CONFIG_LEN) {
323 pr_err("config string too long\n");
324 return -ENOSPC;
327 if (kgdb_connected) {
328 pr_err("Cannot reconfigure while KGDB is connected.\n");
329 return -EBUSY;
332 mutex_lock(&config_mutex);
334 strcpy(config, kmessage);
335 /* Chop out \n char as a result of echo */
336 if (len && config[len - 1] == '\n')
337 config[len - 1] = '\0';
339 if (configured == 1)
340 cleanup_kgdboc();
343 * Configure with the new params as long as init already ran.
344 * Note that we can get called before init if someone loads us
345 * with "modprobe kgdboc kgdboc=..." or if they happen to use the
346 * the odd syntax of "kgdboc.kgdboc=..." on the kernel command.
348 if (configured >= 0)
349 ret = configure_kgdboc();
352 * If we couldn't configure then clear out the config. Note that
353 * specifying an invalid config on the kernel command line vs.
354 * through sysfs have slightly different behaviors. If we fail
355 * to configure what was specified on the kernel command line
356 * we'll leave it in the 'config' and return -EPROBE_DEFER from
357 * our probe. When specified through sysfs userspace is
358 * responsible for loading the tty driver before setting up.
360 if (ret)
361 config[0] = '\0';
363 mutex_unlock(&config_mutex);
365 return ret;
368 static int dbg_restore_graphics;
370 static void kgdboc_pre_exp_handler(void)
372 if (!dbg_restore_graphics && kgdboc_use_kms) {
373 dbg_restore_graphics = 1;
374 con_debug_enter(vc_cons[fg_console].d);
376 /* Increment the module count when the debugger is active */
377 if (!kgdb_connected)
378 try_module_get(THIS_MODULE);
381 static void kgdboc_post_exp_handler(void)
383 /* decrement the module count when the debugger detaches */
384 if (!kgdb_connected)
385 module_put(THIS_MODULE);
386 if (kgdboc_use_kms && dbg_restore_graphics) {
387 dbg_restore_graphics = 0;
388 con_debug_leave();
390 kgdboc_restore_input();
393 static struct kgdb_io kgdboc_io_ops = {
394 .name = "kgdboc",
395 .read_char = kgdboc_get_char,
396 .write_char = kgdboc_put_char,
397 .pre_exception = kgdboc_pre_exp_handler,
398 .post_exception = kgdboc_post_exp_handler,
401 #if IS_BUILTIN(CONFIG_KGDB_SERIAL_CONSOLE)
402 static int kgdboc_option_setup(char *opt)
404 if (!opt) {
405 pr_err("config string not provided\n");
406 return -EINVAL;
409 if (strlen(opt) >= MAX_CONFIG_LEN) {
410 pr_err("config string too long\n");
411 return -ENOSPC;
413 strcpy(config, opt);
415 return 0;
418 __setup("kgdboc=", kgdboc_option_setup);
421 /* This is only available if kgdboc is a built in for early debugging */
422 static int __init kgdboc_early_init(char *opt)
424 kgdboc_option_setup(opt);
425 configure_kgdboc();
426 return 0;
429 early_param("ekgdboc", kgdboc_early_init);
431 static int kgdboc_earlycon_get_char(void)
433 char c;
435 if (!kgdboc_earlycon_io_ops.cons->read(kgdboc_earlycon_io_ops.cons,
436 &c, 1))
437 return NO_POLL_CHAR;
439 return c;
442 static void kgdboc_earlycon_put_char(u8 chr)
444 kgdboc_earlycon_io_ops.cons->write(kgdboc_earlycon_io_ops.cons, &chr,
448 static void kgdboc_earlycon_pre_exp_handler(void)
450 struct console *con;
451 static bool already_warned;
453 if (already_warned)
454 return;
457 * When the first normal console comes up the kernel will take all
458 * the boot consoles out of the list. Really, we should stop using
459 * the boot console when it does that but until a TTY is registered
460 * we have no other choice so we keep using it. Since not all
461 * serial drivers might be OK with this, print a warning once per
462 * boot if we detect this case.
464 for_each_console(con)
465 if (con == kgdboc_earlycon_io_ops.cons)
466 return;
468 already_warned = true;
469 pr_warn("kgdboc_earlycon is still using bootconsole\n");
472 static int kgdboc_earlycon_deferred_exit(struct console *con)
475 * If we get here it means the boot console is going away but we
476 * don't yet have a suitable replacement. Don't pass through to
477 * the original exit routine. We'll call it later in our deinit()
478 * function. For now, restore the original exit() function pointer
479 * as a sentinal that we've hit this point.
481 con->exit = earlycon_orig_exit;
483 return 0;
486 static void kgdboc_earlycon_deinit(void)
488 if (!kgdboc_earlycon_io_ops.cons)
489 return;
491 if (kgdboc_earlycon_io_ops.cons->exit == kgdboc_earlycon_deferred_exit)
493 * kgdboc_earlycon is exiting but original boot console exit
494 * was never called (AKA kgdboc_earlycon_deferred_exit()
495 * didn't ever run). Undo our trap.
497 kgdboc_earlycon_io_ops.cons->exit = earlycon_orig_exit;
498 else if (kgdboc_earlycon_io_ops.cons->exit)
500 * We skipped calling the exit() routine so we could try to
501 * keep using the boot console even after it went away. We're
502 * finally done so call the function now.
504 kgdboc_earlycon_io_ops.cons->exit(kgdboc_earlycon_io_ops.cons);
506 kgdboc_earlycon_io_ops.cons = NULL;
509 static struct kgdb_io kgdboc_earlycon_io_ops = {
510 .name = "kgdboc_earlycon",
511 .read_char = kgdboc_earlycon_get_char,
512 .write_char = kgdboc_earlycon_put_char,
513 .pre_exception = kgdboc_earlycon_pre_exp_handler,
514 .deinit = kgdboc_earlycon_deinit,
517 #define MAX_CONSOLE_NAME_LEN (sizeof((struct console *) 0)->name)
518 static char kgdboc_earlycon_param[MAX_CONSOLE_NAME_LEN] __initdata;
519 static bool kgdboc_earlycon_late_enable __initdata;
521 static int __init kgdboc_earlycon_init(char *opt)
523 struct console *con;
525 kdb_init(KDB_INIT_EARLY);
528 * Look for a matching console, or if the name was left blank just
529 * pick the first one we find.
531 console_lock();
532 for_each_console(con) {
533 if (con->write && con->read &&
534 (con->flags & (CON_BOOT | CON_ENABLED)) &&
535 (!opt || !opt[0] || strcmp(con->name, opt) == 0))
536 break;
539 if (!con) {
541 * Both earlycon and kgdboc_earlycon are initialized during
542 * early parameter parsing. We cannot guarantee earlycon gets
543 * in first and, in any case, on ACPI systems earlycon may
544 * defer its own initialization (usually to somewhere within
545 * setup_arch() ). To cope with either of these situations
546 * we can defer our own initialization to a little later in
547 * the boot.
549 if (!kgdboc_earlycon_late_enable) {
550 pr_info("No suitable earlycon yet, will try later\n");
551 if (opt)
552 strscpy(kgdboc_earlycon_param, opt,
553 sizeof(kgdboc_earlycon_param));
554 kgdboc_earlycon_late_enable = true;
555 } else {
556 pr_info("Couldn't find kgdb earlycon\n");
558 goto unlock;
561 kgdboc_earlycon_io_ops.cons = con;
562 pr_info("Going to register kgdb with earlycon '%s'\n", con->name);
563 if (kgdb_register_io_module(&kgdboc_earlycon_io_ops) != 0) {
564 kgdboc_earlycon_io_ops.cons = NULL;
565 pr_info("Failed to register kgdb with earlycon\n");
566 } else {
567 /* Trap exit so we can keep earlycon longer if needed. */
568 earlycon_orig_exit = con->exit;
569 con->exit = kgdboc_earlycon_deferred_exit;
572 unlock:
573 console_unlock();
575 /* Non-zero means malformed option so we always return zero */
576 return 0;
579 early_param("kgdboc_earlycon", kgdboc_earlycon_init);
582 * This is only intended for the late adoption of an early console.
584 * It is not a reliable way to adopt regular consoles because we can not
585 * control what order console initcalls are made and, in any case, many
586 * regular consoles are registered much later in the boot process than
587 * the console initcalls!
589 static int __init kgdboc_earlycon_late_init(void)
591 if (kgdboc_earlycon_late_enable)
592 kgdboc_earlycon_init(kgdboc_earlycon_param);
593 return 0;
595 console_initcall(kgdboc_earlycon_late_init);
597 #endif /* IS_BUILTIN(CONFIG_KGDB_SERIAL_CONSOLE) */
599 module_init(init_kgdboc);
600 module_exit(exit_kgdboc);
601 module_param_call(kgdboc, param_set_kgdboc_var, param_get_string, &kps, 0644);
602 MODULE_PARM_DESC(kgdboc, "<serial_device>[,baud]");
603 MODULE_DESCRIPTION("KGDB Console TTY Driver");
604 MODULE_LICENSE("GPL");