]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/net/caif/caif_serial.c
UBUNTU: Ubuntu-4.13.0-45.50
[mirror_ubuntu-artful-kernel.git] / drivers / net / caif / caif_serial.c
CommitLineData
9b27105b
SB
1/*
2 * Copyright (C) ST-Ericsson AB 2010
26ee65e6 3 * Author: Sjur Brendeland
9b27105b
SB
4 * License terms: GNU General Public License (GPL) version 2
5 */
6
a6b7a407 7#include <linux/hardirq.h>
9b27105b 8#include <linux/init.h>
9b27105b
SB
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>
22
23MODULE_LICENSE("GPL");
26ee65e6 24MODULE_AUTHOR("Sjur Brendeland");
9b27105b
SB
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
56e0ef52 38static DEFINE_SPINLOCK(ser_lock);
9b27105b 39static LIST_HEAD(ser_list);
56e0ef52 40static LIST_HEAD(ser_release_list);
9b27105b 41
eb939922 42static bool ser_loop;
9b27105b
SB
43module_param(ser_loop, bool, S_IRUGO);
44MODULE_PARM_DESC(ser_loop, "Run in simulated loopback mode.");
45
eb939922 46static bool ser_use_stx = true;
9b27105b
SB
47module_param(ser_use_stx, bool, S_IRUGO);
48MODULE_PARM_DESC(ser_use_stx, "STX enabled or not.");
49
eb939922 50static bool ser_use_fcs = true;
9b27105b
SB
51
52module_param(ser_use_fcs, bool, S_IRUGO);
53MODULE_PARM_DESC(ser_use_fcs, "FCS enabled or not.");
54
55static int ser_write_chunk = MAX_WRITE_CHUNK;
56module_param(ser_write_chunk, int, S_IRUGO);
57
58MODULE_PARM_DESC(ser_write_chunk, "Maximum size of data written to UART.");
59
60static struct dentry *debugfsdir;
61
62static int caif_net_open(struct net_device *dev);
63static int caif_net_close(struct net_device *dev);
64
65struct ser_device {
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;
71 bool tx_started;
72 unsigned long state;
9b27105b
SB
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 |
9b27105b
SB
91 ser->tty->flow_stopped << 3 |
92 ser->tty->packet << 2 |
0d3b88d1 93 ser->tty->port->low_latency << 1;
9b27105b
SB
94}
95static inline void debugfs_init(struct ser_device *ser, struct tty_struct *tty)
96{
97 ser->debugfs_tty_dir =
98 debugfs_create_dir(tty->name, debugfsdir);
99 if (!IS_ERR(ser->debugfs_tty_dir)) {
100 debugfs_create_blob("last_tx_msg", S_IRUSR,
101 ser->debugfs_tty_dir,
102 &ser->tx_blob);
103
104 debugfs_create_blob("last_rx_msg", S_IRUSR,
105 ser->debugfs_tty_dir,
106 &ser->rx_blob);
107
108 debugfs_create_x32("ser_state", S_IRUSR,
109 ser->debugfs_tty_dir,
110 (u32 *)&ser->state);
111
112 debugfs_create_x8("tty_status", S_IRUSR,
113 ser->debugfs_tty_dir,
114 &ser->tty_status);
115
116 }
117 ser->tx_blob.data = ser->tx_data;
118 ser->tx_blob.size = 0;
119 ser->rx_blob.data = ser->rx_data;
120 ser->rx_blob.size = 0;
121}
122
123static inline void debugfs_deinit(struct ser_device *ser)
124{
125 debugfs_remove_recursive(ser->debugfs_tty_dir);
126}
127
128static inline void debugfs_rx(struct ser_device *ser, const u8 *data, int size)
129{
130 if (size > sizeof(ser->rx_data))
131 size = sizeof(ser->rx_data);
132 memcpy(ser->rx_data, data, size);
133 ser->rx_blob.data = ser->rx_data;
134 ser->rx_blob.size = size;
135}
136
137static inline void debugfs_tx(struct ser_device *ser, const u8 *data, int size)
138{
139 if (size > sizeof(ser->tx_data))
140 size = sizeof(ser->tx_data);
141 memcpy(ser->tx_data, data, size);
142 ser->tx_blob.data = ser->tx_data;
143 ser->tx_blob.size = size;
144}
145#else
146static inline void debugfs_init(struct ser_device *ser, struct tty_struct *tty)
147{
148}
149
150static inline void debugfs_deinit(struct ser_device *ser)
151{
152}
153
154static inline void update_tty_status(struct ser_device *ser)
155{
156}
157
158static inline void debugfs_rx(struct ser_device *ser, const u8 *data, int size)
159{
160}
161
162static inline void debugfs_tx(struct ser_device *ser, const u8 *data, int size)
163{
164}
165
166#endif
167
55db4c64
LT
168static void ldisc_receive(struct tty_struct *tty, const u8 *data,
169 char *flags, int count)
9b27105b
SB
170{
171 struct sk_buff *skb = NULL;
172 struct ser_device *ser;
173 int ret;
c1f8fc57 174
9b27105b
SB
175 ser = tty->disc_data;
176
177 /*
178 * NOTE: flags may contain information about break or overrun.
179 * This is not yet handled.
180 */
181
182
183 /*
184 * Workaround for garbage at start of transmission,
185 * only enable if STX handling is not enabled.
186 */
187 if (!ser->common.use_stx && !ser->tx_started) {
188 dev_info(&ser->dev->dev,
189 "Bytes received before initial transmission -"
190 "bytes discarded.\n");
191 return;
192 }
193
194 BUG_ON(ser->dev == NULL);
195
196 /* Get a suitable caif packet and copy in data. */
197 skb = netdev_alloc_skb(ser->dev, count+1);
d3f744e0
SB
198 if (skb == NULL)
199 return;
b952f4df 200 skb_put_data(skb, data, count);
9b27105b
SB
201
202 skb->protocol = htons(ETH_P_CAIF);
203 skb_reset_mac_header(skb);
9b27105b
SB
204 debugfs_rx(ser, data, count);
205 /* Push received packet up the stack. */
206 ret = netif_rx_ni(skb);
207 if (!ret) {
208 ser->dev->stats.rx_packets++;
209 ser->dev->stats.rx_bytes += count;
210 } else
211 ++ser->dev->stats.rx_dropped;
212 update_tty_status(ser);
213}
214
215static int handle_tx(struct ser_device *ser)
216{
217 struct tty_struct *tty;
218 struct sk_buff *skb;
219 int tty_wr, len, room;
c1f8fc57 220
9b27105b
SB
221 tty = ser->tty;
222 ser->tx_started = true;
223
224 /* Enter critical section */
225 if (test_and_set_bit(CAIF_SENDING, &ser->state))
226 return 0;
227
228 /* skb_peek is safe because handle_tx is called after skb_queue_tail */
229 while ((skb = skb_peek(&ser->head)) != NULL) {
230
231 /* Make sure you don't write too much */
232 len = skb->len;
233 room = tty_write_room(tty);
234 if (!room)
235 break;
236 if (room > ser_write_chunk)
237 room = ser_write_chunk;
238 if (len > room)
239 len = room;
240
241 /* Write to tty or loopback */
242 if (!ser_loop) {
243 tty_wr = tty->ops->write(tty, skb->data, len);
244 update_tty_status(ser);
245 } else {
246 tty_wr = len;
247 ldisc_receive(tty, skb->data, NULL, len);
248 }
249 ser->dev->stats.tx_packets++;
250 ser->dev->stats.tx_bytes += tty_wr;
251
252 /* Error on TTY ?! */
253 if (tty_wr < 0)
254 goto error;
255 /* Reduce buffer written, and discard if empty */
256 skb_pull(skb, tty_wr);
257 if (skb->len == 0) {
258 struct sk_buff *tmp = skb_dequeue(&ser->head);
f84ea779 259 WARN_ON(tmp != skb);
9b27105b
SB
260 if (in_interrupt())
261 dev_kfree_skb_irq(skb);
262 else
263 kfree_skb(skb);
264 }
265 }
266 /* Send flow off if queue is empty */
267 if (ser->head.qlen <= SEND_QUEUE_LOW &&
268 test_and_clear_bit(CAIF_FLOW_OFF_SENT, &ser->state) &&
269 ser->common.flowctrl != NULL)
270 ser->common.flowctrl(ser->dev, ON);
271 clear_bit(CAIF_SENDING, &ser->state);
272 return 0;
273error:
274 clear_bit(CAIF_SENDING, &ser->state);
275 return tty_wr;
276}
277
278static int caif_xmit(struct sk_buff *skb, struct net_device *dev)
279{
280 struct ser_device *ser;
c1f8fc57 281
9b27105b
SB
282 BUG_ON(dev == NULL);
283 ser = netdev_priv(dev);
284
285 /* Send flow off once, on high water mark */
286 if (ser->head.qlen > SEND_QUEUE_HIGH &&
287 !test_and_set_bit(CAIF_FLOW_OFF_SENT, &ser->state) &&
288 ser->common.flowctrl != NULL)
289
290 ser->common.flowctrl(ser->dev, OFF);
291
292 skb_queue_tail(&ser->head, skb);
293 return handle_tx(ser);
294}
295
296
297static void ldisc_tx_wakeup(struct tty_struct *tty)
298{
299 struct ser_device *ser;
c1f8fc57 300
9b27105b
SB
301 ser = tty->disc_data;
302 BUG_ON(ser == NULL);
f84ea779 303 WARN_ON(ser->tty != tty);
9b27105b
SB
304 handle_tx(ser);
305}
306
307
56e0ef52
KK
308static void ser_release(struct work_struct *work)
309{
310 struct list_head list;
311 struct ser_device *ser, *tmp;
312
313 spin_lock(&ser_lock);
314 list_replace_init(&ser_release_list, &list);
315 spin_unlock(&ser_lock);
316
317 if (!list_empty(&list)) {
318 rtnl_lock();
319 list_for_each_entry_safe(ser, tmp, &list, node) {
320 dev_close(ser->dev);
321 unregister_netdevice(ser->dev);
322 debugfs_deinit(ser);
323 }
324 rtnl_unlock();
325 }
326}
327
328static DECLARE_WORK(ser_release_work, ser_release);
329
9b27105b
SB
330static int ldisc_open(struct tty_struct *tty)
331{
332 struct ser_device *ser;
333 struct net_device *dev;
334 char name[64];
335 int result;
336
c93f0940
AC
337 /* No write no play */
338 if (tty->ops->write == NULL)
339 return -EOPNOTSUPP;
d3f744e0
SB
340 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_TTY_CONFIG))
341 return -EPERM;
c93f0940 342
56e0ef52
KK
343 /* release devices to avoid name collision */
344 ser_release(NULL);
345
cab6ce9e
DC
346 result = snprintf(name, sizeof(name), "cf%s", tty->name);
347 if (result >= IFNAMSIZ)
348 return -EINVAL;
c835a677
TG
349 dev = alloc_netdev(sizeof(*ser), name, NET_NAME_UNKNOWN,
350 caifdev_setup);
c66b9b7d
AC
351 if (!dev)
352 return -ENOMEM;
353
9b27105b 354 ser = netdev_priv(dev);
e31d5a05 355 ser->tty = tty_kref_get(tty);
9b27105b
SB
356 ser->dev = dev;
357 debugfs_init(ser, tty);
358 tty->receive_room = N_TTY_BUF_SIZE;
359 tty->disc_data = ser;
360 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
361 rtnl_lock();
362 result = register_netdevice(dev);
363 if (result) {
364 rtnl_unlock();
365 free_netdev(dev);
366 return -ENODEV;
367 }
368
56e0ef52 369 spin_lock(&ser_lock);
9b27105b 370 list_add(&ser->node, &ser_list);
56e0ef52 371 spin_unlock(&ser_lock);
9b27105b
SB
372 rtnl_unlock();
373 netif_stop_queue(dev);
374 update_tty_status(ser);
375 return 0;
376}
377
378static void ldisc_close(struct tty_struct *tty)
379{
380 struct ser_device *ser = tty->disc_data;
c1f8fc57 381
e31d5a05 382 tty_kref_put(ser->tty);
56e0ef52
KK
383
384 spin_lock(&ser_lock);
385 list_move(&ser->node, &ser_release_list);
386 spin_unlock(&ser_lock);
387 schedule_work(&ser_release_work);
9b27105b
SB
388}
389
390/* The line discipline structure. */
391static struct tty_ldisc_ops caif_ldisc = {
392 .owner = THIS_MODULE,
393 .magic = TTY_LDISC_MAGIC,
394 .name = "n_caif",
395 .open = ldisc_open,
396 .close = ldisc_close,
397 .receive_buf = ldisc_receive,
398 .write_wakeup = ldisc_tx_wakeup
399};
400
401static int register_ldisc(void)
402{
403 int result;
c1f8fc57 404
9b27105b
SB
405 result = tty_register_ldisc(N_CAIF, &caif_ldisc);
406 if (result < 0) {
407 pr_err("cannot register CAIF ldisc=%d err=%d\n", N_CAIF,
408 result);
409 return result;
410 }
411 return result;
412}
413static const struct net_device_ops netdev_ops = {
414 .ndo_open = caif_net_open,
415 .ndo_stop = caif_net_close,
416 .ndo_start_xmit = caif_xmit
417};
418
419static void caifdev_setup(struct net_device *dev)
420{
421 struct ser_device *serdev = netdev_priv(dev);
c1f8fc57 422
9b27105b
SB
423 dev->features = 0;
424 dev->netdev_ops = &netdev_ops;
425 dev->type = ARPHRD_CAIF;
426 dev->flags = IFF_POINTOPOINT | IFF_NOARP;
427 dev->mtu = CAIF_MAX_MTU;
4676a152 428 dev->priv_flags |= IFF_NO_QUEUE;
cf124db5 429 dev->needs_free_netdev = true;
9b27105b
SB
430 skb_queue_head_init(&serdev->head);
431 serdev->common.link_select = CAIF_LINK_LOW_LATENCY;
432 serdev->common.use_frag = true;
433 serdev->common.use_stx = ser_use_stx;
434 serdev->common.use_fcs = ser_use_fcs;
435 serdev->dev = dev;
436}
437
438
439static int caif_net_open(struct net_device *dev)
440{
9b27105b
SB
441 netif_wake_queue(dev);
442 return 0;
443}
444
445static int caif_net_close(struct net_device *dev)
446{
447 netif_stop_queue(dev);
448 return 0;
449}
450
451static int __init caif_ser_init(void)
452{
453 int ret;
c1f8fc57 454
9b27105b
SB
455 ret = register_ldisc();
456 debugfsdir = debugfs_create_dir("caif_serial", NULL);
457 return ret;
458}
459
460static void __exit caif_ser_exit(void)
461{
56e0ef52
KK
462 spin_lock(&ser_lock);
463 list_splice(&ser_list, &ser_release_list);
464 spin_unlock(&ser_lock);
465 ser_release(NULL);
466 cancel_work_sync(&ser_release_work);
9b27105b
SB
467 tty_unregister_ldisc(N_CAIF);
468 debugfs_remove_recursive(debugfsdir);
469}
470
471module_init(caif_ser_init);
472module_exit(caif_ser_exit);