aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/tty/n_tracerouter.c
blob: 8b2e43258f51913c74062ffe46a88ff54f674a4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
 *  n_tracerouter.c - PTI data router for JTAG data extration
 *
 *  Copyright (C) Intel 2010
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2
 *  as published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 *  02110-1301, USA
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 * This trace router uses the Linux line discipline framework to route
 * trace data coming from a HW Modem to a PTI (Parallel Trace Module) port.
 * The solution is not specific to a HW modem and this line disciple can
 * be used to route any stream of data in kernel space.
 * This is part of a solution for the P1149.7, compact JTAG, standard.
 *
 */

#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/ioctl.h>
#include <linux/tty.h>
#include <linux/tty_ldisc.h>
#include <linux/errno.h>
#include <linux/string.h>
#include <linux/mutex.h>
#include <linux/slab.h>
#include <asm-generic/bug.h>
#include <linux/pti.h>

/* Other ldisc drivers use 65536 which basically means,
 * 'I can always accept 64k' and flow control is off.
 * This number is deemed appropriate for this driver.
 */

#define RECEIVE_ROOM	65536
#define DRIVERNAME	"n_tracerouter"

/* FIXME: ioctl stuff to specifically set PTI HW; it's being assumed
 * the default and more common extraction case is to route
 * data to an output port like USB.  This feature is needed
 * now to meet requests; however, once a second tty is added
 * to the PTI driver and the right write aperture is assigned to
 * that tty then this can be removed.
 */
#define PTIHW_SETCONF	_IOW('J', 0, int)

/* struct to hold private configuration data for this ldisc.
 * routedata() used to hold the function to call to route the data.
 * routedata_ptihw is used as switch a user app tells the ldisc where
 * to route.
 */
struct tracerouter_data {
	void (*routedata)(struct masterchannel *mc, u8 *cp, int count);
	u8 routedata_ptihw;
	u8 opencalled;
	struct tty_struct *kref_tty;
};
static struct tracerouter_data *tr_data;

/* lock for ioctl() setting of tracerouter_data values */
static DEFINE_MUTEX(routelock);

/**
 * tracerouter_reset
 *
 * Provides an initial state to route data.
 *
 * @param ptr expects an existing ptr to be passed in
 * to set the parameters involved in actually routing
 * the data (routedata, routedata_ptihw, opencalled).
 */
static void tracerouter_resetroute(struct tracerouter_data *ptr)
{
	ptr->routedata	       = mipi_pti_sinkdata;
	ptr->routedata_ptihw   = 0;
	ptr->opencalled        = 0;
}

/**
 * tracerouter_alloc
 *
 * Allocates the structure needed for this ldisc.
 */
static struct tracerouter_data *tracerouter_alloc(void)
{
	struct tracerouter_data *tptr = kzalloc(
					sizeof(struct tracerouter_data),
					GFP_KERNEL);
	if (tptr == NULL)
		return NULL;

	/* default to shifting data out to the USB and specifically configure
	 * it to shift out to PTI.  This is because it will probably be possible
	 * in the field to shift out data to USB, but the PTI HW may not be
	 * available 'in the field'.
	 */
	tracerouter_resetroute(tptr);
	return tptr;
}

/**
 * n_tracerouter_open() - Called when a tty is opened by a SW entity.
 * @tty: terminal device to the ldisc.
 *
 * Return:
 *      0 for success.
 *
 * Caveats: This should only be opened one time per SW entity.
 */
static int n_tracerouter_open(struct tty_struct *tty)
{
	int retval = -EEXIST;

	mutex_lock(&routelock);
	if (tr_data->opencalled == 0) {

		tr_data->kref_tty = tty_kref_get(tty);
		if (tr_data->kref_tty == NULL)
			retval = -EFAULT;
		else {
			tr_data->opencalled = 1;
			tty->disc_data      = tr_data;
			tty->receive_room   = RECEIVE_ROOM;
			tty_driver_flush_buffer(tty);
			retval = 0;
		}
	}
	mutex_unlock(&routelock);

	pr_debug("%s(%s): called. Return value %d\n",
		 __FILE__, __func__, retval);

	return retval;
}

/**
 * n_tracerouter_close() - close connection
 * @tty: terminal device to the ldisc.
 *
 * Called when a software entity wants to close a connection.
 */
static void n_tracerouter_close(struct tty_struct *tty)
{
	struct tracerouter_data *tptr = tty->disc_data;

	WARN_ON(tptr->kref_tty != tr_data->kref_tty);
	tty_driver_flush_buffer(tty);
	mutex_lock(&routelock);
	tty_kref_put(tr_data->kref_tty);
	tr_data->kref_tty = NULL;
	tracerouter_resetroute(tr_data);
	tty->disc_data = NULL;
	mutex_unlock(&routelock);

	pr_debug("%s(%s): called.\n",
		 __FILE__, __func__);
}

/**
 * n_tracerouter_read() - read request from user space
 * @tty:  terminal device passed into the ldisc.
 * @file: pointer to open file object.
 * @buf:  pointer to the data buffer that gets eventually returned.
 * @nr:   number of bytes of the data buffer that is returned.
 *
 * function that allows read() functionality in userspace. By default if this
 * is not implemented it returns -EIO. This module is functioning like a
 * router via n_tracerouter_receivebuf(), and there is no real requirement
 * to implement this function. However, an error return value other than
 * -EIO should be used just to show that there was an intent not to have
 * this function implemented.  Return value based on read() man pages.
 *
 * Return:
 *	 -EINVAL
 */
static ssize_t n_tracerouter_read(struct tty_struct *tty, struct file *file,
				  unsigned char *buf, size_t nr) {
	return -EINVAL;
}

/**
 * n_tracerouter_write() - Function that allows write() in userspace.
 * @tty:  terminal device passed into the ldisc.
 * @file: pointer to open file object.
 * @buf:  pointer to the data buffer that gets eventually returned.
 * @nr:   number of bytes of the data buffer that is returned.
 *
 * By default if this is not implemented, it returns -EIO.
 * This should not be implemented, ever, because
 * 1. this driver is functioning like a router via
 *    n_tracerouter_receivebuf()
 * 2. No writes to HW will ever go through this line discpline driver.
 * However, an error return value other than -EIO should be used
 * just to show that there was an intent not to have this function
 * implemented.  Return value based on write() man pages.
 *
 * Return:
 *	-EINVAL
 */
static ssize_t n_tracerouter_write(struct tty_struct *tty, struct file *file,
				   const unsigned char *buf, size_t nr) {
	return -EINVAL;
}

/**
 * FIXME: This whole routine is part of the FIXME ioctl() at the
 * begining and can be removed when the PTI driver gets a 2nd
 * tty port and is validated both ports can handle continuous
 * simultaneous constant writes. n_tracerouter will then
 * always route to n_tracesink and whatever tty n_tracesink sits
 * on top of is where trace data winds up going.  The
 * combination n_tracerouter and n_tracesink will then make up a
 * generic solution of being able to route any data (not just
 * for capturing modem debug data) to any tty port a user-app
 * configures.
 *
 * n_tracerouter_ioctl()
 * - Defined for custom behavior for the PTI trace router.
 *
 * @tty: the tty device the ldisc sits on top of.
 * @file: File descriptor
 * @cmd: The defined ioctl.  The custom ioctl defined is
 *     PTIHW_SETCONF, which sets if trace data should go
 *     to the PTI HW that is on the system.
 * @arg: A pointer to the thing in userspace.  If it's 0, the
 *     router will route tot he n_ptisink driver.  Otherwise, it
 *     will send the data to the PTI modem.
 *
 * Return:
 *      0 for success, anything else error
 *
 * Caveats:
 *      This function winds up being a NOP when the PTI device
 *      driver, misc/pti.c is not compiled with the kernel.
 */
static int n_tracerouter_ioctl(struct tty_struct *tty, struct file *file,
			       unsigned int cmd, unsigned long arg)
{
	u8 copy_tmp = 0;
	int retval = 0;
	struct tracerouter_data *tptr = tty->disc_data;

	pr_debug("%s(%s): called\n", __FILE__, __func__);

	switch (cmd) {
	case PTIHW_SETCONF:

		mutex_lock(&routelock);

		/* with the #ifdef below, this ioctl() basically is
		 * (and should be a harmless) NOP in the case a
		 * target system has not configured
		 * the PTI driver.  alloc() makes sure routing to n_tracesink
		 * is default case, and this if() makes sure that in
		 * the event PTI driver is configured into a Linux
		 * solution that trace data can be routed to the
		 * n_tracesink if the PTI driver is first selected.
		 */
		if (access_ok(VERIFY_READ, (void *) arg, sizeof(int))) {
			if (get_user(copy_tmp, (int __user *) arg))
				retval = -EFAULT;
		}
		pr_debug("%s(%s): after access_ok() ioctl. Return value %d\n",
				 __FILE__, __func__, retval);
		pr_debug("%s(%s): after access_ok() ioctl. copy_tmp %d\n",
				 __FILE__, __func__, copy_tmp);
		tptr->routedata_ptihw = copy_tmp;
		if (tptr->routedata_ptihw == 0)
			tptr->routedata = mipi_pti_sinkdata;

		/* in case someone does not configure misc/pti.c
		 * into the kernel build (like the build is
		 * targeted for a HW solution without PTI HW),
		 * this will prevent an
		 * 'undefined reference' warning. For this
		 * system, it is perfectly acceptable for a kernel
		 * user to not configure the PTI HW into the
		 * kernel because it may not be on the HW platform, but
		 * to use this trace router and the trace sink
		 * for a given system's trace route needs.
		 */
#ifdef CONFIG_INTEL_MID_PTI
		else {
			tptr->routedata = mipi_pti_writedata;
			pr_debug("%s(%s): writedata set. Return value %d\n",
				 __FILE__, __func__, retval);
		}
#endif
		mutex_unlock(&routelock);
		return retval;

	default:
		retval = n_tty_ioctl_helper(tty, file, cmd, arg);
		return retval;
	}
}

/**
 * n_tracerouter_receivebuf() - Routing function for driver.
 * @tty: terminal device passed into the ldisc.  It's assumed
 *       tty will never be NULL.
 * @cp:  buffer, block of characters to be eventually read by
 *       someone, somewhere (user read() call or some kernel function).
 * @fp:  flag buffer.
 * @count: number of characters (aka, bytes) in cp.
 *
 * This function takes the input buffer, cp, and passes it to
 * an external API function for processing.
 */
static void n_tracerouter_receivebuf(struct tty_struct *tty,
					const unsigned char *cp,
					char *fp, int count)
{
	struct tracerouter_data *tptr = tty->disc_data;

	/* 71 is the master ID for modem messages */
	/* Only channel 0 for now */
	static struct masterchannel mc = {.master = 71, .channel = 0 };

	pr_debug("%s(%s): calling routedata()\n", __FILE__, __func__);
	mutex_lock(&routelock);
	tptr->routedata((void *) &mc, (u8 *)cp, count);
	mutex_unlock(&routelock);
}

/* Flush buffer is not impelemented as the ldisc has no internal buffering
 * so the tty_driver_flush_buffer() is sufficient for this driver's needs.
 */

static struct tty_ldisc_ops tty_ptirouter_ldisc = {
	.owner		= THIS_MODULE,
	.magic		= TTY_LDISC_MAGIC,
	.name		= DRIVERNAME,
	.open		= n_tracerouter_open,
	.close		= n_tracerouter_close,
	.read		= n_tracerouter_read,
	.write		= n_tracerouter_write,
	.ioctl		= n_tracerouter_ioctl,
	.receive_buf	= n_tracerouter_receivebuf
};

/**
 * n_tracerouter_init		-	module initialisation
 *
 * Registers this module as a line discipline driver.
 *
 * Return:
 *	0 for success, any other value error.
 */
static int __init n_tracerouter_init(void)
{
	int retval;

	tr_data = tracerouter_alloc();
	if (tr_data == NULL)
		return -ENOMEM;

	/* Note N_TRACEROUTER is defined in linux/tty.h */
	retval = tty_register_ldisc(N_TRACEROUTER, &tty_ptirouter_ldisc);
	if (retval < 0) {
		pr_err("%s: Registration failed: %d\n",
					__func__, retval);
		kfree(tr_data);
	}
	return retval;
}

/**
 * n_tracerouter_exit -	-	module unload
 *
 * Removes this module as a line discipline driver.
 */
static void __exit n_tracerouter_exit(void)
{
	int retval;

	kfree(tr_data);
	retval = tty_unregister_ldisc(N_TRACEROUTER);
	if (retval < 0)
		pr_err("%s: Unregistration failed: %d\n",
					__func__,  retval);
}

module_init(n_tracerouter_init);
module_exit(n_tracerouter_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Jay Freyensee");
MODULE_ALIAS_LDISC(N_TRACEROUTER);
MODULE_DESCRIPTION("PTI Router ldisc driver");