kgdboc.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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->ops->poll_get_char(kgdb_tty_driver,
  78. kgdb_tty_line);
  79. }
  80. static void kgdboc_put_char(u8 chr)
  81. {
  82. kgdb_tty_driver->ops->poll_put_char(kgdb_tty_driver,
  83. kgdb_tty_line, chr);
  84. }
  85. static int param_set_kgdboc_var(const char *kmessage, struct kernel_param *kp)
  86. {
  87. int len = strlen(kmessage);
  88. if (len >= MAX_CONFIG_LEN) {
  89. printk(KERN_ERR "kgdboc: config string too long\n");
  90. return -ENOSPC;
  91. }
  92. /* Only copy in the string if the init function has not run yet */
  93. if (configured < 0) {
  94. strcpy(config, kmessage);
  95. return 0;
  96. }
  97. if (kgdb_connected) {
  98. printk(KERN_ERR
  99. "kgdboc: Cannot reconfigure while KGDB is connected.\n");
  100. return -EBUSY;
  101. }
  102. strcpy(config, kmessage);
  103. /* Chop out \n char as a result of echo */
  104. if (config[len - 1] == '\n')
  105. config[len - 1] = '\0';
  106. if (configured == 1)
  107. cleanup_kgdboc();
  108. /* Go and configure with the new params. */
  109. return configure_kgdboc();
  110. }
  111. static void kgdboc_pre_exp_handler(void)
  112. {
  113. /* Increment the module count when the debugger is active */
  114. if (!kgdb_connected)
  115. try_module_get(THIS_MODULE);
  116. }
  117. static void kgdboc_post_exp_handler(void)
  118. {
  119. /* decrement the module count when the debugger detaches */
  120. if (!kgdb_connected)
  121. module_put(THIS_MODULE);
  122. }
  123. static struct kgdb_io kgdboc_io_ops = {
  124. .name = "kgdboc",
  125. .read_char = kgdboc_get_char,
  126. .write_char = kgdboc_put_char,
  127. .pre_exception = kgdboc_pre_exp_handler,
  128. .post_exception = kgdboc_post_exp_handler,
  129. };
  130. module_init(init_kgdboc);
  131. module_exit(cleanup_kgdboc);
  132. module_param_call(kgdboc, param_set_kgdboc_var, param_get_string, &kps, 0644);
  133. MODULE_PARM_DESC(kgdboc, "<serial_device>[,baud]");
  134. MODULE_DESCRIPTION("KGDB Console TTY Driver");
  135. MODULE_LICENSE("GPL");