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