Revert "tty: hvc: Fix data abort due to race in hvc_open"
[linux/fpc-iii.git] / drivers / tty / serial / kgdboc.c
blob151256f70d376ca170d0e572e97ef006d12058c2
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>
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 = {
35 .string = config,
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 */
53 return -ENODEV;
56 static void kgdboc_reset_disconnect(struct input_handle *handle)
58 /* We do not expect anyone to actually bind to us */
59 BUG();
62 static const struct input_device_id kgdboc_reset_ids[] = {
64 .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
65 .evbit = { BIT_MASK(EV_KEY) },
67 { }
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;
108 kdb_poll_idx++;
109 if (cptr[0][3] == ',')
110 *cptr += 4;
111 else
112 return 1;
115 return 0;
118 static void kgdboc_unregister_kbd(void)
120 int i;
122 for (i = 0; i < kdb_poll_idx; i++) {
123 if (kdb_poll_funcs[i] == kdb_get_kbd_char) {
124 kdb_poll_idx--;
125 kdb_poll_funcs[i] = kdb_poll_funcs[kdb_poll_idx];
126 kdb_poll_funcs[kdb_poll_idx] = NULL;
127 i--;
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)
140 if (configured != 1)
141 return;
143 if (kgdb_unregister_nmi_console())
144 return;
145 kgdboc_unregister_kbd();
146 kgdb_unregister_io_module(&kgdboc_io_ops);
149 static int configure_kgdboc(void)
151 struct tty_driver *p;
152 int tty_line = 0;
153 int err = -ENODEV;
154 char *cptr = config;
155 struct console *cons;
157 if (!strlen(config) || isspace(config[0])) {
158 err = 0;
159 goto noconfig;
162 kgdboc_io_ops.is_console = 0;
163 kgdb_tty_driver = NULL;
165 kgdboc_use_kms = 0;
166 if (strncmp(cptr, "kms,", 4) == 0) {
167 cptr += 4;
168 kgdboc_use_kms = 1;
171 if (kgdboc_register_kbd(&cptr))
172 goto do_register;
174 p = tty_find_polling_driver(cptr, &tty_line);
175 if (!p)
176 goto noconfig;
178 for_each_console(cons) {
179 int idx;
180 if (cons->device && cons->device(cons, &idx) == p &&
181 idx == tty_line) {
182 kgdboc_io_ops.is_console = 1;
183 break;
187 kgdb_tty_driver = p;
188 kgdb_tty_line = tty_line;
190 do_register:
191 err = kgdb_register_io_module(&kgdboc_io_ops);
192 if (err)
193 goto noconfig;
195 err = kgdb_register_nmi_console();
196 if (err)
197 goto nmi_con_failed;
199 configured = 1;
201 return 0;
203 nmi_con_failed:
204 kgdb_unregister_io_module(&kgdboc_io_ops);
205 noconfig:
206 kgdboc_unregister_kbd();
207 configured = 0;
209 return err;
212 static int kgdboc_probe(struct platform_device *pdev)
214 int ret = 0;
216 mutex_lock(&config_mutex);
217 if (configured != 1) {
218 ret = configure_kgdboc();
220 /* Convert "no device" to "defer" so we'll keep trying */
221 if (ret == -ENODEV)
222 ret = -EPROBE_DEFER;
224 mutex_unlock(&config_mutex);
226 return ret;
229 static struct platform_driver kgdboc_platform_driver = {
230 .probe = kgdboc_probe,
231 .driver = {
232 .name = "kgdboc",
233 .suppress_bind_attrs = true,
237 static int __init init_kgdboc(void)
239 int ret;
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);
252 if (ret)
253 return ret;
255 kgdboc_pdev = platform_device_alloc("kgdboc", PLATFORM_DEVID_NONE);
256 if (!kgdboc_pdev) {
257 ret = -ENOMEM;
258 goto err_did_register;
261 ret = platform_device_add(kgdboc_pdev);
262 if (!ret)
263 return 0;
265 platform_device_put(kgdboc_pdev);
267 err_did_register:
268 platform_driver_unregister(&kgdboc_platform_driver);
269 return ret;
272 static void exit_kgdboc(void)
274 mutex_lock(&config_mutex);
275 cleanup_kgdboc();
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)
285 return -1;
286 return kgdb_tty_driver->ops->poll_get_char(kgdb_tty_driver,
287 kgdb_tty_line);
290 static void kgdboc_put_char(u8 chr)
292 if (!kgdb_tty_driver)
293 return;
294 kgdb_tty_driver->ops->poll_put_char(kgdb_tty_driver,
295 kgdb_tty_line, chr);
298 static int param_set_kgdboc_var(const char *kmessage,
299 const struct kernel_param *kp)
301 size_t len = strlen(kmessage);
302 int ret = 0;
304 if (len >= MAX_CONFIG_LEN) {
305 pr_err("config string too long\n");
306 return -ENOSPC;
309 if (kgdb_connected) {
310 pr_err("Cannot reconfigure while KGDB is connected.\n");
311 return -EBUSY;
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';
321 if (configured == 1)
322 cleanup_kgdboc();
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.
330 if (configured >= 0)
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.
342 if (ret)
343 config[0] = '\0';
345 mutex_unlock(&config_mutex);
347 return ret;
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 */
359 if (!kgdb_connected)
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 */
370 if (!kgdb_connected)
371 module_put(THIS_MODULE);
372 if (kgdboc_use_kms && dbg_restore_graphics) {
373 dbg_restore_graphics = 0;
374 con_debug_leave();
376 kgdboc_restore_input();
379 static struct kgdb_io kgdboc_io_ops = {
380 .name = "kgdboc",
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)
390 if (!opt) {
391 pr_err("config string not provided\n");
392 return -EINVAL;
395 if (strlen(opt) >= MAX_CONFIG_LEN) {
396 pr_err("config string too long\n");
397 return -ENOSPC;
399 strcpy(config, opt);
401 return 0;
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);
411 configure_kgdboc();
412 return 0;
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");