nfcon.c 3.4 KB

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