1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Remote Processor Procedure Call Driver
4 *
5 * Copyright (C) 2012-2019 Texas Instruments Incorporated - http://www.ti.com/
6 * Erik Rainey <erik.rainey@ti.com>
7 * Suman Anna <s-anna@ti.com>
8 */
10 #include <linux/device.h>
11 #include <linux/slab.h>
12 #include <linux/rpmsg_rpc.h>
14 #include "rpmsg_rpc_internal.h"
16 static ssize_t show_numfuncs(struct device *dev, struct device_attribute *attr,
17 char *buf)
18 {
19 struct rppc_device *rppcdev = dev_get_drvdata(dev);
21 return snprintf(buf, PAGE_SIZE, "%u\n", rppcdev->num_funcs - 1);
22 }
24 static ssize_t set_type_c(char *buf, uint32_t len,
25 struct rppc_param_signature *psig)
26 {
27 char *isptr = (psig->type & RPPC_PARAM_PTR ? " *" : "");
29 switch (psig->type & RPPC_PARAM_MASK) {
30 case RPPC_PARAM_S08:
31 return snprintf(buf, len, "int8_t%s", isptr);
32 case RPPC_PARAM_U08:
33 return snprintf(buf, len, "uint8_t%s", isptr);
34 case RPPC_PARAM_S16:
35 return snprintf(buf, len, "int16_t%s", isptr);
36 case RPPC_PARAM_U16:
37 return snprintf(buf, len, "uint16_t%s", isptr);
38 case RPPC_PARAM_S32:
39 return snprintf(buf, len, "int32_t%s", isptr);
40 case RPPC_PARAM_U32:
41 return snprintf(buf, len, "uint32_t%s", isptr);
42 case RPPC_PARAM_S64:
43 return snprintf(buf, len, "int64_t%s", isptr);
44 case RPPC_PARAM_U64:
45 return snprintf(buf, len, "uint64_t%s", isptr);
46 default:
47 return snprintf(buf, len, "<unknown>%s", isptr);
48 }
49 }
51 static ssize_t set_type_doxy(char *buf, uint32_t len,
52 struct rppc_param_signature *psig)
53 {
54 char *isptr = (psig->type & RPPC_PARAM_PTR ? " *" : "");
55 char dir[10];
57 switch (psig->direction) {
58 case RPPC_PARAMDIR_IN:
59 snprintf(dir, sizeof(dir), "[in]");
60 break;
61 case RPPC_PARAMDIR_OUT:
62 snprintf(dir, sizeof(dir), "[out]");
63 break;
64 case RPPC_PARAMDIR_BI:
65 snprintf(dir, sizeof(dir), "[in,out]");
66 break;
67 default:
68 snprintf(dir, sizeof(dir), "[unknown]");
69 break;
70 }
72 switch (psig->type & RPPC_PARAM_MASK) {
73 case RPPC_PARAM_S08:
74 return snprintf(buf, len, "%s int8_t%s", dir, isptr);
75 case RPPC_PARAM_U08:
76 return snprintf(buf, len, "%s uint8_t%s", dir, isptr);
77 case RPPC_PARAM_S16:
78 return snprintf(buf, len, "%s int16_t%s", dir, isptr);
79 case RPPC_PARAM_U16:
80 return snprintf(buf, len, "%s uint16_t%s", dir, isptr);
81 case RPPC_PARAM_S32:
82 return snprintf(buf, len, "%s int32_t%s", dir, isptr);
83 case RPPC_PARAM_U32:
84 return snprintf(buf, len, "%s uint32_t%s", dir, isptr);
85 case RPPC_PARAM_S64:
86 return snprintf(buf, len, "%s int64_t%s", dir, isptr);
87 case RPPC_PARAM_U64:
88 return snprintf(buf, len, "%s uint64_t%s", dir, isptr);
89 default:
90 return snprintf(buf, len, "%s <unknown>%s", dir, isptr);
91 }
92 }
94 static ssize_t show_c_function(struct device *dev,
95 struct device_attribute *attr, char *buf)
96 {
97 struct rppc_device *rppcdev = dev_get_drvdata(dev);
98 char return_value[11]; /* longest string is strlen("uintXX_t *") = 10 */
99 char parameters[110]; /* longest string * 10 + 9(,) */
100 char comment[300];
101 int p;
102 ssize_t pidx = 0;
103 ssize_t cidx = 0;
104 __u32 index = 0;
106 if (sscanf(attr->attr.name, "c_function%u\n", &index) != 1)
107 return -EIO;
109 memset(return_value, 0, sizeof(return_value));
110 memset(parameters, 0, sizeof(parameters));
112 strcpy(return_value, "void");
113 strcpy(parameters, "void");
114 cidx += snprintf(&comment[cidx], sizeof(comment) - cidx, "/**\n");
115 cidx += snprintf(&comment[cidx], sizeof(comment) - cidx,
116 " * \\fn %s\n", rppcdev->signatures[index].name);
117 for (p = 0; p < rppcdev->signatures[index].num_param; p++) {
118 if (p == 0) {
119 set_type_c(return_value, sizeof(return_value),
120 &rppcdev->signatures[index].params[0]);
121 cidx += snprintf(&comment[cidx], sizeof(comment) - cidx,
122 " * \\return %s\n", return_value);
123 } else {
124 pidx += set_type_c(¶meters[pidx],
125 sizeof(parameters) - pidx,
126 &rppcdev->signatures[index].params[p]);
127 if (p != rppcdev->signatures[index].num_param - 1)
128 parameters[pidx++] = ',';
129 cidx += snprintf(&comment[cidx], sizeof(comment) - cidx,
130 " * \\param ");
131 cidx += set_type_doxy(&comment[cidx],
132 sizeof(comment) - cidx,
133 &rppcdev->signatures[index].params[p]);
134 cidx += snprintf(&comment[cidx], sizeof(comment) - cidx,
135 "\n");
136 }
137 }
138 if (p <= 1)
139 pidx += strlen("void");
140 if (pidx < sizeof(parameters))
141 parameters[pidx] = '\0';
142 cidx += snprintf(&comment[cidx], sizeof(comment) - cidx, " */");
143 return snprintf(buf, PAGE_SIZE, "%s\nextern \"C\" %s %s(%s);\n",
144 comment, return_value, rppcdev->signatures[index].name,
145 parameters);
146 }
148 static struct device_attribute rppc_attrs[] = {
149 __ATTR(num_funcs, 0444, show_numfuncs, NULL),
150 };
152 /**
153 * rppc_create_sysfs - Creates the sysfs entry structures for the instance
154 * @rppcdev: the rppc device (remote server instance) handle
155 *
156 * Helper function to create all the sysfs entries associated with a rppc
157 * device. Each device is associated with a number of remote procedure
158 * functions. The number of such functions and the signatures of those
159 * functions are created in sysfs. Function is invoked after querying
160 * the remote side about the supported functions on this device.
161 *
162 * The entries are split into a set of static entries, which are common
163 * between all rppc devices, and a set of dynamic entries specific to
164 * each rppc device.
165 *
166 * Return: 0 on success, or an appropriate error code otherwise
167 */
168 int rppc_create_sysfs(struct rppc_device *rppcdev)
169 {
170 int i;
171 int ret;
173 rppcdev->sig_attr = kcalloc(rppcdev->num_funcs,
174 sizeof(*rppcdev->sig_attr), GFP_KERNEL);
175 if (!rppcdev->sig_attr)
176 return -ENOMEM;
178 for (i = 0; i < ARRAY_SIZE(rppc_attrs); i++) {
179 ret = device_create_file(rppcdev->dev, &rppc_attrs[i]);
180 if (ret) {
181 dev_err(rppcdev->dev, "failed to create sysfs entry\n");
182 goto clean_static_entries;
183 }
184 }
186 for (i = 1; i < rppcdev->num_funcs; i++) {
187 sysfs_attr_init(&rppcdev->sig_attr[i].attr);
188 rppcdev->sig_attr[i].attr.name =
189 kzalloc(RPPC_MAX_FUNC_NAMELEN, GFP_KERNEL);
190 if (!rppcdev->sig_attr[i].attr.name) {
191 ret = -ENOMEM;
192 goto clean_dynamic_entries;
193 }
194 snprintf((char *)rppcdev->sig_attr[i].attr.name,
195 RPPC_MAX_FUNC_NAMELEN, "c_function%u", i);
196 rppcdev->sig_attr[i].attr.mode = 0444;
197 rppcdev->sig_attr[i].show = show_c_function;
198 rppcdev->sig_attr[i].store = NULL;
200 ret = device_create_file(rppcdev->dev, &rppcdev->sig_attr[i]);
201 if (ret) {
202 dev_err(rppcdev->dev, "failed to create sysfs function entry (%d)\n",
203 ret);
204 goto clean_dynamic_entries;
205 }
206 }
207 return 0;
209 clean_dynamic_entries:
210 while (i-- > 1) {
211 device_remove_file(rppcdev->dev, &rppcdev->sig_attr[i]);
212 kfree(rppcdev->sig_attr[i].attr.name);
213 }
214 i = ARRAY_SIZE(rppc_attrs);
215 clean_static_entries:
216 while (i-- > 0)
217 device_remove_file(rppcdev->dev, &rppc_attrs[i]);
218 kfree(rppcdev->sig_attr);
219 return ret;
220 }
222 /**
223 * rppc_remove_sysfs: Removes the sysfs entry structures for the instance
224 * @rppcdev: the rppc device (remote server instance) handle
225 *
226 * Helper function to remove all the sysfs entries associated with the
227 * rppc device.
228 *
229 * Return: 0 on success, or an appropriate error code otherwise
230 */
231 int rppc_remove_sysfs(struct rppc_device *rppcdev)
232 {
233 int i;
235 if (rppcdev->sig_attr) {
236 for (i = 1; i < rppcdev->num_funcs; i++) {
237 device_remove_file(rppcdev->dev, &rppcdev->sig_attr[i]);
238 kfree(rppcdev->sig_attr[i].attr.name);
239 }
240 }
241 kfree(rppcdev->sig_attr);
243 for (i = 0; i < ARRAY_SIZE(rppc_attrs); i++)
244 device_remove_file(rppcdev->dev, &rppc_attrs[i]);
246 return 0;
247 }