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