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