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