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>
This commit is contained in:
Henrik Brix Andersen 2021-12-08 00:01:57 +01:00 committed by Anas Nashif
commit f499559434
18 changed files with 162 additions and 152 deletions

View file

@ -73,7 +73,7 @@ void tx_thread(void *data_arg, void *arg2, void *arg3)
if (!frame.cb) {
k_sem_give(frame.tx_compl);
} else {
frame.cb(CAN_TX_OK, frame.cb_arg);
frame.cb(0, frame.cb_arg);
}
}
}
@ -96,7 +96,7 @@ int can_loopback_send(const struct device *dev,
if (frame->dlc > CAN_MAX_DLC) {
LOG_ERR("DLC of %d exceeds maximum (%d)", frame->dlc, CAN_MAX_DLC);
return CAN_TX_EINVAL;
return -EINVAL;
}
if (!data->loopback) {
@ -118,7 +118,7 @@ int can_loopback_send(const struct device *dev,
k_sem_take(&tx_sem, K_FOREVER);
}
return ret ? CAN_TIMEOUT : CAN_TX_OK;
return ret ? -EAGAIN : 0;
}
@ -130,7 +130,7 @@ static inline int get_free_filter(struct can_loopback_filter *filters)
}
}
return CAN_NO_FREE_FILTER;
return -ENOSPC;
}
int can_loopback_attach_isr(const struct device *dev, can_rx_callback_t isr,

View file

@ -34,7 +34,7 @@ static int can_exit_sleep_mode(struct can_mcan_reg *can)
if (k_cycle_get_32() - start_time >
k_ms_to_cyc_ceil32(CAN_INIT_TIMEOUT)) {
can->cccr |= CAN_MCAN_CCCR_CSR;
return CAN_TIMEOUT;
return -EAGAIN;
}
}
@ -51,7 +51,7 @@ static int can_enter_init_mode(struct can_mcan_reg *can, k_timeout_t timeout)
while ((can->cccr & CAN_MCAN_CCCR_INIT) == 0U) {
if (k_uptime_ticks() - start_time > timeout.ticks) {
can->cccr &= ~CAN_MCAN_CCCR_INIT;
return CAN_TIMEOUT;
return -EAGAIN;
}
}
@ -67,7 +67,7 @@ static int can_leave_init_mode(struct can_mcan_reg *can, k_timeout_t timeout)
while ((can->cccr & CAN_MCAN_CCCR_INIT) != 0U) {
if (k_uptime_ticks() - start_time > timeout.ticks) {
return CAN_TIMEOUT;
return -EAGAIN;
}
}
@ -431,7 +431,7 @@ static void can_mcan_tc_event_handler(struct can_mcan_reg *can,
if (tx_cb == NULL) {
k_sem_give(&data->tx_fin_sem[tx_idx]);
} else {
tx_cb(CAN_TX_OK, data->tx_fin_cb_arg[tx_idx]);
tx_cb(0, data->tx_fin_cb_arg[tx_idx]);
}
}
}
@ -658,21 +658,21 @@ int can_mcan_send(const struct can_mcan_config *cfg,
if (data_length > sizeof(frame->data)) {
LOG_ERR("data length (%zu) > max frame data length (%zu)",
data_length, sizeof(frame->data));
return CAN_TX_EINVAL;
return -EINVAL;
}
if (frame->fd != 1 && frame->dlc > MCAN_MAX_DLC) {
LOG_ERR("DLC of %d without fd flag set.", frame->dlc);
return CAN_TX_EINVAL;
return -EINVAL;
}
if (can->psr & CAN_MCAN_PSR_BO) {
return CAN_TX_BUS_OFF;
return -ENETDOWN;
}
ret = k_sem_take(&data->tx_sem, timeout);
if (ret != 0) {
return CAN_TIMEOUT;
return -EAGAIN;
}
__ASSERT_NO_MSG((can->txfqs & CAN_MCAN_TXFQS_TFQF) !=
@ -715,7 +715,7 @@ int can_mcan_send(const struct can_mcan_config *cfg,
k_sem_take(&data->tx_fin_sem[put_idx], K_FOREVER);
}
return CAN_TX_OK;
return 0;
}
static int can_mcan_get_free_std(volatile struct can_mcan_std_filter *filters)
@ -726,7 +726,7 @@ static int can_mcan_get_free_std(volatile struct can_mcan_std_filter *filters)
}
}
return CAN_NO_FREE_FILTER;
return -ENOSPC;
}
/* Use masked configuration only for simplicity. If someone needs more than
@ -749,9 +749,9 @@ int can_mcan_attach_std(struct can_mcan_data *data,
k_mutex_lock(&data->inst_mutex, K_FOREVER);
filter_nr = can_mcan_get_free_std(msg_ram->std_filt);
if (filter_nr == CAN_NO_FREE_FILTER) {
if (filter_nr == -ENOSPC) {
LOG_INF("No free standard id filter left");
return CAN_NO_FREE_FILTER;
return -ENOSPC;
}
/* TODO propper fifo balancing */
@ -790,7 +790,7 @@ static int can_mcan_get_free_ext(volatile struct can_mcan_ext_filter *filters)
}
}
return CAN_NO_FREE_FILTER;
return -ENOSPC;
}
static int can_mcan_attach_ext(struct can_mcan_data *data,
@ -808,9 +808,9 @@ static int can_mcan_attach_ext(struct can_mcan_data *data,
k_mutex_lock(&data->inst_mutex, K_FOREVER);
filter_nr = can_mcan_get_free_ext(msg_ram->ext_filt);
if (filter_nr == CAN_NO_FREE_FILTER) {
if (filter_nr == -ENOSPC) {
LOG_INF("No free extender id filter left");
return CAN_NO_FREE_FILTER;
return -ENOSPC;
}
/* TODO propper fifo balancing */
@ -861,7 +861,7 @@ int can_mcan_attach_isr(struct can_mcan_data *data,
filter_nr += NUM_STD_FILTER_DATA;
}
if (filter_nr == CAN_NO_FREE_FILTER) {
if (filter_nr == -ENOSPC) {
LOG_INF("No free filter left");
}

View file

@ -470,11 +470,11 @@ static int mcp2515_send(const struct device *dev,
if (frame->dlc > CAN_MAX_DLC) {
LOG_ERR("DLC of %d exceeds maximum (%d)",
frame->dlc, CAN_MAX_DLC);
return CAN_TX_EINVAL;
return -EINVAL;
}
if (k_sem_take(&dev_data->tx_sem, timeout) != 0) {
return CAN_TIMEOUT;
return -EAGAIN;
}
k_mutex_lock(&dev_data->mutex, K_FOREVER);
@ -491,7 +491,7 @@ static int mcp2515_send(const struct device *dev,
if (tx_idx == MCP2515_TX_CNT) {
LOG_WRN("no free tx slot available");
return CAN_TX_ERR;
return -EIO;
}
dev_data->tx_cb[tx_idx].cb = callback;
@ -545,7 +545,7 @@ static int mcp2515_attach_isr(const struct device *dev,
dev_data->cb_arg[filter_idx] = cb_arg;
} else {
filter_idx = CAN_NO_FREE_FILTER;
filter_idx = -ENOSPC;
}
k_mutex_unlock(&dev_data->mutex);

View file

@ -328,7 +328,7 @@ static int mcux_flexcan_send(const struct device *dev,
if (frame->dlc > CAN_MAX_DLC) {
LOG_ERR("DLC of %d exceeds maximum (%d)", frame->dlc, CAN_MAX_DLC);
return CAN_TX_EINVAL;
return -EINVAL;
}
while (true) {
@ -342,7 +342,7 @@ static int mcux_flexcan_send(const struct device *dev,
}
if (k_sem_take(&data->tx_allocs_sem, timeout) != 0) {
return CAN_TIMEOUT;
return -EAGAIN;
}
}
@ -355,7 +355,7 @@ static int mcux_flexcan_send(const struct device *dev,
status = FLEXCAN_TransferSendNonBlocking(config->base, &data->handle,
&xfer);
if (status != kStatus_Success) {
return CAN_TX_ERR;
return -EIO;
}
if (callback == NULL) {
@ -363,7 +363,7 @@ static int mcux_flexcan_send(const struct device *dev,
return data->tx_cbs[alloc].status;
}
return CAN_TX_OK;
return 0;
}
static int mcux_flexcan_attach_isr(const struct device *dev,
@ -376,7 +376,7 @@ static int mcux_flexcan_attach_isr(const struct device *dev,
flexcan_mb_transfer_t xfer;
status_t status;
uint32_t mask;
int alloc = CAN_NO_FREE_FILTER;
int alloc = -ENOSPC;
int i;
__ASSERT_NO_MSG(isr);
@ -391,7 +391,7 @@ static int mcux_flexcan_attach_isr(const struct device *dev,
}
}
if (alloc == CAN_NO_FREE_FILTER) {
if (alloc == -ENOSPC) {
return alloc;
}
@ -414,7 +414,7 @@ static int mcux_flexcan_attach_isr(const struct device *dev,
if (status != kStatus_Success) {
LOG_ERR("Failed to start rx for filter id %d (err = %d)",
alloc, status);
alloc = CAN_NO_FREE_FILTER;
alloc = -ENOSPC;
}
k_mutex_unlock(&data->rx_mutex);
@ -473,7 +473,7 @@ int mcux_flexcan_recover(const struct device *dev, k_timeout_t timeout)
while (mcux_flexcan_get_state(dev, NULL) == CAN_BUS_OFF) {
if (!K_TIMEOUT_EQ(timeout, K_FOREVER) &&
k_uptime_ticks() - start_time >= timeout.ticks) {
ret = CAN_TIMEOUT;
ret = -EAGAIN;
}
}
}
@ -518,7 +518,7 @@ static inline void mcux_flexcan_transfer_error_status(const struct device *dev,
const struct mcux_flexcan_config *config = dev->config;
struct mcux_flexcan_data *data = dev->data;
can_tx_callback_t function;
int status = CAN_TX_OK;
int status = 0;
void *arg;
int alloc;
enum can_state state;
@ -526,14 +526,14 @@ static inline void mcux_flexcan_transfer_error_status(const struct device *dev,
if (error & CAN_ESR1_FLTCONF(2)) {
LOG_DBG("Tx bus off (error 0x%08llx)", error);
status = CAN_TX_BUS_OFF;
status = -ENETDOWN;
} else if ((error & kFLEXCAN_Bit0Error) ||
(error & kFLEXCAN_Bit1Error)) {
LOG_DBG("TX arbitration lost (error 0x%08llx)", error);
status = CAN_TX_ARB_LOST;
status = -EBUSY;
} else if (error & kFLEXCAN_AckError) {
LOG_DBG("TX no ACK received (error 0x%08llx)", error);
status = CAN_TX_ERR;
status = -EIO;
} else if (error & kFLEXCAN_StuffingError) {
LOG_DBG("RX stuffing error (error 0x%08llx)", error);
} else if (error & kFLEXCAN_FormError) {
@ -552,7 +552,7 @@ static inline void mcux_flexcan_transfer_error_status(const struct device *dev,
}
}
if (status == CAN_TX_OK) {
if (status == 0) {
/*
* Error/status is not TX related. No further action
* required.
@ -605,9 +605,9 @@ static inline void mcux_flexcan_transfer_tx_idle(const struct device *dev,
if (atomic_test_and_clear_bit(data->tx_allocs, alloc)) {
if (function != NULL) {
function(CAN_TX_OK, arg);
function(0, arg);
} else {
data->tx_cbs[alloc].status = CAN_TX_OK;
data->tx_cbs[alloc].status = 0;
k_sem_give(&data->tx_cbs[alloc].done);
}
k_sem_give(&data->tx_allocs_sem);

View file

@ -232,7 +232,7 @@ static void can_rcar_tx_done(const struct device *dev)
data->tx_unsent--;
if (tx_cb->cb != NULL) {
tx_cb->cb(CAN_TX_OK, tx_cb->cb_arg);
tx_cb->cb(0, tx_cb->cb_arg);
} else {
k_sem_give(&tx_cb->sem);
}
@ -494,7 +494,7 @@ static int can_rcar_leave_sleep_mode(const struct can_rcar_cfg *config)
return 0;
}
}
return CAN_TIMEOUT;
return -EAGAIN;
}
static int can_rcar_enter_reset_mode(const struct can_rcar_cfg *config, bool force)
@ -514,7 +514,7 @@ static int can_rcar_enter_reset_mode(const struct can_rcar_cfg *config, bool for
return 0;
}
}
return CAN_TIMEOUT;
return -EAGAIN;
}
static int can_rcar_enter_halt_mode(const struct can_rcar_cfg *config)
@ -532,7 +532,7 @@ static int can_rcar_enter_halt_mode(const struct can_rcar_cfg *config)
}
}
return CAN_TIMEOUT;
return -EAGAIN;
}
static int can_rcar_enter_operation_mode(const struct can_rcar_cfg *config)
@ -552,7 +552,7 @@ static int can_rcar_enter_operation_mode(const struct can_rcar_cfg *config)
}
if (i == MAX_STR_READS) {
return CAN_TIMEOUT;
return -EAGAIN;
}
/* Enable Rx and Tx FIFO */
@ -688,7 +688,7 @@ int can_rcar_recover(const struct device *dev, k_timeout_t timeout)
}
if (k_mutex_lock(&data->inst_mutex, K_FOREVER)) {
return CAN_TIMEOUT;
return -EAGAIN;
}
start_time = k_uptime_ticks();
@ -700,7 +700,7 @@ int can_rcar_recover(const struct device *dev, k_timeout_t timeout)
if (!K_TIMEOUT_EQ(timeout, K_FOREVER) &&
k_uptime_ticks() - start_time >= timeout.ticks) {
ret = CAN_TIMEOUT;
ret = -EAGAIN;
goto done;
}
}
@ -736,12 +736,12 @@ int can_rcar_send(const struct device *dev, const struct zcan_frame *frame,
if (frame->dlc > CAN_MAX_DLC) {
LOG_ERR("DLC of %d exceeds maximum (%d)",
frame->dlc, CAN_MAX_DLC);
return CAN_TX_EINVAL;
return -EINVAL;
}
/* Wait for a slot into the tx FIFO */
if (k_sem_take(&data->tx_sem, timeout) != 0) {
return CAN_TIMEOUT;
return -EAGAIN;
}
k_mutex_lock(&data->inst_mutex, K_FOREVER);
@ -788,7 +788,7 @@ int can_rcar_send(const struct device *dev, const struct zcan_frame *frame,
k_sem_take(&tx_cb->sem, K_FOREVER);
}
return CAN_TX_OK;
return 0;
}
static inline int can_rcar_attach(const struct device *dev,
@ -809,7 +809,7 @@ static inline int can_rcar_attach(const struct device *dev,
}
}
return CAN_NO_FREE_FILTER;
return -ENOSPC;
}
int can_rcar_attach_isr(const struct device *dev, can_rx_callback_t isr,

View file

@ -371,7 +371,7 @@ static int cmd_attach(const struct shell *shell, size_t argc, char **argv)
ret = can_attach_workq(can_dev, &k_sys_work_q, &work, print_frame,
(void *)shell, &filter);
if (ret < 0) {
if (ret == CAN_NO_FREE_FILTER) {
if (ret == -ENOSPC) {
shell_error(shell, "Can't attach, no free filter left");
} else {
shell_error(shell, "Failed to attach filter [%d]", ret);

View file

@ -152,11 +152,11 @@ void can_stm32_tx_isr_handler(CAN_TypeDef *can, struct can_stm32_data *data)
if ((can->TSR & CAN_TSR_RQCP0) | bus_off) {
data->mb0.error_flags =
can->TSR & CAN_TSR_TXOK0 ? CAN_TX_OK :
can->TSR & CAN_TSR_TERR0 ? CAN_TX_ERR :
can->TSR & CAN_TSR_ALST0 ? CAN_TX_ARB_LOST :
bus_off ? CAN_TX_BUS_OFF :
CAN_TX_UNKNOWN;
can->TSR & CAN_TSR_TXOK0 ? 0 :
can->TSR & CAN_TSR_TERR0 ? -EIO :
can->TSR & CAN_TSR_ALST0 ? -EBUSY :
bus_off ? -ENETDOWN :
-EIO;
/* clear the request. */
can->TSR |= CAN_TSR_RQCP0;
can_stm32_signal_tx_complete(&data->mb0);
@ -164,11 +164,11 @@ void can_stm32_tx_isr_handler(CAN_TypeDef *can, struct can_stm32_data *data)
if ((can->TSR & CAN_TSR_RQCP1) | bus_off) {
data->mb1.error_flags =
can->TSR & CAN_TSR_TXOK1 ? CAN_TX_OK :
can->TSR & CAN_TSR_TERR1 ? CAN_TX_ERR :
can->TSR & CAN_TSR_ALST1 ? CAN_TX_ARB_LOST :
bus_off ? CAN_TX_BUS_OFF :
CAN_TX_UNKNOWN;
can->TSR & CAN_TSR_TXOK1 ? 0 :
can->TSR & CAN_TSR_TERR1 ? -EIO :
can->TSR & CAN_TSR_ALST1 ? -EBUSY :
bus_off ? -ENETDOWN :
-EIO;
/* clear the request. */
can->TSR |= CAN_TSR_RQCP1;
can_stm32_signal_tx_complete(&data->mb1);
@ -176,11 +176,11 @@ void can_stm32_tx_isr_handler(CAN_TypeDef *can, struct can_stm32_data *data)
if ((can->TSR & CAN_TSR_RQCP2) | bus_off) {
data->mb2.error_flags =
can->TSR & CAN_TSR_TXOK2 ? CAN_TX_OK :
can->TSR & CAN_TSR_TERR2 ? CAN_TX_ERR :
can->TSR & CAN_TSR_ALST2 ? CAN_TX_ARB_LOST :
bus_off ? CAN_TX_BUS_OFF :
CAN_TX_UNKNOWN;
can->TSR & CAN_TSR_TXOK2 ? 0 :
can->TSR & CAN_TSR_TERR2 ? -EIO :
can->TSR & CAN_TSR_ALST2 ? -EBUSY :
bus_off ? -ENETDOWN :
-EIO;
/* clear the request. */
can->TSR |= CAN_TSR_RQCP2;
can_stm32_signal_tx_complete(&data->mb2);
@ -270,7 +270,7 @@ static int can_enter_init_mode(CAN_TypeDef *can)
while ((can->MSR & CAN_MSR_INAK) == 0U) {
if (k_cycle_get_32() - start_time > CAN_INIT_TIMEOUT) {
can->MCR &= ~CAN_MCR_INRQ;
return CAN_TIMEOUT;
return -EAGAIN;
}
}
@ -286,7 +286,7 @@ static int can_leave_init_mode(CAN_TypeDef *can)
while ((can->MSR & CAN_MSR_INAK) != 0U) {
if (k_cycle_get_32() - start_time > CAN_INIT_TIMEOUT) {
return CAN_TIMEOUT;
return -EAGAIN;
}
}
@ -302,7 +302,7 @@ static int can_leave_sleep_mode(CAN_TypeDef *can)
while ((can->MSR & CAN_MSR_SLAK) != 0) {
if (k_cycle_get_32() - start_time > CAN_INIT_TIMEOUT) {
return CAN_TIMEOUT;
return -EAGAIN;
}
}
@ -570,7 +570,7 @@ int can_stm32_recover(const struct device *dev, k_timeout_t timeout)
const struct can_stm32_config *cfg = DEV_CFG(dev);
struct can_stm32_data *data = DEV_DATA(dev);
CAN_TypeDef *can = cfg->can;
int ret = CAN_TIMEOUT;
int ret = -EAGAIN;
int64_t start_time;
if (!(can->ESR & CAN_ESR_BOFF)) {
@ -578,7 +578,7 @@ int can_stm32_recover(const struct device *dev, k_timeout_t timeout)
}
if (k_mutex_lock(&data->inst_mutex, K_FOREVER)) {
return CAN_TIMEOUT;
return -EAGAIN;
}
ret = can_enter_init_mode(can);
@ -631,11 +631,11 @@ int can_stm32_send(const struct device *dev, const struct zcan_frame *frame,
if (frame->dlc > CAN_MAX_DLC) {
LOG_ERR("DLC of %d exceeds maximum (%d)", frame->dlc, CAN_MAX_DLC);
return CAN_TX_EINVAL;
return -EINVAL;
}
if (can->ESR & CAN_ESR_BOFF) {
return CAN_TX_BUS_OFF;
return -ENETDOWN;
}
k_mutex_lock(&data->inst_mutex, K_FOREVER);
@ -643,7 +643,7 @@ int can_stm32_send(const struct device *dev, const struct zcan_frame *frame,
k_mutex_unlock(&data->inst_mutex);
LOG_DBG("Transmit buffer full");
if (k_sem_take(&data->tx_int_sem, timeout)) {
return CAN_TIMEOUT;
return -EAGAIN;
}
k_mutex_lock(&data->inst_mutex, K_FOREVER);
@ -717,7 +717,7 @@ static int can_stm32_shift_arr(void **arr, int start, int count)
size_t cnt;
if (start > CONFIG_CAN_MAX_FILTER) {
return CAN_NO_FREE_FILTER;
return -ENOSPC;
}
if (count > 0) {
@ -726,7 +726,7 @@ static int can_stm32_shift_arr(void **arr, int start, int count)
/* Check if nothing used will be overwritten */
if (!can_stm32_check_free(arr, CONFIG_CAN_MAX_FILTER - count,
CONFIG_CAN_MAX_FILTER - 1)) {
return CAN_NO_FREE_FILTER;
return -ENOSPC;
}
/* No need to shift. Destination is already outside the arr*/
@ -742,7 +742,7 @@ static int can_stm32_shift_arr(void **arr, int start, int count)
count = -count;
if (start - count < 0) {
return CAN_NO_FREE_FILTER;
return -ENOSPC;
}
cnt = (CONFIG_CAN_MAX_FILTER - start) * sizeof(void *);
@ -889,7 +889,7 @@ static inline int can_stm32_set_filter(const struct zcan_filter *filter,
uint32_t mask = 0U;
uint32_t id = 0U;
int filter_nr = 0;
int filter_index_new = CAN_NO_FREE_FILTER;
int filter_index_new = -ENOSPC;
int bank_nr;
uint32_t bank_bit;
int register_demand;
@ -952,7 +952,7 @@ static inline int can_stm32_set_filter(const struct zcan_filter *filter,
if (!usage_shifted) {
LOG_INF("No free filter bank found");
return CAN_NO_FREE_FILTER;
return -ENOSPC;
}
} while (filter_nr < CAN_MAX_NUMBER_OF_FILTERS);
@ -988,7 +988,7 @@ static inline int can_stm32_set_filter(const struct zcan_filter *filter,
if (filter_index_new >= CONFIG_CAN_MAX_FILTER || res) {
LOG_INF("No space for a new filter!");
filter_nr = CAN_NO_FREE_FILTER;
filter_nr = -ENOSPC;
goto done;
}
}
@ -999,7 +999,7 @@ static inline int can_stm32_set_filter(const struct zcan_filter *filter,
filter_index_new = can_calc_filter_index(filter_nr, can->FM1R,
can->FS1R);
if (filter_index_new >= CAN_MAX_NUMBER_OF_FILTERS) {
filter_nr = CAN_NO_FREE_FILTER;
filter_nr = -ENOSPC;
goto done;
}
}
@ -1027,7 +1027,7 @@ static inline int can_stm32_attach(const struct device *dev,
int filter_nr;
filter_nr = can_stm32_set_filter(filter, data, can, &filter_index);
if (filter_nr != CAN_NO_FREE_FILTER) {
if (filter_nr != -ENOSPC) {
data->rx_cb[filter_index] = cb;
data->cb_arg[filter_index] = cb_arg;
}

View file

@ -95,7 +95,7 @@ static inline int socket_can_setsockopt(const struct device *dev, void *obj,
ret = can_attach_msgq(socket_context->can_dev, socket_context->msgq,
optval);
if (ret == CAN_NO_FREE_FILTER) {
if (ret == -ENOSPC) {
errno = ENOSPC;
return -1;
}