caif_serial.c 10.3 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3
/*
 * Copyright (C) ST-Ericsson AB 2010
4
 * Author:	Sjur Brendeland
5 6
 */

7
#include <linux/hardirq.h>
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include <linux/init.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/types.h>
#include <linux/skbuff.h>
#include <linux/netdevice.h>
#include <linux/rtnetlink.h>
#include <linux/tty.h>
#include <linux/file.h>
#include <linux/if_arp.h>
#include <net/caif/caif_device.h>
#include <net/caif/cfcnfg.h>
#include <linux/err.h>
#include <linux/debugfs.h>

MODULE_LICENSE("GPL");
24
MODULE_AUTHOR("Sjur Brendeland");
25 26 27 28 29 30 31 32 33 34 35 36 37
MODULE_DESCRIPTION("CAIF serial device TTY line discipline");
MODULE_LICENSE("GPL");
MODULE_ALIAS_LDISC(N_CAIF);

#define SEND_QUEUE_LOW 10
#define SEND_QUEUE_HIGH 100
#define CAIF_SENDING	        1 /* Bit 1 = 0x02*/
#define CAIF_FLOW_OFF_SENT	4 /* Bit 4 = 0x10 */
#define MAX_WRITE_CHUNK	     4096
#define ON 1
#define OFF 0
#define CAIF_MAX_MTU 4096

38
static DEFINE_SPINLOCK(ser_lock);
39
static LIST_HEAD(ser_list);
40
static LIST_HEAD(ser_release_list);
41

42
static bool ser_loop;
43
module_param(ser_loop, bool, 0444);
44 45
MODULE_PARM_DESC(ser_loop, "Run in simulated loopback mode.");

46
static bool ser_use_stx = true;
47
module_param(ser_use_stx, bool, 0444);
48 49
MODULE_PARM_DESC(ser_use_stx, "STX enabled or not.");

50
static bool ser_use_fcs = true;
51

52
module_param(ser_use_fcs, bool, 0444);
53 54 55
MODULE_PARM_DESC(ser_use_fcs, "FCS enabled or not.");

static int ser_write_chunk = MAX_WRITE_CHUNK;
56
module_param(ser_write_chunk, int, 0444);
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

MODULE_PARM_DESC(ser_write_chunk, "Maximum size of data written to UART.");

static struct dentry *debugfsdir;

static int caif_net_open(struct net_device *dev);
static int caif_net_close(struct net_device *dev);

struct ser_device {
	struct caif_dev_common common;
	struct list_head node;
	struct net_device *dev;
	struct sk_buff_head head;
	struct tty_struct *tty;
	bool tx_started;
	unsigned long state;
#ifdef CONFIG_DEBUG_FS
	struct dentry *debugfs_tty_dir;
	struct debugfs_blob_wrapper tx_blob;
	struct debugfs_blob_wrapper rx_blob;
	u8 rx_data[128];
	u8 tx_data[128];
	u8 tty_status;

#endif
};

static void caifdev_setup(struct net_device *dev);
static void ldisc_tx_wakeup(struct tty_struct *tty);
#ifdef CONFIG_DEBUG_FS
static inline void update_tty_status(struct ser_device *ser)
{
	ser->tty_status =
		ser->tty->stopped << 5 |
		ser->tty->flow_stopped << 3 |
92
		ser->tty->packet << 2;
93 94 95
}
static inline void debugfs_init(struct ser_device *ser, struct tty_struct *tty)
{
96 97 98 99 100 101 102 103
	ser->debugfs_tty_dir = debugfs_create_dir(tty->name, debugfsdir);

	debugfs_create_blob("last_tx_msg", 0400, ser->debugfs_tty_dir,
			    &ser->tx_blob);

	debugfs_create_blob("last_rx_msg", 0400, ser->debugfs_tty_dir,
			    &ser->rx_blob);

104 105
	debugfs_create_xul("ser_state", 0400, ser->debugfs_tty_dir,
			   &ser->state);
106 107 108 109

	debugfs_create_x8("tty_status", 0400, ser->debugfs_tty_dir,
			  &ser->tty_status);

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
	ser->tx_blob.data = ser->tx_data;
	ser->tx_blob.size = 0;
	ser->rx_blob.data = ser->rx_data;
	ser->rx_blob.size = 0;
}

static inline void debugfs_deinit(struct ser_device *ser)
{
	debugfs_remove_recursive(ser->debugfs_tty_dir);
}

static inline void debugfs_rx(struct ser_device *ser, const u8 *data, int size)
{
	if (size > sizeof(ser->rx_data))
		size = sizeof(ser->rx_data);
	memcpy(ser->rx_data, data, size);
	ser->rx_blob.data = ser->rx_data;
	ser->rx_blob.size = size;
}

static inline void debugfs_tx(struct ser_device *ser, const u8 *data, int size)
{
	if (size > sizeof(ser->tx_data))
		size = sizeof(ser->tx_data);
	memcpy(ser->tx_data, data, size);
	ser->tx_blob.data = ser->tx_data;
	ser->tx_blob.size = size;
}
#else
static inline void debugfs_init(struct ser_device *ser, struct tty_struct *tty)
{
}

static inline void debugfs_deinit(struct ser_device *ser)
{
}

static inline void update_tty_status(struct ser_device *ser)
{
}

static inline void debugfs_rx(struct ser_device *ser, const u8 *data, int size)
{
}

static inline void debugfs_tx(struct ser_device *ser, const u8 *data, int size)
{
}

#endif

161 162
static void ldisc_receive(struct tty_struct *tty, const u8 *data,
			char *flags, int count)
163 164 165 166
{
	struct sk_buff *skb = NULL;
	struct ser_device *ser;
	int ret;
167

168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
	ser = tty->disc_data;

	/*
	 * NOTE: flags may contain information about break or overrun.
	 * This is not yet handled.
	 */


	/*
	 * Workaround for garbage at start of transmission,
	 * only enable if STX handling is not enabled.
	 */
	if (!ser->common.use_stx && !ser->tx_started) {
		dev_info(&ser->dev->dev,
			"Bytes received before initial transmission -"
			"bytes discarded.\n");
		return;
	}

	BUG_ON(ser->dev == NULL);

	/* Get a suitable caif packet and copy in data. */
	skb = netdev_alloc_skb(ser->dev, count+1);
191 192
	if (skb == NULL)
		return;
193
	skb_put_data(skb, data, count);
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212

	skb->protocol = htons(ETH_P_CAIF);
	skb_reset_mac_header(skb);
	debugfs_rx(ser, data, count);
	/* Push received packet up the stack. */
	ret = netif_rx_ni(skb);
	if (!ret) {
		ser->dev->stats.rx_packets++;
		ser->dev->stats.rx_bytes += count;
	} else
		++ser->dev->stats.rx_dropped;
	update_tty_status(ser);
}

static int handle_tx(struct ser_device *ser)
{
	struct tty_struct *tty;
	struct sk_buff *skb;
	int tty_wr, len, room;
213

214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
	tty = ser->tty;
	ser->tx_started = true;

	/* Enter critical section */
	if (test_and_set_bit(CAIF_SENDING, &ser->state))
		return 0;

	/* skb_peek is safe because handle_tx is called after skb_queue_tail */
	while ((skb = skb_peek(&ser->head)) != NULL) {

		/* Make sure you don't write too much */
		len = skb->len;
		room = tty_write_room(tty);
		if (!room)
			break;
		if (room > ser_write_chunk)
			room = ser_write_chunk;
		if (len > room)
			len = room;

		/* Write to tty or loopback */
		if (!ser_loop) {
			tty_wr = tty->ops->write(tty, skb->data, len);
			update_tty_status(ser);
		} else {
			tty_wr = len;
			ldisc_receive(tty, skb->data, NULL, len);
		}
		ser->dev->stats.tx_packets++;
		ser->dev->stats.tx_bytes += tty_wr;

		/* Error on TTY ?! */
		if (tty_wr < 0)
			goto error;
		/* Reduce buffer written, and discard if empty */
		skb_pull(skb, tty_wr);
		if (skb->len == 0) {
			struct sk_buff *tmp = skb_dequeue(&ser->head);
R
Roar Førde 已提交
252
			WARN_ON(tmp != skb);
253
			dev_consume_skb_any(skb);
254 255 256 257 258 259 260 261 262 263 264 265 266 267
		}
	}
	/* Send flow off if queue is empty */
	if (ser->head.qlen <= SEND_QUEUE_LOW &&
		test_and_clear_bit(CAIF_FLOW_OFF_SENT, &ser->state) &&
		ser->common.flowctrl != NULL)
				ser->common.flowctrl(ser->dev, ON);
	clear_bit(CAIF_SENDING, &ser->state);
	return 0;
error:
	clear_bit(CAIF_SENDING, &ser->state);
	return tty_wr;
}

268
static netdev_tx_t caif_xmit(struct sk_buff *skb, struct net_device *dev)
269 270
{
	struct ser_device *ser;
271

272 273 274
	if (WARN_ON(!dev))
		return -EINVAL;

275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
	ser = netdev_priv(dev);

	/* Send flow off once, on high water mark */
	if (ser->head.qlen > SEND_QUEUE_HIGH &&
		!test_and_set_bit(CAIF_FLOW_OFF_SENT, &ser->state) &&
		ser->common.flowctrl != NULL)

		ser->common.flowctrl(ser->dev, OFF);

	skb_queue_tail(&ser->head, skb);
	return handle_tx(ser);
}


static void ldisc_tx_wakeup(struct tty_struct *tty)
{
	struct ser_device *ser;
292

293 294
	ser = tty->disc_data;
	BUG_ON(ser == NULL);
R
Roar Førde 已提交
295
	WARN_ON(ser->tty != tty);
296 297 298 299
	handle_tx(ser);
}


300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
static void ser_release(struct work_struct *work)
{
	struct list_head list;
	struct ser_device *ser, *tmp;

	spin_lock(&ser_lock);
	list_replace_init(&ser_release_list, &list);
	spin_unlock(&ser_lock);

	if (!list_empty(&list)) {
		rtnl_lock();
		list_for_each_entry_safe(ser, tmp, &list, node) {
			dev_close(ser->dev);
			unregister_netdevice(ser->dev);
			debugfs_deinit(ser);
		}
		rtnl_unlock();
	}
}

static DECLARE_WORK(ser_release_work, ser_release);

322 323 324 325 326 327 328
static int ldisc_open(struct tty_struct *tty)
{
	struct ser_device *ser;
	struct net_device *dev;
	char name[64];
	int result;

A
Alan Cox 已提交
329 330 331
	/* No write no play */
	if (tty->ops->write == NULL)
		return -EOPNOTSUPP;
332 333
	if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_TTY_CONFIG))
		return -EPERM;
A
Alan Cox 已提交
334

335 336 337
	/* release devices to avoid name collision */
	ser_release(NULL);

338 339 340
	result = snprintf(name, sizeof(name), "cf%s", tty->name);
	if (result >= IFNAMSIZ)
		return -EINVAL;
341 342
	dev = alloc_netdev(sizeof(*ser), name, NET_NAME_UNKNOWN,
			   caifdev_setup);
A
Alan Cox 已提交
343 344 345
	if (!dev)
		return -ENOMEM;

346
	ser = netdev_priv(dev);
347
	ser->tty = tty_kref_get(tty);
348 349 350 351 352 353 354 355 356 357 358 359 360
	ser->dev = dev;
	debugfs_init(ser, tty);
	tty->receive_room = N_TTY_BUF_SIZE;
	tty->disc_data = ser;
	set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
	rtnl_lock();
	result = register_netdevice(dev);
	if (result) {
		rtnl_unlock();
		free_netdev(dev);
		return -ENODEV;
	}

361
	spin_lock(&ser_lock);
362
	list_add(&ser->node, &ser_list);
363
	spin_unlock(&ser_lock);
364 365 366 367 368 369 370 371 372
	rtnl_unlock();
	netif_stop_queue(dev);
	update_tty_status(ser);
	return 0;
}

static void ldisc_close(struct tty_struct *tty)
{
	struct ser_device *ser = tty->disc_data;
373

374
	tty_kref_put(ser->tty);
375 376 377 378 379

	spin_lock(&ser_lock);
	list_move(&ser->node, &ser_release_list);
	spin_unlock(&ser_lock);
	schedule_work(&ser_release_work);
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
}

/* The line discipline structure. */
static struct tty_ldisc_ops caif_ldisc = {
	.owner =	THIS_MODULE,
	.name =		"n_caif",
	.open =		ldisc_open,
	.close =	ldisc_close,
	.receive_buf =	ldisc_receive,
	.write_wakeup =	ldisc_tx_wakeup
};

static const struct net_device_ops netdev_ops = {
	.ndo_open = caif_net_open,
	.ndo_stop = caif_net_close,
	.ndo_start_xmit = caif_xmit
};

static void caifdev_setup(struct net_device *dev)
{
	struct ser_device *serdev = netdev_priv(dev);
401

402 403 404 405 406
	dev->features = 0;
	dev->netdev_ops = &netdev_ops;
	dev->type = ARPHRD_CAIF;
	dev->flags = IFF_POINTOPOINT | IFF_NOARP;
	dev->mtu = CAIF_MAX_MTU;
407
	dev->priv_flags |= IFF_NO_QUEUE;
408
	dev->needs_free_netdev = true;
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
	skb_queue_head_init(&serdev->head);
	serdev->common.link_select = CAIF_LINK_LOW_LATENCY;
	serdev->common.use_frag = true;
	serdev->common.use_stx = ser_use_stx;
	serdev->common.use_fcs = ser_use_fcs;
	serdev->dev = dev;
}


static int caif_net_open(struct net_device *dev)
{
	netif_wake_queue(dev);
	return 0;
}

static int caif_net_close(struct net_device *dev)
{
	netif_stop_queue(dev);
	return 0;
}

static int __init caif_ser_init(void)
{
	int ret;
433

J
Jiri Slaby 已提交
434 435 436 437
	ret = tty_register_ldisc(N_CAIF, &caif_ldisc);
	if (ret < 0)
		pr_err("cannot register CAIF ldisc=%d err=%d\n", N_CAIF, ret);

438 439 440 441 442 443
	debugfsdir = debugfs_create_dir("caif_serial", NULL);
	return ret;
}

static void __exit caif_ser_exit(void)
{
444 445 446 447 448
	spin_lock(&ser_lock);
	list_splice(&ser_list, &ser_release_list);
	spin_unlock(&ser_lock);
	ser_release(NULL);
	cancel_work_sync(&ser_release_work);
449 450 451 452 453 454
	tty_unregister_ldisc(N_CAIF);
	debugfs_remove_recursive(debugfsdir);
}

module_init(caif_ser_init);
module_exit(caif_ser_exit);