rpmsg_rpc_sysfs.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Remote Processor Procedure Call Driver
  3. *
  4. * Copyright (C) 2012-2017 Texas Instruments Incorporated - http://www.ti.com/
  5. *
  6. * Erik Rainey <erik.rainey@ti.com>
  7. * Suman Anna <s-anna@ti.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * version 2 as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/device.h>
  19. #include <linux/slab.h>
  20. #include <linux/rpmsg_rpc.h>
  21. #include "rpmsg_rpc_internal.h"
  22. static ssize_t show_numfuncs(struct device *dev, struct device_attribute *attr,
  23. char *buf)
  24. {
  25. struct rppc_device *rppcdev = dev_get_drvdata(dev);
  26. return snprintf(buf, PAGE_SIZE, "%u\n", rppcdev->num_funcs - 1);
  27. }
  28. static ssize_t set_type_c(char *buf, uint32_t len,
  29. struct rppc_param_signature *psig)
  30. {
  31. char *isptr = (psig->type & RPPC_PARAM_PTR ? " *" : "");
  32. switch (psig->type & RPPC_PARAM_MASK) {
  33. case RPPC_PARAM_S08:
  34. return snprintf(buf, len, "int8_t%s", isptr);
  35. case RPPC_PARAM_U08:
  36. return snprintf(buf, len, "uint8_t%s", isptr);
  37. case RPPC_PARAM_S16:
  38. return snprintf(buf, len, "int16_t%s", isptr);
  39. case RPPC_PARAM_U16:
  40. return snprintf(buf, len, "uint16_t%s", isptr);
  41. case RPPC_PARAM_S32:
  42. return snprintf(buf, len, "int32_t%s", isptr);
  43. case RPPC_PARAM_U32:
  44. return snprintf(buf, len, "uint32_t%s", isptr);
  45. case RPPC_PARAM_S64:
  46. return snprintf(buf, len, "int64_t%s", isptr);
  47. case RPPC_PARAM_U64:
  48. return snprintf(buf, len, "uint64_t%s", isptr);
  49. default:
  50. return snprintf(buf, len, "<unknown>%s", isptr);
  51. }
  52. }
  53. static ssize_t set_type_doxy(char *buf, uint32_t len,
  54. struct rppc_param_signature *psig)
  55. {
  56. char *isptr = (psig->type & RPPC_PARAM_PTR ? " *" : "");
  57. char dir[10];
  58. switch (psig->direction) {
  59. case RPPC_PARAMDIR_IN:
  60. snprintf(dir, sizeof(dir), "[in]");
  61. break;
  62. case RPPC_PARAMDIR_OUT:
  63. snprintf(dir, sizeof(dir), "[out]");
  64. break;
  65. case RPPC_PARAMDIR_BI:
  66. snprintf(dir, sizeof(dir), "[in,out]");
  67. break;
  68. default:
  69. snprintf(dir, sizeof(dir), "[unknown]");
  70. break;
  71. }
  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. }
  93. static ssize_t show_c_function(struct device *dev,
  94. struct device_attribute *attr, char *buf)
  95. {
  96. struct rppc_device *rppcdev = dev_get_drvdata(dev);
  97. char return_value[11]; /* longest string is strlen("uintXX_t *") = 10 */
  98. char parameters[110]; /* longest string * 10 + 9(,) */
  99. char comment[300];
  100. int p;
  101. ssize_t pidx = 0;
  102. ssize_t cidx = 0;
  103. __u32 index = 0;
  104. if (sscanf(attr->attr.name, "c_function%u\n", &index) != 1)
  105. return -EIO;
  106. memset(return_value, 0, sizeof(return_value));
  107. memset(parameters, 0, sizeof(parameters));
  108. strcpy(return_value, "void");
  109. strcpy(parameters, "void");
  110. cidx += snprintf(&comment[cidx], sizeof(comment) - cidx, "/**\n");
  111. cidx += snprintf(&comment[cidx], sizeof(comment) - cidx,
  112. " * \\fn %s\n", rppcdev->signatures[index].name);
  113. for (p = 0; p < rppcdev->signatures[index].num_param; p++) {
  114. if (p == 0) {
  115. set_type_c(return_value, sizeof(return_value),
  116. &rppcdev->signatures[index].params[0]);
  117. cidx += snprintf(&comment[cidx], sizeof(comment) - cidx,
  118. " * \\return %s\n", return_value);
  119. } else {
  120. pidx += set_type_c(&parameters[pidx],
  121. sizeof(parameters) - pidx,
  122. &rppcdev->signatures[index].params[p]);
  123. if (p != rppcdev->signatures[index].num_param - 1)
  124. parameters[pidx++] = ',';
  125. cidx += snprintf(&comment[cidx], sizeof(comment) - cidx,
  126. " * \\param ");
  127. cidx += set_type_doxy(&comment[cidx],
  128. sizeof(comment) - cidx,
  129. &rppcdev->signatures[index].params[p]);
  130. cidx += snprintf(&comment[cidx], sizeof(comment) - cidx,
  131. "\n");
  132. }
  133. }
  134. if (p <= 1)
  135. pidx += strlen("void");
  136. if (pidx < sizeof(parameters))
  137. parameters[pidx] = '\0';
  138. cidx += snprintf(&comment[cidx], sizeof(comment) - cidx, " */");
  139. return snprintf(buf, PAGE_SIZE, "%s\nextern \"C\" %s %s(%s);\n",
  140. comment, return_value, rppcdev->signatures[index].name,
  141. parameters);
  142. }
  143. static struct device_attribute rppc_attrs[] = {
  144. __ATTR(num_funcs, 0444, show_numfuncs, NULL),
  145. };
  146. /**
  147. * rppc_create_sysfs - Creates the sysfs entry structures for the instance
  148. * @rppcdev: the rppc device (remote server instance) handle
  149. *
  150. * Helper function to create all the sysfs entries associated with a rppc
  151. * device. Each device is associated with a number of remote procedure
  152. * functions. The number of such functions and the signatures of those
  153. * functions are created in sysfs. Function is invoked after querying
  154. * the remote side about the supported functions on this device.
  155. *
  156. * The entries are split into a set of static entries, which are common
  157. * between all rppc devices, and a set of dynamic entries specific to
  158. * each rppc device.
  159. *
  160. * Return: 0 on success, or an appropriate error code otherwise
  161. */
  162. int rppc_create_sysfs(struct rppc_device *rppcdev)
  163. {
  164. int i;
  165. int ret;
  166. rppcdev->sig_attr = kzalloc(sizeof(*rppcdev->sig_attr) *
  167. rppcdev->num_funcs, GFP_KERNEL);
  168. if (!rppcdev->sig_attr)
  169. return -ENOMEM;
  170. for (i = 0; i < ARRAY_SIZE(rppc_attrs); i++) {
  171. ret = device_create_file(rppcdev->dev, &rppc_attrs[i]);
  172. if (ret) {
  173. dev_err(rppcdev->dev, "failed to create sysfs entry\n");
  174. goto clean_static_entries;
  175. }
  176. }
  177. for (i = 1; i < rppcdev->num_funcs; i++) {
  178. sysfs_attr_init(&rppcdev->sig_attr[i].attr);
  179. rppcdev->sig_attr[i].attr.name =
  180. kzalloc(RPPC_MAX_FUNC_NAMELEN, GFP_KERNEL);
  181. if (!rppcdev->sig_attr[i].attr.name) {
  182. ret = -ENOMEM;
  183. goto clean_dynamic_entries;
  184. }
  185. snprintf((char *)rppcdev->sig_attr[i].attr.name,
  186. RPPC_MAX_FUNC_NAMELEN, "c_function%u", i);
  187. rppcdev->sig_attr[i].attr.mode = 0444;
  188. rppcdev->sig_attr[i].show = show_c_function;
  189. rppcdev->sig_attr[i].store = NULL;
  190. ret = device_create_file(rppcdev->dev, &rppcdev->sig_attr[i]);
  191. if (ret) {
  192. dev_err(rppcdev->dev, "failed to create sysfs function entry (%d)\n",
  193. ret);
  194. goto clean_dynamic_entries;
  195. }
  196. }
  197. return 0;
  198. clean_dynamic_entries:
  199. while (i-- > 1) {
  200. device_remove_file(rppcdev->dev, &rppcdev->sig_attr[i]);
  201. kfree(rppcdev->sig_attr[i].attr.name);
  202. }
  203. i = ARRAY_SIZE(rppc_attrs);
  204. clean_static_entries:
  205. while (i-- > 0)
  206. device_remove_file(rppcdev->dev, &rppc_attrs[i]);
  207. kfree(rppcdev->sig_attr);
  208. return ret;
  209. }
  210. /**
  211. * rppc_remove_sysfs: Removes the sysfs entry structures for the instance
  212. * @rppcdev: the rppc device (remote server instance) handle
  213. *
  214. * Helper function to remove all the sysfs entries associated with the
  215. * rppc device.
  216. *
  217. * Return: 0 on success, or an appropriate error code otherwise
  218. */
  219. int rppc_remove_sysfs(struct rppc_device *rppcdev)
  220. {
  221. int i;
  222. if (rppcdev->sig_attr) {
  223. for (i = 1; i < rppcdev->num_funcs; i++) {
  224. device_remove_file(rppcdev->dev, &rppcdev->sig_attr[i]);
  225. kfree(rppcdev->sig_attr[i].attr.name);
  226. }
  227. }
  228. kfree(rppcdev->sig_attr);
  229. for (i = 0; i < ARRAY_SIZE(rppc_attrs); i++)
  230. device_remove_file(rppcdev->dev, &rppc_attrs[i]);
  231. return 0;
  232. }