1 /*
2  * Copyright (c) 2012 GCT Semiconductor, Inc. All rights reserved.
3  *
4  * This software is licensed under the terms of the GNU General Public
5  * License version 2, as published by the Free Software Foundation, and
6  * may be copied, distributed, and modified under those terms.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  */
13 
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 
16 #include <linux/version.h>
17 #include <linux/kernel.h>
18 #include <linux/errno.h>
19 #include <linux/init.h>
20 #include <linux/tty.h>
21 #include <linux/tty_driver.h>
22 #include <linux/tty_flip.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/usb/cdc.h>
26 #include <linux/serial.h>
27 #include "gdm_tty.h"
28 
29 #define GDM_TTY_MAJOR 0
30 #define GDM_TTY_MINOR 32
31 
32 #define ACM_CTRL_DTR 0x01
33 #define ACM_CTRL_RTS 0x02
34 #define ACM_CTRL_DSR 0x02
35 #define ACM_CTRL_RI  0x08
36 #define ACM_CTRL_DCD 0x01
37 
38 #define WRITE_SIZE 2048
39 
40 #define MUX_TX_MAX_SIZE 2048
41 
42 #define gdm_tty_send(n, d, l, i, c, b) (\
43 	n->tty_dev->send_func(n->tty_dev->priv_dev, d, l, i, c, b))
44 #define gdm_tty_recv(n, c) (\
45 	n->tty_dev->recv_func(n->tty_dev->priv_dev, c))
46 #define gdm_tty_send_control(n, r, v, d, l) (\
47 	n->tty_dev->send_control(n->tty_dev->priv_dev, r, v, d, l))
48 
49 #define acm_set_comm_feature(n, v)	\
50 	gdm_tty_send_control(n, 0x02, v, NULL, 0)
51 
52 #define GDM_TTY_READY(tty_str) (tty_str && tty_str->tty_dev && tty_str->port.count)
53 
54 struct tty_driver *g_tty_drv[TTY_MAX_COUNT] = {NULL, };
55 struct tty_str *g_tty_str[TTY_MAX_COUNT][GDM_TTY_MINOR] = {{NULL, }, };
56 
57 static char *DRIVER_STRING[TTY_MAX_COUNT] = {"GCTATC", "GCTDM"};
58 static char *DEVICE_STRING[TTY_MAX_COUNT] = {"GCT-ATC", "GCT-DM"};
59 
60 static DEFINE_MUTEX(open_mutex);
61 
62 static struct tty_port_operations gdm_tty_port_ops = {
63 };
64 
65 static int gdm_tty_open(struct tty_struct *tty, struct file *filp)
66 {
67 	struct tty_str *tty_str = NULL;
68 	int i;
69 	int ret = 0;
70 
71 	mutex_lock(&open_mutex);
72 
73 	for (i = 0; i < TTY_MAX_COUNT; i++) {
74 		if (!strcmp(tty->driver->driver_name, DRIVER_STRING[i])) {
75 			tty_str = g_tty_str[i][tty->index];
76 			break;
77 		}
78 	}
79 
80 	if (!tty_str) {
81 		pr_info("no tty device\n");
82 		mutex_unlock(&open_mutex);
83 		return -ENODEV;
84 	}
85 
86 	set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
87 
88 	tty->driver_data = tty_str;
89 	tty_port_tty_set(&tty_str->port, tty);
90 	tty_str->port.count++;
91 	set_bit(ASYNCB_INITIALIZED, &tty_str->port.flags);
92 	ret = tty_port_block_til_ready(&tty_str->port, tty, filp);
93 
94 	mutex_unlock(&open_mutex);
95 
96 	return ret;
97 }
98 
99 static void gdm_tty_close(struct tty_struct *tty, struct file *filp)
100 {
101 	struct tty_str *tty_str = tty->driver_data;
102 	int i;
103 
104 	if (!tty_str) {
105 		pr_info("tty device already closed\n");
106 		return;
107 	}
108 
109 	if (tty_str->port.count != 0) {
110 		tty_port_close_start(&tty_str->port, tty, filp);
111 		tty_port_close_end(&tty_str->port, tty);
112 
113 		if (tty_str->port.count == 0)
114 			tty_port_tty_set(&tty_str->port, NULL);
115 			tty_str->port.tty = NULL;
116 	}
117 
118 	if (!tty_str->tty_dev) {
119 		for (i = 0; i < TTY_MAX_COUNT; i++) {
120 			if (!strcmp(tty->driver->driver_name, DRIVER_STRING[i]))
121 				break;
122 		}
123 
124 		if (i < TTY_MAX_COUNT) {
125 			tty_unregister_device(g_tty_drv[i], tty->index);
126 			tty_port_tty_set(&tty_str->port, NULL);
127 			kfree(tty_str);
128 			g_tty_str[i][tty->index] = NULL;
129 		}
130 	}
131 }
132 
133 static int gdm_tty_recv_complete(void *data, int len, int index, int minor, int complete)
134 {
135 	struct tty_str *tty_str = g_tty_str[index][minor];
136 	struct tty_port *tty_port;
137 
138 	if (!GDM_TTY_READY(tty_str)) {
139 		if (complete == RECV_PACKET_PROCESS_COMPLETE)
140 			gdm_tty_recv(tty_str, gdm_tty_recv_complete);
141 		return TO_HOST_PORT_CLOSE;
142 	}
143 
144 	if (!data || !len)
145 		goto complete_routine;
146 
147 	tty_port = &tty_str->port;
148 
149 	if (tty_buffer_request_room(tty_port, len) == len) {
150 		tty_insert_flip_string(tty_port, data, len);
151 		tty_flip_buffer_push(tty_port);
152 	} else {
153 		return TO_HOST_BUFFER_REQUEST_FAIL;
154 	}
155 
156 complete_routine:
157 	if (complete == RECV_PACKET_PROCESS_COMPLETE)
158 		gdm_tty_recv(tty_str, gdm_tty_recv_complete);
159 
160 	return 0;
161 }
162 
163 static void gdm_tty_send_complete(void *arg)
164 {
165 	struct tty_str *tty_str = (struct tty_str *)arg;
166 	struct tty_struct *tty;
167 
168 	if (!GDM_TTY_READY(tty_str))
169 		return;
170 
171 	tty = tty_port_tty_get(&tty_str->port);
172 	tty_wakeup(tty);
173 	tty_kref_put(tty);
174 }
175 
176 static int gdm_tty_write(struct tty_struct *tty, const unsigned char *buf, int len)
177 {
178 	struct tty_str *tty_str = tty->driver_data;
179 	int remain = len;
180 	int sent_len = 0;
181 	int sending_len = 0;
182 
183 	if (!GDM_TTY_READY(tty_str))
184 		return -ENODEV;
185 
186 	if (!len)
187 		return 0;
188 
189 	while (1) {
190 		sending_len = remain > MUX_TX_MAX_SIZE ? MUX_TX_MAX_SIZE : remain;
191 		gdm_tty_send(tty_str,
192 			     (void *)(buf+sent_len),
193 			     sending_len,
194 			     tty_str->tty_drv_index,
195 			     gdm_tty_send_complete,
196 			     tty_str
197 			    );
198 		sent_len += sending_len;
199 		remain -= sending_len;
200 		if (remain <= 0)
201 			break;
202 	}
203 
204 	return len;
205 }
206 
207 static int gdm_tty_write_room(struct tty_struct *tty)
208 {
209 	struct tty_str *tty_str = tty->driver_data;
210 
211 	if (!GDM_TTY_READY(tty_str))
212 		return -ENODEV;
213 
214 	return WRITE_SIZE;
215 }
216 
217 static int gdm_tty_tiocmget(struct tty_struct *tty)
218 {
219 	struct tty_str *tty_str = tty->driver_data;
220 
221 	if (!GDM_TTY_READY(tty_str))
222 		return -ENODEV;
223 
224 	return (0 & ACM_CTRL_DTR ? TIOCM_DTR : 0) |
225 	       (0 & ACM_CTRL_RTS ? TIOCM_RTS : 0) |
226 	       (0 & ACM_CTRL_DSR ? TIOCM_DSR : 0) |
227 	       (0 & ACM_CTRL_RI  ? TIOCM_RI  : 0) |
228 	       (0 & ACM_CTRL_DCD ? TIOCM_CD  : 0) |
229 	       TIOCM_CTS;
230 }
231 
232 static int gdm_tty_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
233 {
234 	struct tty_str *tty_str = tty->driver_data;
235 
236 	if (!GDM_TTY_READY(tty_str))
237 		return -ENODEV;
238 
239 	return 1;
240 }
241 
242 int register_lte_tty_device(struct tty_dev *tty_dev, struct device *dev)
243 {
244 	struct tty_str *tty_str;
245 	int i, j;
246 
247 	for (i = 0; i < TTY_MAX_COUNT; i++) {
248 		for (j = 0; j < GDM_TTY_MINOR; j++) {
249 			if (!g_tty_str[i][j])
250 				break;
251 		}
252 
253 		if (j == GDM_TTY_MINOR) {
254 			tty_dev->minor[i] = j;
255 			return -1;
256 		}
257 
258 		tty_str = kmalloc(sizeof(struct tty_str), GFP_KERNEL);
259 		if (!tty_str)
260 			return -ENOMEM;
261 
262 		g_tty_str[i][j] = tty_str;
263 
264 		tty_str->tty_dev = tty_dev;
265 		tty_str->tty_drv_index = i;
266 		tty_dev->minor[i] = j;
267 		tty_port_init(&tty_str->port);
268 		tty_str->port.ops = &gdm_tty_port_ops;
269 
270 		if (strcmp(DEVICE_STRING[i], "GCT-ATC") != 0)
271 			dev = NULL;
272 		tty_register_device(g_tty_drv[i], j, dev);
273 	}
274 
275 	acm_set_comm_feature(tty_str, 1);
276 
277 	for (i = 0; i < MAX_ISSUE_NUM; i++)
278 		gdm_tty_recv(tty_str, gdm_tty_recv_complete);
279 
280 	return 0;
281 }
282 
283 void unregister_lte_tty_device(struct tty_dev *tty_dev)
284 {
285 	struct tty_str *tty_str;
286 	int i;
287 
288 	for (i = 0; i < TTY_MAX_COUNT; i++) {
289 		if (tty_dev->minor[i] >= GDM_TTY_MINOR)
290 			continue;
291 
292 		tty_str = g_tty_str[i][tty_dev->minor[i]];
293 		if (!tty_str)
294 			continue;
295 
296 		tty_str->tty_dev = NULL;
297 
298 		if (!tty_str->port.count) {
299 			tty_unregister_device(g_tty_drv[i], tty_dev->minor[i]);
300 			tty_port_tty_set(&tty_str->port, NULL);
301 			kfree(tty_str);
302 			g_tty_str[i][tty_dev->minor[i]] = NULL;
303 		}
304 	}
305 }
306 
307 static void gdm_tty_set_termios(struct tty_struct *tty, struct ktermios *termios_old)
308 {
309 	return;
310 }
311 
312 static const struct tty_operations gdm_tty_ops = {
313 	.open = gdm_tty_open,
314 	.close = gdm_tty_close,
315 	.write = gdm_tty_write,
316 	.write_room = gdm_tty_write_room,
317 	.tiocmget = gdm_tty_tiocmget,
318 	.tiocmset = gdm_tty_tiocmset,
319 	.set_termios = gdm_tty_set_termios,
320 };
321 
322 int register_lte_tty_driver(void)
323 {
324 	struct tty_driver *tty_driver = NULL;
325 	int i;
326 	int ret;
327 
328 	for (i = 0; i < TTY_MAX_COUNT; i++) {
329 		tty_driver = alloc_tty_driver(GDM_TTY_MINOR);
330 		if (!tty_driver) {
331 			pr_err("alloc_tty_driver fail\n");
332 			return -ENOMEM;
333 		}
334 
335 		tty_driver->owner = THIS_MODULE;
336 		tty_driver->driver_name = DRIVER_STRING[i];
337 		tty_driver->name = DEVICE_STRING[i];
338 		tty_driver->major = GDM_TTY_MAJOR;
339 		tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
340 		tty_driver->subtype = SERIAL_TYPE_NORMAL;
341 		tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
342 		tty_driver->init_termios = tty_std_termios;
343 		tty_driver->init_termios.c_cflag = B9600 | CS8 | HUPCL | CLOCAL;
344 		tty_driver->init_termios.c_lflag = ISIG | ICANON | IEXTEN;
345 		tty_set_operations(tty_driver, &gdm_tty_ops);
346 
347 		ret = tty_register_driver(tty_driver);
348 
349 		g_tty_drv[i] = tty_driver;
350 	}
351 
352 	return ret;
353 }
354 
355 void unregister_lte_tty_driver(void)
356 {
357 	struct tty_driver *tty_driver;
358 	int i;
359 
360 	for (i = 0; i < TTY_MAX_COUNT; i++) {
361 		tty_driver = g_tty_drv[i];
362 		if (tty_driver) {
363 			tty_unregister_driver(tty_driver);
364 			put_tty_driver(tty_driver);
365 		}
366 	}
367 }
368