diff options
Diffstat (limited to 'Documentation/networking')
-rw-r--r-- | Documentation/networking/can.txt | 154 |
1 files changed, 146 insertions, 8 deletions
diff --git a/Documentation/networking/can.txt b/Documentation/networking/can.txt index a06741898f29..820f55344edc 100644 --- a/Documentation/networking/can.txt +++ b/Documentation/networking/can.txt | |||
@@ -22,7 +22,8 @@ This file contains | |||
22 | 4.1.2 RAW socket option CAN_RAW_ERR_FILTER | 22 | 4.1.2 RAW socket option CAN_RAW_ERR_FILTER |
23 | 4.1.3 RAW socket option CAN_RAW_LOOPBACK | 23 | 4.1.3 RAW socket option CAN_RAW_LOOPBACK |
24 | 4.1.4 RAW socket option CAN_RAW_RECV_OWN_MSGS | 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 | 4.2 Broadcast Manager protocol sockets (SOCK_DGRAM) | 27 | 4.2 Broadcast Manager protocol sockets (SOCK_DGRAM) |
27 | 4.3 connected transport protocols (SOCK_SEQPACKET) | 28 | 4.3 connected transport protocols (SOCK_SEQPACKET) |
28 | 4.4 unconnected transport protocols (SOCK_DGRAM) | 29 | 4.4 unconnected transport protocols (SOCK_DGRAM) |
@@ -41,7 +42,8 @@ This file contains | |||
41 | 6.5.1 Netlink interface to set/get devices properties | 42 | 6.5.1 Netlink interface to set/get devices properties |
42 | 6.5.2 Setting the CAN bit-timing | 43 | 6.5.2 Setting the CAN bit-timing |
43 | 6.5.3 Starting and stopping the CAN network device | 44 | 6.5.3 Starting and stopping the CAN network device |
44 | 6.6 supported CAN hardware | 45 | 6.6 CAN FD (flexible data rate) driver support |
46 | 6.7 supported CAN hardware | ||
45 | 47 | ||
46 | 7 Socket CAN resources | 48 | 7 Socket CAN resources |
47 | 49 | ||
@@ -273,7 +275,7 @@ solution for a couple of reasons: | |||
273 | 275 | ||
274 | struct can_frame { | 276 | struct can_frame { |
275 | canid_t can_id; /* 32 bit CAN_ID + EFF/RTR/ERR flags */ | 277 | canid_t can_id; /* 32 bit CAN_ID + EFF/RTR/ERR flags */ |
276 | __u8 can_dlc; /* data length code: 0 .. 8 */ | 278 | __u8 can_dlc; /* frame payload length in byte (0 .. 8) */ |
277 | __u8 data[8] __attribute__((aligned(8))); | 279 | __u8 data[8] __attribute__((aligned(8))); |
278 | }; | 280 | }; |
279 | 281 | ||
@@ -375,6 +377,51 @@ solution for a couple of reasons: | |||
375 | nbytes = sendto(s, &frame, sizeof(struct can_frame), | 377 | nbytes = sendto(s, &frame, sizeof(struct can_frame), |
376 | 0, (struct sockaddr*)&addr, sizeof(addr)); | 378 | 0, (struct sockaddr*)&addr, sizeof(addr)); |
377 | 379 | ||
380 | Remark about CAN FD (flexible data rate) support: | ||
381 | |||
382 | Generally the handling of CAN FD is very similar to the formerly described | ||
383 | examples. The new CAN FD capable CAN controllers support two different | ||
384 | bitrates for the arbitration phase and the payload phase of the CAN FD frame | ||
385 | and up to 64 bytes of payload. This extended payload length breaks all the | ||
386 | kernel interfaces (ABI) which heavily rely on the CAN frame with fixed eight | ||
387 | bytes of payload (struct can_frame) like the CAN_RAW socket. Therefore e.g. | ||
388 | the CAN_RAW socket supports a new socket option CAN_RAW_FD_FRAMES that | ||
389 | switches the socket into a mode that allows the handling of CAN FD frames | ||
390 | and (legacy) CAN frames simultaneously (see section 4.1.5). | ||
391 | |||
392 | The struct canfd_frame is defined in include/linux/can.h: | ||
393 | |||
394 | struct canfd_frame { | ||
395 | canid_t can_id; /* 32 bit CAN_ID + EFF/RTR/ERR flags */ | ||
396 | __u8 len; /* frame payload length in byte (0 .. 64) */ | ||
397 | __u8 flags; /* additional flags for CAN FD */ | ||
398 | __u8 __res0; /* reserved / padding */ | ||
399 | __u8 __res1; /* reserved / padding */ | ||
400 | __u8 data[64] __attribute__((aligned(8))); | ||
401 | }; | ||
402 | |||
403 | The struct canfd_frame and the existing struct can_frame have the can_id, | ||
404 | the payload length and the payload data at the same offset inside their | ||
405 | structures. This allows to handle the different structures very similar. | ||
406 | When the content of a struct can_frame is copied into a struct canfd_frame | ||
407 | all structure elements can be used as-is - only the data[] becomes extended. | ||
408 | |||
409 | When introducing the struct canfd_frame it turned out that the data length | ||
410 | code (DLC) of the struct can_frame was used as a length information as the | ||
411 | length and the DLC has a 1:1 mapping in the range of 0 .. 8. To preserve | ||
412 | the easy handling of the length information the canfd_frame.len element | ||
413 | contains a plain length value from 0 .. 64. So both canfd_frame.len and | ||
414 | can_frame.can_dlc are equal and contain a length information and no DLC. | ||
415 | For details about the distinction of CAN and CAN FD capable devices and | ||
416 | the mapping to the bus-relevant data length code (DLC), see chapter 6.6. | ||
417 | |||
418 | The length of the two CAN(FD) frame structures define the maximum transfer | ||
419 | unit (MTU) of the CAN(FD) network interface and skbuff data length. Two | ||
420 | definitions are specified for CAN specific MTUs in include/linux/can.h : | ||
421 | |||
422 | #define CAN_MTU (sizeof(struct can_frame)) == 16 => 'legacy' CAN frame | ||
423 | #define CANFD_MTU (sizeof(struct canfd_frame)) == 72 => CAN FD frame | ||
424 | |||
378 | 4.1 RAW protocol sockets with can_filters (SOCK_RAW) | 425 | 4.1 RAW protocol sockets with can_filters (SOCK_RAW) |
379 | 426 | ||
380 | Using CAN_RAW sockets is extensively comparable to the commonly | 427 | Using CAN_RAW sockets is extensively comparable to the commonly |
@@ -472,7 +519,69 @@ solution for a couple of reasons: | |||
472 | setsockopt(s, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS, | 519 | setsockopt(s, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS, |
473 | &recv_own_msgs, sizeof(recv_own_msgs)); | 520 | &recv_own_msgs, sizeof(recv_own_msgs)); |
474 | 521 | ||
475 | 4.1.5 RAW socket returned message flags | 522 | 4.1.5 RAW socket option CAN_RAW_FD_FRAMES |
523 | |||
524 | CAN FD support in CAN_RAW sockets can be enabled with a new socket option | ||
525 | CAN_RAW_FD_FRAMES which is off by default. When the new socket option is | ||
526 | not supported by the CAN_RAW socket (e.g. on older kernels), switching the | ||
527 | CAN_RAW_FD_FRAMES option returns the error -ENOPROTOOPT. | ||
528 | |||
529 | Once CAN_RAW_FD_FRAMES is enabled the application can send both CAN frames | ||
530 | and CAN FD frames. OTOH the application has to handle CAN and CAN FD frames | ||
531 | when reading from the socket. | ||
532 | |||
533 | CAN_RAW_FD_FRAMES enabled: CAN_MTU and CANFD_MTU are allowed | ||
534 | CAN_RAW_FD_FRAMES disabled: only CAN_MTU is allowed (default) | ||
535 | |||
536 | Example: | ||
537 | [ remember: CANFD_MTU == sizeof(struct canfd_frame) ] | ||
538 | |||
539 | struct canfd_frame cfd; | ||
540 | |||
541 | nbytes = read(s, &cfd, CANFD_MTU); | ||
542 | |||
543 | if (nbytes == CANFD_MTU) { | ||
544 | printf("got CAN FD frame with length %d\n", cfd.len); | ||
545 | /* cfd.flags contains valid data */ | ||
546 | } else if (nbytes == CAN_MTU) { | ||
547 | printf("got legacy CAN frame with length %d\n", cfd.len); | ||
548 | /* cfd.flags is undefined */ | ||
549 | } else { | ||
550 | fprintf(stderr, "read: invalid CAN(FD) frame\n"); | ||
551 | return 1; | ||
552 | } | ||
553 | |||
554 | /* the content can be handled independently from the received MTU size */ | ||
555 | |||
556 | printf("can_id: %X data length: %d data: ", cfd.can_id, cfd.len); | ||
557 | for (i = 0; i < cfd.len; i++) | ||
558 | printf("%02X ", cfd.data[i]); | ||
559 | |||
560 | When reading with size CANFD_MTU only returns CAN_MTU bytes that have | ||
561 | been received from the socket a legacy CAN frame has been read into the | ||
562 | provided CAN FD structure. Note that the canfd_frame.flags data field is | ||
563 | not specified in the struct can_frame and therefore it is only valid in | ||
564 | CANFD_MTU sized CAN FD frames. | ||
565 | |||
566 | As long as the payload length is <=8 the received CAN frames from CAN FD | ||
567 | capable CAN devices can be received and read by legacy sockets too. When | ||
568 | user-generated CAN FD frames have a payload length <=8 these can be send | ||
569 | by legacy CAN network interfaces too. Sending CAN FD frames with payload | ||
570 | length > 8 to a legacy CAN network interface returns an -EMSGSIZE error. | ||
571 | |||
572 | Implementation hint for new CAN applications: | ||
573 | |||
574 | To build a CAN FD aware application use struct canfd_frame as basic CAN | ||
575 | data structure for CAN_RAW based applications. When the application is | ||
576 | executed on an older Linux kernel and switching the CAN_RAW_FD_FRAMES | ||
577 | socket option returns an error: No problem. You'll get legacy CAN frames | ||
578 | or CAN FD frames and can process them the same way. | ||
579 | |||
580 | When sending to CAN devices make sure that the device is capable to handle | ||
581 | CAN FD frames by checking if the device maximum transfer unit is CANFD_MTU. | ||
582 | The CAN device MTU can be retrieved e.g. with a SIOCGIFMTU ioctl() syscall. | ||
583 | |||
584 | 4.1.6 RAW socket returned message flags | ||
476 | 585 | ||
477 | When using recvmsg() call, the msg->msg_flags may contain following flags: | 586 | When using recvmsg() call, the msg->msg_flags may contain following flags: |
478 | 587 | ||
@@ -573,10 +682,13 @@ solution for a couple of reasons: | |||
573 | dev->type = ARPHRD_CAN; /* the netdevice hardware type */ | 682 | dev->type = ARPHRD_CAN; /* the netdevice hardware type */ |
574 | dev->flags = IFF_NOARP; /* CAN has no arp */ | 683 | dev->flags = IFF_NOARP; /* CAN has no arp */ |
575 | 684 | ||
576 | dev->mtu = sizeof(struct can_frame); | 685 | dev->mtu = CAN_MTU; /* sizeof(struct can_frame) -> legacy CAN interface */ |
577 | 686 | ||
578 | The struct can_frame is the payload of each socket buffer in the | 687 | or alternative, when the controller supports CAN with flexible data rate: |
579 | protocol family PF_CAN. | 688 | dev->mtu = CANFD_MTU; /* sizeof(struct canfd_frame) -> CAN FD interface */ |
689 | |||
690 | The struct can_frame or struct canfd_frame is the payload of each socket | ||
691 | buffer (skbuff) in the protocol family PF_CAN. | ||
580 | 692 | ||
581 | 6.2 local loopback of sent frames | 693 | 6.2 local loopback of sent frames |
582 | 694 | ||
@@ -792,7 +904,33 @@ solution for a couple of reasons: | |||
792 | Note that a restart will also create a CAN error message frame (see | 904 | Note that a restart will also create a CAN error message frame (see |
793 | also chapter 3.4). | 905 | also chapter 3.4). |
794 | 906 | ||
795 | 6.6 Supported CAN hardware | 907 | 6.6 CAN FD (flexible data rate) driver support |
908 | |||
909 | CAN FD capable CAN controllers support two different bitrates for the | ||
910 | arbitration phase and the payload phase of the CAN FD frame. Therefore a | ||
911 | second bittiming has to be specified in order to enable the CAN FD bitrate. | ||
912 | |||
913 | Additionally CAN FD capable CAN controllers support up to 64 bytes of | ||
914 | payload. The representation of this length in can_frame.can_dlc and | ||
915 | canfd_frame.len for userspace applications and inside the Linux network | ||
916 | layer is a plain value from 0 .. 64 instead of the CAN 'data length code'. | ||
917 | The data length code was a 1:1 mapping to the payload length in the legacy | ||
918 | CAN frames anyway. The payload length to the bus-relevant DLC mapping is | ||
919 | only performed inside the CAN drivers, preferably with the helper | ||
920 | functions can_dlc2len() and can_len2dlc(). | ||
921 | |||
922 | The CAN netdevice driver capabilities can be distinguished by the network | ||
923 | devices maximum transfer unit (MTU): | ||
924 | |||
925 | MTU = 16 (CAN_MTU) => sizeof(struct can_frame) => 'legacy' CAN device | ||
926 | MTU = 72 (CANFD_MTU) => sizeof(struct canfd_frame) => CAN FD capable device | ||
927 | |||
928 | The CAN device MTU can be retrieved e.g. with a SIOCGIFMTU ioctl() syscall. | ||
929 | N.B. CAN FD capable devices can also handle and send legacy CAN frames. | ||
930 | |||
931 | FIXME: Add details about the CAN FD controller configuration when available. | ||
932 | |||
933 | 6.7 Supported CAN hardware | ||
796 | 934 | ||
797 | Please check the "Kconfig" file in "drivers/net/can" to get an actual | 935 | Please check the "Kconfig" file in "drivers/net/can" to get an actual |
798 | list of the support CAN hardware. On the Socket CAN project website | 936 | list of the support CAN hardware. On the Socket CAN project website |