Commit graph

71 commits

Author SHA1 Message Date
Henrik Brix Andersen 7390740c29 canbus: isotp: use proper CAN ID masks
Use CAN ID masks matching the flags used in the CAN RX filter.

Signed-off-by: Henrik Brix Andersen <henrik@brixandersen.dk>
2024-04-24 09:53:06 +02:00
Henrik Brix Andersen 3436c93387 drivers: can: remove run-time RTR filtering, add build-time RTR filter
A growing number of CAN controllers do not have support for individual RX
hardware filters based on the Remote Transmission Request (RTR) bit. This
leads to various work-arounds on the driver level mixing hardware and
software filtering.

As the use of RTR frames is discouraged by CAN in Automation (CiA) - and
not even supported by newer standards, e.g. CAN FD - this often leads to
unnecessary overhead, added complexity, and worst-case to non-portable
behavior between various CAN controller drivers.

Instead, move to a simpler approach where the ability to accept/reject RTR
frames is globally configured via Kconfig. By default, all incoming RTR
frames are rejected at the driver level, a setting which can be supported
in hardware by most in-tree CAN controllers drivers.

Legacy applications or protocol implementations, where RTR reception is
required, can now select CONFIG_CAN_ACCEPT_RTR to accept incoming RTR
frames matching added CAN filters. These applications or protocols will
need to distinguish between RTR and data frames in their respective CAN RX
frame handling routines.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2024-01-21 11:00:31 +01:00
Henrik Brix Andersen 75117a0deb drivers: can: remove CAN_FILTER_FDF flag
Remove the CAN_FILTER_FDF flag for filtering on classic CAN/CAN FD frames
as it is not supported natively by any known CAN controller.

Applications can still filter on classic CAN/CAN FD frames in their receive
callback functions as needed.

Fixes: #64554

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2024-01-19 09:55:43 +01:00
Henrik Brix Andersen 5d5249d85b drivers: can: unify spelling of CAN Flexible Data-rate abbreviation
Unify spelling of CAN Flexible Data-rate abbreviation to "CAN FD" instead
of "CAN-FD". The former aligns with the CAN in Automation (CiA)
recommendation.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2023-11-01 11:17:17 +00:00
Martin Jäger 6b0135b151 canbus: isotp: rename ctx function parameters to rctx/sctx
This makes clear if the context is a recv_ctx or a send_ctx and
improves code readability.

Signed-off-by: Martin Jäger <martin@libre.solar>
2023-09-21 11:54:11 +02:00
Martin Jäger b82a4478ad canbus: isotp: add _TIMEOUT_MS suffix to defines for timeouts
The defines like ISOTP_A were obfuscating that this is actually a
timeout value.

Signed-off-by: Martin Jäger <martin@libre.solar>
2023-09-21 11:54:11 +02:00
Martin Jäger 5fdc43270f canbus: isotp: improve internal function naming
Use prefixes to make naming more clear and improve code readability.

Signed-off-by: Martin Jäger <martin@libre.solar>
2023-09-21 11:54:11 +02:00
Grant Ramsay a4383c2fd1 canbus: isotp: add CAN-FD support
ISO-TP CAN-FD support can be enabled at runtime.

Signed-off-by: Grant Ramsay <gramsay@enphaseenergy.com>
Signed-off-by: Martin Jäger <martin@libre.solar>
2023-09-14 08:34:09 +02:00
Martin Jäger f13791d5ba canbus: isotp: use flags for configuration in isotp_msg_id
The previous design with dedicated bits in the structure required a
user to explicitly set each bit.

Using one flags variable allows to extend the features more easily
in the future and avoids breaking existing code.

This change is particularly useful for the FDF and BRS flags required
for CAN-FD support.

See also previous similar change for the CAN driver in
f8a88cdb27

Signed-off-by: Martin Jäger <martin@libre.solar>
2023-09-07 09:43:56 +02:00
Carles Cufi 8c748fd005 kernel: Modify the signature of k_mem_slab_free()
Modify the signature of the k_mem_slab_free() function with a new one,
replacing the old void **mem with void *mem as a parameter.

The following function:
void k_mem_slab_free(struct k_mem_slab *slab, void **mem);

has the wrong signature. mem is only used as a regular pointer, so there
is no need to use a double-pointer. The correct signature should be:
void k_mem_slab_free(struct k_mem_slab *slab, void *mem);

The issue with the current signature, although functional, is that it is
extremely confusing. I myself, a veteran Zephyr developer, was confused
by this parameter when looking at it recently.

All in-tree uses of the function have been adapted.

Fixes #61888.

Signed-off-by: Carles Cufi <carles.cufi@nordicsemi.no>
2023-09-03 18:20:59 -04:00
Martin Jäger 7f7201f79b canbus: isotp: replace internal z_timeout with k_timer
The use of private APIs z_add_timeout() and friends is not recommended.

With this commit, usual kernel timer APIs are used.

Fixes #61738

Signed-off-by: Martin Jäger <martin@libre.solar>
2023-08-29 16:14:35 +02:00
Henrik Brix Andersen 1b3d1e01de canbus: isotp: convert SF length check from ASSERT to runtime check
Convert the ISO-TP SF length check in send_sf() from __ASSERT() to a
runtime check.

Fixes: #61501

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2023-08-15 10:08:23 -07:00
Grant Ramsay e98fa4e590 canbus: isotp: Enable TX padding by default if RX padding is required
It would be strange to enforce padding for other bus participants but not
use it yourself. This default suggests the likely proper usage, although
it is not a hard dependency

Signed-off-by: Grant Ramsay <gramsay@enphaseenergy.com>
2023-07-28 09:08:20 +00:00
Grant Ramsay 42dcc4e57f canbus: isotp: Fix ISO-TP padding config usage
ISOTP_ENABLE_TX_PADDING makes the device transmit frames with TX padding.
ISOTP_REQUIRE_RX_PADDING ensures other devices on the bus use TX padding,
by rejecting non-padded RX frames

Signed-off-by: Grant Ramsay <gramsay@enphaseenergy.com>
2023-07-28 09:08:20 +00:00
Grant Ramsay e439b5e56d canbus: isotp: Fix context buffer memory leaks
Ensure context buffers are free'd when errors occur

Signed-off-by: Grant Ramsay <gramsay@enphaseenergy.com>
2023-07-25 09:15:13 +02:00
Jiapeng Li 2568ab661d canbus: isotp: fix single frame state machine stuck issue
it is too late to set ctx->state to ISOTP_TX_WAIT_FIN after send_sf
because send_state_machine could be called just between `send_sf` and
`ctx->state = ISOTP_TX_WAIT_FIN;`  in extremely case. like below:
```c
	ret = send_sf(ctx);
	-> send_state_machine (irq handler)
	ctx->state = ISOTP_TX_WAIT_FIN;
```
it will cause isotp_send never return.

Signed-off-by: Jiapeng Li <mail@jiapeng.me>
2023-04-12 17:45:04 +02:00
William MARTIN e121ea28d9 canbus: isotp: Allow to override ISOTP_FIXED_ADDR_* constants
This patch allow to use isotp use_fixed_addr feature
with CAN identifier which not match SAE J1939 format.


Signed-off-by: William MARTIN <william.martin@power-lan.com>
2023-04-06 19:43:02 +02:00
Henrik Brix Andersen f8a88cdb27 drivers: can: use flags fields for can_frame and can_filter structs
The can_frame and can_filter structs support a number of different flags
(standard/extended CAN ID type, Remote Transmission Request, CAN-FD format,
Bit Rate Switch, ...). Each of these flags is represented as a discrete bit
in the given structure.

This design pattern requires every user of these structs to initialize all
of these flags to either 0 or 1, which does not scale well for future flag
additions.

Some of these flags have associated enumerations to be used for assignment,
some do not. CAN drivers and protocols tend to rely on the logical value of
the flag instead of using the enumeration, leading to a very fragile
API. The enumerations are used inconsistently between the can_frame and
can_filter structures, which further complicates the API.

Instead, convert these flags to bitfields with separate flag definitions
for the can_frame and can_filter structures. This API allows for future
extensions without having to revisit existing users of the two
structures. Furthermore, this allows driver to easily check for unsupported
flags in the respective API calls.

As this change leads to the "id_mask" field of the can_filter to be the
only mask present in that structure, rename it to "mask" for simplicity.

Fixes: #50776

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-10-25 16:32:10 +02:00
Henrik Brix Andersen a9c7c58345 canbus: isotp: avoid reusing CAN controller driver API definitions
Avoid reusing the CAN_EXTENDED_IDENTIFIER and CAN_STANDARD_IDENTIFIER
definitions from the CAN controller driver API for ISO-TP structure members
as this is a fragile design.

The ISO-TP layer must be responsible for its own definitions where
needed. Replace the "id_type" ISO-TP struct member with a well-known
abbreviated "ide" bit (Identifier Extension Bit) struct member.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-10-25 16:32:10 +02:00
Henrik Brix Andersen c5352e99a1 canbus: isotp: fix size_t format specifier
Fix the format specifier for logging size_t values.

Signed-off-by: Henrik Brix Andersen <henrik@brixandersen.dk>
2022-10-22 14:36:02 +09:00
Henrik Brix Andersen 13c75417ba drivers: can: remove z prefix from public CAN API types
Remove the "z" prefix from the public CAN controller API types as this
makes them appear as internal APIs.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-08-18 10:19:29 +02:00
Gerard Marull-Paretas 5113c1418d subsystems: migrate includes to <zephyr/...>
In order to bring consistency in-tree, migrate all subsystems code to
the new prefix <zephyr/...>. Note that the conversion has been scripted,
refer to zephyrproject-rtos#45388 for more details.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-05-09 12:07:35 +02:00
Bartosz Bilas 9e3d9784ff canbus: isotp: remove superfluous default n for boolean
bool symbols implicitly default to n so
there is no need to redundant those values.

Signed-off-by: Bartosz Bilas <bartosz.bilas@hotmail.com>
2022-04-19 11:49:27 +02:00
Henrik Brix Andersen 67ba9900f0 drivers: can: add struct device argument to callback functions
Include a pointer to the CAN controller device for the CAN
transmit, receive, and state change callback functions.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-03-19 14:31:22 -04:00
Nazar Kazakov f483b1bc4c everywhere: fix typos
Fix a lot of typos

Signed-off-by: Nazar Kazakov <nazar.kazakov.work@gmail.com>
2022-03-18 13:24:08 -04:00
Gerard Marull-Paretas 95fb0ded6b kconfig: remove Enable from boolean prompts
According to Kconfig guidelines, boolean prompts must not start with
"Enable...". The following command has been used to automate the changes
in this patch:

sed -i "s/bool \"[Ee]nables\? \(\w\)/bool \"\U\1/g" **/Kconfig*

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-03-09 15:35:54 +01:00
Henrik Brix Andersen 1f57e73730 canbus: isotp: ensure consecutive frames are sent in FIFO order
Ensure that ISO-TP Consecutive Frames (CF) are sent in
FIFO/chronological order.

In order to ensure this, we can only have one CF queued in the CAN
controller TX mailboxes at a time, since transmit order for mailboxes
with the same CAN-ID (priority) is hardware specific.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-02-21 19:37:35 -05:00
Henrik Brix Andersen 8af4bb722d drivers: can: rename API functions for better consistency
Rename a few CAN API functions for clarity and consistency with other
Zephyr RTOS APIs.

CAN_DEFINE_MSGQ() becomes CAN_MSGQ_DEFINE() to match K_MSGQ_DEFINE().

can_attach_isr() becomes can_add_rx_filter() since a filter callback
function is not an interrupt service routine (although it is called in
isr context). The word "attach" is replaced with "add" since filters are
added, not attached. This matches the terminology used is other Zephyr
APIs better.

can_detach() becomes can_remove_rx_filter() to pair with
can_add_rx_filter().

can_attach_msgq() becomes can_add_rx_filter_msgq() and documentation is
updated to mention its relationship with can_add_rx_filter().

can_register_state_change_isr() becomes can_set_state_change_callback()
since a state change callback function is not an interrupt service
routine (although it is called in isr context). The word "register" is
replaced with "set" since only one state change callback can be in
place.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-01-10 10:44:37 +01:00
Henrik Brix Andersen b1b77c1774 drivers: can: change can_tx_callback_t function signature
Change the can_tx_callback_t function signature to use an "int" (not an
uint32_t) for representing transmission errors.

The "error" callback function parameter is functionally equivalent to
the return value from can_send() and thus needs to use the same data
type and needs to be able to hold negative errno values.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-01-01 07:40:29 -05:00
Henrik Brix Andersen f499559434 drivers: can: deprecate the use of CAN-specific error return values
Deprecate the use of CAN-specific error return values and replace them
with standard errno values.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2021-12-09 12:37:40 -05:00
Jordan Yates 8236b600f7 net: buf: POOL_VAR_DEFINE explicit user data
Update the macro prototype to explicitly require the length of the
desired user data. Update all in-tree usage of this macro.

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
2021-11-24 16:04:50 +02:00
Martin Jäger 27fc74feff canbus: isotp: fix net_buf usage in recv function
isotp_recv and the called pull_frags functions were violating the
net_buf API by interacting directly with net_buf fragments pulled from
a k_fifo.

This commit reworks the isotp_recv function. The currently processed
net_buf is stored in an additional context variable so that reading from
it can be continued in the next call to isotp_recv if not all fragments
could be fit into the provided uint8_t *data buffer.

Fixes #40070

Signed-off-by: Martin Jäger <martin@libre.solar>
2021-11-09 18:12:08 +02:00
Torsten Rasmussen 587285e4de kconfig: canbus: experimental settings now uses select EXPERIMENTAL
With the introduction of `EXPERIMENTAL` and `WARN_EXPERIMENTAL` in
Zephyr all subsys/canbus, subsys/net/l2/canbus, and drivers/can settings
having `[EXPERIMENTAL]` in their prompt has has been updated to include
`select EXPERIMENTAL` so that developers can enable warnings when
experimental features are enabled.

Signed-off-by: Torsten Rasmussen <Torsten.Rasmussen@nordicsemi.no>
2021-10-22 13:59:21 +02:00
Henrik Brix Andersen d679037643 modules: canopennode: move glue code to modules directory
Move the Zephyr-specific interface and support code for CANopenNode into
the modules directory. Consolidate the CMakeLists.txt files into one.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2021-07-01 13:32:18 -04:00
Henrik Brix Andersen 374629af90 canbus: isotp: remove unused work queue
Remove unused local work queue.

Fixes #34862

Signed-off-by: Henrik Brix Andersen <henrik@brixandersen.dk>
2021-05-22 08:14:35 -05:00
Martin Jäger 08b067f855 canbus: isotp: use new k_work API
Replace soon-to-be-deprecated call to start queue.

Signed-off-by: Martin Jäger <martin@libre.solar>
2021-05-05 13:24:29 -05:00
Henrik Brix Andersen db22c42ba7 canbus: canopen: conversion of k_work API
Replace all existing deprecated API with the recommended alternative.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2021-05-04 10:12:47 -05:00
Martin Jäger 962e361e54 canbus: isotp: prevent race-condition during debugging
During debugging I got "Got unexpected PDU" errors because a new CF was
received before the state was set to ISOTP_TX_WAIT_FC in the send state
machine. Setting the state before printing the debug information fixes
the issue.

Signed-off-by: Martin Jäger <martin@libre.solar>
2021-04-06 16:44:34 -04:00
Martin Jäger 0c8cb0586a canbus: isotp: add fixed addressing feature
Fixed addressing as specified in ISO 15765-2 encodes the source and
target address of a device or function inside the CAN ID according to
SAE J1939.

In order to allow to receive incoming requests from different nodes,
the CAN filter mask has to be set such that the source address is
ignored in the receive context. In addition to that, flow control
frames have to be sent back to the actual source of the request, which
requires adjustments to the TX CAN ID.

This commit implements above features and thus allows ISO-TP to be
used in a network based on SAE J1939 or NMEA-2000.

Signed-off-by: Martin Jäger <martin@libre.solar>
2021-04-06 16:44:34 -04:00
Martin Jäger f5496147b5 canbus: isotp: allow frames with padding
ISO 15765-2 specifies that the CAN frame length for single frames and
the last consecutive frame may or may not be optimized to the actual
length of the PDU payload. The ISO-TP implementation should only
consider the information from the N_PCI section.

The previous implementation did not allow padding. With this commit,
padding is allowed by default and can even be required to be more
compliant to AUTOSAR standards.

Signed-off-by: Martin Jäger <martin@libre.solar>
2021-04-06 16:44:34 -04:00
Martin Jäger ade6cc4a71 canbus: isotp: fix asserts to check rx and tx addr
Previous implementation contained a typo so that only the rx address
was checked.

Signed-off-by: Martin Jäger <martin@libre.solar>
2021-04-06 16:44:34 -04:00
Flavio Ceolin 9fd4ea91b7 coccinelle: Remove extra semicolon
coccicheck --mode=patch --cocci=semicolon.cocci

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2021-03-25 11:35:30 -05:00
Klaus H. Sorensen 8216e320b6 canbus: canopen: program: read flash in 32 byte chunks
Add routine to calculate crc of flash region. Read 32 bytes at a time
from flash instead of 4 bytes, for a significant speed up of the flash
image crc calculation.

Signed-off-by: Klaus H. Sorensen <khso@vestas.com>
2021-01-20 14:18:03 -06:00
Klaus H. Sorensen cb24e3c22e canbus: canopen: program: unlock can od during flash access
The callback function canopen_odf_1f56 is called with the can od lock
held. Release the lock while performing time consuming flash reading and
crc calculations, and reacquire the lock before returning from the
function.

Signed-off-by: Klaus H. Sorensen <khso@vestas.com>
2021-01-20 14:18:03 -06:00
Alexander Wachter 05275ecf6e drivers: can: rework zcan_frame and zcan_filter
Reordering of the struct elements to match the Linux format.
The __packed() is not necessary anymore.
std_id and ext_id is merged to id in the frame and filter.
Additionally, the frames are ready for CAN-FD.

Signed-off-by: Alexander Wachter <alexander@wachter.cloud>
2020-12-17 11:07:53 +01:00
Alexander Wachter 8b6c1bd4b7 drivers: can: Rework can_configure API
The previous API can't change the sampling-point and only allowed
bitrates that fit the time segments.
The new API allows for shifting the sampling-point and adjusts the
number of time quantum in a bit to all more possible bitrates.
The functions to calculate the timings are moved to the can_common file.
They can be used for all drivers.

Signed-off-by: Alexander Wachter <alexander@wachter.cloud>
2020-12-17 11:07:53 +01:00
Tomasz Bursztyka 8d0a3f3b5c subsys: canopen: Add a wrapper to avoid passing device to CO_init
Add a dedicated structure instead.

Fixes #27399

Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2020-09-02 13:48:13 +02:00
Tomasz Bursztyka e18fcbba5a device: Const-ify all device driver instance pointers
Now that device_api attribute is unmodified at runtime, as well as all
the other attributes, it is possible to switch all device driver
instance to be constant.

A coccinelle rule is used for this:

@r_const_dev_1
  disable optional_qualifier
@
@@
-struct device *
+const struct device *

@r_const_dev_2
 disable optional_qualifier
@
@@
-struct device * const
+const struct device *

Fixes #27399

Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2020-09-02 13:48:13 +02:00
Flavio Ceolin 0aaae4a039 guideline: Make explicit fallthrough cases
-Wimplicit-fallthrough=2 requires a fallthrough comment or a compiler
to tells gcc that this happens intentionally.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2020-08-24 20:28:47 -04:00
Andrew Boie 8b531aa996 canbus: use kernel stacks
These threads don't run in user mode, save some memory
if userspace is enabled.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2020-08-02 10:50:12 -04:00