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