kgdboc.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. #define MAX_CONFIG_LEN 40
  21. static struct kgdb_io kgdboc_io_ops;
  22. /* -1 = init not run yet, 0 = unconfigured, 1 = configured. */
  23. static int configured = -1;
  24. static char config[MAX_CONFIG_LEN];
  25. static struct kparam_string kps = {
  26. .string = config,
  27. .maxlen = MAX_CONFIG_LEN,
  28. };
  29. static struct tty_driver *kgdb_tty_driver;
  30. static int kgdb_tty_line;
  31. #ifdef CONFIG_KDB_KEYBOARD
  32. static int kgdboc_register_kbd(char **cptr)
  33. {
  34. if (strncmp(*cptr, "kbd", 3) == 0) {
  35. if (kdb_poll_idx < KDB_POLL_FUNC_MAX) {
  36. kdb_poll_funcs[kdb_poll_idx] = kdb_get_kbd_char;
  37. kdb_poll_idx++;
  38. if (cptr[0][3] == ',')
  39. *cptr += 4;
  40. else
  41. return 1;
  42. }
  43. }
  44. return 0;
  45. }
  46. static void kgdboc_unregister_kbd(void)
  47. {
  48. int i;
  49. for (i = 0; i < kdb_poll_idx; i++) {
  50. if (kdb_poll_funcs[i] == kdb_get_kbd_char) {
  51. kdb_poll_idx--;
  52. kdb_poll_funcs[i] = kdb_poll_funcs[kdb_poll_idx];
  53. kdb_poll_funcs[kdb_poll_idx] = NULL;
  54. i--;
  55. }
  56. }
  57. }
  58. #else /* ! CONFIG_KDB_KEYBOARD */
  59. #define kgdboc_register_kbd(x) 0
  60. #define kgdboc_unregister_kbd()
  61. #endif /* ! CONFIG_KDB_KEYBOARD */
  62. static int kgdboc_option_setup(char *opt)
  63. {
  64. if (strlen(opt) > MAX_CONFIG_LEN) {
  65. printk(KERN_ERR "kgdboc: config string too long\n");
  66. return -ENOSPC;
  67. }
  68. strcpy(config, opt);
  69. return 0;
  70. }
  71. __setup("kgdboc=", kgdboc_option_setup);
  72. static void cleanup_kgdboc(void)
  73. {
  74. kgdboc_unregister_kbd();
  75. if (configured == 1)
  76. kgdb_unregister_io_module(&kgdboc_io_ops);
  77. }
  78. static int configure_kgdboc(void)
  79. {
  80. struct tty_driver *p;
  81. int tty_line = 0;
  82. int err;
  83. char *cptr = config;
  84. struct console *cons;
  85. err = kgdboc_option_setup(config);
  86. if (err || !strlen(config) || isspace(config[0]))
  87. goto noconfig;
  88. err = -ENODEV;
  89. kgdboc_io_ops.is_console = 0;
  90. kgdb_tty_driver = NULL;
  91. if (kgdboc_register_kbd(&cptr))
  92. goto do_register;
  93. p = tty_find_polling_driver(cptr, &tty_line);
  94. if (!p)
  95. goto noconfig;
  96. cons = console_drivers;
  97. while (cons) {
  98. int idx;
  99. if (cons->device && cons->device(cons, &idx) == p &&
  100. idx == tty_line) {
  101. kgdboc_io_ops.is_console = 1;
  102. break;
  103. }
  104. cons = cons->next;
  105. }
  106. kgdb_tty_driver = p;
  107. kgdb_tty_line = tty_line;
  108. do_register:
  109. err = kgdb_register_io_module(&kgdboc_io_ops);
  110. if (err)
  111. goto noconfig;
  112. configured = 1;
  113. return 0;
  114. noconfig:
  115. config[0] = 0;
  116. configured = 0;
  117. cleanup_kgdboc();
  118. return err;
  119. }
  120. static int __init init_kgdboc(void)
  121. {
  122. /* Already configured? */
  123. if (configured == 1)
  124. return 0;
  125. return configure_kgdboc();
  126. }
  127. static int kgdboc_get_char(void)
  128. {
  129. if (!kgdb_tty_driver)
  130. return -1;
  131. return kgdb_tty_driver->ops->poll_get_char(kgdb_tty_driver,
  132. kgdb_tty_line);
  133. }
  134. static void kgdboc_put_char(u8 chr)
  135. {
  136. if (!kgdb_tty_driver)
  137. return;
  138. kgdb_tty_driver->ops->poll_put_char(kgdb_tty_driver,
  139. kgdb_tty_line, chr);
  140. }
  141. static int param_set_kgdboc_var(const char *kmessage, struct kernel_param *kp)
  142. {
  143. int len = strlen(kmessage);
  144. if (len >= MAX_CONFIG_LEN) {
  145. printk(KERN_ERR "kgdboc: config string too long\n");
  146. return -ENOSPC;
  147. }
  148. /* Only copy in the string if the init function has not run yet */
  149. if (configured < 0) {
  150. strcpy(config, kmessage);
  151. return 0;
  152. }
  153. if (kgdb_connected) {
  154. printk(KERN_ERR
  155. "kgdboc: Cannot reconfigure while KGDB is connected.\n");
  156. return -EBUSY;
  157. }
  158. strcpy(config, kmessage);
  159. /* Chop out \n char as a result of echo */
  160. if (config[len - 1] == '\n')
  161. config[len - 1] = '\0';
  162. if (configured == 1)
  163. cleanup_kgdboc();
  164. /* Go and configure with the new params. */
  165. return configure_kgdboc();
  166. }
  167. static void kgdboc_pre_exp_handler(void)
  168. {
  169. /* Increment the module count when the debugger is active */
  170. if (!kgdb_connected)
  171. try_module_get(THIS_MODULE);
  172. }
  173. static void kgdboc_post_exp_handler(void)
  174. {
  175. /* decrement the module count when the debugger detaches */
  176. if (!kgdb_connected)
  177. module_put(THIS_MODULE);
  178. }
  179. static struct kgdb_io kgdboc_io_ops = {
  180. .name = "kgdboc",
  181. .read_char = kgdboc_get_char,
  182. .write_char = kgdboc_put_char,
  183. .pre_exception = kgdboc_pre_exp_handler,
  184. .post_exception = kgdboc_post_exp_handler,
  185. };
  186. #ifdef CONFIG_KGDB_SERIAL_CONSOLE
  187. /* This is only available if kgdboc is a built in for early debugging */
  188. int __init kgdboc_early_init(char *opt)
  189. {
  190. /* save the first character of the config string because the
  191. * init routine can destroy it.
  192. */
  193. char save_ch;
  194. kgdboc_option_setup(opt);
  195. save_ch = config[0];
  196. init_kgdboc();
  197. config[0] = save_ch;
  198. return 0;
  199. }
  200. early_param("ekgdboc", kgdboc_early_init);
  201. #endif /* CONFIG_KGDB_SERIAL_CONSOLE */
  202. module_init(init_kgdboc);
  203. module_exit(cleanup_kgdboc);
  204. module_param_call(kgdboc, param_set_kgdboc_var, param_get_string, &kps, 0644);
  205. MODULE_PARM_DESC(kgdboc, "<serial_device>[,baud]");
  206. MODULE_DESCRIPTION("KGDB Console TTY Driver");
  207. MODULE_LICENSE("GPL");