nfcon.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * ARAnyM console driver
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License. See the file COPYING in the main directory of this archive
  6. * for more details.
  7. */
  8. #include <linux/module.h>
  9. #include <linux/init.h>
  10. #include <linux/console.h>
  11. #include <linux/tty.h>
  12. #include <linux/tty_driver.h>
  13. #include <linux/tty_flip.h>
  14. #include <linux/slab.h>
  15. #include <linux/err.h>
  16. #include <linux/uaccess.h>
  17. #include <asm/natfeat.h>
  18. static int stderr_id;
  19. static struct tty_driver *nfcon_tty_driver;
  20. static void nfputs(const char *str, unsigned int count)
  21. {
  22. char buf[68];
  23. buf[64] = 0;
  24. while (count > 64) {
  25. memcpy(buf, str, 64);
  26. nf_call(stderr_id, buf);
  27. str += 64;
  28. count -= 64;
  29. }
  30. memcpy(buf, str, count);
  31. buf[count] = 0;
  32. nf_call(stderr_id, buf);
  33. }
  34. static void nfcon_write(struct console *con, const char *str,
  35. unsigned int count)
  36. {
  37. nfputs(str, count);
  38. }
  39. static struct tty_driver *nfcon_device(struct console *con, int *index)
  40. {
  41. *index = 0;
  42. return (con->flags & CON_ENABLED) ? nfcon_tty_driver : NULL;
  43. }
  44. static struct console nf_console = {
  45. .name = "nfcon",
  46. .write = nfcon_write,
  47. .device = nfcon_device,
  48. .flags = CON_PRINTBUFFER,
  49. .index = -1,
  50. };
  51. static int nfcon_tty_open(struct tty_struct *tty, struct file *filp)
  52. {
  53. return 0;
  54. }
  55. static void nfcon_tty_close(struct tty_struct *tty, struct file *filp)
  56. {
  57. }
  58. static int nfcon_tty_write(struct tty_struct *tty, const unsigned char *buf,
  59. int count)
  60. {
  61. nfputs(buf, count);
  62. return count;
  63. }
  64. static int nfcon_tty_put_char(struct tty_struct *tty, unsigned char ch)
  65. {
  66. char temp[2] = { ch, 0 };
  67. nf_call(stderr_id, temp);
  68. return 1;
  69. }
  70. static int nfcon_tty_write_room(struct tty_struct *tty)
  71. {
  72. return 64;
  73. }
  74. static const struct tty_operations nfcon_tty_ops = {
  75. .open = nfcon_tty_open,
  76. .close = nfcon_tty_close,
  77. .write = nfcon_tty_write,
  78. .put_char = nfcon_tty_put_char,
  79. .write_room = nfcon_tty_write_room,
  80. };
  81. #ifndef MODULE
  82. static int __init nf_debug_setup(char *arg)
  83. {
  84. if (strcmp(arg, "nfcon"))
  85. return 0;
  86. stderr_id = nf_get_id("NF_STDERR");
  87. if (stderr_id) {
  88. nf_console.flags |= CON_ENABLED;
  89. register_console(&nf_console);
  90. }
  91. return 0;
  92. }
  93. early_param("debug", nf_debug_setup);
  94. #endif /* !MODULE */
  95. static int __init nfcon_init(void)
  96. {
  97. int res;
  98. stderr_id = nf_get_id("NF_STDERR");
  99. if (!stderr_id)
  100. return -ENODEV;
  101. nfcon_tty_driver = alloc_tty_driver(1);
  102. if (!nfcon_tty_driver)
  103. return -ENOMEM;
  104. nfcon_tty_driver->driver_name = "nfcon";
  105. nfcon_tty_driver->name = "nfcon";
  106. nfcon_tty_driver->type = TTY_DRIVER_TYPE_SYSTEM;
  107. nfcon_tty_driver->subtype = SYSTEM_TYPE_TTY;
  108. nfcon_tty_driver->init_termios = tty_std_termios;
  109. nfcon_tty_driver->flags = TTY_DRIVER_REAL_RAW;
  110. tty_set_operations(nfcon_tty_driver, &nfcon_tty_ops);
  111. res = tty_register_driver(nfcon_tty_driver);
  112. if (res) {
  113. pr_err("failed to register nfcon tty driver\n");
  114. put_tty_driver(nfcon_tty_driver);
  115. return res;
  116. }
  117. if (!(nf_console.flags & CON_ENABLED))
  118. register_console(&nf_console);
  119. return 0;
  120. }
  121. static void __exit nfcon_exit(void)
  122. {
  123. unregister_console(&nf_console);
  124. tty_unregister_driver(nfcon_tty_driver);
  125. put_tty_driver(nfcon_tty_driver);
  126. }
  127. module_init(nfcon_init);
  128. module_exit(nfcon_exit);
  129. MODULE_LICENSE("GPL");