blob: 6248ae2502c765d1bf263059d2f58baffab930a6 [file] [log] [blame]
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001/*
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/init.h>
Oliver Hartkopp73e87e02008-04-15 19:29:14 -070046#include <linux/hrtimer.h>
Oliver Hartkoppffd980f2007-11-16 15:53:52 -080047#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>
60
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 */
65
66/* get best masking value for can_rx_register() for a given single can_id */
Oliver Hartkoppd253eee2008-12-03 15:52:35 -080067#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))
Oliver Hartkoppffd980f2007-11-16 15:53:52 -080070
Oliver Hartkoppd253eee2008-12-03 15:52:35 -080071#define CAN_BCM_VERSION CAN_VERSION
Oliver Hartkoppffd980f2007-11-16 15:53:52 -080072static __initdata const char banner[] = KERN_INFO
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -080073 "can: broadcast manager protocol (rev " CAN_BCM_VERSION " t)\n";
Oliver Hartkoppffd980f2007-11-16 15:53:52 -080074
75MODULE_DESCRIPTION("PF_CAN broadcast manager protocol");
76MODULE_LICENSE("Dual BSD/GPL");
77MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
78
79/* easy access to can_frame payload */
80static inline u64 GET_U64(const struct can_frame *cp)
81{
82 return *(u64 *)cp->data;
83}
84
85struct bcm_op {
86 struct list_head list;
87 int ifindex;
88 canid_t can_id;
89 int flags;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -080090 unsigned long frames_abs, frames_filtered;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -080091 struct timeval ival1, ival2;
Oliver Hartkopp73e87e02008-04-15 19:29:14 -070092 struct hrtimer timer, thrtimer;
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -080093 struct tasklet_struct tsklet, thrtsklet;
Oliver Hartkopp73e87e02008-04-15 19:29:14 -070094 ktime_t rx_stamp, kt_ival1, kt_ival2, kt_lastmsg;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -080095 int rx_ifindex;
96 int count;
97 int nframes;
98 int currframe;
99 struct can_frame *frames;
100 struct can_frame *last_frames;
101 struct can_frame sframe;
102 struct can_frame last_sframe;
103 struct sock *sk;
104 struct net_device *rx_reg_dev;
105};
106
107static struct proc_dir_entry *proc_dir;
108
109struct bcm_sock {
110 struct sock sk;
111 int bound;
112 int ifindex;
113 struct notifier_block notifier;
114 struct list_head rx_ops;
115 struct list_head tx_ops;
116 unsigned long dropped_usr_msgs;
117 struct proc_dir_entry *bcm_proc_read;
118 char procname [9]; /* pointer printed in ASCII with \0 */
119};
120
121static inline struct bcm_sock *bcm_sk(const struct sock *sk)
122{
123 return (struct bcm_sock *)sk;
124}
125
126#define CFSIZ sizeof(struct can_frame)
127#define OPSIZ sizeof(struct bcm_op)
128#define MHSIZ sizeof(struct bcm_msg_head)
129
130/*
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800131 * procfs functions
132 */
133static char *bcm_proc_getifname(int ifindex)
134{
135 struct net_device *dev;
136
137 if (!ifindex)
138 return "any";
139
140 /* no usage counting */
141 dev = __dev_get_by_index(&init_net, ifindex);
142 if (dev)
143 return dev->name;
144
145 return "???";
146}
147
148static int bcm_read_proc(char *page, char **start, off_t off,
149 int count, int *eof, void *data)
150{
151 int len = 0;
152 struct sock *sk = (struct sock *)data;
153 struct bcm_sock *bo = bcm_sk(sk);
154 struct bcm_op *op;
155
156 len += snprintf(page + len, PAGE_SIZE - len, ">>> socket %p",
157 sk->sk_socket);
158 len += snprintf(page + len, PAGE_SIZE - len, " / sk %p", sk);
159 len += snprintf(page + len, PAGE_SIZE - len, " / bo %p", bo);
160 len += snprintf(page + len, PAGE_SIZE - len, " / dropped %lu",
161 bo->dropped_usr_msgs);
162 len += snprintf(page + len, PAGE_SIZE - len, " / bound %s",
163 bcm_proc_getifname(bo->ifindex));
164 len += snprintf(page + len, PAGE_SIZE - len, " <<<\n");
165
166 list_for_each_entry(op, &bo->rx_ops, list) {
167
168 unsigned long reduction;
169
170 /* print only active entries & prevent division by zero */
171 if (!op->frames_abs)
172 continue;
173
174 len += snprintf(page + len, PAGE_SIZE - len,
175 "rx_op: %03X %-5s ",
176 op->can_id, bcm_proc_getifname(op->ifindex));
177 len += snprintf(page + len, PAGE_SIZE - len, "[%d]%c ",
178 op->nframes,
179 (op->flags & RX_CHECK_DLC)?'d':' ');
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700180 if (op->kt_ival1.tv64)
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800181 len += snprintf(page + len, PAGE_SIZE - len,
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700182 "timeo=%lld ",
183 (long long)
184 ktime_to_us(op->kt_ival1));
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800185
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700186 if (op->kt_ival2.tv64)
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800187 len += snprintf(page + len, PAGE_SIZE - len,
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700188 "thr=%lld ",
189 (long long)
190 ktime_to_us(op->kt_ival2));
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800191
192 len += snprintf(page + len, PAGE_SIZE - len,
193 "# recv %ld (%ld) => reduction: ",
194 op->frames_filtered, op->frames_abs);
195
196 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
197
198 len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
199 (reduction == 100)?"near ":"", reduction);
200
201 if (len > PAGE_SIZE - 200) {
202 /* mark output cut off */
203 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
204 break;
205 }
206 }
207
208 list_for_each_entry(op, &bo->tx_ops, list) {
209
210 len += snprintf(page + len, PAGE_SIZE - len,
211 "tx_op: %03X %s [%d] ",
212 op->can_id, bcm_proc_getifname(op->ifindex),
213 op->nframes);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800214
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700215 if (op->kt_ival1.tv64)
216 len += snprintf(page + len, PAGE_SIZE - len, "t1=%lld ",
217 (long long) ktime_to_us(op->kt_ival1));
218
219 if (op->kt_ival2.tv64)
220 len += snprintf(page + len, PAGE_SIZE - len, "t2=%lld ",
221 (long long) ktime_to_us(op->kt_ival2));
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800222
223 len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n",
224 op->frames_abs);
225
226 if (len > PAGE_SIZE - 100) {
227 /* mark output cut off */
228 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
229 break;
230 }
231 }
232
233 len += snprintf(page + len, PAGE_SIZE - len, "\n");
234
235 *eof = 1;
236 return len;
237}
238
239/*
240 * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
241 * of the given bcm tx op
242 */
243static void bcm_can_tx(struct bcm_op *op)
244{
245 struct sk_buff *skb;
246 struct net_device *dev;
247 struct can_frame *cf = &op->frames[op->currframe];
248
249 /* no target device? => exit */
250 if (!op->ifindex)
251 return;
252
253 dev = dev_get_by_index(&init_net, op->ifindex);
254 if (!dev) {
255 /* RFC: should this bcm_op remove itself here? */
256 return;
257 }
258
259 skb = alloc_skb(CFSIZ, gfp_any());
260 if (!skb)
261 goto out;
262
263 memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
264
265 /* send with loopback */
266 skb->dev = dev;
267 skb->sk = op->sk;
268 can_send(skb, 1);
269
270 /* update statistics */
271 op->currframe++;
272 op->frames_abs++;
273
274 /* reached last frame? */
275 if (op->currframe >= op->nframes)
276 op->currframe = 0;
277 out:
278 dev_put(dev);
279}
280
281/*
282 * bcm_send_to_user - send a BCM message to the userspace
283 * (consisting of bcm_msg_head + x CAN frames)
284 */
285static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
286 struct can_frame *frames, int has_timestamp)
287{
288 struct sk_buff *skb;
289 struct can_frame *firstframe;
290 struct sockaddr_can *addr;
291 struct sock *sk = op->sk;
292 int datalen = head->nframes * CFSIZ;
293 int err;
294
295 skb = alloc_skb(sizeof(*head) + datalen, gfp_any());
296 if (!skb)
297 return;
298
299 memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
300
301 if (head->nframes) {
302 /* can_frames starting here */
Oliver Hartkopp7f2d38e2008-07-05 23:38:43 -0700303 firstframe = (struct can_frame *)skb_tail_pointer(skb);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800304
305 memcpy(skb_put(skb, datalen), frames, datalen);
306
307 /*
308 * the BCM uses the can_dlc-element of the can_frame
309 * structure for internal purposes. This is only
310 * relevant for updates that are generated by the
311 * BCM, where nframes is 1
312 */
313 if (head->nframes == 1)
314 firstframe->can_dlc &= BCM_CAN_DLC_MASK;
315 }
316
317 if (has_timestamp) {
318 /* restore rx timestamp */
319 skb->tstamp = op->rx_stamp;
320 }
321
322 /*
323 * Put the datagram to the queue so that bcm_recvmsg() can
324 * get it from there. We need to pass the interface index to
325 * bcm_recvmsg(). We pass a whole struct sockaddr_can in skb->cb
326 * containing the interface index.
327 */
328
329 BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct sockaddr_can));
330 addr = (struct sockaddr_can *)skb->cb;
331 memset(addr, 0, sizeof(*addr));
332 addr->can_family = AF_CAN;
333 addr->can_ifindex = op->rx_ifindex;
334
335 err = sock_queue_rcv_skb(sk, skb);
336 if (err < 0) {
337 struct bcm_sock *bo = bcm_sk(sk);
338
339 kfree_skb(skb);
340 /* don't care about overflows in this statistic */
341 bo->dropped_usr_msgs++;
342 }
343}
344
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800345static void bcm_tx_timeout_tsklet(unsigned long data)
346{
347 struct bcm_op *op = (struct bcm_op *)data;
348 struct bcm_msg_head msg_head;
349
350 /* create notification to user */
351 msg_head.opcode = TX_EXPIRED;
352 msg_head.flags = op->flags;
353 msg_head.count = op->count;
354 msg_head.ival1 = op->ival1;
355 msg_head.ival2 = op->ival2;
356 msg_head.can_id = op->can_id;
357 msg_head.nframes = 0;
358
359 bcm_send_to_user(op, &msg_head, NULL, 0);
360}
361
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800362/*
363 * bcm_tx_timeout_handler - performes cyclic CAN frame transmissions
364 */
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700365static enum hrtimer_restart bcm_tx_timeout_handler(struct hrtimer *hrtimer)
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800366{
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700367 struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
368 enum hrtimer_restart ret = HRTIMER_NORESTART;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800369
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700370 if (op->kt_ival1.tv64 && (op->count > 0)) {
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800371
372 op->count--;
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800373 if (!op->count && (op->flags & TX_COUNTEVT))
374 tasklet_schedule(&op->tsklet);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800375 }
376
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700377 if (op->kt_ival1.tv64 && (op->count > 0)) {
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800378
379 /* send (next) frame */
380 bcm_can_tx(op);
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700381 hrtimer_forward(hrtimer, ktime_get(), op->kt_ival1);
382 ret = HRTIMER_RESTART;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800383
384 } else {
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700385 if (op->kt_ival2.tv64) {
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800386
387 /* send (next) frame */
388 bcm_can_tx(op);
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700389 hrtimer_forward(hrtimer, ktime_get(), op->kt_ival2);
390 ret = HRTIMER_RESTART;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800391 }
392 }
393
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700394 return ret;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800395}
396
397/*
398 * bcm_rx_changed - create a RX_CHANGED notification due to changed content
399 */
400static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
401{
402 struct bcm_msg_head head;
403
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800404 /* update statistics */
405 op->frames_filtered++;
406
407 /* prevent statistics overflow */
408 if (op->frames_filtered > ULONG_MAX/100)
409 op->frames_filtered = op->frames_abs = 0;
410
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800411 /* this element is not throttled anymore */
412 data->can_dlc &= (BCM_CAN_DLC_MASK|RX_RECV);
413
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800414 head.opcode = RX_CHANGED;
415 head.flags = op->flags;
416 head.count = op->count;
417 head.ival1 = op->ival1;
418 head.ival2 = op->ival2;
419 head.can_id = op->can_id;
420 head.nframes = 1;
421
422 bcm_send_to_user(op, &head, data, 1);
423}
424
425/*
426 * bcm_rx_update_and_send - process a detected relevant receive content change
427 * 1. update the last received data
428 * 2. send a notification to the user (if possible)
429 */
430static void bcm_rx_update_and_send(struct bcm_op *op,
431 struct can_frame *lastdata,
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800432 const struct can_frame *rxdata)
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800433{
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800434 memcpy(lastdata, rxdata, CFSIZ);
435
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800436 /* mark as used and throttled by default */
437 lastdata->can_dlc |= (RX_RECV|RX_THR);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800438
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800439 /* throtteling mode inactive ? */
440 if (!op->kt_ival2.tv64) {
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800441 /* send RX_CHANGED to the user immediately */
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800442 bcm_rx_changed(op, lastdata);
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700443 return;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800444 }
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700445
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800446 /* with active throttling timer we are just done here */
447 if (hrtimer_active(&op->thrtimer))
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700448 return;
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700449
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800450 /* first receiption with enabled throttling mode */
451 if (!op->kt_lastmsg.tv64)
452 goto rx_changed_settime;
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700453
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800454 /* got a second frame inside a potential throttle period? */
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700455 if (ktime_us_delta(ktime_get(), op->kt_lastmsg) <
456 ktime_to_us(op->kt_ival2)) {
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800457 /* do not send the saved data - only start throttle timer */
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700458 hrtimer_start(&op->thrtimer,
459 ktime_add(op->kt_lastmsg, op->kt_ival2),
460 HRTIMER_MODE_ABS);
461 return;
462 }
463
464 /* the gap was that big, that throttling was not needed here */
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800465rx_changed_settime:
466 bcm_rx_changed(op, lastdata);
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700467 op->kt_lastmsg = ktime_get();
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800468}
469
470/*
471 * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly
472 * received data stored in op->last_frames[]
473 */
474static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800475 const struct can_frame *rxdata)
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800476{
477 /*
478 * no one uses the MSBs of can_dlc for comparation,
479 * so we use it here to detect the first time of reception
480 */
481
482 if (!(op->last_frames[index].can_dlc & RX_RECV)) {
483 /* received data for the first time => send update to user */
484 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
485 return;
486 }
487
488 /* do a real check in can_frame data section */
489
490 if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) !=
491 (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) {
492 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
493 return;
494 }
495
496 if (op->flags & RX_CHECK_DLC) {
497 /* do a real check in can_frame dlc */
498 if (rxdata->can_dlc != (op->last_frames[index].can_dlc &
499 BCM_CAN_DLC_MASK)) {
500 bcm_rx_update_and_send(op, &op->last_frames[index],
501 rxdata);
502 return;
503 }
504 }
505}
506
507/*
508 * bcm_rx_starttimer - enable timeout monitoring for CAN frame receiption
509 */
510static void bcm_rx_starttimer(struct bcm_op *op)
511{
512 if (op->flags & RX_NO_AUTOTIMER)
513 return;
514
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700515 if (op->kt_ival1.tv64)
516 hrtimer_start(&op->timer, op->kt_ival1, HRTIMER_MODE_REL);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800517}
518
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800519static void bcm_rx_timeout_tsklet(unsigned long data)
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800520{
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800521 struct bcm_op *op = (struct bcm_op *)data;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800522 struct bcm_msg_head msg_head;
523
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800524 /* create notification to user */
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800525 msg_head.opcode = RX_TIMEOUT;
526 msg_head.flags = op->flags;
527 msg_head.count = op->count;
528 msg_head.ival1 = op->ival1;
529 msg_head.ival2 = op->ival2;
530 msg_head.can_id = op->can_id;
531 msg_head.nframes = 0;
532
533 bcm_send_to_user(op, &msg_head, NULL, 0);
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800534}
535
536/*
537 * bcm_rx_timeout_handler - when the (cyclic) CAN frame receiption timed out
538 */
539static enum hrtimer_restart bcm_rx_timeout_handler(struct hrtimer *hrtimer)
540{
541 struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
542
543 /* schedule before NET_RX_SOFTIRQ */
544 tasklet_hi_schedule(&op->tsklet);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800545
546 /* no restart of the timer is done here! */
547
548 /* if user wants to be informed, when cyclic CAN-Messages come back */
549 if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
550 /* clear received can_frames to indicate 'nothing received' */
551 memset(op->last_frames, 0, op->nframes * CFSIZ);
552 }
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700553
554 return HRTIMER_NORESTART;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800555}
556
557/*
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800558 * bcm_rx_do_flush - helper for bcm_rx_thr_flush
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800559 */
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800560static inline int bcm_rx_do_flush(struct bcm_op *op, int update, int index)
561{
562 if ((op->last_frames) && (op->last_frames[index].can_dlc & RX_THR)) {
563 if (update)
564 bcm_rx_changed(op, &op->last_frames[index]);
565 return 1;
566 }
567 return 0;
568}
569
570/*
571 * bcm_rx_thr_flush - Check for throttled data and send it to the userspace
572 *
573 * update == 0 : just check if throttled data is available (any irq context)
574 * update == 1 : check and send throttled data to userspace (soft_irq context)
575 */
576static int bcm_rx_thr_flush(struct bcm_op *op, int update)
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800577{
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700578 int updated = 0;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800579
580 if (op->nframes > 1) {
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700581 int i;
582
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800583 /* for MUX filter we start at index 1 */
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800584 for (i = 1; i < op->nframes; i++)
585 updated += bcm_rx_do_flush(op, update, i);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800586
587 } else {
588 /* for RX_FILTER_ID and simple filter */
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800589 updated += bcm_rx_do_flush(op, update, 0);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800590 }
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700591
592 return updated;
593}
594
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800595static void bcm_rx_thr_tsklet(unsigned long data)
596{
597 struct bcm_op *op = (struct bcm_op *)data;
598
599 /* push the changed data to the userspace */
600 bcm_rx_thr_flush(op, 1);
601}
602
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700603/*
604 * bcm_rx_thr_handler - the time for blocked content updates is over now:
605 * Check for throttled data and send it to the userspace
606 */
607static enum hrtimer_restart bcm_rx_thr_handler(struct hrtimer *hrtimer)
608{
609 struct bcm_op *op = container_of(hrtimer, struct bcm_op, thrtimer);
610
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800611 tasklet_schedule(&op->thrtsklet);
612
613 if (bcm_rx_thr_flush(op, 0)) {
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700614 hrtimer_forward(hrtimer, ktime_get(), op->kt_ival2);
615 return HRTIMER_RESTART;
616 } else {
617 /* rearm throttle handling */
618 op->kt_lastmsg = ktime_set(0, 0);
619 return HRTIMER_NORESTART;
620 }
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800621}
622
623/*
624 * bcm_rx_handler - handle a CAN frame receiption
625 */
626static void bcm_rx_handler(struct sk_buff *skb, void *data)
627{
628 struct bcm_op *op = (struct bcm_op *)data;
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800629 const struct can_frame *rxframe = (struct can_frame *)skb->data;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800630 int i;
631
632 /* disable timeout */
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700633 hrtimer_cancel(&op->timer);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800634
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800635 if (op->can_id != rxframe->can_id)
636 goto rx_freeskb;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800637
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800638 /* save rx timestamp */
639 op->rx_stamp = skb->tstamp;
640 /* save originator for recvfrom() */
641 op->rx_ifindex = skb->dev->ifindex;
642 /* update statistics */
643 op->frames_abs++;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800644
645 if (op->flags & RX_RTR_FRAME) {
646 /* send reply for RTR-request (placed in op->frames[0]) */
647 bcm_can_tx(op);
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800648 goto rx_freeskb;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800649 }
650
651 if (op->flags & RX_FILTER_ID) {
652 /* the easiest case */
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800653 bcm_rx_update_and_send(op, &op->last_frames[0], rxframe);
654 goto rx_freeskb_starttimer;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800655 }
656
657 if (op->nframes == 1) {
658 /* simple compare with index 0 */
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800659 bcm_rx_cmp_to_index(op, 0, rxframe);
660 goto rx_freeskb_starttimer;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800661 }
662
663 if (op->nframes > 1) {
664 /*
665 * multiplex compare
666 *
667 * find the first multiplex mask that fits.
668 * Remark: The MUX-mask is stored in index 0
669 */
670
671 for (i = 1; i < op->nframes; i++) {
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800672 if ((GET_U64(&op->frames[0]) & GET_U64(rxframe)) ==
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800673 (GET_U64(&op->frames[0]) &
674 GET_U64(&op->frames[i]))) {
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800675 bcm_rx_cmp_to_index(op, i, rxframe);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800676 break;
677 }
678 }
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800679 }
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800680
681rx_freeskb_starttimer:
682 bcm_rx_starttimer(op);
683rx_freeskb:
684 kfree_skb(skb);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800685}
686
687/*
688 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
689 */
690static 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
703static void bcm_remove_op(struct bcm_op *op)
704{
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700705 hrtimer_cancel(&op->timer);
706 hrtimer_cancel(&op->thrtimer);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800707
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800708 if (op->tsklet.func)
709 tasklet_kill(&op->tsklet);
710
711 if (op->thrtsklet.func)
712 tasklet_kill(&op->thrtsklet);
713
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800714 if ((op->frames) && (op->frames != &op->sframe))
715 kfree(op->frames);
716
717 if ((op->last_frames) && (op->last_frames != &op->last_sframe))
718 kfree(op->last_frames);
719
720 kfree(op);
721
722 return;
723}
724
725static void bcm_rx_unreg(struct net_device *dev, struct bcm_op *op)
726{
727 if (op->rx_reg_dev == dev) {
728 can_rx_unregister(dev, op->can_id, REGMASK(op->can_id),
729 bcm_rx_handler, op);
730
731 /* mark as removed subscription */
732 op->rx_reg_dev = NULL;
733 } else
734 printk(KERN_ERR "can-bcm: bcm_rx_unreg: registered device "
735 "mismatch %p %p\n", op->rx_reg_dev, dev);
736}
737
738/*
739 * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
740 */
741static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
742{
743 struct bcm_op *op, *n;
744
745 list_for_each_entry_safe(op, n, ops, list) {
746 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
747
748 /*
749 * Don't care if we're bound or not (due to netdev
750 * problems) can_rx_unregister() is always a save
751 * thing to do here.
752 */
753 if (op->ifindex) {
754 /*
755 * Only remove subscriptions that had not
756 * been removed due to NETDEV_UNREGISTER
757 * in bcm_notifier()
758 */
759 if (op->rx_reg_dev) {
760 struct net_device *dev;
761
762 dev = dev_get_by_index(&init_net,
763 op->ifindex);
764 if (dev) {
765 bcm_rx_unreg(dev, op);
766 dev_put(dev);
767 }
768 }
769 } else
770 can_rx_unregister(NULL, op->can_id,
771 REGMASK(op->can_id),
772 bcm_rx_handler, op);
773
774 list_del(&op->list);
775 bcm_remove_op(op);
776 return 1; /* done */
777 }
778 }
779
780 return 0; /* not found */
781}
782
783/*
784 * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
785 */
786static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
787{
788 struct bcm_op *op, *n;
789
790 list_for_each_entry_safe(op, n, ops, list) {
791 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
792 list_del(&op->list);
793 bcm_remove_op(op);
794 return 1; /* done */
795 }
796 }
797
798 return 0; /* not found */
799}
800
801/*
802 * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg)
803 */
804static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
805 int ifindex)
806{
807 struct bcm_op *op = bcm_find_op(ops, msg_head->can_id, ifindex);
808
809 if (!op)
810 return -EINVAL;
811
812 /* put current values into msg_head */
813 msg_head->flags = op->flags;
814 msg_head->count = op->count;
815 msg_head->ival1 = op->ival1;
816 msg_head->ival2 = op->ival2;
817 msg_head->nframes = op->nframes;
818
819 bcm_send_to_user(op, msg_head, op->frames, 0);
820
821 return MHSIZ;
822}
823
824/*
825 * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg)
826 */
827static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
828 int ifindex, struct sock *sk)
829{
830 struct bcm_sock *bo = bcm_sk(sk);
831 struct bcm_op *op;
832 int i, err;
833
834 /* we need a real device to send frames */
835 if (!ifindex)
836 return -ENODEV;
837
838 /* we need at least one can_frame */
839 if (msg_head->nframes < 1)
840 return -EINVAL;
841
842 /* check the given can_id */
843 op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex);
844
845 if (op) {
846 /* update existing BCM operation */
847
848 /*
849 * Do we need more space for the can_frames than currently
850 * allocated? -> This is a _really_ unusual use-case and
851 * therefore (complexity / locking) it is not supported.
852 */
853 if (msg_head->nframes > op->nframes)
854 return -E2BIG;
855
856 /* update can_frames content */
857 for (i = 0; i < msg_head->nframes; i++) {
858 err = memcpy_fromiovec((u8 *)&op->frames[i],
859 msg->msg_iov, CFSIZ);
Oliver Hartkopp7f2d38e2008-07-05 23:38:43 -0700860
861 if (op->frames[i].can_dlc > 8)
862 err = -EINVAL;
863
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800864 if (err < 0)
865 return err;
866
867 if (msg_head->flags & TX_CP_CAN_ID) {
868 /* copy can_id into frame */
869 op->frames[i].can_id = msg_head->can_id;
870 }
871 }
872
873 } else {
874 /* insert new BCM operation for the given can_id */
875
876 op = kzalloc(OPSIZ, GFP_KERNEL);
877 if (!op)
878 return -ENOMEM;
879
880 op->can_id = msg_head->can_id;
881
882 /* create array for can_frames and copy the data */
883 if (msg_head->nframes > 1) {
884 op->frames = kmalloc(msg_head->nframes * CFSIZ,
885 GFP_KERNEL);
886 if (!op->frames) {
887 kfree(op);
888 return -ENOMEM;
889 }
890 } else
891 op->frames = &op->sframe;
892
893 for (i = 0; i < msg_head->nframes; i++) {
894 err = memcpy_fromiovec((u8 *)&op->frames[i],
895 msg->msg_iov, CFSIZ);
Oliver Hartkopp7f2d38e2008-07-05 23:38:43 -0700896
897 if (op->frames[i].can_dlc > 8)
898 err = -EINVAL;
899
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800900 if (err < 0) {
901 if (op->frames != &op->sframe)
902 kfree(op->frames);
903 kfree(op);
904 return err;
905 }
906
907 if (msg_head->flags & TX_CP_CAN_ID) {
908 /* copy can_id into frame */
909 op->frames[i].can_id = msg_head->can_id;
910 }
911 }
912
913 /* tx_ops never compare with previous received messages */
914 op->last_frames = NULL;
915
916 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
917 op->sk = sk;
918 op->ifindex = ifindex;
919
920 /* initialize uninitialized (kzalloc) structure */
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700921 hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
922 op->timer.function = bcm_tx_timeout_handler;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800923
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800924 /* initialize tasklet for tx countevent notification */
925 tasklet_init(&op->tsklet, bcm_tx_timeout_tsklet,
926 (unsigned long) op);
927
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800928 /* currently unused in tx_ops */
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700929 hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800930
931 /* add this bcm_op to the list of the tx_ops */
932 list_add(&op->list, &bo->tx_ops);
933
934 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
935
936 if (op->nframes != msg_head->nframes) {
937 op->nframes = msg_head->nframes;
938 /* start multiple frame transmission with index 0 */
939 op->currframe = 0;
940 }
941
942 /* check flags */
943
944 op->flags = msg_head->flags;
945
946 if (op->flags & TX_RESET_MULTI_IDX) {
947 /* start multiple frame transmission with index 0 */
948 op->currframe = 0;
949 }
950
951 if (op->flags & SETTIMER) {
952 /* set timer values */
953 op->count = msg_head->count;
954 op->ival1 = msg_head->ival1;
955 op->ival2 = msg_head->ival2;
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700956 op->kt_ival1 = timeval_to_ktime(msg_head->ival1);
957 op->kt_ival2 = timeval_to_ktime(msg_head->ival2);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800958
959 /* disable an active timer due to zero values? */
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700960 if (!op->kt_ival1.tv64 && !op->kt_ival2.tv64)
961 hrtimer_cancel(&op->timer);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800962 }
963
964 if ((op->flags & STARTTIMER) &&
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700965 ((op->kt_ival1.tv64 && op->count) || op->kt_ival2.tv64)) {
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800966
967 /* spec: send can_frame when starting timer */
968 op->flags |= TX_ANNOUNCE;
969
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700970 if (op->kt_ival1.tv64 && (op->count > 0)) {
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800971 /* op->count-- is done in bcm_tx_timeout_handler */
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700972 hrtimer_start(&op->timer, op->kt_ival1,
973 HRTIMER_MODE_REL);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800974 } else
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700975 hrtimer_start(&op->timer, op->kt_ival2,
976 HRTIMER_MODE_REL);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800977 }
978
979 if (op->flags & TX_ANNOUNCE)
980 bcm_can_tx(op);
981
982 return msg_head->nframes * CFSIZ + MHSIZ;
983}
984
985/*
986 * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
987 */
988static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
989 int ifindex, struct sock *sk)
990{
991 struct bcm_sock *bo = bcm_sk(sk);
992 struct bcm_op *op;
993 int do_rx_register;
994 int err = 0;
995
996 if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
997 /* be robust against wrong usage ... */
998 msg_head->flags |= RX_FILTER_ID;
999 /* ignore trailing garbage */
1000 msg_head->nframes = 0;
1001 }
1002
1003 if ((msg_head->flags & RX_RTR_FRAME) &&
1004 ((msg_head->nframes != 1) ||
1005 (!(msg_head->can_id & CAN_RTR_FLAG))))
1006 return -EINVAL;
1007
1008 /* check the given can_id */
1009 op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex);
1010 if (op) {
1011 /* update existing BCM operation */
1012
1013 /*
1014 * Do we need more space for the can_frames than currently
1015 * allocated? -> This is a _really_ unusual use-case and
1016 * therefore (complexity / locking) it is not supported.
1017 */
1018 if (msg_head->nframes > op->nframes)
1019 return -E2BIG;
1020
1021 if (msg_head->nframes) {
1022 /* update can_frames content */
1023 err = memcpy_fromiovec((u8 *)op->frames,
1024 msg->msg_iov,
1025 msg_head->nframes * CFSIZ);
1026 if (err < 0)
1027 return err;
1028
1029 /* clear last_frames to indicate 'nothing received' */
1030 memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
1031 }
1032
1033 op->nframes = msg_head->nframes;
1034
1035 /* Only an update -> do not call can_rx_register() */
1036 do_rx_register = 0;
1037
1038 } else {
1039 /* insert new BCM operation for the given can_id */
1040 op = kzalloc(OPSIZ, GFP_KERNEL);
1041 if (!op)
1042 return -ENOMEM;
1043
1044 op->can_id = msg_head->can_id;
1045 op->nframes = msg_head->nframes;
1046
1047 if (msg_head->nframes > 1) {
1048 /* create array for can_frames and copy the data */
1049 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1050 GFP_KERNEL);
1051 if (!op->frames) {
1052 kfree(op);
1053 return -ENOMEM;
1054 }
1055
1056 /* create and init array for received can_frames */
1057 op->last_frames = kzalloc(msg_head->nframes * CFSIZ,
1058 GFP_KERNEL);
1059 if (!op->last_frames) {
1060 kfree(op->frames);
1061 kfree(op);
1062 return -ENOMEM;
1063 }
1064
1065 } else {
1066 op->frames = &op->sframe;
1067 op->last_frames = &op->last_sframe;
1068 }
1069
1070 if (msg_head->nframes) {
1071 err = memcpy_fromiovec((u8 *)op->frames, msg->msg_iov,
1072 msg_head->nframes * CFSIZ);
1073 if (err < 0) {
1074 if (op->frames != &op->sframe)
1075 kfree(op->frames);
1076 if (op->last_frames != &op->last_sframe)
1077 kfree(op->last_frames);
1078 kfree(op);
1079 return err;
1080 }
1081 }
1082
1083 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1084 op->sk = sk;
1085 op->ifindex = ifindex;
1086
1087 /* initialize uninitialized (kzalloc) structure */
Oliver Hartkopp73e87e02008-04-15 19:29:14 -07001088 hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1089 op->timer.function = bcm_rx_timeout_handler;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001090
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -08001091 /* initialize tasklet for rx timeout notification */
1092 tasklet_init(&op->tsklet, bcm_rx_timeout_tsklet,
1093 (unsigned long) op);
1094
Oliver Hartkopp73e87e02008-04-15 19:29:14 -07001095 hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1096 op->thrtimer.function = bcm_rx_thr_handler;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001097
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -08001098 /* initialize tasklet for rx throttle handling */
1099 tasklet_init(&op->thrtsklet, bcm_rx_thr_tsklet,
1100 (unsigned long) op);
1101
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001102 /* add this bcm_op to the list of the rx_ops */
1103 list_add(&op->list, &bo->rx_ops);
1104
1105 /* call can_rx_register() */
1106 do_rx_register = 1;
1107
1108 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
1109
1110 /* check flags */
1111 op->flags = msg_head->flags;
1112
1113 if (op->flags & RX_RTR_FRAME) {
1114
1115 /* no timers in RTR-mode */
Oliver Hartkopp73e87e02008-04-15 19:29:14 -07001116 hrtimer_cancel(&op->thrtimer);
1117 hrtimer_cancel(&op->timer);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001118
1119 /*
1120 * funny feature in RX(!)_SETUP only for RTR-mode:
1121 * copy can_id into frame BUT without RTR-flag to
1122 * prevent a full-load-loopback-test ... ;-]
1123 */
1124 if ((op->flags & TX_CP_CAN_ID) ||
1125 (op->frames[0].can_id == op->can_id))
1126 op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
1127
1128 } else {
1129 if (op->flags & SETTIMER) {
1130
1131 /* set timer value */
1132 op->ival1 = msg_head->ival1;
1133 op->ival2 = msg_head->ival2;
Oliver Hartkopp73e87e02008-04-15 19:29:14 -07001134 op->kt_ival1 = timeval_to_ktime(msg_head->ival1);
1135 op->kt_ival2 = timeval_to_ktime(msg_head->ival2);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001136
1137 /* disable an active timer due to zero value? */
Oliver Hartkopp73e87e02008-04-15 19:29:14 -07001138 if (!op->kt_ival1.tv64)
1139 hrtimer_cancel(&op->timer);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001140
1141 /*
Oliver Hartkopp73e87e02008-04-15 19:29:14 -07001142 * In any case cancel the throttle timer, flush
1143 * potentially blocked msgs and reset throttle handling
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001144 */
Oliver Hartkopp73e87e02008-04-15 19:29:14 -07001145 op->kt_lastmsg = ktime_set(0, 0);
1146 hrtimer_cancel(&op->thrtimer);
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -08001147 bcm_rx_thr_flush(op, 1);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001148 }
1149
Oliver Hartkopp73e87e02008-04-15 19:29:14 -07001150 if ((op->flags & STARTTIMER) && op->kt_ival1.tv64)
1151 hrtimer_start(&op->timer, op->kt_ival1,
1152 HRTIMER_MODE_REL);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001153 }
1154
1155 /* now we can register for can_ids, if we added a new bcm_op */
1156 if (do_rx_register) {
1157 if (ifindex) {
1158 struct net_device *dev;
1159
1160 dev = dev_get_by_index(&init_net, ifindex);
1161 if (dev) {
1162 err = can_rx_register(dev, op->can_id,
1163 REGMASK(op->can_id),
1164 bcm_rx_handler, op,
1165 "bcm");
1166
1167 op->rx_reg_dev = dev;
1168 dev_put(dev);
1169 }
1170
1171 } else
1172 err = can_rx_register(NULL, op->can_id,
1173 REGMASK(op->can_id),
1174 bcm_rx_handler, op, "bcm");
1175 if (err) {
1176 /* this bcm rx op is broken -> remove it */
1177 list_del(&op->list);
1178 bcm_remove_op(op);
1179 return err;
1180 }
1181 }
1182
1183 return msg_head->nframes * CFSIZ + MHSIZ;
1184}
1185
1186/*
1187 * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1188 */
1189static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
1190{
1191 struct sk_buff *skb;
1192 struct net_device *dev;
1193 int err;
1194
1195 /* we need a real device to send frames */
1196 if (!ifindex)
1197 return -ENODEV;
1198
1199 skb = alloc_skb(CFSIZ, GFP_KERNEL);
1200
1201 if (!skb)
1202 return -ENOMEM;
1203
1204 err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ);
1205 if (err < 0) {
1206 kfree_skb(skb);
1207 return err;
1208 }
1209
1210 dev = dev_get_by_index(&init_net, ifindex);
1211 if (!dev) {
1212 kfree_skb(skb);
1213 return -ENODEV;
1214 }
1215
1216 skb->dev = dev;
1217 skb->sk = sk;
Oliver Hartkopp7f2d38e2008-07-05 23:38:43 -07001218 err = can_send(skb, 1); /* send with loopback */
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001219 dev_put(dev);
1220
Oliver Hartkopp7f2d38e2008-07-05 23:38:43 -07001221 if (err)
1222 return err;
1223
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001224 return CFSIZ + MHSIZ;
1225}
1226
1227/*
1228 * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1229 */
1230static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
1231 struct msghdr *msg, size_t size)
1232{
1233 struct sock *sk = sock->sk;
1234 struct bcm_sock *bo = bcm_sk(sk);
1235 int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
1236 struct bcm_msg_head msg_head;
1237 int ret; /* read bytes or error codes as return value */
1238
1239 if (!bo->bound)
1240 return -ENOTCONN;
1241
Oliver Hartkopp7f2d38e2008-07-05 23:38:43 -07001242 /* check for valid message length from userspace */
1243 if (size < MHSIZ || (size - MHSIZ) % CFSIZ)
1244 return -EINVAL;
1245
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001246 /* check for alternative ifindex for this bcm_op */
1247
1248 if (!ifindex && msg->msg_name) {
1249 /* no bound device as default => check msg_name */
1250 struct sockaddr_can *addr =
1251 (struct sockaddr_can *)msg->msg_name;
1252
1253 if (addr->can_family != AF_CAN)
1254 return -EINVAL;
1255
1256 /* ifindex from sendto() */
1257 ifindex = addr->can_ifindex;
1258
1259 if (ifindex) {
1260 struct net_device *dev;
1261
1262 dev = dev_get_by_index(&init_net, ifindex);
1263 if (!dev)
1264 return -ENODEV;
1265
1266 if (dev->type != ARPHRD_CAN) {
1267 dev_put(dev);
1268 return -ENODEV;
1269 }
1270
1271 dev_put(dev);
1272 }
1273 }
1274
1275 /* read message head information */
1276
1277 ret = memcpy_fromiovec((u8 *)&msg_head, msg->msg_iov, MHSIZ);
1278 if (ret < 0)
1279 return ret;
1280
1281 lock_sock(sk);
1282
1283 switch (msg_head.opcode) {
1284
1285 case TX_SETUP:
1286 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
1287 break;
1288
1289 case RX_SETUP:
1290 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
1291 break;
1292
1293 case TX_DELETE:
1294 if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
1295 ret = MHSIZ;
1296 else
1297 ret = -EINVAL;
1298 break;
1299
1300 case RX_DELETE:
1301 if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
1302 ret = MHSIZ;
1303 else
1304 ret = -EINVAL;
1305 break;
1306
1307 case TX_READ:
1308 /* reuse msg_head for the reply to TX_READ */
1309 msg_head.opcode = TX_STATUS;
1310 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
1311 break;
1312
1313 case RX_READ:
1314 /* reuse msg_head for the reply to RX_READ */
1315 msg_head.opcode = RX_STATUS;
1316 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
1317 break;
1318
1319 case TX_SEND:
Oliver Hartkopp7f2d38e2008-07-05 23:38:43 -07001320 /* we need exactly one can_frame behind the msg head */
1321 if ((msg_head.nframes != 1) || (size != CFSIZ + MHSIZ))
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001322 ret = -EINVAL;
1323 else
1324 ret = bcm_tx_send(msg, ifindex, sk);
1325 break;
1326
1327 default:
1328 ret = -EINVAL;
1329 break;
1330 }
1331
1332 release_sock(sk);
1333
1334 return ret;
1335}
1336
1337/*
1338 * notification handler for netdevice status changes
1339 */
1340static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
1341 void *data)
1342{
1343 struct net_device *dev = (struct net_device *)data;
1344 struct bcm_sock *bo = container_of(nb, struct bcm_sock, notifier);
1345 struct sock *sk = &bo->sk;
1346 struct bcm_op *op;
1347 int notify_enodev = 0;
1348
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -07001349 if (!net_eq(dev_net(dev), &init_net))
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001350 return NOTIFY_DONE;
1351
1352 if (dev->type != ARPHRD_CAN)
1353 return NOTIFY_DONE;
1354
1355 switch (msg) {
1356
1357 case NETDEV_UNREGISTER:
1358 lock_sock(sk);
1359
1360 /* remove device specific receive entries */
1361 list_for_each_entry(op, &bo->rx_ops, list)
1362 if (op->rx_reg_dev == dev)
1363 bcm_rx_unreg(dev, op);
1364
1365 /* remove device reference, if this is our bound device */
1366 if (bo->bound && bo->ifindex == dev->ifindex) {
1367 bo->bound = 0;
1368 bo->ifindex = 0;
1369 notify_enodev = 1;
1370 }
1371
1372 release_sock(sk);
1373
1374 if (notify_enodev) {
1375 sk->sk_err = ENODEV;
1376 if (!sock_flag(sk, SOCK_DEAD))
1377 sk->sk_error_report(sk);
1378 }
1379 break;
1380
1381 case NETDEV_DOWN:
1382 if (bo->bound && bo->ifindex == dev->ifindex) {
1383 sk->sk_err = ENETDOWN;
1384 if (!sock_flag(sk, SOCK_DEAD))
1385 sk->sk_error_report(sk);
1386 }
1387 }
1388
1389 return NOTIFY_DONE;
1390}
1391
1392/*
1393 * initial settings for all BCM sockets to be set at socket creation time
1394 */
1395static int bcm_init(struct sock *sk)
1396{
1397 struct bcm_sock *bo = bcm_sk(sk);
1398
1399 bo->bound = 0;
1400 bo->ifindex = 0;
1401 bo->dropped_usr_msgs = 0;
1402 bo->bcm_proc_read = NULL;
1403
1404 INIT_LIST_HEAD(&bo->tx_ops);
1405 INIT_LIST_HEAD(&bo->rx_ops);
1406
1407 /* set notifier */
1408 bo->notifier.notifier_call = bcm_notifier;
1409
1410 register_netdevice_notifier(&bo->notifier);
1411
1412 return 0;
1413}
1414
1415/*
1416 * standard socket functions
1417 */
1418static int bcm_release(struct socket *sock)
1419{
1420 struct sock *sk = sock->sk;
1421 struct bcm_sock *bo = bcm_sk(sk);
1422 struct bcm_op *op, *next;
1423
1424 /* remove bcm_ops, timer, rx_unregister(), etc. */
1425
1426 unregister_netdevice_notifier(&bo->notifier);
1427
1428 lock_sock(sk);
1429
1430 list_for_each_entry_safe(op, next, &bo->tx_ops, list)
1431 bcm_remove_op(op);
1432
1433 list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
1434 /*
1435 * Don't care if we're bound or not (due to netdev problems)
1436 * can_rx_unregister() is always a save thing to do here.
1437 */
1438 if (op->ifindex) {
1439 /*
1440 * Only remove subscriptions that had not
1441 * been removed due to NETDEV_UNREGISTER
1442 * in bcm_notifier()
1443 */
1444 if (op->rx_reg_dev) {
1445 struct net_device *dev;
1446
1447 dev = dev_get_by_index(&init_net, op->ifindex);
1448 if (dev) {
1449 bcm_rx_unreg(dev, op);
1450 dev_put(dev);
1451 }
1452 }
1453 } else
1454 can_rx_unregister(NULL, op->can_id,
1455 REGMASK(op->can_id),
1456 bcm_rx_handler, op);
1457
1458 bcm_remove_op(op);
1459 }
1460
1461 /* remove procfs entry */
1462 if (proc_dir && bo->bcm_proc_read)
1463 remove_proc_entry(bo->procname, proc_dir);
1464
1465 /* remove device reference */
1466 if (bo->bound) {
1467 bo->bound = 0;
1468 bo->ifindex = 0;
1469 }
1470
1471 release_sock(sk);
1472 sock_put(sk);
1473
1474 return 0;
1475}
1476
1477static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1478 int flags)
1479{
1480 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1481 struct sock *sk = sock->sk;
1482 struct bcm_sock *bo = bcm_sk(sk);
1483
1484 if (bo->bound)
1485 return -EISCONN;
1486
1487 /* bind a device to this socket */
1488 if (addr->can_ifindex) {
1489 struct net_device *dev;
1490
1491 dev = dev_get_by_index(&init_net, addr->can_ifindex);
1492 if (!dev)
1493 return -ENODEV;
1494
1495 if (dev->type != ARPHRD_CAN) {
1496 dev_put(dev);
1497 return -ENODEV;
1498 }
1499
1500 bo->ifindex = dev->ifindex;
1501 dev_put(dev);
1502
1503 } else {
1504 /* no interface reference for ifindex = 0 ('any' CAN device) */
1505 bo->ifindex = 0;
1506 }
1507
1508 bo->bound = 1;
1509
1510 if (proc_dir) {
1511 /* unique socket address as filename */
1512 sprintf(bo->procname, "%p", sock);
1513 bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
1514 proc_dir,
1515 bcm_read_proc, sk);
1516 }
1517
1518 return 0;
1519}
1520
1521static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
1522 struct msghdr *msg, size_t size, int flags)
1523{
1524 struct sock *sk = sock->sk;
1525 struct sk_buff *skb;
1526 int error = 0;
1527 int noblock;
1528 int err;
1529
1530 noblock = flags & MSG_DONTWAIT;
1531 flags &= ~MSG_DONTWAIT;
1532 skb = skb_recv_datagram(sk, flags, noblock, &error);
1533 if (!skb)
1534 return error;
1535
1536 if (skb->len < size)
1537 size = skb->len;
1538
1539 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
1540 if (err < 0) {
1541 skb_free_datagram(sk, skb);
1542 return err;
1543 }
1544
1545 sock_recv_timestamp(msg, sk, skb);
1546
1547 if (msg->msg_name) {
1548 msg->msg_namelen = sizeof(struct sockaddr_can);
1549 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1550 }
1551
1552 skb_free_datagram(sk, skb);
1553
1554 return size;
1555}
1556
1557static struct proto_ops bcm_ops __read_mostly = {
1558 .family = PF_CAN,
1559 .release = bcm_release,
1560 .bind = sock_no_bind,
1561 .connect = bcm_connect,
1562 .socketpair = sock_no_socketpair,
1563 .accept = sock_no_accept,
1564 .getname = sock_no_getname,
1565 .poll = datagram_poll,
1566 .ioctl = NULL, /* use can_ioctl() from af_can.c */
1567 .listen = sock_no_listen,
1568 .shutdown = sock_no_shutdown,
1569 .setsockopt = sock_no_setsockopt,
1570 .getsockopt = sock_no_getsockopt,
1571 .sendmsg = bcm_sendmsg,
1572 .recvmsg = bcm_recvmsg,
1573 .mmap = sock_no_mmap,
1574 .sendpage = sock_no_sendpage,
1575};
1576
1577static struct proto bcm_proto __read_mostly = {
1578 .name = "CAN_BCM",
1579 .owner = THIS_MODULE,
1580 .obj_size = sizeof(struct bcm_sock),
1581 .init = bcm_init,
1582};
1583
1584static struct can_proto bcm_can_proto __read_mostly = {
1585 .type = SOCK_DGRAM,
1586 .protocol = CAN_BCM,
1587 .capability = -1,
1588 .ops = &bcm_ops,
1589 .prot = &bcm_proto,
1590};
1591
1592static int __init bcm_module_init(void)
1593{
1594 int err;
1595
1596 printk(banner);
1597
1598 err = can_proto_register(&bcm_can_proto);
1599 if (err < 0) {
1600 printk(KERN_ERR "can: registration of bcm protocol failed\n");
1601 return err;
1602 }
1603
1604 /* create /proc/net/can-bcm directory */
1605 proc_dir = proc_mkdir("can-bcm", init_net.proc_net);
1606
1607 if (proc_dir)
1608 proc_dir->owner = THIS_MODULE;
1609
1610 return 0;
1611}
1612
1613static void __exit bcm_module_exit(void)
1614{
1615 can_proto_unregister(&bcm_can_proto);
1616
1617 if (proc_dir)
1618 proc_net_remove(&init_net, "can-bcm");
1619}
1620
1621module_init(bcm_module_init);
1622module_exit(bcm_module_exit);