1 /*
2  * Copyright (c) 2006-2023, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2023-09-23     GuEe-GUI     first version
9  */
10 
11 #ifndef __MAILBOX_H__
12 #define __MAILBOX_H__
13 
14 #include <rtdef.h>
15 #include <drivers/ofw.h>
16 
17 struct rt_mbox_chan;
18 struct rt_mbox_client;
19 struct rt_mbox_controller_ops;
20 
21 struct rt_mbox_controller
22 {
23     rt_list_t list;
24 
25     struct rt_device *dev;
26 
27     const struct rt_mbox_controller_ops *ops;
28 
29     rt_size_t num_chans;
30     struct rt_mbox_chan *chans;
31 };
32 
33 struct rt_mbox_controller_ops
34 {
35     rt_err_t (*request)(struct rt_mbox_chan *);
36     void (*release)(struct rt_mbox_chan *);
37     rt_err_t (*send)(struct rt_mbox_chan *, const void *data);
38     rt_bool_t (*peek)(struct rt_mbox_chan *);
39     int (*ofw_parse)(struct rt_mbox_controller *, struct rt_ofw_cell_args *);
40 };
41 
42 struct rt_mbox_chan
43 {
44     struct rt_mbox_controller *ctrl;
45     struct rt_mbox_client *client;
46 
47     void *data;
48     rt_bool_t complete;
49     struct rt_timer timer;
50     struct rt_spinlock lock;
51 
52     void *priv;
53 };
54 
55 struct rt_mbox_client
56 {
57     struct rt_device *dev;
58 
59     void (*rx_callback)(struct rt_mbox_client *, void *data);
60     void (*tx_prepare)(struct rt_mbox_client *, const void *data);
61     void (*tx_done)(struct rt_mbox_client *, const void *data, rt_err_t err);
62 };
63 
64 rt_err_t rt_mbox_controller_register(struct rt_mbox_controller *ctrl);
65 rt_err_t rt_mbox_controller_unregister(struct rt_mbox_controller *ctrl);
66 
67 rt_err_t rt_mbox_send(struct rt_mbox_chan *chan, const void *data,
68         rt_uint32_t timeout_ms);
69 void rt_mbox_send_done(struct rt_mbox_chan *chan, rt_err_t err);
70 rt_bool_t rt_mbox_peek(struct rt_mbox_chan *chan);
71 rt_err_t rt_mbox_recv(struct rt_mbox_chan *chan, void *data);
72 
73 struct rt_mbox_chan *rt_mbox_request_by_index(struct rt_mbox_client *client, int index);
74 struct rt_mbox_chan *rt_mbox_request_by_name(struct rt_mbox_client *client, char *name);
75 rt_err_t rt_mbox_release(struct rt_mbox_chan *chan);
76 
77 #endif /* __MAILBOX_H__ */
78