···2222 4.1.2 RAW socket option CAN_RAW_ERR_FILTER2323 4.1.3 RAW socket option CAN_RAW_LOOPBACK2424 4.1.4 RAW socket option CAN_RAW_RECV_OWN_MSGS2525- 4.1.5 RAW socket returned message flags2525+ 4.1.5 RAW socket option CAN_RAW_FD_FRAMES2626+ 4.1.6 RAW socket returned message flags2627 4.2 Broadcast Manager protocol sockets (SOCK_DGRAM)2728 4.3 connected transport protocols (SOCK_SEQPACKET)2829 4.4 unconnected transport protocols (SOCK_DGRAM)···4241 6.5.1 Netlink interface to set/get devices properties4342 6.5.2 Setting the CAN bit-timing4443 6.5.3 Starting and stopping the CAN network device4545- 6.6 supported CAN hardware4444+ 6.6 CAN FD (flexible data rate) driver support4545+ 6.7 supported CAN hardware46464747 7 Socket CAN resources4848···275273276274 struct can_frame {277275 canid_t can_id; /* 32 bit CAN_ID + EFF/RTR/ERR flags */278278- __u8 can_dlc; /* data length code: 0 .. 8 */276276+ __u8 can_dlc; /* frame payload length in byte (0 .. 8) */279277 __u8 data[8] __attribute__((aligned(8)));280278 };281279···377375 nbytes = sendto(s, &frame, sizeof(struct can_frame),378376 0, (struct sockaddr*)&addr, sizeof(addr));379377378378+ Remark about CAN FD (flexible data rate) support:379379+380380+ Generally the handling of CAN FD is very similar to the formerly described381381+ examples. The new CAN FD capable CAN controllers support two different382382+ bitrates for the arbitration phase and the payload phase of the CAN FD frame383383+ and up to 64 bytes of payload. This extended payload length breaks all the384384+ kernel interfaces (ABI) which heavily rely on the CAN frame with fixed eight385385+ bytes of payload (struct can_frame) like the CAN_RAW socket. Therefore e.g.386386+ the CAN_RAW socket supports a new socket option CAN_RAW_FD_FRAMES that387387+ switches the socket into a mode that allows the handling of CAN FD frames388388+ and (legacy) CAN frames simultaneously (see section 4.1.5).389389+390390+ The struct canfd_frame is defined in include/linux/can.h:391391+392392+ struct canfd_frame {393393+ canid_t can_id; /* 32 bit CAN_ID + EFF/RTR/ERR flags */394394+ __u8 len; /* frame payload length in byte (0 .. 64) */395395+ __u8 flags; /* additional flags for CAN FD */396396+ __u8 __res0; /* reserved / padding */397397+ __u8 __res1; /* reserved / padding */398398+ __u8 data[64] __attribute__((aligned(8)));399399+ };400400+401401+ The struct canfd_frame and the existing struct can_frame have the can_id,402402+ the payload length and the payload data at the same offset inside their403403+ structures. This allows to handle the different structures very similar.404404+ When the content of a struct can_frame is copied into a struct canfd_frame405405+ all structure elements can be used as-is - only the data[] becomes extended.406406+407407+ When introducing the struct canfd_frame it turned out that the data length408408+ code (DLC) of the struct can_frame was used as a length information as the409409+ length and the DLC has a 1:1 mapping in the range of 0 .. 8. To preserve410410+ the easy handling of the length information the canfd_frame.len element411411+ contains a plain length value from 0 .. 64. So both canfd_frame.len and412412+ can_frame.can_dlc are equal and contain a length information and no DLC.413413+ For details about the distinction of CAN and CAN FD capable devices and414414+ the mapping to the bus-relevant data length code (DLC), see chapter 6.6.415415+416416+ The length of the two CAN(FD) frame structures define the maximum transfer417417+ unit (MTU) of the CAN(FD) network interface and skbuff data length. Two418418+ definitions are specified for CAN specific MTUs in include/linux/can.h :419419+420420+ #define CAN_MTU (sizeof(struct can_frame)) == 16 => 'legacy' CAN frame421421+ #define CANFD_MTU (sizeof(struct canfd_frame)) == 72 => CAN FD frame422422+380423 4.1 RAW protocol sockets with can_filters (SOCK_RAW)381424382425 Using CAN_RAW sockets is extensively comparable to the commonly···519472 setsockopt(s, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,520473 &recv_own_msgs, sizeof(recv_own_msgs));521474522522- 4.1.5 RAW socket returned message flags475475+ 4.1.5 RAW socket option CAN_RAW_FD_FRAMES476476+477477+ CAN FD support in CAN_RAW sockets can be enabled with a new socket option478478+ CAN_RAW_FD_FRAMES which is off by default. When the new socket option is479479+ not supported by the CAN_RAW socket (e.g. on older kernels), switching the480480+ CAN_RAW_FD_FRAMES option returns the error -ENOPROTOOPT.481481+482482+ Once CAN_RAW_FD_FRAMES is enabled the application can send both CAN frames483483+ and CAN FD frames. OTOH the application has to handle CAN and CAN FD frames484484+ when reading from the socket.485485+486486+ CAN_RAW_FD_FRAMES enabled: CAN_MTU and CANFD_MTU are allowed487487+ CAN_RAW_FD_FRAMES disabled: only CAN_MTU is allowed (default)488488+489489+ Example:490490+ [ remember: CANFD_MTU == sizeof(struct canfd_frame) ]491491+492492+ struct canfd_frame cfd;493493+494494+ nbytes = read(s, &cfd, CANFD_MTU);495495+496496+ if (nbytes == CANFD_MTU) {497497+ printf("got CAN FD frame with length %d\n", cfd.len);498498+ /* cfd.flags contains valid data */499499+ } else if (nbytes == CAN_MTU) {500500+ printf("got legacy CAN frame with length %d\n", cfd.len);501501+ /* cfd.flags is undefined */502502+ } else {503503+ fprintf(stderr, "read: invalid CAN(FD) frame\n");504504+ return 1;505505+ }506506+507507+ /* the content can be handled independently from the received MTU size */508508+509509+ printf("can_id: %X data length: %d data: ", cfd.can_id, cfd.len);510510+ for (i = 0; i < cfd.len; i++)511511+ printf("%02X ", cfd.data[i]);512512+513513+ When reading with size CANFD_MTU only returns CAN_MTU bytes that have514514+ been received from the socket a legacy CAN frame has been read into the515515+ provided CAN FD structure. Note that the canfd_frame.flags data field is516516+ not specified in the struct can_frame and therefore it is only valid in517517+ CANFD_MTU sized CAN FD frames.518518+519519+ As long as the payload length is <=8 the received CAN frames from CAN FD520520+ capable CAN devices can be received and read by legacy sockets too. When521521+ user-generated CAN FD frames have a payload length <=8 these can be send522522+ by legacy CAN network interfaces too. Sending CAN FD frames with payload523523+ length > 8 to a legacy CAN network interface returns an -EMSGSIZE error.524524+525525+ Implementation hint for new CAN applications:526526+527527+ To build a CAN FD aware application use struct canfd_frame as basic CAN528528+ data structure for CAN_RAW based applications. When the application is529529+ executed on an older Linux kernel and switching the CAN_RAW_FD_FRAMES530530+ socket option returns an error: No problem. You'll get legacy CAN frames531531+ or CAN FD frames and can process them the same way.532532+533533+ When sending to CAN devices make sure that the device is capable to handle534534+ CAN FD frames by checking if the device maximum transfer unit is CANFD_MTU.535535+ The CAN device MTU can be retrieved e.g. with a SIOCGIFMTU ioctl() syscall.536536+537537+ 4.1.6 RAW socket returned message flags523538524539 When using recvmsg() call, the msg->msg_flags may contain following flags:525540···682573 dev->type = ARPHRD_CAN; /* the netdevice hardware type */683574 dev->flags = IFF_NOARP; /* CAN has no arp */684575685685- dev->mtu = sizeof(struct can_frame);576576+ dev->mtu = CAN_MTU; /* sizeof(struct can_frame) -> legacy CAN interface */686577687687- The struct can_frame is the payload of each socket buffer in the688688- protocol family PF_CAN.578578+ or alternative, when the controller supports CAN with flexible data rate:579579+ dev->mtu = CANFD_MTU; /* sizeof(struct canfd_frame) -> CAN FD interface */580580+581581+ The struct can_frame or struct canfd_frame is the payload of each socket582582+ buffer (skbuff) in the protocol family PF_CAN.689583690584 6.2 local loopback of sent frames691585···904792 Note that a restart will also create a CAN error message frame (see905793 also chapter 3.4).906794907907- 6.6 Supported CAN hardware795795+ 6.6 CAN FD (flexible data rate) driver support796796+797797+ CAN FD capable CAN controllers support two different bitrates for the798798+ arbitration phase and the payload phase of the CAN FD frame. Therefore a799799+ second bittiming has to be specified in order to enable the CAN FD bitrate.800800+801801+ Additionally CAN FD capable CAN controllers support up to 64 bytes of802802+ payload. The representation of this length in can_frame.can_dlc and803803+ canfd_frame.len for userspace applications and inside the Linux network804804+ layer is a plain value from 0 .. 64 instead of the CAN 'data length code'.805805+ The data length code was a 1:1 mapping to the payload length in the legacy806806+ CAN frames anyway. The payload length to the bus-relevant DLC mapping is807807+ only performed inside the CAN drivers, preferably with the helper808808+ functions can_dlc2len() and can_len2dlc().809809+810810+ The CAN netdevice driver capabilities can be distinguished by the network811811+ devices maximum transfer unit (MTU):812812+813813+ MTU = 16 (CAN_MTU) => sizeof(struct can_frame) => 'legacy' CAN device814814+ MTU = 72 (CANFD_MTU) => sizeof(struct canfd_frame) => CAN FD capable device815815+816816+ The CAN device MTU can be retrieved e.g. with a SIOCGIFMTU ioctl() syscall.817817+ N.B. CAN FD capable devices can also handle and send legacy CAN frames.818818+819819+ FIXME: Add details about the CAN FD controller configuration when available.820820+821821+ 6.7 Supported CAN hardware908822909823 Please check the "Kconfig" file in "drivers/net/can" to get an actual910824 list of the support CAN hardware. On the Socket CAN project website