]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - kernel/2.6/drivers/net/can/dev.c
Make CAN bit-timing calculation configurable
[socketcan-devel.git] / kernel / 2.6 / drivers / net / can / dev.c
1 /*
2  * $Id$
3  *
4  * Copyright (C) 2005 Marc Kleine-Budde, Pengutronix
5  * Copyright (C) 2006 Andrey Volkov, Varma Electronics
6  * Copyright (C) 2008 Wolfgang Grandegger <wg@grandegger.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the version 2 of the GNU General Public License
10  * as published by the Free Software Foundation
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include <linux/module.h>
23 #include <linux/netdevice.h>
24 #include <linux/if_arp.h>
25 #include <linux/can.h>
26 #include <linux/can/dev.h>
27 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
28 #include <net/rtnetlink.h>
29 #endif
30
31 #include "sysfs.h"
32
33 #define MOD_DESC "CAN netdevice library"
34
35 MODULE_DESCRIPTION(MOD_DESC);
36 MODULE_LICENSE("GPL v2");
37 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
38
39 #ifdef CONFIG_CAN_CALC_BITTIMING
40 #define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
41
42 /*
43  * Bit-timing calculation derived from:
44  *
45  * Code based on LinCAN sources and H8S2638 project
46  * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
47  * Copyright 2005      Stanislav Marek
48  * email: pisa@cmp.felk.cvut.cz
49  */
50 static int can_update_spt(const struct can_bittiming_const *btc,
51                           int sampl_pt, int tseg, int *tseg1, int *tseg2)
52 {
53         *tseg2 = tseg + 1 - (sampl_pt * (tseg + 1)) / 1000;
54         if (*tseg2 < btc->tseg2_min)
55                 *tseg2 = btc->tseg2_min;
56         if (*tseg2 > btc->tseg2_max)
57                 *tseg2 = btc->tseg2_max;
58         *tseg1 = tseg - *tseg2;
59         if (*tseg1 > btc->tseg1_max) {
60                 *tseg1 = btc->tseg1_max;
61                 *tseg2 = tseg - *tseg1;
62         }
63         return 1000 * (tseg + 1 - *tseg2) / (tseg + 1);
64 }
65
66 static int can_calc_bittiming(struct net_device *dev)
67 {
68         struct can_priv *priv = netdev_priv(dev);
69         struct can_bittiming *bt = &priv->bittiming;
70         const struct can_bittiming_const *btc = priv->bittiming_const;
71         long rate, best_rate = 0;
72         long best_error = 1000000000, error = 0;
73         int best_tseg = 0, best_brp = 0, brp = 0;
74         int tsegall, tseg = 0, tseg1 = 0, tseg2 = 0;
75         int spt_error = 1000, spt = 0, sampl_pt;
76         uint64_t v64;
77
78         if (!priv->bittiming_const)
79                 return -ENOTSUPP;
80
81         /* Use CIA recommended sample points */
82         if (bt->sample_point) {
83                 sampl_pt = bt->sample_point;
84         } else {
85                 if (bt->bitrate > 800000)
86                         sampl_pt = 750;
87                 else if (bt->bitrate > 500000)
88                         sampl_pt = 800;
89                 else
90                         sampl_pt = 875;
91         }
92
93         /* tseg even = round down, odd = round up */
94         for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
95              tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
96                 tsegall = 1 + tseg / 2;
97                 /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
98                 brp = bt->clock / (tsegall * bt->bitrate) + tseg % 2;
99                 /* chose brp step which is possible in system */
100                 brp = (brp / btc->brp_inc) * btc->brp_inc;
101                 if ((brp < btc->brp_min) || (brp > btc->brp_max))
102                         continue;
103                 rate = bt->clock / (brp * tsegall);
104                 error = bt->bitrate - rate;
105                 /* tseg brp biterror */
106                 if (error < 0)
107                         error = -error;
108                 if (error > best_error)
109                         continue;
110                 best_error = error;
111                 if (error == 0) {
112                         spt = can_update_spt(btc, sampl_pt, tseg / 2,
113                                              &tseg1, &tseg2);
114                         error = sampl_pt - spt;
115                         if (error < 0)
116                                 error = -error;
117                         if (error > spt_error)
118                                 continue;
119                         spt_error = error;
120                 }
121                 best_tseg = tseg / 2;
122                 best_brp = brp;
123                 best_rate = rate;
124                 if (error == 0)
125                         break;
126         }
127
128         if (best_error) {
129                 /* Error in one-tenth of a percent */
130                 error = (best_error * 1000) / bt->bitrate;
131                 if (error > CAN_CALC_MAX_ERROR) {
132                         dev_err(ND2D(dev), "bitrate error %ld.%ld%% too high\n",
133                                 error / 10, error % 10);
134                         return -EDOM;
135                 } else {
136                         dev_warn(ND2D(dev), "bitrate error %ld.%ld%%\n",
137                                  error / 10, error % 10);
138                 }
139         }
140
141         spt = can_update_spt(btc, sampl_pt, best_tseg, &tseg1, &tseg2);
142
143         v64 = (u64)best_brp * 1000000000UL;
144         do_div(v64, bt->clock);
145         bt->tq = (u32)v64;
146         bt->prop_seg = tseg1 / 2;
147         bt->phase_seg1 = tseg1 - bt->prop_seg;
148         bt->phase_seg2 = tseg2;
149         bt->sjw = 1;
150         bt->brp = best_brp;
151
152         return 0;
153 }
154 #else /* !CONFIG_CAN_CALC_BITTIMING */
155 static int can_calc_bittiming(struct net_device *dev)
156 {
157         return -EINVAL;
158 }
159 #endif /* CONFIG_CAN_CALC_BITTIMING */
160
161 int can_sample_point(struct can_bittiming *bt)
162 {
163         return ((bt->prop_seg + bt->phase_seg1 + 1) * 1000) /
164                 (bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1);
165 }
166
167 int can_fixup_bittiming(struct net_device *dev)
168 {
169         struct can_priv *priv = netdev_priv(dev);
170         struct can_bittiming *bt = &priv->bittiming;
171         const struct can_bittiming_const *btc = priv->bittiming_const;
172         int tseg1, alltseg;
173         u32 bitrate;
174         u64 brp64;
175
176         if (!priv->bittiming_const)
177                 return -ENOTSUPP;
178
179         tseg1 = bt->prop_seg + bt->phase_seg1;
180         if (bt->sjw > btc->sjw_max ||
181             tseg1 < btc->tseg1_min || tseg1 > btc->tseg1_max ||
182             bt->phase_seg2 < btc->tseg2_min || bt->phase_seg2 > btc->tseg2_max)
183                 return -EINVAL;
184
185         brp64 = (u64)bt->clock * (u64)bt->tq;
186         if (btc->brp_inc > 1)
187                 do_div(brp64, btc->brp_inc);
188         brp64 += 500000000UL - 1;
189         do_div(brp64, 1000000000UL); /* the practicable BRP */
190         if (btc->brp_inc > 1)
191                 brp64 *= btc->brp_inc;
192         bt->brp = (u32)brp64;
193
194         if (bt->brp < btc->brp_min || bt->brp > btc->brp_max)
195                 return -EINVAL;
196
197         alltseg = bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1;
198         bitrate = bt->clock / (bt->brp * alltseg);
199         bt->bitrate = bitrate;
200
201         return 0;
202 }
203
204 int can_set_bittiming(struct net_device *dev)
205 {
206         struct can_priv *priv = netdev_priv(dev);
207         int err;
208
209         /* Check if bit-timing parameters have been pre-defined */
210         if (!priv->bittiming.tq && !priv->bittiming.bitrate)
211                 return -EINVAL;
212
213         /* Check if the CAN device has bit-timing parameters */
214         if (priv->bittiming_const) {
215
216                 /* Check if bit-timing parameters have already been set */
217                 if (priv->bittiming.tq && priv->bittiming.bitrate)
218                         return 0;
219
220                 /* Non-expert mode? Check if the bitrate has been pre-defined */
221                 if (!priv->bittiming.tq)
222                         /* Determine bit-timing parameters */
223                         err = can_calc_bittiming(dev);
224                 else
225                         /* Check bit-timing params and calculate proper brp */
226                         err = can_fixup_bittiming(dev);
227                 if (err)
228                         return err;
229         }
230
231         if (priv->do_set_bittiming) {
232                 /* Finally, set the bit-timing registers */
233                 err = priv->do_set_bittiming(dev);
234                 if (err)
235                         return err;
236         }
237
238         return 0;
239 }
240 EXPORT_SYMBOL(can_set_bittiming);
241
242 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
243 struct net_device_stats *can_get_stats(struct net_device *dev)
244 {
245         struct can_priv *priv = netdev_priv(dev);
246
247         return &priv->net_stats;
248 }
249 EXPORT_SYMBOL(can_get_stats);
250 #endif
251
252 static void can_setup(struct net_device *dev)
253 {
254         dev->type = ARPHRD_CAN;
255         dev->mtu = sizeof(struct can_frame);
256         dev->hard_header_len = 0;
257         dev->addr_len = 0;
258         dev->tx_queue_len = 10;
259
260         /* New-style flags. */
261         dev->flags = IFF_NOARP;
262         dev->features = NETIF_F_NO_CSUM;
263 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
264         dev->get_stats = can_get_stats;
265 #endif
266 }
267
268 /*
269  * Function  alloc_candev
270  *      Allocates and sets up an CAN device
271  */
272 struct net_device *alloc_candev(int sizeof_priv)
273 {
274         struct net_device *dev;
275         struct can_priv *priv;
276
277         dev = alloc_netdev(sizeof_priv, "can%d", can_setup);
278         if (!dev)
279                 return NULL;
280
281         priv = netdev_priv(dev);
282
283         priv->state = CAN_STATE_STOPPED;
284         spin_lock_init(&priv->irq_lock);
285
286         init_timer(&priv->timer);
287         priv->timer.expires = 0;
288
289         return dev;
290 }
291 EXPORT_SYMBOL(alloc_candev);
292
293 void free_candev(struct net_device *dev)
294 {
295         free_netdev(dev);
296 }
297 EXPORT_SYMBOL(free_candev);
298
299 int register_candev(struct net_device *dev)
300 {
301         int err;
302
303         err = register_netdev(dev);
304         if (err)
305                 return err;
306
307 #ifdef CONFIG_SYSFS
308         can_create_sysfs(dev);
309 #endif
310         return 0;
311 }
312 EXPORT_SYMBOL(register_candev);
313
314 void unregister_candev(struct net_device *dev)
315 {
316 #ifdef CONFIG_SYSFS
317         can_remove_sysfs(dev);
318 #endif
319         unregister_netdev(dev);
320 }
321 EXPORT_SYMBOL(unregister_candev);
322
323 /*
324  * Local echo of CAN messages
325  *
326  * CAN network devices *should* support a local echo functionality
327  * (see Documentation/networking/can.txt). To test the handling of CAN
328  * interfaces that do not support the local echo both driver types are
329  * implemented. In the case that the driver does not support the echo
330  * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
331  * to perform the echo as a fallback solution.
332  */
333
334 void can_flush_echo_skb(struct net_device *dev)
335 {
336         struct can_priv *priv = netdev_priv(dev);
337 #ifdef FIXME
338 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
339         struct net_device_stats *stats = can_get_stats(dev);
340 #else
341         struct net_device_stats *stats = &dev->stats;
342 #endif
343 #endif
344         int i;
345
346         for (i = 0; i < CAN_ECHO_SKB_MAX; i++) {
347                 if (priv->echo_skb[i]) {
348                         kfree_skb(priv->echo_skb[i]);
349                         priv->echo_skb[i] = NULL;
350 #ifdef FIXME
351                         stats->tx_dropped++;
352                         stats->tx_aborted_errors++;
353 #endif
354                 }
355         }
356 }
357
358 void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev, int idx)
359 {
360         struct can_priv *priv = netdev_priv(dev);
361
362         /* set flag whether this packet has to be looped back */
363         if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK) {
364                 kfree_skb(skb);
365                 return;
366         }
367
368         if (!priv->echo_skb[idx]) {
369                 struct sock *srcsk = skb->sk;
370
371                 if (atomic_read(&skb->users) != 1) {
372                         struct sk_buff *old_skb = skb;
373
374                         skb = skb_clone(old_skb, GFP_ATOMIC);
375                         kfree_skb(old_skb);
376                         if (!skb)
377                                 return;
378                 } else
379                         skb_orphan(skb);
380
381                 skb->sk = srcsk;
382
383                 /* make settings for echo to reduce code in irq context */
384                 skb->protocol = htons(ETH_P_CAN);
385                 skb->pkt_type = PACKET_BROADCAST;
386                 skb->ip_summed = CHECKSUM_UNNECESSARY;
387                 skb->dev = dev;
388
389                 /* save this skb for tx interrupt echo handling */
390                 priv->echo_skb[idx] = skb;
391         } else {
392                 /* locking problem with netif_stop_queue() ?? */
393                 printk(KERN_ERR "%s: %s: BUG! echo_skb is occupied!\n",
394                        dev->name, __func__);
395                 kfree_skb(skb);
396         }
397 }
398 EXPORT_SYMBOL(can_put_echo_skb);
399
400 void can_get_echo_skb(struct net_device *dev, int idx)
401 {
402         struct can_priv *priv = netdev_priv(dev);
403
404         if ((dev->flags & IFF_ECHO) && priv->echo_skb[idx]) {
405                 netif_rx(priv->echo_skb[idx]);
406                 priv->echo_skb[idx] = NULL;
407         }
408 }
409 EXPORT_SYMBOL(can_get_echo_skb);
410
411 /*
412  * CAN bus-off handling
413  * FIXME: we need some synchronization
414  */
415 int can_restart_now(struct net_device *dev)
416 {
417         struct can_priv *priv = netdev_priv(dev);
418 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
419         struct net_device_stats *stats = can_get_stats(dev);
420 #else
421         struct net_device_stats *stats = &dev->stats;
422 #endif
423         struct sk_buff *skb;
424         struct can_frame *cf;
425         int err;
426
427         if (netif_carrier_ok(dev))
428                 netif_carrier_off(dev);
429
430         /* Cancel restart in progress */
431         if (priv->timer.expires) {
432                 del_timer(&priv->timer);
433                 priv->timer.expires = 0; /* mark inactive timer */
434         }
435
436         can_flush_echo_skb(dev);
437
438         err = priv->do_set_mode(dev, CAN_MODE_START);
439         if (err)
440                 return err;
441
442         netif_carrier_on(dev);
443
444         priv->can_stats.restarts++;
445
446         /* send restart message upstream */
447         skb = dev_alloc_skb(sizeof(struct can_frame));
448         if (skb == NULL)
449                 return -ENOMEM;
450         skb->dev = dev;
451         skb->protocol = htons(ETH_P_CAN);
452         cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
453         memset(cf, 0, sizeof(struct can_frame));
454         cf->can_id = CAN_ERR_FLAG | CAN_ERR_RESTARTED;
455         cf->can_dlc = CAN_ERR_DLC;
456
457         netif_rx(skb);
458
459         dev->last_rx = jiffies;
460         stats->rx_packets++;
461         stats->rx_bytes += cf->can_dlc;
462
463         return 0;
464 }
465
466 static void can_restart_after(unsigned long data)
467 {
468         struct net_device *dev = (struct net_device *)data;
469         struct can_priv *priv = netdev_priv(dev);
470
471         priv->timer.expires = 0; /* mark inactive timer */
472         can_restart_now(dev);
473 }
474
475 void can_bus_off(struct net_device *dev)
476 {
477         struct can_priv *priv = netdev_priv(dev);
478
479         netif_carrier_off(dev);
480
481         if (priv->restart_ms > 0 && !priv->timer.expires) {
482
483                 priv->timer.function = can_restart_after;
484                 priv->timer.data = (unsigned long)dev;
485                 priv->timer.expires =
486                         jiffies + (priv->restart_ms * HZ) / 1000;
487                 add_timer(&priv->timer);
488         }
489 }
490 EXPORT_SYMBOL(can_bus_off);
491
492 void can_close_cleanup(struct net_device *dev)
493 {
494         struct can_priv *priv = netdev_priv(dev);
495
496         if (priv->timer.expires) {
497                 del_timer(&priv->timer);
498                 priv->timer.expires = 0;
499         }
500
501         can_flush_echo_skb(dev);
502 }
503 EXPORT_SYMBOL(can_close_cleanup);
504
505 static __init int can_dev_init(void)
506 {
507         printk(KERN_INFO MOD_DESC "\n");
508
509         return 0;
510 }
511 module_init(can_dev_init);
512
513 static __exit void can_dev_exit(void)
514 {
515 }
516 module_exit(can_dev_exit);