aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/misc/cw_tty.c
blob: 016f8e23279ba75ba344852aa2c1c4d9948a4ac4 (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
/* linux/arch/arm/mach-xxxx/board-c1ctc-modems.c
 * Copyright (C) 2010 Samsung Electronics. All rights reserved.
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * 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.
 */

#include <linux/module.h>
#include <linux/errno.h>
#include <linux/signal.h>
#include <linux/sched.h>
#include <linux/timer.h>
#include <linux/interrupt.h>
#include <linux/tty.h>
#include <linux/tty_flip.h>
#include <linux/major.h>
#include <linux/string.h>
#include <linux/fcntl.h>
#include <linux/mm.h>
#include <linux/kernel.h>
#include <linux/console.h>
#include <linux/reboot.h>
#include <linux/keyboard.h>
#include <linux/init.h>
#include <linux/pm.h>
#include <linux/bitops.h>
#include <linux/delay.h>
#include <linux/gfp.h>
#include <linux/io.h>
#include <linux/irq.h>
#include <linux/delay.h>
#include <linux/uaccess.h>

#include <linux/time.h>
#include <linux/slab.h>
#include <linux/unistd.h>

#define ID_CW_TTY_CWPPP			36
#define ID_CW_TTY_CWXAN			41

#define CW_TTY_MAJOR_NUM		235

/* Maximum PDP data length */
#define MAX_PDP_DATA_LEN		1500

/* Maximum PDP packet length including header and start/stop bytes */
#define MAX_PDP_PACKET_LEN		(MAX_PDP_DATA_LEN + 4 + 2)

#define MAX_CW_TTY_BUFFER_LEN		(8192*2)

struct cw_tty_info {
	/* PDP context ID */
	u8			id;

	/* Tx packet buffer */
	u8			tx_buf[MAX_CW_TTY_BUFFER_LEN];

	/* App device interface */
	union {
		/* Virtual serial interface */
		struct {
			struct tty_driver	tty_driver[2];
			int			refcount;
			struct tty_struct	*tty_table[1];
			struct ktermios		*termios[1];
			struct ktermios		*termios_locked[1];
			char			tty_name[64];
			struct tty_struct	*tty;
			struct semaphore	write_lock;
		} vs_u;
	} dev_u;
#define vs_dev		dev_u.vs_u
};

struct cw_tty_info *cw_tty[2];

struct cw_tty_info *cw_tty_get_dev_by_id(int id)
{
	struct cw_tty_info *dev;

	if (id == ID_CW_TTY_CWPPP)
		dev = cw_tty[0];
	else if (id == ID_CW_TTY_CWXAN)
		dev = cw_tty[1];
	else
		dev = NULL;

	return dev;
}

struct cw_tty_info *cw_tty_get_dev_by_name(const char *name)
{
	struct cw_tty_info *dev;

	if (strcmp("ttyCWPPP", name) == 0)
		dev = cw_tty[0];
	else if (strcmp("ttyCWXAN", name) == 0)
		dev = cw_tty[1];
	else
		dev = NULL;

	return dev;
}

/* Added by Venkatesh GR SISO Received PPP Data is sent to ppp */
static int XanCwReceivedWiFiData(int nDestChannelID,
				u8 *pi8_RecvdBuf, int nRecvdBuflen)
{
	int ret = 0;
	struct cw_tty_info *dev = NULL;

	if (pi8_RecvdBuf == NULL) {
		pr_err("[%s] input buffer is NULL\n", __func__);
		return 0;
	}

	dev = cw_tty_get_dev_by_id(nDestChannelID);
	if (dev == NULL) {
		pr_err("[%s] cw_tty_get_dev_by_id is NULL\n", __func__);
		return 0;
	}

	if (dev->vs_dev.tty != NULL && dev->vs_dev.refcount) {
		ret = tty_insert_flip_string(dev->vs_dev.tty,
					(u8 *)pi8_RecvdBuf, nRecvdBuflen);
		if (ret < nRecvdBuflen)
			pr_err("[%s] tty_insert_flip_string fail %d\n",
								__func__, ret);

		tty_flip_buffer_push(dev->vs_dev.tty);
	} else
		pr_err("[%s] refcount: %d\n", __func__,
						dev->vs_dev.refcount);

	return ret;
}

/* Added by Venkatesh GR SISO Received PPP Data from pppd to Xanadu */
static int XanCwSendToXanadu(int nSrcChannelID,
		int nDestChannelID, u8 *pi8_RecvdBuf, int nRecvdBuflen)
{
	int ret = 0;
	struct cw_tty_info *dev = NULL;
	int nXanChannelID = nDestChannelID;

	if (pi8_RecvdBuf == NULL) {
		pr_err("[%s] input buffer is NULL\n", __func__);
		return 0;
	}

	dev = cw_tty_get_dev_by_id(nXanChannelID);
	if (dev == NULL) {
		pr_err("[%s] cw_tty_get_dev_by_id is NULL\n", __func__);
		return 0;
	}

	if (dev->vs_dev.tty != NULL && dev->vs_dev.refcount) {
		ret = tty_insert_flip_string(dev->vs_dev.tty,
					(u8 *)pi8_RecvdBuf, nRecvdBuflen);
		if (ret < nRecvdBuflen)
			pr_err("[%s] tty_insert_flip_string returned %d\n",
								__func__, ret);

		tty_flip_buffer_push(dev->vs_dev.tty);
	} else
		pr_err("[%s] refcount: %d\n", __func__,
						dev->vs_dev.refcount);

	return ret;
}


static int cw_tty_open(struct tty_struct *tty, struct file *filp)
{
	struct cw_tty_info *dev;

	pr_info("[%s] name: %s, current: %s\n", __func__,
					tty->driver->name, current->comm);

	dev = cw_tty_get_dev_by_name(tty->driver->name);
	if (dev == NULL) {
		pr_err("[%s] cw_tty_get_dev_by_name is NULL\n", __func__);
		return -ENODEV;
	}

	tty->driver_data = (void *)dev;
	tty->low_latency = 1;
	dev->vs_dev.tty = tty;
	dev->vs_dev.refcount++;

	pr_info("[%s] name:%s, refcount: %d\n", __func__,
				tty->driver->name, dev->vs_dev.refcount);

	return 0;
}

static void cw_tty_close(struct tty_struct *tty, struct file *filp)
{
	struct cw_tty_info *dev;

	dev = cw_tty_get_dev_by_name(tty->driver->name);
	if (dev == NULL) {
		pr_err("[%s] cw_tty_get_dev_by_name is NULL\n", __func__);
		return;
	}

	dev->vs_dev.refcount--;
	pr_info("[%s] name: %s, refcount: %d\n", __func__,
				tty->driver->name, dev->vs_dev.refcount);

	return;
}

static int cw_tty_write(struct tty_struct *tty,
			const unsigned char *buf, int count)
{
	int ret = 0;
	struct cw_tty_info *dev = (struct cw_tty_info *)tty->driver_data;
	int nDestChannelID = 0;

	/* Added by Venkatesh GR, SISO For CDMA+WiFi requirements -- Start*/
	if (strcmp(dev->vs_dev.tty_name, "ttyCWPPP") == 0) {
		nDestChannelID = ID_CW_TTY_CWXAN;

		/* Send the Buffer Received From PPPD to CWTunnel */
		ret = XanCwSendToXanadu(dev->id, nDestChannelID,
							(u8 *)buf, count);

	} else if (strcmp(dev->vs_dev.tty_name, "ttyCWXAN") == 0) {
		nDestChannelID = ID_CW_TTY_CWPPP;

		/* Send the Buffer Received From CWTunnel to PPPD */
		ret = XanCwReceivedWiFiData(nDestChannelID, (u8 *)buf, count);
	}

	return ret;
}

static int cw_tty_write_room(struct tty_struct *tty)
{
	return MAX_CW_TTY_BUFFER_LEN;
}

static int cw_tty_chars_in_buffer(struct tty_struct *tty)
{
	return 0;
}

static int cw_tty_ioctl(struct tty_struct *tty,
		    unsigned int cmd, unsigned long arg)
{
	pr_info("[%s] name: %s cmd: %x\n", __func__,
					tty->driver->name, cmd);

	return -ENOIOCTLCMD;
}

static const struct tty_operations cw_tty_ops = {
	.open = cw_tty_open,
	.close = cw_tty_close,
	.write = cw_tty_write,
	.write_room = cw_tty_write_room,
	.chars_in_buffer = cw_tty_chars_in_buffer,
	.ioctl = cw_tty_ioctl,
};

static int __init cw_tty_init(void)
{
	struct tty_driver	*tty_driver;
	struct cw_tty_info  *dev;

	dev = kmalloc(sizeof(struct cw_tty_info)
			+ MAX_PDP_PACKET_LEN, GFP_KERNEL);
	if (dev == NULL) {
		pr_err("[%s] out of memory\n", __func__);
		return -ENOMEM;
	}
	memset(dev, 0, sizeof(struct cw_tty_info));

	dev->id = ID_CW_TTY_CWPPP;
	strcpy(dev->vs_dev.tty_name, "ttyCWPPP");
	tty_driver = &dev->vs_dev.tty_driver[0];

	kref_init(&tty_driver->kref);

	tty_driver->magic = TTY_DRIVER_MAGIC;
	tty_driver->driver_name	= "ttyCW";
	tty_driver->name = "ttyCWPPP";
	tty_driver->major = CW_TTY_MAJOR_NUM;
	tty_driver->minor_start = dev->id;
	tty_driver->num		= 1;
	tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
	tty_driver->subtype = SERIAL_TYPE_NORMAL;
	tty_driver->flags = TTY_DRIVER_REAL_RAW;
	tty_driver->ttys	= dev->vs_dev.tty_table;
	tty_driver->termios	= dev->vs_dev.termios;
	tty_driver->termios_locked	= dev->vs_dev.termios_locked;
	tty_set_operations(tty_driver, &cw_tty_ops);

	if (tty_register_driver(tty_driver)) {
		put_tty_driver(tty_driver);
		pr_err("[%s] Couldn't register ttyCWPPP driver\n", __func__);
		return -ENOMEM;
	}
	sema_init(&dev->vs_dev.write_lock, 1);
	cw_tty[0] = dev;

	dev = kmalloc(sizeof(struct cw_tty_info)
			+ MAX_PDP_PACKET_LEN, GFP_KERNEL);
	if (dev == NULL) {
		pr_err("[%s] out of memory\n", __func__);
		return -ENOMEM;
	}
	memset(dev, 0, sizeof(struct cw_tty_info));

	dev->id = ID_CW_TTY_CWXAN;
	strcpy(dev->vs_dev.tty_name, "ttyCWXAN");
	tty_driver = &dev->vs_dev.tty_driver[1];

	kref_init(&tty_driver->kref);

	tty_driver->magic = TTY_DRIVER_MAGIC;
	tty_driver->driver_name	= "ttyCW";
	tty_driver->name = "ttyCWXAN";
	tty_driver->major = CW_TTY_MAJOR_NUM;
	tty_driver->minor_start = dev->id;
	tty_driver->num		= 1;
	tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
	tty_driver->subtype = SERIAL_TYPE_NORMAL;
	tty_driver->flags = TTY_DRIVER_REAL_RAW;
	tty_driver->ttys	= dev->vs_dev.tty_table;
	tty_driver->termios	= dev->vs_dev.termios;
	tty_driver->termios_locked	= dev->vs_dev.termios_locked;
	tty_set_operations(tty_driver, &cw_tty_ops);

	if (tty_register_driver(tty_driver)) {
		put_tty_driver(tty_driver);
		pr_err("[%s] Couldn't register ttyCWXAN driver\n", __func__);
		return -ENOMEM;
	}
	sema_init(&dev->vs_dev.write_lock, 1);
	cw_tty[1] = dev;

	return 0;
}

late_initcall(cw_tty_init);