property.c 7.4 KB

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