1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) ST-Ericsson AB 2010
4 * Author: Sjur Brendeland
7 #include <linux/hardirq.h>
8 #include <linux/init.h>
9 #include <linux/module.h>
10 #include <linux/device.h>
11 #include <linux/types.h>
12 #include <linux/skbuff.h>
13 #include <linux/netdevice.h>
14 #include <linux/rtnetlink.h>
15 #include <linux/tty.h>
16 #include <linux/file.h>
17 #include <linux/if_arp.h>
18 #include <net/caif/caif_device.h>
19 #include <net/caif/cfcnfg.h>
20 #include <linux/err.h>
21 #include <linux/debugfs.h>
23 MODULE_LICENSE("GPL");
24 MODULE_AUTHOR("Sjur Brendeland");
25 MODULE_DESCRIPTION("CAIF serial device TTY line discipline");
26 MODULE_LICENSE("GPL");
27 MODULE_ALIAS_LDISC(N_CAIF
);
29 #define SEND_QUEUE_LOW 10
30 #define SEND_QUEUE_HIGH 100
31 #define CAIF_SENDING 1 /* Bit 1 = 0x02*/
32 #define CAIF_FLOW_OFF_SENT 4 /* Bit 4 = 0x10 */
33 #define MAX_WRITE_CHUNK 4096
36 #define CAIF_MAX_MTU 4096
38 static DEFINE_SPINLOCK(ser_lock
);
39 static LIST_HEAD(ser_list
);
40 static LIST_HEAD(ser_release_list
);
43 module_param(ser_loop
, bool, 0444);
44 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 MODULE_PARM_DESC(ser_use_stx
, "STX enabled or not.");
50 static bool ser_use_fcs
= true;
52 module_param(ser_use_fcs
, bool, 0444);
53 MODULE_PARM_DESC(ser_use_fcs
, "FCS enabled or not.");
55 static int ser_write_chunk
= MAX_WRITE_CHUNK
;
56 module_param(ser_write_chunk
, int, 0444);
58 MODULE_PARM_DESC(ser_write_chunk
, "Maximum size of data written to UART.");
60 static struct dentry
*debugfsdir
;
62 static int caif_net_open(struct net_device
*dev
);
63 static int caif_net_close(struct net_device
*dev
);
66 struct caif_dev_common common
;
67 struct list_head node
;
68 struct net_device
*dev
;
69 struct sk_buff_head head
;
70 struct tty_struct
*tty
;
73 #ifdef CONFIG_DEBUG_FS
74 struct dentry
*debugfs_tty_dir
;
75 struct debugfs_blob_wrapper tx_blob
;
76 struct debugfs_blob_wrapper rx_blob
;
84 static void caifdev_setup(struct net_device
*dev
);
85 static void ldisc_tx_wakeup(struct tty_struct
*tty
);
86 #ifdef CONFIG_DEBUG_FS
87 static inline void update_tty_status(struct ser_device
*ser
)
90 ser
->tty
->stopped
<< 5 |
91 ser
->tty
->flow_stopped
<< 3 |
92 ser
->tty
->packet
<< 2 |
93 ser
->tty
->port
->low_latency
<< 1;
95 static inline void debugfs_init(struct ser_device
*ser
, struct tty_struct
*tty
)
97 ser
->debugfs_tty_dir
= debugfs_create_dir(tty
->name
, debugfsdir
);
99 debugfs_create_blob("last_tx_msg", 0400, ser
->debugfs_tty_dir
,
102 debugfs_create_blob("last_rx_msg", 0400, ser
->debugfs_tty_dir
,
105 debugfs_create_xul("ser_state", 0400, ser
->debugfs_tty_dir
,
108 debugfs_create_x8("tty_status", 0400, ser
->debugfs_tty_dir
,
111 ser
->tx_blob
.data
= ser
->tx_data
;
112 ser
->tx_blob
.size
= 0;
113 ser
->rx_blob
.data
= ser
->rx_data
;
114 ser
->rx_blob
.size
= 0;
117 static inline void debugfs_deinit(struct ser_device
*ser
)
119 debugfs_remove_recursive(ser
->debugfs_tty_dir
);
122 static inline void debugfs_rx(struct ser_device
*ser
, const u8
*data
, int size
)
124 if (size
> sizeof(ser
->rx_data
))
125 size
= sizeof(ser
->rx_data
);
126 memcpy(ser
->rx_data
, data
, size
);
127 ser
->rx_blob
.data
= ser
->rx_data
;
128 ser
->rx_blob
.size
= size
;
131 static inline void debugfs_tx(struct ser_device
*ser
, const u8
*data
, int size
)
133 if (size
> sizeof(ser
->tx_data
))
134 size
= sizeof(ser
->tx_data
);
135 memcpy(ser
->tx_data
, data
, size
);
136 ser
->tx_blob
.data
= ser
->tx_data
;
137 ser
->tx_blob
.size
= size
;
140 static inline void debugfs_init(struct ser_device
*ser
, struct tty_struct
*tty
)
144 static inline void debugfs_deinit(struct ser_device
*ser
)
148 static inline void update_tty_status(struct ser_device
*ser
)
152 static inline void debugfs_rx(struct ser_device
*ser
, const u8
*data
, int size
)
156 static inline void debugfs_tx(struct ser_device
*ser
, const u8
*data
, int size
)
162 static void ldisc_receive(struct tty_struct
*tty
, const u8
*data
,
163 char *flags
, int count
)
165 struct sk_buff
*skb
= NULL
;
166 struct ser_device
*ser
;
169 ser
= tty
->disc_data
;
172 * NOTE: flags may contain information about break or overrun.
173 * This is not yet handled.
178 * Workaround for garbage at start of transmission,
179 * only enable if STX handling is not enabled.
181 if (!ser
->common
.use_stx
&& !ser
->tx_started
) {
182 dev_info(&ser
->dev
->dev
,
183 "Bytes received before initial transmission -"
184 "bytes discarded.\n");
188 BUG_ON(ser
->dev
== NULL
);
190 /* Get a suitable caif packet and copy in data. */
191 skb
= netdev_alloc_skb(ser
->dev
, count
+1);
194 skb_put_data(skb
, data
, count
);
196 skb
->protocol
= htons(ETH_P_CAIF
);
197 skb_reset_mac_header(skb
);
198 debugfs_rx(ser
, data
, count
);
199 /* Push received packet up the stack. */
200 ret
= netif_rx_ni(skb
);
202 ser
->dev
->stats
.rx_packets
++;
203 ser
->dev
->stats
.rx_bytes
+= count
;
205 ++ser
->dev
->stats
.rx_dropped
;
206 update_tty_status(ser
);
209 static int handle_tx(struct ser_device
*ser
)
211 struct tty_struct
*tty
;
213 int tty_wr
, len
, room
;
216 ser
->tx_started
= true;
218 /* Enter critical section */
219 if (test_and_set_bit(CAIF_SENDING
, &ser
->state
))
222 /* skb_peek is safe because handle_tx is called after skb_queue_tail */
223 while ((skb
= skb_peek(&ser
->head
)) != NULL
) {
225 /* Make sure you don't write too much */
227 room
= tty_write_room(tty
);
230 if (room
> ser_write_chunk
)
231 room
= ser_write_chunk
;
235 /* Write to tty or loopback */
237 tty_wr
= tty
->ops
->write(tty
, skb
->data
, len
);
238 update_tty_status(ser
);
241 ldisc_receive(tty
, skb
->data
, NULL
, len
);
243 ser
->dev
->stats
.tx_packets
++;
244 ser
->dev
->stats
.tx_bytes
+= tty_wr
;
246 /* Error on TTY ?! */
249 /* Reduce buffer written, and discard if empty */
250 skb_pull(skb
, tty_wr
);
252 struct sk_buff
*tmp
= skb_dequeue(&ser
->head
);
254 dev_consume_skb_any(skb
);
257 /* Send flow off if queue is empty */
258 if (ser
->head
.qlen
<= SEND_QUEUE_LOW
&&
259 test_and_clear_bit(CAIF_FLOW_OFF_SENT
, &ser
->state
) &&
260 ser
->common
.flowctrl
!= NULL
)
261 ser
->common
.flowctrl(ser
->dev
, ON
);
262 clear_bit(CAIF_SENDING
, &ser
->state
);
265 clear_bit(CAIF_SENDING
, &ser
->state
);
269 static int caif_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
271 struct ser_device
*ser
;
276 ser
= netdev_priv(dev
);
278 /* Send flow off once, on high water mark */
279 if (ser
->head
.qlen
> SEND_QUEUE_HIGH
&&
280 !test_and_set_bit(CAIF_FLOW_OFF_SENT
, &ser
->state
) &&
281 ser
->common
.flowctrl
!= NULL
)
283 ser
->common
.flowctrl(ser
->dev
, OFF
);
285 skb_queue_tail(&ser
->head
, skb
);
286 return handle_tx(ser
);
290 static void ldisc_tx_wakeup(struct tty_struct
*tty
)
292 struct ser_device
*ser
;
294 ser
= tty
->disc_data
;
296 WARN_ON(ser
->tty
!= tty
);
301 static void ser_release(struct work_struct
*work
)
303 struct list_head list
;
304 struct ser_device
*ser
, *tmp
;
306 spin_lock(&ser_lock
);
307 list_replace_init(&ser_release_list
, &list
);
308 spin_unlock(&ser_lock
);
310 if (!list_empty(&list
)) {
312 list_for_each_entry_safe(ser
, tmp
, &list
, node
) {
314 unregister_netdevice(ser
->dev
);
321 static DECLARE_WORK(ser_release_work
, ser_release
);
323 static int ldisc_open(struct tty_struct
*tty
)
325 struct ser_device
*ser
;
326 struct net_device
*dev
;
330 /* No write no play */
331 if (tty
->ops
->write
== NULL
)
333 if (!capable(CAP_SYS_ADMIN
) && !capable(CAP_SYS_TTY_CONFIG
))
336 /* release devices to avoid name collision */
339 result
= snprintf(name
, sizeof(name
), "cf%s", tty
->name
);
340 if (result
>= IFNAMSIZ
)
342 dev
= alloc_netdev(sizeof(*ser
), name
, NET_NAME_UNKNOWN
,
347 ser
= netdev_priv(dev
);
348 ser
->tty
= tty_kref_get(tty
);
350 debugfs_init(ser
, tty
);
351 tty
->receive_room
= N_TTY_BUF_SIZE
;
352 tty
->disc_data
= ser
;
353 set_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
);
355 result
= register_netdevice(dev
);
362 spin_lock(&ser_lock
);
363 list_add(&ser
->node
, &ser_list
);
364 spin_unlock(&ser_lock
);
366 netif_stop_queue(dev
);
367 update_tty_status(ser
);
371 static void ldisc_close(struct tty_struct
*tty
)
373 struct ser_device
*ser
= tty
->disc_data
;
375 tty_kref_put(ser
->tty
);
377 spin_lock(&ser_lock
);
378 list_move(&ser
->node
, &ser_release_list
);
379 spin_unlock(&ser_lock
);
380 schedule_work(&ser_release_work
);
383 /* The line discipline structure. */
384 static struct tty_ldisc_ops caif_ldisc
= {
385 .owner
= THIS_MODULE
,
386 .magic
= TTY_LDISC_MAGIC
,
389 .close
= ldisc_close
,
390 .receive_buf
= ldisc_receive
,
391 .write_wakeup
= ldisc_tx_wakeup
394 static int register_ldisc(void)
398 result
= tty_register_ldisc(N_CAIF
, &caif_ldisc
);
400 pr_err("cannot register CAIF ldisc=%d err=%d\n", N_CAIF
,
406 static const struct net_device_ops netdev_ops
= {
407 .ndo_open
= caif_net_open
,
408 .ndo_stop
= caif_net_close
,
409 .ndo_start_xmit
= caif_xmit
412 static void caifdev_setup(struct net_device
*dev
)
414 struct ser_device
*serdev
= netdev_priv(dev
);
417 dev
->netdev_ops
= &netdev_ops
;
418 dev
->type
= ARPHRD_CAIF
;
419 dev
->flags
= IFF_POINTOPOINT
| IFF_NOARP
;
420 dev
->mtu
= CAIF_MAX_MTU
;
421 dev
->priv_flags
|= IFF_NO_QUEUE
;
422 dev
->needs_free_netdev
= true;
423 skb_queue_head_init(&serdev
->head
);
424 serdev
->common
.link_select
= CAIF_LINK_LOW_LATENCY
;
425 serdev
->common
.use_frag
= true;
426 serdev
->common
.use_stx
= ser_use_stx
;
427 serdev
->common
.use_fcs
= ser_use_fcs
;
432 static int caif_net_open(struct net_device
*dev
)
434 netif_wake_queue(dev
);
438 static int caif_net_close(struct net_device
*dev
)
440 netif_stop_queue(dev
);
444 static int __init
caif_ser_init(void)
448 ret
= register_ldisc();
449 debugfsdir
= debugfs_create_dir("caif_serial", NULL
);
453 static void __exit
caif_ser_exit(void)
455 spin_lock(&ser_lock
);
456 list_splice(&ser_list
, &ser_release_list
);
457 spin_unlock(&ser_lock
);
459 cancel_work_sync(&ser_release_work
);
460 tty_unregister_ldisc(N_CAIF
);
461 debugfs_remove_recursive(debugfsdir
);
464 module_init(caif_ser_init
);
465 module_exit(caif_ser_exit
);