ttyprintk.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * linux/drivers/char/ttyprintk.c
  3. *
  4. * Copyright (C) 2010 Samo Pogacnik
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the smems of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 of the License.
  9. */
  10. /*
  11. * This pseudo device allows user to make printk messages. It is possible
  12. * to store "console" messages inline with kernel messages for better analyses
  13. * of the boot process, for example.
  14. */
  15. #include <linux/device.h>
  16. #include <linux/serial.h>
  17. #include <linux/tty.h>
  18. #include <linux/module.h>
  19. #include <linux/spinlock.h>
  20. struct ttyprintk_port {
  21. struct tty_port port;
  22. spinlock_t spinlock;
  23. };
  24. static struct ttyprintk_port tpk_port;
  25. /*
  26. * Our simple preformatting supports transparent output of (time-stamped)
  27. * printk messages (also suitable for logging service):
  28. * - any cr is replaced by nl
  29. * - adds a ttyprintk source tag in front of each line
  30. * - too long message is fragmented, with '\'nl between fragments
  31. * - TPK_STR_SIZE isn't really the write_room limiting factor, because
  32. * it is emptied on the fly during preformatting.
  33. */
  34. #define TPK_STR_SIZE 508 /* should be bigger then max expected line length */
  35. #define TPK_MAX_ROOM 4096 /* we could assume 4K for instance */
  36. static int tpk_curr;
  37. static char tpk_buffer[TPK_STR_SIZE + 4];
  38. static void tpk_flush(void)
  39. {
  40. if (tpk_curr > 0) {
  41. tpk_buffer[tpk_curr] = '\0';
  42. pr_info("[U] %s\n", tpk_buffer);
  43. tpk_curr = 0;
  44. }
  45. }
  46. static int tpk_printk(const unsigned char *buf, int count)
  47. {
  48. int i = tpk_curr;
  49. if (buf == NULL) {
  50. tpk_flush();
  51. return i;
  52. }
  53. for (i = 0; i < count; i++) {
  54. if (tpk_curr >= TPK_STR_SIZE) {
  55. /* end of tmp buffer reached: cut the message in two */
  56. tpk_buffer[tpk_curr++] = '\\';
  57. tpk_flush();
  58. }
  59. switch (buf[i]) {
  60. case '\r':
  61. tpk_flush();
  62. if ((i + 1) < count && buf[i + 1] == '\n')
  63. i++;
  64. break;
  65. case '\n':
  66. tpk_flush();
  67. break;
  68. default:
  69. tpk_buffer[tpk_curr++] = buf[i];
  70. break;
  71. }
  72. }
  73. return count;
  74. }
  75. /*
  76. * TTY operations open function.
  77. */
  78. static int tpk_open(struct tty_struct *tty, struct file *filp)
  79. {
  80. tty->driver_data = &tpk_port;
  81. return tty_port_open(&tpk_port.port, tty, filp);
  82. }
  83. /*
  84. * TTY operations close function.
  85. */
  86. static void tpk_close(struct tty_struct *tty, struct file *filp)
  87. {
  88. struct ttyprintk_port *tpkp = tty->driver_data;
  89. unsigned long flags;
  90. spin_lock_irqsave(&tpkp->spinlock, flags);
  91. /* flush tpk_printk buffer */
  92. tpk_printk(NULL, 0);
  93. spin_unlock_irqrestore(&tpkp->spinlock, flags);
  94. tty_port_close(&tpkp->port, tty, filp);
  95. }
  96. /*
  97. * TTY operations write function.
  98. */
  99. static int tpk_write(struct tty_struct *tty,
  100. const unsigned char *buf, int count)
  101. {
  102. struct ttyprintk_port *tpkp = tty->driver_data;
  103. unsigned long flags;
  104. int ret;
  105. /* exclusive use of tpk_printk within this tty */
  106. spin_lock_irqsave(&tpkp->spinlock, flags);
  107. ret = tpk_printk(buf, count);
  108. spin_unlock_irqrestore(&tpkp->spinlock, flags);
  109. return ret;
  110. }
  111. /*
  112. * TTY operations write_room function.
  113. */
  114. static int tpk_write_room(struct tty_struct *tty)
  115. {
  116. return TPK_MAX_ROOM;
  117. }
  118. /*
  119. * TTY operations ioctl function.
  120. */
  121. static int tpk_ioctl(struct tty_struct *tty,
  122. unsigned int cmd, unsigned long arg)
  123. {
  124. struct ttyprintk_port *tpkp = tty->driver_data;
  125. if (!tpkp)
  126. return -EINVAL;
  127. switch (cmd) {
  128. /* Stop TIOCCONS */
  129. case TIOCCONS:
  130. return -EOPNOTSUPP;
  131. default:
  132. return -ENOIOCTLCMD;
  133. }
  134. return 0;
  135. }
  136. static const struct tty_operations ttyprintk_ops = {
  137. .open = tpk_open,
  138. .close = tpk_close,
  139. .write = tpk_write,
  140. .write_room = tpk_write_room,
  141. .ioctl = tpk_ioctl,
  142. };
  143. static const struct tty_port_operations null_ops = { };
  144. static struct tty_driver *ttyprintk_driver;
  145. static int __init ttyprintk_init(void)
  146. {
  147. int ret = -ENOMEM;
  148. spin_lock_init(&tpk_port.spinlock);
  149. ttyprintk_driver = tty_alloc_driver(1,
  150. TTY_DRIVER_RESET_TERMIOS |
  151. TTY_DRIVER_REAL_RAW |
  152. TTY_DRIVER_UNNUMBERED_NODE);
  153. if (IS_ERR(ttyprintk_driver))
  154. return PTR_ERR(ttyprintk_driver);
  155. tty_port_init(&tpk_port.port);
  156. tpk_port.port.ops = &null_ops;
  157. ttyprintk_driver->driver_name = "ttyprintk";
  158. ttyprintk_driver->name = "ttyprintk";
  159. ttyprintk_driver->major = TTYAUX_MAJOR;
  160. ttyprintk_driver->minor_start = 3;
  161. ttyprintk_driver->type = TTY_DRIVER_TYPE_CONSOLE;
  162. ttyprintk_driver->init_termios = tty_std_termios;
  163. ttyprintk_driver->init_termios.c_oflag = OPOST | OCRNL | ONOCR | ONLRET;
  164. tty_set_operations(ttyprintk_driver, &ttyprintk_ops);
  165. tty_port_link_device(&tpk_port.port, ttyprintk_driver, 0);
  166. ret = tty_register_driver(ttyprintk_driver);
  167. if (ret < 0) {
  168. printk(KERN_ERR "Couldn't register ttyprintk driver\n");
  169. goto error;
  170. }
  171. return 0;
  172. error:
  173. put_tty_driver(ttyprintk_driver);
  174. tty_port_destroy(&tpk_port.port);
  175. return ret;
  176. }
  177. static void __exit ttyprintk_exit(void)
  178. {
  179. tty_unregister_driver(ttyprintk_driver);
  180. put_tty_driver(ttyprintk_driver);
  181. tty_port_destroy(&tpk_port.port);
  182. }
  183. device_initcall(ttyprintk_init);
  184. module_exit(ttyprintk_exit);
  185. MODULE_LICENSE("GPL");