1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/module.h>
14#include <linux/types.h>
15#include <linux/list.h>
16#include <linux/init.h>
17#include <linux/slab.h>
18#include <linux/timer.h>
19#include <linux/jiffies.h>
20#include <linux/mutex.h>
21
22#include "csr1212.h"
23#include "ieee1394.h"
24#include "ieee1394_types.h"
25#include "hosts.h"
26#include "ieee1394_core.h"
27#include "highlevel.h"
28#include "nodemgr.h"
29#include "csr.h"
30#include "config_roms.h"
31
32
33static void delayed_reset_bus(struct work_struct *work)
34{
35 struct hpsb_host *host =
36 container_of(work, struct hpsb_host, delayed_reset.work);
37 u8 generation = host->csr.generation + 1;
38
39
40
41 if (generation > 0xf || generation < 2)
42 generation = 2;
43
44 csr_set_bus_info_generation(host->csr.rom, generation);
45 if (csr1212_generate_csr_image(host->csr.rom) != CSR1212_SUCCESS) {
46
47
48 csr_set_bus_info_generation(host->csr.rom,
49 host->csr.generation);
50 return;
51 }
52
53 host->csr.generation = generation;
54
55 host->update_config_rom = 0;
56 if (host->driver->set_hw_config_rom)
57 host->driver->set_hw_config_rom(host,
58 host->csr.rom->bus_info_data);
59
60 host->csr.gen_timestamp[host->csr.generation] = jiffies;
61 hpsb_reset_bus(host, SHORT_RESET);
62}
63
64static int dummy_transmit_packet(struct hpsb_host *h, struct hpsb_packet *p)
65{
66 return 0;
67}
68
69static int dummy_devctl(struct hpsb_host *h, enum devctl_cmd c, int arg)
70{
71 return -1;
72}
73
74static int dummy_isoctl(struct hpsb_iso *iso, enum isoctl_cmd command,
75 unsigned long arg)
76{
77 return -1;
78}
79
80static struct hpsb_host_driver dummy_driver = {
81 .transmit_packet = dummy_transmit_packet,
82 .devctl = dummy_devctl,
83 .isoctl = dummy_isoctl
84};
85
86static int alloc_hostnum_cb(struct hpsb_host *host, void *__data)
87{
88 int *hostnum = __data;
89
90 if (host->id == *hostnum)
91 return 1;
92
93 return 0;
94}
95
96static DEFINE_MUTEX(host_num_alloc);
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113struct hpsb_host *hpsb_alloc_host(struct hpsb_host_driver *drv, size_t extra,
114 struct device *dev)
115{
116 struct hpsb_host *h;
117 int i;
118 int hostnum = 0;
119
120 h = kzalloc(sizeof(*h) + extra, GFP_KERNEL);
121 if (!h)
122 return NULL;
123
124 h->csr.rom = csr1212_create_csr(&csr_bus_ops, CSR_BUS_INFO_SIZE, h);
125 if (!h->csr.rom)
126 goto fail;
127
128 h->hostdata = h + 1;
129 h->driver = drv;
130
131 INIT_LIST_HEAD(&h->pending_packets);
132 INIT_LIST_HEAD(&h->addr_space);
133
134 for (i = 2; i < 16; i++)
135 h->csr.gen_timestamp[i] = jiffies - 60 * HZ;
136
137 atomic_set(&h->generation, 0);
138
139 INIT_DELAYED_WORK(&h->delayed_reset, delayed_reset_bus);
140
141 init_timer(&h->timeout);
142 h->timeout.data = (unsigned long) h;
143 h->timeout.function = abort_timedouts;
144 h->timeout_interval = HZ / 20;
145
146 h->topology_map = h->csr.topology_map + 3;
147 h->speed_map = (u8 *)(h->csr.speed_map + 2);
148
149 mutex_lock(&host_num_alloc);
150 while (nodemgr_for_each_host(&hostnum, alloc_hostnum_cb))
151 hostnum++;
152 mutex_unlock(&host_num_alloc);
153 h->id = hostnum;
154
155 memcpy(&h->device, &nodemgr_dev_template_host, sizeof(h->device));
156 h->device.parent = dev;
157 set_dev_node(&h->device, dev_to_node(dev));
158 dev_set_name(&h->device, "fw-host%d", h->id);
159
160 h->host_dev.parent = &h->device;
161 h->host_dev.class = &hpsb_host_class;
162 dev_set_name(&h->host_dev, "fw-host%d", h->id);
163
164 if (device_register(&h->device))
165 goto fail;
166 if (device_register(&h->host_dev)) {
167 device_unregister(&h->device);
168 goto fail;
169 }
170 get_device(&h->device);
171
172 return h;
173
174fail:
175 kfree(h);
176 return NULL;
177}
178
179int hpsb_add_host(struct hpsb_host *host)
180{
181 if (hpsb_default_host_entry(host))
182 return -ENOMEM;
183
184 highlevel_add_host(host);
185 return 0;
186}
187
188void hpsb_resume_host(struct hpsb_host *host)
189{
190 if (host->driver->set_hw_config_rom)
191 host->driver->set_hw_config_rom(host,
192 host->csr.rom->bus_info_data);
193 host->driver->devctl(host, RESET_BUS, SHORT_RESET);
194}
195
196void hpsb_remove_host(struct hpsb_host *host)
197{
198 host->is_shutdown = 1;
199
200 cancel_delayed_work(&host->delayed_reset);
201 flush_scheduled_work();
202
203 host->driver = &dummy_driver;
204 highlevel_remove_host(host);
205
206 device_unregister(&host->host_dev);
207 device_unregister(&host->device);
208}
209
210
211
212
213
214
215
216
217
218int hpsb_update_config_rom_image(struct hpsb_host *host)
219{
220 unsigned long reset_delay;
221 int next_gen = host->csr.generation + 1;
222
223 if (!host->update_config_rom)
224 return -EINVAL;
225
226 if (next_gen > 0xf)
227 next_gen = 2;
228
229
230
231 cancel_delayed_work(&host->delayed_reset);
232
233
234
235 if (time_before(jiffies, host->csr.gen_timestamp[next_gen] + 60 * HZ))
236
237
238 reset_delay =
239 (60 * HZ) + host->csr.gen_timestamp[next_gen] - jiffies;
240 else
241
242
243 reset_delay = HZ;
244
245 PREPARE_DELAYED_WORK(&host->delayed_reset, delayed_reset_bus);
246 schedule_delayed_work(&host->delayed_reset, reset_delay);
247
248 return 0;
249}
250