1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40#include <linux/module.h>
41#include <linux/moduleparam.h>
42
43#include <linux/uaccess.h>
44#include <linux/bitops.h>
45#include <linux/string.h>
46#include <linux/tty.h>
47#include <linux/errno.h>
48#include <linux/netdevice.h>
49#include <linux/skbuff.h>
50#include <linux/rtnetlink.h>
51#include <linux/if_arp.h>
52#include <linux/if_ether.h>
53#include <linux/sched.h>
54#include <linux/delay.h>
55#include <linux/init.h>
56#include <linux/kernel.h>
57#include <linux/can.h>
58#include <linux/can/skb.h>
59
60static __initconst const char banner[] =
61 KERN_INFO "slcan: serial line CAN interface driver\n";
62
63MODULE_ALIAS_LDISC(N_SLCAN);
64MODULE_DESCRIPTION("serial line CAN interface");
65MODULE_LICENSE("GPL");
66MODULE_AUTHOR("Oliver Hartkopp <socketcan@hartkopp.net>");
67
68#define SLCAN_MAGIC 0x53CA
69
70static int maxdev = 10;
71
72
73module_param(maxdev, int, 0);
74MODULE_PARM_DESC(maxdev, "Maximum number of slcan interfaces");
75
76
77#define SLC_MTU (sizeof("T1111222281122334455667788EA5F\r")+1)
78
79#define SLC_CMD_LEN 1
80#define SLC_SFF_ID_LEN 3
81#define SLC_EFF_ID_LEN 8
82
83struct slcan {
84 int magic;
85
86
87 struct tty_struct *tty;
88 struct net_device *dev;
89 spinlock_t lock;
90
91
92 unsigned char rbuff[SLC_MTU];
93 int rcount;
94 unsigned char xbuff[SLC_MTU];
95 unsigned char *xhead;
96 int xleft;
97
98 unsigned long flags;
99#define SLF_INUSE 0
100#define SLF_ERROR 1
101};
102
103static struct net_device **slcan_devs;
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145static void slc_bump(struct slcan *sl)
146{
147 struct sk_buff *skb;
148 struct can_frame cf;
149 int i, tmp;
150 u32 tmpid;
151 char *cmd = sl->rbuff;
152
153 cf.can_id = 0;
154
155 switch (*cmd) {
156 case 'r':
157 cf.can_id = CAN_RTR_FLAG;
158
159 case 't':
160
161 cf.can_dlc = sl->rbuff[SLC_CMD_LEN + SLC_SFF_ID_LEN];
162 sl->rbuff[SLC_CMD_LEN + SLC_SFF_ID_LEN] = 0;
163
164 cmd += SLC_CMD_LEN + SLC_SFF_ID_LEN + 1;
165 break;
166 case 'R':
167 cf.can_id = CAN_RTR_FLAG;
168
169 case 'T':
170 cf.can_id |= CAN_EFF_FLAG;
171
172 cf.can_dlc = sl->rbuff[SLC_CMD_LEN + SLC_EFF_ID_LEN];
173 sl->rbuff[SLC_CMD_LEN + SLC_EFF_ID_LEN] = 0;
174
175 cmd += SLC_CMD_LEN + SLC_EFF_ID_LEN + 1;
176 break;
177 default:
178 return;
179 }
180
181 if (kstrtou32(sl->rbuff + SLC_CMD_LEN, 16, &tmpid))
182 return;
183
184 cf.can_id |= tmpid;
185
186
187 if (cf.can_dlc >= '0' && cf.can_dlc < '9')
188 cf.can_dlc -= '0';
189 else
190 return;
191
192 *(u64 *) (&cf.data) = 0;
193
194
195 if (!(cf.can_id & CAN_RTR_FLAG)) {
196 for (i = 0; i < cf.can_dlc; i++) {
197 tmp = hex_to_bin(*cmd++);
198 if (tmp < 0)
199 return;
200 cf.data[i] = (tmp << 4);
201 tmp = hex_to_bin(*cmd++);
202 if (tmp < 0)
203 return;
204 cf.data[i] |= tmp;
205 }
206 }
207
208 skb = dev_alloc_skb(sizeof(struct can_frame) +
209 sizeof(struct can_skb_priv));
210 if (!skb)
211 return;
212
213 skb->dev = sl->dev;
214 skb->protocol = htons(ETH_P_CAN);
215 skb->pkt_type = PACKET_BROADCAST;
216 skb->ip_summed = CHECKSUM_UNNECESSARY;
217
218 can_skb_reserve(skb);
219 can_skb_prv(skb)->ifindex = sl->dev->ifindex;
220
221 memcpy(skb_put(skb, sizeof(struct can_frame)),
222 &cf, sizeof(struct can_frame));
223 netif_rx_ni(skb);
224
225 sl->dev->stats.rx_packets++;
226 sl->dev->stats.rx_bytes += cf.can_dlc;
227}
228
229
230static void slcan_unesc(struct slcan *sl, unsigned char s)
231{
232 if ((s == '\r') || (s == '\a')) {
233 if (!test_and_clear_bit(SLF_ERROR, &sl->flags) &&
234 (sl->rcount > 4)) {
235 slc_bump(sl);
236 }
237 sl->rcount = 0;
238 } else {
239 if (!test_bit(SLF_ERROR, &sl->flags)) {
240 if (sl->rcount < SLC_MTU) {
241 sl->rbuff[sl->rcount++] = s;
242 return;
243 } else {
244 sl->dev->stats.rx_over_errors++;
245 set_bit(SLF_ERROR, &sl->flags);
246 }
247 }
248 }
249}
250
251
252
253
254
255
256static void slc_encaps(struct slcan *sl, struct can_frame *cf)
257{
258 int actual, i;
259 unsigned char *pos;
260 unsigned char *endpos;
261 canid_t id = cf->can_id;
262
263 pos = sl->xbuff;
264
265 if (cf->can_id & CAN_RTR_FLAG)
266 *pos = 'R';
267 else
268 *pos = 'T';
269
270
271 if (cf->can_id & CAN_EFF_FLAG) {
272 id &= CAN_EFF_MASK;
273 endpos = pos + SLC_EFF_ID_LEN;
274 } else {
275 *pos |= 0x20;
276 id &= CAN_SFF_MASK;
277 endpos = pos + SLC_SFF_ID_LEN;
278 }
279
280
281 pos++;
282 while (endpos >= pos) {
283 *endpos-- = hex_asc_upper[id & 0xf];
284 id >>= 4;
285 }
286
287 pos += (cf->can_id & CAN_EFF_FLAG) ? SLC_EFF_ID_LEN : SLC_SFF_ID_LEN;
288
289 *pos++ = cf->can_dlc + '0';
290
291
292 if (!(cf->can_id & CAN_RTR_FLAG)) {
293 for (i = 0; i < cf->can_dlc; i++)
294 pos = hex_byte_pack_upper(pos, cf->data[i]);
295 }
296
297 *pos++ = '\r';
298
299
300
301
302
303
304
305
306
307 set_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
308 actual = sl->tty->ops->write(sl->tty, sl->xbuff, pos - sl->xbuff);
309 sl->xleft = (pos - sl->xbuff) - actual;
310 sl->xhead = sl->xbuff + actual;
311 sl->dev->stats.tx_bytes += cf->can_dlc;
312}
313
314
315
316
317
318static void slcan_write_wakeup(struct tty_struct *tty)
319{
320 int actual;
321 struct slcan *sl = (struct slcan *) tty->disc_data;
322
323
324 if (!sl || sl->magic != SLCAN_MAGIC || !netif_running(sl->dev))
325 return;
326
327 spin_lock(&sl->lock);
328 if (sl->xleft <= 0) {
329
330
331 sl->dev->stats.tx_packets++;
332 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
333 spin_unlock(&sl->lock);
334 netif_wake_queue(sl->dev);
335 return;
336 }
337
338 actual = tty->ops->write(tty, sl->xhead, sl->xleft);
339 sl->xleft -= actual;
340 sl->xhead += actual;
341 spin_unlock(&sl->lock);
342}
343
344
345static netdev_tx_t slc_xmit(struct sk_buff *skb, struct net_device *dev)
346{
347 struct slcan *sl = netdev_priv(dev);
348
349 if (skb->len != sizeof(struct can_frame))
350 goto out;
351
352 spin_lock(&sl->lock);
353 if (!netif_running(dev)) {
354 spin_unlock(&sl->lock);
355 printk(KERN_WARNING "%s: xmit: iface is down\n", dev->name);
356 goto out;
357 }
358 if (sl->tty == NULL) {
359 spin_unlock(&sl->lock);
360 goto out;
361 }
362
363 netif_stop_queue(sl->dev);
364 slc_encaps(sl, (struct can_frame *) skb->data);
365 spin_unlock(&sl->lock);
366
367out:
368 kfree_skb(skb);
369 return NETDEV_TX_OK;
370}
371
372
373
374
375
376
377
378static int slc_close(struct net_device *dev)
379{
380 struct slcan *sl = netdev_priv(dev);
381
382 spin_lock_bh(&sl->lock);
383 if (sl->tty) {
384
385 clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
386 }
387 netif_stop_queue(dev);
388 sl->rcount = 0;
389 sl->xleft = 0;
390 spin_unlock_bh(&sl->lock);
391
392 return 0;
393}
394
395
396static int slc_open(struct net_device *dev)
397{
398 struct slcan *sl = netdev_priv(dev);
399
400 if (sl->tty == NULL)
401 return -ENODEV;
402
403 sl->flags &= (1 << SLF_INUSE);
404 netif_start_queue(dev);
405 return 0;
406}
407
408
409static void slc_free_netdev(struct net_device *dev)
410{
411 int i = dev->base_addr;
412 free_netdev(dev);
413 slcan_devs[i] = NULL;
414}
415
416static const struct net_device_ops slc_netdev_ops = {
417 .ndo_open = slc_open,
418 .ndo_stop = slc_close,
419 .ndo_start_xmit = slc_xmit,
420};
421
422static void slc_setup(struct net_device *dev)
423{
424 dev->netdev_ops = &slc_netdev_ops;
425 dev->destructor = slc_free_netdev;
426
427 dev->hard_header_len = 0;
428 dev->addr_len = 0;
429 dev->tx_queue_len = 10;
430
431 dev->mtu = sizeof(struct can_frame);
432 dev->type = ARPHRD_CAN;
433
434
435 dev->flags = IFF_NOARP;
436 dev->features = NETIF_F_HW_CSUM;
437}
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452static void slcan_receive_buf(struct tty_struct *tty,
453 const unsigned char *cp, char *fp, int count)
454{
455 struct slcan *sl = (struct slcan *) tty->disc_data;
456
457 if (!sl || sl->magic != SLCAN_MAGIC || !netif_running(sl->dev))
458 return;
459
460
461 while (count--) {
462 if (fp && *fp++) {
463 if (!test_and_set_bit(SLF_ERROR, &sl->flags))
464 sl->dev->stats.rx_errors++;
465 cp++;
466 continue;
467 }
468 slcan_unesc(sl, *cp++);
469 }
470}
471
472
473
474
475
476
477static void slc_sync(void)
478{
479 int i;
480 struct net_device *dev;
481 struct slcan *sl;
482
483 for (i = 0; i < maxdev; i++) {
484 dev = slcan_devs[i];
485 if (dev == NULL)
486 break;
487
488 sl = netdev_priv(dev);
489 if (sl->tty)
490 continue;
491 if (dev->flags & IFF_UP)
492 dev_close(dev);
493 }
494}
495
496
497static struct slcan *slc_alloc(dev_t line)
498{
499 int i;
500 char name[IFNAMSIZ];
501 struct net_device *dev = NULL;
502 struct slcan *sl;
503
504 for (i = 0; i < maxdev; i++) {
505 dev = slcan_devs[i];
506 if (dev == NULL)
507 break;
508
509 }
510
511
512 if (i >= maxdev)
513 return NULL;
514
515 sprintf(name, "slcan%d", i);
516 dev = alloc_netdev(sizeof(*sl), name, slc_setup);
517 if (!dev)
518 return NULL;
519
520 dev->base_addr = i;
521 sl = netdev_priv(dev);
522
523
524 sl->magic = SLCAN_MAGIC;
525 sl->dev = dev;
526 spin_lock_init(&sl->lock);
527 slcan_devs[i] = dev;
528
529 return sl;
530}
531
532
533
534
535
536
537
538
539
540
541
542static int slcan_open(struct tty_struct *tty)
543{
544 struct slcan *sl;
545 int err;
546
547 if (!capable(CAP_NET_ADMIN))
548 return -EPERM;
549
550 if (tty->ops->write == NULL)
551 return -EOPNOTSUPP;
552
553
554
555
556
557 rtnl_lock();
558
559
560 slc_sync();
561
562 sl = tty->disc_data;
563
564 err = -EEXIST;
565
566 if (sl && sl->magic == SLCAN_MAGIC)
567 goto err_exit;
568
569
570 err = -ENFILE;
571 sl = slc_alloc(tty_devnum(tty));
572 if (sl == NULL)
573 goto err_exit;
574
575 sl->tty = tty;
576 tty->disc_data = sl;
577
578 if (!test_bit(SLF_INUSE, &sl->flags)) {
579
580 sl->rcount = 0;
581 sl->xleft = 0;
582
583 set_bit(SLF_INUSE, &sl->flags);
584
585 err = register_netdevice(sl->dev);
586 if (err)
587 goto err_free_chan;
588 }
589
590
591 rtnl_unlock();
592 tty->receive_room = 65536;
593
594
595 return 0;
596
597err_free_chan:
598 sl->tty = NULL;
599 tty->disc_data = NULL;
600 clear_bit(SLF_INUSE, &sl->flags);
601
602err_exit:
603 rtnl_unlock();
604
605
606 return err;
607}
608
609
610
611
612
613
614
615
616
617static void slcan_close(struct tty_struct *tty)
618{
619 struct slcan *sl = (struct slcan *) tty->disc_data;
620
621
622 if (!sl || sl->magic != SLCAN_MAGIC || sl->tty != tty)
623 return;
624
625 tty->disc_data = NULL;
626 sl->tty = NULL;
627
628
629 unregister_netdev(sl->dev);
630
631}
632
633static int slcan_hangup(struct tty_struct *tty)
634{
635 slcan_close(tty);
636 return 0;
637}
638
639
640static int slcan_ioctl(struct tty_struct *tty, struct file *file,
641 unsigned int cmd, unsigned long arg)
642{
643 struct slcan *sl = (struct slcan *) tty->disc_data;
644 unsigned int tmp;
645
646
647 if (!sl || sl->magic != SLCAN_MAGIC)
648 return -EINVAL;
649
650 switch (cmd) {
651 case SIOCGIFNAME:
652 tmp = strlen(sl->dev->name) + 1;
653 if (copy_to_user((void __user *)arg, sl->dev->name, tmp))
654 return -EFAULT;
655 return 0;
656
657 case SIOCSIFHWADDR:
658 return -EINVAL;
659
660 default:
661 return tty_mode_ioctl(tty, file, cmd, arg);
662 }
663}
664
665static struct tty_ldisc_ops slc_ldisc = {
666 .owner = THIS_MODULE,
667 .magic = TTY_LDISC_MAGIC,
668 .name = "slcan",
669 .open = slcan_open,
670 .close = slcan_close,
671 .hangup = slcan_hangup,
672 .ioctl = slcan_ioctl,
673 .receive_buf = slcan_receive_buf,
674 .write_wakeup = slcan_write_wakeup,
675};
676
677static int __init slcan_init(void)
678{
679 int status;
680
681 if (maxdev < 4)
682 maxdev = 4;
683
684 printk(banner);
685 printk(KERN_INFO "slcan: %d dynamic interface channels.\n", maxdev);
686
687 slcan_devs = kzalloc(sizeof(struct net_device *)*maxdev, GFP_KERNEL);
688 if (!slcan_devs)
689 return -ENOMEM;
690
691
692 status = tty_register_ldisc(N_SLCAN, &slc_ldisc);
693 if (status) {
694 printk(KERN_ERR "slcan: can't register line discipline\n");
695 kfree(slcan_devs);
696 }
697 return status;
698}
699
700static void __exit slcan_exit(void)
701{
702 int i;
703 struct net_device *dev;
704 struct slcan *sl;
705 unsigned long timeout = jiffies + HZ;
706 int busy = 0;
707
708 if (slcan_devs == NULL)
709 return;
710
711
712
713 do {
714 if (busy)
715 msleep_interruptible(100);
716
717 busy = 0;
718 for (i = 0; i < maxdev; i++) {
719 dev = slcan_devs[i];
720 if (!dev)
721 continue;
722 sl = netdev_priv(dev);
723 spin_lock_bh(&sl->lock);
724 if (sl->tty) {
725 busy++;
726 tty_hangup(sl->tty);
727 }
728 spin_unlock_bh(&sl->lock);
729 }
730 } while (busy && time_before(jiffies, timeout));
731
732
733
734
735 for (i = 0; i < maxdev; i++) {
736 dev = slcan_devs[i];
737 if (!dev)
738 continue;
739 slcan_devs[i] = NULL;
740
741 sl = netdev_priv(dev);
742 if (sl->tty) {
743 printk(KERN_ERR "%s: tty discipline still running\n",
744 dev->name);
745
746 dev->destructor = NULL;
747 }
748
749 unregister_netdev(dev);
750 }
751
752 kfree(slcan_devs);
753 slcan_devs = NULL;
754
755 i = tty_unregister_ldisc(N_SLCAN);
756 if (i)
757 printk(KERN_ERR "slcan: can't unregister ldisc (err %d)\n", i);
758}
759
760module_init(slcan_init);
761module_exit(slcan_exit);
762