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