18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  n_tracesink.c - Trace data router and sink path through tty space.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Copyright (C) Intel 2011
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * The trace sink uses the Linux line discipline framework to receive
108c2ecf20Sopenharmony_ci * trace data coming from the PTI source line discipline driver
118c2ecf20Sopenharmony_ci * to a user-desired tty port, like USB.
128c2ecf20Sopenharmony_ci * This is to provide a way to extract modem trace data on
138c2ecf20Sopenharmony_ci * devices that do not have a PTI HW module, or just need modem
148c2ecf20Sopenharmony_ci * trace data to come out of a different HW output port.
158c2ecf20Sopenharmony_ci * This is part of a solution for the P1149.7, compact JTAG, standard.
168c2ecf20Sopenharmony_ci */
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#include <linux/init.h>
198c2ecf20Sopenharmony_ci#include <linux/kernel.h>
208c2ecf20Sopenharmony_ci#include <linux/module.h>
218c2ecf20Sopenharmony_ci#include <linux/types.h>
228c2ecf20Sopenharmony_ci#include <linux/ioctl.h>
238c2ecf20Sopenharmony_ci#include <linux/tty.h>
248c2ecf20Sopenharmony_ci#include <linux/tty_ldisc.h>
258c2ecf20Sopenharmony_ci#include <linux/errno.h>
268c2ecf20Sopenharmony_ci#include <linux/string.h>
278c2ecf20Sopenharmony_ci#include <linux/bug.h>
288c2ecf20Sopenharmony_ci#include "n_tracesink.h"
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci/*
318c2ecf20Sopenharmony_ci * Other ldisc drivers use 65536 which basically means,
328c2ecf20Sopenharmony_ci * 'I can always accept 64k' and flow control is off.
338c2ecf20Sopenharmony_ci * This number is deemed appropriate for this driver.
348c2ecf20Sopenharmony_ci */
358c2ecf20Sopenharmony_ci#define RECEIVE_ROOM	65536
368c2ecf20Sopenharmony_ci#define DRIVERNAME	"n_tracesink"
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci/*
398c2ecf20Sopenharmony_ci * there is a quirk with this ldisc is he can write data
408c2ecf20Sopenharmony_ci * to a tty from anyone calling his kernel API, which
418c2ecf20Sopenharmony_ci * meets customer requirements in the drivers/misc/pti.c
428c2ecf20Sopenharmony_ci * project.  So he needs to know when he can and cannot write when
438c2ecf20Sopenharmony_ci * the API is called. In theory, the API can be called
448c2ecf20Sopenharmony_ci * after an init() but before a successful open() which
458c2ecf20Sopenharmony_ci * would crash the system if tty is not checked.
468c2ecf20Sopenharmony_ci */
478c2ecf20Sopenharmony_cistatic struct tty_struct *this_tty;
488c2ecf20Sopenharmony_cistatic DEFINE_MUTEX(writelock);
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci/**
518c2ecf20Sopenharmony_ci * n_tracesink_open() - Called when a tty is opened by a SW entity.
528c2ecf20Sopenharmony_ci * @tty: terminal device to the ldisc.
538c2ecf20Sopenharmony_ci *
548c2ecf20Sopenharmony_ci * Return:
558c2ecf20Sopenharmony_ci *      0 for success,
568c2ecf20Sopenharmony_ci *      -EFAULT = couldn't get a tty kref n_tracesink will sit
578c2ecf20Sopenharmony_ci *       on top of
588c2ecf20Sopenharmony_ci *      -EEXIST = open() called successfully once and it cannot
598c2ecf20Sopenharmony_ci *      be called again.
608c2ecf20Sopenharmony_ci *
618c2ecf20Sopenharmony_ci * Caveats: open() should only be successful the first time a
628c2ecf20Sopenharmony_ci * SW entity calls it.
638c2ecf20Sopenharmony_ci */
648c2ecf20Sopenharmony_cistatic int n_tracesink_open(struct tty_struct *tty)
658c2ecf20Sopenharmony_ci{
668c2ecf20Sopenharmony_ci	int retval = -EEXIST;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	mutex_lock(&writelock);
698c2ecf20Sopenharmony_ci	if (this_tty == NULL) {
708c2ecf20Sopenharmony_ci		this_tty = tty_kref_get(tty);
718c2ecf20Sopenharmony_ci		if (this_tty == NULL) {
728c2ecf20Sopenharmony_ci			retval = -EFAULT;
738c2ecf20Sopenharmony_ci		} else {
748c2ecf20Sopenharmony_ci			tty->disc_data = this_tty;
758c2ecf20Sopenharmony_ci			tty_driver_flush_buffer(tty);
768c2ecf20Sopenharmony_ci			retval = 0;
778c2ecf20Sopenharmony_ci		}
788c2ecf20Sopenharmony_ci	}
798c2ecf20Sopenharmony_ci	mutex_unlock(&writelock);
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	return retval;
828c2ecf20Sopenharmony_ci}
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci/**
858c2ecf20Sopenharmony_ci * n_tracesink_close() - close connection
868c2ecf20Sopenharmony_ci * @tty: terminal device to the ldisc.
878c2ecf20Sopenharmony_ci *
888c2ecf20Sopenharmony_ci * Called when a software entity wants to close a connection.
898c2ecf20Sopenharmony_ci */
908c2ecf20Sopenharmony_cistatic void n_tracesink_close(struct tty_struct *tty)
918c2ecf20Sopenharmony_ci{
928c2ecf20Sopenharmony_ci	mutex_lock(&writelock);
938c2ecf20Sopenharmony_ci	tty_driver_flush_buffer(tty);
948c2ecf20Sopenharmony_ci	tty_kref_put(this_tty);
958c2ecf20Sopenharmony_ci	this_tty = NULL;
968c2ecf20Sopenharmony_ci	tty->disc_data = NULL;
978c2ecf20Sopenharmony_ci	mutex_unlock(&writelock);
988c2ecf20Sopenharmony_ci}
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci/**
1018c2ecf20Sopenharmony_ci * n_tracesink_read() - read request from user space
1028c2ecf20Sopenharmony_ci * @tty:  terminal device passed into the ldisc.
1038c2ecf20Sopenharmony_ci * @file: pointer to open file object.
1048c2ecf20Sopenharmony_ci * @buf:  pointer to the data buffer that gets eventually returned.
1058c2ecf20Sopenharmony_ci * @nr:   number of bytes of the data buffer that is returned.
1068c2ecf20Sopenharmony_ci *
1078c2ecf20Sopenharmony_ci * function that allows read() functionality in userspace. By default if this
1088c2ecf20Sopenharmony_ci * is not implemented it returns -EIO. This module is functioning like a
1098c2ecf20Sopenharmony_ci * router via n_tracesink_receivebuf(), and there is no real requirement
1108c2ecf20Sopenharmony_ci * to implement this function. However, an error return value other than
1118c2ecf20Sopenharmony_ci * -EIO should be used just to show that there was an intent not to have
1128c2ecf20Sopenharmony_ci * this function implemented.  Return value based on read() man pages.
1138c2ecf20Sopenharmony_ci *
1148c2ecf20Sopenharmony_ci * Return:
1158c2ecf20Sopenharmony_ci *	 -EINVAL
1168c2ecf20Sopenharmony_ci */
1178c2ecf20Sopenharmony_cistatic ssize_t n_tracesink_read(struct tty_struct *tty, struct file *file,
1188c2ecf20Sopenharmony_ci				unsigned char *buf, size_t nr,
1198c2ecf20Sopenharmony_ci				void **cookie, unsigned long offset)
1208c2ecf20Sopenharmony_ci{
1218c2ecf20Sopenharmony_ci	return -EINVAL;
1228c2ecf20Sopenharmony_ci}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci/**
1258c2ecf20Sopenharmony_ci * n_tracesink_write() - Function that allows write() in userspace.
1268c2ecf20Sopenharmony_ci * @tty:  terminal device passed into the ldisc.
1278c2ecf20Sopenharmony_ci * @file: pointer to open file object.
1288c2ecf20Sopenharmony_ci * @buf:  pointer to the data buffer that gets eventually returned.
1298c2ecf20Sopenharmony_ci * @nr:   number of bytes of the data buffer that is returned.
1308c2ecf20Sopenharmony_ci *
1318c2ecf20Sopenharmony_ci * By default if this is not implemented, it returns -EIO.
1328c2ecf20Sopenharmony_ci * This should not be implemented, ever, because
1338c2ecf20Sopenharmony_ci * 1. this driver is functioning like a router via
1348c2ecf20Sopenharmony_ci *    n_tracesink_receivebuf()
1358c2ecf20Sopenharmony_ci * 2. No writes to HW will ever go through this line discpline driver.
1368c2ecf20Sopenharmony_ci * However, an error return value other than -EIO should be used
1378c2ecf20Sopenharmony_ci * just to show that there was an intent not to have this function
1388c2ecf20Sopenharmony_ci * implemented.  Return value based on write() man pages.
1398c2ecf20Sopenharmony_ci *
1408c2ecf20Sopenharmony_ci * Return:
1418c2ecf20Sopenharmony_ci *	-EINVAL
1428c2ecf20Sopenharmony_ci */
1438c2ecf20Sopenharmony_cistatic ssize_t n_tracesink_write(struct tty_struct *tty, struct file *file,
1448c2ecf20Sopenharmony_ci				 const unsigned char *buf, size_t nr) {
1458c2ecf20Sopenharmony_ci	return -EINVAL;
1468c2ecf20Sopenharmony_ci}
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci/**
1498c2ecf20Sopenharmony_ci * n_tracesink_datadrain() - Kernel API function used to route
1508c2ecf20Sopenharmony_ci *			     trace debugging data to user-defined
1518c2ecf20Sopenharmony_ci *			     port like USB.
1528c2ecf20Sopenharmony_ci *
1538c2ecf20Sopenharmony_ci * @buf:   Trace debuging data buffer to write to tty target
1548c2ecf20Sopenharmony_ci *         port. Null value will return with no write occurring.
1558c2ecf20Sopenharmony_ci * @count: Size of buf. Value of 0 or a negative number will
1568c2ecf20Sopenharmony_ci *         return with no write occuring.
1578c2ecf20Sopenharmony_ci *
1588c2ecf20Sopenharmony_ci * Caveat: If this line discipline does not set the tty it sits
1598c2ecf20Sopenharmony_ci * on top of via an open() call, this API function will not
1608c2ecf20Sopenharmony_ci * call the tty's write() call because it will have no pointer
1618c2ecf20Sopenharmony_ci * to call the write().
1628c2ecf20Sopenharmony_ci */
1638c2ecf20Sopenharmony_civoid n_tracesink_datadrain(u8 *buf, int count)
1648c2ecf20Sopenharmony_ci{
1658c2ecf20Sopenharmony_ci	mutex_lock(&writelock);
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	if ((buf != NULL) && (count > 0) && (this_tty != NULL))
1688c2ecf20Sopenharmony_ci		this_tty->ops->write(this_tty, buf, count);
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	mutex_unlock(&writelock);
1718c2ecf20Sopenharmony_ci}
1728c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(n_tracesink_datadrain);
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci/*
1758c2ecf20Sopenharmony_ci * Flush buffer is not impelemented as the ldisc has no internal buffering
1768c2ecf20Sopenharmony_ci * so the tty_driver_flush_buffer() is sufficient for this driver's needs.
1778c2ecf20Sopenharmony_ci */
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci/*
1808c2ecf20Sopenharmony_ci * tty_ldisc function operations for this driver.
1818c2ecf20Sopenharmony_ci */
1828c2ecf20Sopenharmony_cistatic struct tty_ldisc_ops tty_n_tracesink = {
1838c2ecf20Sopenharmony_ci	.owner		= THIS_MODULE,
1848c2ecf20Sopenharmony_ci	.magic		= TTY_LDISC_MAGIC,
1858c2ecf20Sopenharmony_ci	.name		= DRIVERNAME,
1868c2ecf20Sopenharmony_ci	.open		= n_tracesink_open,
1878c2ecf20Sopenharmony_ci	.close		= n_tracesink_close,
1888c2ecf20Sopenharmony_ci	.read		= n_tracesink_read,
1898c2ecf20Sopenharmony_ci	.write		= n_tracesink_write
1908c2ecf20Sopenharmony_ci};
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci/**
1938c2ecf20Sopenharmony_ci * n_tracesink_init-	module initialisation
1948c2ecf20Sopenharmony_ci *
1958c2ecf20Sopenharmony_ci * Registers this module as a line discipline driver.
1968c2ecf20Sopenharmony_ci *
1978c2ecf20Sopenharmony_ci * Return:
1988c2ecf20Sopenharmony_ci *	0 for success, any other value error.
1998c2ecf20Sopenharmony_ci */
2008c2ecf20Sopenharmony_cistatic int __init n_tracesink_init(void)
2018c2ecf20Sopenharmony_ci{
2028c2ecf20Sopenharmony_ci	/* Note N_TRACESINK is defined in linux/tty.h */
2038c2ecf20Sopenharmony_ci	int retval = tty_register_ldisc(N_TRACESINK, &tty_n_tracesink);
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	if (retval < 0)
2068c2ecf20Sopenharmony_ci		pr_err("%s: Registration failed: %d\n", __func__, retval);
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	return retval;
2098c2ecf20Sopenharmony_ci}
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci/**
2128c2ecf20Sopenharmony_ci * n_tracesink_exit -	module unload
2138c2ecf20Sopenharmony_ci *
2148c2ecf20Sopenharmony_ci * Removes this module as a line discipline driver.
2158c2ecf20Sopenharmony_ci */
2168c2ecf20Sopenharmony_cistatic void __exit n_tracesink_exit(void)
2178c2ecf20Sopenharmony_ci{
2188c2ecf20Sopenharmony_ci	int retval = tty_unregister_ldisc(N_TRACESINK);
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	if (retval < 0)
2218c2ecf20Sopenharmony_ci		pr_err("%s: Unregistration failed: %d\n", __func__,  retval);
2228c2ecf20Sopenharmony_ci}
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_cimodule_init(n_tracesink_init);
2258c2ecf20Sopenharmony_cimodule_exit(n_tracesink_exit);
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
2288c2ecf20Sopenharmony_ciMODULE_AUTHOR("Jay Freyensee");
2298c2ecf20Sopenharmony_ciMODULE_ALIAS_LDISC(N_TRACESINK);
2308c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Trace sink ldisc driver");
231