nfcon.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 u8 *str, size_t count)
  23. {
  24. u8 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 console_is_registered(con) ? 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 ssize_t nfcon_tty_write(struct tty_struct *tty, const u8 *buf,
  62. size_t count)
  63. {
  64. nfputs(buf, count);
  65. return count;
  66. }
  67. static int nfcon_tty_put_char(struct tty_struct *tty, u8 ch)
  68. {
  69. u8 temp[2] = { ch, 0 };
  70. nf_call(stderr_id, virt_to_phys(temp));
  71. return 1;
  72. }
  73. static unsigned 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. /*
  92. * The console will be enabled when debug=nfcon is specified
  93. * as a kernel parameter. Since this is a non-standard way
  94. * of enabling consoles, it must be explicitly enabled.
  95. */
  96. nf_console.flags |= CON_ENABLED;
  97. register_console(&nf_console);
  98. }
  99. return 0;
  100. }
  101. early_param("debug", nf_debug_setup);
  102. #endif /* !MODULE */
  103. static int __init nfcon_init(void)
  104. {
  105. struct tty_driver *driver;
  106. int res;
  107. stderr_id = nf_get_id("NF_STDERR");
  108. if (!stderr_id)
  109. return -ENODEV;
  110. driver = tty_alloc_driver(1, TTY_DRIVER_REAL_RAW);
  111. if (IS_ERR(driver))
  112. return PTR_ERR(driver);
  113. tty_port_init(&nfcon_tty_port);
  114. driver->driver_name = "nfcon";
  115. driver->name = "nfcon";
  116. driver->type = TTY_DRIVER_TYPE_SYSTEM;
  117. driver->subtype = SYSTEM_TYPE_TTY;
  118. driver->init_termios = tty_std_termios;
  119. tty_set_operations(driver, &nfcon_tty_ops);
  120. tty_port_link_device(&nfcon_tty_port, driver, 0);
  121. res = tty_register_driver(driver);
  122. if (res) {
  123. pr_err("failed to register nfcon tty driver\n");
  124. tty_driver_kref_put(driver);
  125. tty_port_destroy(&nfcon_tty_port);
  126. return res;
  127. }
  128. nfcon_tty_driver = driver;
  129. if (!console_is_registered(&nf_console))
  130. register_console(&nf_console);
  131. return 0;
  132. }
  133. static void __exit nfcon_exit(void)
  134. {
  135. unregister_console(&nf_console);
  136. tty_unregister_driver(nfcon_tty_driver);
  137. tty_driver_kref_put(nfcon_tty_driver);
  138. tty_port_destroy(&nfcon_tty_port);
  139. }
  140. module_init(nfcon_init);
  141. module_exit(nfcon_exit);
  142. MODULE_DESCRIPTION("Atari NatFeat console driver");
  143. MODULE_LICENSE("GPL");