]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - kernel/2.6/drivers/net/can/dev.c
dev: add can_get_echo_skb() for cleanup purposes
[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 device driver interface"
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         u64 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         dev_err(ND2D(dev), "bit-timing calculation not available\n");
158         return -EINVAL;
159 }
160 #endif /* CONFIG_CAN_CALC_BITTIMING */
161
162 int can_sample_point(struct can_bittiming *bt)
163 {
164         return ((bt->prop_seg + bt->phase_seg1 + 1) * 1000) /
165                 (bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1);
166 }
167
168 static int can_fixup_bittiming(struct net_device *dev)
169 {
170         struct can_priv *priv = netdev_priv(dev);
171         struct can_bittiming *bt = &priv->bittiming;
172         const struct can_bittiming_const *btc = priv->bittiming_const;
173         int tseg1, alltseg;
174         u32 bitrate;
175         u64 brp64;
176
177         if (!priv->bittiming_const)
178                 return -ENOTSUPP;
179
180         tseg1 = bt->prop_seg + bt->phase_seg1;
181         if (bt->sjw > btc->sjw_max ||
182             tseg1 < btc->tseg1_min || tseg1 > btc->tseg1_max ||
183             bt->phase_seg2 < btc->tseg2_min || bt->phase_seg2 > btc->tseg2_max)
184                 return -EINVAL;
185
186         brp64 = (u64)bt->clock * (u64)bt->tq;
187         if (btc->brp_inc > 1)
188                 do_div(brp64, btc->brp_inc);
189         brp64 += 500000000UL - 1;
190         do_div(brp64, 1000000000UL); /* the practicable BRP */
191         if (btc->brp_inc > 1)
192                 brp64 *= btc->brp_inc;
193         bt->brp = (u32)brp64;
194
195         if (bt->brp < btc->brp_min || bt->brp > btc->brp_max)
196                 return -EINVAL;
197
198         alltseg = bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1;
199         bitrate = bt->clock / (bt->brp * alltseg);
200         bt->bitrate = bitrate;
201
202         return 0;
203 }
204
205 /*
206  * Set CAN bit-timing for the device
207  *
208  * This functions should be called in the open function of the device
209  * driver to determine, check and set appropriate bit-timing parameters.
210  */
211 int can_set_bittiming(struct net_device *dev)
212 {
213         struct can_priv *priv = netdev_priv(dev);
214         int err;
215
216         /* Check if bit-timing parameters have been pre-defined */
217         if (!priv->bittiming.tq && !priv->bittiming.bitrate) {
218                 dev_err(ND2D(dev), "bit-timing not yet defined\n");
219                 return -EINVAL;
220         }
221
222         /* Check if the CAN device has bit-timing parameters */
223         if (priv->bittiming_const) {
224
225                 /* Check if bit-timing parameters have already been set */
226                 if (priv->bittiming.tq && priv->bittiming.bitrate)
227                         return 0;
228
229                 /* Non-expert mode? Check if the bitrate has been pre-defined */
230                 if (!priv->bittiming.tq)
231                         /* Determine bit-timing parameters */
232                         err = can_calc_bittiming(dev);
233                 else
234                         /* Check bit-timing params and calculate proper brp */
235                         err = can_fixup_bittiming(dev);
236                 if (err)
237                         return err;
238         }
239
240         if (priv->do_set_bittiming) {
241                 /* Finally, set the bit-timing registers */
242                 err = priv->do_set_bittiming(dev);
243                 if (err)
244                         return err;
245         }
246
247         return 0;
248 }
249 EXPORT_SYMBOL_GPL(can_set_bittiming);
250
251 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
252 struct net_device_stats *can_get_stats(struct net_device *dev)
253 {
254         struct can_priv *priv = netdev_priv(dev);
255
256         return &priv->net_stats;
257 }
258 EXPORT_SYMBOL_GPL(can_get_stats);
259 #endif
260
261 static void can_setup(struct net_device *dev)
262 {
263         dev->type = ARPHRD_CAN;
264         dev->mtu = sizeof(struct can_frame);
265         dev->hard_header_len = 0;
266         dev->addr_len = 0;
267         dev->tx_queue_len = 10;
268
269         /* New-style flags. */
270         dev->flags = IFF_NOARP;
271         dev->features = NETIF_F_NO_CSUM;
272 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
273         dev->get_stats = can_get_stats;
274 #endif
275 }
276
277 /*
278  * Allocate and setup space for the CAN network device
279  */
280 struct net_device *alloc_candev(int sizeof_priv)
281 {
282         struct net_device *dev;
283         struct can_priv *priv;
284
285         dev = alloc_netdev(sizeof_priv, "can%d", can_setup);
286         if (!dev)
287                 return NULL;
288
289         priv = netdev_priv(dev);
290
291         priv->state = CAN_STATE_STOPPED;
292         spin_lock_init(&priv->irq_lock);
293
294         init_timer(&priv->timer);
295         priv->timer.expires = 0;
296
297         return dev;
298 }
299 EXPORT_SYMBOL_GPL(alloc_candev);
300
301 /*
302  * Allocate space of the CAN network device
303  */
304 void free_candev(struct net_device *dev)
305 {
306         free_netdev(dev);
307 }
308 EXPORT_SYMBOL_GPL(free_candev);
309
310 /*
311  * Register the CAN network device
312  */
313 int register_candev(struct net_device *dev)
314 {
315         int err;
316
317         err = register_netdev(dev);
318         if (err)
319                 return err;
320
321         can_create_sysfs(dev);
322
323         return 0;
324 }
325 EXPORT_SYMBOL_GPL(register_candev);
326
327 /*
328  * Unregister the CAN network device
329  */
330 void unregister_candev(struct net_device *dev)
331 {
332         can_remove_sysfs(dev);
333         unregister_netdev(dev);
334 }
335 EXPORT_SYMBOL_GPL(unregister_candev);
336
337 /*
338  * Local echo of CAN messages
339  *
340  * CAN network devices *should* support a local echo functionality
341  * (see Documentation/networking/can.txt). To test the handling of CAN
342  * interfaces that do not support the local echo both driver types are
343  * implemented. In the case that the driver does not support the echo
344  * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
345  * to perform the echo as a fallback solution.
346  */
347
348 static void can_flush_echo_skb(struct net_device *dev)
349 {
350         struct can_priv *priv = netdev_priv(dev);
351 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
352         struct net_device_stats *stats = can_get_stats(dev);
353 #else
354         struct net_device_stats *stats = &dev->stats;
355 #endif
356         int i;
357
358         for (i = 0; i < CAN_ECHO_SKB_MAX; i++) {
359                 if (priv->echo_skb[i]) {
360                         kfree_skb(priv->echo_skb[i]);
361                         priv->echo_skb[i] = NULL;
362                         stats->tx_dropped++;
363                         stats->tx_aborted_errors++;
364                 }
365         }
366 }
367
368 /*
369  * Put the skb on the stack to be looped backed locally lateron
370  *
371  * The function is typically called in the start_xmit function
372  * of the device driver.
373  */
374 void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev, int idx)
375 {
376         struct can_priv *priv = netdev_priv(dev);
377
378         /* set flag whether this packet has to be looped back */
379         if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK) {
380                 kfree_skb(skb);
381                 return;
382         }
383
384         if (!priv->echo_skb[idx]) {
385                 struct sock *srcsk = skb->sk;
386
387                 if (atomic_read(&skb->users) != 1) {
388                         struct sk_buff *old_skb = skb;
389
390                         skb = skb_clone(old_skb, GFP_ATOMIC);
391                         kfree_skb(old_skb);
392                         if (!skb)
393                                 return;
394                 } else
395                         skb_orphan(skb);
396
397                 skb->sk = srcsk;
398
399                 /* make settings for echo to reduce code in irq context */
400                 skb->protocol = htons(ETH_P_CAN);
401                 skb->pkt_type = PACKET_BROADCAST;
402                 skb->ip_summed = CHECKSUM_UNNECESSARY;
403                 skb->dev = dev;
404
405                 /* save this skb for tx interrupt echo handling */
406                 priv->echo_skb[idx] = skb;
407         } else {
408                 /* locking problem with netif_stop_queue() ?? */
409                 printk(KERN_ERR "%s: %s: BUG! echo_skb is occupied!\n",
410                        dev->name, __func__);
411                 kfree_skb(skb);
412         }
413 }
414 EXPORT_SYMBOL_GPL(can_put_echo_skb);
415
416 /*
417  * Get the skb from the stack and loop it back locally
418  *
419  * The function is typically called when the TX done interrupt
420  * is handled in the device driver.
421  */
422 void can_get_echo_skb(struct net_device *dev, int idx)
423 {
424         struct can_priv *priv = netdev_priv(dev);
425
426         if (priv->echo_skb[idx]) {
427                 netif_rx(priv->echo_skb[idx]);
428                 priv->echo_skb[idx] = NULL;
429         }
430 }
431 EXPORT_SYMBOL_GPL(can_get_echo_skb);
432
433 /*
434   * Remove the skb from the stack and free it.
435   *
436   * The function is typically called when TX failed.
437   */
438 void can_free_echo_skb(struct net_device *dev, int idx)
439 {
440         struct can_priv *priv = netdev_priv(dev);
441
442         if (priv->echo_skb[idx]) {
443                 kfree_skb(priv->echo_skb[idx]);
444                 priv->echo_skb[idx] = NULL;
445         }
446 }
447 EXPORT_SYMBOL_GPL(can_free_echo_skb);
448
449 /*
450  * CAN device restart for bus-off recovery
451  */
452 int can_restart_now(struct net_device *dev)
453 {
454         struct can_priv *priv = netdev_priv(dev);
455 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
456         struct net_device_stats *stats = can_get_stats(dev);
457 #else
458         struct net_device_stats *stats = &dev->stats;
459 #endif
460         struct sk_buff *skb;
461         struct can_frame *cf;
462         int err;
463
464         if (netif_carrier_ok(dev))
465                 netif_carrier_off(dev);
466
467         /* Cancel restart in progress */
468         if (priv->timer.expires) {
469                 del_timer(&priv->timer);
470                 priv->timer.expires = 0; /* mark inactive timer */
471         }
472
473         can_flush_echo_skb(dev);
474
475         err = priv->do_set_mode(dev, CAN_MODE_START);
476         if (err)
477                 return err;
478
479         netif_carrier_on(dev);
480
481         dev_dbg(ND2D(dev), "restarted\n");
482         priv->can_stats.restarts++;
483
484         /* send restart message upstream */
485         skb = dev_alloc_skb(sizeof(struct can_frame));
486         if (skb == NULL)
487                 return -ENOMEM;
488         skb->dev = dev;
489         skb->protocol = htons(ETH_P_CAN);
490         cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
491         memset(cf, 0, sizeof(struct can_frame));
492         cf->can_id = CAN_ERR_FLAG | CAN_ERR_RESTARTED;
493         cf->can_dlc = CAN_ERR_DLC;
494
495         netif_rx(skb);
496
497         dev->last_rx = jiffies;
498         stats->rx_packets++;
499         stats->rx_bytes += cf->can_dlc;
500
501         return 0;
502 }
503
504 static void can_restart_after(unsigned long data)
505 {
506         struct net_device *dev = (struct net_device *)data;
507         struct can_priv *priv = netdev_priv(dev);
508
509         priv->timer.expires = 0; /* mark inactive timer */
510         can_restart_now(dev);
511 }
512
513 /*
514  * CAN bus-off
515  *
516  * This functions should be called when the device goes bus-off to
517  * tell the netif layer that no more packets can be sent or received.
518  * If enabled, a timer is started to trigger bus-off recovery.
519  */
520 void can_bus_off(struct net_device *dev)
521 {
522         struct can_priv *priv = netdev_priv(dev);
523
524         dev_dbg(ND2D(dev), "bus-off\n");
525
526         netif_carrier_off(dev);
527
528         if (priv->restart_ms > 0 && !priv->timer.expires) {
529
530                 priv->timer.function = can_restart_after;
531                 priv->timer.data = (unsigned long)dev;
532                 priv->timer.expires =
533                         jiffies + (priv->restart_ms * HZ) / 1000;
534                 add_timer(&priv->timer);
535         }
536 }
537 EXPORT_SYMBOL_GPL(can_bus_off);
538
539 /*
540  * Cleanup function before the device gets closed.
541  *
542  * This functions should be called in the close function of the device
543  * driver.
544  */
545 void can_close_cleanup(struct net_device *dev)
546 {
547         struct can_priv *priv = netdev_priv(dev);
548
549         if (priv->timer.expires) {
550                 del_timer(&priv->timer);
551                 priv->timer.expires = 0;
552         }
553
554         can_flush_echo_skb(dev);
555 }
556 EXPORT_SYMBOL_GPL(can_close_cleanup);
557
558 static __init int can_dev_init(void)
559 {
560         printk(KERN_INFO MOD_DESC "\n");
561
562         return 0;
563 }
564 module_init(can_dev_init);
565
566 static __exit void can_dev_exit(void)
567 {
568 }
569 module_exit(can_dev_exit);