gdm_tty.c 7.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * Copyright (c) 2012 GCT Semiconductor, Inc. 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.
 */

J
Joe Perches 已提交
14 15
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

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
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/tty.h>
#include <linux/tty_driver.h>
#include <linux/tty_flip.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/usb/cdc.h>
#include <linux/serial.h>
#include "gdm_tty.h"

#define GDM_TTY_MAJOR 0
#define GDM_TTY_MINOR 32

#define ACM_CTRL_DTR 0x01
#define ACM_CTRL_RTS 0x02
#define ACM_CTRL_DSR 0x02
#define ACM_CTRL_RI  0x08
#define ACM_CTRL_DCD 0x01

#define WRITE_SIZE 2048

#define MUX_TX_MAX_SIZE 2048

#define gdm_tty_send(n, d, l, i, c, b) (\
	n->tty_dev->send_func(n->tty_dev->priv_dev, d, l, i, c, b))
#define gdm_tty_recv(n, c) (\
	n->tty_dev->recv_func(n->tty_dev->priv_dev, c))
#define gdm_tty_send_control(n, r, v, d, l) (\
	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)
49

50 51
static struct tty_driver *gdm_driver[TTY_MAX_COUNT];
static struct gdm *gdm_table[TTY_MAX_COUNT][GDM_TTY_MINOR];
52
static DEFINE_MUTEX(gdm_table_lock);
53 54 55 56

static char *DRIVER_STRING[TTY_MAX_COUNT] = {"GCTATC", "GCTDM"};
static char *DEVICE_STRING[TTY_MAX_COUNT] = {"GCT-ATC", "GCT-DM"};

57 58 59 60 61 62 63 64 65 66
static void gdm_port_destruct(struct tty_port *port)
{
	struct gdm *gdm = container_of(port, struct gdm, port);

	mutex_lock(&gdm_table_lock);
	gdm_table[gdm->index][gdm->minor] = NULL;
	mutex_unlock(&gdm_table_lock);

	kfree(gdm);
}
67

68 69
static struct tty_port_operations gdm_port_ops = {
	.destruct = gdm_port_destruct,
70 71
};

72
static int gdm_tty_install(struct tty_driver *driver, struct tty_struct *tty)
73
{
74 75
	struct gdm *gdm = NULL;
	int ret;
76
	int i;
77
	int j;
78

79
	j = GDM_TTY_MINOR;
80 81
	for (i = 0; i < TTY_MAX_COUNT; i++) {
		if (!strcmp(tty->driver->driver_name, DRIVER_STRING[i])) {
82
			j = tty->index;
83 84 85 86
			break;
		}
	}

87
	if (j == GDM_TTY_MINOR)
88 89
		return -ENODEV;

90 91
	mutex_lock(&gdm_table_lock);
	gdm = gdm_table[i][j];
92 93
	if (gdm == NULL) {
		mutex_unlock(&gdm_table_lock);
94
		return -ENODEV;
95
	}
96

97
	tty_port_get(&gdm->port);
98

99 100 101
	ret = tty_standard_install(driver, tty);
	if (ret) {
		tty_port_put(&gdm->port);
102
		mutex_unlock(&gdm_table_lock);
103 104
		return ret;
	}
105

106
	tty->driver_data = gdm;
107
	mutex_unlock(&gdm_table_lock);
108 109

	return 0;
110 111
}

112
static int gdm_tty_open(struct tty_struct *tty, struct file *filp)
113
{
114 115 116
	struct gdm *gdm = tty->driver_data;
	return tty_port_open(&gdm->port, tty, filp);
}
117

118 119 120 121 122
static void gdm_tty_cleanup(struct tty_struct *tty)
{
	struct gdm *gdm = tty->driver_data;
	tty_port_put(&gdm->port);
}
123

124 125 126 127 128
static void gdm_tty_hangup(struct tty_struct *tty)
{
	struct gdm *gdm = tty->driver_data;
	tty_port_hangup(&gdm->port);
}
129

130 131 132 133
static void gdm_tty_close(struct tty_struct *tty, struct file *filp)
{
	struct gdm *gdm = tty->driver_data;
	tty_port_close(&gdm->port, tty, filp);
134 135
}

136 137 138 139 140
static int gdm_tty_recv_complete(void *data,
				 int len,
				 int index,
				 struct tty_dev *tty_dev,
				 int complete)
141
{
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 147 148
		return TO_HOST_PORT_CLOSE;
	}

149 150 151 152 153 154 155
	if (data && len) {
		if (tty_buffer_request_room(&gdm->port, len) == len) {
			tty_insert_flip_string(&gdm->port, data, len);
			tty_flip_buffer_push(&gdm->port);
		} else {
			return TO_HOST_BUFFER_REQUEST_FAIL;
		}
156 157 158
	}

	if (complete == RECV_PACKET_PROCESS_COMPLETE)
159
		gdm_tty_recv(gdm, gdm_tty_recv_complete);
160

161
	return 0;
162 163 164 165
}

static void gdm_tty_send_complete(void *arg)
{
166
	struct gdm *gdm = (struct gdm *)arg;
167

168
	if (!GDM_TTY_READY(gdm))
169 170
		return;

171
	tty_port_tty_wakeup(&gdm->port);
172 173 174 175
}

static int gdm_tty_write(struct tty_struct *tty, const unsigned char *buf, int len)
{
176
	struct gdm *gdm = tty->driver_data;
177 178 179 180
	int remain = len;
	int sent_len = 0;
	int sending_len = 0;

181
	if (!GDM_TTY_READY(gdm))
182 183 184 185 186 187 188
		return -ENODEV;

	if (!len)
		return 0;

	while (1) {
		sending_len = remain > MUX_TX_MAX_SIZE ? MUX_TX_MAX_SIZE : remain;
189
		gdm_tty_send(gdm,
190 191
			     (void *)(buf+sent_len),
			     sending_len,
192
			     gdm->index,
193
			     gdm_tty_send_complete,
194
			     gdm
195 196 197 198 199 200 201 202 203 204 205 206
			    );
		sent_len += sending_len;
		remain -= sending_len;
		if (remain <= 0)
			break;
	}

	return len;
}

static int gdm_tty_write_room(struct tty_struct *tty)
{
207
	struct gdm *gdm = tty->driver_data;
208

209
	if (!GDM_TTY_READY(gdm))
210 211 212 213 214
		return -ENODEV;

	return WRITE_SIZE;
}

215
int register_lte_tty_device(struct tty_dev *tty_dev, struct device *device)
216
{
217 218 219
	struct gdm *gdm;
	int i;
	int j;
220

221
	for (i = 0; i < TTY_MAX_COUNT; i++) {
222

223 224 225
		gdm = kmalloc(sizeof(struct gdm), GFP_KERNEL);
		if (!gdm)
			return -ENOMEM;
226

227
		mutex_lock(&gdm_table_lock);
228
		for (j = 0; j < GDM_TTY_MINOR; j++) {
229
			if (!gdm_table[i][j])
230 231 232 233
				break;
		}

		if (j == GDM_TTY_MINOR) {
234
			kfree(gdm);
235 236
			mutex_unlock(&gdm_table_lock);
			return -EINVAL;
237 238
		}

239 240
		gdm_table[i][j] = gdm;
		mutex_unlock(&gdm_table_lock);
241

242
		tty_dev->gdm[i] = gdm;
243
		tty_port_init(&gdm->port);
244

245 246 247 248
		gdm->port.ops = &gdm_port_ops;
		gdm->index = i;
		gdm->minor = j;
		gdm->tty_dev = tty_dev;
249

250
		tty_port_register_device(&gdm->port, gdm_driver[i], gdm->minor, device);
251
	}
252 253

	for (i = 0; i < MAX_ISSUE_NUM; i++)
254
		gdm_tty_recv(gdm, gdm_tty_recv_complete);
255 256 257 258 259 260

	return 0;
}

void unregister_lte_tty_device(struct tty_dev *tty_dev)
{
261 262
	struct gdm *gdm;
	struct tty_struct *tty;
263 264 265
	int i;

	for (i = 0; i < TTY_MAX_COUNT; i++) {
266 267
		gdm = tty_dev->gdm[i];
		if (!gdm)
268 269
			continue;

270
		mutex_lock(&gdm_table_lock);
271
		gdm_table[gdm->index][gdm->minor] = NULL;
272
		mutex_unlock(&gdm_table_lock);
273

274 275 276 277
		tty = tty_port_tty_get(&gdm->port);
		if (tty) {
			tty_vhangup(tty);
			tty_kref_put(tty);
278 279
		}

280
		tty_unregister_device(gdm_driver[i], gdm->minor);
281 282
		tty_port_put(&gdm->port);
	}
283 284 285
}

static const struct tty_operations gdm_tty_ops = {
286 287 288 289 290 291 292
	.install =	gdm_tty_install,
	.open =		gdm_tty_open,
	.close =	gdm_tty_close,
	.cleanup =	gdm_tty_cleanup,
	.hangup =	gdm_tty_hangup,
	.write =	gdm_tty_write,
	.write_room =	gdm_tty_write_room,
293 294 295 296
};

int register_lte_tty_driver(void)
{
297
	struct tty_driver *tty_driver;
298 299 300 301 302
	int i;
	int ret;

	for (i = 0; i < TTY_MAX_COUNT; i++) {
		tty_driver = alloc_tty_driver(GDM_TTY_MINOR);
303
		if (!tty_driver)
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
			return -ENOMEM;

		tty_driver->owner = THIS_MODULE;
		tty_driver->driver_name = DRIVER_STRING[i];
		tty_driver->name = DEVICE_STRING[i];
		tty_driver->major = GDM_TTY_MAJOR;
		tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
		tty_driver->subtype = SERIAL_TYPE_NORMAL;
		tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
		tty_driver->init_termios = tty_std_termios;
		tty_driver->init_termios.c_cflag = B9600 | CS8 | HUPCL | CLOCAL;
		tty_driver->init_termios.c_lflag = ISIG | ICANON | IEXTEN;
		tty_set_operations(tty_driver, &gdm_tty_ops);

		ret = tty_register_driver(tty_driver);
319 320 321 322
		if (ret) {
			put_tty_driver(tty_driver);
			return ret;
		}
323

324
		gdm_driver[i] = tty_driver;
325 326 327 328 329 330 331 332 333 334 335
	}

	return ret;
}

void unregister_lte_tty_driver(void)
{
	struct tty_driver *tty_driver;
	int i;

	for (i = 0; i < TTY_MAX_COUNT; i++) {
336
		tty_driver = gdm_driver[i];
337 338 339 340 341 342
		if (tty_driver) {
			tty_unregister_driver(tty_driver);
			put_tty_driver(tty_driver);
		}
	}
}
343