Lines Matching refs:the
5 Readme file for the Controller Area Network Protocol Family (aka SocketCAN)
11 2 Motivation / Why using the socket API
49 6.5.2 Setting the CAN bit-timing
50 6.5.3 Starting and stopping the CAN network device
67 for Linux based on character devices, SocketCAN uses the Berkeley
68 socket API, the Linux network stack and implements the CAN device
70 as similar as possible to the TCP/IP protocols to allow programmers,
74 2. Motivation / Why using the socket API
77 There have been CAN implementations for Linux before SocketCAN so the
83 receive raw CAN frames, directly to/from the controller hardware.
87 open the device at a time, similar to a serial interface. Exchanging
88 the CAN controller requires employment of another device driver and
89 often the need for adaption of large parts of the application to the
94 to user space applications and which builds upon the Linux network
95 layer, enabling use all of the provided queueing functionality. A device
96 driver for CAN controller hardware registers itself with the Linux
97 network layer as a network device, so that CAN frames from the
98 controller can be passed up to the network layer and on to the CAN
99 protocol family module and also vice-versa. Also, the protocol family
102 dynamically. In fact, the can core module alone does not provide any
104 protocol module. Multiple sockets can be opened at the same time,
105 on different or the same protocol module and they can listen/send
106 frames on different or the same CAN IDs. Several sockets listening on
107 the same interface for frames with the same CAN ID are all passed the
110 selects that protocol when opening the socket, and then can read and
122 * Code duplication. A character device cannot make use of the Linux
126 * Abstraction. In most existing character-device implementations, the
128 provides the character device for the application to work with.
134 which provide a unified character or block device interface to the
135 application on the one hand, and a interface for hardware-specific
136 device drivers on the other hand. These abstractions are provided
137 by subsystems like the tty layer, the audio subsystem or the SCSI
138 and IDE subsystems for the devices mentioned above.
143 layer with all the functionality like registering for certain CAN
147 it would be no more difficult, or may be even easier, to use the
148 networking framework provided by the Linux kernel, and this is what
151 The use of the networking framework of the Linux kernel is just the
157 As described in chapter 2 it is the main goal of SocketCAN to
159 upon the Linux network layer. In contrast to the commonly known
160 TCP/IP and ethernet networking, the CAN bus is a broadcast-only(!)
162 (can_id) is used for arbitration on the CAN-bus. Therefore the CAN-IDs
163 have to be chosen uniquely on the bus. When designing a CAN-ECU
164 network the CAN-IDs are mapped to be sent by a specific ECU.
169 The network transparent access of multiple applications leads to the
170 problem that different applications may be interested in the same
171 CAN-IDs from the same CAN network interface. The SocketCAN core
172 module - which implements the protocol family CAN - provides several
174 application opens a CAN RAW socket, the raw protocol module itself
175 requests the (range of) CAN-IDs from the SocketCAN core that are
176 requested by the user. The subscription and unsubscription of
178 CAN interfaces with the can_rx_(un)register() functions provided to
179 CAN protocol modules by the SocketCAN core (see chapter 5).
180 To optimize the CPU usage at runtime the receive lists are split up
181 into several specific lists per device that match the requested
186 As known from other networking concepts the data exchanging
187 applications may run on the same or different nodes without any
188 change (except for the according addressing information):
197 To ensure that application A receives the same information in the
199 some kind of local loopback of the sent CAN frames on the appropriate
202 The Linux network devices (by default) just can handle the
203 transmission and reception of media dependent frames. Due to the
204 arbitration on the CAN bus the transmission of a low prio CAN-ID
205 may be delayed by the reception of a high prio CAN frame. To
206 reflect the correct* traffic on the node the loopback of the sent
208 the CAN network interface is not capable of performing the loopback for
209 some reason the SocketCAN core can do this task as a fallback solution.
214 the RT-SocketCAN group the loopback optionally may be disabled for each
215 separate socket. See sockopts from the CAN RAW sockets in chapter 4.1.
218 like 'candump' or 'cansniffer' on the (same) node.
222 The use of the CAN bus may lead to several problems on the physical
225 hardware issues on the physical transceiver layer as well as
226 arbitration problems and error frames caused by the different
228 and have to be logged together with the exact timestamp. For this
229 reason the CAN interface driver can generate so called Error Message
230 Frames that can optionally be passed to the user application in the
231 same way as other CAN frames. Whenever an error on the physical layer
232 or the MAC layer is detected (e.g. by the CAN controller) the driver
234 be requested by the user application using the common CAN filter
235 mechanisms. Inside this filter definition the (interested) type of
237 by default. The format of the CAN error message frame is briefly
238 described in the Linux header file "include/uapi/linux/can/error.h".
245 need to pass PF_CAN as the first argument to the socket(2) system
246 call. Currently, there are two CAN protocols to choose from, the raw
247 socket protocol and the broadcast manager (BCM). So to open a socket,
256 respectively. After the successful creation of the socket, you would
257 normally use the bind(2) system call to bind the socket to a CAN
260 the socket, you can read(2) and write(2) from/to the socket or use
261 send(2), sendto(2), sendmsg(2) and the recv* counterpart operations
262 on the socket as usual. There are also CAN specific socket options
265 The basic CAN frame structure and the sockaddr structure are defined
274 The alignment of the (linear) payload data[] to a 64bit boundary
275 allows the user to define their own structs and unions to easily access
276 the CAN payload. There is no given byteorder on the CAN bus by
278 struct can_frame to the user space.
280 The sockaddr_can structure has an interface index like the
294 To determine the interface index an appropriate ioctl() has to
313 To bind a socket to all(!) CAN interfaces the interface index must
314 be 0 (zero). In this case the socket receives CAN frames from every
315 enabled CAN interface. To determine the originating CAN interface
316 the system call recvfrom(2) may be used instead of read(2). To send
318 specify the outgoing interface.
338 /* do something with the received CAN frame */
340 Writing CAN frames can be done similarly, with the write(2) system call:
344 When the CAN interface is bound to 'any' existing CAN interface
345 (addr.can_ifindex = 0) it is recommended to use recvfrom(2) if the
346 information about the originating CAN interface is needed:
356 /* get interface name of the received CAN frame */
361 To write CAN frames on sockets bound to 'any' CAN interface the
374 Generally the handling of CAN FD is very similar to the formerly described
376 bitrates for the arbitration phase and the payload phase of the CAN FD frame
377 and up to 64 bytes of payload. This extended payload length breaks all the
378 kernel interfaces (ABI) which heavily rely on the CAN frame with fixed eight
379 bytes of payload (struct can_frame) like the CAN_RAW socket. Therefore e.g.
380 the CAN_RAW socket supports a new socket option CAN_RAW_FD_FRAMES that
381 switches the socket into a mode that allows the handling of CAN FD frames
395 The struct canfd_frame and the existing struct can_frame have the can_id,
396 the payload length and the payload data at the same offset inside their
397 structures. This allows to handle the different structures very similar.
398 When the content of a struct can_frame is copied into a struct canfd_frame
399 all structure elements can be used as-is - only the data[] becomes extended.
401 When introducing the struct canfd_frame it turned out that the data length
402 code (DLC) of the struct can_frame was used as a length information as the
403 length and the DLC has a 1:1 mapping in the range of 0 .. 8. To preserve
404 the easy handling of the length information the canfd_frame.len element
407 For details about the distinction of CAN and CAN FD capable devices and
408 the mapping to the bus-relevant data length code (DLC), see chapter 6.6.
410 The length of the two CAN(FD) frame structures define the maximum transfer
411 unit (MTU) of the CAN(FD) network interface and skbuff data length. Two
419 Using CAN_RAW sockets is extensively comparable to the commonly
420 known access to CAN character devices. To meet the new possibilities
421 provided by the multi user SocketCAN approach, some reasonable
429 These default settings may be changed before or after binding the socket.
430 To use the referenced definitions of the socket options for CAN_RAW
436 by defining 0 .. n filters with the CAN_RAW_FILTER socket option.
450 The filter can be inverted in this semantic, when the CAN_INV_FILTER
451 bit is set in can_id element of the can_filter structure. In
452 contrast to CAN controller hardware filters the user may set 0 .. n
464 To disable the reception of CAN frames on the selected CAN_RAW socket:
468 To set the filters to zero filters is quite obsolete as to not read
469 data causes the raw socket to discard the received CAN frames. But
470 having this 'send only' use-case we may remove the receive list in the
476 reception time. To reduce the number of checks that need to be performed
477 while walking through the filter lists the CAN core provides an optimized
478 filter handling when the filter subscription focusses on a single CAN ID.
480 For the possible 2048 SFF CAN identifiers the identifier is used as an index
481 to access the corresponding subscription list without any further checks.
482 For the 2^29 possible EFF CAN identifiers a 10 bit XOR folding is used as
483 hash function to retrieve the EFF table index.
485 To benefit from the optimized filters for single CAN identifiers the
487 with set CAN_EFF_FLAG and CAN_RTR_FLAG bits. A set CAN_EFF_FLAG bit in the
489 subscribed. E.g. in the example from above
496 To filter for only 0x123 (SFF) and 0x12345678 (EFF) CAN identifiers the
497 filter has to be defined in this way to benefit from the optimized filters:
510 As described in chapter 3.4 the CAN interface driver can generate so
511 called Error Message Frames that can optionally be passed to the user
512 application in the same way as other CAN frames. The possible
514 using the appropriate error mask. To register for every possible
515 error condition CAN_ERR_MASK can be used as value for the error mask.
516 The values for the error mask are defined in linux/can/error.h .
525 To meet multi user needs the local loopback is enabled by default
527 (e.g. when only one application uses the CAN bus) this loopback
536 When the local loopback is enabled, all the sent CAN frames are
537 looped back to the open CAN sockets that registered for the CAN
538 frames' CAN-ID on this given interface to meet the multi user
539 needs. The reception of the CAN frames on the same socket that was
540 sending the CAN frame is assumed to be unwanted and therefore
552 CAN_RAW_FD_FRAMES which is off by default. When the new socket option is
553 not supported by the CAN_RAW socket (e.g. on older kernels), switching the
554 CAN_RAW_FD_FRAMES option returns the error -ENOPROTOOPT.
556 Once CAN_RAW_FD_FRAMES is enabled the application can send both CAN frames
557 and CAN FD frames. OTOH the application has to handle CAN and CAN FD frames
558 when reading from the socket.
581 /* the content can be handled independently from the received MTU size */
588 been received from the socket a legacy CAN frame has been read into the
589 provided CAN FD structure. Note that the canfd_frame.flags data field is
590 not specified in the struct can_frame and therefore it is only valid in
596 data structure for CAN_RAW based applications. When the application is
597 executed on an older Linux kernel and switching the CAN_RAW_FD_FRAMES
599 or CAN FD frames and can process them the same way.
601 When sending to CAN devices make sure that the device is capable to handle
602 CAN FD frames by checking if the device maximum transfer unit is CANFD_MTU.
608 lead to multiple filters in the af_can.c filter processing. These filters
612 This socket option joines the given CAN filters in the way that only CAN
614 semantic for the applied filters is therefore changed to a logical AND.
616 This is useful especially when the filterset is a combination of filters
617 where the CAN_INV_FILTER flag is set in order to notch single CAN IDs or
618 CAN ID ranges from the incoming traffic.
622 When using recvmsg() call, the msg->msg_flags may contain following flags:
624 MSG_DONTROUTE: set when the received frame was created on the local host.
626 MSG_CONFIRM: set when the frame was sent via the socket it is received on.
627 This flag can be interpreted as a 'transmission confirmation' when the
628 CAN driver supports the echo of frames on driver level, see 3.2 and 6.2.
641 created and modified at runtime; both the message content and the two
644 A BCM socket is not intended for sending individual CAN frames using the
645 struct can_frame as known from the CAN_RAW socket. Instead a special BCM
647 to communicate with the broadcast manager and the available operations are
648 defined in the linux/can/bcm.h include. The BCM message consists of a
650 The broadcast manager sends responses to user space in the same form:
662 The aligned payload 'frames' uses the same basic CAN frame structure defined
663 at the beginning of section 4 and in the include/linux/can.h include. All
664 messages to the broadcast manager from user space have this structure.
687 distinguished by the unique can_id in each BCM message. However additional
689 When the broadcast manager socket is bound to 'any' CAN interface (=> the
690 interface index is set to zero) the configured receive filters apply to any
691 CAN interface unless the sendto() syscall is used to overrule the 'any' CAN
693 socket messages the originating CAN interface is provided in can_ifindex.
697 The opcode defines the operation for the broadcast manager to carry out,
698 or details the broadcast managers response to several events, including
716 'ival1'. Requires the TX_COUNTEVT flag to be set at TX_SETUP.
737 When sending a message to the broadcast manager the 'flags' element may
738 contain the following flag definitions which influence the behaviour:
740 SETTIMER: Set the values of ival1, ival2 and count
742 STARTTIMER: Start the timer with the actual values of ival1, ival2
743 and count. Starting the timer leads simultaneously to emit a CAN frame.
745 TX_COUNTEVT: Create the message TX_EXPIRED when count expires
747 TX_ANNOUNCE: A change of data by the process is emitted immediately.
749 TX_CP_CAN_ID: Copies the can_id from the message header to each
751 TX tasks the unique can_id from the message header may differ from the
752 can_id(s) stored for transmission in the subsequent struct can_frame(s).
756 RX_CHECK_DLC: A change of the DLC leads to an RX_CHANGED.
758 RX_NO_AUTOTIMER: Prevent automatically starting the timeout monitor.
761 RX_CHANGED message will be generated when the (cyclic) receive restarts.
763 TX_RESET_MULTI_IDX: Reset the index for the multiple frame transmission.
770 In this case the BCM sends a number of messages ('count') at an interval
773 When SET_TIMER and START_TIMER flag were set the timers are activated.
778 Up to 256 CAN frames can be transmitted in a sequence in the case of a cyclic
779 TX task configuration. The number of CAN frames is provided in the 'nframes'
780 element of the BCM message head. The defined number of CAN frames are added
781 as array to the TX_SETUP BCM configuration message.
795 With every transmission the index in the array of CAN frames is increased
801 When the SET_TIMER flag is set the timers are enabled:
804 the given time. When START_TIMER is set at RX_SETUP the timeout detection
807 ival2: Throttle the received message rate down to the value of ival2. This
808 is useful to reduce messages for the application when the signal inside the
809 CAN frame is stateless as state changes within the ival2 periode may get
816 data bytes of the first CAN frame contain the mask of relevant bits that
817 have to match in the subsequent CAN frames with the received CAN frame.
818 If one of the subsequent CAN frames is matching the bits in that frame data
819 mark the relevant content to be compared with the previous received content.
821 filters) can be added as array to the TX_SETUP BCM configuration message.
850 The SocketCAN core module implements the protocol family
851 PF_CAN. CAN protocol modules are loaded by the core module at
857 - stats_timer: To calculate the SocketCAN core statistics
860 disabled by using stattimer=0 on the module commandline.
866 As described in chapter 3.1 the SocketCAN core uses several filter
868 receive lists, their filters and the count of filter matches can be
869 checked in the appropriate receive list. All entries contain the
895 version - prints the SocketCAN core version and the ABI version
899 To implement a new protocol in the protocol family PF_CAN a new
901 The prototypes and definitions to use the SocketCAN core can be
903 In addition to functions that register the CAN protocol and the
911 For details see the kerneldoc documentation in net/can/af_can.c or
912 the source code of net/can/raw.c or net/can/bcm.c .
921 - TX: Put the CAN frame from the socket buffer to the CAN controller.
922 - RX: Put the CAN frame from the CAN controller to the socket buffer.
929 dev->type = ARPHRD_CAN; /* the netdevice hardware type */
934 or alternative, when the controller supports CAN with flexible data rate:
937 The struct can_frame or struct canfd_frame is the payload of each socket
938 buffer (skbuff) in the protocol family PF_CAN.
942 As described in chapter 3.2 the CAN network device driver should
943 support a local loopback functionality similar to the local echo
944 e.g. of tty devices. In this case the driver flag IFF_ECHO has to be
945 set to prevent the PF_CAN core from locally echoing sent frames
952 To reduce the interrupt load on deep embedded systems some CAN
953 controllers support the filtering of CAN IDs or ranges of CAN IDs.
956 networking approach. The use of the very controller specific
958 filter on driver level would affect all users in the multi-user
959 system. The high efficient filter sets inside the PF_CAN core allow
961 Therefore the use of hardware filters goes to the category 'handmade
968 Similar to the network loopback devices, vcan offers a virtual local
972 - the CAN bus this CAN ID is transmitted on (e.g. can0)
976 The virtual CAN interfaces allow the transmission and reception of CAN
979 When compiled as a module the virtual CAN driver module is called vcan.ko
981 Since Linux Kernel version 2.6.24 the vcan driver supports the Kernel
983 removal of vcan network devices can be managed with the ip(8) tool:
998 configure the CAN device, like setting the bit-timing parameters, via
999 the netlink interface using the program "ip" from the "IPROUTE2"
1001 Furthermore, the interface uses a common data structure and exports a
1003 should use. Please have a look to the SJA1000 or MSCAN driver to
1004 understand how to use them. The name of the module is can-dev.ko.
1010 "include/linux/can/netlink.h". CAN link support for the program "ip"
1011 of the IPROUTE2 utility suite is available and it can be used as shown
1055 More info to the above output:
1058 Shows the list of selected CAN controller modes: LOOPBACK,
1062 The current state of the CAN controller: "ERROR-ACTIVE",
1067 restart of the CAN controller will be triggered automatically
1068 in case of a bus-off condition after the specified delay time
1072 Shows the real bit-rate in bits/sec and the sample-point in the
1073 range 0.000..0.999. If the calculation of bit-timing parameters
1074 is enabled in the kernel (CONFIG_CAN_CALC_BITTIMING=y), the
1075 bit-timing can be defined by setting the "bitrate" argument.
1076 Optionally the "sample-point" can be specified. By default it's
1080 Shows the time quanta in ns, propagation segment, phase buffer
1081 segment 1 and 2 and the synchronisation jump width in units of
1082 tq. They allow to define the CAN bit-timing in a hardware
1083 independent format as proposed by the Bosch CAN 2.0 spec (see
1088 Shows the bit-timing constants of the CAN controller, here the
1089 "sja1000". The minimum and maximum values of the time segment 1
1090 and 2, the synchronisation jump width in units of tq, the
1091 bitrate pre-scaler and the CAN system clock frequency in Hz.
1096 Shows the number of restarts, bus and arbitration lost errors,
1097 and the state changes to the error-warning, error-passive and
1098 bus-off state. RX overrun errors are listed in the "overrun"
1099 field of the standard network statistics.
1101 6.5.2 Setting the CAN bit-timing
1104 independent format as proposed in the Bosch CAN 2.0 specification
1105 specifying the arguments "tq", "prop_seg", "phase_seg1", "phase_seg2"
1111 If the kernel option CONFIG_CAN_CALC_BITTIMING is enabled, CIA
1112 recommended CAN bit-timing parameters will be calculated if the bit-
1113 rate is specified with the argument "bitrate":
1117 Note that this works fine for the most common CAN controllers with
1120 space and allows user-space tools to solely determine and set the
1122 constants can be used for that purpose. They are listed by the
1129 6.5.3 Starting and stopping the CAN network device
1131 A CAN network device is started or stopped as usual with the command
1138 A device may enter the "bus-off" state if too many errors occurred on
1139 the CAN bus. Then no more messages are received or sent. An automatic
1140 bus-off recovery can be enabled by setting the "restart-ms" to a
1145 Alternatively, the application may realize the "bus-off" condition
1147 appropriate with the command:
1156 CAN FD capable CAN controllers support two different bitrates for the
1157 arbitration phase and the payload phase of the CAN FD frame. Therefore a
1158 second bit timing has to be specified in order to enable the CAN FD bitrate.
1162 canfd_frame.len for userspace applications and inside the Linux network
1163 layer is a plain value from 0 .. 64 instead of the CAN 'data length code'.
1164 The data length code was a 1:1 mapping to the payload length in the legacy
1165 CAN frames anyway. The payload length to the bus-relevant DLC mapping is
1166 only performed inside the CAN drivers, preferably with the helper
1169 The CAN netdevice driver capabilities can be distinguished by the network
1178 FIXME: Add details about the CAN FD controller configuration when available.
1182 Please check the "Kconfig" file in "drivers/net/can" to get an actual
1183 list of the support CAN hardware. On the SocketCAN project website
1191 are referenced in the MAINTAINERS file in the Linux source tree.