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/module.h>
47 #include <linux/version.h>
48 #include <linux/slab.h>
49 #include <linux/kmod.h>
50 #include <linux/init.h>
51 #include <linux/list.h>
52 #include <linux/spinlock.h>
53 #include <linux/rcupdate.h>
54 #include <linux/socket.h>
55 #include <linux/if_ether.h>
56 #include <linux/if_arp.h>
57 #include <linux/skbuff.h>
58 #include <linux/net.h>
59 #include <linux/netdevice.h>
61 #include <asm/uaccess.h>
63 #include <linux/can.h>
64 #include <linux/can/core.h>
67 #include <linux/can/version.h> /* for RCSID. Removed by mkpatch script */
71 static __initdata const char banner[] =
72 KERN_INFO "can: controller area network core # "
73 CAN_VERSION_STRING "\n";
75 MODULE_DESCRIPTION("Controller Area Network PF_CAN core");
76 MODULE_LICENSE("Dual BSD/GPL");
77 MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>, "
78 "Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
80 MODULE_ALIAS_NETPROTO(PF_CAN);
82 int stats_timer = 1; /* default: on */
83 module_param(stats_timer, int, S_IRUGO);
84 MODULE_PARM_DESC(stats_timer, "enable timer for statistics (default:on)");
86 #ifdef CONFIG_CAN_DEBUG_CORE
88 module_param(debug, int, S_IRUGO);
89 MODULE_PARM_DESC(debug, "debug print mask: 1:debug, 2:frames, 4:skbs");
93 struct list_head list;
94 struct net_device *dev;
95 void (*func)(unsigned long msg, void *data);
99 static LIST_HEAD(notifier_list);
100 static DEFINE_RWLOCK(notifier_lock);
102 HLIST_HEAD(rx_dev_list);
103 static struct dev_rcv_lists rx_alldev_list;
104 static DEFINE_SPINLOCK(rcv_lists_lock);
106 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
107 static struct kmem_cache *rcv_cache __read_mostly;
109 static kmem_cache_t *rcv_cache;
112 /* table of registered CAN protocols */
113 static struct can_proto *proto_tab[CAN_NPROTO];
115 struct timer_list stattimer; /* timer for statistics update */
116 struct s_stats stats; /* packet statistics */
117 struct s_pstats pstats; /* receive list statistics */
119 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
120 static void *kzalloc(size_t size, unsigned int __nocast flags)
122 void *ret = kmalloc(size, flags);
124 memset(ret, 0, size);
130 * af_can socket functions
133 static int can_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
135 struct sock *sk = sock->sk;
140 return sock_get_timestamp(sk, (struct timeval __user *)arg);
143 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
146 return dev_ioctl(cmd, (void __user *)arg);
151 static void can_sock_destruct(struct sock *sk)
153 DBG("called for sock %p\n", sk);
155 skb_queue_purge(&sk->sk_receive_queue);
157 kfree(sk->sk_protinfo);
160 static int can_create(struct socket *sock, int protocol)
163 struct can_proto *cp;
164 char module_name[30];
167 DBG("socket %p, type %d, proto %d\n", sock, sock->type, protocol);
169 sock->state = SS_UNCONNECTED;
171 if (protocol < 0 || protocol >= CAN_NPROTO)
174 DBG("looking up proto %d in proto_tab[]\n", protocol);
176 /* try to load protocol module, when CONFIG_KMOD is defined */
177 if (!proto_tab[protocol]) {
178 sprintf(module_name, "can-proto-%d", protocol);
179 if (request_module(module_name) == -ENOSYS)
180 printk(KERN_INFO "can: request_module(%s) not"
181 " implemented.\n", module_name);
184 /* check for success and correct type */
185 cp = proto_tab[protocol];
186 if (!cp || cp->type != sock->type)
187 return -EPROTONOSUPPORT;
189 if (cp->capability >= 0 && !capable(cp->capability))
194 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
195 sk = sk_alloc(PF_CAN, GFP_KERNEL, cp->prot, 1);
199 sk = sk_alloc(PF_CAN, GFP_KERNEL, 1, 0);
204 sk->sk_protinfo = kmalloc(cp->obj_size, GFP_KERNEL);
205 if (!sk->sk_protinfo) {
210 sk_set_owner(sk, proto_tab[protocol]->owner);
213 sock_init_data(sock, sk);
214 sk->sk_destruct = can_sock_destruct;
216 DBG("created sock: %p\n", sk);
218 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
219 if (sk->sk_prot->init)
220 ret = sk->sk_prot->init(sk);
227 /* release sk on errors */
240 * can_send - transmit a CAN frame (optional with local loopback)
241 * @skb: pointer to socket buffer with CAN frame in data section
242 * @loop: loopback for listeners on local CAN sockets (recommended default!)
246 * -ENETDOWN when the selected interface is down
247 * -ENOBUFS on full driver queue (see net_xmit_errno())
249 int can_send(struct sk_buff *skb, int loop)
251 struct sock **tx_sk = (struct sock **)skb->cb;
254 if (skb->dev->type != ARPHRD_CAN) {
260 /* local loopback of sent CAN frames (default) */
262 /* indication for the CAN driver: do loopback */
266 * The reference to the originating sock may be also required
267 * by the receiving socket to indicate (and ignore) his own
268 * sent data. Example: can_raw sockopt CAN_RAW_RECV_OWN_MSGS
271 /* interface not capabable to do the loopback itself? */
272 if (!(skb->dev->flags & IFF_LOOPBACK)) {
273 struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
275 /* perform the local loopback here */
276 newskb->protocol = htons(ETH_P_CAN);
277 newskb->ip_summed = CHECKSUM_UNNECESSARY;
281 /* indication for the CAN driver: no loopback required */
285 if (!(skb->dev->flags & IFF_UP))
288 /* send to netdevice */
289 err = dev_queue_xmit(skb);
291 err = net_xmit_errno(err);
293 /* update statistics */
295 stats.tx_frames_delta++;
299 EXPORT_SYMBOL(can_send);
305 static struct dev_rcv_lists *find_dev_rcv_lists(struct net_device *dev)
307 struct dev_rcv_lists *d;
308 struct hlist_node *n;
311 * find receive list for this device
313 * The hlist_for_each_entry*() macros curse through the list
314 * using the pointer variable n and set d to the containing
315 * struct in each list iteration. Therefore, after list
316 * iteration, d is unmodified when the list is empty, and it
317 * points to last list element, when the list is non-empty
318 * but no match in the loop body is found. I.e. d is *not*
319 * NULL when no match is found. We can, however, use the
320 * cursor variable n to decide if a match was found.
323 hlist_for_each_entry(d, n, &rx_dev_list, list) {
331 static struct hlist_head *find_rcv_list(canid_t *can_id, canid_t *mask,
332 struct dev_rcv_lists *d)
334 canid_t inv = *can_id & CAN_INV_FILTER; /* save flag before masking */
336 /* filter error frames */
337 if (*mask & CAN_ERR_FLAG) {
338 /* clear CAN_ERR_FLAG in list entry */
339 *mask &= CAN_ERR_MASK;
343 /* ensure valid values in can_mask */
344 if (*mask & CAN_EFF_FLAG)
345 *mask &= (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG);
347 *mask &= (CAN_SFF_MASK | CAN_RTR_FLAG);
349 /* reduce condition testing at receive time */
352 /* inverse can_id/can_mask filter */
356 /* mask == 0 => no condition testing at receive time */
360 /* use extra filterset for the subscription of exactly *ONE* can_id */
361 if (*can_id & CAN_EFF_FLAG) {
362 if (*mask == (CAN_EFF_MASK | CAN_EFF_FLAG)) {
363 /* RFC: a use-case for hash-tables in the future? */
367 if (*mask == CAN_SFF_MASK)
368 return &d->rx_sff[*can_id];
371 /* default: filter via can_id/can_mask */
376 * can_rx_register - subscribe CAN frames from a specific interface
377 * @dev: pointer to netdevice (NULL => subcribe from 'all' CAN devices list)
378 * @can_id: CAN identifier (see description)
379 * @mask: CAN mask (see description)
380 * @func: callback function on filter match
381 * @data: returned parameter for callback function
382 * @ident: string for calling module indentification
385 * Invokes the callback function with the received sk_buff and the given
386 * parameter 'data' on a matching receive filter. A filter matches, when
388 * <received_can_id> & mask == can_id & mask
390 * The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can
391 * filter for error frames (CAN_ERR_FLAG bit set in mask).
395 * -ENOMEM on missing cache mem to create subscription entry
396 * -ENODEV unknown device
398 int can_rx_register(struct net_device *dev, canid_t can_id, canid_t mask,
399 void (*func)(struct sk_buff *, void *), void *data,
403 struct hlist_head *rl;
404 struct dev_rcv_lists *d;
407 /* insert new receiver (dev,canid,mask) -> (func,data) */
409 DBG("dev %p, id %03X, mask %03X, callback %p, data %p, ident %s\n",
410 dev, can_id, mask, func, data, ident);
412 r = kmem_cache_alloc(rcv_cache, GFP_KERNEL);
416 spin_lock_bh(&rcv_lists_lock);
418 d = find_dev_rcv_lists(dev);
420 rl = find_rcv_list(&can_id, &mask, d);
429 hlist_add_head_rcu(&r->list, rl);
432 pstats.rcv_entries++;
433 if (pstats.rcv_entries_max < pstats.rcv_entries)
434 pstats.rcv_entries_max = pstats.rcv_entries;
436 DBG("receive list not found for dev %s, id %03X, mask %03X\n",
437 DNAME(dev), can_id, mask);
438 kmem_cache_free(rcv_cache, r);
442 spin_unlock_bh(&rcv_lists_lock);
446 EXPORT_SYMBOL(can_rx_register);
448 static void can_rcv_lists_delete(struct rcu_head *rp)
450 struct dev_rcv_lists *d = container_of(rp, struct dev_rcv_lists, rcu);
455 static void can_rx_delete(struct rcu_head *rp)
457 struct receiver *r = container_of(rp, struct receiver, rcu);
459 kmem_cache_free(rcv_cache, r);
462 static void can_rx_delete_all(struct hlist_head *rl)
465 struct hlist_node *n;
467 hlist_for_each_entry_rcu(r, n, rl, list) {
468 hlist_del_rcu(&r->list);
469 call_rcu(&r->rcu, can_rx_delete);
474 * can_rx_unregister - unsubscribe CAN frames from a specific interface
475 * @dev: pointer to netdevice (NULL => unsubcribe from 'all' CAN devices list)
476 * @can_id: CAN identifier
478 * @func: callback function on filter match
479 * @data: returned parameter for callback function
482 * Removes subscription entry depending on given (subscription) values.
486 * -EINVAL on missing subscription entry
487 * -ENODEV unknown device
489 int can_rx_unregister(struct net_device *dev, canid_t can_id, canid_t mask,
490 void (*func)(struct sk_buff *, void *), void *data)
492 struct receiver *r = NULL;
493 struct hlist_head *rl;
494 struct hlist_node *next;
495 struct dev_rcv_lists *d;
498 DBG("dev %p, id %03X, mask %03X, callback %p, data %p\n",
499 dev, can_id, mask, func, data);
501 spin_lock_bh(&rcv_lists_lock);
503 d = find_dev_rcv_lists(dev);
505 DBG("receive list not found for dev %s, id %03X, mask %03X\n",
506 DNAME(dev), can_id, mask);
511 rl = find_rcv_list(&can_id, &mask, d);
514 * Search the receiver list for the item to delete. This should
515 * exist, since no receiver may be unregistered that hasn't
516 * been registered before.
519 hlist_for_each_entry(r, next, rl, list) {
520 if (r->can_id == can_id && r->mask == mask
521 && r->func == func && r->data == data)
526 * Check for bug in CAN protocol implementations:
527 * If no matching list item was found, the list cursor variable next
528 * will be NULL, while r will point to the last item of the list.
532 DBG("receive list entry not found for "
533 "dev %s, id %03X, mask %03X\n", DNAME(dev), can_id, mask);
539 hlist_del_rcu(&r->list);
542 if (pstats.rcv_entries > 0)
543 pstats.rcv_entries--;
546 spin_unlock_bh(&rcv_lists_lock);
548 /* schedule the receiver item for deletion */
550 call_rcu(&r->rcu, can_rx_delete);
554 EXPORT_SYMBOL(can_rx_unregister);
556 static inline void deliver(struct sk_buff *skb, struct receiver *r)
558 struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
560 DBG("skbuff %p cloned to %p\n", skb, clone);
562 r->func(clone, r->data);
567 static int can_rcv_filter(struct dev_rcv_lists *d, struct sk_buff *skb)
570 struct hlist_node *n;
572 struct can_frame *cf = (struct can_frame*)skb->data;
573 canid_t can_id = cf->can_id;
578 if (can_id & CAN_ERR_FLAG) {
579 /* check for error frame entries only */
580 hlist_for_each_entry_rcu(r, n, &d->rx_err, list) {
581 if (can_id & r->mask) {
582 DBG("match on rx_err skbuff %p\n", skb);
590 /* check for unfiltered entries */
591 hlist_for_each_entry_rcu(r, n, &d->rx_all, list) {
592 DBG("match on rx_all skbuff %p\n", skb);
597 /* check for can_id/mask entries */
598 hlist_for_each_entry_rcu(r, n, &d->rx_fil, list) {
599 if ((can_id & r->mask) == r->can_id) {
600 DBG("match on rx_fil skbuff %p\n", skb);
606 /* check for inverted can_id/mask entries */
607 hlist_for_each_entry_rcu(r, n, &d->rx_inv, list) {
608 if ((can_id & r->mask) != r->can_id) {
609 DBG("match on rx_inv skbuff %p\n", skb);
615 /* check CAN_ID specific entries */
616 if (can_id & CAN_EFF_FLAG) {
617 hlist_for_each_entry_rcu(r, n, &d->rx_eff, list) {
618 if (r->can_id == can_id) {
619 DBG("match on rx_eff skbuff %p\n", skb);
625 can_id &= CAN_SFF_MASK;
626 hlist_for_each_entry_rcu(r, n, &d->rx_sff[can_id], list) {
627 DBG("match on rx_sff skbuff %p\n", skb);
636 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
637 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
638 struct packet_type *pt, struct net_device *orig_dev)
640 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
641 struct packet_type *pt)
644 struct dev_rcv_lists *d;
647 DBG("received skbuff on device %s, ptype %04x\n",
648 dev->name, ntohs(pt->type));
650 DBG_FRAME("af_can: can_rcv: received CAN frame",
651 (struct can_frame *)skb->data);
653 if (dev->type != ARPHRD_CAN) {
658 /* update statistics */
660 stats.rx_frames_delta++;
664 /* deliver the packet to sockets listening on all devices */
665 matches = can_rcv_filter(&rx_alldev_list, skb);
667 /* find receive list for this device */
668 d = find_dev_rcv_lists(dev);
670 matches += can_rcv_filter(d, skb);
674 /* free the skbuff allocated by the netdevice driver */
675 DBG("freeing skbuff %p\n", skb);
680 stats.matches_delta++;
687 * af_can debugging stuff
690 #ifdef CONFIG_CAN_DEBUG_CORE
692 #define DBG_BSIZE 1024
695 * can_debug_cframe - print CAN frame
696 * @msg: pointer to message printed before the given CAN frame
697 * @cf: pointer to CAN frame
699 void can_debug_cframe(const char *msg, struct can_frame *cf, ...)
706 buf = kmalloc(DBG_BSIZE, GFP_ATOMIC);
710 len = sprintf(buf, KERN_DEBUG);
712 len += snprintf(buf + len, DBG_BSIZE - 64, msg, ap);
721 if (cf->can_id & CAN_EFF_FLAG)
722 len += sprintf(buf + len, "<%08X> [%X] ",
723 cf->can_id & CAN_EFF_MASK, dlc);
725 len += sprintf(buf + len, "<%03X> [%X] ",
726 cf->can_id & CAN_SFF_MASK, dlc);
728 for (i = 0; i < dlc; i++)
729 len += sprintf(buf + len, "%02X ", cf->data[i]);
731 if (cf->can_id & CAN_RTR_FLAG)
732 len += sprintf(buf + len, "(RTR)");
739 EXPORT_SYMBOL(can_debug_cframe);
742 * can_debug_skb - print socket buffer content to kernel log
743 * @skb: pointer to socket buffer
745 void can_debug_skb(struct sk_buff *skb)
750 buf = kmalloc(DBG_BSIZE, GFP_ATOMIC);
755 KERN_DEBUG " skbuff at %p, dev: %d, proto: %04x\n"
756 KERN_DEBUG " users: %d, dataref: %d, nr_frags: %d, "
757 "h,d,t,e,l: %p %+d %+d %+d, %d",
758 skb, skb->dev ? skb->dev->ifindex : -1,
759 ntohs(skb->protocol),
760 atomic_read(&skb->users),
761 atomic_read(&(skb_shinfo(skb)->dataref)),
762 skb_shinfo(skb)->nr_frags,
763 skb->head, skb->data - skb->head,
764 skb->tail - skb->head, skb->end - skb->head, skb->len);
765 nbytes = skb->end - skb->head;
766 for (i = 0; i < nbytes; i++) {
768 len += sprintf(buf + len, "\n" KERN_DEBUG " ");
769 if (len < DBG_BSIZE - 16) {
770 len += sprintf(buf + len, " %02x", skb->head[i]);
772 len += sprintf(buf + len, "...");
781 EXPORT_SYMBOL(can_debug_skb);
786 * af_can protocol functions
790 * can_proto_register - register CAN transport protocol
791 * @cp: pointer to CAN protocol structure
795 * -EINVAL invalid (out of range) protocol number
796 * -EBUSY protocol already in use
797 * -ENOBUF if proto_register() fails
799 int can_proto_register(struct can_proto *cp)
801 int proto = cp->protocol;
804 if (proto < 0 || proto >= CAN_NPROTO) {
805 printk(KERN_ERR "can: protocol number %d out "
806 "of range\n", proto);
809 if (proto_tab[proto]) {
810 printk(KERN_ERR "can: protocol %d already "
811 "registered\n", proto);
815 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
816 err = proto_register(cp->prot, 0);
821 proto_tab[proto] = cp;
823 /* use generic ioctl function if the module doesn't bring its own */
825 cp->ops->ioctl = can_ioctl;
829 EXPORT_SYMBOL(can_proto_register);
832 * can_proto_unregister - unregister CAN transport protocol
833 * @cp: pointer to CAN protocol structure
837 * -ESRCH protocol number was not registered
839 int can_proto_unregister(struct can_proto *cp)
841 int proto = cp->protocol;
843 if (!proto_tab[proto]) {
844 printk(KERN_ERR "can: protocol %d is not registered\n", proto);
847 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
848 proto_unregister(cp->prot);
850 proto_tab[proto] = NULL;
854 EXPORT_SYMBOL(can_proto_unregister);
857 * can_dev_register - subscribe notifier for CAN device status changes
858 * @dev: pointer to netdevice
859 * @func: callback function on status change
860 * @data: returned parameter for callback function
863 * Invokes the callback function with the status 'msg' and the given
864 * parameter 'data' on a status change of the given CAN network device.
868 * -ENOMEM on missing mem to create subscription entry
869 * -ENODEV unknown device
871 int can_dev_register(struct net_device *dev,
872 void (*func)(unsigned long msg, void *), void *data)
876 DBG("called for %s\n", dev->name);
878 if (!dev || dev->type != ARPHRD_CAN)
881 n = kmalloc(sizeof(*n), GFP_KERNEL);
889 write_lock(¬ifier_lock);
890 list_add(&n->list, ¬ifier_list);
891 write_unlock(¬ifier_lock);
895 EXPORT_SYMBOL(can_dev_register);
898 * can_dev_unregister - unsubscribe notifier for CAN device status changes
899 * @dev: pointer to netdevice
900 * @func: callback function on filter match
901 * @data: returned parameter for callback function
904 * Removes subscription entry depending on given (subscription) values.
908 * -EINVAL on missing subscription entry
910 int can_dev_unregister(struct net_device *dev,
911 void (*func)(unsigned long msg, void *), void *data)
913 struct notifier *n, *next;
916 DBG("called for %s\n", dev->name);
918 write_lock(¬ifier_lock);
919 list_for_each_entry_safe(n, next, ¬ifier_list, list) {
920 if (n->dev == dev && n->func == func && n->data == data) {
927 write_unlock(¬ifier_lock);
931 EXPORT_SYMBOL(can_dev_unregister);
933 static int can_notifier(struct notifier_block *nb,
934 unsigned long msg, void *data)
936 struct net_device *dev = (struct net_device *)data;
938 struct dev_rcv_lists *d;
941 DBG("called for %s, msg = %lu\n", dev->name, msg);
943 if (dev->type != ARPHRD_CAN)
948 case NETDEV_REGISTER:
951 * create new dev_rcv_lists for this device
953 * N.B. zeroing the struct is the correct initialization
954 * for the embedded hlist_head structs.
955 * Another list type, e.g. list_head, would require
956 * explicit initialization.
959 DBG("creating new dev_rcv_lists for %s\n", dev->name);
961 d = kzalloc(sizeof(*d),
962 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
964 printk(KERN_ERR "can: allocation of receive "
970 spin_lock_bh(&rcv_lists_lock);
971 hlist_add_head_rcu(&d->list, &rx_dev_list);
972 spin_unlock_bh(&rcv_lists_lock);
976 case NETDEV_UNREGISTER:
977 spin_lock_bh(&rcv_lists_lock);
979 d = find_dev_rcv_lists(dev);
981 hlist_del_rcu(&d->list);
983 /* remove all receivers hooked at this netdevice */
984 can_rx_delete_all(&d->rx_err);
985 can_rx_delete_all(&d->rx_all);
986 can_rx_delete_all(&d->rx_fil);
987 can_rx_delete_all(&d->rx_inv);
988 can_rx_delete_all(&d->rx_eff);
989 for (i = 0; i < 2048; i++)
990 can_rx_delete_all(&d->rx_sff[i]);
992 printk(KERN_ERR "can: notifier: receive list not "
993 "found for dev %s\n", dev->name);
995 spin_unlock_bh(&rcv_lists_lock);
998 call_rcu(&d->rcu, can_rcv_lists_delete);
1003 read_lock(¬ifier_lock);
1004 list_for_each_entry(n, ¬ifier_list, list) {
1006 n->func(msg, n->data);
1008 read_unlock(¬ifier_lock);
1014 * af_can module init/exit functions
1017 static struct packet_type can_packet = {
1018 .type = __constant_htons(ETH_P_CAN),
1023 static struct net_proto_family can_family_ops = {
1025 .create = can_create,
1026 .owner = THIS_MODULE,
1029 /* notifier block for netdevice event */
1030 static struct notifier_block can_netdev_notifier = {
1031 .notifier_call = can_notifier,
1034 static __init int can_init(void)
1038 rcv_cache = kmem_cache_create("can_receiver", sizeof(struct receiver),
1044 * Insert struct dev_rcv_lists for reception on all devices.
1045 * This struct is zero initialized which is correct for the
1046 * embedded hlist heads, the dev pointer, and the entries counter.
1049 spin_lock_bh(&rcv_lists_lock);
1050 hlist_add_head_rcu(&rx_alldev_list.list, &rx_dev_list);
1051 spin_unlock_bh(&rcv_lists_lock);
1054 /* the statistics are updated every second (timer triggered) */
1055 init_timer(&stattimer);
1056 stattimer.function = can_stat_update;
1058 /* update every second */
1059 stattimer.expires = jiffies + HZ;
1060 /* start statistics timer */
1061 add_timer(&stattimer);
1063 stattimer.function = NULL;
1068 /* protocol register */
1069 sock_register(&can_family_ops);
1070 register_netdevice_notifier(&can_netdev_notifier);
1071 dev_add_pack(&can_packet);
1076 static __exit void can_exit(void)
1078 struct dev_rcv_lists *d;
1079 struct hlist_node *n, *next;
1082 del_timer(&stattimer);
1087 /* protocol unregister */
1088 dev_remove_pack(&can_packet);
1089 unregister_netdevice_notifier(&can_netdev_notifier);
1090 sock_unregister(PF_CAN);
1092 /* remove rx_dev_list */
1093 spin_lock_bh(&rcv_lists_lock);
1094 hlist_del(&rx_alldev_list.list);
1095 hlist_for_each_entry_safe(d, n, next, &rx_dev_list, list) {
1096 hlist_del(&d->list);
1099 spin_unlock_bh(&rcv_lists_lock);
1101 kmem_cache_destroy(rcv_cache);
1104 module_init(can_init);
1105 module_exit(can_exit);