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/init.h>
49 #include <linux/kmod.h>
50 #include <linux/slab.h>
51 #include <linux/list.h>
52 #include <linux/spinlock.h>
53 #include <linux/rcupdate.h>
54 #include <linux/net.h>
55 #include <linux/netdevice.h>
56 #include <linux/socket.h>
57 #include <linux/if_ether.h>
58 #include <linux/if_arp.h>
59 #include <linux/skbuff.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[] = KERN_INFO
72 "can: controller area network core (" CAN_VERSION_STRING ")\n";
74 MODULE_DESCRIPTION("Controller Area Network PF_CAN core");
75 MODULE_LICENSE("Dual BSD/GPL");
76 MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>, "
77 "Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
79 MODULE_ALIAS_NETPROTO(PF_CAN);
81 int stats_timer = 1; /* default: on */
82 module_param(stats_timer, int, S_IRUGO);
83 MODULE_PARM_DESC(stats_timer, "enable timer for statistics (default:on)");
85 #ifdef CONFIG_CAN_DEBUG_CORE
87 module_param(debug, int, S_IRUGO);
88 MODULE_PARM_DESC(debug, "debug print mask: 1:debug, 2:frames, 4:skbs");
91 HLIST_HEAD(rx_dev_list);
92 static struct dev_rcv_lists rx_alldev_list;
93 static DEFINE_SPINLOCK(rcv_lists_lock);
95 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
96 static struct kmem_cache *rcv_cache __read_mostly;
98 static kmem_cache_t *rcv_cache;
101 /* table of registered CAN protocols */
102 static struct can_proto *proto_tab[CAN_NPROTO];
104 struct timer_list stattimer; /* timer for statistics update */
105 struct s_stats stats; /* packet statistics */
106 struct s_pstats pstats; /* receive list statistics */
108 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
109 static void *kzalloc(size_t size, unsigned int __nocast flags)
111 void *ret = kmalloc(size, flags);
113 memset(ret, 0, size);
119 * af_can socket functions
122 static int can_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
124 struct sock *sk = sock->sk;
129 return sock_get_timestamp(sk, (struct timeval __user *)arg);
132 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
135 return dev_ioctl(cmd, (void __user *)arg);
140 static void can_sock_destruct(struct sock *sk)
142 DBG("called for sock %p\n", sk);
144 skb_queue_purge(&sk->sk_receive_queue);
146 kfree(sk->sk_protinfo);
149 static int can_create(struct socket *sock, int protocol)
152 struct can_proto *cp;
153 char module_name[sizeof("can-proto-000")];
156 DBG("socket %p, type %d, proto %d\n", sock, sock->type, protocol);
158 sock->state = SS_UNCONNECTED;
160 if (protocol < 0 || protocol >= CAN_NPROTO)
163 DBG("looking up proto %d in proto_tab[]\n", protocol);
165 /* try to load protocol module, when CONFIG_KMOD is defined */
166 if (!proto_tab[protocol]) {
167 sprintf(module_name, "can-proto-%d", protocol);
168 ret = request_module(module_name);
171 * In case of error we only print a message but don't
172 * return the error code immediately. Below we will
173 * return -EPROTONOSUPPORT
176 printk(KERN_INFO "can: request_module(%s) not"
177 " implemented.\n", module_name);
179 printk(KERN_ERR "can: request_module(%s) failed\n",
183 /* check for success and correct type */
184 cp = proto_tab[protocol];
185 if (!cp || cp->type != sock->type)
186 return -EPROTONOSUPPORT;
188 if (cp->capability >= 0 && !capable(cp->capability))
193 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
194 sk = sk_alloc(PF_CAN, GFP_KERNEL, cp->prot, 1);
198 sk = sk_alloc(PF_CAN, GFP_KERNEL, 1, 0);
203 sk->sk_protinfo = kmalloc(cp->obj_size, GFP_KERNEL);
204 if (!sk->sk_protinfo) {
209 sk_set_owner(sk, proto_tab[protocol]->owner);
212 sock_init_data(sock, sk);
213 sk->sk_destruct = can_sock_destruct;
215 DBG("created sock: %p\n", sk);
217 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
218 if (sk->sk_prot->init)
219 ret = sk->sk_prot->init(sk);
226 /* release sk on errors */
239 * can_send - transmit a CAN frame (optional with local loopback)
240 * @skb: pointer to socket buffer with CAN frame in data section
241 * @loop: loopback for listeners on local CAN sockets (recommended default!)
245 * -ENETDOWN when the selected interface is down
246 * -ENOBUFS on full driver queue (see net_xmit_errno())
247 * -ENOMEM when local loopback failed at calling skb_clone()
249 int can_send(struct sk_buff *skb, int loop)
253 if (skb->dev->type != ARPHRD_CAN) {
258 if (!(skb->dev->flags & IFF_UP)) {
263 skb->protocol = htons(ETH_P_CAN);
266 /* local loopback of sent CAN frames */
268 /* indication for the CAN driver: do loopback */
269 skb->pkt_type = PACKET_LOOPBACK;
272 * The reference to the originating sock may be required
273 * by the receiving socket to check whether the frame is
274 * its own. Example: can_raw sockopt CAN_RAW_RECV_OWN_MSGS
275 * Therefore we have to ensure that skb->sk remains the
276 * reference to the originating sock by restoring skb->sk
277 * after each skb_clone() or skb_orphan() usage.
280 if (!(skb->dev->flags & IFF_LOOPBACK)) {
282 * If the interface is not capable to do loopback
283 * itself, we do it here.
285 struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
292 newskb->sk = skb->sk;
293 newskb->ip_summed = CHECKSUM_UNNECESSARY;
294 newskb->pkt_type = PACKET_BROADCAST;
298 /* indication for the CAN driver: no loopback required */
299 skb->pkt_type = PACKET_HOST;
302 /* send to netdevice */
303 err = dev_queue_xmit(skb);
305 err = net_xmit_errno(err);
307 /* update statistics */
309 stats.tx_frames_delta++;
313 EXPORT_SYMBOL(can_send);
319 static struct dev_rcv_lists *find_dev_rcv_lists(struct net_device *dev)
321 struct dev_rcv_lists *d;
322 struct hlist_node *n;
325 * find receive list for this device
327 * The hlist_for_each_entry*() macros curse through the list
328 * using the pointer variable n and set d to the containing
329 * struct in each list iteration. Therefore, after list
330 * iteration, d is unmodified when the list is empty, and it
331 * points to last list element, when the list is non-empty
332 * but no match in the loop body is found. I.e. d is *not*
333 * NULL when no match is found. We can, however, use the
334 * cursor variable n to decide if a match was found.
337 hlist_for_each_entry(d, n, &rx_dev_list, list) {
345 static struct hlist_head *find_rcv_list(canid_t *can_id, canid_t *mask,
346 struct dev_rcv_lists *d)
348 canid_t inv = *can_id & CAN_INV_FILTER; /* save flag before masking */
350 /* filter error frames */
351 if (*mask & CAN_ERR_FLAG) {
352 /* clear CAN_ERR_FLAG in list entry */
353 *mask &= CAN_ERR_MASK;
354 return &d->rx[RX_ERR];
357 /* ensure valid values in can_mask */
358 if (*mask & CAN_EFF_FLAG)
359 *mask &= (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG);
361 *mask &= (CAN_SFF_MASK | CAN_RTR_FLAG);
363 /* reduce condition testing at receive time */
366 /* inverse can_id/can_mask filter */
368 return &d->rx[RX_INV];
370 /* mask == 0 => no condition testing at receive time */
372 return &d->rx[RX_ALL];
374 /* use extra filterset for the subscription of exactly *ONE* can_id */
375 if (*can_id & CAN_EFF_FLAG) {
376 if (*mask == (CAN_EFF_MASK | CAN_EFF_FLAG)) {
377 /* RFC: a use-case for hash-tables in the future? */
378 return &d->rx[RX_EFF];
381 if (*mask == CAN_SFF_MASK)
382 return &d->rx_sff[*can_id];
385 /* default: filter via can_id/can_mask */
386 return &d->rx[RX_FIL];
390 * can_rx_register - subscribe CAN frames from a specific interface
391 * @dev: pointer to netdevice (NULL => subcribe from 'all' CAN devices list)
392 * @can_id: CAN identifier (see description)
393 * @mask: CAN mask (see description)
394 * @func: callback function on filter match
395 * @data: returned parameter for callback function
396 * @ident: string for calling module indentification
399 * Invokes the callback function with the received sk_buff and the given
400 * parameter 'data' on a matching receive filter. A filter matches, when
402 * <received_can_id> & mask == can_id & mask
404 * The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can
405 * filter for error frames (CAN_ERR_FLAG bit set in mask).
409 * -ENOMEM on missing cache mem to create subscription entry
410 * -ENODEV unknown device
412 int can_rx_register(struct net_device *dev, canid_t can_id, canid_t mask,
413 void (*func)(struct sk_buff *, void *), void *data,
417 struct hlist_head *rl;
418 struct dev_rcv_lists *d;
421 /* insert new receiver (dev,canid,mask) -> (func,data) */
423 DBG("dev %p (%s), id %03X, mask %03X, callback %p, data %p, "
424 "ident %s\n", dev, DNAME(dev), can_id, mask, func, data, ident);
426 r = kmem_cache_alloc(rcv_cache, GFP_KERNEL);
430 spin_lock_bh(&rcv_lists_lock);
432 d = find_dev_rcv_lists(dev);
434 rl = find_rcv_list(&can_id, &mask, d);
443 hlist_add_head_rcu(&r->list, rl);
446 pstats.rcv_entries++;
447 if (pstats.rcv_entries_max < pstats.rcv_entries)
448 pstats.rcv_entries_max = pstats.rcv_entries;
450 DBG("receive list not found for dev %s, id %03X, mask %03X\n",
451 DNAME(dev), can_id, mask);
452 kmem_cache_free(rcv_cache, r);
456 spin_unlock_bh(&rcv_lists_lock);
460 EXPORT_SYMBOL(can_rx_register);
463 * can_rx_delete_device - rcu callback for dev_rcv_lists structure removal
465 static void can_rx_delete_device(struct rcu_head *rp)
467 struct dev_rcv_lists *d = container_of(rp, struct dev_rcv_lists, rcu);
469 DBG("removing dev_rcv_list at %p\n", d);
474 * can_rx_delete_receiver - rcu callback for single receiver entry removal
476 static void can_rx_delete_receiver(struct rcu_head *rp)
478 struct receiver *r = container_of(rp, struct receiver, rcu);
480 DBG("removing receiver at %p\n", r);
481 kmem_cache_free(rcv_cache, r);
485 * can_rx_unregister - unsubscribe CAN frames from a specific interface
486 * @dev: pointer to netdevice (NULL => unsubcribe from 'all' CAN devices list)
487 * @can_id: CAN identifier
489 * @func: callback function on filter match
490 * @data: returned parameter for callback function
493 * Removes subscription entry depending on given (subscription) values.
497 * -EINVAL on missing subscription entry
498 * -ENODEV unknown device
500 int can_rx_unregister(struct net_device *dev, canid_t can_id, canid_t mask,
501 void (*func)(struct sk_buff *, void *), void *data)
503 struct receiver *r = NULL;
504 struct hlist_head *rl;
505 struct hlist_node *next;
506 struct dev_rcv_lists *d;
509 DBG("dev %p (%s), id %03X, mask %03X, callback %p, data %p\n",
510 dev, DNAME(dev), can_id, mask, func, data);
512 spin_lock_bh(&rcv_lists_lock);
514 d = find_dev_rcv_lists(dev);
516 DBG("receive list not found for dev %s, id %03X, mask %03X\n",
517 DNAME(dev), can_id, mask);
522 rl = find_rcv_list(&can_id, &mask, d);
525 * Search the receiver list for the item to delete. This should
526 * exist, since no receiver may be unregistered that hasn't
527 * been registered before.
530 hlist_for_each_entry(r, next, rl, list) {
531 if (r->can_id == can_id && r->mask == mask
532 && r->func == func && r->data == data)
537 * Check for bug in CAN protocol implementations:
538 * If no matching list item was found, the list cursor variable next
539 * will be NULL, while r will point to the last item of the list.
543 DBG("receive list entry not found for "
544 "dev %s, id %03X, mask %03X\n", DNAME(dev), can_id, mask);
551 hlist_del_rcu(&r->list);
554 if (pstats.rcv_entries > 0)
555 pstats.rcv_entries--;
557 /* remove device structure requested by NETDEV_UNREGISTER */
558 if (d->remove_on_zero_entries && !d->entries) {
559 DBG("removing dev_rcv_list for %s on zero entries\n",
561 hlist_del_rcu(&d->list);
566 spin_unlock_bh(&rcv_lists_lock);
568 /* schedule the receiver item for deletion */
570 call_rcu(&r->rcu, can_rx_delete_receiver);
572 /* schedule the device structure for deletion */
574 call_rcu(&d->rcu, can_rx_delete_device);
578 EXPORT_SYMBOL(can_rx_unregister);
580 static inline void deliver(struct sk_buff *skb, struct receiver *r)
582 struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
584 DBG("skbuff %p cloned to %p\n", skb, clone);
587 r->func(clone, r->data);
592 static int can_rcv_filter(struct dev_rcv_lists *d, struct sk_buff *skb)
595 struct hlist_node *n;
597 struct can_frame *cf = (struct can_frame*)skb->data;
598 canid_t can_id = cf->can_id;
603 if (can_id & CAN_ERR_FLAG) {
604 /* check for error frame entries only */
605 hlist_for_each_entry_rcu(r, n, &d->rx[RX_ERR], list) {
606 if (can_id & r->mask) {
607 DBG("match on rx_err skbuff %p\n", skb);
615 /* check for unfiltered entries */
616 hlist_for_each_entry_rcu(r, n, &d->rx[RX_ALL], list) {
617 DBG("match on rx_all skbuff %p\n", skb);
622 /* check for can_id/mask entries */
623 hlist_for_each_entry_rcu(r, n, &d->rx[RX_FIL], list) {
624 if ((can_id & r->mask) == r->can_id) {
625 DBG("match on rx_fil skbuff %p\n", skb);
631 /* check for inverted can_id/mask entries */
632 hlist_for_each_entry_rcu(r, n, &d->rx[RX_INV], list) {
633 if ((can_id & r->mask) != r->can_id) {
634 DBG("match on rx_inv skbuff %p\n", skb);
640 /* check CAN_ID specific entries */
641 if (can_id & CAN_EFF_FLAG) {
642 hlist_for_each_entry_rcu(r, n, &d->rx[RX_EFF], list) {
643 if (r->can_id == can_id) {
644 DBG("match on rx_eff skbuff %p\n", skb);
650 can_id &= CAN_SFF_MASK;
651 hlist_for_each_entry_rcu(r, n, &d->rx_sff[can_id], list) {
652 DBG("match on rx_sff skbuff %p\n", skb);
661 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
662 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
663 struct packet_type *pt, struct net_device *orig_dev)
665 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
666 struct packet_type *pt)
669 struct dev_rcv_lists *d;
672 DBG("received skbuff on device %s, ptype %04x\n",
673 dev->name, ntohs(pt->type));
675 DBG_FRAME("af_can: can_rcv: received CAN frame",
676 (struct can_frame *)skb->data);
678 if (dev->type != ARPHRD_CAN) {
683 /* update statistics */
685 stats.rx_frames_delta++;
689 /* deliver the packet to sockets listening on all devices */
690 matches = can_rcv_filter(&rx_alldev_list, skb);
692 /* find receive list for this device */
693 d = find_dev_rcv_lists(dev);
695 matches += can_rcv_filter(d, skb);
699 /* free the skbuff allocated by the netdevice driver */
700 DBG("freeing skbuff %p\n", skb);
705 stats.matches_delta++;
712 * af_can protocol functions
716 * can_proto_register - register CAN transport protocol
717 * @cp: pointer to CAN protocol structure
721 * -EINVAL invalid (out of range) protocol number
722 * -EBUSY protocol already in use
723 * -ENOBUF if proto_register() fails
725 int can_proto_register(struct can_proto *cp)
727 int proto = cp->protocol;
730 if (proto < 0 || proto >= CAN_NPROTO) {
731 printk(KERN_ERR "can: protocol number %d out of range\n",
735 if (proto_tab[proto]) {
736 printk(KERN_ERR "can: protocol %d already registered\n",
741 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
742 err = proto_register(cp->prot, 0);
747 proto_tab[proto] = cp;
749 /* use generic ioctl function if the module doesn't bring its own */
751 cp->ops->ioctl = can_ioctl;
755 EXPORT_SYMBOL(can_proto_register);
758 * can_proto_unregister - unregister CAN transport protocol
759 * @cp: pointer to CAN protocol structure
763 * -ESRCH protocol number was not registered
765 int can_proto_unregister(struct can_proto *cp)
767 int proto = cp->protocol;
769 if (!proto_tab[proto]) {
770 printk(KERN_ERR "can: protocol %d is not registered\n", proto);
773 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
774 proto_unregister(cp->prot);
776 proto_tab[proto] = NULL;
780 EXPORT_SYMBOL(can_proto_unregister);
783 * af_can notifier to create/remove CAN netdevice specific structs
785 static int can_notifier(struct notifier_block *nb, unsigned long msg,
788 struct net_device *dev = (struct net_device *)data;
789 struct dev_rcv_lists *d;
791 DBG("msg %ld for dev %p (%s idx %d)\n",
792 msg, dev, dev->name, dev->ifindex);
794 if (dev->type != ARPHRD_CAN)
799 case NETDEV_REGISTER:
802 * create new dev_rcv_lists for this device
804 * N.B. zeroing the struct is the correct initialization
805 * for the embedded hlist_head structs.
806 * Another list type, e.g. list_head, would require
807 * explicit initialization.
810 DBG("creating new dev_rcv_lists for %s\n", dev->name);
812 d = kzalloc(sizeof(*d),
813 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
816 "can: allocation of receive list failed\n");
821 spin_lock_bh(&rcv_lists_lock);
822 hlist_add_head_rcu(&d->list, &rx_dev_list);
823 spin_unlock_bh(&rcv_lists_lock);
827 case NETDEV_UNREGISTER:
828 spin_lock_bh(&rcv_lists_lock);
830 d = find_dev_rcv_lists(dev);
832 DBG("remove dev_rcv_list for %s (%d entries)\n",
833 dev->name, d->entries);
836 d->remove_on_zero_entries = 1;
839 hlist_del_rcu(&d->list);
841 printk(KERN_ERR "can: notifier: receive list not "
842 "found for dev %s\n", dev->name);
844 spin_unlock_bh(&rcv_lists_lock);
847 call_rcu(&d->rcu, can_rx_delete_device);
856 * af_can debugging stuff
859 #ifdef CONFIG_CAN_DEBUG_CORE
861 #define DBG_BSIZE 1024
864 * can_debug_cframe - print CAN frame
865 * @msg: pointer to message printed before the given CAN frame
866 * @cf: pointer to CAN frame
868 void can_debug_cframe(const char *msg, struct can_frame *cf, ...)
875 buf = kmalloc(DBG_BSIZE, GFP_ATOMIC);
879 len = sprintf(buf, KERN_DEBUG);
881 len += snprintf(buf + len, DBG_BSIZE - 64, msg, ap);
890 if (cf->can_id & CAN_EFF_FLAG)
891 len += sprintf(buf + len, "<%08X> [%X] ",
892 cf->can_id & CAN_EFF_MASK, dlc);
894 len += sprintf(buf + len, "<%03X> [%X] ",
895 cf->can_id & CAN_SFF_MASK, dlc);
897 for (i = 0; i < dlc; i++)
898 len += sprintf(buf + len, "%02X ", cf->data[i]);
900 if (cf->can_id & CAN_RTR_FLAG)
901 len += sprintf(buf + len, "(RTR)");
908 EXPORT_SYMBOL(can_debug_cframe);
911 * can_debug_skb - print socket buffer content to kernel log
912 * @skb: pointer to socket buffer
914 void can_debug_skb(struct sk_buff *skb)
919 buf = kmalloc(DBG_BSIZE, GFP_ATOMIC);
924 KERN_DEBUG " skbuff at %p, dev: %d, proto: %04x\n"
925 KERN_DEBUG " users: %d, dataref: %d, nr_frags: %d, "
926 "h,d,t,e,l: %p %+d %+d %+d, %d",
927 skb, skb->dev ? skb->dev->ifindex : -1,
928 ntohs(skb->protocol),
929 atomic_read(&skb->users),
930 atomic_read(&(skb_shinfo(skb)->dataref)),
931 skb_shinfo(skb)->nr_frags,
932 skb->head, skb->data - skb->head,
933 skb->tail - skb->head, skb->end - skb->head, skb->len);
934 nbytes = skb->end - skb->head;
935 for (i = 0; i < nbytes; i++) {
937 len += sprintf(buf + len, "\n" KERN_DEBUG " ");
938 if (len < DBG_BSIZE - 16) {
939 len += sprintf(buf + len, " %02x", skb->head[i]);
941 len += sprintf(buf + len, "...");
950 EXPORT_SYMBOL(can_debug_skb);
955 * af_can module init/exit functions
958 static struct packet_type can_packet = {
959 .type = __constant_htons(ETH_P_CAN),
964 static struct net_proto_family can_family_ops = {
966 .create = can_create,
967 .owner = THIS_MODULE,
970 /* notifier block for netdevice event */
971 static struct notifier_block can_netdev_notifier = {
972 .notifier_call = can_notifier,
975 static __init int can_init(void)
979 rcv_cache = kmem_cache_create("can_receiver", sizeof(struct receiver),
985 * Insert rx_alldev_list for reception on all devices.
986 * This struct is zero initialized which is correct for the
987 * embedded hlist heads, the dev pointer, and the entries counter.
990 spin_lock_bh(&rcv_lists_lock);
991 hlist_add_head_rcu(&rx_alldev_list.list, &rx_dev_list);
992 spin_unlock_bh(&rcv_lists_lock);
995 /* the statistics are updated every second (timer triggered) */
996 init_timer(&stattimer);
997 stattimer.function = can_stat_update;
999 /* update every second */
1000 stattimer.expires = jiffies + HZ;
1001 /* start statistics timer */
1002 add_timer(&stattimer);
1004 stattimer.function = NULL;
1009 /* protocol register */
1010 sock_register(&can_family_ops);
1011 register_netdevice_notifier(&can_netdev_notifier);
1012 dev_add_pack(&can_packet);
1017 static __exit void can_exit(void)
1019 struct dev_rcv_lists *d;
1020 struct hlist_node *n, *next;
1023 del_timer(&stattimer);
1028 /* protocol unregister */
1029 dev_remove_pack(&can_packet);
1030 unregister_netdevice_notifier(&can_netdev_notifier);
1031 sock_unregister(PF_CAN);
1033 /* remove rx_dev_list */
1034 spin_lock_bh(&rcv_lists_lock);
1035 hlist_del(&rx_alldev_list.list);
1036 hlist_for_each_entry_safe(d, n, next, &rx_dev_list, list) {
1037 hlist_del(&d->list);
1040 spin_unlock_bh(&rcv_lists_lock);
1042 kmem_cache_destroy(rcv_cache);
1045 module_init(can_init);
1046 module_exit(can_exit);