config_roms.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * IEEE 1394 for Linux
  3. *
  4. * ConfigROM entries
  5. *
  6. * Copyright (C) 2004 Ben Collins
  7. *
  8. * This code is licensed under the GPL. See the file COPYING in the root
  9. * directory of the kernel sources for details.
  10. */
  11. #include <linux/types.h>
  12. #include "csr1212.h"
  13. #include "ieee1394.h"
  14. #include "ieee1394_types.h"
  15. #include "hosts.h"
  16. #include "ieee1394_core.h"
  17. #include "highlevel.h"
  18. #include "csr.h"
  19. #include "config_roms.h"
  20. struct hpsb_config_rom_entry {
  21. const char *name;
  22. /* Base initialization, called at module load */
  23. int (*init)(void);
  24. /* Add entry to specified host */
  25. int (*add)(struct hpsb_host *host);
  26. /* Remove entry from specified host */
  27. void (*remove)(struct hpsb_host *host);
  28. /* Cleanup called at module exit */
  29. void (*cleanup)(void);
  30. /* The flag added to host->config_roms */
  31. unsigned int flag;
  32. };
  33. /* The default host entry. This must succeed. */
  34. int hpsb_default_host_entry(struct hpsb_host *host)
  35. {
  36. struct csr1212_keyval *root;
  37. struct csr1212_keyval *vend_id = NULL;
  38. struct csr1212_keyval *text = NULL;
  39. char csr_name[128];
  40. int ret;
  41. sprintf(csr_name, "Linux - %s", host->driver->name);
  42. root = host->csr.rom->root_kv;
  43. vend_id = csr1212_new_immediate(CSR1212_KV_ID_VENDOR, host->csr.guid_hi >> 8);
  44. text = csr1212_new_string_descriptor_leaf(csr_name);
  45. if (!vend_id || !text) {
  46. if (vend_id)
  47. csr1212_release_keyval(vend_id);
  48. if (text)
  49. csr1212_release_keyval(text);
  50. csr1212_destroy_csr(host->csr.rom);
  51. return -ENOMEM;
  52. }
  53. csr1212_associate_keyval(vend_id, text);
  54. csr1212_release_keyval(text);
  55. ret = csr1212_attach_keyval_to_directory(root, vend_id);
  56. csr1212_release_keyval(vend_id);
  57. if (ret != CSR1212_SUCCESS) {
  58. csr1212_destroy_csr(host->csr.rom);
  59. return -ENOMEM;
  60. }
  61. host->update_config_rom = 1;
  62. return 0;
  63. }
  64. #ifdef CONFIG_IEEE1394_CONFIG_ROM_IP1394
  65. #include "eth1394.h"
  66. static struct csr1212_keyval *ip1394_ud;
  67. static int config_rom_ip1394_init(void)
  68. {
  69. struct csr1212_keyval *spec_id = NULL;
  70. struct csr1212_keyval *spec_desc = NULL;
  71. struct csr1212_keyval *ver = NULL;
  72. struct csr1212_keyval *ver_desc = NULL;
  73. int ret = -ENOMEM;
  74. ip1394_ud = csr1212_new_directory(CSR1212_KV_ID_UNIT);
  75. spec_id = csr1212_new_immediate(CSR1212_KV_ID_SPECIFIER_ID,
  76. ETHER1394_GASP_SPECIFIER_ID);
  77. spec_desc = csr1212_new_string_descriptor_leaf("IANA");
  78. ver = csr1212_new_immediate(CSR1212_KV_ID_VERSION,
  79. ETHER1394_GASP_VERSION);
  80. ver_desc = csr1212_new_string_descriptor_leaf("IPv4");
  81. if (!ip1394_ud || !spec_id || !spec_desc || !ver || !ver_desc)
  82. goto ip1394_fail;
  83. csr1212_associate_keyval(spec_id, spec_desc);
  84. csr1212_associate_keyval(ver, ver_desc);
  85. if (csr1212_attach_keyval_to_directory(ip1394_ud, spec_id)
  86. == CSR1212_SUCCESS &&
  87. csr1212_attach_keyval_to_directory(ip1394_ud, ver)
  88. == CSR1212_SUCCESS)
  89. ret = 0;
  90. ip1394_fail:
  91. if (ret && ip1394_ud) {
  92. csr1212_release_keyval(ip1394_ud);
  93. ip1394_ud = NULL;
  94. }
  95. if (spec_id)
  96. csr1212_release_keyval(spec_id);
  97. if (spec_desc)
  98. csr1212_release_keyval(spec_desc);
  99. if (ver)
  100. csr1212_release_keyval(ver);
  101. if (ver_desc)
  102. csr1212_release_keyval(ver_desc);
  103. return ret;
  104. }
  105. static void config_rom_ip1394_cleanup(void)
  106. {
  107. if (ip1394_ud) {
  108. csr1212_release_keyval(ip1394_ud);
  109. ip1394_ud = NULL;
  110. }
  111. }
  112. static int config_rom_ip1394_add(struct hpsb_host *host)
  113. {
  114. if (!ip1394_ud)
  115. return -ENODEV;
  116. if (csr1212_attach_keyval_to_directory(host->csr.rom->root_kv,
  117. ip1394_ud) != CSR1212_SUCCESS)
  118. return -ENOMEM;
  119. return 0;
  120. }
  121. static void config_rom_ip1394_remove(struct hpsb_host *host)
  122. {
  123. csr1212_detach_keyval_from_directory(host->csr.rom->root_kv, ip1394_ud);
  124. }
  125. static struct hpsb_config_rom_entry ip1394_entry = {
  126. .name = "ip1394",
  127. .init = config_rom_ip1394_init,
  128. .add = config_rom_ip1394_add,
  129. .remove = config_rom_ip1394_remove,
  130. .cleanup = config_rom_ip1394_cleanup,
  131. .flag = HPSB_CONFIG_ROM_ENTRY_IP1394,
  132. };
  133. #endif /* CONFIG_IEEE1394_CONFIG_ROM_IP1394 */
  134. static struct hpsb_config_rom_entry *const config_rom_entries[] = {
  135. #ifdef CONFIG_IEEE1394_CONFIG_ROM_IP1394
  136. &ip1394_entry,
  137. #endif
  138. NULL,
  139. };
  140. /* Initialize all config roms */
  141. int hpsb_init_config_roms(void)
  142. {
  143. int i, error = 0;
  144. for (i = 0; config_rom_entries[i]; i++) {
  145. if (!config_rom_entries[i]->init)
  146. continue;
  147. if (config_rom_entries[i]->init()) {
  148. HPSB_ERR("Failed to initialize config rom entry `%s'",
  149. config_rom_entries[i]->name);
  150. error = -1;
  151. } else
  152. HPSB_DEBUG("Initialized config rom entry `%s'",
  153. config_rom_entries[i]->name);
  154. }
  155. return error;
  156. }
  157. /* Cleanup all config roms */
  158. void hpsb_cleanup_config_roms(void)
  159. {
  160. int i;
  161. for (i = 0; config_rom_entries[i]; i++) {
  162. if (config_rom_entries[i]->cleanup)
  163. config_rom_entries[i]->cleanup();
  164. }
  165. }
  166. /* Add extra config roms to specified host */
  167. int hpsb_add_extra_config_roms(struct hpsb_host *host)
  168. {
  169. int i, error = 0;
  170. for (i = 0; config_rom_entries[i]; i++) {
  171. if (config_rom_entries[i]->add(host)) {
  172. HPSB_ERR("fw-host%d: Failed to attach config rom entry `%s'",
  173. host->id, config_rom_entries[i]->name);
  174. error = -1;
  175. } else {
  176. host->config_roms |= config_rom_entries[i]->flag;
  177. host->update_config_rom = 1;
  178. }
  179. }
  180. return error;
  181. }
  182. /* Remove extra config roms from specified host */
  183. void hpsb_remove_extra_config_roms(struct hpsb_host *host)
  184. {
  185. int i;
  186. for (i = 0; config_rom_entries[i]; i++) {
  187. if (!(host->config_roms & config_rom_entries[i]->flag))
  188. continue;
  189. config_rom_entries[i]->remove(host);
  190. host->config_roms &= ~config_rom_entries[i]->flag;
  191. host->update_config_rom = 1;
  192. }
  193. }