Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: BSD-3-Clause */
0002 /*
0003  * Remote processor messaging
0004  *
0005  * Copyright (C) 2011 Texas Instruments, Inc.
0006  * Copyright (C) 2011 Google, Inc.
0007  * All rights reserved.
0008  */
0009 
0010 #ifndef _LINUX_RPMSG_H
0011 #define _LINUX_RPMSG_H
0012 
0013 #include <linux/types.h>
0014 #include <linux/device.h>
0015 #include <linux/err.h>
0016 #include <linux/mod_devicetable.h>
0017 #include <linux/kref.h>
0018 #include <linux/mutex.h>
0019 #include <linux/poll.h>
0020 #include <linux/rpmsg/byteorder.h>
0021 #include <uapi/linux/rpmsg.h>
0022 
0023 struct rpmsg_device;
0024 struct rpmsg_endpoint;
0025 struct rpmsg_device_ops;
0026 struct rpmsg_endpoint_ops;
0027 
0028 /**
0029  * struct rpmsg_channel_info - channel info representation
0030  * @name: name of service
0031  * @src: local address
0032  * @dst: destination address
0033  */
0034 struct rpmsg_channel_info {
0035     char name[RPMSG_NAME_SIZE];
0036     u32 src;
0037     u32 dst;
0038 };
0039 
0040 /**
0041  * rpmsg_device - device that belong to the rpmsg bus
0042  * @dev: the device struct
0043  * @id: device id (used to match between rpmsg drivers and devices)
0044  * @driver_override: driver name to force a match; do not set directly,
0045  *                   because core frees it; use driver_set_override() to
0046  *                   set or clear it.
0047  * @src: local address
0048  * @dst: destination address
0049  * @ept: the rpmsg endpoint of this channel
0050  * @announce: if set, rpmsg will announce the creation/removal of this channel
0051  * @little_endian: True if transport is using little endian byte representation
0052  */
0053 struct rpmsg_device {
0054     struct device dev;
0055     struct rpmsg_device_id id;
0056     const char *driver_override;
0057     u32 src;
0058     u32 dst;
0059     struct rpmsg_endpoint *ept;
0060     bool announce;
0061     bool little_endian;
0062 
0063     const struct rpmsg_device_ops *ops;
0064 };
0065 
0066 typedef int (*rpmsg_rx_cb_t)(struct rpmsg_device *, void *, int, void *, u32);
0067 
0068 /**
0069  * struct rpmsg_endpoint - binds a local rpmsg address to its user
0070  * @rpdev: rpmsg channel device
0071  * @refcount: when this drops to zero, the ept is deallocated
0072  * @cb: rx callback handler
0073  * @cb_lock: must be taken before accessing/changing @cb
0074  * @addr: local rpmsg address
0075  * @priv: private data for the driver's use
0076  *
0077  * In essence, an rpmsg endpoint represents a listener on the rpmsg bus, as
0078  * it binds an rpmsg address with an rx callback handler.
0079  *
0080  * Simple rpmsg drivers shouldn't use this struct directly, because
0081  * things just work: every rpmsg driver provides an rx callback upon
0082  * registering to the bus, and that callback is then bound to its rpmsg
0083  * address when the driver is probed. When relevant inbound messages arrive
0084  * (i.e. messages which their dst address equals to the src address of
0085  * the rpmsg channel), the driver's handler is invoked to process it.
0086  *
0087  * More complicated drivers though, that do need to allocate additional rpmsg
0088  * addresses, and bind them to different rx callbacks, must explicitly
0089  * create additional endpoints by themselves (see rpmsg_create_ept()).
0090  */
0091 struct rpmsg_endpoint {
0092     struct rpmsg_device *rpdev;
0093     struct kref refcount;
0094     rpmsg_rx_cb_t cb;
0095     struct mutex cb_lock;
0096     u32 addr;
0097     void *priv;
0098 
0099     const struct rpmsg_endpoint_ops *ops;
0100 };
0101 
0102 /**
0103  * struct rpmsg_driver - rpmsg driver struct
0104  * @drv: underlying device driver
0105  * @id_table: rpmsg ids serviced by this driver
0106  * @probe: invoked when a matching rpmsg channel (i.e. device) is found
0107  * @remove: invoked when the rpmsg channel is removed
0108  * @callback: invoked when an inbound message is received on the channel
0109  */
0110 struct rpmsg_driver {
0111     struct device_driver drv;
0112     const struct rpmsg_device_id *id_table;
0113     int (*probe)(struct rpmsg_device *dev);
0114     void (*remove)(struct rpmsg_device *dev);
0115     int (*callback)(struct rpmsg_device *, void *, int, void *, u32);
0116 };
0117 
0118 static inline u16 rpmsg16_to_cpu(struct rpmsg_device *rpdev, __rpmsg16 val)
0119 {
0120     if (!rpdev)
0121         return __rpmsg16_to_cpu(rpmsg_is_little_endian(), val);
0122     else
0123         return __rpmsg16_to_cpu(rpdev->little_endian, val);
0124 }
0125 
0126 static inline __rpmsg16 cpu_to_rpmsg16(struct rpmsg_device *rpdev, u16 val)
0127 {
0128     if (!rpdev)
0129         return __cpu_to_rpmsg16(rpmsg_is_little_endian(), val);
0130     else
0131         return __cpu_to_rpmsg16(rpdev->little_endian, val);
0132 }
0133 
0134 static inline u32 rpmsg32_to_cpu(struct rpmsg_device *rpdev, __rpmsg32 val)
0135 {
0136     if (!rpdev)
0137         return __rpmsg32_to_cpu(rpmsg_is_little_endian(), val);
0138     else
0139         return __rpmsg32_to_cpu(rpdev->little_endian, val);
0140 }
0141 
0142 static inline __rpmsg32 cpu_to_rpmsg32(struct rpmsg_device *rpdev, u32 val)
0143 {
0144     if (!rpdev)
0145         return __cpu_to_rpmsg32(rpmsg_is_little_endian(), val);
0146     else
0147         return __cpu_to_rpmsg32(rpdev->little_endian, val);
0148 }
0149 
0150 static inline u64 rpmsg64_to_cpu(struct rpmsg_device *rpdev, __rpmsg64 val)
0151 {
0152     if (!rpdev)
0153         return __rpmsg64_to_cpu(rpmsg_is_little_endian(), val);
0154     else
0155         return __rpmsg64_to_cpu(rpdev->little_endian, val);
0156 }
0157 
0158 static inline __rpmsg64 cpu_to_rpmsg64(struct rpmsg_device *rpdev, u64 val)
0159 {
0160     if (!rpdev)
0161         return __cpu_to_rpmsg64(rpmsg_is_little_endian(), val);
0162     else
0163         return __cpu_to_rpmsg64(rpdev->little_endian, val);
0164 }
0165 
0166 #if IS_ENABLED(CONFIG_RPMSG)
0167 
0168 int rpmsg_register_device_override(struct rpmsg_device *rpdev,
0169                    const char *driver_override);
0170 int rpmsg_register_device(struct rpmsg_device *rpdev);
0171 int rpmsg_unregister_device(struct device *parent,
0172                 struct rpmsg_channel_info *chinfo);
0173 int __register_rpmsg_driver(struct rpmsg_driver *drv, struct module *owner);
0174 void unregister_rpmsg_driver(struct rpmsg_driver *drv);
0175 void rpmsg_destroy_ept(struct rpmsg_endpoint *);
0176 struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_device *,
0177                     rpmsg_rx_cb_t cb, void *priv,
0178                     struct rpmsg_channel_info chinfo);
0179 
0180 int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len);
0181 int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst);
0182 int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
0183               void *data, int len);
0184 
0185 int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len);
0186 int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst);
0187 int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
0188                  void *data, int len);
0189 
0190 __poll_t rpmsg_poll(struct rpmsg_endpoint *ept, struct file *filp,
0191             poll_table *wait);
0192 
0193 ssize_t rpmsg_get_mtu(struct rpmsg_endpoint *ept);
0194 
0195 #else
0196 
0197 static inline int rpmsg_register_device_override(struct rpmsg_device *rpdev,
0198                          const char *driver_override)
0199 {
0200     return -ENXIO;
0201 }
0202 
0203 static inline int rpmsg_register_device(struct rpmsg_device *rpdev)
0204 {
0205     return -ENXIO;
0206 }
0207 
0208 static inline int rpmsg_unregister_device(struct device *parent,
0209                       struct rpmsg_channel_info *chinfo)
0210 {
0211     /* This shouldn't be possible */
0212     WARN_ON(1);
0213 
0214     return -ENXIO;
0215 }
0216 
0217 static inline int __register_rpmsg_driver(struct rpmsg_driver *drv,
0218                       struct module *owner)
0219 {
0220     /* This shouldn't be possible */
0221     WARN_ON(1);
0222 
0223     return -ENXIO;
0224 }
0225 
0226 static inline void unregister_rpmsg_driver(struct rpmsg_driver *drv)
0227 {
0228     /* This shouldn't be possible */
0229     WARN_ON(1);
0230 }
0231 
0232 static inline void rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
0233 {
0234     /* This shouldn't be possible */
0235     WARN_ON(1);
0236 }
0237 
0238 static inline struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_device *rpdev,
0239                               rpmsg_rx_cb_t cb,
0240                               void *priv,
0241                               struct rpmsg_channel_info chinfo)
0242 {
0243     /* This shouldn't be possible */
0244     WARN_ON(1);
0245 
0246     return NULL;
0247 }
0248 
0249 static inline int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len)
0250 {
0251     /* This shouldn't be possible */
0252     WARN_ON(1);
0253 
0254     return -ENXIO;
0255 }
0256 
0257 static inline int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len,
0258                    u32 dst)
0259 {
0260     /* This shouldn't be possible */
0261     WARN_ON(1);
0262 
0263     return -ENXIO;
0264 
0265 }
0266 
0267 static inline int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src,
0268                     u32 dst, void *data, int len)
0269 {
0270     /* This shouldn't be possible */
0271     WARN_ON(1);
0272 
0273     return -ENXIO;
0274 }
0275 
0276 static inline int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len)
0277 {
0278     /* This shouldn't be possible */
0279     WARN_ON(1);
0280 
0281     return -ENXIO;
0282 }
0283 
0284 static inline int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data,
0285                   int len, u32 dst)
0286 {
0287     /* This shouldn't be possible */
0288     WARN_ON(1);
0289 
0290     return -ENXIO;
0291 }
0292 
0293 static inline int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src,
0294                        u32 dst, void *data, int len)
0295 {
0296     /* This shouldn't be possible */
0297     WARN_ON(1);
0298 
0299     return -ENXIO;
0300 }
0301 
0302 static inline __poll_t rpmsg_poll(struct rpmsg_endpoint *ept,
0303                       struct file *filp, poll_table *wait)
0304 {
0305     /* This shouldn't be possible */
0306     WARN_ON(1);
0307 
0308     return 0;
0309 }
0310 
0311 static inline ssize_t rpmsg_get_mtu(struct rpmsg_endpoint *ept)
0312 {
0313     /* This shouldn't be possible */
0314     WARN_ON(1);
0315 
0316     return -ENXIO;
0317 }
0318 
0319 #endif /* IS_ENABLED(CONFIG_RPMSG) */
0320 
0321 /* use a macro to avoid include chaining to get THIS_MODULE */
0322 #define register_rpmsg_driver(drv) \
0323     __register_rpmsg_driver(drv, THIS_MODULE)
0324 
0325 /**
0326  * module_rpmsg_driver() - Helper macro for registering an rpmsg driver
0327  * @__rpmsg_driver: rpmsg_driver struct
0328  *
0329  * Helper macro for rpmsg drivers which do not do anything special in module
0330  * init/exit. This eliminates a lot of boilerplate.  Each module may only
0331  * use this macro once, and calling it replaces module_init() and module_exit()
0332  */
0333 #define module_rpmsg_driver(__rpmsg_driver) \
0334     module_driver(__rpmsg_driver, register_rpmsg_driver, \
0335             unregister_rpmsg_driver)
0336 
0337 #endif /* _LINUX_RPMSG_H */