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>
60 #include <linux/can.h>
61 #include <linux/can/core.h>
63 #include <asm/uaccess.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[sizeof("can-proto-000")];
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 ret = request_module(module_name);
181 /* In case of error we only print a message but don't
182 * return the error code immediately. Below we will
183 * return -EPROTONOSUPPORT
186 printk(KERN_INFO "can: request_module(%s) not"
187 " implemented.\n", module_name);
189 printk(KERN_ERR "can: request_module(%s) failed\n",
193 /* check for success and correct type */
194 cp = proto_tab[protocol];
195 if (!cp || cp->type != sock->type)
196 return -EPROTONOSUPPORT;
198 if (cp->capability >= 0 && !capable(cp->capability))
203 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
204 sk = sk_alloc(PF_CAN, GFP_KERNEL, cp->prot, 1);
208 sk = sk_alloc(PF_CAN, GFP_KERNEL, 1, 0);
213 sk->sk_protinfo = kmalloc(cp->obj_size, GFP_KERNEL);
214 if (!sk->sk_protinfo) {
219 sk_set_owner(sk, proto_tab[protocol]->owner);
222 sock_init_data(sock, sk);
223 sk->sk_destruct = can_sock_destruct;
225 DBG("created sock: %p\n", sk);
227 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
228 if (sk->sk_prot->init)
229 ret = sk->sk_prot->init(sk);
236 /* release sk on errors */
249 * can_send - transmit a CAN frame (optional with local loopback)
250 * @skb: pointer to socket buffer with CAN frame in data section
251 * @loop: loopback for listeners on local CAN sockets (recommended default!)
255 * -ENETDOWN when the selected interface is down
256 * -ENOBUFS on full driver queue (see net_xmit_errno())
258 int can_send(struct sk_buff *skb, int loop)
260 struct sock **tx_sk = (struct sock **)skb->cb;
263 if (skb->dev->type != ARPHRD_CAN) {
269 /* local loopback of sent CAN frames (default) */
271 /* indication for the CAN driver: do loopback */
275 * The reference to the originating sock may be also required
276 * by the receiving socket to indicate (and ignore) his own
277 * sent data. Example: can_raw sockopt CAN_RAW_RECV_OWN_MSGS
280 /* interface not capabable to do the loopback itself? */
281 if (!(skb->dev->flags & IFF_LOOPBACK)) {
282 struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
284 /* perform the local loopback here */
285 newskb->protocol = htons(ETH_P_CAN);
286 newskb->ip_summed = CHECKSUM_UNNECESSARY;
290 /* indication for the CAN driver: no loopback required */
294 if (!(skb->dev->flags & IFF_UP))
297 /* send to netdevice */
298 err = dev_queue_xmit(skb);
300 err = net_xmit_errno(err);
302 /* update statistics */
304 stats.tx_frames_delta++;
308 EXPORT_SYMBOL(can_send);
314 static struct dev_rcv_lists *find_dev_rcv_lists(struct net_device *dev)
316 struct dev_rcv_lists *d;
317 struct hlist_node *n;
320 * find receive list for this device
322 * The hlist_for_each_entry*() macros curse through the list
323 * using the pointer variable n and set d to the containing
324 * struct in each list iteration. Therefore, after list
325 * iteration, d is unmodified when the list is empty, and it
326 * points to last list element, when the list is non-empty
327 * but no match in the loop body is found. I.e. d is *not*
328 * NULL when no match is found. We can, however, use the
329 * cursor variable n to decide if a match was found.
332 hlist_for_each_entry(d, n, &rx_dev_list, list) {
340 static struct hlist_head *find_rcv_list(canid_t *can_id, canid_t *mask,
341 struct dev_rcv_lists *d)
343 canid_t inv = *can_id & CAN_INV_FILTER; /* save flag before masking */
345 /* filter error frames */
346 if (*mask & CAN_ERR_FLAG) {
347 /* clear CAN_ERR_FLAG in list entry */
348 *mask &= CAN_ERR_MASK;
352 /* ensure valid values in can_mask */
353 if (*mask & CAN_EFF_FLAG)
354 *mask &= (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG);
356 *mask &= (CAN_SFF_MASK | CAN_RTR_FLAG);
358 /* reduce condition testing at receive time */
361 /* inverse can_id/can_mask filter */
365 /* mask == 0 => no condition testing at receive time */
369 /* use extra filterset for the subscription of exactly *ONE* can_id */
370 if (*can_id & CAN_EFF_FLAG) {
371 if (*mask == (CAN_EFF_MASK | CAN_EFF_FLAG)) {
372 /* RFC: a use-case for hash-tables in the future? */
376 if (*mask == CAN_SFF_MASK)
377 return &d->rx_sff[*can_id];
380 /* default: filter via can_id/can_mask */
385 * can_rx_register - subscribe CAN frames from a specific interface
386 * @dev: pointer to netdevice (NULL => subcribe from 'all' CAN devices list)
387 * @can_id: CAN identifier (see description)
388 * @mask: CAN mask (see description)
389 * @func: callback function on filter match
390 * @data: returned parameter for callback function
391 * @ident: string for calling module indentification
394 * Invokes the callback function with the received sk_buff and the given
395 * parameter 'data' on a matching receive filter. A filter matches, when
397 * <received_can_id> & mask == can_id & mask
399 * The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can
400 * filter for error frames (CAN_ERR_FLAG bit set in mask).
404 * -ENOMEM on missing cache mem to create subscription entry
405 * -ENODEV unknown device
407 int can_rx_register(struct net_device *dev, canid_t can_id, canid_t mask,
408 void (*func)(struct sk_buff *, void *), void *data,
412 struct hlist_head *rl;
413 struct dev_rcv_lists *d;
416 /* insert new receiver (dev,canid,mask) -> (func,data) */
418 DBG("dev %p, id %03X, mask %03X, callback %p, data %p, ident %s\n",
419 dev, can_id, mask, func, data, ident);
421 r = kmem_cache_alloc(rcv_cache, GFP_KERNEL);
425 spin_lock_bh(&rcv_lists_lock);
427 d = find_dev_rcv_lists(dev);
429 rl = find_rcv_list(&can_id, &mask, d);
438 hlist_add_head_rcu(&r->list, rl);
441 pstats.rcv_entries++;
442 if (pstats.rcv_entries_max < pstats.rcv_entries)
443 pstats.rcv_entries_max = pstats.rcv_entries;
445 DBG("receive list not found for dev %s, id %03X, mask %03X\n",
446 DNAME(dev), can_id, mask);
447 kmem_cache_free(rcv_cache, r);
451 spin_unlock_bh(&rcv_lists_lock);
455 EXPORT_SYMBOL(can_rx_register);
457 static void can_rx_delete_list(struct hlist_head *rl)
460 struct hlist_node *n;
462 hlist_for_each_entry_rcu(r, n, rl, list) {
463 hlist_del_rcu(&r->list);
464 kmem_cache_free(rcv_cache, r);
469 * can_rx_delete_device - rcu callback for dev_rcv_lists structure removal
471 static void can_rx_delete_device(struct rcu_head *rp)
473 struct dev_rcv_lists *d = container_of(rp, struct dev_rcv_lists, rcu);
476 /* remove all receivers hooked at this netdevice */
477 can_rx_delete_list(&d->rx_err);
478 can_rx_delete_list(&d->rx_all);
479 can_rx_delete_list(&d->rx_fil);
480 can_rx_delete_list(&d->rx_inv);
481 can_rx_delete_list(&d->rx_eff);
483 for (i = 0; i < 2048; i++)
484 can_rx_delete_list(&d->rx_sff[i]);
490 * can_rx_delete_receiver - rcu callback for single receiver entry removal
492 static void can_rx_delete_receiver(struct rcu_head *rp)
494 struct receiver *r = container_of(rp, struct receiver, rcu);
496 kmem_cache_free(rcv_cache, r);
500 * can_rx_unregister - unsubscribe CAN frames from a specific interface
501 * @dev: pointer to netdevice (NULL => unsubcribe from 'all' CAN devices list)
502 * @can_id: CAN identifier
504 * @func: callback function on filter match
505 * @data: returned parameter for callback function
508 * Removes subscription entry depending on given (subscription) values.
512 * -EINVAL on missing subscription entry
513 * -ENODEV unknown device
515 int can_rx_unregister(struct net_device *dev, canid_t can_id, canid_t mask,
516 void (*func)(struct sk_buff *, void *), void *data)
518 struct receiver *r = NULL;
519 struct hlist_head *rl;
520 struct hlist_node *next;
521 struct dev_rcv_lists *d;
524 DBG("dev %p, id %03X, mask %03X, callback %p, data %p\n",
525 dev, can_id, mask, func, data);
527 spin_lock_bh(&rcv_lists_lock);
529 d = find_dev_rcv_lists(dev);
531 DBG("receive list not found for dev %s, id %03X, mask %03X\n",
532 DNAME(dev), can_id, mask);
537 rl = find_rcv_list(&can_id, &mask, d);
540 * Search the receiver list for the item to delete. This should
541 * exist, since no receiver may be unregistered that hasn't
542 * been registered before.
545 hlist_for_each_entry(r, next, rl, list) {
546 if (r->can_id == can_id && r->mask == mask
547 && r->func == func && r->data == data)
552 * Check for bug in CAN protocol implementations:
553 * If no matching list item was found, the list cursor variable next
554 * will be NULL, while r will point to the last item of the list.
558 DBG("receive list entry not found for "
559 "dev %s, id %03X, mask %03X\n", DNAME(dev), can_id, mask);
565 hlist_del_rcu(&r->list);
568 if (pstats.rcv_entries > 0)
569 pstats.rcv_entries--;
572 spin_unlock_bh(&rcv_lists_lock);
574 /* schedule the receiver item for deletion */
576 call_rcu(&r->rcu, can_rx_delete_receiver);
580 EXPORT_SYMBOL(can_rx_unregister);
582 static inline void deliver(struct sk_buff *skb, struct receiver *r)
584 struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
586 DBG("skbuff %p cloned to %p\n", skb, clone);
588 r->func(clone, r->data);
593 static int can_rcv_filter(struct dev_rcv_lists *d, struct sk_buff *skb)
596 struct hlist_node *n;
598 struct can_frame *cf = (struct can_frame*)skb->data;
599 canid_t can_id = cf->can_id;
604 if (can_id & CAN_ERR_FLAG) {
605 /* check for error frame entries only */
606 hlist_for_each_entry_rcu(r, n, &d->rx_err, list) {
607 if (can_id & r->mask) {
608 DBG("match on rx_err skbuff %p\n", skb);
616 /* check for unfiltered entries */
617 hlist_for_each_entry_rcu(r, n, &d->rx_all, list) {
618 DBG("match on rx_all skbuff %p\n", skb);
623 /* check for can_id/mask entries */
624 hlist_for_each_entry_rcu(r, n, &d->rx_fil, list) {
625 if ((can_id & r->mask) == r->can_id) {
626 DBG("match on rx_fil skbuff %p\n", skb);
632 /* check for inverted can_id/mask entries */
633 hlist_for_each_entry_rcu(r, n, &d->rx_inv, list) {
634 if ((can_id & r->mask) != r->can_id) {
635 DBG("match on rx_inv skbuff %p\n", skb);
641 /* check CAN_ID specific entries */
642 if (can_id & CAN_EFF_FLAG) {
643 hlist_for_each_entry_rcu(r, n, &d->rx_eff, list) {
644 if (r->can_id == can_id) {
645 DBG("match on rx_eff skbuff %p\n", skb);
651 can_id &= CAN_SFF_MASK;
652 hlist_for_each_entry_rcu(r, n, &d->rx_sff[can_id], list) {
653 DBG("match on rx_sff skbuff %p\n", skb);
662 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
663 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
664 struct packet_type *pt, struct net_device *orig_dev)
666 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
667 struct packet_type *pt)
670 struct dev_rcv_lists *d;
673 DBG("received skbuff on device %s, ptype %04x\n",
674 dev->name, ntohs(pt->type));
676 DBG_FRAME("af_can: can_rcv: received CAN frame",
677 (struct can_frame *)skb->data);
679 if (dev->type != ARPHRD_CAN) {
684 /* update statistics */
686 stats.rx_frames_delta++;
690 /* deliver the packet to sockets listening on all devices */
691 matches = can_rcv_filter(&rx_alldev_list, skb);
693 /* find receive list for this device */
694 d = find_dev_rcv_lists(dev);
696 matches += can_rcv_filter(d, skb);
700 /* free the skbuff allocated by the netdevice driver */
701 DBG("freeing skbuff %p\n", skb);
706 stats.matches_delta++;
713 * af_can debugging stuff
716 #ifdef CONFIG_CAN_DEBUG_CORE
718 #define DBG_BSIZE 1024
721 * can_debug_cframe - print CAN frame
722 * @msg: pointer to message printed before the given CAN frame
723 * @cf: pointer to CAN frame
725 void can_debug_cframe(const char *msg, struct can_frame *cf, ...)
732 buf = kmalloc(DBG_BSIZE, GFP_ATOMIC);
736 len = sprintf(buf, KERN_DEBUG);
738 len += snprintf(buf + len, DBG_BSIZE - 64, msg, ap);
747 if (cf->can_id & CAN_EFF_FLAG)
748 len += sprintf(buf + len, "<%08X> [%X] ",
749 cf->can_id & CAN_EFF_MASK, dlc);
751 len += sprintf(buf + len, "<%03X> [%X] ",
752 cf->can_id & CAN_SFF_MASK, dlc);
754 for (i = 0; i < dlc; i++)
755 len += sprintf(buf + len, "%02X ", cf->data[i]);
757 if (cf->can_id & CAN_RTR_FLAG)
758 len += sprintf(buf + len, "(RTR)");
765 EXPORT_SYMBOL(can_debug_cframe);
768 * can_debug_skb - print socket buffer content to kernel log
769 * @skb: pointer to socket buffer
771 void can_debug_skb(struct sk_buff *skb)
776 buf = kmalloc(DBG_BSIZE, GFP_ATOMIC);
781 KERN_DEBUG " skbuff at %p, dev: %d, proto: %04x\n"
782 KERN_DEBUG " users: %d, dataref: %d, nr_frags: %d, "
783 "h,d,t,e,l: %p %+d %+d %+d, %d",
784 skb, skb->dev ? skb->dev->ifindex : -1,
785 ntohs(skb->protocol),
786 atomic_read(&skb->users),
787 atomic_read(&(skb_shinfo(skb)->dataref)),
788 skb_shinfo(skb)->nr_frags,
789 skb->head, skb->data - skb->head,
790 skb->tail - skb->head, skb->end - skb->head, skb->len);
791 nbytes = skb->end - skb->head;
792 for (i = 0; i < nbytes; i++) {
794 len += sprintf(buf + len, "\n" KERN_DEBUG " ");
795 if (len < DBG_BSIZE - 16) {
796 len += sprintf(buf + len, " %02x", skb->head[i]);
798 len += sprintf(buf + len, "...");
807 EXPORT_SYMBOL(can_debug_skb);
812 * af_can protocol functions
816 * can_proto_register - register CAN transport protocol
817 * @cp: pointer to CAN protocol structure
821 * -EINVAL invalid (out of range) protocol number
822 * -EBUSY protocol already in use
823 * -ENOBUF if proto_register() fails
825 int can_proto_register(struct can_proto *cp)
827 int proto = cp->protocol;
830 if (proto < 0 || proto >= CAN_NPROTO) {
831 printk(KERN_ERR "can: protocol number %d out "
832 "of range\n", proto);
835 if (proto_tab[proto]) {
836 printk(KERN_ERR "can: protocol %d already "
837 "registered\n", proto);
841 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
842 err = proto_register(cp->prot, 0);
847 proto_tab[proto] = cp;
849 /* use generic ioctl function if the module doesn't bring its own */
851 cp->ops->ioctl = can_ioctl;
855 EXPORT_SYMBOL(can_proto_register);
858 * can_proto_unregister - unregister CAN transport protocol
859 * @cp: pointer to CAN protocol structure
863 * -ESRCH protocol number was not registered
865 int can_proto_unregister(struct can_proto *cp)
867 int proto = cp->protocol;
869 if (!proto_tab[proto]) {
870 printk(KERN_ERR "can: protocol %d is not registered\n", proto);
873 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
874 proto_unregister(cp->prot);
876 proto_tab[proto] = NULL;
880 EXPORT_SYMBOL(can_proto_unregister);
883 * can_dev_register - subscribe notifier for CAN device status changes
884 * @dev: pointer to netdevice
885 * @func: callback function on status change
886 * @data: returned parameter for callback function
889 * Invokes the callback function with the status 'msg' and the given
890 * parameter 'data' on a status change of the given CAN network device.
894 * -ENOMEM on missing mem to create subscription entry
895 * -ENODEV unknown device
897 int can_dev_register(struct net_device *dev,
898 void (*func)(unsigned long msg, void *), void *data)
902 DBG("called for %s\n", dev->name);
904 if (!dev || dev->type != ARPHRD_CAN)
907 n = kmalloc(sizeof(*n), GFP_KERNEL);
915 write_lock(¬ifier_lock);
916 list_add(&n->list, ¬ifier_list);
917 write_unlock(¬ifier_lock);
921 EXPORT_SYMBOL(can_dev_register);
924 * can_dev_unregister - unsubscribe notifier for CAN device status changes
925 * @dev: pointer to netdevice
926 * @func: callback function on filter match
927 * @data: returned parameter for callback function
930 * Removes subscription entry depending on given (subscription) values.
934 * -EINVAL on missing subscription entry
936 int can_dev_unregister(struct net_device *dev,
937 void (*func)(unsigned long msg, void *), void *data)
939 struct notifier *n, *next;
942 DBG("called for %s\n", dev->name);
944 write_lock(¬ifier_lock);
945 list_for_each_entry_safe(n, next, ¬ifier_list, list) {
946 if (n->dev == dev && n->func == func && n->data == data) {
953 write_unlock(¬ifier_lock);
957 EXPORT_SYMBOL(can_dev_unregister);
959 static int can_notifier(struct notifier_block *nb,
960 unsigned long msg, void *data)
962 struct net_device *dev = (struct net_device *)data;
964 struct dev_rcv_lists *d;
966 DBG("called for %s, msg = %lu\n", dev->name, msg);
968 if (dev->type != ARPHRD_CAN)
973 case NETDEV_REGISTER:
976 * create new dev_rcv_lists for this device
978 * N.B. zeroing the struct is the correct initialization
979 * for the embedded hlist_head structs.
980 * Another list type, e.g. list_head, would require
981 * explicit initialization.
984 DBG("creating new dev_rcv_lists for %s\n", dev->name);
986 d = kzalloc(sizeof(*d),
987 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
989 printk(KERN_ERR "can: allocation of receive "
995 spin_lock_bh(&rcv_lists_lock);
996 hlist_add_head_rcu(&d->list, &rx_dev_list);
997 spin_unlock_bh(&rcv_lists_lock);
1001 case NETDEV_UNREGISTER:
1002 spin_lock_bh(&rcv_lists_lock);
1004 d = find_dev_rcv_lists(dev);
1006 hlist_del_rcu(&d->list);
1008 printk(KERN_ERR "can: notifier: receive list not "
1009 "found for dev %s\n", dev->name);
1011 spin_unlock_bh(&rcv_lists_lock);
1014 call_rcu(&d->rcu, can_rx_delete_device);
1019 read_lock(¬ifier_lock);
1020 list_for_each_entry(n, ¬ifier_list, list) {
1022 n->func(msg, n->data);
1024 read_unlock(¬ifier_lock);
1030 * af_can module init/exit functions
1033 static struct packet_type can_packet = {
1034 .type = __constant_htons(ETH_P_CAN),
1039 static struct net_proto_family can_family_ops = {
1041 .create = can_create,
1042 .owner = THIS_MODULE,
1045 /* notifier block for netdevice event */
1046 static struct notifier_block can_netdev_notifier = {
1047 .notifier_call = can_notifier,
1050 static __init int can_init(void)
1054 rcv_cache = kmem_cache_create("can_receiver", sizeof(struct receiver),
1060 * Insert struct dev_rcv_lists for reception on all devices.
1061 * This struct is zero initialized which is correct for the
1062 * embedded hlist heads, the dev pointer, and the entries counter.
1065 spin_lock_bh(&rcv_lists_lock);
1066 hlist_add_head_rcu(&rx_alldev_list.list, &rx_dev_list);
1067 spin_unlock_bh(&rcv_lists_lock);
1070 /* the statistics are updated every second (timer triggered) */
1071 init_timer(&stattimer);
1072 stattimer.function = can_stat_update;
1074 /* update every second */
1075 stattimer.expires = jiffies + HZ;
1076 /* start statistics timer */
1077 add_timer(&stattimer);
1079 stattimer.function = NULL;
1084 /* protocol register */
1085 sock_register(&can_family_ops);
1086 register_netdevice_notifier(&can_netdev_notifier);
1087 dev_add_pack(&can_packet);
1092 static __exit void can_exit(void)
1094 struct dev_rcv_lists *d;
1095 struct hlist_node *n, *next;
1098 del_timer(&stattimer);
1103 /* protocol unregister */
1104 dev_remove_pack(&can_packet);
1105 unregister_netdevice_notifier(&can_netdev_notifier);
1106 sock_unregister(PF_CAN);
1108 /* remove rx_dev_list */
1109 spin_lock_bh(&rcv_lists_lock);
1110 hlist_del(&rx_alldev_list.list);
1111 hlist_for_each_entry_safe(d, n, next, &rx_dev_list, list) {
1112 hlist_del(&d->list);
1115 spin_unlock_bh(&rcv_lists_lock);
1117 kmem_cache_destroy(rcv_cache);
1120 module_init(can_init);
1121 module_exit(can_exit);