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/tty.h>
19 #include <linux/tty_driver.h>
20 #include <linux/tty_flip.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/usb/cdc.h>
24 #include <linux/serial.h>
27 #define GDM_TTY_MAJOR 0
28 #define GDM_TTY_MINOR 32
30 #define ACM_CTRL_DTR 0x01
31 #define ACM_CTRL_RTS 0x02
32 #define ACM_CTRL_DSR 0x02
33 #define ACM_CTRL_RI 0x08
34 #define ACM_CTRL_DCD 0x01
36 #define WRITE_SIZE 2048
38 #define MUX_TX_MAX_SIZE 2048
40 #define gdm_tty_send(n, d, l, i, c, b) (\
41 n->tty_dev->send_func(n->tty_dev->priv_dev, d, l, i, c, b))
42 #define gdm_tty_recv(n, c) (\
43 n->tty_dev->recv_func(n->tty_dev->priv_dev, c))
44 #define gdm_tty_send_control(n, r, v, d, l) (\
45 n->tty_dev->send_control(n->tty_dev->priv_dev, r, v, d, l))
47 #define GDM_TTY_READY(gdm) (gdm && gdm->tty_dev && gdm->port.count)
49 static struct tty_driver
*gdm_driver
[TTY_MAX_COUNT
];
50 static struct gdm
*gdm_table
[TTY_MAX_COUNT
][GDM_TTY_MINOR
];
51 static DEFINE_MUTEX(gdm_table_lock
);
53 static char *DRIVER_STRING
[TTY_MAX_COUNT
] = {"GCTATC", "GCTDM"};
54 static char *DEVICE_STRING
[TTY_MAX_COUNT
] = {"GCT-ATC", "GCT-DM"};
56 static void gdm_port_destruct(struct tty_port
*port
)
58 struct gdm
*gdm
= container_of(port
, struct gdm
, port
);
60 mutex_lock(&gdm_table_lock
);
61 gdm_table
[gdm
->index
][gdm
->minor
] = NULL
;
62 mutex_unlock(&gdm_table_lock
);
67 static struct tty_port_operations gdm_port_ops
= {
68 .destruct
= gdm_port_destruct
,
71 static int gdm_tty_install(struct tty_driver
*driver
, struct tty_struct
*tty
)
73 struct gdm
*gdm
= NULL
;
79 for (i
= 0; i
< TTY_MAX_COUNT
; i
++) {
80 if (!strcmp(tty
->driver
->driver_name
, DRIVER_STRING
[i
])) {
86 if (j
== GDM_TTY_MINOR
)
89 mutex_lock(&gdm_table_lock
);
90 gdm
= gdm_table
[i
][j
];
92 mutex_unlock(&gdm_table_lock
);
96 tty_port_get(&gdm
->port
);
98 ret
= tty_standard_install(driver
, tty
);
100 tty_port_put(&gdm
->port
);
101 mutex_unlock(&gdm_table_lock
);
105 tty
->driver_data
= gdm
;
106 mutex_unlock(&gdm_table_lock
);
111 static int gdm_tty_open(struct tty_struct
*tty
, struct file
*filp
)
113 struct gdm
*gdm
= tty
->driver_data
;
114 return tty_port_open(&gdm
->port
, tty
, filp
);
117 static void gdm_tty_cleanup(struct tty_struct
*tty
)
119 struct gdm
*gdm
= tty
->driver_data
;
120 tty_port_put(&gdm
->port
);
123 static void gdm_tty_hangup(struct tty_struct
*tty
)
125 struct gdm
*gdm
= tty
->driver_data
;
126 tty_port_hangup(&gdm
->port
);
129 static void gdm_tty_close(struct tty_struct
*tty
, struct file
*filp
)
131 struct gdm
*gdm
= tty
->driver_data
;
132 tty_port_close(&gdm
->port
, tty
, filp
);
135 static int gdm_tty_recv_complete(void *data
,
138 struct tty_dev
*tty_dev
,
141 struct gdm
*gdm
= tty_dev
->gdm
[index
];
142 if (!GDM_TTY_READY(gdm
)) {
143 if (complete
== RECV_PACKET_PROCESS_COMPLETE
)
144 gdm_tty_recv(gdm
, gdm_tty_recv_complete
);
145 return TO_HOST_PORT_CLOSE
;
149 if (tty_buffer_request_room(&gdm
->port
, len
) == len
) {
150 tty_insert_flip_string(&gdm
->port
, data
, len
);
151 tty_flip_buffer_push(&gdm
->port
);
153 return TO_HOST_BUFFER_REQUEST_FAIL
;
157 if (complete
== RECV_PACKET_PROCESS_COMPLETE
)
158 gdm_tty_recv(gdm
, gdm_tty_recv_complete
);
163 static void gdm_tty_send_complete(void *arg
)
165 struct gdm
*gdm
= (struct gdm
*)arg
;
167 if (!GDM_TTY_READY(gdm
))
170 tty_port_tty_wakeup(&gdm
->port
);
173 static int gdm_tty_write(struct tty_struct
*tty
, const unsigned char *buf
,
176 struct gdm
*gdm
= tty
->driver_data
;
181 if (!GDM_TTY_READY(gdm
))
188 sending_len
= remain
> MUX_TX_MAX_SIZE
? MUX_TX_MAX_SIZE
:
191 (void *)(buf
+sent_len
),
194 gdm_tty_send_complete
,
197 sent_len
+= sending_len
;
198 remain
-= sending_len
;
206 static int gdm_tty_write_room(struct tty_struct
*tty
)
208 struct gdm
*gdm
= tty
->driver_data
;
210 if (!GDM_TTY_READY(gdm
))
216 int register_lte_tty_device(struct tty_dev
*tty_dev
, struct device
*device
)
222 for (i
= 0; i
< TTY_MAX_COUNT
; i
++) {
224 gdm
= kmalloc(sizeof(struct gdm
), GFP_KERNEL
);
228 mutex_lock(&gdm_table_lock
);
229 for (j
= 0; j
< GDM_TTY_MINOR
; j
++) {
230 if (!gdm_table
[i
][j
])
234 if (j
== GDM_TTY_MINOR
) {
236 mutex_unlock(&gdm_table_lock
);
240 gdm_table
[i
][j
] = gdm
;
241 mutex_unlock(&gdm_table_lock
);
243 tty_dev
->gdm
[i
] = gdm
;
244 tty_port_init(&gdm
->port
);
246 gdm
->port
.ops
= &gdm_port_ops
;
249 gdm
->tty_dev
= tty_dev
;
251 tty_port_register_device(&gdm
->port
, gdm_driver
[i
],
255 for (i
= 0; i
< MAX_ISSUE_NUM
; i
++)
256 gdm_tty_recv(gdm
, gdm_tty_recv_complete
);
261 void unregister_lte_tty_device(struct tty_dev
*tty_dev
)
264 struct tty_struct
*tty
;
267 for (i
= 0; i
< TTY_MAX_COUNT
; i
++) {
268 gdm
= tty_dev
->gdm
[i
];
272 mutex_lock(&gdm_table_lock
);
273 gdm_table
[gdm
->index
][gdm
->minor
] = NULL
;
274 mutex_unlock(&gdm_table_lock
);
276 tty
= tty_port_tty_get(&gdm
->port
);
282 tty_unregister_device(gdm_driver
[i
], gdm
->minor
);
283 tty_port_put(&gdm
->port
);
287 static const struct tty_operations gdm_tty_ops
= {
288 .install
= gdm_tty_install
,
289 .open
= gdm_tty_open
,
290 .close
= gdm_tty_close
,
291 .cleanup
= gdm_tty_cleanup
,
292 .hangup
= gdm_tty_hangup
,
293 .write
= gdm_tty_write
,
294 .write_room
= gdm_tty_write_room
,
297 int register_lte_tty_driver(void)
299 struct tty_driver
*tty_driver
;
303 for (i
= 0; i
< TTY_MAX_COUNT
; i
++) {
304 tty_driver
= alloc_tty_driver(GDM_TTY_MINOR
);
308 tty_driver
->owner
= THIS_MODULE
;
309 tty_driver
->driver_name
= DRIVER_STRING
[i
];
310 tty_driver
->name
= DEVICE_STRING
[i
];
311 tty_driver
->major
= GDM_TTY_MAJOR
;
312 tty_driver
->type
= TTY_DRIVER_TYPE_SERIAL
;
313 tty_driver
->subtype
= SERIAL_TYPE_NORMAL
;
314 tty_driver
->flags
= TTY_DRIVER_REAL_RAW
|
315 TTY_DRIVER_DYNAMIC_DEV
;
316 tty_driver
->init_termios
= tty_std_termios
;
317 tty_driver
->init_termios
.c_cflag
= B9600
| CS8
| HUPCL
| CLOCAL
;
318 tty_driver
->init_termios
.c_lflag
= ISIG
| ICANON
| IEXTEN
;
319 tty_set_operations(tty_driver
, &gdm_tty_ops
);
321 ret
= tty_register_driver(tty_driver
);
323 put_tty_driver(tty_driver
);
327 gdm_driver
[i
] = tty_driver
;
333 void unregister_lte_tty_driver(void)
335 struct tty_driver
*tty_driver
;
338 for (i
= 0; i
< TTY_MAX_COUNT
; i
++) {
339 tty_driver
= gdm_driver
[i
];
341 tty_unregister_driver(tty_driver
);
342 put_tty_driver(tty_driver
);