kgdboc.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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/tty.h>
  18. #define MAX_CONFIG_LEN 40
  19. static struct kgdb_io kgdboc_io_ops;
  20. /* -1 = init not run yet, 0 = unconfigured, 1 = configured. */
  21. static int configured = -1;
  22. static char config[MAX_CONFIG_LEN];
  23. static struct kparam_string kps = {
  24. .string = config,
  25. .maxlen = MAX_CONFIG_LEN,
  26. };
  27. static struct tty_driver *kgdb_tty_driver;
  28. static int kgdb_tty_line;
  29. static int kgdboc_option_setup(char *opt)
  30. {
  31. if (strlen(opt) > MAX_CONFIG_LEN) {
  32. printk(KERN_ERR "kgdboc: config string too long\n");
  33. return -ENOSPC;
  34. }
  35. strcpy(config, opt);
  36. return 0;
  37. }
  38. __setup("kgdboc=", kgdboc_option_setup);
  39. static int configure_kgdboc(void)
  40. {
  41. struct tty_driver *p;
  42. int tty_line = 0;
  43. int err;
  44. err = kgdboc_option_setup(config);
  45. if (err || !strlen(config) || isspace(config[0]))
  46. goto noconfig;
  47. err = -ENODEV;
  48. p = tty_find_polling_driver(config, &tty_line);
  49. if (!p)
  50. goto noconfig;
  51. kgdb_tty_driver = p;
  52. kgdb_tty_line = tty_line;
  53. err = kgdb_register_io_module(&kgdboc_io_ops);
  54. if (err)
  55. goto noconfig;
  56. configured = 1;
  57. return 0;
  58. noconfig:
  59. config[0] = 0;
  60. configured = 0;
  61. return err;
  62. }
  63. static int __init init_kgdboc(void)
  64. {
  65. /* Already configured? */
  66. if (configured == 1)
  67. return 0;
  68. return configure_kgdboc();
  69. }
  70. static void cleanup_kgdboc(void)
  71. {
  72. if (configured == 1)
  73. kgdb_unregister_io_module(&kgdboc_io_ops);
  74. }
  75. static int kgdboc_get_char(void)
  76. {
  77. return kgdb_tty_driver->poll_get_char(kgdb_tty_driver, kgdb_tty_line);
  78. }
  79. static void kgdboc_put_char(u8 chr)
  80. {
  81. kgdb_tty_driver->poll_put_char(kgdb_tty_driver, kgdb_tty_line, chr);
  82. }
  83. static int param_set_kgdboc_var(const char *kmessage, struct kernel_param *kp)
  84. {
  85. int len = strlen(kmessage);
  86. if (len >= MAX_CONFIG_LEN) {
  87. printk(KERN_ERR "kgdboc: config string too long\n");
  88. return -ENOSPC;
  89. }
  90. /* Only copy in the string if the init function has not run yet */
  91. if (configured < 0) {
  92. strcpy(config, kmessage);
  93. return 0;
  94. }
  95. if (kgdb_connected) {
  96. printk(KERN_ERR
  97. "kgdboc: Cannot reconfigure while KGDB is connected.\n");
  98. return -EBUSY;
  99. }
  100. strcpy(config, kmessage);
  101. /* Chop out \n char as a result of echo */
  102. if (config[len - 1] == '\n')
  103. config[len - 1] = '\0';
  104. if (configured == 1)
  105. cleanup_kgdboc();
  106. /* Go and configure with the new params. */
  107. return configure_kgdboc();
  108. }
  109. static void kgdboc_pre_exp_handler(void)
  110. {
  111. /* Increment the module count when the debugger is active */
  112. if (!kgdb_connected)
  113. try_module_get(THIS_MODULE);
  114. }
  115. static void kgdboc_post_exp_handler(void)
  116. {
  117. /* decrement the module count when the debugger detaches */
  118. if (!kgdb_connected)
  119. module_put(THIS_MODULE);
  120. }
  121. static struct kgdb_io kgdboc_io_ops = {
  122. .name = "kgdboc",
  123. .read_char = kgdboc_get_char,
  124. .write_char = kgdboc_put_char,
  125. .pre_exception = kgdboc_pre_exp_handler,
  126. .post_exception = kgdboc_post_exp_handler,
  127. };
  128. module_init(init_kgdboc);
  129. module_exit(cleanup_kgdboc);
  130. module_param_call(kgdboc, param_set_kgdboc_var, param_get_string, &kps, 0644);
  131. MODULE_PARM_DESC(kgdboc, "<serial_device>[,baud]");
  132. MODULE_DESCRIPTION("KGDB Console TTY Driver");
  133. MODULE_LICENSE("GPL");