]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - rpmsg/rpmsg.git/blob - include/linux/rpmsg.h
rpmsg: fix lockdep warnings in virtio rpmsg bus driver
[rpmsg/rpmsg.git] / include / linux / rpmsg.h
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /*
3  * Remote processor messaging
4  *
5  * Copyright (C) 2011 Texas Instruments, Inc.
6  * Copyright (C) 2011 Google, Inc.
7  * All rights reserved.
8  */
10 #ifndef _LINUX_RPMSG_H
11 #define _LINUX_RPMSG_H
13 #include <linux/types.h>
14 #include <linux/device.h>
15 #include <linux/err.h>
16 #include <linux/mod_devicetable.h>
17 #include <linux/kref.h>
18 #include <linux/mutex.h>
19 #include <linux/poll.h>
21 #define RPMSG_ADDR_ANY          0xFFFFFFFF
23 struct rpmsg_device;
24 struct rpmsg_endpoint;
25 struct rpmsg_device_ops;
26 struct rpmsg_endpoint_ops;
28 /* lockdep subclasses for use with ept cb_lock mutex nested calls */
29 #define RPMSG_LOCKDEP_SUBCLASS_NORMAL   0 /* regular ept cb_lock */
30 #define RPMSG_LOCKDEP_SUBCLASS_NS       1 /* name service ept cb_lock */
32 /**
33  * struct rpmsg_channel_info - channel info representation
34  * @name: name of service
35  * @desc: description of service
36  * @src: local address
37  * @dst: destination address
38  */
39 struct rpmsg_channel_info {
40         char name[RPMSG_NAME_SIZE];
41         char desc[RPMSG_NAME_SIZE];
42         u32 src;
43         u32 dst;
44 };
46 /**
47  * rpmsg_device - device that belong to the rpmsg bus
48  * @dev: the device struct
49  * @id: device id (used to match between rpmsg drivers and devices)
50  * @driver_override: driver name to force a match
51  * @desc: description of remote service
52  * @src: local address
53  * @dst: destination address
54  * @ept: the rpmsg endpoint of this channel
55  * @announce: if set, rpmsg will announce the creation/removal of this channel
56  */
57 struct rpmsg_device {
58         struct device dev;
59         struct rpmsg_device_id id;
60         char *driver_override;
61         char desc[RPMSG_NAME_SIZE];
62         u32 src;
63         u32 dst;
64         struct rpmsg_endpoint *ept;
65         bool announce;
67         const struct rpmsg_device_ops *ops;
68 };
70 typedef int (*rpmsg_rx_cb_t)(struct rpmsg_device *, void *, int, void *, u32);
72 /**
73  * struct rpmsg_endpoint - binds a local rpmsg address to its user
74  * @rpdev: rpmsg channel device
75  * @refcount: when this drops to zero, the ept is deallocated
76  * @cb: rx callback handler
77  * @cb_lock: must be taken before accessing/changing @cb
78  * @cb_lockdep_class: mutex lockdep class to be used with @cb_lock
79  * @addr: local rpmsg address
80  * @priv: private data for the driver's use
81  *
82  * In essence, an rpmsg endpoint represents a listener on the rpmsg bus, as
83  * it binds an rpmsg address with an rx callback handler.
84  *
85  * Simple rpmsg drivers shouldn't use this struct directly, because
86  * things just work: every rpmsg driver provides an rx callback upon
87  * registering to the bus, and that callback is then bound to its rpmsg
88  * address when the driver is probed. When relevant inbound messages arrive
89  * (i.e. messages which their dst address equals to the src address of
90  * the rpmsg channel), the driver's handler is invoked to process it.
91  *
92  * More complicated drivers though, that do need to allocate additional rpmsg
93  * addresses, and bind them to different rx callbacks, must explicitly
94  * create additional endpoints by themselves (see rpmsg_create_ept()).
95  */
96 struct rpmsg_endpoint {
97         struct rpmsg_device *rpdev;
98         struct kref refcount;
99         rpmsg_rx_cb_t cb;
100         struct mutex cb_lock;
101         int cb_lockdep_class;
102         u32 addr;
103         void *priv;
105         const struct rpmsg_endpoint_ops *ops;
106 };
108 /**
109  * struct rpmsg_driver - rpmsg driver struct
110  * @drv: underlying device driver
111  * @id_table: rpmsg ids serviced by this driver
112  * @probe: invoked when a matching rpmsg channel (i.e. device) is found
113  * @remove: invoked when the rpmsg channel is removed
114  * @callback: invoked when an inbound message is received on the channel
115  */
116 struct rpmsg_driver {
117         struct device_driver drv;
118         const struct rpmsg_device_id *id_table;
119         int (*probe)(struct rpmsg_device *dev);
120         void (*remove)(struct rpmsg_device *dev);
121         int (*callback)(struct rpmsg_device *, void *, int, void *, u32);
122 };
124 #if IS_ENABLED(CONFIG_RPMSG)
126 int register_rpmsg_device(struct rpmsg_device *dev);
127 void unregister_rpmsg_device(struct rpmsg_device *dev);
128 int __register_rpmsg_driver(struct rpmsg_driver *drv, struct module *owner);
129 void unregister_rpmsg_driver(struct rpmsg_driver *drv);
130 void rpmsg_destroy_ept(struct rpmsg_endpoint *);
131 struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_device *,
132                                         rpmsg_rx_cb_t cb, void *priv,
133                                         struct rpmsg_channel_info chinfo);
135 int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len);
136 int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst);
137 int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
138                           void *data, int len);
140 int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len);
141 int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst);
142 int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
143                              void *data, int len);
145 __poll_t rpmsg_poll(struct rpmsg_endpoint *ept, struct file *filp,
146                         poll_table *wait);
148 #else
150 static inline int register_rpmsg_device(struct rpmsg_device *dev)
152         return -ENXIO;
155 static inline void unregister_rpmsg_device(struct rpmsg_device *dev)
157         /* This shouldn't be possible */
158         WARN_ON(1);
161 static inline int __register_rpmsg_driver(struct rpmsg_driver *drv,
162                                           struct module *owner)
164         /* This shouldn't be possible */
165         WARN_ON(1);
167         return -ENXIO;
170 static inline void unregister_rpmsg_driver(struct rpmsg_driver *drv)
172         /* This shouldn't be possible */
173         WARN_ON(1);
176 static inline void rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
178         /* This shouldn't be possible */
179         WARN_ON(1);
182 static inline struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_device *rpdev,
183                                                       rpmsg_rx_cb_t cb,
184                                                       void *priv,
185                                                       struct rpmsg_channel_info chinfo)
187         /* This shouldn't be possible */
188         WARN_ON(1);
190         return ERR_PTR(-ENXIO);
193 static inline int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len)
195         /* This shouldn't be possible */
196         WARN_ON(1);
198         return -ENXIO;
201 static inline int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len,
202                                u32 dst)
204         /* This shouldn't be possible */
205         WARN_ON(1);
207         return -ENXIO;
211 static inline int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src,
212                                         u32 dst, void *data, int len)
214         /* This shouldn't be possible */
215         WARN_ON(1);
217         return -ENXIO;
220 static inline int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len)
222         /* This shouldn't be possible */
223         WARN_ON(1);
225         return -ENXIO;
228 static inline int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data,
229                                   int len, u32 dst)
231         /* This shouldn't be possible */
232         WARN_ON(1);
234         return -ENXIO;
237 static inline int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src,
238                                            u32 dst, void *data, int len)
240         /* This shouldn't be possible */
241         WARN_ON(1);
243         return -ENXIO;
246 static inline __poll_t rpmsg_poll(struct rpmsg_endpoint *ept,
247                                       struct file *filp, poll_table *wait)
249         /* This shouldn't be possible */
250         WARN_ON(1);
252         return 0;
255 #endif /* IS_ENABLED(CONFIG_RPMSG) */
257 /* use a macro to avoid include chaining to get THIS_MODULE */
258 #define register_rpmsg_driver(drv) \
259         __register_rpmsg_driver(drv, THIS_MODULE)
261 /**
262  * module_rpmsg_driver() - Helper macro for registering an rpmsg driver
263  * @__rpmsg_driver: rpmsg_driver struct
264  *
265  * Helper macro for rpmsg drivers which do not do anything special in module
266  * init/exit. This eliminates a lot of boilerplate.  Each module may only
267  * use this macro once, and calling it replaces module_init() and module_exit()
268  */
269 #define module_rpmsg_driver(__rpmsg_driver) \
270         module_driver(__rpmsg_driver, register_rpmsg_driver, \
271                         unregister_rpmsg_driver)
273 #endif /* _LINUX_RPMSG_H */