nfcon.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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_port nfcon_tty_port;
  20. static struct tty_driver *nfcon_tty_driver;
  21. static void nfputs(const char *str, unsigned int count)
  22. {
  23. char buf[68];
  24. buf[64] = 0;
  25. while (count > 64) {
  26. memcpy(buf, str, 64);
  27. nf_call(stderr_id, buf);
  28. str += 64;
  29. count -= 64;
  30. }
  31. memcpy(buf, str, count);
  32. buf[count] = 0;
  33. nf_call(stderr_id, buf);
  34. }
  35. static void nfcon_write(struct console *con, const char *str,
  36. unsigned int count)
  37. {
  38. nfputs(str, count);
  39. }
  40. static struct tty_driver *nfcon_device(struct console *con, int *index)
  41. {
  42. *index = 0;
  43. return (con->flags & CON_ENABLED) ? nfcon_tty_driver : NULL;
  44. }
  45. static struct console nf_console = {
  46. .name = "nfcon",
  47. .write = nfcon_write,
  48. .device = nfcon_device,
  49. .flags = CON_PRINTBUFFER,
  50. .index = -1,
  51. };
  52. static int nfcon_tty_open(struct tty_struct *tty, struct file *filp)
  53. {
  54. return 0;
  55. }
  56. static void nfcon_tty_close(struct tty_struct *tty, struct file *filp)
  57. {
  58. }
  59. static int nfcon_tty_write(struct tty_struct *tty, const unsigned char *buf,
  60. int count)
  61. {
  62. nfputs(buf, count);
  63. return count;
  64. }
  65. static int nfcon_tty_put_char(struct tty_struct *tty, unsigned char ch)
  66. {
  67. char temp[2] = { ch, 0 };
  68. nf_call(stderr_id, temp);
  69. return 1;
  70. }
  71. static int nfcon_tty_write_room(struct tty_struct *tty)
  72. {
  73. return 64;
  74. }
  75. static const struct tty_operations nfcon_tty_ops = {
  76. .open = nfcon_tty_open,
  77. .close = nfcon_tty_close,
  78. .write = nfcon_tty_write,
  79. .put_char = nfcon_tty_put_char,
  80. .write_room = nfcon_tty_write_room,
  81. };
  82. #ifndef MODULE
  83. static int __init nf_debug_setup(char *arg)
  84. {
  85. if (strcmp(arg, "nfcon"))
  86. return 0;
  87. stderr_id = nf_get_id("NF_STDERR");
  88. if (stderr_id) {
  89. nf_console.flags |= CON_ENABLED;
  90. register_console(&nf_console);
  91. }
  92. return 0;
  93. }
  94. early_param("debug", nf_debug_setup);
  95. #endif /* !MODULE */
  96. static int __init nfcon_init(void)
  97. {
  98. int res;
  99. stderr_id = nf_get_id("NF_STDERR");
  100. if (!stderr_id)
  101. return -ENODEV;
  102. nfcon_tty_driver = alloc_tty_driver(1);
  103. if (!nfcon_tty_driver)
  104. return -ENOMEM;
  105. tty_port_init(&nfcon_tty_port);
  106. nfcon_tty_driver->driver_name = "nfcon";
  107. nfcon_tty_driver->name = "nfcon";
  108. nfcon_tty_driver->type = TTY_DRIVER_TYPE_SYSTEM;
  109. nfcon_tty_driver->subtype = SYSTEM_TYPE_TTY;
  110. nfcon_tty_driver->init_termios = tty_std_termios;
  111. nfcon_tty_driver->flags = TTY_DRIVER_REAL_RAW;
  112. tty_set_operations(nfcon_tty_driver, &nfcon_tty_ops);
  113. tty_port_link_device(&nfcon_tty_port, nfcon_tty_driver, 0);
  114. res = tty_register_driver(nfcon_tty_driver);
  115. if (res) {
  116. pr_err("failed to register nfcon tty driver\n");
  117. put_tty_driver(nfcon_tty_driver);
  118. tty_port_destroy(&nfcon_tty_port);
  119. return res;
  120. }
  121. if (!(nf_console.flags & CON_ENABLED))
  122. register_console(&nf_console);
  123. return 0;
  124. }
  125. static void __exit nfcon_exit(void)
  126. {
  127. unregister_console(&nf_console);
  128. tty_unregister_driver(nfcon_tty_driver);
  129. put_tty_driver(nfcon_tty_driver);
  130. tty_port_destroy(&nfcon_tty_port);
  131. }
  132. module_init(nfcon_init);
  133. module_exit(nfcon_exit);
  134. MODULE_LICENSE("GPL");