rsrc_nonstatic.c 25.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * rsrc_nonstatic.c -- Resource management routines for !SS_CAP_STATIC_MAP sockets
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * The initial developer of the original code is David A. Hinds
 * <dahinds@users.sourceforge.net>.  Portions created by David A. Hinds
 * are Copyright (C) 1999 David A. Hinds.  All Rights Reserved.
 *
 * (C) 1999		David A. Hinds
 */

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/slab.h>
#include <linux/ioport.h>
#include <linux/timer.h>
#include <linux/pci.h>
#include <linux/device.h>
D
Dominik Brodowski 已提交
27
#include <linux/io.h>
L
Linus Torvalds 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

#include <asm/irq.h>

#include <pcmcia/cs_types.h>
#include <pcmcia/ss.h>
#include <pcmcia/cs.h>
#include <pcmcia/cistpl.h>
#include "cs_internal.h"

MODULE_AUTHOR("David A. Hinds, Dominik Brodowski");
MODULE_LICENSE("GPL");

/* Parameters that can be set with 'insmod' */

#define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0444)

INT_MODULE_PARM(probe_mem,	1);		/* memory probe? */
#ifdef CONFIG_PCMCIA_PROBE
INT_MODULE_PARM(probe_io,	1);		/* IO port probe? */
INT_MODULE_PARM(mem_limit,	0x10000);
#endif

/* for io_db and mem_db */
struct resource_map {
	u_long			base, num;
	struct resource_map	*next;
};

struct socket_data {
	struct resource_map		mem_db;
	struct resource_map		io_db;
	unsigned int			rsrc_mem_probe;
};

62
static DEFINE_MUTEX(rsrc_mutex);
L
Linus Torvalds 已提交
63 64 65 66 67 68 69 70 71 72 73
#define MEM_PROBE_LOW	(1 << 0)
#define MEM_PROBE_HIGH	(1 << 1)


/*======================================================================

    Linux resource management extensions

======================================================================*/

static struct resource *
74
make_resource(resource_size_t b, resource_size_t n, int flags, const char *name)
L
Linus Torvalds 已提交
75
{
76
	struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
L
Linus Torvalds 已提交
77 78 79 80 81 82 83 84 85 86 87

	if (res) {
		res->name = name;
		res->start = b;
		res->end = b + n - 1;
		res->flags = flags;
	}
	return res;
}

static struct resource *
88 89
claim_region(struct pcmcia_socket *s, resource_size_t base,
		resource_size_t size, int type, char *name)
L
Linus Torvalds 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
{
	struct resource *res, *parent;

	parent = type & IORESOURCE_MEM ? &iomem_resource : &ioport_resource;
	res = make_resource(base, size, type | IORESOURCE_BUSY, name);

	if (res) {
#ifdef CONFIG_PCI
		if (s && s->cb_dev)
			parent = pci_find_parent_resource(s->cb_dev, res);
#endif
		if (!parent || request_resource(parent, res)) {
			kfree(res);
			res = NULL;
		}
	}
	return res;
}

static void free_region(struct resource *res)
{
	if (res) {
		release_resource(res);
		kfree(res);
	}
}

/*======================================================================

    These manage the internal databases of available resources.

======================================================================*/

static int add_interval(struct resource_map *map, u_long base, u_long num)
{
125
	struct resource_map *p, *q;
L
Linus Torvalds 已提交
126

127 128 129 130 131 132 133 134 135 136 137 138 139 140
	for (p = map; ; p = p->next) {
		if ((p != map) && (p->base+p->num-1 >= base))
			return -1;
		if ((p->next == map) || (p->next->base > base+num-1))
			break;
	}
	q = kmalloc(sizeof(struct resource_map), GFP_KERNEL);
	if (!q) {
		printk(KERN_WARNING "out of memory to update resources\n");
		return -ENOMEM;
	}
	q->base = base; q->num = num;
	q->next = p->next; p->next = q;
	return 0;
L
Linus Torvalds 已提交
141 142 143 144 145 146
}

/*====================================================================*/

static int sub_interval(struct resource_map *map, u_long base, u_long num)
{
D
Dominik Brodowski 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
	struct resource_map *p, *q;

	for (p = map; ; p = q) {
		q = p->next;
		if (q == map)
			break;
		if ((q->base+q->num > base) && (base+num > q->base)) {
			if (q->base >= base) {
				if (q->base+q->num <= base+num) {
					/* Delete whole block */
					p->next = q->next;
					kfree(q);
					/* don't advance the pointer yet */
					q = p;
				} else {
					/* Cut off bit from the front */
					q->num = q->base + q->num - base - num;
					q->base = base + num;
				}
			} else if (q->base+q->num <= base+num) {
				/* Cut off bit from the end */
				q->num = base - q->base;
			} else {
				/* Split the block into two pieces */
				p = kmalloc(sizeof(struct resource_map),
					GFP_KERNEL);
				if (!p) {
					printk(KERN_WARNING "out of memory to update resources\n");
					return -ENOMEM;
				}
				p->base = base+num;
				p->num = q->base+q->num - p->base;
				q->num = base - q->base;
				p->next = q->next ; q->next = p;
			}
182
		}
D
Dominik Brodowski 已提交
183 184
	}
	return 0;
L
Linus Torvalds 已提交
185 186 187 188 189 190 191 192 193 194
}

/*======================================================================

    These routines examine a region of IO or memory addresses to
    determine what ranges might be genuinely available.

======================================================================*/

#ifdef CONFIG_PCMCIA_PROBE
195 196
static void do_io_probe(struct pcmcia_socket *s, unsigned int base,
			unsigned int num)
L
Linus Torvalds 已提交
197
{
D
Dominik Brodowski 已提交
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
	struct resource *res;
	struct socket_data *s_data = s->resource_data;
	unsigned int i, j, bad;
	int any;
	u_char *b, hole, most;

	dev_printk(KERN_INFO, &s->dev, "cs: IO port probe %#x-%#x:",
		base, base+num-1);

	/* First, what does a floating port look like? */
	b = kzalloc(256, GFP_KERNEL);
	if (!b) {
		printk("\n");
		dev_printk(KERN_ERR, &s->dev,
			"do_io_probe: unable to kmalloc 256 bytes");
		return;
	}
	for (i = base, most = 0; i < base+num; i += 8) {
		res = claim_region(NULL, i, 8, IORESOURCE_IO, "PCMCIA ioprobe");
		if (!res)
			continue;
		hole = inb(i);
		for (j = 1; j < 8; j++)
			if (inb(i+j) != hole)
				break;
		free_region(res);
		if ((j == 8) && (++b[hole] > b[most]))
			most = hole;
		if (b[most] == 127)
			break;
	}
	kfree(b);

	bad = any = 0;
	for (i = base; i < base+num; i += 8) {
		res = claim_region(NULL, i, 8, IORESOURCE_IO, "PCMCIA ioprobe");
		if (!res)
			continue;
		for (j = 0; j < 8; j++)
			if (inb(i+j) != most)
				break;
		free_region(res);
		if (j < 8) {
			if (!any)
				printk(" excluding");
			if (!bad)
				bad = any = i;
		} else {
			if (bad) {
				sub_interval(&s_data->io_db, bad, i-bad);
				printk(" %#x-%#x", bad, i-1);
				bad = 0;
			}
		}
	}
	if (bad) {
		if ((num > 16) && (bad == base) && (i == base+num)) {
			printk(" nothing: probe failed.\n");
			return;
		} else {
			sub_interval(&s_data->io_db, bad, i-bad);
			printk(" %#x-%#x", bad, i-1);
		}
	}

	printk(any ? "\n" : " clean.\n");
L
Linus Torvalds 已提交
264 265 266 267 268 269 270 271 272 273 274
}
#endif

/*======================================================================

    This is tricky... when we set up CIS memory, we try to validate
    the memory window space allocations.

======================================================================*/

/* Validation function for cards with a valid CIS */
275 276
static int readable(struct pcmcia_socket *s, struct resource *res,
		    unsigned int *count)
L
Linus Torvalds 已提交
277 278 279 280 281 282
{
	int ret = -1;

	s->cis_mem.res = res;
	s->cis_virt = ioremap(res->start, s->map_size);
	if (s->cis_virt) {
283
		ret = pccard_validate_cis(s, count);
L
Linus Torvalds 已提交
284 285 286 287 288 289
		/* invalidate mapping and CIS cache */
		iounmap(s->cis_virt);
		s->cis_virt = NULL;
		destroy_cis_cache(s);
	}
	s->cis_mem.res = NULL;
290
	if ((ret != 0) || (*count == 0))
L
Linus Torvalds 已提交
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
		return 0;
	return 1;
}

/* Validation function for simple memory cards */
static int checksum(struct pcmcia_socket *s, struct resource *res)
{
	pccard_mem_map map;
	int i, a = 0, b = -1, d;
	void __iomem *virt;

	virt = ioremap(res->start, s->map_size);
	if (virt) {
		map.map = 0;
		map.flags = MAP_ACTIVE;
		map.speed = 0;
		map.res = res;
		map.card_start = 0;
		s->ops->set_mem_map(s, &map);

		/* Don't bother checking every word... */
		for (i = 0; i < s->map_size; i += 44) {
			d = readl(virt+i);
			a += d;
			b &= d;
		}

		map.flags = 0;
		s->ops->set_mem_map(s, &map);

		iounmap(virt);
	}

	return (b == -1) ? -1 : (a>>1);
}

static int
cis_readable(struct pcmcia_socket *s, unsigned long base, unsigned long size)
{
	struct resource *res1, *res2;
331
	unsigned int info1, info2;
L
Linus Torvalds 已提交
332 333
	int ret = 0;

D
Dominik Brodowski 已提交
334 335 336
	res1 = claim_region(s, base, size/2, IORESOURCE_MEM, "PCMCIA memprobe");
	res2 = claim_region(s, base + size/2, size/2, IORESOURCE_MEM,
			"PCMCIA memprobe");
L
Linus Torvalds 已提交
337 338 339 340 341 342 343 344 345

	if (res1 && res2) {
		ret = readable(s, res1, &info1);
		ret += readable(s, res2, &info2);
	}

	free_region(res2);
	free_region(res1);

346
	return (ret == 2) && (info1 == info2);
L
Linus Torvalds 已提交
347 348 349 350 351 352 353 354
}

static int
checksum_match(struct pcmcia_socket *s, unsigned long base, unsigned long size)
{
	struct resource *res1, *res2;
	int a = -1, b = -1;

D
Dominik Brodowski 已提交
355 356 357
	res1 = claim_region(s, base, size/2, IORESOURCE_MEM, "PCMCIA memprobe");
	res2 = claim_region(s, base + size/2, size/2, IORESOURCE_MEM,
			"PCMCIA memprobe");
L
Linus Torvalds 已提交
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379

	if (res1 && res2) {
		a = checksum(s, res1);
		b = checksum(s, res2);
	}

	free_region(res2);
	free_region(res1);

	return (a == b) && (a >= 0);
}

/*======================================================================

    The memory probe.  If the memory list includes a 64K-aligned block
    below 1MB, we probe in 64K chunks, and as soon as we accumulate at
    least mem_limit free space, we quit.

======================================================================*/

static int do_mem_probe(u_long base, u_long num, struct pcmcia_socket *s)
{
D
Dominik Brodowski 已提交
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
	struct socket_data *s_data = s->resource_data;
	u_long i, j, bad, fail, step;

	dev_printk(KERN_INFO, &s->dev, "cs: memory probe 0x%06lx-0x%06lx:",
		base, base+num-1);
	bad = fail = 0;
	step = (num < 0x20000) ? 0x2000 : ((num>>4) & ~0x1fff);
	/* don't allow too large steps */
	if (step > 0x800000)
		step = 0x800000;
	/* cis_readable wants to map 2x map_size */
	if (step < 2 * s->map_size)
		step = 2 * s->map_size;
	for (i = j = base; i < base+num; i = j + step) {
		if (!fail) {
			for (j = i; j < base+num; j += step) {
				if (cis_readable(s, j, step))
					break;
			}
			fail = ((i == base) && (j == base+num));
		}
		if (fail) {
			for (j = i; j < base+num; j += 2*step)
				if (checksum_match(s, j, step) &&
					checksum_match(s, j + step, step))
					break;
		}
		if (i != j) {
			if (!bad)
				printk(" excluding");
			printk(" %#05lx-%#05lx", i, j-1);
			sub_interval(&s_data->mem_db, i, j-i);
			bad += j-i;
		}
	}
	printk(bad ? "\n" : " clean.\n");
	return num - bad;
L
Linus Torvalds 已提交
417 418 419 420 421 422
}

#ifdef CONFIG_PCMCIA_PROBE

static u_long inv_probe(struct resource_map *m, struct pcmcia_socket *s)
{
423 424 425 426 427 428 429 430 431 432 433 434 435
	struct socket_data *s_data = s->resource_data;
	u_long ok;
	if (m == &s_data->mem_db)
		return 0;
	ok = inv_probe(m->next, s);
	if (ok) {
		if (m->base >= 0x100000)
			sub_interval(&s_data->mem_db, m->base, m->num);
		return ok;
	}
	if (m->base < 0x100000)
		return 0;
	return do_mem_probe(m->base, m->num, s);
L
Linus Torvalds 已提交
436 437
}

438
static int validate_mem(struct pcmcia_socket *s, unsigned int probe_mask)
L
Linus Torvalds 已提交
439
{
440 441 442 443
	struct resource_map *m, mm;
	static unsigned char order[] = { 0xd0, 0xe0, 0xc0, 0xf0 };
	unsigned long b, i, ok = 0;
	struct socket_data *s_data = s->resource_data;
L
Linus Torvalds 已提交
444

445 446 447 448
	/* We do up to four passes through the list */
	if (probe_mask & MEM_PROBE_HIGH) {
		if (inv_probe(s_data->mem_db.next, s) > 0)
			return 0;
449 450
		dev_printk(KERN_NOTICE, &s->dev,
			   "cs: warning: no high memory space available!\n");
451
		return -ENODEV;
L
Linus Torvalds 已提交
452
	}
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472

	for (m = s_data->mem_db.next; m != &s_data->mem_db; m = mm.next) {
		mm = *m;
		/* Only probe < 1 MB */
		if (mm.base >= 0x100000)
			continue;
		if ((mm.base | mm.num) & 0xffff) {
			ok += do_mem_probe(mm.base, mm.num, s);
			continue;
		}
		/* Special probe for 64K-aligned block */
		for (i = 0; i < 4; i++) {
			b = order[i] << 12;
			if ((b >= mm.base) && (b+0x10000 <= mm.base+mm.num)) {
				if (ok >= mem_limit)
					sub_interval(&s_data->mem_db, b, 0x10000);
				else
					ok += do_mem_probe(b, 0x10000, s);
			}
		}
L
Linus Torvalds 已提交
473
	}
474 475 476 477 478

	if (ok > 0)
		return 0;

	return -ENODEV;
L
Linus Torvalds 已提交
479 480 481 482
}

#else /* CONFIG_PCMCIA_PROBE */

A
Andrew Morton 已提交
483
static int validate_mem(struct pcmcia_socket *s, unsigned int probe_mask)
L
Linus Torvalds 已提交
484 485 486
{
	struct resource_map *m, mm;
	struct socket_data *s_data = s->resource_data;
A
Andrew Morton 已提交
487
	unsigned long ok = 0;
L
Linus Torvalds 已提交
488 489 490

	for (m = s_data->mem_db.next; m != &s_data->mem_db; m = mm.next) {
		mm = *m;
A
Andrew Morton 已提交
491
		ok += do_mem_probe(mm.base, mm.num, s);
L
Linus Torvalds 已提交
492
	}
A
Andrew Morton 已提交
493 494 495
	if (ok > 0)
		return 0;
	return -ENODEV;
L
Linus Torvalds 已提交
496 497 498 499 500 501
}

#endif /* CONFIG_PCMCIA_PROBE */


/*
502
 * Locking note: Must be called with skt_mutex held!
L
Linus Torvalds 已提交
503
 */
504
static int pcmcia_nonstatic_validate_mem(struct pcmcia_socket *s)
L
Linus Torvalds 已提交
505 506
{
	struct socket_data *s_data = s->resource_data;
507 508 509 510 511
	unsigned int probe_mask = MEM_PROBE_LOW;
	int ret = 0;

	if (!probe_mem)
		return 0;
L
Linus Torvalds 已提交
512

513
	mutex_lock(&rsrc_mutex);
L
Linus Torvalds 已提交
514

515 516
	if (s->features & SS_CAP_PAGE_REGS)
		probe_mask = MEM_PROBE_HIGH;
L
Linus Torvalds 已提交
517

518 519 520 521
	if (probe_mask & ~s_data->rsrc_mem_probe) {
		if (s->state & SOCKET_PRESENT)
			ret = validate_mem(s, probe_mask);
		if (!ret)
L
Linus Torvalds 已提交
522
			s_data->rsrc_mem_probe |= probe_mask;
523
	}
L
Linus Torvalds 已提交
524

525
	mutex_unlock(&rsrc_mutex);
L
Linus Torvalds 已提交
526

527
	return ret;
L
Linus Torvalds 已提交
528 529 530 531 532 533 534 535
}

struct pcmcia_align_data {
	unsigned long	mask;
	unsigned long	offset;
	struct resource_map	*map;
};

536
static resource_size_t
L
Linus Torvalds 已提交
537
pcmcia_common_align(void *align_data, struct resource *res,
538
			resource_size_t size, resource_size_t align)
L
Linus Torvalds 已提交
539 540
{
	struct pcmcia_align_data *data = align_data;
541
	resource_size_t start;
L
Linus Torvalds 已提交
542 543 544 545 546 547
	/*
	 * Ensure that we have the correct start address
	 */
	start = (res->start & ~data->mask) + data->offset;
	if (start < res->start)
		start += data->mask + 1;
548
	return start;
L
Linus Torvalds 已提交
549 550
}

551
static resource_size_t
552 553
pcmcia_align(void *align_data, struct resource *res, resource_size_t size,
		resource_size_t align)
L
Linus Torvalds 已提交
554 555 556
{
	struct pcmcia_align_data *data = align_data;
	struct resource_map *m;
557
	resource_size_t start;
L
Linus Torvalds 已提交
558

559
	start = pcmcia_common_align(data, res, size, align);
L
Linus Torvalds 已提交
560 561 562 563 564 565 566 567 568 569 570

	for (m = data->map->next; m != data->map; m = m->next) {
		unsigned long start = m->base;
		unsigned long end = m->base + m->num - 1;

		/*
		 * If the lower resources are not available, try aligning
		 * to this entry of the resource database to see if it'll
		 * fit here.
		 */
		if (res->start < start) {
571
			start = pcmcia_common_align(data, res, size, align);
L
Linus Torvalds 已提交
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
		}

		/*
		 * If we're above the area which was passed in, there's
		 * no point proceeding.
		 */
		if (res->start >= res->end)
			break;

		if ((res->start + size - 1) <= end)
			break;
	}

	/*
	 * If we failed to find something suitable, ensure we fail.
	 */
	if (m == data->map)
589 590 591
		start = res->end;

	return start;
L
Linus Torvalds 已提交
592 593 594 595 596 597 598 599 600 601 602 603 604
}

/*
 * Adjust an existing IO region allocation, but making sure that we don't
 * encroach outside the resources which the user supplied.
 */
static int nonstatic_adjust_io_region(struct resource *res, unsigned long r_start,
				      unsigned long r_end, struct pcmcia_socket *s)
{
	struct resource_map *m;
	struct socket_data *s_data = s->resource_data;
	int ret = -ENOMEM;

605
	mutex_lock(&rsrc_mutex);
L
Linus Torvalds 已提交
606 607 608 609 610 611 612 613 614 615
	for (m = s_data->io_db.next; m != &s_data->io_db; m = m->next) {
		unsigned long start = m->base;
		unsigned long end = m->base + m->num - 1;

		if (start > r_start || r_end > end)
			continue;

		ret = adjust_resource(res, r_start, r_end - r_start + 1);
		break;
	}
616
	mutex_unlock(&rsrc_mutex);
L
Linus Torvalds 已提交
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633

	return ret;
}

/*======================================================================

    These find ranges of I/O ports or memory addresses that are not
    currently allocated by other devices.

    The 'align' field should reflect the number of bits of address
    that need to be preserved from the initial value of *base.  It
    should be a power of two, greater than or equal to 'num'.  A value
    of 0 means that all bits of *base are significant.  *base should
    also be strictly less than 'align'.

======================================================================*/

D
Dominik Brodowski 已提交
634
static struct resource *nonstatic_find_io_region(unsigned long base, int num,
L
Linus Torvalds 已提交
635 636
		   unsigned long align, struct pcmcia_socket *s)
{
637
	struct resource *res = make_resource(0, num, IORESOURCE_IO, dev_name(&s->dev));
L
Linus Torvalds 已提交
638 639 640 641 642 643 644 645 646 647 648 649
	struct socket_data *s_data = s->resource_data;
	struct pcmcia_align_data data;
	unsigned long min = base;
	int ret;

	if (align == 0)
		align = 0x10000;

	data.mask = align - 1;
	data.offset = base & data.mask;
	data.map = &s_data->io_db;

650
	mutex_lock(&rsrc_mutex);
L
Linus Torvalds 已提交
651 652 653 654 655 656 657 658
#ifdef CONFIG_PCI
	if (s->cb_dev) {
		ret = pci_bus_alloc_resource(s->cb_dev->bus, res, num, 1,
					     min, 0, pcmcia_align, &data);
	} else
#endif
		ret = allocate_resource(&ioport_resource, res, num, min, ~0UL,
					1, pcmcia_align, &data);
659
	mutex_unlock(&rsrc_mutex);
L
Linus Torvalds 已提交
660 661 662 663 664 665 666 667

	if (ret != 0) {
		kfree(res);
		res = NULL;
	}
	return res;
}

D
Dominik Brodowski 已提交
668
static struct resource *nonstatic_find_mem_region(u_long base, u_long num,
D
Dominik Brodowski 已提交
669
		u_long align, int low, struct pcmcia_socket *s)
L
Linus Torvalds 已提交
670
{
671
	struct resource *res = make_resource(0, num, IORESOURCE_MEM, dev_name(&s->dev));
L
Linus Torvalds 已提交
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
	struct socket_data *s_data = s->resource_data;
	struct pcmcia_align_data data;
	unsigned long min, max;
	int ret, i;

	low = low || !(s->features & SS_CAP_PAGE_REGS);

	data.mask = align - 1;
	data.offset = base & data.mask;
	data.map = &s_data->mem_db;

	for (i = 0; i < 2; i++) {
		if (low) {
			max = 0x100000UL;
			min = base < max ? base : 0;
		} else {
			max = ~0UL;
			min = 0x100000UL + base;
		}

692
		mutex_lock(&rsrc_mutex);
L
Linus Torvalds 已提交
693 694 695 696 697 698 699 700 701
#ifdef CONFIG_PCI
		if (s->cb_dev) {
			ret = pci_bus_alloc_resource(s->cb_dev->bus, res, num,
						     1, min, 0,
						     pcmcia_align, &data);
		} else
#endif
			ret = allocate_resource(&iomem_resource, res, num, min,
						max, 1, pcmcia_align, &data);
702
		mutex_unlock(&rsrc_mutex);
L
Linus Torvalds 已提交
703 704 705 706 707 708 709 710 711 712 713 714 715
		if (ret == 0 || low)
			break;
		low = 1;
	}

	if (ret != 0) {
		kfree(res);
		res = NULL;
	}
	return res;
}


716
static int adjust_memory(struct pcmcia_socket *s, unsigned int action, unsigned long start, unsigned long end)
L
Linus Torvalds 已提交
717 718
{
	struct socket_data *data = s->resource_data;
719 720
	unsigned long size = end - start + 1;
	int ret = 0;
L
Linus Torvalds 已提交
721

722
	if (end < start)
723
		return -EINVAL;
L
Linus Torvalds 已提交
724

725
	mutex_lock(&rsrc_mutex);
726
	switch (action) {
L
Linus Torvalds 已提交
727
	case ADD_MANAGED_RESOURCE:
728
		ret = add_interval(&data->mem_db, start, size);
L
Linus Torvalds 已提交
729 730
		break;
	case REMOVE_MANAGED_RESOURCE:
731 732
		ret = sub_interval(&data->mem_db, start, size);
		if (!ret) {
L
Linus Torvalds 已提交
733 734 735 736 737 738 739 740
			struct pcmcia_socket *socket;
			down_read(&pcmcia_socket_list_rwsem);
			list_for_each_entry(socket, &pcmcia_socket_list, socket_list)
				release_cis_mem(socket);
			up_read(&pcmcia_socket_list_rwsem);
		}
		break;
	default:
741
		ret = -EINVAL;
L
Linus Torvalds 已提交
742
	}
743
	mutex_unlock(&rsrc_mutex);
L
Linus Torvalds 已提交
744 745 746 747 748

	return ret;
}


749
static int adjust_io(struct pcmcia_socket *s, unsigned int action, unsigned long start, unsigned long end)
L
Linus Torvalds 已提交
750 751
{
	struct socket_data *data = s->resource_data;
752 753
	unsigned long size = end - start + 1;
	int ret = 0;
L
Linus Torvalds 已提交
754

755
	if (end < start)
756 757 758 759
		return -EINVAL;

	if (end > IO_SPACE_LIMIT)
		return -EINVAL;
L
Linus Torvalds 已提交
760

761
	mutex_lock(&rsrc_mutex);
762
	switch (action) {
L
Linus Torvalds 已提交
763
	case ADD_MANAGED_RESOURCE:
764 765
		if (add_interval(&data->io_db, start, size) != 0) {
			ret = -EBUSY;
L
Linus Torvalds 已提交
766 767 768 769
			break;
		}
#ifdef CONFIG_PCMCIA_PROBE
		if (probe_io)
770
			do_io_probe(s, start, size);
L
Linus Torvalds 已提交
771 772 773
#endif
		break;
	case REMOVE_MANAGED_RESOURCE:
774
		sub_interval(&data->io_db, start, size);
L
Linus Torvalds 已提交
775 776
		break;
	default:
777
		ret = -EINVAL;
L
Linus Torvalds 已提交
778 779
		break;
	}
780
	mutex_unlock(&rsrc_mutex);
L
Linus Torvalds 已提交
781 782 783 784 785

	return ret;
}


786 787 788 789 790 791 792 793 794
#ifdef CONFIG_PCI
static int nonstatic_autoadd_resources(struct pcmcia_socket *s)
{
	struct resource *res;
	int i, done = 0;

	if (!s->cb_dev || !s->cb_dev->bus)
		return -ENODEV;

B
Brian Gerst 已提交
795
#if defined(CONFIG_X86)
796 797 798 799 800 801 802 803 804 805
	/* If this is the root bus, the risk of hitting
	 * some strange system devices which aren't protected
	 * by either ACPI resource tables or properly requested
	 * resources is too big. Therefore, don't do auto-adding
	 * of resources at the moment.
	 */
	if (s->cb_dev->bus->number == 0)
		return -EINVAL;
#endif

D
Dominik Brodowski 已提交
806
	for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
807 808 809 810 811 812 813
		res = s->cb_dev->bus->resource[i];
		if (!res)
			continue;

		if (res->flags & IORESOURCE_IO) {
			if (res == &ioport_resource)
				continue;
814 815 816 817 818
			dev_printk(KERN_INFO, &s->cb_dev->dev,
				   "pcmcia: parent PCI bridge I/O "
				   "window: 0x%llx - 0x%llx\n",
				   (unsigned long long)res->start,
				   (unsigned long long)res->end);
819 820 821 822 823 824 825 826
			if (!adjust_io(s, ADD_MANAGED_RESOURCE, res->start, res->end))
				done |= IORESOURCE_IO;

		}

		if (res->flags & IORESOURCE_MEM) {
			if (res == &iomem_resource)
				continue;
827 828 829 830 831
			dev_printk(KERN_INFO, &s->cb_dev->dev,
				   "pcmcia: parent PCI bridge Memory "
				   "window: 0x%llx - 0x%llx\n",
				   (unsigned long long)res->start,
				   (unsigned long long)res->end);
832 833 834 835 836 837 838
			if (!adjust_memory(s, ADD_MANAGED_RESOURCE, res->start, res->end))
				done |= IORESOURCE_MEM;
		}
	}

	/* if we got at least one of IO, and one of MEM, we can be glad and
	 * activate the PCMCIA subsystem */
839
	if (done == (IORESOURCE_MEM | IORESOURCE_IO))
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
		s->resource_setup_done = 1;

	return 0;
}

#else

static inline int nonstatic_autoadd_resources(struct pcmcia_socket *s)
{
	return -ENODEV;
}

#endif


L
Linus Torvalds 已提交
855 856 857 858
static int nonstatic_init(struct pcmcia_socket *s)
{
	struct socket_data *data;

859
	data = kzalloc(sizeof(struct socket_data), GFP_KERNEL);
L
Linus Torvalds 已提交
860 861 862 863 864 865 866 867
	if (!data)
		return -ENOMEM;

	data->mem_db.next = &data->mem_db;
	data->io_db.next = &data->io_db;

	s->resource_data = (void *) data;

868 869
	nonstatic_autoadd_resources(s);

L
Linus Torvalds 已提交
870 871 872 873 874 875 876 877
	return 0;
}

static void nonstatic_release_resource_db(struct pcmcia_socket *s)
{
	struct socket_data *data = s->resource_data;
	struct resource_map *p, *q;

878
	mutex_lock(&rsrc_mutex);
L
Linus Torvalds 已提交
879 880 881 882 883 884 885 886
	for (p = data->mem_db.next; p != &data->mem_db; p = q) {
		q = p->next;
		kfree(p);
	}
	for (p = data->io_db.next; p != &data->io_db; p = q) {
		q = p->next;
		kfree(p);
	}
887
	mutex_unlock(&rsrc_mutex);
L
Linus Torvalds 已提交
888 889 890 891 892 893 894 895
}


struct pccard_resource_ops pccard_nonstatic_ops = {
	.validate_mem = pcmcia_nonstatic_validate_mem,
	.adjust_io_region = nonstatic_adjust_io_region,
	.find_io = nonstatic_find_io_region,
	.find_mem = nonstatic_find_mem_region,
896 897
	.add_io = adjust_io,
	.add_mem = adjust_memory,
L
Linus Torvalds 已提交
898 899 900 901 902 903 904 905
	.init = nonstatic_init,
	.exit = nonstatic_release_resource_db,
};
EXPORT_SYMBOL(pccard_nonstatic_ops);


/* sysfs interface to the resource database */

906 907
static ssize_t show_io_db(struct device *dev,
			  struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
908
{
909
	struct pcmcia_socket *s = dev_get_drvdata(dev);
L
Linus Torvalds 已提交
910 911 912 913
	struct socket_data *data;
	struct resource_map *p;
	ssize_t ret = 0;

914
	mutex_lock(&rsrc_mutex);
L
Linus Torvalds 已提交
915 916 917 918 919
	data = s->resource_data;

	for (p = data->io_db.next; p != &data->io_db; p = p->next) {
		if (ret > (PAGE_SIZE - 10))
			continue;
D
Dominik Brodowski 已提交
920 921 922 923
		ret += snprintf(&buf[ret], (PAGE_SIZE - ret - 1),
				"0x%08lx - 0x%08lx\n",
				((unsigned long) p->base),
				((unsigned long) p->base + p->num - 1));
L
Linus Torvalds 已提交
924 925
	}

926
	mutex_unlock(&rsrc_mutex);
D
Dominik Brodowski 已提交
927
	return ret;
L
Linus Torvalds 已提交
928 929
}

930 931 932
static ssize_t store_io_db(struct device *dev,
			   struct device_attribute *attr,
			   const char *buf, size_t count)
L
Linus Torvalds 已提交
933
{
934
	struct pcmcia_socket *s = dev_get_drvdata(dev);
L
Linus Torvalds 已提交
935
	unsigned long start_addr, end_addr;
936
	unsigned int add = ADD_MANAGED_RESOURCE;
L
Linus Torvalds 已提交
937 938
	ssize_t ret = 0;

D
Dominik Brodowski 已提交
939
	ret = sscanf(buf, "+ 0x%lx - 0x%lx", &start_addr, &end_addr);
L
Linus Torvalds 已提交
940
	if (ret != 2) {
D
Dominik Brodowski 已提交
941
		ret = sscanf(buf, "- 0x%lx - 0x%lx", &start_addr, &end_addr);
942
		add = REMOVE_MANAGED_RESOURCE;
L
Linus Torvalds 已提交
943
		if (ret != 2) {
D
Dominik Brodowski 已提交
944 945
			ret = sscanf(buf, "0x%lx - 0x%lx", &start_addr,
				&end_addr);
946
			add = ADD_MANAGED_RESOURCE;
L
Linus Torvalds 已提交
947 948 949 950
			if (ret != 2)
				return -EINVAL;
		}
	}
951
	if (end_addr < start_addr)
L
Linus Torvalds 已提交
952 953
		return -EINVAL;

954
	ret = adjust_io(s, add, start_addr, end_addr);
955 956
	if (!ret)
		s->resource_setup_new = 1;
L
Linus Torvalds 已提交
957 958 959

	return ret ? ret : count;
}
960
static DEVICE_ATTR(available_resources_io, 0600, show_io_db, store_io_db);
L
Linus Torvalds 已提交
961

962 963
static ssize_t show_mem_db(struct device *dev,
			   struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
964
{
965
	struct pcmcia_socket *s = dev_get_drvdata(dev);
L
Linus Torvalds 已提交
966 967 968 969
	struct socket_data *data;
	struct resource_map *p;
	ssize_t ret = 0;

970
	mutex_lock(&rsrc_mutex);
L
Linus Torvalds 已提交
971 972 973 974 975
	data = s->resource_data;

	for (p = data->mem_db.next; p != &data->mem_db; p = p->next) {
		if (ret > (PAGE_SIZE - 10))
			continue;
D
Dominik Brodowski 已提交
976 977 978 979
		ret += snprintf(&buf[ret], (PAGE_SIZE - ret - 1),
				"0x%08lx - 0x%08lx\n",
				((unsigned long) p->base),
				((unsigned long) p->base + p->num - 1));
L
Linus Torvalds 已提交
980 981
	}

982
	mutex_unlock(&rsrc_mutex);
D
Dominik Brodowski 已提交
983
	return ret;
L
Linus Torvalds 已提交
984 985
}

986 987 988
static ssize_t store_mem_db(struct device *dev,
			    struct device_attribute *attr,
			    const char *buf, size_t count)
L
Linus Torvalds 已提交
989
{
990
	struct pcmcia_socket *s = dev_get_drvdata(dev);
L
Linus Torvalds 已提交
991
	unsigned long start_addr, end_addr;
992
	unsigned int add = ADD_MANAGED_RESOURCE;
L
Linus Torvalds 已提交
993 994
	ssize_t ret = 0;

D
Dominik Brodowski 已提交
995
	ret = sscanf(buf, "+ 0x%lx - 0x%lx", &start_addr, &end_addr);
L
Linus Torvalds 已提交
996
	if (ret != 2) {
D
Dominik Brodowski 已提交
997
		ret = sscanf(buf, "- 0x%lx - 0x%lx", &start_addr, &end_addr);
998
		add = REMOVE_MANAGED_RESOURCE;
L
Linus Torvalds 已提交
999
		if (ret != 2) {
D
Dominik Brodowski 已提交
1000 1001
			ret = sscanf(buf, "0x%lx - 0x%lx", &start_addr,
				&end_addr);
1002
			add = ADD_MANAGED_RESOURCE;
L
Linus Torvalds 已提交
1003 1004 1005 1006
			if (ret != 2)
				return -EINVAL;
		}
	}
1007
	if (end_addr < start_addr)
L
Linus Torvalds 已提交
1008 1009
		return -EINVAL;

1010
	ret = adjust_memory(s, add, start_addr, end_addr);
1011 1012
	if (!ret)
		s->resource_setup_new = 1;
L
Linus Torvalds 已提交
1013 1014 1015

	return ret ? ret : count;
}
1016
static DEVICE_ATTR(available_resources_mem, 0600, show_mem_db, store_mem_db);
L
Linus Torvalds 已提交
1017

1018 1019 1020
static struct attribute *pccard_rsrc_attributes[] = {
	&dev_attr_available_resources_io.attr,
	&dev_attr_available_resources_mem.attr,
L
Linus Torvalds 已提交
1021 1022 1023
	NULL,
};

1024 1025 1026 1027
static const struct attribute_group rsrc_attributes = {
	.attrs = pccard_rsrc_attributes,
};

1028
static int __devinit pccard_sysfs_add_rsrc(struct device *dev,
1029
					   struct class_interface *class_intf)
L
Linus Torvalds 已提交
1030
{
1031
	struct pcmcia_socket *s = dev_get_drvdata(dev);
1032

L
Linus Torvalds 已提交
1033 1034
	if (s->resource_ops != &pccard_nonstatic_ops)
		return 0;
1035
	return sysfs_create_group(&dev->kobj, &rsrc_attributes);
L
Linus Torvalds 已提交
1036 1037
}

1038
static void __devexit pccard_sysfs_remove_rsrc(struct device *dev,
1039
					       struct class_interface *class_intf)
L
Linus Torvalds 已提交
1040
{
1041
	struct pcmcia_socket *s = dev_get_drvdata(dev);
L
Linus Torvalds 已提交
1042 1043 1044

	if (s->resource_ops != &pccard_nonstatic_ops)
		return;
1045
	sysfs_remove_group(&dev->kobj, &rsrc_attributes);
L
Linus Torvalds 已提交
1046 1047
}

1048
static struct class_interface pccard_rsrc_interface __refdata = {
L
Linus Torvalds 已提交
1049
	.class = &pcmcia_socket_class,
1050 1051
	.add_dev = &pccard_sysfs_add_rsrc,
	.remove_dev = __devexit_p(&pccard_sysfs_remove_rsrc),
L
Linus Torvalds 已提交
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
};

static int __init nonstatic_sysfs_init(void)
{
	return class_interface_register(&pccard_rsrc_interface);
}

static void __exit nonstatic_sysfs_exit(void)
{
	class_interface_unregister(&pccard_rsrc_interface);
}

module_init(nonstatic_sysfs_init);
module_exit(nonstatic_sysfs_exit);