eisa-bus.c 10.7 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * EISA bus support functions for sysfs.
 *
 * (C) 2002, 2003 Marc Zyngier <maz@wild-wind.fr.eu.org>
 *
 * This code is released under the GPL version 2.
 */

#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/eisa.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/ioport.h>
#include <asm/io.h>

#define SLOT_ADDRESS(r,n) (r->bus_base_addr + (0x1000 * n))

#define EISA_DEVINFO(i,s) { .id = { .sig = i }, .name = s }

struct eisa_device_info {
	struct eisa_device_id id;
25
	char name[50];
Linus Torvalds's avatar
Linus Torvalds committed
26 27 28 29 30 31 32 33 34 35 36 37
};

#ifdef CONFIG_EISA_NAMES
static struct eisa_device_info __initdata eisa_table[] = {
#include "devlist.h"
};
#define EISA_INFOS (sizeof (eisa_table) / (sizeof (struct eisa_device_info)))
#endif

#define EISA_MAX_FORCED_DEV 16

static int enable_dev[EISA_MAX_FORCED_DEV];
38
static unsigned int enable_dev_count;
Linus Torvalds's avatar
Linus Torvalds committed
39
static int disable_dev[EISA_MAX_FORCED_DEV];
40
static unsigned int disable_dev_count;
Linus Torvalds's avatar
Linus Torvalds committed
41

42 43 44 45
static int is_forced_dev(int *forced_tab,
			 int forced_count,
			 struct eisa_root_device *root,
			 struct eisa_device *edev)
Linus Torvalds's avatar
Linus Torvalds committed
46 47 48 49 50 51 52 53 54 55 56 57
{
	int i, x;

	for (i = 0; i < forced_count; i++) {
		x = (root->bus_nr << 8) | edev->slot;
		if (forced_tab[i] == x)
			return 1;
	}

	return 0;
}

58
static void __init eisa_name_device(struct eisa_device *edev)
Linus Torvalds's avatar
Linus Torvalds committed
59 60 61 62
{
#ifdef CONFIG_EISA_NAMES
	int i;
	for (i = 0; i < EISA_INFOS; i++) {
63 64 65 66
		if (!strcmp(edev->id.sig, eisa_table[i].id.sig)) {
			strlcpy(edev->pretty_name,
				eisa_table[i].name,
				sizeof(edev->pretty_name));
Linus Torvalds's avatar
Linus Torvalds committed
67 68 69 70 71
			return;
		}
	}

	/* No name was found */
72
	sprintf(edev->pretty_name, "EISA device %.7s", edev->id.sig);
Linus Torvalds's avatar
Linus Torvalds committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
#endif
}

static char __init *decode_eisa_sig(unsigned long addr)
{
        static char sig_str[EISA_SIG_LEN];
	u8 sig[4];
        u16 rev;
	int i;

	for (i = 0; i < 4; i++) {
#ifdef CONFIG_EISA_VLB_PRIMING
		/*
		 * This ugly stuff is used to wake up VL-bus cards
		 * (AHA-284x is the only known example), so we can
		 * read the EISA id.
		 *
		 * Thankfully, this only exists on x86...
		 */
		outb(0x80 + i, addr);
#endif
94
		sig[i] = inb(addr + i);
Linus Torvalds's avatar
Linus Torvalds committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108

		if (!i && (sig[0] & 0x80))
			return NULL;
	}
	
        sig_str[0] = ((sig[0] >> 2) & 0x1f) + ('A' - 1);
        sig_str[1] = (((sig[0] & 3) << 3) | (sig[1] >> 5)) + ('A' - 1);
        sig_str[2] = (sig[1] & 0x1f) + ('A' - 1);
        rev = (sig[2] << 8) | sig[3];
        sprintf(sig_str + 3, "%04X", rev);

        return sig_str;
}

109
static int eisa_bus_match(struct device *dev, struct device_driver *drv)
Linus Torvalds's avatar
Linus Torvalds committed
110
{
111 112
	struct eisa_device *edev = to_eisa_device(dev);
	struct eisa_driver *edrv = to_eisa_driver(drv);
Linus Torvalds's avatar
Linus Torvalds committed
113 114 115 116 117
	const struct eisa_device_id *eids = edrv->id_table;

	if (!eids)
		return 0;

118 119
	while (strlen(eids->sig)) {
		if (!strcmp(eids->sig, edev->id.sig) &&
Linus Torvalds's avatar
Linus Torvalds committed
120 121 122 123 124 125 126 127 128 129 130
		    edev->state & EISA_CONFIG_ENABLED) {
			edev->id.driver_data = eids->driver_data;
			return 1;
		}

		eids++;
	}

	return 0;
}

131
static int eisa_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
132 133 134
{
	struct eisa_device *edev = to_eisa_device(dev);

135
	add_uevent_var(env, "MODALIAS=" EISA_DEVICE_MODALIAS_FMT, edev->id.sig);
136 137 138
	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
139 140 141
struct bus_type eisa_bus_type = {
	.name  = "eisa",
	.match = eisa_bus_match,
142
	.uevent = eisa_bus_uevent,
Linus Torvalds's avatar
Linus Torvalds committed
143
};
144
EXPORT_SYMBOL(eisa_bus_type);
Linus Torvalds's avatar
Linus Torvalds committed
145

146
int eisa_driver_register(struct eisa_driver *edrv)
Linus Torvalds's avatar
Linus Torvalds committed
147 148
{
	edrv->driver.bus = &eisa_bus_type;
149
	return driver_register(&edrv->driver);
Linus Torvalds's avatar
Linus Torvalds committed
150
}
151
EXPORT_SYMBOL(eisa_driver_register);
Linus Torvalds's avatar
Linus Torvalds committed
152

153
void eisa_driver_unregister(struct eisa_driver *edrv)
Linus Torvalds's avatar
Linus Torvalds committed
154
{
155
	driver_unregister(&edrv->driver);
Linus Torvalds's avatar
Linus Torvalds committed
156
}
157
EXPORT_SYMBOL(eisa_driver_unregister);
Linus Torvalds's avatar
Linus Torvalds committed
158

159 160
static ssize_t eisa_show_sig(struct device *dev, struct device_attribute *attr,
			     char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
161
{
162 163
	struct eisa_device *edev = to_eisa_device(dev);
	return sprintf(buf, "%s\n", edev->id.sig);
Linus Torvalds's avatar
Linus Torvalds committed
164 165 166 167
}

static DEVICE_ATTR(signature, S_IRUGO, eisa_show_sig, NULL);

168 169 170
static ssize_t eisa_show_state(struct device *dev,
			       struct device_attribute *attr,
			       char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
171
{
172 173
	struct eisa_device *edev = to_eisa_device(dev);
	return sprintf(buf, "%d\n", edev->state & EISA_CONFIG_ENABLED);
Linus Torvalds's avatar
Linus Torvalds committed
174 175 176 177
}

static DEVICE_ATTR(enabled, S_IRUGO, eisa_show_state, NULL);

178 179 180
static ssize_t eisa_show_modalias(struct device *dev,
				  struct device_attribute *attr,
				  char *buf)
181
{
182 183
	struct eisa_device *edev = to_eisa_device(dev);
	return sprintf(buf, EISA_DEVICE_MODALIAS_FMT "\n", edev->id.sig);
184 185 186 187
}

static DEVICE_ATTR(modalias, S_IRUGO, eisa_show_modalias, NULL);

188 189 190
static int __init eisa_init_device(struct eisa_root_device *root,
				   struct eisa_device *edev,
				   int slot)
Linus Torvalds's avatar
Linus Torvalds committed
191 192
{
	char *sig;
193
	unsigned long sig_addr;
Linus Torvalds's avatar
Linus Torvalds committed
194 195
	int i;

196
	sig_addr = SLOT_ADDRESS(root, slot) + EISA_VENDOR_ID_OFFSET;
Linus Torvalds's avatar
Linus Torvalds committed
197

198 199
	sig = decode_eisa_sig(sig_addr);
	if (!sig)
Linus Torvalds's avatar
Linus Torvalds committed
200 201
		return -1;	/* No EISA device here */
	
202
	memcpy(edev->id.sig, sig, EISA_SIG_LEN);
Linus Torvalds's avatar
Linus Torvalds committed
203
	edev->slot = slot;
204 205 206
	edev->state = inb(SLOT_ADDRESS(root, slot) + EISA_CONFIG_OFFSET)
		      & EISA_CONFIG_ENABLED;
	edev->base_addr = SLOT_ADDRESS(root, slot);
Linus Torvalds's avatar
Linus Torvalds committed
207
	edev->dma_mask = root->dma_mask; /* Default DMA mask */
208
	eisa_name_device(edev);
Linus Torvalds's avatar
Linus Torvalds committed
209 210 211 212
	edev->dev.parent = root->dev;
	edev->dev.bus = &eisa_bus_type;
	edev->dev.dma_mask = &edev->dma_mask;
	edev->dev.coherent_dma_mask = edev->dma_mask;
213
	dev_set_name(&edev->dev, "%02X:%02X", root->bus_nr, slot);
Linus Torvalds's avatar
Linus Torvalds committed
214 215 216 217 218 219 220 221 222

	for (i = 0; i < EISA_MAX_RESOURCES; i++) {
#ifdef CONFIG_EISA_NAMES
		edev->res[i].name = edev->pretty_name;
#else
		edev->res[i].name = edev->id.sig;
#endif
	}

223
	if (is_forced_dev(enable_dev, enable_dev_count, root, edev))
Linus Torvalds's avatar
Linus Torvalds committed
224 225
		edev->state = EISA_CONFIG_ENABLED | EISA_CONFIG_FORCED;
	
226
	if (is_forced_dev(disable_dev, disable_dev_count, root, edev))
Linus Torvalds's avatar
Linus Torvalds committed
227 228 229 230 231
		edev->state = EISA_CONFIG_FORCED;

	return 0;
}

232
static int __init eisa_register_device(struct eisa_device *edev)
Linus Torvalds's avatar
Linus Torvalds committed
233
{
234
	int rc = device_register(&edev->dev);
235 236
	if (rc) {
		put_device(&edev->dev);
237
		return rc;
238
	}
Linus Torvalds's avatar
Linus Torvalds committed
239

240 241 242 243 244 245 246 247 248
	rc = device_create_file(&edev->dev, &dev_attr_signature);
	if (rc)
		goto err_devreg;
	rc = device_create_file(&edev->dev, &dev_attr_enabled);
	if (rc)
		goto err_sig;
	rc = device_create_file(&edev->dev, &dev_attr_modalias);
	if (rc)
		goto err_enab;
Linus Torvalds's avatar
Linus Torvalds committed
249 250

	return 0;
251 252

err_enab:
253
	device_remove_file(&edev->dev, &dev_attr_enabled);
254
err_sig:
255
	device_remove_file(&edev->dev, &dev_attr_signature);
256 257 258
err_devreg:
	device_unregister(&edev->dev);
	return rc;
Linus Torvalds's avatar
Linus Torvalds committed
259 260
}

261 262 263
static int __init eisa_request_resources(struct eisa_root_device *root,
					 struct eisa_device *edev,
					 int slot)
Linus Torvalds's avatar
Linus Torvalds committed
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
{
	int i;

	for (i = 0; i < EISA_MAX_RESOURCES; i++) {
		/* Don't register resource for slot 0, since this is
		 * very likely to fail... :-( Instead, grab the EISA
		 * id, now we can display something in /proc/ioports.
		 */

		/* Only one region for mainboard */
		if (!slot && i > 0) {
			edev->res[i].start = edev->res[i].end = 0;
			continue;
		}
		
		if (slot) {
280
			edev->res[i].name  = NULL;
281 282
			edev->res[i].start = SLOT_ADDRESS(root, slot)
					     + (i * 0x400);
Linus Torvalds's avatar
Linus Torvalds committed
283 284 285
			edev->res[i].end   = edev->res[i].start + 0xff;
			edev->res[i].flags = IORESOURCE_IO;
		} else {
286
			edev->res[i].name  = NULL;
287 288
			edev->res[i].start = SLOT_ADDRESS(root, slot)
					     + EISA_VENDOR_ID_OFFSET;
Linus Torvalds's avatar
Linus Torvalds committed
289
			edev->res[i].end   = edev->res[i].start + 3;
290
			edev->res[i].flags = IORESOURCE_IO | IORESOURCE_BUSY;
Linus Torvalds's avatar
Linus Torvalds committed
291 292
		}

293
		if (request_resource(root->res, &edev->res[i]))
Linus Torvalds's avatar
Linus Torvalds committed
294 295 296 297 298 299 300
			goto failed;
	}

	return 0;
	
 failed:
	while (--i >= 0)
301
		release_resource(&edev->res[i]);
Linus Torvalds's avatar
Linus Torvalds committed
302 303 304 305

	return -1;
}

306
static void __init eisa_release_resources(struct eisa_device *edev)
Linus Torvalds's avatar
Linus Torvalds committed
307 308 309 310 311
{
	int i;

	for (i = 0; i < EISA_MAX_RESOURCES; i++)
		if (edev->res[i].start || edev->res[i].end)
312
			release_resource(&edev->res[i]);
Linus Torvalds's avatar
Linus Torvalds committed
313 314
}

315
static int __init eisa_probe(struct eisa_root_device *root)
Linus Torvalds's avatar
Linus Torvalds committed
316 317 318
{
        int i, c;
	struct eisa_device *edev;
319
	char *enabled_str;
Linus Torvalds's avatar
Linus Torvalds committed
320

321
	dev_info(root->dev, "Probing EISA bus %d\n", root->bus_nr);
Linus Torvalds's avatar
Linus Torvalds committed
322 323 324 325

	/* First try to get hold of slot 0. If there is no device
	 * here, simply fail, unless root->force_probe is set. */
	
326 327
	edev = kzalloc(sizeof(*edev), GFP_KERNEL);
	if (!edev) {
328
		dev_err(root->dev, "EISA: Couldn't allocate mainboard slot\n");
Linus Torvalds's avatar
Linus Torvalds committed
329 330 331
		return -ENOMEM;
	}
		
332 333 334
	if (eisa_request_resources(root, edev, 0)) {
		dev_warn(root->dev,
		         "EISA: Cannot allocate resource for mainboard\n");
335
		kfree(edev);
Linus Torvalds's avatar
Linus Torvalds committed
336
		if (!root->force_probe)
337
			return -EBUSY;
Linus Torvalds's avatar
Linus Torvalds committed
338 339 340
		goto force_probe;
	}

341 342
	if (eisa_init_device(root, edev, 0)) {
		eisa_release_resources(edev);
343
		kfree(edev);
Linus Torvalds's avatar
Linus Torvalds committed
344
		if (!root->force_probe)
345
			return -ENODEV;
Linus Torvalds's avatar
Linus Torvalds committed
346 347 348
		goto force_probe;
	}

349
	dev_info(&edev->dev, "EISA: Mainboard %s detected\n", edev->id.sig);
Linus Torvalds's avatar
Linus Torvalds committed
350

351
	if (eisa_register_device(edev)) {
352 353
		dev_err(&edev->dev, "EISA: Failed to register %s\n",
		        edev->id.sig);
354 355
		eisa_release_resources(edev);
		kfree(edev);
Linus Torvalds's avatar
Linus Torvalds committed
356 357 358 359 360
	}
	
 force_probe:
	
        for (c = 0, i = 1; i <= root->slots; i++) {
361 362
		edev = kzalloc(sizeof(*edev), GFP_KERNEL);
		if (!edev) {
363 364
			dev_err(root->dev, "EISA: Out of memory for slot %d\n",
				i);
Linus Torvalds's avatar
Linus Torvalds committed
365 366 367
			continue;
		}

368 369 370 371
		if (eisa_request_resources(root, edev, i)) {
			dev_warn(root->dev,
			         "Cannot allocate resource for EISA slot %d\n",
			         i);
372
			kfree(edev);
Linus Torvalds's avatar
Linus Torvalds committed
373 374
			continue;
		}
375

376 377 378 379 380 381
		if (eisa_init_device(root, edev, i)) {
			eisa_release_resources(edev);
			kfree(edev);
			continue;
		}

382 383 384 385 386 387 388 389 390 391 392
		if (edev->state == (EISA_CONFIG_ENABLED | EISA_CONFIG_FORCED))
			enabled_str = " (forced enabled)";
		else if (edev->state == EISA_CONFIG_FORCED)
			enabled_str = " (forced disabled)";
		else if (edev->state == 0)
			enabled_str = " (disabled)";
		else
			enabled_str = "";

		dev_info(&edev->dev, "EISA: slot %d: %s detected%s\n", i,
			 edev->id.sig, enabled_str);
Linus Torvalds's avatar
Linus Torvalds committed
393 394 395

		c++;

396
		if (eisa_register_device(edev)) {
397 398
			dev_err(&edev->dev, "EISA: Failed to register %s\n",
			        edev->id.sig);
399 400
			eisa_release_resources(edev);
			kfree(edev);
Linus Torvalds's avatar
Linus Torvalds committed
401 402 403
		}
        }

404
	dev_info(root->dev, "EISA: Detected %d card%s\n", c, c == 1 ? "" : "s");
Linus Torvalds's avatar
Linus Torvalds committed
405 406 407 408 409 410 411 412 413 414 415 416
	return 0;
}

static struct resource eisa_root_res = {
	.name  = "EISA root resource",
	.start = 0,
	.end   = 0xffffffff,
	.flags = IORESOURCE_IO,
};

static int eisa_bus_count;

417
int __init eisa_root_register(struct eisa_root_device *root)
Linus Torvalds's avatar
Linus Torvalds committed
418 419 420 421 422 423 424 425 426 427 428 429 430
{
	int err;

	/* Use our own resources to check if this bus base address has
	 * been already registered. This prevents the virtual root
	 * device from registering after the real one has, for
	 * example... */
	
	root->eisa_root_res.name  = eisa_root_res.name;
	root->eisa_root_res.start = root->res->start;
	root->eisa_root_res.end   = root->res->end;
	root->eisa_root_res.flags = IORESOURCE_BUSY;

431 432
	err = request_resource(&eisa_root_res, &root->eisa_root_res);
	if (err)
Linus Torvalds's avatar
Linus Torvalds committed
433 434 435 436
		return err;
	
	root->bus_nr = eisa_bus_count++;

437 438 439
	err = eisa_probe(root);
	if (err)
		release_resource(&root->eisa_root_res);
Linus Torvalds's avatar
Linus Torvalds committed
440 441 442 443

	return err;
}

444
static int __init eisa_init(void)
Linus Torvalds's avatar
Linus Torvalds committed
445 446 447
{
	int r;
	
448 449
	r = bus_register(&eisa_bus_type);
	if (r)
Linus Torvalds's avatar
Linus Torvalds committed
450 451
		return r;

452
	printk(KERN_INFO "EISA bus registered\n");
Linus Torvalds's avatar
Linus Torvalds committed
453 454 455 456 457 458
	return 0;
}

module_param_array(enable_dev, int, &enable_dev_count, 0444);
module_param_array(disable_dev, int, &disable_dev_count, 0444);

459
postcore_initcall(eisa_init);
Linus Torvalds's avatar
Linus Torvalds committed
460 461

int EISA_bus;		/* for legacy drivers */
462
EXPORT_SYMBOL(EISA_bus);