2 * rsrc_nonstatic.c -- Resource management routines for !SS_CAP_STATIC_MAP sockets
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.
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.
12 * (C) 1999 David A. Hinds
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>
31 #include <pcmcia/ss.h>
32 #include <pcmcia/cs.h>
33 #include <pcmcia/cistpl.h>
34 #include "cs_internal.h"
36 /* moved to rsrc_mgr.c
37 MODULE_AUTHOR("David A. Hinds, Dominik Brodowski");
38 MODULE_LICENSE("GPL");
41 /* Parameters that can be set with 'insmod' */
43 #define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0444)
45 INT_MODULE_PARM(probe_mem
, 1); /* memory probe? */
46 #ifdef CONFIG_PCMCIA_PROBE
47 INT_MODULE_PARM(probe_io
, 1); /* IO port probe? */
48 INT_MODULE_PARM(mem_limit
, 0x10000);
51 /* for io_db and mem_db */
54 struct resource_map
*next
;
58 struct resource_map mem_db
;
59 struct resource_map mem_db_valid
;
60 struct resource_map io_db
;
63 #define MEM_PROBE_LOW (1 << 0)
64 #define MEM_PROBE_HIGH (1 << 1)
67 #define REMOVE_MANAGED_RESOURCE 1
68 #define ADD_MANAGED_RESOURCE 2
70 /*======================================================================
72 Linux resource management extensions
74 ======================================================================*/
76 static struct resource
*
77 claim_region(struct pcmcia_socket
*s
, resource_size_t base
,
78 resource_size_t size
, int type
, char *name
)
80 struct resource
*res
, *parent
;
82 parent
= type
& IORESOURCE_MEM
? &iomem_resource
: &ioport_resource
;
83 res
= pcmcia_make_resource(base
, size
, type
| IORESOURCE_BUSY
, name
);
88 parent
= pci_find_parent_resource(s
->cb_dev
, res
);
90 if (!parent
|| request_resource(parent
, res
)) {
98 static void free_region(struct resource
*res
)
101 release_resource(res
);
106 /*======================================================================
108 These manage the internal databases of available resources.
110 ======================================================================*/
112 static int add_interval(struct resource_map
*map
, u_long base
, u_long num
)
114 struct resource_map
*p
, *q
;
116 for (p
= map
; ; p
= p
->next
) {
117 if ((p
!= map
) && (p
->base
+p
->num
>= base
)) {
118 p
->num
= max(num
+ base
- p
->base
, p
->num
);
121 if ((p
->next
== map
) || (p
->next
->base
> base
+num
-1))
124 q
= kmalloc(sizeof(struct resource_map
), GFP_KERNEL
);
126 printk(KERN_WARNING
"out of memory to update resources\n");
129 q
->base
= base
; q
->num
= num
;
130 q
->next
= p
->next
; p
->next
= q
;
134 /*====================================================================*/
136 static int sub_interval(struct resource_map
*map
, u_long base
, u_long num
)
138 struct resource_map
*p
, *q
;
140 for (p
= map
; ; p
= q
) {
144 if ((q
->base
+q
->num
> base
) && (base
+num
> q
->base
)) {
145 if (q
->base
>= base
) {
146 if (q
->base
+q
->num
<= base
+num
) {
147 /* Delete whole block */
150 /* don't advance the pointer yet */
153 /* Cut off bit from the front */
154 q
->num
= q
->base
+ q
->num
- base
- num
;
155 q
->base
= base
+ num
;
157 } else if (q
->base
+q
->num
<= base
+num
) {
158 /* Cut off bit from the end */
159 q
->num
= base
- q
->base
;
161 /* Split the block into two pieces */
162 p
= kmalloc(sizeof(struct resource_map
),
165 printk(KERN_WARNING
"out of memory to update resources\n");
169 p
->num
= q
->base
+q
->num
- p
->base
;
170 q
->num
= base
- q
->base
;
171 p
->next
= q
->next
; q
->next
= p
;
178 /*======================================================================
180 These routines examine a region of IO or memory addresses to
181 determine what ranges might be genuinely available.
183 ======================================================================*/
185 #ifdef CONFIG_PCMCIA_PROBE
186 static void do_io_probe(struct pcmcia_socket
*s
, unsigned int base
,
189 struct resource
*res
;
190 struct socket_data
*s_data
= s
->resource_data
;
191 unsigned int i
, j
, bad
;
193 u_char
*b
, hole
, most
;
195 dev_printk(KERN_INFO
, &s
->dev
, "cs: IO port probe %#x-%#x:",
198 /* First, what does a floating port look like? */
199 b
= kzalloc(256, GFP_KERNEL
);
202 dev_printk(KERN_ERR
, &s
->dev
,
203 "do_io_probe: unable to kmalloc 256 bytes");
206 for (i
= base
, most
= 0; i
< base
+num
; i
+= 8) {
207 res
= claim_region(s
, i
, 8, IORESOURCE_IO
, "PCMCIA ioprobe");
211 for (j
= 1; j
< 8; j
++)
212 if (inb(i
+j
) != hole
)
215 if ((j
== 8) && (++b
[hole
] > b
[most
]))
223 for (i
= base
; i
< base
+num
; i
+= 8) {
224 res
= claim_region(s
, i
, 8, IORESOURCE_IO
, "PCMCIA ioprobe");
227 printk(" excluding");
232 for (j
= 0; j
< 8; j
++)
233 if (inb(i
+j
) != most
)
238 printk(" excluding");
243 sub_interval(&s_data
->io_db
, bad
, i
-bad
);
244 printk(" %#x-%#x", bad
, i
-1);
250 if ((num
> 16) && (bad
== base
) && (i
== base
+num
)) {
251 sub_interval(&s_data
->io_db
, bad
, i
-bad
);
252 printk(" nothing: probe failed.\n");
255 sub_interval(&s_data
->io_db
, bad
, i
-bad
);
256 printk(" %#x-%#x", bad
, i
-1);
260 printk(any
? "\n" : " clean.\n");
264 /*======================================================================*/
267 * readable() - iomem validation function for cards with a valid CIS
269 static int readable(struct pcmcia_socket
*s
, struct resource
*res
,
275 dev_dbg(&s
->dev
, "fake CIS is being used: can't validate mem\n");
279 s
->cis_mem
.res
= res
;
280 s
->cis_virt
= ioremap(res
->start
, s
->map_size
);
282 mutex_unlock(&s
->ops_mutex
);
283 /* as we're only called from pcmcia.c, we're safe */
284 if (s
->callback
->validate
)
285 ret
= s
->callback
->validate(s
, count
);
286 /* invalidate mapping */
287 mutex_lock(&s
->ops_mutex
);
288 iounmap(s
->cis_virt
);
291 s
->cis_mem
.res
= NULL
;
292 if ((ret
) || (*count
== 0))
298 * checksum() - iomem validation function for simple memory cards
300 static int checksum(struct pcmcia_socket
*s
, struct resource
*res
,
304 int i
, a
= 0, b
= -1, d
;
307 virt
= ioremap(res
->start
, s
->map_size
);
310 map
.flags
= MAP_ACTIVE
;
314 s
->ops
->set_mem_map(s
, &map
);
316 /* Don't bother checking every word... */
317 for (i
= 0; i
< s
->map_size
; i
+= 44) {
324 s
->ops
->set_mem_map(s
, &map
);
338 * do_validate_mem() - low level validate a memory region for PCMCIA use
339 * @s: PCMCIA socket to validate
340 * @base: start address of resource to check
341 * @size: size of resource to check
342 * @validate: validation function to use
344 * do_validate_mem() splits up the memory region which is to be checked
345 * into two parts. Both are passed to the @validate() function. If
346 * @validate() returns non-zero, or the value parameter to @validate()
347 * is zero, or the value parameter is different between both calls,
348 * the check fails, and -EINVAL is returned. Else, 0 is returned.
350 static int do_validate_mem(struct pcmcia_socket
*s
,
351 unsigned long base
, unsigned long size
,
352 int validate (struct pcmcia_socket
*s
,
353 struct resource
*res
,
354 unsigned int *value
))
356 struct socket_data
*s_data
= s
->resource_data
;
357 struct resource
*res1
, *res2
;
358 unsigned int info1
= 1, info2
= 1;
361 res1
= claim_region(s
, base
, size
/2, IORESOURCE_MEM
, "PCMCIA memprobe");
362 res2
= claim_region(s
, base
+ size
/2, size
/2, IORESOURCE_MEM
,
368 ret
= validate(s
, res1
, &info1
);
369 ret
+= validate(s
, res2
, &info2
);
376 dev_dbg(&s
->dev
, "cs: memory probe 0x%06lx-0x%06lx: %p %p %u %u %u",
377 base
, base
+size
-1, res1
, res2
, ret
, info1
, info2
);
379 if ((ret
) || (info1
!= info2
) || (info1
== 0))
382 if (validate
&& !s
->fake_cis
) {
383 /* move it to the validated data set */
384 add_interval(&s_data
->mem_db_valid
, base
, size
);
385 sub_interval(&s_data
->mem_db
, base
, size
);
393 * do_mem_probe() - validate a memory region for PCMCIA use
394 * @s: PCMCIA socket to validate
395 * @base: start address of resource to check
396 * @num: size of resource to check
397 * @validate: validation function to use
398 * @fallback: validation function to use if validate fails
400 * do_mem_probe() checks a memory region for use by the PCMCIA subsystem.
401 * To do so, the area is split up into sensible parts, and then passed
402 * into the @validate() function. Only if @validate() and @fallback() fail,
403 * the area is marked as unavaibale for use by the PCMCIA subsystem. The
404 * function returns the size of the usable memory area.
406 static int do_mem_probe(struct pcmcia_socket
*s
, u_long base
, u_long num
,
407 int validate (struct pcmcia_socket
*s
,
408 struct resource
*res
,
409 unsigned int *value
),
410 int fallback (struct pcmcia_socket
*s
,
411 struct resource
*res
,
412 unsigned int *value
))
414 struct socket_data
*s_data
= s
->resource_data
;
415 u_long i
, j
, bad
, fail
, step
;
417 dev_printk(KERN_INFO
, &s
->dev
, "cs: memory probe 0x%06lx-0x%06lx:",
420 step
= (num
< 0x20000) ? 0x2000 : ((num
>>4) & ~0x1fff);
421 /* don't allow too large steps */
424 /* cis_readable wants to map 2x map_size */
425 if (step
< 2 * s
->map_size
)
426 step
= 2 * s
->map_size
;
427 for (i
= j
= base
; i
< base
+num
; i
= j
+ step
) {
429 for (j
= i
; j
< base
+num
; j
+= step
) {
430 if (!do_validate_mem(s
, j
, step
, validate
))
433 fail
= ((i
== base
) && (j
== base
+num
));
435 if ((fail
) && (fallback
)) {
436 for (j
= i
; j
< base
+num
; j
+= step
)
437 if (!do_validate_mem(s
, j
, step
, fallback
))
442 printk(" excluding");
443 printk(" %#05lx-%#05lx", i
, j
-1);
444 sub_interval(&s_data
->mem_db
, i
, j
-i
);
448 printk(bad
? "\n" : " clean.\n");
453 #ifdef CONFIG_PCMCIA_PROBE
456 * inv_probe() - top-to-bottom search for one usuable high memory area
457 * @s: PCMCIA socket to validate
458 * @m: resource_map to check
460 static u_long
inv_probe(struct resource_map
*m
, struct pcmcia_socket
*s
)
462 struct socket_data
*s_data
= s
->resource_data
;
464 if (m
== &s_data
->mem_db
)
466 ok
= inv_probe(m
->next
, s
);
468 if (m
->base
>= 0x100000)
469 sub_interval(&s_data
->mem_db
, m
->base
, m
->num
);
472 if (m
->base
< 0x100000)
474 return do_mem_probe(s
, m
->base
, m
->num
, readable
, checksum
);
478 * validate_mem() - memory probe function
479 * @s: PCMCIA socket to validate
480 * @probe_mask: MEM_PROBE_LOW | MEM_PROBE_HIGH
482 * The memory probe. If the memory list includes a 64K-aligned block
483 * below 1MB, we probe in 64K chunks, and as soon as we accumulate at
484 * least mem_limit free space, we quit. Returns 0 on usuable ports.
486 static int validate_mem(struct pcmcia_socket
*s
, unsigned int probe_mask
)
488 struct resource_map
*m
, mm
;
489 static unsigned char order
[] = { 0xd0, 0xe0, 0xc0, 0xf0 };
490 unsigned long b
, i
, ok
= 0;
491 struct socket_data
*s_data
= s
->resource_data
;
493 /* We do up to four passes through the list */
494 if (probe_mask
& MEM_PROBE_HIGH
) {
495 if (inv_probe(s_data
->mem_db
.next
, s
) > 0)
497 if (s_data
->mem_db_valid
.next
!= &s_data
->mem_db_valid
)
499 dev_printk(KERN_NOTICE
, &s
->dev
,
500 "cs: warning: no high memory space available!\n");
504 for (m
= s_data
->mem_db
.next
; m
!= &s_data
->mem_db
; m
= mm
.next
) {
506 /* Only probe < 1 MB */
507 if (mm
.base
>= 0x100000)
509 if ((mm
.base
| mm
.num
) & 0xffff) {
510 ok
+= do_mem_probe(s
, mm
.base
, mm
.num
, readable
,
514 /* Special probe for 64K-aligned block */
515 for (i
= 0; i
< 4; i
++) {
517 if ((b
>= mm
.base
) && (b
+0x10000 <= mm
.base
+mm
.num
)) {
519 sub_interval(&s_data
->mem_db
, b
, 0x10000);
521 ok
+= do_mem_probe(s
, b
, 0x10000,
533 #else /* CONFIG_PCMCIA_PROBE */
536 * validate_mem() - memory probe function
537 * @s: PCMCIA socket to validate
538 * @probe_mask: ignored
540 * Returns 0 on usuable ports.
542 static int validate_mem(struct pcmcia_socket
*s
, unsigned int probe_mask
)
544 struct resource_map
*m
, mm
;
545 struct socket_data
*s_data
= s
->resource_data
;
546 unsigned long ok
= 0;
548 for (m
= s_data
->mem_db
.next
; m
!= &s_data
->mem_db
; m
= mm
.next
) {
550 ok
+= do_mem_probe(s
, mm
.base
, mm
.num
, readable
, checksum
);
557 #endif /* CONFIG_PCMCIA_PROBE */
561 * pcmcia_nonstatic_validate_mem() - try to validate iomem for PCMCIA use
562 * @s: PCMCIA socket to validate
564 * This is tricky... when we set up CIS memory, we try to validate
565 * the memory window space allocations.
567 * Locking note: Must be called with skt_mutex held!
569 static int pcmcia_nonstatic_validate_mem(struct pcmcia_socket
*s
)
571 struct socket_data
*s_data
= s
->resource_data
;
572 unsigned int probe_mask
= MEM_PROBE_LOW
;
575 if (!probe_mem
|| !(s
->state
& SOCKET_PRESENT
))
578 if (s
->features
& SS_CAP_PAGE_REGS
)
579 probe_mask
= MEM_PROBE_HIGH
;
581 ret
= validate_mem(s
, probe_mask
);
583 if (s_data
->mem_db_valid
.next
!= &s_data
->mem_db_valid
)
589 struct pcmcia_align_data
{
591 unsigned long offset
;
592 struct resource_map
*map
;
595 static resource_size_t
pcmcia_common_align(struct pcmcia_align_data
*align_data
,
596 resource_size_t start
)
600 * Ensure that we have the correct start address
602 ret
= (start
& ~align_data
->mask
) + align_data
->offset
;
604 ret
+= align_data
->mask
+ 1;
608 static resource_size_t
609 pcmcia_align(void *align_data
, const struct resource
*res
,
610 resource_size_t size
, resource_size_t align
)
612 struct pcmcia_align_data
*data
= align_data
;
613 struct resource_map
*m
;
614 resource_size_t start
;
616 start
= pcmcia_common_align(data
, res
->start
);
618 for (m
= data
->map
->next
; m
!= data
->map
; m
= m
->next
) {
619 unsigned long map_start
= m
->base
;
620 unsigned long map_end
= m
->base
+ m
->num
- 1;
623 * If the lower resources are not available, try aligning
624 * to this entry of the resource database to see if it'll
627 if (start
< map_start
)
628 start
= pcmcia_common_align(data
, map_start
);
631 * If we're above the area which was passed in, there's
632 * no point proceeding.
634 if (start
>= res
->end
)
637 if ((start
+ size
- 1) <= map_end
)
642 * If we failed to find something suitable, ensure we fail.
651 * Adjust an existing IO region allocation, but making sure that we don't
652 * encroach outside the resources which the user supplied.
654 static int __nonstatic_adjust_io_region(struct pcmcia_socket
*s
,
655 unsigned long r_start
,
658 struct resource_map
*m
;
659 struct socket_data
*s_data
= s
->resource_data
;
662 for (m
= s_data
->io_db
.next
; m
!= &s_data
->io_db
; m
= m
->next
) {
663 unsigned long start
= m
->base
;
664 unsigned long end
= m
->base
+ m
->num
- 1;
666 if (start
> r_start
|| r_end
> end
)
675 /*======================================================================
677 These find ranges of I/O ports or memory addresses that are not
678 currently allocated by other devices.
680 The 'align' field should reflect the number of bits of address
681 that need to be preserved from the initial value of *base. It
682 should be a power of two, greater than or equal to 'num'. A value
683 of 0 means that all bits of *base are significant. *base should
684 also be strictly less than 'align'.
686 ======================================================================*/
688 static struct resource
*__nonstatic_find_io_region(struct pcmcia_socket
*s
,
689 unsigned long base
, int num
,
692 struct resource
*res
= pcmcia_make_resource(0, num
, IORESOURCE_IO
,
694 struct socket_data
*s_data
= s
->resource_data
;
695 struct pcmcia_align_data data
;
696 unsigned long min
= base
;
699 data
.mask
= align
- 1;
700 data
.offset
= base
& data
.mask
;
701 data
.map
= &s_data
->io_db
;
705 ret
= pci_bus_alloc_resource(s
->cb_dev
->bus
, res
, num
, 1,
706 min
, 0, pcmcia_align
, &data
);
709 ret
= allocate_resource(&ioport_resource
, res
, num
, min
, ~0UL,
710 1, pcmcia_align
, &data
);
719 static int nonstatic_find_io(struct pcmcia_socket
*s
, unsigned int attr
,
720 unsigned int *base
, unsigned int num
,
721 unsigned int align
, struct resource
**parent
)
725 /* Check for an already-allocated window that must conflict with
726 * what was asked for. It is a hack because it does not catch all
727 * potential conflicts, just the most obvious ones.
729 for (i
= 0; i
< MAX_IO_WIN
; i
++) {
736 if ((s
->io
[i
].res
->start
& (align
-1)) == *base
)
740 for (i
= 0; i
< MAX_IO_WIN
; i
++) {
741 struct resource
*res
= s
->io
[i
].res
;
744 if (res
&& (res
->flags
& IORESOURCE_BITS
) !=
745 (attr
& IORESOURCE_BITS
))
752 res
= s
->io
[i
].res
= __nonstatic_find_io_region(s
,
759 s
->io
[i
].res
->flags
=
760 ((res
->flags
& ~IORESOURCE_BITS
) |
761 (attr
& IORESOURCE_BITS
));
762 s
->io
[i
].InUse
= num
;
767 /* Try to extend top of window */
769 if ((*base
== 0) || (*base
== try)) {
770 ret
= __nonstatic_adjust_io_region(s
, res
->start
,
773 ret
= adjust_resource(s
->io
[i
].res
, res
->start
,
774 res
->end
- res
->start
+ num
+ 1);
778 s
->io
[i
].InUse
+= num
;
784 /* Try to extend bottom of window */
785 try = res
->start
- num
;
786 if ((*base
== 0) || (*base
== try)) {
787 ret
= __nonstatic_adjust_io_region(s
,
791 ret
= adjust_resource(s
->io
[i
].res
,
793 res
->end
- res
->start
+ num
+ 1);
797 s
->io
[i
].InUse
+= num
;
808 static struct resource
*nonstatic_find_mem_region(u_long base
, u_long num
,
809 u_long align
, int low
, struct pcmcia_socket
*s
)
811 struct resource
*res
= pcmcia_make_resource(0, num
, IORESOURCE_MEM
,
813 struct socket_data
*s_data
= s
->resource_data
;
814 struct pcmcia_align_data data
;
815 unsigned long min
, max
;
818 low
= low
|| !(s
->features
& SS_CAP_PAGE_REGS
);
820 data
.mask
= align
- 1;
821 data
.offset
= base
& data
.mask
;
823 for (i
= 0; i
< 2; i
++) {
824 data
.map
= &s_data
->mem_db_valid
;
827 min
= base
< max
? base
: 0;
830 min
= 0x100000UL
+ base
;
833 for (j
= 0; j
< 2; j
++) {
836 ret
= pci_bus_alloc_resource(s
->cb_dev
->bus
,
838 pcmcia_align
, &data
);
842 ret
= allocate_resource(&iomem_resource
,
843 res
, num
, min
, max
, 1,
844 pcmcia_align
, &data
);
848 data
.map
= &s_data
->mem_db
;
863 static int adjust_memory(struct pcmcia_socket
*s
, unsigned int action
, unsigned long start
, unsigned long end
)
865 struct socket_data
*data
= s
->resource_data
;
866 unsigned long size
= end
- start
+ 1;
873 case ADD_MANAGED_RESOURCE
:
874 ret
= add_interval(&data
->mem_db
, start
, size
);
876 do_mem_probe(s
, start
, size
, NULL
, NULL
);
878 case REMOVE_MANAGED_RESOURCE
:
879 ret
= sub_interval(&data
->mem_db
, start
, size
);
889 static int adjust_io(struct pcmcia_socket
*s
, unsigned int action
, unsigned long start
, unsigned long end
)
891 struct socket_data
*data
= s
->resource_data
;
895 #if defined(CONFIG_X86)
896 /* on x86, avoid anything < 0x100 for it is often used for
897 * legacy platform devices */
902 size
= end
- start
+ 1;
907 if (end
> IO_SPACE_LIMIT
)
911 case ADD_MANAGED_RESOURCE
:
912 if (add_interval(&data
->io_db
, start
, size
) != 0) {
916 #ifdef CONFIG_PCMCIA_PROBE
918 do_io_probe(s
, start
, size
);
921 case REMOVE_MANAGED_RESOURCE
:
922 sub_interval(&data
->io_db
, start
, size
);
934 static int nonstatic_autoadd_resources(struct pcmcia_socket
*s
)
936 struct resource
*res
;
939 if (!s
->cb_dev
|| !s
->cb_dev
->bus
)
942 #if defined(CONFIG_X86)
943 /* If this is the root bus, the risk of hitting some strange
944 * system devices is too high: If a driver isn't loaded, the
945 * resources are not claimed; even if a driver is loaded, it
946 * may not request all resources or even the wrong one. We
947 * can neither trust the rest of the kernel nor ACPI/PNP and
948 * CRS parsing to get it right. Therefore, use several
951 * - Do not auto-add resources if the CardBus bridge is on
954 * - Avoid any I/O ports < 0x100.
956 * - On PCI-PCI bridges, only use resources which are set up
957 * exclusively for the secondary PCI bus: the risk of hitting
958 * system devices is quite low, as they usually aren't
959 * connected to the secondary PCI bus.
961 if (s
->cb_dev
->bus
->number
== 0)
964 for (i
= 0; i
< PCI_BRIDGE_RESOURCE_NUM
; i
++) {
965 res
= s
->cb_dev
->bus
->resource
[i
];
967 pci_bus_for_each_resource(s
->cb_dev
->bus
, res
, i
) {
972 if (res
->flags
& IORESOURCE_IO
) {
973 /* safeguard against the root resource, where the
974 * risk of hitting any other device would be too
976 if (res
== &ioport_resource
)
979 dev_printk(KERN_INFO
, &s
->cb_dev
->dev
,
980 "pcmcia: parent PCI bridge window: %pR\n",
982 if (!adjust_io(s
, ADD_MANAGED_RESOURCE
, res
->start
, res
->end
))
983 done
|= IORESOURCE_IO
;
987 if (res
->flags
& IORESOURCE_MEM
) {
988 /* safeguard against the root resource, where the
989 * risk of hitting any other device would be too
991 if (res
== &iomem_resource
)
994 dev_printk(KERN_INFO
, &s
->cb_dev
->dev
,
995 "pcmcia: parent PCI bridge window: %pR\n",
997 if (!adjust_memory(s
, ADD_MANAGED_RESOURCE
, res
->start
, res
->end
))
998 done
|= IORESOURCE_MEM
;
1002 /* if we got at least one of IO, and one of MEM, we can be glad and
1003 * activate the PCMCIA subsystem */
1004 if (done
== (IORESOURCE_MEM
| IORESOURCE_IO
))
1005 s
->resource_setup_done
= 1;
1012 static inline int nonstatic_autoadd_resources(struct pcmcia_socket
*s
)
1020 static int nonstatic_init(struct pcmcia_socket
*s
)
1022 struct socket_data
*data
;
1024 data
= kzalloc(sizeof(struct socket_data
), GFP_KERNEL
);
1028 data
->mem_db
.next
= &data
->mem_db
;
1029 data
->mem_db_valid
.next
= &data
->mem_db_valid
;
1030 data
->io_db
.next
= &data
->io_db
;
1032 s
->resource_data
= (void *) data
;
1034 nonstatic_autoadd_resources(s
);
1039 static void nonstatic_release_resource_db(struct pcmcia_socket
*s
)
1041 struct socket_data
*data
= s
->resource_data
;
1042 struct resource_map
*p
, *q
;
1044 for (p
= data
->mem_db_valid
.next
; p
!= &data
->mem_db_valid
; p
= q
) {
1048 for (p
= data
->mem_db
.next
; p
!= &data
->mem_db
; p
= q
) {
1052 for (p
= data
->io_db
.next
; p
!= &data
->io_db
; p
= q
) {
1059 struct pccard_resource_ops pccard_nonstatic_ops
= {
1060 .validate_mem
= pcmcia_nonstatic_validate_mem
,
1061 .find_io
= nonstatic_find_io
,
1062 .find_mem
= nonstatic_find_mem_region
,
1063 .init
= nonstatic_init
,
1064 .exit
= nonstatic_release_resource_db
,
1066 EXPORT_SYMBOL(pccard_nonstatic_ops
);
1069 /* sysfs interface to the resource database */
1071 static ssize_t
show_io_db(struct device
*dev
,
1072 struct device_attribute
*attr
, char *buf
)
1074 struct pcmcia_socket
*s
= dev_get_drvdata(dev
);
1075 struct socket_data
*data
;
1076 struct resource_map
*p
;
1079 mutex_lock(&s
->ops_mutex
);
1080 data
= s
->resource_data
;
1082 for (p
= data
->io_db
.next
; p
!= &data
->io_db
; p
= p
->next
) {
1083 if (ret
> (PAGE_SIZE
- 10))
1085 ret
+= snprintf(&buf
[ret
], (PAGE_SIZE
- ret
- 1),
1086 "0x%08lx - 0x%08lx\n",
1087 ((unsigned long) p
->base
),
1088 ((unsigned long) p
->base
+ p
->num
- 1));
1091 mutex_unlock(&s
->ops_mutex
);
1095 static ssize_t
store_io_db(struct device
*dev
,
1096 struct device_attribute
*attr
,
1097 const char *buf
, size_t count
)
1099 struct pcmcia_socket
*s
= dev_get_drvdata(dev
);
1100 unsigned long start_addr
, end_addr
;
1101 unsigned int add
= ADD_MANAGED_RESOURCE
;
1104 ret
= sscanf(buf
, "+ 0x%lx - 0x%lx", &start_addr
, &end_addr
);
1106 ret
= sscanf(buf
, "- 0x%lx - 0x%lx", &start_addr
, &end_addr
);
1107 add
= REMOVE_MANAGED_RESOURCE
;
1109 ret
= sscanf(buf
, "0x%lx - 0x%lx", &start_addr
,
1111 add
= ADD_MANAGED_RESOURCE
;
1116 if (end_addr
< start_addr
)
1119 mutex_lock(&s
->ops_mutex
);
1120 ret
= adjust_io(s
, add
, start_addr
, end_addr
);
1121 mutex_unlock(&s
->ops_mutex
);
1123 return ret
? ret
: count
;
1125 static DEVICE_ATTR(available_resources_io
, 0600, show_io_db
, store_io_db
);
1127 static ssize_t
show_mem_db(struct device
*dev
,
1128 struct device_attribute
*attr
, char *buf
)
1130 struct pcmcia_socket
*s
= dev_get_drvdata(dev
);
1131 struct socket_data
*data
;
1132 struct resource_map
*p
;
1135 mutex_lock(&s
->ops_mutex
);
1136 data
= s
->resource_data
;
1138 for (p
= data
->mem_db_valid
.next
; p
!= &data
->mem_db_valid
;
1140 if (ret
> (PAGE_SIZE
- 10))
1142 ret
+= snprintf(&buf
[ret
], (PAGE_SIZE
- ret
- 1),
1143 "0x%08lx - 0x%08lx\n",
1144 ((unsigned long) p
->base
),
1145 ((unsigned long) p
->base
+ p
->num
- 1));
1148 for (p
= data
->mem_db
.next
; p
!= &data
->mem_db
; p
= p
->next
) {
1149 if (ret
> (PAGE_SIZE
- 10))
1151 ret
+= snprintf(&buf
[ret
], (PAGE_SIZE
- ret
- 1),
1152 "0x%08lx - 0x%08lx\n",
1153 ((unsigned long) p
->base
),
1154 ((unsigned long) p
->base
+ p
->num
- 1));
1157 mutex_unlock(&s
->ops_mutex
);
1161 static ssize_t
store_mem_db(struct device
*dev
,
1162 struct device_attribute
*attr
,
1163 const char *buf
, size_t count
)
1165 struct pcmcia_socket
*s
= dev_get_drvdata(dev
);
1166 unsigned long start_addr
, end_addr
;
1167 unsigned int add
= ADD_MANAGED_RESOURCE
;
1170 ret
= sscanf(buf
, "+ 0x%lx - 0x%lx", &start_addr
, &end_addr
);
1172 ret
= sscanf(buf
, "- 0x%lx - 0x%lx", &start_addr
, &end_addr
);
1173 add
= REMOVE_MANAGED_RESOURCE
;
1175 ret
= sscanf(buf
, "0x%lx - 0x%lx", &start_addr
,
1177 add
= ADD_MANAGED_RESOURCE
;
1182 if (end_addr
< start_addr
)
1185 mutex_lock(&s
->ops_mutex
);
1186 ret
= adjust_memory(s
, add
, start_addr
, end_addr
);
1187 mutex_unlock(&s
->ops_mutex
);
1189 return ret
? ret
: count
;
1191 static DEVICE_ATTR(available_resources_mem
, 0600, show_mem_db
, store_mem_db
);
1193 static struct attribute
*pccard_rsrc_attributes
[] = {
1194 &dev_attr_available_resources_io
.attr
,
1195 &dev_attr_available_resources_mem
.attr
,
1199 static const struct attribute_group rsrc_attributes
= {
1200 .attrs
= pccard_rsrc_attributes
,
1203 static int __devinit
pccard_sysfs_add_rsrc(struct device
*dev
,
1204 struct class_interface
*class_intf
)
1206 struct pcmcia_socket
*s
= dev_get_drvdata(dev
);
1208 if (s
->resource_ops
!= &pccard_nonstatic_ops
)
1210 return sysfs_create_group(&dev
->kobj
, &rsrc_attributes
);
1213 static void __devexit
pccard_sysfs_remove_rsrc(struct device
*dev
,
1214 struct class_interface
*class_intf
)
1216 struct pcmcia_socket
*s
= dev_get_drvdata(dev
);
1218 if (s
->resource_ops
!= &pccard_nonstatic_ops
)
1220 sysfs_remove_group(&dev
->kobj
, &rsrc_attributes
);
1223 static struct class_interface pccard_rsrc_interface __refdata
= {
1224 .class = &pcmcia_socket_class
,
1225 .add_dev
= &pccard_sysfs_add_rsrc
,
1226 .remove_dev
= __devexit_p(&pccard_sysfs_remove_rsrc
),
1229 static int __init
nonstatic_sysfs_init(void)
1231 return class_interface_register(&pccard_rsrc_interface
);
1234 static void __exit
nonstatic_sysfs_exit(void)
1236 class_interface_unregister(&pccard_rsrc_interface
);
1239 module_init(nonstatic_sysfs_init
);
1240 module_exit(nonstatic_sysfs_exit
);