4 * Copyright (c) 2002-2005 Volkswagen Group Electronic Research
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, the following disclaimer and
12 * the referenced file 'COPYING'.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of Volkswagen nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * Alternatively, provided that this notice is retained in full, this
21 * software may be distributed under the terms of the GNU General
22 * Public License ("GPL") version 2 as distributed in the 'COPYING'
23 * file from the main directory of the linux kernel source.
25 * The provided data structures and external interfaces from this code
26 * are not restricted to be used by modules with a GPL compatible license.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
41 * Send feedback to <llcf@volkswagen.de>
45 #include <linux/module.h>
46 #include <linux/version.h>
47 #include <linux/kmod.h>
48 #include <linux/init.h>
49 #include <linux/spinlock.h>
50 #include <linux/socket.h>
51 #include <linux/skbuff.h>
52 #include <linux/net.h>
53 #include <linux/netdevice.h>
55 #include <asm/uaccess.h>
57 #include <linux/can/af_can.h>
63 #define NAME "Volkswagen AG - Low Level CAN Framework (LLCF)"
64 #define IDENT "af_can"
65 static __initdata const char banner[] = BANNER(NAME);
67 MODULE_DESCRIPTION(NAME);
68 MODULE_LICENSE("Dual BSD/GPL");
69 MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>, "
70 "Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
72 int stats_timer = 1; /* default: on */
73 module_param(stats_timer, int, S_IRUGO);
77 module_param(debug, int, S_IRUGO);
78 #define DBG(args...) (debug & 1 ? \
79 (printk(KERN_DEBUG "CAN %s: ", __func__), \
81 #define DBG_FRAME(args...) (debug & 2 ? can_debug_cframe(args) : 0)
82 #define DBG_SKB(skb) (debug & 4 ? can_debug_skb(skb) : 0)
85 #define DBG_FRAME(args...)
89 static __init int can_init(void);
90 static __exit void can_exit(void);
92 static int can_create(struct socket *sock, int protocol);
93 static int can_notifier(struct notifier_block *nb,
94 unsigned long msg, void *data);
95 static int can_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg);
96 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
97 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
98 struct packet_type *pt, struct net_device *orig_dev);
100 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
101 struct packet_type *pt);
103 static int can_rcv_filter(struct rcv_dev_list *q, struct sk_buff *skb);
104 static struct rcv_list **find_rcv_list(canid_t *can_id, canid_t *mask, struct net_device *dev);
106 struct notifier_list {
107 struct notifier_list *next;
108 struct net_device *dev;
109 void (*func)(unsigned long msg, void *data);
113 static struct notifier_list *nlist;
115 struct rcv_dev_list *rx_dev_list;
116 struct rcv_dev_list rx_alldev_list;
117 rwlock_t rcv_lists_lock = RW_LOCK_UNLOCKED;
119 static struct packet_type can_packet = {
120 .type = __constant_htons(ETH_P_CAN),
125 static struct net_proto_family can_family_ops = {
127 .create = can_create,
128 .owner = THIS_MODULE,
131 static struct notifier_block can_netdev_notifier = {
132 .notifier_call = can_notifier,
135 static struct can_proto *proto_tab[CAN_MAX];
137 extern struct timer_list stattimer; /* timer for statistics update */
138 extern struct s_stats stats; /* statistics */
139 extern struct s_pstats pstats;
141 module_init(can_init);
142 module_exit(can_exit);
144 /**************************************************/
145 /* af_can module init/exit functions */
146 /**************************************************/
148 static __init int can_init(void)
153 /* statistics init */
154 init_timer(&stattimer);
160 /* protocol register */
161 sock_register(&can_family_ops);
162 register_netdevice_notifier(&can_netdev_notifier);
163 dev_add_pack(&can_packet);
168 static __exit void can_exit(void)
171 /* stop statistics timer */
172 del_timer(&stattimer);
178 /* protocol unregister */
179 dev_remove_pack(&can_packet);
180 unregister_netdevice_notifier(&can_netdev_notifier);
181 sock_unregister(PF_CAN);
184 /**************************************************/
185 /* af_can protocol functions */
186 /**************************************************/
188 void can_proto_register(int proto, struct can_proto *cp)
190 if (proto < 0 || proto >= CAN_MAX) {
191 printk(KERN_ERR "CAN: protocol number %d out of range\n", proto);
194 if (proto_tab[proto]) {
195 printk(KERN_ERR "CAN: protocol %d already registered\n", proto);
199 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
200 if (proto_register(cp->prot, 0) != 0) {
204 proto_tab[proto] = cp;
206 /* use our generic ioctl function if the module doesn't bring its own */
208 cp->ops->ioctl = can_ioctl;
211 void can_proto_unregister(int proto)
213 struct can_proto *cp;
215 if (!(cp = proto_tab[proto])) {
216 printk(KERN_ERR "CAN: protocol %d is not registered\n", proto);
219 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
220 proto_unregister(cp->prot);
222 proto_tab[proto] = NULL;
225 void can_dev_register(struct net_device *dev,
226 void (*func)(unsigned long msg, void *), void *data)
228 struct notifier_list *p = kmalloc(GFP_KERNEL, sizeof(*p));
230 DBG("called for %s\n", dev->name);
241 void can_dev_unregister(struct net_device *dev,
242 void (*func)(unsigned long msg, void *), void *data)
244 struct notifier_list *p, **q;
246 DBG("called for %s\n", dev->name);
248 for (q = &nlist; p = *q; q = &p->next) {
249 if (p->dev == dev && p->func == func && p->data == data) {
257 /**************************************************/
258 /* af_can socket functions */
259 /**************************************************/
261 static void can_sock_destruct(struct sock *sk)
263 DBG("called for sock %p\n", sk);
265 skb_queue_purge(&sk->sk_receive_queue);
267 kfree(sk->sk_protinfo);
270 static int can_create(struct socket *sock, int protocol)
273 struct can_proto *cp;
275 DBG("socket %p, type %d, proto %d\n", sock, sock->type, protocol);
277 sock->state = SS_UNCONNECTED;
279 switch (sock->type) {
291 return -EPROTONOSUPPORT;
301 return -EPROTONOSUPPORT;
309 return -EPROTONOSUPPORT;
313 return -ESOCKTNOSUPPORT;
317 DBG("looking up proto %d in proto_tab[]\n", protocol);
319 /* try to load protocol module, when CONFIG_KMOD is defined */
320 if (!proto_tab[protocol]) {
321 char module_name[30];
322 sprintf(module_name, "can-proto-%d", protocol);
323 if (request_module(module_name) == -ENOSYS)
324 printk(KERN_INFO "af_can: request_module(%s) not implemented.\n",
328 /* check for success */
329 if (!(cp = proto_tab[protocol]))
330 return -EPROTONOSUPPORT;
334 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
335 sk = sk_alloc(PF_CAN, GFP_KERNEL, cp->prot, 1);
339 sk = sk_alloc(PF_CAN, GFP_KERNEL, 1, 0);
343 !(sk->sk_protinfo = kmalloc(cp->obj_size, GFP_KERNEL))) {
347 sk_set_owner(sk, proto_tab[protocol]->owner);
349 sock_init_data(sock, sk);
350 sk->sk_destruct = can_sock_destruct;
352 DBG("created sock: %p\n", sk);
360 static int can_notifier(struct notifier_block *nb,
361 unsigned long msg, void *data)
363 struct net_device *dev = (struct net_device *)data;
364 struct notifier_list *p;
366 DBG("called for %s, msg = %lu\n", dev->name, msg);
368 for (p = nlist; p; p = p->next) {
370 p->func(msg, p->data);
375 static int can_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
377 struct sock *sk = sock->sk;
381 return sock_get_timestamp(sk, (struct timeval __user *)arg);
383 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
386 return dev_ioctl(cmd, (void *)arg);
392 /**************************************************/
394 /**************************************************/
396 int can_send(struct sk_buff *skb)
398 struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
401 if (!(skb->dev->flags & IFF_UP))
403 else if ((err = dev_queue_xmit(skb)) > 0) /* send to netdevice */
404 err = net_xmit_errno(err);
406 /* update statistics */
408 stats.tx_frames_delta++;
410 newskb->protocol = htons(ETH_P_CAN);
411 newskb->ip_summed = CHECKSUM_UNNECESSARY;
412 netif_rx(newskb); /* local loopback */
417 /**************************************************/
419 /**************************************************/
421 void can_rx_register(struct net_device *dev, canid_t can_id, canid_t mask,
422 void (*func)(struct sk_buff *, void *), void *data, char *ident)
424 struct rcv_list *p, **q;
425 struct rcv_dev_list *d;
427 DBG("dev %p, id %03X, mask %03X, callback %p, data %p, ident %s\n",
428 dev, can_id, mask, func, data, ident);
430 write_lock(&rcv_lists_lock);
432 q = find_rcv_list(&can_id, &mask, dev);
435 printk(KERN_ERR "CAN: receive list not found for "
436 "dev %s, id %03X, mask %03X, ident %s\n",
437 dev->name, can_id, mask, ident);
441 /* insert (dev,canid,mask) -> (func,data) */
442 if (!(p = kmalloc(sizeof(struct rcv_list), GFP_KERNEL)))
457 for (d = rx_dev_list; d; d = d->next)
462 pstats.rcv_entries++;
463 if (pstats.rcv_entries_max < pstats.rcv_entries)
464 pstats.rcv_entries_max = pstats.rcv_entries;
467 write_unlock(&rcv_lists_lock);
470 void can_rx_unregister(struct net_device *dev, canid_t can_id, canid_t mask,
471 void (*func)(struct sk_buff *, void *), void *data)
473 struct rcv_list *p, **q;
474 struct rcv_dev_list *d;
476 DBG("dev %p, id %03X, mask %03X, callback %p, data %p\n",
477 dev, can_id, mask, func, data);
479 write_lock(&rcv_lists_lock);
481 q = find_rcv_list(&can_id, &mask, dev);
484 printk(KERN_ERR "CAN: receive list not found for "
485 "dev %s, id %03X, mask %03X\n", dev->name, can_id, mask);
489 for (; p = *q; q = &p->next) {
490 if (p->can_id == can_id && p->mask == mask
491 && p->func == func && p->data == data)
496 printk(KERN_ERR "CAN: receive list entry not found for "
497 "dev %s, id %03X, mask %03X\n", dev->name, can_id, mask);
504 if (pstats.rcv_entries > 0)
505 pstats.rcv_entries--;
510 for (d = rx_dev_list; d; d = d->next)
516 d->dev = NULL; /* mark unused */
519 write_unlock(&rcv_lists_lock);
522 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
523 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
524 struct packet_type *pt, struct net_device *orig_dev)
526 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
527 struct packet_type *pt)
530 struct rcv_dev_list *q;
533 DBG("received skbuff on device %s, ptype %04x\n",
534 dev->name, ntohs(pt->type));
536 DBG_FRAME("af_can: can_rcv: received CAN frame",
537 (struct can_frame *)skb->data);
539 /* update statistics */
541 stats.rx_frames_delta++;
543 matches = can_rcv_filter(&rx_alldev_list, skb);
545 /* find receive list for this device */
546 for (q = rx_dev_list; q; q = q->next)
551 matches += can_rcv_filter(q, skb);
553 DBG("freeing skbuff %p\n", skb);
558 stats.matches_delta++;
565 static inline void deliver(struct sk_buff *skb, struct rcv_list *p)
567 struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
568 DBG("skbuff %p cloned to %p\n", skb, clone);
570 p->func(clone, p->data);
571 p->matches++; /* update specific statistics */
575 static int can_rcv_filter(struct rcv_dev_list *q, struct sk_buff *skb)
579 struct can_frame *cf = (struct can_frame*)skb->data;
580 canid_t can_id = cf->can_id;
585 if (can_id & CAN_ERR_FLAG) {
586 /* check for error frame entries only */
587 for (p = q->rx_err; p; p = p->next) {
588 if (can_id & p->mask) {
589 DBG("match on rx_err skbuff %p\n", skb);
597 /* check for unfiltered entries */
598 for (p = q->rx_all; p; p = p->next) {
599 DBG("match on rx_all skbuff %p\n", skb);
604 /* check for can_id/mask entries */
605 for (p = q->rx_fil; p; p = p->next) {
606 if ((can_id & p->mask) == p->can_id) {
607 DBG("match on rx_fil skbuff %p\n", skb);
613 /* check for inverted can_id/mask entries */
614 for (p = q->rx_inv; p; p = p->next) {
615 if ((can_id & p->mask) != p->can_id) {
616 DBG("match on rx_inv skbuff %p\n", skb);
622 /* check CAN_ID specific entries */
623 if (can_id & CAN_EFF_FLAG) {
624 for (p = q->rx_eff; p; p = p->next) {
625 if (p->can_id == can_id) {
626 DBG("match on rx_eff skbuff %p\n", skb);
632 for (p = q->rx_sff[can_id & CAN_SFF_MASK]; p; p = p->next) {
633 DBG("match on rx_sff skbuff %p\n", skb);
643 static struct rcv_list **find_rcv_list(canid_t *can_id, canid_t *mask, struct net_device *dev)
645 canid_t inv = *can_id & CAN_INV_FILTER; /* save flag before masking values */
646 canid_t eff = *can_id & *mask & CAN_EFF_FLAG; /* correct EFF check? */
647 canid_t rtr = *can_id & *mask & CAN_RTR_FLAG; /* correct RTR check? */
648 canid_t err = *mask & CAN_ERR_FLAG; /* mask for error frames only */
650 struct rcv_dev_list *p;
652 /* make some paranoic operations */
653 if (*can_id & CAN_EFF_FLAG)
654 *mask &= (CAN_EFF_MASK | eff | rtr);
656 *mask &= (CAN_SFF_MASK | rtr);
660 /* find receive list for this device */
664 for (p = rx_dev_list; p; p = p->next)
669 /* arrange new rcv_dev_list for this device */
671 /* find deactivated receive list for this device */
672 for (p = rx_dev_list; p; p = p->next)
677 DBG("reactivating rcv_dev_list for %s\n", dev->name);
680 /* create new rcv_dev_list for this device */
681 DBG("creating new rcv_dev_list for %s\n", dev->name);
682 if (!(p = kmalloc(sizeof(struct rcv_dev_list), GFP_KERNEL))) {
683 printk(KERN_ERR "CAN: allocation of receive list failed\n");
686 memset (p, 0, sizeof(struct rcv_dev_list));
688 p->next = rx_dev_list;
693 if (err) /* error frames */
696 if (inv) /* inverse can_id/can_mask filter and RTR */
699 if (*can_id & CAN_RTR_FLAG) /* positive filter RTR */
702 if (!(*mask)) /* mask == 0 => no filter */
705 if (*can_id & CAN_EFF_FLAG) {
706 if (*mask == CAN_EFF_MASK) /* filter exact EFF can_id */
709 if (*mask == CAN_SFF_MASK) /* filter exact SFF can_id */
710 return &p->rx_sff[*can_id];
713 return &p->rx_fil; /* filter via can_id/can_mask */
716 /**************************************************/
717 /* af_can utility stuff */
718 /**************************************************/
720 unsigned long timeval2jiffies(struct timeval *tv, int round_up)
723 unsigned long sec = tv->tv_sec;
724 unsigned long usec = tv->tv_usec;
726 if (sec > ULONG_MAX / HZ) /* check for overflow */
729 if (round_up) /* any usec below one HZ? */
730 usec += 1000000 / HZ - 1; /* pump it up */
732 jif = usec / (1000000 / HZ);
734 if (sec * HZ > ULONG_MAX - jif) /* check for overflow */
737 return jif + sec * HZ;
741 /**************************************************/
742 /* af_can debugging stuff */
743 /**************************************************/
747 void can_debug_cframe(const char *msg, struct can_frame *cf, ...)
754 len = sprintf(buf, KERN_DEBUG);
756 len += snprintf(buf + len, sizeof(buf) - 64, msg, ap);
761 if ((dlc = cf->can_dlc) > 8)
764 if (cf->can_id & CAN_EFF_FLAG)
765 len += sprintf(buf + len, "<%08X> [%X] ",
766 cf->can_id & CAN_EFF_MASK, dlc);
768 len += sprintf(buf + len, "<%03X> [%X] ",
769 cf->can_id & CAN_SFF_MASK, dlc);
771 for (i = 0; i < dlc; i++)
772 len += sprintf(buf + len, "%02X ", cf->data[i]);
774 if (cf->can_id & CAN_RTR_FLAG)
775 len += sprintf(buf + len, "(RTR)");
782 void can_debug_skb(struct sk_buff *skb)
788 KERN_DEBUG " skbuff at %p, dev: %d, proto: %04x\n"
789 KERN_DEBUG " users: %d, dataref: %d, nr_frags: %d, "
790 "h,d,t,e,l: %p %+d %+d %+d, %d",
791 skb, skb->dev ? skb->dev->ifindex : -1, ntohs(skb->protocol),
792 atomic_read(&skb->users),
793 atomic_read(&(skb_shinfo(skb)->dataref)),
794 skb_shinfo(skb)->nr_frags,
795 skb->head, skb->data - skb->head,
796 skb->tail - skb->head, skb->end - skb->head, skb->len);
797 nbytes = skb->end - skb->head;
798 for (i = 0; i < nbytes; i++) {
800 len += sprintf(buf + len, "\n" KERN_DEBUG " ");
801 if (len < sizeof(buf) - 16) {
802 len += sprintf(buf + len, " %02x", skb->head[i]);
804 len += sprintf(buf + len, "...");
813 EXPORT_SYMBOL(can_debug_cframe);
814 EXPORT_SYMBOL(can_debug_skb);
818 /**************************************************/
819 /* Exported symbols */
820 /**************************************************/
821 EXPORT_SYMBOL(can_proto_register);
822 EXPORT_SYMBOL(can_proto_unregister);
823 EXPORT_SYMBOL(can_rx_register);
824 EXPORT_SYMBOL(can_rx_unregister);
825 EXPORT_SYMBOL(can_dev_register);
826 EXPORT_SYMBOL(can_dev_unregister);
827 EXPORT_SYMBOL(can_send);
828 EXPORT_SYMBOL(timeval2jiffies);