linux/drivers/net/can/dev.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) 2005 Marc Kleine-Budde, Pengutronix
   3 * Copyright (C) 2006 Andrey Volkov, Varma Electronics
   4 * Copyright (C) 2008-2009 Wolfgang Grandegger <wg@grandegger.com>
   5 *
   6 * This program is free software; you can redistribute it and/or modify
   7 * it under the terms of the version 2 of the GNU General Public License
   8 * as published by the Free Software Foundation
   9 *
  10 * This program is distributed in the hope that it will be useful,
  11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13 * GNU General Public License for more details.
  14 *
  15 * You should have received a copy of the GNU General Public License
  16 * along with this program; if not, write to the Free Software
  17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18 */
  19
  20#include <linux/module.h>
  21#include <linux/kernel.h>
  22#include <linux/slab.h>
  23#include <linux/netdevice.h>
  24#include <linux/if_arp.h>
  25#include <linux/can.h>
  26#include <linux/can/dev.h>
  27#include <linux/can/skb.h>
  28#include <linux/can/netlink.h>
  29#include <linux/can/led.h>
  30#include <net/rtnetlink.h>
  31
  32#define MOD_DESC "CAN device driver interface"
  33
  34MODULE_DESCRIPTION(MOD_DESC);
  35MODULE_LICENSE("GPL v2");
  36MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
  37
  38/* CAN DLC to real data length conversion helpers */
  39
  40static const u8 dlc2len[] = {0, 1, 2, 3, 4, 5, 6, 7,
  41                             8, 12, 16, 20, 24, 32, 48, 64};
  42
  43/* get data length from can_dlc with sanitized can_dlc */
  44u8 can_dlc2len(u8 can_dlc)
  45{
  46        return dlc2len[can_dlc & 0x0F];
  47}
  48EXPORT_SYMBOL_GPL(can_dlc2len);
  49
  50static const u8 len2dlc[] = {0, 1, 2, 3, 4, 5, 6, 7, 8,         /* 0 - 8 */
  51                             9, 9, 9, 9,                        /* 9 - 12 */
  52                             10, 10, 10, 10,                    /* 13 - 16 */
  53                             11, 11, 11, 11,                    /* 17 - 20 */
  54                             12, 12, 12, 12,                    /* 21 - 24 */
  55                             13, 13, 13, 13, 13, 13, 13, 13,    /* 25 - 32 */
  56                             14, 14, 14, 14, 14, 14, 14, 14,    /* 33 - 40 */
  57                             14, 14, 14, 14, 14, 14, 14, 14,    /* 41 - 48 */
  58                             15, 15, 15, 15, 15, 15, 15, 15,    /* 49 - 56 */
  59                             15, 15, 15, 15, 15, 15, 15, 15};   /* 57 - 64 */
  60
  61/* map the sanitized data length to an appropriate data length code */
  62u8 can_len2dlc(u8 len)
  63{
  64        if (unlikely(len > 64))
  65                return 0xF;
  66
  67        return len2dlc[len];
  68}
  69EXPORT_SYMBOL_GPL(can_len2dlc);
  70
  71#ifdef CONFIG_CAN_CALC_BITTIMING
  72#define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
  73
  74/*
  75 * Bit-timing calculation derived from:
  76 *
  77 * Code based on LinCAN sources and H8S2638 project
  78 * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
  79 * Copyright 2005      Stanislav Marek
  80 * email: pisa@cmp.felk.cvut.cz
  81 *
  82 * Calculates proper bit-timing parameters for a specified bit-rate
  83 * and sample-point, which can then be used to set the bit-timing
  84 * registers of the CAN controller. You can find more information
  85 * in the header file linux/can/netlink.h.
  86 */
  87static int can_update_spt(const struct can_bittiming_const *btc,
  88                          int sampl_pt, int tseg, int *tseg1, int *tseg2)
  89{
  90        *tseg2 = tseg + 1 - (sampl_pt * (tseg + 1)) / 1000;
  91        if (*tseg2 < btc->tseg2_min)
  92                *tseg2 = btc->tseg2_min;
  93        if (*tseg2 > btc->tseg2_max)
  94                *tseg2 = btc->tseg2_max;
  95        *tseg1 = tseg - *tseg2;
  96        if (*tseg1 > btc->tseg1_max) {
  97                *tseg1 = btc->tseg1_max;
  98                *tseg2 = tseg - *tseg1;
  99        }
 100        return 1000 * (tseg + 1 - *tseg2) / (tseg + 1);
 101}
 102
 103static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt)
 104{
 105        struct can_priv *priv = netdev_priv(dev);
 106        const struct can_bittiming_const *btc = priv->bittiming_const;
 107        long rate, best_rate = 0;
 108        long best_error = 1000000000, error = 0;
 109        int best_tseg = 0, best_brp = 0, brp = 0;
 110        int tsegall, tseg = 0, tseg1 = 0, tseg2 = 0;
 111        int spt_error = 1000, spt = 0, sampl_pt;
 112        u64 v64;
 113
 114        if (!priv->bittiming_const)
 115                return -ENOTSUPP;
 116
 117        /* Use CIA recommended sample points */
 118        if (bt->sample_point) {
 119                sampl_pt = bt->sample_point;
 120        } else {
 121                if (bt->bitrate > 800000)
 122                        sampl_pt = 750;
 123                else if (bt->bitrate > 500000)
 124                        sampl_pt = 800;
 125                else
 126                        sampl_pt = 875;
 127        }
 128
 129        /* tseg even = round down, odd = round up */
 130        for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
 131             tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
 132                tsegall = 1 + tseg / 2;
 133                /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
 134                brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;
 135                /* chose brp step which is possible in system */
 136                brp = (brp / btc->brp_inc) * btc->brp_inc;
 137                if ((brp < btc->brp_min) || (brp > btc->brp_max))
 138                        continue;
 139                rate = priv->clock.freq / (brp * tsegall);
 140                error = bt->bitrate - rate;
 141                /* tseg brp biterror */
 142                if (error < 0)
 143                        error = -error;
 144                if (error > best_error)
 145                        continue;
 146                best_error = error;
 147                if (error == 0) {
 148                        spt = can_update_spt(btc, sampl_pt, tseg / 2,
 149                                             &tseg1, &tseg2);
 150                        error = sampl_pt - spt;
 151                        if (error < 0)
 152                                error = -error;
 153                        if (error > spt_error)
 154                                continue;
 155                        spt_error = error;
 156                }
 157                best_tseg = tseg / 2;
 158                best_brp = brp;
 159                best_rate = rate;
 160                if (error == 0)
 161                        break;
 162        }
 163
 164        if (best_error) {
 165                /* Error in one-tenth of a percent */
 166                error = (best_error * 1000) / bt->bitrate;
 167                if (error > CAN_CALC_MAX_ERROR) {
 168                        netdev_err(dev,
 169                                   "bitrate error %ld.%ld%% too high\n",
 170                                   error / 10, error % 10);
 171                        return -EDOM;
 172                } else {
 173                        netdev_warn(dev, "bitrate error %ld.%ld%%\n",
 174                                    error / 10, error % 10);
 175                }
 176        }
 177
 178        /* real sample point */
 179        bt->sample_point = can_update_spt(btc, sampl_pt, best_tseg,
 180                                          &tseg1, &tseg2);
 181
 182        v64 = (u64)best_brp * 1000000000UL;
 183        do_div(v64, priv->clock.freq);
 184        bt->tq = (u32)v64;
 185        bt->prop_seg = tseg1 / 2;
 186        bt->phase_seg1 = tseg1 - bt->prop_seg;
 187        bt->phase_seg2 = tseg2;
 188
 189        /* check for sjw user settings */
 190        if (!bt->sjw || !btc->sjw_max)
 191                bt->sjw = 1;
 192        else {
 193                /* bt->sjw is at least 1 -> sanitize upper bound to sjw_max */
 194                if (bt->sjw > btc->sjw_max)
 195                        bt->sjw = btc->sjw_max;
 196                /* bt->sjw must not be higher than tseg2 */
 197                if (tseg2 < bt->sjw)
 198                        bt->sjw = tseg2;
 199        }
 200
 201        bt->brp = best_brp;
 202        /* real bit-rate */
 203        bt->bitrate = priv->clock.freq / (bt->brp * (tseg1 + tseg2 + 1));
 204
 205        return 0;
 206}
 207#else /* !CONFIG_CAN_CALC_BITTIMING */
 208static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt)
 209{
 210        netdev_err(dev, "bit-timing calculation not available\n");
 211        return -EINVAL;
 212}
 213#endif /* CONFIG_CAN_CALC_BITTIMING */
 214
 215/*
 216 * Checks the validity of the specified bit-timing parameters prop_seg,
 217 * phase_seg1, phase_seg2 and sjw and tries to determine the bitrate
 218 * prescaler value brp. You can find more information in the header
 219 * file linux/can/netlink.h.
 220 */
 221static int can_fixup_bittiming(struct net_device *dev, struct can_bittiming *bt)
 222{
 223        struct can_priv *priv = netdev_priv(dev);
 224        const struct can_bittiming_const *btc = priv->bittiming_const;
 225        int tseg1, alltseg;
 226        u64 brp64;
 227
 228        if (!priv->bittiming_const)
 229                return -ENOTSUPP;
 230
 231        tseg1 = bt->prop_seg + bt->phase_seg1;
 232        if (!bt->sjw)
 233                bt->sjw = 1;
 234        if (bt->sjw > btc->sjw_max ||
 235            tseg1 < btc->tseg1_min || tseg1 > btc->tseg1_max ||
 236            bt->phase_seg2 < btc->tseg2_min || bt->phase_seg2 > btc->tseg2_max)
 237                return -ERANGE;
 238
 239        brp64 = (u64)priv->clock.freq * (u64)bt->tq;
 240        if (btc->brp_inc > 1)
 241                do_div(brp64, btc->brp_inc);
 242        brp64 += 500000000UL - 1;
 243        do_div(brp64, 1000000000UL); /* the practicable BRP */
 244        if (btc->brp_inc > 1)
 245                brp64 *= btc->brp_inc;
 246        bt->brp = (u32)brp64;
 247
 248        if (bt->brp < btc->brp_min || bt->brp > btc->brp_max)
 249                return -EINVAL;
 250
 251        alltseg = bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1;
 252        bt->bitrate = priv->clock.freq / (bt->brp * alltseg);
 253        bt->sample_point = ((tseg1 + 1) * 1000) / alltseg;
 254
 255        return 0;
 256}
 257
 258static int can_get_bittiming(struct net_device *dev, struct can_bittiming *bt)
 259{
 260        struct can_priv *priv = netdev_priv(dev);
 261        int err;
 262
 263        /* Check if the CAN device has bit-timing parameters */
 264        if (!priv->bittiming_const)
 265                return 0;
 266
 267        /*
 268         * Depending on the given can_bittiming parameter structure the CAN
 269         * timing parameters are calculated based on the provided bitrate OR
 270         * alternatively the CAN timing parameters (tq, prop_seg, etc.) are
 271         * provided directly which are then checked and fixed up.
 272         */
 273        if (!bt->tq && bt->bitrate)
 274                err = can_calc_bittiming(dev, bt);
 275        else if (bt->tq && !bt->bitrate)
 276                err = can_fixup_bittiming(dev, bt);
 277        else
 278                err = -EINVAL;
 279
 280        return err;
 281}
 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 */
 293static void can_flush_echo_skb(struct net_device *dev)
 294{
 295        struct can_priv *priv = netdev_priv(dev);
 296        struct net_device_stats *stats = &dev->stats;
 297        int i;
 298
 299        for (i = 0; i < priv->echo_skb_max; i++) {
 300                if (priv->echo_skb[i]) {
 301                        kfree_skb(priv->echo_skb[i]);
 302                        priv->echo_skb[i] = NULL;
 303                        stats->tx_dropped++;
 304                        stats->tx_aborted_errors++;
 305                }
 306        }
 307}
 308
 309/*
 310 * Put the skb on the stack to be looped backed locally lateron
 311 *
 312 * The function is typically called in the start_xmit function
 313 * of the device driver. The driver must protect access to
 314 * priv->echo_skb, if necessary.
 315 */
 316void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
 317                      unsigned int idx)
 318{
 319        struct can_priv *priv = netdev_priv(dev);
 320
 321        BUG_ON(idx >= priv->echo_skb_max);
 322
 323        /* check flag whether this packet has to be looped back */
 324        if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK ||
 325            (skb->protocol != htons(ETH_P_CAN) &&
 326             skb->protocol != htons(ETH_P_CANFD))) {
 327                kfree_skb(skb);
 328                return;
 329        }
 330
 331        if (!priv->echo_skb[idx]) {
 332                struct sock *srcsk = skb->sk;
 333
 334                if (atomic_read(&skb->users) != 1) {
 335                        struct sk_buff *old_skb = skb;
 336
 337                        skb = skb_clone(old_skb, GFP_ATOMIC);
 338                        kfree_skb(old_skb);
 339                        if (!skb)
 340                                return;
 341                } else
 342                        skb_orphan(skb);
 343
 344                skb->sk = srcsk;
 345
 346                /* make settings for echo to reduce code in irq context */
 347                skb->pkt_type = PACKET_BROADCAST;
 348                skb->ip_summed = CHECKSUM_UNNECESSARY;
 349                skb->dev = dev;
 350
 351                /* save this skb for tx interrupt echo handling */
 352                priv->echo_skb[idx] = skb;
 353        } else {
 354                /* locking problem with netif_stop_queue() ?? */
 355                netdev_err(dev, "%s: BUG! echo_skb is occupied!\n", __func__);
 356                kfree_skb(skb);
 357        }
 358}
 359EXPORT_SYMBOL_GPL(can_put_echo_skb);
 360
 361/*
 362 * Get the skb from the stack and loop it back locally
 363 *
 364 * The function is typically called when the TX done interrupt
 365 * is handled in the device driver. The driver must protect
 366 * access to priv->echo_skb, if necessary.
 367 */
 368unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx)
 369{
 370        struct can_priv *priv = netdev_priv(dev);
 371
 372        BUG_ON(idx >= priv->echo_skb_max);
 373
 374        if (priv->echo_skb[idx]) {
 375                struct sk_buff *skb = priv->echo_skb[idx];
 376                struct can_frame *cf = (struct can_frame *)skb->data;
 377                u8 dlc = cf->can_dlc;
 378
 379                netif_rx(priv->echo_skb[idx]);
 380                priv->echo_skb[idx] = NULL;
 381
 382                return dlc;
 383        }
 384
 385        return 0;
 386}
 387EXPORT_SYMBOL_GPL(can_get_echo_skb);
 388
 389/*
 390  * Remove the skb from the stack and free it.
 391  *
 392  * The function is typically called when TX failed.
 393  */
 394void can_free_echo_skb(struct net_device *dev, unsigned int idx)
 395{
 396        struct can_priv *priv = netdev_priv(dev);
 397
 398        BUG_ON(idx >= priv->echo_skb_max);
 399
 400        if (priv->echo_skb[idx]) {
 401                kfree_skb(priv->echo_skb[idx]);
 402                priv->echo_skb[idx] = NULL;
 403        }
 404}
 405EXPORT_SYMBOL_GPL(can_free_echo_skb);
 406
 407/*
 408 * CAN device restart for bus-off recovery
 409 */
 410static void can_restart(unsigned long data)
 411{
 412        struct net_device *dev = (struct net_device *)data;
 413        struct can_priv *priv = netdev_priv(dev);
 414        struct net_device_stats *stats = &dev->stats;
 415        struct sk_buff *skb;
 416        struct can_frame *cf;
 417        int err;
 418
 419        BUG_ON(netif_carrier_ok(dev));
 420
 421        /*
 422         * No synchronization needed because the device is bus-off and
 423         * no messages can come in or go out.
 424         */
 425        can_flush_echo_skb(dev);
 426
 427        /* send restart message upstream */
 428        skb = alloc_can_err_skb(dev, &cf);
 429        if (skb == NULL) {
 430                err = -ENOMEM;
 431                goto restart;
 432        }
 433        cf->can_id |= CAN_ERR_RESTARTED;
 434
 435        netif_rx(skb);
 436
 437        stats->rx_packets++;
 438        stats->rx_bytes += cf->can_dlc;
 439
 440restart:
 441        netdev_dbg(dev, "restarted\n");
 442        priv->can_stats.restarts++;
 443
 444        /* Now restart the device */
 445        err = priv->do_set_mode(dev, CAN_MODE_START);
 446
 447        netif_carrier_on(dev);
 448        if (err)
 449                netdev_err(dev, "Error %d during restart", err);
 450}
 451
 452int can_restart_now(struct net_device *dev)
 453{
 454        struct can_priv *priv = netdev_priv(dev);
 455
 456        /*
 457         * A manual restart is only permitted if automatic restart is
 458         * disabled and the device is in the bus-off state
 459         */
 460        if (priv->restart_ms)
 461                return -EINVAL;
 462        if (priv->state != CAN_STATE_BUS_OFF)
 463                return -EBUSY;
 464
 465        /* Runs as soon as possible in the timer context */
 466        mod_timer(&priv->restart_timer, jiffies);
 467
 468        return 0;
 469}
 470
 471/*
 472 * CAN bus-off
 473 *
 474 * This functions should be called when the device goes bus-off to
 475 * tell the netif layer that no more packets can be sent or received.
 476 * If enabled, a timer is started to trigger bus-off recovery.
 477 */
 478void can_bus_off(struct net_device *dev)
 479{
 480        struct can_priv *priv = netdev_priv(dev);
 481
 482        netdev_dbg(dev, "bus-off\n");
 483
 484        netif_carrier_off(dev);
 485        priv->can_stats.bus_off++;
 486
 487        if (priv->restart_ms)
 488                mod_timer(&priv->restart_timer,
 489                          jiffies + (priv->restart_ms * HZ) / 1000);
 490}
 491EXPORT_SYMBOL_GPL(can_bus_off);
 492
 493static void can_setup(struct net_device *dev)
 494{
 495        dev->type = ARPHRD_CAN;
 496        dev->mtu = CAN_MTU;
 497        dev->hard_header_len = 0;
 498        dev->addr_len = 0;
 499        dev->tx_queue_len = 10;
 500
 501        /* New-style flags. */
 502        dev->flags = IFF_NOARP;
 503        dev->features = NETIF_F_HW_CSUM;
 504}
 505
 506struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf)
 507{
 508        struct sk_buff *skb;
 509
 510        skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
 511                               sizeof(struct can_frame));
 512        if (unlikely(!skb))
 513                return NULL;
 514
 515        skb->protocol = htons(ETH_P_CAN);
 516        skb->pkt_type = PACKET_BROADCAST;
 517        skb->ip_summed = CHECKSUM_UNNECESSARY;
 518
 519        skb_reset_mac_header(skb);
 520        skb_reset_network_header(skb);
 521        skb_reset_transport_header(skb);
 522
 523        can_skb_reserve(skb);
 524        can_skb_prv(skb)->ifindex = dev->ifindex;
 525        can_skb_prv(skb)->skbcnt = 0;
 526
 527        *cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
 528        memset(*cf, 0, sizeof(struct can_frame));
 529
 530        return skb;
 531}
 532EXPORT_SYMBOL_GPL(alloc_can_skb);
 533
 534struct sk_buff *alloc_can_err_skb(struct net_device *dev, struct can_frame **cf)
 535{
 536        struct sk_buff *skb;
 537
 538        skb = alloc_can_skb(dev, cf);
 539        if (unlikely(!skb))
 540                return NULL;
 541
 542        (*cf)->can_id = CAN_ERR_FLAG;
 543        (*cf)->can_dlc = CAN_ERR_DLC;
 544
 545        return skb;
 546}
 547EXPORT_SYMBOL_GPL(alloc_can_err_skb);
 548
 549/*
 550 * Allocate and setup space for the CAN network device
 551 */
 552struct net_device *alloc_candev(int sizeof_priv, unsigned int echo_skb_max)
 553{
 554        struct net_device *dev;
 555        struct can_priv *priv;
 556        int size;
 557
 558        if (echo_skb_max)
 559                size = ALIGN(sizeof_priv, sizeof(struct sk_buff *)) +
 560                        echo_skb_max * sizeof(struct sk_buff *);
 561        else
 562                size = sizeof_priv;
 563
 564        dev = alloc_netdev(size, "can%d", can_setup);
 565        if (!dev)
 566                return NULL;
 567
 568        priv = netdev_priv(dev);
 569
 570        if (echo_skb_max) {
 571                priv->echo_skb_max = echo_skb_max;
 572                priv->echo_skb = (void *)priv +
 573                        ALIGN(sizeof_priv, sizeof(struct sk_buff *));
 574        }
 575
 576        priv->state = CAN_STATE_STOPPED;
 577
 578        init_timer(&priv->restart_timer);
 579
 580        return dev;
 581}
 582EXPORT_SYMBOL_GPL(alloc_candev);
 583
 584/*
 585 * Free space of the CAN network device
 586 */
 587void free_candev(struct net_device *dev)
 588{
 589        free_netdev(dev);
 590}
 591EXPORT_SYMBOL_GPL(free_candev);
 592
 593/*
 594 * Common open function when the device gets opened.
 595 *
 596 * This function should be called in the open function of the device
 597 * driver.
 598 */
 599int open_candev(struct net_device *dev)
 600{
 601        struct can_priv *priv = netdev_priv(dev);
 602
 603        if (!priv->bittiming.tq && !priv->bittiming.bitrate) {
 604                netdev_err(dev, "bit-timing not yet defined\n");
 605                return -EINVAL;
 606        }
 607
 608        /* Switch carrier on if device was stopped while in bus-off state */
 609        if (!netif_carrier_ok(dev))
 610                netif_carrier_on(dev);
 611
 612        setup_timer(&priv->restart_timer, can_restart, (unsigned long)dev);
 613
 614        return 0;
 615}
 616EXPORT_SYMBOL_GPL(open_candev);
 617
 618/*
 619 * Common close function for cleanup before the device gets closed.
 620 *
 621 * This function should be called in the close function of the device
 622 * driver.
 623 */
 624void close_candev(struct net_device *dev)
 625{
 626        struct can_priv *priv = netdev_priv(dev);
 627
 628        del_timer_sync(&priv->restart_timer);
 629        can_flush_echo_skb(dev);
 630}
 631EXPORT_SYMBOL_GPL(close_candev);
 632
 633/*
 634 * CAN netlink interface
 635 */
 636static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = {
 637        [IFLA_CAN_STATE]        = { .type = NLA_U32 },
 638        [IFLA_CAN_CTRLMODE]     = { .len = sizeof(struct can_ctrlmode) },
 639        [IFLA_CAN_RESTART_MS]   = { .type = NLA_U32 },
 640        [IFLA_CAN_RESTART]      = { .type = NLA_U32 },
 641        [IFLA_CAN_BITTIMING]    = { .len = sizeof(struct can_bittiming) },
 642        [IFLA_CAN_BITTIMING_CONST]
 643                                = { .len = sizeof(struct can_bittiming_const) },
 644        [IFLA_CAN_CLOCK]        = { .len = sizeof(struct can_clock) },
 645        [IFLA_CAN_BERR_COUNTER] = { .len = sizeof(struct can_berr_counter) },
 646};
 647
 648static int can_changelink(struct net_device *dev,
 649                          struct nlattr *tb[], struct nlattr *data[])
 650{
 651        struct can_priv *priv = netdev_priv(dev);
 652        int err;
 653
 654        /* We need synchronization with dev->stop() */
 655        ASSERT_RTNL();
 656
 657        if (data[IFLA_CAN_CTRLMODE]) {
 658                struct can_ctrlmode *cm;
 659
 660                /* Do not allow changing controller mode while running */
 661                if (dev->flags & IFF_UP)
 662                        return -EBUSY;
 663                cm = nla_data(data[IFLA_CAN_CTRLMODE]);
 664                if (cm->flags & ~priv->ctrlmode_supported)
 665                        return -EOPNOTSUPP;
 666                priv->ctrlmode &= ~cm->mask;
 667                priv->ctrlmode |= cm->flags;
 668        }
 669
 670        if (data[IFLA_CAN_BITTIMING]) {
 671                struct can_bittiming bt;
 672
 673                /* Do not allow changing bittiming while running */
 674                if (dev->flags & IFF_UP)
 675                        return -EBUSY;
 676                memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt));
 677                err = can_get_bittiming(dev, &bt);
 678                if (err)
 679                        return err;
 680                memcpy(&priv->bittiming, &bt, sizeof(bt));
 681
 682                if (priv->do_set_bittiming) {
 683                        /* Finally, set the bit-timing registers */
 684                        err = priv->do_set_bittiming(dev);
 685                        if (err)
 686                                return err;
 687                }
 688        }
 689
 690        if (data[IFLA_CAN_RESTART_MS]) {
 691                /* Do not allow changing restart delay while running */
 692                if (dev->flags & IFF_UP)
 693                        return -EBUSY;
 694                priv->restart_ms = nla_get_u32(data[IFLA_CAN_RESTART_MS]);
 695        }
 696
 697        if (data[IFLA_CAN_RESTART]) {
 698                /* Do not allow a restart while not running */
 699                if (!(dev->flags & IFF_UP))
 700                        return -EINVAL;
 701                err = can_restart_now(dev);
 702                if (err)
 703                        return err;
 704        }
 705
 706        return 0;
 707}
 708
 709static size_t can_get_size(const struct net_device *dev)
 710{
 711        struct can_priv *priv = netdev_priv(dev);
 712        size_t size;
 713
 714        size = nla_total_size(sizeof(u32));   /* IFLA_CAN_STATE */
 715        size += nla_total_size(sizeof(struct can_ctrlmode));  /* IFLA_CAN_CTRLMODE */
 716        size += nla_total_size(sizeof(u32));  /* IFLA_CAN_RESTART_MS */
 717        size += nla_total_size(sizeof(struct can_bittiming)); /* IFLA_CAN_BITTIMING */
 718        size += nla_total_size(sizeof(struct can_clock));     /* IFLA_CAN_CLOCK */
 719        if (priv->do_get_berr_counter)        /* IFLA_CAN_BERR_COUNTER */
 720                size += nla_total_size(sizeof(struct can_berr_counter));
 721        if (priv->bittiming_const)            /* IFLA_CAN_BITTIMING_CONST */
 722                size += nla_total_size(sizeof(struct can_bittiming_const));
 723
 724        return size;
 725}
 726
 727static int can_fill_info(struct sk_buff *skb, const struct net_device *dev)
 728{
 729        struct can_priv *priv = netdev_priv(dev);
 730        struct can_ctrlmode cm = {.flags = priv->ctrlmode};
 731        struct can_berr_counter bec;
 732        enum can_state state = priv->state;
 733
 734        if (priv->do_get_state)
 735                priv->do_get_state(dev, &state);
 736        if (nla_put_u32(skb, IFLA_CAN_STATE, state) ||
 737            nla_put(skb, IFLA_CAN_CTRLMODE, sizeof(cm), &cm) ||
 738            nla_put_u32(skb, IFLA_CAN_RESTART_MS, priv->restart_ms) ||
 739            nla_put(skb, IFLA_CAN_BITTIMING,
 740                    sizeof(priv->bittiming), &priv->bittiming) ||
 741            nla_put(skb, IFLA_CAN_CLOCK, sizeof(cm), &priv->clock) ||
 742            (priv->do_get_berr_counter &&
 743             !priv->do_get_berr_counter(dev, &bec) &&
 744             nla_put(skb, IFLA_CAN_BERR_COUNTER, sizeof(bec), &bec)) ||
 745            (priv->bittiming_const &&
 746             nla_put(skb, IFLA_CAN_BITTIMING_CONST,
 747                     sizeof(*priv->bittiming_const), priv->bittiming_const)))
 748                goto nla_put_failure;
 749        return 0;
 750
 751nla_put_failure:
 752        return -EMSGSIZE;
 753}
 754
 755static size_t can_get_xstats_size(const struct net_device *dev)
 756{
 757        return sizeof(struct can_device_stats);
 758}
 759
 760static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev)
 761{
 762        struct can_priv *priv = netdev_priv(dev);
 763
 764        if (nla_put(skb, IFLA_INFO_XSTATS,
 765                    sizeof(priv->can_stats), &priv->can_stats))
 766                goto nla_put_failure;
 767        return 0;
 768
 769nla_put_failure:
 770        return -EMSGSIZE;
 771}
 772
 773static int can_newlink(struct net *src_net, struct net_device *dev,
 774                       struct nlattr *tb[], struct nlattr *data[])
 775{
 776        return -EOPNOTSUPP;
 777}
 778
 779static struct rtnl_link_ops can_link_ops __read_mostly = {
 780        .kind           = "can",
 781        .maxtype        = IFLA_CAN_MAX,
 782        .policy         = can_policy,
 783        .setup          = can_setup,
 784        .newlink        = can_newlink,
 785        .changelink     = can_changelink,
 786        .get_size       = can_get_size,
 787        .fill_info      = can_fill_info,
 788        .get_xstats_size = can_get_xstats_size,
 789        .fill_xstats    = can_fill_xstats,
 790};
 791
 792/*
 793 * Register the CAN network device
 794 */
 795int register_candev(struct net_device *dev)
 796{
 797        dev->rtnl_link_ops = &can_link_ops;
 798        return register_netdev(dev);
 799}
 800EXPORT_SYMBOL_GPL(register_candev);
 801
 802/*
 803 * Unregister the CAN network device
 804 */
 805void unregister_candev(struct net_device *dev)
 806{
 807        unregister_netdev(dev);
 808}
 809EXPORT_SYMBOL_GPL(unregister_candev);
 810
 811/*
 812 * Test if a network device is a candev based device
 813 * and return the can_priv* if so.
 814 */
 815struct can_priv *safe_candev_priv(struct net_device *dev)
 816{
 817        if ((dev->type != ARPHRD_CAN) || (dev->rtnl_link_ops != &can_link_ops))
 818                return NULL;
 819
 820        return netdev_priv(dev);
 821}
 822EXPORT_SYMBOL_GPL(safe_candev_priv);
 823
 824static __init int can_dev_init(void)
 825{
 826        int err;
 827
 828        can_led_notifier_init();
 829
 830        err = rtnl_link_register(&can_link_ops);
 831        if (!err)
 832                printk(KERN_INFO MOD_DESC "\n");
 833
 834        return err;
 835}
 836module_init(can_dev_init);
 837
 838static __exit void can_dev_exit(void)
 839{
 840        rtnl_link_unregister(&can_link_ops);
 841
 842        can_led_notifier_exit();
 843}
 844module_exit(can_dev_exit);
 845
 846MODULE_ALIAS_RTNL_LINK("can");
 847