Skip to content

Commit f499559

Browse files
henrikbrixandersennashif
authored andcommitted
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 <[email protected]>
1 parent 28d9b54 commit f499559

File tree

18 files changed

+162
-152
lines changed

18 files changed

+162
-152
lines changed

doc/reference/networking/can_api.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ a mailbox. When a transmitting mailbox is assigned, sending cannot be canceled.
164164
can_dev = device_get_binding("CAN_0");
165165
166166
ret = can_send(can_dev, &frame, K_MSEC(100), NULL, NULL);
167-
if (ret != CAN_TX_OK) {
167+
if (ret != 0) {
168168
LOG_ERR("Sending failed [%d]", ret);
169169
}
170170

drivers/can/can_loopback.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void tx_thread(void *data_arg, void *arg2, void *arg3)
7373
if (!frame.cb) {
7474
k_sem_give(frame.tx_compl);
7575
} else {
76-
frame.cb(CAN_TX_OK, frame.cb_arg);
76+
frame.cb(0, frame.cb_arg);
7777
}
7878
}
7979
}
@@ -96,7 +96,7 @@ int can_loopback_send(const struct device *dev,
9696

9797
if (frame->dlc > CAN_MAX_DLC) {
9898
LOG_ERR("DLC of %d exceeds maximum (%d)", frame->dlc, CAN_MAX_DLC);
99-
return CAN_TX_EINVAL;
99+
return -EINVAL;
100100
}
101101

102102
if (!data->loopback) {
@@ -118,7 +118,7 @@ int can_loopback_send(const struct device *dev,
118118
k_sem_take(&tx_sem, K_FOREVER);
119119
}
120120

121-
return ret ? CAN_TIMEOUT : CAN_TX_OK;
121+
return ret ? -EAGAIN : 0;
122122
}
123123

124124

@@ -130,7 +130,7 @@ static inline int get_free_filter(struct can_loopback_filter *filters)
130130
}
131131
}
132132

133-
return CAN_NO_FREE_FILTER;
133+
return -ENOSPC;
134134
}
135135

136136
int can_loopback_attach_isr(const struct device *dev, can_rx_callback_t isr,

drivers/can/can_mcan.c

+16-16
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ static int can_exit_sleep_mode(struct can_mcan_reg *can)
3434
if (k_cycle_get_32() - start_time >
3535
k_ms_to_cyc_ceil32(CAN_INIT_TIMEOUT)) {
3636
can->cccr |= CAN_MCAN_CCCR_CSR;
37-
return CAN_TIMEOUT;
37+
return -EAGAIN;
3838
}
3939
}
4040

@@ -51,7 +51,7 @@ static int can_enter_init_mode(struct can_mcan_reg *can, k_timeout_t timeout)
5151
while ((can->cccr & CAN_MCAN_CCCR_INIT) == 0U) {
5252
if (k_uptime_ticks() - start_time > timeout.ticks) {
5353
can->cccr &= ~CAN_MCAN_CCCR_INIT;
54-
return CAN_TIMEOUT;
54+
return -EAGAIN;
5555
}
5656
}
5757

@@ -67,7 +67,7 @@ static int can_leave_init_mode(struct can_mcan_reg *can, k_timeout_t timeout)
6767

6868
while ((can->cccr & CAN_MCAN_CCCR_INIT) != 0U) {
6969
if (k_uptime_ticks() - start_time > timeout.ticks) {
70-
return CAN_TIMEOUT;
70+
return -EAGAIN;
7171
}
7272
}
7373

@@ -431,7 +431,7 @@ static void can_mcan_tc_event_handler(struct can_mcan_reg *can,
431431
if (tx_cb == NULL) {
432432
k_sem_give(&data->tx_fin_sem[tx_idx]);
433433
} else {
434-
tx_cb(CAN_TX_OK, data->tx_fin_cb_arg[tx_idx]);
434+
tx_cb(0, data->tx_fin_cb_arg[tx_idx]);
435435
}
436436
}
437437
}
@@ -658,21 +658,21 @@ int can_mcan_send(const struct can_mcan_config *cfg,
658658
if (data_length > sizeof(frame->data)) {
659659
LOG_ERR("data length (%zu) > max frame data length (%zu)",
660660
data_length, sizeof(frame->data));
661-
return CAN_TX_EINVAL;
661+
return -EINVAL;
662662
}
663663

664664
if (frame->fd != 1 && frame->dlc > MCAN_MAX_DLC) {
665665
LOG_ERR("DLC of %d without fd flag set.", frame->dlc);
666-
return CAN_TX_EINVAL;
666+
return -EINVAL;
667667
}
668668

669669
if (can->psr & CAN_MCAN_PSR_BO) {
670-
return CAN_TX_BUS_OFF;
670+
return -ENETDOWN;
671671
}
672672

673673
ret = k_sem_take(&data->tx_sem, timeout);
674674
if (ret != 0) {
675-
return CAN_TIMEOUT;
675+
return -EAGAIN;
676676
}
677677

678678
__ASSERT_NO_MSG((can->txfqs & CAN_MCAN_TXFQS_TFQF) !=
@@ -715,7 +715,7 @@ int can_mcan_send(const struct can_mcan_config *cfg,
715715
k_sem_take(&data->tx_fin_sem[put_idx], K_FOREVER);
716716
}
717717

718-
return CAN_TX_OK;
718+
return 0;
719719
}
720720

721721
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)
726726
}
727727
}
728728

729-
return CAN_NO_FREE_FILTER;
729+
return -ENOSPC;
730730
}
731731

732732
/* 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,
749749
k_mutex_lock(&data->inst_mutex, K_FOREVER);
750750
filter_nr = can_mcan_get_free_std(msg_ram->std_filt);
751751

752-
if (filter_nr == CAN_NO_FREE_FILTER) {
752+
if (filter_nr == -ENOSPC) {
753753
LOG_INF("No free standard id filter left");
754-
return CAN_NO_FREE_FILTER;
754+
return -ENOSPC;
755755
}
756756

757757
/* TODO propper fifo balancing */
@@ -790,7 +790,7 @@ static int can_mcan_get_free_ext(volatile struct can_mcan_ext_filter *filters)
790790
}
791791
}
792792

793-
return CAN_NO_FREE_FILTER;
793+
return -ENOSPC;
794794
}
795795

796796
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,
808808
k_mutex_lock(&data->inst_mutex, K_FOREVER);
809809
filter_nr = can_mcan_get_free_ext(msg_ram->ext_filt);
810810

811-
if (filter_nr == CAN_NO_FREE_FILTER) {
811+
if (filter_nr == -ENOSPC) {
812812
LOG_INF("No free extender id filter left");
813-
return CAN_NO_FREE_FILTER;
813+
return -ENOSPC;
814814
}
815815

816816
/* TODO propper fifo balancing */
@@ -861,7 +861,7 @@ int can_mcan_attach_isr(struct can_mcan_data *data,
861861
filter_nr += NUM_STD_FILTER_DATA;
862862
}
863863

864-
if (filter_nr == CAN_NO_FREE_FILTER) {
864+
if (filter_nr == -ENOSPC) {
865865
LOG_INF("No free filter left");
866866
}
867867

drivers/can/can_mcp2515.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -470,11 +470,11 @@ static int mcp2515_send(const struct device *dev,
470470
if (frame->dlc > CAN_MAX_DLC) {
471471
LOG_ERR("DLC of %d exceeds maximum (%d)",
472472
frame->dlc, CAN_MAX_DLC);
473-
return CAN_TX_EINVAL;
473+
return -EINVAL;
474474
}
475475

476476
if (k_sem_take(&dev_data->tx_sem, timeout) != 0) {
477-
return CAN_TIMEOUT;
477+
return -EAGAIN;
478478
}
479479

480480
k_mutex_lock(&dev_data->mutex, K_FOREVER);
@@ -491,7 +491,7 @@ static int mcp2515_send(const struct device *dev,
491491

492492
if (tx_idx == MCP2515_TX_CNT) {
493493
LOG_WRN("no free tx slot available");
494-
return CAN_TX_ERR;
494+
return -EIO;
495495
}
496496

497497
dev_data->tx_cb[tx_idx].cb = callback;
@@ -545,7 +545,7 @@ static int mcp2515_attach_isr(const struct device *dev,
545545
dev_data->cb_arg[filter_idx] = cb_arg;
546546

547547
} else {
548-
filter_idx = CAN_NO_FREE_FILTER;
548+
filter_idx = -ENOSPC;
549549
}
550550

551551
k_mutex_unlock(&dev_data->mutex);

drivers/can/can_mcux_flexcan.c

+15-15
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ static int mcux_flexcan_send(const struct device *dev,
328328

329329
if (frame->dlc > CAN_MAX_DLC) {
330330
LOG_ERR("DLC of %d exceeds maximum (%d)", frame->dlc, CAN_MAX_DLC);
331-
return CAN_TX_EINVAL;
331+
return -EINVAL;
332332
}
333333

334334
while (true) {
@@ -342,7 +342,7 @@ static int mcux_flexcan_send(const struct device *dev,
342342
}
343343

344344
if (k_sem_take(&data->tx_allocs_sem, timeout) != 0) {
345-
return CAN_TIMEOUT;
345+
return -EAGAIN;
346346
}
347347
}
348348

@@ -355,15 +355,15 @@ static int mcux_flexcan_send(const struct device *dev,
355355
status = FLEXCAN_TransferSendNonBlocking(config->base, &data->handle,
356356
&xfer);
357357
if (status != kStatus_Success) {
358-
return CAN_TX_ERR;
358+
return -EIO;
359359
}
360360

361361
if (callback == NULL) {
362362
k_sem_take(&data->tx_cbs[alloc].done, K_FOREVER);
363363
return data->tx_cbs[alloc].status;
364364
}
365365

366-
return CAN_TX_OK;
366+
return 0;
367367
}
368368

369369
static int mcux_flexcan_attach_isr(const struct device *dev,
@@ -376,7 +376,7 @@ static int mcux_flexcan_attach_isr(const struct device *dev,
376376
flexcan_mb_transfer_t xfer;
377377
status_t status;
378378
uint32_t mask;
379-
int alloc = CAN_NO_FREE_FILTER;
379+
int alloc = -ENOSPC;
380380
int i;
381381

382382
__ASSERT_NO_MSG(isr);
@@ -391,7 +391,7 @@ static int mcux_flexcan_attach_isr(const struct device *dev,
391391
}
392392
}
393393

394-
if (alloc == CAN_NO_FREE_FILTER) {
394+
if (alloc == -ENOSPC) {
395395
return alloc;
396396
}
397397

@@ -414,7 +414,7 @@ static int mcux_flexcan_attach_isr(const struct device *dev,
414414
if (status != kStatus_Success) {
415415
LOG_ERR("Failed to start rx for filter id %d (err = %d)",
416416
alloc, status);
417-
alloc = CAN_NO_FREE_FILTER;
417+
alloc = -ENOSPC;
418418
}
419419

420420
k_mutex_unlock(&data->rx_mutex);
@@ -473,7 +473,7 @@ int mcux_flexcan_recover(const struct device *dev, k_timeout_t timeout)
473473
while (mcux_flexcan_get_state(dev, NULL) == CAN_BUS_OFF) {
474474
if (!K_TIMEOUT_EQ(timeout, K_FOREVER) &&
475475
k_uptime_ticks() - start_time >= timeout.ticks) {
476-
ret = CAN_TIMEOUT;
476+
ret = -EAGAIN;
477477
}
478478
}
479479
}
@@ -518,22 +518,22 @@ static inline void mcux_flexcan_transfer_error_status(const struct device *dev,
518518
const struct mcux_flexcan_config *config = dev->config;
519519
struct mcux_flexcan_data *data = dev->data;
520520
can_tx_callback_t function;
521-
int status = CAN_TX_OK;
521+
int status = 0;
522522
void *arg;
523523
int alloc;
524524
enum can_state state;
525525
struct can_bus_err_cnt err_cnt;
526526

527527
if (error & CAN_ESR1_FLTCONF(2)) {
528528
LOG_DBG("Tx bus off (error 0x%08llx)", error);
529-
status = CAN_TX_BUS_OFF;
529+
status = -ENETDOWN;
530530
} else if ((error & kFLEXCAN_Bit0Error) ||
531531
(error & kFLEXCAN_Bit1Error)) {
532532
LOG_DBG("TX arbitration lost (error 0x%08llx)", error);
533-
status = CAN_TX_ARB_LOST;
533+
status = -EBUSY;
534534
} else if (error & kFLEXCAN_AckError) {
535535
LOG_DBG("TX no ACK received (error 0x%08llx)", error);
536-
status = CAN_TX_ERR;
536+
status = -EIO;
537537
} else if (error & kFLEXCAN_StuffingError) {
538538
LOG_DBG("RX stuffing error (error 0x%08llx)", error);
539539
} else if (error & kFLEXCAN_FormError) {
@@ -552,7 +552,7 @@ static inline void mcux_flexcan_transfer_error_status(const struct device *dev,
552552
}
553553
}
554554

555-
if (status == CAN_TX_OK) {
555+
if (status == 0) {
556556
/*
557557
* Error/status is not TX related. No further action
558558
* required.
@@ -605,9 +605,9 @@ static inline void mcux_flexcan_transfer_tx_idle(const struct device *dev,
605605

606606
if (atomic_test_and_clear_bit(data->tx_allocs, alloc)) {
607607
if (function != NULL) {
608-
function(CAN_TX_OK, arg);
608+
function(0, arg);
609609
} else {
610-
data->tx_cbs[alloc].status = CAN_TX_OK;
610+
data->tx_cbs[alloc].status = 0;
611611
k_sem_give(&data->tx_cbs[alloc].done);
612612
}
613613
k_sem_give(&data->tx_allocs_sem);

0 commit comments

Comments
 (0)