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