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