Linux 2.6.17.7
[linux/fpc-iii.git] / drivers / eisa / eisa-bus.c
blob6078e2f588172e73aad9ad45ec2a3cfb9eb7e97d
1 /*
2 * EISA bus support functions for sysfs.
4 * (C) 2002, 2003 Marc Zyngier <maz@wild-wind.fr.eu.org>
6 * This code is released under the GPL version 2.
7 */
9 #include <linux/kernel.h>
10 #include <linux/device.h>
11 #include <linux/eisa.h>
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/ioport.h>
17 #include <asm/io.h>
19 #define SLOT_ADDRESS(r,n) (r->bus_base_addr + (0x1000 * n))
21 #define EISA_DEVINFO(i,s) { .id = { .sig = i }, .name = s }
23 struct eisa_device_info {
24 struct eisa_device_id id;
25 char name[DEVICE_NAME_SIZE];
28 #ifdef CONFIG_EISA_NAMES
29 static struct eisa_device_info __initdata eisa_table[] = {
30 #include "devlist.h"
32 #define EISA_INFOS (sizeof (eisa_table) / (sizeof (struct eisa_device_info)))
33 #endif
35 #define EISA_MAX_FORCED_DEV 16
37 static int enable_dev[EISA_MAX_FORCED_DEV];
38 static int enable_dev_count;
39 static int disable_dev[EISA_MAX_FORCED_DEV];
40 static int disable_dev_count;
42 static int is_forced_dev (int *forced_tab,
43 int forced_count,
44 struct eisa_root_device *root,
45 struct eisa_device *edev)
47 int i, x;
49 for (i = 0; i < forced_count; i++) {
50 x = (root->bus_nr << 8) | edev->slot;
51 if (forced_tab[i] == x)
52 return 1;
55 return 0;
58 static void __init eisa_name_device (struct eisa_device *edev)
60 #ifdef CONFIG_EISA_NAMES
61 int i;
62 for (i = 0; i < EISA_INFOS; i++) {
63 if (!strcmp (edev->id.sig, eisa_table[i].id.sig)) {
64 strlcpy (edev->pretty_name,
65 eisa_table[i].name,
66 DEVICE_NAME_SIZE);
67 return;
71 /* No name was found */
72 sprintf (edev->pretty_name, "EISA device %.7s", edev->id.sig);
73 #endif
76 static char __init *decode_eisa_sig(unsigned long addr)
78 static char sig_str[EISA_SIG_LEN];
79 u8 sig[4];
80 u16 rev;
81 int i;
83 for (i = 0; i < 4; i++) {
84 #ifdef CONFIG_EISA_VLB_PRIMING
86 * This ugly stuff is used to wake up VL-bus cards
87 * (AHA-284x is the only known example), so we can
88 * read the EISA id.
90 * Thankfully, this only exists on x86...
92 outb(0x80 + i, addr);
93 #endif
94 sig[i] = inb (addr + i);
96 if (!i && (sig[0] & 0x80))
97 return NULL;
100 sig_str[0] = ((sig[0] >> 2) & 0x1f) + ('A' - 1);
101 sig_str[1] = (((sig[0] & 3) << 3) | (sig[1] >> 5)) + ('A' - 1);
102 sig_str[2] = (sig[1] & 0x1f) + ('A' - 1);
103 rev = (sig[2] << 8) | sig[3];
104 sprintf(sig_str + 3, "%04X", rev);
106 return sig_str;
109 static int eisa_bus_match (struct device *dev, struct device_driver *drv)
111 struct eisa_device *edev = to_eisa_device (dev);
112 struct eisa_driver *edrv = to_eisa_driver (drv);
113 const struct eisa_device_id *eids = edrv->id_table;
115 if (!eids)
116 return 0;
118 while (strlen (eids->sig)) {
119 if (!strcmp (eids->sig, edev->id.sig) &&
120 edev->state & EISA_CONFIG_ENABLED) {
121 edev->id.driver_data = eids->driver_data;
122 return 1;
125 eids++;
128 return 0;
131 struct bus_type eisa_bus_type = {
132 .name = "eisa",
133 .match = eisa_bus_match,
136 int eisa_driver_register (struct eisa_driver *edrv)
138 edrv->driver.bus = &eisa_bus_type;
139 return driver_register (&edrv->driver);
142 void eisa_driver_unregister (struct eisa_driver *edrv)
144 driver_unregister (&edrv->driver);
147 static ssize_t eisa_show_sig (struct device *dev, struct device_attribute *attr, char *buf)
149 struct eisa_device *edev = to_eisa_device (dev);
150 return sprintf (buf,"%s\n", edev->id.sig);
153 static DEVICE_ATTR(signature, S_IRUGO, eisa_show_sig, NULL);
155 static ssize_t eisa_show_state (struct device *dev, struct device_attribute *attr, char *buf)
157 struct eisa_device *edev = to_eisa_device (dev);
158 return sprintf (buf,"%d\n", edev->state & EISA_CONFIG_ENABLED);
161 static DEVICE_ATTR(enabled, S_IRUGO, eisa_show_state, NULL);
163 static int __init eisa_init_device (struct eisa_root_device *root,
164 struct eisa_device *edev,
165 int slot)
167 char *sig;
168 unsigned long sig_addr;
169 int i;
171 sig_addr = SLOT_ADDRESS (root, slot) + EISA_VENDOR_ID_OFFSET;
173 if (!(sig = decode_eisa_sig (sig_addr)))
174 return -1; /* No EISA device here */
176 memcpy (edev->id.sig, sig, EISA_SIG_LEN);
177 edev->slot = slot;
178 edev->state = inb (SLOT_ADDRESS (root, slot) + EISA_CONFIG_OFFSET) & EISA_CONFIG_ENABLED;
179 edev->base_addr = SLOT_ADDRESS (root, slot);
180 edev->dma_mask = root->dma_mask; /* Default DMA mask */
181 eisa_name_device (edev);
182 edev->dev.parent = root->dev;
183 edev->dev.bus = &eisa_bus_type;
184 edev->dev.dma_mask = &edev->dma_mask;
185 edev->dev.coherent_dma_mask = edev->dma_mask;
186 sprintf (edev->dev.bus_id, "%02X:%02X", root->bus_nr, slot);
188 for (i = 0; i < EISA_MAX_RESOURCES; i++) {
189 #ifdef CONFIG_EISA_NAMES
190 edev->res[i].name = edev->pretty_name;
191 #else
192 edev->res[i].name = edev->id.sig;
193 #endif
196 if (is_forced_dev (enable_dev, enable_dev_count, root, edev))
197 edev->state = EISA_CONFIG_ENABLED | EISA_CONFIG_FORCED;
199 if (is_forced_dev (disable_dev, disable_dev_count, root, edev))
200 edev->state = EISA_CONFIG_FORCED;
202 return 0;
205 static int __init eisa_register_device (struct eisa_device *edev)
207 if (device_register (&edev->dev))
208 return -1;
210 device_create_file (&edev->dev, &dev_attr_signature);
211 device_create_file (&edev->dev, &dev_attr_enabled);
213 return 0;
216 static int __init eisa_request_resources (struct eisa_root_device *root,
217 struct eisa_device *edev,
218 int slot)
220 int i;
222 for (i = 0; i < EISA_MAX_RESOURCES; i++) {
223 /* Don't register resource for slot 0, since this is
224 * very likely to fail... :-( Instead, grab the EISA
225 * id, now we can display something in /proc/ioports.
228 /* Only one region for mainboard */
229 if (!slot && i > 0) {
230 edev->res[i].start = edev->res[i].end = 0;
231 continue;
234 if (slot) {
235 edev->res[i].name = NULL;
236 edev->res[i].start = SLOT_ADDRESS (root, slot) + (i * 0x400);
237 edev->res[i].end = edev->res[i].start + 0xff;
238 edev->res[i].flags = IORESOURCE_IO;
239 } else {
240 edev->res[i].name = NULL;
241 edev->res[i].start = SLOT_ADDRESS (root, slot) + EISA_VENDOR_ID_OFFSET;
242 edev->res[i].end = edev->res[i].start + 3;
243 edev->res[i].flags = IORESOURCE_BUSY;
246 if (request_resource (root->res, &edev->res[i]))
247 goto failed;
250 return 0;
252 failed:
253 while (--i >= 0)
254 release_resource (&edev->res[i]);
256 return -1;
259 static void __init eisa_release_resources (struct eisa_device *edev)
261 int i;
263 for (i = 0; i < EISA_MAX_RESOURCES; i++)
264 if (edev->res[i].start || edev->res[i].end)
265 release_resource (&edev->res[i]);
268 static int __init eisa_probe (struct eisa_root_device *root)
270 int i, c;
271 struct eisa_device *edev;
273 printk (KERN_INFO "EISA: Probing bus %d at %s\n",
274 root->bus_nr, root->dev->bus_id);
276 /* First try to get hold of slot 0. If there is no device
277 * here, simply fail, unless root->force_probe is set. */
279 if (!(edev = kzalloc (sizeof (*edev), GFP_KERNEL))) {
280 printk (KERN_ERR "EISA: Couldn't allocate mainboard slot\n");
281 return -ENOMEM;
284 if (eisa_request_resources (root, edev, 0)) {
285 printk (KERN_WARNING \
286 "EISA: Cannot allocate resource for mainboard\n");
287 kfree (edev);
288 if (!root->force_probe)
289 return -EBUSY;
290 goto force_probe;
293 if (eisa_init_device (root, edev, 0)) {
294 eisa_release_resources (edev);
295 kfree (edev);
296 if (!root->force_probe)
297 return -ENODEV;
298 goto force_probe;
301 printk (KERN_INFO "EISA: Mainboard %s detected.\n", edev->id.sig);
303 if (eisa_register_device (edev)) {
304 printk (KERN_ERR "EISA: Failed to register %s\n",
305 edev->id.sig);
306 eisa_release_resources (edev);
307 kfree (edev);
310 force_probe:
312 for (c = 0, i = 1; i <= root->slots; i++) {
313 if (!(edev = kzalloc (sizeof (*edev), GFP_KERNEL))) {
314 printk (KERN_ERR "EISA: Out of memory for slot %d\n",
316 continue;
319 if (eisa_request_resources (root, edev, i)) {
320 printk (KERN_WARNING \
321 "Cannot allocate resource for EISA slot %d\n",
323 kfree (edev);
324 continue;
327 if (eisa_init_device (root, edev, i)) {
328 eisa_release_resources (edev);
329 kfree (edev);
330 continue;
333 printk (KERN_INFO "EISA: slot %d : %s detected",
334 i, edev->id.sig);
336 switch (edev->state) {
337 case EISA_CONFIG_ENABLED | EISA_CONFIG_FORCED:
338 printk (" (forced enabled)");
339 break;
341 case EISA_CONFIG_FORCED:
342 printk (" (forced disabled)");
343 break;
345 case 0:
346 printk (" (disabled)");
347 break;
350 printk (".\n");
352 c++;
354 if (eisa_register_device (edev)) {
355 printk (KERN_ERR "EISA: Failed to register %s\n",
356 edev->id.sig);
357 eisa_release_resources (edev);
358 kfree (edev);
362 printk (KERN_INFO "EISA: Detected %d card%s.\n", c, c == 1 ? "" : "s");
364 return 0;
367 static struct resource eisa_root_res = {
368 .name = "EISA root resource",
369 .start = 0,
370 .end = 0xffffffff,
371 .flags = IORESOURCE_IO,
374 static int eisa_bus_count;
376 int __init eisa_root_register (struct eisa_root_device *root)
378 int err;
380 /* Use our own resources to check if this bus base address has
381 * been already registered. This prevents the virtual root
382 * device from registering after the real one has, for
383 * example... */
385 root->eisa_root_res.name = eisa_root_res.name;
386 root->eisa_root_res.start = root->res->start;
387 root->eisa_root_res.end = root->res->end;
388 root->eisa_root_res.flags = IORESOURCE_BUSY;
390 if ((err = request_resource (&eisa_root_res, &root->eisa_root_res)))
391 return err;
393 root->bus_nr = eisa_bus_count++;
395 if ((err = eisa_probe (root)))
396 release_resource (&root->eisa_root_res);
398 return err;
401 static int __init eisa_init (void)
403 int r;
405 if ((r = bus_register (&eisa_bus_type)))
406 return r;
408 printk (KERN_INFO "EISA bus registered\n");
409 return 0;
412 module_param_array(enable_dev, int, &enable_dev_count, 0444);
413 module_param_array(disable_dev, int, &disable_dev_count, 0444);
415 postcore_initcall (eisa_init);
417 int EISA_bus; /* for legacy drivers */
418 EXPORT_SYMBOL (EISA_bus);
419 EXPORT_SYMBOL (eisa_bus_type);
420 EXPORT_SYMBOL (eisa_driver_register);
421 EXPORT_SYMBOL (eisa_driver_unregister);