msm_smd_tty.c 5.5 KB

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