2 * af_can.c - Protocol family CAN core module
3 * (used by different CAN protocol modules)
5 * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions, the following disclaimer and
13 * the referenced file 'COPYING'.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of Volkswagen nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * Alternatively, provided that this notice is retained in full, this
22 * software may be distributed under the terms of the GNU General
23 * Public License ("GPL") version 2 as distributed in the 'COPYING'
24 * file from the main directory of the linux kernel source.
26 * The provided data structures and external interfaces from this code
27 * are not restricted to be used by modules with a GPL compatible license.
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
42 * Send feedback to <socketcan-users@lists.berlios.de>
46 #include <linux/autoconf.h>
47 #include <linux/module.h>
48 #include <linux/version.h>
49 #include <linux/slab.h>
50 #include <linux/kmod.h>
51 #include <linux/init.h>
52 #include <linux/list.h>
53 #include <linux/spinlock.h>
54 #include <linux/rcupdate.h>
55 #include <linux/socket.h>
56 #include <linux/if_ether.h>
57 #include <linux/if_arp.h>
58 #include <linux/skbuff.h>
59 #include <linux/net.h>
60 #include <linux/netdevice.h>
62 #include <asm/uaccess.h>
64 #include <linux/can.h>
65 #include <linux/can/core.h>
68 #include <linux/can/version.h> /* for RCSID. Removed by mkpatch script */
72 static __initdata const char banner[] =
73 KERN_INFO "can: controller area network core # "
74 CAN_VERSION_STRING "\n";
76 MODULE_DESCRIPTION("Controller Area Network PF_CAN core");
77 MODULE_LICENSE("Dual BSD/GPL");
78 MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>, "
79 "Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
81 MODULE_ALIAS_NETPROTO(PF_CAN);
83 int stats_timer = 1; /* default: on */
84 module_param(stats_timer, int, S_IRUGO);
85 MODULE_PARM_DESC(stats_timer, "enable timer for statistics (default:on)");
87 #ifdef CONFIG_CAN_DEBUG_CORE
89 module_param(debug, int, S_IRUGO);
90 MODULE_PARM_DESC(debug, "debug print mask: 1:debug, 2:frames, 4:skbs");
94 struct list_head list;
95 struct net_device *dev;
96 void (*func)(unsigned long msg, void *data);
100 static LIST_HEAD(notifier_list);
101 static DEFINE_RWLOCK(notifier_lock);
103 HLIST_HEAD(rx_dev_list);
104 static struct dev_rcv_lists rx_alldev_list;
105 static DEFINE_SPINLOCK(rcv_lists_lock);
107 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
108 static struct kmem_cache *rcv_cache __read_mostly;
110 static kmem_cache_t *rcv_cache;
113 /* table of registered CAN protocols */
114 static struct can_proto *proto_tab[CAN_NPROTO];
116 struct timer_list stattimer; /* timer for statistics update */
117 struct s_stats stats; /* packet statistics */
118 struct s_pstats pstats; /* receive list statistics */
119 DEFINE_SPINLOCK(stats_lock);
121 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
122 static void *kzalloc(size_t size, unsigned int __nocast flags)
124 void *ret = kmalloc(size, flags);
126 memset(ret, 0, size);
132 * af_can socket functions
135 static int can_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
137 struct sock *sk = sock->sk;
142 return sock_get_timestamp(sk, (struct timeval __user *)arg);
145 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
148 return dev_ioctl(cmd, (void __user *)arg);
153 static void can_sock_destruct(struct sock *sk)
155 DBG("called for sock %p\n", sk);
157 skb_queue_purge(&sk->sk_receive_queue);
159 kfree(sk->sk_protinfo);
162 static int can_create(struct socket *sock, int protocol)
165 struct can_proto *cp;
166 char module_name[30];
169 DBG("socket %p, type %d, proto %d\n", sock, sock->type, protocol);
171 sock->state = SS_UNCONNECTED;
173 if (protocol < 0 || protocol >= CAN_NPROTO)
176 DBG("looking up proto %d in proto_tab[]\n", protocol);
178 /* try to load protocol module, when CONFIG_KMOD is defined */
179 if (!proto_tab[protocol]) {
180 sprintf(module_name, "can-proto-%d", protocol);
181 if (request_module(module_name) == -ENOSYS)
182 printk(KERN_INFO "can: request_module(%s) not"
183 " implemented.\n", module_name);
186 /* check for success and correct type */
187 cp = proto_tab[protocol];
188 if (!cp || cp->type != sock->type)
189 return -EPROTONOSUPPORT;
191 if (cp->capability >= 0 && !capable(cp->capability))
196 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
197 sk = sk_alloc(PF_CAN, GFP_KERNEL, cp->prot, 1);
201 sk = sk_alloc(PF_CAN, GFP_KERNEL, 1, 0);
206 sk->sk_protinfo = kmalloc(cp->obj_size, GFP_KERNEL);
207 if (!sk->sk_protinfo) {
212 sk_set_owner(sk, proto_tab[protocol]->owner);
215 sock_init_data(sock, sk);
216 sk->sk_destruct = can_sock_destruct;
218 DBG("created sock: %p\n", sk);
220 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
221 if (sk->sk_prot->init)
222 ret = sk->sk_prot->init(sk);
229 /* release sk on errors */
242 * can_send - transmit a CAN frame (optional with local loopback)
243 * @skb: pointer to socket buffer with CAN frame in data section
244 * @loop: loopback for listeners on local CAN sockets (recommended default!)
248 * -ENETDOWN when the selected interface is down
249 * -ENOBUFS on full driver queue (see net_xmit_errno())
251 int can_send(struct sk_buff *skb, int loop)
253 struct sock **tx_sk = (struct sock **)skb->cb;
256 if (skb->dev->type != ARPHRD_CAN) {
262 /* local loopback of sent CAN frames (default) */
264 /* indication for the CAN driver: do loopback */
268 * The reference to the originating sock may be also required
269 * by the receiving socket to indicate (and ignore) his own
270 * sent data. Example: can_raw sockopt CAN_RAW_RECV_OWN_MSGS
273 /* interface not capabable to do the loopback itself? */
274 if (!(skb->dev->flags & IFF_LOOPBACK)) {
275 struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
277 /* perform the local loopback here */
278 newskb->protocol = htons(ETH_P_CAN);
279 newskb->ip_summed = CHECKSUM_UNNECESSARY;
283 /* indication for the CAN driver: no loopback required */
287 if (!(skb->dev->flags & IFF_UP))
290 /* send to netdevice */
291 err = dev_queue_xmit(skb);
293 err = net_xmit_errno(err);
295 /* update statistics */
296 spin_lock(&stats_lock);
298 stats.tx_frames_delta++;
299 spin_unlock(&stats_lock);
303 EXPORT_SYMBOL(can_send);
309 static struct dev_rcv_lists *find_dev_rcv_lists(struct net_device *dev)
311 struct dev_rcv_lists *d;
312 struct hlist_node *n;
315 * find receive list for this device
317 * The hlist_for_each_entry*() macros curse through the list
318 * using the pointer variable n and set d to the containing
319 * struct in each list iteration. Therefore, after list
320 * iteration, d is unmodified when the list is empty, and it
321 * points to last list element, when the list is non-empty
322 * but no match in the loop body is found. I.e. d is *not*
323 * NULL when no match is found. We can, however, use the
324 * cursor variable n to decide if a match was found.
327 hlist_for_each_entry(d, n, &rx_dev_list, list) {
335 static struct hlist_head *find_rcv_list(canid_t *can_id, canid_t *mask,
336 struct dev_rcv_lists *d)
338 canid_t inv = *can_id & CAN_INV_FILTER; /* save flag before masking */
340 /* filter error frames */
341 if (*mask & CAN_ERR_FLAG) {
342 /* clear CAN_ERR_FLAG in list entry */
343 *mask &= CAN_ERR_MASK;
347 /* ensure valid values in can_mask */
348 if (*mask & CAN_EFF_FLAG)
349 *mask &= (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG);
351 *mask &= (CAN_SFF_MASK | CAN_RTR_FLAG);
353 /* reduce condition testing at receive time */
356 /* inverse can_id/can_mask filter */
360 /* mask == 0 => no condition testing at receive time */
364 /* use extra filterset for the subscription of exactly *ONE* can_id */
365 if (*can_id & CAN_EFF_FLAG) {
366 if (*mask == (CAN_EFF_MASK | CAN_EFF_FLAG)) {
367 /* RFC: a use-case for hash-tables in the future? */
371 if (*mask == CAN_SFF_MASK)
372 return &d->rx_sff[*can_id];
375 /* default: filter via can_id/can_mask */
380 * can_rx_register - subscribe CAN frames from a specific interface
381 * @dev: pointer to netdevice (NULL => subcribe from 'all' CAN devices list)
382 * @can_id: CAN identifier (see description)
383 * @mask: CAN mask (see description)
384 * @func: callback function on filter match
385 * @data: returned parameter for callback function
386 * @ident: string for calling module indentification
389 * Invokes the callback function with the received sk_buff and the given
390 * parameter 'data' on a matching receive filter. A filter matches, when
392 * <received_can_id> & mask == can_id & mask
394 * The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can
395 * filter for error frames (CAN_ERR_FLAG bit set in mask).
399 * -ENOMEM on missing cache mem to create subscription entry
400 * -ENODEV unknown device
402 int can_rx_register(struct net_device *dev, canid_t can_id, canid_t mask,
403 void (*func)(struct sk_buff *, void *), void *data,
407 struct hlist_head *rl;
408 struct dev_rcv_lists *d;
411 /* insert new receiver (dev,canid,mask) -> (func,data) */
413 DBG("dev %p, id %03X, mask %03X, callback %p, data %p, ident %s\n",
414 dev, can_id, mask, func, data, ident);
416 r = kmem_cache_alloc(rcv_cache, GFP_KERNEL);
420 spin_lock_bh(&rcv_lists_lock);
422 d = find_dev_rcv_lists(dev);
424 rl = find_rcv_list(&can_id, &mask, d);
433 hlist_add_head_rcu(&r->list, rl);
436 pstats.rcv_entries++;
437 if (pstats.rcv_entries_max < pstats.rcv_entries)
438 pstats.rcv_entries_max = pstats.rcv_entries;
440 DBG("receive list not found for dev %s, id %03X, mask %03X\n",
441 DNAME(dev), can_id, mask);
442 kmem_cache_free(rcv_cache, r);
446 spin_unlock_bh(&rcv_lists_lock);
450 EXPORT_SYMBOL(can_rx_register);
452 static void can_rcv_lists_delete(struct rcu_head *rp)
454 struct dev_rcv_lists *d = container_of(rp, struct dev_rcv_lists, rcu);
459 static void can_rx_delete(struct rcu_head *rp)
461 struct receiver *r = container_of(rp, struct receiver, rcu);
463 kmem_cache_free(rcv_cache, r);
466 static void can_rx_delete_all(struct hlist_head *rl)
469 struct hlist_node *n;
471 hlist_for_each_entry_rcu(r, n, rl, list) {
472 hlist_del_rcu(&r->list);
473 call_rcu(&r->rcu, can_rx_delete);
478 * can_rx_unregister - unsubscribe CAN frames from a specific interface
479 * @dev: pointer to netdevice (NULL => unsubcribe from 'all' CAN devices list)
480 * @can_id: CAN identifier
482 * @func: callback function on filter match
483 * @data: returned parameter for callback function
486 * Removes subscription entry depending on given (subscription) values.
490 * -EINVAL on missing subscription entry
491 * -ENODEV unknown device
493 int can_rx_unregister(struct net_device *dev, canid_t can_id, canid_t mask,
494 void (*func)(struct sk_buff *, void *), void *data)
496 struct receiver *r = NULL;
497 struct hlist_head *rl;
498 struct hlist_node *next;
499 struct dev_rcv_lists *d;
502 DBG("dev %p, id %03X, mask %03X, callback %p, data %p\n",
503 dev, can_id, mask, func, data);
505 spin_lock_bh(&rcv_lists_lock);
507 d = find_dev_rcv_lists(dev);
509 DBG("receive list not found for dev %s, id %03X, mask %03X\n",
510 DNAME(dev), can_id, mask);
515 rl = find_rcv_list(&can_id, &mask, d);
518 * Search the receiver list for the item to delete. This should
519 * exist, since no receiver may be unregistered that hasn't
520 * been registered before.
523 hlist_for_each_entry(r, next, rl, list) {
524 if (r->can_id == can_id && r->mask == mask
525 && r->func == func && r->data == data)
530 * Check for bug in CAN protocol implementations:
531 * If no matching list item was found, the list cursor variable next
532 * will be NULL, while r will point to the last item of the list.
536 DBG("receive list entry not found for "
537 "dev %s, id %03X, mask %03X\n", DNAME(dev), can_id, mask);
543 hlist_del_rcu(&r->list);
546 if (pstats.rcv_entries > 0)
547 pstats.rcv_entries--;
550 spin_unlock_bh(&rcv_lists_lock);
552 /* schedule the receiver item for deletion */
554 call_rcu(&r->rcu, can_rx_delete);
558 EXPORT_SYMBOL(can_rx_unregister);
560 static inline void deliver(struct sk_buff *skb, struct receiver *r)
562 struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
564 DBG("skbuff %p cloned to %p\n", skb, clone);
566 r->func(clone, r->data);
571 static int can_rcv_filter(struct dev_rcv_lists *d, struct sk_buff *skb)
574 struct hlist_node *n;
576 struct can_frame *cf = (struct can_frame*)skb->data;
577 canid_t can_id = cf->can_id;
582 if (can_id & CAN_ERR_FLAG) {
583 /* check for error frame entries only */
584 hlist_for_each_entry_rcu(r, n, &d->rx_err, list) {
585 if (can_id & r->mask) {
586 DBG("match on rx_err skbuff %p\n", skb);
594 /* check for unfiltered entries */
595 hlist_for_each_entry_rcu(r, n, &d->rx_all, list) {
596 DBG("match on rx_all skbuff %p\n", skb);
601 /* check for can_id/mask entries */
602 hlist_for_each_entry_rcu(r, n, &d->rx_fil, list) {
603 if ((can_id & r->mask) == r->can_id) {
604 DBG("match on rx_fil skbuff %p\n", skb);
610 /* check for inverted can_id/mask entries */
611 hlist_for_each_entry_rcu(r, n, &d->rx_inv, list) {
612 if ((can_id & r->mask) != r->can_id) {
613 DBG("match on rx_inv skbuff %p\n", skb);
619 /* check CAN_ID specific entries */
620 if (can_id & CAN_EFF_FLAG) {
621 hlist_for_each_entry_rcu(r, n, &d->rx_eff, list) {
622 if (r->can_id == can_id) {
623 DBG("match on rx_eff skbuff %p\n", skb);
629 can_id &= CAN_SFF_MASK;
630 hlist_for_each_entry_rcu(r, n, &d->rx_sff[can_id], list) {
631 DBG("match on rx_sff skbuff %p\n", skb);
640 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
641 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
642 struct packet_type *pt, struct net_device *orig_dev)
644 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
645 struct packet_type *pt)
648 struct dev_rcv_lists *d;
651 DBG("received skbuff on device %s, ptype %04x\n",
652 dev->name, ntohs(pt->type));
654 DBG_FRAME("af_can: can_rcv: received CAN frame",
655 (struct can_frame *)skb->data);
657 if (dev->type != ARPHRD_CAN) {
662 /* update statistics */
663 spin_lock(&stats_lock);
665 stats.rx_frames_delta++;
666 spin_unlock(&stats_lock);
670 /* deliver the packet to sockets listening on all devices */
671 matches = can_rcv_filter(&rx_alldev_list, skb);
673 /* find receive list for this device */
674 d = find_dev_rcv_lists(dev);
676 matches += can_rcv_filter(d, skb);
680 /* free the skbuff allocated by the netdevice driver */
681 DBG("freeing skbuff %p\n", skb);
685 spin_lock(&stats_lock);
687 stats.matches_delta++;
688 spin_unlock(&stats_lock);
695 * af_can debugging stuff
698 #ifdef CONFIG_CAN_DEBUG_CORE
700 #define DBG_BSIZE 1024
703 * can_debug_cframe - print CAN frame
704 * @msg: pointer to message printed before the given CAN frame
705 * @cf: pointer to CAN frame
707 void can_debug_cframe(const char *msg, struct can_frame *cf, ...)
714 buf = kmalloc(DBG_BSIZE, GFP_ATOMIC);
718 len = sprintf(buf, KERN_DEBUG);
720 len += snprintf(buf + len, DBG_BSIZE - 64, msg, ap);
729 if (cf->can_id & CAN_EFF_FLAG)
730 len += sprintf(buf + len, "<%08X> [%X] ",
731 cf->can_id & CAN_EFF_MASK, dlc);
733 len += sprintf(buf + len, "<%03X> [%X] ",
734 cf->can_id & CAN_SFF_MASK, dlc);
736 for (i = 0; i < dlc; i++)
737 len += sprintf(buf + len, "%02X ", cf->data[i]);
739 if (cf->can_id & CAN_RTR_FLAG)
740 len += sprintf(buf + len, "(RTR)");
747 EXPORT_SYMBOL(can_debug_cframe);
750 * can_debug_skb - print socket buffer content to kernel log
751 * @skb: pointer to socket buffer
753 void can_debug_skb(struct sk_buff *skb)
758 buf = kmalloc(DBG_BSIZE, GFP_ATOMIC);
763 KERN_DEBUG " skbuff at %p, dev: %d, proto: %04x\n"
764 KERN_DEBUG " users: %d, dataref: %d, nr_frags: %d, "
765 "h,d,t,e,l: %p %+d %+d %+d, %d",
766 skb, skb->dev ? skb->dev->ifindex : -1,
767 ntohs(skb->protocol),
768 atomic_read(&skb->users),
769 atomic_read(&(skb_shinfo(skb)->dataref)),
770 skb_shinfo(skb)->nr_frags,
771 skb->head, skb->data - skb->head,
772 skb->tail - skb->head, skb->end - skb->head, skb->len);
773 nbytes = skb->end - skb->head;
774 for (i = 0; i < nbytes; i++) {
776 len += sprintf(buf + len, "\n" KERN_DEBUG " ");
777 if (len < DBG_BSIZE - 16) {
778 len += sprintf(buf + len, " %02x", skb->head[i]);
780 len += sprintf(buf + len, "...");
789 EXPORT_SYMBOL(can_debug_skb);
794 * af_can protocol functions
798 * can_proto_register - register CAN transport protocol
799 * @cp: pointer to CAN protocol structure
803 * -EINVAL invalid (out of range) protocol number
804 * -EBUSY protocol already in use
805 * -ENOBUF if proto_register() fails
807 int can_proto_register(struct can_proto *cp)
809 int proto = cp->protocol;
812 if (proto < 0 || proto >= CAN_NPROTO) {
813 printk(KERN_ERR "can: protocol number %d out "
814 "of range\n", proto);
817 if (proto_tab[proto]) {
818 printk(KERN_ERR "can: protocol %d already "
819 "registered\n", proto);
823 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
824 err = proto_register(cp->prot, 0);
829 proto_tab[proto] = cp;
831 /* use generic ioctl function if the module doesn't bring its own */
833 cp->ops->ioctl = can_ioctl;
837 EXPORT_SYMBOL(can_proto_register);
840 * can_proto_unregister - unregister CAN transport protocol
841 * @cp: pointer to CAN protocol structure
845 * -ESRCH protocol number was not registered
847 int can_proto_unregister(struct can_proto *cp)
849 int proto = cp->protocol;
851 if (!proto_tab[proto]) {
852 printk(KERN_ERR "can: protocol %d is not registered\n", proto);
855 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
856 proto_unregister(cp->prot);
858 proto_tab[proto] = NULL;
862 EXPORT_SYMBOL(can_proto_unregister);
865 * can_dev_register - subscribe notifier for CAN device status changes
866 * @dev: pointer to netdevice
867 * @func: callback function on status change
868 * @data: returned parameter for callback function
871 * Invokes the callback function with the status 'msg' and the given
872 * parameter 'data' on a status change of the given CAN network device.
876 * -ENOMEM on missing mem to create subscription entry
877 * -ENODEV unknown device
879 int can_dev_register(struct net_device *dev,
880 void (*func)(unsigned long msg, void *), void *data)
884 DBG("called for %s\n", dev->name);
886 if (!dev || dev->type != ARPHRD_CAN)
889 n = kmalloc(sizeof(*n), GFP_KERNEL);
897 write_lock(¬ifier_lock);
898 list_add(&n->list, ¬ifier_list);
899 write_unlock(¬ifier_lock);
903 EXPORT_SYMBOL(can_dev_register);
906 * can_dev_unregister - unsubscribe notifier for CAN device status changes
907 * @dev: pointer to netdevice
908 * @func: callback function on filter match
909 * @data: returned parameter for callback function
912 * Removes subscription entry depending on given (subscription) values.
916 * -EINVAL on missing subscription entry
918 int can_dev_unregister(struct net_device *dev,
919 void (*func)(unsigned long msg, void *), void *data)
921 struct notifier *n, *next;
924 DBG("called for %s\n", dev->name);
926 write_lock(¬ifier_lock);
927 list_for_each_entry_safe(n, next, ¬ifier_list, list) {
928 if (n->dev == dev && n->func == func && n->data == data) {
935 write_unlock(¬ifier_lock);
939 EXPORT_SYMBOL(can_dev_unregister);
941 static int can_notifier(struct notifier_block *nb,
942 unsigned long msg, void *data)
944 struct net_device *dev = (struct net_device *)data;
946 struct dev_rcv_lists *d;
949 DBG("called for %s, msg = %lu\n", dev->name, msg);
951 if (dev->type != ARPHRD_CAN)
956 case NETDEV_REGISTER:
959 * create new dev_rcv_lists for this device
961 * N.B. zeroing the struct is the correct initialization
962 * for the embedded hlist_head structs.
963 * Another list type, e.g. list_head, would require
964 * explicit initialization.
967 DBG("creating new dev_rcv_lists for %s\n", dev->name);
969 d = kzalloc(sizeof(*d),
970 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
972 printk(KERN_ERR "can: allocation of receive "
978 spin_lock_bh(&rcv_lists_lock);
979 hlist_add_head_rcu(&d->list, &rx_dev_list);
980 spin_unlock_bh(&rcv_lists_lock);
984 case NETDEV_UNREGISTER:
985 spin_lock_bh(&rcv_lists_lock);
987 d = find_dev_rcv_lists(dev);
989 hlist_del_rcu(&d->list);
991 /* remove all receivers hooked at this netdevice */
992 can_rx_delete_all(&d->rx_err);
993 can_rx_delete_all(&d->rx_all);
994 can_rx_delete_all(&d->rx_fil);
995 can_rx_delete_all(&d->rx_inv);
996 can_rx_delete_all(&d->rx_eff);
997 for (i = 0; i < 2048; i++)
998 can_rx_delete_all(&d->rx_sff[i]);
1000 printk(KERN_ERR "can: notifier: receive list not "
1001 "found for dev %s\n", dev->name);
1003 spin_unlock_bh(&rcv_lists_lock);
1006 call_rcu(&d->rcu, can_rcv_lists_delete);
1011 read_lock(¬ifier_lock);
1012 list_for_each_entry(n, ¬ifier_list, list) {
1014 n->func(msg, n->data);
1016 read_unlock(¬ifier_lock);
1022 * af_can module init/exit functions
1025 static struct packet_type can_packet = {
1026 .type = __constant_htons(ETH_P_CAN),
1031 static struct net_proto_family can_family_ops = {
1033 .create = can_create,
1034 .owner = THIS_MODULE,
1037 /* notifier block for netdevice event */
1038 static struct notifier_block can_netdev_notifier = {
1039 .notifier_call = can_notifier,
1042 static __init int can_init(void)
1046 rcv_cache = kmem_cache_create("can_receiver", sizeof(struct receiver),
1052 * Insert struct dev_rcv_lists for reception on all devices.
1053 * This struct is zero initialized which is correct for the
1054 * embedded hlist heads, the dev pointer, and the entries counter.
1057 spin_lock_bh(&rcv_lists_lock);
1058 hlist_add_head_rcu(&rx_alldev_list.list, &rx_dev_list);
1059 spin_unlock_bh(&rcv_lists_lock);
1062 /* the statistics are updated every second (timer triggered) */
1063 init_timer(&stattimer);
1064 stattimer.function = can_stat_update;
1066 /* update every second */
1067 stattimer.expires = jiffies + HZ;
1068 /* start statistics timer */
1069 add_timer(&stattimer);
1071 stattimer.function = NULL;
1076 /* protocol register */
1077 sock_register(&can_family_ops);
1078 register_netdevice_notifier(&can_netdev_notifier);
1079 dev_add_pack(&can_packet);
1084 static __exit void can_exit(void)
1086 struct dev_rcv_lists *d;
1087 struct hlist_node *n, *next;
1090 del_timer(&stattimer);
1095 /* protocol unregister */
1096 dev_remove_pack(&can_packet);
1097 unregister_netdevice_notifier(&can_netdev_notifier);
1098 sock_unregister(PF_CAN);
1100 /* remove rx_dev_list */
1101 spin_lock_bh(&rcv_lists_lock);
1102 hlist_del(&rx_alldev_list.list);
1103 hlist_for_each_entry_safe(d, n, next, &rx_dev_list, list) {
1104 hlist_del(&d->list);
1107 spin_unlock_bh(&rcv_lists_lock);
1109 kmem_cache_destroy(rcv_cache);
1112 module_init(can_init);
1113 module_exit(can_exit);