1 // SPDX-License-Identifier: GPL-2.0-only
3 * rsrc_nonstatic.c -- Resource management routines for !SS_CAP_STATIC_MAP sockets
5 * The initial developer of the original code is David A. Hinds
6 * <dahinds@users.sourceforge.net>. Portions created by David A. Hinds
7 * are Copyright (C) 1999 David A. Hinds. All Rights Reserved.
9 * (C) 1999 David A. Hinds
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/kernel.h>
17 #include <linux/errno.h>
18 #include <linux/types.h>
19 #include <linux/slab.h>
20 #include <linux/ioport.h>
21 #include <linux/timer.h>
22 #include <linux/pci.h>
23 #include <linux/device.h>
28 #include <pcmcia/ss.h>
29 #include <pcmcia/cistpl.h>
30 #include "cs_internal.h"
32 /* moved to rsrc_mgr.c
33 MODULE_AUTHOR("David A. Hinds, Dominik Brodowski");
34 MODULE_LICENSE("GPL");
37 /* Parameters that can be set with 'insmod' */
39 #define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0444)
41 INT_MODULE_PARM(probe_mem
, 1); /* memory probe? */
42 #ifdef CONFIG_PCMCIA_PROBE
43 INT_MODULE_PARM(probe_io
, 1); /* IO port probe? */
44 INT_MODULE_PARM(mem_limit
, 0x10000);
47 /* for io_db and mem_db */
50 struct resource_map
*next
;
54 struct resource_map mem_db
;
55 struct resource_map mem_db_valid
;
56 struct resource_map io_db
;
59 #define MEM_PROBE_LOW (1 << 0)
60 #define MEM_PROBE_HIGH (1 << 1)
63 #define REMOVE_MANAGED_RESOURCE 1
64 #define ADD_MANAGED_RESOURCE 2
66 /*======================================================================
68 Linux resource management extensions
70 ======================================================================*/
72 static struct resource
*
73 claim_region(struct pcmcia_socket
*s
, resource_size_t base
,
74 resource_size_t size
, int type
, char *name
)
76 struct resource
*res
, *parent
;
78 parent
= type
& IORESOURCE_MEM
? &iomem_resource
: &ioport_resource
;
79 res
= pcmcia_make_resource(base
, size
, type
| IORESOURCE_BUSY
, name
);
84 parent
= pci_find_parent_resource(s
->cb_dev
, res
);
86 if (!parent
|| request_resource(parent
, res
)) {
94 static void free_region(struct resource
*res
)
97 release_resource(res
);
102 /*======================================================================
104 These manage the internal databases of available resources.
106 ======================================================================*/
108 static int add_interval(struct resource_map
*map
, u_long base
, u_long num
)
110 struct resource_map
*p
, *q
;
112 for (p
= map
; ; p
= p
->next
) {
113 if ((p
!= map
) && (p
->base
+p
->num
>= base
)) {
114 p
->num
= max(num
+ base
- p
->base
, p
->num
);
117 if ((p
->next
== map
) || (p
->next
->base
> base
+num
-1))
120 q
= kmalloc(sizeof(struct resource_map
), GFP_KERNEL
);
122 printk(KERN_WARNING
"out of memory to update resources\n");
125 q
->base
= base
; q
->num
= num
;
126 q
->next
= p
->next
; p
->next
= q
;
130 /*====================================================================*/
132 static int sub_interval(struct resource_map
*map
, u_long base
, u_long num
)
134 struct resource_map
*p
, *q
;
136 for (p
= map
; ; p
= q
) {
140 if ((q
->base
+q
->num
> base
) && (base
+num
> q
->base
)) {
141 if (q
->base
>= base
) {
142 if (q
->base
+q
->num
<= base
+num
) {
143 /* Delete whole block */
146 /* don't advance the pointer yet */
149 /* Cut off bit from the front */
150 q
->num
= q
->base
+ q
->num
- base
- num
;
151 q
->base
= base
+ num
;
153 } else if (q
->base
+q
->num
<= base
+num
) {
154 /* Cut off bit from the end */
155 q
->num
= base
- q
->base
;
157 /* Split the block into two pieces */
158 p
= kmalloc(sizeof(struct resource_map
),
161 printk(KERN_WARNING
"out of memory to update resources\n");
165 p
->num
= q
->base
+q
->num
- p
->base
;
166 q
->num
= base
- q
->base
;
167 p
->next
= q
->next
; q
->next
= p
;
174 /*======================================================================
176 These routines examine a region of IO or memory addresses to
177 determine what ranges might be genuinely available.
179 ======================================================================*/
181 #ifdef CONFIG_PCMCIA_PROBE
182 static void do_io_probe(struct pcmcia_socket
*s
, unsigned int base
,
185 struct resource
*res
;
186 struct socket_data
*s_data
= s
->resource_data
;
187 unsigned int i
, j
, bad
;
189 u_char
*b
, hole
, most
;
191 dev_info(&s
->dev
, "cs: IO port probe %#x-%#x:", base
, base
+num
-1);
193 /* First, what does a floating port look like? */
194 b
= kzalloc(256, GFP_KERNEL
);
197 dev_err(&s
->dev
, "do_io_probe: unable to kmalloc 256 bytes\n");
200 for (i
= base
, most
= 0; i
< base
+num
; i
+= 8) {
201 res
= claim_region(s
, i
, 8, IORESOURCE_IO
, "PCMCIA ioprobe");
205 for (j
= 1; j
< 8; j
++)
206 if (inb(i
+j
) != hole
)
209 if ((j
== 8) && (++b
[hole
] > b
[most
]))
217 for (i
= base
; i
< base
+num
; i
+= 8) {
218 res
= claim_region(s
, i
, 8, IORESOURCE_IO
, "PCMCIA ioprobe");
221 pr_cont(" excluding");
226 for (j
= 0; j
< 8; j
++)
227 if (inb(i
+j
) != most
)
232 pr_cont(" excluding");
237 sub_interval(&s_data
->io_db
, bad
, i
-bad
);
238 pr_cont(" %#x-%#x", bad
, i
-1);
244 if ((num
> 16) && (bad
== base
) && (i
== base
+num
)) {
245 sub_interval(&s_data
->io_db
, bad
, i
-bad
);
246 pr_cont(" nothing: probe failed.\n");
249 sub_interval(&s_data
->io_db
, bad
, i
-bad
);
250 pr_cont(" %#x-%#x", bad
, i
-1);
254 pr_cont("%s\n", !any
? " clean" : "");
258 /*======================================================================*/
261 * readable() - iomem validation function for cards with a valid CIS
263 static int readable(struct pcmcia_socket
*s
, struct resource
*res
,
269 dev_dbg(&s
->dev
, "fake CIS is being used: can't validate mem\n");
273 s
->cis_mem
.res
= res
;
274 s
->cis_virt
= ioremap(res
->start
, s
->map_size
);
276 mutex_unlock(&s
->ops_mutex
);
277 /* as we're only called from pcmcia.c, we're safe */
278 if (s
->callback
->validate
)
279 ret
= s
->callback
->validate(s
, count
);
280 /* invalidate mapping */
281 mutex_lock(&s
->ops_mutex
);
282 iounmap(s
->cis_virt
);
285 s
->cis_mem
.res
= NULL
;
286 if ((ret
) || (*count
== 0))
292 * checksum() - iomem validation function for simple memory cards
294 static int checksum(struct pcmcia_socket
*s
, struct resource
*res
,
298 int i
, a
= 0, b
= -1, d
;
301 virt
= ioremap(res
->start
, s
->map_size
);
304 map
.flags
= MAP_ACTIVE
;
308 s
->ops
->set_mem_map(s
, &map
);
310 /* Don't bother checking every word... */
311 for (i
= 0; i
< s
->map_size
; i
+= 44) {
318 s
->ops
->set_mem_map(s
, &map
);
332 * do_validate_mem() - low level validate a memory region for PCMCIA use
333 * @s: PCMCIA socket to validate
334 * @base: start address of resource to check
335 * @size: size of resource to check
336 * @validate: validation function to use
338 * do_validate_mem() splits up the memory region which is to be checked
339 * into two parts. Both are passed to the @validate() function. If
340 * @validate() returns non-zero, or the value parameter to @validate()
341 * is zero, or the value parameter is different between both calls,
342 * the check fails, and -EINVAL is returned. Else, 0 is returned.
344 static int do_validate_mem(struct pcmcia_socket
*s
,
345 unsigned long base
, unsigned long size
,
346 int validate (struct pcmcia_socket
*s
,
347 struct resource
*res
,
348 unsigned int *value
))
350 struct socket_data
*s_data
= s
->resource_data
;
351 struct resource
*res1
, *res2
;
352 unsigned int info1
= 1, info2
= 1;
355 res1
= claim_region(s
, base
, size
/2, IORESOURCE_MEM
, "PCMCIA memprobe");
356 res2
= claim_region(s
, base
+ size
/2, size
/2, IORESOURCE_MEM
,
362 ret
= validate(s
, res1
, &info1
);
363 ret
+= validate(s
, res2
, &info2
);
367 dev_dbg(&s
->dev
, "cs: memory probe 0x%06lx-0x%06lx: %pr %pr %u %u %u",
368 base
, base
+size
-1, res1
, res2
, ret
, info1
, info2
);
373 if ((ret
) || (info1
!= info2
) || (info1
== 0))
376 if (validate
&& !s
->fake_cis
) {
377 /* move it to the validated data set */
378 add_interval(&s_data
->mem_db_valid
, base
, size
);
379 sub_interval(&s_data
->mem_db
, base
, size
);
387 * do_mem_probe() - validate a memory region for PCMCIA use
388 * @s: PCMCIA socket to validate
389 * @base: start address of resource to check
390 * @num: size of resource to check
391 * @validate: validation function to use
392 * @fallback: validation function to use if validate fails
394 * do_mem_probe() checks a memory region for use by the PCMCIA subsystem.
395 * To do so, the area is split up into sensible parts, and then passed
396 * into the @validate() function. Only if @validate() and @fallback() fail,
397 * the area is marked as unavaibale for use by the PCMCIA subsystem. The
398 * function returns the size of the usable memory area.
400 static int do_mem_probe(struct pcmcia_socket
*s
, u_long base
, u_long num
,
401 int validate (struct pcmcia_socket
*s
,
402 struct resource
*res
,
403 unsigned int *value
),
404 int fallback (struct pcmcia_socket
*s
,
405 struct resource
*res
,
406 unsigned int *value
))
408 struct socket_data
*s_data
= s
->resource_data
;
409 u_long i
, j
, bad
, fail
, step
;
411 dev_info(&s
->dev
, "cs: memory probe 0x%06lx-0x%06lx:",
414 step
= (num
< 0x20000) ? 0x2000 : ((num
>>4) & ~0x1fff);
415 /* don't allow too large steps */
418 /* cis_readable wants to map 2x map_size */
419 if (step
< 2 * s
->map_size
)
420 step
= 2 * s
->map_size
;
421 for (i
= j
= base
; i
< base
+num
; i
= j
+ step
) {
423 for (j
= i
; j
< base
+num
; j
+= step
) {
424 if (!do_validate_mem(s
, j
, step
, validate
))
427 fail
= ((i
== base
) && (j
== base
+num
));
429 if ((fail
) && (fallback
)) {
430 for (j
= i
; j
< base
+num
; j
+= step
)
431 if (!do_validate_mem(s
, j
, step
, fallback
))
436 pr_cont(" excluding");
437 pr_cont(" %#05lx-%#05lx", i
, j
-1);
438 sub_interval(&s_data
->mem_db
, i
, j
-i
);
442 pr_cont("%s\n", !bad
? " clean" : "");
447 #ifdef CONFIG_PCMCIA_PROBE
450 * inv_probe() - top-to-bottom search for one usuable high memory area
451 * @s: PCMCIA socket to validate
452 * @m: resource_map to check
454 static u_long
inv_probe(struct resource_map
*m
, struct pcmcia_socket
*s
)
456 struct socket_data
*s_data
= s
->resource_data
;
458 if (m
== &s_data
->mem_db
)
460 ok
= inv_probe(m
->next
, s
);
462 if (m
->base
>= 0x100000)
463 sub_interval(&s_data
->mem_db
, m
->base
, m
->num
);
466 if (m
->base
< 0x100000)
468 return do_mem_probe(s
, m
->base
, m
->num
, readable
, checksum
);
472 * validate_mem() - memory probe function
473 * @s: PCMCIA socket to validate
474 * @probe_mask: MEM_PROBE_LOW | MEM_PROBE_HIGH
476 * The memory probe. If the memory list includes a 64K-aligned block
477 * below 1MB, we probe in 64K chunks, and as soon as we accumulate at
478 * least mem_limit free space, we quit. Returns 0 on usuable ports.
480 static int validate_mem(struct pcmcia_socket
*s
, unsigned int probe_mask
)
482 struct resource_map
*m
, mm
;
483 static unsigned char order
[] = { 0xd0, 0xe0, 0xc0, 0xf0 };
484 unsigned long b
, i
, ok
= 0;
485 struct socket_data
*s_data
= s
->resource_data
;
487 /* We do up to four passes through the list */
488 if (probe_mask
& MEM_PROBE_HIGH
) {
489 if (inv_probe(s_data
->mem_db
.next
, s
) > 0)
491 if (s_data
->mem_db_valid
.next
!= &s_data
->mem_db_valid
)
494 "cs: warning: no high memory space available!\n");
498 for (m
= s_data
->mem_db
.next
; m
!= &s_data
->mem_db
; m
= mm
.next
) {
500 /* Only probe < 1 MB */
501 if (mm
.base
>= 0x100000)
503 if ((mm
.base
| mm
.num
) & 0xffff) {
504 ok
+= do_mem_probe(s
, mm
.base
, mm
.num
, readable
,
508 /* Special probe for 64K-aligned block */
509 for (i
= 0; i
< 4; i
++) {
511 if ((b
>= mm
.base
) && (b
+0x10000 <= mm
.base
+mm
.num
)) {
513 sub_interval(&s_data
->mem_db
, b
, 0x10000);
515 ok
+= do_mem_probe(s
, b
, 0x10000,
527 #else /* CONFIG_PCMCIA_PROBE */
530 * validate_mem() - memory probe function
531 * @s: PCMCIA socket to validate
532 * @probe_mask: ignored
534 * Returns 0 on usuable ports.
536 static int validate_mem(struct pcmcia_socket
*s
, unsigned int probe_mask
)
538 struct resource_map
*m
, mm
;
539 struct socket_data
*s_data
= s
->resource_data
;
540 unsigned long ok
= 0;
542 for (m
= s_data
->mem_db
.next
; m
!= &s_data
->mem_db
; m
= mm
.next
) {
544 ok
+= do_mem_probe(s
, mm
.base
, mm
.num
, readable
, checksum
);
551 #endif /* CONFIG_PCMCIA_PROBE */
555 * pcmcia_nonstatic_validate_mem() - try to validate iomem for PCMCIA use
556 * @s: PCMCIA socket to validate
558 * This is tricky... when we set up CIS memory, we try to validate
559 * the memory window space allocations.
561 * Locking note: Must be called with skt_mutex held!
563 static int pcmcia_nonstatic_validate_mem(struct pcmcia_socket
*s
)
565 struct socket_data
*s_data
= s
->resource_data
;
566 unsigned int probe_mask
= MEM_PROBE_LOW
;
569 if (!probe_mem
|| !(s
->state
& SOCKET_PRESENT
))
572 if (s
->features
& SS_CAP_PAGE_REGS
)
573 probe_mask
= MEM_PROBE_HIGH
;
575 ret
= validate_mem(s
, probe_mask
);
577 if (s_data
->mem_db_valid
.next
!= &s_data
->mem_db_valid
)
583 struct pcmcia_align_data
{
585 unsigned long offset
;
586 struct resource_map
*map
;
589 static resource_size_t
pcmcia_common_align(struct pcmcia_align_data
*align_data
,
590 resource_size_t start
)
594 * Ensure that we have the correct start address
596 ret
= (start
& ~align_data
->mask
) + align_data
->offset
;
598 ret
+= align_data
->mask
+ 1;
602 static resource_size_t
603 pcmcia_align(void *align_data
, const struct resource
*res
,
604 resource_size_t size
, resource_size_t align
)
606 struct pcmcia_align_data
*data
= align_data
;
607 struct resource_map
*m
;
608 resource_size_t start
;
610 start
= pcmcia_common_align(data
, res
->start
);
612 for (m
= data
->map
->next
; m
!= data
->map
; m
= m
->next
) {
613 unsigned long map_start
= m
->base
;
614 unsigned long map_end
= m
->base
+ m
->num
- 1;
617 * If the lower resources are not available, try aligning
618 * to this entry of the resource database to see if it'll
621 if (start
< map_start
)
622 start
= pcmcia_common_align(data
, map_start
);
625 * If we're above the area which was passed in, there's
626 * no point proceeding.
628 if (start
>= res
->end
)
631 if ((start
+ size
- 1) <= map_end
)
636 * If we failed to find something suitable, ensure we fail.
645 * Adjust an existing IO region allocation, but making sure that we don't
646 * encroach outside the resources which the user supplied.
648 static int __nonstatic_adjust_io_region(struct pcmcia_socket
*s
,
649 unsigned long r_start
,
652 struct resource_map
*m
;
653 struct socket_data
*s_data
= s
->resource_data
;
656 for (m
= s_data
->io_db
.next
; m
!= &s_data
->io_db
; m
= m
->next
) {
657 unsigned long start
= m
->base
;
658 unsigned long end
= m
->base
+ m
->num
- 1;
660 if (start
> r_start
|| r_end
> end
)
669 /*======================================================================
671 These find ranges of I/O ports or memory addresses that are not
672 currently allocated by other devices.
674 The 'align' field should reflect the number of bits of address
675 that need to be preserved from the initial value of *base. It
676 should be a power of two, greater than or equal to 'num'. A value
677 of 0 means that all bits of *base are significant. *base should
678 also be strictly less than 'align'.
680 ======================================================================*/
682 static struct resource
*__nonstatic_find_io_region(struct pcmcia_socket
*s
,
683 unsigned long base
, int num
,
686 struct resource
*res
= pcmcia_make_resource(0, num
, IORESOURCE_IO
,
688 struct socket_data
*s_data
= s
->resource_data
;
689 struct pcmcia_align_data data
;
690 unsigned long min
= base
;
693 data
.mask
= align
- 1;
694 data
.offset
= base
& data
.mask
;
695 data
.map
= &s_data
->io_db
;
699 ret
= pci_bus_alloc_resource(s
->cb_dev
->bus
, res
, num
, 1,
700 min
, 0, pcmcia_align
, &data
);
703 ret
= allocate_resource(&ioport_resource
, res
, num
, min
, ~0UL,
704 1, pcmcia_align
, &data
);
713 static int nonstatic_find_io(struct pcmcia_socket
*s
, unsigned int attr
,
714 unsigned int *base
, unsigned int num
,
715 unsigned int align
, struct resource
**parent
)
719 /* Check for an already-allocated window that must conflict with
720 * what was asked for. It is a hack because it does not catch all
721 * potential conflicts, just the most obvious ones.
723 for (i
= 0; i
< MAX_IO_WIN
; i
++) {
730 if ((s
->io
[i
].res
->start
& (align
-1)) == *base
)
734 for (i
= 0; i
< MAX_IO_WIN
; i
++) {
735 struct resource
*res
= s
->io
[i
].res
;
738 if (res
&& (res
->flags
& IORESOURCE_BITS
) !=
739 (attr
& IORESOURCE_BITS
))
746 res
= s
->io
[i
].res
= __nonstatic_find_io_region(s
,
753 s
->io
[i
].res
->flags
=
754 ((res
->flags
& ~IORESOURCE_BITS
) |
755 (attr
& IORESOURCE_BITS
));
756 s
->io
[i
].InUse
= num
;
761 /* Try to extend top of window */
763 if ((*base
== 0) || (*base
== try)) {
764 ret
= __nonstatic_adjust_io_region(s
, res
->start
,
767 ret
= adjust_resource(s
->io
[i
].res
, res
->start
,
768 resource_size(res
) + num
);
772 s
->io
[i
].InUse
+= num
;
778 /* Try to extend bottom of window */
779 try = res
->start
- num
;
780 if ((*base
== 0) || (*base
== try)) {
781 ret
= __nonstatic_adjust_io_region(s
,
785 ret
= adjust_resource(s
->io
[i
].res
,
787 resource_size(res
) + num
);
791 s
->io
[i
].InUse
+= num
;
802 static struct resource
*nonstatic_find_mem_region(u_long base
, u_long num
,
803 u_long align
, int low
, struct pcmcia_socket
*s
)
805 struct resource
*res
= pcmcia_make_resource(0, num
, IORESOURCE_MEM
,
807 struct socket_data
*s_data
= s
->resource_data
;
808 struct pcmcia_align_data data
;
809 unsigned long min
, max
;
812 low
= low
|| !(s
->features
& SS_CAP_PAGE_REGS
);
814 data
.mask
= align
- 1;
815 data
.offset
= base
& data
.mask
;
817 for (i
= 0; i
< 2; i
++) {
818 data
.map
= &s_data
->mem_db_valid
;
821 min
= base
< max
? base
: 0;
824 min
= 0x100000UL
+ base
;
827 for (j
= 0; j
< 2; j
++) {
830 ret
= pci_bus_alloc_resource(s
->cb_dev
->bus
,
832 pcmcia_align
, &data
);
836 ret
= allocate_resource(&iomem_resource
,
837 res
, num
, min
, max
, 1,
838 pcmcia_align
, &data
);
842 data
.map
= &s_data
->mem_db
;
857 static int adjust_memory(struct pcmcia_socket
*s
, unsigned int action
, unsigned long start
, unsigned long end
)
859 struct socket_data
*data
= s
->resource_data
;
860 unsigned long size
= end
- start
+ 1;
867 case ADD_MANAGED_RESOURCE
:
868 ret
= add_interval(&data
->mem_db
, start
, size
);
870 do_mem_probe(s
, start
, size
, NULL
, NULL
);
872 case REMOVE_MANAGED_RESOURCE
:
873 ret
= sub_interval(&data
->mem_db
, start
, size
);
883 static int adjust_io(struct pcmcia_socket
*s
, unsigned int action
, unsigned long start
, unsigned long end
)
885 struct socket_data
*data
= s
->resource_data
;
889 #if defined(CONFIG_X86)
890 /* on x86, avoid anything < 0x100 for it is often used for
891 * legacy platform devices */
896 size
= end
- start
+ 1;
901 if (end
> IO_SPACE_LIMIT
)
905 case ADD_MANAGED_RESOURCE
:
906 if (add_interval(&data
->io_db
, start
, size
) != 0) {
910 #ifdef CONFIG_PCMCIA_PROBE
912 do_io_probe(s
, start
, size
);
915 case REMOVE_MANAGED_RESOURCE
:
916 sub_interval(&data
->io_db
, start
, size
);
928 static int nonstatic_autoadd_resources(struct pcmcia_socket
*s
)
930 struct resource
*res
;
933 if (!s
->cb_dev
|| !s
->cb_dev
->bus
)
936 #if defined(CONFIG_X86)
937 /* If this is the root bus, the risk of hitting some strange
938 * system devices is too high: If a driver isn't loaded, the
939 * resources are not claimed; even if a driver is loaded, it
940 * may not request all resources or even the wrong one. We
941 * can neither trust the rest of the kernel nor ACPI/PNP and
942 * CRS parsing to get it right. Therefore, use several
945 * - Do not auto-add resources if the CardBus bridge is on
948 * - Avoid any I/O ports < 0x100.
950 * - On PCI-PCI bridges, only use resources which are set up
951 * exclusively for the secondary PCI bus: the risk of hitting
952 * system devices is quite low, as they usually aren't
953 * connected to the secondary PCI bus.
955 if (s
->cb_dev
->bus
->number
== 0)
958 for (i
= 0; i
< PCI_BRIDGE_RESOURCE_NUM
; i
++) {
959 res
= s
->cb_dev
->bus
->resource
[i
];
961 pci_bus_for_each_resource(s
->cb_dev
->bus
, res
, i
) {
966 if (res
->flags
& IORESOURCE_IO
) {
967 /* safeguard against the root resource, where the
968 * risk of hitting any other device would be too
970 if (res
== &ioport_resource
)
973 dev_info(&s
->cb_dev
->dev
,
974 "pcmcia: parent PCI bridge window: %pR\n",
976 if (!adjust_io(s
, ADD_MANAGED_RESOURCE
, res
->start
, res
->end
))
977 done
|= IORESOURCE_IO
;
981 if (res
->flags
& IORESOURCE_MEM
) {
982 /* safeguard against the root resource, where the
983 * risk of hitting any other device would be too
985 if (res
== &iomem_resource
)
988 dev_info(&s
->cb_dev
->dev
,
989 "pcmcia: parent PCI bridge window: %pR\n",
991 if (!adjust_memory(s
, ADD_MANAGED_RESOURCE
, res
->start
, res
->end
))
992 done
|= IORESOURCE_MEM
;
996 /* if we got at least one of IO, and one of MEM, we can be glad and
997 * activate the PCMCIA subsystem */
998 if (done
== (IORESOURCE_MEM
| IORESOURCE_IO
))
999 s
->resource_setup_done
= 1;
1006 static inline int nonstatic_autoadd_resources(struct pcmcia_socket
*s
)
1014 static int nonstatic_init(struct pcmcia_socket
*s
)
1016 struct socket_data
*data
;
1018 data
= kzalloc(sizeof(struct socket_data
), GFP_KERNEL
);
1022 data
->mem_db
.next
= &data
->mem_db
;
1023 data
->mem_db_valid
.next
= &data
->mem_db_valid
;
1024 data
->io_db
.next
= &data
->io_db
;
1026 s
->resource_data
= (void *) data
;
1028 nonstatic_autoadd_resources(s
);
1033 static void nonstatic_release_resource_db(struct pcmcia_socket
*s
)
1035 struct socket_data
*data
= s
->resource_data
;
1036 struct resource_map
*p
, *q
;
1038 for (p
= data
->mem_db_valid
.next
; p
!= &data
->mem_db_valid
; p
= q
) {
1042 for (p
= data
->mem_db
.next
; p
!= &data
->mem_db
; p
= q
) {
1046 for (p
= data
->io_db
.next
; p
!= &data
->io_db
; p
= q
) {
1053 struct pccard_resource_ops pccard_nonstatic_ops
= {
1054 .validate_mem
= pcmcia_nonstatic_validate_mem
,
1055 .find_io
= nonstatic_find_io
,
1056 .find_mem
= nonstatic_find_mem_region
,
1057 .init
= nonstatic_init
,
1058 .exit
= nonstatic_release_resource_db
,
1060 EXPORT_SYMBOL(pccard_nonstatic_ops
);
1063 /* sysfs interface to the resource database */
1065 static ssize_t
show_io_db(struct device
*dev
,
1066 struct device_attribute
*attr
, char *buf
)
1068 struct pcmcia_socket
*s
= dev_get_drvdata(dev
);
1069 struct socket_data
*data
;
1070 struct resource_map
*p
;
1073 mutex_lock(&s
->ops_mutex
);
1074 data
= s
->resource_data
;
1076 for (p
= data
->io_db
.next
; p
!= &data
->io_db
; p
= p
->next
) {
1077 if (ret
> (PAGE_SIZE
- 10))
1079 ret
+= snprintf(&buf
[ret
], (PAGE_SIZE
- ret
- 1),
1080 "0x%08lx - 0x%08lx\n",
1081 ((unsigned long) p
->base
),
1082 ((unsigned long) p
->base
+ p
->num
- 1));
1085 mutex_unlock(&s
->ops_mutex
);
1089 static ssize_t
store_io_db(struct device
*dev
,
1090 struct device_attribute
*attr
,
1091 const char *buf
, size_t count
)
1093 struct pcmcia_socket
*s
= dev_get_drvdata(dev
);
1094 unsigned long start_addr
, end_addr
;
1095 unsigned int add
= ADD_MANAGED_RESOURCE
;
1098 ret
= sscanf(buf
, "+ 0x%lx - 0x%lx", &start_addr
, &end_addr
);
1100 ret
= sscanf(buf
, "- 0x%lx - 0x%lx", &start_addr
, &end_addr
);
1101 add
= REMOVE_MANAGED_RESOURCE
;
1103 ret
= sscanf(buf
, "0x%lx - 0x%lx", &start_addr
,
1105 add
= ADD_MANAGED_RESOURCE
;
1110 if (end_addr
< start_addr
)
1113 mutex_lock(&s
->ops_mutex
);
1114 ret
= adjust_io(s
, add
, start_addr
, end_addr
);
1115 mutex_unlock(&s
->ops_mutex
);
1117 return ret
? ret
: count
;
1119 static DEVICE_ATTR(available_resources_io
, 0600, show_io_db
, store_io_db
);
1121 static ssize_t
show_mem_db(struct device
*dev
,
1122 struct device_attribute
*attr
, char *buf
)
1124 struct pcmcia_socket
*s
= dev_get_drvdata(dev
);
1125 struct socket_data
*data
;
1126 struct resource_map
*p
;
1129 mutex_lock(&s
->ops_mutex
);
1130 data
= s
->resource_data
;
1132 for (p
= data
->mem_db_valid
.next
; p
!= &data
->mem_db_valid
;
1134 if (ret
> (PAGE_SIZE
- 10))
1136 ret
+= snprintf(&buf
[ret
], (PAGE_SIZE
- ret
- 1),
1137 "0x%08lx - 0x%08lx\n",
1138 ((unsigned long) p
->base
),
1139 ((unsigned long) p
->base
+ p
->num
- 1));
1142 for (p
= data
->mem_db
.next
; p
!= &data
->mem_db
; p
= p
->next
) {
1143 if (ret
> (PAGE_SIZE
- 10))
1145 ret
+= snprintf(&buf
[ret
], (PAGE_SIZE
- ret
- 1),
1146 "0x%08lx - 0x%08lx\n",
1147 ((unsigned long) p
->base
),
1148 ((unsigned long) p
->base
+ p
->num
- 1));
1151 mutex_unlock(&s
->ops_mutex
);
1155 static ssize_t
store_mem_db(struct device
*dev
,
1156 struct device_attribute
*attr
,
1157 const char *buf
, size_t count
)
1159 struct pcmcia_socket
*s
= dev_get_drvdata(dev
);
1160 unsigned long start_addr
, end_addr
;
1161 unsigned int add
= ADD_MANAGED_RESOURCE
;
1164 ret
= sscanf(buf
, "+ 0x%lx - 0x%lx", &start_addr
, &end_addr
);
1166 ret
= sscanf(buf
, "- 0x%lx - 0x%lx", &start_addr
, &end_addr
);
1167 add
= REMOVE_MANAGED_RESOURCE
;
1169 ret
= sscanf(buf
, "0x%lx - 0x%lx", &start_addr
,
1171 add
= ADD_MANAGED_RESOURCE
;
1176 if (end_addr
< start_addr
)
1179 mutex_lock(&s
->ops_mutex
);
1180 ret
= adjust_memory(s
, add
, start_addr
, end_addr
);
1181 mutex_unlock(&s
->ops_mutex
);
1183 return ret
? ret
: count
;
1185 static DEVICE_ATTR(available_resources_mem
, 0600, show_mem_db
, store_mem_db
);
1187 static struct attribute
*pccard_rsrc_attributes
[] = {
1188 &dev_attr_available_resources_io
.attr
,
1189 &dev_attr_available_resources_mem
.attr
,
1193 static const struct attribute_group rsrc_attributes
= {
1194 .attrs
= pccard_rsrc_attributes
,
1197 static int pccard_sysfs_add_rsrc(struct device
*dev
,
1198 struct class_interface
*class_intf
)
1200 struct pcmcia_socket
*s
= dev_get_drvdata(dev
);
1202 if (s
->resource_ops
!= &pccard_nonstatic_ops
)
1204 return sysfs_create_group(&dev
->kobj
, &rsrc_attributes
);
1207 static void pccard_sysfs_remove_rsrc(struct device
*dev
,
1208 struct class_interface
*class_intf
)
1210 struct pcmcia_socket
*s
= dev_get_drvdata(dev
);
1212 if (s
->resource_ops
!= &pccard_nonstatic_ops
)
1214 sysfs_remove_group(&dev
->kobj
, &rsrc_attributes
);
1217 static struct class_interface pccard_rsrc_interface __refdata
= {
1218 .class = &pcmcia_socket_class
,
1219 .add_dev
= &pccard_sysfs_add_rsrc
,
1220 .remove_dev
= &pccard_sysfs_remove_rsrc
,
1223 static int __init
nonstatic_sysfs_init(void)
1225 return class_interface_register(&pccard_rsrc_interface
);
1228 static void __exit
nonstatic_sysfs_exit(void)
1230 class_interface_unregister(&pccard_rsrc_interface
);
1233 module_init(nonstatic_sysfs_init
);
1234 module_exit(nonstatic_sysfs_exit
);