Linux Audio

Check our new training course

Loading...
v6.8
  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.
  6 *
  7 * Maintainer: Jason Wessel <jason.wessel@windriver.com>
  8 *
  9 * 2007-2008 (c) Jason Wessel - Wind River Systems, Inc.
 
 
 
 
 10 */
 11
 12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 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#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>
 25
 26#define MAX_CONFIG_LEN		40
 27
 28static struct kgdb_io		kgdboc_io_ops;
 29
 30/* -1 = init not run yet, 0 = unconfigured, 1 = configured. */
 31static int configured		= -1;
 32static DEFINE_MUTEX(config_mutex);
 33
 34static char config[MAX_CONFIG_LEN];
 35static struct kparam_string kps = {
 36	.string			= config,
 37	.maxlen			= MAX_CONFIG_LEN,
 38};
 39
 40static int kgdboc_use_kms;  /* 1 if we use kernel mode switching */
 41static struct tty_driver	*kgdb_tty_driver;
 42static int			kgdb_tty_line;
 43
 44static struct platform_device *kgdboc_pdev;
 45
 46#if IS_BUILTIN(CONFIG_KGDB_SERIAL_CONSOLE)
 47static struct kgdb_io		kgdboc_earlycon_io_ops;
 48static int                      (*earlycon_orig_exit)(struct console *con);
 49#endif /* IS_BUILTIN(CONFIG_KGDB_SERIAL_CONSOLE) */
 50
 51#ifdef CONFIG_KDB_KEYBOARD
 52static int kgdboc_reset_connect(struct input_handler *handler,
 53				struct input_dev *dev,
 54				const struct input_device_id *id)
 55{
 56	input_reset_device(dev);
 57
 58	/* Return an error - we do not want to bind, just to reset */
 59	return -ENODEV;
 60}
 61
 62static void kgdboc_reset_disconnect(struct input_handle *handle)
 63{
 64	/* We do not expect anyone to actually bind to us */
 65	BUG();
 66}
 67
 68static const struct input_device_id kgdboc_reset_ids[] = {
 69	{
 70		.flags = INPUT_DEVICE_ID_MATCH_EVBIT,
 71		.evbit = { BIT_MASK(EV_KEY) },
 72	},
 73	{ }
 74};
 75
 76static 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,
 81};
 82
 83static DEFINE_MUTEX(kgdboc_reset_mutex);
 84
 85static void kgdboc_restore_input_helper(struct work_struct *dummy)
 86{
 87	/*
 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.
 91	 */
 92	mutex_lock(&kgdboc_reset_mutex);
 93
 94	if (input_register_handler(&kgdboc_reset_handler) == 0)
 95		input_unregister_handler(&kgdboc_reset_handler);
 96
 97	mutex_unlock(&kgdboc_reset_mutex);
 98}
 99
100static DECLARE_WORK(kgdboc_restore_input_work, kgdboc_restore_input_helper);
101
102static void kgdboc_restore_input(void)
103{
104	if (likely(system_state == SYSTEM_RUNNING))
105		schedule_work(&kgdboc_restore_input_work);
106}
107
108static int kgdboc_register_kbd(char **cptr)
109{
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;
119		}
120	}
121	return 0;
122}
123
124static void kgdboc_unregister_kbd(void)
125{
126	int i;
127
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--;
134		}
135	}
136	flush_work(&kgdboc_restore_input_work);
137}
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 */
143
144#if IS_BUILTIN(CONFIG_KGDB_SERIAL_CONSOLE)
145static void cleanup_earlycon(void)
146{
147	if (kgdboc_earlycon_io_ops.cons)
148		kgdb_unregister_io_module(&kgdboc_earlycon_io_ops);
 
 
 
 
 
149}
150#else /* !IS_BUILTIN(CONFIG_KGDB_SERIAL_CONSOLE) */
151static inline void cleanup_earlycon(void) { }
152#endif /* !IS_BUILTIN(CONFIG_KGDB_SERIAL_CONSOLE) */
153
154static void cleanup_kgdboc(void)
155{
156	cleanup_earlycon();
157
158	if (configured != 1)
159		return;
160
161	if (kgdb_unregister_nmi_console())
162		return;
163	kgdboc_unregister_kbd();
164	kgdb_unregister_io_module(&kgdboc_io_ops);
 
165}
166
167static int configure_kgdboc(void)
168{
169	struct tty_driver *p;
170	int tty_line = 0;
171	int err = -ENODEV;
172	char *cptr = config;
173	struct console *cons;
174	int cookie;
175
176	if (!strlen(config) || isspace(config[0])) {
177		err = 0;
178		goto noconfig;
179	}
180
181	kgdboc_io_ops.cons = NULL;
 
182	kgdb_tty_driver = NULL;
183
184	kgdboc_use_kms = 0;
185	if (strncmp(cptr, "kms,", 4) == 0) {
186		cptr += 4;
187		kgdboc_use_kms = 1;
188	}
189
190	if (kgdboc_register_kbd(&cptr))
191		goto do_register;
192
193	p = tty_find_polling_driver(cptr, &tty_line);
194	if (!p)
195		goto noconfig;
196
197	/*
198	 * Take console_lock to serialize device() callback with
199	 * other console operations. For example, fg_console is
200	 * modified under console_lock when switching vt.
201	 */
202	console_lock();
203
204	cookie = console_srcu_read_lock();
205	for_each_console_srcu(cons) {
206		int idx;
207		if (cons->device && cons->device(cons, &idx) == p &&
208		    idx == tty_line) {
209			kgdboc_io_ops.cons = cons;
210			break;
211		}
 
212	}
213	console_srcu_read_unlock(cookie);
214
215	console_unlock();
216
217	kgdb_tty_driver = p;
218	kgdb_tty_line = tty_line;
219
220do_register:
221	err = kgdb_register_io_module(&kgdboc_io_ops);
222	if (err)
223		goto noconfig;
224
225	err = kgdb_register_nmi_console();
226	if (err)
227		goto nmi_con_failed;
228
229	configured = 1;
230
231	return 0;
232
233nmi_con_failed:
234	kgdb_unregister_io_module(&kgdboc_io_ops);
235noconfig:
236	kgdboc_unregister_kbd();
237	configured = 0;
 
238
239	return err;
240}
241
242static int kgdboc_probe(struct platform_device *pdev)
243{
244	int ret = 0;
245
246	mutex_lock(&config_mutex);
247	if (configured != 1) {
248		ret = configure_kgdboc();
249
250		/* Convert "no device" to "defer" so we'll keep trying */
251		if (ret == -ENODEV)
252			ret = -EPROBE_DEFER;
253	}
254	mutex_unlock(&config_mutex);
255
256	return ret;
257}
258
259static struct platform_driver kgdboc_platform_driver = {
260	.probe = kgdboc_probe,
261	.driver = {
262		.name = "kgdboc",
263		.suppress_bind_attrs = true,
264	},
265};
266
267static int __init init_kgdboc(void)
268{
269	int ret;
270
271	/*
272	 * kgdboc is a little bit of an odd "platform_driver".  It can be
273	 * up and running long before the platform_driver object is
274	 * created and thus doesn't actually store anything in it.  There's
275	 * only one instance of kgdb so anything is stored as global state.
276	 * The platform_driver is only created so that we can leverage the
277	 * kernel's mechanisms (like -EPROBE_DEFER) to call us when our
278	 * underlying tty is ready.  Here we init our platform driver and
279	 * then create the single kgdboc instance.
280	 */
281	ret = platform_driver_register(&kgdboc_platform_driver);
282	if (ret)
283		return ret;
284
285	kgdboc_pdev = platform_device_alloc("kgdboc", PLATFORM_DEVID_NONE);
286	if (!kgdboc_pdev) {
287		ret = -ENOMEM;
288		goto err_did_register;
289	}
290
291	ret = platform_device_add(kgdboc_pdev);
292	if (!ret)
293		return 0;
294
295	platform_device_put(kgdboc_pdev);
296
297err_did_register:
298	platform_driver_unregister(&kgdboc_platform_driver);
299	return ret;
300}
301
302static void exit_kgdboc(void)
303{
304	mutex_lock(&config_mutex);
305	cleanup_kgdboc();
306	mutex_unlock(&config_mutex);
307
308	platform_device_unregister(kgdboc_pdev);
309	platform_driver_unregister(&kgdboc_platform_driver);
310}
311
312static int kgdboc_get_char(void)
313{
314	if (!kgdb_tty_driver)
315		return -1;
316	return kgdb_tty_driver->ops->poll_get_char(kgdb_tty_driver,
317						kgdb_tty_line);
318}
319
320static void kgdboc_put_char(u8 chr)
321{
322	if (!kgdb_tty_driver)
323		return;
324	kgdb_tty_driver->ops->poll_put_char(kgdb_tty_driver,
325					kgdb_tty_line, chr);
326}
327
328static int param_set_kgdboc_var(const char *kmessage,
329				const struct kernel_param *kp)
330{
331	size_t len = strlen(kmessage);
332	int ret = 0;
333
334	if (len >= MAX_CONFIG_LEN) {
335		pr_err("config string too long\n");
336		return -ENOSPC;
337	}
338
 
 
 
 
 
 
339	if (kgdb_connected) {
340		pr_err("Cannot reconfigure while KGDB is connected.\n");
 
 
341		return -EBUSY;
342	}
343
344	mutex_lock(&config_mutex);
345
346	strcpy(config, kmessage);
347	/* Chop out \n char as a result of echo */
348	if (len && config[len - 1] == '\n')
349		config[len - 1] = '\0';
350
351	if (configured == 1)
352		cleanup_kgdboc();
353
354	/*
355	 * Configure with the new params as long as init already ran.
356	 * Note that we can get called before init if someone loads us
357	 * with "modprobe kgdboc kgdboc=..." or if they happen to use
358	 * the odd syntax of "kgdboc.kgdboc=..." on the kernel command.
359	 */
360	if (configured >= 0)
361		ret = configure_kgdboc();
362
363	/*
364	 * If we couldn't configure then clear out the config.  Note that
365	 * specifying an invalid config on the kernel command line vs.
366	 * through sysfs have slightly different behaviors.  If we fail
367	 * to configure what was specified on the kernel command line
368	 * we'll leave it in the 'config' and return -EPROBE_DEFER from
369	 * our probe.  When specified through sysfs userspace is
370	 * responsible for loading the tty driver before setting up.
371	 */
372	if (ret)
373		config[0] = '\0';
374
375	mutex_unlock(&config_mutex);
376
377	return ret;
378}
379
380static int dbg_restore_graphics;
381
382static void kgdboc_pre_exp_handler(void)
383{
384	if (!dbg_restore_graphics && kgdboc_use_kms) {
385		dbg_restore_graphics = 1;
386		con_debug_enter(vc_cons[fg_console].d);
387	}
388	/* Increment the module count when the debugger is active */
389	if (!kgdb_connected)
390		try_module_get(THIS_MODULE);
391}
392
393static void kgdboc_post_exp_handler(void)
394{
395	/* decrement the module count when the debugger detaches */
396	if (!kgdb_connected)
397		module_put(THIS_MODULE);
398	if (kgdboc_use_kms && dbg_restore_graphics) {
399		dbg_restore_graphics = 0;
400		con_debug_leave();
401	}
402	kgdboc_restore_input();
403}
404
405static struct kgdb_io kgdboc_io_ops = {
406	.name			= "kgdboc",
407	.read_char		= kgdboc_get_char,
408	.write_char		= kgdboc_put_char,
409	.pre_exception		= kgdboc_pre_exp_handler,
410	.post_exception		= kgdboc_post_exp_handler,
411};
412
413#if IS_BUILTIN(CONFIG_KGDB_SERIAL_CONSOLE)
414static int kgdboc_option_setup(char *opt)
415{
416	if (!opt) {
417		pr_err("config string not provided\n");
418		return 1;
419	}
420
421	if (strlen(opt) >= MAX_CONFIG_LEN) {
422		pr_err("config string too long\n");
423		return 1;
424	}
425	strcpy(config, opt);
426
427	return 1;
428}
429
430__setup("kgdboc=", kgdboc_option_setup);
431
432
433/* This is only available if kgdboc is a built in for early debugging */
434static int __init kgdboc_early_init(char *opt)
435{
436	kgdboc_option_setup(opt);
437	configure_kgdboc();
438	return 0;
439}
440
441early_param("ekgdboc", kgdboc_early_init);
442
443static int kgdboc_earlycon_get_char(void)
444{
445	char c;
446
447	if (!kgdboc_earlycon_io_ops.cons->read(kgdboc_earlycon_io_ops.cons,
448					       &c, 1))
449		return NO_POLL_CHAR;
450
451	return c;
452}
453
454static void kgdboc_earlycon_put_char(u8 chr)
455{
456	kgdboc_earlycon_io_ops.cons->write(kgdboc_earlycon_io_ops.cons, &chr,
457					   1);
458}
459
460static void kgdboc_earlycon_pre_exp_handler(void)
461{
462	struct console *con;
463	static bool already_warned;
464	int cookie;
465
466	if (already_warned)
467		return;
468
469	/*
470	 * When the first normal console comes up the kernel will take all
471	 * the boot consoles out of the list.  Really, we should stop using
472	 * the boot console when it does that but until a TTY is registered
473	 * we have no other choice so we keep using it.  Since not all
474	 * serial drivers might be OK with this, print a warning once per
475	 * boot if we detect this case.
476	 */
477	cookie = console_srcu_read_lock();
478	for_each_console_srcu(con) {
479		if (con == kgdboc_earlycon_io_ops.cons)
480			break;
481	}
482	console_srcu_read_unlock(cookie);
483	if (con)
484		return;
485
486	already_warned = true;
487	pr_warn("kgdboc_earlycon is still using bootconsole\n");
488}
489
490static int kgdboc_earlycon_deferred_exit(struct console *con)
491{
492	/*
493	 * If we get here it means the boot console is going away but we
494	 * don't yet have a suitable replacement.  Don't pass through to
495	 * the original exit routine.  We'll call it later in our deinit()
496	 * function.  For now, restore the original exit() function pointer
497	 * as a sentinal that we've hit this point.
498	 */
499	con->exit = earlycon_orig_exit;
500
501	return 0;
502}
503
504static void kgdboc_earlycon_deinit(void)
505{
506	if (!kgdboc_earlycon_io_ops.cons)
507		return;
508
509	if (kgdboc_earlycon_io_ops.cons->exit == kgdboc_earlycon_deferred_exit)
510		/*
511		 * kgdboc_earlycon is exiting but original boot console exit
512		 * was never called (AKA kgdboc_earlycon_deferred_exit()
513		 * didn't ever run).  Undo our trap.
514		 */
515		kgdboc_earlycon_io_ops.cons->exit = earlycon_orig_exit;
516	else if (kgdboc_earlycon_io_ops.cons->exit)
517		/*
518		 * We skipped calling the exit() routine so we could try to
519		 * keep using the boot console even after it went away.  We're
520		 * finally done so call the function now.
521		 */
522		kgdboc_earlycon_io_ops.cons->exit(kgdboc_earlycon_io_ops.cons);
523
524	kgdboc_earlycon_io_ops.cons = NULL;
525}
526
527static struct kgdb_io kgdboc_earlycon_io_ops = {
528	.name			= "kgdboc_earlycon",
529	.read_char		= kgdboc_earlycon_get_char,
530	.write_char		= kgdboc_earlycon_put_char,
531	.pre_exception		= kgdboc_earlycon_pre_exp_handler,
532	.deinit			= kgdboc_earlycon_deinit,
533};
534
535#define MAX_CONSOLE_NAME_LEN (sizeof((struct console *) 0)->name)
536static char kgdboc_earlycon_param[MAX_CONSOLE_NAME_LEN] __initdata;
537static bool kgdboc_earlycon_late_enable __initdata;
538
539static int __init kgdboc_earlycon_init(char *opt)
540{
541	struct console *con;
542
543	kdb_init(KDB_INIT_EARLY);
544
545	/*
546	 * Look for a matching console, or if the name was left blank just
547	 * pick the first one we find.
548	 */
549
550	/*
551	 * Hold the console_list_lock to guarantee that no consoles are
552	 * unregistered until the kgdboc_earlycon setup is complete.
553	 * Trapping the exit() callback relies on exit() not being
554	 * called until the trap is setup. This also allows safe
555	 * traversal of the console list and race-free reading of @flags.
556	 */
557	console_list_lock();
558	for_each_console(con) {
559		if (con->write && con->read &&
560		    (con->flags & (CON_BOOT | CON_ENABLED)) &&
561		    (!opt || !opt[0] || strcmp(con->name, opt) == 0))
562			break;
563	}
564
565	if (!con) {
566		/*
567		 * Both earlycon and kgdboc_earlycon are initialized during
568		 * early parameter parsing. We cannot guarantee earlycon gets
569		 * in first and, in any case, on ACPI systems earlycon may
570		 * defer its own initialization (usually to somewhere within
571		 * setup_arch() ). To cope with either of these situations
572		 * we can defer our own initialization to a little later in
573		 * the boot.
574		 */
575		if (!kgdboc_earlycon_late_enable) {
576			pr_info("No suitable earlycon yet, will try later\n");
577			if (opt)
578				strscpy(kgdboc_earlycon_param, opt,
579					sizeof(kgdboc_earlycon_param));
580			kgdboc_earlycon_late_enable = true;
581		} else {
582			pr_info("Couldn't find kgdb earlycon\n");
583		}
584		goto unlock;
585	}
586
587	kgdboc_earlycon_io_ops.cons = con;
588	pr_info("Going to register kgdb with earlycon '%s'\n", con->name);
589	if (kgdb_register_io_module(&kgdboc_earlycon_io_ops) != 0) {
590		kgdboc_earlycon_io_ops.cons = NULL;
591		pr_info("Failed to register kgdb with earlycon\n");
592	} else {
593		/* Trap exit so we can keep earlycon longer if needed. */
594		earlycon_orig_exit = con->exit;
595		con->exit = kgdboc_earlycon_deferred_exit;
596	}
597
598unlock:
599	console_list_unlock();
600
601	/* Non-zero means malformed option so we always return zero */
602	return 0;
603}
604
605early_param("kgdboc_earlycon", kgdboc_earlycon_init);
606
607/*
608 * This is only intended for the late adoption of an early console.
609 *
610 * It is not a reliable way to adopt regular consoles because we can not
611 * control what order console initcalls are made and, in any case, many
612 * regular consoles are registered much later in the boot process than
613 * the console initcalls!
614 */
615static int __init kgdboc_earlycon_late_init(void)
616{
617	if (kgdboc_earlycon_late_enable)
618		kgdboc_earlycon_init(kgdboc_earlycon_param);
619	return 0;
620}
621console_initcall(kgdboc_earlycon_late_init);
622
623#endif /* IS_BUILTIN(CONFIG_KGDB_SERIAL_CONSOLE) */
 
624
625module_init(init_kgdboc);
626module_exit(exit_kgdboc);
627module_param_call(kgdboc, param_set_kgdboc_var, param_get_string, &kps, 0644);
628MODULE_PARM_DESC(kgdboc, "<serial_device>[,baud]");
629MODULE_DESCRIPTION("KGDB Console TTY Driver");
630MODULE_LICENSE("GPL");
v3.1
 
  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#include <linux/console.h>
 20#include <linux/vt_kern.h>
 21#include <linux/input.h>
 
 
 
 22
 23#define MAX_CONFIG_LEN		40
 24
 25static struct kgdb_io		kgdboc_io_ops;
 26
 27/* -1 = init not run yet, 0 = unconfigured, 1 = configured. */
 28static int configured		= -1;
 
 29
 30static char config[MAX_CONFIG_LEN];
 31static struct kparam_string kps = {
 32	.string			= config,
 33	.maxlen			= MAX_CONFIG_LEN,
 34};
 35
 36static int kgdboc_use_kms;  /* 1 if we use kernel mode switching */
 37static struct tty_driver	*kgdb_tty_driver;
 38static int			kgdb_tty_line;
 39
 
 
 
 
 
 
 
 40#ifdef CONFIG_KDB_KEYBOARD
 41static int kgdboc_reset_connect(struct input_handler *handler,
 42				struct input_dev *dev,
 43				const struct input_device_id *id)
 44{
 45	input_reset_device(dev);
 46
 47	/* Retrun an error - we do not want to bind, just to reset */
 48	return -ENODEV;
 49}
 50
 51static void kgdboc_reset_disconnect(struct input_handle *handle)
 52{
 53	/* We do not expect anyone to actually bind to us */
 54	BUG();
 55}
 56
 57static const struct input_device_id kgdboc_reset_ids[] = {
 58	{
 59		.flags = INPUT_DEVICE_ID_MATCH_EVBIT,
 60		.evbit = { BIT_MASK(EV_KEY) },
 61	},
 62	{ }
 63};
 64
 65static struct input_handler kgdboc_reset_handler = {
 66	.connect	= kgdboc_reset_connect,
 67	.disconnect	= kgdboc_reset_disconnect,
 68	.name		= "kgdboc_reset",
 69	.id_table	= kgdboc_reset_ids,
 70};
 71
 72static DEFINE_MUTEX(kgdboc_reset_mutex);
 73
 74static void kgdboc_restore_input_helper(struct work_struct *dummy)
 75{
 76	/*
 77	 * We need to take a mutex to prevent several instances of
 78	 * this work running on different CPUs so they don't try
 79	 * to register again already registered handler.
 80	 */
 81	mutex_lock(&kgdboc_reset_mutex);
 82
 83	if (input_register_handler(&kgdboc_reset_handler) == 0)
 84		input_unregister_handler(&kgdboc_reset_handler);
 85
 86	mutex_unlock(&kgdboc_reset_mutex);
 87}
 88
 89static DECLARE_WORK(kgdboc_restore_input_work, kgdboc_restore_input_helper);
 90
 91static void kgdboc_restore_input(void)
 92{
 93	if (likely(system_state == SYSTEM_RUNNING))
 94		schedule_work(&kgdboc_restore_input_work);
 95}
 96
 97static int kgdboc_register_kbd(char **cptr)
 98{
 99	if (strncmp(*cptr, "kbd", 3) == 0) {
 
100		if (kdb_poll_idx < KDB_POLL_FUNC_MAX) {
101			kdb_poll_funcs[kdb_poll_idx] = kdb_get_kbd_char;
102			kdb_poll_idx++;
103			if (cptr[0][3] == ',')
104				*cptr += 4;
105			else
106				return 1;
107		}
108	}
109	return 0;
110}
111
112static void kgdboc_unregister_kbd(void)
113{
114	int i;
115
116	for (i = 0; i < kdb_poll_idx; i++) {
117		if (kdb_poll_funcs[i] == kdb_get_kbd_char) {
118			kdb_poll_idx--;
119			kdb_poll_funcs[i] = kdb_poll_funcs[kdb_poll_idx];
120			kdb_poll_funcs[kdb_poll_idx] = NULL;
121			i--;
122		}
123	}
124	flush_work_sync(&kgdboc_restore_input_work);
125}
126#else /* ! CONFIG_KDB_KEYBOARD */
127#define kgdboc_register_kbd(x) 0
128#define kgdboc_unregister_kbd()
129#define kgdboc_restore_input()
130#endif /* ! CONFIG_KDB_KEYBOARD */
131
132static int kgdboc_option_setup(char *opt)
 
133{
134	if (strlen(opt) >= MAX_CONFIG_LEN) {
135		printk(KERN_ERR "kgdboc: config string too long\n");
136		return -ENOSPC;
137	}
138	strcpy(config, opt);
139
140	return 0;
141}
142
143__setup("kgdboc=", kgdboc_option_setup);
 
144
145static void cleanup_kgdboc(void)
146{
 
 
 
 
 
 
 
147	kgdboc_unregister_kbd();
148	if (configured == 1)
149		kgdb_unregister_io_module(&kgdboc_io_ops);
150}
151
152static int configure_kgdboc(void)
153{
154	struct tty_driver *p;
155	int tty_line = 0;
156	int err;
157	char *cptr = config;
158	struct console *cons;
 
159
160	err = kgdboc_option_setup(config);
161	if (err || !strlen(config) || isspace(config[0]))
162		goto noconfig;
 
163
164	err = -ENODEV;
165	kgdboc_io_ops.is_console = 0;
166	kgdb_tty_driver = NULL;
167
168	kgdboc_use_kms = 0;
169	if (strncmp(cptr, "kms,", 4) == 0) {
170		cptr += 4;
171		kgdboc_use_kms = 1;
172	}
173
174	if (kgdboc_register_kbd(&cptr))
175		goto do_register;
176
177	p = tty_find_polling_driver(cptr, &tty_line);
178	if (!p)
179		goto noconfig;
180
181	cons = console_drivers;
182	while (cons) {
 
 
 
 
 
 
 
183		int idx;
184		if (cons->device && cons->device(cons, &idx) == p &&
185		    idx == tty_line) {
186			kgdboc_io_ops.is_console = 1;
187			break;
188		}
189		cons = cons->next;
190	}
 
 
 
191
192	kgdb_tty_driver = p;
193	kgdb_tty_line = tty_line;
194
195do_register:
196	err = kgdb_register_io_module(&kgdboc_io_ops);
197	if (err)
198		goto noconfig;
199
 
 
 
 
200	configured = 1;
201
202	return 0;
203
 
 
204noconfig:
205	config[0] = 0;
206	configured = 0;
207	cleanup_kgdboc();
208
209	return err;
210}
211
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
212static int __init init_kgdboc(void)
213{
214	/* Already configured? */
215	if (configured == 1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
216		return 0;
217
218	return configure_kgdboc();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
219}
220
221static int kgdboc_get_char(void)
222{
223	if (!kgdb_tty_driver)
224		return -1;
225	return kgdb_tty_driver->ops->poll_get_char(kgdb_tty_driver,
226						kgdb_tty_line);
227}
228
229static void kgdboc_put_char(u8 chr)
230{
231	if (!kgdb_tty_driver)
232		return;
233	kgdb_tty_driver->ops->poll_put_char(kgdb_tty_driver,
234					kgdb_tty_line, chr);
235}
236
237static int param_set_kgdboc_var(const char *kmessage, struct kernel_param *kp)
 
238{
239	int len = strlen(kmessage);
 
240
241	if (len >= MAX_CONFIG_LEN) {
242		printk(KERN_ERR "kgdboc: config string too long\n");
243		return -ENOSPC;
244	}
245
246	/* Only copy in the string if the init function has not run yet */
247	if (configured < 0) {
248		strcpy(config, kmessage);
249		return 0;
250	}
251
252	if (kgdb_connected) {
253		printk(KERN_ERR
254		       "kgdboc: Cannot reconfigure while KGDB is connected.\n");
255
256		return -EBUSY;
257	}
258
 
 
259	strcpy(config, kmessage);
260	/* Chop out \n char as a result of echo */
261	if (config[len - 1] == '\n')
262		config[len - 1] = '\0';
263
264	if (configured == 1)
265		cleanup_kgdboc();
266
267	/* Go and configure with the new params. */
268	return configure_kgdboc();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
269}
270
271static int dbg_restore_graphics;
272
273static void kgdboc_pre_exp_handler(void)
274{
275	if (!dbg_restore_graphics && kgdboc_use_kms) {
276		dbg_restore_graphics = 1;
277		con_debug_enter(vc_cons[fg_console].d);
278	}
279	/* Increment the module count when the debugger is active */
280	if (!kgdb_connected)
281		try_module_get(THIS_MODULE);
282}
283
284static void kgdboc_post_exp_handler(void)
285{
286	/* decrement the module count when the debugger detaches */
287	if (!kgdb_connected)
288		module_put(THIS_MODULE);
289	if (kgdboc_use_kms && dbg_restore_graphics) {
290		dbg_restore_graphics = 0;
291		con_debug_leave();
292	}
293	kgdboc_restore_input();
294}
295
296static struct kgdb_io kgdboc_io_ops = {
297	.name			= "kgdboc",
298	.read_char		= kgdboc_get_char,
299	.write_char		= kgdboc_put_char,
300	.pre_exception		= kgdboc_pre_exp_handler,
301	.post_exception		= kgdboc_post_exp_handler,
302};
303
304#ifdef CONFIG_KGDB_SERIAL_CONSOLE
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
305/* This is only available if kgdboc is a built in for early debugging */
306static int __init kgdboc_early_init(char *opt)
307{
308	/* save the first character of the config string because the
309	 * init routine can destroy it.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
310	 */
311	char save_ch;
 
 
 
 
 
 
 
312
313	kgdboc_option_setup(opt);
314	save_ch = config[0];
315	init_kgdboc();
316	config[0] = save_ch;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
317	return 0;
318}
 
319
320early_param("ekgdboc", kgdboc_early_init);
321#endif /* CONFIG_KGDB_SERIAL_CONSOLE */
322
323module_init(init_kgdboc);
324module_exit(cleanup_kgdboc);
325module_param_call(kgdboc, param_set_kgdboc_var, param_get_string, &kps, 0644);
326MODULE_PARM_DESC(kgdboc, "<serial_device>[,baud]");
327MODULE_DESCRIPTION("KGDB Console TTY Driver");
328MODULE_LICENSE("GPL");