]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - kernel/2.6/net/can/af_can.c
Removed linux/autoconf.h which is not needed, when compiling inside the
[socketcan-devel.git] / kernel / 2.6 / net / can / af_can.c
1 /*
2  * af_can.c - Protocol family CAN core module
3  *            (used by different CAN protocol modules)
4  *
5  * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
20  *
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.
25  *
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.
28  *
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
40  * DAMAGE.
41  *
42  * Send feedback to <socketcan-users@lists.berlios.de>
43  *
44  */
45
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 <net/sock.h>
61 #include <asm/uaccess.h>
62
63 #include <linux/can.h>
64 #include <linux/can/core.h>
65 #include "af_can.h"
66
67 #include <linux/can/version.h> /* for RCSID. Removed by mkpatch script */
68 RCSID("$Id$");
69
70 #define IDENT "core"
71 static __initdata const char banner[] =
72         KERN_INFO "can: controller area network core # "
73         CAN_VERSION_STRING "\n";
74
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>");
79
80 MODULE_ALIAS_NETPROTO(PF_CAN);
81
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)");
85
86 #ifdef CONFIG_CAN_DEBUG_CORE
87 static int debug = 0;
88 module_param(debug, int, S_IRUGO);
89 MODULE_PARM_DESC(debug, "debug print mask: 1:debug, 2:frames, 4:skbs");
90 #endif
91
92 struct notifier {
93         struct list_head list;
94         struct net_device *dev;
95         void (*func)(unsigned long msg, void *data);
96         void *data;
97 };
98
99 static LIST_HEAD(notifier_list);
100 static DEFINE_RWLOCK(notifier_lock);
101
102 HLIST_HEAD(rx_dev_list);
103 static struct dev_rcv_lists rx_alldev_list;
104 static DEFINE_SPINLOCK(rcv_lists_lock);
105
106 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
107 static struct kmem_cache *rcv_cache __read_mostly;
108 #else
109 static kmem_cache_t *rcv_cache;
110 #endif
111
112 /* table of registered CAN protocols */
113 static struct can_proto *proto_tab[CAN_NPROTO];
114
115 struct timer_list stattimer; /* timer for statistics update */
116 struct s_stats  stats;       /* packet statistics */
117 struct s_pstats pstats;      /* receive list statistics */
118
119 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
120 static void *kzalloc(size_t size, unsigned int __nocast flags)
121 {
122         void *ret = kmalloc(size, flags);
123         if (ret)
124                 memset(ret, 0, size);
125         return ret;
126 }
127 #endif
128
129 /*
130  * af_can socket functions
131  */
132
133 static int can_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
134 {
135         struct sock *sk = sock->sk;
136
137         switch (cmd) {
138
139         case SIOCGSTAMP:
140                 return sock_get_timestamp(sk, (struct timeval __user *)arg);
141
142         default:
143 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
144                 return -ENOIOCTLCMD;
145 #else
146                 return dev_ioctl(cmd, (void __user *)arg);
147 #endif
148         }
149 }
150
151 static void can_sock_destruct(struct sock *sk)
152 {
153         DBG("called for sock %p\n", sk);
154
155         skb_queue_purge(&sk->sk_receive_queue);
156         if (sk->sk_protinfo)
157                 kfree(sk->sk_protinfo);
158 }
159
160 static int can_create(struct socket *sock, int protocol)
161 {
162         struct sock *sk;
163         struct can_proto *cp;
164         char module_name[30];
165         int ret = 0;
166
167         DBG("socket %p, type %d, proto %d\n", sock, sock->type, protocol);
168
169         sock->state = SS_UNCONNECTED;
170
171         if (protocol < 0 || protocol >= CAN_NPROTO)
172                 return -EINVAL;
173
174         DBG("looking up proto %d in proto_tab[]\n", protocol);
175
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);
182         }
183
184         /* check for success and correct type */
185         cp = proto_tab[protocol];
186         if (!cp || cp->type != sock->type)
187                 return -EPROTONOSUPPORT;
188
189         if (cp->capability >= 0 && !capable(cp->capability))
190                 return -EPERM;
191
192         sock->ops = cp->ops;
193
194 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
195         sk = sk_alloc(PF_CAN, GFP_KERNEL, cp->prot, 1);
196         if (!sk)
197                 return -ENOMEM;
198 #else
199         sk = sk_alloc(PF_CAN, GFP_KERNEL, 1, 0);
200         if (!sk)
201                 return -ENOMEM;
202
203         if (cp->obj_size) {
204                 sk->sk_protinfo = kmalloc(cp->obj_size, GFP_KERNEL);
205                 if (!sk->sk_protinfo) {
206                         sk_free(sk);
207                         return -ENOMEM;
208                 }
209         }
210         sk_set_owner(sk, proto_tab[protocol]->owner);
211 #endif
212
213         sock_init_data(sock, sk);
214         sk->sk_destruct = can_sock_destruct;
215
216         DBG("created sock: %p\n", sk);
217
218 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
219         if (sk->sk_prot->init)
220                 ret = sk->sk_prot->init(sk);
221 #else
222         if (cp->init)
223                 ret = cp->init(sk);
224 #endif
225
226         if (ret) {
227                 /* release sk on errors */
228                 sock_orphan(sk);
229                 sock_put(sk);
230         }
231
232         return ret;
233 }
234
235 /*
236  * af_can tx path
237  */
238
239 /**
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!)
243  *
244  * Return:
245  *  0 on success
246  *  -ENETDOWN when the selected interface is down
247  *  -ENOBUFS on full driver queue (see net_xmit_errno())
248  */
249 int can_send(struct sk_buff *skb, int loop)
250 {
251         struct sock **tx_sk = (struct sock **)skb->cb;
252         int err;
253
254         if (skb->dev->type != ARPHRD_CAN) {
255                 kfree_skb(skb);
256                 return -EPERM;
257         }
258
259         if (loop) {
260                 /* local loopback of sent CAN frames (default) */
261
262                 /* indication for the CAN driver: do loopback */
263                 *tx_sk = skb->sk;
264
265                 /*
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
269                  */
270
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);
274
275                         /* perform the local loopback here */
276                         newskb->protocol  = htons(ETH_P_CAN);
277                         newskb->ip_summed = CHECKSUM_UNNECESSARY;
278                         netif_rx(newskb);
279                 }
280         } else {
281                 /* indication for the CAN driver: no loopback required */
282                 *tx_sk = NULL;
283         }
284
285         if (!(skb->dev->flags & IFF_UP))
286                 return -ENETDOWN;
287
288         /* send to netdevice */
289         err = dev_queue_xmit(skb);
290         if (err > 0)
291                 err = net_xmit_errno(err);
292
293         /* update statistics */
294         stats.tx_frames++;
295         stats.tx_frames_delta++;
296
297         return err;
298 }
299 EXPORT_SYMBOL(can_send);
300
301 /*
302  * af_can rx path
303  */
304
305 static struct dev_rcv_lists *find_dev_rcv_lists(struct net_device *dev)
306 {
307         struct dev_rcv_lists *d;
308         struct hlist_node *n;
309
310         /*
311          * find receive list for this device
312          *
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.
321          */
322
323         hlist_for_each_entry(d, n, &rx_dev_list, list) {
324                 if (d->dev == dev)
325                         break;
326         }
327
328         return n ? d : NULL;
329 }
330
331 static struct hlist_head *find_rcv_list(canid_t *can_id, canid_t *mask,
332                                         struct dev_rcv_lists *d)
333 {
334         canid_t inv = *can_id & CAN_INV_FILTER; /* save flag before masking */
335
336         /* filter error frames */
337         if (*mask & CAN_ERR_FLAG) {
338                 /* clear CAN_ERR_FLAG in list entry */
339                 *mask &= CAN_ERR_MASK;
340                 return &d->rx_err;
341         }
342
343         /* ensure valid values in can_mask */
344         if (*mask & CAN_EFF_FLAG)
345                 *mask &= (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG);
346         else
347                 *mask &= (CAN_SFF_MASK | CAN_RTR_FLAG);
348
349         /* reduce condition testing at receive time */
350         *can_id &= *mask;
351
352         /* inverse can_id/can_mask filter */
353         if (inv)
354                 return &d->rx_inv;
355
356         /* mask == 0 => no condition testing at receive time */
357         if (!(*mask))
358                 return &d->rx_all;
359
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? */
364                         return &d->rx_eff;
365                 }
366         } else {
367                 if (*mask == CAN_SFF_MASK)
368                         return &d->rx_sff[*can_id];
369         }
370
371         /* default: filter via can_id/can_mask */
372         return &d->rx_fil;
373 }
374
375 /**
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
383  *
384  * Description:
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
387  *
388  *          <received_can_id> & mask == can_id & mask
389  *
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).
392  *
393  * Return:
394  *  0 on success
395  *  -ENOMEM on missing cache mem to create subscription entry
396  *  -ENODEV unknown device
397  */
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,
400                     char *ident)
401 {
402         struct receiver *r;
403         struct hlist_head *rl;
404         struct dev_rcv_lists *d;
405         int ret = 0;
406
407         /* insert new receiver  (dev,canid,mask) -> (func,data) */
408
409         DBG("dev %p, id %03X, mask %03X, callback %p, data %p, ident %s\n",
410             dev, can_id, mask, func, data, ident);
411
412         r = kmem_cache_alloc(rcv_cache, GFP_KERNEL);
413         if (!r)
414                 return -ENOMEM;
415
416         spin_lock_bh(&rcv_lists_lock);
417
418         d = find_dev_rcv_lists(dev);
419         if (d) {
420                 rl = find_rcv_list(&can_id, &mask, d);
421
422                 r->can_id  = can_id;
423                 r->mask    = mask;
424                 r->matches = 0;
425                 r->func    = func;
426                 r->data    = data;
427                 r->ident   = ident;
428
429                 hlist_add_head_rcu(&r->list, rl);
430                 d->entries++;
431
432                 pstats.rcv_entries++;
433                 if (pstats.rcv_entries_max < pstats.rcv_entries)
434                         pstats.rcv_entries_max = pstats.rcv_entries;
435         } else {
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);
439                 ret = -ENODEV;
440         }
441
442         spin_unlock_bh(&rcv_lists_lock);
443
444         return ret;
445 }
446 EXPORT_SYMBOL(can_rx_register);
447
448 static void can_rcv_lists_delete(struct rcu_head *rp)
449 {
450         struct dev_rcv_lists *d = container_of(rp, struct dev_rcv_lists, rcu);
451
452         kfree(d);
453 }
454
455 static void can_rx_delete(struct rcu_head *rp)
456 {
457         struct receiver *r = container_of(rp, struct receiver, rcu);
458
459         kmem_cache_free(rcv_cache, r);
460 }
461
462 static void can_rx_delete_all(struct hlist_head *rl)
463 {
464         struct receiver *r;
465         struct hlist_node *n;
466
467         hlist_for_each_entry_rcu(r, n, rl, list) {
468                 hlist_del_rcu(&r->list);
469                 call_rcu(&r->rcu, can_rx_delete);
470         }
471 }
472
473 /**
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
477  * @mask: CAN mask
478  * @func: callback function on filter match
479  * @data: returned parameter for callback function
480  *
481  * Description:
482  *  Removes subscription entry depending on given (subscription) values.
483  *
484  * Return:
485  *  0 on success
486  *  -EINVAL on missing subscription entry
487  *  -ENODEV unknown device
488  */
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)
491 {
492         struct receiver *r = NULL;
493         struct hlist_head *rl;
494         struct hlist_node *next;
495         struct dev_rcv_lists *d;
496         int ret = 0;
497
498         DBG("dev %p, id %03X, mask %03X, callback %p, data %p\n",
499             dev, can_id, mask, func, data);
500
501         spin_lock_bh(&rcv_lists_lock);
502
503         d = find_dev_rcv_lists(dev);
504         if (!d) {
505                 DBG("receive list not found for dev %s, id %03X, mask %03X\n",
506                     DNAME(dev), can_id, mask);
507                 ret = -ENODEV;
508                 goto out;
509         }
510
511         rl = find_rcv_list(&can_id, &mask, d);
512
513         /*
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.
517          */
518
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)
522                         break;
523         }
524
525         /*
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.
529          */
530
531         if (!next) {
532                 DBG("receive list entry not found for "
533                     "dev %s, id %03X, mask %03X\n", DNAME(dev), can_id, mask);
534                 ret = -EINVAL;
535                 r = NULL;
536                 goto out;
537         }
538
539         hlist_del_rcu(&r->list);
540         d->entries--;
541
542         if (pstats.rcv_entries > 0)
543                 pstats.rcv_entries--;
544
545  out:
546         spin_unlock_bh(&rcv_lists_lock);
547
548         /* schedule the receiver item for deletion */
549         if (r)
550                 call_rcu(&r->rcu, can_rx_delete);
551
552         return ret;
553 }
554 EXPORT_SYMBOL(can_rx_unregister);
555
556 static inline void deliver(struct sk_buff *skb, struct receiver *r)
557 {
558         struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
559
560         DBG("skbuff %p cloned to %p\n", skb, clone);
561         if (clone) {
562                 r->func(clone, r->data);
563                 r->matches++;
564         }
565 }
566
567 static int can_rcv_filter(struct dev_rcv_lists *d, struct sk_buff *skb)
568 {
569         struct receiver *r;
570         struct hlist_node *n;
571         int matches = 0;
572         struct can_frame *cf = (struct can_frame*)skb->data;
573         canid_t can_id = cf->can_id;
574
575         if (d->entries == 0)
576                 return 0;
577
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);
583                                 deliver(skb, r);
584                                 matches++;
585                         }
586                 }
587                 return matches;
588         }
589
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);
593                 deliver(skb, r);
594                 matches++;
595         }
596
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);
601                         deliver(skb, r);
602                         matches++;
603                 }
604         }
605
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);
610                         deliver(skb, r);
611                         matches++;
612                 }
613         }
614
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);
620                                 deliver(skb, r);
621                                 matches++;
622                         }
623                 }
624         } else {
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);
628                         deliver(skb, r);
629                         matches++;
630                 }
631         }
632
633         return matches;
634 }
635
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)
639 #else
640 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
641                    struct packet_type *pt)
642 #endif
643 {
644         struct dev_rcv_lists *d;
645         int matches;
646
647         DBG("received skbuff on device %s, ptype %04x\n",
648             dev->name, ntohs(pt->type));
649         DBG_SKB(skb);
650         DBG_FRAME("af_can: can_rcv: received CAN frame",
651                   (struct can_frame *)skb->data);
652
653         if (dev->type != ARPHRD_CAN) {
654                 kfree_skb(skb);
655                 return 0;
656         }
657
658         /* update statistics */
659         stats.rx_frames++;
660         stats.rx_frames_delta++;
661
662         rcu_read_lock();
663
664         /* deliver the packet to sockets listening on all devices */
665         matches = can_rcv_filter(&rx_alldev_list, skb);
666
667         /* find receive list for this device */
668         d = find_dev_rcv_lists(dev);
669         if (d)
670                 matches += can_rcv_filter(d, skb);
671
672         rcu_read_unlock();
673
674         /* free the skbuff allocated by the netdevice driver */
675         DBG("freeing skbuff %p\n", skb);
676         kfree_skb(skb);
677
678         if (matches > 0) {
679                 stats.matches++;
680                 stats.matches_delta++;
681         }
682
683         return 0;
684 }
685
686 /*
687  * af_can debugging stuff
688  */
689
690 #ifdef CONFIG_CAN_DEBUG_CORE
691
692 #define DBG_BSIZE 1024
693
694 /**
695  * can_debug_cframe - print CAN frame
696  * @msg: pointer to message printed before the given CAN frame
697  * @cf: pointer to CAN frame
698  */
699 void can_debug_cframe(const char *msg, struct can_frame *cf, ...)
700 {
701         va_list ap;
702         int len;
703         int dlc, i;
704         char *buf;
705
706         buf = kmalloc(DBG_BSIZE, GFP_ATOMIC);
707         if (!buf)
708                 return;
709
710         len = sprintf(buf, KERN_DEBUG);
711         va_start(ap, cf);
712         len += snprintf(buf + len, DBG_BSIZE - 64, msg, ap);
713         buf[len++] = ':';
714         buf[len++] = ' ';
715         va_end(ap);
716
717         dlc = cf->can_dlc;
718         if (dlc > 8)
719                 dlc = 8;
720
721         if (cf->can_id & CAN_EFF_FLAG)
722                 len += sprintf(buf + len, "<%08X> [%X] ",
723                                cf->can_id & CAN_EFF_MASK, dlc);
724         else
725                 len += sprintf(buf + len, "<%03X> [%X] ",
726                                cf->can_id & CAN_SFF_MASK, dlc);
727
728         for (i = 0; i < dlc; i++)
729                 len += sprintf(buf + len, "%02X ", cf->data[i]);
730
731         if (cf->can_id & CAN_RTR_FLAG)
732                 len += sprintf(buf + len, "(RTR)");
733
734         buf[len++] = '\n';
735         buf[len]   = '\0';
736         printk(buf);
737         kfree(buf);
738 }
739 EXPORT_SYMBOL(can_debug_cframe);
740
741 /**
742  * can_debug_skb - print socket buffer content to kernel log
743  * @skb: pointer to socket buffer
744  */
745 void can_debug_skb(struct sk_buff *skb)
746 {
747         int len, nbytes, i;
748         char *buf;
749
750         buf = kmalloc(DBG_BSIZE, GFP_ATOMIC);
751         if (!buf)
752                 return;
753
754         len = sprintf(buf,
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++) {
767                 if (i % 16 == 0)
768                         len += sprintf(buf + len, "\n" KERN_DEBUG "  ");
769                 if (len < DBG_BSIZE - 16) {
770                         len += sprintf(buf + len, " %02x", skb->head[i]);
771                 } else {
772                         len += sprintf(buf + len, "...");
773                         break;
774                 }
775         }
776         buf[len++] = '\n';
777         buf[len]   = '\0';
778         printk(buf);
779         kfree(buf);
780 }
781 EXPORT_SYMBOL(can_debug_skb);
782
783 #endif
784
785 /*
786  * af_can protocol functions
787  */
788
789 /**
790  * can_proto_register - register CAN transport protocol
791  * @cp: pointer to CAN protocol structure
792  *
793  * Return:
794  *  0 on success
795  *  -EINVAL invalid (out of range) protocol number
796  *  -EBUSY  protocol already in use
797  *  -ENOBUF if proto_register() fails
798  */
799 int can_proto_register(struct can_proto *cp)
800 {
801         int proto = cp->protocol;
802         int err = 0;
803
804         if (proto < 0 || proto >= CAN_NPROTO) {
805                 printk(KERN_ERR "can: protocol number %d out "
806                        "of range\n", proto);
807                 return -EINVAL;
808         }
809         if (proto_tab[proto]) {
810                 printk(KERN_ERR "can: protocol %d already "
811                        "registered\n", proto);
812                 return -EBUSY;
813         }
814
815 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
816         err = proto_register(cp->prot, 0);
817         if (err < 0)
818                 return err;
819 #endif
820
821         proto_tab[proto] = cp;
822
823         /* use generic ioctl function if the module doesn't bring its own */
824         if (!cp->ops->ioctl)
825                 cp->ops->ioctl = can_ioctl;
826
827         return err;
828 }
829 EXPORT_SYMBOL(can_proto_register);
830
831 /**
832  * can_proto_unregister - unregister CAN transport protocol
833  * @cp: pointer to CAN protocol structure
834  *
835  * Return:
836  *  0 on success
837  *  -ESRCH protocol number was not registered
838  */
839 int can_proto_unregister(struct can_proto *cp)
840 {
841         int proto = cp->protocol;
842
843         if (!proto_tab[proto]) {
844                 printk(KERN_ERR "can: protocol %d is not registered\n", proto);
845                 return -ESRCH;
846         }
847 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
848         proto_unregister(cp->prot);
849 #endif
850         proto_tab[proto] = NULL;
851
852         return 0;
853 }
854 EXPORT_SYMBOL(can_proto_unregister);
855
856 /**
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
861  *
862  * Description:
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.
865  *
866  * Return:
867  *  0 on success
868  *  -ENOMEM on missing mem to create subscription entry
869  *  -ENODEV unknown device
870  */
871 int can_dev_register(struct net_device *dev,
872                      void (*func)(unsigned long msg, void *), void *data)
873 {
874         struct notifier *n;
875
876         DBG("called for %s\n", dev->name);
877
878         if (!dev || dev->type != ARPHRD_CAN)
879                 return -ENODEV;
880
881         n = kmalloc(sizeof(*n), GFP_KERNEL);
882         if (!n)
883                 return -ENOMEM;
884
885         n->dev  = dev;
886         n->func = func;
887         n->data = data;
888
889         write_lock(&notifier_lock);
890         list_add(&n->list, &notifier_list);
891         write_unlock(&notifier_lock);
892
893         return 0;
894 }
895 EXPORT_SYMBOL(can_dev_register);
896
897 /**
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
902  *
903  * Description:
904  *  Removes subscription entry depending on given (subscription) values.
905  *
906  * Return:
907  *  0 on success
908  *  -EINVAL on missing subscription entry
909  */
910 int can_dev_unregister(struct net_device *dev,
911                        void (*func)(unsigned long msg, void *), void *data)
912 {
913         struct notifier *n, *next;
914         int ret = -EINVAL;
915
916         DBG("called for %s\n", dev->name);
917
918         write_lock(&notifier_lock);
919         list_for_each_entry_safe(n, next, &notifier_list, list) {
920                 if (n->dev == dev && n->func == func && n->data == data) {
921                         list_del(&n->list);
922                         kfree(n);
923                         ret = 0;
924                         break;
925                 }
926         }
927         write_unlock(&notifier_lock);
928
929         return ret;
930 }
931 EXPORT_SYMBOL(can_dev_unregister);
932
933 static int can_notifier(struct notifier_block *nb,
934                         unsigned long msg, void *data)
935 {
936         struct net_device *dev = (struct net_device *)data;
937         struct notifier *n;
938         struct dev_rcv_lists *d;
939         int i;
940
941         DBG("called for %s, msg = %lu\n", dev->name, msg);
942
943         if (dev->type != ARPHRD_CAN)
944                 return NOTIFY_DONE;
945
946         switch (msg) {
947
948         case NETDEV_REGISTER:
949
950                 /*
951                  * create new dev_rcv_lists for this device
952                  *
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.
957                  */
958
959                 DBG("creating new dev_rcv_lists for %s\n", dev->name);
960
961                 d = kzalloc(sizeof(*d),
962                             in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
963                 if (!d) {
964                         printk(KERN_ERR "can: allocation of receive "
965                                "list failed\n");
966                         return NOTIFY_DONE;
967                 }
968                 d->dev = dev;
969
970                 spin_lock_bh(&rcv_lists_lock);
971                 hlist_add_head_rcu(&d->list, &rx_dev_list);
972                 spin_unlock_bh(&rcv_lists_lock);
973
974                 break;
975
976         case NETDEV_UNREGISTER:
977                 spin_lock_bh(&rcv_lists_lock);
978
979                 d = find_dev_rcv_lists(dev);
980                 if (d) {
981                         hlist_del_rcu(&d->list);
982
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]);
991                 } else
992                         printk(KERN_ERR "can: notifier: receive list not "
993                                "found for dev %s\n", dev->name);
994
995                 spin_unlock_bh(&rcv_lists_lock);
996
997                 if (d)
998                         call_rcu(&d->rcu, can_rcv_lists_delete);
999
1000                 break;
1001         }
1002
1003         read_lock(&notifier_lock);
1004         list_for_each_entry(n, &notifier_list, list) {
1005                 if (n->dev == dev)
1006                         n->func(msg, n->data);
1007         }
1008         read_unlock(&notifier_lock);
1009
1010         return NOTIFY_DONE;
1011 }
1012
1013 /*
1014  * af_can module init/exit functions
1015  */
1016
1017 static struct packet_type can_packet = {
1018         .type = __constant_htons(ETH_P_CAN),
1019         .dev  = NULL,
1020         .func = can_rcv,
1021 };
1022
1023 static struct net_proto_family can_family_ops = {
1024         .family = PF_CAN,
1025         .create = can_create,
1026         .owner  = THIS_MODULE,
1027 };
1028
1029 /* notifier block for netdevice event */
1030 static struct notifier_block can_netdev_notifier = {
1031         .notifier_call = can_notifier,
1032 };
1033
1034 static __init int can_init(void)
1035 {
1036         printk(banner);
1037
1038         rcv_cache = kmem_cache_create("can_receiver", sizeof(struct receiver),
1039                                       0, 0, NULL, NULL);
1040         if (!rcv_cache)
1041                 return -ENOMEM;
1042
1043         /*
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.
1047          */
1048
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);
1052
1053         if (stats_timer) {
1054                 /* the statistics are updated every second (timer triggered) */
1055                 init_timer(&stattimer);
1056                 stattimer.function = can_stat_update;
1057                 stattimer.data = 0;
1058                 /* update every second */
1059                 stattimer.expires = jiffies + HZ;
1060                 /* start statistics timer */
1061                 add_timer(&stattimer);
1062         } else
1063                 stattimer.function = NULL;
1064
1065         /* procfs init */
1066         can_init_proc();
1067
1068         /* protocol register */
1069         sock_register(&can_family_ops);
1070         register_netdevice_notifier(&can_netdev_notifier);
1071         dev_add_pack(&can_packet);
1072
1073         return 0;
1074 }
1075
1076 static __exit void can_exit(void)
1077 {
1078         struct dev_rcv_lists *d;
1079         struct hlist_node *n, *next;
1080
1081         if (stats_timer)
1082                 del_timer(&stattimer);
1083
1084         /* procfs remove */
1085         can_remove_proc();
1086
1087         /* protocol unregister */
1088         dev_remove_pack(&can_packet);
1089         unregister_netdevice_notifier(&can_netdev_notifier);
1090         sock_unregister(PF_CAN);
1091
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);
1097                 kfree(d);
1098         }
1099         spin_unlock_bh(&rcv_lists_lock);
1100
1101         kmem_cache_destroy(rcv_cache);
1102 }
1103
1104 module_init(can_init);
1105 module_exit(can_exit);