]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - kernel/2.6/drivers/net/can/dev.c
Fix warnings reported by make sparse
[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         uint64_t 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(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(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(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(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 #ifdef CONFIG_SYSFS
322         can_create_sysfs(dev);
323 #endif
324         return 0;
325 }
326 EXPORT_SYMBOL(register_candev);
327
328 /*
329  * Unregister the CAN network device
330  */
331 void unregister_candev(struct net_device *dev)
332 {
333 #ifdef CONFIG_SYSFS
334         can_remove_sysfs(dev);
335 #endif
336         unregister_netdev(dev);
337 }
338 EXPORT_SYMBOL(unregister_candev);
339
340 /*
341  * Local echo of CAN messages
342  *
343  * CAN network devices *should* support a local echo functionality
344  * (see Documentation/networking/can.txt). To test the handling of CAN
345  * interfaces that do not support the local echo both driver types are
346  * implemented. In the case that the driver does not support the echo
347  * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
348  * to perform the echo as a fallback solution.
349  */
350
351 static void can_flush_echo_skb(struct net_device *dev)
352 {
353         struct can_priv *priv = netdev_priv(dev);
354 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
355         struct net_device_stats *stats = can_get_stats(dev);
356 #else
357         struct net_device_stats *stats = &dev->stats;
358 #endif
359         int i;
360
361         for (i = 0; i < CAN_ECHO_SKB_MAX; i++) {
362                 if (priv->echo_skb[i]) {
363                         kfree_skb(priv->echo_skb[i]);
364                         priv->echo_skb[i] = NULL;
365                         stats->tx_dropped++;
366                         stats->tx_aborted_errors++;
367                 }
368         }
369 }
370
371 /*
372  * Put the skb on the stack to be looped backed locally lateron
373  *
374  * The function is typically called in the start_xmit function
375  * of the device driver.
376  */
377 void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev, int idx)
378 {
379         struct can_priv *priv = netdev_priv(dev);
380
381         /* set flag whether this packet has to be looped back */
382         if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK) {
383                 kfree_skb(skb);
384                 return;
385         }
386
387         if (!priv->echo_skb[idx]) {
388                 struct sock *srcsk = skb->sk;
389
390                 if (atomic_read(&skb->users) != 1) {
391                         struct sk_buff *old_skb = skb;
392
393                         skb = skb_clone(old_skb, GFP_ATOMIC);
394                         kfree_skb(old_skb);
395                         if (!skb)
396                                 return;
397                 } else
398                         skb_orphan(skb);
399
400                 skb->sk = srcsk;
401
402                 /* make settings for echo to reduce code in irq context */
403                 skb->protocol = htons(ETH_P_CAN);
404                 skb->pkt_type = PACKET_BROADCAST;
405                 skb->ip_summed = CHECKSUM_UNNECESSARY;
406                 skb->dev = dev;
407
408                 /* save this skb for tx interrupt echo handling */
409                 priv->echo_skb[idx] = skb;
410         } else {
411                 /* locking problem with netif_stop_queue() ?? */
412                 printk(KERN_ERR "%s: %s: BUG! echo_skb is occupied!\n",
413                        dev->name, __func__);
414                 kfree_skb(skb);
415         }
416 }
417 EXPORT_SYMBOL(can_put_echo_skb);
418
419 /*
420  * Get the skb from the stack and loop it back locally
421  *
422  * The function is typically called when the TX done interrupt
423  * is handled in the device driver.
424  */
425 void can_get_echo_skb(struct net_device *dev, int idx)
426 {
427         struct can_priv *priv = netdev_priv(dev);
428
429         if ((dev->flags & IFF_ECHO) && priv->echo_skb[idx]) {
430                 netif_rx(priv->echo_skb[idx]);
431                 priv->echo_skb[idx] = NULL;
432         }
433 }
434 EXPORT_SYMBOL(can_get_echo_skb);
435
436 /*
437  * CAN device restart for bus-off recovery
438  */
439 int can_restart_now(struct net_device *dev)
440 {
441         struct can_priv *priv = netdev_priv(dev);
442 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
443         struct net_device_stats *stats = can_get_stats(dev);
444 #else
445         struct net_device_stats *stats = &dev->stats;
446 #endif
447         struct sk_buff *skb;
448         struct can_frame *cf;
449         int err;
450
451         if (netif_carrier_ok(dev))
452                 netif_carrier_off(dev);
453
454         /* Cancel restart in progress */
455         if (priv->timer.expires) {
456                 del_timer(&priv->timer);
457                 priv->timer.expires = 0; /* mark inactive timer */
458         }
459
460         can_flush_echo_skb(dev);
461
462         err = priv->do_set_mode(dev, CAN_MODE_START);
463         if (err)
464                 return err;
465
466         netif_carrier_on(dev);
467
468         dev_dbg(ND2D(dev), "restarted\n");
469         priv->can_stats.restarts++;
470
471         /* send restart message upstream */
472         skb = dev_alloc_skb(sizeof(struct can_frame));
473         if (skb == NULL)
474                 return -ENOMEM;
475         skb->dev = dev;
476         skb->protocol = htons(ETH_P_CAN);
477         cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
478         memset(cf, 0, sizeof(struct can_frame));
479         cf->can_id = CAN_ERR_FLAG | CAN_ERR_RESTARTED;
480         cf->can_dlc = CAN_ERR_DLC;
481
482         netif_rx(skb);
483
484         dev->last_rx = jiffies;
485         stats->rx_packets++;
486         stats->rx_bytes += cf->can_dlc;
487
488         return 0;
489 }
490
491 static void can_restart_after(unsigned long data)
492 {
493         struct net_device *dev = (struct net_device *)data;
494         struct can_priv *priv = netdev_priv(dev);
495
496         priv->timer.expires = 0; /* mark inactive timer */
497         can_restart_now(dev);
498 }
499
500 /*
501  * CAN bus-off
502  *
503  * This functions should be called when the device goes bus-off to
504  * tell the netif layer that no more packets can be sent or received.
505  * If enabled, a timer is started to trigger bus-off recovery.
506  */
507 void can_bus_off(struct net_device *dev)
508 {
509         struct can_priv *priv = netdev_priv(dev);
510
511         dev_dbg(ND2D(dev), "bus-off\n");
512
513         netif_carrier_off(dev);
514
515         if (priv->restart_ms > 0 && !priv->timer.expires) {
516
517                 priv->timer.function = can_restart_after;
518                 priv->timer.data = (unsigned long)dev;
519                 priv->timer.expires =
520                         jiffies + (priv->restart_ms * HZ) / 1000;
521                 add_timer(&priv->timer);
522         }
523 }
524 EXPORT_SYMBOL(can_bus_off);
525
526 /*
527  * Cleanup function before the device gets closed.
528  *
529  * This functions should be called in the close function of the device
530  * driver.
531  */
532 void can_close_cleanup(struct net_device *dev)
533 {
534         struct can_priv *priv = netdev_priv(dev);
535
536         if (priv->timer.expires) {
537                 del_timer(&priv->timer);
538                 priv->timer.expires = 0;
539         }
540
541         can_flush_echo_skb(dev);
542 }
543 EXPORT_SYMBOL(can_close_cleanup);
544
545 static __init int can_dev_init(void)
546 {
547         printk(KERN_INFO MOD_DESC "\n");
548
549         return 0;
550 }
551 module_init(can_dev_init);
552
553 static __exit void can_dev_exit(void)
554 {
555 }
556 module_exit(can_dev_exit);