Merge branch 'i2c-for-linus' of git://jdelvare.pck.nerim.net/jdelvare-2.6
[linux-2.6/zen-sources.git] / net / can / bcm.c
blobda0d426c0ce473f53450c65690a7bfe4a3e79f42
1 /*
2 * bcm.c - Broadcast Manager to filter/send (cyclic) CAN content
4 * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
5 * All rights reserved.
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.
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.
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.
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.
40 * Send feedback to <socketcan-users@lists.berlios.de>
44 #include <linux/module.h>
45 #include <linux/init.h>
46 #include <linux/hrtimer.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 <net/net_namespace.h>
61 /* use of last_frames[index].can_dlc */
62 #define RX_RECV 0x40 /* received data for this element */
63 #define RX_THR 0x80 /* element not been sent due to throttle feature */
64 #define BCM_CAN_DLC_MASK 0x0F /* clean private flags in can_dlc by masking */
66 /* get best masking value for can_rx_register() for a given single can_id */
67 #define REGMASK(id) ((id & CAN_EFF_FLAG) ? \
68 (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG) : \
69 (CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG))
71 #define CAN_BCM_VERSION CAN_VERSION
72 static __initdata const char banner[] = KERN_INFO
73 "can: broadcast manager protocol (rev " CAN_BCM_VERSION ")\n";
75 MODULE_DESCRIPTION("PF_CAN broadcast manager protocol");
76 MODULE_LICENSE("Dual BSD/GPL");
77 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
79 /* easy access to can_frame payload */
80 static inline u64 GET_U64(const struct can_frame *cp)
82 return *(u64 *)cp->data;
85 struct bcm_op {
86 struct list_head list;
87 int ifindex;
88 canid_t can_id;
89 int flags;
90 unsigned long frames_abs, frames_filtered;
91 struct timeval ival1, ival2;
92 struct hrtimer timer, thrtimer;
93 ktime_t rx_stamp, kt_ival1, kt_ival2, kt_lastmsg;
94 int rx_ifindex;
95 int count;
96 int nframes;
97 int currframe;
98 struct can_frame *frames;
99 struct can_frame *last_frames;
100 struct can_frame sframe;
101 struct can_frame last_sframe;
102 struct sock *sk;
103 struct net_device *rx_reg_dev;
106 static struct proc_dir_entry *proc_dir;
108 struct bcm_sock {
109 struct sock sk;
110 int bound;
111 int ifindex;
112 struct notifier_block notifier;
113 struct list_head rx_ops;
114 struct list_head tx_ops;
115 unsigned long dropped_usr_msgs;
116 struct proc_dir_entry *bcm_proc_read;
117 char procname [9]; /* pointer printed in ASCII with \0 */
120 static inline struct bcm_sock *bcm_sk(const struct sock *sk)
122 return (struct bcm_sock *)sk;
125 #define CFSIZ sizeof(struct can_frame)
126 #define OPSIZ sizeof(struct bcm_op)
127 #define MHSIZ sizeof(struct bcm_msg_head)
130 * procfs functions
132 static char *bcm_proc_getifname(int ifindex)
134 struct net_device *dev;
136 if (!ifindex)
137 return "any";
139 /* no usage counting */
140 dev = __dev_get_by_index(&init_net, ifindex);
141 if (dev)
142 return dev->name;
144 return "???";
147 static int bcm_read_proc(char *page, char **start, off_t off,
148 int count, int *eof, void *data)
150 int len = 0;
151 struct sock *sk = (struct sock *)data;
152 struct bcm_sock *bo = bcm_sk(sk);
153 struct bcm_op *op;
155 len += snprintf(page + len, PAGE_SIZE - len, ">>> socket %p",
156 sk->sk_socket);
157 len += snprintf(page + len, PAGE_SIZE - len, " / sk %p", sk);
158 len += snprintf(page + len, PAGE_SIZE - len, " / bo %p", bo);
159 len += snprintf(page + len, PAGE_SIZE - len, " / dropped %lu",
160 bo->dropped_usr_msgs);
161 len += snprintf(page + len, PAGE_SIZE - len, " / bound %s",
162 bcm_proc_getifname(bo->ifindex));
163 len += snprintf(page + len, PAGE_SIZE - len, " <<<\n");
165 list_for_each_entry(op, &bo->rx_ops, list) {
167 unsigned long reduction;
169 /* print only active entries & prevent division by zero */
170 if (!op->frames_abs)
171 continue;
173 len += snprintf(page + len, PAGE_SIZE - len,
174 "rx_op: %03X %-5s ",
175 op->can_id, bcm_proc_getifname(op->ifindex));
176 len += snprintf(page + len, PAGE_SIZE - len, "[%d]%c ",
177 op->nframes,
178 (op->flags & RX_CHECK_DLC)?'d':' ');
179 if (op->kt_ival1.tv64)
180 len += snprintf(page + len, PAGE_SIZE - len,
181 "timeo=%lld ",
182 (long long)
183 ktime_to_us(op->kt_ival1));
185 if (op->kt_ival2.tv64)
186 len += snprintf(page + len, PAGE_SIZE - len,
187 "thr=%lld ",
188 (long long)
189 ktime_to_us(op->kt_ival2));
191 len += snprintf(page + len, PAGE_SIZE - len,
192 "# recv %ld (%ld) => reduction: ",
193 op->frames_filtered, op->frames_abs);
195 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
197 len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
198 (reduction == 100)?"near ":"", reduction);
200 if (len > PAGE_SIZE - 200) {
201 /* mark output cut off */
202 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
203 break;
207 list_for_each_entry(op, &bo->tx_ops, list) {
209 len += snprintf(page + len, PAGE_SIZE - len,
210 "tx_op: %03X %s [%d] ",
211 op->can_id, bcm_proc_getifname(op->ifindex),
212 op->nframes);
214 if (op->kt_ival1.tv64)
215 len += snprintf(page + len, PAGE_SIZE - len, "t1=%lld ",
216 (long long) ktime_to_us(op->kt_ival1));
218 if (op->kt_ival2.tv64)
219 len += snprintf(page + len, PAGE_SIZE - len, "t2=%lld ",
220 (long long) ktime_to_us(op->kt_ival2));
222 len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n",
223 op->frames_abs);
225 if (len > PAGE_SIZE - 100) {
226 /* mark output cut off */
227 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
228 break;
232 len += snprintf(page + len, PAGE_SIZE - len, "\n");
234 *eof = 1;
235 return len;
239 * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
240 * of the given bcm tx op
242 static void bcm_can_tx(struct bcm_op *op)
244 struct sk_buff *skb;
245 struct net_device *dev;
246 struct can_frame *cf = &op->frames[op->currframe];
248 /* no target device? => exit */
249 if (!op->ifindex)
250 return;
252 dev = dev_get_by_index(&init_net, op->ifindex);
253 if (!dev) {
254 /* RFC: should this bcm_op remove itself here? */
255 return;
258 skb = alloc_skb(CFSIZ, gfp_any());
259 if (!skb)
260 goto out;
262 memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
264 /* send with loopback */
265 skb->dev = dev;
266 skb->sk = op->sk;
267 can_send(skb, 1);
269 /* update statistics */
270 op->currframe++;
271 op->frames_abs++;
273 /* reached last frame? */
274 if (op->currframe >= op->nframes)
275 op->currframe = 0;
276 out:
277 dev_put(dev);
281 * bcm_send_to_user - send a BCM message to the userspace
282 * (consisting of bcm_msg_head + x CAN frames)
284 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
285 struct can_frame *frames, int has_timestamp)
287 struct sk_buff *skb;
288 struct can_frame *firstframe;
289 struct sockaddr_can *addr;
290 struct sock *sk = op->sk;
291 int datalen = head->nframes * CFSIZ;
292 int err;
294 skb = alloc_skb(sizeof(*head) + datalen, gfp_any());
295 if (!skb)
296 return;
298 memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
300 if (head->nframes) {
301 /* can_frames starting here */
302 firstframe = (struct can_frame *)skb_tail_pointer(skb);
304 memcpy(skb_put(skb, datalen), frames, datalen);
307 * the BCM uses the can_dlc-element of the can_frame
308 * structure for internal purposes. This is only
309 * relevant for updates that are generated by the
310 * BCM, where nframes is 1
312 if (head->nframes == 1)
313 firstframe->can_dlc &= BCM_CAN_DLC_MASK;
316 if (has_timestamp) {
317 /* restore rx timestamp */
318 skb->tstamp = op->rx_stamp;
322 * Put the datagram to the queue so that bcm_recvmsg() can
323 * get it from there. We need to pass the interface index to
324 * bcm_recvmsg(). We pass a whole struct sockaddr_can in skb->cb
325 * containing the interface index.
328 BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct sockaddr_can));
329 addr = (struct sockaddr_can *)skb->cb;
330 memset(addr, 0, sizeof(*addr));
331 addr->can_family = AF_CAN;
332 addr->can_ifindex = op->rx_ifindex;
334 err = sock_queue_rcv_skb(sk, skb);
335 if (err < 0) {
336 struct bcm_sock *bo = bcm_sk(sk);
338 kfree_skb(skb);
339 /* don't care about overflows in this statistic */
340 bo->dropped_usr_msgs++;
345 * bcm_tx_timeout_handler - performes cyclic CAN frame transmissions
347 static enum hrtimer_restart bcm_tx_timeout_handler(struct hrtimer *hrtimer)
349 struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
350 enum hrtimer_restart ret = HRTIMER_NORESTART;
352 if (op->kt_ival1.tv64 && (op->count > 0)) {
354 op->count--;
355 if (!op->count && (op->flags & TX_COUNTEVT)) {
356 struct bcm_msg_head msg_head;
358 /* create notification to user */
359 msg_head.opcode = TX_EXPIRED;
360 msg_head.flags = op->flags;
361 msg_head.count = op->count;
362 msg_head.ival1 = op->ival1;
363 msg_head.ival2 = op->ival2;
364 msg_head.can_id = op->can_id;
365 msg_head.nframes = 0;
367 bcm_send_to_user(op, &msg_head, NULL, 0);
371 if (op->kt_ival1.tv64 && (op->count > 0)) {
373 /* send (next) frame */
374 bcm_can_tx(op);
375 hrtimer_forward(hrtimer, ktime_get(), op->kt_ival1);
376 ret = HRTIMER_RESTART;
378 } else {
379 if (op->kt_ival2.tv64) {
381 /* send (next) frame */
382 bcm_can_tx(op);
383 hrtimer_forward(hrtimer, ktime_get(), op->kt_ival2);
384 ret = HRTIMER_RESTART;
388 return ret;
392 * bcm_rx_changed - create a RX_CHANGED notification due to changed content
394 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
396 struct bcm_msg_head head;
398 /* update statistics */
399 op->frames_filtered++;
401 /* prevent statistics overflow */
402 if (op->frames_filtered > ULONG_MAX/100)
403 op->frames_filtered = op->frames_abs = 0;
405 head.opcode = RX_CHANGED;
406 head.flags = op->flags;
407 head.count = op->count;
408 head.ival1 = op->ival1;
409 head.ival2 = op->ival2;
410 head.can_id = op->can_id;
411 head.nframes = 1;
413 bcm_send_to_user(op, &head, data, 1);
417 * bcm_rx_update_and_send - process a detected relevant receive content change
418 * 1. update the last received data
419 * 2. send a notification to the user (if possible)
421 static void bcm_rx_update_and_send(struct bcm_op *op,
422 struct can_frame *lastdata,
423 struct can_frame *rxdata)
425 memcpy(lastdata, rxdata, CFSIZ);
427 /* mark as used */
428 lastdata->can_dlc |= RX_RECV;
430 /* throtteling mode inactive OR data update already on the run ? */
431 if (!op->kt_ival2.tv64 || hrtimer_callback_running(&op->thrtimer)) {
432 /* send RX_CHANGED to the user immediately */
433 bcm_rx_changed(op, rxdata);
434 return;
437 if (hrtimer_active(&op->thrtimer)) {
438 /* mark as 'throttled' */
439 lastdata->can_dlc |= RX_THR;
440 return;
443 if (!op->kt_lastmsg.tv64) {
444 /* send first RX_CHANGED to the user immediately */
445 bcm_rx_changed(op, rxdata);
446 op->kt_lastmsg = ktime_get();
447 return;
450 if (ktime_us_delta(ktime_get(), op->kt_lastmsg) <
451 ktime_to_us(op->kt_ival2)) {
452 /* mark as 'throttled' and start timer */
453 lastdata->can_dlc |= RX_THR;
454 hrtimer_start(&op->thrtimer,
455 ktime_add(op->kt_lastmsg, op->kt_ival2),
456 HRTIMER_MODE_ABS);
457 return;
460 /* the gap was that big, that throttling was not needed here */
461 bcm_rx_changed(op, rxdata);
462 op->kt_lastmsg = ktime_get();
466 * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly
467 * received data stored in op->last_frames[]
469 static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
470 struct can_frame *rxdata)
473 * no one uses the MSBs of can_dlc for comparation,
474 * so we use it here to detect the first time of reception
477 if (!(op->last_frames[index].can_dlc & RX_RECV)) {
478 /* received data for the first time => send update to user */
479 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
480 return;
483 /* do a real check in can_frame data section */
485 if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) !=
486 (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) {
487 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
488 return;
491 if (op->flags & RX_CHECK_DLC) {
492 /* do a real check in can_frame dlc */
493 if (rxdata->can_dlc != (op->last_frames[index].can_dlc &
494 BCM_CAN_DLC_MASK)) {
495 bcm_rx_update_and_send(op, &op->last_frames[index],
496 rxdata);
497 return;
503 * bcm_rx_starttimer - enable timeout monitoring for CAN frame receiption
505 static void bcm_rx_starttimer(struct bcm_op *op)
507 if (op->flags & RX_NO_AUTOTIMER)
508 return;
510 if (op->kt_ival1.tv64)
511 hrtimer_start(&op->timer, op->kt_ival1, HRTIMER_MODE_REL);
515 * bcm_rx_timeout_handler - when the (cyclic) CAN frame receiption timed out
517 static enum hrtimer_restart bcm_rx_timeout_handler(struct hrtimer *hrtimer)
519 struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
520 struct bcm_msg_head msg_head;
522 msg_head.opcode = RX_TIMEOUT;
523 msg_head.flags = op->flags;
524 msg_head.count = op->count;
525 msg_head.ival1 = op->ival1;
526 msg_head.ival2 = op->ival2;
527 msg_head.can_id = op->can_id;
528 msg_head.nframes = 0;
530 bcm_send_to_user(op, &msg_head, NULL, 0);
532 /* no restart of the timer is done here! */
534 /* if user wants to be informed, when cyclic CAN-Messages come back */
535 if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
536 /* clear received can_frames to indicate 'nothing received' */
537 memset(op->last_frames, 0, op->nframes * CFSIZ);
540 return HRTIMER_NORESTART;
544 * bcm_rx_thr_flush - Check for throttled data and send it to the userspace
546 static int bcm_rx_thr_flush(struct bcm_op *op)
548 int updated = 0;
550 if (op->nframes > 1) {
551 int i;
553 /* for MUX filter we start at index 1 */
554 for (i = 1; i < op->nframes; i++) {
555 if ((op->last_frames) &&
556 (op->last_frames[i].can_dlc & RX_THR)) {
557 op->last_frames[i].can_dlc &= ~RX_THR;
558 bcm_rx_changed(op, &op->last_frames[i]);
559 updated++;
563 } else {
564 /* for RX_FILTER_ID and simple filter */
565 if (op->last_frames && (op->last_frames[0].can_dlc & RX_THR)) {
566 op->last_frames[0].can_dlc &= ~RX_THR;
567 bcm_rx_changed(op, &op->last_frames[0]);
568 updated++;
572 return updated;
576 * bcm_rx_thr_handler - the time for blocked content updates is over now:
577 * Check for throttled data and send it to the userspace
579 static enum hrtimer_restart bcm_rx_thr_handler(struct hrtimer *hrtimer)
581 struct bcm_op *op = container_of(hrtimer, struct bcm_op, thrtimer);
583 if (bcm_rx_thr_flush(op)) {
584 hrtimer_forward(hrtimer, ktime_get(), op->kt_ival2);
585 return HRTIMER_RESTART;
586 } else {
587 /* rearm throttle handling */
588 op->kt_lastmsg = ktime_set(0, 0);
589 return HRTIMER_NORESTART;
594 * bcm_rx_handler - handle a CAN frame receiption
596 static void bcm_rx_handler(struct sk_buff *skb, void *data)
598 struct bcm_op *op = (struct bcm_op *)data;
599 struct can_frame rxframe;
600 int i;
602 /* disable timeout */
603 hrtimer_cancel(&op->timer);
605 if (skb->len == sizeof(rxframe)) {
606 memcpy(&rxframe, skb->data, sizeof(rxframe));
607 /* save rx timestamp */
608 op->rx_stamp = skb->tstamp;
609 /* save originator for recvfrom() */
610 op->rx_ifindex = skb->dev->ifindex;
611 /* update statistics */
612 op->frames_abs++;
613 kfree_skb(skb);
615 } else {
616 kfree_skb(skb);
617 return;
620 if (op->can_id != rxframe.can_id)
621 return;
623 if (op->flags & RX_RTR_FRAME) {
624 /* send reply for RTR-request (placed in op->frames[0]) */
625 bcm_can_tx(op);
626 return;
629 if (op->flags & RX_FILTER_ID) {
630 /* the easiest case */
631 bcm_rx_update_and_send(op, &op->last_frames[0], &rxframe);
632 bcm_rx_starttimer(op);
633 return;
636 if (op->nframes == 1) {
637 /* simple compare with index 0 */
638 bcm_rx_cmp_to_index(op, 0, &rxframe);
639 bcm_rx_starttimer(op);
640 return;
643 if (op->nframes > 1) {
645 * multiplex compare
647 * find the first multiplex mask that fits.
648 * Remark: The MUX-mask is stored in index 0
651 for (i = 1; i < op->nframes; i++) {
652 if ((GET_U64(&op->frames[0]) & GET_U64(&rxframe)) ==
653 (GET_U64(&op->frames[0]) &
654 GET_U64(&op->frames[i]))) {
655 bcm_rx_cmp_to_index(op, i, &rxframe);
656 break;
659 bcm_rx_starttimer(op);
664 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
666 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
667 int ifindex)
669 struct bcm_op *op;
671 list_for_each_entry(op, ops, list) {
672 if ((op->can_id == can_id) && (op->ifindex == ifindex))
673 return op;
676 return NULL;
679 static void bcm_remove_op(struct bcm_op *op)
681 hrtimer_cancel(&op->timer);
682 hrtimer_cancel(&op->thrtimer);
684 if ((op->frames) && (op->frames != &op->sframe))
685 kfree(op->frames);
687 if ((op->last_frames) && (op->last_frames != &op->last_sframe))
688 kfree(op->last_frames);
690 kfree(op);
692 return;
695 static void bcm_rx_unreg(struct net_device *dev, struct bcm_op *op)
697 if (op->rx_reg_dev == dev) {
698 can_rx_unregister(dev, op->can_id, REGMASK(op->can_id),
699 bcm_rx_handler, op);
701 /* mark as removed subscription */
702 op->rx_reg_dev = NULL;
703 } else
704 printk(KERN_ERR "can-bcm: bcm_rx_unreg: registered device "
705 "mismatch %p %p\n", op->rx_reg_dev, dev);
709 * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
711 static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
713 struct bcm_op *op, *n;
715 list_for_each_entry_safe(op, n, ops, list) {
716 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
719 * Don't care if we're bound or not (due to netdev
720 * problems) can_rx_unregister() is always a save
721 * thing to do here.
723 if (op->ifindex) {
725 * Only remove subscriptions that had not
726 * been removed due to NETDEV_UNREGISTER
727 * in bcm_notifier()
729 if (op->rx_reg_dev) {
730 struct net_device *dev;
732 dev = dev_get_by_index(&init_net,
733 op->ifindex);
734 if (dev) {
735 bcm_rx_unreg(dev, op);
736 dev_put(dev);
739 } else
740 can_rx_unregister(NULL, op->can_id,
741 REGMASK(op->can_id),
742 bcm_rx_handler, op);
744 list_del(&op->list);
745 bcm_remove_op(op);
746 return 1; /* done */
750 return 0; /* not found */
754 * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
756 static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
758 struct bcm_op *op, *n;
760 list_for_each_entry_safe(op, n, ops, list) {
761 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
762 list_del(&op->list);
763 bcm_remove_op(op);
764 return 1; /* done */
768 return 0; /* not found */
772 * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg)
774 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
775 int ifindex)
777 struct bcm_op *op = bcm_find_op(ops, msg_head->can_id, ifindex);
779 if (!op)
780 return -EINVAL;
782 /* put current values into msg_head */
783 msg_head->flags = op->flags;
784 msg_head->count = op->count;
785 msg_head->ival1 = op->ival1;
786 msg_head->ival2 = op->ival2;
787 msg_head->nframes = op->nframes;
789 bcm_send_to_user(op, msg_head, op->frames, 0);
791 return MHSIZ;
795 * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg)
797 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
798 int ifindex, struct sock *sk)
800 struct bcm_sock *bo = bcm_sk(sk);
801 struct bcm_op *op;
802 int i, err;
804 /* we need a real device to send frames */
805 if (!ifindex)
806 return -ENODEV;
808 /* we need at least one can_frame */
809 if (msg_head->nframes < 1)
810 return -EINVAL;
812 /* check the given can_id */
813 op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex);
815 if (op) {
816 /* update existing BCM operation */
819 * Do we need more space for the can_frames than currently
820 * allocated? -> This is a _really_ unusual use-case and
821 * therefore (complexity / locking) it is not supported.
823 if (msg_head->nframes > op->nframes)
824 return -E2BIG;
826 /* update can_frames content */
827 for (i = 0; i < msg_head->nframes; i++) {
828 err = memcpy_fromiovec((u8 *)&op->frames[i],
829 msg->msg_iov, CFSIZ);
831 if (op->frames[i].can_dlc > 8)
832 err = -EINVAL;
834 if (err < 0)
835 return err;
837 if (msg_head->flags & TX_CP_CAN_ID) {
838 /* copy can_id into frame */
839 op->frames[i].can_id = msg_head->can_id;
843 } else {
844 /* insert new BCM operation for the given can_id */
846 op = kzalloc(OPSIZ, GFP_KERNEL);
847 if (!op)
848 return -ENOMEM;
850 op->can_id = msg_head->can_id;
852 /* create array for can_frames and copy the data */
853 if (msg_head->nframes > 1) {
854 op->frames = kmalloc(msg_head->nframes * CFSIZ,
855 GFP_KERNEL);
856 if (!op->frames) {
857 kfree(op);
858 return -ENOMEM;
860 } else
861 op->frames = &op->sframe;
863 for (i = 0; i < msg_head->nframes; i++) {
864 err = memcpy_fromiovec((u8 *)&op->frames[i],
865 msg->msg_iov, CFSIZ);
867 if (op->frames[i].can_dlc > 8)
868 err = -EINVAL;
870 if (err < 0) {
871 if (op->frames != &op->sframe)
872 kfree(op->frames);
873 kfree(op);
874 return err;
877 if (msg_head->flags & TX_CP_CAN_ID) {
878 /* copy can_id into frame */
879 op->frames[i].can_id = msg_head->can_id;
883 /* tx_ops never compare with previous received messages */
884 op->last_frames = NULL;
886 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
887 op->sk = sk;
888 op->ifindex = ifindex;
890 /* initialize uninitialized (kzalloc) structure */
891 hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
892 op->timer.function = bcm_tx_timeout_handler;
894 /* currently unused in tx_ops */
895 hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
897 /* add this bcm_op to the list of the tx_ops */
898 list_add(&op->list, &bo->tx_ops);
900 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
902 if (op->nframes != msg_head->nframes) {
903 op->nframes = msg_head->nframes;
904 /* start multiple frame transmission with index 0 */
905 op->currframe = 0;
908 /* check flags */
910 op->flags = msg_head->flags;
912 if (op->flags & TX_RESET_MULTI_IDX) {
913 /* start multiple frame transmission with index 0 */
914 op->currframe = 0;
917 if (op->flags & SETTIMER) {
918 /* set timer values */
919 op->count = msg_head->count;
920 op->ival1 = msg_head->ival1;
921 op->ival2 = msg_head->ival2;
922 op->kt_ival1 = timeval_to_ktime(msg_head->ival1);
923 op->kt_ival2 = timeval_to_ktime(msg_head->ival2);
925 /* disable an active timer due to zero values? */
926 if (!op->kt_ival1.tv64 && !op->kt_ival2.tv64)
927 hrtimer_cancel(&op->timer);
930 if ((op->flags & STARTTIMER) &&
931 ((op->kt_ival1.tv64 && op->count) || op->kt_ival2.tv64)) {
933 /* spec: send can_frame when starting timer */
934 op->flags |= TX_ANNOUNCE;
936 if (op->kt_ival1.tv64 && (op->count > 0)) {
937 /* op->count-- is done in bcm_tx_timeout_handler */
938 hrtimer_start(&op->timer, op->kt_ival1,
939 HRTIMER_MODE_REL);
940 } else
941 hrtimer_start(&op->timer, op->kt_ival2,
942 HRTIMER_MODE_REL);
945 if (op->flags & TX_ANNOUNCE)
946 bcm_can_tx(op);
948 return msg_head->nframes * CFSIZ + MHSIZ;
952 * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
954 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
955 int ifindex, struct sock *sk)
957 struct bcm_sock *bo = bcm_sk(sk);
958 struct bcm_op *op;
959 int do_rx_register;
960 int err = 0;
962 if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
963 /* be robust against wrong usage ... */
964 msg_head->flags |= RX_FILTER_ID;
965 /* ignore trailing garbage */
966 msg_head->nframes = 0;
969 if ((msg_head->flags & RX_RTR_FRAME) &&
970 ((msg_head->nframes != 1) ||
971 (!(msg_head->can_id & CAN_RTR_FLAG))))
972 return -EINVAL;
974 /* check the given can_id */
975 op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex);
976 if (op) {
977 /* update existing BCM operation */
980 * Do we need more space for the can_frames than currently
981 * allocated? -> This is a _really_ unusual use-case and
982 * therefore (complexity / locking) it is not supported.
984 if (msg_head->nframes > op->nframes)
985 return -E2BIG;
987 if (msg_head->nframes) {
988 /* update can_frames content */
989 err = memcpy_fromiovec((u8 *)op->frames,
990 msg->msg_iov,
991 msg_head->nframes * CFSIZ);
992 if (err < 0)
993 return err;
995 /* clear last_frames to indicate 'nothing received' */
996 memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
999 op->nframes = msg_head->nframes;
1001 /* Only an update -> do not call can_rx_register() */
1002 do_rx_register = 0;
1004 } else {
1005 /* insert new BCM operation for the given can_id */
1006 op = kzalloc(OPSIZ, GFP_KERNEL);
1007 if (!op)
1008 return -ENOMEM;
1010 op->can_id = msg_head->can_id;
1011 op->nframes = msg_head->nframes;
1013 if (msg_head->nframes > 1) {
1014 /* create array for can_frames and copy the data */
1015 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1016 GFP_KERNEL);
1017 if (!op->frames) {
1018 kfree(op);
1019 return -ENOMEM;
1022 /* create and init array for received can_frames */
1023 op->last_frames = kzalloc(msg_head->nframes * CFSIZ,
1024 GFP_KERNEL);
1025 if (!op->last_frames) {
1026 kfree(op->frames);
1027 kfree(op);
1028 return -ENOMEM;
1031 } else {
1032 op->frames = &op->sframe;
1033 op->last_frames = &op->last_sframe;
1036 if (msg_head->nframes) {
1037 err = memcpy_fromiovec((u8 *)op->frames, msg->msg_iov,
1038 msg_head->nframes * CFSIZ);
1039 if (err < 0) {
1040 if (op->frames != &op->sframe)
1041 kfree(op->frames);
1042 if (op->last_frames != &op->last_sframe)
1043 kfree(op->last_frames);
1044 kfree(op);
1045 return err;
1049 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1050 op->sk = sk;
1051 op->ifindex = ifindex;
1053 /* initialize uninitialized (kzalloc) structure */
1054 hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1055 op->timer.function = bcm_rx_timeout_handler;
1057 hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1058 op->thrtimer.function = bcm_rx_thr_handler;
1060 /* add this bcm_op to the list of the rx_ops */
1061 list_add(&op->list, &bo->rx_ops);
1063 /* call can_rx_register() */
1064 do_rx_register = 1;
1066 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
1068 /* check flags */
1069 op->flags = msg_head->flags;
1071 if (op->flags & RX_RTR_FRAME) {
1073 /* no timers in RTR-mode */
1074 hrtimer_cancel(&op->thrtimer);
1075 hrtimer_cancel(&op->timer);
1078 * funny feature in RX(!)_SETUP only for RTR-mode:
1079 * copy can_id into frame BUT without RTR-flag to
1080 * prevent a full-load-loopback-test ... ;-]
1082 if ((op->flags & TX_CP_CAN_ID) ||
1083 (op->frames[0].can_id == op->can_id))
1084 op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
1086 } else {
1087 if (op->flags & SETTIMER) {
1089 /* set timer value */
1090 op->ival1 = msg_head->ival1;
1091 op->ival2 = msg_head->ival2;
1092 op->kt_ival1 = timeval_to_ktime(msg_head->ival1);
1093 op->kt_ival2 = timeval_to_ktime(msg_head->ival2);
1095 /* disable an active timer due to zero value? */
1096 if (!op->kt_ival1.tv64)
1097 hrtimer_cancel(&op->timer);
1100 * In any case cancel the throttle timer, flush
1101 * potentially blocked msgs and reset throttle handling
1103 op->kt_lastmsg = ktime_set(0, 0);
1104 hrtimer_cancel(&op->thrtimer);
1105 bcm_rx_thr_flush(op);
1108 if ((op->flags & STARTTIMER) && op->kt_ival1.tv64)
1109 hrtimer_start(&op->timer, op->kt_ival1,
1110 HRTIMER_MODE_REL);
1113 /* now we can register for can_ids, if we added a new bcm_op */
1114 if (do_rx_register) {
1115 if (ifindex) {
1116 struct net_device *dev;
1118 dev = dev_get_by_index(&init_net, ifindex);
1119 if (dev) {
1120 err = can_rx_register(dev, op->can_id,
1121 REGMASK(op->can_id),
1122 bcm_rx_handler, op,
1123 "bcm");
1125 op->rx_reg_dev = dev;
1126 dev_put(dev);
1129 } else
1130 err = can_rx_register(NULL, op->can_id,
1131 REGMASK(op->can_id),
1132 bcm_rx_handler, op, "bcm");
1133 if (err) {
1134 /* this bcm rx op is broken -> remove it */
1135 list_del(&op->list);
1136 bcm_remove_op(op);
1137 return err;
1141 return msg_head->nframes * CFSIZ + MHSIZ;
1145 * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1147 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
1149 struct sk_buff *skb;
1150 struct net_device *dev;
1151 int err;
1153 /* we need a real device to send frames */
1154 if (!ifindex)
1155 return -ENODEV;
1157 skb = alloc_skb(CFSIZ, GFP_KERNEL);
1159 if (!skb)
1160 return -ENOMEM;
1162 err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ);
1163 if (err < 0) {
1164 kfree_skb(skb);
1165 return err;
1168 dev = dev_get_by_index(&init_net, ifindex);
1169 if (!dev) {
1170 kfree_skb(skb);
1171 return -ENODEV;
1174 skb->dev = dev;
1175 skb->sk = sk;
1176 err = can_send(skb, 1); /* send with loopback */
1177 dev_put(dev);
1179 if (err)
1180 return err;
1182 return CFSIZ + MHSIZ;
1186 * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1188 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
1189 struct msghdr *msg, size_t size)
1191 struct sock *sk = sock->sk;
1192 struct bcm_sock *bo = bcm_sk(sk);
1193 int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
1194 struct bcm_msg_head msg_head;
1195 int ret; /* read bytes or error codes as return value */
1197 if (!bo->bound)
1198 return -ENOTCONN;
1200 /* check for valid message length from userspace */
1201 if (size < MHSIZ || (size - MHSIZ) % CFSIZ)
1202 return -EINVAL;
1204 /* check for alternative ifindex for this bcm_op */
1206 if (!ifindex && msg->msg_name) {
1207 /* no bound device as default => check msg_name */
1208 struct sockaddr_can *addr =
1209 (struct sockaddr_can *)msg->msg_name;
1211 if (addr->can_family != AF_CAN)
1212 return -EINVAL;
1214 /* ifindex from sendto() */
1215 ifindex = addr->can_ifindex;
1217 if (ifindex) {
1218 struct net_device *dev;
1220 dev = dev_get_by_index(&init_net, ifindex);
1221 if (!dev)
1222 return -ENODEV;
1224 if (dev->type != ARPHRD_CAN) {
1225 dev_put(dev);
1226 return -ENODEV;
1229 dev_put(dev);
1233 /* read message head information */
1235 ret = memcpy_fromiovec((u8 *)&msg_head, msg->msg_iov, MHSIZ);
1236 if (ret < 0)
1237 return ret;
1239 lock_sock(sk);
1241 switch (msg_head.opcode) {
1243 case TX_SETUP:
1244 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
1245 break;
1247 case RX_SETUP:
1248 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
1249 break;
1251 case TX_DELETE:
1252 if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
1253 ret = MHSIZ;
1254 else
1255 ret = -EINVAL;
1256 break;
1258 case RX_DELETE:
1259 if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
1260 ret = MHSIZ;
1261 else
1262 ret = -EINVAL;
1263 break;
1265 case TX_READ:
1266 /* reuse msg_head for the reply to TX_READ */
1267 msg_head.opcode = TX_STATUS;
1268 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
1269 break;
1271 case RX_READ:
1272 /* reuse msg_head for the reply to RX_READ */
1273 msg_head.opcode = RX_STATUS;
1274 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
1275 break;
1277 case TX_SEND:
1278 /* we need exactly one can_frame behind the msg head */
1279 if ((msg_head.nframes != 1) || (size != CFSIZ + MHSIZ))
1280 ret = -EINVAL;
1281 else
1282 ret = bcm_tx_send(msg, ifindex, sk);
1283 break;
1285 default:
1286 ret = -EINVAL;
1287 break;
1290 release_sock(sk);
1292 return ret;
1296 * notification handler for netdevice status changes
1298 static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
1299 void *data)
1301 struct net_device *dev = (struct net_device *)data;
1302 struct bcm_sock *bo = container_of(nb, struct bcm_sock, notifier);
1303 struct sock *sk = &bo->sk;
1304 struct bcm_op *op;
1305 int notify_enodev = 0;
1307 if (!net_eq(dev_net(dev), &init_net))
1308 return NOTIFY_DONE;
1310 if (dev->type != ARPHRD_CAN)
1311 return NOTIFY_DONE;
1313 switch (msg) {
1315 case NETDEV_UNREGISTER:
1316 lock_sock(sk);
1318 /* remove device specific receive entries */
1319 list_for_each_entry(op, &bo->rx_ops, list)
1320 if (op->rx_reg_dev == dev)
1321 bcm_rx_unreg(dev, op);
1323 /* remove device reference, if this is our bound device */
1324 if (bo->bound && bo->ifindex == dev->ifindex) {
1325 bo->bound = 0;
1326 bo->ifindex = 0;
1327 notify_enodev = 1;
1330 release_sock(sk);
1332 if (notify_enodev) {
1333 sk->sk_err = ENODEV;
1334 if (!sock_flag(sk, SOCK_DEAD))
1335 sk->sk_error_report(sk);
1337 break;
1339 case NETDEV_DOWN:
1340 if (bo->bound && bo->ifindex == dev->ifindex) {
1341 sk->sk_err = ENETDOWN;
1342 if (!sock_flag(sk, SOCK_DEAD))
1343 sk->sk_error_report(sk);
1347 return NOTIFY_DONE;
1351 * initial settings for all BCM sockets to be set at socket creation time
1353 static int bcm_init(struct sock *sk)
1355 struct bcm_sock *bo = bcm_sk(sk);
1357 bo->bound = 0;
1358 bo->ifindex = 0;
1359 bo->dropped_usr_msgs = 0;
1360 bo->bcm_proc_read = NULL;
1362 INIT_LIST_HEAD(&bo->tx_ops);
1363 INIT_LIST_HEAD(&bo->rx_ops);
1365 /* set notifier */
1366 bo->notifier.notifier_call = bcm_notifier;
1368 register_netdevice_notifier(&bo->notifier);
1370 return 0;
1374 * standard socket functions
1376 static int bcm_release(struct socket *sock)
1378 struct sock *sk = sock->sk;
1379 struct bcm_sock *bo = bcm_sk(sk);
1380 struct bcm_op *op, *next;
1382 /* remove bcm_ops, timer, rx_unregister(), etc. */
1384 unregister_netdevice_notifier(&bo->notifier);
1386 lock_sock(sk);
1388 list_for_each_entry_safe(op, next, &bo->tx_ops, list)
1389 bcm_remove_op(op);
1391 list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
1393 * Don't care if we're bound or not (due to netdev problems)
1394 * can_rx_unregister() is always a save thing to do here.
1396 if (op->ifindex) {
1398 * Only remove subscriptions that had not
1399 * been removed due to NETDEV_UNREGISTER
1400 * in bcm_notifier()
1402 if (op->rx_reg_dev) {
1403 struct net_device *dev;
1405 dev = dev_get_by_index(&init_net, op->ifindex);
1406 if (dev) {
1407 bcm_rx_unreg(dev, op);
1408 dev_put(dev);
1411 } else
1412 can_rx_unregister(NULL, op->can_id,
1413 REGMASK(op->can_id),
1414 bcm_rx_handler, op);
1416 bcm_remove_op(op);
1419 /* remove procfs entry */
1420 if (proc_dir && bo->bcm_proc_read)
1421 remove_proc_entry(bo->procname, proc_dir);
1423 /* remove device reference */
1424 if (bo->bound) {
1425 bo->bound = 0;
1426 bo->ifindex = 0;
1429 release_sock(sk);
1430 sock_put(sk);
1432 return 0;
1435 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1436 int flags)
1438 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1439 struct sock *sk = sock->sk;
1440 struct bcm_sock *bo = bcm_sk(sk);
1442 if (bo->bound)
1443 return -EISCONN;
1445 /* bind a device to this socket */
1446 if (addr->can_ifindex) {
1447 struct net_device *dev;
1449 dev = dev_get_by_index(&init_net, addr->can_ifindex);
1450 if (!dev)
1451 return -ENODEV;
1453 if (dev->type != ARPHRD_CAN) {
1454 dev_put(dev);
1455 return -ENODEV;
1458 bo->ifindex = dev->ifindex;
1459 dev_put(dev);
1461 } else {
1462 /* no interface reference for ifindex = 0 ('any' CAN device) */
1463 bo->ifindex = 0;
1466 bo->bound = 1;
1468 if (proc_dir) {
1469 /* unique socket address as filename */
1470 sprintf(bo->procname, "%p", sock);
1471 bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
1472 proc_dir,
1473 bcm_read_proc, sk);
1476 return 0;
1479 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
1480 struct msghdr *msg, size_t size, int flags)
1482 struct sock *sk = sock->sk;
1483 struct sk_buff *skb;
1484 int error = 0;
1485 int noblock;
1486 int err;
1488 noblock = flags & MSG_DONTWAIT;
1489 flags &= ~MSG_DONTWAIT;
1490 skb = skb_recv_datagram(sk, flags, noblock, &error);
1491 if (!skb)
1492 return error;
1494 if (skb->len < size)
1495 size = skb->len;
1497 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
1498 if (err < 0) {
1499 skb_free_datagram(sk, skb);
1500 return err;
1503 sock_recv_timestamp(msg, sk, skb);
1505 if (msg->msg_name) {
1506 msg->msg_namelen = sizeof(struct sockaddr_can);
1507 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1510 skb_free_datagram(sk, skb);
1512 return size;
1515 static struct proto_ops bcm_ops __read_mostly = {
1516 .family = PF_CAN,
1517 .release = bcm_release,
1518 .bind = sock_no_bind,
1519 .connect = bcm_connect,
1520 .socketpair = sock_no_socketpair,
1521 .accept = sock_no_accept,
1522 .getname = sock_no_getname,
1523 .poll = datagram_poll,
1524 .ioctl = NULL, /* use can_ioctl() from af_can.c */
1525 .listen = sock_no_listen,
1526 .shutdown = sock_no_shutdown,
1527 .setsockopt = sock_no_setsockopt,
1528 .getsockopt = sock_no_getsockopt,
1529 .sendmsg = bcm_sendmsg,
1530 .recvmsg = bcm_recvmsg,
1531 .mmap = sock_no_mmap,
1532 .sendpage = sock_no_sendpage,
1535 static struct proto bcm_proto __read_mostly = {
1536 .name = "CAN_BCM",
1537 .owner = THIS_MODULE,
1538 .obj_size = sizeof(struct bcm_sock),
1539 .init = bcm_init,
1542 static struct can_proto bcm_can_proto __read_mostly = {
1543 .type = SOCK_DGRAM,
1544 .protocol = CAN_BCM,
1545 .capability = -1,
1546 .ops = &bcm_ops,
1547 .prot = &bcm_proto,
1550 static int __init bcm_module_init(void)
1552 int err;
1554 printk(banner);
1556 err = can_proto_register(&bcm_can_proto);
1557 if (err < 0) {
1558 printk(KERN_ERR "can: registration of bcm protocol failed\n");
1559 return err;
1562 /* create /proc/net/can-bcm directory */
1563 proc_dir = proc_mkdir("can-bcm", init_net.proc_net);
1565 if (proc_dir)
1566 proc_dir->owner = THIS_MODULE;
1568 return 0;
1571 static void __exit bcm_module_exit(void)
1573 can_proto_unregister(&bcm_can_proto);
1575 if (proc_dir)
1576 proc_net_remove(&init_net, "can-bcm");
1579 module_init(bcm_module_init);
1580 module_exit(bcm_module_exit);