2022-08-05 12:44:44 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022 Martin Jäger <martin@libre.solar>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define DT_DRV_COMPAT zephyr_native_posix_linux_can
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <zephyr/drivers/can.h>
|
|
|
|
#include <zephyr/kernel.h>
|
|
|
|
#include <zephyr/logging/log.h>
|
|
|
|
#include <zephyr/net/net_pkt.h>
|
|
|
|
#include <zephyr/net/socketcan.h>
|
|
|
|
#include <zephyr/net/socketcan_utils.h>
|
|
|
|
|
|
|
|
#include "can_native_posix_linux_socketcan.h"
|
|
|
|
|
|
|
|
LOG_MODULE_REGISTER(can_npl, CONFIG_CAN_LOG_LEVEL);
|
|
|
|
|
|
|
|
struct can_filter_context {
|
|
|
|
can_rx_callback_t rx_cb;
|
|
|
|
void *cb_arg;
|
|
|
|
struct can_filter filter;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct can_npl_data {
|
|
|
|
struct can_filter_context filters[CONFIG_CAN_MAX_FILTER];
|
|
|
|
struct k_mutex filter_mutex;
|
|
|
|
struct k_sem tx_idle;
|
|
|
|
can_tx_callback_t tx_callback;
|
|
|
|
void *tx_user_data;
|
|
|
|
bool loopback;
|
|
|
|
bool mode_fd;
|
|
|
|
int dev_fd; /* Linux socket file descriptor */
|
|
|
|
struct k_thread rx_thread;
|
2022-08-31 14:28:42 +02:00
|
|
|
bool started;
|
2022-08-05 12:44:44 +02:00
|
|
|
|
|
|
|
K_KERNEL_STACK_MEMBER(rx_thread_stack, CONFIG_ARCH_POSIX_RECOMMENDED_STACK_SIZE);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct can_npl_config {
|
|
|
|
const char *if_name;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void dispatch_frame(const struct device *dev, struct can_frame *frame)
|
|
|
|
{
|
|
|
|
struct can_npl_data *data = dev->data;
|
|
|
|
can_rx_callback_t callback;
|
|
|
|
struct can_frame tmp_frame;
|
|
|
|
|
|
|
|
k_mutex_lock(&data->filter_mutex, K_FOREVER);
|
|
|
|
|
|
|
|
for (int filter_id = 0; filter_id < ARRAY_SIZE(data->filters); filter_id++) {
|
|
|
|
if (data->filters[filter_id].rx_cb == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-02-07 10:26:19 +01:00
|
|
|
if (!can_frame_matches_filter(frame, &data->filters[filter_id].filter)) {
|
2022-08-05 12:44:44 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make a temporary copy in case the user modifies the message */
|
|
|
|
tmp_frame = *frame;
|
|
|
|
|
|
|
|
callback = data->filters[filter_id].rx_cb;
|
|
|
|
callback(dev, &tmp_frame, data->filters[filter_id].cb_arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
k_mutex_unlock(&data->filter_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rx_thread(void *arg1, void *arg2, void *arg3)
|
|
|
|
{
|
|
|
|
const struct device *dev = arg1;
|
|
|
|
struct can_npl_data *data = dev->data;
|
|
|
|
struct socketcan_frame sframe;
|
|
|
|
struct can_frame frame;
|
|
|
|
bool msg_confirm;
|
|
|
|
int count;
|
|
|
|
|
|
|
|
ARG_UNUSED(arg2);
|
|
|
|
ARG_UNUSED(arg3);
|
|
|
|
|
|
|
|
LOG_DBG("Starting Linux SocketCAN RX thread");
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
while (linux_socketcan_poll_data(data->dev_fd) == 0) {
|
|
|
|
count = linux_socketcan_read_data(data->dev_fd, (void *)(&sframe),
|
|
|
|
sizeof(sframe), &msg_confirm);
|
|
|
|
if (msg_confirm) {
|
2022-09-20 17:49:17 +02:00
|
|
|
data->tx_callback(dev, 0, data->tx_user_data);
|
2022-08-05 12:44:44 +02:00
|
|
|
k_sem_give(&data->tx_idle);
|
|
|
|
|
2022-09-15 12:57:03 +02:00
|
|
|
if (!data->loopback) {
|
2022-08-05 12:44:44 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2022-09-15 12:57:03 +02:00
|
|
|
if ((count <= 0) || !data->started) {
|
2022-08-05 12:44:44 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
socketcan_to_can_frame(&sframe, &frame);
|
|
|
|
|
|
|
|
LOG_DBG("Received %d bytes. Id: 0x%x, ID type: %s %s",
|
|
|
|
frame.dlc, frame.id,
|
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-09-30 14:44:16 +02:00
|
|
|
(frame.flags & CAN_FRAME_IDE) != 0 ? "extended" : "standard",
|
|
|
|
(frame.flags & CAN_FRAME_RTR) != 0 ? ", RTR frame" : "");
|
2022-08-05 12:44:44 +02:00
|
|
|
|
|
|
|
dispatch_frame(dev, &frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* short sleep required to avoid blocking the whole native_posix process */
|
|
|
|
k_sleep(K_MSEC(1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int can_npl_send(const struct device *dev, const struct can_frame *frame,
|
|
|
|
k_timeout_t timeout, can_tx_callback_t callback, void *user_data)
|
|
|
|
{
|
|
|
|
struct can_npl_data *data = dev->data;
|
|
|
|
struct socketcan_frame sframe;
|
|
|
|
uint8_t max_dlc = CAN_MAX_DLC;
|
|
|
|
size_t mtu = CAN_MTU;
|
|
|
|
int ret = -EIO;
|
|
|
|
|
|
|
|
LOG_DBG("Sending %d bytes on %s. Id: 0x%x, ID type: %s %s",
|
|
|
|
frame->dlc, dev->name, frame->id,
|
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-09-30 14:44:16 +02:00
|
|
|
(frame->flags & CAN_FRAME_IDE) != 0 ? "extended" : "standard",
|
|
|
|
(frame->flags & CAN_FRAME_RTR) != 0 ? ", RTR frame" : "");
|
2022-08-05 12:44:44 +02:00
|
|
|
|
2022-09-20 17:49:17 +02:00
|
|
|
__ASSERT_NO_MSG(callback != NULL);
|
|
|
|
|
2022-08-05 12:44:44 +02:00
|
|
|
#ifdef CONFIG_CAN_FD_MODE
|
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-09-30 14:44:16 +02:00
|
|
|
if ((frame->flags & ~(CAN_FRAME_IDE | CAN_FRAME_RTR |
|
|
|
|
CAN_FRAME_FDF | CAN_FRAME_BRS)) != 0) {
|
|
|
|
LOG_ERR("unsupported CAN frame flags 0x%02x", frame->flags);
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((frame->flags & CAN_FRAME_FDF) != 0) {
|
|
|
|
if (!data->mode_fd) {
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
2022-08-05 12:44:44 +02:00
|
|
|
max_dlc = CANFD_MAX_DLC;
|
|
|
|
mtu = CANFD_MTU;
|
|
|
|
}
|
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-09-30 14:44:16 +02:00
|
|
|
#else /* CONFIG_CAN_FD_MODE */
|
|
|
|
if ((frame->flags & ~(CAN_FRAME_IDE | CAN_FRAME_RTR)) != 0) {
|
|
|
|
LOG_ERR("unsupported CAN frame flags 0x%02x", frame->flags);
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
#endif /* !CONFIG_CAN_FD_MODE */
|
2022-08-05 12:44:44 +02:00
|
|
|
|
|
|
|
if (frame->dlc > max_dlc) {
|
|
|
|
LOG_ERR("DLC of %d exceeds maximum (%d)", frame->dlc, max_dlc);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data->dev_fd <= 0) {
|
|
|
|
LOG_ERR("No file descriptor: %d", data->dev_fd);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2022-08-31 14:28:42 +02:00
|
|
|
if (!data->started) {
|
|
|
|
return -ENETDOWN;
|
|
|
|
}
|
|
|
|
|
2022-08-05 12:44:44 +02:00
|
|
|
socketcan_from_can_frame(frame, &sframe);
|
|
|
|
|
|
|
|
if (k_sem_take(&data->tx_idle, timeout) != 0) {
|
|
|
|
return -EAGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->tx_callback = callback;
|
|
|
|
data->tx_user_data = user_data;
|
|
|
|
|
|
|
|
ret = linux_socketcan_write_data(data->dev_fd, &sframe, mtu);
|
|
|
|
if (ret < 0) {
|
|
|
|
LOG_ERR("Cannot send CAN data len %d (%d)", sframe.len, -errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int can_npl_add_rx_filter(const struct device *dev, can_rx_callback_t cb,
|
|
|
|
void *cb_arg, const struct can_filter *filter)
|
|
|
|
{
|
|
|
|
struct can_npl_data *data = dev->data;
|
|
|
|
struct can_filter_context *filter_ctx;
|
|
|
|
int filter_id = -ENOSPC;
|
|
|
|
|
|
|
|
LOG_DBG("Setting filter ID: 0x%x, mask: 0x%x", filter->id,
|
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-09-30 14:44:16 +02:00
|
|
|
filter->mask);
|
|
|
|
|
2022-11-09 11:23:54 +07:00
|
|
|
#ifdef CONFIG_CAN_FD_MODE
|
|
|
|
if ((filter->flags & ~(CAN_FILTER_IDE | CAN_FILTER_DATA |
|
|
|
|
CAN_FILTER_RTR | CAN_FILTER_FDF)) != 0) {
|
|
|
|
#else
|
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-09-30 14:44:16 +02:00
|
|
|
if ((filter->flags & ~(CAN_FILTER_IDE | CAN_FILTER_DATA | CAN_FILTER_RTR)) != 0) {
|
2022-11-09 11:23:54 +07:00
|
|
|
#endif
|
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-09-30 14:44:16 +02:00
|
|
|
LOG_ERR("unsupported CAN filter flags 0x%02x", filter->flags);
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
2022-08-05 12:44:44 +02:00
|
|
|
|
|
|
|
k_mutex_lock(&data->filter_mutex, K_FOREVER);
|
|
|
|
|
|
|
|
for (int i = 0; i < ARRAY_SIZE(data->filters); i++) {
|
|
|
|
if (data->filters[i].rx_cb == NULL) {
|
|
|
|
filter_id = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filter_id < 0) {
|
|
|
|
LOG_ERR("No free filter left");
|
|
|
|
k_mutex_unlock(&data->filter_mutex);
|
|
|
|
return filter_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
filter_ctx = &data->filters[filter_id];
|
|
|
|
filter_ctx->rx_cb = cb;
|
|
|
|
filter_ctx->cb_arg = cb_arg;
|
|
|
|
filter_ctx->filter = *filter;
|
|
|
|
|
|
|
|
k_mutex_unlock(&data->filter_mutex);
|
|
|
|
|
|
|
|
LOG_DBG("Filter added. ID: %d", filter_id);
|
|
|
|
|
|
|
|
return filter_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void can_npl_remove_rx_filter(const struct device *dev, int filter_id)
|
|
|
|
{
|
|
|
|
struct can_npl_data *data = dev->data;
|
|
|
|
|
|
|
|
if (filter_id < 0 || filter_id >= ARRAY_SIZE(data->filters)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
k_mutex_lock(&data->filter_mutex, K_FOREVER);
|
|
|
|
data->filters[filter_id].rx_cb = NULL;
|
|
|
|
k_mutex_unlock(&data->filter_mutex);
|
|
|
|
|
|
|
|
LOG_DBG("Filter removed. ID: %d", filter_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int can_npl_get_capabilities(const struct device *dev, can_mode_t *cap)
|
|
|
|
{
|
|
|
|
ARG_UNUSED(dev);
|
|
|
|
|
|
|
|
*cap = CAN_MODE_NORMAL | CAN_MODE_LOOPBACK;
|
|
|
|
|
|
|
|
#if CONFIG_CAN_FD_MODE
|
|
|
|
*cap |= CAN_MODE_FD;
|
|
|
|
#endif /* CONFIG_CAN_FD_MODE */
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-08-31 14:28:42 +02:00
|
|
|
static int can_npl_start(const struct device *dev)
|
|
|
|
{
|
|
|
|
struct can_npl_data *data = dev->data;
|
|
|
|
|
|
|
|
if (data->started) {
|
|
|
|
return -EALREADY;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->started = true;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int can_npl_stop(const struct device *dev)
|
|
|
|
{
|
|
|
|
struct can_npl_data *data = dev->data;
|
|
|
|
|
|
|
|
if (!data->started) {
|
|
|
|
return -EALREADY;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->started = false;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-08-05 12:44:44 +02:00
|
|
|
static int can_npl_set_mode(const struct device *dev, can_mode_t mode)
|
|
|
|
{
|
|
|
|
struct can_npl_data *data = dev->data;
|
|
|
|
|
|
|
|
#ifdef CONFIG_CAN_FD_MODE
|
|
|
|
if ((mode & ~(CAN_MODE_LOOPBACK | CAN_MODE_FD)) != 0) {
|
|
|
|
LOG_ERR("unsupported mode: 0x%08x", mode);
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if ((mode & ~(CAN_MODE_LOOPBACK)) != 0) {
|
|
|
|
LOG_ERR("unsupported mode: 0x%08x", mode);
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_CAN_FD_MODE */
|
|
|
|
|
2022-08-31 14:28:42 +02:00
|
|
|
if (data->started) {
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
2022-08-05 12:44:44 +02:00
|
|
|
/* loopback is handled internally in rx_thread */
|
|
|
|
data->loopback = (mode & CAN_MODE_LOOPBACK) != 0;
|
|
|
|
|
|
|
|
data->mode_fd = (mode & CAN_MODE_FD) != 0;
|
|
|
|
linux_socketcan_set_mode_fd(data->dev_fd, data->mode_fd);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int can_npl_set_timing(const struct device *dev, const struct can_timing *timing)
|
|
|
|
{
|
2022-08-31 14:28:42 +02:00
|
|
|
struct can_npl_data *data = dev->data;
|
|
|
|
|
2022-08-05 12:44:44 +02:00
|
|
|
ARG_UNUSED(timing);
|
|
|
|
|
2022-08-31 14:28:42 +02:00
|
|
|
if (data->started) {
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
2022-08-05 12:44:44 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_CAN_FD_MODE
|
|
|
|
static int can_npl_set_timing_data(const struct device *dev, const struct can_timing *timing)
|
|
|
|
{
|
2022-08-31 14:28:42 +02:00
|
|
|
struct can_npl_data *data = dev->data;
|
|
|
|
|
2022-08-05 12:44:44 +02:00
|
|
|
ARG_UNUSED(timing);
|
|
|
|
|
2022-08-31 14:28:42 +02:00
|
|
|
if (data->started) {
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
2022-08-05 12:44:44 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_CAN_FD_MODE */
|
|
|
|
|
|
|
|
static int can_npl_get_state(const struct device *dev, enum can_state *state,
|
|
|
|
struct can_bus_err_cnt *err_cnt)
|
|
|
|
{
|
2022-08-31 14:28:42 +02:00
|
|
|
struct can_npl_data *data = dev->data;
|
2022-08-05 12:44:44 +02:00
|
|
|
|
|
|
|
if (state != NULL) {
|
2022-08-31 14:28:42 +02:00
|
|
|
if (!data->started) {
|
|
|
|
*state = CAN_STATE_STOPPED;
|
|
|
|
} else {
|
|
|
|
/* SocketCAN does not forward error frames by default */
|
|
|
|
*state = CAN_STATE_ERROR_ACTIVE;
|
|
|
|
}
|
2022-08-05 12:44:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (err_cnt) {
|
|
|
|
err_cnt->tx_err_cnt = 0;
|
|
|
|
err_cnt->rx_err_cnt = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef CONFIG_CAN_AUTO_BUS_OFF_RECOVERY
|
|
|
|
static int can_npl_recover(const struct device *dev, k_timeout_t timeout)
|
|
|
|
{
|
2022-08-31 14:28:42 +02:00
|
|
|
struct can_npl_data *data = dev->data;
|
|
|
|
|
2022-08-05 12:44:44 +02:00
|
|
|
ARG_UNUSED(timeout);
|
|
|
|
|
2022-08-31 14:28:42 +02:00
|
|
|
if (!data->started) {
|
|
|
|
return -ENETDOWN;
|
|
|
|
}
|
|
|
|
|
2022-08-05 12:44:44 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_CAN_AUTO_BUS_OFF_RECOVERY */
|
|
|
|
|
|
|
|
static void can_npl_set_state_change_callback(const struct device *dev,
|
|
|
|
can_state_change_callback_t cb,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
ARG_UNUSED(dev);
|
|
|
|
ARG_UNUSED(cb);
|
|
|
|
ARG_UNUSED(user_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int can_npl_get_core_clock(const struct device *dev, uint32_t *rate)
|
|
|
|
{
|
|
|
|
/* Return 16MHz as an realistic value for the testcases */
|
|
|
|
*rate = 16000000;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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-09-30 14:44:16 +02:00
|
|
|
static int can_npl_get_max_filters(const struct device *dev, bool ide)
|
2022-08-05 12:44:44 +02:00
|
|
|
{
|
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-09-30 14:44:16 +02:00
|
|
|
ARG_UNUSED(ide);
|
2022-08-05 12:44:44 +02:00
|
|
|
|
|
|
|
return CONFIG_CAN_MAX_FILTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct can_driver_api can_npl_driver_api = {
|
2022-08-31 14:28:42 +02:00
|
|
|
.start = can_npl_start,
|
|
|
|
.stop = can_npl_stop,
|
2022-08-05 12:44:44 +02:00
|
|
|
.get_capabilities = can_npl_get_capabilities,
|
|
|
|
.set_mode = can_npl_set_mode,
|
|
|
|
.set_timing = can_npl_set_timing,
|
|
|
|
.send = can_npl_send,
|
|
|
|
.add_rx_filter = can_npl_add_rx_filter,
|
|
|
|
.remove_rx_filter = can_npl_remove_rx_filter,
|
|
|
|
.get_state = can_npl_get_state,
|
|
|
|
#ifndef CONFIG_CAN_AUTO_BUS_OFF_RECOVERY
|
|
|
|
.recover = can_npl_recover,
|
|
|
|
#endif
|
|
|
|
.set_state_change_callback = can_npl_set_state_change_callback,
|
|
|
|
.get_core_clock = can_npl_get_core_clock,
|
|
|
|
.get_max_filters = can_npl_get_max_filters,
|
|
|
|
.timing_min = {
|
|
|
|
.sjw = 0x1,
|
|
|
|
.prop_seg = 0x01,
|
|
|
|
.phase_seg1 = 0x01,
|
|
|
|
.phase_seg2 = 0x01,
|
|
|
|
.prescaler = 0x01
|
|
|
|
},
|
|
|
|
.timing_max = {
|
|
|
|
.sjw = 0x0F,
|
|
|
|
.prop_seg = 0x0F,
|
|
|
|
.phase_seg1 = 0x0F,
|
|
|
|
.phase_seg2 = 0x0F,
|
|
|
|
.prescaler = 0xFFFF
|
|
|
|
},
|
|
|
|
#ifdef CONFIG_CAN_FD_MODE
|
|
|
|
.set_timing_data = can_npl_set_timing_data,
|
|
|
|
.timing_data_min = {
|
|
|
|
.sjw = 0x1,
|
|
|
|
.prop_seg = 0x01,
|
|
|
|
.phase_seg1 = 0x01,
|
|
|
|
.phase_seg2 = 0x01,
|
|
|
|
.prescaler = 0x01
|
|
|
|
},
|
|
|
|
.timing_data_max = {
|
|
|
|
.sjw = 0x0F,
|
|
|
|
.prop_seg = 0x0F,
|
|
|
|
.phase_seg1 = 0x0F,
|
|
|
|
.phase_seg2 = 0x0F,
|
|
|
|
.prescaler = 0xFFFF
|
|
|
|
},
|
|
|
|
#endif /* CONFIG_CAN_FD_MODE */
|
|
|
|
};
|
|
|
|
|
|
|
|
static int can_npl_init(const struct device *dev)
|
|
|
|
{
|
|
|
|
const struct can_npl_config *cfg = dev->config;
|
|
|
|
struct can_npl_data *data = dev->data;
|
|
|
|
|
|
|
|
k_mutex_init(&data->filter_mutex);
|
|
|
|
k_sem_init(&data->tx_idle, 1, 1);
|
|
|
|
|
|
|
|
data->dev_fd = linux_socketcan_iface_open(cfg->if_name);
|
|
|
|
if (data->dev_fd < 0) {
|
|
|
|
LOG_ERR("Cannot open %s (%d)", cfg->if_name, data->dev_fd);
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
k_thread_create(&data->rx_thread, data->rx_thread_stack,
|
|
|
|
K_KERNEL_STACK_SIZEOF(data->rx_thread_stack),
|
|
|
|
rx_thread, (void *)dev, NULL, NULL,
|
|
|
|
CONFIG_CAN_NATIVE_POSIX_LINUX_RX_THREAD_PRIORITY,
|
|
|
|
0, K_NO_WAIT);
|
|
|
|
|
|
|
|
LOG_DBG("Init of %s done", dev->name);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CAN_NATIVE_POSIX_LINUX_INIT(inst) \
|
|
|
|
\
|
|
|
|
static const struct can_npl_config can_npl_cfg_##inst = { \
|
|
|
|
.if_name = DT_INST_PROP(inst, host_interface), \
|
|
|
|
}; \
|
|
|
|
\
|
|
|
|
static struct can_npl_data can_npl_data_##inst; \
|
|
|
|
\
|
|
|
|
DEVICE_DT_INST_DEFINE(inst, &can_npl_init, NULL, \
|
|
|
|
&can_npl_data_##inst, &can_npl_cfg_##inst, \
|
|
|
|
POST_KERNEL, CONFIG_CAN_INIT_PRIORITY, \
|
|
|
|
&can_npl_driver_api);
|
|
|
|
|
|
|
|
DT_INST_FOREACH_STATUS_OKAY(CAN_NATIVE_POSIX_LINUX_INIT)
|