1 /*
2  * rsrc_nonstatic.c -- Resource management routines for !SS_CAP_STATIC_MAP sockets
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * The initial developer of the original code is David A. Hinds
9  * <dahinds@users.sourceforge.net>.  Portions created by David A. Hinds
10  * are Copyright (C) 1999 David A. Hinds.  All Rights Reserved.
11  *
12  * (C) 1999		David A. Hinds
13  */
14 
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <linux/init.h>
18 #include <linux/interrupt.h>
19 #include <linux/kernel.h>
20 #include <linux/errno.h>
21 #include <linux/types.h>
22 #include <linux/slab.h>
23 #include <linux/ioport.h>
24 #include <linux/timer.h>
25 #include <linux/pci.h>
26 #include <linux/device.h>
27 #include <linux/io.h>
28 
29 #include <asm/irq.h>
30 
31 #include <pcmcia/cs_types.h>
32 #include <pcmcia/ss.h>
33 #include <pcmcia/cs.h>
34 #include <pcmcia/cistpl.h>
35 #include "cs_internal.h"
36 
37 /* moved to rsrc_mgr.c
38 MODULE_AUTHOR("David A. Hinds, Dominik Brodowski");
39 MODULE_LICENSE("GPL");
40 */
41 
42 /* Parameters that can be set with 'insmod' */
43 
44 #define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0444)
45 
46 INT_MODULE_PARM(probe_mem,	1);		/* memory probe? */
47 #ifdef CONFIG_PCMCIA_PROBE
48 INT_MODULE_PARM(probe_io,	1);		/* IO port probe? */
49 INT_MODULE_PARM(mem_limit,	0x10000);
50 #endif
51 
52 /* for io_db and mem_db */
53 struct resource_map {
54 	u_long			base, num;
55 	struct resource_map	*next;
56 };
57 
58 struct socket_data {
59 	struct resource_map		mem_db;
60 	struct resource_map		mem_db_valid;
61 	struct resource_map		io_db;
62 };
63 
64 #define MEM_PROBE_LOW	(1 << 0)
65 #define MEM_PROBE_HIGH	(1 << 1)
66 
67 
68 /*======================================================================
69 
70     Linux resource management extensions
71 
72 ======================================================================*/
73 
74 static struct resource *
75 claim_region(struct pcmcia_socket *s, resource_size_t base,
76 		resource_size_t size, int type, char *name)
77 {
78 	struct resource *res, *parent;
79 
80 	parent = type & IORESOURCE_MEM ? &iomem_resource : &ioport_resource;
81 	res = pcmcia_make_resource(base, size, type | IORESOURCE_BUSY, name);
82 
83 	if (res) {
84 #ifdef CONFIG_PCI
85 		if (s && s->cb_dev)
86 			parent = pci_find_parent_resource(s->cb_dev, res);
87 #endif
88 		if (!parent || request_resource(parent, res)) {
89 			kfree(res);
90 			res = NULL;
91 		}
92 	}
93 	return res;
94 }
95 
96 static void free_region(struct resource *res)
97 {
98 	if (res) {
99 		release_resource(res);
100 		kfree(res);
101 	}
102 }
103 
104 /*======================================================================
105 
106     These manage the internal databases of available resources.
107 
108 ======================================================================*/
109 
110 static int add_interval(struct resource_map *map, u_long base, u_long num)
111 {
112 	struct resource_map *p, *q;
113 
114 	for (p = map; ; p = p->next) {
115 		if ((p != map) && (p->base+p->num >= base)) {
116 			p->num = max(num + base - p->base, p->num);
117 			return 0;
118 		}
119 		if ((p->next == map) || (p->next->base > base+num-1))
120 			break;
121 	}
122 	q = kmalloc(sizeof(struct resource_map), GFP_KERNEL);
123 	if (!q) {
124 		printk(KERN_WARNING "out of memory to update resources\n");
125 		return -ENOMEM;
126 	}
127 	q->base = base; q->num = num;
128 	q->next = p->next; p->next = q;
129 	return 0;
130 }
131 
132 /*====================================================================*/
133 
134 static int sub_interval(struct resource_map *map, u_long base, u_long num)
135 {
136 	struct resource_map *p, *q;
137 
138 	for (p = map; ; p = q) {
139 		q = p->next;
140 		if (q == map)
141 			break;
142 		if ((q->base+q->num > base) && (base+num > q->base)) {
143 			if (q->base >= base) {
144 				if (q->base+q->num <= base+num) {
145 					/* Delete whole block */
146 					p->next = q->next;
147 					kfree(q);
148 					/* don't advance the pointer yet */
149 					q = p;
150 				} else {
151 					/* Cut off bit from the front */
152 					q->num = q->base + q->num - base - num;
153 					q->base = base + num;
154 				}
155 			} else if (q->base+q->num <= base+num) {
156 				/* Cut off bit from the end */
157 				q->num = base - q->base;
158 			} else {
159 				/* Split the block into two pieces */
160 				p = kmalloc(sizeof(struct resource_map),
161 					GFP_KERNEL);
162 				if (!p) {
163 					printk(KERN_WARNING "out of memory to update resources\n");
164 					return -ENOMEM;
165 				}
166 				p->base = base+num;
167 				p->num = q->base+q->num - p->base;
168 				q->num = base - q->base;
169 				p->next = q->next ; q->next = p;
170 			}
171 		}
172 	}
173 	return 0;
174 }
175 
176 /*======================================================================
177 
178     These routines examine a region of IO or memory addresses to
179     determine what ranges might be genuinely available.
180 
181 ======================================================================*/
182 
183 #ifdef CONFIG_PCMCIA_PROBE
184 static void do_io_probe(struct pcmcia_socket *s, unsigned int base,
185 			unsigned int num)
186 {
187 	struct resource *res;
188 	struct socket_data *s_data = s->resource_data;
189 	unsigned int i, j, bad;
190 	int any;
191 	u_char *b, hole, most;
192 
193 	dev_printk(KERN_INFO, &s->dev, "cs: IO port probe %#x-%#x:",
194 		base, base+num-1);
195 
196 	/* First, what does a floating port look like? */
197 	b = kzalloc(256, GFP_KERNEL);
198 	if (!b) {
199 		printk("\n");
200 		dev_printk(KERN_ERR, &s->dev,
201 			"do_io_probe: unable to kmalloc 256 bytes");
202 		return;
203 	}
204 	for (i = base, most = 0; i < base+num; i += 8) {
205 		res = claim_region(s, i, 8, IORESOURCE_IO, "PCMCIA ioprobe");
206 		if (!res)
207 			continue;
208 		hole = inb(i);
209 		for (j = 1; j < 8; j++)
210 			if (inb(i+j) != hole)
211 				break;
212 		free_region(res);
213 		if ((j == 8) && (++b[hole] > b[most]))
214 			most = hole;
215 		if (b[most] == 127)
216 			break;
217 	}
218 	kfree(b);
219 
220 	bad = any = 0;
221 	for (i = base; i < base+num; i += 8) {
222 		res = claim_region(s, i, 8, IORESOURCE_IO, "PCMCIA ioprobe");
223 		if (!res) {
224 			if (!any)
225 				printk(" excluding");
226 			if (!bad)
227 				bad = any = i;
228 			continue;
229 		}
230 		for (j = 0; j < 8; j++)
231 			if (inb(i+j) != most)
232 				break;
233 		free_region(res);
234 		if (j < 8) {
235 			if (!any)
236 				printk(" excluding");
237 			if (!bad)
238 				bad = any = i;
239 		} else {
240 			if (bad) {
241 				sub_interval(&s_data->io_db, bad, i-bad);
242 				printk(" %#x-%#x", bad, i-1);
243 				bad = 0;
244 			}
245 		}
246 	}
247 	if (bad) {
248 		if ((num > 16) && (bad == base) && (i == base+num)) {
249 			sub_interval(&s_data->io_db, bad, i-bad);
250 			printk(" nothing: probe failed.\n");
251 			return;
252 		} else {
253 			sub_interval(&s_data->io_db, bad, i-bad);
254 			printk(" %#x-%#x", bad, i-1);
255 		}
256 	}
257 
258 	printk(any ? "\n" : " clean.\n");
259 }
260 #endif
261 
262 /*======================================================================*/
263 
264 /**
265  * readable() - iomem validation function for cards with a valid CIS
266  */
267 static int readable(struct pcmcia_socket *s, struct resource *res,
268 		    unsigned int *count)
269 {
270 	int ret = -EINVAL;
271 
272 	if (s->fake_cis) {
273 		dev_dbg(&s->dev, "fake CIS is being used: can't validate mem\n");
274 		return 0;
275 	}
276 
277 	s->cis_mem.res = res;
278 	s->cis_virt = ioremap(res->start, s->map_size);
279 	if (s->cis_virt) {
280 		mutex_unlock(&s->ops_mutex);
281 		/* as we're only called from pcmcia.c, we're safe */
282 		if (s->callback->validate)
283 			ret = s->callback->validate(s, count);
284 		/* invalidate mapping */
285 		mutex_lock(&s->ops_mutex);
286 		iounmap(s->cis_virt);
287 		s->cis_virt = NULL;
288 	}
289 	s->cis_mem.res = NULL;
290 	if ((ret) || (*count == 0))
291 		return -EINVAL;
292 	return 0;
293 }
294 
295 /**
296  * checksum() - iomem validation function for simple memory cards
297  */
298 static int checksum(struct pcmcia_socket *s, struct resource *res,
299 		    unsigned int *value)
300 {
301 	pccard_mem_map map;
302 	int i, a = 0, b = -1, d;
303 	void __iomem *virt;
304 
305 	virt = ioremap(res->start, s->map_size);
306 	if (virt) {
307 		map.map = 0;
308 		map.flags = MAP_ACTIVE;
309 		map.speed = 0;
310 		map.res = res;
311 		map.card_start = 0;
312 		s->ops->set_mem_map(s, &map);
313 
314 		/* Don't bother checking every word... */
315 		for (i = 0; i < s->map_size; i += 44) {
316 			d = readl(virt+i);
317 			a += d;
318 			b &= d;
319 		}
320 
321 		map.flags = 0;
322 		s->ops->set_mem_map(s, &map);
323 
324 		iounmap(virt);
325 	}
326 
327 	if (b == -1)
328 		return -EINVAL;
329 
330 	*value = a;
331 
332 	return 0;
333 }
334 
335 /**
336  * do_validate_mem() - low level validate a memory region for PCMCIA use
337  * @s:		PCMCIA socket to validate
338  * @base:	start address of resource to check
339  * @size:	size of resource to check
340  * @validate:	validation function to use
341  *
342  * do_validate_mem() splits up the memory region which is to be checked
343  * into two parts. Both are passed to the @validate() function. If
344  * @validate() returns non-zero, or the value parameter to @validate()
345  * is zero, or the value parameter is different between both calls,
346  * the check fails, and -EINVAL is returned. Else, 0 is returned.
347  */
348 static int do_validate_mem(struct pcmcia_socket *s,
349 			   unsigned long base, unsigned long size,
350 			   int validate (struct pcmcia_socket *s,
351 					 struct resource *res,
352 					 unsigned int *value))
353 {
354 	struct socket_data *s_data = s->resource_data;
355 	struct resource *res1, *res2;
356 	unsigned int info1 = 1, info2 = 1;
357 	int ret = -EINVAL;
358 
359 	res1 = claim_region(s, base, size/2, IORESOURCE_MEM, "PCMCIA memprobe");
360 	res2 = claim_region(s, base + size/2, size/2, IORESOURCE_MEM,
361 			"PCMCIA memprobe");
362 
363 	if (res1 && res2) {
364 		ret = 0;
365 		if (validate) {
366 			ret = validate(s, res1, &info1);
367 			ret += validate(s, res2, &info2);
368 		}
369 	}
370 
371 	free_region(res2);
372 	free_region(res1);
373 
374 	dev_dbg(&s->dev, "cs: memory probe 0x%06lx-0x%06lx: %p %p %u %u %u",
375 		base, base+size-1, res1, res2, ret, info1, info2);
376 
377 	if ((ret) || (info1 != info2) || (info1 == 0))
378 		return -EINVAL;
379 
380 	if (validate && !s->fake_cis) {
381 		/* move it to the validated data set */
382 		add_interval(&s_data->mem_db_valid, base, size);
383 		sub_interval(&s_data->mem_db, base, size);
384 	}
385 
386 	return 0;
387 }
388 
389 
390 /**
391  * do_mem_probe() - validate a memory region for PCMCIA use
392  * @s:		PCMCIA socket to validate
393  * @base:	start address of resource to check
394  * @num:	size of resource to check
395  * @validate:	validation function to use
396  * @fallback:	validation function to use if validate fails
397  *
398  * do_mem_probe() checks a memory region for use by the PCMCIA subsystem.
399  * To do so, the area is split up into sensible parts, and then passed
400  * into the @validate() function. Only if @validate() and @fallback() fail,
401  * the area is marked as unavaibale for use by the PCMCIA subsystem. The
402  * function returns the size of the usable memory area.
403  */
404 static int do_mem_probe(struct pcmcia_socket *s, u_long base, u_long num,
405 			int validate (struct pcmcia_socket *s,
406 				      struct resource *res,
407 				      unsigned int *value),
408 			int fallback (struct pcmcia_socket *s,
409 				      struct resource *res,
410 				      unsigned int *value))
411 {
412 	struct socket_data *s_data = s->resource_data;
413 	u_long i, j, bad, fail, step;
414 
415 	dev_printk(KERN_INFO, &s->dev, "cs: memory probe 0x%06lx-0x%06lx:",
416 		base, base+num-1);
417 	bad = fail = 0;
418 	step = (num < 0x20000) ? 0x2000 : ((num>>4) & ~0x1fff);
419 	/* don't allow too large steps */
420 	if (step > 0x800000)
421 		step = 0x800000;
422 	/* cis_readable wants to map 2x map_size */
423 	if (step < 2 * s->map_size)
424 		step = 2 * s->map_size;
425 	for (i = j = base; i < base+num; i = j + step) {
426 		if (!fail) {
427 			for (j = i; j < base+num; j += step) {
428 				if (!do_validate_mem(s, j, step, validate))
429 					break;
430 			}
431 			fail = ((i == base) && (j == base+num));
432 		}
433 		if ((fail) && (fallback)) {
434 			for (j = i; j < base+num; j += step)
435 				if (!do_validate_mem(s, j, step, fallback))
436 					break;
437 		}
438 		if (i != j) {
439 			if (!bad)
440 				printk(" excluding");
441 			printk(" %#05lx-%#05lx", i, j-1);
442 			sub_interval(&s_data->mem_db, i, j-i);
443 			bad += j-i;
444 		}
445 	}
446 	printk(bad ? "\n" : " clean.\n");
447 	return num - bad;
448 }
449 
450 
451 #ifdef CONFIG_PCMCIA_PROBE
452 
453 /**
454  * inv_probe() - top-to-bottom search for one usuable high memory area
455  * @s:		PCMCIA socket to validate
456  * @m:		resource_map to check
457  */
458 static u_long inv_probe(struct resource_map *m, struct pcmcia_socket *s)
459 {
460 	struct socket_data *s_data = s->resource_data;
461 	u_long ok;
462 	if (m == &s_data->mem_db)
463 		return 0;
464 	ok = inv_probe(m->next, s);
465 	if (ok) {
466 		if (m->base >= 0x100000)
467 			sub_interval(&s_data->mem_db, m->base, m->num);
468 		return ok;
469 	}
470 	if (m->base < 0x100000)
471 		return 0;
472 	return do_mem_probe(s, m->base, m->num, readable, checksum);
473 }
474 
475 /**
476  * validate_mem() - memory probe function
477  * @s:		PCMCIA socket to validate
478  * @probe_mask: MEM_PROBE_LOW | MEM_PROBE_HIGH
479  *
480  * The memory probe.  If the memory list includes a 64K-aligned block
481  * below 1MB, we probe in 64K chunks, and as soon as we accumulate at
482  * least mem_limit free space, we quit. Returns 0 on usuable ports.
483  */
484 static int validate_mem(struct pcmcia_socket *s, unsigned int probe_mask)
485 {
486 	struct resource_map *m, mm;
487 	static unsigned char order[] = { 0xd0, 0xe0, 0xc0, 0xf0 };
488 	unsigned long b, i, ok = 0;
489 	struct socket_data *s_data = s->resource_data;
490 
491 	/* We do up to four passes through the list */
492 	if (probe_mask & MEM_PROBE_HIGH) {
493 		if (inv_probe(s_data->mem_db.next, s) > 0)
494 			return 0;
495 		if (s_data->mem_db_valid.next != &s_data->mem_db_valid)
496 			return 0;
497 		dev_printk(KERN_NOTICE, &s->dev,
498 			   "cs: warning: no high memory space available!\n");
499 		return -ENODEV;
500 	}
501 
502 	for (m = s_data->mem_db.next; m != &s_data->mem_db; m = mm.next) {
503 		mm = *m;
504 		/* Only probe < 1 MB */
505 		if (mm.base >= 0x100000)
506 			continue;
507 		if ((mm.base | mm.num) & 0xffff) {
508 			ok += do_mem_probe(s, mm.base, mm.num, readable,
509 					   checksum);
510 			continue;
511 		}
512 		/* Special probe for 64K-aligned block */
513 		for (i = 0; i < 4; i++) {
514 			b = order[i] << 12;
515 			if ((b >= mm.base) && (b+0x10000 <= mm.base+mm.num)) {
516 				if (ok >= mem_limit)
517 					sub_interval(&s_data->mem_db, b, 0x10000);
518 				else
519 					ok += do_mem_probe(s, b, 0x10000,
520 							   readable, checksum);
521 			}
522 		}
523 	}
524 
525 	if (ok > 0)
526 		return 0;
527 
528 	return -ENODEV;
529 }
530 
531 #else /* CONFIG_PCMCIA_PROBE */
532 
533 /**
534  * validate_mem() - memory probe function
535  * @s:		PCMCIA socket to validate
536  * @probe_mask: ignored
537  *
538  * Returns 0 on usuable ports.
539  */
540 static int validate_mem(struct pcmcia_socket *s, unsigned int probe_mask)
541 {
542 	struct resource_map *m, mm;
543 	struct socket_data *s_data = s->resource_data;
544 	unsigned long ok = 0;
545 
546 	for (m = s_data->mem_db.next; m != &s_data->mem_db; m = mm.next) {
547 		mm = *m;
548 		ok += do_mem_probe(s, mm.base, mm.num, readable, checksum);
549 	}
550 	if (ok > 0)
551 		return 0;
552 	return -ENODEV;
553 }
554 
555 #endif /* CONFIG_PCMCIA_PROBE */
556 
557 
558 /**
559  * pcmcia_nonstatic_validate_mem() - try to validate iomem for PCMCIA use
560  * @s:		PCMCIA socket to validate
561  *
562  * This is tricky... when we set up CIS memory, we try to validate
563  * the memory window space allocations.
564  *
565  * Locking note: Must be called with skt_mutex held!
566  */
567 static int pcmcia_nonstatic_validate_mem(struct pcmcia_socket *s)
568 {
569 	struct socket_data *s_data = s->resource_data;
570 	unsigned int probe_mask = MEM_PROBE_LOW;
571 	int ret;
572 
573 	if (!probe_mem || !(s->state & SOCKET_PRESENT))
574 		return 0;
575 
576 	if (s->features & SS_CAP_PAGE_REGS)
577 		probe_mask = MEM_PROBE_HIGH;
578 
579 	ret = validate_mem(s, probe_mask);
580 
581 	if (s_data->mem_db_valid.next != &s_data->mem_db_valid)
582 		return 0;
583 
584 	return ret;
585 }
586 
587 struct pcmcia_align_data {
588 	unsigned long	mask;
589 	unsigned long	offset;
590 	struct resource_map	*map;
591 };
592 
593 static resource_size_t pcmcia_common_align(struct pcmcia_align_data *align_data,
594 					resource_size_t start)
595 {
596 	resource_size_t ret;
597 	/*
598 	 * Ensure that we have the correct start address
599 	 */
600 	ret = (start & ~align_data->mask) + align_data->offset;
601 	if (ret < start)
602 		ret += align_data->mask + 1;
603 	return ret;
604 }
605 
606 static resource_size_t
607 pcmcia_align(void *align_data, const struct resource *res,
608 	resource_size_t size, resource_size_t align)
609 {
610 	struct pcmcia_align_data *data = align_data;
611 	struct resource_map *m;
612 	resource_size_t start;
613 
614 	start = pcmcia_common_align(data, res->start);
615 
616 	for (m = data->map->next; m != data->map; m = m->next) {
617 		unsigned long map_start = m->base;
618 		unsigned long map_end = m->base + m->num - 1;
619 
620 		/*
621 		 * If the lower resources are not available, try aligning
622 		 * to this entry of the resource database to see if it'll
623 		 * fit here.
624 		 */
625 		if (start < map_start)
626 			start = pcmcia_common_align(data, map_start);
627 
628 		/*
629 		 * If we're above the area which was passed in, there's
630 		 * no point proceeding.
631 		 */
632 		if (start >= res->end)
633 			break;
634 
635 		if ((start + size - 1) <= map_end)
636 			break;
637 	}
638 
639 	/*
640 	 * If we failed to find something suitable, ensure we fail.
641 	 */
642 	if (m == data->map)
643 		start = res->end;
644 
645 	return start;
646 }
647 
648 /*
649  * Adjust an existing IO region allocation, but making sure that we don't
650  * encroach outside the resources which the user supplied.
651  */
652 static int __nonstatic_adjust_io_region(struct pcmcia_socket *s,
653 					unsigned long r_start,
654 					unsigned long r_end)
655 {
656 	struct resource_map *m;
657 	struct socket_data *s_data = s->resource_data;
658 	int ret = -ENOMEM;
659 
660 	for (m = s_data->io_db.next; m != &s_data->io_db; m = m->next) {
661 		unsigned long start = m->base;
662 		unsigned long end = m->base + m->num - 1;
663 
664 		if (start > r_start || r_end > end)
665 			continue;
666 
667 		ret = 0;
668 	}
669 
670 	return ret;
671 }
672 
673 /*======================================================================
674 
675     These find ranges of I/O ports or memory addresses that are not
676     currently allocated by other devices.
677 
678     The 'align' field should reflect the number of bits of address
679     that need to be preserved from the initial value of *base.  It
680     should be a power of two, greater than or equal to 'num'.  A value
681     of 0 means that all bits of *base are significant.  *base should
682     also be strictly less than 'align'.
683 
684 ======================================================================*/
685 
686 static struct resource *__nonstatic_find_io_region(struct pcmcia_socket *s,
687 						unsigned long base, int num,
688 						unsigned long align)
689 {
690 	struct resource *res = pcmcia_make_resource(0, num, IORESOURCE_IO,
691 						dev_name(&s->dev));
692 	struct socket_data *s_data = s->resource_data;
693 	struct pcmcia_align_data data;
694 	unsigned long min = base;
695 	int ret;
696 
697 	data.mask = align - 1;
698 	data.offset = base & data.mask;
699 	data.map = &s_data->io_db;
700 
701 #ifdef CONFIG_PCI
702 	if (s->cb_dev) {
703 		ret = pci_bus_alloc_resource(s->cb_dev->bus, res, num, 1,
704 					     min, 0, pcmcia_align, &data);
705 	} else
706 #endif
707 		ret = allocate_resource(&ioport_resource, res, num, min, ~0UL,
708 					1, pcmcia_align, &data);
709 
710 	if (ret != 0) {
711 		kfree(res);
712 		res = NULL;
713 	}
714 	return res;
715 }
716 
717 static int nonstatic_find_io(struct pcmcia_socket *s, unsigned int attr,
718 			unsigned int *base, unsigned int num,
719 			unsigned int align)
720 {
721 	int i, ret = 0;
722 
723 	/* Check for an already-allocated window that must conflict with
724 	 * what was asked for.  It is a hack because it does not catch all
725 	 * potential conflicts, just the most obvious ones.
726 	 */
727 	for (i = 0; i < MAX_IO_WIN; i++) {
728 		if (!s->io[i].res)
729 			continue;
730 
731 		if (!*base)
732 			continue;
733 
734 		if ((s->io[i].res->start & (align-1)) == *base)
735 			return -EBUSY;
736 	}
737 
738 	for (i = 0; i < MAX_IO_WIN; i++) {
739 		struct resource *res = s->io[i].res;
740 		unsigned int try;
741 
742 		if (res && (res->flags & IORESOURCE_BITS) !=
743 			(attr & IORESOURCE_BITS))
744 			continue;
745 
746 		if (!res) {
747 			if (align == 0)
748 				align = 0x10000;
749 
750 			res = s->io[i].res = __nonstatic_find_io_region(s,
751 								*base, num,
752 								align);
753 			if (!res)
754 				return -EINVAL;
755 
756 			*base = res->start;
757 			s->io[i].res->flags =
758 				((res->flags & ~IORESOURCE_BITS) |
759 					(attr & IORESOURCE_BITS));
760 			s->io[i].InUse = num;
761 			return 0;
762 		}
763 
764 		/* Try to extend top of window */
765 		try = res->end + 1;
766 		if ((*base == 0) || (*base == try)) {
767 			ret =  __nonstatic_adjust_io_region(s, res->start,
768 							res->end + num);
769 			if (!ret) {
770 				ret = adjust_resource(s->io[i].res, res->start,
771 					       res->end - res->start + num + 1);
772 				if (ret)
773 					continue;
774 				*base = try;
775 				s->io[i].InUse += num;
776 				return 0;
777 			}
778 		}
779 
780 		/* Try to extend bottom of window */
781 		try = res->start - num;
782 		if ((*base == 0) || (*base == try)) {
783 			ret =  __nonstatic_adjust_io_region(s,
784 							res->start - num,
785 							res->end);
786 			if (!ret) {
787 				ret = adjust_resource(s->io[i].res,
788 					       res->start - num,
789 					       res->end - res->start + num + 1);
790 				if (ret)
791 					continue;
792 				*base = try;
793 				s->io[i].InUse += num;
794 				return 0;
795 			}
796 		}
797 	}
798 
799 	return -EINVAL;
800 }
801 
802 
803 static struct resource *nonstatic_find_mem_region(u_long base, u_long num,
804 		u_long align, int low, struct pcmcia_socket *s)
805 {
806 	struct resource *res = pcmcia_make_resource(0, num, IORESOURCE_MEM,
807 						dev_name(&s->dev));
808 	struct socket_data *s_data = s->resource_data;
809 	struct pcmcia_align_data data;
810 	unsigned long min, max;
811 	int ret, i, j;
812 
813 	low = low || !(s->features & SS_CAP_PAGE_REGS);
814 
815 	data.mask = align - 1;
816 	data.offset = base & data.mask;
817 
818 	for (i = 0; i < 2; i++) {
819 		data.map = &s_data->mem_db_valid;
820 		if (low) {
821 			max = 0x100000UL;
822 			min = base < max ? base : 0;
823 		} else {
824 			max = ~0UL;
825 			min = 0x100000UL + base;
826 		}
827 
828 		for (j = 0; j < 2; j++) {
829 #ifdef CONFIG_PCI
830 			if (s->cb_dev) {
831 				ret = pci_bus_alloc_resource(s->cb_dev->bus,
832 							res, num, 1, min, 0,
833 							pcmcia_align, &data);
834 			} else
835 #endif
836 			{
837 				ret = allocate_resource(&iomem_resource,
838 							res, num, min, max, 1,
839 							pcmcia_align, &data);
840 			}
841 			if (ret == 0)
842 				break;
843 			data.map = &s_data->mem_db;
844 		}
845 		if (ret == 0 || low)
846 			break;
847 		low = 1;
848 	}
849 
850 	if (ret != 0) {
851 		kfree(res);
852 		res = NULL;
853 	}
854 	return res;
855 }
856 
857 
858 static int adjust_memory(struct pcmcia_socket *s, unsigned int action, unsigned long start, unsigned long end)
859 {
860 	struct socket_data *data = s->resource_data;
861 	unsigned long size = end - start + 1;
862 	int ret = 0;
863 
864 	if (end < start)
865 		return -EINVAL;
866 
867 	switch (action) {
868 	case ADD_MANAGED_RESOURCE:
869 		ret = add_interval(&data->mem_db, start, size);
870 		if (!ret)
871 			do_mem_probe(s, start, size, NULL, NULL);
872 		break;
873 	case REMOVE_MANAGED_RESOURCE:
874 		ret = sub_interval(&data->mem_db, start, size);
875 		break;
876 	default:
877 		ret = -EINVAL;
878 	}
879 
880 	return ret;
881 }
882 
883 
884 static int adjust_io(struct pcmcia_socket *s, unsigned int action, unsigned long start, unsigned long end)
885 {
886 	struct socket_data *data = s->resource_data;
887 	unsigned long size;
888 	int ret = 0;
889 
890 #if defined(CONFIG_X86)
891 	/* on x86, avoid anything < 0x100 for it is often used for
892 	 * legacy platform devices */
893 	if (start < 0x100)
894 		start = 0x100;
895 #endif
896 
897 	size = end - start + 1;
898 
899 	if (end < start)
900 		return -EINVAL;
901 
902 	if (end > IO_SPACE_LIMIT)
903 		return -EINVAL;
904 
905 	switch (action) {
906 	case ADD_MANAGED_RESOURCE:
907 		if (add_interval(&data->io_db, start, size) != 0) {
908 			ret = -EBUSY;
909 			break;
910 		}
911 #ifdef CONFIG_PCMCIA_PROBE
912 		if (probe_io)
913 			do_io_probe(s, start, size);
914 #endif
915 		break;
916 	case REMOVE_MANAGED_RESOURCE:
917 		sub_interval(&data->io_db, start, size);
918 		break;
919 	default:
920 		ret = -EINVAL;
921 		break;
922 	}
923 
924 	return ret;
925 }
926 
927 
928 #ifdef CONFIG_PCI
929 static int nonstatic_autoadd_resources(struct pcmcia_socket *s)
930 {
931 	struct resource *res;
932 	int i, done = 0;
933 
934 	if (!s->cb_dev || !s->cb_dev->bus)
935 		return -ENODEV;
936 
937 #if defined(CONFIG_X86)
938 	/* If this is the root bus, the risk of hitting some strange
939 	 * system devices is too high: If a driver isn't loaded, the
940 	 * resources are not claimed; even if a driver is loaded, it
941 	 * may not request all resources or even the wrong one. We
942 	 * can neither trust the rest of the kernel nor ACPI/PNP and
943 	 * CRS parsing to get it right. Therefore, use several
944 	 * safeguards:
945 	 *
946 	 * - Do not auto-add resources if the CardBus bridge is on
947 	 *   the PCI root bus
948 	 *
949 	 * - Avoid any I/O ports < 0x100.
950 	 *
951 	 * - On PCI-PCI bridges, only use resources which are set up
952 	 *   exclusively for the secondary PCI bus: the risk of hitting
953 	 *   system devices is quite low, as they usually aren't
954 	 *   connected to the secondary PCI bus.
955 	 */
956 	if (s->cb_dev->bus->number == 0)
957 		return -EINVAL;
958 
959 	for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {
960 		res = s->cb_dev->bus->resource[i];
961 #else
962 	pci_bus_for_each_resource(s->cb_dev->bus, res, i) {
963 #endif
964 		if (!res)
965 			continue;
966 
967 		if (res->flags & IORESOURCE_IO) {
968 			/* safeguard against the root resource, where the
969 			 * risk of hitting any other device would be too
970 			 * high */
971 			if (res == &ioport_resource)
972 				continue;
973 
974 			dev_printk(KERN_INFO, &s->cb_dev->dev,
975 				   "pcmcia: parent PCI bridge window: %pR\n",
976 				   res);
977 			if (!adjust_io(s, ADD_MANAGED_RESOURCE, res->start, res->end))
978 				done |= IORESOURCE_IO;
979 
980 		}
981 
982 		if (res->flags & IORESOURCE_MEM) {
983 			/* safeguard against the root resource, where the
984 			 * risk of hitting any other device would be too
985 			 * high */
986 			if (res == &iomem_resource)
987 				continue;
988 
989 			dev_printk(KERN_INFO, &s->cb_dev->dev,
990 				   "pcmcia: parent PCI bridge window: %pR\n",
991 				   res);
992 			if (!adjust_memory(s, ADD_MANAGED_RESOURCE, res->start, res->end))
993 				done |= IORESOURCE_MEM;
994 		}
995 	}
996 
997 	/* if we got at least one of IO, and one of MEM, we can be glad and
998 	 * activate the PCMCIA subsystem */
999 	if (done == (IORESOURCE_MEM | IORESOURCE_IO))
1000 		s->resource_setup_done = 1;
1001 
1002 	return 0;
1003 }
1004 
1005 #else
1006 
1007 static inline int nonstatic_autoadd_resources(struct pcmcia_socket *s)
1008 {
1009 	return -ENODEV;
1010 }
1011 
1012 #endif
1013 
1014 
1015 static int nonstatic_init(struct pcmcia_socket *s)
1016 {
1017 	struct socket_data *data;
1018 
1019 	data = kzalloc(sizeof(struct socket_data), GFP_KERNEL);
1020 	if (!data)
1021 		return -ENOMEM;
1022 
1023 	data->mem_db.next = &data->mem_db;
1024 	data->mem_db_valid.next = &data->mem_db_valid;
1025 	data->io_db.next = &data->io_db;
1026 
1027 	s->resource_data = (void *) data;
1028 
1029 	nonstatic_autoadd_resources(s);
1030 
1031 	return 0;
1032 }
1033 
1034 static void nonstatic_release_resource_db(struct pcmcia_socket *s)
1035 {
1036 	struct socket_data *data = s->resource_data;
1037 	struct resource_map *p, *q;
1038 
1039 	for (p = data->mem_db_valid.next; p != &data->mem_db_valid; p = q) {
1040 		q = p->next;
1041 		kfree(p);
1042 	}
1043 	for (p = data->mem_db.next; p != &data->mem_db; p = q) {
1044 		q = p->next;
1045 		kfree(p);
1046 	}
1047 	for (p = data->io_db.next; p != &data->io_db; p = q) {
1048 		q = p->next;
1049 		kfree(p);
1050 	}
1051 }
1052 
1053 
1054 struct pccard_resource_ops pccard_nonstatic_ops = {
1055 	.validate_mem = pcmcia_nonstatic_validate_mem,
1056 	.find_io = nonstatic_find_io,
1057 	.find_mem = nonstatic_find_mem_region,
1058 	.init = nonstatic_init,
1059 	.exit = nonstatic_release_resource_db,
1060 };
1061 EXPORT_SYMBOL(pccard_nonstatic_ops);
1062 
1063 
1064 /* sysfs interface to the resource database */
1065 
1066 static ssize_t show_io_db(struct device *dev,
1067 			  struct device_attribute *attr, char *buf)
1068 {
1069 	struct pcmcia_socket *s = dev_get_drvdata(dev);
1070 	struct socket_data *data;
1071 	struct resource_map *p;
1072 	ssize_t ret = 0;
1073 
1074 	mutex_lock(&s->ops_mutex);
1075 	data = s->resource_data;
1076 
1077 	for (p = data->io_db.next; p != &data->io_db; p = p->next) {
1078 		if (ret > (PAGE_SIZE - 10))
1079 			continue;
1080 		ret += snprintf(&buf[ret], (PAGE_SIZE - ret - 1),
1081 				"0x%08lx - 0x%08lx\n",
1082 				((unsigned long) p->base),
1083 				((unsigned long) p->base + p->num - 1));
1084 	}
1085 
1086 	mutex_unlock(&s->ops_mutex);
1087 	return ret;
1088 }
1089 
1090 static ssize_t store_io_db(struct device *dev,
1091 			   struct device_attribute *attr,
1092 			   const char *buf, size_t count)
1093 {
1094 	struct pcmcia_socket *s = dev_get_drvdata(dev);
1095 	unsigned long start_addr, end_addr;
1096 	unsigned int add = ADD_MANAGED_RESOURCE;
1097 	ssize_t ret = 0;
1098 
1099 	ret = sscanf(buf, "+ 0x%lx - 0x%lx", &start_addr, &end_addr);
1100 	if (ret != 2) {
1101 		ret = sscanf(buf, "- 0x%lx - 0x%lx", &start_addr, &end_addr);
1102 		add = REMOVE_MANAGED_RESOURCE;
1103 		if (ret != 2) {
1104 			ret = sscanf(buf, "0x%lx - 0x%lx", &start_addr,
1105 				&end_addr);
1106 			add = ADD_MANAGED_RESOURCE;
1107 			if (ret != 2)
1108 				return -EINVAL;
1109 		}
1110 	}
1111 	if (end_addr < start_addr)
1112 		return -EINVAL;
1113 
1114 	mutex_lock(&s->ops_mutex);
1115 	ret = adjust_io(s, add, start_addr, end_addr);
1116 	mutex_unlock(&s->ops_mutex);
1117 
1118 	return ret ? ret : count;
1119 }
1120 static DEVICE_ATTR(available_resources_io, 0600, show_io_db, store_io_db);
1121 
1122 static ssize_t show_mem_db(struct device *dev,
1123 			   struct device_attribute *attr, char *buf)
1124 {
1125 	struct pcmcia_socket *s = dev_get_drvdata(dev);
1126 	struct socket_data *data;
1127 	struct resource_map *p;
1128 	ssize_t ret = 0;
1129 
1130 	mutex_lock(&s->ops_mutex);
1131 	data = s->resource_data;
1132 
1133 	for (p = data->mem_db_valid.next; p != &data->mem_db_valid;
1134 	     p = p->next) {
1135 		if (ret > (PAGE_SIZE - 10))
1136 			continue;
1137 		ret += snprintf(&buf[ret], (PAGE_SIZE - ret - 1),
1138 				"0x%08lx - 0x%08lx\n",
1139 				((unsigned long) p->base),
1140 				((unsigned long) p->base + p->num - 1));
1141 	}
1142 
1143 	for (p = data->mem_db.next; p != &data->mem_db; p = p->next) {
1144 		if (ret > (PAGE_SIZE - 10))
1145 			continue;
1146 		ret += snprintf(&buf[ret], (PAGE_SIZE - ret - 1),
1147 				"0x%08lx - 0x%08lx\n",
1148 				((unsigned long) p->base),
1149 				((unsigned long) p->base + p->num - 1));
1150 	}
1151 
1152 	mutex_unlock(&s->ops_mutex);
1153 	return ret;
1154 }
1155 
1156 static ssize_t store_mem_db(struct device *dev,
1157 			    struct device_attribute *attr,
1158 			    const char *buf, size_t count)
1159 {
1160 	struct pcmcia_socket *s = dev_get_drvdata(dev);
1161 	unsigned long start_addr, end_addr;
1162 	unsigned int add = ADD_MANAGED_RESOURCE;
1163 	ssize_t ret = 0;
1164 
1165 	ret = sscanf(buf, "+ 0x%lx - 0x%lx", &start_addr, &end_addr);
1166 	if (ret != 2) {
1167 		ret = sscanf(buf, "- 0x%lx - 0x%lx", &start_addr, &end_addr);
1168 		add = REMOVE_MANAGED_RESOURCE;
1169 		if (ret != 2) {
1170 			ret = sscanf(buf, "0x%lx - 0x%lx", &start_addr,
1171 				&end_addr);
1172 			add = ADD_MANAGED_RESOURCE;
1173 			if (ret != 2)
1174 				return -EINVAL;
1175 		}
1176 	}
1177 	if (end_addr < start_addr)
1178 		return -EINVAL;
1179 
1180 	mutex_lock(&s->ops_mutex);
1181 	ret = adjust_memory(s, add, start_addr, end_addr);
1182 	mutex_unlock(&s->ops_mutex);
1183 
1184 	return ret ? ret : count;
1185 }
1186 static DEVICE_ATTR(available_resources_mem, 0600, show_mem_db, store_mem_db);
1187 
1188 static struct attribute *pccard_rsrc_attributes[] = {
1189 	&dev_attr_available_resources_io.attr,
1190 	&dev_attr_available_resources_mem.attr,
1191 	NULL,
1192 };
1193 
1194 static const struct attribute_group rsrc_attributes = {
1195 	.attrs = pccard_rsrc_attributes,
1196 };
1197 
1198 static int __devinit pccard_sysfs_add_rsrc(struct device *dev,
1199 					   struct class_interface *class_intf)
1200 {
1201 	struct pcmcia_socket *s = dev_get_drvdata(dev);
1202 
1203 	if (s->resource_ops != &pccard_nonstatic_ops)
1204 		return 0;
1205 	return sysfs_create_group(&dev->kobj, &rsrc_attributes);
1206 }
1207 
1208 static void __devexit pccard_sysfs_remove_rsrc(struct device *dev,
1209 					       struct class_interface *class_intf)
1210 {
1211 	struct pcmcia_socket *s = dev_get_drvdata(dev);
1212 
1213 	if (s->resource_ops != &pccard_nonstatic_ops)
1214 		return;
1215 	sysfs_remove_group(&dev->kobj, &rsrc_attributes);
1216 }
1217 
1218 static struct class_interface pccard_rsrc_interface __refdata = {
1219 	.class = &pcmcia_socket_class,
1220 	.add_dev = &pccard_sysfs_add_rsrc,
1221 	.remove_dev = __devexit_p(&pccard_sysfs_remove_rsrc),
1222 };
1223 
1224 static int __init nonstatic_sysfs_init(void)
1225 {
1226 	return class_interface_register(&pccard_rsrc_interface);
1227 }
1228 
1229 static void __exit nonstatic_sysfs_exit(void)
1230 {
1231 	class_interface_unregister(&pccard_rsrc_interface);
1232 }
1233 
1234 module_init(nonstatic_sysfs_init);
1235 module_exit(nonstatic_sysfs_exit);
1236