n_tracesink.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * n_tracesink.c - Trace data router and sink path through tty space.
  3. *
  4. * Copyright (C) Intel 2011
  5. *
  6. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  18. *
  19. * The trace sink uses the Linux line discipline framework to receive
  20. * trace data coming from the PTI source line discipline driver
  21. * to a user-desired tty port, like USB.
  22. * This is to provide a way to extract modem trace data on
  23. * devices that do not have a PTI HW module, or just need modem
  24. * trace data to come out of a different HW output port.
  25. * This is part of a solution for the P1149.7, compact JTAG, standard.
  26. */
  27. #include <linux/init.h>
  28. #include <linux/kernel.h>
  29. #include <linux/module.h>
  30. #include <linux/types.h>
  31. #include <linux/ioctl.h>
  32. #include <linux/tty.h>
  33. #include <linux/tty_ldisc.h>
  34. #include <linux/errno.h>
  35. #include <linux/string.h>
  36. #include <linux/bug.h>
  37. #include "n_tracesink.h"
  38. /*
  39. * Other ldisc drivers use 65536 which basically means,
  40. * 'I can always accept 64k' and flow control is off.
  41. * This number is deemed appropriate for this driver.
  42. */
  43. #define RECEIVE_ROOM 65536
  44. #define DRIVERNAME "n_tracesink"
  45. /*
  46. * there is a quirk with this ldisc is he can write data
  47. * to a tty from anyone calling his kernel API, which
  48. * meets customer requirements in the drivers/misc/pti.c
  49. * project. So he needs to know when he can and cannot write when
  50. * the API is called. In theory, the API can be called
  51. * after an init() but before a successful open() which
  52. * would crash the system if tty is not checked.
  53. */
  54. static struct tty_struct *this_tty;
  55. static DEFINE_MUTEX(writelock);
  56. /**
  57. * n_tracesink_open() - Called when a tty is opened by a SW entity.
  58. * @tty: terminal device to the ldisc.
  59. *
  60. * Return:
  61. * 0 for success,
  62. * -EFAULT = couldn't get a tty kref n_tracesink will sit
  63. * on top of
  64. * -EEXIST = open() called successfully once and it cannot
  65. * be called again.
  66. *
  67. * Caveats: open() should only be successful the first time a
  68. * SW entity calls it.
  69. */
  70. static int n_tracesink_open(struct tty_struct *tty)
  71. {
  72. int retval = -EEXIST;
  73. mutex_lock(&writelock);
  74. if (this_tty == NULL) {
  75. this_tty = tty_kref_get(tty);
  76. if (this_tty == NULL) {
  77. retval = -EFAULT;
  78. } else {
  79. tty->disc_data = this_tty;
  80. tty_driver_flush_buffer(tty);
  81. retval = 0;
  82. }
  83. }
  84. mutex_unlock(&writelock);
  85. return retval;
  86. }
  87. /**
  88. * n_tracesink_close() - close connection
  89. * @tty: terminal device to the ldisc.
  90. *
  91. * Called when a software entity wants to close a connection.
  92. */
  93. static void n_tracesink_close(struct tty_struct *tty)
  94. {
  95. mutex_lock(&writelock);
  96. tty_driver_flush_buffer(tty);
  97. tty_kref_put(this_tty);
  98. this_tty = NULL;
  99. tty->disc_data = NULL;
  100. mutex_unlock(&writelock);
  101. }
  102. /**
  103. * n_tracesink_read() - read request from user space
  104. * @tty: terminal device passed into the ldisc.
  105. * @file: pointer to open file object.
  106. * @buf: pointer to the data buffer that gets eventually returned.
  107. * @nr: number of bytes of the data buffer that is returned.
  108. *
  109. * function that allows read() functionality in userspace. By default if this
  110. * is not implemented it returns -EIO. This module is functioning like a
  111. * router via n_tracesink_receivebuf(), and there is no real requirement
  112. * to implement this function. However, an error return value other than
  113. * -EIO should be used just to show that there was an intent not to have
  114. * this function implemented. Return value based on read() man pages.
  115. *
  116. * Return:
  117. * -EINVAL
  118. */
  119. static ssize_t n_tracesink_read(struct tty_struct *tty, struct file *file,
  120. unsigned char __user *buf, size_t nr) {
  121. return -EINVAL;
  122. }
  123. /**
  124. * n_tracesink_write() - Function that allows write() in userspace.
  125. * @tty: terminal device passed into the ldisc.
  126. * @file: pointer to open file object.
  127. * @buf: pointer to the data buffer that gets eventually returned.
  128. * @nr: number of bytes of the data buffer that is returned.
  129. *
  130. * By default if this is not implemented, it returns -EIO.
  131. * This should not be implemented, ever, because
  132. * 1. this driver is functioning like a router via
  133. * n_tracesink_receivebuf()
  134. * 2. No writes to HW will ever go through this line discpline driver.
  135. * However, an error return value other than -EIO should be used
  136. * just to show that there was an intent not to have this function
  137. * implemented. Return value based on write() man pages.
  138. *
  139. * Return:
  140. * -EINVAL
  141. */
  142. static ssize_t n_tracesink_write(struct tty_struct *tty, struct file *file,
  143. const unsigned char *buf, size_t nr) {
  144. return -EINVAL;
  145. }
  146. /**
  147. * n_tracesink_datadrain() - Kernel API function used to route
  148. * trace debugging data to user-defined
  149. * port like USB.
  150. *
  151. * @buf: Trace debuging data buffer to write to tty target
  152. * port. Null value will return with no write occurring.
  153. * @count: Size of buf. Value of 0 or a negative number will
  154. * return with no write occuring.
  155. *
  156. * Caveat: If this line discipline does not set the tty it sits
  157. * on top of via an open() call, this API function will not
  158. * call the tty's write() call because it will have no pointer
  159. * to call the write().
  160. */
  161. void n_tracesink_datadrain(u8 *buf, int count)
  162. {
  163. mutex_lock(&writelock);
  164. if ((buf != NULL) && (count > 0) && (this_tty != NULL))
  165. this_tty->ops->write(this_tty, buf, count);
  166. mutex_unlock(&writelock);
  167. }
  168. EXPORT_SYMBOL_GPL(n_tracesink_datadrain);
  169. /*
  170. * Flush buffer is not impelemented as the ldisc has no internal buffering
  171. * so the tty_driver_flush_buffer() is sufficient for this driver's needs.
  172. */
  173. /*
  174. * tty_ldisc function operations for this driver.
  175. */
  176. static struct tty_ldisc_ops tty_n_tracesink = {
  177. .owner = THIS_MODULE,
  178. .magic = TTY_LDISC_MAGIC,
  179. .name = DRIVERNAME,
  180. .open = n_tracesink_open,
  181. .close = n_tracesink_close,
  182. .read = n_tracesink_read,
  183. .write = n_tracesink_write
  184. };
  185. /**
  186. * n_tracesink_init- module initialisation
  187. *
  188. * Registers this module as a line discipline driver.
  189. *
  190. * Return:
  191. * 0 for success, any other value error.
  192. */
  193. static int __init n_tracesink_init(void)
  194. {
  195. /* Note N_TRACESINK is defined in linux/tty.h */
  196. int retval = tty_register_ldisc(N_TRACESINK, &tty_n_tracesink);
  197. if (retval < 0)
  198. pr_err("%s: Registration failed: %d\n", __func__, retval);
  199. return retval;
  200. }
  201. /**
  202. * n_tracesink_exit - module unload
  203. *
  204. * Removes this module as a line discipline driver.
  205. */
  206. static void __exit n_tracesink_exit(void)
  207. {
  208. int retval = tty_unregister_ldisc(N_TRACESINK);
  209. if (retval < 0)
  210. pr_err("%s: Unregistration failed: %d\n", __func__, retval);
  211. }
  212. module_init(n_tracesink_init);
  213. module_exit(n_tracesink_exit);
  214. MODULE_LICENSE("GPL");
  215. MODULE_AUTHOR("Jay Freyensee");
  216. MODULE_ALIAS_LDISC(N_TRACESINK);
  217. MODULE_DESCRIPTION("Trace sink ldisc driver");