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