4196137e66dea5b9e191b86b6988766985a040ba
[linux-2.6.git] / drivers / eisa / eisa-bus.c
1 /*
2  * EISA bus support functions for sysfs.
3  *
4  * (C) 2002, 2003 Marc Zyngier <maz@wild-wind.fr.eu.org>
5  *
6  * This code is released under the GPL version 2.
7  */
8
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>
18
19 #define SLOT_ADDRESS(r,n) (r->bus_base_addr + (0x1000 * n))
20
21 #define EISA_DEVINFO(i,s) { .id = { .sig = i }, .name = s }
22
23 struct eisa_device_info {
24         struct eisa_device_id id;
25         char name[DEVICE_NAME_SIZE];
26 };
27
28 #ifdef CONFIG_EISA_NAMES
29 static struct eisa_device_info __initdata eisa_table[] = {
30 #include "devlist.h"
31 };
32 #define EISA_INFOS (sizeof (eisa_table) / (sizeof (struct eisa_device_info)))
33 #endif
34
35 #define EISA_MAX_FORCED_DEV 16
36
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;
41
42 static int is_forced_dev (int *forced_tab,
43                           int forced_count,
44                           struct eisa_root_device *root,
45                           struct eisa_device *edev)
46 {
47         int i, x;
48
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;
53         }
54
55         return 0;
56 }
57
58 static void __init eisa_name_device (struct eisa_device *edev)
59 {
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;
68                 }
69         }
70
71         /* No name was found */
72         sprintf (edev->pretty_name, "EISA device %.7s", edev->id.sig);
73 #endif
74 }
75
76 static char __init *decode_eisa_sig(unsigned long addr)
77 {
78         static char sig_str[EISA_SIG_LEN];
79         u8 sig[4];
80         u16 rev;
81         int i;
82
83         for (i = 0; i < 4; i++) {
84 #ifdef CONFIG_EISA_VLB_PRIMING
85                 /*
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.
89                  *
90                  * Thankfully, this only exists on x86...
91                  */
92                 outb(0x80 + i, addr);
93 #endif
94                 sig[i] = inb (addr + i);
95
96                 if (!i && (sig[0] & 0x80))
97                         return NULL;
98         }
99         
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);
105
106         return sig_str;
107 }
108
109 static int eisa_bus_match (struct device *dev, struct device_driver *drv)
110 {
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;
114
115         if (!eids)
116                 return 0;
117
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;
123                 }
124
125                 eids++;
126         }
127
128         return 0;
129 }
130
131 struct bus_type eisa_bus_type = {
132         .name  = "eisa",
133         .match = eisa_bus_match,
134 };
135
136 int eisa_driver_register (struct eisa_driver *edrv)
137 {
138         int r;
139         
140         edrv->driver.bus = &eisa_bus_type;
141         if ((r = driver_register (&edrv->driver)) < 0)
142                 return r;
143
144         return 0;
145 }
146
147 void eisa_driver_unregister (struct eisa_driver *edrv)
148 {
149         driver_unregister (&edrv->driver);
150 }
151
152 static ssize_t eisa_show_sig (struct device *dev, struct device_attribute *attr, char *buf)
153 {
154         struct eisa_device *edev = to_eisa_device (dev);
155         return sprintf (buf,"%s\n", edev->id.sig);
156 }
157
158 static DEVICE_ATTR(signature, S_IRUGO, eisa_show_sig, NULL);
159
160 static ssize_t eisa_show_state (struct device *dev, struct device_attribute *attr, char *buf)
161 {
162         struct eisa_device *edev = to_eisa_device (dev);
163         return sprintf (buf,"%d\n", edev->state & EISA_CONFIG_ENABLED);
164 }
165
166 static DEVICE_ATTR(enabled, S_IRUGO, eisa_show_state, NULL);
167
168 static int __init eisa_init_device (struct eisa_root_device *root,
169                                     struct eisa_device *edev,
170                                     int slot)
171 {
172         char *sig;
173         unsigned long sig_addr;
174         int i;
175
176         sig_addr = SLOT_ADDRESS (root, slot) + EISA_VENDOR_ID_OFFSET;
177
178         if (!(sig = decode_eisa_sig (sig_addr)))
179                 return -1;      /* No EISA device here */
180         
181         memcpy (edev->id.sig, sig, EISA_SIG_LEN);
182         edev->slot = slot;
183         edev->state = inb (SLOT_ADDRESS (root, slot) + EISA_CONFIG_OFFSET) & EISA_CONFIG_ENABLED;
184         edev->base_addr = SLOT_ADDRESS (root, slot);
185         edev->dma_mask = root->dma_mask; /* Default DMA mask */
186         eisa_name_device (edev);
187         edev->dev.parent = root->dev;
188         edev->dev.bus = &eisa_bus_type;
189         edev->dev.dma_mask = &edev->dma_mask;
190         edev->dev.coherent_dma_mask = edev->dma_mask;
191         sprintf (edev->dev.bus_id, "%02X:%02X", root->bus_nr, slot);
192
193         for (i = 0; i < EISA_MAX_RESOURCES; i++) {
194 #ifdef CONFIG_EISA_NAMES
195                 edev->res[i].name = edev->pretty_name;
196 #else
197                 edev->res[i].name = edev->id.sig;
198 #endif
199         }
200
201         if (is_forced_dev (enable_dev, enable_dev_count, root, edev))
202                 edev->state = EISA_CONFIG_ENABLED | EISA_CONFIG_FORCED;
203         
204         if (is_forced_dev (disable_dev, disable_dev_count, root, edev))
205                 edev->state = EISA_CONFIG_FORCED;
206
207         return 0;
208 }
209
210 static int __init eisa_register_device (struct eisa_device *edev)
211 {
212         if (device_register (&edev->dev))
213                 return -1;
214
215         device_create_file (&edev->dev, &dev_attr_signature);
216         device_create_file (&edev->dev, &dev_attr_enabled);
217
218         return 0;
219 }
220
221 static int __init eisa_request_resources (struct eisa_root_device *root,
222                                           struct eisa_device *edev,
223                                           int slot)
224 {
225         int i;
226
227         for (i = 0; i < EISA_MAX_RESOURCES; i++) {
228                 /* Don't register resource for slot 0, since this is
229                  * very likely to fail... :-( Instead, grab the EISA
230                  * id, now we can display something in /proc/ioports.
231                  */
232
233                 /* Only one region for mainboard */
234                 if (!slot && i > 0) {
235                         edev->res[i].start = edev->res[i].end = 0;
236                         continue;
237                 }
238                 
239                 if (slot) {
240                         edev->res[i].name  = NULL;
241                         edev->res[i].start = SLOT_ADDRESS (root, slot) + (i * 0x400);
242                         edev->res[i].end   = edev->res[i].start + 0xff;
243                         edev->res[i].flags = IORESOURCE_IO;
244                 } else {
245                         edev->res[i].name  = NULL;
246                         edev->res[i].start = SLOT_ADDRESS (root, slot) + EISA_VENDOR_ID_OFFSET;
247                         edev->res[i].end   = edev->res[i].start + 3;
248                         edev->res[i].flags = IORESOURCE_BUSY;
249                 }
250
251                 if (request_resource (root->res, &edev->res[i]))
252                         goto failed;
253         }
254
255         return 0;
256         
257  failed:
258         while (--i >= 0)
259                 release_resource (&edev->res[i]);
260
261         return -1;
262 }
263
264 static void __init eisa_release_resources (struct eisa_device *edev)
265 {
266         int i;
267
268         for (i = 0; i < EISA_MAX_RESOURCES; i++)
269                 if (edev->res[i].start || edev->res[i].end)
270                         release_resource (&edev->res[i]);
271 }
272
273 static int __init eisa_probe (struct eisa_root_device *root)
274 {
275         int i, c;
276         struct eisa_device *edev;
277
278         printk (KERN_INFO "EISA: Probing bus %d at %s\n",
279                 root->bus_nr, root->dev->bus_id);
280
281         /* First try to get hold of slot 0. If there is no device
282          * here, simply fail, unless root->force_probe is set. */
283         
284         if (!(edev = kzalloc (sizeof (*edev), GFP_KERNEL))) {
285                 printk (KERN_ERR "EISA: Couldn't allocate mainboard slot\n");
286                 return -ENOMEM;
287         }
288                 
289         if (eisa_request_resources (root, edev, 0)) {
290                 printk (KERN_WARNING \
291                         "EISA: Cannot allocate resource for mainboard\n");
292                 kfree (edev);
293                 if (!root->force_probe)
294                         return -EBUSY;
295                 goto force_probe;
296         }
297
298         if (eisa_init_device (root, edev, 0)) {
299                 eisa_release_resources (edev);
300                 kfree (edev);
301                 if (!root->force_probe)
302                         return -ENODEV;
303                 goto force_probe;
304         }
305
306         printk (KERN_INFO "EISA: Mainboard %s detected.\n", edev->id.sig);
307
308         if (eisa_register_device (edev)) {
309                 printk (KERN_ERR "EISA: Failed to register %s\n",
310                         edev->id.sig);
311                 eisa_release_resources (edev);
312                 kfree (edev);
313         }
314         
315  force_probe:
316         
317         for (c = 0, i = 1; i <= root->slots; i++) {
318                 if (!(edev = kzalloc (sizeof (*edev), GFP_KERNEL))) {
319                         printk (KERN_ERR "EISA: Out of memory for slot %d\n",
320                                 i);
321                         continue;
322                 }
323
324                 if (eisa_request_resources (root, edev, i)) {
325                         printk (KERN_WARNING \
326                                 "Cannot allocate resource for EISA slot %d\n",
327                                 i);
328                         kfree (edev);
329                         continue;
330                 }
331
332                 if (eisa_init_device (root, edev, i)) {
333                         eisa_release_resources (edev);
334                         kfree (edev);
335                         continue;
336                 }
337                 
338                 printk (KERN_INFO "EISA: slot %d : %s detected",
339                         i, edev->id.sig);
340                         
341                 switch (edev->state) {
342                 case EISA_CONFIG_ENABLED | EISA_CONFIG_FORCED:
343                         printk (" (forced enabled)");
344                         break;
345
346                 case EISA_CONFIG_FORCED:
347                         printk (" (forced disabled)");
348                         break;
349
350                 case 0:
351                         printk (" (disabled)");
352                         break;
353                 }
354                         
355                 printk (".\n");
356
357                 c++;
358
359                 if (eisa_register_device (edev)) {
360                         printk (KERN_ERR "EISA: Failed to register %s\n",
361                                 edev->id.sig);
362                         eisa_release_resources (edev);
363                         kfree (edev);
364                 }
365         }
366
367         printk (KERN_INFO "EISA: Detected %d card%s.\n", c, c == 1 ? "" : "s");
368
369         return 0;
370 }
371
372 static struct resource eisa_root_res = {
373         .name  = "EISA root resource",
374         .start = 0,
375         .end   = 0xffffffff,
376         .flags = IORESOURCE_IO,
377 };
378
379 static int eisa_bus_count;
380
381 int __init eisa_root_register (struct eisa_root_device *root)
382 {
383         int err;
384
385         /* Use our own resources to check if this bus base address has
386          * been already registered. This prevents the virtual root
387          * device from registering after the real one has, for
388          * example... */
389         
390         root->eisa_root_res.name  = eisa_root_res.name;
391         root->eisa_root_res.start = root->res->start;
392         root->eisa_root_res.end   = root->res->end;
393         root->eisa_root_res.flags = IORESOURCE_BUSY;
394
395         if ((err = request_resource (&eisa_root_res, &root->eisa_root_res)))
396                 return err;
397         
398         root->bus_nr = eisa_bus_count++;
399
400         if ((err = eisa_probe (root)))
401                 release_resource (&root->eisa_root_res);
402
403         return err;
404 }
405
406 static int __init eisa_init (void)
407 {
408         int r;
409         
410         if ((r = bus_register (&eisa_bus_type)))
411                 return r;
412
413         printk (KERN_INFO "EISA bus registered\n");
414         return 0;
415 }
416
417 module_param_array(enable_dev, int, &enable_dev_count, 0444);
418 module_param_array(disable_dev, int, &disable_dev_count, 0444);
419
420 postcore_initcall (eisa_init);
421
422 int EISA_bus;           /* for legacy drivers */
423 EXPORT_SYMBOL (EISA_bus);
424 EXPORT_SYMBOL (eisa_bus_type);
425 EXPORT_SYMBOL (eisa_driver_register);
426 EXPORT_SYMBOL (eisa_driver_unregister);