Skip to content

Commit 67ba990

Browse files
henrikbrixandersennashif
authored andcommitted
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 <[email protected]>
1 parent 9cc32af commit 67ba990

File tree

16 files changed

+137
-105
lines changed

16 files changed

+137
-105
lines changed

doc/reference/canbus/controller.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,9 @@ occurred. It does not block until the message is sent like the example above.
175175

176176
.. code-block:: C
177177
178-
void tx_irq_callback(int error, void *arg)
178+
void tx_callback(const struct device *dev, int error, void *user_data)
179179
{
180-
char *sender = (char *)arg;
180+
char *sender = (char *)user_data;
181181
182182
if (error != 0) {
183183
LOG_ERR("Sending failed [%d]\nSender: %s\n", error, sender);
@@ -211,7 +211,7 @@ added.
211211

212212
.. code-block:: C
213213
214-
void rx_callback_function(struct zcan_frame *frame, void *user_data)
214+
void rx_callback_function(const struct device *dev, struct zcan_frame *frame, void *user_data)
215215
{
216216
... do something with the frame ...
217217
}

drivers/can/can_common.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,18 @@ LOG_MODULE_REGISTER(can_common, CONFIG_CAN_LOG_LEVEL);
1414
/* CAN sync segment is always one time quantum */
1515
#define CAN_SYNC_SEG 1
1616

17-
static void can_msgq_put(struct zcan_frame *frame, void *arg)
17+
static void can_msgq_put(const struct device *dev, struct zcan_frame *frame, void *user_data)
1818
{
19-
struct k_msgq *msgq = (struct k_msgq *)arg;
19+
struct k_msgq *msgq = (struct k_msgq *)user_data;
2020
int ret;
2121

22+
ARG_UNUSED(dev);
23+
2224
__ASSERT_NO_MSG(msgq);
2325

2426
ret = k_msgq_put(msgq, frame, K_NO_WAIT);
2527
if (ret) {
26-
LOG_ERR("Msgq %p overflowed. Frame ID: 0x%x", arg, frame->id);
28+
LOG_ERR("Msgq %p overflowed. Frame ID: 0x%x", msgq, frame->id);
2729
}
2830
}
2931

drivers/can/can_loopback.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ struct can_loopback_data {
4141
CONFIG_CAN_LOOPBACK_TX_THREAD_STACK_SIZE);
4242
};
4343

44-
static void dispatch_frame(const struct zcan_frame *frame,
44+
static void dispatch_frame(const struct device *dev,
45+
const struct zcan_frame *frame,
4546
struct can_loopback_filter *filter)
4647
{
4748
struct zcan_frame frame_tmp = *frame;
@@ -52,7 +53,7 @@ static void dispatch_frame(const struct zcan_frame *frame,
5253
"standard" : "extended",
5354
frame->rtr == CAN_DATAFRAME ? "" : ", RTR frame");
5455

55-
filter->rx_cb(&frame_tmp, filter->cb_arg);
56+
filter->rx_cb(dev, &frame_tmp, filter->cb_arg);
5657
}
5758

5859
static inline int check_filter_match(const struct zcan_frame *frame,
@@ -62,13 +63,15 @@ static inline int check_filter_match(const struct zcan_frame *frame,
6263
(frame->id & filter->id_mask));
6364
}
6465

65-
void tx_thread(void *data_arg, void *arg2, void *arg3)
66+
void tx_thread(void *arg1, void *arg2, void *arg3)
6667
{
67-
ARG_UNUSED(arg2);
68-
ARG_UNUSED(arg3);
68+
const struct device *dev = arg1;
69+
struct can_loopback_data *data = dev->data;
6970
struct can_loopback_frame frame;
7071
struct can_loopback_filter *filter;
71-
struct can_loopback_data *data = (struct can_loopback_data *)data_arg;
72+
73+
ARG_UNUSED(arg2);
74+
ARG_UNUSED(arg3);
7275

7376
while (1) {
7477
k_msgq_get(&data->tx_msgq, &frame, K_FOREVER);
@@ -78,7 +81,7 @@ void tx_thread(void *data_arg, void *arg2, void *arg3)
7881
filter = &data->filters[i];
7982
if (filter->rx_cb &&
8083
check_filter_match(&frame.frame, &filter->filter)) {
81-
dispatch_frame(&frame.frame, filter);
84+
dispatch_frame(dev, &frame.frame, filter);
8285
}
8386
}
8487

@@ -87,7 +90,7 @@ void tx_thread(void *data_arg, void *arg2, void *arg3)
8790
if (!frame.cb) {
8891
k_sem_give(frame.tx_compl);
8992
} else {
90-
frame.cb(0, frame.cb_arg);
93+
frame.cb(dev, 0, frame.cb_arg);
9194
}
9295
}
9396
}
@@ -307,7 +310,7 @@ static int can_loopback_init(const struct device *dev)
307310

308311
tx_tid = k_thread_create(&data->tx_thread_data, data->tx_thread_stack,
309312
K_KERNEL_STACK_SIZEOF(data->tx_thread_stack),
310-
tx_thread, data, NULL, NULL,
313+
tx_thread, (void *)dev, NULL, NULL,
311314
CONFIG_CAN_LOOPBACK_TX_THREAD_PRIORITY,
312315
0, K_NO_WAIT);
313316
if (!tx_tid) {

drivers/can/can_mcan.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,11 @@ int can_mcan_init(const struct device *dev, const struct can_mcan_config *cfg,
288288
#endif
289289
int ret;
290290

291+
data->dev = dev;
291292
k_mutex_init(&data->inst_mutex);
292293
k_mutex_init(&data->tx_mtx);
293294
k_sem_init(&data->tx_sem, NUM_TX_BUF_ELEMENTS, NUM_TX_BUF_ELEMENTS);
295+
294296
for (int i = 0; i < ARRAY_SIZE(data->tx_fin_sem); ++i) {
295297
k_sem_init(&data->tx_fin_sem[i], 0, 1);
296298
}
@@ -502,7 +504,7 @@ static void can_mcan_state_change_handler(const struct can_mcan_config *cfg,
502504
(void)can_mcan_get_state(cfg, &state, &err_cnt);
503505

504506
if (cb != NULL) {
505-
cb(state, err_cnt, cb_data);
507+
cb(data->dev, state, err_cnt, cb_data);
506508
}
507509
}
508510

@@ -530,7 +532,7 @@ static void can_mcan_tc_event_handler(struct can_mcan_reg *can,
530532
if (tx_cb == NULL) {
531533
k_sem_give(&data->tx_fin_sem[tx_idx]);
532534
} else {
533-
tx_cb(0, data->tx_fin_cb_arg[tx_idx]);
535+
tx_cb(data->dev, 0, data->tx_fin_cb_arg[tx_idx]);
534536
}
535537
}
536538
}
@@ -643,7 +645,7 @@ static void can_mcan_get_message(struct can_mcan_data *data,
643645
}
644646

645647
if (cb) {
646-
cb(&frame, cb_arg);
648+
cb(data->dev, &frame, cb_arg);
647649
} else {
648650
LOG_DBG("cb missing");
649651
}

drivers/can/can_mcan.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ struct can_mcan_msg_sram {
166166
} __packed __aligned(4);
167167

168168
struct can_mcan_data {
169+
const struct device *dev;
169170
struct k_mutex inst_mutex;
170171
struct k_sem tx_sem;
171172
struct k_mutex tx_mtx;

drivers/can/can_mcp2515.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ static void mcp2515_rx_filter(const struct device *dev,
635635
/*Make a temporary copy in case the user modifies the message*/
636636
tmp_frame = *frame;
637637

638-
callback(&tmp_frame, dev_data->cb_arg[filter_id]);
638+
callback(dev, &tmp_frame, dev_data->cb_arg[filter_id]);
639639
}
640640

641641
k_mutex_unlock(&dev_data->mutex);
@@ -665,7 +665,7 @@ static void mcp2515_tx_done(const struct device *dev, uint8_t tx_idx)
665665
if (dev_data->tx_cb[tx_idx].cb == NULL) {
666666
k_sem_give(&dev_data->tx_cb[tx_idx].sem);
667667
} else {
668-
dev_data->tx_cb[tx_idx].cb(0, dev_data->tx_cb[tx_idx].cb_arg);
668+
dev_data->tx_cb[tx_idx].cb(dev, 0, dev_data->tx_cb[tx_idx].cb_arg);
669669
}
670670

671671
k_mutex_lock(&dev_data->mutex, K_FOREVER);
@@ -731,7 +731,7 @@ static void mcp2515_handle_errors(const struct device *dev)
731731

732732
if (state_change_cb && dev_data->old_state != state) {
733733
dev_data->old_state = state;
734-
state_change_cb(state, err_cnt, state_change_cb_data);
734+
state_change_cb(dev, state, err_cnt, state_change_cb_data);
735735
}
736736
}
737737

drivers/can/can_mcux_flexcan.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ static inline void mcux_flexcan_transfer_error_status(const struct device *dev,
547547
data->state = state;
548548

549549
if (cb != NULL) {
550-
cb(state, err_cnt, cb_data);
550+
cb(dev, state, err_cnt, cb_data);
551551
}
552552
}
553553

@@ -562,7 +562,7 @@ static inline void mcux_flexcan_transfer_error_status(const struct device *dev,
562562
FLEXCAN_TransferAbortSend(config->base, &data->handle,
563563
ALLOC_IDX_TO_TXMB_IDX(alloc));
564564
if (function != NULL) {
565-
function(-ENETDOWN, arg);
565+
function(dev, -ENETDOWN, arg);
566566
} else {
567567
data->tx_cbs[alloc].status = -ENETDOWN;
568568
k_sem_give(&data->tx_cbs[alloc].done);
@@ -590,7 +590,7 @@ static inline void mcux_flexcan_transfer_tx_idle(const struct device *dev,
590590

591591
if (atomic_test_and_clear_bit(data->tx_allocs, alloc)) {
592592
if (function != NULL) {
593-
function(0, arg);
593+
function(dev, 0, arg);
594594
} else {
595595
data->tx_cbs[alloc].status = 0;
596596
k_sem_give(&data->tx_cbs[alloc].done);
@@ -618,7 +618,7 @@ static inline void mcux_flexcan_transfer_rx_idle(const struct device *dev,
618618
if (atomic_test_bit(data->rx_allocs, alloc)) {
619619
mcux_flexcan_copy_frame_to_zframe(&data->rx_cbs[alloc].frame,
620620
&frame);
621-
function(&frame, arg);
621+
function(dev, &frame, arg);
622622

623623
/* Setup RX message buffer to receive next message */
624624
FLEXCAN_SetRxMbConfig(config->base, mb,

drivers/can/can_rcar.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ static void can_rcar_tx_done(const struct device *dev)
233233

234234
data->tx_unsent--;
235235
if (tx_cb->cb != NULL) {
236-
tx_cb->cb(0, tx_cb->cb_arg);
236+
tx_cb->cb(dev, 0, tx_cb->cb_arg);
237237
} else {
238238
k_sem_give(&tx_cb->sem);
239239
}
@@ -267,7 +267,7 @@ static void can_rcar_state_change(const struct device *dev, uint32_t newstate)
267267
return;
268268
}
269269
can_rcar_get_error_count(config, &err_cnt);
270-
cb(newstate, err_cnt, state_change_cb_data);
270+
cb(dev, newstate, err_cnt, state_change_cb_data);
271271
}
272272

273273
static void can_rcar_error(const struct device *dev)
@@ -366,7 +366,8 @@ static void can_rcar_error(const struct device *dev)
366366
}
367367
}
368368

369-
static void can_rcar_rx_filter_isr(struct can_rcar_data *data,
369+
static void can_rcar_rx_filter_isr(const struct device *dev,
370+
struct can_rcar_data *data,
370371
const struct zcan_frame *frame)
371372
{
372373
struct zcan_frame tmp_frame;
@@ -385,7 +386,7 @@ static void can_rcar_rx_filter_isr(struct can_rcar_data *data,
385386
* modifies the message.
386387
*/
387388
tmp_frame = *frame;
388-
data->rx_callback[i](&tmp_frame, data->rx_callback_arg[i]);
389+
data->rx_callback[i](dev, &tmp_frame, data->rx_callback_arg[i]);
389390
}
390391
}
391392

@@ -437,7 +438,7 @@ static void can_rcar_rx_isr(const struct device *dev)
437438
/* Increment CPU side pointer */
438439
sys_write8(0xff, config->reg_addr + RCAR_CAN_RFPCR);
439440

440-
can_rcar_rx_filter_isr(data, &frame);
441+
can_rcar_rx_filter_isr(dev, data, &frame);
441442
}
442443

443444
static void can_rcar_isr(const struct device *dev)

0 commit comments

Comments
 (0)