n_tracerouter.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * n_tracerouter.c - Trace data router 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. * This trace router uses the Linux line discipline framework to route
  20. * trace data coming from a HW Modem to a PTI (Parallel Trace Module) port.
  21. * The solution is not specific to a HW modem and this line disciple can
  22. * be used to route any stream of data in kernel space.
  23. * This is part of a solution for the P1149.7, compact JTAG, standard.
  24. */
  25. #include <linux/init.h>
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/types.h>
  29. #include <linux/ioctl.h>
  30. #include <linux/tty.h>
  31. #include <linux/tty_ldisc.h>
  32. #include <linux/errno.h>
  33. #include <linux/string.h>
  34. #include <linux/mutex.h>
  35. #include <linux/slab.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_tracerouter"
  45. /*
  46. * struct to hold private configuration data for this ldisc.
  47. * opencalled is used to hold if this ldisc has been opened.
  48. * kref_tty holds the tty reference the ldisc sits on top of.
  49. */
  50. struct tracerouter_data {
  51. u8 opencalled;
  52. struct tty_struct *kref_tty;
  53. };
  54. static struct tracerouter_data *tr_data;
  55. /* lock for when tty reference is being used */
  56. static DEFINE_MUTEX(routelock);
  57. /**
  58. * n_tracerouter_open() - Called when a tty is opened by a SW entity.
  59. * @tty: terminal device to the ldisc.
  60. *
  61. * Return:
  62. * 0 for success.
  63. *
  64. * Caveats: This should only be opened one time per SW entity.
  65. */
  66. static int n_tracerouter_open(struct tty_struct *tty)
  67. {
  68. int retval = -EEXIST;
  69. mutex_lock(&routelock);
  70. if (tr_data->opencalled == 0) {
  71. tr_data->kref_tty = tty_kref_get(tty);
  72. if (tr_data->kref_tty == NULL) {
  73. retval = -EFAULT;
  74. } else {
  75. tr_data->opencalled = 1;
  76. tty->disc_data = tr_data;
  77. tty->receive_room = RECEIVE_ROOM;
  78. tty_driver_flush_buffer(tty);
  79. retval = 0;
  80. }
  81. }
  82. mutex_unlock(&routelock);
  83. return retval;
  84. }
  85. /**
  86. * n_tracerouter_close() - close connection
  87. * @tty: terminal device to the ldisc.
  88. *
  89. * Called when a software entity wants to close a connection.
  90. */
  91. static void n_tracerouter_close(struct tty_struct *tty)
  92. {
  93. struct tracerouter_data *tptr = tty->disc_data;
  94. mutex_lock(&routelock);
  95. WARN_ON(tptr->kref_tty != tr_data->kref_tty);
  96. tty_driver_flush_buffer(tty);
  97. tty_kref_put(tr_data->kref_tty);
  98. tr_data->kref_tty = NULL;
  99. tr_data->opencalled = 0;
  100. tty->disc_data = NULL;
  101. mutex_unlock(&routelock);
  102. }
  103. /**
  104. * n_tracerouter_read() - read request from user space
  105. * @tty: terminal device passed into the ldisc.
  106. * @file: pointer to open file object.
  107. * @buf: pointer to the data buffer that gets eventually returned.
  108. * @nr: number of bytes of the data buffer that is returned.
  109. *
  110. * function that allows read() functionality in userspace. By default if this
  111. * is not implemented it returns -EIO. This module is functioning like a
  112. * router via n_tracerouter_receivebuf(), and there is no real requirement
  113. * to implement this function. However, an error return value other than
  114. * -EIO should be used just to show that there was an intent not to have
  115. * this function implemented. Return value based on read() man pages.
  116. *
  117. * Return:
  118. * -EINVAL
  119. */
  120. static ssize_t n_tracerouter_read(struct tty_struct *tty, struct file *file,
  121. unsigned char __user *buf, size_t nr) {
  122. return -EINVAL;
  123. }
  124. /**
  125. * n_tracerouter_write() - Function that allows write() in userspace.
  126. * @tty: terminal device passed into the ldisc.
  127. * @file: pointer to open file object.
  128. * @buf: pointer to the data buffer that gets eventually returned.
  129. * @nr: number of bytes of the data buffer that is returned.
  130. *
  131. * By default if this is not implemented, it returns -EIO.
  132. * This should not be implemented, ever, because
  133. * 1. this driver is functioning like a router via
  134. * n_tracerouter_receivebuf()
  135. * 2. No writes to HW will ever go through this line discpline driver.
  136. * However, an error return value other than -EIO should be used
  137. * just to show that there was an intent not to have this function
  138. * implemented. Return value based on write() man pages.
  139. *
  140. * Return:
  141. * -EINVAL
  142. */
  143. static ssize_t n_tracerouter_write(struct tty_struct *tty, struct file *file,
  144. const unsigned char *buf, size_t nr) {
  145. return -EINVAL;
  146. }
  147. /**
  148. * n_tracerouter_receivebuf() - Routing function for driver.
  149. * @tty: terminal device passed into the ldisc. It's assumed
  150. * tty will never be NULL.
  151. * @cp: buffer, block of characters to be eventually read by
  152. * someone, somewhere (user read() call or some kernel function).
  153. * @fp: flag buffer.
  154. * @count: number of characters (aka, bytes) in cp.
  155. *
  156. * This function takes the input buffer, cp, and passes it to
  157. * an external API function for processing.
  158. */
  159. static void n_tracerouter_receivebuf(struct tty_struct *tty,
  160. const unsigned char *cp,
  161. char *fp, int count)
  162. {
  163. mutex_lock(&routelock);
  164. n_tracesink_datadrain((u8 *) cp, count);
  165. mutex_unlock(&routelock);
  166. }
  167. /*
  168. * Flush buffer is not impelemented as the ldisc has no internal buffering
  169. * so the tty_driver_flush_buffer() is sufficient for this driver's needs.
  170. */
  171. static struct tty_ldisc_ops tty_ptirouter_ldisc = {
  172. .owner = THIS_MODULE,
  173. .magic = TTY_LDISC_MAGIC,
  174. .name = DRIVERNAME,
  175. .open = n_tracerouter_open,
  176. .close = n_tracerouter_close,
  177. .read = n_tracerouter_read,
  178. .write = n_tracerouter_write,
  179. .receive_buf = n_tracerouter_receivebuf
  180. };
  181. /**
  182. * n_tracerouter_init - module initialisation
  183. *
  184. * Registers this module as a line discipline driver.
  185. *
  186. * Return:
  187. * 0 for success, any other value error.
  188. */
  189. static int __init n_tracerouter_init(void)
  190. {
  191. int retval;
  192. tr_data = kzalloc(sizeof(struct tracerouter_data), GFP_KERNEL);
  193. if (tr_data == NULL)
  194. return -ENOMEM;
  195. /* Note N_TRACEROUTER is defined in linux/tty.h */
  196. retval = tty_register_ldisc(N_TRACEROUTER, &tty_ptirouter_ldisc);
  197. if (retval < 0) {
  198. pr_err("%s: Registration failed: %d\n", __func__, retval);
  199. kfree(tr_data);
  200. }
  201. return retval;
  202. }
  203. /**
  204. * n_tracerouter_exit - module unload
  205. *
  206. * Removes this module as a line discipline driver.
  207. */
  208. static void __exit n_tracerouter_exit(void)
  209. {
  210. int retval = tty_unregister_ldisc(N_TRACEROUTER);
  211. if (retval < 0)
  212. pr_err("%s: Unregistration failed: %d\n", __func__, retval);
  213. else
  214. kfree(tr_data);
  215. }
  216. module_init(n_tracerouter_init);
  217. module_exit(n_tracerouter_exit);
  218. MODULE_LICENSE("GPL");
  219. MODULE_AUTHOR("Jay Freyensee");
  220. MODULE_ALIAS_LDISC(N_TRACEROUTER);
  221. MODULE_DESCRIPTION("Trace router ldisc driver");