r8169: PCI ID for Corega Gigabit network card
[linux-2.6/linux-loongson.git] / drivers / ieee1394 / hosts.c
blobd90a3a1898c0d4276bd6a1c7c0688f62a184a46b
1 /*
2 * IEEE 1394 for Linux
4 * Low level (host adapter) management.
6 * Copyright (C) 1999 Andreas E. Bombe
7 * Copyright (C) 1999 Emanuel Pirker
9 * This code is licensed under the GPL. See the file COPYING in the root
10 * directory of the kernel sources for details.
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/pci.h>
19 #include <linux/timer.h>
20 #include <linux/jiffies.h>
21 #include <linux/mutex.h>
23 #include "csr1212.h"
24 #include "ieee1394.h"
25 #include "ieee1394_types.h"
26 #include "hosts.h"
27 #include "ieee1394_core.h"
28 #include "highlevel.h"
29 #include "nodemgr.h"
30 #include "csr.h"
31 #include "config_roms.h"
34 static void delayed_reset_bus(void * __reset_info)
36 struct hpsb_host *host = (struct hpsb_host*)__reset_info;
37 int generation = host->csr.generation + 1;
39 /* The generation field rolls over to 2 rather than 0 per IEEE
40 * 1394a-2000. */
41 if (generation > 0xf || generation < 2)
42 generation = 2;
44 CSR_SET_BUS_INFO_GENERATION(host->csr.rom, generation);
45 if (csr1212_generate_csr_image(host->csr.rom) != CSR1212_SUCCESS) {
46 /* CSR image creation failed, reset generation field and do not
47 * issue a bus reset. */
48 CSR_SET_BUS_INFO_GENERATION(host->csr.rom, host->csr.generation);
49 return;
52 host->csr.generation = generation;
54 host->update_config_rom = 0;
55 if (host->driver->set_hw_config_rom)
56 host->driver->set_hw_config_rom(host, host->csr.rom->bus_info_data);
58 host->csr.gen_timestamp[host->csr.generation] = jiffies;
59 hpsb_reset_bus(host, SHORT_RESET);
62 static int dummy_transmit_packet(struct hpsb_host *h, struct hpsb_packet *p)
64 return 0;
67 static int dummy_devctl(struct hpsb_host *h, enum devctl_cmd c, int arg)
69 return -1;
72 static int dummy_isoctl(struct hpsb_iso *iso, enum isoctl_cmd command, unsigned long arg)
74 return -1;
77 static struct hpsb_host_driver dummy_driver = {
78 .transmit_packet = dummy_transmit_packet,
79 .devctl = dummy_devctl,
80 .isoctl = dummy_isoctl
83 static int alloc_hostnum_cb(struct hpsb_host *host, void *__data)
85 int *hostnum = __data;
87 if (host->id == *hostnum)
88 return 1;
90 return 0;
94 * The pending_packet_queue is special in that it's processed
95 * from hardirq context too (such as hpsb_bus_reset()). Hence
96 * split the lock class from the usual networking skb-head
97 * lock class by using a separate key for it:
99 static struct lock_class_key pending_packet_queue_key;
101 static DEFINE_MUTEX(host_num_alloc);
104 * hpsb_alloc_host - allocate a new host controller.
105 * @drv: the driver that will manage the host controller
106 * @extra: number of extra bytes to allocate for the driver
108 * Allocate a &hpsb_host and initialize the general subsystem specific
109 * fields. If the driver needs to store per host data, as drivers
110 * usually do, the amount of memory required can be specified by the
111 * @extra parameter. Once allocated, the driver should initialize the
112 * driver specific parts, enable the controller and make it available
113 * to the general subsystem using hpsb_add_host().
115 * Return Value: a pointer to the &hpsb_host if successful, %NULL if
116 * no memory was available.
118 struct hpsb_host *hpsb_alloc_host(struct hpsb_host_driver *drv, size_t extra,
119 struct device *dev)
121 struct hpsb_host *h;
122 int i;
123 int hostnum = 0;
125 h = kzalloc(sizeof(*h) + extra, SLAB_KERNEL);
126 if (!h)
127 return NULL;
129 h->csr.rom = csr1212_create_csr(&csr_bus_ops, CSR_BUS_INFO_SIZE, h);
130 if (!h->csr.rom) {
131 kfree(h);
132 return NULL;
135 h->hostdata = h + 1;
136 h->driver = drv;
138 skb_queue_head_init(&h->pending_packet_queue);
139 lockdep_set_class(&h->pending_packet_queue.lock,
140 &pending_packet_queue_key);
141 INIT_LIST_HEAD(&h->addr_space);
143 for (i = 2; i < 16; i++)
144 h->csr.gen_timestamp[i] = jiffies - 60 * HZ;
146 atomic_set(&h->generation, 0);
148 INIT_WORK(&h->delayed_reset, delayed_reset_bus, h);
150 init_timer(&h->timeout);
151 h->timeout.data = (unsigned long) h;
152 h->timeout.function = abort_timedouts;
153 h->timeout_interval = HZ / 20; // 50ms by default
155 h->topology_map = h->csr.topology_map + 3;
156 h->speed_map = (u8 *)(h->csr.speed_map + 2);
158 mutex_lock(&host_num_alloc);
160 while (nodemgr_for_each_host(&hostnum, alloc_hostnum_cb))
161 hostnum++;
163 h->id = hostnum;
165 memcpy(&h->device, &nodemgr_dev_template_host, sizeof(h->device));
166 h->device.parent = dev;
167 snprintf(h->device.bus_id, BUS_ID_SIZE, "fw-host%d", h->id);
169 h->class_dev.dev = &h->device;
170 h->class_dev.class = &hpsb_host_class;
171 snprintf(h->class_dev.class_id, BUS_ID_SIZE, "fw-host%d", h->id);
173 device_register(&h->device);
174 class_device_register(&h->class_dev);
175 get_device(&h->device);
177 mutex_unlock(&host_num_alloc);
179 return h;
182 int hpsb_add_host(struct hpsb_host *host)
184 if (hpsb_default_host_entry(host))
185 return -ENOMEM;
187 hpsb_add_extra_config_roms(host);
189 highlevel_add_host(host);
191 return 0;
194 void hpsb_remove_host(struct hpsb_host *host)
196 host->is_shutdown = 1;
198 cancel_delayed_work(&host->delayed_reset);
199 flush_scheduled_work();
201 host->driver = &dummy_driver;
203 highlevel_remove_host(host);
205 hpsb_remove_extra_config_roms(host);
207 class_device_unregister(&host->class_dev);
208 device_unregister(&host->device);
211 int hpsb_update_config_rom_image(struct hpsb_host *host)
213 unsigned long reset_delay;
214 int next_gen = host->csr.generation + 1;
216 if (!host->update_config_rom)
217 return -EINVAL;
219 if (next_gen > 0xf)
220 next_gen = 2;
222 /* Stop the delayed interrupt, we're about to change the config rom and
223 * it would be a waste to do a bus reset twice. */
224 cancel_delayed_work(&host->delayed_reset);
226 /* IEEE 1394a-2000 prohibits using the same generation number
227 * twice in a 60 second period. */
228 if (time_before(jiffies, host->csr.gen_timestamp[next_gen] + 60 * HZ))
229 /* Wait 60 seconds from the last time this generation number was
230 * used. */
231 reset_delay = (60 * HZ) + host->csr.gen_timestamp[next_gen] - jiffies;
232 else
233 /* Wait 1 second in case some other code wants to change the
234 * Config ROM in the near future. */
235 reset_delay = HZ;
237 PREPARE_WORK(&host->delayed_reset, delayed_reset_bus, host);
238 schedule_delayed_work(&host->delayed_reset, reset_delay);
240 return 0;