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