msm_smd_tty.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright (C) 2007 Google, Inc.
  3. * Copyright (c) 2011, The Linux Foundation. All rights reserved.
  4. * Author: Brian Swetland <swetland@google.com>
  5. *
  6. * This software is licensed under the terms of the GNU General Public
  7. * License version 2, as published by the Free Software Foundation, and
  8. * may be copied, distributed, and modified under those terms.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. */
  16. #include <linux/module.h>
  17. #include <linux/fs.h>
  18. #include <linux/cdev.h>
  19. #include <linux/device.h>
  20. #include <linux/wait.h>
  21. #include <linux/tty.h>
  22. #include <linux/tty_driver.h>
  23. #include <linux/tty_flip.h>
  24. #include <mach/msm_smd.h>
  25. #define MAX_SMD_TTYS 32
  26. struct smd_tty_info {
  27. struct tty_port port;
  28. smd_channel_t *ch;
  29. };
  30. struct smd_tty_channel_desc {
  31. int id;
  32. const char *name;
  33. };
  34. static struct smd_tty_info smd_tty[MAX_SMD_TTYS];
  35. static const struct smd_tty_channel_desc smd_default_tty_channels[] = {
  36. { .id = 0, .name = "SMD_DS" },
  37. { .id = 27, .name = "SMD_GPSNMEA" },
  38. };
  39. static const struct smd_tty_channel_desc *smd_tty_channels =
  40. smd_default_tty_channels;
  41. static int smd_tty_channels_len = ARRAY_SIZE(smd_default_tty_channels);
  42. static void smd_tty_notify(void *priv, unsigned event)
  43. {
  44. unsigned char *ptr;
  45. int avail;
  46. struct smd_tty_info *info = priv;
  47. struct tty_struct *tty;
  48. if (event != SMD_EVENT_DATA)
  49. return;
  50. tty = tty_port_tty_get(&info->port);
  51. if (!tty)
  52. return;
  53. for (;;) {
  54. if (test_bit(TTY_THROTTLED, &tty->flags))
  55. break;
  56. avail = smd_read_avail(info->ch);
  57. if (avail == 0)
  58. break;
  59. avail = tty_prepare_flip_string(tty, &ptr, avail);
  60. if (smd_read(info->ch, ptr, avail) != avail) {
  61. /* shouldn't be possible since we're in interrupt
  62. ** context here and nobody else could 'steal' our
  63. ** characters.
  64. */
  65. pr_err("OOPS - smd_tty_buffer mismatch?!");
  66. }
  67. tty_flip_buffer_push(tty);
  68. }
  69. /* XXX only when writable and necessary */
  70. tty_wakeup(tty);
  71. tty_kref_put(tty);
  72. }
  73. static int smd_tty_port_activate(struct tty_port *tport, struct tty_struct *tty)
  74. {
  75. int i, res = 0;
  76. int n = tty->index;
  77. const char *name = NULL;
  78. struct smd_tty_info *info = smd_tty + n;
  79. for (i = 0; i < smd_tty_channels_len; i++) {
  80. if (smd_tty_channels[i].id == n) {
  81. name = smd_tty_channels[i].name;
  82. break;
  83. }
  84. }
  85. if (!name)
  86. return -ENODEV;
  87. if (info->ch)
  88. smd_kick(info->ch);
  89. else
  90. res = smd_open(name, &info->ch, info, smd_tty_notify);
  91. if (!res)
  92. tty->driver_data = info;
  93. return res;
  94. }
  95. static void smd_tty_port_shutdown(struct tty_port *tport)
  96. {
  97. struct smd_tty_info *info;
  98. struct tty_struct *tty = tty_port_tty_get(tport);
  99. info = tty->driver_data;
  100. if (info->ch) {
  101. smd_close(info->ch);
  102. info->ch = 0;
  103. }
  104. tty->driver_data = 0;
  105. tty_kref_put(tty);
  106. }
  107. static int smd_tty_open(struct tty_struct *tty, struct file *f)
  108. {
  109. struct smd_tty_info *info = smd_tty + tty->index;
  110. return tty_port_open(&info->port, tty, f);
  111. }
  112. static void smd_tty_close(struct tty_struct *tty, struct file *f)
  113. {
  114. struct smd_tty_info *info = tty->driver_data;
  115. tty_port_close(&info->port, tty, f);
  116. }
  117. static int smd_tty_write(struct tty_struct *tty,
  118. const unsigned char *buf, int len)
  119. {
  120. struct smd_tty_info *info = tty->driver_data;
  121. int avail;
  122. /* if we're writing to a packet channel we will
  123. ** never be able to write more data than there
  124. ** is currently space for
  125. */
  126. avail = smd_write_avail(info->ch);
  127. if (len > avail)
  128. len = avail;
  129. return smd_write(info->ch, buf, len);
  130. }
  131. static int smd_tty_write_room(struct tty_struct *tty)
  132. {
  133. struct smd_tty_info *info = tty->driver_data;
  134. return smd_write_avail(info->ch);
  135. }
  136. static int smd_tty_chars_in_buffer(struct tty_struct *tty)
  137. {
  138. struct smd_tty_info *info = tty->driver_data;
  139. return smd_read_avail(info->ch);
  140. }
  141. static void smd_tty_unthrottle(struct tty_struct *tty)
  142. {
  143. struct smd_tty_info *info = tty->driver_data;
  144. smd_kick(info->ch);
  145. }
  146. static const struct tty_port_operations smd_tty_port_ops = {
  147. .shutdown = smd_tty_port_shutdown,
  148. .activate = smd_tty_port_activate,
  149. };
  150. static const struct tty_operations smd_tty_ops = {
  151. .open = smd_tty_open,
  152. .close = smd_tty_close,
  153. .write = smd_tty_write,
  154. .write_room = smd_tty_write_room,
  155. .chars_in_buffer = smd_tty_chars_in_buffer,
  156. .unthrottle = smd_tty_unthrottle,
  157. };
  158. static struct tty_driver *smd_tty_driver;
  159. static int __init smd_tty_init(void)
  160. {
  161. int ret, i;
  162. smd_tty_driver = alloc_tty_driver(MAX_SMD_TTYS);
  163. if (smd_tty_driver == 0)
  164. return -ENOMEM;
  165. smd_tty_driver->driver_name = "smd_tty_driver";
  166. smd_tty_driver->name = "smd";
  167. smd_tty_driver->major = 0;
  168. smd_tty_driver->minor_start = 0;
  169. smd_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
  170. smd_tty_driver->subtype = SERIAL_TYPE_NORMAL;
  171. smd_tty_driver->init_termios = tty_std_termios;
  172. smd_tty_driver->init_termios.c_iflag = 0;
  173. smd_tty_driver->init_termios.c_oflag = 0;
  174. smd_tty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  175. smd_tty_driver->init_termios.c_lflag = 0;
  176. smd_tty_driver->flags = TTY_DRIVER_RESET_TERMIOS |
  177. TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  178. tty_set_operations(smd_tty_driver, &smd_tty_ops);
  179. ret = tty_register_driver(smd_tty_driver);
  180. if (ret)
  181. return ret;
  182. for (i = 0; i < smd_tty_channels_len; i++) {
  183. tty_port_init(&smd_tty[smd_tty_channels[i].id].port);
  184. smd_tty[smd_tty_channels[i].id].port.ops = &smd_tty_port_ops;
  185. tty_register_device(smd_tty_driver, smd_tty_channels[i].id, 0);
  186. }
  187. return 0;
  188. }
  189. module_init(smd_tty_init);