]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - kernel/2.6/net/can/af_can.c
Print KERN_ERR message if request_module() fails.
[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 <linux/can.h>
61 #include <linux/can/core.h>
62 #include <net/sock.h>
63 #include <asm/uaccess.h>
64
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                 ret = request_module(module_name);
180
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
184                  */
185                 if (ret == -ENOSYS)
186                         printk(KERN_INFO "can: request_module(%s) not"
187                                " implemented.\n", module_name);
188                 else if (ret)
189                         printk(KERN_ERR "can: request_module(%s) failed\n",
190                                module_name);
191         }
192
193         /* check for success and correct type */
194         cp = proto_tab[protocol];
195         if (!cp || cp->type != sock->type)
196                 return -EPROTONOSUPPORT;
197
198         if (cp->capability >= 0 && !capable(cp->capability))
199                 return -EPERM;
200
201         sock->ops = cp->ops;
202
203 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
204         sk = sk_alloc(PF_CAN, GFP_KERNEL, cp->prot, 1);
205         if (!sk)
206                 return -ENOMEM;
207 #else
208         sk = sk_alloc(PF_CAN, GFP_KERNEL, 1, 0);
209         if (!sk)
210                 return -ENOMEM;
211
212         if (cp->obj_size) {
213                 sk->sk_protinfo = kmalloc(cp->obj_size, GFP_KERNEL);
214                 if (!sk->sk_protinfo) {
215                         sk_free(sk);
216                         return -ENOMEM;
217                 }
218         }
219         sk_set_owner(sk, proto_tab[protocol]->owner);
220 #endif
221
222         sock_init_data(sock, sk);
223         sk->sk_destruct = can_sock_destruct;
224
225         DBG("created sock: %p\n", sk);
226
227 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
228         if (sk->sk_prot->init)
229                 ret = sk->sk_prot->init(sk);
230 #else
231         if (cp->init)
232                 ret = cp->init(sk);
233 #endif
234
235         if (ret) {
236                 /* release sk on errors */
237                 sock_orphan(sk);
238                 sock_put(sk);
239         }
240
241         return ret;
242 }
243
244 /*
245  * af_can tx path
246  */
247
248 /**
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!)
252  *
253  * Return:
254  *  0 on success
255  *  -ENETDOWN when the selected interface is down
256  *  -ENOBUFS on full driver queue (see net_xmit_errno())
257  */
258 int can_send(struct sk_buff *skb, int loop)
259 {
260         struct sock **tx_sk = (struct sock **)skb->cb;
261         int err;
262
263         if (skb->dev->type != ARPHRD_CAN) {
264                 kfree_skb(skb);
265                 return -EPERM;
266         }
267
268         if (loop) {
269                 /* local loopback of sent CAN frames (default) */
270
271                 /* indication for the CAN driver: do loopback */
272                 *tx_sk = skb->sk;
273
274                 /*
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
278                  */
279
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);
283
284                         /* perform the local loopback here */
285                         newskb->protocol  = htons(ETH_P_CAN);
286                         newskb->ip_summed = CHECKSUM_UNNECESSARY;
287                         netif_rx(newskb);
288                 }
289         } else {
290                 /* indication for the CAN driver: no loopback required */
291                 *tx_sk = NULL;
292         }
293
294         if (!(skb->dev->flags & IFF_UP))
295                 return -ENETDOWN;
296
297         /* send to netdevice */
298         err = dev_queue_xmit(skb);
299         if (err > 0)
300                 err = net_xmit_errno(err);
301
302         /* update statistics */
303         stats.tx_frames++;
304         stats.tx_frames_delta++;
305
306         return err;
307 }
308 EXPORT_SYMBOL(can_send);
309
310 /*
311  * af_can rx path
312  */
313
314 static struct dev_rcv_lists *find_dev_rcv_lists(struct net_device *dev)
315 {
316         struct dev_rcv_lists *d;
317         struct hlist_node *n;
318
319         /*
320          * find receive list for this device
321          *
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.
330          */
331
332         hlist_for_each_entry(d, n, &rx_dev_list, list) {
333                 if (d->dev == dev)
334                         break;
335         }
336
337         return n ? d : NULL;
338 }
339
340 static struct hlist_head *find_rcv_list(canid_t *can_id, canid_t *mask,
341                                         struct dev_rcv_lists *d)
342 {
343         canid_t inv = *can_id & CAN_INV_FILTER; /* save flag before masking */
344
345         /* filter error frames */
346         if (*mask & CAN_ERR_FLAG) {
347                 /* clear CAN_ERR_FLAG in list entry */
348                 *mask &= CAN_ERR_MASK;
349                 return &d->rx_err;
350         }
351
352         /* ensure valid values in can_mask */
353         if (*mask & CAN_EFF_FLAG)
354                 *mask &= (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG);
355         else
356                 *mask &= (CAN_SFF_MASK | CAN_RTR_FLAG);
357
358         /* reduce condition testing at receive time */
359         *can_id &= *mask;
360
361         /* inverse can_id/can_mask filter */
362         if (inv)
363                 return &d->rx_inv;
364
365         /* mask == 0 => no condition testing at receive time */
366         if (!(*mask))
367                 return &d->rx_all;
368
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? */
373                         return &d->rx_eff;
374                 }
375         } else {
376                 if (*mask == CAN_SFF_MASK)
377                         return &d->rx_sff[*can_id];
378         }
379
380         /* default: filter via can_id/can_mask */
381         return &d->rx_fil;
382 }
383
384 /**
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
392  *
393  * Description:
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
396  *
397  *          <received_can_id> & mask == can_id & mask
398  *
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).
401  *
402  * Return:
403  *  0 on success
404  *  -ENOMEM on missing cache mem to create subscription entry
405  *  -ENODEV unknown device
406  */
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,
409                     char *ident)
410 {
411         struct receiver *r;
412         struct hlist_head *rl;
413         struct dev_rcv_lists *d;
414         int ret = 0;
415
416         /* insert new receiver  (dev,canid,mask) -> (func,data) */
417
418         DBG("dev %p, id %03X, mask %03X, callback %p, data %p, ident %s\n",
419             dev, can_id, mask, func, data, ident);
420
421         r = kmem_cache_alloc(rcv_cache, GFP_KERNEL);
422         if (!r)
423                 return -ENOMEM;
424
425         spin_lock_bh(&rcv_lists_lock);
426
427         d = find_dev_rcv_lists(dev);
428         if (d) {
429                 rl = find_rcv_list(&can_id, &mask, d);
430
431                 r->can_id  = can_id;
432                 r->mask    = mask;
433                 r->matches = 0;
434                 r->func    = func;
435                 r->data    = data;
436                 r->ident   = ident;
437
438                 hlist_add_head_rcu(&r->list, rl);
439                 d->entries++;
440
441                 pstats.rcv_entries++;
442                 if (pstats.rcv_entries_max < pstats.rcv_entries)
443                         pstats.rcv_entries_max = pstats.rcv_entries;
444         } else {
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);
448                 ret = -ENODEV;
449         }
450
451         spin_unlock_bh(&rcv_lists_lock);
452
453         return ret;
454 }
455 EXPORT_SYMBOL(can_rx_register);
456
457 static void can_rcv_lists_delete(struct rcu_head *rp)
458 {
459         struct dev_rcv_lists *d = container_of(rp, struct dev_rcv_lists, rcu);
460
461         kfree(d);
462 }
463
464 static void can_rx_delete(struct rcu_head *rp)
465 {
466         struct receiver *r = container_of(rp, struct receiver, rcu);
467
468         kmem_cache_free(rcv_cache, r);
469 }
470
471 static void can_rx_delete_all(struct hlist_head *rl)
472 {
473         struct receiver *r;
474         struct hlist_node *n;
475
476         hlist_for_each_entry_rcu(r, n, rl, list) {
477                 hlist_del_rcu(&r->list);
478                 call_rcu(&r->rcu, can_rx_delete);
479         }
480 }
481
482 /**
483  * can_rx_unregister - unsubscribe CAN frames from a specific interface
484  * @dev: pointer to netdevice (NULL => unsubcribe from 'all' CAN devices list)
485  * @can_id: CAN identifier
486  * @mask: CAN mask
487  * @func: callback function on filter match
488  * @data: returned parameter for callback function
489  *
490  * Description:
491  *  Removes subscription entry depending on given (subscription) values.
492  *
493  * Return:
494  *  0 on success
495  *  -EINVAL on missing subscription entry
496  *  -ENODEV unknown device
497  */
498 int can_rx_unregister(struct net_device *dev, canid_t can_id, canid_t mask,
499                       void (*func)(struct sk_buff *, void *), void *data)
500 {
501         struct receiver *r = NULL;
502         struct hlist_head *rl;
503         struct hlist_node *next;
504         struct dev_rcv_lists *d;
505         int ret = 0;
506
507         DBG("dev %p, id %03X, mask %03X, callback %p, data %p\n",
508             dev, can_id, mask, func, data);
509
510         spin_lock_bh(&rcv_lists_lock);
511
512         d = find_dev_rcv_lists(dev);
513         if (!d) {
514                 DBG("receive list not found for dev %s, id %03X, mask %03X\n",
515                     DNAME(dev), can_id, mask);
516                 ret = -ENODEV;
517                 goto out;
518         }
519
520         rl = find_rcv_list(&can_id, &mask, d);
521
522         /*
523          * Search the receiver list for the item to delete.  This should
524          * exist, since no receiver may be unregistered that hasn't
525          * been registered before.
526          */
527
528         hlist_for_each_entry(r, next, rl, list) {
529                 if (r->can_id == can_id && r->mask == mask
530                     && r->func == func && r->data == data)
531                         break;
532         }
533
534         /*
535          * Check for bug in CAN protocol implementations:
536          * If no matching list item was found, the list cursor variable next
537          * will be NULL, while r will point to the last item of the list.
538          */
539
540         if (!next) {
541                 DBG("receive list entry not found for "
542                     "dev %s, id %03X, mask %03X\n", DNAME(dev), can_id, mask);
543                 ret = -EINVAL;
544                 r = NULL;
545                 goto out;
546         }
547
548         hlist_del_rcu(&r->list);
549         d->entries--;
550
551         if (pstats.rcv_entries > 0)
552                 pstats.rcv_entries--;
553
554  out:
555         spin_unlock_bh(&rcv_lists_lock);
556
557         /* schedule the receiver item for deletion */
558         if (r)
559                 call_rcu(&r->rcu, can_rx_delete);
560
561         return ret;
562 }
563 EXPORT_SYMBOL(can_rx_unregister);
564
565 static inline void deliver(struct sk_buff *skb, struct receiver *r)
566 {
567         struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
568
569         DBG("skbuff %p cloned to %p\n", skb, clone);
570         if (clone) {
571                 r->func(clone, r->data);
572                 r->matches++;
573         }
574 }
575
576 static int can_rcv_filter(struct dev_rcv_lists *d, struct sk_buff *skb)
577 {
578         struct receiver *r;
579         struct hlist_node *n;
580         int matches = 0;
581         struct can_frame *cf = (struct can_frame*)skb->data;
582         canid_t can_id = cf->can_id;
583
584         if (d->entries == 0)
585                 return 0;
586
587         if (can_id & CAN_ERR_FLAG) {
588                 /* check for error frame entries only */
589                 hlist_for_each_entry_rcu(r, n, &d->rx_err, list) {
590                         if (can_id & r->mask) {
591                                 DBG("match on rx_err skbuff %p\n", skb);
592                                 deliver(skb, r);
593                                 matches++;
594                         }
595                 }
596                 return matches;
597         }
598
599         /* check for unfiltered entries */
600         hlist_for_each_entry_rcu(r, n, &d->rx_all, list) {
601                 DBG("match on rx_all skbuff %p\n", skb);
602                 deliver(skb, r);
603                 matches++;
604         }
605
606         /* check for can_id/mask entries */
607         hlist_for_each_entry_rcu(r, n, &d->rx_fil, list) {
608                 if ((can_id & r->mask) == r->can_id) {
609                         DBG("match on rx_fil skbuff %p\n", skb);
610                         deliver(skb, r);
611                         matches++;
612                 }
613         }
614
615         /* check for inverted can_id/mask entries */
616         hlist_for_each_entry_rcu(r, n, &d->rx_inv, list) {
617                 if ((can_id & r->mask) != r->can_id) {
618                         DBG("match on rx_inv skbuff %p\n", skb);
619                         deliver(skb, r);
620                         matches++;
621                 }
622         }
623
624         /* check CAN_ID specific entries */
625         if (can_id & CAN_EFF_FLAG) {
626                 hlist_for_each_entry_rcu(r, n, &d->rx_eff, list) {
627                         if (r->can_id == can_id) {
628                                 DBG("match on rx_eff skbuff %p\n", skb);
629                                 deliver(skb, r);
630                                 matches++;
631                         }
632                 }
633         } else {
634                 can_id &= CAN_SFF_MASK;
635                 hlist_for_each_entry_rcu(r, n, &d->rx_sff[can_id], list) {
636                         DBG("match on rx_sff skbuff %p\n", skb);
637                         deliver(skb, r);
638                         matches++;
639                 }
640         }
641
642         return matches;
643 }
644
645 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
646 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
647                    struct packet_type *pt, struct net_device *orig_dev)
648 #else
649 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
650                    struct packet_type *pt)
651 #endif
652 {
653         struct dev_rcv_lists *d;
654         int matches;
655
656         DBG("received skbuff on device %s, ptype %04x\n",
657             dev->name, ntohs(pt->type));
658         DBG_SKB(skb);
659         DBG_FRAME("af_can: can_rcv: received CAN frame",
660                   (struct can_frame *)skb->data);
661
662         if (dev->type != ARPHRD_CAN) {
663                 kfree_skb(skb);
664                 return 0;
665         }
666
667         /* update statistics */
668         stats.rx_frames++;
669         stats.rx_frames_delta++;
670
671         rcu_read_lock();
672
673         /* deliver the packet to sockets listening on all devices */
674         matches = can_rcv_filter(&rx_alldev_list, skb);
675
676         /* find receive list for this device */
677         d = find_dev_rcv_lists(dev);
678         if (d)
679                 matches += can_rcv_filter(d, skb);
680
681         rcu_read_unlock();
682
683         /* free the skbuff allocated by the netdevice driver */
684         DBG("freeing skbuff %p\n", skb);
685         kfree_skb(skb);
686
687         if (matches > 0) {
688                 stats.matches++;
689                 stats.matches_delta++;
690         }
691
692         return 0;
693 }
694
695 /*
696  * af_can debugging stuff
697  */
698
699 #ifdef CONFIG_CAN_DEBUG_CORE
700
701 #define DBG_BSIZE 1024
702
703 /**
704  * can_debug_cframe - print CAN frame
705  * @msg: pointer to message printed before the given CAN frame
706  * @cf: pointer to CAN frame
707  */
708 void can_debug_cframe(const char *msg, struct can_frame *cf, ...)
709 {
710         va_list ap;
711         int len;
712         int dlc, i;
713         char *buf;
714
715         buf = kmalloc(DBG_BSIZE, GFP_ATOMIC);
716         if (!buf)
717                 return;
718
719         len = sprintf(buf, KERN_DEBUG);
720         va_start(ap, cf);
721         len += snprintf(buf + len, DBG_BSIZE - 64, msg, ap);
722         buf[len++] = ':';
723         buf[len++] = ' ';
724         va_end(ap);
725
726         dlc = cf->can_dlc;
727         if (dlc > 8)
728                 dlc = 8;
729
730         if (cf->can_id & CAN_EFF_FLAG)
731                 len += sprintf(buf + len, "<%08X> [%X] ",
732                                cf->can_id & CAN_EFF_MASK, dlc);
733         else
734                 len += sprintf(buf + len, "<%03X> [%X] ",
735                                cf->can_id & CAN_SFF_MASK, dlc);
736
737         for (i = 0; i < dlc; i++)
738                 len += sprintf(buf + len, "%02X ", cf->data[i]);
739
740         if (cf->can_id & CAN_RTR_FLAG)
741                 len += sprintf(buf + len, "(RTR)");
742
743         buf[len++] = '\n';
744         buf[len]   = '\0';
745         printk(buf);
746         kfree(buf);
747 }
748 EXPORT_SYMBOL(can_debug_cframe);
749
750 /**
751  * can_debug_skb - print socket buffer content to kernel log
752  * @skb: pointer to socket buffer
753  */
754 void can_debug_skb(struct sk_buff *skb)
755 {
756         int len, nbytes, i;
757         char *buf;
758
759         buf = kmalloc(DBG_BSIZE, GFP_ATOMIC);
760         if (!buf)
761                 return;
762
763         len = sprintf(buf,
764                       KERN_DEBUG "  skbuff at %p, dev: %d, proto: %04x\n"
765                       KERN_DEBUG "  users: %d, dataref: %d, nr_frags: %d, "
766                       "h,d,t,e,l: %p %+d %+d %+d, %d",
767                       skb, skb->dev ? skb->dev->ifindex : -1,
768                       ntohs(skb->protocol),
769                       atomic_read(&skb->users),
770                       atomic_read(&(skb_shinfo(skb)->dataref)),
771                       skb_shinfo(skb)->nr_frags,
772                       skb->head, skb->data - skb->head,
773                       skb->tail - skb->head, skb->end - skb->head, skb->len);
774         nbytes = skb->end - skb->head;
775         for (i = 0; i < nbytes; i++) {
776                 if (i % 16 == 0)
777                         len += sprintf(buf + len, "\n" KERN_DEBUG "  ");
778                 if (len < DBG_BSIZE - 16) {
779                         len += sprintf(buf + len, " %02x", skb->head[i]);
780                 } else {
781                         len += sprintf(buf + len, "...");
782                         break;
783                 }
784         }
785         buf[len++] = '\n';
786         buf[len]   = '\0';
787         printk(buf);
788         kfree(buf);
789 }
790 EXPORT_SYMBOL(can_debug_skb);
791
792 #endif
793
794 /*
795  * af_can protocol functions
796  */
797
798 /**
799  * can_proto_register - register CAN transport protocol
800  * @cp: pointer to CAN protocol structure
801  *
802  * Return:
803  *  0 on success
804  *  -EINVAL invalid (out of range) protocol number
805  *  -EBUSY  protocol already in use
806  *  -ENOBUF if proto_register() fails
807  */
808 int can_proto_register(struct can_proto *cp)
809 {
810         int proto = cp->protocol;
811         int err = 0;
812
813         if (proto < 0 || proto >= CAN_NPROTO) {
814                 printk(KERN_ERR "can: protocol number %d out "
815                        "of range\n", proto);
816                 return -EINVAL;
817         }
818         if (proto_tab[proto]) {
819                 printk(KERN_ERR "can: protocol %d already "
820                        "registered\n", proto);
821                 return -EBUSY;
822         }
823
824 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
825         err = proto_register(cp->prot, 0);
826         if (err < 0)
827                 return err;
828 #endif
829
830         proto_tab[proto] = cp;
831
832         /* use generic ioctl function if the module doesn't bring its own */
833         if (!cp->ops->ioctl)
834                 cp->ops->ioctl = can_ioctl;
835
836         return err;
837 }
838 EXPORT_SYMBOL(can_proto_register);
839
840 /**
841  * can_proto_unregister - unregister CAN transport protocol
842  * @cp: pointer to CAN protocol structure
843  *
844  * Return:
845  *  0 on success
846  *  -ESRCH protocol number was not registered
847  */
848 int can_proto_unregister(struct can_proto *cp)
849 {
850         int proto = cp->protocol;
851
852         if (!proto_tab[proto]) {
853                 printk(KERN_ERR "can: protocol %d is not registered\n", proto);
854                 return -ESRCH;
855         }
856 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
857         proto_unregister(cp->prot);
858 #endif
859         proto_tab[proto] = NULL;
860
861         return 0;
862 }
863 EXPORT_SYMBOL(can_proto_unregister);
864
865 /**
866  * can_dev_register - subscribe notifier for CAN device status changes
867  * @dev: pointer to netdevice
868  * @func: callback function on status change
869  * @data: returned parameter for callback function
870  *
871  * Description:
872  *  Invokes the callback function with the status 'msg' and the given
873  *  parameter 'data' on a status change of the given CAN network device.
874  *
875  * Return:
876  *  0 on success
877  *  -ENOMEM on missing mem to create subscription entry
878  *  -ENODEV unknown device
879  */
880 int can_dev_register(struct net_device *dev,
881                      void (*func)(unsigned long msg, void *), void *data)
882 {
883         struct notifier *n;
884
885         DBG("called for %s\n", dev->name);
886
887         if (!dev || dev->type != ARPHRD_CAN)
888                 return -ENODEV;
889
890         n = kmalloc(sizeof(*n), GFP_KERNEL);
891         if (!n)
892                 return -ENOMEM;
893
894         n->dev  = dev;
895         n->func = func;
896         n->data = data;
897
898         write_lock(&notifier_lock);
899         list_add(&n->list, &notifier_list);
900         write_unlock(&notifier_lock);
901
902         return 0;
903 }
904 EXPORT_SYMBOL(can_dev_register);
905
906 /**
907  * can_dev_unregister - unsubscribe notifier for CAN device status changes
908  * @dev: pointer to netdevice
909  * @func: callback function on filter match
910  * @data: returned parameter for callback function
911  *
912  * Description:
913  *  Removes subscription entry depending on given (subscription) values.
914  *
915  * Return:
916  *  0 on success
917  *  -EINVAL on missing subscription entry
918  */
919 int can_dev_unregister(struct net_device *dev,
920                        void (*func)(unsigned long msg, void *), void *data)
921 {
922         struct notifier *n, *next;
923         int ret = -EINVAL;
924
925         DBG("called for %s\n", dev->name);
926
927         write_lock(&notifier_lock);
928         list_for_each_entry_safe(n, next, &notifier_list, list) {
929                 if (n->dev == dev && n->func == func && n->data == data) {
930                         list_del(&n->list);
931                         kfree(n);
932                         ret = 0;
933                         break;
934                 }
935         }
936         write_unlock(&notifier_lock);
937
938         return ret;
939 }
940 EXPORT_SYMBOL(can_dev_unregister);
941
942 static int can_notifier(struct notifier_block *nb,
943                         unsigned long msg, void *data)
944 {
945         struct net_device *dev = (struct net_device *)data;
946         struct notifier *n;
947         struct dev_rcv_lists *d;
948         int i;
949
950         DBG("called for %s, msg = %lu\n", dev->name, msg);
951
952         if (dev->type != ARPHRD_CAN)
953                 return NOTIFY_DONE;
954
955         switch (msg) {
956
957         case NETDEV_REGISTER:
958
959                 /*
960                  * create new dev_rcv_lists for this device
961                  *
962                  * N.B. zeroing the struct is the correct initialization
963                  * for the embedded hlist_head structs.
964                  * Another list type, e.g. list_head, would require
965                  * explicit initialization.
966                  */
967
968                 DBG("creating new dev_rcv_lists for %s\n", dev->name);
969
970                 d = kzalloc(sizeof(*d),
971                             in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
972                 if (!d) {
973                         printk(KERN_ERR "can: allocation of receive "
974                                "list failed\n");
975                         return NOTIFY_DONE;
976                 }
977                 d->dev = dev;
978
979                 spin_lock_bh(&rcv_lists_lock);
980                 hlist_add_head_rcu(&d->list, &rx_dev_list);
981                 spin_unlock_bh(&rcv_lists_lock);
982
983                 break;
984
985         case NETDEV_UNREGISTER:
986                 spin_lock_bh(&rcv_lists_lock);
987
988                 d = find_dev_rcv_lists(dev);
989                 if (d) {
990                         hlist_del_rcu(&d->list);
991
992                         /* remove all receivers hooked at this netdevice */
993                         can_rx_delete_all(&d->rx_err);
994                         can_rx_delete_all(&d->rx_all);
995                         can_rx_delete_all(&d->rx_fil);
996                         can_rx_delete_all(&d->rx_inv);
997                         can_rx_delete_all(&d->rx_eff);
998                         for (i = 0; i < 2048; i++)
999                                 can_rx_delete_all(&d->rx_sff[i]);
1000                 } else
1001                         printk(KERN_ERR "can: notifier: receive list not "
1002                                "found for dev %s\n", dev->name);
1003
1004                 spin_unlock_bh(&rcv_lists_lock);
1005
1006                 if (d)
1007                         call_rcu(&d->rcu, can_rcv_lists_delete);
1008
1009                 break;
1010         }
1011
1012         read_lock(&notifier_lock);
1013         list_for_each_entry(n, &notifier_list, list) {
1014                 if (n->dev == dev)
1015                         n->func(msg, n->data);
1016         }
1017         read_unlock(&notifier_lock);
1018
1019         return NOTIFY_DONE;
1020 }
1021
1022 /*
1023  * af_can module init/exit functions
1024  */
1025
1026 static struct packet_type can_packet = {
1027         .type = __constant_htons(ETH_P_CAN),
1028         .dev  = NULL,
1029         .func = can_rcv,
1030 };
1031
1032 static struct net_proto_family can_family_ops = {
1033         .family = PF_CAN,
1034         .create = can_create,
1035         .owner  = THIS_MODULE,
1036 };
1037
1038 /* notifier block for netdevice event */
1039 static struct notifier_block can_netdev_notifier = {
1040         .notifier_call = can_notifier,
1041 };
1042
1043 static __init int can_init(void)
1044 {
1045         printk(banner);
1046
1047         rcv_cache = kmem_cache_create("can_receiver", sizeof(struct receiver),
1048                                       0, 0, NULL, NULL);
1049         if (!rcv_cache)
1050                 return -ENOMEM;
1051
1052         /*
1053          * Insert struct dev_rcv_lists for reception on all devices.
1054          * This struct is zero initialized which is correct for the
1055          * embedded hlist heads, the dev pointer, and the entries counter.
1056          */
1057
1058         spin_lock_bh(&rcv_lists_lock);
1059         hlist_add_head_rcu(&rx_alldev_list.list, &rx_dev_list);
1060         spin_unlock_bh(&rcv_lists_lock);
1061
1062         if (stats_timer) {
1063                 /* the statistics are updated every second (timer triggered) */
1064                 init_timer(&stattimer);
1065                 stattimer.function = can_stat_update;
1066                 stattimer.data = 0;
1067                 /* update every second */
1068                 stattimer.expires = jiffies + HZ;
1069                 /* start statistics timer */
1070                 add_timer(&stattimer);
1071         } else
1072                 stattimer.function = NULL;
1073
1074         /* procfs init */
1075         can_init_proc();
1076
1077         /* protocol register */
1078         sock_register(&can_family_ops);
1079         register_netdevice_notifier(&can_netdev_notifier);
1080         dev_add_pack(&can_packet);
1081
1082         return 0;
1083 }
1084
1085 static __exit void can_exit(void)
1086 {
1087         struct dev_rcv_lists *d;
1088         struct hlist_node *n, *next;
1089
1090         if (stats_timer)
1091                 del_timer(&stattimer);
1092
1093         /* procfs remove */
1094         can_remove_proc();
1095
1096         /* protocol unregister */
1097         dev_remove_pack(&can_packet);
1098         unregister_netdevice_notifier(&can_netdev_notifier);
1099         sock_unregister(PF_CAN);
1100
1101         /* remove rx_dev_list */
1102         spin_lock_bh(&rcv_lists_lock);
1103         hlist_del(&rx_alldev_list.list);
1104         hlist_for_each_entry_safe(d, n, next, &rx_dev_list, list) {
1105                 hlist_del(&d->list);
1106                 kfree(d);
1107         }
1108         spin_unlock_bh(&rcv_lists_lock);
1109
1110         kmem_cache_destroy(rcv_cache);
1111 }
1112
1113 module_init(can_init);
1114 module_exit(can_exit);