kgdboc: Use a platform device to handle tty drivers showing up late
[platform/kernel/linux-rpi.git] / drivers / tty / serial / kgdboc.c
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
25 #define MAX_CONFIG_LEN          40
26
27 static struct kgdb_io           kgdboc_io_ops;
28
29 /* -1 = init not run yet, 0 = unconfigured, 1 = configured. */
30 static int configured           = -1;
31 static DEFINE_MUTEX(config_mutex);
32
33 static char config[MAX_CONFIG_LEN];
34 static struct kparam_string kps = {
35         .string                 = config,
36         .maxlen                 = MAX_CONFIG_LEN,
37 };
38
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;
42
43 static struct platform_device *kgdboc_pdev;
44
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)
49 {
50         input_reset_device(dev);
51
52         /* Return an error - we do not want to bind, just to reset */
53         return -ENODEV;
54 }
55
56 static void kgdboc_reset_disconnect(struct input_handle *handle)
57 {
58         /* We do not expect anyone to actually bind to us */
59         BUG();
60 }
61
62 static const struct input_device_id kgdboc_reset_ids[] = {
63         {
64                 .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
65                 .evbit = { BIT_MASK(EV_KEY) },
66         },
67         { }
68 };
69
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,
75 };
76
77 static DEFINE_MUTEX(kgdboc_reset_mutex);
78
79 static void kgdboc_restore_input_helper(struct work_struct *dummy)
80 {
81         /*
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.
85          */
86         mutex_lock(&kgdboc_reset_mutex);
87
88         if (input_register_handler(&kgdboc_reset_handler) == 0)
89                 input_unregister_handler(&kgdboc_reset_handler);
90
91         mutex_unlock(&kgdboc_reset_mutex);
92 }
93
94 static DECLARE_WORK(kgdboc_restore_input_work, kgdboc_restore_input_helper);
95
96 static void kgdboc_restore_input(void)
97 {
98         if (likely(system_state == SYSTEM_RUNNING))
99                 schedule_work(&kgdboc_restore_input_work);
100 }
101
102 static int kgdboc_register_kbd(char **cptr)
103 {
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;
113                 }
114         }
115         return 0;
116 }
117
118 static void kgdboc_unregister_kbd(void)
119 {
120         int i;
121
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--;
128                 }
129         }
130         flush_work(&kgdboc_restore_input_work);
131 }
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 */
137
138 static void cleanup_kgdboc(void)
139 {
140         if (configured != 1)
141                 return;
142
143         if (kgdb_unregister_nmi_console())
144                 return;
145         kgdboc_unregister_kbd();
146         kgdb_unregister_io_module(&kgdboc_io_ops);
147 }
148
149 static int configure_kgdboc(void)
150 {
151         struct tty_driver *p;
152         int tty_line = 0;
153         int err = -ENODEV;
154         char *cptr = config;
155         struct console *cons;
156
157         if (!strlen(config) || isspace(config[0])) {
158                 err = 0;
159                 goto noconfig;
160         }
161
162         kgdboc_io_ops.is_console = 0;
163         kgdb_tty_driver = NULL;
164
165         kgdboc_use_kms = 0;
166         if (strncmp(cptr, "kms,", 4) == 0) {
167                 cptr += 4;
168                 kgdboc_use_kms = 1;
169         }
170
171         if (kgdboc_register_kbd(&cptr))
172                 goto do_register;
173
174         p = tty_find_polling_driver(cptr, &tty_line);
175         if (!p)
176                 goto noconfig;
177
178         cons = console_drivers;
179         while (cons) {
180                 int idx;
181                 if (cons->device && cons->device(cons, &idx) == p &&
182                     idx == tty_line) {
183                         kgdboc_io_ops.is_console = 1;
184                         break;
185                 }
186                 cons = cons->next;
187         }
188
189         kgdb_tty_driver = p;
190         kgdb_tty_line = tty_line;
191
192 do_register:
193         err = kgdb_register_io_module(&kgdboc_io_ops);
194         if (err)
195                 goto noconfig;
196
197         err = kgdb_register_nmi_console();
198         if (err)
199                 goto nmi_con_failed;
200
201         configured = 1;
202
203         return 0;
204
205 nmi_con_failed:
206         kgdb_unregister_io_module(&kgdboc_io_ops);
207 noconfig:
208         kgdboc_unregister_kbd();
209         configured = 0;
210
211         return err;
212 }
213
214 static int kgdboc_probe(struct platform_device *pdev)
215 {
216         int ret = 0;
217
218         mutex_lock(&config_mutex);
219         if (configured != 1) {
220                 ret = configure_kgdboc();
221
222                 /* Convert "no device" to "defer" so we'll keep trying */
223                 if (ret == -ENODEV)
224                         ret = -EPROBE_DEFER;
225         }
226         mutex_unlock(&config_mutex);
227
228         return ret;
229 }
230
231 static struct platform_driver kgdboc_platform_driver = {
232         .probe = kgdboc_probe,
233         .driver = {
234                 .name = "kgdboc",
235                 .suppress_bind_attrs = true,
236         },
237 };
238
239 static int __init init_kgdboc(void)
240 {
241         int ret;
242
243         /*
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.
252          */
253         ret = platform_driver_register(&kgdboc_platform_driver);
254         if (ret)
255                 return ret;
256
257         kgdboc_pdev = platform_device_alloc("kgdboc", PLATFORM_DEVID_NONE);
258         if (!kgdboc_pdev) {
259                 ret = -ENOMEM;
260                 goto err_did_register;
261         }
262
263         ret = platform_device_add(kgdboc_pdev);
264         if (!ret)
265                 return 0;
266
267         platform_device_put(kgdboc_pdev);
268
269 err_did_register:
270         platform_driver_unregister(&kgdboc_platform_driver);
271         return ret;
272 }
273
274 static void exit_kgdboc(void)
275 {
276         mutex_lock(&config_mutex);
277         cleanup_kgdboc();
278         mutex_unlock(&config_mutex);
279
280         platform_device_unregister(kgdboc_pdev);
281         platform_driver_unregister(&kgdboc_platform_driver);
282 }
283
284 static int kgdboc_get_char(void)
285 {
286         if (!kgdb_tty_driver)
287                 return -1;
288         return kgdb_tty_driver->ops->poll_get_char(kgdb_tty_driver,
289                                                 kgdb_tty_line);
290 }
291
292 static void kgdboc_put_char(u8 chr)
293 {
294         if (!kgdb_tty_driver)
295                 return;
296         kgdb_tty_driver->ops->poll_put_char(kgdb_tty_driver,
297                                         kgdb_tty_line, chr);
298 }
299
300 static int param_set_kgdboc_var(const char *kmessage,
301                                 const struct kernel_param *kp)
302 {
303         size_t len = strlen(kmessage);
304         int ret = 0;
305
306         if (len >= MAX_CONFIG_LEN) {
307                 pr_err("config string too long\n");
308                 return -ENOSPC;
309         }
310
311         if (kgdb_connected) {
312                 pr_err("Cannot reconfigure while KGDB is connected.\n");
313                 return -EBUSY;
314         }
315
316         mutex_lock(&config_mutex);
317
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';
322
323         if (configured == 1)
324                 cleanup_kgdboc();
325
326         /*
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.
331          */
332         if (configured >= 0)
333                 ret = configure_kgdboc();
334
335         /*
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.
343          */
344         if (ret)
345                 config[0] = '\0';
346
347         mutex_unlock(&config_mutex);
348
349         return ret;
350 }
351
352 static int dbg_restore_graphics;
353
354 static void kgdboc_pre_exp_handler(void)
355 {
356         if (!dbg_restore_graphics && kgdboc_use_kms) {
357                 dbg_restore_graphics = 1;
358                 con_debug_enter(vc_cons[fg_console].d);
359         }
360         /* Increment the module count when the debugger is active */
361         if (!kgdb_connected)
362                 try_module_get(THIS_MODULE);
363
364         atomic_inc(&ignore_console_lock_warning);
365 }
366
367 static void kgdboc_post_exp_handler(void)
368 {
369         atomic_dec(&ignore_console_lock_warning);
370
371         /* decrement the module count when the debugger detaches */
372         if (!kgdb_connected)
373                 module_put(THIS_MODULE);
374         if (kgdboc_use_kms && dbg_restore_graphics) {
375                 dbg_restore_graphics = 0;
376                 con_debug_leave();
377         }
378         kgdboc_restore_input();
379 }
380
381 static struct kgdb_io kgdboc_io_ops = {
382         .name                   = "kgdboc",
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,
387 };
388
389 #ifdef CONFIG_KGDB_SERIAL_CONSOLE
390 static int kgdboc_option_setup(char *opt)
391 {
392         if (!opt) {
393                 pr_err("config string not provided\n");
394                 return -EINVAL;
395         }
396
397         if (strlen(opt) >= MAX_CONFIG_LEN) {
398                 pr_err("config string too long\n");
399                 return -ENOSPC;
400         }
401         strcpy(config, opt);
402
403         return 0;
404 }
405
406 __setup("kgdboc=", kgdboc_option_setup);
407
408
409 /* This is only available if kgdboc is a built in for early debugging */
410 static int __init kgdboc_early_init(char *opt)
411 {
412         kgdboc_option_setup(opt);
413         configure_kgdboc();
414         return 0;
415 }
416
417 early_param("ekgdboc", kgdboc_early_init);
418 #endif /* CONFIG_KGDB_SERIAL_CONSOLE */
419
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");