2 * Copyright (C) ST-Ericsson AB 2010
3 * Author: Sjur Brendeland
4 * License terms: GNU General Public License (GPL) version 2
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, S_IRUGO
);
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, S_IRUGO
);
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, S_IRUGO
);
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, S_IRUGO
);
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
;
74 #ifdef CONFIG_DEBUG_FS
75 struct dentry
*debugfs_tty_dir
;
76 struct debugfs_blob_wrapper tx_blob
;
77 struct debugfs_blob_wrapper rx_blob
;
85 static void caifdev_setup(struct net_device
*dev
);
86 static void ldisc_tx_wakeup(struct tty_struct
*tty
);
87 #ifdef CONFIG_DEBUG_FS
88 static inline void update_tty_status(struct ser_device
*ser
)
91 ser
->tty
->stopped
<< 5 |
92 ser
->tty
->flow_stopped
<< 3 |
93 ser
->tty
->packet
<< 2 |
94 ser
->tty
->port
->low_latency
<< 1;
96 static inline void debugfs_init(struct ser_device
*ser
, struct tty_struct
*tty
)
98 ser
->debugfs_tty_dir
=
99 debugfs_create_dir(tty
->name
, debugfsdir
);
100 if (!IS_ERR(ser
->debugfs_tty_dir
)) {
101 debugfs_create_blob("last_tx_msg", S_IRUSR
,
102 ser
->debugfs_tty_dir
,
105 debugfs_create_blob("last_rx_msg", S_IRUSR
,
106 ser
->debugfs_tty_dir
,
109 debugfs_create_x32("ser_state", S_IRUSR
,
110 ser
->debugfs_tty_dir
,
113 debugfs_create_x8("tty_status", S_IRUSR
,
114 ser
->debugfs_tty_dir
,
118 ser
->tx_blob
.data
= ser
->tx_data
;
119 ser
->tx_blob
.size
= 0;
120 ser
->rx_blob
.data
= ser
->rx_data
;
121 ser
->rx_blob
.size
= 0;
124 static inline void debugfs_deinit(struct ser_device
*ser
)
126 debugfs_remove_recursive(ser
->debugfs_tty_dir
);
129 static inline void debugfs_rx(struct ser_device
*ser
, const u8
*data
, int size
)
131 if (size
> sizeof(ser
->rx_data
))
132 size
= sizeof(ser
->rx_data
);
133 memcpy(ser
->rx_data
, data
, size
);
134 ser
->rx_blob
.data
= ser
->rx_data
;
135 ser
->rx_blob
.size
= size
;
138 static inline void debugfs_tx(struct ser_device
*ser
, const u8
*data
, int size
)
140 if (size
> sizeof(ser
->tx_data
))
141 size
= sizeof(ser
->tx_data
);
142 memcpy(ser
->tx_data
, data
, size
);
143 ser
->tx_blob
.data
= ser
->tx_data
;
144 ser
->tx_blob
.size
= size
;
147 static inline void debugfs_init(struct ser_device
*ser
, struct tty_struct
*tty
)
151 static inline void debugfs_deinit(struct ser_device
*ser
)
155 static inline void update_tty_status(struct ser_device
*ser
)
159 static inline void debugfs_rx(struct ser_device
*ser
, const u8
*data
, int size
)
163 static inline void debugfs_tx(struct ser_device
*ser
, const u8
*data
, int size
)
169 static void ldisc_receive(struct tty_struct
*tty
, const u8
*data
,
170 char *flags
, int count
)
172 struct sk_buff
*skb
= NULL
;
173 struct ser_device
*ser
;
177 ser
= tty
->disc_data
;
180 * NOTE: flags may contain information about break or overrun.
181 * This is not yet handled.
186 * Workaround for garbage at start of transmission,
187 * only enable if STX handling is not enabled.
189 if (!ser
->common
.use_stx
&& !ser
->tx_started
) {
190 dev_info(&ser
->dev
->dev
,
191 "Bytes received before initial transmission -"
192 "bytes discarded.\n");
196 BUG_ON(ser
->dev
== NULL
);
198 /* Get a suitable caif packet and copy in data. */
199 skb
= netdev_alloc_skb(ser
->dev
, count
+1);
202 p
= skb_put(skb
, count
);
203 memcpy(p
, data
, count
);
205 skb
->protocol
= htons(ETH_P_CAIF
);
206 skb_reset_mac_header(skb
);
208 debugfs_rx(ser
, data
, count
);
209 /* Push received packet up the stack. */
210 ret
= netif_rx_ni(skb
);
212 ser
->dev
->stats
.rx_packets
++;
213 ser
->dev
->stats
.rx_bytes
+= count
;
215 ++ser
->dev
->stats
.rx_dropped
;
216 update_tty_status(ser
);
219 static int handle_tx(struct ser_device
*ser
)
221 struct tty_struct
*tty
;
223 int tty_wr
, len
, room
;
226 ser
->tx_started
= true;
228 /* Enter critical section */
229 if (test_and_set_bit(CAIF_SENDING
, &ser
->state
))
232 /* skb_peek is safe because handle_tx is called after skb_queue_tail */
233 while ((skb
= skb_peek(&ser
->head
)) != NULL
) {
235 /* Make sure you don't write too much */
237 room
= tty_write_room(tty
);
240 if (room
> ser_write_chunk
)
241 room
= ser_write_chunk
;
245 /* Write to tty or loopback */
247 tty_wr
= tty
->ops
->write(tty
, skb
->data
, len
);
248 update_tty_status(ser
);
251 ldisc_receive(tty
, skb
->data
, NULL
, len
);
253 ser
->dev
->stats
.tx_packets
++;
254 ser
->dev
->stats
.tx_bytes
+= tty_wr
;
256 /* Error on TTY ?! */
259 /* Reduce buffer written, and discard if empty */
260 skb_pull(skb
, tty_wr
);
262 struct sk_buff
*tmp
= skb_dequeue(&ser
->head
);
265 dev_kfree_skb_irq(skb
);
270 /* Send flow off if queue is empty */
271 if (ser
->head
.qlen
<= SEND_QUEUE_LOW
&&
272 test_and_clear_bit(CAIF_FLOW_OFF_SENT
, &ser
->state
) &&
273 ser
->common
.flowctrl
!= NULL
)
274 ser
->common
.flowctrl(ser
->dev
, ON
);
275 clear_bit(CAIF_SENDING
, &ser
->state
);
278 clear_bit(CAIF_SENDING
, &ser
->state
);
282 static int caif_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
284 struct ser_device
*ser
;
287 ser
= netdev_priv(dev
);
289 /* Send flow off once, on high water mark */
290 if (ser
->head
.qlen
> SEND_QUEUE_HIGH
&&
291 !test_and_set_bit(CAIF_FLOW_OFF_SENT
, &ser
->state
) &&
292 ser
->common
.flowctrl
!= NULL
)
294 ser
->common
.flowctrl(ser
->dev
, OFF
);
296 skb_queue_tail(&ser
->head
, skb
);
297 return handle_tx(ser
);
301 static void ldisc_tx_wakeup(struct tty_struct
*tty
)
303 struct ser_device
*ser
;
305 ser
= tty
->disc_data
;
307 WARN_ON(ser
->tty
!= tty
);
312 static void ser_release(struct work_struct
*work
)
314 struct list_head list
;
315 struct ser_device
*ser
, *tmp
;
317 spin_lock(&ser_lock
);
318 list_replace_init(&ser_release_list
, &list
);
319 spin_unlock(&ser_lock
);
321 if (!list_empty(&list
)) {
323 list_for_each_entry_safe(ser
, tmp
, &list
, node
) {
325 unregister_netdevice(ser
->dev
);
332 static DECLARE_WORK(ser_release_work
, ser_release
);
334 static int ldisc_open(struct tty_struct
*tty
)
336 struct ser_device
*ser
;
337 struct net_device
*dev
;
341 /* No write no play */
342 if (tty
->ops
->write
== NULL
)
344 if (!capable(CAP_SYS_ADMIN
) && !capable(CAP_SYS_TTY_CONFIG
))
347 /* release devices to avoid name collision */
350 sprintf(name
, "cf%s", tty
->name
);
351 dev
= alloc_netdev(sizeof(*ser
), name
, caifdev_setup
);
355 ser
= netdev_priv(dev
);
356 ser
->tty
= tty_kref_get(tty
);
358 debugfs_init(ser
, tty
);
359 tty
->receive_room
= N_TTY_BUF_SIZE
;
360 tty
->disc_data
= ser
;
361 set_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
);
363 result
= register_netdevice(dev
);
370 spin_lock(&ser_lock
);
371 list_add(&ser
->node
, &ser_list
);
372 spin_unlock(&ser_lock
);
374 netif_stop_queue(dev
);
375 update_tty_status(ser
);
379 static void ldisc_close(struct tty_struct
*tty
)
381 struct ser_device
*ser
= tty
->disc_data
;
383 tty_kref_put(ser
->tty
);
385 spin_lock(&ser_lock
);
386 list_move(&ser
->node
, &ser_release_list
);
387 spin_unlock(&ser_lock
);
388 schedule_work(&ser_release_work
);
391 /* The line discipline structure. */
392 static struct tty_ldisc_ops caif_ldisc
= {
393 .owner
= THIS_MODULE
,
394 .magic
= TTY_LDISC_MAGIC
,
397 .close
= ldisc_close
,
398 .receive_buf
= ldisc_receive
,
399 .write_wakeup
= ldisc_tx_wakeup
402 static int register_ldisc(void)
406 result
= tty_register_ldisc(N_CAIF
, &caif_ldisc
);
408 pr_err("cannot register CAIF ldisc=%d err=%d\n", N_CAIF
,
414 static const struct net_device_ops netdev_ops
= {
415 .ndo_open
= caif_net_open
,
416 .ndo_stop
= caif_net_close
,
417 .ndo_start_xmit
= caif_xmit
420 static void caifdev_setup(struct net_device
*dev
)
422 struct ser_device
*serdev
= netdev_priv(dev
);
425 dev
->netdev_ops
= &netdev_ops
;
426 dev
->type
= ARPHRD_CAIF
;
427 dev
->flags
= IFF_POINTOPOINT
| IFF_NOARP
;
428 dev
->mtu
= CAIF_MAX_MTU
;
429 dev
->tx_queue_len
= 0;
430 dev
->destructor
= free_netdev
;
431 skb_queue_head_init(&serdev
->head
);
432 serdev
->common
.link_select
= CAIF_LINK_LOW_LATENCY
;
433 serdev
->common
.use_frag
= true;
434 serdev
->common
.use_stx
= ser_use_stx
;
435 serdev
->common
.use_fcs
= ser_use_fcs
;
440 static int caif_net_open(struct net_device
*dev
)
442 netif_wake_queue(dev
);
446 static int caif_net_close(struct net_device
*dev
)
448 netif_stop_queue(dev
);
452 static int __init
caif_ser_init(void)
456 ret
= register_ldisc();
457 debugfsdir
= debugfs_create_dir("caif_serial", NULL
);
461 static void __exit
caif_ser_exit(void)
463 spin_lock(&ser_lock
);
464 list_splice(&ser_list
, &ser_release_list
);
465 spin_unlock(&ser_lock
);
467 cancel_work_sync(&ser_release_work
);
468 tty_unregister_ldisc(N_CAIF
);
469 debugfs_remove_recursive(debugfsdir
);
472 module_init(caif_ser_init
);
473 module_exit(caif_ser_exit
);