dcc_tty.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /* drivers/char/dcc_tty.c
  2. *
  3. * Copyright (C) 2007 Google, Inc.
  4. *
  5. * This software is licensed under the terms of the GNU General Public
  6. * License version 2, as published by the Free Software Foundation, and
  7. * may be copied, distributed, and modified under those terms.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. */
  15. #include <linux/module.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/delay.h>
  18. #include <linux/console.h>
  19. #include <linux/hrtimer.h>
  20. #include <linux/tty.h>
  21. #include <linux/tty_driver.h>
  22. #include <linux/tty_flip.h>
  23. MODULE_DESCRIPTION("DCC TTY Driver");
  24. MODULE_LICENSE("GPL");
  25. MODULE_VERSION("1.0");
  26. static spinlock_t g_dcc_tty_lock = SPIN_LOCK_UNLOCKED;
  27. static struct hrtimer g_dcc_timer;
  28. static char g_dcc_buffer[16];
  29. static int g_dcc_buffer_head;
  30. static int g_dcc_buffer_count;
  31. static unsigned g_dcc_write_delay_usecs = 1;
  32. static struct tty_driver *g_dcc_tty_driver;
  33. static struct tty_struct *g_dcc_tty;
  34. static int g_dcc_tty_open_count;
  35. static void dcc_poll_locked(void)
  36. {
  37. char ch;
  38. int rch;
  39. int written;
  40. while (g_dcc_buffer_count) {
  41. ch = g_dcc_buffer[g_dcc_buffer_head];
  42. asm(
  43. "mrc 14, 0, r15, c0, c1, 0\n"
  44. "mcrcc 14, 0, %1, c0, c5, 0\n"
  45. "movcc %0, #1\n"
  46. "movcs %0, #0\n"
  47. : "=r" (written)
  48. : "r" (ch)
  49. );
  50. if (written) {
  51. if (ch == '\n')
  52. g_dcc_buffer[g_dcc_buffer_head] = '\r';
  53. else {
  54. g_dcc_buffer_head = (g_dcc_buffer_head + 1) % ARRAY_SIZE(g_dcc_buffer);
  55. g_dcc_buffer_count--;
  56. if (g_dcc_tty)
  57. tty_wakeup(g_dcc_tty);
  58. }
  59. g_dcc_write_delay_usecs = 1;
  60. } else {
  61. if (g_dcc_write_delay_usecs > 0x100)
  62. break;
  63. g_dcc_write_delay_usecs <<= 1;
  64. udelay(g_dcc_write_delay_usecs);
  65. }
  66. }
  67. if (g_dcc_tty && !test_bit(TTY_THROTTLED, &g_dcc_tty->flags)) {
  68. asm(
  69. "mrc 14, 0, %0, c0, c1, 0\n"
  70. "tst %0, #(1 << 30)\n"
  71. "moveq %0, #-1\n"
  72. "mrcne 14, 0, %0, c0, c5, 0\n"
  73. : "=r" (rch)
  74. );
  75. if (rch >= 0) {
  76. ch = rch;
  77. tty_insert_flip_string(g_dcc_tty, &ch, 1);
  78. tty_flip_buffer_push(g_dcc_tty);
  79. }
  80. }
  81. if (g_dcc_buffer_count)
  82. hrtimer_start(&g_dcc_timer, ktime_set(0, g_dcc_write_delay_usecs * NSEC_PER_USEC), HRTIMER_MODE_REL);
  83. else
  84. hrtimer_start(&g_dcc_timer, ktime_set(0, 20 * NSEC_PER_MSEC), HRTIMER_MODE_REL);
  85. }
  86. static int dcc_tty_open(struct tty_struct * tty, struct file * filp)
  87. {
  88. int ret;
  89. unsigned long irq_flags;
  90. spin_lock_irqsave(&g_dcc_tty_lock, irq_flags);
  91. if (g_dcc_tty == NULL || g_dcc_tty == tty) {
  92. g_dcc_tty = tty;
  93. g_dcc_tty_open_count++;
  94. ret = 0;
  95. } else
  96. ret = -EBUSY;
  97. spin_unlock_irqrestore(&g_dcc_tty_lock, irq_flags);
  98. printk("dcc_tty_open, tty %p, f_flags %x, returned %d\n", tty, filp->f_flags, ret);
  99. return ret;
  100. }
  101. static void dcc_tty_close(struct tty_struct * tty, struct file * filp)
  102. {
  103. printk("dcc_tty_close, tty %p, f_flags %x\n", tty, filp->f_flags);
  104. if (g_dcc_tty == tty) {
  105. if (--g_dcc_tty_open_count == 0)
  106. g_dcc_tty = NULL;
  107. }
  108. }
  109. static int dcc_write(const unsigned char *buf_start, int count)
  110. {
  111. const unsigned char *buf = buf_start;
  112. unsigned long irq_flags;
  113. int copy_len;
  114. int space_left;
  115. int tail;
  116. if (count < 1)
  117. return 0;
  118. spin_lock_irqsave(&g_dcc_tty_lock, irq_flags);
  119. do {
  120. tail = (g_dcc_buffer_head + g_dcc_buffer_count) % ARRAY_SIZE(g_dcc_buffer);
  121. copy_len = ARRAY_SIZE(g_dcc_buffer) - tail;
  122. space_left = ARRAY_SIZE(g_dcc_buffer) - g_dcc_buffer_count;
  123. if (copy_len > space_left)
  124. copy_len = space_left;
  125. if (copy_len > count)
  126. copy_len = count;
  127. memcpy(&g_dcc_buffer[tail], buf, copy_len);
  128. g_dcc_buffer_count += copy_len;
  129. buf += copy_len;
  130. count -= copy_len;
  131. if (copy_len < count && copy_len < space_left) {
  132. space_left -= copy_len;
  133. copy_len = count;
  134. if (copy_len > space_left) {
  135. copy_len = space_left;
  136. }
  137. memcpy(g_dcc_buffer, buf, copy_len);
  138. buf += copy_len;
  139. count -= copy_len;
  140. g_dcc_buffer_count += copy_len;
  141. }
  142. dcc_poll_locked();
  143. space_left = ARRAY_SIZE(g_dcc_buffer) - g_dcc_buffer_count;
  144. } while(count && space_left);
  145. spin_unlock_irqrestore(&g_dcc_tty_lock, irq_flags);
  146. return buf - buf_start;
  147. }
  148. static int dcc_tty_write(struct tty_struct * tty, const unsigned char *buf, int count)
  149. {
  150. int ret;
  151. /* printk("dcc_tty_write %p, %d\n", buf, count); */
  152. ret = dcc_write(buf, count);
  153. if (ret != count)
  154. printk("dcc_tty_write %p, %d, returned %d\n", buf, count, ret);
  155. return ret;
  156. }
  157. static int dcc_tty_write_room(struct tty_struct *tty)
  158. {
  159. int space_left;
  160. unsigned long irq_flags;
  161. spin_lock_irqsave(&g_dcc_tty_lock, irq_flags);
  162. space_left = ARRAY_SIZE(g_dcc_buffer) - g_dcc_buffer_count;
  163. spin_unlock_irqrestore(&g_dcc_tty_lock, irq_flags);
  164. return space_left;
  165. }
  166. static int dcc_tty_chars_in_buffer(struct tty_struct *tty)
  167. {
  168. int ret;
  169. asm(
  170. "mrc 14, 0, %0, c0, c1, 0\n"
  171. "mov %0, %0, LSR #30\n"
  172. "and %0, %0, #1\n"
  173. : "=r" (ret)
  174. );
  175. return ret;
  176. }
  177. static void dcc_tty_unthrottle(struct tty_struct * tty)
  178. {
  179. unsigned long irq_flags;
  180. spin_lock_irqsave(&g_dcc_tty_lock, irq_flags);
  181. dcc_poll_locked();
  182. spin_unlock_irqrestore(&g_dcc_tty_lock, irq_flags);
  183. }
  184. static enum hrtimer_restart dcc_tty_timer_func(struct hrtimer *timer)
  185. {
  186. unsigned long irq_flags;
  187. spin_lock_irqsave(&g_dcc_tty_lock, irq_flags);
  188. dcc_poll_locked();
  189. spin_unlock_irqrestore(&g_dcc_tty_lock, irq_flags);
  190. return HRTIMER_NORESTART;
  191. }
  192. void dcc_console_write(struct console *co, const char *b, unsigned count)
  193. {
  194. #if 1
  195. dcc_write(b, count);
  196. #else
  197. /* blocking printk */
  198. while (count > 0) {
  199. int written;
  200. written = dcc_write(b, count);
  201. if (written) {
  202. b += written;
  203. count -= written;
  204. }
  205. }
  206. #endif
  207. }
  208. static struct tty_driver *dcc_console_device(struct console *c, int *index)
  209. {
  210. *index = 0;
  211. return g_dcc_tty_driver;
  212. }
  213. static int __init dcc_console_setup(struct console *co, char *options)
  214. {
  215. if (co->index != 0)
  216. return -ENODEV;
  217. return 0;
  218. }
  219. static struct console dcc_console =
  220. {
  221. .name = "ttyDCC",
  222. .write = dcc_console_write,
  223. .device = dcc_console_device,
  224. .setup = dcc_console_setup,
  225. .flags = CON_PRINTBUFFER,
  226. .index = -1,
  227. };
  228. static struct tty_operations dcc_tty_ops = {
  229. .open = dcc_tty_open,
  230. .close = dcc_tty_close,
  231. .write = dcc_tty_write,
  232. .write_room = dcc_tty_write_room,
  233. .chars_in_buffer = dcc_tty_chars_in_buffer,
  234. .unthrottle = dcc_tty_unthrottle,
  235. };
  236. static int __init dcc_tty_init(void)
  237. {
  238. int ret;
  239. hrtimer_init(&g_dcc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  240. g_dcc_timer.function = dcc_tty_timer_func;
  241. g_dcc_tty_driver = alloc_tty_driver(1);
  242. if (!g_dcc_tty_driver) {
  243. printk(KERN_ERR "dcc_tty_probe: alloc_tty_driver failed\n");
  244. ret = -ENOMEM;
  245. goto err_alloc_tty_driver_failed;
  246. }
  247. g_dcc_tty_driver->owner = THIS_MODULE;
  248. g_dcc_tty_driver->driver_name = "dcc";
  249. g_dcc_tty_driver->name = "ttyDCC";
  250. g_dcc_tty_driver->major = 0; // auto assign
  251. g_dcc_tty_driver->minor_start = 0;
  252. g_dcc_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
  253. g_dcc_tty_driver->subtype = SERIAL_TYPE_NORMAL;
  254. g_dcc_tty_driver->init_termios = tty_std_termios;
  255. g_dcc_tty_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  256. tty_set_operations(g_dcc_tty_driver, &dcc_tty_ops);
  257. ret = tty_register_driver(g_dcc_tty_driver);
  258. if (ret) {
  259. printk(KERN_ERR "dcc_tty_probe: tty_register_driver failed, %d\n", ret);
  260. goto err_tty_register_driver_failed;
  261. }
  262. tty_register_device(g_dcc_tty_driver, 0, NULL);
  263. register_console(&dcc_console);
  264. hrtimer_start(&g_dcc_timer, ktime_set(0, 0), HRTIMER_MODE_REL);
  265. return 0;
  266. err_tty_register_driver_failed:
  267. put_tty_driver(g_dcc_tty_driver);
  268. g_dcc_tty_driver = NULL;
  269. err_alloc_tty_driver_failed:
  270. return ret;
  271. }
  272. static void __exit dcc_tty_exit(void)
  273. {
  274. int ret;
  275. tty_unregister_device(g_dcc_tty_driver, 0);
  276. ret = tty_unregister_driver(g_dcc_tty_driver);
  277. if (ret < 0) {
  278. printk(KERN_ERR "dcc_tty_remove: tty_unregister_driver failed, %d\n", ret);
  279. } else {
  280. put_tty_driver(g_dcc_tty_driver);
  281. }
  282. g_dcc_tty_driver = NULL;
  283. }
  284. module_init(dcc_tty_init);
  285. module_exit(dcc_tty_exit);