xref: /linux/drivers/net/caif/caif_serial.c (revision a6b7a407865aab9f849dd99a71072b7cd1175116)
19b27105bSSjur Braendeland /*
29b27105bSSjur Braendeland  * Copyright (C) ST-Ericsson AB 2010
39b27105bSSjur Braendeland  * Author:	Sjur Brendeland / sjur.brandeland@stericsson.com
49b27105bSSjur Braendeland  * License terms: GNU General Public License (GPL) version 2
59b27105bSSjur Braendeland  */
69b27105bSSjur Braendeland 
7*a6b7a407SAlexey Dobriyan #include <linux/hardirq.h>
89b27105bSSjur Braendeland #include <linux/init.h>
99b27105bSSjur Braendeland #include <linux/version.h>
109b27105bSSjur Braendeland #include <linux/module.h>
119b27105bSSjur Braendeland #include <linux/device.h>
129b27105bSSjur Braendeland #include <linux/types.h>
139b27105bSSjur Braendeland #include <linux/skbuff.h>
149b27105bSSjur Braendeland #include <linux/netdevice.h>
159b27105bSSjur Braendeland #include <linux/rtnetlink.h>
169b27105bSSjur Braendeland #include <linux/tty.h>
179b27105bSSjur Braendeland #include <linux/file.h>
189b27105bSSjur Braendeland #include <linux/if_arp.h>
199b27105bSSjur Braendeland #include <net/caif/caif_device.h>
209b27105bSSjur Braendeland #include <net/caif/cfcnfg.h>
219b27105bSSjur Braendeland #include <linux/err.h>
229b27105bSSjur Braendeland #include <linux/debugfs.h>
239b27105bSSjur Braendeland 
249b27105bSSjur Braendeland MODULE_LICENSE("GPL");
259b27105bSSjur Braendeland MODULE_AUTHOR("Sjur Brendeland<sjur.brandeland@stericsson.com>");
269b27105bSSjur Braendeland MODULE_DESCRIPTION("CAIF serial device TTY line discipline");
279b27105bSSjur Braendeland MODULE_LICENSE("GPL");
289b27105bSSjur Braendeland MODULE_ALIAS_LDISC(N_CAIF);
299b27105bSSjur Braendeland 
309b27105bSSjur Braendeland #define SEND_QUEUE_LOW 10
319b27105bSSjur Braendeland #define SEND_QUEUE_HIGH 100
329b27105bSSjur Braendeland #define CAIF_SENDING	        1 /* Bit 1 = 0x02*/
339b27105bSSjur Braendeland #define CAIF_FLOW_OFF_SENT	4 /* Bit 4 = 0x10 */
349b27105bSSjur Braendeland #define MAX_WRITE_CHUNK	     4096
359b27105bSSjur Braendeland #define ON 1
369b27105bSSjur Braendeland #define OFF 0
379b27105bSSjur Braendeland #define CAIF_MAX_MTU 4096
389b27105bSSjur Braendeland 
399b27105bSSjur Braendeland /*This list is protected by the rtnl lock. */
409b27105bSSjur Braendeland static LIST_HEAD(ser_list);
419b27105bSSjur Braendeland 
429b27105bSSjur Braendeland static int ser_loop;
439b27105bSSjur Braendeland module_param(ser_loop, bool, S_IRUGO);
449b27105bSSjur Braendeland MODULE_PARM_DESC(ser_loop, "Run in simulated loopback mode.");
459b27105bSSjur Braendeland 
469b27105bSSjur Braendeland static int ser_use_stx = 1;
479b27105bSSjur Braendeland module_param(ser_use_stx, bool, S_IRUGO);
489b27105bSSjur Braendeland MODULE_PARM_DESC(ser_use_stx, "STX enabled or not.");
499b27105bSSjur Braendeland 
509b27105bSSjur Braendeland static int ser_use_fcs = 1;
519b27105bSSjur Braendeland 
529b27105bSSjur Braendeland module_param(ser_use_fcs, bool, S_IRUGO);
539b27105bSSjur Braendeland MODULE_PARM_DESC(ser_use_fcs, "FCS enabled or not.");
549b27105bSSjur Braendeland 
559b27105bSSjur Braendeland static int ser_write_chunk = MAX_WRITE_CHUNK;
569b27105bSSjur Braendeland module_param(ser_write_chunk, int, S_IRUGO);
579b27105bSSjur Braendeland 
589b27105bSSjur Braendeland MODULE_PARM_DESC(ser_write_chunk, "Maximum size of data written to UART.");
599b27105bSSjur Braendeland 
609b27105bSSjur Braendeland static struct dentry *debugfsdir;
619b27105bSSjur Braendeland 
629b27105bSSjur Braendeland static int caif_net_open(struct net_device *dev);
639b27105bSSjur Braendeland static int caif_net_close(struct net_device *dev);
649b27105bSSjur Braendeland 
659b27105bSSjur Braendeland struct ser_device {
669b27105bSSjur Braendeland 	struct caif_dev_common common;
679b27105bSSjur Braendeland 	struct list_head node;
689b27105bSSjur Braendeland 	struct net_device *dev;
699b27105bSSjur Braendeland 	struct sk_buff_head head;
709b27105bSSjur Braendeland 	struct tty_struct *tty;
719b27105bSSjur Braendeland 	bool tx_started;
729b27105bSSjur Braendeland 	unsigned long state;
739b27105bSSjur Braendeland 	char *tty_name;
749b27105bSSjur Braendeland #ifdef CONFIG_DEBUG_FS
759b27105bSSjur Braendeland 	struct dentry *debugfs_tty_dir;
769b27105bSSjur Braendeland 	struct debugfs_blob_wrapper tx_blob;
779b27105bSSjur Braendeland 	struct debugfs_blob_wrapper rx_blob;
789b27105bSSjur Braendeland 	u8 rx_data[128];
799b27105bSSjur Braendeland 	u8 tx_data[128];
809b27105bSSjur Braendeland 	u8 tty_status;
819b27105bSSjur Braendeland 
829b27105bSSjur Braendeland #endif
839b27105bSSjur Braendeland };
849b27105bSSjur Braendeland 
859b27105bSSjur Braendeland static void caifdev_setup(struct net_device *dev);
869b27105bSSjur Braendeland static void ldisc_tx_wakeup(struct tty_struct *tty);
879b27105bSSjur Braendeland #ifdef CONFIG_DEBUG_FS
889b27105bSSjur Braendeland static inline void update_tty_status(struct ser_device *ser)
899b27105bSSjur Braendeland {
909b27105bSSjur Braendeland 	ser->tty_status =
919b27105bSSjur Braendeland 		ser->tty->stopped << 5 |
929b27105bSSjur Braendeland 		ser->tty->hw_stopped << 4 |
939b27105bSSjur Braendeland 		ser->tty->flow_stopped << 3 |
949b27105bSSjur Braendeland 		ser->tty->packet << 2 |
959b27105bSSjur Braendeland 		ser->tty->low_latency << 1 |
969b27105bSSjur Braendeland 		ser->tty->warned;
979b27105bSSjur Braendeland }
989b27105bSSjur Braendeland static inline void debugfs_init(struct ser_device *ser, struct tty_struct *tty)
999b27105bSSjur Braendeland {
1009b27105bSSjur Braendeland 	ser->debugfs_tty_dir =
1019b27105bSSjur Braendeland 			debugfs_create_dir(tty->name, debugfsdir);
1029b27105bSSjur Braendeland 	if (!IS_ERR(ser->debugfs_tty_dir)) {
1039b27105bSSjur Braendeland 		debugfs_create_blob("last_tx_msg", S_IRUSR,
1049b27105bSSjur Braendeland 				ser->debugfs_tty_dir,
1059b27105bSSjur Braendeland 				&ser->tx_blob);
1069b27105bSSjur Braendeland 
1079b27105bSSjur Braendeland 		debugfs_create_blob("last_rx_msg", S_IRUSR,
1089b27105bSSjur Braendeland 				ser->debugfs_tty_dir,
1099b27105bSSjur Braendeland 				&ser->rx_blob);
1109b27105bSSjur Braendeland 
1119b27105bSSjur Braendeland 		debugfs_create_x32("ser_state", S_IRUSR,
1129b27105bSSjur Braendeland 				ser->debugfs_tty_dir,
1139b27105bSSjur Braendeland 				(u32 *)&ser->state);
1149b27105bSSjur Braendeland 
1159b27105bSSjur Braendeland 		debugfs_create_x8("tty_status", S_IRUSR,
1169b27105bSSjur Braendeland 				ser->debugfs_tty_dir,
1179b27105bSSjur Braendeland 				&ser->tty_status);
1189b27105bSSjur Braendeland 
1199b27105bSSjur Braendeland 	}
1209b27105bSSjur Braendeland 	ser->tx_blob.data = ser->tx_data;
1219b27105bSSjur Braendeland 	ser->tx_blob.size = 0;
1229b27105bSSjur Braendeland 	ser->rx_blob.data = ser->rx_data;
1239b27105bSSjur Braendeland 	ser->rx_blob.size = 0;
1249b27105bSSjur Braendeland }
1259b27105bSSjur Braendeland 
1269b27105bSSjur Braendeland static inline void debugfs_deinit(struct ser_device *ser)
1279b27105bSSjur Braendeland {
1289b27105bSSjur Braendeland 	debugfs_remove_recursive(ser->debugfs_tty_dir);
1299b27105bSSjur Braendeland }
1309b27105bSSjur Braendeland 
1319b27105bSSjur Braendeland static inline void debugfs_rx(struct ser_device *ser, const u8 *data, int size)
1329b27105bSSjur Braendeland {
1339b27105bSSjur Braendeland 	if (size > sizeof(ser->rx_data))
1349b27105bSSjur Braendeland 		size = sizeof(ser->rx_data);
1359b27105bSSjur Braendeland 	memcpy(ser->rx_data, data, size);
1369b27105bSSjur Braendeland 	ser->rx_blob.data = ser->rx_data;
1379b27105bSSjur Braendeland 	ser->rx_blob.size = size;
1389b27105bSSjur Braendeland }
1399b27105bSSjur Braendeland 
1409b27105bSSjur Braendeland static inline void debugfs_tx(struct ser_device *ser, const u8 *data, int size)
1419b27105bSSjur Braendeland {
1429b27105bSSjur Braendeland 	if (size > sizeof(ser->tx_data))
1439b27105bSSjur Braendeland 		size = sizeof(ser->tx_data);
1449b27105bSSjur Braendeland 	memcpy(ser->tx_data, data, size);
1459b27105bSSjur Braendeland 	ser->tx_blob.data = ser->tx_data;
1469b27105bSSjur Braendeland 	ser->tx_blob.size = size;
1479b27105bSSjur Braendeland }
1489b27105bSSjur Braendeland #else
1499b27105bSSjur Braendeland static inline void debugfs_init(struct ser_device *ser, struct tty_struct *tty)
1509b27105bSSjur Braendeland {
1519b27105bSSjur Braendeland }
1529b27105bSSjur Braendeland 
1539b27105bSSjur Braendeland static inline void debugfs_deinit(struct ser_device *ser)
1549b27105bSSjur Braendeland {
1559b27105bSSjur Braendeland }
1569b27105bSSjur Braendeland 
1579b27105bSSjur Braendeland static inline void update_tty_status(struct ser_device *ser)
1589b27105bSSjur Braendeland {
1599b27105bSSjur Braendeland }
1609b27105bSSjur Braendeland 
1619b27105bSSjur Braendeland static inline void debugfs_rx(struct ser_device *ser, const u8 *data, int size)
1629b27105bSSjur Braendeland {
1639b27105bSSjur Braendeland }
1649b27105bSSjur Braendeland 
1659b27105bSSjur Braendeland static inline void debugfs_tx(struct ser_device *ser, const u8 *data, int size)
1669b27105bSSjur Braendeland {
1679b27105bSSjur Braendeland }
1689b27105bSSjur Braendeland 
1699b27105bSSjur Braendeland #endif
1709b27105bSSjur Braendeland 
17155db4c64SLinus Torvalds static void ldisc_receive(struct tty_struct *tty, const u8 *data,
17255db4c64SLinus Torvalds 			char *flags, int count)
1739b27105bSSjur Braendeland {
1749b27105bSSjur Braendeland 	struct sk_buff *skb = NULL;
1759b27105bSSjur Braendeland 	struct ser_device *ser;
1769b27105bSSjur Braendeland 	int ret;
1779b27105bSSjur Braendeland 	u8 *p;
178c1f8fc57SDan Carpenter 
1799b27105bSSjur Braendeland 	ser = tty->disc_data;
1809b27105bSSjur Braendeland 
1819b27105bSSjur Braendeland 	/*
1829b27105bSSjur Braendeland 	 * NOTE: flags may contain information about break or overrun.
1839b27105bSSjur Braendeland 	 * This is not yet handled.
1849b27105bSSjur Braendeland 	 */
1859b27105bSSjur Braendeland 
1869b27105bSSjur Braendeland 
1879b27105bSSjur Braendeland 	/*
1889b27105bSSjur Braendeland 	 * Workaround for garbage at start of transmission,
1899b27105bSSjur Braendeland 	 * only enable if STX handling is not enabled.
1909b27105bSSjur Braendeland 	 */
1919b27105bSSjur Braendeland 	if (!ser->common.use_stx && !ser->tx_started) {
1929b27105bSSjur Braendeland 		dev_info(&ser->dev->dev,
1939b27105bSSjur Braendeland 			"Bytes received before initial transmission -"
1949b27105bSSjur Braendeland 			"bytes discarded.\n");
1959b27105bSSjur Braendeland 		return;
1969b27105bSSjur Braendeland 	}
1979b27105bSSjur Braendeland 
1989b27105bSSjur Braendeland 	BUG_ON(ser->dev == NULL);
1999b27105bSSjur Braendeland 
2009b27105bSSjur Braendeland 	/* Get a suitable caif packet and copy in data. */
2019b27105bSSjur Braendeland 	skb = netdev_alloc_skb(ser->dev, count+1);
202d3f744e0SSjur Braendeland 	if (skb == NULL)
203d3f744e0SSjur Braendeland 		return;
2049b27105bSSjur Braendeland 	p = skb_put(skb, count);
2059b27105bSSjur Braendeland 	memcpy(p, data, count);
2069b27105bSSjur Braendeland 
2079b27105bSSjur Braendeland 	skb->protocol = htons(ETH_P_CAIF);
2089b27105bSSjur Braendeland 	skb_reset_mac_header(skb);
2099b27105bSSjur Braendeland 	skb->dev = ser->dev;
2109b27105bSSjur Braendeland 	debugfs_rx(ser, data, count);
2119b27105bSSjur Braendeland 	/* Push received packet up the stack. */
2129b27105bSSjur Braendeland 	ret = netif_rx_ni(skb);
2139b27105bSSjur Braendeland 	if (!ret) {
2149b27105bSSjur Braendeland 		ser->dev->stats.rx_packets++;
2159b27105bSSjur Braendeland 		ser->dev->stats.rx_bytes += count;
2169b27105bSSjur Braendeland 	} else
2179b27105bSSjur Braendeland 		++ser->dev->stats.rx_dropped;
2189b27105bSSjur Braendeland 	update_tty_status(ser);
2199b27105bSSjur Braendeland }
2209b27105bSSjur Braendeland 
2219b27105bSSjur Braendeland static int handle_tx(struct ser_device *ser)
2229b27105bSSjur Braendeland {
2239b27105bSSjur Braendeland 	struct tty_struct *tty;
2249b27105bSSjur Braendeland 	struct sk_buff *skb;
2259b27105bSSjur Braendeland 	int tty_wr, len, room;
226c1f8fc57SDan Carpenter 
2279b27105bSSjur Braendeland 	tty = ser->tty;
2289b27105bSSjur Braendeland 	ser->tx_started = true;
2299b27105bSSjur Braendeland 
2309b27105bSSjur Braendeland 	/* Enter critical section */
2319b27105bSSjur Braendeland 	if (test_and_set_bit(CAIF_SENDING, &ser->state))
2329b27105bSSjur Braendeland 		return 0;
2339b27105bSSjur Braendeland 
2349b27105bSSjur Braendeland 	/* skb_peek is safe because handle_tx is called after skb_queue_tail */
2359b27105bSSjur Braendeland 	while ((skb = skb_peek(&ser->head)) != NULL) {
2369b27105bSSjur Braendeland 
2379b27105bSSjur Braendeland 		/* Make sure you don't write too much */
2389b27105bSSjur Braendeland 		len = skb->len;
2399b27105bSSjur Braendeland 		room = tty_write_room(tty);
2409b27105bSSjur Braendeland 		if (!room)
2419b27105bSSjur Braendeland 			break;
2429b27105bSSjur Braendeland 		if (room > ser_write_chunk)
2439b27105bSSjur Braendeland 			room = ser_write_chunk;
2449b27105bSSjur Braendeland 		if (len > room)
2459b27105bSSjur Braendeland 			len = room;
2469b27105bSSjur Braendeland 
2479b27105bSSjur Braendeland 		/* Write to tty or loopback */
2489b27105bSSjur Braendeland 		if (!ser_loop) {
2499b27105bSSjur Braendeland 			tty_wr = tty->ops->write(tty, skb->data, len);
2509b27105bSSjur Braendeland 			update_tty_status(ser);
2519b27105bSSjur Braendeland 		} else {
2529b27105bSSjur Braendeland 			tty_wr = len;
2539b27105bSSjur Braendeland 			ldisc_receive(tty, skb->data, NULL, len);
2549b27105bSSjur Braendeland 		}
2559b27105bSSjur Braendeland 		ser->dev->stats.tx_packets++;
2569b27105bSSjur Braendeland 		ser->dev->stats.tx_bytes += tty_wr;
2579b27105bSSjur Braendeland 
2589b27105bSSjur Braendeland 		/* Error on TTY ?! */
2599b27105bSSjur Braendeland 		if (tty_wr < 0)
2609b27105bSSjur Braendeland 			goto error;
2619b27105bSSjur Braendeland 		/* Reduce buffer written, and discard if empty */
2629b27105bSSjur Braendeland 		skb_pull(skb, tty_wr);
2639b27105bSSjur Braendeland 		if (skb->len == 0) {
2649b27105bSSjur Braendeland 			struct sk_buff *tmp = skb_dequeue(&ser->head);
2659b27105bSSjur Braendeland 			BUG_ON(tmp != skb);
2669b27105bSSjur Braendeland 			if (in_interrupt())
2679b27105bSSjur Braendeland 				dev_kfree_skb_irq(skb);
2689b27105bSSjur Braendeland 			else
2699b27105bSSjur Braendeland 				kfree_skb(skb);
2709b27105bSSjur Braendeland 		}
2719b27105bSSjur Braendeland 	}
2729b27105bSSjur Braendeland 	/* Send flow off if queue is empty */
2739b27105bSSjur Braendeland 	if (ser->head.qlen <= SEND_QUEUE_LOW &&
2749b27105bSSjur Braendeland 		test_and_clear_bit(CAIF_FLOW_OFF_SENT, &ser->state) &&
2759b27105bSSjur Braendeland 		ser->common.flowctrl != NULL)
2769b27105bSSjur Braendeland 				ser->common.flowctrl(ser->dev, ON);
2779b27105bSSjur Braendeland 	clear_bit(CAIF_SENDING, &ser->state);
2789b27105bSSjur Braendeland 	return 0;
2799b27105bSSjur Braendeland error:
2809b27105bSSjur Braendeland 	clear_bit(CAIF_SENDING, &ser->state);
2819b27105bSSjur Braendeland 	return tty_wr;
2829b27105bSSjur Braendeland }
2839b27105bSSjur Braendeland 
2849b27105bSSjur Braendeland static int caif_xmit(struct sk_buff *skb, struct net_device *dev)
2859b27105bSSjur Braendeland {
2869b27105bSSjur Braendeland 	struct ser_device *ser;
287c1f8fc57SDan Carpenter 
2889b27105bSSjur Braendeland 	BUG_ON(dev == NULL);
2899b27105bSSjur Braendeland 	ser = netdev_priv(dev);
2909b27105bSSjur Braendeland 
2919b27105bSSjur Braendeland 	/* Send flow off once, on high water mark */
2929b27105bSSjur Braendeland 	if (ser->head.qlen > SEND_QUEUE_HIGH &&
2939b27105bSSjur Braendeland 		!test_and_set_bit(CAIF_FLOW_OFF_SENT, &ser->state) &&
2949b27105bSSjur Braendeland 		ser->common.flowctrl != NULL)
2959b27105bSSjur Braendeland 
2969b27105bSSjur Braendeland 		ser->common.flowctrl(ser->dev, OFF);
2979b27105bSSjur Braendeland 
2989b27105bSSjur Braendeland 	skb_queue_tail(&ser->head, skb);
2999b27105bSSjur Braendeland 	return handle_tx(ser);
3009b27105bSSjur Braendeland }
3019b27105bSSjur Braendeland 
3029b27105bSSjur Braendeland 
3039b27105bSSjur Braendeland static void ldisc_tx_wakeup(struct tty_struct *tty)
3049b27105bSSjur Braendeland {
3059b27105bSSjur Braendeland 	struct ser_device *ser;
306c1f8fc57SDan Carpenter 
3079b27105bSSjur Braendeland 	ser = tty->disc_data;
3089b27105bSSjur Braendeland 	BUG_ON(ser == NULL);
3099b27105bSSjur Braendeland 	BUG_ON(ser->tty != tty);
3109b27105bSSjur Braendeland 	handle_tx(ser);
3119b27105bSSjur Braendeland }
3129b27105bSSjur Braendeland 
3139b27105bSSjur Braendeland 
3149b27105bSSjur Braendeland static int ldisc_open(struct tty_struct *tty)
3159b27105bSSjur Braendeland {
3169b27105bSSjur Braendeland 	struct ser_device *ser;
3179b27105bSSjur Braendeland 	struct net_device *dev;
3189b27105bSSjur Braendeland 	char name[64];
3199b27105bSSjur Braendeland 	int result;
3209b27105bSSjur Braendeland 
321c93f0940SAlan Cox 	/* No write no play */
322c93f0940SAlan Cox 	if (tty->ops->write == NULL)
323c93f0940SAlan Cox 		return -EOPNOTSUPP;
324d3f744e0SSjur Braendeland 	if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_TTY_CONFIG))
325d3f744e0SSjur Braendeland 		return -EPERM;
326c93f0940SAlan Cox 
3279b27105bSSjur Braendeland 	sprintf(name, "cf%s", tty->name);
3289b27105bSSjur Braendeland 	dev = alloc_netdev(sizeof(*ser), name, caifdev_setup);
3299b27105bSSjur Braendeland 	ser = netdev_priv(dev);
330e31d5a05SAlan Cox 	ser->tty = tty_kref_get(tty);
3319b27105bSSjur Braendeland 	ser->dev = dev;
3329b27105bSSjur Braendeland 	debugfs_init(ser, tty);
3339b27105bSSjur Braendeland 	tty->receive_room = N_TTY_BUF_SIZE;
3349b27105bSSjur Braendeland 	tty->disc_data = ser;
3359b27105bSSjur Braendeland 	set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
3369b27105bSSjur Braendeland 	rtnl_lock();
3379b27105bSSjur Braendeland 	result = register_netdevice(dev);
3389b27105bSSjur Braendeland 	if (result) {
3399b27105bSSjur Braendeland 		rtnl_unlock();
3409b27105bSSjur Braendeland 		free_netdev(dev);
3419b27105bSSjur Braendeland 		return -ENODEV;
3429b27105bSSjur Braendeland 	}
3439b27105bSSjur Braendeland 
3449b27105bSSjur Braendeland 	list_add(&ser->node, &ser_list);
3459b27105bSSjur Braendeland 	rtnl_unlock();
3469b27105bSSjur Braendeland 	netif_stop_queue(dev);
3479b27105bSSjur Braendeland 	update_tty_status(ser);
3489b27105bSSjur Braendeland 	return 0;
3499b27105bSSjur Braendeland }
3509b27105bSSjur Braendeland 
3519b27105bSSjur Braendeland static void ldisc_close(struct tty_struct *tty)
3529b27105bSSjur Braendeland {
3539b27105bSSjur Braendeland 	struct ser_device *ser = tty->disc_data;
3549b27105bSSjur Braendeland 	/* Remove may be called inside or outside of rtnl_lock */
3559b27105bSSjur Braendeland 	int islocked = rtnl_is_locked();
356c1f8fc57SDan Carpenter 
3579b27105bSSjur Braendeland 	if (!islocked)
3589b27105bSSjur Braendeland 		rtnl_lock();
3599b27105bSSjur Braendeland 	/* device is freed automagically by net-sysfs */
3609b27105bSSjur Braendeland 	dev_close(ser->dev);
3619b27105bSSjur Braendeland 	unregister_netdevice(ser->dev);
3629b27105bSSjur Braendeland 	list_del(&ser->node);
3639b27105bSSjur Braendeland 	debugfs_deinit(ser);
364e31d5a05SAlan Cox 	tty_kref_put(ser->tty);
3659b27105bSSjur Braendeland 	if (!islocked)
3669b27105bSSjur Braendeland 		rtnl_unlock();
3679b27105bSSjur Braendeland }
3689b27105bSSjur Braendeland 
3699b27105bSSjur Braendeland /* The line discipline structure. */
3709b27105bSSjur Braendeland static struct tty_ldisc_ops caif_ldisc = {
3719b27105bSSjur Braendeland 	.owner =	THIS_MODULE,
3729b27105bSSjur Braendeland 	.magic =	TTY_LDISC_MAGIC,
3739b27105bSSjur Braendeland 	.name =		"n_caif",
3749b27105bSSjur Braendeland 	.open =		ldisc_open,
3759b27105bSSjur Braendeland 	.close =	ldisc_close,
3769b27105bSSjur Braendeland 	.receive_buf =	ldisc_receive,
3779b27105bSSjur Braendeland 	.write_wakeup =	ldisc_tx_wakeup
3789b27105bSSjur Braendeland };
3799b27105bSSjur Braendeland 
3809b27105bSSjur Braendeland static int register_ldisc(void)
3819b27105bSSjur Braendeland {
3829b27105bSSjur Braendeland 	int result;
383c1f8fc57SDan Carpenter 
3849b27105bSSjur Braendeland 	result = tty_register_ldisc(N_CAIF, &caif_ldisc);
3859b27105bSSjur Braendeland 	if (result < 0) {
3869b27105bSSjur Braendeland 		pr_err("cannot register CAIF ldisc=%d err=%d\n", N_CAIF,
3879b27105bSSjur Braendeland 			result);
3889b27105bSSjur Braendeland 		return result;
3899b27105bSSjur Braendeland 	}
3909b27105bSSjur Braendeland 	return result;
3919b27105bSSjur Braendeland }
3929b27105bSSjur Braendeland static const struct net_device_ops netdev_ops = {
3939b27105bSSjur Braendeland 	.ndo_open = caif_net_open,
3949b27105bSSjur Braendeland 	.ndo_stop = caif_net_close,
3959b27105bSSjur Braendeland 	.ndo_start_xmit = caif_xmit
3969b27105bSSjur Braendeland };
3979b27105bSSjur Braendeland 
3989b27105bSSjur Braendeland static void caifdev_setup(struct net_device *dev)
3999b27105bSSjur Braendeland {
4009b27105bSSjur Braendeland 	struct ser_device *serdev = netdev_priv(dev);
401c1f8fc57SDan Carpenter 
4029b27105bSSjur Braendeland 	dev->features = 0;
4039b27105bSSjur Braendeland 	dev->netdev_ops = &netdev_ops;
4049b27105bSSjur Braendeland 	dev->type = ARPHRD_CAIF;
4059b27105bSSjur Braendeland 	dev->flags = IFF_POINTOPOINT | IFF_NOARP;
4069b27105bSSjur Braendeland 	dev->mtu = CAIF_MAX_MTU;
4079b27105bSSjur Braendeland 	dev->tx_queue_len = 0;
4089b27105bSSjur Braendeland 	dev->destructor = free_netdev;
4099b27105bSSjur Braendeland 	skb_queue_head_init(&serdev->head);
4109b27105bSSjur Braendeland 	serdev->common.link_select = CAIF_LINK_LOW_LATENCY;
4119b27105bSSjur Braendeland 	serdev->common.use_frag = true;
4129b27105bSSjur Braendeland 	serdev->common.use_stx = ser_use_stx;
4139b27105bSSjur Braendeland 	serdev->common.use_fcs = ser_use_fcs;
4149b27105bSSjur Braendeland 	serdev->dev = dev;
4159b27105bSSjur Braendeland }
4169b27105bSSjur Braendeland 
4179b27105bSSjur Braendeland 
4189b27105bSSjur Braendeland static int caif_net_open(struct net_device *dev)
4199b27105bSSjur Braendeland {
4209b27105bSSjur Braendeland 	netif_wake_queue(dev);
4219b27105bSSjur Braendeland 	return 0;
4229b27105bSSjur Braendeland }
4239b27105bSSjur Braendeland 
4249b27105bSSjur Braendeland static int caif_net_close(struct net_device *dev)
4259b27105bSSjur Braendeland {
4269b27105bSSjur Braendeland 	netif_stop_queue(dev);
4279b27105bSSjur Braendeland 	return 0;
4289b27105bSSjur Braendeland }
4299b27105bSSjur Braendeland 
4309b27105bSSjur Braendeland static int __init caif_ser_init(void)
4319b27105bSSjur Braendeland {
4329b27105bSSjur Braendeland 	int ret;
433c1f8fc57SDan Carpenter 
4349b27105bSSjur Braendeland 	ret = register_ldisc();
4359b27105bSSjur Braendeland 	debugfsdir = debugfs_create_dir("caif_serial", NULL);
4369b27105bSSjur Braendeland 	return ret;
4379b27105bSSjur Braendeland }
4389b27105bSSjur Braendeland 
4399b27105bSSjur Braendeland static void __exit caif_ser_exit(void)
4409b27105bSSjur Braendeland {
4419b27105bSSjur Braendeland 	struct ser_device *ser = NULL;
4429b27105bSSjur Braendeland 	struct list_head *node;
4439b27105bSSjur Braendeland 	struct list_head *_tmp;
444c1f8fc57SDan Carpenter 
4459b27105bSSjur Braendeland 	list_for_each_safe(node, _tmp, &ser_list) {
4469b27105bSSjur Braendeland 		ser = list_entry(node, struct ser_device, node);
4479b27105bSSjur Braendeland 		dev_close(ser->dev);
4489b27105bSSjur Braendeland 		unregister_netdevice(ser->dev);
4499b27105bSSjur Braendeland 		list_del(node);
4509b27105bSSjur Braendeland 	}
4519b27105bSSjur Braendeland 	tty_unregister_ldisc(N_CAIF);
4529b27105bSSjur Braendeland 	debugfs_remove_recursive(debugfsdir);
4539b27105bSSjur Braendeland }
4549b27105bSSjur Braendeland 
4559b27105bSSjur Braendeland module_init(caif_ser_init);
4569b27105bSSjur Braendeland module_exit(caif_ser_exit);
457