9 #include <linux/module.h>
10 #include <linux/device.h>
11 #include <linux/types.h>
13 #include <linux/netdevice.h>
14 #include <linux/rtnetlink.h>
15 #include <linux/tty.h>
17 #include <linux/if_arp.h>
29 #define SEND_QUEUE_LOW 10
30 #define SEND_QUEUE_HIGH 100
31 #define CAIF_SENDING 1
32 #define CAIF_FLOW_OFF_SENT 4
33 #define MAX_WRITE_CHUNK 4096
36 #define CAIF_MAX_MTU 4096
45 static bool ser_use_stx =
true;
49 static bool ser_use_fcs =
true;
59 static struct dentry *debugfsdir;
73 #ifdef CONFIG_DEBUG_FS
74 struct dentry *debugfs_tty_dir;
85 static void ldisc_tx_wakeup(
struct tty_struct *tty);
86 #ifdef CONFIG_DEBUG_FS
90 ser->
tty->stopped << 5 |
91 ser->
tty->hw_stopped << 4 |
92 ser->
tty->flow_stopped << 3 |
93 ser->
tty->packet << 2 |
94 ser->
tty->low_latency << 1 |
99 ser->debugfs_tty_dir =
101 if (!IS_ERR(ser->debugfs_tty_dir)) {
103 ser->debugfs_tty_dir,
107 ser->debugfs_tty_dir,
111 ser->debugfs_tty_dir,
115 ser->debugfs_tty_dir,
119 ser->tx_blob.data = ser->tx_data;
120 ser->tx_blob.size = 0;
121 ser->rx_blob.data = ser->rx_data;
122 ser->rx_blob.size = 0;
125 static inline void debugfs_deinit(
struct ser_device *ser)
132 if (size >
sizeof(ser->rx_data))
133 size =
sizeof(ser->rx_data);
134 memcpy(ser->rx_data, data, size);
135 ser->rx_blob.data = ser->rx_data;
136 ser->rx_blob.size =
size;
139 static inline void debugfs_tx(
struct ser_device *ser,
const u8 *data,
int size)
141 if (size >
sizeof(ser->tx_data))
142 size =
sizeof(ser->tx_data);
143 memcpy(ser->tx_data, data, size);
144 ser->tx_blob.data = ser->tx_data;
145 ser->tx_blob.size =
size;
152 static inline void debugfs_deinit(
struct ser_device *ser)
156 static inline void update_tty_status(
struct ser_device *ser)
160 static inline void debugfs_rx(
struct ser_device *ser,
const u8 *data,
int size)
164 static inline void debugfs_tx(
struct ser_device *ser,
const u8 *data,
int size)
170 static void ldisc_receive(
struct tty_struct *tty,
const u8 *data,
192 "Bytes received before initial transmission -"
193 "bytes discarded.\n");
200 skb = netdev_alloc_skb(ser->
dev, count+1);
207 skb_reset_mac_header(skb);
209 debugfs_rx(ser, data, count);
213 ser->
dev->stats.rx_packets++;
216 ++ser->
dev->stats.rx_dropped;
217 update_tty_status(ser);
224 int tty_wr,
len, room;
234 while ((skb = skb_peek(&ser->
head)) !=
NULL) {
241 if (room > ser_write_chunk)
242 room = ser_write_chunk;
248 tty_wr = tty->
ops->write(tty, skb->
data, len);
249 update_tty_status(ser);
252 ldisc_receive(tty, skb->
data,
NULL, len);
254 ser->
dev->stats.tx_packets++;
255 ser->
dev->stats.tx_bytes += tty_wr;
288 ser = netdev_priv(dev);
298 return handle_tx(ser);
302 static void ldisc_tx_wakeup(
struct tty_struct *tty)
331 ser = netdev_priv(dev);
332 ser->
tty = tty_kref_get(tty);
334 debugfs_init(ser, tty);
346 list_add(&ser->
node, &ser_list);
348 netif_stop_queue(dev);
349 update_tty_status(ser);
353 static void ldisc_close(
struct tty_struct *tty)
363 unregister_netdevice(ser->
dev);
377 .close = ldisc_close,
378 .receive_buf = ldisc_receive,
379 .write_wakeup = ldisc_tx_wakeup
382 static int register_ldisc(
void)
388 pr_err(
"cannot register CAIF ldisc=%d err=%d\n",
N_CAIF,
395 .ndo_open = caif_net_open,
396 .ndo_stop = caif_net_close,
397 .ndo_start_xmit = caif_xmit
400 static void caifdev_setup(
struct net_device *dev)
411 skb_queue_head_init(&serdev->
head);
413 serdev->
common.use_frag =
true;
414 serdev->
common.use_stx = ser_use_stx;
415 serdev->
common.use_fcs = ser_use_fcs;
420 static int caif_net_open(
struct net_device *dev)
422 netif_wake_queue(dev);
426 static int caif_net_close(
struct net_device *dev)
428 netif_stop_queue(dev);
432 static int __init caif_ser_init(
void)
436 ret = register_ldisc();
441 static void __exit caif_ser_exit(
void)
450 unregister_netdevice(ser->
dev);