blob: d1bf0ff93ae0ed069617d6c58e50f84d9b061cf5 [file] [log] [blame]
Sjur Braendeland9b271052010-03-30 13:56:30 +00001/*
2 * Copyright (C) ST-Ericsson AB 2010
3 * Author: Sjur Brendeland / sjur.brandeland@stericsson.com
4 * License terms: GNU General Public License (GPL) version 2
5 */
6
Alexey Dobriyana6b7a402011-06-06 10:43:46 +00007#include <linux/hardirq.h>
Sjur Braendeland9b271052010-03-30 13:56:30 +00008#include <linux/init.h>
Sjur Braendeland9b271052010-03-30 13:56:30 +00009#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>
22
23MODULE_LICENSE("GPL");
24MODULE_AUTHOR("Sjur Brendeland<sjur.brandeland@stericsson.com>");
25MODULE_DESCRIPTION("CAIF serial device TTY line discipline");
26MODULE_LICENSE("GPL");
27MODULE_ALIAS_LDISC(N_CAIF);
28
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
34#define ON 1
35#define OFF 0
36#define CAIF_MAX_MTU 4096
37
38/*This list is protected by the rtnl lock. */
39static LIST_HEAD(ser_list);
40
Rusty Russelleb939922011-12-19 14:08:01 +000041static bool ser_loop;
Sjur Braendeland9b271052010-03-30 13:56:30 +000042module_param(ser_loop, bool, S_IRUGO);
43MODULE_PARM_DESC(ser_loop, "Run in simulated loopback mode.");
44
Rusty Russelleb939922011-12-19 14:08:01 +000045static bool ser_use_stx = true;
Sjur Braendeland9b271052010-03-30 13:56:30 +000046module_param(ser_use_stx, bool, S_IRUGO);
47MODULE_PARM_DESC(ser_use_stx, "STX enabled or not.");
48
Rusty Russelleb939922011-12-19 14:08:01 +000049static bool ser_use_fcs = true;
Sjur Braendeland9b271052010-03-30 13:56:30 +000050
51module_param(ser_use_fcs, bool, S_IRUGO);
52MODULE_PARM_DESC(ser_use_fcs, "FCS enabled or not.");
53
54static int ser_write_chunk = MAX_WRITE_CHUNK;
55module_param(ser_write_chunk, int, S_IRUGO);
56
57MODULE_PARM_DESC(ser_write_chunk, "Maximum size of data written to UART.");
58
59static struct dentry *debugfsdir;
60
61static int caif_net_open(struct net_device *dev);
62static int caif_net_close(struct net_device *dev);
63
64struct ser_device {
65 struct caif_dev_common common;
66 struct list_head node;
67 struct net_device *dev;
68 struct sk_buff_head head;
69 struct tty_struct *tty;
70 bool tx_started;
71 unsigned long state;
72 char *tty_name;
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;
77 u8 rx_data[128];
78 u8 tx_data[128];
79 u8 tty_status;
80
81#endif
82};
83
84static void caifdev_setup(struct net_device *dev);
85static void ldisc_tx_wakeup(struct tty_struct *tty);
86#ifdef CONFIG_DEBUG_FS
87static inline void update_tty_status(struct ser_device *ser)
88{
89 ser->tty_status =
90 ser->tty->stopped << 5 |
Sjur Braendeland9b271052010-03-30 13:56:30 +000091 ser->tty->flow_stopped << 3 |
92 ser->tty->packet << 2 |
Jiri Slabyd6c53c02013-01-03 15:53:05 +010093 ser->tty->port->low_latency << 1 |
Sjur Braendeland9b271052010-03-30 13:56:30 +000094 ser->tty->warned;
95}
96static inline void debugfs_init(struct ser_device *ser, struct tty_struct *tty)
97{
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,
103 &ser->tx_blob);
104
105 debugfs_create_blob("last_rx_msg", S_IRUSR,
106 ser->debugfs_tty_dir,
107 &ser->rx_blob);
108
109 debugfs_create_x32("ser_state", S_IRUSR,
110 ser->debugfs_tty_dir,
111 (u32 *)&ser->state);
112
113 debugfs_create_x8("tty_status", S_IRUSR,
114 ser->debugfs_tty_dir,
115 &ser->tty_status);
116
117 }
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;
122}
123
124static inline void debugfs_deinit(struct ser_device *ser)
125{
126 debugfs_remove_recursive(ser->debugfs_tty_dir);
127}
128
129static inline void debugfs_rx(struct ser_device *ser, const u8 *data, int size)
130{
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;
136}
137
138static inline void debugfs_tx(struct ser_device *ser, const u8 *data, int size)
139{
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;
145}
146#else
147static inline void debugfs_init(struct ser_device *ser, struct tty_struct *tty)
148{
149}
150
151static inline void debugfs_deinit(struct ser_device *ser)
152{
153}
154
155static inline void update_tty_status(struct ser_device *ser)
156{
157}
158
159static inline void debugfs_rx(struct ser_device *ser, const u8 *data, int size)
160{
161}
162
163static inline void debugfs_tx(struct ser_device *ser, const u8 *data, int size)
164{
165}
166
167#endif
168
Linus Torvalds55db4c62011-06-04 06:33:24 +0900169static void ldisc_receive(struct tty_struct *tty, const u8 *data,
170 char *flags, int count)
Sjur Braendeland9b271052010-03-30 13:56:30 +0000171{
172 struct sk_buff *skb = NULL;
173 struct ser_device *ser;
174 int ret;
175 u8 *p;
Dan Carpenterc1f8fc52010-05-31 21:09:33 +0000176
Sjur Braendeland9b271052010-03-30 13:56:30 +0000177 ser = tty->disc_data;
178
179 /*
180 * NOTE: flags may contain information about break or overrun.
181 * This is not yet handled.
182 */
183
184
185 /*
186 * Workaround for garbage at start of transmission,
187 * only enable if STX handling is not enabled.
188 */
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");
193 return;
194 }
195
196 BUG_ON(ser->dev == NULL);
197
198 /* Get a suitable caif packet and copy in data. */
199 skb = netdev_alloc_skb(ser->dev, count+1);
Sjur Braendelandd3f744e2010-04-28 08:54:34 +0000200 if (skb == NULL)
201 return;
Sjur Braendeland9b271052010-03-30 13:56:30 +0000202 p = skb_put(skb, count);
203 memcpy(p, data, count);
204
205 skb->protocol = htons(ETH_P_CAIF);
206 skb_reset_mac_header(skb);
207 skb->dev = ser->dev;
208 debugfs_rx(ser, data, count);
209 /* Push received packet up the stack. */
210 ret = netif_rx_ni(skb);
211 if (!ret) {
212 ser->dev->stats.rx_packets++;
213 ser->dev->stats.rx_bytes += count;
214 } else
215 ++ser->dev->stats.rx_dropped;
216 update_tty_status(ser);
217}
218
219static int handle_tx(struct ser_device *ser)
220{
221 struct tty_struct *tty;
222 struct sk_buff *skb;
223 int tty_wr, len, room;
Dan Carpenterc1f8fc52010-05-31 21:09:33 +0000224
Sjur Braendeland9b271052010-03-30 13:56:30 +0000225 tty = ser->tty;
226 ser->tx_started = true;
227
228 /* Enter critical section */
229 if (test_and_set_bit(CAIF_SENDING, &ser->state))
230 return 0;
231
232 /* skb_peek is safe because handle_tx is called after skb_queue_tail */
233 while ((skb = skb_peek(&ser->head)) != NULL) {
234
235 /* Make sure you don't write too much */
236 len = skb->len;
237 room = tty_write_room(tty);
238 if (!room)
239 break;
240 if (room > ser_write_chunk)
241 room = ser_write_chunk;
242 if (len > room)
243 len = room;
244
245 /* Write to tty or loopback */
246 if (!ser_loop) {
247 tty_wr = tty->ops->write(tty, skb->data, len);
248 update_tty_status(ser);
249 } else {
250 tty_wr = len;
251 ldisc_receive(tty, skb->data, NULL, len);
252 }
253 ser->dev->stats.tx_packets++;
254 ser->dev->stats.tx_bytes += tty_wr;
255
256 /* Error on TTY ?! */
257 if (tty_wr < 0)
258 goto error;
259 /* Reduce buffer written, and discard if empty */
260 skb_pull(skb, tty_wr);
261 if (skb->len == 0) {
262 struct sk_buff *tmp = skb_dequeue(&ser->head);
Roar Førdef84ea772011-12-06 12:15:44 +0000263 WARN_ON(tmp != skb);
Sjur Braendeland9b271052010-03-30 13:56:30 +0000264 if (in_interrupt())
265 dev_kfree_skb_irq(skb);
266 else
267 kfree_skb(skb);
268 }
269 }
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);
276 return 0;
277error:
278 clear_bit(CAIF_SENDING, &ser->state);
279 return tty_wr;
280}
281
282static int caif_xmit(struct sk_buff *skb, struct net_device *dev)
283{
284 struct ser_device *ser;
Dan Carpenterc1f8fc52010-05-31 21:09:33 +0000285
Sjur Braendeland9b271052010-03-30 13:56:30 +0000286 BUG_ON(dev == NULL);
287 ser = netdev_priv(dev);
288
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)
293
294 ser->common.flowctrl(ser->dev, OFF);
295
296 skb_queue_tail(&ser->head, skb);
297 return handle_tx(ser);
298}
299
300
301static void ldisc_tx_wakeup(struct tty_struct *tty)
302{
303 struct ser_device *ser;
Dan Carpenterc1f8fc52010-05-31 21:09:33 +0000304
Sjur Braendeland9b271052010-03-30 13:56:30 +0000305 ser = tty->disc_data;
306 BUG_ON(ser == NULL);
Roar Førdef84ea772011-12-06 12:15:44 +0000307 WARN_ON(ser->tty != tty);
Sjur Braendeland9b271052010-03-30 13:56:30 +0000308 handle_tx(ser);
309}
310
311
312static int ldisc_open(struct tty_struct *tty)
313{
314 struct ser_device *ser;
315 struct net_device *dev;
316 char name[64];
317 int result;
318
Alan Coxc93f0942010-04-07 16:49:31 -0700319 /* No write no play */
320 if (tty->ops->write == NULL)
321 return -EOPNOTSUPP;
Sjur Braendelandd3f744e2010-04-28 08:54:34 +0000322 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_TTY_CONFIG))
323 return -EPERM;
Alan Coxc93f0942010-04-07 16:49:31 -0700324
Sjur Braendeland9b271052010-03-30 13:56:30 +0000325 sprintf(name, "cf%s", tty->name);
326 dev = alloc_netdev(sizeof(*ser), name, caifdev_setup);
Alan Coxc66b9b72012-07-24 02:42:14 +0000327 if (!dev)
328 return -ENOMEM;
329
Sjur Braendeland9b271052010-03-30 13:56:30 +0000330 ser = netdev_priv(dev);
Alan Coxe31d5a02010-04-07 16:50:00 -0700331 ser->tty = tty_kref_get(tty);
Sjur Braendeland9b271052010-03-30 13:56:30 +0000332 ser->dev = dev;
333 debugfs_init(ser, tty);
334 tty->receive_room = N_TTY_BUF_SIZE;
335 tty->disc_data = ser;
336 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
337 rtnl_lock();
338 result = register_netdevice(dev);
339 if (result) {
340 rtnl_unlock();
341 free_netdev(dev);
342 return -ENODEV;
343 }
344
345 list_add(&ser->node, &ser_list);
346 rtnl_unlock();
347 netif_stop_queue(dev);
348 update_tty_status(ser);
349 return 0;
350}
351
352static void ldisc_close(struct tty_struct *tty)
353{
354 struct ser_device *ser = tty->disc_data;
355 /* Remove may be called inside or outside of rtnl_lock */
356 int islocked = rtnl_is_locked();
Dan Carpenterc1f8fc52010-05-31 21:09:33 +0000357
Sjur Braendeland9b271052010-03-30 13:56:30 +0000358 if (!islocked)
359 rtnl_lock();
360 /* device is freed automagically by net-sysfs */
361 dev_close(ser->dev);
362 unregister_netdevice(ser->dev);
363 list_del(&ser->node);
364 debugfs_deinit(ser);
Alan Coxe31d5a02010-04-07 16:50:00 -0700365 tty_kref_put(ser->tty);
Sjur Braendeland9b271052010-03-30 13:56:30 +0000366 if (!islocked)
367 rtnl_unlock();
368}
369
370/* The line discipline structure. */
371static struct tty_ldisc_ops caif_ldisc = {
372 .owner = THIS_MODULE,
373 .magic = TTY_LDISC_MAGIC,
374 .name = "n_caif",
375 .open = ldisc_open,
376 .close = ldisc_close,
377 .receive_buf = ldisc_receive,
378 .write_wakeup = ldisc_tx_wakeup
379};
380
381static int register_ldisc(void)
382{
383 int result;
Dan Carpenterc1f8fc52010-05-31 21:09:33 +0000384
Sjur Braendeland9b271052010-03-30 13:56:30 +0000385 result = tty_register_ldisc(N_CAIF, &caif_ldisc);
386 if (result < 0) {
387 pr_err("cannot register CAIF ldisc=%d err=%d\n", N_CAIF,
388 result);
389 return result;
390 }
391 return result;
392}
393static const struct net_device_ops netdev_ops = {
394 .ndo_open = caif_net_open,
395 .ndo_stop = caif_net_close,
396 .ndo_start_xmit = caif_xmit
397};
398
399static void caifdev_setup(struct net_device *dev)
400{
401 struct ser_device *serdev = netdev_priv(dev);
Dan Carpenterc1f8fc52010-05-31 21:09:33 +0000402
Sjur Braendeland9b271052010-03-30 13:56:30 +0000403 dev->features = 0;
404 dev->netdev_ops = &netdev_ops;
405 dev->type = ARPHRD_CAIF;
406 dev->flags = IFF_POINTOPOINT | IFF_NOARP;
407 dev->mtu = CAIF_MAX_MTU;
Sjur Braendeland9b271052010-03-30 13:56:30 +0000408 dev->tx_queue_len = 0;
409 dev->destructor = free_netdev;
410 skb_queue_head_init(&serdev->head);
411 serdev->common.link_select = CAIF_LINK_LOW_LATENCY;
412 serdev->common.use_frag = true;
413 serdev->common.use_stx = ser_use_stx;
414 serdev->common.use_fcs = ser_use_fcs;
415 serdev->dev = dev;
416}
417
418
419static int caif_net_open(struct net_device *dev)
420{
Sjur Braendeland9b271052010-03-30 13:56:30 +0000421 netif_wake_queue(dev);
422 return 0;
423}
424
425static int caif_net_close(struct net_device *dev)
426{
427 netif_stop_queue(dev);
428 return 0;
429}
430
431static int __init caif_ser_init(void)
432{
433 int ret;
Dan Carpenterc1f8fc52010-05-31 21:09:33 +0000434
Sjur Braendeland9b271052010-03-30 13:56:30 +0000435 ret = register_ldisc();
436 debugfsdir = debugfs_create_dir("caif_serial", NULL);
437 return ret;
438}
439
440static void __exit caif_ser_exit(void)
441{
442 struct ser_device *ser = NULL;
443 struct list_head *node;
444 struct list_head *_tmp;
Dan Carpenterc1f8fc52010-05-31 21:09:33 +0000445
Sjur Braendeland9b271052010-03-30 13:56:30 +0000446 list_for_each_safe(node, _tmp, &ser_list) {
447 ser = list_entry(node, struct ser_device, node);
448 dev_close(ser->dev);
449 unregister_netdevice(ser->dev);
450 list_del(node);
451 }
452 tty_unregister_ldisc(N_CAIF);
453 debugfs_remove_recursive(debugfsdir);
454}
455
456module_init(caif_ser_init);
457module_exit(caif_ser_exit);