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/cs_types.h>
32 #include <pcmcia/ss.h>
33 #include <pcmcia/cs.h>
34 #include <pcmcia/bulkmem.h>
35 #include <pcmcia/cistpl.h>
36 #include "cs_internal.h"
38 MODULE_AUTHOR("David A. Hinds, Dominik Brodowski");
39 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 io_db
;
60 unsigned int rsrc_mem_probe
;
63 static DEFINE_MUTEX(rsrc_mutex
);
64 #define MEM_PROBE_LOW (1 << 0)
65 #define MEM_PROBE_HIGH (1 << 1)
68 /*======================================================================
70 Linux resource management extensions
72 ======================================================================*/
74 static struct resource
*
75 make_resource(resource_size_t b
, resource_size_t n
, int flags
, char *name
)
77 struct resource
*res
= kzalloc(sizeof(*res
), GFP_KERNEL
);
88 static struct resource
*
89 claim_region(struct pcmcia_socket
*s
, resource_size_t base
,
90 resource_size_t size
, int type
, char *name
)
92 struct resource
*res
, *parent
;
94 parent
= type
& IORESOURCE_MEM
? &iomem_resource
: &ioport_resource
;
95 res
= make_resource(base
, size
, type
| IORESOURCE_BUSY
, name
);
100 parent
= pci_find_parent_resource(s
->cb_dev
, res
);
102 if (!parent
|| request_resource(parent
, res
)) {
110 static void free_region(struct resource
*res
)
113 release_resource(res
);
118 /*======================================================================
120 These manage the internal databases of available resources.
122 ======================================================================*/
124 static int add_interval(struct resource_map
*map
, u_long base
, u_long num
)
126 struct resource_map
*p
, *q
;
128 for (p
= map
; ; p
= p
->next
) {
129 if ((p
!= map
) && (p
->base
+p
->num
-1 >= base
))
131 if ((p
->next
== map
) || (p
->next
->base
> base
+num
-1))
134 q
= kmalloc(sizeof(struct resource_map
), GFP_KERNEL
);
135 if (!q
) return CS_OUT_OF_RESOURCE
;
136 q
->base
= base
; q
->num
= num
;
137 q
->next
= p
->next
; p
->next
= q
;
141 /*====================================================================*/
143 static int sub_interval(struct resource_map
*map
, u_long base
, u_long num
)
145 struct resource_map
*p
, *q
;
147 for (p
= map
; ; p
= q
) {
151 if ((q
->base
+q
->num
> base
) && (base
+num
> q
->base
)) {
152 if (q
->base
>= base
) {
153 if (q
->base
+q
->num
<= base
+num
) {
154 /* Delete whole block */
157 /* don't advance the pointer yet */
160 /* Cut off bit from the front */
161 q
->num
= q
->base
+ q
->num
- base
- num
;
162 q
->base
= base
+ num
;
164 } else if (q
->base
+q
->num
<= base
+num
) {
165 /* Cut off bit from the end */
166 q
->num
= base
- q
->base
;
168 /* Split the block into two pieces */
169 p
= kmalloc(sizeof(struct resource_map
), GFP_KERNEL
);
170 if (!p
) return CS_OUT_OF_RESOURCE
;
172 p
->num
= q
->base
+q
->num
- p
->base
;
173 q
->num
= base
- q
->base
;
174 p
->next
= q
->next
; q
->next
= p
;
181 /*======================================================================
183 These routines examine a region of IO or memory addresses to
184 determine what ranges might be genuinely available.
186 ======================================================================*/
188 #ifdef CONFIG_PCMCIA_PROBE
189 static void do_io_probe(struct pcmcia_socket
*s
, kio_addr_t base
, kio_addr_t num
)
191 struct resource
*res
;
192 struct socket_data
*s_data
= s
->resource_data
;
193 kio_addr_t i
, j
, bad
;
195 u_char
*b
, hole
, most
;
197 printk(KERN_INFO
"cs: IO port probe %#lx-%#lx:",
200 /* First, what does a floating port look like? */
201 b
= kzalloc(256, GFP_KERNEL
);
203 printk(KERN_ERR
"do_io_probe: unable to kmalloc 256 bytes");
206 for (i
= base
, most
= 0; i
< base
+num
; i
+= 8) {
207 res
= claim_region(NULL
, i
, 8, IORESOURCE_IO
, "PCMCIA IO probe");
211 for (j
= 1; j
< 8; j
++)
212 if (inb(i
+j
) != hole
) break;
214 if ((j
== 8) && (++b
[hole
] > b
[most
]))
216 if (b
[most
] == 127) break;
221 for (i
= base
; i
< base
+num
; i
+= 8) {
222 res
= claim_region(NULL
, i
, 8, IORESOURCE_IO
, "PCMCIA IO probe");
225 for (j
= 0; j
< 8; j
++)
226 if (inb(i
+j
) != most
) break;
230 printk(" excluding");
235 sub_interval(&s_data
->io_db
, bad
, i
-bad
);
236 printk(" %#lx-%#lx", bad
, i
-1);
242 if ((num
> 16) && (bad
== base
) && (i
== base
+num
)) {
243 printk(" nothing: probe failed.\n");
246 sub_interval(&s_data
->io_db
, bad
, i
-bad
);
247 printk(" %#lx-%#lx", bad
, i
-1);
251 printk(any
? "\n" : " clean.\n");
255 /*======================================================================
257 This is tricky... when we set up CIS memory, we try to validate
258 the memory window space allocations.
260 ======================================================================*/
262 /* Validation function for cards with a valid CIS */
263 static int readable(struct pcmcia_socket
*s
, struct resource
*res
, cisinfo_t
*info
)
267 s
->cis_mem
.res
= res
;
268 s
->cis_virt
= ioremap(res
->start
, s
->map_size
);
270 ret
= pccard_validate_cis(s
, BIND_FN_ALL
, info
);
271 /* invalidate mapping and CIS cache */
272 iounmap(s
->cis_virt
);
274 destroy_cis_cache(s
);
276 s
->cis_mem
.res
= NULL
;
277 if ((ret
!= 0) || (info
->Chains
== 0))
282 /* Validation function for simple memory cards */
283 static int checksum(struct pcmcia_socket
*s
, struct resource
*res
)
286 int i
, a
= 0, b
= -1, d
;
289 virt
= ioremap(res
->start
, s
->map_size
);
292 map
.flags
= MAP_ACTIVE
;
296 s
->ops
->set_mem_map(s
, &map
);
298 /* Don't bother checking every word... */
299 for (i
= 0; i
< s
->map_size
; i
+= 44) {
306 s
->ops
->set_mem_map(s
, &map
);
311 return (b
== -1) ? -1 : (a
>>1);
315 cis_readable(struct pcmcia_socket
*s
, unsigned long base
, unsigned long size
)
317 struct resource
*res1
, *res2
;
318 cisinfo_t info1
, info2
;
321 res1
= claim_region(s
, base
, size
/2, IORESOURCE_MEM
, "cs memory probe");
322 res2
= claim_region(s
, base
+ size
/2, size
/2, IORESOURCE_MEM
, "cs memory probe");
325 ret
= readable(s
, res1
, &info1
);
326 ret
+= readable(s
, res2
, &info2
);
332 return (ret
== 2) && (info1
.Chains
== info2
.Chains
);
336 checksum_match(struct pcmcia_socket
*s
, unsigned long base
, unsigned long size
)
338 struct resource
*res1
, *res2
;
341 res1
= claim_region(s
, base
, size
/2, IORESOURCE_MEM
, "cs memory probe");
342 res2
= claim_region(s
, base
+ size
/2, size
/2, IORESOURCE_MEM
, "cs memory probe");
345 a
= checksum(s
, res1
);
346 b
= checksum(s
, res2
);
352 return (a
== b
) && (a
>= 0);
355 /*======================================================================
357 The memory probe. If the memory list includes a 64K-aligned block
358 below 1MB, we probe in 64K chunks, and as soon as we accumulate at
359 least mem_limit free space, we quit.
361 ======================================================================*/
363 static int do_mem_probe(u_long base
, u_long num
, struct pcmcia_socket
*s
)
365 struct socket_data
*s_data
= s
->resource_data
;
366 u_long i
, j
, bad
, fail
, step
;
368 printk(KERN_INFO
"cs: memory probe 0x%06lx-0x%06lx:",
371 step
= (num
< 0x20000) ? 0x2000 : ((num
>>4) & ~0x1fff);
372 /* don't allow too large steps */
375 /* cis_readable wants to map 2x map_size */
376 if (step
< 2 * s
->map_size
)
377 step
= 2 * s
->map_size
;
378 for (i
= j
= base
; i
< base
+num
; i
= j
+ step
) {
380 for (j
= i
; j
< base
+num
; j
+= step
) {
381 if (cis_readable(s
, j
, step
))
384 fail
= ((i
== base
) && (j
== base
+num
));
387 for (j
= i
; j
< base
+num
; j
+= 2*step
)
388 if (checksum_match(s
, j
, step
) &&
389 checksum_match(s
, j
+ step
, step
))
393 if (!bad
) printk(" excluding");
394 printk(" %#05lx-%#05lx", i
, j
-1);
395 sub_interval(&s_data
->mem_db
, i
, j
-i
);
399 printk(bad
? "\n" : " clean.\n");
403 #ifdef CONFIG_PCMCIA_PROBE
405 static u_long
inv_probe(struct resource_map
*m
, struct pcmcia_socket
*s
)
407 struct socket_data
*s_data
= s
->resource_data
;
409 if (m
== &s_data
->mem_db
)
411 ok
= inv_probe(m
->next
, s
);
413 if (m
->base
>= 0x100000)
414 sub_interval(&s_data
->mem_db
, m
->base
, m
->num
);
417 if (m
->base
< 0x100000)
419 return do_mem_probe(m
->base
, m
->num
, s
);
422 static int validate_mem(struct pcmcia_socket
*s
, unsigned int probe_mask
)
424 struct resource_map
*m
, mm
;
425 static unsigned char order
[] = { 0xd0, 0xe0, 0xc0, 0xf0 };
426 unsigned long b
, i
, ok
= 0;
427 struct socket_data
*s_data
= s
->resource_data
;
429 /* We do up to four passes through the list */
430 if (probe_mask
& MEM_PROBE_HIGH
) {
431 if (inv_probe(s_data
->mem_db
.next
, s
) > 0)
433 printk(KERN_NOTICE
"cs: warning: no high memory space "
438 for (m
= s_data
->mem_db
.next
; m
!= &s_data
->mem_db
; m
= mm
.next
) {
440 /* Only probe < 1 MB */
441 if (mm
.base
>= 0x100000)
443 if ((mm
.base
| mm
.num
) & 0xffff) {
444 ok
+= do_mem_probe(mm
.base
, mm
.num
, s
);
447 /* Special probe for 64K-aligned block */
448 for (i
= 0; i
< 4; i
++) {
450 if ((b
>= mm
.base
) && (b
+0x10000 <= mm
.base
+mm
.num
)) {
452 sub_interval(&s_data
->mem_db
, b
, 0x10000);
454 ok
+= do_mem_probe(b
, 0x10000, s
);
465 #else /* CONFIG_PCMCIA_PROBE */
467 static int validate_mem(struct pcmcia_socket
*s
, unsigned int probe_mask
)
469 struct resource_map
*m
, mm
;
470 struct socket_data
*s_data
= s
->resource_data
;
471 unsigned long ok
= 0;
473 for (m
= s_data
->mem_db
.next
; m
!= &s_data
->mem_db
; m
= mm
.next
) {
475 ok
+= do_mem_probe(mm
.base
, mm
.num
, s
);
482 #endif /* CONFIG_PCMCIA_PROBE */
486 * Locking note: Must be called with skt_mutex held!
488 static int pcmcia_nonstatic_validate_mem(struct pcmcia_socket
*s
)
490 struct socket_data
*s_data
= s
->resource_data
;
491 unsigned int probe_mask
= MEM_PROBE_LOW
;
497 mutex_lock(&rsrc_mutex
);
499 if (s
->features
& SS_CAP_PAGE_REGS
)
500 probe_mask
= MEM_PROBE_HIGH
;
502 if (probe_mask
& ~s_data
->rsrc_mem_probe
) {
503 if (s
->state
& SOCKET_PRESENT
)
504 ret
= validate_mem(s
, probe_mask
);
506 s_data
->rsrc_mem_probe
|= probe_mask
;
509 mutex_unlock(&rsrc_mutex
);
514 struct pcmcia_align_data
{
516 unsigned long offset
;
517 struct resource_map
*map
;
521 pcmcia_common_align(void *align_data
, struct resource
*res
,
522 resource_size_t size
, resource_size_t align
)
524 struct pcmcia_align_data
*data
= align_data
;
525 resource_size_t start
;
527 * Ensure that we have the correct start address
529 start
= (res
->start
& ~data
->mask
) + data
->offset
;
530 if (start
< res
->start
)
531 start
+= data
->mask
+ 1;
536 pcmcia_align(void *align_data
, struct resource
*res
, resource_size_t size
,
537 resource_size_t align
)
539 struct pcmcia_align_data
*data
= align_data
;
540 struct resource_map
*m
;
542 pcmcia_common_align(data
, res
, size
, align
);
544 for (m
= data
->map
->next
; m
!= data
->map
; m
= m
->next
) {
545 unsigned long start
= m
->base
;
546 unsigned long end
= m
->base
+ m
->num
- 1;
549 * If the lower resources are not available, try aligning
550 * to this entry of the resource database to see if it'll
553 if (res
->start
< start
) {
555 pcmcia_common_align(data
, res
, size
, align
);
559 * If we're above the area which was passed in, there's
560 * no point proceeding.
562 if (res
->start
>= res
->end
)
565 if ((res
->start
+ size
- 1) <= end
)
570 * If we failed to find something suitable, ensure we fail.
573 res
->start
= res
->end
;
577 * Adjust an existing IO region allocation, but making sure that we don't
578 * encroach outside the resources which the user supplied.
580 static int nonstatic_adjust_io_region(struct resource
*res
, unsigned long r_start
,
581 unsigned long r_end
, struct pcmcia_socket
*s
)
583 struct resource_map
*m
;
584 struct socket_data
*s_data
= s
->resource_data
;
587 mutex_lock(&rsrc_mutex
);
588 for (m
= s_data
->io_db
.next
; m
!= &s_data
->io_db
; m
= m
->next
) {
589 unsigned long start
= m
->base
;
590 unsigned long end
= m
->base
+ m
->num
- 1;
592 if (start
> r_start
|| r_end
> end
)
595 ret
= adjust_resource(res
, r_start
, r_end
- r_start
+ 1);
598 mutex_unlock(&rsrc_mutex
);
603 /*======================================================================
605 These find ranges of I/O ports or memory addresses that are not
606 currently allocated by other devices.
608 The 'align' field should reflect the number of bits of address
609 that need to be preserved from the initial value of *base. It
610 should be a power of two, greater than or equal to 'num'. A value
611 of 0 means that all bits of *base are significant. *base should
612 also be strictly less than 'align'.
614 ======================================================================*/
616 static struct resource
*nonstatic_find_io_region(unsigned long base
, int num
,
617 unsigned long align
, struct pcmcia_socket
*s
)
619 struct resource
*res
= make_resource(0, num
, IORESOURCE_IO
, s
->dev
.class_id
);
620 struct socket_data
*s_data
= s
->resource_data
;
621 struct pcmcia_align_data data
;
622 unsigned long min
= base
;
628 data
.mask
= align
- 1;
629 data
.offset
= base
& data
.mask
;
630 data
.map
= &s_data
->io_db
;
632 mutex_lock(&rsrc_mutex
);
635 ret
= pci_bus_alloc_resource(s
->cb_dev
->bus
, res
, num
, 1,
636 min
, 0, pcmcia_align
, &data
);
639 ret
= allocate_resource(&ioport_resource
, res
, num
, min
, ~0UL,
640 1, pcmcia_align
, &data
);
641 mutex_unlock(&rsrc_mutex
);
650 static struct resource
* nonstatic_find_mem_region(u_long base
, u_long num
,
651 u_long align
, int low
, struct pcmcia_socket
*s
)
653 struct resource
*res
= make_resource(0, num
, IORESOURCE_MEM
, s
->dev
.class_id
);
654 struct socket_data
*s_data
= s
->resource_data
;
655 struct pcmcia_align_data data
;
656 unsigned long min
, max
;
659 low
= low
|| !(s
->features
& SS_CAP_PAGE_REGS
);
661 data
.mask
= align
- 1;
662 data
.offset
= base
& data
.mask
;
663 data
.map
= &s_data
->mem_db
;
665 for (i
= 0; i
< 2; i
++) {
668 min
= base
< max
? base
: 0;
671 min
= 0x100000UL
+ base
;
674 mutex_lock(&rsrc_mutex
);
677 ret
= pci_bus_alloc_resource(s
->cb_dev
->bus
, res
, num
,
679 pcmcia_align
, &data
);
682 ret
= allocate_resource(&iomem_resource
, res
, num
, min
,
683 max
, 1, pcmcia_align
, &data
);
684 mutex_unlock(&rsrc_mutex
);
698 static int adjust_memory(struct pcmcia_socket
*s
, unsigned int action
, unsigned long start
, unsigned long end
)
700 struct socket_data
*data
= s
->resource_data
;
701 unsigned long size
= end
- start
+ 1;
707 mutex_lock(&rsrc_mutex
);
709 case ADD_MANAGED_RESOURCE
:
710 ret
= add_interval(&data
->mem_db
, start
, size
);
712 case REMOVE_MANAGED_RESOURCE
:
713 ret
= sub_interval(&data
->mem_db
, start
, size
);
715 struct pcmcia_socket
*socket
;
716 down_read(&pcmcia_socket_list_rwsem
);
717 list_for_each_entry(socket
, &pcmcia_socket_list
, socket_list
)
718 release_cis_mem(socket
);
719 up_read(&pcmcia_socket_list_rwsem
);
725 mutex_unlock(&rsrc_mutex
);
731 static int adjust_io(struct pcmcia_socket
*s
, unsigned int action
, unsigned long start
, unsigned long end
)
733 struct socket_data
*data
= s
->resource_data
;
734 unsigned long size
= end
- start
+ 1;
740 if (end
> IO_SPACE_LIMIT
)
743 mutex_lock(&rsrc_mutex
);
745 case ADD_MANAGED_RESOURCE
:
746 if (add_interval(&data
->io_db
, start
, size
) != 0) {
750 #ifdef CONFIG_PCMCIA_PROBE
752 do_io_probe(s
, start
, size
);
755 case REMOVE_MANAGED_RESOURCE
:
756 sub_interval(&data
->io_db
, start
, size
);
762 mutex_unlock(&rsrc_mutex
);
768 static int nonstatic_adjust_resource_info(struct pcmcia_socket
*s
, adjust_t
*adj
)
772 switch (adj
->Resource
) {
773 case RES_MEMORY_RANGE
:
774 end
= adj
->resource
.memory
.Base
+ adj
->resource
.memory
.Size
- 1;
775 return adjust_memory(s
, adj
->Action
, adj
->resource
.memory
.Base
, end
);
777 end
= adj
->resource
.io
.BasePort
+ adj
->resource
.io
.NumPorts
- 1;
778 return adjust_io(s
, adj
->Action
, adj
->resource
.io
.BasePort
, end
);
780 return CS_UNSUPPORTED_FUNCTION
;
784 static int nonstatic_autoadd_resources(struct pcmcia_socket
*s
)
786 struct resource
*res
;
789 if (!s
->cb_dev
|| !s
->cb_dev
->bus
)
792 #if defined(CONFIG_X86)
793 /* If this is the root bus, the risk of hitting
794 * some strange system devices which aren't protected
795 * by either ACPI resource tables or properly requested
796 * resources is too big. Therefore, don't do auto-adding
797 * of resources at the moment.
799 if (s
->cb_dev
->bus
->number
== 0)
803 for (i
=0; i
< PCI_BUS_NUM_RESOURCES
; i
++) {
804 res
= s
->cb_dev
->bus
->resource
[i
];
808 if (res
->flags
& IORESOURCE_IO
) {
809 if (res
== &ioport_resource
)
811 printk(KERN_INFO
"pcmcia: parent PCI bridge I/O "
812 "window: 0x%llx - 0x%llx\n",
813 (unsigned long long)res
->start
,
814 (unsigned long long)res
->end
);
815 if (!adjust_io(s
, ADD_MANAGED_RESOURCE
, res
->start
, res
->end
))
816 done
|= IORESOURCE_IO
;
820 if (res
->flags
& IORESOURCE_MEM
) {
821 if (res
== &iomem_resource
)
823 printk(KERN_INFO
"pcmcia: parent PCI bridge Memory "
824 "window: 0x%llx - 0x%llx\n",
825 (unsigned long long)res
->start
,
826 (unsigned long long)res
->end
);
827 if (!adjust_memory(s
, ADD_MANAGED_RESOURCE
, res
->start
, res
->end
))
828 done
|= IORESOURCE_MEM
;
832 /* if we got at least one of IO, and one of MEM, we can be glad and
833 * activate the PCMCIA subsystem */
834 if (done
== (IORESOURCE_MEM
| IORESOURCE_IO
))
835 s
->resource_setup_done
= 1;
842 static inline int nonstatic_autoadd_resources(struct pcmcia_socket
*s
)
850 static int nonstatic_init(struct pcmcia_socket
*s
)
852 struct socket_data
*data
;
854 data
= kzalloc(sizeof(struct socket_data
), GFP_KERNEL
);
858 data
->mem_db
.next
= &data
->mem_db
;
859 data
->io_db
.next
= &data
->io_db
;
861 s
->resource_data
= (void *) data
;
863 nonstatic_autoadd_resources(s
);
868 static void nonstatic_release_resource_db(struct pcmcia_socket
*s
)
870 struct socket_data
*data
= s
->resource_data
;
871 struct resource_map
*p
, *q
;
873 mutex_lock(&rsrc_mutex
);
874 for (p
= data
->mem_db
.next
; p
!= &data
->mem_db
; p
= q
) {
878 for (p
= data
->io_db
.next
; p
!= &data
->io_db
; p
= q
) {
882 mutex_unlock(&rsrc_mutex
);
886 struct pccard_resource_ops pccard_nonstatic_ops
= {
887 .validate_mem
= pcmcia_nonstatic_validate_mem
,
888 .adjust_io_region
= nonstatic_adjust_io_region
,
889 .find_io
= nonstatic_find_io_region
,
890 .find_mem
= nonstatic_find_mem_region
,
891 .adjust_resource
= nonstatic_adjust_resource_info
,
892 .init
= nonstatic_init
,
893 .exit
= nonstatic_release_resource_db
,
895 EXPORT_SYMBOL(pccard_nonstatic_ops
);
898 /* sysfs interface to the resource database */
900 static ssize_t
show_io_db(struct class_device
*class_dev
, char *buf
)
902 struct pcmcia_socket
*s
= class_get_devdata(class_dev
);
903 struct socket_data
*data
;
904 struct resource_map
*p
;
907 mutex_lock(&rsrc_mutex
);
908 data
= s
->resource_data
;
910 for (p
= data
->io_db
.next
; p
!= &data
->io_db
; p
= p
->next
) {
911 if (ret
> (PAGE_SIZE
- 10))
913 ret
+= snprintf (&buf
[ret
], (PAGE_SIZE
- ret
- 1),
914 "0x%08lx - 0x%08lx\n",
915 ((unsigned long) p
->base
),
916 ((unsigned long) p
->base
+ p
->num
- 1));
919 mutex_unlock(&rsrc_mutex
);
923 static ssize_t
store_io_db(struct class_device
*class_dev
, const char *buf
, size_t count
)
925 struct pcmcia_socket
*s
= class_get_devdata(class_dev
);
926 unsigned long start_addr
, end_addr
;
927 unsigned int add
= ADD_MANAGED_RESOURCE
;
930 ret
= sscanf (buf
, "+ 0x%lx - 0x%lx", &start_addr
, &end_addr
);
932 ret
= sscanf (buf
, "- 0x%lx - 0x%lx", &start_addr
, &end_addr
);
933 add
= REMOVE_MANAGED_RESOURCE
;
935 ret
= sscanf (buf
, "0x%lx - 0x%lx", &start_addr
, &end_addr
);
936 add
= ADD_MANAGED_RESOURCE
;
941 if (end_addr
< start_addr
)
944 ret
= adjust_io(s
, add
, start_addr
, end_addr
);
946 s
->resource_setup_new
= 1;
948 return ret
? ret
: count
;
950 static CLASS_DEVICE_ATTR(available_resources_io
, 0600, show_io_db
, store_io_db
);
952 static ssize_t
show_mem_db(struct class_device
*class_dev
, char *buf
)
954 struct pcmcia_socket
*s
= class_get_devdata(class_dev
);
955 struct socket_data
*data
;
956 struct resource_map
*p
;
959 mutex_lock(&rsrc_mutex
);
960 data
= s
->resource_data
;
962 for (p
= data
->mem_db
.next
; p
!= &data
->mem_db
; p
= p
->next
) {
963 if (ret
> (PAGE_SIZE
- 10))
965 ret
+= snprintf (&buf
[ret
], (PAGE_SIZE
- ret
- 1),
966 "0x%08lx - 0x%08lx\n",
967 ((unsigned long) p
->base
),
968 ((unsigned long) p
->base
+ p
->num
- 1));
971 mutex_unlock(&rsrc_mutex
);
975 static ssize_t
store_mem_db(struct class_device
*class_dev
, const char *buf
, size_t count
)
977 struct pcmcia_socket
*s
= class_get_devdata(class_dev
);
978 unsigned long start_addr
, end_addr
;
979 unsigned int add
= ADD_MANAGED_RESOURCE
;
982 ret
= sscanf (buf
, "+ 0x%lx - 0x%lx", &start_addr
, &end_addr
);
984 ret
= sscanf (buf
, "- 0x%lx - 0x%lx", &start_addr
, &end_addr
);
985 add
= REMOVE_MANAGED_RESOURCE
;
987 ret
= sscanf (buf
, "0x%lx - 0x%lx", &start_addr
, &end_addr
);
988 add
= ADD_MANAGED_RESOURCE
;
993 if (end_addr
< start_addr
)
996 ret
= adjust_memory(s
, add
, start_addr
, end_addr
);
998 s
->resource_setup_new
= 1;
1000 return ret
? ret
: count
;
1002 static CLASS_DEVICE_ATTR(available_resources_mem
, 0600, show_mem_db
, store_mem_db
);
1004 static struct class_device_attribute
*pccard_rsrc_attributes
[] = {
1005 &class_device_attr_available_resources_io
,
1006 &class_device_attr_available_resources_mem
,
1010 static int __devinit
pccard_sysfs_add_rsrc(struct class_device
*class_dev
,
1011 struct class_interface
*class_intf
)
1013 struct pcmcia_socket
*s
= class_get_devdata(class_dev
);
1014 struct class_device_attribute
**attr
;
1016 if (s
->resource_ops
!= &pccard_nonstatic_ops
)
1019 for (attr
= pccard_rsrc_attributes
; *attr
; attr
++) {
1020 ret
= class_device_create_file(class_dev
, *attr
);
1028 static void __devexit
pccard_sysfs_remove_rsrc(struct class_device
*class_dev
,
1029 struct class_interface
*class_intf
)
1031 struct pcmcia_socket
*s
= class_get_devdata(class_dev
);
1032 struct class_device_attribute
**attr
;
1034 if (s
->resource_ops
!= &pccard_nonstatic_ops
)
1037 for (attr
= pccard_rsrc_attributes
; *attr
; attr
++)
1038 class_device_remove_file(class_dev
, *attr
);
1041 static struct class_interface pccard_rsrc_interface
= {
1042 .class = &pcmcia_socket_class
,
1043 .add
= &pccard_sysfs_add_rsrc
,
1044 .remove
= __devexit_p(&pccard_sysfs_remove_rsrc
),
1047 static int __init
nonstatic_sysfs_init(void)
1049 return class_interface_register(&pccard_rsrc_interface
);
1052 static void __exit
nonstatic_sysfs_exit(void)
1054 class_interface_unregister(&pccard_rsrc_interface
);
1057 module_init(nonstatic_sysfs_init
);
1058 module_exit(nonstatic_sysfs_exit
);