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 extern struct timer_list stattimer; /* timer for statistics update */
117 extern struct s_stats stats; /* packet statistics */
118 extern struct s_pstats pstats; /* receive list statistics */
120 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
121 static void *kzalloc(size_t size, unsigned int __nocast flags)
123 void *ret = kmalloc(size, flags);
125 memset(ret, 0, size);
131 * af_can socket functions
134 static int can_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
136 struct sock *sk = sock->sk;
141 return sock_get_timestamp(sk, (struct timeval __user *)arg);
144 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
147 return dev_ioctl(cmd, (void __user *)arg);
152 static void can_sock_destruct(struct sock *sk)
154 DBG("called for sock %p\n", sk);
156 skb_queue_purge(&sk->sk_receive_queue);
158 kfree(sk->sk_protinfo);
161 static int can_create(struct socket *sock, int protocol)
164 struct can_proto *cp;
165 char module_name[30];
168 DBG("socket %p, type %d, proto %d\n", sock, sock->type, protocol);
170 sock->state = SS_UNCONNECTED;
172 if (protocol < 0 || protocol >= CAN_NPROTO)
175 DBG("looking up proto %d in proto_tab[]\n", protocol);
177 /* try to load protocol module, when CONFIG_KMOD is defined */
178 if (!proto_tab[protocol]) {
179 sprintf(module_name, "can-proto-%d", protocol);
180 if (request_module(module_name) == -ENOSYS)
181 printk(KERN_INFO "can: request_module(%s) not"
182 " implemented.\n", module_name);
185 /* check for success and correct type */
186 cp = proto_tab[protocol];
187 if (!cp || cp->type != sock->type)
188 return -EPROTONOSUPPORT;
190 if (cp->capability >= 0 && !capable(cp->capability))
195 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
196 sk = sk_alloc(PF_CAN, GFP_KERNEL, cp->prot, 1);
200 sk = sk_alloc(PF_CAN, GFP_KERNEL, 1, 0);
205 sk->sk_protinfo = kmalloc(cp->obj_size, GFP_KERNEL);
206 if (!sk->sk_protinfo) {
211 sk_set_owner(sk, proto_tab[protocol]->owner);
214 sock_init_data(sock, sk);
215 sk->sk_destruct = can_sock_destruct;
217 DBG("created sock: %p\n", sk);
219 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
220 if (sk->sk_prot->init)
221 ret = sk->sk_prot->init(sk);
228 /* release sk on errors */
241 * can_send - transmit a CAN frame (optional with local loopback)
242 * @skb: pointer to socket buffer with CAN frame in data section
243 * @loop: loopback for listeners on local CAN sockets (recommended default!)
247 * -ENETDOWN when the selected interface is down
248 * -ENOBUFS on full driver queue (see net_xmit_errno())
250 int can_send(struct sk_buff *skb, int loop)
255 /* local loopback of sent CAN frames (default) */
257 /* indication for the CAN driver: do loopback */
258 *(struct sock **)skb->cb = skb->sk;
261 * The reference to the originating sock may be also required
262 * by the receiving socket to indicate (and ignore) his own
263 * sent data. Example: can_raw sockopt CAN_RAW_RECV_OWN_MSGS
266 /* interface not capabable to do the loopback itself? */
267 if (!(skb->dev->flags & IFF_LOOPBACK)) {
268 struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
270 /* perform the local loopback here */
271 newskb->protocol = htons(ETH_P_CAN);
272 newskb->ip_summed = CHECKSUM_UNNECESSARY;
276 /* indication for the CAN driver: no loopback required */
277 *(struct sock **)skb->cb = NULL;
280 if (!(skb->dev->flags & IFF_UP))
283 /* send to netdevice */
284 err = dev_queue_xmit(skb);
286 err = net_xmit_errno(err);
288 /* update statistics */
290 stats.tx_frames_delta++;
294 EXPORT_SYMBOL_GPL(can_send);
300 static struct dev_rcv_lists *find_dev_rcv_lists(struct net_device *dev)
302 struct dev_rcv_lists *d;
303 struct hlist_node *n;
306 * find receive list for this device
308 * The hlist_for_each_entry*() macros curse through the list
309 * using the pointer variable n and set d to the containing
310 * struct in each list iteration. Therefore, after list
311 * iteration, d is unmodified when the list is empty, and it
312 * points to last list element, when the list is non-empty
313 * but no match in the loop body is found. I.e. d is *not*
314 * NULL when no match is found. We can, however, use the
315 * cursor variable n to decide if a match was found.
318 hlist_for_each_entry(d, n, &rx_dev_list, list) {
326 static struct hlist_head *find_rcv_list(canid_t *can_id, canid_t *mask,
327 struct dev_rcv_lists *d)
329 canid_t inv = *can_id & CAN_INV_FILTER; /* save flag before masking */
331 if (*mask & CAN_ERR_FLAG) { /* filter error frames */
332 *mask &= CAN_ERR_MASK; /* clear CAN_ERR_FLAG in list entry */
336 /* ensure valid values in can_mask */
337 if (*mask & CAN_EFF_FLAG)
338 *mask &= (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG);
340 *mask &= (CAN_SFF_MASK | CAN_RTR_FLAG);
342 /* reduce condition testing at receive time */
345 /* inverse can_id/can_mask filter */
349 /* mask == 0 => no condition testing at receive time */
353 /* use extra filterset for the subscription of exactly *ONE* can_id */
354 if (*can_id & CAN_EFF_FLAG) {
355 if (*mask == (CAN_EFF_MASK | CAN_EFF_FLAG)) {
356 /* RFC: a use-case for hash-tables in the future? */
360 if (*mask == CAN_SFF_MASK)
361 return &d->rx_sff[*can_id];
364 /* default: filter via can_id/can_mask */
369 * can_rx_register - subscribe CAN frames from a specific interface
370 * @dev: pointer to netdevice (NULL => subcribe from 'all' CAN devices list)
371 * @can_id: CAN identifier (see description)
372 * @mask: CAN mask (see description)
373 * @func: callback function on filter match
374 * @data: returned parameter for callback function
375 * @ident: string for calling module indentification
378 * Invokes the callback function with the received sk_buff and the given
379 * parameter 'data' on a matching receive filter. A filter matches, when
381 * <received_can_id> & mask == can_id & mask
383 * The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can
384 * filter for error frames (CAN_ERR_FLAG bit set in mask).
388 * -ENOMEM on missing cache mem to create subscription entry
389 * -ENODEV unknown device
391 int can_rx_register(struct net_device *dev, canid_t can_id, canid_t mask,
392 void (*func)(struct sk_buff *, void *), void *data,
396 struct hlist_head *rl;
397 struct dev_rcv_lists *d;
400 /* insert new receiver (dev,canid,mask) -> (func,data) */
402 DBG("dev %p, id %03X, mask %03X, callback %p, data %p, ident %s\n",
403 dev, can_id, mask, func, data, ident);
405 r = kmem_cache_alloc(rcv_cache, GFP_KERNEL);
409 spin_lock_bh(&rcv_lists_lock);
411 d = find_dev_rcv_lists(dev);
413 rl = find_rcv_list(&can_id, &mask, d);
422 hlist_add_head_rcu(&r->list, rl);
425 pstats.rcv_entries++;
426 if (pstats.rcv_entries_max < pstats.rcv_entries)
427 pstats.rcv_entries_max = pstats.rcv_entries;
429 DBG("receive list not found for dev %s, id %03X, mask %03X\n",
430 DNAME(dev), can_id, mask);
431 kmem_cache_free(rcv_cache, r);
435 spin_unlock_bh(&rcv_lists_lock);
439 EXPORT_SYMBOL_GPL(can_rx_register);
441 static void can_rcv_lists_delete(struct rcu_head *rp)
443 struct dev_rcv_lists *d = container_of(rp, struct dev_rcv_lists, rcu);
447 static void can_rx_delete(struct rcu_head *rp)
449 struct receiver *r = container_of(rp, struct receiver, rcu);
450 kmem_cache_free(rcv_cache, r);
453 static void can_rx_delete_all(struct hlist_head *rl)
456 struct hlist_node *n;
458 hlist_for_each_entry_rcu(r, n, rl, list) {
459 hlist_del_rcu(&r->list);
460 call_rcu(&r->rcu, can_rx_delete);
465 * can_rx_unregister - unsubscribe CAN frames from a specific interface
466 * @dev: pointer to netdevice (NULL => unsubcribe from 'all' CAN devices list)
467 * @can_id: CAN identifier
469 * @func: callback function on filter match
470 * @data: returned parameter for callback function
473 * Removes subscription entry depending on given (subscription) values.
477 * -EINVAL on missing subscription entry
478 * -ENODEV unknown device
480 int can_rx_unregister(struct net_device *dev, canid_t can_id, canid_t mask,
481 void (*func)(struct sk_buff *, void *), void *data)
483 struct receiver *r = NULL;
484 struct hlist_head *rl;
485 struct hlist_node *next;
486 struct dev_rcv_lists *d;
489 DBG("dev %p, id %03X, mask %03X, callback %p, data %p\n",
490 dev, can_id, mask, func, data);
492 spin_lock_bh(&rcv_lists_lock);
494 d = find_dev_rcv_lists(dev);
496 DBG("receive list not found for dev %s, id %03X, mask %03X\n",
497 DNAME(dev), can_id, mask);
502 rl = find_rcv_list(&can_id, &mask, d);
505 * Search the receiver list for the item to delete. This should
506 * exist, since no receiver may be unregistered that hasn't
507 * been registered before.
510 hlist_for_each_entry(r, next, rl, list) {
511 if (r->can_id == can_id && r->mask == mask
512 && r->func == func && r->data == data)
517 * Check for bug in CAN protocol implementations:
518 * If no matching list item was found, the list cursor variable next
519 * will be NULL, while r will point to the last item of the list.
523 DBG("receive list entry not found for "
524 "dev %s, id %03X, mask %03X\n", DNAME(dev), can_id, mask);
530 hlist_del_rcu(&r->list);
533 if (pstats.rcv_entries > 0)
534 pstats.rcv_entries--;
537 spin_unlock_bh(&rcv_lists_lock);
539 /* schedule the receiver item for deletion */
541 call_rcu(&r->rcu, can_rx_delete);
545 EXPORT_SYMBOL_GPL(can_rx_unregister);
547 static inline void deliver(struct sk_buff *skb, struct receiver *r)
549 struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
550 DBG("skbuff %p cloned to %p\n", skb, clone);
552 r->func(clone, r->data);
557 static int can_rcv_filter(struct dev_rcv_lists *d, struct sk_buff *skb)
560 struct hlist_node *n;
562 struct can_frame *cf = (struct can_frame*)skb->data;
563 canid_t can_id = cf->can_id;
568 if (can_id & CAN_ERR_FLAG) {
569 /* check for error frame entries only */
570 hlist_for_each_entry_rcu(r, n, &d->rx_err, list) {
571 if (can_id & r->mask) {
572 DBG("match on rx_err skbuff %p\n", skb);
580 /* check for unfiltered entries */
581 hlist_for_each_entry_rcu(r, n, &d->rx_all, list) {
582 DBG("match on rx_all skbuff %p\n", skb);
587 /* check for can_id/mask entries */
588 hlist_for_each_entry_rcu(r, n, &d->rx_fil, list) {
589 if ((can_id & r->mask) == r->can_id) {
590 DBG("match on rx_fil skbuff %p\n", skb);
596 /* check for inverted can_id/mask entries */
597 hlist_for_each_entry_rcu(r, n, &d->rx_inv, list) {
598 if ((can_id & r->mask) != r->can_id) {
599 DBG("match on rx_inv skbuff %p\n", skb);
605 /* check CAN_ID specific entries */
606 if (can_id & CAN_EFF_FLAG) {
607 hlist_for_each_entry_rcu(r, n, &d->rx_eff, list) {
608 if (r->can_id == can_id) {
609 DBG("match on rx_eff skbuff %p\n", skb);
615 can_id &= CAN_SFF_MASK;
616 hlist_for_each_entry_rcu(r, n, &d->rx_sff[can_id], list) {
617 DBG("match on rx_sff skbuff %p\n", skb);
626 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
627 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
628 struct packet_type *pt, struct net_device *orig_dev)
630 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
631 struct packet_type *pt)
634 struct dev_rcv_lists *d;
637 DBG("received skbuff on device %s, ptype %04x\n",
638 dev->name, ntohs(pt->type));
640 DBG_FRAME("af_can: can_rcv: received CAN frame",
641 (struct can_frame *)skb->data);
643 /* update statistics */
645 stats.rx_frames_delta++;
649 /* deliver the packet to sockets listening on all devices */
650 matches = can_rcv_filter(&rx_alldev_list, skb);
652 /* find receive list for this device */
653 d = find_dev_rcv_lists(dev);
655 matches += can_rcv_filter(d, skb);
659 /* free the skbuff allocated by the netdevice driver */
660 DBG("freeing skbuff %p\n", skb);
665 stats.matches_delta++;
672 * af_can utility stuff
676 * timeval2jiffies - calculate jiffies from timeval including optional round up
677 * @tv: pointer to timeval
678 * @round_up: return at least 1 jiffie
681 * calculated jiffies (max: ULONG_MAX)
683 unsigned long timeval2jiffies(struct timeval *tv, int round_up)
686 unsigned long sec = tv->tv_sec;
687 unsigned long usec = tv->tv_usec;
689 /* check for overflow */
690 if (sec > ULONG_MAX / HZ)
693 /* any usec below one HZ? => pump it up */
695 usec += 1000000 / HZ - 1;
697 jif = usec / (1000000 / HZ);
699 /* check for overflow */
700 if (sec * HZ > ULONG_MAX - jif)
703 return jif + sec * HZ;
705 EXPORT_SYMBOL_GPL(timeval2jiffies);
708 * af_can debugging stuff
711 #ifdef CONFIG_CAN_DEBUG_CORE
713 #define DBG_BSIZE 1024
716 * can_debug_cframe - print CAN frame
717 * @msg: pointer to message printed before the given CAN frame
718 * @cf: pointer to CAN frame
720 void can_debug_cframe(const char *msg, struct can_frame *cf, ...)
727 buf = kmalloc(DBG_BSIZE, GFP_ATOMIC);
731 len = sprintf(buf, KERN_DEBUG);
733 len += snprintf(buf + len, DBG_BSIZE - 64, msg, ap);
742 if (cf->can_id & CAN_EFF_FLAG)
743 len += sprintf(buf + len, "<%08X> [%X] ",
744 cf->can_id & CAN_EFF_MASK, dlc);
746 len += sprintf(buf + len, "<%03X> [%X] ",
747 cf->can_id & CAN_SFF_MASK, dlc);
749 for (i = 0; i < dlc; i++)
750 len += sprintf(buf + len, "%02X ", cf->data[i]);
752 if (cf->can_id & CAN_RTR_FLAG)
753 len += sprintf(buf + len, "(RTR)");
760 EXPORT_SYMBOL_GPL(can_debug_cframe);
763 * can_debug_skb - print socket buffer content to kernel log
764 * @skb: pointer to socket buffer
766 void can_debug_skb(struct sk_buff *skb)
771 buf = kmalloc(DBG_BSIZE, GFP_ATOMIC);
776 KERN_DEBUG " skbuff at %p, dev: %d, proto: %04x\n"
777 KERN_DEBUG " users: %d, dataref: %d, nr_frags: %d, "
778 "h,d,t,e,l: %p %+d %+d %+d, %d",
779 skb, skb->dev ? skb->dev->ifindex : -1,
780 ntohs(skb->protocol),
781 atomic_read(&skb->users),
782 atomic_read(&(skb_shinfo(skb)->dataref)),
783 skb_shinfo(skb)->nr_frags,
784 skb->head, skb->data - skb->head,
785 skb->tail - skb->head, skb->end - skb->head, skb->len);
786 nbytes = skb->end - skb->head;
787 for (i = 0; i < nbytes; i++) {
789 len += sprintf(buf + len, "\n" KERN_DEBUG " ");
790 if (len < DBG_BSIZE - 16) {
791 len += sprintf(buf + len, " %02x", skb->head[i]);
793 len += sprintf(buf + len, "...");
802 EXPORT_SYMBOL_GPL(can_debug_skb);
807 * af_can protocol functions
811 * can_proto_register - register CAN transport protocol
812 * @cp: pointer to CAN protocol structure
816 * -EINVAL invalid (out of range) protocol number
817 * -EBUSY protocol already in use
818 * -ENOBUF if proto_register() fails
820 int can_proto_register(struct can_proto *cp)
822 int proto = cp->protocol;
825 if (proto < 0 || proto >= CAN_NPROTO) {
826 printk(KERN_ERR "can: protocol number %d out "
827 "of range\n", proto);
830 if (proto_tab[proto]) {
831 printk(KERN_ERR "can: protocol %d already "
832 "registered\n", proto);
836 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
837 err = proto_register(cp->prot, 0);
842 proto_tab[proto] = cp;
844 /* use generic ioctl function if the module doesn't bring its own */
846 cp->ops->ioctl = can_ioctl;
850 EXPORT_SYMBOL_GPL(can_proto_register);
853 * can_proto_unregister - unregister CAN transport protocol
854 * @cp: pointer to CAN protocol structure
858 * -ESRCH protocol number was not registered
860 int can_proto_unregister(struct can_proto *cp)
862 int proto = cp->protocol;
864 if (!proto_tab[proto]) {
865 printk(KERN_ERR "can: protocol %d is not registered\n", proto);
868 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
869 proto_unregister(cp->prot);
871 proto_tab[proto] = NULL;
875 EXPORT_SYMBOL_GPL(can_proto_unregister);
878 * can_dev_register - subscribe notifier for CAN device status changes
879 * @dev: pointer to netdevice
880 * @func: callback function on status change
881 * @data: returned parameter for callback function
884 * Invokes the callback function with the status 'msg' and the given
885 * parameter 'data' on a status change of the given CAN network device.
889 * -ENOMEM on missing mem to create subscription entry
890 * -ENODEV unknown device
892 int can_dev_register(struct net_device *dev,
893 void (*func)(unsigned long msg, void *), void *data)
897 DBG("called for %s\n", dev->name);
899 if (!dev || dev->type != ARPHRD_CAN)
902 n = kmalloc(sizeof(*n), GFP_KERNEL);
910 write_lock(¬ifier_lock);
911 list_add(&n->list, ¬ifier_list);
912 write_unlock(¬ifier_lock);
916 EXPORT_SYMBOL_GPL(can_dev_register);
919 * can_dev_unregister - unsubscribe notifier for CAN device status changes
920 * @dev: pointer to netdevice
921 * @func: callback function on filter match
922 * @data: returned parameter for callback function
925 * Removes subscription entry depending on given (subscription) values.
929 * -EINVAL on missing subscription entry
931 int can_dev_unregister(struct net_device *dev,
932 void (*func)(unsigned long msg, void *), void *data)
934 struct notifier *n, *next;
937 DBG("called for %s\n", dev->name);
939 write_lock(¬ifier_lock);
940 list_for_each_entry_safe(n, next, ¬ifier_list, list) {
941 if (n->dev == dev && n->func == func && n->data == data) {
948 write_unlock(¬ifier_lock);
952 EXPORT_SYMBOL_GPL(can_dev_unregister);
954 static int can_notifier(struct notifier_block *nb,
955 unsigned long msg, void *data)
957 struct net_device *dev = (struct net_device *)data;
959 struct dev_rcv_lists *d;
962 DBG("called for %s, msg = %lu\n", dev->name, msg);
964 if (dev->type != ARPHRD_CAN)
969 case NETDEV_REGISTER:
972 * create new dev_rcv_lists for this device
974 * N.B. zeroing the struct is the correct initialization
975 * for the embedded hlist_head structs.
976 * Another list type, e.g. list_head, would require
977 * explicit initialization.
980 DBG("creating new dev_rcv_lists for %s\n", dev->name);
982 d = kzalloc(sizeof(*d),
983 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
985 printk(KERN_ERR "can: allocation of receive "
991 spin_lock_bh(&rcv_lists_lock);
992 hlist_add_head_rcu(&d->list, &rx_dev_list);
993 spin_unlock_bh(&rcv_lists_lock);
997 case NETDEV_UNREGISTER:
998 spin_lock_bh(&rcv_lists_lock);
1000 d = find_dev_rcv_lists(dev);
1002 hlist_del_rcu(&d->list);
1004 /* remove all receivers hooked at this netdevice */
1005 can_rx_delete_all(&d->rx_err);
1006 can_rx_delete_all(&d->rx_all);
1007 can_rx_delete_all(&d->rx_fil);
1008 can_rx_delete_all(&d->rx_inv);
1009 can_rx_delete_all(&d->rx_eff);
1010 for (i = 0; i < 2048; i++)
1011 can_rx_delete_all(&d->rx_sff[i]);
1013 printk(KERN_ERR "can: notifier: receive list not "
1014 "found for dev %s\n", dev->name);
1016 spin_unlock_bh(&rcv_lists_lock);
1019 call_rcu(&d->rcu, can_rcv_lists_delete);
1024 read_lock(¬ifier_lock);
1025 list_for_each_entry(n, ¬ifier_list, list) {
1027 n->func(msg, n->data);
1029 read_unlock(¬ifier_lock);
1035 * af_can module init/exit functions
1038 static struct packet_type can_packet = {
1039 .type = __constant_htons(ETH_P_CAN),
1044 static struct net_proto_family can_family_ops = {
1046 .create = can_create,
1047 .owner = THIS_MODULE,
1050 /* notifier block for netdevice event */
1051 static struct notifier_block can_netdev_notifier = {
1052 .notifier_call = can_notifier,
1055 static __init int can_init(void)
1059 rcv_cache = kmem_cache_create("can_receiver", sizeof(struct receiver),
1065 * Insert struct dev_rcv_lists for reception on all devices.
1066 * This struct is zero initialized which is correct for the
1067 * embedded hlist heads, the dev pointer, and the entries counter.
1070 spin_lock_bh(&rcv_lists_lock);
1071 hlist_add_head_rcu(&rx_alldev_list.list, &rx_dev_list);
1072 spin_unlock_bh(&rcv_lists_lock);
1075 init_timer(&stattimer);
1080 /* protocol register */
1081 sock_register(&can_family_ops);
1082 register_netdevice_notifier(&can_netdev_notifier);
1083 dev_add_pack(&can_packet);
1088 static __exit void can_exit(void)
1090 struct dev_rcv_lists *d;
1091 struct hlist_node *n, *next;
1094 del_timer(&stattimer);
1099 /* protocol unregister */
1100 dev_remove_pack(&can_packet);
1101 unregister_netdevice_notifier(&can_netdev_notifier);
1102 sock_unregister(PF_CAN);
1104 /* remove rx_dev_list */
1105 spin_lock_bh(&rcv_lists_lock);
1106 hlist_del(&rx_alldev_list.list);
1107 hlist_for_each_entry_safe(d, n, next, &rx_dev_list, list) {
1108 hlist_del(&d->list);
1111 spin_unlock_bh(&rcv_lists_lock);
1113 kmem_cache_destroy(rcv_cache);
1116 module_init(can_init);
1117 module_exit(can_exit);