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
23struct eisa_device_info {
24	struct eisa_device_id id;
25	char name[DEVICE_NAME_SIZE];
26};
27
28#ifdef CONFIG_EISA_NAMES
29static 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
37static int enable_dev[EISA_MAX_FORCED_DEV];
38static int enable_dev_count;
39static int disable_dev[EISA_MAX_FORCED_DEV];
40static int disable_dev_count;
41
42static 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
58static 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
76static 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
109static 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
131static int eisa_bus_uevent(struct device *dev, char **envp, int num_envp,
132			   char *buffer, int buffer_size)
133{
134	struct eisa_device *edev = to_eisa_device(dev);
135	int i = 0;
136	int length = 0;
137
138	add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
139		       "MODALIAS=" EISA_DEVICE_MODALIAS_FMT, edev->id.sig);
140	envp[i] = NULL;
141	return 0;
142}
143
144struct bus_type eisa_bus_type = {
145	.name  = "eisa",
146	.match = eisa_bus_match,
147	.uevent = eisa_bus_uevent,
148};
149
150int eisa_driver_register (struct eisa_driver *edrv)
151{
152	edrv->driver.bus = &eisa_bus_type;
153	return driver_register (&edrv->driver);
154}
155
156void eisa_driver_unregister (struct eisa_driver *edrv)
157{
158	driver_unregister (&edrv->driver);
159}
160
161static ssize_t eisa_show_sig (struct device *dev, struct device_attribute *attr, char *buf)
162{
163        struct eisa_device *edev = to_eisa_device (dev);
164        return sprintf (buf,"%s\n", edev->id.sig);
165}
166
167static DEVICE_ATTR(signature, S_IRUGO, eisa_show_sig, NULL);
168
169static ssize_t eisa_show_state (struct device *dev, struct device_attribute *attr, char *buf)
170{
171        struct eisa_device *edev = to_eisa_device (dev);
172        return sprintf (buf,"%d\n", edev->state & EISA_CONFIG_ENABLED);
173}
174
175static DEVICE_ATTR(enabled, S_IRUGO, eisa_show_state, NULL);
176
177static ssize_t eisa_show_modalias (struct device *dev, struct device_attribute *attr, char *buf)
178{
179        struct eisa_device *edev = to_eisa_device (dev);
180        return sprintf (buf, EISA_DEVICE_MODALIAS_FMT "\n", edev->id.sig);
181}
182
183static DEVICE_ATTR(modalias, S_IRUGO, eisa_show_modalias, NULL);
184
185static int __init eisa_init_device (struct eisa_root_device *root,
186				    struct eisa_device *edev,
187				    int slot)
188{
189	char *sig;
190        unsigned long sig_addr;
191	int i;
192
193	sig_addr = SLOT_ADDRESS (root, slot) + EISA_VENDOR_ID_OFFSET;
194
195	if (!(sig = decode_eisa_sig (sig_addr)))
196		return -1;	/* No EISA device here */
197
198	memcpy (edev->id.sig, sig, EISA_SIG_LEN);
199	edev->slot = slot;
200	edev->state = inb (SLOT_ADDRESS (root, slot) + EISA_CONFIG_OFFSET) & EISA_CONFIG_ENABLED;
201	edev->base_addr = SLOT_ADDRESS (root, slot);
202	edev->dma_mask = root->dma_mask; /* Default DMA mask */
203	eisa_name_device (edev);
204	edev->dev.parent = root->dev;
205	edev->dev.bus = &eisa_bus_type;
206	edev->dev.dma_mask = &edev->dma_mask;
207	edev->dev.coherent_dma_mask = edev->dma_mask;
208	sprintf (edev->dev.bus_id, "%02X:%02X", root->bus_nr, slot);
209
210	for (i = 0; i < EISA_MAX_RESOURCES; i++) {
211#ifdef CONFIG_EISA_NAMES
212		edev->res[i].name = edev->pretty_name;
213#else
214		edev->res[i].name = edev->id.sig;
215#endif
216	}
217
218	if (is_forced_dev (enable_dev, enable_dev_count, root, edev))
219		edev->state = EISA_CONFIG_ENABLED | EISA_CONFIG_FORCED;
220
221	if (is_forced_dev (disable_dev, disable_dev_count, root, edev))
222		edev->state = EISA_CONFIG_FORCED;
223
224	return 0;
225}
226
227static int __init eisa_register_device (struct eisa_device *edev)
228{
229	int rc = device_register (&edev->dev);
230	if (rc)
231		return rc;
232
233	rc = device_create_file (&edev->dev, &dev_attr_signature);
234	if (rc) goto err_devreg;
235	rc = device_create_file (&edev->dev, &dev_attr_enabled);
236	if (rc) goto err_sig;
237	rc = device_create_file (&edev->dev, &dev_attr_modalias);
238	if (rc) goto err_enab;
239
240	return 0;
241
242err_enab:
243	device_remove_file (&edev->dev, &dev_attr_enabled);
244err_sig:
245	device_remove_file (&edev->dev, &dev_attr_signature);
246err_devreg:
247	device_unregister(&edev->dev);
248	return rc;
249}
250
251static int __init eisa_request_resources (struct eisa_root_device *root,
252					  struct eisa_device *edev,
253					  int slot)
254{
255	int i;
256
257	for (i = 0; i < EISA_MAX_RESOURCES; i++) {
258		/* Don't register resource for slot 0, since this is
259		 * very likely to fail... :-( Instead, grab the EISA
260		 * id, now we can display something in /proc/ioports.
261		 */
262
263		/* Only one region for mainboard */
264		if (!slot && i > 0) {
265			edev->res[i].start = edev->res[i].end = 0;
266			continue;
267		}
268
269		if (slot) {
270			edev->res[i].name  = NULL;
271			edev->res[i].start = SLOT_ADDRESS (root, slot) + (i * 0x400);
272			edev->res[i].end   = edev->res[i].start + 0xff;
273			edev->res[i].flags = IORESOURCE_IO;
274		} else {
275			edev->res[i].name  = NULL;
276			edev->res[i].start = SLOT_ADDRESS (root, slot) + EISA_VENDOR_ID_OFFSET;
277			edev->res[i].end   = edev->res[i].start + 3;
278			edev->res[i].flags = IORESOURCE_BUSY;
279		}
280
281		if (request_resource (root->res, &edev->res[i]))
282			goto failed;
283	}
284
285	return 0;
286
287 failed:
288	while (--i >= 0)
289		release_resource (&edev->res[i]);
290
291	return -1;
292}
293
294static void __init eisa_release_resources (struct eisa_device *edev)
295{
296	int i;
297
298	for (i = 0; i < EISA_MAX_RESOURCES; i++)
299		if (edev->res[i].start || edev->res[i].end)
300			release_resource (&edev->res[i]);
301}
302
303static int __init eisa_probe (struct eisa_root_device *root)
304{
305        int i, c;
306	struct eisa_device *edev;
307
308        printk (KERN_INFO "EISA: Probing bus %d at %s\n",
309		root->bus_nr, root->dev->bus_id);
310
311	/* First try to get hold of slot 0. If there is no device
312	 * here, simply fail, unless root->force_probe is set. */
313
314	if (!(edev = kzalloc (sizeof (*edev), GFP_KERNEL))) {
315		printk (KERN_ERR "EISA: Couldn't allocate mainboard slot\n");
316		return -ENOMEM;
317	}
318
319	if (eisa_request_resources (root, edev, 0)) {
320		printk (KERN_WARNING \
321			"EISA: Cannot allocate resource for mainboard\n");
322		kfree (edev);
323		if (!root->force_probe)
324			return -EBUSY;
325		goto force_probe;
326	}
327
328	if (eisa_init_device (root, edev, 0)) {
329		eisa_release_resources (edev);
330		kfree (edev);
331		if (!root->force_probe)
332			return -ENODEV;
333		goto force_probe;
334	}
335
336	printk (KERN_INFO "EISA: Mainboard %s detected.\n", edev->id.sig);
337
338	if (eisa_register_device (edev)) {
339		printk (KERN_ERR "EISA: Failed to register %s\n",
340			edev->id.sig);
341		eisa_release_resources (edev);
342		kfree (edev);
343	}
344
345 force_probe:
346
347        for (c = 0, i = 1; i <= root->slots; i++) {
348		if (!(edev = kzalloc (sizeof (*edev), GFP_KERNEL))) {
349			printk (KERN_ERR "EISA: Out of memory for slot %d\n",
350				i);
351			continue;
352		}
353
354		if (eisa_request_resources (root, edev, i)) {
355			printk (KERN_WARNING \
356				"Cannot allocate resource for EISA slot %d\n",
357				i);
358			kfree (edev);
359			continue;
360		}
361
362                if (eisa_init_device (root, edev, i)) {
363			eisa_release_resources (edev);
364			kfree (edev);
365			continue;
366		}
367
368		printk (KERN_INFO "EISA: slot %d : %s detected",
369			i, edev->id.sig);
370
371		switch (edev->state) {
372		case EISA_CONFIG_ENABLED | EISA_CONFIG_FORCED:
373			printk (" (forced enabled)");
374			break;
375
376		case EISA_CONFIG_FORCED:
377			printk (" (forced disabled)");
378			break;
379
380		case 0:
381			printk (" (disabled)");
382			break;
383		}
384
385		printk (".\n");
386
387		c++;
388
389		if (eisa_register_device (edev)) {
390			printk (KERN_ERR "EISA: Failed to register %s\n",
391				edev->id.sig);
392			eisa_release_resources (edev);
393			kfree (edev);
394		}
395        }
396
397        printk (KERN_INFO "EISA: Detected %d card%s.\n", c, c == 1 ? "" : "s");
398
399	return 0;
400}
401
402static struct resource eisa_root_res = {
403	.name  = "EISA root resource",
404	.start = 0,
405	.end   = 0xffffffff,
406	.flags = IORESOURCE_IO,
407};
408
409static int eisa_bus_count;
410
411int __init eisa_root_register (struct eisa_root_device *root)
412{
413	int err;
414
415	/* Use our own resources to check if this bus base address has
416	 * been already registered. This prevents the virtual root
417	 * device from registering after the real one has, for
418	 * example... */
419
420	root->eisa_root_res.name  = eisa_root_res.name;
421	root->eisa_root_res.start = root->res->start;
422	root->eisa_root_res.end   = root->res->end;
423	root->eisa_root_res.flags = IORESOURCE_BUSY;
424
425	if ((err = request_resource (&eisa_root_res, &root->eisa_root_res)))
426		return err;
427
428	root->bus_nr = eisa_bus_count++;
429
430	if ((err = eisa_probe (root)))
431		release_resource (&root->eisa_root_res);
432
433	return err;
434}
435
436static int __init eisa_init (void)
437{
438	int r;
439
440	if ((r = bus_register (&eisa_bus_type)))
441		return r;
442
443	printk (KERN_INFO "EISA bus registered\n");
444	return 0;
445}
446
447module_param_array(enable_dev, int, &enable_dev_count, 0444);
448module_param_array(disable_dev, int, &disable_dev_count, 0444);
449
450postcore_initcall (eisa_init);
451
452int EISA_bus;		/* for legacy drivers */
453EXPORT_SYMBOL (EISA_bus);
454EXPORT_SYMBOL (eisa_bus_type);
455EXPORT_SYMBOL (eisa_driver_register);
456EXPORT_SYMBOL (eisa_driver_unregister);
457