aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--drivers/net/Kconfig2
-rw-r--r--drivers/net/Makefile1
-rw-r--r--drivers/net/caif/Kconfig17
-rw-r--r--drivers/net/caif/Makefile12
-rw-r--r--drivers/net/caif/caif_serial.c441
-rw-r--r--include/linux/tty.h4
6 files changed, 475 insertions, 2 deletions
diff --git a/drivers/net/Kconfig b/drivers/net/Kconfig
index 4f1f9741c71d..da6657c11667 100644
--- a/drivers/net/Kconfig
+++ b/drivers/net/Kconfig
@@ -2837,6 +2837,8 @@ source "drivers/ieee802154/Kconfig"
2837 2837
2838source "drivers/s390/net/Kconfig" 2838source "drivers/s390/net/Kconfig"
2839 2839
2840source "drivers/net/caif/Kconfig"
2841
2840config XEN_NETDEV_FRONTEND 2842config XEN_NETDEV_FRONTEND
2841 tristate "Xen network device frontend driver" 2843 tristate "Xen network device frontend driver"
2842 depends on XEN 2844 depends on XEN
diff --git a/drivers/net/Makefile b/drivers/net/Makefile
index 478886234c28..ba06c1c0db8b 100644
--- a/drivers/net/Makefile
+++ b/drivers/net/Makefile
@@ -290,5 +290,6 @@ obj-$(CONFIG_VIRTIO_NET) += virtio_net.o
290obj-$(CONFIG_SFC) += sfc/ 290obj-$(CONFIG_SFC) += sfc/
291 291
292obj-$(CONFIG_WIMAX) += wimax/ 292obj-$(CONFIG_WIMAX) += wimax/
293obj-$(CONFIG_CAIF) += caif/
293 294
294obj-$(CONFIG_OCTEON_MGMT_ETHERNET) += octeon/ 295obj-$(CONFIG_OCTEON_MGMT_ETHERNET) += octeon/
diff --git a/drivers/net/caif/Kconfig b/drivers/net/caif/Kconfig
new file mode 100644
index 000000000000..0b28e0107697
--- /dev/null
+++ b/drivers/net/caif/Kconfig
@@ -0,0 +1,17 @@
1#
2# CAIF physical drivers
3#
4
5if CAIF
6
7comment "CAIF transport drivers"
8
9config CAIF_TTY
10 tristate "CAIF TTY transport driver"
11 default n
12 ---help---
13 The CAIF TTY transport driver is a Line Discipline (ldisc)
14 identified as N_CAIF. When this ldisc is opened from user space
15 it will redirect the TTY's traffic into the CAIF stack.
16
17endif # CAIF
diff --git a/drivers/net/caif/Makefile b/drivers/net/caif/Makefile
new file mode 100644
index 000000000000..52b6d1f826f8
--- /dev/null
+++ b/drivers/net/caif/Makefile
@@ -0,0 +1,12 @@
1ifeq ($(CONFIG_CAIF_DEBUG),1)
2CAIF_DBG_FLAGS := -DDEBUG
3endif
4
5KBUILD_EXTRA_SYMBOLS=net/caif/Module.symvers
6
7ccflags-y := $(CAIF_FLAGS) $(CAIF_DBG_FLAGS)
8clean-dirs:= .tmp_versions
9clean-files:= Module.symvers modules.order *.cmd *~ \
10
11# Serial interface
12obj-$(CONFIG_CAIF_TTY) += caif_serial.o
diff --git a/drivers/net/caif/caif_serial.c b/drivers/net/caif/caif_serial.c
new file mode 100644
index 000000000000..3502f607117f
--- /dev/null
+++ b/drivers/net/caif/caif_serial.c
@@ -0,0 +1,441 @@
1/*
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
7#include <linux/init.h>
8#include <linux/version.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>
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
41static int ser_loop;
42module_param(ser_loop, bool, S_IRUGO);
43MODULE_PARM_DESC(ser_loop, "Run in simulated loopback mode.");
44
45static int ser_use_stx = 1;
46module_param(ser_use_stx, bool, S_IRUGO);
47MODULE_PARM_DESC(ser_use_stx, "STX enabled or not.");
48
49static int ser_use_fcs = 1;
50
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 |
91 ser->tty->hw_stopped << 4 |
92 ser->tty->flow_stopped << 3 |
93 ser->tty->packet << 2 |
94 ser->tty->low_latency << 1 |
95 ser->tty->warned;
96}
97static inline void debugfs_init(struct ser_device *ser, struct tty_struct *tty)
98{
99 ser->debugfs_tty_dir =
100 debugfs_create_dir(tty->name, debugfsdir);
101 if (!IS_ERR(ser->debugfs_tty_dir)) {
102 debugfs_create_blob("last_tx_msg", S_IRUSR,
103 ser->debugfs_tty_dir,
104 &ser->tx_blob);
105
106 debugfs_create_blob("last_rx_msg", S_IRUSR,
107 ser->debugfs_tty_dir,
108 &ser->rx_blob);
109
110 debugfs_create_x32("ser_state", S_IRUSR,
111 ser->debugfs_tty_dir,
112 (u32 *)&ser->state);
113
114 debugfs_create_x8("tty_status", S_IRUSR,
115 ser->debugfs_tty_dir,
116 &ser->tty_status);
117
118 }
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;
123}
124
125static inline void debugfs_deinit(struct ser_device *ser)
126{
127 debugfs_remove_recursive(ser->debugfs_tty_dir);
128}
129
130static inline void debugfs_rx(struct ser_device *ser, const u8 *data, int size)
131{
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;
137}
138
139static inline void debugfs_tx(struct ser_device *ser, const u8 *data, int size)
140{
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;
146}
147#else
148static inline void debugfs_init(struct ser_device *ser, struct tty_struct *tty)
149{
150}
151
152static inline void debugfs_deinit(struct ser_device *ser)
153{
154}
155
156static inline void update_tty_status(struct ser_device *ser)
157{
158}
159
160static inline void debugfs_rx(struct ser_device *ser, const u8 *data, int size)
161{
162}
163
164static inline void debugfs_tx(struct ser_device *ser, const u8 *data, int size)
165{
166}
167
168#endif
169
170static void ldisc_receive(struct tty_struct *tty, const u8 *data,
171 char *flags, int count)
172{
173 struct sk_buff *skb = NULL;
174 struct ser_device *ser;
175 int ret;
176 u8 *p;
177 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);
200 BUG_ON(skb == NULL);
201 p = skb_put(skb, count);
202 memcpy(p, data, count);
203
204 skb->protocol = htons(ETH_P_CAIF);
205 skb_reset_mac_header(skb);
206 skb->dev = ser->dev;
207 debugfs_rx(ser, data, count);
208 /* Push received packet up the stack. */
209 ret = netif_rx_ni(skb);
210 if (!ret) {
211 ser->dev->stats.rx_packets++;
212 ser->dev->stats.rx_bytes += count;
213 } else
214 ++ser->dev->stats.rx_dropped;
215 update_tty_status(ser);
216}
217
218static int handle_tx(struct ser_device *ser)
219{
220 struct tty_struct *tty;
221 struct sk_buff *skb;
222 int tty_wr, len, room;
223 tty = ser->tty;
224 ser->tx_started = true;
225
226 /* Enter critical section */
227 if (test_and_set_bit(CAIF_SENDING, &ser->state))
228 return 0;
229
230 /* skb_peek is safe because handle_tx is called after skb_queue_tail */
231 while ((skb = skb_peek(&ser->head)) != NULL) {
232
233 /* Make sure you don't write too much */
234 len = skb->len;
235 room = tty_write_room(tty);
236 if (!room)
237 break;
238 if (room > ser_write_chunk)
239 room = ser_write_chunk;
240 if (len > room)
241 len = room;
242
243 /* Write to tty or loopback */
244 if (!ser_loop) {
245 tty_wr = tty->ops->write(tty, skb->data, len);
246 update_tty_status(ser);
247 } else {
248 tty_wr = len;
249 ldisc_receive(tty, skb->data, NULL, len);
250 }
251 ser->dev->stats.tx_packets++;
252 ser->dev->stats.tx_bytes += tty_wr;
253
254 /* Error on TTY ?! */
255 if (tty_wr < 0)
256 goto error;
257 /* Reduce buffer written, and discard if empty */
258 skb_pull(skb, tty_wr);
259 if (skb->len == 0) {
260 struct sk_buff *tmp = skb_dequeue(&ser->head);
261 BUG_ON(tmp != skb);
262 if (in_interrupt())
263 dev_kfree_skb_irq(skb);
264 else
265 kfree_skb(skb);
266 }
267 }
268 /* Send flow off if queue is empty */
269 if (ser->head.qlen <= SEND_QUEUE_LOW &&
270 test_and_clear_bit(CAIF_FLOW_OFF_SENT, &ser->state) &&
271 ser->common.flowctrl != NULL)
272 ser->common.flowctrl(ser->dev, ON);
273 clear_bit(CAIF_SENDING, &ser->state);
274 return 0;
275error:
276 clear_bit(CAIF_SENDING, &ser->state);
277 return tty_wr;
278}
279
280static int caif_xmit(struct sk_buff *skb, struct net_device *dev)
281{
282 struct ser_device *ser;
283 BUG_ON(dev == NULL);
284 ser = netdev_priv(dev);
285
286 /* Send flow off once, on high water mark */
287 if (ser->head.qlen > SEND_QUEUE_HIGH &&
288 !test_and_set_bit(CAIF_FLOW_OFF_SENT, &ser->state) &&
289 ser->common.flowctrl != NULL)
290
291 ser->common.flowctrl(ser->dev, OFF);
292
293 skb_queue_tail(&ser->head, skb);
294 return handle_tx(ser);
295}
296
297
298static void ldisc_tx_wakeup(struct tty_struct *tty)
299{
300 struct ser_device *ser;
301 ser = tty->disc_data;
302 BUG_ON(ser == NULL);
303 BUG_ON(ser->tty != tty);
304 handle_tx(ser);
305}
306
307
308static int ldisc_open(struct tty_struct *tty)
309{
310 struct ser_device *ser;
311 struct net_device *dev;
312 char name[64];
313 int result;
314
315 sprintf(name, "cf%s", tty->name);
316 dev = alloc_netdev(sizeof(*ser), name, caifdev_setup);
317 ser = netdev_priv(dev);
318 ser->tty = tty;
319 ser->dev = dev;
320 debugfs_init(ser, tty);
321 tty->receive_room = N_TTY_BUF_SIZE;
322 tty->disc_data = ser;
323 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
324 rtnl_lock();
325 result = register_netdevice(dev);
326 if (result) {
327 rtnl_unlock();
328 free_netdev(dev);
329 return -ENODEV;
330 }
331
332 list_add(&ser->node, &ser_list);
333 rtnl_unlock();
334 netif_stop_queue(dev);
335 update_tty_status(ser);
336 return 0;
337}
338
339static void ldisc_close(struct tty_struct *tty)
340{
341 struct ser_device *ser = tty->disc_data;
342 /* Remove may be called inside or outside of rtnl_lock */
343 int islocked = rtnl_is_locked();
344 if (!islocked)
345 rtnl_lock();
346 /* device is freed automagically by net-sysfs */
347 dev_close(ser->dev);
348 unregister_netdevice(ser->dev);
349 list_del(&ser->node);
350 debugfs_deinit(ser);
351 if (!islocked)
352 rtnl_unlock();
353}
354
355/* The line discipline structure. */
356static struct tty_ldisc_ops caif_ldisc = {
357 .owner = THIS_MODULE,
358 .magic = TTY_LDISC_MAGIC,
359 .name = "n_caif",
360 .open = ldisc_open,
361 .close = ldisc_close,
362 .receive_buf = ldisc_receive,
363 .write_wakeup = ldisc_tx_wakeup
364};
365
366static int register_ldisc(void)
367{
368 int result;
369 result = tty_register_ldisc(N_CAIF, &caif_ldisc);
370 if (result < 0) {
371 pr_err("cannot register CAIF ldisc=%d err=%d\n", N_CAIF,
372 result);
373 return result;
374 }
375 return result;
376}
377static const struct net_device_ops netdev_ops = {
378 .ndo_open = caif_net_open,
379 .ndo_stop = caif_net_close,
380 .ndo_start_xmit = caif_xmit
381};
382
383static void caifdev_setup(struct net_device *dev)
384{
385 struct ser_device *serdev = netdev_priv(dev);
386 dev->features = 0;
387 dev->netdev_ops = &netdev_ops;
388 dev->type = ARPHRD_CAIF;
389 dev->flags = IFF_POINTOPOINT | IFF_NOARP;
390 dev->mtu = CAIF_MAX_MTU;
391 dev->hard_header_len = CAIF_NEEDED_HEADROOM;
392 dev->tx_queue_len = 0;
393 dev->destructor = free_netdev;
394 skb_queue_head_init(&serdev->head);
395 serdev->common.link_select = CAIF_LINK_LOW_LATENCY;
396 serdev->common.use_frag = true;
397 serdev->common.use_stx = ser_use_stx;
398 serdev->common.use_fcs = ser_use_fcs;
399 serdev->dev = dev;
400}
401
402
403static int caif_net_open(struct net_device *dev)
404{
405 struct ser_device *ser;
406 ser = netdev_priv(dev);
407 netif_wake_queue(dev);
408 return 0;
409}
410
411static int caif_net_close(struct net_device *dev)
412{
413 netif_stop_queue(dev);
414 return 0;
415}
416
417static int __init caif_ser_init(void)
418{
419 int ret;
420 ret = register_ldisc();
421 debugfsdir = debugfs_create_dir("caif_serial", NULL);
422 return ret;
423}
424
425static void __exit caif_ser_exit(void)
426{
427 struct ser_device *ser = NULL;
428 struct list_head *node;
429 struct list_head *_tmp;
430 list_for_each_safe(node, _tmp, &ser_list) {
431 ser = list_entry(node, struct ser_device, node);
432 dev_close(ser->dev);
433 unregister_netdevice(ser->dev);
434 list_del(node);
435 }
436 tty_unregister_ldisc(N_CAIF);
437 debugfs_remove_recursive(debugfsdir);
438}
439
440module_init(caif_ser_init);
441module_exit(caif_ser_exit);
diff --git a/include/linux/tty.h b/include/linux/tty.h
index 568369a86306..71c7e9c96b23 100644
--- a/include/linux/tty.h
+++ b/include/linux/tty.h
@@ -23,7 +23,7 @@
23 */ 23 */
24#define NR_UNIX98_PTY_DEFAULT 4096 /* Default maximum for Unix98 ptys */ 24#define NR_UNIX98_PTY_DEFAULT 4096 /* Default maximum for Unix98 ptys */
25#define NR_UNIX98_PTY_MAX (1 << MINORBITS) /* Absolute limit */ 25#define NR_UNIX98_PTY_MAX (1 << MINORBITS) /* Absolute limit */
26#define NR_LDISCS 20 26#define NR_LDISCS 21
27 27
28/* line disciplines */ 28/* line disciplines */
29#define N_TTY 0 29#define N_TTY 0
@@ -46,8 +46,8 @@
46#define N_GIGASET_M101 16 /* Siemens Gigaset M101 serial DECT adapter */ 46#define N_GIGASET_M101 16 /* Siemens Gigaset M101 serial DECT adapter */
47#define N_SLCAN 17 /* Serial / USB serial CAN Adaptors */ 47#define N_SLCAN 17 /* Serial / USB serial CAN Adaptors */
48#define N_PPS 18 /* Pulse per Second */ 48#define N_PPS 18 /* Pulse per Second */
49
50#define N_V253 19 /* Codec control over voice modem */ 49#define N_V253 19 /* Codec control over voice modem */
50#define N_CAIF 20 /* CAIF protocol for talking to modems */
51 51
52/* 52/*
53 * This character is the same as _POSIX_VDISABLE: it cannot be used as 53 * This character is the same as _POSIX_VDISABLE: it cannot be used as