Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

canfd: update documentation according to CAN FD extensions

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>

authored by

Oliver Hartkopp and committed by
Marc Kleine-Budde
ea53fe0c 41052ef6

+146 -8
+146 -8
Documentation/networking/can.txt
··· 22 22 4.1.2 RAW socket option CAN_RAW_ERR_FILTER 23 23 4.1.3 RAW socket option CAN_RAW_LOOPBACK 24 24 4.1.4 RAW socket option CAN_RAW_RECV_OWN_MSGS 25 - 4.1.5 RAW socket returned message flags 25 + 4.1.5 RAW socket option CAN_RAW_FD_FRAMES 26 + 4.1.6 RAW socket returned message flags 26 27 4.2 Broadcast Manager protocol sockets (SOCK_DGRAM) 27 28 4.3 connected transport protocols (SOCK_SEQPACKET) 28 29 4.4 unconnected transport protocols (SOCK_DGRAM) ··· 42 41 6.5.1 Netlink interface to set/get devices properties 43 42 6.5.2 Setting the CAN bit-timing 44 43 6.5.3 Starting and stopping the CAN network device 45 - 6.6 supported CAN hardware 44 + 6.6 CAN FD (flexible data rate) driver support 45 + 6.7 supported CAN hardware 46 46 47 47 7 Socket CAN resources 48 48 ··· 275 273 276 274 struct can_frame { 277 275 canid_t can_id; /* 32 bit CAN_ID + EFF/RTR/ERR flags */ 278 - __u8 can_dlc; /* data length code: 0 .. 8 */ 276 + __u8 can_dlc; /* frame payload length in byte (0 .. 8) */ 279 277 __u8 data[8] __attribute__((aligned(8))); 280 278 }; 281 279 ··· 377 375 nbytes = sendto(s, &frame, sizeof(struct can_frame), 378 376 0, (struct sockaddr*)&addr, sizeof(addr)); 379 377 378 + Remark about CAN FD (flexible data rate) support: 379 + 380 + Generally the handling of CAN FD is very similar to the formerly described 381 + examples. The new CAN FD capable CAN controllers support two different 382 + bitrates for the arbitration phase and the payload phase of the CAN FD frame 383 + and up to 64 bytes of payload. This extended payload length breaks all the 384 + kernel interfaces (ABI) which heavily rely on the CAN frame with fixed eight 385 + bytes of payload (struct can_frame) like the CAN_RAW socket. Therefore e.g. 386 + the CAN_RAW socket supports a new socket option CAN_RAW_FD_FRAMES that 387 + switches the socket into a mode that allows the handling of CAN FD frames 388 + and (legacy) CAN frames simultaneously (see section 4.1.5). 389 + 390 + The struct canfd_frame is defined in include/linux/can.h: 391 + 392 + struct canfd_frame { 393 + canid_t can_id; /* 32 bit CAN_ID + EFF/RTR/ERR flags */ 394 + __u8 len; /* frame payload length in byte (0 .. 64) */ 395 + __u8 flags; /* additional flags for CAN FD */ 396 + __u8 __res0; /* reserved / padding */ 397 + __u8 __res1; /* reserved / padding */ 398 + __u8 data[64] __attribute__((aligned(8))); 399 + }; 400 + 401 + The struct canfd_frame and the existing struct can_frame have the can_id, 402 + the payload length and the payload data at the same offset inside their 403 + structures. This allows to handle the different structures very similar. 404 + When the content of a struct can_frame is copied into a struct canfd_frame 405 + all structure elements can be used as-is - only the data[] becomes extended. 406 + 407 + When introducing the struct canfd_frame it turned out that the data length 408 + code (DLC) of the struct can_frame was used as a length information as the 409 + length and the DLC has a 1:1 mapping in the range of 0 .. 8. To preserve 410 + the easy handling of the length information the canfd_frame.len element 411 + contains a plain length value from 0 .. 64. So both canfd_frame.len and 412 + can_frame.can_dlc are equal and contain a length information and no DLC. 413 + For details about the distinction of CAN and CAN FD capable devices and 414 + the mapping to the bus-relevant data length code (DLC), see chapter 6.6. 415 + 416 + The length of the two CAN(FD) frame structures define the maximum transfer 417 + unit (MTU) of the CAN(FD) network interface and skbuff data length. Two 418 + definitions are specified for CAN specific MTUs in include/linux/can.h : 419 + 420 + #define CAN_MTU (sizeof(struct can_frame)) == 16 => 'legacy' CAN frame 421 + #define CANFD_MTU (sizeof(struct canfd_frame)) == 72 => CAN FD frame 422 + 380 423 4.1 RAW protocol sockets with can_filters (SOCK_RAW) 381 424 382 425 Using CAN_RAW sockets is extensively comparable to the commonly ··· 519 472 setsockopt(s, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS, 520 473 &recv_own_msgs, sizeof(recv_own_msgs)); 521 474 522 - 4.1.5 RAW socket returned message flags 475 + 4.1.5 RAW socket option CAN_RAW_FD_FRAMES 476 + 477 + CAN FD support in CAN_RAW sockets can be enabled with a new socket option 478 + CAN_RAW_FD_FRAMES which is off by default. When the new socket option is 479 + not supported by the CAN_RAW socket (e.g. on older kernels), switching the 480 + CAN_RAW_FD_FRAMES option returns the error -ENOPROTOOPT. 481 + 482 + Once CAN_RAW_FD_FRAMES is enabled the application can send both CAN frames 483 + and CAN FD frames. OTOH the application has to handle CAN and CAN FD frames 484 + when reading from the socket. 485 + 486 + CAN_RAW_FD_FRAMES enabled: CAN_MTU and CANFD_MTU are allowed 487 + CAN_RAW_FD_FRAMES disabled: only CAN_MTU is allowed (default) 488 + 489 + Example: 490 + [ remember: CANFD_MTU == sizeof(struct canfd_frame) ] 491 + 492 + struct canfd_frame cfd; 493 + 494 + nbytes = read(s, &cfd, CANFD_MTU); 495 + 496 + if (nbytes == CANFD_MTU) { 497 + printf("got CAN FD frame with length %d\n", cfd.len); 498 + /* cfd.flags contains valid data */ 499 + } else if (nbytes == CAN_MTU) { 500 + printf("got legacy CAN frame with length %d\n", cfd.len); 501 + /* cfd.flags is undefined */ 502 + } else { 503 + fprintf(stderr, "read: invalid CAN(FD) frame\n"); 504 + return 1; 505 + } 506 + 507 + /* the content can be handled independently from the received MTU size */ 508 + 509 + printf("can_id: %X data length: %d data: ", cfd.can_id, cfd.len); 510 + for (i = 0; i < cfd.len; i++) 511 + printf("%02X ", cfd.data[i]); 512 + 513 + When reading with size CANFD_MTU only returns CAN_MTU bytes that have 514 + been received from the socket a legacy CAN frame has been read into the 515 + provided CAN FD structure. Note that the canfd_frame.flags data field is 516 + not specified in the struct can_frame and therefore it is only valid in 517 + CANFD_MTU sized CAN FD frames. 518 + 519 + As long as the payload length is <=8 the received CAN frames from CAN FD 520 + capable CAN devices can be received and read by legacy sockets too. When 521 + user-generated CAN FD frames have a payload length <=8 these can be send 522 + by legacy CAN network interfaces too. Sending CAN FD frames with payload 523 + length > 8 to a legacy CAN network interface returns an -EMSGSIZE error. 524 + 525 + Implementation hint for new CAN applications: 526 + 527 + To build a CAN FD aware application use struct canfd_frame as basic CAN 528 + data structure for CAN_RAW based applications. When the application is 529 + executed on an older Linux kernel and switching the CAN_RAW_FD_FRAMES 530 + socket option returns an error: No problem. You'll get legacy CAN frames 531 + or CAN FD frames and can process them the same way. 532 + 533 + When sending to CAN devices make sure that the device is capable to handle 534 + CAN FD frames by checking if the device maximum transfer unit is CANFD_MTU. 535 + The CAN device MTU can be retrieved e.g. with a SIOCGIFMTU ioctl() syscall. 536 + 537 + 4.1.6 RAW socket returned message flags 523 538 524 539 When using recvmsg() call, the msg->msg_flags may contain following flags: 525 540 ··· 682 573 dev->type = ARPHRD_CAN; /* the netdevice hardware type */ 683 574 dev->flags = IFF_NOARP; /* CAN has no arp */ 684 575 685 - dev->mtu = sizeof(struct can_frame); 576 + dev->mtu = CAN_MTU; /* sizeof(struct can_frame) -> legacy CAN interface */ 686 577 687 - The struct can_frame is the payload of each socket buffer in the 688 - protocol family PF_CAN. 578 + or alternative, when the controller supports CAN with flexible data rate: 579 + dev->mtu = CANFD_MTU; /* sizeof(struct canfd_frame) -> CAN FD interface */ 580 + 581 + The struct can_frame or struct canfd_frame is the payload of each socket 582 + buffer (skbuff) in the protocol family PF_CAN. 689 583 690 584 6.2 local loopback of sent frames 691 585 ··· 904 792 Note that a restart will also create a CAN error message frame (see 905 793 also chapter 3.4). 906 794 907 - 6.6 Supported CAN hardware 795 + 6.6 CAN FD (flexible data rate) driver support 796 + 797 + CAN FD capable CAN controllers support two different bitrates for the 798 + arbitration phase and the payload phase of the CAN FD frame. Therefore a 799 + second bittiming has to be specified in order to enable the CAN FD bitrate. 800 + 801 + Additionally CAN FD capable CAN controllers support up to 64 bytes of 802 + payload. The representation of this length in can_frame.can_dlc and 803 + canfd_frame.len for userspace applications and inside the Linux network 804 + layer is a plain value from 0 .. 64 instead of the CAN 'data length code'. 805 + The data length code was a 1:1 mapping to the payload length in the legacy 806 + CAN frames anyway. The payload length to the bus-relevant DLC mapping is 807 + only performed inside the CAN drivers, preferably with the helper 808 + functions can_dlc2len() and can_len2dlc(). 809 + 810 + The CAN netdevice driver capabilities can be distinguished by the network 811 + devices maximum transfer unit (MTU): 812 + 813 + MTU = 16 (CAN_MTU) => sizeof(struct can_frame) => 'legacy' CAN device 814 + MTU = 72 (CANFD_MTU) => sizeof(struct canfd_frame) => CAN FD capable device 815 + 816 + The CAN device MTU can be retrieved e.g. with a SIOCGIFMTU ioctl() syscall. 817 + N.B. CAN FD capable devices can also handle and send legacy CAN frames. 818 + 819 + FIXME: Add details about the CAN FD controller configuration when available. 820 + 821 + 6.7 Supported CAN hardware 908 822 909 823 Please check the "Kconfig" file in "drivers/net/can" to get an actual 910 824 list of the support CAN hardware. On the Socket CAN project website