CAIF: fix tty->warned build error
[deliverable/linux.git] / drivers / net / caif / caif_serial.c
CommitLineData
9b27105b
SB
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
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");
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
eb939922 41static bool ser_loop;
9b27105b
SB
42module_param(ser_loop, bool, S_IRUGO);
43MODULE_PARM_DESC(ser_loop, "Run in simulated loopback mode.");
44
eb939922 45static bool ser_use_stx = true;
9b27105b
SB
46module_param(ser_use_stx, bool, S_IRUGO);
47MODULE_PARM_DESC(ser_use_stx, "STX enabled or not.");
48
eb939922 49static bool ser_use_fcs = true;
9b27105b
SB
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 |
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;
174 u8 *p;
c1f8fc57 175
9b27105b
SB
176 ser = tty->disc_data;
177
178 /*
179 * NOTE: flags may contain information about break or overrun.
180 * This is not yet handled.
181 */
182
183
184 /*
185 * Workaround for garbage at start of transmission,
186 * only enable if STX handling is not enabled.
187 */
188 if (!ser->common.use_stx && !ser->tx_started) {
189 dev_info(&ser->dev->dev,
190 "Bytes received before initial transmission -"
191 "bytes discarded.\n");
192 return;
193 }
194
195 BUG_ON(ser->dev == NULL);
196
197 /* Get a suitable caif packet and copy in data. */
198 skb = netdev_alloc_skb(ser->dev, count+1);
d3f744e0
SB
199 if (skb == NULL)
200 return;
9b27105b
SB
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;
c1f8fc57 223
9b27105b
SB
224 tty = ser->tty;
225 ser->tx_started = true;
226
227 /* Enter critical section */
228 if (test_and_set_bit(CAIF_SENDING, &ser->state))
229 return 0;
230
231 /* skb_peek is safe because handle_tx is called after skb_queue_tail */
232 while ((skb = skb_peek(&ser->head)) != NULL) {
233
234 /* Make sure you don't write too much */
235 len = skb->len;
236 room = tty_write_room(tty);
237 if (!room)
238 break;
239 if (room > ser_write_chunk)
240 room = ser_write_chunk;
241 if (len > room)
242 len = room;
243
244 /* Write to tty or loopback */
245 if (!ser_loop) {
246 tty_wr = tty->ops->write(tty, skb->data, len);
247 update_tty_status(ser);
248 } else {
249 tty_wr = len;
250 ldisc_receive(tty, skb->data, NULL, len);
251 }
252 ser->dev->stats.tx_packets++;
253 ser->dev->stats.tx_bytes += tty_wr;
254
255 /* Error on TTY ?! */
256 if (tty_wr < 0)
257 goto error;
258 /* Reduce buffer written, and discard if empty */
259 skb_pull(skb, tty_wr);
260 if (skb->len == 0) {
261 struct sk_buff *tmp = skb_dequeue(&ser->head);
f84ea779 262 WARN_ON(tmp != skb);
9b27105b
SB
263 if (in_interrupt())
264 dev_kfree_skb_irq(skb);
265 else
266 kfree_skb(skb);
267 }
268 }
269 /* Send flow off if queue is empty */
270 if (ser->head.qlen <= SEND_QUEUE_LOW &&
271 test_and_clear_bit(CAIF_FLOW_OFF_SENT, &ser->state) &&
272 ser->common.flowctrl != NULL)
273 ser->common.flowctrl(ser->dev, ON);
274 clear_bit(CAIF_SENDING, &ser->state);
275 return 0;
276error:
277 clear_bit(CAIF_SENDING, &ser->state);
278 return tty_wr;
279}
280
281static int caif_xmit(struct sk_buff *skb, struct net_device *dev)
282{
283 struct ser_device *ser;
c1f8fc57 284
9b27105b
SB
285 BUG_ON(dev == NULL);
286 ser = netdev_priv(dev);
287
288 /* Send flow off once, on high water mark */
289 if (ser->head.qlen > SEND_QUEUE_HIGH &&
290 !test_and_set_bit(CAIF_FLOW_OFF_SENT, &ser->state) &&
291 ser->common.flowctrl != NULL)
292
293 ser->common.flowctrl(ser->dev, OFF);
294
295 skb_queue_tail(&ser->head, skb);
296 return handle_tx(ser);
297}
298
299
300static void ldisc_tx_wakeup(struct tty_struct *tty)
301{
302 struct ser_device *ser;
c1f8fc57 303
9b27105b
SB
304 ser = tty->disc_data;
305 BUG_ON(ser == NULL);
f84ea779 306 WARN_ON(ser->tty != tty);
9b27105b
SB
307 handle_tx(ser);
308}
309
310
311static int ldisc_open(struct tty_struct *tty)
312{
313 struct ser_device *ser;
314 struct net_device *dev;
315 char name[64];
316 int result;
317
c93f0940
AC
318 /* No write no play */
319 if (tty->ops->write == NULL)
320 return -EOPNOTSUPP;
d3f744e0
SB
321 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_TTY_CONFIG))
322 return -EPERM;
c93f0940 323
9b27105b
SB
324 sprintf(name, "cf%s", tty->name);
325 dev = alloc_netdev(sizeof(*ser), name, caifdev_setup);
c66b9b7d
AC
326 if (!dev)
327 return -ENOMEM;
328
9b27105b 329 ser = netdev_priv(dev);
e31d5a05 330 ser->tty = tty_kref_get(tty);
9b27105b
SB
331 ser->dev = dev;
332 debugfs_init(ser, tty);
333 tty->receive_room = N_TTY_BUF_SIZE;
334 tty->disc_data = ser;
335 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
336 rtnl_lock();
337 result = register_netdevice(dev);
338 if (result) {
339 rtnl_unlock();
340 free_netdev(dev);
341 return -ENODEV;
342 }
343
344 list_add(&ser->node, &ser_list);
345 rtnl_unlock();
346 netif_stop_queue(dev);
347 update_tty_status(ser);
348 return 0;
349}
350
351static void ldisc_close(struct tty_struct *tty)
352{
353 struct ser_device *ser = tty->disc_data;
354 /* Remove may be called inside or outside of rtnl_lock */
355 int islocked = rtnl_is_locked();
c1f8fc57 356
9b27105b
SB
357 if (!islocked)
358 rtnl_lock();
359 /* device is freed automagically by net-sysfs */
360 dev_close(ser->dev);
361 unregister_netdevice(ser->dev);
362 list_del(&ser->node);
363 debugfs_deinit(ser);
e31d5a05 364 tty_kref_put(ser->tty);
9b27105b
SB
365 if (!islocked)
366 rtnl_unlock();
367}
368
369/* The line discipline structure. */
370static struct tty_ldisc_ops caif_ldisc = {
371 .owner = THIS_MODULE,
372 .magic = TTY_LDISC_MAGIC,
373 .name = "n_caif",
374 .open = ldisc_open,
375 .close = ldisc_close,
376 .receive_buf = ldisc_receive,
377 .write_wakeup = ldisc_tx_wakeup
378};
379
380static int register_ldisc(void)
381{
382 int result;
c1f8fc57 383
9b27105b
SB
384 result = tty_register_ldisc(N_CAIF, &caif_ldisc);
385 if (result < 0) {
386 pr_err("cannot register CAIF ldisc=%d err=%d\n", N_CAIF,
387 result);
388 return result;
389 }
390 return result;
391}
392static const struct net_device_ops netdev_ops = {
393 .ndo_open = caif_net_open,
394 .ndo_stop = caif_net_close,
395 .ndo_start_xmit = caif_xmit
396};
397
398static void caifdev_setup(struct net_device *dev)
399{
400 struct ser_device *serdev = netdev_priv(dev);
c1f8fc57 401
9b27105b
SB
402 dev->features = 0;
403 dev->netdev_ops = &netdev_ops;
404 dev->type = ARPHRD_CAIF;
405 dev->flags = IFF_POINTOPOINT | IFF_NOARP;
406 dev->mtu = CAIF_MAX_MTU;
9b27105b
SB
407 dev->tx_queue_len = 0;
408 dev->destructor = free_netdev;
409 skb_queue_head_init(&serdev->head);
410 serdev->common.link_select = CAIF_LINK_LOW_LATENCY;
411 serdev->common.use_frag = true;
412 serdev->common.use_stx = ser_use_stx;
413 serdev->common.use_fcs = ser_use_fcs;
414 serdev->dev = dev;
415}
416
417
418static int caif_net_open(struct net_device *dev)
419{
9b27105b
SB
420 netif_wake_queue(dev);
421 return 0;
422}
423
424static int caif_net_close(struct net_device *dev)
425{
426 netif_stop_queue(dev);
427 return 0;
428}
429
430static int __init caif_ser_init(void)
431{
432 int ret;
c1f8fc57 433
9b27105b
SB
434 ret = register_ldisc();
435 debugfsdir = debugfs_create_dir("caif_serial", NULL);
436 return ret;
437}
438
439static void __exit caif_ser_exit(void)
440{
441 struct ser_device *ser = NULL;
442 struct list_head *node;
443 struct list_head *_tmp;
c1f8fc57 444
9b27105b
SB
445 list_for_each_safe(node, _tmp, &ser_list) {
446 ser = list_entry(node, struct ser_device, node);
447 dev_close(ser->dev);
448 unregister_netdevice(ser->dev);
449 list_del(node);
450 }
451 tty_unregister_ldisc(N_CAIF);
452 debugfs_remove_recursive(debugfsdir);
453}
454
455module_init(caif_ser_init);
456module_exit(caif_ser_exit);
This page took 0.251049 seconds and 5 git commands to generate.