blob: 7ad6c205f110146ae2084be763bc1f0bd441ac61 [file] [log] [blame]
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001/*
2 * Remote processor messaging
3 *
4 * Copyright (C) 2011 Texas Instruments, Inc.
5 * Copyright (C) 2011 Google, Inc.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 * * Neither the name Texas Instruments nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35#ifndef _LINUX_RPMSG_H
36#define _LINUX_RPMSG_H
37
38#include <linux/types.h>
39#include <linux/device.h>
40#include <linux/mod_devicetable.h>
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +030041#include <linux/kref.h>
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +030042#include <linux/mutex.h>
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020043
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020044#define RPMSG_ADDR_ANY 0xFFFFFFFF
45
Bjorn Andersson8a228ec2016-09-01 15:28:00 -070046struct rpmsg_device;
Bjorn Andersson36b72c72016-09-01 15:27:58 -070047struct rpmsg_endpoint;
48struct rpmsg_device_ops;
Bjorn Andersson8a228ec2016-09-01 15:28:00 -070049struct rpmsg_endpoint_ops;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020050
51/**
Bjorn Andersson2b263d22016-09-01 15:27:56 -070052 * struct rpmsg_channel_info - channel info representation
53 * @name: name of service
54 * @src: local address
55 * @dst: destination address
56 */
57struct rpmsg_channel_info {
58 char name[RPMSG_NAME_SIZE];
59 u32 src;
60 u32 dst;
61};
62
63/**
Bjorn Andersson92e1de52016-09-01 15:27:57 -070064 * rpmsg_device - device that belong to the rpmsg bus
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020065 * @dev: the device struct
66 * @id: device id (used to match between rpmsg drivers and devices)
Bjorn Anderssone9506042016-10-07 21:23:12 -070067 * @driver_override: driver name to force a match
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020068 * @src: local address
69 * @dst: destination address
70 * @ept: the rpmsg endpoint of this channel
71 * @announce: if set, rpmsg will announce the creation/removal of this channel
72 */
Bjorn Andersson92e1de52016-09-01 15:27:57 -070073struct rpmsg_device {
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020074 struct device dev;
75 struct rpmsg_device_id id;
Bjorn Anderssone9506042016-10-07 21:23:12 -070076 char *driver_override;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020077 u32 src;
78 u32 dst;
79 struct rpmsg_endpoint *ept;
80 bool announce;
Bjorn Andersson36b72c72016-09-01 15:27:58 -070081
82 const struct rpmsg_device_ops *ops;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020083};
84
Bjorn Andersson4b83c522016-09-01 15:28:08 -070085typedef int (*rpmsg_rx_cb_t)(struct rpmsg_device *, void *, int, void *, u32);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020086
87/**
88 * struct rpmsg_endpoint - binds a local rpmsg address to its user
89 * @rpdev: rpmsg channel device
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +030090 * @refcount: when this drops to zero, the ept is deallocated
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020091 * @cb: rx callback handler
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +030092 * @cb_lock: must be taken before accessing/changing @cb
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020093 * @addr: local rpmsg address
94 * @priv: private data for the driver's use
95 *
96 * In essence, an rpmsg endpoint represents a listener on the rpmsg bus, as
97 * it binds an rpmsg address with an rx callback handler.
98 *
99 * Simple rpmsg drivers shouldn't use this struct directly, because
100 * things just work: every rpmsg driver provides an rx callback upon
101 * registering to the bus, and that callback is then bound to its rpmsg
102 * address when the driver is probed. When relevant inbound messages arrive
103 * (i.e. messages which their dst address equals to the src address of
104 * the rpmsg channel), the driver's handler is invoked to process it.
105 *
106 * More complicated drivers though, that do need to allocate additional rpmsg
107 * addresses, and bind them to different rx callbacks, must explicitly
108 * create additional endpoints by themselves (see rpmsg_create_ept()).
109 */
110struct rpmsg_endpoint {
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700111 struct rpmsg_device *rpdev;
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300112 struct kref refcount;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200113 rpmsg_rx_cb_t cb;
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300114 struct mutex cb_lock;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200115 u32 addr;
116 void *priv;
Bjorn Andersson8a228ec2016-09-01 15:28:00 -0700117
118 const struct rpmsg_endpoint_ops *ops;
119};
120
121/**
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200122 * struct rpmsg_driver - rpmsg driver struct
123 * @drv: underlying device driver
124 * @id_table: rpmsg ids serviced by this driver
125 * @probe: invoked when a matching rpmsg channel (i.e. device) is found
126 * @remove: invoked when the rpmsg channel is removed
127 * @callback: invoked when an inbound message is received on the channel
128 */
129struct rpmsg_driver {
130 struct device_driver drv;
131 const struct rpmsg_device_id *id_table;
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700132 int (*probe)(struct rpmsg_device *dev);
133 void (*remove)(struct rpmsg_device *dev);
Bjorn Andersson4b83c522016-09-01 15:28:08 -0700134 int (*callback)(struct rpmsg_device *, void *, int, void *, u32);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200135};
136
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700137int register_rpmsg_device(struct rpmsg_device *dev);
138void unregister_rpmsg_device(struct rpmsg_device *dev);
Andrew F. Davisbc3c57c2016-05-04 17:01:36 -0500139int __register_rpmsg_driver(struct rpmsg_driver *drv, struct module *owner);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200140void unregister_rpmsg_driver(struct rpmsg_driver *drv);
141void rpmsg_destroy_ept(struct rpmsg_endpoint *);
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700142struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_device *,
Bjorn Andersson2b263d22016-09-01 15:27:56 -0700143 rpmsg_rx_cb_t cb, void *priv,
144 struct rpmsg_channel_info chinfo);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200145
Andrew F. Davisbc3c57c2016-05-04 17:01:36 -0500146/* use a macro to avoid include chaining to get THIS_MODULE */
147#define register_rpmsg_driver(drv) \
148 __register_rpmsg_driver(drv, THIS_MODULE)
149
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200150/**
Andrew F. Davisf3d9f1c2016-05-04 17:01:38 -0500151 * module_rpmsg_driver() - Helper macro for registering an rpmsg driver
152 * @__rpmsg_driver: rpmsg_driver struct
153 *
154 * Helper macro for rpmsg drivers which do not do anything special in module
155 * init/exit. This eliminates a lot of boilerplate. Each module may only
156 * use this macro once, and calling it replaces module_init() and module_exit()
157 */
158#define module_rpmsg_driver(__rpmsg_driver) \
159 module_driver(__rpmsg_driver, register_rpmsg_driver, \
160 unregister_rpmsg_driver)
161
Bjorn Anderssonc9bd6f42016-09-01 15:28:01 -0700162int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len);
163int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst);
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700164int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
Bjorn Anderssonc9bd6f42016-09-01 15:28:01 -0700165 void *data, int len);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200166
Bjorn Anderssonc9bd6f42016-09-01 15:28:01 -0700167int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len);
168int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst);
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700169int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
Bjorn Anderssonc9bd6f42016-09-01 15:28:01 -0700170 void *data, int len);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200171
172#endif /* _LINUX_RPMSG_H */