]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - kernel/2.6/net/can/bcm.c
Reverted the use of skb->iif in the rx path that has been introduced in
[socketcan-devel.git] / kernel / 2.6 / net / can / bcm.c
1 /*
2  * bcm.c - Broadcast Manager to filter/send (cyclic) CAN content
3  *
4  * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions, the following disclaimer and
12  *    the referenced file 'COPYING'.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of Volkswagen nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * Alternatively, provided that this notice is retained in full, this
21  * software may be distributed under the terms of the GNU General
22  * Public License ("GPL") version 2 as distributed in the 'COPYING'
23  * file from the main directory of the linux kernel source.
24  *
25  * The provided data structures and external interfaces from this code
26  * are not restricted to be used by modules with a GPL compatible license.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
39  * DAMAGE.
40  *
41  * Send feedback to <socketcan-users@lists.berlios.de>
42  *
43  */
44
45 #include <linux/module.h>
46 #include <linux/version.h>
47 #include <linux/init.h>
48 #include <linux/list.h>
49 #include <linux/proc_fs.h>
50 #include <linux/uio.h>
51 #include <linux/poll.h>
52 #include <linux/net.h>
53 #include <linux/netdevice.h>
54 #include <linux/socket.h>
55 #include <linux/if_arp.h>
56 #include <linux/skbuff.h>
57 #include <linux/can.h>
58 #include <linux/can/core.h>
59 #include <linux/can/bcm.h>
60 #include <net/sock.h>
61
62 #include <linux/can/version.h> /* for RCSID. Removed by mkpatch script */
63 RCSID("$Id$");
64
65 /* use of last_frames[index].can_dlc */
66 #define RX_RECV    0x40 /* received data for this element */
67 #define RX_THR     0x80 /* element not been sent due to throttle feature */
68 #define BCM_CAN_DLC_MASK 0x0F /* clean private flags in can_dlc by masking */
69
70 /* get best masking value for can_rx_register() for a given single can_id */
71 #define REGMASK(id) ((id & CAN_RTR_FLAG) | ((id & CAN_EFF_FLAG) ? \
72                         (CAN_EFF_MASK | CAN_EFF_FLAG) : CAN_SFF_MASK))
73
74 #define IDENT "bcm"
75 #define CAN_BCM_VERSION CAN_VERSION
76 static __initdata const char banner[] = KERN_INFO
77         "can: broadcast manager protocol (rev " CAN_BCM_VERSION ")\n";
78
79 MODULE_DESCRIPTION("PF_CAN broadcast manager protocol");
80 MODULE_LICENSE("Dual BSD/GPL");
81 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
82
83 #ifdef CONFIG_CAN_DEBUG_CORE
84 static int debug = 0;
85 module_param(debug, int, S_IRUGO);
86 MODULE_PARM_DESC(debug, "debug print mask: 1:debug, 2:frames, 4:skbs");
87 #endif
88
89 /* easy access to can_frame payload */
90 static inline u64 GET_U64(const struct can_frame *cp)
91 {
92         return *(u64*)cp->data;
93 }
94
95 struct bcm_op {
96         struct list_head list;
97         int ifindex;
98         canid_t can_id;
99         int flags;
100         unsigned long j_ival1, j_ival2, j_lastmsg;
101         unsigned long frames_abs, frames_filtered;
102         struct timer_list timer, thrtimer;
103         struct timeval ival1, ival2;
104 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
105         ktime_t rx_stamp;
106 #else
107         struct timeval rx_stamp;
108 #endif
109         int rx_ifindex;
110         int count;
111         int nframes;
112         int currframe;
113         struct can_frame *frames;
114         struct can_frame *last_frames;
115         struct can_frame sframe;
116         struct can_frame last_sframe;
117         struct sock *sk;
118         struct net_device *rx_reg_dev;
119 };
120
121 static struct proc_dir_entry *proc_dir = NULL;
122
123 #ifdef CONFIG_CAN_BCM_USER
124 #define BCM_CAP (-1)
125 #else
126 #define BCM_CAP CAP_NET_RAW
127 #endif
128
129 struct bcm_sock {
130 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
131         struct sock sk;
132 #else
133         struct sock *sk;
134 #endif
135         int bound;
136         int ifindex;
137         struct notifier_block notifier;
138         struct list_head rx_ops;
139         struct list_head tx_ops;
140         unsigned long dropped_usr_msgs;
141         struct proc_dir_entry *bcm_proc_read;
142         char procname [9]; /* pointer printed in ASCII with \0 */
143 };
144
145 static inline struct bcm_sock *bcm_sk(const struct sock *sk)
146 {
147 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
148         return (struct bcm_sock *)sk;
149 #else
150         return (struct bcm_sock *)sk->sk_protinfo;
151 #endif
152 }
153
154 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
155 static void *kzalloc(size_t size, unsigned int __nocast flags)
156 {
157         void *ret = kmalloc(size, flags);
158
159         if (ret)
160                 memset(ret, 0, size);
161
162         return ret;
163 }
164
165 static inline void skb_get_timestamp(const struct sk_buff *skb,
166                                      struct timeval *stamp)
167 {
168         stamp->tv_sec  = skb->stamp.tv_sec;
169         stamp->tv_usec = skb->stamp.tv_usec;
170 }
171
172 static inline void skb_set_timestamp(struct sk_buff *skb,
173                                      const struct timeval *stamp)
174 {
175         skb->stamp.tv_sec  = stamp->tv_sec;
176         skb->stamp.tv_usec = stamp->tv_usec;
177 }
178 #endif
179
180 #define CFSIZ sizeof(struct can_frame)
181 #define OPSIZ sizeof(struct bcm_op)
182 #define MHSIZ sizeof(struct bcm_msg_head)
183
184 /*
185  * rounded_tv2jif - calculate jiffies from timeval including optional up
186  * @tv: pointer to timeval
187  *
188  * Description:
189  * In opposite to timeval_to_jiffies() provided in include/linux/jiffies.h this
190  * function is intentionally more relaxed on precise timer ticks to get exact
191  * one jiffy for requested 1000us on a 1000HZ machine.
192  * This code is to be removed when upgrading to kernel hrtimer.
193  *
194  * Return:
195  *  calculated jiffies (max: ULONG_MAX)
196  */
197 static unsigned long rounded_tv2jif(const struct timeval *tv)
198 {
199         unsigned long sec  = tv->tv_sec;
200         unsigned long usec = tv->tv_usec;
201         unsigned long jif;
202
203         if (sec > ULONG_MAX / HZ)
204                 return ULONG_MAX;
205
206         /* round up to get at least the requested time */
207         usec += 1000000 / HZ - 1;
208
209         jif  = usec / (1000000 / HZ);
210
211         if (sec * HZ > ULONG_MAX - jif)
212                 return ULONG_MAX;
213
214         return jif + sec * HZ;
215 }
216
217 /*
218  * procfs functions
219  */
220 static char *bcm_proc_getifname(int ifindex)
221 {
222         struct net_device *dev;
223
224         if (!ifindex)
225                 return "any";
226
227         dev = __dev_get_by_index(ifindex); /* no usage counting */
228         if (dev)
229                 return dev->name;
230
231         return "???";
232 }
233
234 static int bcm_read_proc(char *page, char **start, off_t off,
235                          int count, int *eof, void *data)
236 {
237         int len = 0;
238         struct sock *sk = (struct sock *)data;
239         struct bcm_sock *bo = bcm_sk(sk);
240         struct bcm_op *op;
241
242         len += snprintf(page + len, PAGE_SIZE - len, ">>> socket %p",
243                         sk->sk_socket);
244         len += snprintf(page + len, PAGE_SIZE - len, " / sk %p", sk);
245         len += snprintf(page + len, PAGE_SIZE - len, " / bo %p", bo);
246         len += snprintf(page + len, PAGE_SIZE - len, " / dropped %lu",
247                         bo->dropped_usr_msgs);
248         len += snprintf(page + len, PAGE_SIZE - len, " / bound %s",
249                         bcm_proc_getifname(bo->ifindex));
250         len += snprintf(page + len, PAGE_SIZE - len, " <<<\n");
251
252         list_for_each_entry(op, &bo->rx_ops, list) {
253
254                 unsigned long reduction;
255
256                 /* print only active entries & prevent division by zero */
257                 if (!op->frames_abs)
258                         continue;
259
260                 len += snprintf(page + len, PAGE_SIZE - len,
261                                 "rx_op: %03X %-5s ",
262                                 op->can_id, bcm_proc_getifname(op->ifindex));
263                 len += snprintf(page + len, PAGE_SIZE - len, "[%d]%c ",
264                                 op->nframes,
265                                 (op->flags & RX_CHECK_DLC)?'d':' ');
266                 if (op->j_ival1)
267                         len += snprintf(page + len, PAGE_SIZE - len,
268                                         "timeo=%ld ", op->j_ival1);
269
270                 if (op->j_ival2)
271                         len += snprintf(page + len, PAGE_SIZE - len,
272                                         "thr=%ld ", op->j_ival2);
273
274                 len += snprintf(page + len, PAGE_SIZE - len,
275                                 "# recv %ld (%ld) => reduction: ",
276                                 op->frames_filtered, op->frames_abs);
277
278                 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
279
280                 len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
281                                 (reduction == 100)?"near ":"", reduction);
282
283                 if (len > PAGE_SIZE - 200) {
284                         /* mark output cut off */
285                         len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
286                         break;
287                 }
288         }
289
290         list_for_each_entry(op, &bo->tx_ops, list) {
291
292                 len += snprintf(page + len, PAGE_SIZE - len,
293                                 "tx_op: %03X %s [%d] ",
294                                 op->can_id, bcm_proc_getifname(op->ifindex),
295                                 op->nframes);
296                 if (op->j_ival1)
297                         len += snprintf(page + len, PAGE_SIZE - len, "t1=%ld ",
298                                         op->j_ival1);
299
300                 if (op->j_ival2)
301                         len += snprintf(page + len, PAGE_SIZE - len, "t2=%ld ",
302                                         op->j_ival2);
303
304                 len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n",
305                                 op->frames_abs);
306
307                 if (len > PAGE_SIZE - 100) {
308                         /* mark output cut off */
309                         len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
310                         break;
311                 }
312         }
313
314         len += snprintf(page + len, PAGE_SIZE - len, "\n");
315
316         *eof = 1;
317         return len;
318 }
319
320 /*
321  * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
322  *              of the given bcm tx op
323  */
324 static void bcm_can_tx(struct bcm_op *op)
325 {
326         struct sk_buff *skb;
327         struct net_device *dev;
328         struct can_frame *cf = &op->frames[op->currframe];
329
330         DBG_FRAME("BCM: bcm_can_tx: sending frame", cf);
331
332         /* no target device? => exit */
333         if (!op->ifindex)
334                 return;
335
336         dev = dev_get_by_index(op->ifindex);
337
338         if (!dev) {
339                 /* RFC: should this bcm_op remove itself here? */
340                 return;
341         }
342
343         skb = alloc_skb(CFSIZ,
344                         in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
345
346         if (!skb)
347                 goto out;
348
349         memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
350
351         /* send with loopback */
352         skb->dev = dev;
353         skb->sk = op->sk;
354         can_send(skb, 1);
355
356         /* update statistics */
357         op->currframe++;
358         op->frames_abs++;
359
360         /* reached last frame? */
361         if (op->currframe >= op->nframes)
362                 op->currframe = 0;
363  out:
364         dev_put(dev);
365 }
366
367 /*
368  * bcm_send_to_user - send a BCM message to the userspace
369  *                    (consisting of bcm_msg_head + x CAN frames)
370  */
371 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
372                              struct can_frame *frames, int has_timestamp)
373 {
374         struct sk_buff *skb;
375         struct can_frame *firstframe;
376         struct sockaddr_can *addr;
377         struct sock *sk = op->sk;
378         int datalen = head->nframes * CFSIZ;
379         int err;
380
381         skb = alloc_skb(sizeof(*head) + datalen,
382                         in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
383         if (!skb)
384                 return;
385
386         memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
387
388         /* can_frames starting here */
389         firstframe = (struct can_frame *) skb->tail;
390
391
392         if (has_timestamp) {
393                 /* restore rx timestamp */
394 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
395                 skb->tstamp = op->rx_stamp;
396 #else
397                 skb_set_timestamp(skb, &op->rx_stamp);
398 #endif
399         }
400
401         /* restore originator for recvfrom() */
402         addr = (struct sockaddr_can *)skb->cb;
403         memset(addr, 0, sizeof(*addr));
404         addr->can_family  = AF_CAN;
405         addr->can_ifindex = op->rx_ifindex;
406
407         if (head->nframes) {
408                 memcpy(skb_put(skb, datalen), frames, datalen);
409
410                 /*
411                  * the BCM uses the can_dlc-element of the can_frame
412                  * structure for internal purposes. This is only
413                  * relevant for updates that are generated by the
414                  * BCM, where nframes is 1
415                  */
416                 if (head->nframes == 1)
417                         firstframe->can_dlc &= BCM_CAN_DLC_MASK;
418         }
419
420         err = sock_queue_rcv_skb(sk, skb);
421         if (err < 0) {
422                 struct bcm_sock *bo = bcm_sk(sk);
423
424                 DBG("sock_queue_rcv_skb failed: %d\n", err);
425                 kfree_skb(skb);
426                 /* don't care about overflows in this statistic */
427                 bo->dropped_usr_msgs++;
428         }
429 }
430
431 /*
432  * bcm_tx_timeout_handler - performes cyclic CAN frame transmissions
433  */
434 static void bcm_tx_timeout_handler(unsigned long data)
435 {
436         struct bcm_op *op = (struct bcm_op*)data;
437
438         DBG("Called with bcm_op %p\n", op);
439
440         if (op->j_ival1 && (op->count > 0)) {
441
442                 op->count--;
443                 if (!op->count && (op->flags & TX_COUNTEVT)) {
444                         struct bcm_msg_head msg_head;
445
446                         /* create notification to user */
447                         DBG("sending TX_EXPIRED for can_id %03X\n",
448                             op->can_id);
449
450                         msg_head.opcode  = TX_EXPIRED;
451                         msg_head.flags   = op->flags;
452                         msg_head.count   = op->count;
453                         msg_head.ival1   = op->ival1;
454                         msg_head.ival2   = op->ival2;
455                         msg_head.can_id  = op->can_id;
456                         msg_head.nframes = 0;
457
458                         bcm_send_to_user(op, &msg_head, NULL, 0);
459                 }
460         }
461
462         DBG("count=%d j_ival1=%ld j_ival2=%ld\n",
463             op->count, op->j_ival1, op->j_ival2);
464
465         if (op->j_ival1 && (op->count > 0)) {
466
467                 op->timer.expires = jiffies + op->j_ival1;
468                 add_timer(&op->timer);
469
470                 DBG("adding timer ival1. func=%p data=%p exp=0x%08X\n",
471                     op->timer.function,
472                     (char*) op->timer.data,
473                     (unsigned int) op->timer.expires);
474
475                 /* send (next) frame */
476                 bcm_can_tx(op);
477
478         } else {
479                 if (op->j_ival2) {
480                         op->timer.expires = jiffies + op->j_ival2;
481                         add_timer(&op->timer);
482
483                         DBG("adding timer ival2. func=%p data=%p exp=0x%08X\n",
484                             op->timer.function,
485                             (char*) op->timer.data,
486                             (unsigned int) op->timer.expires);
487
488                         /* send (next) frame */
489                         bcm_can_tx(op);
490
491                 } else
492                         DBG("no timer restart\n");
493         }
494
495         return;
496 }
497
498 /*
499  * bcm_rx_changed - create a RX_CHANGED notification due to changed content
500  */
501 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
502 {
503         struct bcm_msg_head head;
504
505         op->j_lastmsg = jiffies;
506
507         /* update statistics */
508         op->frames_filtered++;
509
510         /* prevent statistics overflow */
511         if (op->frames_filtered > ULONG_MAX/100)
512                 op->frames_filtered = op->frames_abs = 0;
513
514         DBG("setting j_lastmsg to 0x%08X for rx_op %p\n",
515             (unsigned int) op->j_lastmsg, op);
516         DBG("sending notification\n");
517
518         head.opcode  = RX_CHANGED;
519         head.flags   = op->flags;
520         head.count   = op->count;
521         head.ival1   = op->ival1;
522         head.ival2   = op->ival2;
523         head.can_id  = op->can_id;
524         head.nframes = 1;
525
526         bcm_send_to_user(op, &head, data, 1);
527 }
528
529 /*
530  * bcm_rx_update_and_send - process a detected relevant receive content change
531  *                          1. update the last received data
532  *                          2. send a notification to the user (if possible)
533  */
534 static void bcm_rx_update_and_send(struct bcm_op *op,
535                                    struct can_frame *lastdata,
536                                    struct can_frame *rxdata)
537 {
538         unsigned long nexttx = op->j_lastmsg + op->j_ival2;
539
540         memcpy(lastdata, rxdata, CFSIZ);
541
542         /* mark as used */
543         lastdata->can_dlc |= RX_RECV;
544
545         /* throttle bcm_rx_changed ? */
546         if ((op->thrtimer.expires) ||
547             ((op->j_ival2) && (nexttx > jiffies))) {
548                 /* we are already waiting OR we have to start waiting */
549
550                 /* mark as 'throttled' */
551                 lastdata->can_dlc |= RX_THR;
552
553                 if (!(op->thrtimer.expires)) {
554                         /* start the timer only the first time */
555                         op->thrtimer.expires = nexttx;
556                         add_timer(&op->thrtimer);
557
558                         DBG("adding thrtimer. func=%p data=%p exp=0x%08X\n",
559                             op->thrtimer.function,
560                             (char*) op->thrtimer.data,
561                             (unsigned int) op->thrtimer.expires);
562                 }
563
564         } else {
565                 /* send RX_CHANGED to the user immediately */
566                 bcm_rx_changed(op, rxdata);
567         }
568 }
569
570 /*
571  * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly
572  *                       received data stored in op->last_frames[]
573  */
574 static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
575                                 struct can_frame *rxdata)
576 {
577         /*
578          * no one uses the MSBs of can_dlc for comparation,
579          * so we use it here to detect the first time of reception
580          */
581
582         if (!(op->last_frames[index].can_dlc & RX_RECV)) {
583                 /* received data for the first time => send update to user */
584                 DBG("first time :)\n");
585                 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
586                 return;
587         }
588
589         /* do a real check in can_frame data section */
590
591         DBG("op->frames[index].data = 0x%016llx\n",
592             GET_U64(&op->frames[index]));
593         DBG("op->last_frames[index].data = 0x%016llx\n",
594             GET_U64(&op->last_frames[index]));
595         DBG("rxdata->data = 0x%016llx\n", GET_U64(rxdata));
596
597         if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) !=
598             (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) {
599                 DBG("relevant data change :)\n");
600                 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
601                 return;
602         }
603
604         if (op->flags & RX_CHECK_DLC) {
605                 /* do a real check in can_frame dlc */
606                 if (rxdata->can_dlc != (op->last_frames[index].can_dlc &
607                                         BCM_CAN_DLC_MASK)) {
608                         DBG("dlc change :)\n");
609                         bcm_rx_update_and_send(op, &op->last_frames[index],
610                                                rxdata);
611                         return;
612                 }
613         }
614         DBG("no relevant change :(\n");
615 }
616
617 /*
618  * bcm_rx_starttimer - enable timeout monitoring for CAN frame receiption
619  */
620 static void bcm_rx_starttimer(struct bcm_op *op)
621 {
622         if (op->flags & RX_NO_AUTOTIMER)
623                 return;
624
625         if (op->j_ival1) {
626                 op->timer.expires = jiffies + op->j_ival1;
627
628                 DBG("adding rx timeout timer ival1. func=%p data=%p "
629                     "exp=0x%08X\n",
630                     op->timer.function,
631                     (char*) op->timer.data,
632                     (unsigned int) op->timer.expires);
633
634                 add_timer(&op->timer);
635         }
636 }
637
638 /*
639  * bcm_rx_timeout_handler - when the (cyclic) CAN frame receiption timed out
640  */
641 static void bcm_rx_timeout_handler(unsigned long data)
642 {
643         struct bcm_op *op = (struct bcm_op*)data;
644         struct bcm_msg_head msg_head;
645
646         DBG("sending RX_TIMEOUT for can_id %03X. op is %p\n", op->can_id, op);
647
648         msg_head.opcode  = RX_TIMEOUT;
649         msg_head.flags   = op->flags;
650         msg_head.count   = op->count;
651         msg_head.ival1   = op->ival1;
652         msg_head.ival2   = op->ival2;
653         msg_head.can_id  = op->can_id;
654         msg_head.nframes = 0;
655
656         bcm_send_to_user(op, &msg_head, NULL, 0);
657
658         /* no restart of the timer is done here! */
659
660         /* if user wants to be informed, when cyclic CAN-Messages come back */
661         if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
662                 /* clear received can_frames to indicate 'nothing received' */
663                 memset(op->last_frames, 0, op->nframes * CFSIZ);
664                 DBG("RX_ANNOUNCE_RESTART\n");
665         }
666 }
667
668 /*
669  * bcm_rx_thr_handler - the time for blocked content updates is over now:
670  *                      Check for throttled data and send it to the userspace
671  */
672 static void bcm_rx_thr_handler(unsigned long data)
673 {
674         struct bcm_op *op = (struct bcm_op*)data;
675         int i = 0;
676
677         /* mark disabled / consumed timer */
678         op->thrtimer.expires = 0;
679
680         if (op->nframes > 1) {
681                 DBG("sending MUX RX_CHANGED for can_id %03X. op is %p\n",
682                     op->can_id, op);
683                 /* for MUX filter we start at index 1 */
684                 for (i=1; i<op->nframes; i++) {
685                         if ((op->last_frames) &&
686                             (op->last_frames[i].can_dlc & RX_THR)) {
687                                 op->last_frames[i].can_dlc &= ~RX_THR;
688                                 bcm_rx_changed(op, &op->last_frames[i]);
689                         }
690                 }
691
692         } else {
693                 DBG("sending simple RX_CHANGED for can_id %03X. op is %p\n",
694                     op->can_id, op);
695                 /* for RX_FILTER_ID and simple filter */
696                 if (op->last_frames && (op->last_frames[0].can_dlc & RX_THR)) {
697                         op->last_frames[0].can_dlc &= ~RX_THR;
698                         bcm_rx_changed(op, &op->last_frames[0]);
699                 }
700         }
701 }
702
703 /*
704  * bcm_rx_handler - handle a CAN frame receiption
705  */
706 static void bcm_rx_handler(struct sk_buff *skb, void *data)
707 {
708         struct bcm_op *op = (struct bcm_op*)data;
709         struct can_frame rxframe;
710         int i;
711
712         /* disable timeout */
713         del_timer(&op->timer);
714
715         DBG("Called with bcm_op %p\n", op);
716
717         if (skb->len == sizeof(rxframe)) {
718                 memcpy(&rxframe, skb->data, sizeof(rxframe));
719                 /* save rx timestamp */
720 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
721                 op->rx_stamp = skb->tstamp;
722 #else
723                 skb_get_timestamp(skb, &op->rx_stamp);
724 #endif
725                 /* save originator for recvfrom() */
726                 op->rx_ifindex = skb->dev->ifindex;
727                 /* update statistics */
728                 op->frames_abs++;
729                 kfree_skb(skb);
730                 DBG("got can_frame with can_id %03X\n", rxframe.can_id);
731
732         } else {
733                 DBG("Wrong skb->len = %d\n", skb->len);
734                 kfree_skb(skb);
735                 return;
736         }
737
738         DBG_FRAME("BCM: bcm_rx_handler: CAN frame", &rxframe);
739
740         if (op->can_id != rxframe.can_id) {
741                 DBG("ERROR! Got wrong can_id %03X! Expected %03X.\n",
742                     rxframe.can_id, op->can_id);
743                 return;
744         }
745
746         if (op->flags & RX_RTR_FRAME) {
747                 /* send reply for RTR-request */
748                 DBG("RTR-request\n");
749
750                 /* send op->frames[0] to CAN device */
751                 bcm_can_tx(op);
752                 return;
753         }
754
755         if (op->flags & RX_FILTER_ID) {
756                 /* the easiest case */
757                 DBG("Easy does it with RX_FILTER_ID\n");
758
759                 bcm_rx_update_and_send(op, &op->last_frames[0], &rxframe);
760                 bcm_rx_starttimer(op);
761                 return;
762         }
763
764         if (op->nframes == 1) {
765                 /* simple compare with index 0 */
766                 DBG("Simple compare\n");
767
768                 bcm_rx_cmp_to_index(op, 0, &rxframe);
769                 bcm_rx_starttimer(op);
770                 return;
771         }
772
773         if (op->nframes > 1) {
774                 /* multiplex compare */
775                 DBG("Multiplex compare\n");
776
777                 /*
778                  * find the first multiplex mask that fits.
779                  * Remark: The MUX-mask is stored in index 0
780                  */
781
782                 for (i=1; i < op->nframes; i++) {
783                         if ((GET_U64(&op->frames[0]) & GET_U64(&rxframe)) ==
784                             (GET_U64(&op->frames[0]) &
785                              GET_U64(&op->frames[i]))) {
786                                 DBG("found MUX index %d\n", i);
787                                 bcm_rx_cmp_to_index(op, i, &rxframe);
788                                 break;
789                         }
790                 }
791                 bcm_rx_starttimer(op);
792         }
793 }
794
795 /*
796  * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
797  */
798 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
799                                   int ifindex)
800 {
801         struct bcm_op *op;
802
803         list_for_each_entry(op, ops, list) {
804                 if ((op->can_id == can_id) && (op->ifindex == ifindex))
805                         return op;
806         }
807
808         return NULL;
809 }
810
811 static void bcm_remove_op(struct bcm_op *op)
812 {
813         del_timer(&op->timer);
814         del_timer(&op->thrtimer);
815
816         if ((op->frames) && (op->frames != &op->sframe))
817                 kfree(op->frames);
818
819         if ((op->last_frames) && (op->last_frames != &op->last_sframe))
820                 kfree(op->last_frames);
821
822         kfree(op);
823
824         return;
825 }
826
827 static void bcm_rx_unreg(struct net_device *dev, struct bcm_op *op)
828 {
829         if (op->rx_reg_dev == dev) {
830                 can_rx_unregister(dev, op->can_id, REGMASK(op->can_id),
831                                   bcm_rx_handler, op);
832
833                 /* mark as removed subscription */
834                 op->rx_reg_dev = NULL;
835         } else
836                 printk(KERN_ERR "can-bcm: bcm_rx_unreg: registered device "
837                        "mismatch %p %p\n", op->rx_reg_dev, dev);
838 }
839
840 /*
841  * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
842  */
843 static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
844 {
845         struct bcm_op *op, *n;
846
847         list_for_each_entry_safe(op, n, ops, list) {
848                 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
849                         DBG("removing rx_op %p for can_id %03X\n",
850                             op, op->can_id);
851
852                         /*
853                          * Don't care if we're bound or not (due to netdev
854                          * problems) can_rx_unregister() is always a save
855                          * thing to do here.
856                          */
857                         if (op->ifindex) {
858                                 /*
859                                  * Only remove subscriptions that had not
860                                  * been removed due to NETDEV_UNREGISTER
861                                  * in bcm_notifier()
862                                  */
863                                 if (op->rx_reg_dev) {
864                                         struct net_device *dev;
865
866                                         dev = dev_get_by_index(op->ifindex);
867                                         if (dev) {
868                                                 bcm_rx_unreg(dev, op);
869                                                 dev_put(dev);
870                                         }
871                                 }
872                         } else
873                                 can_rx_unregister(NULL, op->can_id,
874                                                   REGMASK(op->can_id),
875                                                   bcm_rx_handler, op);
876
877                         list_del(&op->list);
878                         bcm_remove_op(op);
879                         return 1; /* done */
880                 }
881         }
882
883         return 0; /* not found */
884 }
885
886 /*
887  * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
888  */
889 static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
890 {
891         struct bcm_op *op, *n;
892
893         list_for_each_entry_safe(op, n, ops, list) {
894                 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
895                         DBG("removing rx_op %p for can_id %03X\n",
896                             op, op->can_id);
897                         list_del(&op->list);
898                         bcm_remove_op(op);
899                         return 1; /* done */
900                 }
901         }
902
903         return 0; /* not found */
904 }
905
906 /*
907  * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg)
908  */
909 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
910                        int ifindex)
911 {
912         struct bcm_op *op = bcm_find_op(ops, msg_head->can_id, ifindex);
913
914         if (!op) {
915                 DBG("TRX_READ: did not find op for can_id %03X\n",
916                     msg_head->can_id);
917                 return -EINVAL;
918         }
919
920         DBG("TRX_READ: sending status for can_id %03X\n",
921             msg_head->can_id);
922         /* put current values into msg_head */
923         msg_head->flags   = op->flags;
924         msg_head->count   = op->count;
925         msg_head->ival1   = op->ival1;
926         msg_head->ival2   = op->ival2;
927         msg_head->nframes = op->nframes;
928
929         bcm_send_to_user(op, msg_head, op->frames, 0);
930
931         return MHSIZ;
932 }
933
934 /*
935  * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg)
936  */
937 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
938                         int ifindex, struct sock *sk)
939 {
940         struct bcm_sock *bo = bcm_sk(sk);
941         struct bcm_op *op;
942         int i, err;
943
944         /* we need a real device to send frames */
945         if (!ifindex)
946                 return -ENODEV;
947
948         /* we need at least one can_frame */
949         if (msg_head->nframes < 1)
950                 return -EINVAL;
951
952         /* check the given can_id */
953         op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex);
954
955         if (op) {
956                 /* update existing BCM operation */
957
958                 DBG("TX_SETUP: modifying existing tx_op %p for can_id %03X\n",
959                     op, msg_head->can_id);
960
961                 /*
962                  * Do we need more space for the can_frames than currently
963                  * allocated? -> This is a _really_ unusual use-case and
964                  * therefore (complexity / locking) it is not supported.
965                  */
966                 if (msg_head->nframes > op->nframes)
967                         return -E2BIG;
968
969                 /* update can_frames content */
970                 for (i = 0; i < msg_head->nframes; i++) {
971                         err = memcpy_fromiovec((u8*)&op->frames[i],
972                                                msg->msg_iov, CFSIZ);
973                         if (err < 0)
974                                 return err;
975
976                         if (msg_head->flags & TX_CP_CAN_ID) {
977                                 /* copy can_id into frame */
978                                 op->frames[i].can_id = msg_head->can_id;
979                         }
980                 }
981
982         } else {
983                 /* insert new BCM operation for the given can_id */
984
985                 op = kzalloc(OPSIZ, GFP_KERNEL);
986                 if (!op)
987                         return -ENOMEM;
988
989                 DBG("TX_SETUP: creating new tx_op %p for can_id %03X\n",
990                     op, msg_head->can_id);
991
992                 op->can_id    = msg_head->can_id;
993
994                 /* create array for can_frames and copy the data */
995                 if (msg_head->nframes > 1) {
996                         op->frames = kmalloc(msg_head->nframes * CFSIZ,
997                                              GFP_KERNEL);
998                         if (!op->frames) {
999                                 kfree(op);
1000                                 return -ENOMEM;
1001                         }
1002                 } else
1003                         op->frames = &op->sframe;
1004
1005                 for (i = 0; i < msg_head->nframes; i++) {
1006                         err = memcpy_fromiovec((u8*)&op->frames[i],
1007                                                msg->msg_iov, CFSIZ);
1008                         if (err < 0) {
1009                                 if (op->frames != &op->sframe)
1010                                         kfree(op->frames);
1011                                 kfree(op);
1012                                 return err;
1013                         }
1014
1015                         if (msg_head->flags & TX_CP_CAN_ID) {
1016                                 /* copy can_id into frame */
1017                                 op->frames[i].can_id = msg_head->can_id;
1018                         }
1019                 }
1020
1021                 /* tx_ops never compare with previous received messages */
1022                 op->last_frames = NULL;
1023
1024                 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1025                 op->sk = sk;
1026
1027                 op->ifindex = ifindex;
1028
1029                 /* initialize uninitialized (kmalloc) structure */
1030                 init_timer(&op->timer);
1031
1032                 /* currently unused in tx_ops */
1033                 init_timer(&op->thrtimer);
1034
1035                 /* handler for tx_ops */
1036                 op->timer.function = bcm_tx_timeout_handler;
1037
1038                 /* timer.data points to this op-structure */
1039                 op->timer.data = (unsigned long)op;
1040
1041                 /* add this bcm_op to the list of the tx_ops */
1042                 list_add(&op->list, &bo->tx_ops);
1043
1044         } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
1045
1046         if (op->nframes != msg_head->nframes) {
1047                 op->nframes   = msg_head->nframes;
1048                 /* start multiple frame transmission with index 0 */
1049                 op->currframe = 0;
1050         }
1051
1052         /* check flags */
1053
1054         op->flags = msg_head->flags;
1055
1056         if (op->flags & TX_RESET_MULTI_IDX) {
1057                 /* start multiple frame transmission with index 0 */
1058                 op->currframe = 0;
1059         }
1060
1061         if (op->flags & SETTIMER) {
1062                 /* set timer values */
1063
1064                 op->count = msg_head->count;
1065                 op->ival1 = msg_head->ival1;
1066                 op->ival2 = msg_head->ival2;
1067                 op->j_ival1 = rounded_tv2jif(&msg_head->ival1);
1068                 op->j_ival2 = rounded_tv2jif(&msg_head->ival2);
1069
1070                 DBG("TX_SETUP: SETTIMER count=%d j_ival1=%ld j_ival2=%ld\n",
1071                     op->count, op->j_ival1, op->j_ival2);
1072
1073                 /* disable an active timer due to zero values? */
1074                 if (!op->j_ival1 && !op->j_ival2) {
1075                         del_timer(&op->timer);
1076                         DBG("TX_SETUP: SETTIMER disabled timer.\n");
1077                 }
1078         }
1079
1080         if ((op->flags & STARTTIMER) &&
1081             ((op->j_ival1 && op->count) || op->j_ival2)) {
1082
1083                 del_timer(&op->timer);
1084
1085                 /* spec: send can_frame when starting timer */
1086                 op->flags |= TX_ANNOUNCE;
1087
1088                 if (op->j_ival1 && (op->count > 0)) {
1089                         op->timer.expires = jiffies + op->j_ival1;
1090                         /* op->count-- is done in bcm_tx_timeout_handler */
1091                         DBG("TX_SETUP: adding timer ival1. func=%p data=%p "
1092                             "exp=0x%08X\n",
1093                             op->timer.function,
1094                             (char*) op->timer.data,
1095                             (unsigned int) op->timer.expires);
1096
1097                 } else {
1098                         op->timer.expires = jiffies + op->j_ival2;
1099                         DBG("TX_SETUP: adding timer ival2. func=%p data=%p "
1100                             "exp=0x%08X\n",
1101                             op->timer.function,
1102                             (char*) op->timer.data,
1103                             (unsigned int) op->timer.expires);
1104                 }
1105
1106                 add_timer(&op->timer);
1107         }
1108
1109         if (op->flags & TX_ANNOUNCE)
1110                 bcm_can_tx(op);
1111
1112         return msg_head->nframes * CFSIZ + MHSIZ;
1113 }
1114
1115 /*
1116  * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
1117  */
1118 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
1119                         int ifindex, struct sock *sk)
1120 {
1121         struct bcm_sock *bo = bcm_sk(sk);
1122         struct bcm_op *op;
1123         int do_rx_register;
1124         int err;
1125
1126         if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
1127                 /* be robust against wrong usage ... */
1128                 msg_head->flags |= RX_FILTER_ID;
1129                 msg_head->nframes = 0; /* ignore trailing garbage */
1130         }
1131
1132         if ((msg_head->flags & RX_RTR_FRAME) &&
1133             ((msg_head->nframes != 1) ||
1134              (!(msg_head->can_id & CAN_RTR_FLAG)))) {
1135
1136                 DBG("RX_SETUP: bad RX_RTR_FRAME setup!\n");
1137                 return -EINVAL;
1138         }
1139
1140         /* check the given can_id */
1141         op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex);
1142         if (op) {
1143                 /* update existing BCM operation */
1144
1145                 DBG("RX_SETUP: modifying existing rx_op %p for can_id %03X\n",
1146                     op, msg_head->can_id);
1147
1148                 /*
1149                  * Do we need more space for the can_frames than currently
1150                  * allocated? -> This is a _really_ unusual use-case and
1151                  * therefore (complexity / locking) it is not supported.
1152                  */
1153                 if (msg_head->nframes > op->nframes)
1154                         return -E2BIG;
1155
1156                 if (msg_head->nframes) {
1157                         /* update can_frames content */
1158                         err = memcpy_fromiovec((u8*)op->frames,
1159                                                msg->msg_iov,
1160                                                msg_head->nframes * CFSIZ);
1161                         if (err < 0)
1162                                 return err;
1163
1164                         /* clear last_frames to indicate 'nothing received' */
1165                         memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
1166                 }
1167
1168                 op->nframes = msg_head->nframes;
1169
1170                 /* Only an update -> do not call can_rx_register() */
1171                 do_rx_register = 0;
1172
1173         } else {
1174                 /* insert new BCM operation for the given can_id */
1175
1176                 op = kzalloc(OPSIZ, GFP_KERNEL);
1177                 if (!op)
1178                         return -ENOMEM;
1179
1180                 DBG("RX_SETUP: creating new rx_op %p for can_id %03X\n",
1181                     op, msg_head->can_id);
1182
1183                 op->can_id    = msg_head->can_id;
1184                 op->nframes   = msg_head->nframes;
1185
1186                 if (msg_head->nframes > 1) {
1187                         /* create array for can_frames and copy the data */
1188                         op->frames = kmalloc(msg_head->nframes * CFSIZ,
1189                                              GFP_KERNEL);
1190                         if (!op->frames) {
1191                                 kfree(op);
1192                                 return -ENOMEM;
1193                         }
1194
1195                         /* create and init array for received can_frames */
1196                         op->last_frames = kzalloc(msg_head->nframes * CFSIZ,
1197                                                   GFP_KERNEL);
1198                         if (!op->last_frames) {
1199                                 kfree(op->frames);
1200                                 kfree(op);
1201                                 return -ENOMEM;
1202                         }
1203
1204                 } else {
1205                         op->frames = &op->sframe;
1206                         op->last_frames = &op->last_sframe;
1207                 }
1208
1209                 if (msg_head->nframes) {
1210                         err = memcpy_fromiovec((u8*)op->frames, msg->msg_iov,
1211                                                msg_head->nframes * CFSIZ);
1212                         if (err < 0) {
1213                                 if (op->frames != &op->sframe)
1214                                         kfree(op->frames);
1215                                 if (op->last_frames != &op->last_sframe)
1216                                         kfree(op->last_frames);
1217                                 kfree(op);
1218                                 return err;
1219                         }
1220                 }
1221
1222                 op->sk = sk;
1223                 op->ifindex = ifindex;
1224
1225                 /* initialize uninitialized (kzalloc) structure */
1226                 init_timer(&op->timer);
1227
1228                 /* init throttle timer for RX_CHANGED */
1229                 init_timer(&op->thrtimer);
1230
1231                 /* handler for rx timeouts */
1232                 op->timer.function = bcm_rx_timeout_handler;
1233
1234                 /* timer.data points to this op-structure */
1235                 op->timer.data = (unsigned long)op;
1236
1237                 /* handler for RX_CHANGED throttle timeouts */
1238                 op->thrtimer.function = bcm_rx_thr_handler;
1239
1240                 /* timer.data points to this op-structure */
1241                 op->thrtimer.data = (unsigned long)op;
1242
1243                 /* mark disabled timer */
1244                 op->thrtimer.expires = 0;
1245
1246                 /* add this bcm_op to the list of the tx_ops */
1247                 list_add(&op->list, &bo->rx_ops);
1248
1249                 /* call can_rx_register() */
1250                 do_rx_register = 1;
1251
1252         } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
1253
1254         /* check flags */
1255         op->flags = msg_head->flags;
1256
1257         if (op->flags & RX_RTR_FRAME) {
1258
1259                 /* no timers in RTR-mode */
1260                 del_timer(&op->thrtimer);
1261                 del_timer(&op->timer);
1262
1263                 /*
1264                  * funny feature in RX(!)_SETUP only for RTR-mode:
1265                  * copy can_id into frame BUT without RTR-flag to
1266                  * prevent a full-load-loopback-test ... ;-]
1267                  */
1268                 if ((op->flags & TX_CP_CAN_ID) ||
1269                     (op->frames[0].can_id == op->can_id))
1270                         op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
1271
1272         } else {
1273                 if (op->flags & SETTIMER) {
1274
1275                         /* set timer value */
1276                         op->ival1 = msg_head->ival1;
1277                         op->ival2 = msg_head->ival2;
1278                         op->j_ival1 = rounded_tv2jif(&msg_head->ival1);
1279                         op->j_ival2 = rounded_tv2jif(&msg_head->ival2);
1280
1281                         DBG("RX_SETUP: SETTIMER j_ival1=%ld j_ival2=%ld\n",
1282                             op->j_ival1, op->j_ival2);
1283
1284                         /* disable an active timer due to zero value? */
1285                         if (!op->j_ival1) {
1286                                 del_timer(&op->timer);
1287                                 DBG("RX_SETUP: disabled timer rx timeouts.\n");
1288                         }
1289
1290                         /* free currently blocked msgs ? */
1291                         if (op->thrtimer.expires) {
1292                                 DBG("RX_SETUP: unblocking throttled msgs.\n");
1293                                 del_timer(&op->thrtimer);
1294                                 /* send blocked msgs hereafter */
1295                                 op->thrtimer.expires = jiffies + 2;
1296                                 add_timer(&op->thrtimer);
1297                         }
1298                         /*
1299                          * if (op->j_ival2) is zero, no (new) throttling
1300                          * will happen. For details see functions
1301                          * bcm_rx_update_and_send() and bcm_rx_thr_handler()
1302                          */
1303                 }
1304
1305                 if ((op->flags & STARTTIMER) && op->j_ival1) {
1306
1307                         del_timer(&op->timer);
1308                         op->timer.expires = jiffies + op->j_ival1;
1309
1310                         DBG("RX_SETUP: adding timer ival1. func=%p data=%p"
1311                             " exp=0x%08X\n",
1312                             (char *) op->timer.function,
1313                             (char *) op->timer.data,
1314                             (unsigned int) op->timer.expires);
1315
1316                         add_timer(&op->timer);
1317                 }
1318         }
1319
1320         /* now we can register for can_ids, if we added a new bcm_op */
1321         if (do_rx_register) {
1322                 DBG("RX_SETUP: can_rx_register() for can_id %03X. "
1323                     "rx_op is %p\n", op->can_id, op);
1324
1325                 if (ifindex) {
1326                         struct net_device *dev = dev_get_by_index(ifindex);
1327
1328                         if (dev) {
1329                                 can_rx_register(dev, op->can_id,
1330                                                 REGMASK(op->can_id),
1331                                                 bcm_rx_handler, op, IDENT);
1332                                 op->rx_reg_dev = dev;
1333                                 dev_put(dev);
1334                         }
1335
1336                 } else
1337                         can_rx_register(NULL, op->can_id, REGMASK(op->can_id),
1338                                         bcm_rx_handler, op, IDENT);
1339         }
1340
1341         return msg_head->nframes * CFSIZ + MHSIZ;
1342 }
1343
1344 /*
1345  * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1346  */
1347 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
1348 {
1349         struct sk_buff *skb;
1350         struct net_device *dev;
1351         int err;
1352
1353         /* just copy and send one can_frame */
1354
1355         if (!ifindex) /* we need a real device to send frames */
1356                 return -ENODEV;
1357
1358         skb = alloc_skb(CFSIZ, GFP_KERNEL);
1359
1360         if (!skb)
1361                 return -ENOMEM;
1362
1363         err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ);
1364         if (err < 0) {
1365                 kfree_skb(skb);
1366                 return err;
1367         }
1368
1369         DBG_FRAME("BCM: TX_SEND: sending frame",
1370                   (struct can_frame *)skb->data);
1371
1372         dev = dev_get_by_index(ifindex);
1373         if (!dev) {
1374                 kfree_skb(skb);
1375                 return -ENODEV;
1376         }
1377
1378         skb->dev = dev;
1379         skb->sk  = sk;
1380         can_send(skb, 1); /* send with loopback */
1381         dev_put(dev);
1382
1383         return CFSIZ + MHSIZ;
1384 }
1385
1386 /*
1387  * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1388  */
1389 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
1390                        struct msghdr *msg, size_t size)
1391 {
1392         struct sock *sk = sock->sk;
1393         struct bcm_sock *bo = bcm_sk(sk);
1394         int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
1395         struct bcm_msg_head msg_head;
1396         int ret; /* read bytes or error codes as return value */
1397
1398         if (!bo->bound) {
1399                 DBG("sock %p not bound\n", sk);
1400                 return -ENOTCONN;
1401         }
1402
1403         /* check for alternative ifindex for this bcm_op */
1404
1405         if (!ifindex && msg->msg_name) {
1406                 /* no bound device as default => check msg_name */
1407                 struct sockaddr_can *addr =
1408                         (struct sockaddr_can *)msg->msg_name;
1409
1410                 if (addr->can_family != AF_CAN)
1411                         return -EINVAL;
1412
1413                 ifindex = addr->can_ifindex; /* ifindex from sendto() */
1414
1415                 if (ifindex) {
1416                         struct net_device *dev = dev_get_by_index(ifindex);
1417
1418                         if (!dev) {
1419                                 DBG("device %d not found\n", ifindex);
1420                                 return -ENODEV;
1421                         }
1422
1423                         if (dev->type != ARPHRD_CAN) {
1424                                 DBG("device %d no CAN device\n", ifindex);
1425                                 dev_put(dev);
1426                                 return -ENODEV;
1427                         }
1428
1429                         dev_put(dev);
1430                 }
1431         }
1432
1433         /* read message head information */
1434
1435         ret = memcpy_fromiovec((u8*)&msg_head, msg->msg_iov, MHSIZ);
1436         if (ret < 0)
1437                 return ret;
1438
1439         DBG("opcode %d for can_id %03X\n", msg_head.opcode, msg_head.can_id);
1440
1441         lock_sock(sk);
1442
1443         switch (msg_head.opcode) {
1444
1445         case TX_SETUP:
1446                 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
1447                 break;
1448
1449         case RX_SETUP:
1450                 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
1451                 break;
1452
1453         case TX_DELETE:
1454                 if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
1455                         ret = MHSIZ;
1456                 else
1457                         ret = -EINVAL;
1458                 break;
1459
1460         case RX_DELETE:
1461                 if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
1462                         ret = MHSIZ;
1463                 else
1464                         ret = -EINVAL;
1465                 break;
1466
1467         case TX_READ:
1468                 /* reuse msg_head for the reply to TX_READ */
1469                 msg_head.opcode  = TX_STATUS;
1470                 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
1471                 break;
1472
1473         case RX_READ:
1474                 /* reuse msg_head for the reply to RX_READ */
1475                 msg_head.opcode  = RX_STATUS;
1476                 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
1477                 break;
1478
1479         case TX_SEND:
1480                 /* we need at least one can_frame */
1481                 if (msg_head.nframes < 1)
1482                         ret = -EINVAL;
1483                 else
1484                         ret = bcm_tx_send(msg, ifindex, sk);
1485                 break;
1486
1487         default:
1488                 DBG("Unknown opcode %d\n", msg_head.opcode);
1489                 ret = -EINVAL;
1490                 break;
1491         }
1492
1493         release_sock(sk);
1494
1495         return ret;
1496 }
1497
1498 /*
1499  * notification handler for netdevice status changes
1500  */
1501 static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
1502                         void *data)
1503 {
1504         struct net_device *dev = (struct net_device *)data;
1505         struct bcm_sock *bo = container_of(nb, struct bcm_sock, notifier);
1506 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
1507         struct sock *sk = &bo->sk;
1508 #else
1509         struct sock *sk = bo->sk;
1510 #endif
1511         struct bcm_op *op;
1512         int notify_enodev = 0;
1513
1514         DBG("msg %ld for dev %p (%s idx %d) sk %p bo->ifindex %d\n",
1515             msg, dev, dev->name, dev->ifindex, sk, bo->ifindex);
1516
1517         if (dev->type != ARPHRD_CAN)
1518                 return NOTIFY_DONE;
1519
1520         switch (msg) {
1521
1522         case NETDEV_UNREGISTER:
1523                 lock_sock(sk);
1524
1525                 /* remove device specific receive entries */
1526                 list_for_each_entry(op, &bo->rx_ops, list)
1527                         if (op->rx_reg_dev == dev)
1528                                 bcm_rx_unreg(dev, op);
1529
1530                 /* remove device reference, if this is our bound device */
1531                 if (bo->bound && bo->ifindex == dev->ifindex) {
1532                         bo->bound   = 0;
1533                         bo->ifindex = 0;
1534                         notify_enodev = 1;
1535                 }
1536
1537                 release_sock(sk);
1538
1539                 if (notify_enodev) {
1540                         sk->sk_err = ENODEV;
1541                         if (!sock_flag(sk, SOCK_DEAD))
1542                                 sk->sk_error_report(sk);
1543                 }
1544                 break;
1545
1546         case NETDEV_DOWN:
1547                 if (bo->bound && bo->ifindex == dev->ifindex) {
1548                         sk->sk_err = ENETDOWN;
1549                         if (!sock_flag(sk, SOCK_DEAD))
1550                                 sk->sk_error_report(sk);
1551                 }
1552         }
1553
1554         return NOTIFY_DONE;
1555 }
1556
1557 /*
1558  * initial settings for all BCM sockets to be set at socket creation time
1559  */
1560 static int bcm_init(struct sock *sk)
1561 {
1562         struct bcm_sock *bo = bcm_sk(sk);
1563
1564 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,12)
1565         bo->sk               = sk;
1566 #endif
1567         bo->bound            = 0;
1568         bo->ifindex          = 0;
1569         bo->dropped_usr_msgs = 0;
1570         bo->bcm_proc_read    = NULL;
1571
1572         INIT_LIST_HEAD(&bo->tx_ops);
1573         INIT_LIST_HEAD(&bo->rx_ops);
1574
1575         /* set notifier */
1576         bo->notifier.notifier_call = bcm_notifier;
1577
1578         register_netdevice_notifier(&bo->notifier);
1579
1580         return 0;
1581 }
1582
1583 /*
1584  * standard socket functions
1585  */
1586 static int bcm_release(struct socket *sock)
1587 {
1588         struct sock *sk = sock->sk;
1589         struct bcm_sock *bo = bcm_sk(sk);
1590         struct bcm_op *op, *next;
1591
1592         DBG("socket %p, sk %p\n", sock, sk);
1593
1594         /* remove bcm_ops, timer, rx_unregister(), etc. */
1595
1596         unregister_netdevice_notifier(&bo->notifier);
1597
1598         lock_sock(sk);
1599
1600         list_for_each_entry_safe(op, next, &bo->tx_ops, list) {
1601                 DBG("removing tx_op %p for can_id %03X\n", op, op->can_id);
1602                 bcm_remove_op(op);
1603         }
1604
1605         list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
1606                 DBG("removing rx_op %p for can_id %03X\n", op, op->can_id);
1607
1608                 /*
1609                  * Don't care if we're bound or not (due to netdev problems)
1610                  * can_rx_unregister() is always a save thing to do here.
1611                  */
1612                 if (op->ifindex) {
1613                         /*
1614                          * Only remove subscriptions that had not
1615                          * been removed due to NETDEV_UNREGISTER
1616                          * in bcm_notifier()
1617                          */
1618                         if (op->rx_reg_dev) {
1619                                 struct net_device *dev;
1620
1621                                 dev = dev_get_by_index(op->ifindex);
1622                                 if (dev) {
1623                                         bcm_rx_unreg(dev, op);
1624                                         dev_put(dev);
1625                                 }
1626                         }
1627                 } else
1628                         can_rx_unregister(NULL, op->can_id,
1629                                           REGMASK(op->can_id),
1630                                           bcm_rx_handler, op);
1631
1632                 bcm_remove_op(op);
1633         }
1634
1635         /* remove procfs entry */
1636         if (proc_dir && bo->bcm_proc_read)
1637                 remove_proc_entry(bo->procname, proc_dir);
1638
1639         /* remove device reference */
1640         if (bo->bound) {
1641                 bo->bound   = 0;
1642                 bo->ifindex = 0;
1643         }
1644
1645         release_sock(sk);
1646         sock_put(sk);
1647
1648         return 0;
1649 }
1650
1651 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1652                        int flags)
1653 {
1654         struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1655         struct sock *sk = sock->sk;
1656         struct bcm_sock *bo = bcm_sk(sk);
1657
1658         if (bo->bound)
1659                 return -EISCONN;
1660
1661         /* bind a device to this socket */
1662         if (addr->can_ifindex) {
1663                 struct net_device *dev = dev_get_by_index(addr->can_ifindex);
1664
1665                 if (!dev) {
1666                         DBG("could not find device index %d\n",
1667                             addr->can_ifindex);
1668                         return -ENODEV;
1669                 }
1670
1671                 if (dev->type != ARPHRD_CAN) {
1672                         DBG("device %d no CAN device\n", addr->can_ifindex);
1673                         dev_put(dev);
1674                         return -ENODEV;
1675                 }
1676
1677                 bo->ifindex = dev->ifindex;
1678                 dev_put(dev);
1679
1680                 DBG("socket %p bound to device %s (idx %d)\n",
1681                     sock, dev->name, dev->ifindex);
1682
1683         } else {
1684                 /* no interface reference for ifindex = 0 ('any' CAN device) */
1685                 bo->ifindex = 0;
1686         }
1687
1688         bo->bound = 1;
1689
1690         if (proc_dir) {
1691                 /* unique socket address as filename */
1692                 sprintf(bo->procname, "%p", sock);
1693                 bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
1694                                                            proc_dir,
1695                                                            bcm_read_proc, sk);
1696         }
1697
1698         return 0;
1699 }
1700
1701 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
1702                        struct msghdr *msg, size_t size, int flags)
1703 {
1704         struct sock *sk = sock->sk;
1705         struct sk_buff *skb;
1706         int error = 0;
1707         int noblock;
1708         int err;
1709
1710         DBG("socket %p, sk %p\n", sock, sk);
1711
1712         noblock =  flags & MSG_DONTWAIT;
1713         flags   &= ~MSG_DONTWAIT;
1714         skb = skb_recv_datagram(sk, flags, noblock, &error);
1715         if (!skb)
1716                 return error;
1717
1718         DBG("delivering skbuff %p\n", skb);
1719         DBG_SKB(skb);
1720
1721         if (skb->len < size)
1722                 size = skb->len;
1723
1724         err = memcpy_toiovec(msg->msg_iov, skb->data, size);
1725         if (err < 0) {
1726                 skb_free_datagram(sk, skb);
1727                 return err;
1728         }
1729
1730         sock_recv_timestamp(msg, sk, skb);
1731
1732         if (msg->msg_name) {
1733                 msg->msg_namelen = sizeof(struct sockaddr_can);
1734                 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1735         }
1736
1737         DBG("freeing sock %p, skbuff %p\n", sk, skb);
1738         skb_free_datagram(sk, skb);
1739
1740         return size;
1741 }
1742
1743 static unsigned int bcm_poll(struct file *file, struct socket *sock,
1744                              poll_table *wait)
1745 {
1746         unsigned int mask = 0;
1747
1748         DBG("socket %p\n", sock);
1749
1750         mask = datagram_poll(file, sock, wait);
1751         return mask;
1752 }
1753
1754 static struct proto_ops bcm_ops = {
1755         .family        = PF_CAN,
1756         .release       = bcm_release,
1757         .bind          = sock_no_bind,
1758         .connect       = bcm_connect,
1759         .socketpair    = sock_no_socketpair,
1760         .accept        = sock_no_accept,
1761         .getname       = sock_no_getname,
1762         .poll          = bcm_poll,
1763         .ioctl         = NULL,          /* use can_ioctl() from af_can.c */
1764         .listen        = sock_no_listen,
1765         .shutdown      = sock_no_shutdown,
1766         .setsockopt    = sock_no_setsockopt,
1767         .getsockopt    = sock_no_getsockopt,
1768         .sendmsg       = bcm_sendmsg,
1769         .recvmsg       = bcm_recvmsg,
1770         .mmap          = sock_no_mmap,
1771         .sendpage      = sock_no_sendpage,
1772 };
1773
1774 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
1775 static struct proto bcm_proto = {
1776         .name       = "CAN_BCM",
1777         .owner      = THIS_MODULE,
1778         .obj_size   = sizeof(struct bcm_sock),
1779         .init       = bcm_init,
1780 };
1781
1782 static struct can_proto bcm_can_proto = {
1783         .type       = SOCK_DGRAM,
1784         .protocol   = CAN_BCM,
1785         .capability = BCM_CAP,
1786         .ops        = &bcm_ops,
1787         .prot       = &bcm_proto,
1788 };
1789 #else
1790 static struct can_proto bcm_can_proto = {
1791         .type       = SOCK_DGRAM,
1792         .protocol   = CAN_BCM,
1793         .capability = BCM_CAP,
1794         .ops        = &bcm_ops,
1795         .owner      = THIS_MODULE,
1796         .obj_size   = sizeof(struct bcm_sock),
1797         .init       = bcm_init,
1798 };
1799 #endif
1800
1801 static int __init bcm_module_init(void)
1802 {
1803         printk(banner);
1804
1805         can_proto_register(&bcm_can_proto);
1806
1807         /* create /proc/net/can/bcm directory */
1808         proc_dir = proc_mkdir(CAN_PROC_DIR"/"IDENT, NULL);
1809
1810         if (proc_dir)
1811                 proc_dir->owner = THIS_MODULE;
1812
1813         return 0;
1814 }
1815
1816 static void __exit bcm_module_exit(void)
1817 {
1818         can_proto_unregister(&bcm_can_proto);
1819
1820         if (proc_dir)
1821                 remove_proc_entry(CAN_PROC_DIR"/"IDENT, NULL);
1822 }
1823
1824 module_init(bcm_module_init);
1825 module_exit(bcm_module_exit);