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