property.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #define _GNU_SOURCE
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <strings.h>
  5. #include <stdlib.h>
  6. #include <views/register.h>
  7. #include "pci.h"
  8. #include "bank.h"
  9. #include "view.h"
  10. #include "register.h"
  11. #include "property.h"
  12. #include "tools.h"
  13. #include "error.h"
  14. int pcilib_add_registers_from_properties(pcilib_t *ctx, size_t n, pcilib_view_context_t* const *view_ctx, pcilib_view_description_t* const *v) {
  15. int err;
  16. pcilib_view_t i;
  17. pcilib_register_t pos = 0;
  18. pcilib_register_description_t regs[n];
  19. int access;
  20. pcilib_register_bank_t bank;
  21. bank = pcilib_find_register_bank_by_addr(ctx, pcilib_property_register_bank.addr);
  22. if (bank == PCILIB_REGISTER_BANK_INVALID) {
  23. err = pcilib_add_register_banks(ctx, 0, 1, &pcilib_property_register_bank, &bank);
  24. if (err) {
  25. pcilib_error("Error (%i) adding register bank (%s)", err, pcilib_property_register_bank.name);
  26. return err;
  27. }
  28. }
  29. access = ctx->banks[bank].access / 8;
  30. for (i = 0; i < n; i++) {
  31. if ((v[i]->flags&PCILIB_VIEW_FLAG_REGISTER) == 0) continue;
  32. regs[pos++] = (pcilib_register_description_t){
  33. .addr = view_ctx[i]->view * access,
  34. .bits = 8 * sizeof(pcilib_register_value_t),
  35. .mode = v[i]->mode,
  36. .type = PCILIB_REGISTER_PROPERTY,
  37. .bank = PCILIB_REGISTER_BANK_PROPERTY,
  38. .name = (v[i]->regname?v[i]->regname:v[i]->name),
  39. .description = v[i]->description
  40. };
  41. }
  42. if (pos)
  43. return pcilib_add_registers(ctx, 0, pos, regs, NULL);
  44. return 0;
  45. }
  46. int pcilib_add_properties_from_registers(pcilib_t *ctx, size_t n, const pcilib_register_bank_t *banks, const pcilib_register_description_t *registers) {
  47. int err;
  48. pcilib_register_t i;
  49. pcilib_view_t cur_view = ctx->num_views;
  50. if (!n)
  51. return PCILIB_ERROR_INVALID_ARGUMENT;
  52. for (i = 0; i < n; i++) {
  53. char *view_name;
  54. pcilib_access_mode_t mode = 0;
  55. pcilib_register_view_description_t v = {{0}};
  56. pcilib_register_bank_description_t *b = &ctx->banks[banks[i]];
  57. if (registers[i].type == PCILIB_REGISTER_PROPERTY) continue;
  58. view_name = malloc(strlen(registers[i].name) + strlen(b->name) + 13);
  59. if (!view_name) {
  60. pcilib_clean_views(ctx, cur_view);
  61. return PCILIB_ERROR_MEMORY;
  62. }
  63. sprintf(view_name, "/registers/%s/%s", b->name, registers[i].name);
  64. if ((registers[i].views)&&(registers[i].views[0].view)) {
  65. pcilib_view_t view = pcilib_find_view_by_name(ctx, registers[i].views[0].view);
  66. if (view == PCILIB_VIEW_INVALID) return PCILIB_ERROR_NOTFOUND;
  67. memcpy(&v, ctx->views[view], sizeof(pcilib_view_description_t));
  68. v.view = registers[i].views[0].name;
  69. if (ctx->views[view]->api->read_from_reg) mode |= PCILIB_ACCESS_R;
  70. if (ctx->views[view]->api->write_to_reg) mode |= PCILIB_ACCESS_W;
  71. mode &= ctx->views[view]->mode;
  72. } else {
  73. v.base.type = PCILIB_TYPE_LONG;
  74. mode = PCILIB_ACCESS_RW;
  75. }
  76. v.base.api = &pcilib_register_view_api;
  77. v.base.name = view_name;
  78. v.base.description = registers[i].description;
  79. v.base.mode = registers[i].mode&mode;
  80. v.base.flags |= PCILIB_VIEW_FLAG_PROPERTY;
  81. v.reg = registers[i].name;
  82. v.bank = b->name;
  83. err = pcilib_add_views(ctx, 1, (pcilib_view_description_t*)&v);
  84. if (err) {
  85. free(view_name);
  86. pcilib_clean_views(ctx, cur_view);
  87. return err;
  88. }
  89. /*
  90. pcilib_view_context_t *view_ctx = pcilib_find_view_context_by_name(ctx, v.base.name);
  91. view_ctx->flags |= PCILIB_VIEW_FLAG_PROPERTY;
  92. */
  93. }
  94. return 0;
  95. }
  96. pcilib_property_info_t *pcilib_get_property_list(pcilib_t *ctx, const char *branch, pcilib_list_flags_t flags) {
  97. int err = 0;
  98. size_t pos = 0;
  99. size_t name_offset = 0;
  100. pcilib_view_context_t *view_ctx, *view_tmp;
  101. pcilib_property_info_t *info = (pcilib_property_info_t*)malloc((ctx->num_views + 1) * sizeof(pcilib_property_info_t));
  102. struct dir_hash_s {
  103. char *name;
  104. UT_hash_handle hh;
  105. } *dir_hash = NULL, *dir, *dir_tmp;
  106. if (branch) {
  107. name_offset = strlen(branch);
  108. if (branch[name_offset - 1] != '/') name_offset++;
  109. }
  110. // Find all folders
  111. HASH_ITER(hh, ctx->view_hash, view_ctx, view_tmp) {
  112. const pcilib_view_description_t *v = ctx->views[view_ctx->view];
  113. const char *subname = v->name + name_offset;
  114. const char *suffix;
  115. if (!(v->flags&PCILIB_VIEW_FLAG_PROPERTY)) continue;
  116. if ((branch)&&(strncasecmp(branch, v->name, strlen(branch)))) continue;
  117. suffix = strchr(subname, '/');
  118. if (suffix) {
  119. char *name = strndup(v->name, suffix - v->name);
  120. if (!name) {
  121. err = PCILIB_ERROR_MEMORY;
  122. break;
  123. }
  124. HASH_FIND_STR(dir_hash, name + name_offset, dir);
  125. if (dir) {
  126. free(name);
  127. continue;
  128. }
  129. dir = (struct dir_hash_s*)malloc(sizeof(struct dir_hash_s));
  130. if (!dir) {
  131. err = PCILIB_ERROR_MEMORY;
  132. break;
  133. }
  134. dir->name = name;
  135. HASH_ADD_KEYPTR(hh, dir_hash, dir->name + name_offset, strlen(dir->name + name_offset), dir);
  136. }
  137. }
  138. HASH_ITER(hh, ctx->view_hash, view_ctx, view_tmp) {
  139. const pcilib_view_description_t *v = ctx->views[view_ctx->view];
  140. const char *subname = v->name + name_offset;
  141. if (!(v->flags&PCILIB_VIEW_FLAG_PROPERTY)) continue;
  142. if ((branch)&&(strncasecmp(branch, v->name, strlen(branch)))) continue;
  143. if (!strchr(subname, '/')) {
  144. pcilib_view_context_t *found_view;
  145. char *path = strdup(v->name);
  146. if (!path) {
  147. err = PCILIB_ERROR_MEMORY;
  148. break;
  149. }
  150. char *name = strrchr(v->name, '/');
  151. if (name) name++;
  152. else name = path;
  153. HASH_FIND_STR(dir_hash, name, found_view);
  154. info[pos++] = (pcilib_property_info_t) {
  155. .name = name,
  156. .path = path,
  157. .description = v->description,
  158. .type = v->type,
  159. .mode = v->mode,
  160. .unit = v->unit,
  161. .flags = (found_view?PCILIB_LIST_FLAG_CHILDS:0)
  162. };
  163. if (found_view) HASH_DEL(dir_hash, found_view);
  164. }
  165. }
  166. HASH_ITER(hh, dir_hash, dir, dir_tmp) {
  167. char *name = strrchr(dir->name, '/');
  168. if (name) name++;
  169. else name = dir->name;
  170. info[pos++] = (pcilib_property_info_t) {
  171. .name = name,
  172. .path = dir->name,
  173. .type = PCILIB_TYPE_INVALID,
  174. .flags = PCILIB_LIST_FLAG_CHILDS
  175. };
  176. }
  177. HASH_ITER(hh, dir_hash, dir, dir_tmp) {
  178. HASH_DEL(dir_hash, dir);
  179. free(dir);
  180. }
  181. HASH_CLEAR(hh, dir_hash);
  182. memset(&info[pos], 0, sizeof(pcilib_property_info_t));
  183. if (err) {
  184. pcilib_free_property_info(ctx, info);
  185. return NULL;
  186. }
  187. return info;
  188. }
  189. void pcilib_free_property_info(pcilib_t *ctx, pcilib_property_info_t *info) {
  190. int i;
  191. for (i = 0; info[i].path; i++)
  192. free((char*)info[i].path);
  193. free(info);
  194. }
  195. int pcilib_get_property(pcilib_t *ctx, const char *prop, pcilib_value_t *val) {
  196. return pcilib_read_register_view(ctx, NULL, NULL, prop, val);
  197. }
  198. int pcilib_set_property(pcilib_t *ctx, const char *prop, const pcilib_value_t *val) {
  199. return pcilib_write_register_view(ctx, NULL, NULL, prop, val);
  200. }
  201. int pcilib_get_property_attr(pcilib_t *ctx, const char *prop, const char *attr, pcilib_value_t *val) {
  202. pcilib_view_context_t *view_ctx;
  203. view_ctx = pcilib_find_view_context_by_name(ctx, prop);
  204. if (!view_ctx) {
  205. pcilib_error("The specified property (%s) is not found", prop);
  206. return PCILIB_ERROR_NOTFOUND;
  207. }
  208. if (!view_ctx->xml) return PCILIB_ERROR_NOTFOUND;
  209. return pcilib_get_xml_attr(ctx, view_ctx->xml, attr, val);
  210. }