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