dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / net / can / bcm.c
blob4ccfd356baedef930a15551ccc0c503a5e1e372b
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.
42 #include <linux/module.h>
43 #include <linux/init.h>
44 #include <linux/interrupt.h>
45 #include <linux/hrtimer.h>
46 #include <linux/list.h>
47 #include <linux/proc_fs.h>
48 #include <linux/seq_file.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/skb.h>
58 #include <linux/can/bcm.h>
59 #include <linux/slab.h>
60 #include <net/sock.h>
61 #include <net/net_namespace.h>
64 * To send multiple CAN frame content within TX_SETUP or to filter
65 * CAN messages with multiplex index within RX_SETUP, the number of
66 * different filters is limited to 256 due to the one byte index value.
68 #define MAX_NFRAMES 256
70 /* use of last_frames[index].can_dlc */
71 #define RX_RECV 0x40 /* received data for this element */
72 #define RX_THR 0x80 /* element not been sent due to throttle feature */
73 #define BCM_CAN_DLC_MASK 0x0F /* clean private flags in can_dlc by masking */
75 /* get best masking value for can_rx_register() for a given single can_id */
76 #define REGMASK(id) ((id & CAN_EFF_FLAG) ? \
77 (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG) : \
78 (CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG))
80 #define CAN_BCM_VERSION CAN_VERSION
82 MODULE_DESCRIPTION("PF_CAN broadcast manager protocol");
83 MODULE_LICENSE("Dual BSD/GPL");
84 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
85 MODULE_ALIAS("can-proto-2");
87 /* easy access to can_frame payload */
88 static inline u64 GET_U64(const struct can_frame *cp)
90 return *(u64 *)cp->data;
93 struct bcm_op {
94 struct list_head list;
95 int ifindex;
96 canid_t can_id;
97 u32 flags;
98 unsigned long frames_abs, frames_filtered;
99 struct bcm_timeval ival1, ival2;
100 struct hrtimer timer, thrtimer;
101 struct tasklet_struct tsklet, thrtsklet;
102 ktime_t rx_stamp, kt_ival1, kt_ival2, kt_lastmsg;
103 int rx_ifindex;
104 u32 count;
105 u32 nframes;
106 u32 currframe;
107 struct can_frame *frames;
108 struct can_frame *last_frames;
109 struct can_frame sframe;
110 struct can_frame last_sframe;
111 struct sock *sk;
112 struct net_device *rx_reg_dev;
115 static struct proc_dir_entry *proc_dir;
117 struct bcm_sock {
118 struct sock sk;
119 int bound;
120 int ifindex;
121 struct notifier_block notifier;
122 struct list_head rx_ops;
123 struct list_head tx_ops;
124 unsigned long dropped_usr_msgs;
125 struct proc_dir_entry *bcm_proc_read;
126 char procname [32]; /* inode number in decimal with \0 */
129 static inline struct bcm_sock *bcm_sk(const struct sock *sk)
131 return (struct bcm_sock *)sk;
134 static inline ktime_t bcm_timeval_to_ktime(struct bcm_timeval tv)
136 return ktime_set(tv.tv_sec, tv.tv_usec * NSEC_PER_USEC);
139 #define CFSIZ sizeof(struct can_frame)
140 #define OPSIZ sizeof(struct bcm_op)
141 #define MHSIZ sizeof(struct bcm_msg_head)
144 * procfs functions
146 static char *bcm_proc_getifname(char *result, int ifindex)
148 struct net_device *dev;
150 if (!ifindex)
151 return "any";
153 rcu_read_lock();
154 dev = dev_get_by_index_rcu(&init_net, ifindex);
155 if (dev)
156 strcpy(result, dev->name);
157 else
158 strcpy(result, "???");
159 rcu_read_unlock();
161 return result;
164 static int bcm_proc_show(struct seq_file *m, void *v)
166 char ifname[IFNAMSIZ];
167 struct sock *sk = (struct sock *)m->private;
168 struct bcm_sock *bo = bcm_sk(sk);
169 struct bcm_op *op;
171 seq_printf(m, ">>> socket %pK", sk->sk_socket);
172 seq_printf(m, " / sk %pK", sk);
173 seq_printf(m, " / bo %pK", bo);
174 seq_printf(m, " / dropped %lu", bo->dropped_usr_msgs);
175 seq_printf(m, " / bound %s", bcm_proc_getifname(ifname, bo->ifindex));
176 seq_printf(m, " <<<\n");
178 list_for_each_entry(op, &bo->rx_ops, list) {
180 unsigned long reduction;
182 /* print only active entries & prevent division by zero */
183 if (!op->frames_abs)
184 continue;
186 seq_printf(m, "rx_op: %03X %-5s ",
187 op->can_id, bcm_proc_getifname(ifname, op->ifindex));
188 seq_printf(m, "[%u]%c ", op->nframes,
189 (op->flags & RX_CHECK_DLC)?'d':' ');
190 if (op->kt_ival1.tv64)
191 seq_printf(m, "timeo=%lld ",
192 (long long)
193 ktime_to_us(op->kt_ival1));
195 if (op->kt_ival2.tv64)
196 seq_printf(m, "thr=%lld ",
197 (long long)
198 ktime_to_us(op->kt_ival2));
200 seq_printf(m, "# recv %ld (%ld) => reduction: ",
201 op->frames_filtered, op->frames_abs);
203 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
205 seq_printf(m, "%s%ld%%\n",
206 (reduction == 100)?"near ":"", reduction);
209 list_for_each_entry(op, &bo->tx_ops, list) {
211 seq_printf(m, "tx_op: %03X %s [%u] ",
212 op->can_id,
213 bcm_proc_getifname(ifname, op->ifindex),
214 op->nframes);
216 if (op->kt_ival1.tv64)
217 seq_printf(m, "t1=%lld ",
218 (long long) ktime_to_us(op->kt_ival1));
220 if (op->kt_ival2.tv64)
221 seq_printf(m, "t2=%lld ",
222 (long long) ktime_to_us(op->kt_ival2));
224 seq_printf(m, "# sent %ld\n", op->frames_abs);
226 seq_putc(m, '\n');
227 return 0;
230 static int bcm_proc_open(struct inode *inode, struct file *file)
232 return single_open(file, bcm_proc_show, PDE_DATA(inode));
235 static const struct file_operations bcm_proc_fops = {
236 .owner = THIS_MODULE,
237 .open = bcm_proc_open,
238 .read = seq_read,
239 .llseek = seq_lseek,
240 .release = single_release,
244 * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
245 * of the given bcm tx op
247 static void bcm_can_tx(struct bcm_op *op)
249 struct sk_buff *skb;
250 struct net_device *dev;
251 struct can_frame *cf = &op->frames[op->currframe];
253 /* no target device? => exit */
254 if (!op->ifindex)
255 return;
257 dev = dev_get_by_index(&init_net, op->ifindex);
258 if (!dev) {
259 /* RFC: should this bcm_op remove itself here? */
260 return;
263 skb = alloc_skb(CFSIZ + sizeof(struct can_skb_priv), gfp_any());
264 if (!skb)
265 goto out;
267 can_skb_reserve(skb);
268 can_skb_prv(skb)->ifindex = dev->ifindex;
269 can_skb_prv(skb)->skbcnt = 0;
271 memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
273 /* send with loopback */
274 skb->dev = dev;
275 can_skb_set_owner(skb, op->sk);
276 can_send(skb, 1);
278 /* update statistics */
279 op->currframe++;
280 op->frames_abs++;
282 /* reached last frame? */
283 if (op->currframe >= op->nframes)
284 op->currframe = 0;
285 out:
286 dev_put(dev);
290 * bcm_send_to_user - send a BCM message to the userspace
291 * (consisting of bcm_msg_head + x CAN frames)
293 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
294 struct can_frame *frames, int has_timestamp)
296 struct sk_buff *skb;
297 struct can_frame *firstframe;
298 struct sockaddr_can *addr;
299 struct sock *sk = op->sk;
300 unsigned int datalen = head->nframes * CFSIZ;
301 int err;
303 skb = alloc_skb(sizeof(*head) + datalen, gfp_any());
304 if (!skb)
305 return;
307 memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
309 if (head->nframes) {
310 /* can_frames starting here */
311 firstframe = (struct can_frame *)skb_tail_pointer(skb);
313 memcpy(skb_put(skb, datalen), frames, datalen);
316 * the BCM uses the can_dlc-element of the can_frame
317 * structure for internal purposes. This is only
318 * relevant for updates that are generated by the
319 * BCM, where nframes is 1
321 if (head->nframes == 1)
322 firstframe->can_dlc &= BCM_CAN_DLC_MASK;
325 if (has_timestamp) {
326 /* restore rx timestamp */
327 skb->tstamp = op->rx_stamp;
331 * Put the datagram to the queue so that bcm_recvmsg() can
332 * get it from there. We need to pass the interface index to
333 * bcm_recvmsg(). We pass a whole struct sockaddr_can in skb->cb
334 * containing the interface index.
337 sock_skb_cb_check_size(sizeof(struct sockaddr_can));
338 addr = (struct sockaddr_can *)skb->cb;
339 memset(addr, 0, sizeof(*addr));
340 addr->can_family = AF_CAN;
341 addr->can_ifindex = op->rx_ifindex;
343 err = sock_queue_rcv_skb(sk, skb);
344 if (err < 0) {
345 struct bcm_sock *bo = bcm_sk(sk);
347 kfree_skb(skb);
348 /* don't care about overflows in this statistic */
349 bo->dropped_usr_msgs++;
353 static void bcm_tx_start_timer(struct bcm_op *op)
355 if (op->kt_ival1.tv64 && op->count)
356 hrtimer_start(&op->timer,
357 ktime_add(ktime_get(), op->kt_ival1),
358 HRTIMER_MODE_ABS);
359 else if (op->kt_ival2.tv64)
360 hrtimer_start(&op->timer,
361 ktime_add(ktime_get(), op->kt_ival2),
362 HRTIMER_MODE_ABS);
365 static void bcm_tx_timeout_tsklet(unsigned long data)
367 struct bcm_op *op = (struct bcm_op *)data;
368 struct bcm_msg_head msg_head;
370 if (op->kt_ival1.tv64 && (op->count > 0)) {
372 op->count--;
373 if (!op->count && (op->flags & TX_COUNTEVT)) {
375 /* create notification to user */
376 msg_head.opcode = TX_EXPIRED;
377 msg_head.flags = op->flags;
378 msg_head.count = op->count;
379 msg_head.ival1 = op->ival1;
380 msg_head.ival2 = op->ival2;
381 msg_head.can_id = op->can_id;
382 msg_head.nframes = 0;
384 bcm_send_to_user(op, &msg_head, NULL, 0);
386 bcm_can_tx(op);
388 } else if (op->kt_ival2.tv64)
389 bcm_can_tx(op);
391 bcm_tx_start_timer(op);
395 * bcm_tx_timeout_handler - performs cyclic CAN frame transmissions
397 static enum hrtimer_restart bcm_tx_timeout_handler(struct hrtimer *hrtimer)
399 struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
401 tasklet_schedule(&op->tsklet);
403 return HRTIMER_NORESTART;
407 * bcm_rx_changed - create a RX_CHANGED notification due to changed content
409 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
411 struct bcm_msg_head head;
413 /* update statistics */
414 op->frames_filtered++;
416 /* prevent statistics overflow */
417 if (op->frames_filtered > ULONG_MAX/100)
418 op->frames_filtered = op->frames_abs = 0;
420 /* this element is not throttled anymore */
421 data->can_dlc &= (BCM_CAN_DLC_MASK|RX_RECV);
423 head.opcode = RX_CHANGED;
424 head.flags = op->flags;
425 head.count = op->count;
426 head.ival1 = op->ival1;
427 head.ival2 = op->ival2;
428 head.can_id = op->can_id;
429 head.nframes = 1;
431 bcm_send_to_user(op, &head, data, 1);
435 * bcm_rx_update_and_send - process a detected relevant receive content change
436 * 1. update the last received data
437 * 2. send a notification to the user (if possible)
439 static void bcm_rx_update_and_send(struct bcm_op *op,
440 struct can_frame *lastdata,
441 const struct can_frame *rxdata)
443 memcpy(lastdata, rxdata, CFSIZ);
445 /* mark as used and throttled by default */
446 lastdata->can_dlc |= (RX_RECV|RX_THR);
448 /* throttling mode inactive ? */
449 if (!op->kt_ival2.tv64) {
450 /* send RX_CHANGED to the user immediately */
451 bcm_rx_changed(op, lastdata);
452 return;
455 /* with active throttling timer we are just done here */
456 if (hrtimer_active(&op->thrtimer))
457 return;
459 /* first reception with enabled throttling mode */
460 if (!op->kt_lastmsg.tv64)
461 goto rx_changed_settime;
463 /* got a second frame inside a potential throttle period? */
464 if (ktime_us_delta(ktime_get(), op->kt_lastmsg) <
465 ktime_to_us(op->kt_ival2)) {
466 /* do not send the saved data - only start throttle timer */
467 hrtimer_start(&op->thrtimer,
468 ktime_add(op->kt_lastmsg, op->kt_ival2),
469 HRTIMER_MODE_ABS);
470 return;
473 /* the gap was that big, that throttling was not needed here */
474 rx_changed_settime:
475 bcm_rx_changed(op, lastdata);
476 op->kt_lastmsg = ktime_get();
480 * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly
481 * received data stored in op->last_frames[]
483 static void bcm_rx_cmp_to_index(struct bcm_op *op, unsigned int index,
484 const struct can_frame *rxdata)
487 * no one uses the MSBs of can_dlc for comparison,
488 * so we use it here to detect the first time of reception
491 if (!(op->last_frames[index].can_dlc & RX_RECV)) {
492 /* received data for the first time => send update to user */
493 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
494 return;
497 /* do a real check in can_frame data section */
499 if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) !=
500 (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) {
501 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
502 return;
505 if (op->flags & RX_CHECK_DLC) {
506 /* do a real check in can_frame dlc */
507 if (rxdata->can_dlc != (op->last_frames[index].can_dlc &
508 BCM_CAN_DLC_MASK)) {
509 bcm_rx_update_and_send(op, &op->last_frames[index],
510 rxdata);
511 return;
517 * bcm_rx_starttimer - enable timeout monitoring for CAN frame reception
519 static void bcm_rx_starttimer(struct bcm_op *op)
521 if (op->flags & RX_NO_AUTOTIMER)
522 return;
524 if (op->kt_ival1.tv64)
525 hrtimer_start(&op->timer, op->kt_ival1, HRTIMER_MODE_REL);
528 static void bcm_rx_timeout_tsklet(unsigned long data)
530 struct bcm_op *op = (struct bcm_op *)data;
531 struct bcm_msg_head msg_head;
533 /* create notification to user */
534 msg_head.opcode = RX_TIMEOUT;
535 msg_head.flags = op->flags;
536 msg_head.count = op->count;
537 msg_head.ival1 = op->ival1;
538 msg_head.ival2 = op->ival2;
539 msg_head.can_id = op->can_id;
540 msg_head.nframes = 0;
542 bcm_send_to_user(op, &msg_head, NULL, 0);
546 * bcm_rx_timeout_handler - when the (cyclic) CAN frame reception timed out
548 static enum hrtimer_restart bcm_rx_timeout_handler(struct hrtimer *hrtimer)
550 struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
552 /* schedule before NET_RX_SOFTIRQ */
553 tasklet_hi_schedule(&op->tsklet);
555 /* no restart of the timer is done here! */
557 /* if user wants to be informed, when cyclic CAN-Messages come back */
558 if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
559 /* clear received can_frames to indicate 'nothing received' */
560 memset(op->last_frames, 0, op->nframes * CFSIZ);
563 return HRTIMER_NORESTART;
567 * bcm_rx_do_flush - helper for bcm_rx_thr_flush
569 static inline int bcm_rx_do_flush(struct bcm_op *op, int update,
570 unsigned int index)
572 if ((op->last_frames) && (op->last_frames[index].can_dlc & RX_THR)) {
573 if (update)
574 bcm_rx_changed(op, &op->last_frames[index]);
575 return 1;
577 return 0;
581 * bcm_rx_thr_flush - Check for throttled data and send it to the userspace
583 * update == 0 : just check if throttled data is available (any irq context)
584 * update == 1 : check and send throttled data to userspace (soft_irq context)
586 static int bcm_rx_thr_flush(struct bcm_op *op, int update)
588 int updated = 0;
590 if (op->nframes > 1) {
591 unsigned int i;
593 /* for MUX filter we start at index 1 */
594 for (i = 1; i < op->nframes; i++)
595 updated += bcm_rx_do_flush(op, update, i);
597 } else {
598 /* for RX_FILTER_ID and simple filter */
599 updated += bcm_rx_do_flush(op, update, 0);
602 return updated;
605 static void bcm_rx_thr_tsklet(unsigned long data)
607 struct bcm_op *op = (struct bcm_op *)data;
609 /* push the changed data to the userspace */
610 bcm_rx_thr_flush(op, 1);
614 * bcm_rx_thr_handler - the time for blocked content updates is over now:
615 * Check for throttled data and send it to the userspace
617 static enum hrtimer_restart bcm_rx_thr_handler(struct hrtimer *hrtimer)
619 struct bcm_op *op = container_of(hrtimer, struct bcm_op, thrtimer);
621 tasklet_schedule(&op->thrtsklet);
623 if (bcm_rx_thr_flush(op, 0)) {
624 hrtimer_forward(hrtimer, ktime_get(), op->kt_ival2);
625 return HRTIMER_RESTART;
626 } else {
627 /* rearm throttle handling */
628 op->kt_lastmsg = ktime_set(0, 0);
629 return HRTIMER_NORESTART;
634 * bcm_rx_handler - handle a CAN frame reception
636 static void bcm_rx_handler(struct sk_buff *skb, void *data)
638 struct bcm_op *op = (struct bcm_op *)data;
639 const struct can_frame *rxframe = (struct can_frame *)skb->data;
640 unsigned int i;
642 /* disable timeout */
643 hrtimer_cancel(&op->timer);
645 if (op->can_id != rxframe->can_id)
646 return;
648 /* save rx timestamp */
649 op->rx_stamp = skb->tstamp;
650 /* save originator for recvfrom() */
651 op->rx_ifindex = skb->dev->ifindex;
652 /* update statistics */
653 op->frames_abs++;
655 if (op->flags & RX_RTR_FRAME) {
656 /* send reply for RTR-request (placed in op->frames[0]) */
657 bcm_can_tx(op);
658 return;
661 if (op->flags & RX_FILTER_ID) {
662 /* the easiest case */
663 bcm_rx_update_and_send(op, &op->last_frames[0], rxframe);
664 goto rx_starttimer;
667 if (op->nframes == 1) {
668 /* simple compare with index 0 */
669 bcm_rx_cmp_to_index(op, 0, rxframe);
670 goto rx_starttimer;
673 if (op->nframes > 1) {
675 * multiplex compare
677 * find the first multiplex mask that fits.
678 * Remark: The MUX-mask is stored in index 0
681 for (i = 1; i < op->nframes; i++) {
682 if ((GET_U64(&op->frames[0]) & GET_U64(rxframe)) ==
683 (GET_U64(&op->frames[0]) &
684 GET_U64(&op->frames[i]))) {
685 bcm_rx_cmp_to_index(op, i, rxframe);
686 break;
691 rx_starttimer:
692 bcm_rx_starttimer(op);
696 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
698 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
699 int ifindex)
701 struct bcm_op *op;
703 list_for_each_entry(op, ops, list) {
704 if ((op->can_id == can_id) && (op->ifindex == ifindex))
705 return op;
708 return NULL;
711 static void bcm_remove_op(struct bcm_op *op)
713 if (op->tsklet.func) {
714 while (test_bit(TASKLET_STATE_SCHED, &op->tsklet.state) ||
715 test_bit(TASKLET_STATE_RUN, &op->tsklet.state) ||
716 hrtimer_active(&op->timer)) {
717 hrtimer_cancel(&op->timer);
718 tasklet_kill(&op->tsklet);
722 if (op->thrtsklet.func) {
723 while (test_bit(TASKLET_STATE_SCHED, &op->thrtsklet.state) ||
724 test_bit(TASKLET_STATE_RUN, &op->thrtsklet.state) ||
725 hrtimer_active(&op->thrtimer)) {
726 hrtimer_cancel(&op->thrtimer);
727 tasklet_kill(&op->thrtsklet);
731 if ((op->frames) && (op->frames != &op->sframe))
732 kfree(op->frames);
734 if ((op->last_frames) && (op->last_frames != &op->last_sframe))
735 kfree(op->last_frames);
737 kfree(op);
740 static void bcm_rx_unreg(struct net_device *dev, struct bcm_op *op)
742 if (op->rx_reg_dev == dev) {
743 can_rx_unregister(dev, op->can_id, REGMASK(op->can_id),
744 bcm_rx_handler, op);
746 /* mark as removed subscription */
747 op->rx_reg_dev = NULL;
748 } else
749 printk(KERN_ERR "can-bcm: bcm_rx_unreg: registered device "
750 "mismatch %p %p\n", op->rx_reg_dev, dev);
754 * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
756 static int bcm_delete_rx_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)) {
764 * Don't care if we're bound or not (due to netdev
765 * problems) can_rx_unregister() is always a save
766 * thing to do here.
768 if (op->ifindex) {
770 * Only remove subscriptions that had not
771 * been removed due to NETDEV_UNREGISTER
772 * in bcm_notifier()
774 if (op->rx_reg_dev) {
775 struct net_device *dev;
777 dev = dev_get_by_index(&init_net,
778 op->ifindex);
779 if (dev) {
780 bcm_rx_unreg(dev, op);
781 dev_put(dev);
784 } else
785 can_rx_unregister(NULL, op->can_id,
786 REGMASK(op->can_id),
787 bcm_rx_handler, op);
789 list_del(&op->list);
790 bcm_remove_op(op);
791 return 1; /* done */
795 return 0; /* not found */
799 * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
801 static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
803 struct bcm_op *op, *n;
805 list_for_each_entry_safe(op, n, ops, list) {
806 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
807 list_del(&op->list);
808 bcm_remove_op(op);
809 return 1; /* done */
813 return 0; /* not found */
817 * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg)
819 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
820 int ifindex)
822 struct bcm_op *op = bcm_find_op(ops, msg_head->can_id, ifindex);
824 if (!op)
825 return -EINVAL;
827 /* put current values into msg_head */
828 msg_head->flags = op->flags;
829 msg_head->count = op->count;
830 msg_head->ival1 = op->ival1;
831 msg_head->ival2 = op->ival2;
832 msg_head->nframes = op->nframes;
834 bcm_send_to_user(op, msg_head, op->frames, 0);
836 return MHSIZ;
840 * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg)
842 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
843 int ifindex, struct sock *sk)
845 struct bcm_sock *bo = bcm_sk(sk);
846 struct bcm_op *op;
847 unsigned int i;
848 int err;
850 /* we need a real device to send frames */
851 if (!ifindex)
852 return -ENODEV;
854 /* check nframes boundaries - we need at least one can_frame */
855 if (msg_head->nframes < 1 || msg_head->nframes > MAX_NFRAMES)
856 return -EINVAL;
858 /* check the given can_id */
859 op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex);
861 if (op) {
862 /* update existing BCM operation */
865 * Do we need more space for the can_frames than currently
866 * allocated? -> This is a _really_ unusual use-case and
867 * therefore (complexity / locking) it is not supported.
869 if (msg_head->nframes > op->nframes)
870 return -E2BIG;
872 /* update can_frames content */
873 for (i = 0; i < msg_head->nframes; i++) {
874 err = memcpy_from_msg((u8 *)&op->frames[i], msg, CFSIZ);
876 if (op->frames[i].can_dlc > 8)
877 err = -EINVAL;
879 if (err < 0)
880 return err;
882 if (msg_head->flags & TX_CP_CAN_ID) {
883 /* copy can_id into frame */
884 op->frames[i].can_id = msg_head->can_id;
888 } else {
889 /* insert new BCM operation for the given can_id */
891 op = kzalloc(OPSIZ, GFP_KERNEL);
892 if (!op)
893 return -ENOMEM;
895 op->can_id = msg_head->can_id;
897 /* create array for can_frames and copy the data */
898 if (msg_head->nframes > 1) {
899 op->frames = kmalloc(msg_head->nframes * CFSIZ,
900 GFP_KERNEL);
901 if (!op->frames) {
902 kfree(op);
903 return -ENOMEM;
905 } else
906 op->frames = &op->sframe;
908 for (i = 0; i < msg_head->nframes; i++) {
909 err = memcpy_from_msg((u8 *)&op->frames[i], msg, CFSIZ);
911 if (op->frames[i].can_dlc > 8)
912 err = -EINVAL;
914 if (err < 0) {
915 if (op->frames != &op->sframe)
916 kfree(op->frames);
917 kfree(op);
918 return err;
921 if (msg_head->flags & TX_CP_CAN_ID) {
922 /* copy can_id into frame */
923 op->frames[i].can_id = msg_head->can_id;
927 /* tx_ops never compare with previous received messages */
928 op->last_frames = NULL;
930 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
931 op->sk = sk;
932 op->ifindex = ifindex;
934 /* initialize uninitialized (kzalloc) structure */
935 hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
936 op->timer.function = bcm_tx_timeout_handler;
938 /* initialize tasklet for tx countevent notification */
939 tasklet_init(&op->tsklet, bcm_tx_timeout_tsklet,
940 (unsigned long) op);
942 /* currently unused in tx_ops */
943 hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
945 /* add this bcm_op to the list of the tx_ops */
946 list_add(&op->list, &bo->tx_ops);
948 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
950 if (op->nframes != msg_head->nframes) {
951 op->nframes = msg_head->nframes;
952 /* start multiple frame transmission with index 0 */
953 op->currframe = 0;
956 /* check flags */
958 op->flags = msg_head->flags;
960 if (op->flags & TX_RESET_MULTI_IDX) {
961 /* start multiple frame transmission with index 0 */
962 op->currframe = 0;
965 if (op->flags & SETTIMER) {
966 /* set timer values */
967 op->count = msg_head->count;
968 op->ival1 = msg_head->ival1;
969 op->ival2 = msg_head->ival2;
970 op->kt_ival1 = bcm_timeval_to_ktime(msg_head->ival1);
971 op->kt_ival2 = bcm_timeval_to_ktime(msg_head->ival2);
973 /* disable an active timer due to zero values? */
974 if (!op->kt_ival1.tv64 && !op->kt_ival2.tv64)
975 hrtimer_cancel(&op->timer);
978 if (op->flags & STARTTIMER) {
979 hrtimer_cancel(&op->timer);
980 /* spec: send can_frame when starting timer */
981 op->flags |= TX_ANNOUNCE;
984 if (op->flags & TX_ANNOUNCE) {
985 bcm_can_tx(op);
986 if (op->count)
987 op->count--;
990 if (op->flags & STARTTIMER)
991 bcm_tx_start_timer(op);
993 return msg_head->nframes * CFSIZ + MHSIZ;
997 * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
999 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
1000 int ifindex, struct sock *sk)
1002 struct bcm_sock *bo = bcm_sk(sk);
1003 struct bcm_op *op;
1004 int do_rx_register;
1005 int err = 0;
1007 if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
1008 /* be robust against wrong usage ... */
1009 msg_head->flags |= RX_FILTER_ID;
1010 /* ignore trailing garbage */
1011 msg_head->nframes = 0;
1014 /* the first element contains the mux-mask => MAX_NFRAMES + 1 */
1015 if (msg_head->nframes > MAX_NFRAMES + 1)
1016 return -EINVAL;
1018 if ((msg_head->flags & RX_RTR_FRAME) &&
1019 ((msg_head->nframes != 1) ||
1020 (!(msg_head->can_id & CAN_RTR_FLAG))))
1021 return -EINVAL;
1023 /* check the given can_id */
1024 op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex);
1025 if (op) {
1026 /* update existing BCM operation */
1029 * Do we need more space for the can_frames than currently
1030 * allocated? -> This is a _really_ unusual use-case and
1031 * therefore (complexity / locking) it is not supported.
1033 if (msg_head->nframes > op->nframes)
1034 return -E2BIG;
1036 if (msg_head->nframes) {
1037 /* update can_frames content */
1038 err = memcpy_from_msg((u8 *)op->frames, msg,
1039 msg_head->nframes * CFSIZ);
1040 if (err < 0)
1041 return err;
1043 /* clear last_frames to indicate 'nothing received' */
1044 memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
1047 op->nframes = msg_head->nframes;
1049 /* Only an update -> do not call can_rx_register() */
1050 do_rx_register = 0;
1052 } else {
1053 /* insert new BCM operation for the given can_id */
1054 op = kzalloc(OPSIZ, GFP_KERNEL);
1055 if (!op)
1056 return -ENOMEM;
1058 op->can_id = msg_head->can_id;
1059 op->nframes = msg_head->nframes;
1061 if (msg_head->nframes > 1) {
1062 /* create array for can_frames and copy the data */
1063 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1064 GFP_KERNEL);
1065 if (!op->frames) {
1066 kfree(op);
1067 return -ENOMEM;
1070 /* create and init array for received can_frames */
1071 op->last_frames = kzalloc(msg_head->nframes * CFSIZ,
1072 GFP_KERNEL);
1073 if (!op->last_frames) {
1074 kfree(op->frames);
1075 kfree(op);
1076 return -ENOMEM;
1079 } else {
1080 op->frames = &op->sframe;
1081 op->last_frames = &op->last_sframe;
1084 if (msg_head->nframes) {
1085 err = memcpy_from_msg((u8 *)op->frames, msg,
1086 msg_head->nframes * CFSIZ);
1087 if (err < 0) {
1088 if (op->frames != &op->sframe)
1089 kfree(op->frames);
1090 if (op->last_frames != &op->last_sframe)
1091 kfree(op->last_frames);
1092 kfree(op);
1093 return err;
1097 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1098 op->sk = sk;
1099 op->ifindex = ifindex;
1101 /* ifindex for timeout events w/o previous frame reception */
1102 op->rx_ifindex = ifindex;
1104 /* initialize uninitialized (kzalloc) structure */
1105 hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1106 op->timer.function = bcm_rx_timeout_handler;
1108 /* initialize tasklet for rx timeout notification */
1109 tasklet_init(&op->tsklet, bcm_rx_timeout_tsklet,
1110 (unsigned long) op);
1112 hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1113 op->thrtimer.function = bcm_rx_thr_handler;
1115 /* initialize tasklet for rx throttle handling */
1116 tasklet_init(&op->thrtsklet, bcm_rx_thr_tsklet,
1117 (unsigned long) op);
1119 /* add this bcm_op to the list of the rx_ops */
1120 list_add(&op->list, &bo->rx_ops);
1122 /* call can_rx_register() */
1123 do_rx_register = 1;
1125 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
1127 /* check flags */
1128 op->flags = msg_head->flags;
1130 if (op->flags & RX_RTR_FRAME) {
1132 /* no timers in RTR-mode */
1133 hrtimer_cancel(&op->thrtimer);
1134 hrtimer_cancel(&op->timer);
1137 * funny feature in RX(!)_SETUP only for RTR-mode:
1138 * copy can_id into frame BUT without RTR-flag to
1139 * prevent a full-load-loopback-test ... ;-]
1141 if ((op->flags & TX_CP_CAN_ID) ||
1142 (op->frames[0].can_id == op->can_id))
1143 op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
1145 } else {
1146 if (op->flags & SETTIMER) {
1148 /* set timer value */
1149 op->ival1 = msg_head->ival1;
1150 op->ival2 = msg_head->ival2;
1151 op->kt_ival1 = bcm_timeval_to_ktime(msg_head->ival1);
1152 op->kt_ival2 = bcm_timeval_to_ktime(msg_head->ival2);
1154 /* disable an active timer due to zero value? */
1155 if (!op->kt_ival1.tv64)
1156 hrtimer_cancel(&op->timer);
1159 * In any case cancel the throttle timer, flush
1160 * potentially blocked msgs and reset throttle handling
1162 op->kt_lastmsg = ktime_set(0, 0);
1163 hrtimer_cancel(&op->thrtimer);
1164 bcm_rx_thr_flush(op, 1);
1167 if ((op->flags & STARTTIMER) && op->kt_ival1.tv64)
1168 hrtimer_start(&op->timer, op->kt_ival1,
1169 HRTIMER_MODE_REL);
1172 /* now we can register for can_ids, if we added a new bcm_op */
1173 if (do_rx_register) {
1174 if (ifindex) {
1175 struct net_device *dev;
1177 dev = dev_get_by_index(&init_net, ifindex);
1178 if (dev) {
1179 err = can_rx_register(dev, op->can_id,
1180 REGMASK(op->can_id),
1181 bcm_rx_handler, op,
1182 "bcm", sk);
1184 op->rx_reg_dev = dev;
1185 dev_put(dev);
1188 } else
1189 err = can_rx_register(NULL, op->can_id,
1190 REGMASK(op->can_id),
1191 bcm_rx_handler, op, "bcm", sk);
1192 if (err) {
1193 /* this bcm rx op is broken -> remove it */
1194 list_del(&op->list);
1195 bcm_remove_op(op);
1196 return err;
1200 return msg_head->nframes * CFSIZ + MHSIZ;
1204 * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1206 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
1208 struct sk_buff *skb;
1209 struct net_device *dev;
1210 int err;
1212 /* we need a real device to send frames */
1213 if (!ifindex)
1214 return -ENODEV;
1216 skb = alloc_skb(CFSIZ + sizeof(struct can_skb_priv), GFP_KERNEL);
1217 if (!skb)
1218 return -ENOMEM;
1220 can_skb_reserve(skb);
1222 err = memcpy_from_msg(skb_put(skb, CFSIZ), msg, CFSIZ);
1223 if (err < 0) {
1224 kfree_skb(skb);
1225 return err;
1228 dev = dev_get_by_index(&init_net, ifindex);
1229 if (!dev) {
1230 kfree_skb(skb);
1231 return -ENODEV;
1234 can_skb_prv(skb)->ifindex = dev->ifindex;
1235 can_skb_prv(skb)->skbcnt = 0;
1236 skb->dev = dev;
1237 can_skb_set_owner(skb, sk);
1238 err = can_send(skb, 1); /* send with loopback */
1239 dev_put(dev);
1241 if (err)
1242 return err;
1244 return CFSIZ + MHSIZ;
1248 * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1250 static int bcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
1252 struct sock *sk = sock->sk;
1253 struct bcm_sock *bo = bcm_sk(sk);
1254 int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
1255 struct bcm_msg_head msg_head;
1256 int ret; /* read bytes or error codes as return value */
1258 if (!bo->bound)
1259 return -ENOTCONN;
1261 /* check for valid message length from userspace */
1262 if (size < MHSIZ || (size - MHSIZ) % CFSIZ)
1263 return -EINVAL;
1265 /* check for alternative ifindex for this bcm_op */
1267 if (!ifindex && msg->msg_name) {
1268 /* no bound device as default => check msg_name */
1269 DECLARE_SOCKADDR(struct sockaddr_can *, addr, msg->msg_name);
1271 if (msg->msg_namelen < sizeof(*addr))
1272 return -EINVAL;
1274 if (addr->can_family != AF_CAN)
1275 return -EINVAL;
1277 /* ifindex from sendto() */
1278 ifindex = addr->can_ifindex;
1280 if (ifindex) {
1281 struct net_device *dev;
1283 dev = dev_get_by_index(&init_net, ifindex);
1284 if (!dev)
1285 return -ENODEV;
1287 if (dev->type != ARPHRD_CAN) {
1288 dev_put(dev);
1289 return -ENODEV;
1292 dev_put(dev);
1296 /* read message head information */
1298 ret = memcpy_from_msg((u8 *)&msg_head, msg, MHSIZ);
1299 if (ret < 0)
1300 return ret;
1302 lock_sock(sk);
1304 switch (msg_head.opcode) {
1306 case TX_SETUP:
1307 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
1308 break;
1310 case RX_SETUP:
1311 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
1312 break;
1314 case TX_DELETE:
1315 if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
1316 ret = MHSIZ;
1317 else
1318 ret = -EINVAL;
1319 break;
1321 case RX_DELETE:
1322 if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
1323 ret = MHSIZ;
1324 else
1325 ret = -EINVAL;
1326 break;
1328 case TX_READ:
1329 /* reuse msg_head for the reply to TX_READ */
1330 msg_head.opcode = TX_STATUS;
1331 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
1332 break;
1334 case RX_READ:
1335 /* reuse msg_head for the reply to RX_READ */
1336 msg_head.opcode = RX_STATUS;
1337 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
1338 break;
1340 case TX_SEND:
1341 /* we need exactly one can_frame behind the msg head */
1342 if ((msg_head.nframes != 1) || (size != CFSIZ + MHSIZ))
1343 ret = -EINVAL;
1344 else
1345 ret = bcm_tx_send(msg, ifindex, sk);
1346 break;
1348 default:
1349 ret = -EINVAL;
1350 break;
1353 release_sock(sk);
1355 return ret;
1359 * notification handler for netdevice status changes
1361 static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
1362 void *ptr)
1364 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1365 struct bcm_sock *bo = container_of(nb, struct bcm_sock, notifier);
1366 struct sock *sk = &bo->sk;
1367 struct bcm_op *op;
1368 int notify_enodev = 0;
1370 if (!net_eq(dev_net(dev), &init_net))
1371 return NOTIFY_DONE;
1373 if (dev->type != ARPHRD_CAN)
1374 return NOTIFY_DONE;
1376 switch (msg) {
1378 case NETDEV_UNREGISTER:
1379 lock_sock(sk);
1381 /* remove device specific receive entries */
1382 list_for_each_entry(op, &bo->rx_ops, list)
1383 if (op->rx_reg_dev == dev)
1384 bcm_rx_unreg(dev, op);
1386 /* remove device reference, if this is our bound device */
1387 if (bo->bound && bo->ifindex == dev->ifindex) {
1388 bo->bound = 0;
1389 bo->ifindex = 0;
1390 notify_enodev = 1;
1393 release_sock(sk);
1395 if (notify_enodev) {
1396 sk->sk_err = ENODEV;
1397 if (!sock_flag(sk, SOCK_DEAD))
1398 sk->sk_error_report(sk);
1400 break;
1402 case NETDEV_DOWN:
1403 if (bo->bound && bo->ifindex == dev->ifindex) {
1404 sk->sk_err = ENETDOWN;
1405 if (!sock_flag(sk, SOCK_DEAD))
1406 sk->sk_error_report(sk);
1410 return NOTIFY_DONE;
1414 * initial settings for all BCM sockets to be set at socket creation time
1416 static int bcm_init(struct sock *sk)
1418 struct bcm_sock *bo = bcm_sk(sk);
1420 bo->bound = 0;
1421 bo->ifindex = 0;
1422 bo->dropped_usr_msgs = 0;
1423 bo->bcm_proc_read = NULL;
1425 INIT_LIST_HEAD(&bo->tx_ops);
1426 INIT_LIST_HEAD(&bo->rx_ops);
1428 /* set notifier */
1429 bo->notifier.notifier_call = bcm_notifier;
1431 register_netdevice_notifier(&bo->notifier);
1433 return 0;
1437 * standard socket functions
1439 static int bcm_release(struct socket *sock)
1441 struct sock *sk = sock->sk;
1442 struct bcm_sock *bo;
1443 struct bcm_op *op, *next;
1445 if (sk == NULL)
1446 return 0;
1448 bo = bcm_sk(sk);
1450 /* remove bcm_ops, timer, rx_unregister(), etc. */
1452 unregister_netdevice_notifier(&bo->notifier);
1454 lock_sock(sk);
1456 list_for_each_entry_safe(op, next, &bo->tx_ops, list)
1457 bcm_remove_op(op);
1459 list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
1461 * Don't care if we're bound or not (due to netdev problems)
1462 * can_rx_unregister() is always a save thing to do here.
1464 if (op->ifindex) {
1466 * Only remove subscriptions that had not
1467 * been removed due to NETDEV_UNREGISTER
1468 * in bcm_notifier()
1470 if (op->rx_reg_dev) {
1471 struct net_device *dev;
1473 dev = dev_get_by_index(&init_net, op->ifindex);
1474 if (dev) {
1475 bcm_rx_unreg(dev, op);
1476 dev_put(dev);
1479 } else
1480 can_rx_unregister(NULL, op->can_id,
1481 REGMASK(op->can_id),
1482 bcm_rx_handler, op);
1484 bcm_remove_op(op);
1487 /* remove procfs entry */
1488 if (proc_dir && bo->bcm_proc_read)
1489 remove_proc_entry(bo->procname, proc_dir);
1491 /* remove device reference */
1492 if (bo->bound) {
1493 bo->bound = 0;
1494 bo->ifindex = 0;
1497 sock_orphan(sk);
1498 sock->sk = NULL;
1500 release_sock(sk);
1501 sock_put(sk);
1503 return 0;
1506 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1507 int flags)
1509 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1510 struct sock *sk = sock->sk;
1511 struct bcm_sock *bo = bcm_sk(sk);
1512 int ret = 0;
1514 if (len < sizeof(*addr))
1515 return -EINVAL;
1517 lock_sock(sk);
1519 if (bo->bound) {
1520 ret = -EISCONN;
1521 goto fail;
1524 /* bind a device to this socket */
1525 if (addr->can_ifindex) {
1526 struct net_device *dev;
1528 dev = dev_get_by_index(&init_net, addr->can_ifindex);
1529 if (!dev) {
1530 ret = -ENODEV;
1531 goto fail;
1533 if (dev->type != ARPHRD_CAN) {
1534 dev_put(dev);
1535 ret = -ENODEV;
1536 goto fail;
1539 bo->ifindex = dev->ifindex;
1540 dev_put(dev);
1542 } else {
1543 /* no interface reference for ifindex = 0 ('any' CAN device) */
1544 bo->ifindex = 0;
1547 if (proc_dir) {
1548 /* unique socket address as filename */
1549 sprintf(bo->procname, "%lu", sock_i_ino(sk));
1550 bo->bcm_proc_read = proc_create_data(bo->procname, 0644,
1551 proc_dir,
1552 &bcm_proc_fops, sk);
1553 if (!bo->bcm_proc_read) {
1554 ret = -ENOMEM;
1555 goto fail;
1559 bo->bound = 1;
1561 fail:
1562 release_sock(sk);
1564 return ret;
1567 static int bcm_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
1568 int flags)
1570 struct sock *sk = sock->sk;
1571 struct sk_buff *skb;
1572 int error = 0;
1573 int noblock;
1574 int err;
1576 noblock = flags & MSG_DONTWAIT;
1577 flags &= ~MSG_DONTWAIT;
1578 skb = skb_recv_datagram(sk, flags, noblock, &error);
1579 if (!skb)
1580 return error;
1582 if (skb->len < size)
1583 size = skb->len;
1585 err = memcpy_to_msg(msg, skb->data, size);
1586 if (err < 0) {
1587 skb_free_datagram(sk, skb);
1588 return err;
1591 sock_recv_ts_and_drops(msg, sk, skb);
1593 if (msg->msg_name) {
1594 __sockaddr_check_size(sizeof(struct sockaddr_can));
1595 msg->msg_namelen = sizeof(struct sockaddr_can);
1596 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1599 skb_free_datagram(sk, skb);
1601 return size;
1604 static const struct proto_ops bcm_ops = {
1605 .family = PF_CAN,
1606 .release = bcm_release,
1607 .bind = sock_no_bind,
1608 .connect = bcm_connect,
1609 .socketpair = sock_no_socketpair,
1610 .accept = sock_no_accept,
1611 .getname = sock_no_getname,
1612 .poll = datagram_poll,
1613 .ioctl = can_ioctl, /* use can_ioctl() from af_can.c */
1614 .listen = sock_no_listen,
1615 .shutdown = sock_no_shutdown,
1616 .setsockopt = sock_no_setsockopt,
1617 .getsockopt = sock_no_getsockopt,
1618 .sendmsg = bcm_sendmsg,
1619 .recvmsg = bcm_recvmsg,
1620 .mmap = sock_no_mmap,
1621 .sendpage = sock_no_sendpage,
1624 static struct proto bcm_proto __read_mostly = {
1625 .name = "CAN_BCM",
1626 .owner = THIS_MODULE,
1627 .obj_size = sizeof(struct bcm_sock),
1628 .init = bcm_init,
1631 static const struct can_proto bcm_can_proto = {
1632 .type = SOCK_DGRAM,
1633 .protocol = CAN_BCM,
1634 .ops = &bcm_ops,
1635 .prot = &bcm_proto,
1638 static int __init bcm_module_init(void)
1640 int err;
1642 pr_info("can: broadcast manager protocol (rev " CAN_BCM_VERSION " t)\n");
1644 err = can_proto_register(&bcm_can_proto);
1645 if (err < 0) {
1646 printk(KERN_ERR "can: registration of bcm protocol failed\n");
1647 return err;
1650 /* create /proc/net/can-bcm directory */
1651 proc_dir = proc_mkdir("can-bcm", init_net.proc_net);
1652 return 0;
1655 static void __exit bcm_module_exit(void)
1657 can_proto_unregister(&bcm_can_proto);
1659 if (proc_dir)
1660 remove_proc_entry("can-bcm", init_net.proc_net);
1663 module_init(bcm_module_init);
1664 module_exit(bcm_module_exit);