1 // SPDX-License-Identifier: GPL-2.0-only 2 /* Copyright (C) 2005 Marc Kleine-Budde, Pengutronix 3 * Copyright (C) 2006 Andrey Volkov, Varma Electronics 4 * Copyright (C) 2008-2009 Wolfgang Grandegger <[email protected]> 5 */ 6 7 #include <linux/module.h> 8 #include <linux/kernel.h> 9 #include <linux/slab.h> 10 #include <linux/netdevice.h> 11 #include <linux/if_arp.h> 12 #include <linux/workqueue.h> 13 #include <linux/can.h> 14 #include <linux/can/can-ml.h> 15 #include <linux/can/dev.h> 16 #include <linux/can/skb.h> 17 #include <linux/can/netlink.h> 18 #include <linux/can/led.h> 19 #include <linux/of.h> 20 #include <net/rtnetlink.h> 21 22 #define MOD_DESC "CAN device driver interface" 23 24 MODULE_DESCRIPTION(MOD_DESC); 25 MODULE_LICENSE("GPL v2"); 26 MODULE_AUTHOR("Wolfgang Grandegger <[email protected]>"); 27 28 static void can_update_state_error_stats(struct net_device *dev, 29 enum can_state new_state) 30 { 31 struct can_priv *priv = netdev_priv(dev); 32 33 if (new_state <= priv->state) 34 return; 35 36 switch (new_state) { 37 case CAN_STATE_ERROR_WARNING: 38 priv->can_stats.error_warning++; 39 break; 40 case CAN_STATE_ERROR_PASSIVE: 41 priv->can_stats.error_passive++; 42 break; 43 case CAN_STATE_BUS_OFF: 44 priv->can_stats.bus_off++; 45 break; 46 default: 47 break; 48 } 49 } 50 51 static int can_tx_state_to_frame(struct net_device *dev, enum can_state state) 52 { 53 switch (state) { 54 case CAN_STATE_ERROR_ACTIVE: 55 return CAN_ERR_CRTL_ACTIVE; 56 case CAN_STATE_ERROR_WARNING: 57 return CAN_ERR_CRTL_TX_WARNING; 58 case CAN_STATE_ERROR_PASSIVE: 59 return CAN_ERR_CRTL_TX_PASSIVE; 60 default: 61 return 0; 62 } 63 } 64 65 static int can_rx_state_to_frame(struct net_device *dev, enum can_state state) 66 { 67 switch (state) { 68 case CAN_STATE_ERROR_ACTIVE: 69 return CAN_ERR_CRTL_ACTIVE; 70 case CAN_STATE_ERROR_WARNING: 71 return CAN_ERR_CRTL_RX_WARNING; 72 case CAN_STATE_ERROR_PASSIVE: 73 return CAN_ERR_CRTL_RX_PASSIVE; 74 default: 75 return 0; 76 } 77 } 78 79 static const char *can_get_state_str(const enum can_state state) 80 { 81 switch (state) { 82 case CAN_STATE_ERROR_ACTIVE: 83 return "Error Active"; 84 case CAN_STATE_ERROR_WARNING: 85 return "Error Warning"; 86 case CAN_STATE_ERROR_PASSIVE: 87 return "Error Passive"; 88 case CAN_STATE_BUS_OFF: 89 return "Bus Off"; 90 case CAN_STATE_STOPPED: 91 return "Stopped"; 92 case CAN_STATE_SLEEPING: 93 return "Sleeping"; 94 default: 95 return "<unknown>"; 96 } 97 98 return "<unknown>"; 99 } 100 101 void can_change_state(struct net_device *dev, struct can_frame *cf, 102 enum can_state tx_state, enum can_state rx_state) 103 { 104 struct can_priv *priv = netdev_priv(dev); 105 enum can_state new_state = max(tx_state, rx_state); 106 107 if (unlikely(new_state == priv->state)) { 108 netdev_warn(dev, "%s: oops, state did not change", __func__); 109 return; 110 } 111 112 netdev_dbg(dev, "Controller changed from %s State (%d) into %s State (%d).\n", 113 can_get_state_str(priv->state), priv->state, 114 can_get_state_str(new_state), new_state); 115 116 can_update_state_error_stats(dev, new_state); 117 priv->state = new_state; 118 119 if (!cf) 120 return; 121 122 if (unlikely(new_state == CAN_STATE_BUS_OFF)) { 123 cf->can_id |= CAN_ERR_BUSOFF; 124 return; 125 } 126 127 cf->can_id |= CAN_ERR_CRTL; 128 cf->data[1] |= tx_state >= rx_state ? 129 can_tx_state_to_frame(dev, tx_state) : 0; 130 cf->data[1] |= tx_state <= rx_state ? 131 can_rx_state_to_frame(dev, rx_state) : 0; 132 } 133 EXPORT_SYMBOL_GPL(can_change_state); 134 135 /* CAN device restart for bus-off recovery */ 136 static void can_restart(struct net_device *dev) 137 { 138 struct can_priv *priv = netdev_priv(dev); 139 struct net_device_stats *stats = &dev->stats; 140 struct sk_buff *skb; 141 struct can_frame *cf; 142 int err; 143 144 BUG_ON(netif_carrier_ok(dev)); 145 146 /* No synchronization needed because the device is bus-off and 147 * no messages can come in or go out. 148 */ 149 can_flush_echo_skb(dev); 150 151 /* send restart message upstream */ 152 skb = alloc_can_err_skb(dev, &cf); 153 if (!skb) 154 goto restart; 155 156 cf->can_id |= CAN_ERR_RESTARTED; 157 158 netif_rx_ni(skb); 159 160 stats->rx_packets++; 161 stats->rx_bytes += cf->len; 162 163 restart: 164 netdev_dbg(dev, "restarted\n"); 165 priv->can_stats.restarts++; 166 167 /* Now restart the device */ 168 err = priv->do_set_mode(dev, CAN_MODE_START); 169 170 netif_carrier_on(dev); 171 if (err) 172 netdev_err(dev, "Error %d during restart", err); 173 } 174 175 static void can_restart_work(struct work_struct *work) 176 { 177 struct delayed_work *dwork = to_delayed_work(work); 178 struct can_priv *priv = container_of(dwork, struct can_priv, 179 restart_work); 180 181 can_restart(priv->dev); 182 } 183 184 int can_restart_now(struct net_device *dev) 185 { 186 struct can_priv *priv = netdev_priv(dev); 187 188 /* A manual restart is only permitted if automatic restart is 189 * disabled and the device is in the bus-off state 190 */ 191 if (priv->restart_ms) 192 return -EINVAL; 193 if (priv->state != CAN_STATE_BUS_OFF) 194 return -EBUSY; 195 196 cancel_delayed_work_sync(&priv->restart_work); 197 can_restart(dev); 198 199 return 0; 200 } 201 202 /* CAN bus-off 203 * 204 * This functions should be called when the device goes bus-off to 205 * tell the netif layer that no more packets can be sent or received. 206 * If enabled, a timer is started to trigger bus-off recovery. 207 */ 208 void can_bus_off(struct net_device *dev) 209 { 210 struct can_priv *priv = netdev_priv(dev); 211 212 if (priv->restart_ms) 213 netdev_info(dev, "bus-off, scheduling restart in %d ms\n", 214 priv->restart_ms); 215 else 216 netdev_info(dev, "bus-off\n"); 217 218 netif_carrier_off(dev); 219 220 if (priv->restart_ms) 221 schedule_delayed_work(&priv->restart_work, 222 msecs_to_jiffies(priv->restart_ms)); 223 } 224 EXPORT_SYMBOL_GPL(can_bus_off); 225 226 static void can_setup(struct net_device *dev) 227 { 228 dev->type = ARPHRD_CAN; 229 dev->mtu = CAN_MTU; 230 dev->hard_header_len = 0; 231 dev->addr_len = 0; 232 dev->tx_queue_len = 10; 233 234 /* New-style flags. */ 235 dev->flags = IFF_NOARP; 236 dev->features = NETIF_F_HW_CSUM; 237 } 238 239 /* Allocate and setup space for the CAN network device */ 240 struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max, 241 unsigned int txqs, unsigned int rxqs) 242 { 243 struct net_device *dev; 244 struct can_priv *priv; 245 int size; 246 247 /* We put the driver's priv, the CAN mid layer priv and the 248 * echo skb into the netdevice's priv. The memory layout for 249 * the netdev_priv is like this: 250 * 251 * +-------------------------+ 252 * | driver's priv | 253 * +-------------------------+ 254 * | struct can_ml_priv | 255 * +-------------------------+ 256 * | array of struct sk_buff | 257 * +-------------------------+ 258 */ 259 260 size = ALIGN(sizeof_priv, NETDEV_ALIGN) + sizeof(struct can_ml_priv); 261 262 if (echo_skb_max) 263 size = ALIGN(size, sizeof(struct sk_buff *)) + 264 echo_skb_max * sizeof(struct sk_buff *); 265 266 dev = alloc_netdev_mqs(size, "can%d", NET_NAME_UNKNOWN, can_setup, 267 txqs, rxqs); 268 if (!dev) 269 return NULL; 270 271 priv = netdev_priv(dev); 272 priv->dev = dev; 273 274 dev->ml_priv = (void *)priv + ALIGN(sizeof_priv, NETDEV_ALIGN); 275 276 if (echo_skb_max) { 277 priv->echo_skb_max = echo_skb_max; 278 priv->echo_skb = (void *)priv + 279 (size - echo_skb_max * sizeof(struct sk_buff *)); 280 } 281 282 priv->state = CAN_STATE_STOPPED; 283 284 INIT_DELAYED_WORK(&priv->restart_work, can_restart_work); 285 286 return dev; 287 } 288 EXPORT_SYMBOL_GPL(alloc_candev_mqs); 289 290 /* Free space of the CAN network device */ 291 void free_candev(struct net_device *dev) 292 { 293 free_netdev(dev); 294 } 295 EXPORT_SYMBOL_GPL(free_candev); 296 297 /* changing MTU and control mode for CAN/CANFD devices */ 298 int can_change_mtu(struct net_device *dev, int new_mtu) 299 { 300 struct can_priv *priv = netdev_priv(dev); 301 302 /* Do not allow changing the MTU while running */ 303 if (dev->flags & IFF_UP) 304 return -EBUSY; 305 306 /* allow change of MTU according to the CANFD ability of the device */ 307 switch (new_mtu) { 308 case CAN_MTU: 309 /* 'CANFD-only' controllers can not switch to CAN_MTU */ 310 if (priv->ctrlmode_static & CAN_CTRLMODE_FD) 311 return -EINVAL; 312 313 priv->ctrlmode &= ~CAN_CTRLMODE_FD; 314 break; 315 316 case CANFD_MTU: 317 /* check for potential CANFD ability */ 318 if (!(priv->ctrlmode_supported & CAN_CTRLMODE_FD) && 319 !(priv->ctrlmode_static & CAN_CTRLMODE_FD)) 320 return -EINVAL; 321 322 priv->ctrlmode |= CAN_CTRLMODE_FD; 323 break; 324 325 default: 326 return -EINVAL; 327 } 328 329 dev->mtu = new_mtu; 330 return 0; 331 } 332 EXPORT_SYMBOL_GPL(can_change_mtu); 333 334 /* Common open function when the device gets opened. 335 * 336 * This function should be called in the open function of the device 337 * driver. 338 */ 339 int open_candev(struct net_device *dev) 340 { 341 struct can_priv *priv = netdev_priv(dev); 342 343 if (!priv->bittiming.bitrate) { 344 netdev_err(dev, "bit-timing not yet defined\n"); 345 return -EINVAL; 346 } 347 348 /* For CAN FD the data bitrate has to be >= the arbitration bitrate */ 349 if ((priv->ctrlmode & CAN_CTRLMODE_FD) && 350 (!priv->data_bittiming.bitrate || 351 priv->data_bittiming.bitrate < priv->bittiming.bitrate)) { 352 netdev_err(dev, "incorrect/missing data bit-timing\n"); 353 return -EINVAL; 354 } 355 356 /* Switch carrier on if device was stopped while in bus-off state */ 357 if (!netif_carrier_ok(dev)) 358 netif_carrier_on(dev); 359 360 return 0; 361 } 362 EXPORT_SYMBOL_GPL(open_candev); 363 364 #ifdef CONFIG_OF 365 /* Common function that can be used to understand the limitation of 366 * a transceiver when it provides no means to determine these limitations 367 * at runtime. 368 */ 369 void of_can_transceiver(struct net_device *dev) 370 { 371 struct device_node *dn; 372 struct can_priv *priv = netdev_priv(dev); 373 struct device_node *np = dev->dev.parent->of_node; 374 int ret; 375 376 dn = of_get_child_by_name(np, "can-transceiver"); 377 if (!dn) 378 return; 379 380 ret = of_property_read_u32(dn, "max-bitrate", &priv->bitrate_max); 381 of_node_put(dn); 382 if ((ret && ret != -EINVAL) || (!ret && !priv->bitrate_max)) 383 netdev_warn(dev, "Invalid value for transceiver max bitrate. Ignoring bitrate limit.\n"); 384 } 385 EXPORT_SYMBOL_GPL(of_can_transceiver); 386 #endif 387 388 /* Common close function for cleanup before the device gets closed. 389 * 390 * This function should be called in the close function of the device 391 * driver. 392 */ 393 void close_candev(struct net_device *dev) 394 { 395 struct can_priv *priv = netdev_priv(dev); 396 397 cancel_delayed_work_sync(&priv->restart_work); 398 can_flush_echo_skb(dev); 399 } 400 EXPORT_SYMBOL_GPL(close_candev); 401 402 /* CAN netlink interface */ 403 static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = { 404 [IFLA_CAN_STATE] = { .type = NLA_U32 }, 405 [IFLA_CAN_CTRLMODE] = { .len = sizeof(struct can_ctrlmode) }, 406 [IFLA_CAN_RESTART_MS] = { .type = NLA_U32 }, 407 [IFLA_CAN_RESTART] = { .type = NLA_U32 }, 408 [IFLA_CAN_BITTIMING] = { .len = sizeof(struct can_bittiming) }, 409 [IFLA_CAN_BITTIMING_CONST] 410 = { .len = sizeof(struct can_bittiming_const) }, 411 [IFLA_CAN_CLOCK] = { .len = sizeof(struct can_clock) }, 412 [IFLA_CAN_BERR_COUNTER] = { .len = sizeof(struct can_berr_counter) }, 413 [IFLA_CAN_DATA_BITTIMING] 414 = { .len = sizeof(struct can_bittiming) }, 415 [IFLA_CAN_DATA_BITTIMING_CONST] 416 = { .len = sizeof(struct can_bittiming_const) }, 417 [IFLA_CAN_TERMINATION] = { .type = NLA_U16 }, 418 }; 419 420 static int can_validate(struct nlattr *tb[], struct nlattr *data[], 421 struct netlink_ext_ack *extack) 422 { 423 bool is_can_fd = false; 424 425 /* Make sure that valid CAN FD configurations always consist of 426 * - nominal/arbitration bittiming 427 * - data bittiming 428 * - control mode with CAN_CTRLMODE_FD set 429 */ 430 431 if (!data) 432 return 0; 433 434 if (data[IFLA_CAN_CTRLMODE]) { 435 struct can_ctrlmode *cm = nla_data(data[IFLA_CAN_CTRLMODE]); 436 437 is_can_fd = cm->flags & cm->mask & CAN_CTRLMODE_FD; 438 } 439 440 if (is_can_fd) { 441 if (!data[IFLA_CAN_BITTIMING] || !data[IFLA_CAN_DATA_BITTIMING]) 442 return -EOPNOTSUPP; 443 } 444 445 if (data[IFLA_CAN_DATA_BITTIMING]) { 446 if (!is_can_fd || !data[IFLA_CAN_BITTIMING]) 447 return -EOPNOTSUPP; 448 } 449 450 return 0; 451 } 452 453 static int can_changelink(struct net_device *dev, struct nlattr *tb[], 454 struct nlattr *data[], 455 struct netlink_ext_ack *extack) 456 { 457 struct can_priv *priv = netdev_priv(dev); 458 int err; 459 460 /* We need synchronization with dev->stop() */ 461 ASSERT_RTNL(); 462 463 if (data[IFLA_CAN_BITTIMING]) { 464 struct can_bittiming bt; 465 466 /* Do not allow changing bittiming while running */ 467 if (dev->flags & IFF_UP) 468 return -EBUSY; 469 470 /* Calculate bittiming parameters based on 471 * bittiming_const if set, otherwise pass bitrate 472 * directly via do_set_bitrate(). Bail out if neither 473 * is given. 474 */ 475 if (!priv->bittiming_const && !priv->do_set_bittiming) 476 return -EOPNOTSUPP; 477 478 memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt)); 479 err = can_get_bittiming(dev, &bt, 480 priv->bittiming_const, 481 priv->bitrate_const, 482 priv->bitrate_const_cnt); 483 if (err) 484 return err; 485 486 if (priv->bitrate_max && bt.bitrate > priv->bitrate_max) { 487 netdev_err(dev, "arbitration bitrate surpasses transceiver capabilities of %d bps\n", 488 priv->bitrate_max); 489 return -EINVAL; 490 } 491 492 memcpy(&priv->bittiming, &bt, sizeof(bt)); 493 494 if (priv->do_set_bittiming) { 495 /* Finally, set the bit-timing registers */ 496 err = priv->do_set_bittiming(dev); 497 if (err) 498 return err; 499 } 500 } 501 502 if (data[IFLA_CAN_CTRLMODE]) { 503 struct can_ctrlmode *cm; 504 u32 ctrlstatic; 505 u32 maskedflags; 506 507 /* Do not allow changing controller mode while running */ 508 if (dev->flags & IFF_UP) 509 return -EBUSY; 510 cm = nla_data(data[IFLA_CAN_CTRLMODE]); 511 ctrlstatic = priv->ctrlmode_static; 512 maskedflags = cm->flags & cm->mask; 513 514 /* check whether provided bits are allowed to be passed */ 515 if (cm->mask & ~(priv->ctrlmode_supported | ctrlstatic)) 516 return -EOPNOTSUPP; 517 518 /* do not check for static fd-non-iso if 'fd' is disabled */ 519 if (!(maskedflags & CAN_CTRLMODE_FD)) 520 ctrlstatic &= ~CAN_CTRLMODE_FD_NON_ISO; 521 522 /* make sure static options are provided by configuration */ 523 if ((maskedflags & ctrlstatic) != ctrlstatic) 524 return -EOPNOTSUPP; 525 526 /* clear bits to be modified and copy the flag values */ 527 priv->ctrlmode &= ~cm->mask; 528 priv->ctrlmode |= maskedflags; 529 530 /* CAN_CTRLMODE_FD can only be set when driver supports FD */ 531 if (priv->ctrlmode & CAN_CTRLMODE_FD) 532 dev->mtu = CANFD_MTU; 533 else 534 dev->mtu = CAN_MTU; 535 } 536 537 if (data[IFLA_CAN_RESTART_MS]) { 538 /* Do not allow changing restart delay while running */ 539 if (dev->flags & IFF_UP) 540 return -EBUSY; 541 priv->restart_ms = nla_get_u32(data[IFLA_CAN_RESTART_MS]); 542 } 543 544 if (data[IFLA_CAN_RESTART]) { 545 /* Do not allow a restart while not running */ 546 if (!(dev->flags & IFF_UP)) 547 return -EINVAL; 548 err = can_restart_now(dev); 549 if (err) 550 return err; 551 } 552 553 if (data[IFLA_CAN_DATA_BITTIMING]) { 554 struct can_bittiming dbt; 555 556 /* Do not allow changing bittiming while running */ 557 if (dev->flags & IFF_UP) 558 return -EBUSY; 559 560 /* Calculate bittiming parameters based on 561 * data_bittiming_const if set, otherwise pass bitrate 562 * directly via do_set_bitrate(). Bail out if neither 563 * is given. 564 */ 565 if (!priv->data_bittiming_const && !priv->do_set_data_bittiming) 566 return -EOPNOTSUPP; 567 568 memcpy(&dbt, nla_data(data[IFLA_CAN_DATA_BITTIMING]), 569 sizeof(dbt)); 570 err = can_get_bittiming(dev, &dbt, 571 priv->data_bittiming_const, 572 priv->data_bitrate_const, 573 priv->data_bitrate_const_cnt); 574 if (err) 575 return err; 576 577 if (priv->bitrate_max && dbt.bitrate > priv->bitrate_max) { 578 netdev_err(dev, "canfd data bitrate surpasses transceiver capabilities of %d bps\n", 579 priv->bitrate_max); 580 return -EINVAL; 581 } 582 583 memcpy(&priv->data_bittiming, &dbt, sizeof(dbt)); 584 585 if (priv->do_set_data_bittiming) { 586 /* Finally, set the bit-timing registers */ 587 err = priv->do_set_data_bittiming(dev); 588 if (err) 589 return err; 590 } 591 } 592 593 if (data[IFLA_CAN_TERMINATION]) { 594 const u16 termval = nla_get_u16(data[IFLA_CAN_TERMINATION]); 595 const unsigned int num_term = priv->termination_const_cnt; 596 unsigned int i; 597 598 if (!priv->do_set_termination) 599 return -EOPNOTSUPP; 600 601 /* check whether given value is supported by the interface */ 602 for (i = 0; i < num_term; i++) { 603 if (termval == priv->termination_const[i]) 604 break; 605 } 606 if (i >= num_term) 607 return -EINVAL; 608 609 /* Finally, set the termination value */ 610 err = priv->do_set_termination(dev, termval); 611 if (err) 612 return err; 613 614 priv->termination = termval; 615 } 616 617 return 0; 618 } 619 620 static size_t can_get_size(const struct net_device *dev) 621 { 622 struct can_priv *priv = netdev_priv(dev); 623 size_t size = 0; 624 625 if (priv->bittiming.bitrate) /* IFLA_CAN_BITTIMING */ 626 size += nla_total_size(sizeof(struct can_bittiming)); 627 if (priv->bittiming_const) /* IFLA_CAN_BITTIMING_CONST */ 628 size += nla_total_size(sizeof(struct can_bittiming_const)); 629 size += nla_total_size(sizeof(struct can_clock)); /* IFLA_CAN_CLOCK */ 630 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_STATE */ 631 size += nla_total_size(sizeof(struct can_ctrlmode)); /* IFLA_CAN_CTRLMODE */ 632 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_RESTART_MS */ 633 if (priv->do_get_berr_counter) /* IFLA_CAN_BERR_COUNTER */ 634 size += nla_total_size(sizeof(struct can_berr_counter)); 635 if (priv->data_bittiming.bitrate) /* IFLA_CAN_DATA_BITTIMING */ 636 size += nla_total_size(sizeof(struct can_bittiming)); 637 if (priv->data_bittiming_const) /* IFLA_CAN_DATA_BITTIMING_CONST */ 638 size += nla_total_size(sizeof(struct can_bittiming_const)); 639 if (priv->termination_const) { 640 size += nla_total_size(sizeof(priv->termination)); /* IFLA_CAN_TERMINATION */ 641 size += nla_total_size(sizeof(*priv->termination_const) * /* IFLA_CAN_TERMINATION_CONST */ 642 priv->termination_const_cnt); 643 } 644 if (priv->bitrate_const) /* IFLA_CAN_BITRATE_CONST */ 645 size += nla_total_size(sizeof(*priv->bitrate_const) * 646 priv->bitrate_const_cnt); 647 if (priv->data_bitrate_const) /* IFLA_CAN_DATA_BITRATE_CONST */ 648 size += nla_total_size(sizeof(*priv->data_bitrate_const) * 649 priv->data_bitrate_const_cnt); 650 size += sizeof(priv->bitrate_max); /* IFLA_CAN_BITRATE_MAX */ 651 652 return size; 653 } 654 655 static int can_fill_info(struct sk_buff *skb, const struct net_device *dev) 656 { 657 struct can_priv *priv = netdev_priv(dev); 658 struct can_ctrlmode cm = {.flags = priv->ctrlmode}; 659 struct can_berr_counter bec; 660 enum can_state state = priv->state; 661 662 if (priv->do_get_state) 663 priv->do_get_state(dev, &state); 664 665 if ((priv->bittiming.bitrate && 666 nla_put(skb, IFLA_CAN_BITTIMING, 667 sizeof(priv->bittiming), &priv->bittiming)) || 668 669 (priv->bittiming_const && 670 nla_put(skb, IFLA_CAN_BITTIMING_CONST, 671 sizeof(*priv->bittiming_const), priv->bittiming_const)) || 672 673 nla_put(skb, IFLA_CAN_CLOCK, sizeof(priv->clock), &priv->clock) || 674 nla_put_u32(skb, IFLA_CAN_STATE, state) || 675 nla_put(skb, IFLA_CAN_CTRLMODE, sizeof(cm), &cm) || 676 nla_put_u32(skb, IFLA_CAN_RESTART_MS, priv->restart_ms) || 677 678 (priv->do_get_berr_counter && 679 !priv->do_get_berr_counter(dev, &bec) && 680 nla_put(skb, IFLA_CAN_BERR_COUNTER, sizeof(bec), &bec)) || 681 682 (priv->data_bittiming.bitrate && 683 nla_put(skb, IFLA_CAN_DATA_BITTIMING, 684 sizeof(priv->data_bittiming), &priv->data_bittiming)) || 685 686 (priv->data_bittiming_const && 687 nla_put(skb, IFLA_CAN_DATA_BITTIMING_CONST, 688 sizeof(*priv->data_bittiming_const), 689 priv->data_bittiming_const)) || 690 691 (priv->termination_const && 692 (nla_put_u16(skb, IFLA_CAN_TERMINATION, priv->termination) || 693 nla_put(skb, IFLA_CAN_TERMINATION_CONST, 694 sizeof(*priv->termination_const) * 695 priv->termination_const_cnt, 696 priv->termination_const))) || 697 698 (priv->bitrate_const && 699 nla_put(skb, IFLA_CAN_BITRATE_CONST, 700 sizeof(*priv->bitrate_const) * 701 priv->bitrate_const_cnt, 702 priv->bitrate_const)) || 703 704 (priv->data_bitrate_const && 705 nla_put(skb, IFLA_CAN_DATA_BITRATE_CONST, 706 sizeof(*priv->data_bitrate_const) * 707 priv->data_bitrate_const_cnt, 708 priv->data_bitrate_const)) || 709 710 (nla_put(skb, IFLA_CAN_BITRATE_MAX, 711 sizeof(priv->bitrate_max), 712 &priv->bitrate_max)) 713 ) 714 715 return -EMSGSIZE; 716 717 return 0; 718 } 719 720 static size_t can_get_xstats_size(const struct net_device *dev) 721 { 722 return sizeof(struct can_device_stats); 723 } 724 725 static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev) 726 { 727 struct can_priv *priv = netdev_priv(dev); 728 729 if (nla_put(skb, IFLA_INFO_XSTATS, 730 sizeof(priv->can_stats), &priv->can_stats)) 731 goto nla_put_failure; 732 return 0; 733 734 nla_put_failure: 735 return -EMSGSIZE; 736 } 737 738 static int can_newlink(struct net *src_net, struct net_device *dev, 739 struct nlattr *tb[], struct nlattr *data[], 740 struct netlink_ext_ack *extack) 741 { 742 return -EOPNOTSUPP; 743 } 744 745 static void can_dellink(struct net_device *dev, struct list_head *head) 746 { 747 } 748 749 static struct rtnl_link_ops can_link_ops __read_mostly = { 750 .kind = "can", 751 .maxtype = IFLA_CAN_MAX, 752 .policy = can_policy, 753 .setup = can_setup, 754 .validate = can_validate, 755 .newlink = can_newlink, 756 .changelink = can_changelink, 757 .dellink = can_dellink, 758 .get_size = can_get_size, 759 .fill_info = can_fill_info, 760 .get_xstats_size = can_get_xstats_size, 761 .fill_xstats = can_fill_xstats, 762 }; 763 764 /* Register the CAN network device */ 765 int register_candev(struct net_device *dev) 766 { 767 struct can_priv *priv = netdev_priv(dev); 768 769 /* Ensure termination_const, termination_const_cnt and 770 * do_set_termination consistency. All must be either set or 771 * unset. 772 */ 773 if ((!priv->termination_const != !priv->termination_const_cnt) || 774 (!priv->termination_const != !priv->do_set_termination)) 775 return -EINVAL; 776 777 if (!priv->bitrate_const != !priv->bitrate_const_cnt) 778 return -EINVAL; 779 780 if (!priv->data_bitrate_const != !priv->data_bitrate_const_cnt) 781 return -EINVAL; 782 783 dev->rtnl_link_ops = &can_link_ops; 784 netif_carrier_off(dev); 785 786 return register_netdev(dev); 787 } 788 EXPORT_SYMBOL_GPL(register_candev); 789 790 /* Unregister the CAN network device */ 791 void unregister_candev(struct net_device *dev) 792 { 793 unregister_netdev(dev); 794 } 795 EXPORT_SYMBOL_GPL(unregister_candev); 796 797 /* Test if a network device is a candev based device 798 * and return the can_priv* if so. 799 */ 800 struct can_priv *safe_candev_priv(struct net_device *dev) 801 { 802 if (dev->type != ARPHRD_CAN || dev->rtnl_link_ops != &can_link_ops) 803 return NULL; 804 805 return netdev_priv(dev); 806 } 807 EXPORT_SYMBOL_GPL(safe_candev_priv); 808 809 static __init int can_dev_init(void) 810 { 811 int err; 812 813 can_led_notifier_init(); 814 815 err = rtnl_link_register(&can_link_ops); 816 if (!err) 817 pr_info(MOD_DESC "\n"); 818 819 return err; 820 } 821 module_init(can_dev_init); 822 823 static __exit void can_dev_exit(void) 824 { 825 rtnl_link_unregister(&can_link_ops); 826 827 can_led_notifier_exit(); 828 } 829 module_exit(can_dev_exit); 830 831 MODULE_ALIAS_RTNL_LINK("can"); 832