1*4882a593Smuzhiyun=================================== 2*4882a593SmuzhiyunSocketCAN - Controller Area Network 3*4882a593Smuzhiyun=================================== 4*4882a593Smuzhiyun 5*4882a593SmuzhiyunOverview / What is SocketCAN 6*4882a593Smuzhiyun============================ 7*4882a593Smuzhiyun 8*4882a593SmuzhiyunThe socketcan package is an implementation of CAN protocols 9*4882a593Smuzhiyun(Controller Area Network) for Linux. CAN is a networking technology 10*4882a593Smuzhiyunwhich has widespread use in automation, embedded devices, and 11*4882a593Smuzhiyunautomotive fields. While there have been other CAN implementations 12*4882a593Smuzhiyunfor Linux based on character devices, SocketCAN uses the Berkeley 13*4882a593Smuzhiyunsocket API, the Linux network stack and implements the CAN device 14*4882a593Smuzhiyundrivers as network interfaces. The CAN socket API has been designed 15*4882a593Smuzhiyunas similar as possible to the TCP/IP protocols to allow programmers, 16*4882a593Smuzhiyunfamiliar with network programming, to easily learn how to use CAN 17*4882a593Smuzhiyunsockets. 18*4882a593Smuzhiyun 19*4882a593Smuzhiyun 20*4882a593Smuzhiyun.. _socketcan-motivation: 21*4882a593Smuzhiyun 22*4882a593SmuzhiyunMotivation / Why Using the Socket API 23*4882a593Smuzhiyun===================================== 24*4882a593Smuzhiyun 25*4882a593SmuzhiyunThere have been CAN implementations for Linux before SocketCAN so the 26*4882a593Smuzhiyunquestion arises, why we have started another project. Most existing 27*4882a593Smuzhiyunimplementations come as a device driver for some CAN hardware, they 28*4882a593Smuzhiyunare based on character devices and provide comparatively little 29*4882a593Smuzhiyunfunctionality. Usually, there is only a hardware-specific device 30*4882a593Smuzhiyundriver which provides a character device interface to send and 31*4882a593Smuzhiyunreceive raw CAN frames, directly to/from the controller hardware. 32*4882a593SmuzhiyunQueueing of frames and higher-level transport protocols like ISO-TP 33*4882a593Smuzhiyunhave to be implemented in user space applications. Also, most 34*4882a593Smuzhiyuncharacter-device implementations support only one single process to 35*4882a593Smuzhiyunopen the device at a time, similar to a serial interface. Exchanging 36*4882a593Smuzhiyunthe CAN controller requires employment of another device driver and 37*4882a593Smuzhiyunoften the need for adaption of large parts of the application to the 38*4882a593Smuzhiyunnew driver's API. 39*4882a593Smuzhiyun 40*4882a593SmuzhiyunSocketCAN was designed to overcome all of these limitations. A new 41*4882a593Smuzhiyunprotocol family has been implemented which provides a socket interface 42*4882a593Smuzhiyunto user space applications and which builds upon the Linux network 43*4882a593Smuzhiyunlayer, enabling use all of the provided queueing functionality. A device 44*4882a593Smuzhiyundriver for CAN controller hardware registers itself with the Linux 45*4882a593Smuzhiyunnetwork layer as a network device, so that CAN frames from the 46*4882a593Smuzhiyuncontroller can be passed up to the network layer and on to the CAN 47*4882a593Smuzhiyunprotocol family module and also vice-versa. Also, the protocol family 48*4882a593Smuzhiyunmodule provides an API for transport protocol modules to register, so 49*4882a593Smuzhiyunthat any number of transport protocols can be loaded or unloaded 50*4882a593Smuzhiyundynamically. In fact, the can core module alone does not provide any 51*4882a593Smuzhiyunprotocol and cannot be used without loading at least one additional 52*4882a593Smuzhiyunprotocol module. Multiple sockets can be opened at the same time, 53*4882a593Smuzhiyunon different or the same protocol module and they can listen/send 54*4882a593Smuzhiyunframes on different or the same CAN IDs. Several sockets listening on 55*4882a593Smuzhiyunthe same interface for frames with the same CAN ID are all passed the 56*4882a593Smuzhiyunsame received matching CAN frames. An application wishing to 57*4882a593Smuzhiyuncommunicate using a specific transport protocol, e.g. ISO-TP, just 58*4882a593Smuzhiyunselects that protocol when opening the socket, and then can read and 59*4882a593Smuzhiyunwrite application data byte streams, without having to deal with 60*4882a593SmuzhiyunCAN-IDs, frames, etc. 61*4882a593Smuzhiyun 62*4882a593SmuzhiyunSimilar functionality visible from user-space could be provided by a 63*4882a593Smuzhiyuncharacter device, too, but this would lead to a technically inelegant 64*4882a593Smuzhiyunsolution for a couple of reasons: 65*4882a593Smuzhiyun 66*4882a593Smuzhiyun* **Intricate usage:** Instead of passing a protocol argument to 67*4882a593Smuzhiyun socket(2) and using bind(2) to select a CAN interface and CAN ID, an 68*4882a593Smuzhiyun application would have to do all these operations using ioctl(2)s. 69*4882a593Smuzhiyun 70*4882a593Smuzhiyun* **Code duplication:** A character device cannot make use of the Linux 71*4882a593Smuzhiyun network queueing code, so all that code would have to be duplicated 72*4882a593Smuzhiyun for CAN networking. 73*4882a593Smuzhiyun 74*4882a593Smuzhiyun* **Abstraction:** In most existing character-device implementations, the 75*4882a593Smuzhiyun hardware-specific device driver for a CAN controller directly 76*4882a593Smuzhiyun provides the character device for the application to work with. 77*4882a593Smuzhiyun This is at least very unusual in Unix systems for both, char and 78*4882a593Smuzhiyun block devices. For example you don't have a character device for a 79*4882a593Smuzhiyun certain UART of a serial interface, a certain sound chip in your 80*4882a593Smuzhiyun computer, a SCSI or IDE controller providing access to your hard 81*4882a593Smuzhiyun disk or tape streamer device. Instead, you have abstraction layers 82*4882a593Smuzhiyun which provide a unified character or block device interface to the 83*4882a593Smuzhiyun application on the one hand, and a interface for hardware-specific 84*4882a593Smuzhiyun device drivers on the other hand. These abstractions are provided 85*4882a593Smuzhiyun by subsystems like the tty layer, the audio subsystem or the SCSI 86*4882a593Smuzhiyun and IDE subsystems for the devices mentioned above. 87*4882a593Smuzhiyun 88*4882a593Smuzhiyun The easiest way to implement a CAN device driver is as a character 89*4882a593Smuzhiyun device without such a (complete) abstraction layer, as is done by most 90*4882a593Smuzhiyun existing drivers. The right way, however, would be to add such a 91*4882a593Smuzhiyun layer with all the functionality like registering for certain CAN 92*4882a593Smuzhiyun IDs, supporting several open file descriptors and (de)multiplexing 93*4882a593Smuzhiyun CAN frames between them, (sophisticated) queueing of CAN frames, and 94*4882a593Smuzhiyun providing an API for device drivers to register with. However, then 95*4882a593Smuzhiyun it would be no more difficult, or may be even easier, to use the 96*4882a593Smuzhiyun networking framework provided by the Linux kernel, and this is what 97*4882a593Smuzhiyun SocketCAN does. 98*4882a593Smuzhiyun 99*4882a593SmuzhiyunThe use of the networking framework of the Linux kernel is just the 100*4882a593Smuzhiyunnatural and most appropriate way to implement CAN for Linux. 101*4882a593Smuzhiyun 102*4882a593Smuzhiyun 103*4882a593Smuzhiyun.. _socketcan-concept: 104*4882a593Smuzhiyun 105*4882a593SmuzhiyunSocketCAN Concept 106*4882a593Smuzhiyun================= 107*4882a593Smuzhiyun 108*4882a593SmuzhiyunAs described in :ref:`socketcan-motivation` the main goal of SocketCAN is to 109*4882a593Smuzhiyunprovide a socket interface to user space applications which builds 110*4882a593Smuzhiyunupon the Linux network layer. In contrast to the commonly known 111*4882a593SmuzhiyunTCP/IP and ethernet networking, the CAN bus is a broadcast-only(!) 112*4882a593Smuzhiyunmedium that has no MAC-layer addressing like ethernet. The CAN-identifier 113*4882a593Smuzhiyun(can_id) is used for arbitration on the CAN-bus. Therefore the CAN-IDs 114*4882a593Smuzhiyunhave to be chosen uniquely on the bus. When designing a CAN-ECU 115*4882a593Smuzhiyunnetwork the CAN-IDs are mapped to be sent by a specific ECU. 116*4882a593SmuzhiyunFor this reason a CAN-ID can be treated best as a kind of source address. 117*4882a593Smuzhiyun 118*4882a593Smuzhiyun 119*4882a593Smuzhiyun.. _socketcan-receive-lists: 120*4882a593Smuzhiyun 121*4882a593SmuzhiyunReceive Lists 122*4882a593Smuzhiyun------------- 123*4882a593Smuzhiyun 124*4882a593SmuzhiyunThe network transparent access of multiple applications leads to the 125*4882a593Smuzhiyunproblem that different applications may be interested in the same 126*4882a593SmuzhiyunCAN-IDs from the same CAN network interface. The SocketCAN core 127*4882a593Smuzhiyunmodule - which implements the protocol family CAN - provides several 128*4882a593Smuzhiyunhigh efficient receive lists for this reason. If e.g. a user space 129*4882a593Smuzhiyunapplication opens a CAN RAW socket, the raw protocol module itself 130*4882a593Smuzhiyunrequests the (range of) CAN-IDs from the SocketCAN core that are 131*4882a593Smuzhiyunrequested by the user. The subscription and unsubscription of 132*4882a593SmuzhiyunCAN-IDs can be done for specific CAN interfaces or for all(!) known 133*4882a593SmuzhiyunCAN interfaces with the can_rx_(un)register() functions provided to 134*4882a593SmuzhiyunCAN protocol modules by the SocketCAN core (see :ref:`socketcan-core-module`). 135*4882a593SmuzhiyunTo optimize the CPU usage at runtime the receive lists are split up 136*4882a593Smuzhiyuninto several specific lists per device that match the requested 137*4882a593Smuzhiyunfilter complexity for a given use-case. 138*4882a593Smuzhiyun 139*4882a593Smuzhiyun 140*4882a593Smuzhiyun.. _socketcan-local-loopback1: 141*4882a593Smuzhiyun 142*4882a593SmuzhiyunLocal Loopback of Sent Frames 143*4882a593Smuzhiyun----------------------------- 144*4882a593Smuzhiyun 145*4882a593SmuzhiyunAs known from other networking concepts the data exchanging 146*4882a593Smuzhiyunapplications may run on the same or different nodes without any 147*4882a593Smuzhiyunchange (except for the according addressing information): 148*4882a593Smuzhiyun 149*4882a593Smuzhiyun.. code:: 150*4882a593Smuzhiyun 151*4882a593Smuzhiyun ___ ___ ___ _______ ___ 152*4882a593Smuzhiyun | _ | | _ | | _ | | _ _ | | _ | 153*4882a593Smuzhiyun ||A|| ||B|| ||C|| ||A| |B|| ||C|| 154*4882a593Smuzhiyun |___| |___| |___| |_______| |___| 155*4882a593Smuzhiyun | | | | | 156*4882a593Smuzhiyun -----------------(1)- CAN bus -(2)--------------- 157*4882a593Smuzhiyun 158*4882a593SmuzhiyunTo ensure that application A receives the same information in the 159*4882a593Smuzhiyunexample (2) as it would receive in example (1) there is need for 160*4882a593Smuzhiyunsome kind of local loopback of the sent CAN frames on the appropriate 161*4882a593Smuzhiyunnode. 162*4882a593Smuzhiyun 163*4882a593SmuzhiyunThe Linux network devices (by default) just can handle the 164*4882a593Smuzhiyuntransmission and reception of media dependent frames. Due to the 165*4882a593Smuzhiyunarbitration on the CAN bus the transmission of a low prio CAN-ID 166*4882a593Smuzhiyunmay be delayed by the reception of a high prio CAN frame. To 167*4882a593Smuzhiyunreflect the correct [#f1]_ traffic on the node the loopback of the sent 168*4882a593Smuzhiyundata has to be performed right after a successful transmission. If 169*4882a593Smuzhiyunthe CAN network interface is not capable of performing the loopback for 170*4882a593Smuzhiyunsome reason the SocketCAN core can do this task as a fallback solution. 171*4882a593SmuzhiyunSee :ref:`socketcan-local-loopback1` for details (recommended). 172*4882a593Smuzhiyun 173*4882a593SmuzhiyunThe loopback functionality is enabled by default to reflect standard 174*4882a593Smuzhiyunnetworking behaviour for CAN applications. Due to some requests from 175*4882a593Smuzhiyunthe RT-SocketCAN group the loopback optionally may be disabled for each 176*4882a593Smuzhiyunseparate socket. See sockopts from the CAN RAW sockets in :ref:`socketcan-raw-sockets`. 177*4882a593Smuzhiyun 178*4882a593Smuzhiyun.. [#f1] you really like to have this when you're running analyser 179*4882a593Smuzhiyun tools like 'candump' or 'cansniffer' on the (same) node. 180*4882a593Smuzhiyun 181*4882a593Smuzhiyun 182*4882a593Smuzhiyun.. _socketcan-network-problem-notifications: 183*4882a593Smuzhiyun 184*4882a593SmuzhiyunNetwork Problem Notifications 185*4882a593Smuzhiyun----------------------------- 186*4882a593Smuzhiyun 187*4882a593SmuzhiyunThe use of the CAN bus may lead to several problems on the physical 188*4882a593Smuzhiyunand media access control layer. Detecting and logging of these lower 189*4882a593Smuzhiyunlayer problems is a vital requirement for CAN users to identify 190*4882a593Smuzhiyunhardware issues on the physical transceiver layer as well as 191*4882a593Smuzhiyunarbitration problems and error frames caused by the different 192*4882a593SmuzhiyunECUs. The occurrence of detected errors are important for diagnosis 193*4882a593Smuzhiyunand have to be logged together with the exact timestamp. For this 194*4882a593Smuzhiyunreason the CAN interface driver can generate so called Error Message 195*4882a593SmuzhiyunFrames that can optionally be passed to the user application in the 196*4882a593Smuzhiyunsame way as other CAN frames. Whenever an error on the physical layer 197*4882a593Smuzhiyunor the MAC layer is detected (e.g. by the CAN controller) the driver 198*4882a593Smuzhiyuncreates an appropriate error message frame. Error messages frames can 199*4882a593Smuzhiyunbe requested by the user application using the common CAN filter 200*4882a593Smuzhiyunmechanisms. Inside this filter definition the (interested) type of 201*4882a593Smuzhiyunerrors may be selected. The reception of error messages is disabled 202*4882a593Smuzhiyunby default. The format of the CAN error message frame is briefly 203*4882a593Smuzhiyundescribed in the Linux header file "include/uapi/linux/can/error.h". 204*4882a593Smuzhiyun 205*4882a593Smuzhiyun 206*4882a593SmuzhiyunHow to use SocketCAN 207*4882a593Smuzhiyun==================== 208*4882a593Smuzhiyun 209*4882a593SmuzhiyunLike TCP/IP, you first need to open a socket for communicating over a 210*4882a593SmuzhiyunCAN network. Since SocketCAN implements a new protocol family, you 211*4882a593Smuzhiyunneed to pass PF_CAN as the first argument to the socket(2) system 212*4882a593Smuzhiyuncall. Currently, there are two CAN protocols to choose from, the raw 213*4882a593Smuzhiyunsocket protocol and the broadcast manager (BCM). So to open a socket, 214*4882a593Smuzhiyunyou would write:: 215*4882a593Smuzhiyun 216*4882a593Smuzhiyun s = socket(PF_CAN, SOCK_RAW, CAN_RAW); 217*4882a593Smuzhiyun 218*4882a593Smuzhiyunand:: 219*4882a593Smuzhiyun 220*4882a593Smuzhiyun s = socket(PF_CAN, SOCK_DGRAM, CAN_BCM); 221*4882a593Smuzhiyun 222*4882a593Smuzhiyunrespectively. After the successful creation of the socket, you would 223*4882a593Smuzhiyunnormally use the bind(2) system call to bind the socket to a CAN 224*4882a593Smuzhiyuninterface (which is different from TCP/IP due to different addressing 225*4882a593Smuzhiyun- see :ref:`socketcan-concept`). After binding (CAN_RAW) or connecting (CAN_BCM) 226*4882a593Smuzhiyunthe socket, you can read(2) and write(2) from/to the socket or use 227*4882a593Smuzhiyunsend(2), sendto(2), sendmsg(2) and the recv* counterpart operations 228*4882a593Smuzhiyunon the socket as usual. There are also CAN specific socket options 229*4882a593Smuzhiyundescribed below. 230*4882a593Smuzhiyun 231*4882a593SmuzhiyunThe basic CAN frame structure and the sockaddr structure are defined 232*4882a593Smuzhiyunin include/linux/can.h: 233*4882a593Smuzhiyun 234*4882a593Smuzhiyun.. code-block:: C 235*4882a593Smuzhiyun 236*4882a593Smuzhiyun struct can_frame { 237*4882a593Smuzhiyun canid_t can_id; /* 32 bit CAN_ID + EFF/RTR/ERR flags */ 238*4882a593Smuzhiyun __u8 can_dlc; /* frame payload length in byte (0 .. 8) */ 239*4882a593Smuzhiyun __u8 __pad; /* padding */ 240*4882a593Smuzhiyun __u8 __res0; /* reserved / padding */ 241*4882a593Smuzhiyun __u8 __res1; /* reserved / padding */ 242*4882a593Smuzhiyun __u8 data[8] __attribute__((aligned(8))); 243*4882a593Smuzhiyun }; 244*4882a593Smuzhiyun 245*4882a593SmuzhiyunThe alignment of the (linear) payload data[] to a 64bit boundary 246*4882a593Smuzhiyunallows the user to define their own structs and unions to easily access 247*4882a593Smuzhiyunthe CAN payload. There is no given byteorder on the CAN bus by 248*4882a593Smuzhiyundefault. A read(2) system call on a CAN_RAW socket transfers a 249*4882a593Smuzhiyunstruct can_frame to the user space. 250*4882a593Smuzhiyun 251*4882a593SmuzhiyunThe sockaddr_can structure has an interface index like the 252*4882a593SmuzhiyunPF_PACKET socket, that also binds to a specific interface: 253*4882a593Smuzhiyun 254*4882a593Smuzhiyun.. code-block:: C 255*4882a593Smuzhiyun 256*4882a593Smuzhiyun struct sockaddr_can { 257*4882a593Smuzhiyun sa_family_t can_family; 258*4882a593Smuzhiyun int can_ifindex; 259*4882a593Smuzhiyun union { 260*4882a593Smuzhiyun /* transport protocol class address info (e.g. ISOTP) */ 261*4882a593Smuzhiyun struct { canid_t rx_id, tx_id; } tp; 262*4882a593Smuzhiyun 263*4882a593Smuzhiyun /* reserved for future CAN protocols address information */ 264*4882a593Smuzhiyun } can_addr; 265*4882a593Smuzhiyun }; 266*4882a593Smuzhiyun 267*4882a593SmuzhiyunTo determine the interface index an appropriate ioctl() has to 268*4882a593Smuzhiyunbe used (example for CAN_RAW sockets without error checking): 269*4882a593Smuzhiyun 270*4882a593Smuzhiyun.. code-block:: C 271*4882a593Smuzhiyun 272*4882a593Smuzhiyun int s; 273*4882a593Smuzhiyun struct sockaddr_can addr; 274*4882a593Smuzhiyun struct ifreq ifr; 275*4882a593Smuzhiyun 276*4882a593Smuzhiyun s = socket(PF_CAN, SOCK_RAW, CAN_RAW); 277*4882a593Smuzhiyun 278*4882a593Smuzhiyun strcpy(ifr.ifr_name, "can0" ); 279*4882a593Smuzhiyun ioctl(s, SIOCGIFINDEX, &ifr); 280*4882a593Smuzhiyun 281*4882a593Smuzhiyun addr.can_family = AF_CAN; 282*4882a593Smuzhiyun addr.can_ifindex = ifr.ifr_ifindex; 283*4882a593Smuzhiyun 284*4882a593Smuzhiyun bind(s, (struct sockaddr *)&addr, sizeof(addr)); 285*4882a593Smuzhiyun 286*4882a593Smuzhiyun (..) 287*4882a593Smuzhiyun 288*4882a593SmuzhiyunTo bind a socket to all(!) CAN interfaces the interface index must 289*4882a593Smuzhiyunbe 0 (zero). In this case the socket receives CAN frames from every 290*4882a593Smuzhiyunenabled CAN interface. To determine the originating CAN interface 291*4882a593Smuzhiyunthe system call recvfrom(2) may be used instead of read(2). To send 292*4882a593Smuzhiyunon a socket that is bound to 'any' interface sendto(2) is needed to 293*4882a593Smuzhiyunspecify the outgoing interface. 294*4882a593Smuzhiyun 295*4882a593SmuzhiyunReading CAN frames from a bound CAN_RAW socket (see above) consists 296*4882a593Smuzhiyunof reading a struct can_frame: 297*4882a593Smuzhiyun 298*4882a593Smuzhiyun.. code-block:: C 299*4882a593Smuzhiyun 300*4882a593Smuzhiyun struct can_frame frame; 301*4882a593Smuzhiyun 302*4882a593Smuzhiyun nbytes = read(s, &frame, sizeof(struct can_frame)); 303*4882a593Smuzhiyun 304*4882a593Smuzhiyun if (nbytes < 0) { 305*4882a593Smuzhiyun perror("can raw socket read"); 306*4882a593Smuzhiyun return 1; 307*4882a593Smuzhiyun } 308*4882a593Smuzhiyun 309*4882a593Smuzhiyun /* paranoid check ... */ 310*4882a593Smuzhiyun if (nbytes < sizeof(struct can_frame)) { 311*4882a593Smuzhiyun fprintf(stderr, "read: incomplete CAN frame\n"); 312*4882a593Smuzhiyun return 1; 313*4882a593Smuzhiyun } 314*4882a593Smuzhiyun 315*4882a593Smuzhiyun /* do something with the received CAN frame */ 316*4882a593Smuzhiyun 317*4882a593SmuzhiyunWriting CAN frames can be done similarly, with the write(2) system call:: 318*4882a593Smuzhiyun 319*4882a593Smuzhiyun nbytes = write(s, &frame, sizeof(struct can_frame)); 320*4882a593Smuzhiyun 321*4882a593SmuzhiyunWhen the CAN interface is bound to 'any' existing CAN interface 322*4882a593Smuzhiyun(addr.can_ifindex = 0) it is recommended to use recvfrom(2) if the 323*4882a593Smuzhiyuninformation about the originating CAN interface is needed: 324*4882a593Smuzhiyun 325*4882a593Smuzhiyun.. code-block:: C 326*4882a593Smuzhiyun 327*4882a593Smuzhiyun struct sockaddr_can addr; 328*4882a593Smuzhiyun struct ifreq ifr; 329*4882a593Smuzhiyun socklen_t len = sizeof(addr); 330*4882a593Smuzhiyun struct can_frame frame; 331*4882a593Smuzhiyun 332*4882a593Smuzhiyun nbytes = recvfrom(s, &frame, sizeof(struct can_frame), 333*4882a593Smuzhiyun 0, (struct sockaddr*)&addr, &len); 334*4882a593Smuzhiyun 335*4882a593Smuzhiyun /* get interface name of the received CAN frame */ 336*4882a593Smuzhiyun ifr.ifr_ifindex = addr.can_ifindex; 337*4882a593Smuzhiyun ioctl(s, SIOCGIFNAME, &ifr); 338*4882a593Smuzhiyun printf("Received a CAN frame from interface %s", ifr.ifr_name); 339*4882a593Smuzhiyun 340*4882a593SmuzhiyunTo write CAN frames on sockets bound to 'any' CAN interface the 341*4882a593Smuzhiyunoutgoing interface has to be defined certainly: 342*4882a593Smuzhiyun 343*4882a593Smuzhiyun.. code-block:: C 344*4882a593Smuzhiyun 345*4882a593Smuzhiyun strcpy(ifr.ifr_name, "can0"); 346*4882a593Smuzhiyun ioctl(s, SIOCGIFINDEX, &ifr); 347*4882a593Smuzhiyun addr.can_ifindex = ifr.ifr_ifindex; 348*4882a593Smuzhiyun addr.can_family = AF_CAN; 349*4882a593Smuzhiyun 350*4882a593Smuzhiyun nbytes = sendto(s, &frame, sizeof(struct can_frame), 351*4882a593Smuzhiyun 0, (struct sockaddr*)&addr, sizeof(addr)); 352*4882a593Smuzhiyun 353*4882a593SmuzhiyunAn accurate timestamp can be obtained with an ioctl(2) call after reading 354*4882a593Smuzhiyuna message from the socket: 355*4882a593Smuzhiyun 356*4882a593Smuzhiyun.. code-block:: C 357*4882a593Smuzhiyun 358*4882a593Smuzhiyun struct timeval tv; 359*4882a593Smuzhiyun ioctl(s, SIOCGSTAMP, &tv); 360*4882a593Smuzhiyun 361*4882a593SmuzhiyunThe timestamp has a resolution of one microsecond and is set automatically 362*4882a593Smuzhiyunat the reception of a CAN frame. 363*4882a593Smuzhiyun 364*4882a593SmuzhiyunRemark about CAN FD (flexible data rate) support: 365*4882a593Smuzhiyun 366*4882a593SmuzhiyunGenerally the handling of CAN FD is very similar to the formerly described 367*4882a593Smuzhiyunexamples. The new CAN FD capable CAN controllers support two different 368*4882a593Smuzhiyunbitrates for the arbitration phase and the payload phase of the CAN FD frame 369*4882a593Smuzhiyunand up to 64 bytes of payload. This extended payload length breaks all the 370*4882a593Smuzhiyunkernel interfaces (ABI) which heavily rely on the CAN frame with fixed eight 371*4882a593Smuzhiyunbytes of payload (struct can_frame) like the CAN_RAW socket. Therefore e.g. 372*4882a593Smuzhiyunthe CAN_RAW socket supports a new socket option CAN_RAW_FD_FRAMES that 373*4882a593Smuzhiyunswitches the socket into a mode that allows the handling of CAN FD frames 374*4882a593Smuzhiyunand (legacy) CAN frames simultaneously (see :ref:`socketcan-rawfd`). 375*4882a593Smuzhiyun 376*4882a593SmuzhiyunThe struct canfd_frame is defined in include/linux/can.h: 377*4882a593Smuzhiyun 378*4882a593Smuzhiyun.. code-block:: C 379*4882a593Smuzhiyun 380*4882a593Smuzhiyun struct canfd_frame { 381*4882a593Smuzhiyun canid_t can_id; /* 32 bit CAN_ID + EFF/RTR/ERR flags */ 382*4882a593Smuzhiyun __u8 len; /* frame payload length in byte (0 .. 64) */ 383*4882a593Smuzhiyun __u8 flags; /* additional flags for CAN FD */ 384*4882a593Smuzhiyun __u8 __res0; /* reserved / padding */ 385*4882a593Smuzhiyun __u8 __res1; /* reserved / padding */ 386*4882a593Smuzhiyun __u8 data[64] __attribute__((aligned(8))); 387*4882a593Smuzhiyun }; 388*4882a593Smuzhiyun 389*4882a593SmuzhiyunThe struct canfd_frame and the existing struct can_frame have the can_id, 390*4882a593Smuzhiyunthe payload length and the payload data at the same offset inside their 391*4882a593Smuzhiyunstructures. This allows to handle the different structures very similar. 392*4882a593SmuzhiyunWhen the content of a struct can_frame is copied into a struct canfd_frame 393*4882a593Smuzhiyunall structure elements can be used as-is - only the data[] becomes extended. 394*4882a593Smuzhiyun 395*4882a593SmuzhiyunWhen introducing the struct canfd_frame it turned out that the data length 396*4882a593Smuzhiyuncode (DLC) of the struct can_frame was used as a length information as the 397*4882a593Smuzhiyunlength and the DLC has a 1:1 mapping in the range of 0 .. 8. To preserve 398*4882a593Smuzhiyunthe easy handling of the length information the canfd_frame.len element 399*4882a593Smuzhiyuncontains a plain length value from 0 .. 64. So both canfd_frame.len and 400*4882a593Smuzhiyuncan_frame.can_dlc are equal and contain a length information and no DLC. 401*4882a593SmuzhiyunFor details about the distinction of CAN and CAN FD capable devices and 402*4882a593Smuzhiyunthe mapping to the bus-relevant data length code (DLC), see :ref:`socketcan-can-fd-driver`. 403*4882a593Smuzhiyun 404*4882a593SmuzhiyunThe length of the two CAN(FD) frame structures define the maximum transfer 405*4882a593Smuzhiyununit (MTU) of the CAN(FD) network interface and skbuff data length. Two 406*4882a593Smuzhiyundefinitions are specified for CAN specific MTUs in include/linux/can.h: 407*4882a593Smuzhiyun 408*4882a593Smuzhiyun.. code-block:: C 409*4882a593Smuzhiyun 410*4882a593Smuzhiyun #define CAN_MTU (sizeof(struct can_frame)) == 16 => 'legacy' CAN frame 411*4882a593Smuzhiyun #define CANFD_MTU (sizeof(struct canfd_frame)) == 72 => CAN FD frame 412*4882a593Smuzhiyun 413*4882a593Smuzhiyun 414*4882a593Smuzhiyun.. _socketcan-raw-sockets: 415*4882a593Smuzhiyun 416*4882a593SmuzhiyunRAW Protocol Sockets with can_filters (SOCK_RAW) 417*4882a593Smuzhiyun------------------------------------------------ 418*4882a593Smuzhiyun 419*4882a593SmuzhiyunUsing CAN_RAW sockets is extensively comparable to the commonly 420*4882a593Smuzhiyunknown access to CAN character devices. To meet the new possibilities 421*4882a593Smuzhiyunprovided by the multi user SocketCAN approach, some reasonable 422*4882a593Smuzhiyundefaults are set at RAW socket binding time: 423*4882a593Smuzhiyun 424*4882a593Smuzhiyun- The filters are set to exactly one filter receiving everything 425*4882a593Smuzhiyun- The socket only receives valid data frames (=> no error message frames) 426*4882a593Smuzhiyun- The loopback of sent CAN frames is enabled (see :ref:`socketcan-local-loopback2`) 427*4882a593Smuzhiyun- The socket does not receive its own sent frames (in loopback mode) 428*4882a593Smuzhiyun 429*4882a593SmuzhiyunThese default settings may be changed before or after binding the socket. 430*4882a593SmuzhiyunTo use the referenced definitions of the socket options for CAN_RAW 431*4882a593Smuzhiyunsockets, include <linux/can/raw.h>. 432*4882a593Smuzhiyun 433*4882a593Smuzhiyun 434*4882a593Smuzhiyun.. _socketcan-rawfilter: 435*4882a593Smuzhiyun 436*4882a593SmuzhiyunRAW socket option CAN_RAW_FILTER 437*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 438*4882a593Smuzhiyun 439*4882a593SmuzhiyunThe reception of CAN frames using CAN_RAW sockets can be controlled 440*4882a593Smuzhiyunby defining 0 .. n filters with the CAN_RAW_FILTER socket option. 441*4882a593Smuzhiyun 442*4882a593SmuzhiyunThe CAN filter structure is defined in include/linux/can.h: 443*4882a593Smuzhiyun 444*4882a593Smuzhiyun.. code-block:: C 445*4882a593Smuzhiyun 446*4882a593Smuzhiyun struct can_filter { 447*4882a593Smuzhiyun canid_t can_id; 448*4882a593Smuzhiyun canid_t can_mask; 449*4882a593Smuzhiyun }; 450*4882a593Smuzhiyun 451*4882a593SmuzhiyunA filter matches, when: 452*4882a593Smuzhiyun 453*4882a593Smuzhiyun.. code-block:: C 454*4882a593Smuzhiyun 455*4882a593Smuzhiyun <received_can_id> & mask == can_id & mask 456*4882a593Smuzhiyun 457*4882a593Smuzhiyunwhich is analogous to known CAN controllers hardware filter semantics. 458*4882a593SmuzhiyunThe filter can be inverted in this semantic, when the CAN_INV_FILTER 459*4882a593Smuzhiyunbit is set in can_id element of the can_filter structure. In 460*4882a593Smuzhiyuncontrast to CAN controller hardware filters the user may set 0 .. n 461*4882a593Smuzhiyunreceive filters for each open socket separately: 462*4882a593Smuzhiyun 463*4882a593Smuzhiyun.. code-block:: C 464*4882a593Smuzhiyun 465*4882a593Smuzhiyun struct can_filter rfilter[2]; 466*4882a593Smuzhiyun 467*4882a593Smuzhiyun rfilter[0].can_id = 0x123; 468*4882a593Smuzhiyun rfilter[0].can_mask = CAN_SFF_MASK; 469*4882a593Smuzhiyun rfilter[1].can_id = 0x200; 470*4882a593Smuzhiyun rfilter[1].can_mask = 0x700; 471*4882a593Smuzhiyun 472*4882a593Smuzhiyun setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter)); 473*4882a593Smuzhiyun 474*4882a593SmuzhiyunTo disable the reception of CAN frames on the selected CAN_RAW socket: 475*4882a593Smuzhiyun 476*4882a593Smuzhiyun.. code-block:: C 477*4882a593Smuzhiyun 478*4882a593Smuzhiyun setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0); 479*4882a593Smuzhiyun 480*4882a593SmuzhiyunTo set the filters to zero filters is quite obsolete as to not read 481*4882a593Smuzhiyundata causes the raw socket to discard the received CAN frames. But 482*4882a593Smuzhiyunhaving this 'send only' use-case we may remove the receive list in the 483*4882a593SmuzhiyunKernel to save a little (really a very little!) CPU usage. 484*4882a593Smuzhiyun 485*4882a593SmuzhiyunCAN Filter Usage Optimisation 486*4882a593Smuzhiyun............................. 487*4882a593Smuzhiyun 488*4882a593SmuzhiyunThe CAN filters are processed in per-device filter lists at CAN frame 489*4882a593Smuzhiyunreception time. To reduce the number of checks that need to be performed 490*4882a593Smuzhiyunwhile walking through the filter lists the CAN core provides an optimized 491*4882a593Smuzhiyunfilter handling when the filter subscription focusses on a single CAN ID. 492*4882a593Smuzhiyun 493*4882a593SmuzhiyunFor the possible 2048 SFF CAN identifiers the identifier is used as an index 494*4882a593Smuzhiyunto access the corresponding subscription list without any further checks. 495*4882a593SmuzhiyunFor the 2^29 possible EFF CAN identifiers a 10 bit XOR folding is used as 496*4882a593Smuzhiyunhash function to retrieve the EFF table index. 497*4882a593Smuzhiyun 498*4882a593SmuzhiyunTo benefit from the optimized filters for single CAN identifiers the 499*4882a593SmuzhiyunCAN_SFF_MASK or CAN_EFF_MASK have to be set into can_filter.mask together 500*4882a593Smuzhiyunwith set CAN_EFF_FLAG and CAN_RTR_FLAG bits. A set CAN_EFF_FLAG bit in the 501*4882a593Smuzhiyuncan_filter.mask makes clear that it matters whether a SFF or EFF CAN ID is 502*4882a593Smuzhiyunsubscribed. E.g. in the example from above: 503*4882a593Smuzhiyun 504*4882a593Smuzhiyun.. code-block:: C 505*4882a593Smuzhiyun 506*4882a593Smuzhiyun rfilter[0].can_id = 0x123; 507*4882a593Smuzhiyun rfilter[0].can_mask = CAN_SFF_MASK; 508*4882a593Smuzhiyun 509*4882a593Smuzhiyunboth SFF frames with CAN ID 0x123 and EFF frames with 0xXXXXX123 can pass. 510*4882a593Smuzhiyun 511*4882a593SmuzhiyunTo filter for only 0x123 (SFF) and 0x12345678 (EFF) CAN identifiers the 512*4882a593Smuzhiyunfilter has to be defined in this way to benefit from the optimized filters: 513*4882a593Smuzhiyun 514*4882a593Smuzhiyun.. code-block:: C 515*4882a593Smuzhiyun 516*4882a593Smuzhiyun struct can_filter rfilter[2]; 517*4882a593Smuzhiyun 518*4882a593Smuzhiyun rfilter[0].can_id = 0x123; 519*4882a593Smuzhiyun rfilter[0].can_mask = (CAN_EFF_FLAG | CAN_RTR_FLAG | CAN_SFF_MASK); 520*4882a593Smuzhiyun rfilter[1].can_id = 0x12345678 | CAN_EFF_FLAG; 521*4882a593Smuzhiyun rfilter[1].can_mask = (CAN_EFF_FLAG | CAN_RTR_FLAG | CAN_EFF_MASK); 522*4882a593Smuzhiyun 523*4882a593Smuzhiyun setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter)); 524*4882a593Smuzhiyun 525*4882a593Smuzhiyun 526*4882a593SmuzhiyunRAW Socket Option CAN_RAW_ERR_FILTER 527*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 528*4882a593Smuzhiyun 529*4882a593SmuzhiyunAs described in :ref:`socketcan-network-problem-notifications` the CAN interface driver can generate so 530*4882a593Smuzhiyuncalled Error Message Frames that can optionally be passed to the user 531*4882a593Smuzhiyunapplication in the same way as other CAN frames. The possible 532*4882a593Smuzhiyunerrors are divided into different error classes that may be filtered 533*4882a593Smuzhiyunusing the appropriate error mask. To register for every possible 534*4882a593Smuzhiyunerror condition CAN_ERR_MASK can be used as value for the error mask. 535*4882a593SmuzhiyunThe values for the error mask are defined in linux/can/error.h: 536*4882a593Smuzhiyun 537*4882a593Smuzhiyun.. code-block:: C 538*4882a593Smuzhiyun 539*4882a593Smuzhiyun can_err_mask_t err_mask = ( CAN_ERR_TX_TIMEOUT | CAN_ERR_BUSOFF ); 540*4882a593Smuzhiyun 541*4882a593Smuzhiyun setsockopt(s, SOL_CAN_RAW, CAN_RAW_ERR_FILTER, 542*4882a593Smuzhiyun &err_mask, sizeof(err_mask)); 543*4882a593Smuzhiyun 544*4882a593Smuzhiyun 545*4882a593SmuzhiyunRAW Socket Option CAN_RAW_LOOPBACK 546*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 547*4882a593Smuzhiyun 548*4882a593SmuzhiyunTo meet multi user needs the local loopback is enabled by default 549*4882a593Smuzhiyun(see :ref:`socketcan-local-loopback1` for details). But in some embedded use-cases 550*4882a593Smuzhiyun(e.g. when only one application uses the CAN bus) this loopback 551*4882a593Smuzhiyunfunctionality can be disabled (separately for each socket): 552*4882a593Smuzhiyun 553*4882a593Smuzhiyun.. code-block:: C 554*4882a593Smuzhiyun 555*4882a593Smuzhiyun int loopback = 0; /* 0 = disabled, 1 = enabled (default) */ 556*4882a593Smuzhiyun 557*4882a593Smuzhiyun setsockopt(s, SOL_CAN_RAW, CAN_RAW_LOOPBACK, &loopback, sizeof(loopback)); 558*4882a593Smuzhiyun 559*4882a593Smuzhiyun 560*4882a593SmuzhiyunRAW socket option CAN_RAW_RECV_OWN_MSGS 561*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 562*4882a593Smuzhiyun 563*4882a593SmuzhiyunWhen the local loopback is enabled, all the sent CAN frames are 564*4882a593Smuzhiyunlooped back to the open CAN sockets that registered for the CAN 565*4882a593Smuzhiyunframes' CAN-ID on this given interface to meet the multi user 566*4882a593Smuzhiyunneeds. The reception of the CAN frames on the same socket that was 567*4882a593Smuzhiyunsending the CAN frame is assumed to be unwanted and therefore 568*4882a593Smuzhiyundisabled by default. This default behaviour may be changed on 569*4882a593Smuzhiyundemand: 570*4882a593Smuzhiyun 571*4882a593Smuzhiyun.. code-block:: C 572*4882a593Smuzhiyun 573*4882a593Smuzhiyun int recv_own_msgs = 1; /* 0 = disabled (default), 1 = enabled */ 574*4882a593Smuzhiyun 575*4882a593Smuzhiyun setsockopt(s, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS, 576*4882a593Smuzhiyun &recv_own_msgs, sizeof(recv_own_msgs)); 577*4882a593Smuzhiyun 578*4882a593Smuzhiyun 579*4882a593Smuzhiyun.. _socketcan-rawfd: 580*4882a593Smuzhiyun 581*4882a593SmuzhiyunRAW Socket Option CAN_RAW_FD_FRAMES 582*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 583*4882a593Smuzhiyun 584*4882a593SmuzhiyunCAN FD support in CAN_RAW sockets can be enabled with a new socket option 585*4882a593SmuzhiyunCAN_RAW_FD_FRAMES which is off by default. When the new socket option is 586*4882a593Smuzhiyunnot supported by the CAN_RAW socket (e.g. on older kernels), switching the 587*4882a593SmuzhiyunCAN_RAW_FD_FRAMES option returns the error -ENOPROTOOPT. 588*4882a593Smuzhiyun 589*4882a593SmuzhiyunOnce CAN_RAW_FD_FRAMES is enabled the application can send both CAN frames 590*4882a593Smuzhiyunand CAN FD frames. OTOH the application has to handle CAN and CAN FD frames 591*4882a593Smuzhiyunwhen reading from the socket: 592*4882a593Smuzhiyun 593*4882a593Smuzhiyun.. code-block:: C 594*4882a593Smuzhiyun 595*4882a593Smuzhiyun CAN_RAW_FD_FRAMES enabled: CAN_MTU and CANFD_MTU are allowed 596*4882a593Smuzhiyun CAN_RAW_FD_FRAMES disabled: only CAN_MTU is allowed (default) 597*4882a593Smuzhiyun 598*4882a593SmuzhiyunExample: 599*4882a593Smuzhiyun 600*4882a593Smuzhiyun.. code-block:: C 601*4882a593Smuzhiyun 602*4882a593Smuzhiyun [ remember: CANFD_MTU == sizeof(struct canfd_frame) ] 603*4882a593Smuzhiyun 604*4882a593Smuzhiyun struct canfd_frame cfd; 605*4882a593Smuzhiyun 606*4882a593Smuzhiyun nbytes = read(s, &cfd, CANFD_MTU); 607*4882a593Smuzhiyun 608*4882a593Smuzhiyun if (nbytes == CANFD_MTU) { 609*4882a593Smuzhiyun printf("got CAN FD frame with length %d\n", cfd.len); 610*4882a593Smuzhiyun /* cfd.flags contains valid data */ 611*4882a593Smuzhiyun } else if (nbytes == CAN_MTU) { 612*4882a593Smuzhiyun printf("got legacy CAN frame with length %d\n", cfd.len); 613*4882a593Smuzhiyun /* cfd.flags is undefined */ 614*4882a593Smuzhiyun } else { 615*4882a593Smuzhiyun fprintf(stderr, "read: invalid CAN(FD) frame\n"); 616*4882a593Smuzhiyun return 1; 617*4882a593Smuzhiyun } 618*4882a593Smuzhiyun 619*4882a593Smuzhiyun /* the content can be handled independently from the received MTU size */ 620*4882a593Smuzhiyun 621*4882a593Smuzhiyun printf("can_id: %X data length: %d data: ", cfd.can_id, cfd.len); 622*4882a593Smuzhiyun for (i = 0; i < cfd.len; i++) 623*4882a593Smuzhiyun printf("%02X ", cfd.data[i]); 624*4882a593Smuzhiyun 625*4882a593SmuzhiyunWhen reading with size CANFD_MTU only returns CAN_MTU bytes that have 626*4882a593Smuzhiyunbeen received from the socket a legacy CAN frame has been read into the 627*4882a593Smuzhiyunprovided CAN FD structure. Note that the canfd_frame.flags data field is 628*4882a593Smuzhiyunnot specified in the struct can_frame and therefore it is only valid in 629*4882a593SmuzhiyunCANFD_MTU sized CAN FD frames. 630*4882a593Smuzhiyun 631*4882a593SmuzhiyunImplementation hint for new CAN applications: 632*4882a593Smuzhiyun 633*4882a593SmuzhiyunTo build a CAN FD aware application use struct canfd_frame as basic CAN 634*4882a593Smuzhiyundata structure for CAN_RAW based applications. When the application is 635*4882a593Smuzhiyunexecuted on an older Linux kernel and switching the CAN_RAW_FD_FRAMES 636*4882a593Smuzhiyunsocket option returns an error: No problem. You'll get legacy CAN frames 637*4882a593Smuzhiyunor CAN FD frames and can process them the same way. 638*4882a593Smuzhiyun 639*4882a593SmuzhiyunWhen sending to CAN devices make sure that the device is capable to handle 640*4882a593SmuzhiyunCAN FD frames by checking if the device maximum transfer unit is CANFD_MTU. 641*4882a593SmuzhiyunThe CAN device MTU can be retrieved e.g. with a SIOCGIFMTU ioctl() syscall. 642*4882a593Smuzhiyun 643*4882a593Smuzhiyun 644*4882a593SmuzhiyunRAW socket option CAN_RAW_JOIN_FILTERS 645*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 646*4882a593Smuzhiyun 647*4882a593SmuzhiyunThe CAN_RAW socket can set multiple CAN identifier specific filters that 648*4882a593Smuzhiyunlead to multiple filters in the af_can.c filter processing. These filters 649*4882a593Smuzhiyunare indenpendent from each other which leads to logical OR'ed filters when 650*4882a593Smuzhiyunapplied (see :ref:`socketcan-rawfilter`). 651*4882a593Smuzhiyun 652*4882a593SmuzhiyunThis socket option joines the given CAN filters in the way that only CAN 653*4882a593Smuzhiyunframes are passed to user space that matched *all* given CAN filters. The 654*4882a593Smuzhiyunsemantic for the applied filters is therefore changed to a logical AND. 655*4882a593Smuzhiyun 656*4882a593SmuzhiyunThis is useful especially when the filterset is a combination of filters 657*4882a593Smuzhiyunwhere the CAN_INV_FILTER flag is set in order to notch single CAN IDs or 658*4882a593SmuzhiyunCAN ID ranges from the incoming traffic. 659*4882a593Smuzhiyun 660*4882a593Smuzhiyun 661*4882a593SmuzhiyunRAW Socket Returned Message Flags 662*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 663*4882a593Smuzhiyun 664*4882a593SmuzhiyunWhen using recvmsg() call, the msg->msg_flags may contain following flags: 665*4882a593Smuzhiyun 666*4882a593SmuzhiyunMSG_DONTROUTE: 667*4882a593Smuzhiyun set when the received frame was created on the local host. 668*4882a593Smuzhiyun 669*4882a593SmuzhiyunMSG_CONFIRM: 670*4882a593Smuzhiyun set when the frame was sent via the socket it is received on. 671*4882a593Smuzhiyun This flag can be interpreted as a 'transmission confirmation' when the 672*4882a593Smuzhiyun CAN driver supports the echo of frames on driver level, see 673*4882a593Smuzhiyun :ref:`socketcan-local-loopback1` and :ref:`socketcan-local-loopback2`. 674*4882a593Smuzhiyun In order to receive such messages, CAN_RAW_RECV_OWN_MSGS must be set. 675*4882a593Smuzhiyun 676*4882a593Smuzhiyun 677*4882a593SmuzhiyunBroadcast Manager Protocol Sockets (SOCK_DGRAM) 678*4882a593Smuzhiyun----------------------------------------------- 679*4882a593Smuzhiyun 680*4882a593SmuzhiyunThe Broadcast Manager protocol provides a command based configuration 681*4882a593Smuzhiyuninterface to filter and send (e.g. cyclic) CAN messages in kernel space. 682*4882a593Smuzhiyun 683*4882a593SmuzhiyunReceive filters can be used to down sample frequent messages; detect events 684*4882a593Smuzhiyunsuch as message contents changes, packet length changes, and do time-out 685*4882a593Smuzhiyunmonitoring of received messages. 686*4882a593Smuzhiyun 687*4882a593SmuzhiyunPeriodic transmission tasks of CAN frames or a sequence of CAN frames can be 688*4882a593Smuzhiyuncreated and modified at runtime; both the message content and the two 689*4882a593Smuzhiyunpossible transmit intervals can be altered. 690*4882a593Smuzhiyun 691*4882a593SmuzhiyunA BCM socket is not intended for sending individual CAN frames using the 692*4882a593Smuzhiyunstruct can_frame as known from the CAN_RAW socket. Instead a special BCM 693*4882a593Smuzhiyunconfiguration message is defined. The basic BCM configuration message used 694*4882a593Smuzhiyunto communicate with the broadcast manager and the available operations are 695*4882a593Smuzhiyundefined in the linux/can/bcm.h include. The BCM message consists of a 696*4882a593Smuzhiyunmessage header with a command ('opcode') followed by zero or more CAN frames. 697*4882a593SmuzhiyunThe broadcast manager sends responses to user space in the same form: 698*4882a593Smuzhiyun 699*4882a593Smuzhiyun.. code-block:: C 700*4882a593Smuzhiyun 701*4882a593Smuzhiyun struct bcm_msg_head { 702*4882a593Smuzhiyun __u32 opcode; /* command */ 703*4882a593Smuzhiyun __u32 flags; /* special flags */ 704*4882a593Smuzhiyun __u32 count; /* run 'count' times with ival1 */ 705*4882a593Smuzhiyun struct timeval ival1, ival2; /* count and subsequent interval */ 706*4882a593Smuzhiyun canid_t can_id; /* unique can_id for task */ 707*4882a593Smuzhiyun __u32 nframes; /* number of can_frames following */ 708*4882a593Smuzhiyun struct can_frame frames[0]; 709*4882a593Smuzhiyun }; 710*4882a593Smuzhiyun 711*4882a593SmuzhiyunThe aligned payload 'frames' uses the same basic CAN frame structure defined 712*4882a593Smuzhiyunat the beginning of :ref:`socketcan-rawfd` and in the include/linux/can.h include. All 713*4882a593Smuzhiyunmessages to the broadcast manager from user space have this structure. 714*4882a593Smuzhiyun 715*4882a593SmuzhiyunNote a CAN_BCM socket must be connected instead of bound after socket 716*4882a593Smuzhiyuncreation (example without error checking): 717*4882a593Smuzhiyun 718*4882a593Smuzhiyun.. code-block:: C 719*4882a593Smuzhiyun 720*4882a593Smuzhiyun int s; 721*4882a593Smuzhiyun struct sockaddr_can addr; 722*4882a593Smuzhiyun struct ifreq ifr; 723*4882a593Smuzhiyun 724*4882a593Smuzhiyun s = socket(PF_CAN, SOCK_DGRAM, CAN_BCM); 725*4882a593Smuzhiyun 726*4882a593Smuzhiyun strcpy(ifr.ifr_name, "can0"); 727*4882a593Smuzhiyun ioctl(s, SIOCGIFINDEX, &ifr); 728*4882a593Smuzhiyun 729*4882a593Smuzhiyun addr.can_family = AF_CAN; 730*4882a593Smuzhiyun addr.can_ifindex = ifr.ifr_ifindex; 731*4882a593Smuzhiyun 732*4882a593Smuzhiyun connect(s, (struct sockaddr *)&addr, sizeof(addr)); 733*4882a593Smuzhiyun 734*4882a593Smuzhiyun (..) 735*4882a593Smuzhiyun 736*4882a593SmuzhiyunThe broadcast manager socket is able to handle any number of in flight 737*4882a593Smuzhiyuntransmissions or receive filters concurrently. The different RX/TX jobs are 738*4882a593Smuzhiyundistinguished by the unique can_id in each BCM message. However additional 739*4882a593SmuzhiyunCAN_BCM sockets are recommended to communicate on multiple CAN interfaces. 740*4882a593SmuzhiyunWhen the broadcast manager socket is bound to 'any' CAN interface (=> the 741*4882a593Smuzhiyuninterface index is set to zero) the configured receive filters apply to any 742*4882a593SmuzhiyunCAN interface unless the sendto() syscall is used to overrule the 'any' CAN 743*4882a593Smuzhiyuninterface index. When using recvfrom() instead of read() to retrieve BCM 744*4882a593Smuzhiyunsocket messages the originating CAN interface is provided in can_ifindex. 745*4882a593Smuzhiyun 746*4882a593Smuzhiyun 747*4882a593SmuzhiyunBroadcast Manager Operations 748*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 749*4882a593Smuzhiyun 750*4882a593SmuzhiyunThe opcode defines the operation for the broadcast manager to carry out, 751*4882a593Smuzhiyunor details the broadcast managers response to several events, including 752*4882a593Smuzhiyunuser requests. 753*4882a593Smuzhiyun 754*4882a593SmuzhiyunTransmit Operations (user space to broadcast manager): 755*4882a593Smuzhiyun 756*4882a593SmuzhiyunTX_SETUP: 757*4882a593Smuzhiyun Create (cyclic) transmission task. 758*4882a593Smuzhiyun 759*4882a593SmuzhiyunTX_DELETE: 760*4882a593Smuzhiyun Remove (cyclic) transmission task, requires only can_id. 761*4882a593Smuzhiyun 762*4882a593SmuzhiyunTX_READ: 763*4882a593Smuzhiyun Read properties of (cyclic) transmission task for can_id. 764*4882a593Smuzhiyun 765*4882a593SmuzhiyunTX_SEND: 766*4882a593Smuzhiyun Send one CAN frame. 767*4882a593Smuzhiyun 768*4882a593SmuzhiyunTransmit Responses (broadcast manager to user space): 769*4882a593Smuzhiyun 770*4882a593SmuzhiyunTX_STATUS: 771*4882a593Smuzhiyun Reply to TX_READ request (transmission task configuration). 772*4882a593Smuzhiyun 773*4882a593SmuzhiyunTX_EXPIRED: 774*4882a593Smuzhiyun Notification when counter finishes sending at initial interval 775*4882a593Smuzhiyun 'ival1'. Requires the TX_COUNTEVT flag to be set at TX_SETUP. 776*4882a593Smuzhiyun 777*4882a593SmuzhiyunReceive Operations (user space to broadcast manager): 778*4882a593Smuzhiyun 779*4882a593SmuzhiyunRX_SETUP: 780*4882a593Smuzhiyun Create RX content filter subscription. 781*4882a593Smuzhiyun 782*4882a593SmuzhiyunRX_DELETE: 783*4882a593Smuzhiyun Remove RX content filter subscription, requires only can_id. 784*4882a593Smuzhiyun 785*4882a593SmuzhiyunRX_READ: 786*4882a593Smuzhiyun Read properties of RX content filter subscription for can_id. 787*4882a593Smuzhiyun 788*4882a593SmuzhiyunReceive Responses (broadcast manager to user space): 789*4882a593Smuzhiyun 790*4882a593SmuzhiyunRX_STATUS: 791*4882a593Smuzhiyun Reply to RX_READ request (filter task configuration). 792*4882a593Smuzhiyun 793*4882a593SmuzhiyunRX_TIMEOUT: 794*4882a593Smuzhiyun Cyclic message is detected to be absent (timer ival1 expired). 795*4882a593Smuzhiyun 796*4882a593SmuzhiyunRX_CHANGED: 797*4882a593Smuzhiyun BCM message with updated CAN frame (detected content change). 798*4882a593Smuzhiyun Sent on first message received or on receipt of revised CAN messages. 799*4882a593Smuzhiyun 800*4882a593Smuzhiyun 801*4882a593SmuzhiyunBroadcast Manager Message Flags 802*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 803*4882a593Smuzhiyun 804*4882a593SmuzhiyunWhen sending a message to the broadcast manager the 'flags' element may 805*4882a593Smuzhiyuncontain the following flag definitions which influence the behaviour: 806*4882a593Smuzhiyun 807*4882a593SmuzhiyunSETTIMER: 808*4882a593Smuzhiyun Set the values of ival1, ival2 and count 809*4882a593Smuzhiyun 810*4882a593SmuzhiyunSTARTTIMER: 811*4882a593Smuzhiyun Start the timer with the actual values of ival1, ival2 812*4882a593Smuzhiyun and count. Starting the timer leads simultaneously to emit a CAN frame. 813*4882a593Smuzhiyun 814*4882a593SmuzhiyunTX_COUNTEVT: 815*4882a593Smuzhiyun Create the message TX_EXPIRED when count expires 816*4882a593Smuzhiyun 817*4882a593SmuzhiyunTX_ANNOUNCE: 818*4882a593Smuzhiyun A change of data by the process is emitted immediately. 819*4882a593Smuzhiyun 820*4882a593SmuzhiyunTX_CP_CAN_ID: 821*4882a593Smuzhiyun Copies the can_id from the message header to each 822*4882a593Smuzhiyun subsequent frame in frames. This is intended as usage simplification. For 823*4882a593Smuzhiyun TX tasks the unique can_id from the message header may differ from the 824*4882a593Smuzhiyun can_id(s) stored for transmission in the subsequent struct can_frame(s). 825*4882a593Smuzhiyun 826*4882a593SmuzhiyunRX_FILTER_ID: 827*4882a593Smuzhiyun Filter by can_id alone, no frames required (nframes=0). 828*4882a593Smuzhiyun 829*4882a593SmuzhiyunRX_CHECK_DLC: 830*4882a593Smuzhiyun A change of the DLC leads to an RX_CHANGED. 831*4882a593Smuzhiyun 832*4882a593SmuzhiyunRX_NO_AUTOTIMER: 833*4882a593Smuzhiyun Prevent automatically starting the timeout monitor. 834*4882a593Smuzhiyun 835*4882a593SmuzhiyunRX_ANNOUNCE_RESUME: 836*4882a593Smuzhiyun If passed at RX_SETUP and a receive timeout occurred, a 837*4882a593Smuzhiyun RX_CHANGED message will be generated when the (cyclic) receive restarts. 838*4882a593Smuzhiyun 839*4882a593SmuzhiyunTX_RESET_MULTI_IDX: 840*4882a593Smuzhiyun Reset the index for the multiple frame transmission. 841*4882a593Smuzhiyun 842*4882a593SmuzhiyunRX_RTR_FRAME: 843*4882a593Smuzhiyun Send reply for RTR-request (placed in op->frames[0]). 844*4882a593Smuzhiyun 845*4882a593Smuzhiyun 846*4882a593SmuzhiyunBroadcast Manager Transmission Timers 847*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 848*4882a593Smuzhiyun 849*4882a593SmuzhiyunPeriodic transmission configurations may use up to two interval timers. 850*4882a593SmuzhiyunIn this case the BCM sends a number of messages ('count') at an interval 851*4882a593Smuzhiyun'ival1', then continuing to send at another given interval 'ival2'. When 852*4882a593Smuzhiyunonly one timer is needed 'count' is set to zero and only 'ival2' is used. 853*4882a593SmuzhiyunWhen SET_TIMER and START_TIMER flag were set the timers are activated. 854*4882a593SmuzhiyunThe timer values can be altered at runtime when only SET_TIMER is set. 855*4882a593Smuzhiyun 856*4882a593Smuzhiyun 857*4882a593SmuzhiyunBroadcast Manager message sequence transmission 858*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 859*4882a593Smuzhiyun 860*4882a593SmuzhiyunUp to 256 CAN frames can be transmitted in a sequence in the case of a cyclic 861*4882a593SmuzhiyunTX task configuration. The number of CAN frames is provided in the 'nframes' 862*4882a593Smuzhiyunelement of the BCM message head. The defined number of CAN frames are added 863*4882a593Smuzhiyunas array to the TX_SETUP BCM configuration message: 864*4882a593Smuzhiyun 865*4882a593Smuzhiyun.. code-block:: C 866*4882a593Smuzhiyun 867*4882a593Smuzhiyun /* create a struct to set up a sequence of four CAN frames */ 868*4882a593Smuzhiyun struct { 869*4882a593Smuzhiyun struct bcm_msg_head msg_head; 870*4882a593Smuzhiyun struct can_frame frame[4]; 871*4882a593Smuzhiyun } mytxmsg; 872*4882a593Smuzhiyun 873*4882a593Smuzhiyun (..) 874*4882a593Smuzhiyun mytxmsg.msg_head.nframes = 4; 875*4882a593Smuzhiyun (..) 876*4882a593Smuzhiyun 877*4882a593Smuzhiyun write(s, &mytxmsg, sizeof(mytxmsg)); 878*4882a593Smuzhiyun 879*4882a593SmuzhiyunWith every transmission the index in the array of CAN frames is increased 880*4882a593Smuzhiyunand set to zero at index overflow. 881*4882a593Smuzhiyun 882*4882a593Smuzhiyun 883*4882a593SmuzhiyunBroadcast Manager Receive Filter Timers 884*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 885*4882a593Smuzhiyun 886*4882a593SmuzhiyunThe timer values ival1 or ival2 may be set to non-zero values at RX_SETUP. 887*4882a593SmuzhiyunWhen the SET_TIMER flag is set the timers are enabled: 888*4882a593Smuzhiyun 889*4882a593Smuzhiyunival1: 890*4882a593Smuzhiyun Send RX_TIMEOUT when a received message is not received again within 891*4882a593Smuzhiyun the given time. When START_TIMER is set at RX_SETUP the timeout detection 892*4882a593Smuzhiyun is activated directly - even without a former CAN frame reception. 893*4882a593Smuzhiyun 894*4882a593Smuzhiyunival2: 895*4882a593Smuzhiyun Throttle the received message rate down to the value of ival2. This 896*4882a593Smuzhiyun is useful to reduce messages for the application when the signal inside the 897*4882a593Smuzhiyun CAN frame is stateless as state changes within the ival2 periode may get 898*4882a593Smuzhiyun lost. 899*4882a593Smuzhiyun 900*4882a593SmuzhiyunBroadcast Manager Multiplex Message Receive Filter 901*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 902*4882a593Smuzhiyun 903*4882a593SmuzhiyunTo filter for content changes in multiplex message sequences an array of more 904*4882a593Smuzhiyunthan one CAN frames can be passed in a RX_SETUP configuration message. The 905*4882a593Smuzhiyundata bytes of the first CAN frame contain the mask of relevant bits that 906*4882a593Smuzhiyunhave to match in the subsequent CAN frames with the received CAN frame. 907*4882a593SmuzhiyunIf one of the subsequent CAN frames is matching the bits in that frame data 908*4882a593Smuzhiyunmark the relevant content to be compared with the previous received content. 909*4882a593SmuzhiyunUp to 257 CAN frames (multiplex filter bit mask CAN frame plus 256 CAN 910*4882a593Smuzhiyunfilters) can be added as array to the TX_SETUP BCM configuration message: 911*4882a593Smuzhiyun 912*4882a593Smuzhiyun.. code-block:: C 913*4882a593Smuzhiyun 914*4882a593Smuzhiyun /* usually used to clear CAN frame data[] - beware of endian problems! */ 915*4882a593Smuzhiyun #define U64_DATA(p) (*(unsigned long long*)(p)->data) 916*4882a593Smuzhiyun 917*4882a593Smuzhiyun struct { 918*4882a593Smuzhiyun struct bcm_msg_head msg_head; 919*4882a593Smuzhiyun struct can_frame frame[5]; 920*4882a593Smuzhiyun } msg; 921*4882a593Smuzhiyun 922*4882a593Smuzhiyun msg.msg_head.opcode = RX_SETUP; 923*4882a593Smuzhiyun msg.msg_head.can_id = 0x42; 924*4882a593Smuzhiyun msg.msg_head.flags = 0; 925*4882a593Smuzhiyun msg.msg_head.nframes = 5; 926*4882a593Smuzhiyun U64_DATA(&msg.frame[0]) = 0xFF00000000000000ULL; /* MUX mask */ 927*4882a593Smuzhiyun U64_DATA(&msg.frame[1]) = 0x01000000000000FFULL; /* data mask (MUX 0x01) */ 928*4882a593Smuzhiyun U64_DATA(&msg.frame[2]) = 0x0200FFFF000000FFULL; /* data mask (MUX 0x02) */ 929*4882a593Smuzhiyun U64_DATA(&msg.frame[3]) = 0x330000FFFFFF0003ULL; /* data mask (MUX 0x33) */ 930*4882a593Smuzhiyun U64_DATA(&msg.frame[4]) = 0x4F07FC0FF0000000ULL; /* data mask (MUX 0x4F) */ 931*4882a593Smuzhiyun 932*4882a593Smuzhiyun write(s, &msg, sizeof(msg)); 933*4882a593Smuzhiyun 934*4882a593Smuzhiyun 935*4882a593SmuzhiyunBroadcast Manager CAN FD Support 936*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 937*4882a593Smuzhiyun 938*4882a593SmuzhiyunThe programming API of the CAN_BCM depends on struct can_frame which is 939*4882a593Smuzhiyungiven as array directly behind the bcm_msg_head structure. To follow this 940*4882a593Smuzhiyunschema for the CAN FD frames a new flag 'CAN_FD_FRAME' in the bcm_msg_head 941*4882a593Smuzhiyunflags indicates that the concatenated CAN frame structures behind the 942*4882a593Smuzhiyunbcm_msg_head are defined as struct canfd_frame: 943*4882a593Smuzhiyun 944*4882a593Smuzhiyun.. code-block:: C 945*4882a593Smuzhiyun 946*4882a593Smuzhiyun struct { 947*4882a593Smuzhiyun struct bcm_msg_head msg_head; 948*4882a593Smuzhiyun struct canfd_frame frame[5]; 949*4882a593Smuzhiyun } msg; 950*4882a593Smuzhiyun 951*4882a593Smuzhiyun msg.msg_head.opcode = RX_SETUP; 952*4882a593Smuzhiyun msg.msg_head.can_id = 0x42; 953*4882a593Smuzhiyun msg.msg_head.flags = CAN_FD_FRAME; 954*4882a593Smuzhiyun msg.msg_head.nframes = 5; 955*4882a593Smuzhiyun (..) 956*4882a593Smuzhiyun 957*4882a593SmuzhiyunWhen using CAN FD frames for multiplex filtering the MUX mask is still 958*4882a593Smuzhiyunexpected in the first 64 bit of the struct canfd_frame data section. 959*4882a593Smuzhiyun 960*4882a593Smuzhiyun 961*4882a593SmuzhiyunConnected Transport Protocols (SOCK_SEQPACKET) 962*4882a593Smuzhiyun---------------------------------------------- 963*4882a593Smuzhiyun 964*4882a593Smuzhiyun(to be written) 965*4882a593Smuzhiyun 966*4882a593Smuzhiyun 967*4882a593SmuzhiyunUnconnected Transport Protocols (SOCK_DGRAM) 968*4882a593Smuzhiyun-------------------------------------------- 969*4882a593Smuzhiyun 970*4882a593Smuzhiyun(to be written) 971*4882a593Smuzhiyun 972*4882a593Smuzhiyun 973*4882a593Smuzhiyun.. _socketcan-core-module: 974*4882a593Smuzhiyun 975*4882a593SmuzhiyunSocketCAN Core Module 976*4882a593Smuzhiyun===================== 977*4882a593Smuzhiyun 978*4882a593SmuzhiyunThe SocketCAN core module implements the protocol family 979*4882a593SmuzhiyunPF_CAN. CAN protocol modules are loaded by the core module at 980*4882a593Smuzhiyunruntime. The core module provides an interface for CAN protocol 981*4882a593Smuzhiyunmodules to subscribe needed CAN IDs (see :ref:`socketcan-receive-lists`). 982*4882a593Smuzhiyun 983*4882a593Smuzhiyun 984*4882a593Smuzhiyuncan.ko Module Params 985*4882a593Smuzhiyun-------------------- 986*4882a593Smuzhiyun 987*4882a593Smuzhiyun- **stats_timer**: 988*4882a593Smuzhiyun To calculate the SocketCAN core statistics 989*4882a593Smuzhiyun (e.g. current/maximum frames per second) this 1 second timer is 990*4882a593Smuzhiyun invoked at can.ko module start time by default. This timer can be 991*4882a593Smuzhiyun disabled by using stattimer=0 on the module commandline. 992*4882a593Smuzhiyun 993*4882a593Smuzhiyun- **debug**: 994*4882a593Smuzhiyun (removed since SocketCAN SVN r546) 995*4882a593Smuzhiyun 996*4882a593Smuzhiyun 997*4882a593Smuzhiyunprocfs content 998*4882a593Smuzhiyun-------------- 999*4882a593Smuzhiyun 1000*4882a593SmuzhiyunAs described in :ref:`socketcan-receive-lists` the SocketCAN core uses several filter 1001*4882a593Smuzhiyunlists to deliver received CAN frames to CAN protocol modules. These 1002*4882a593Smuzhiyunreceive lists, their filters and the count of filter matches can be 1003*4882a593Smuzhiyunchecked in the appropriate receive list. All entries contain the 1004*4882a593Smuzhiyundevice and a protocol module identifier:: 1005*4882a593Smuzhiyun 1006*4882a593Smuzhiyun foo@bar:~$ cat /proc/net/can/rcvlist_all 1007*4882a593Smuzhiyun 1008*4882a593Smuzhiyun receive list 'rx_all': 1009*4882a593Smuzhiyun (vcan3: no entry) 1010*4882a593Smuzhiyun (vcan2: no entry) 1011*4882a593Smuzhiyun (vcan1: no entry) 1012*4882a593Smuzhiyun device can_id can_mask function userdata matches ident 1013*4882a593Smuzhiyun vcan0 000 00000000 f88e6370 f6c6f400 0 raw 1014*4882a593Smuzhiyun (any: no entry) 1015*4882a593Smuzhiyun 1016*4882a593SmuzhiyunIn this example an application requests any CAN traffic from vcan0:: 1017*4882a593Smuzhiyun 1018*4882a593Smuzhiyun rcvlist_all - list for unfiltered entries (no filter operations) 1019*4882a593Smuzhiyun rcvlist_eff - list for single extended frame (EFF) entries 1020*4882a593Smuzhiyun rcvlist_err - list for error message frames masks 1021*4882a593Smuzhiyun rcvlist_fil - list for mask/value filters 1022*4882a593Smuzhiyun rcvlist_inv - list for mask/value filters (inverse semantic) 1023*4882a593Smuzhiyun rcvlist_sff - list for single standard frame (SFF) entries 1024*4882a593Smuzhiyun 1025*4882a593SmuzhiyunAdditional procfs files in /proc/net/can:: 1026*4882a593Smuzhiyun 1027*4882a593Smuzhiyun stats - SocketCAN core statistics (rx/tx frames, match ratios, ...) 1028*4882a593Smuzhiyun reset_stats - manual statistic reset 1029*4882a593Smuzhiyun version - prints the SocketCAN core version and the ABI version 1030*4882a593Smuzhiyun 1031*4882a593Smuzhiyun 1032*4882a593SmuzhiyunWriting Own CAN Protocol Modules 1033*4882a593Smuzhiyun-------------------------------- 1034*4882a593Smuzhiyun 1035*4882a593SmuzhiyunTo implement a new protocol in the protocol family PF_CAN a new 1036*4882a593Smuzhiyunprotocol has to be defined in include/linux/can.h . 1037*4882a593SmuzhiyunThe prototypes and definitions to use the SocketCAN core can be 1038*4882a593Smuzhiyunaccessed by including include/linux/can/core.h . 1039*4882a593SmuzhiyunIn addition to functions that register the CAN protocol and the 1040*4882a593SmuzhiyunCAN device notifier chain there are functions to subscribe CAN 1041*4882a593Smuzhiyunframes received by CAN interfaces and to send CAN frames:: 1042*4882a593Smuzhiyun 1043*4882a593Smuzhiyun can_rx_register - subscribe CAN frames from a specific interface 1044*4882a593Smuzhiyun can_rx_unregister - unsubscribe CAN frames from a specific interface 1045*4882a593Smuzhiyun can_send - transmit a CAN frame (optional with local loopback) 1046*4882a593Smuzhiyun 1047*4882a593SmuzhiyunFor details see the kerneldoc documentation in net/can/af_can.c or 1048*4882a593Smuzhiyunthe source code of net/can/raw.c or net/can/bcm.c . 1049*4882a593Smuzhiyun 1050*4882a593Smuzhiyun 1051*4882a593SmuzhiyunCAN Network Drivers 1052*4882a593Smuzhiyun=================== 1053*4882a593Smuzhiyun 1054*4882a593SmuzhiyunWriting a CAN network device driver is much easier than writing a 1055*4882a593SmuzhiyunCAN character device driver. Similar to other known network device 1056*4882a593Smuzhiyundrivers you mainly have to deal with: 1057*4882a593Smuzhiyun 1058*4882a593Smuzhiyun- TX: Put the CAN frame from the socket buffer to the CAN controller. 1059*4882a593Smuzhiyun- RX: Put the CAN frame from the CAN controller to the socket buffer. 1060*4882a593Smuzhiyun 1061*4882a593SmuzhiyunSee e.g. at Documentation/networking/netdevices.rst . The differences 1062*4882a593Smuzhiyunfor writing CAN network device driver are described below: 1063*4882a593Smuzhiyun 1064*4882a593Smuzhiyun 1065*4882a593SmuzhiyunGeneral Settings 1066*4882a593Smuzhiyun---------------- 1067*4882a593Smuzhiyun 1068*4882a593Smuzhiyun.. code-block:: C 1069*4882a593Smuzhiyun 1070*4882a593Smuzhiyun dev->type = ARPHRD_CAN; /* the netdevice hardware type */ 1071*4882a593Smuzhiyun dev->flags = IFF_NOARP; /* CAN has no arp */ 1072*4882a593Smuzhiyun 1073*4882a593Smuzhiyun dev->mtu = CAN_MTU; /* sizeof(struct can_frame) -> legacy CAN interface */ 1074*4882a593Smuzhiyun 1075*4882a593Smuzhiyun or alternative, when the controller supports CAN with flexible data rate: 1076*4882a593Smuzhiyun dev->mtu = CANFD_MTU; /* sizeof(struct canfd_frame) -> CAN FD interface */ 1077*4882a593Smuzhiyun 1078*4882a593SmuzhiyunThe struct can_frame or struct canfd_frame is the payload of each socket 1079*4882a593Smuzhiyunbuffer (skbuff) in the protocol family PF_CAN. 1080*4882a593Smuzhiyun 1081*4882a593Smuzhiyun 1082*4882a593Smuzhiyun.. _socketcan-local-loopback2: 1083*4882a593Smuzhiyun 1084*4882a593SmuzhiyunLocal Loopback of Sent Frames 1085*4882a593Smuzhiyun----------------------------- 1086*4882a593Smuzhiyun 1087*4882a593SmuzhiyunAs described in :ref:`socketcan-local-loopback1` the CAN network device driver should 1088*4882a593Smuzhiyunsupport a local loopback functionality similar to the local echo 1089*4882a593Smuzhiyune.g. of tty devices. In this case the driver flag IFF_ECHO has to be 1090*4882a593Smuzhiyunset to prevent the PF_CAN core from locally echoing sent frames 1091*4882a593Smuzhiyun(aka loopback) as fallback solution:: 1092*4882a593Smuzhiyun 1093*4882a593Smuzhiyun dev->flags = (IFF_NOARP | IFF_ECHO); 1094*4882a593Smuzhiyun 1095*4882a593Smuzhiyun 1096*4882a593SmuzhiyunCAN Controller Hardware Filters 1097*4882a593Smuzhiyun------------------------------- 1098*4882a593Smuzhiyun 1099*4882a593SmuzhiyunTo reduce the interrupt load on deep embedded systems some CAN 1100*4882a593Smuzhiyuncontrollers support the filtering of CAN IDs or ranges of CAN IDs. 1101*4882a593SmuzhiyunThese hardware filter capabilities vary from controller to 1102*4882a593Smuzhiyuncontroller and have to be identified as not feasible in a multi-user 1103*4882a593Smuzhiyunnetworking approach. The use of the very controller specific 1104*4882a593Smuzhiyunhardware filters could make sense in a very dedicated use-case, as a 1105*4882a593Smuzhiyunfilter on driver level would affect all users in the multi-user 1106*4882a593Smuzhiyunsystem. The high efficient filter sets inside the PF_CAN core allow 1107*4882a593Smuzhiyunto set different multiple filters for each socket separately. 1108*4882a593SmuzhiyunTherefore the use of hardware filters goes to the category 'handmade 1109*4882a593Smuzhiyuntuning on deep embedded systems'. The author is running a MPC603e 1110*4882a593Smuzhiyun@133MHz with four SJA1000 CAN controllers from 2002 under heavy bus 1111*4882a593Smuzhiyunload without any problems ... 1112*4882a593Smuzhiyun 1113*4882a593Smuzhiyun 1114*4882a593SmuzhiyunThe Virtual CAN Driver (vcan) 1115*4882a593Smuzhiyun----------------------------- 1116*4882a593Smuzhiyun 1117*4882a593SmuzhiyunSimilar to the network loopback devices, vcan offers a virtual local 1118*4882a593SmuzhiyunCAN interface. A full qualified address on CAN consists of 1119*4882a593Smuzhiyun 1120*4882a593Smuzhiyun- a unique CAN Identifier (CAN ID) 1121*4882a593Smuzhiyun- the CAN bus this CAN ID is transmitted on (e.g. can0) 1122*4882a593Smuzhiyun 1123*4882a593Smuzhiyunso in common use cases more than one virtual CAN interface is needed. 1124*4882a593Smuzhiyun 1125*4882a593SmuzhiyunThe virtual CAN interfaces allow the transmission and reception of CAN 1126*4882a593Smuzhiyunframes without real CAN controller hardware. Virtual CAN network 1127*4882a593Smuzhiyundevices are usually named 'vcanX', like vcan0 vcan1 vcan2 ... 1128*4882a593SmuzhiyunWhen compiled as a module the virtual CAN driver module is called vcan.ko 1129*4882a593Smuzhiyun 1130*4882a593SmuzhiyunSince Linux Kernel version 2.6.24 the vcan driver supports the Kernel 1131*4882a593Smuzhiyunnetlink interface to create vcan network devices. The creation and 1132*4882a593Smuzhiyunremoval of vcan network devices can be managed with the ip(8) tool:: 1133*4882a593Smuzhiyun 1134*4882a593Smuzhiyun - Create a virtual CAN network interface: 1135*4882a593Smuzhiyun $ ip link add type vcan 1136*4882a593Smuzhiyun 1137*4882a593Smuzhiyun - Create a virtual CAN network interface with a specific name 'vcan42': 1138*4882a593Smuzhiyun $ ip link add dev vcan42 type vcan 1139*4882a593Smuzhiyun 1140*4882a593Smuzhiyun - Remove a (virtual CAN) network interface 'vcan42': 1141*4882a593Smuzhiyun $ ip link del vcan42 1142*4882a593Smuzhiyun 1143*4882a593Smuzhiyun 1144*4882a593SmuzhiyunThe CAN Network Device Driver Interface 1145*4882a593Smuzhiyun--------------------------------------- 1146*4882a593Smuzhiyun 1147*4882a593SmuzhiyunThe CAN network device driver interface provides a generic interface 1148*4882a593Smuzhiyunto setup, configure and monitor CAN network devices. The user can then 1149*4882a593Smuzhiyunconfigure the CAN device, like setting the bit-timing parameters, via 1150*4882a593Smuzhiyunthe netlink interface using the program "ip" from the "IPROUTE2" 1151*4882a593Smuzhiyunutility suite. The following chapter describes briefly how to use it. 1152*4882a593SmuzhiyunFurthermore, the interface uses a common data structure and exports a 1153*4882a593Smuzhiyunset of common functions, which all real CAN network device drivers 1154*4882a593Smuzhiyunshould use. Please have a look to the SJA1000 or MSCAN driver to 1155*4882a593Smuzhiyununderstand how to use them. The name of the module is can-dev.ko. 1156*4882a593Smuzhiyun 1157*4882a593Smuzhiyun 1158*4882a593SmuzhiyunNetlink interface to set/get devices properties 1159*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1160*4882a593Smuzhiyun 1161*4882a593SmuzhiyunThe CAN device must be configured via netlink interface. The supported 1162*4882a593Smuzhiyunnetlink message types are defined and briefly described in 1163*4882a593Smuzhiyun"include/linux/can/netlink.h". CAN link support for the program "ip" 1164*4882a593Smuzhiyunof the IPROUTE2 utility suite is available and it can be used as shown 1165*4882a593Smuzhiyunbelow: 1166*4882a593Smuzhiyun 1167*4882a593SmuzhiyunSetting CAN device properties:: 1168*4882a593Smuzhiyun 1169*4882a593Smuzhiyun $ ip link set can0 type can help 1170*4882a593Smuzhiyun Usage: ip link set DEVICE type can 1171*4882a593Smuzhiyun [ bitrate BITRATE [ sample-point SAMPLE-POINT] ] | 1172*4882a593Smuzhiyun [ tq TQ prop-seg PROP_SEG phase-seg1 PHASE-SEG1 1173*4882a593Smuzhiyun phase-seg2 PHASE-SEG2 [ sjw SJW ] ] 1174*4882a593Smuzhiyun 1175*4882a593Smuzhiyun [ dbitrate BITRATE [ dsample-point SAMPLE-POINT] ] | 1176*4882a593Smuzhiyun [ dtq TQ dprop-seg PROP_SEG dphase-seg1 PHASE-SEG1 1177*4882a593Smuzhiyun dphase-seg2 PHASE-SEG2 [ dsjw SJW ] ] 1178*4882a593Smuzhiyun 1179*4882a593Smuzhiyun [ loopback { on | off } ] 1180*4882a593Smuzhiyun [ listen-only { on | off } ] 1181*4882a593Smuzhiyun [ triple-sampling { on | off } ] 1182*4882a593Smuzhiyun [ one-shot { on | off } ] 1183*4882a593Smuzhiyun [ berr-reporting { on | off } ] 1184*4882a593Smuzhiyun [ fd { on | off } ] 1185*4882a593Smuzhiyun [ fd-non-iso { on | off } ] 1186*4882a593Smuzhiyun [ presume-ack { on | off } ] 1187*4882a593Smuzhiyun 1188*4882a593Smuzhiyun [ restart-ms TIME-MS ] 1189*4882a593Smuzhiyun [ restart ] 1190*4882a593Smuzhiyun 1191*4882a593Smuzhiyun Where: BITRATE := { 1..1000000 } 1192*4882a593Smuzhiyun SAMPLE-POINT := { 0.000..0.999 } 1193*4882a593Smuzhiyun TQ := { NUMBER } 1194*4882a593Smuzhiyun PROP-SEG := { 1..8 } 1195*4882a593Smuzhiyun PHASE-SEG1 := { 1..8 } 1196*4882a593Smuzhiyun PHASE-SEG2 := { 1..8 } 1197*4882a593Smuzhiyun SJW := { 1..4 } 1198*4882a593Smuzhiyun RESTART-MS := { 0 | NUMBER } 1199*4882a593Smuzhiyun 1200*4882a593SmuzhiyunDisplay CAN device details and statistics:: 1201*4882a593Smuzhiyun 1202*4882a593Smuzhiyun $ ip -details -statistics link show can0 1203*4882a593Smuzhiyun 2: can0: <NOARP,UP,LOWER_UP,ECHO> mtu 16 qdisc pfifo_fast state UP qlen 10 1204*4882a593Smuzhiyun link/can 1205*4882a593Smuzhiyun can <TRIPLE-SAMPLING> state ERROR-ACTIVE restart-ms 100 1206*4882a593Smuzhiyun bitrate 125000 sample_point 0.875 1207*4882a593Smuzhiyun tq 125 prop-seg 6 phase-seg1 7 phase-seg2 2 sjw 1 1208*4882a593Smuzhiyun sja1000: tseg1 1..16 tseg2 1..8 sjw 1..4 brp 1..64 brp-inc 1 1209*4882a593Smuzhiyun clock 8000000 1210*4882a593Smuzhiyun re-started bus-errors arbit-lost error-warn error-pass bus-off 1211*4882a593Smuzhiyun 41 17457 0 41 42 41 1212*4882a593Smuzhiyun RX: bytes packets errors dropped overrun mcast 1213*4882a593Smuzhiyun 140859 17608 17457 0 0 0 1214*4882a593Smuzhiyun TX: bytes packets errors dropped carrier collsns 1215*4882a593Smuzhiyun 861 112 0 41 0 0 1216*4882a593Smuzhiyun 1217*4882a593SmuzhiyunMore info to the above output: 1218*4882a593Smuzhiyun 1219*4882a593Smuzhiyun"<TRIPLE-SAMPLING>" 1220*4882a593Smuzhiyun Shows the list of selected CAN controller modes: LOOPBACK, 1221*4882a593Smuzhiyun LISTEN-ONLY, or TRIPLE-SAMPLING. 1222*4882a593Smuzhiyun 1223*4882a593Smuzhiyun"state ERROR-ACTIVE" 1224*4882a593Smuzhiyun The current state of the CAN controller: "ERROR-ACTIVE", 1225*4882a593Smuzhiyun "ERROR-WARNING", "ERROR-PASSIVE", "BUS-OFF" or "STOPPED" 1226*4882a593Smuzhiyun 1227*4882a593Smuzhiyun"restart-ms 100" 1228*4882a593Smuzhiyun Automatic restart delay time. If set to a non-zero value, a 1229*4882a593Smuzhiyun restart of the CAN controller will be triggered automatically 1230*4882a593Smuzhiyun in case of a bus-off condition after the specified delay time 1231*4882a593Smuzhiyun in milliseconds. By default it's off. 1232*4882a593Smuzhiyun 1233*4882a593Smuzhiyun"bitrate 125000 sample-point 0.875" 1234*4882a593Smuzhiyun Shows the real bit-rate in bits/sec and the sample-point in the 1235*4882a593Smuzhiyun range 0.000..0.999. If the calculation of bit-timing parameters 1236*4882a593Smuzhiyun is enabled in the kernel (CONFIG_CAN_CALC_BITTIMING=y), the 1237*4882a593Smuzhiyun bit-timing can be defined by setting the "bitrate" argument. 1238*4882a593Smuzhiyun Optionally the "sample-point" can be specified. By default it's 1239*4882a593Smuzhiyun 0.000 assuming CIA-recommended sample-points. 1240*4882a593Smuzhiyun 1241*4882a593Smuzhiyun"tq 125 prop-seg 6 phase-seg1 7 phase-seg2 2 sjw 1" 1242*4882a593Smuzhiyun Shows the time quanta in ns, propagation segment, phase buffer 1243*4882a593Smuzhiyun segment 1 and 2 and the synchronisation jump width in units of 1244*4882a593Smuzhiyun tq. They allow to define the CAN bit-timing in a hardware 1245*4882a593Smuzhiyun independent format as proposed by the Bosch CAN 2.0 spec (see 1246*4882a593Smuzhiyun chapter 8 of http://www.semiconductors.bosch.de/pdf/can2spec.pdf). 1247*4882a593Smuzhiyun 1248*4882a593Smuzhiyun"sja1000: tseg1 1..16 tseg2 1..8 sjw 1..4 brp 1..64 brp-inc 1 clock 8000000" 1249*4882a593Smuzhiyun Shows the bit-timing constants of the CAN controller, here the 1250*4882a593Smuzhiyun "sja1000". The minimum and maximum values of the time segment 1 1251*4882a593Smuzhiyun and 2, the synchronisation jump width in units of tq, the 1252*4882a593Smuzhiyun bitrate pre-scaler and the CAN system clock frequency in Hz. 1253*4882a593Smuzhiyun These constants could be used for user-defined (non-standard) 1254*4882a593Smuzhiyun bit-timing calculation algorithms in user-space. 1255*4882a593Smuzhiyun 1256*4882a593Smuzhiyun"re-started bus-errors arbit-lost error-warn error-pass bus-off" 1257*4882a593Smuzhiyun Shows the number of restarts, bus and arbitration lost errors, 1258*4882a593Smuzhiyun and the state changes to the error-warning, error-passive and 1259*4882a593Smuzhiyun bus-off state. RX overrun errors are listed in the "overrun" 1260*4882a593Smuzhiyun field of the standard network statistics. 1261*4882a593Smuzhiyun 1262*4882a593SmuzhiyunSetting the CAN Bit-Timing 1263*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~ 1264*4882a593Smuzhiyun 1265*4882a593SmuzhiyunThe CAN bit-timing parameters can always be defined in a hardware 1266*4882a593Smuzhiyunindependent format as proposed in the Bosch CAN 2.0 specification 1267*4882a593Smuzhiyunspecifying the arguments "tq", "prop_seg", "phase_seg1", "phase_seg2" 1268*4882a593Smuzhiyunand "sjw":: 1269*4882a593Smuzhiyun 1270*4882a593Smuzhiyun $ ip link set canX type can tq 125 prop-seg 6 \ 1271*4882a593Smuzhiyun phase-seg1 7 phase-seg2 2 sjw 1 1272*4882a593Smuzhiyun 1273*4882a593SmuzhiyunIf the kernel option CONFIG_CAN_CALC_BITTIMING is enabled, CIA 1274*4882a593Smuzhiyunrecommended CAN bit-timing parameters will be calculated if the bit- 1275*4882a593Smuzhiyunrate is specified with the argument "bitrate":: 1276*4882a593Smuzhiyun 1277*4882a593Smuzhiyun $ ip link set canX type can bitrate 125000 1278*4882a593Smuzhiyun 1279*4882a593SmuzhiyunNote that this works fine for the most common CAN controllers with 1280*4882a593Smuzhiyunstandard bit-rates but may *fail* for exotic bit-rates or CAN system 1281*4882a593Smuzhiyunclock frequencies. Disabling CONFIG_CAN_CALC_BITTIMING saves some 1282*4882a593Smuzhiyunspace and allows user-space tools to solely determine and set the 1283*4882a593Smuzhiyunbit-timing parameters. The CAN controller specific bit-timing 1284*4882a593Smuzhiyunconstants can be used for that purpose. They are listed by the 1285*4882a593Smuzhiyunfollowing command:: 1286*4882a593Smuzhiyun 1287*4882a593Smuzhiyun $ ip -details link show can0 1288*4882a593Smuzhiyun ... 1289*4882a593Smuzhiyun sja1000: clock 8000000 tseg1 1..16 tseg2 1..8 sjw 1..4 brp 1..64 brp-inc 1 1290*4882a593Smuzhiyun 1291*4882a593Smuzhiyun 1292*4882a593SmuzhiyunStarting and Stopping the CAN Network Device 1293*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1294*4882a593Smuzhiyun 1295*4882a593SmuzhiyunA CAN network device is started or stopped as usual with the command 1296*4882a593Smuzhiyun"ifconfig canX up/down" or "ip link set canX up/down". Be aware that 1297*4882a593Smuzhiyunyou *must* define proper bit-timing parameters for real CAN devices 1298*4882a593Smuzhiyunbefore you can start it to avoid error-prone default settings:: 1299*4882a593Smuzhiyun 1300*4882a593Smuzhiyun $ ip link set canX up type can bitrate 125000 1301*4882a593Smuzhiyun 1302*4882a593SmuzhiyunA device may enter the "bus-off" state if too many errors occurred on 1303*4882a593Smuzhiyunthe CAN bus. Then no more messages are received or sent. An automatic 1304*4882a593Smuzhiyunbus-off recovery can be enabled by setting the "restart-ms" to a 1305*4882a593Smuzhiyunnon-zero value, e.g.:: 1306*4882a593Smuzhiyun 1307*4882a593Smuzhiyun $ ip link set canX type can restart-ms 100 1308*4882a593Smuzhiyun 1309*4882a593SmuzhiyunAlternatively, the application may realize the "bus-off" condition 1310*4882a593Smuzhiyunby monitoring CAN error message frames and do a restart when 1311*4882a593Smuzhiyunappropriate with the command:: 1312*4882a593Smuzhiyun 1313*4882a593Smuzhiyun $ ip link set canX type can restart 1314*4882a593Smuzhiyun 1315*4882a593SmuzhiyunNote that a restart will also create a CAN error message frame (see 1316*4882a593Smuzhiyunalso :ref:`socketcan-network-problem-notifications`). 1317*4882a593Smuzhiyun 1318*4882a593Smuzhiyun 1319*4882a593Smuzhiyun.. _socketcan-can-fd-driver: 1320*4882a593Smuzhiyun 1321*4882a593SmuzhiyunCAN FD (Flexible Data Rate) Driver Support 1322*4882a593Smuzhiyun------------------------------------------ 1323*4882a593Smuzhiyun 1324*4882a593SmuzhiyunCAN FD capable CAN controllers support two different bitrates for the 1325*4882a593Smuzhiyunarbitration phase and the payload phase of the CAN FD frame. Therefore a 1326*4882a593Smuzhiyunsecond bit timing has to be specified in order to enable the CAN FD bitrate. 1327*4882a593Smuzhiyun 1328*4882a593SmuzhiyunAdditionally CAN FD capable CAN controllers support up to 64 bytes of 1329*4882a593Smuzhiyunpayload. The representation of this length in can_frame.can_dlc and 1330*4882a593Smuzhiyuncanfd_frame.len for userspace applications and inside the Linux network 1331*4882a593Smuzhiyunlayer is a plain value from 0 .. 64 instead of the CAN 'data length code'. 1332*4882a593SmuzhiyunThe data length code was a 1:1 mapping to the payload length in the legacy 1333*4882a593SmuzhiyunCAN frames anyway. The payload length to the bus-relevant DLC mapping is 1334*4882a593Smuzhiyunonly performed inside the CAN drivers, preferably with the helper 1335*4882a593Smuzhiyunfunctions can_dlc2len() and can_len2dlc(). 1336*4882a593Smuzhiyun 1337*4882a593SmuzhiyunThe CAN netdevice driver capabilities can be distinguished by the network 1338*4882a593Smuzhiyundevices maximum transfer unit (MTU):: 1339*4882a593Smuzhiyun 1340*4882a593Smuzhiyun MTU = 16 (CAN_MTU) => sizeof(struct can_frame) => 'legacy' CAN device 1341*4882a593Smuzhiyun MTU = 72 (CANFD_MTU) => sizeof(struct canfd_frame) => CAN FD capable device 1342*4882a593Smuzhiyun 1343*4882a593SmuzhiyunThe CAN device MTU can be retrieved e.g. with a SIOCGIFMTU ioctl() syscall. 1344*4882a593SmuzhiyunN.B. CAN FD capable devices can also handle and send legacy CAN frames. 1345*4882a593Smuzhiyun 1346*4882a593SmuzhiyunWhen configuring CAN FD capable CAN controllers an additional 'data' bitrate 1347*4882a593Smuzhiyunhas to be set. This bitrate for the data phase of the CAN FD frame has to be 1348*4882a593Smuzhiyunat least the bitrate which was configured for the arbitration phase. This 1349*4882a593Smuzhiyunsecond bitrate is specified analogue to the first bitrate but the bitrate 1350*4882a593Smuzhiyunsetting keywords for the 'data' bitrate start with 'd' e.g. dbitrate, 1351*4882a593Smuzhiyundsample-point, dsjw or dtq and similar settings. When a data bitrate is set 1352*4882a593Smuzhiyunwithin the configuration process the controller option "fd on" can be 1353*4882a593Smuzhiyunspecified to enable the CAN FD mode in the CAN controller. This controller 1354*4882a593Smuzhiyunoption also switches the device MTU to 72 (CANFD_MTU). 1355*4882a593Smuzhiyun 1356*4882a593SmuzhiyunThe first CAN FD specification presented as whitepaper at the International 1357*4882a593SmuzhiyunCAN Conference 2012 needed to be improved for data integrity reasons. 1358*4882a593SmuzhiyunTherefore two CAN FD implementations have to be distinguished today: 1359*4882a593Smuzhiyun 1360*4882a593Smuzhiyun- ISO compliant: The ISO 11898-1:2015 CAN FD implementation (default) 1361*4882a593Smuzhiyun- non-ISO compliant: The CAN FD implementation following the 2012 whitepaper 1362*4882a593Smuzhiyun 1363*4882a593SmuzhiyunFinally there are three types of CAN FD controllers: 1364*4882a593Smuzhiyun 1365*4882a593Smuzhiyun1. ISO compliant (fixed) 1366*4882a593Smuzhiyun2. non-ISO compliant (fixed, like the M_CAN IP core v3.0.1 in m_can.c) 1367*4882a593Smuzhiyun3. ISO/non-ISO CAN FD controllers (switchable, like the PEAK PCAN-USB FD) 1368*4882a593Smuzhiyun 1369*4882a593SmuzhiyunThe current ISO/non-ISO mode is announced by the CAN controller driver via 1370*4882a593Smuzhiyunnetlink and displayed by the 'ip' tool (controller option FD-NON-ISO). 1371*4882a593SmuzhiyunThe ISO/non-ISO-mode can be altered by setting 'fd-non-iso {on|off}' for 1372*4882a593Smuzhiyunswitchable CAN FD controllers only. 1373*4882a593Smuzhiyun 1374*4882a593SmuzhiyunExample configuring 500 kbit/s arbitration bitrate and 4 Mbit/s data bitrate:: 1375*4882a593Smuzhiyun 1376*4882a593Smuzhiyun $ ip link set can0 up type can bitrate 500000 sample-point 0.75 \ 1377*4882a593Smuzhiyun dbitrate 4000000 dsample-point 0.8 fd on 1378*4882a593Smuzhiyun $ ip -details link show can0 1379*4882a593Smuzhiyun 5: can0: <NOARP,UP,LOWER_UP,ECHO> mtu 72 qdisc pfifo_fast state UNKNOWN \ 1380*4882a593Smuzhiyun mode DEFAULT group default qlen 10 1381*4882a593Smuzhiyun link/can promiscuity 0 1382*4882a593Smuzhiyun can <FD> state ERROR-ACTIVE (berr-counter tx 0 rx 0) restart-ms 0 1383*4882a593Smuzhiyun bitrate 500000 sample-point 0.750 1384*4882a593Smuzhiyun tq 50 prop-seg 14 phase-seg1 15 phase-seg2 10 sjw 1 1385*4882a593Smuzhiyun pcan_usb_pro_fd: tseg1 1..64 tseg2 1..16 sjw 1..16 brp 1..1024 \ 1386*4882a593Smuzhiyun brp-inc 1 1387*4882a593Smuzhiyun dbitrate 4000000 dsample-point 0.800 1388*4882a593Smuzhiyun dtq 12 dprop-seg 7 dphase-seg1 8 dphase-seg2 4 dsjw 1 1389*4882a593Smuzhiyun pcan_usb_pro_fd: dtseg1 1..16 dtseg2 1..8 dsjw 1..4 dbrp 1..1024 \ 1390*4882a593Smuzhiyun dbrp-inc 1 1391*4882a593Smuzhiyun clock 80000000 1392*4882a593Smuzhiyun 1393*4882a593SmuzhiyunExample when 'fd-non-iso on' is added on this switchable CAN FD adapter:: 1394*4882a593Smuzhiyun 1395*4882a593Smuzhiyun can <FD,FD-NON-ISO> state ERROR-ACTIVE (berr-counter tx 0 rx 0) restart-ms 0 1396*4882a593Smuzhiyun 1397*4882a593Smuzhiyun 1398*4882a593SmuzhiyunSupported CAN Hardware 1399*4882a593Smuzhiyun---------------------- 1400*4882a593Smuzhiyun 1401*4882a593SmuzhiyunPlease check the "Kconfig" file in "drivers/net/can" to get an actual 1402*4882a593Smuzhiyunlist of the support CAN hardware. On the SocketCAN project website 1403*4882a593Smuzhiyun(see :ref:`socketcan-resources`) there might be further drivers available, also for 1404*4882a593Smuzhiyunolder kernel versions. 1405*4882a593Smuzhiyun 1406*4882a593Smuzhiyun 1407*4882a593Smuzhiyun.. _socketcan-resources: 1408*4882a593Smuzhiyun 1409*4882a593SmuzhiyunSocketCAN Resources 1410*4882a593Smuzhiyun=================== 1411*4882a593Smuzhiyun 1412*4882a593SmuzhiyunThe Linux CAN / SocketCAN project resources (project site / mailing list) 1413*4882a593Smuzhiyunare referenced in the MAINTAINERS file in the Linux source tree. 1414*4882a593SmuzhiyunSearch for CAN NETWORK [LAYERS|DRIVERS]. 1415*4882a593Smuzhiyun 1416*4882a593SmuzhiyunCredits 1417*4882a593Smuzhiyun======= 1418*4882a593Smuzhiyun 1419*4882a593Smuzhiyun- Oliver Hartkopp (PF_CAN core, filters, drivers, bcm, SJA1000 driver) 1420*4882a593Smuzhiyun- Urs Thuermann (PF_CAN core, kernel integration, socket interfaces, raw, vcan) 1421*4882a593Smuzhiyun- Jan Kizka (RT-SocketCAN core, Socket-API reconciliation) 1422*4882a593Smuzhiyun- Wolfgang Grandegger (RT-SocketCAN core & drivers, Raw Socket-API reviews, CAN device driver interface, MSCAN driver) 1423*4882a593Smuzhiyun- Robert Schwebel (design reviews, PTXdist integration) 1424*4882a593Smuzhiyun- Marc Kleine-Budde (design reviews, Kernel 2.6 cleanups, drivers) 1425*4882a593Smuzhiyun- Benedikt Spranger (reviews) 1426*4882a593Smuzhiyun- Thomas Gleixner (LKML reviews, coding style, posting hints) 1427*4882a593Smuzhiyun- Andrey Volkov (kernel subtree structure, ioctls, MSCAN driver) 1428*4882a593Smuzhiyun- Matthias Brukner (first SJA1000 CAN netdevice implementation Q2/2003) 1429*4882a593Smuzhiyun- Klaus Hitschler (PEAK driver integration) 1430*4882a593Smuzhiyun- Uwe Koppe (CAN netdevices with PF_PACKET approach) 1431*4882a593Smuzhiyun- Michael Schulze (driver layer loopback requirement, RT CAN drivers review) 1432*4882a593Smuzhiyun- Pavel Pisa (Bit-timing calculation) 1433*4882a593Smuzhiyun- Sascha Hauer (SJA1000 platform driver) 1434*4882a593Smuzhiyun- Sebastian Haas (SJA1000 EMS PCI driver) 1435*4882a593Smuzhiyun- Markus Plessing (SJA1000 EMS PCI driver) 1436*4882a593Smuzhiyun- Per Dalen (SJA1000 Kvaser PCI driver) 1437*4882a593Smuzhiyun- Sam Ravnborg (reviews, coding style, kbuild help) 1438