dcc_tty.c 8.0 KB

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