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