ieee1394: stricter error checks in csr1212
[linux-2.6] / drivers / ieee1394 / config_roms.c
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
12 #include <linux/types.h>
13
14 #include "csr1212.h"
15 #include "ieee1394.h"
16 #include "ieee1394_types.h"
17 #include "hosts.h"
18 #include "ieee1394_core.h"
19 #include "highlevel.h"
20 #include "csr.h"
21 #include "config_roms.h"
22
23 struct hpsb_config_rom_entry {
24         const char *name;
25
26         /* Base initialization, called at module load */
27         int (*init)(void);
28
29         /* Add entry to specified host */
30         int (*add)(struct hpsb_host *host);
31
32         /* Remove entry from specified host */
33         void (*remove)(struct hpsb_host *host);
34
35         /* Cleanup called at module exit */
36         void (*cleanup)(void);
37
38         /* The flag added to host->config_roms */
39         unsigned int flag;
40 };
41
42 /* The default host entry. This must succeed. */
43 int hpsb_default_host_entry(struct hpsb_host *host)
44 {
45         struct csr1212_keyval *root;
46         struct csr1212_keyval *vend_id = NULL;
47         struct csr1212_keyval *text = NULL;
48         char csr_name[128];
49         int ret;
50
51         sprintf(csr_name, "Linux - %s", host->driver->name);
52         root = host->csr.rom->root_kv;
53
54         vend_id = csr1212_new_immediate(CSR1212_KV_ID_VENDOR, host->csr.guid_hi >> 8);
55         text = csr1212_new_string_descriptor_leaf(csr_name);
56
57         if (!vend_id || !text) {
58                 if (vend_id)
59                         csr1212_release_keyval(vend_id);
60                 if (text)
61                         csr1212_release_keyval(text);
62                 csr1212_destroy_csr(host->csr.rom);
63                 return -ENOMEM;
64         }
65
66         csr1212_associate_keyval(vend_id, text);
67         csr1212_release_keyval(text);
68         ret = csr1212_attach_keyval_to_directory(root, vend_id);
69         csr1212_release_keyval(vend_id);
70         if (ret != CSR1212_SUCCESS) {
71                 csr1212_destroy_csr(host->csr.rom);
72                 return -ENOMEM;
73         }
74
75         host->update_config_rom = 1;
76
77         return 0;
78 }
79
80
81 #ifdef CONFIG_IEEE1394_CONFIG_ROM_IP1394
82 #include "eth1394.h"
83
84 static struct csr1212_keyval *ip1394_ud;
85
86 static int config_rom_ip1394_init(void)
87 {
88         struct csr1212_keyval *spec_id = NULL;
89         struct csr1212_keyval *spec_desc = NULL;
90         struct csr1212_keyval *ver = NULL;
91         struct csr1212_keyval *ver_desc = NULL;
92         int ret = -ENOMEM;
93
94         ip1394_ud = csr1212_new_directory(CSR1212_KV_ID_UNIT);
95
96         spec_id = csr1212_new_immediate(CSR1212_KV_ID_SPECIFIER_ID,
97                                         ETHER1394_GASP_SPECIFIER_ID);
98         spec_desc = csr1212_new_string_descriptor_leaf("IANA");
99         ver = csr1212_new_immediate(CSR1212_KV_ID_VERSION,
100                                     ETHER1394_GASP_VERSION);
101         ver_desc = csr1212_new_string_descriptor_leaf("IPv4");
102
103         if (!ip1394_ud || !spec_id || !spec_desc || !ver || !ver_desc)
104                 goto ip1394_fail;
105
106         csr1212_associate_keyval(spec_id, spec_desc);
107         csr1212_associate_keyval(ver, ver_desc);
108         if (csr1212_attach_keyval_to_directory(ip1394_ud, spec_id)
109                         == CSR1212_SUCCESS &&
110             csr1212_attach_keyval_to_directory(ip1394_ud, ver)
111                         == CSR1212_SUCCESS)
112                 ret = 0;
113
114 ip1394_fail:
115         if (ret && ip1394_ud) {
116                 csr1212_release_keyval(ip1394_ud);
117                 ip1394_ud = NULL;
118         }
119
120         if (spec_id)
121                 csr1212_release_keyval(spec_id);
122         if (spec_desc)
123                 csr1212_release_keyval(spec_desc);
124         if (ver)
125                 csr1212_release_keyval(ver);
126         if (ver_desc)
127                 csr1212_release_keyval(ver_desc);
128
129         return ret;
130 }
131
132 static void config_rom_ip1394_cleanup(void)
133 {
134         if (ip1394_ud) {
135                 csr1212_release_keyval(ip1394_ud);
136                 ip1394_ud = NULL;
137         }
138 }
139
140 static int config_rom_ip1394_add(struct hpsb_host *host)
141 {
142         if (!ip1394_ud)
143                 return -ENODEV;
144
145         if (csr1212_attach_keyval_to_directory(host->csr.rom->root_kv,
146                                                ip1394_ud) != CSR1212_SUCCESS)
147                 return -ENOMEM;
148
149         return 0;
150 }
151
152 static void config_rom_ip1394_remove(struct hpsb_host *host)
153 {
154         csr1212_detach_keyval_from_directory(host->csr.rom->root_kv, ip1394_ud);
155 }
156
157 static struct hpsb_config_rom_entry ip1394_entry = {
158         .name           = "ip1394",
159         .init           = config_rom_ip1394_init,
160         .add            = config_rom_ip1394_add,
161         .remove         = config_rom_ip1394_remove,
162         .cleanup        = config_rom_ip1394_cleanup,
163         .flag           = HPSB_CONFIG_ROM_ENTRY_IP1394,
164 };
165 #endif /* CONFIG_IEEE1394_CONFIG_ROM_IP1394 */
166
167
168 static struct hpsb_config_rom_entry *const config_rom_entries[] = {
169 #ifdef CONFIG_IEEE1394_CONFIG_ROM_IP1394
170         &ip1394_entry,
171 #endif
172         NULL,
173 };
174
175 /* Initialize all config roms */
176 int hpsb_init_config_roms(void)
177 {
178         int i, error = 0;
179
180         for (i = 0; config_rom_entries[i]; i++) {
181                 if (!config_rom_entries[i]->init)
182                         continue;
183
184                 if (config_rom_entries[i]->init()) {
185                         HPSB_ERR("Failed to initialize config rom entry `%s'",
186                                  config_rom_entries[i]->name);
187                         error = -1;
188                 } else
189                         HPSB_DEBUG("Initialized config rom entry `%s'",
190                                    config_rom_entries[i]->name);
191         }
192
193         return error;
194 }
195
196 /* Cleanup all config roms */
197 void hpsb_cleanup_config_roms(void)
198 {
199         int i;
200
201         for (i = 0; config_rom_entries[i]; i++) {
202                 if (config_rom_entries[i]->cleanup)
203                         config_rom_entries[i]->cleanup();
204         }
205 }
206
207 /* Add extra config roms to specified host */
208 int hpsb_add_extra_config_roms(struct hpsb_host *host)
209 {
210         int i, error = 0;
211
212         for (i = 0; config_rom_entries[i]; i++) {
213                 if (config_rom_entries[i]->add(host)) {
214                         HPSB_ERR("fw-host%d: Failed to attach config rom entry `%s'",
215                                  host->id, config_rom_entries[i]->name);
216                         error = -1;
217                 } else {
218                         host->config_roms |= config_rom_entries[i]->flag;
219                         host->update_config_rom = 1;
220                 }
221         }
222
223         return error;
224 }
225
226 /* Remove extra config roms from specified host */
227 void hpsb_remove_extra_config_roms(struct hpsb_host *host)
228 {
229         int i;
230
231         for (i = 0; config_rom_entries[i]; i++) {
232                 if (!(host->config_roms & config_rom_entries[i]->flag))
233                         continue;
234
235                 config_rom_entries[i]->remove(host);
236
237                 host->config_roms &= ~config_rom_entries[i]->flag;
238                 host->update_config_rom = 1;
239         }
240 }