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