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