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