2 * Copyright (c) 2012 GCT Semiconductor, Inc. All rights reserved.
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.
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.
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
28 #define GDM_TTY_MAJOR 0
29 #define GDM_TTY_MINOR 32
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
37 #define WRITE_SIZE 2048
39 #define MUX_TX_MAX_SIZE 2048
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))
48 #define GDM_TTY_READY(gdm) (gdm && gdm->tty_dev && gdm->port.count)
50 static struct tty_driver *gdm_driver[TTY_MAX_COUNT];
51 static struct gdm *gdm_table[TTY_MAX_COUNT][GDM_TTY_MINOR];
52 static DEFINE_MUTEX(gdm_table_lock);
54 static char *DRIVER_STRING[TTY_MAX_COUNT] = {"GCTATC", "GCTDM"};
55 static char *DEVICE_STRING[TTY_MAX_COUNT] = {"GCT-ATC", "GCT-DM"};
57 static void gdm_port_destruct(struct tty_port *port)
59 struct gdm *gdm = container_of(port, struct gdm, port);
61 mutex_lock(&gdm_table_lock);
62 gdm_table[gdm->index][gdm->minor] = NULL;
63 mutex_unlock(&gdm_table_lock);
68 static struct tty_port_operations gdm_port_ops = {
69 .destruct = gdm_port_destruct,
72 static int gdm_tty_install(struct tty_driver *driver, struct tty_struct *tty)
74 struct gdm *gdm = NULL;
80 for (i = 0; i < TTY_MAX_COUNT; i++) {
81 if (!strcmp(tty->driver->driver_name, DRIVER_STRING[i])) {
87 if (j == GDM_TTY_MINOR)
90 mutex_lock(&gdm_table_lock);
91 gdm = gdm_table[i][j];
93 mutex_unlock(&gdm_table_lock);
97 tty_port_get(&gdm->port);
99 ret = tty_standard_install(driver, tty);
101 tty_port_put(&gdm->port);
102 mutex_unlock(&gdm_table_lock);
106 tty->driver_data = gdm;
107 mutex_unlock(&gdm_table_lock);
112 static int gdm_tty_open(struct tty_struct *tty, struct file *filp)
114 struct gdm *gdm = tty->driver_data;
115 return tty_port_open(&gdm->port, tty, filp);
118 static void gdm_tty_cleanup(struct tty_struct *tty)
120 struct gdm *gdm = tty->driver_data;
121 tty_port_put(&gdm->port);
124 static void gdm_tty_hangup(struct tty_struct *tty)
126 struct gdm *gdm = tty->driver_data;
127 tty_port_hangup(&gdm->port);
130 static void gdm_tty_close(struct tty_struct *tty, struct file *filp)
132 struct gdm *gdm = tty->driver_data;
133 tty_port_close(&gdm->port, tty, filp);
136 static int gdm_tty_recv_complete(void *data,
139 struct tty_dev *tty_dev,
142 struct gdm *gdm = tty_dev->gdm[index];
143 if (!GDM_TTY_READY(gdm)) {
144 if (complete == RECV_PACKET_PROCESS_COMPLETE)
145 gdm_tty_recv(gdm, gdm_tty_recv_complete);
146 return TO_HOST_PORT_CLOSE;
150 if (tty_buffer_request_room(&gdm->port, len) == len) {
151 tty_insert_flip_string(&gdm->port, data, len);
152 tty_flip_buffer_push(&gdm->port);
154 return TO_HOST_BUFFER_REQUEST_FAIL;
158 if (complete == RECV_PACKET_PROCESS_COMPLETE)
159 gdm_tty_recv(gdm, gdm_tty_recv_complete);
164 static void gdm_tty_send_complete(void *arg)
166 struct gdm *gdm = (struct gdm *)arg;
168 if (!GDM_TTY_READY(gdm))
171 tty_port_tty_wakeup(&gdm->port);
174 static int gdm_tty_write(struct tty_struct *tty, const unsigned char *buf,
177 struct gdm *gdm = tty->driver_data;
182 if (!GDM_TTY_READY(gdm))
189 sending_len = remain > MUX_TX_MAX_SIZE ? MUX_TX_MAX_SIZE :
192 (void *)(buf+sent_len),
195 gdm_tty_send_complete,
198 sent_len += sending_len;
199 remain -= sending_len;
207 static int gdm_tty_write_room(struct tty_struct *tty)
209 struct gdm *gdm = tty->driver_data;
211 if (!GDM_TTY_READY(gdm))
217 int register_lte_tty_device(struct tty_dev *tty_dev, struct device *device)
223 for (i = 0; i < TTY_MAX_COUNT; i++) {
225 gdm = kmalloc(sizeof(struct gdm), GFP_KERNEL);
229 mutex_lock(&gdm_table_lock);
230 for (j = 0; j < GDM_TTY_MINOR; j++) {
231 if (!gdm_table[i][j])
235 if (j == GDM_TTY_MINOR) {
237 mutex_unlock(&gdm_table_lock);
241 gdm_table[i][j] = gdm;
242 mutex_unlock(&gdm_table_lock);
244 tty_dev->gdm[i] = gdm;
245 tty_port_init(&gdm->port);
247 gdm->port.ops = &gdm_port_ops;
250 gdm->tty_dev = tty_dev;
252 tty_port_register_device(&gdm->port, gdm_driver[i],
256 for (i = 0; i < MAX_ISSUE_NUM; i++)
257 gdm_tty_recv(gdm, gdm_tty_recv_complete);
262 void unregister_lte_tty_device(struct tty_dev *tty_dev)
265 struct tty_struct *tty;
268 for (i = 0; i < TTY_MAX_COUNT; i++) {
269 gdm = tty_dev->gdm[i];
273 mutex_lock(&gdm_table_lock);
274 gdm_table[gdm->index][gdm->minor] = NULL;
275 mutex_unlock(&gdm_table_lock);
277 tty = tty_port_tty_get(&gdm->port);
283 tty_unregister_device(gdm_driver[i], gdm->minor);
284 tty_port_put(&gdm->port);
288 static const struct tty_operations gdm_tty_ops = {
289 .install = gdm_tty_install,
290 .open = gdm_tty_open,
291 .close = gdm_tty_close,
292 .cleanup = gdm_tty_cleanup,
293 .hangup = gdm_tty_hangup,
294 .write = gdm_tty_write,
295 .write_room = gdm_tty_write_room,
298 int register_lte_tty_driver(void)
300 struct tty_driver *tty_driver;
304 for (i = 0; i < TTY_MAX_COUNT; i++) {
305 tty_driver = alloc_tty_driver(GDM_TTY_MINOR);
309 tty_driver->owner = THIS_MODULE;
310 tty_driver->driver_name = DRIVER_STRING[i];
311 tty_driver->name = DEVICE_STRING[i];
312 tty_driver->major = GDM_TTY_MAJOR;
313 tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
314 tty_driver->subtype = SERIAL_TYPE_NORMAL;
315 tty_driver->flags = TTY_DRIVER_REAL_RAW |
316 TTY_DRIVER_DYNAMIC_DEV;
317 tty_driver->init_termios = tty_std_termios;
318 tty_driver->init_termios.c_cflag = B9600 | CS8 | HUPCL | CLOCAL;
319 tty_driver->init_termios.c_lflag = ISIG | ICANON | IEXTEN;
320 tty_set_operations(tty_driver, &gdm_tty_ops);
322 ret = tty_register_driver(tty_driver);
324 put_tty_driver(tty_driver);
328 gdm_driver[i] = tty_driver;
334 void unregister_lte_tty_driver(void)
336 struct tty_driver *tty_driver;
339 for (i = 0; i < TTY_MAX_COUNT; i++) {
340 tty_driver = gdm_driver[i];
342 tty_unregister_driver(tty_driver);
343 put_tty_driver(tty_driver);