15 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/errno.h>
21 #include <linux/types.h>
22 #include <linux/slab.h>
25 #include <linux/pci.h>
26 #include <linux/device.h>
42 #define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0444)
45 #ifdef CONFIG_PCMCIA_PROBE
62 #define MEM_PROBE_LOW (1 << 0)
63 #define MEM_PROBE_HIGH (1 << 1)
66 #define REMOVE_MANAGED_RESOURCE 1
67 #define ADD_MANAGED_RESOURCE 2
115 for (p = map; ; p = p->
next) {
116 if ((p != map) && (p->
base+p->
num >= base)) {
120 if ((p->
next == map) || (p->
next->base > base+num-1))
139 for (p = map; ; p =
q) {
143 if ((q->
base+q->
num > base) && (base+num > q->
base)) {
144 if (q->
base >= base) {
145 if (q->
base+q->
num <= base+num) {
156 }
else if (q->
base+q->
num <= base+num) {
184 #ifdef CONFIG_PCMCIA_PROBE
185 static void do_io_probe(
struct pcmcia_socket *
s,
unsigned int base,
190 unsigned int i,
j,
bad;
194 dev_printk(
KERN_INFO, &s->
dev,
"cs: IO port probe %#x-%#x:",
202 "do_io_probe: unable to kmalloc 256 bytes");
205 for (i = base, most = 0; i < base+num; i += 8) {
206 res = claim_region(s, i, 8,
IORESOURCE_IO,
"PCMCIA ioprobe");
210 for (j = 1; j < 8; j++)
211 if (
inb(i+j) != hole)
214 if ((j == 8) && (++b[hole] > b[most]))
222 for (i = base; i < base+num; i += 8) {
223 res = claim_region(s, i, 8,
IORESOURCE_IO,
"PCMCIA ioprobe");
231 for (j = 0; j < 8; j++)
232 if (
inb(i+j) != most)
242 sub_interval(&s_data->
io_db, bad, i-bad);
243 printk(
" %#x-%#x", bad, i-1);
249 if ((num > 16) && (bad == base) && (i == base+num)) {
250 sub_interval(&s_data->
io_db, bad, i-bad);
251 printk(
" nothing: probe failed.\n");
254 sub_interval(&s_data->
io_db, bad, i-bad);
255 printk(
" %#x-%#x", bad, i-1);
259 printk(any ?
"\n" :
" clean.\n");
274 dev_dbg(&s->
dev,
"fake CIS is being used: can't validate mem\n");
284 ret = s->
callback->validate(s, count);
291 if ((ret) || (*count == 0))
303 int i,
a = 0, b = -1,
d;
313 s->
ops->set_mem_map(s, &map);
316 for (i = 0; i < s->
map_size; i += 44) {
323 s->
ops->set_mem_map(s, &map);
350 unsigned long base,
unsigned long size,
353 unsigned int *value))
360 res1 = claim_region(s, base, size/2,
IORESOURCE_MEM,
"PCMCIA memprobe");
375 dev_dbg(&s->
dev,
"cs: memory probe 0x%06lx-0x%06lx: %p %p %u %u %u",
376 base, base+size-1, res1, res2, ret, info1,
info2);
378 if ((ret) || (info1 !=
info2) || (info1 == 0))
384 sub_interval(&s_data->
mem_db, base, size);
408 unsigned int *value),
411 unsigned int *value))
416 dev_printk(
KERN_INFO, &s->
dev,
"cs: memory probe 0x%06lx-0x%06lx:",
419 step = (num < 0x20000) ? 0x2000 : ((num>>4) & ~0x1fff);
424 if (step < 2 * s->map_size)
426 for (i = j = base; i < base+num; i = j +
step) {
428 for (j = i; j < base+num; j +=
step) {
429 if (!do_validate_mem(s, j, step,
validate))
432 fail = ((i == base) && (j == base+num));
434 if ((fail) && (fallback)) {
435 for (j = i; j < base+num; j +=
step)
436 if (!do_validate_mem(s, j, step, fallback))
442 printk(
" %#05lx-%#05lx", i, j-1);
443 sub_interval(&s_data->
mem_db, i, j-i);
447 printk(bad ?
"\n" :
" clean.\n");
452 #ifdef CONFIG_PCMCIA_PROBE
465 ok = inv_probe(m->
next, s);
467 if (m->
base >= 0x100000)
471 if (m->
base < 0x100000)
485 static int validate_mem(
struct pcmcia_socket *s,
unsigned int probe_mask)
488 static unsigned char order[] = { 0xd0, 0xe0, 0xc0, 0xf0 };
489 unsigned long b,
i, ok = 0;
494 if (inv_probe(s_data->
mem_db.next, s) > 0)
499 "cs: warning: no high memory space available!\n");
506 if (mm.
base >= 0x100000)
508 if ((mm.
base | mm.
num) & 0xffff) {
514 for (i = 0; i < 4; i++) {
516 if ((b >= mm.
base) && (b+0x10000 <= mm.
base+mm.
num)) {
518 sub_interval(&s_data->
mem_db, b, 0x10000);
520 ok += do_mem_probe(s, b, 0x10000,
541 static int validate_mem(
struct pcmcia_socket *s,
unsigned int probe_mask)
545 unsigned long ok = 0;
568 static int pcmcia_nonstatic_validate_mem(
struct pcmcia_socket *s)
580 ret = validate_mem(s, probe_mask);
601 ret = (start & ~align_data->
mask) + align_data->
offset;
603 ret += align_data->
mask + 1;
608 pcmcia_align(
void *align_data,
const struct resource *res,
615 start = pcmcia_common_align(data, res->
start);
617 for (m = data->
map->next; m != data->
map; m = m->
next) {
618 unsigned long map_start = m->
base;
619 unsigned long map_end = m->
base + m->
num - 1;
626 if (start < map_start)
627 start = pcmcia_common_align(data, map_start);
633 if (start >= res->
end)
636 if ((start + size - 1) <= map_end)
653 static int __nonstatic_adjust_io_region(
struct pcmcia_socket *s,
654 unsigned long r_start,
662 unsigned long start = m->
base;
665 if (start > r_start || r_end > end)
688 unsigned long base,
int num,
695 unsigned long min = base;
698 data.
mask = align - 1;
705 min, 0, pcmcia_align, &data);
709 1, pcmcia_align, &data);
719 unsigned int *base,
unsigned int num,
720 unsigned int align,
struct resource **parent)
735 if ((s->
io[i].
res->start & (align-1)) == *base)
751 res = s->
io[
i].
res = __nonstatic_find_io_region(s,
759 ((res->
flags & ~IORESOURCE_BITS) |
760 (attr & IORESOURCE_BITS));
768 if ((*base == 0) || (*base ==
try)) {
769 ret = __nonstatic_adjust_io_region(s, res->
start,
773 resource_size(res) + num);
784 try = res->
start - num;
785 if ((*base == 0) || (*base ==
try)) {
786 ret = __nonstatic_adjust_io_region(s,
792 resource_size(res) + num);
819 data.
mask = align - 1;
822 for (i = 0; i < 2; i++) {
826 min = base < max ? base : 0;
829 min = 0x100000
UL + base;
832 for (j = 0; j < 2; j++) {
837 pcmcia_align, &data);
842 res, num, min, max, 1,
843 pcmcia_align, &data);
862 static int adjust_memory(
struct pcmcia_socket *s,
unsigned int action,
unsigned long start,
unsigned long end)
865 unsigned long size = end - start + 1;
873 ret = add_interval(&data->
mem_db, start, size);
875 do_mem_probe(s, start, size,
NULL,
NULL);
878 ret = sub_interval(&data->
mem_db, start, size);
888 static int adjust_io(
struct pcmcia_socket *s,
unsigned int action,
unsigned long start,
unsigned long end)
894 #if defined(CONFIG_X86)
901 size = end - start + 1;
911 if (add_interval(&data->
io_db, start, size) != 0) {
915 #ifdef CONFIG_PCMCIA_PROBE
917 do_io_probe(s, start, size);
921 sub_interval(&data->
io_db, start, size);
933 static int nonstatic_autoadd_resources(
struct pcmcia_socket *s)
941 #if defined(CONFIG_X86)
960 if (s->
cb_dev->bus->number == 0)
964 res = s->
cb_dev->bus->resource[
i];
966 pci_bus_for_each_resource(s->
cb_dev->bus, res, i) {
979 "pcmcia: parent PCI bridge window: %pR\n",
994 "pcmcia: parent PCI bridge window: %pR\n",
1011 static inline int nonstatic_autoadd_resources(
struct pcmcia_socket *s)
1033 nonstatic_autoadd_resources(s);
1038 static void nonstatic_release_resource_db(
struct pcmcia_socket *s)
1047 for (p = data->
mem_db.next; p != &data->
mem_db; p = q) {
1051 for (p = data->
io_db.next; p != &data->
io_db; p = q) {
1059 .validate_mem = pcmcia_nonstatic_validate_mem,
1060 .find_io = nonstatic_find_io,
1061 .find_mem = nonstatic_find_mem_region,
1062 .init = nonstatic_init,
1063 .exit = nonstatic_release_resource_db,
1085 "0x%08lx - 0x%08lx\n",
1086 ((
unsigned long) p->
base),
1087 ((
unsigned long) p->
base + p->
num - 1));
1096 const char *buf,
size_t count)
1103 ret =
sscanf(buf,
"+ 0x%lx - 0x%lx", &start_addr, &end_addr);
1105 ret =
sscanf(buf,
"- 0x%lx - 0x%lx", &start_addr, &end_addr);
1108 ret =
sscanf(buf,
"0x%lx - 0x%lx", &start_addr,
1115 if (end_addr < start_addr)
1119 ret = adjust_io(s, add, start_addr, end_addr);
1122 return ret ? ret :
count;
1124 static DEVICE_ATTR(available_resources_io, 0600, show_io_db, store_io_db);
1142 "0x%08lx - 0x%08lx\n",
1143 ((
unsigned long) p->
base),
1144 ((
unsigned long) p->
base + p->
num - 1));
1151 "0x%08lx - 0x%08lx\n",
1152 ((
unsigned long) p->
base),
1153 ((
unsigned long) p->
base + p->
num - 1));
1162 const char *buf,
size_t count)
1169 ret =
sscanf(buf,
"+ 0x%lx - 0x%lx", &start_addr, &end_addr);
1171 ret =
sscanf(buf,
"- 0x%lx - 0x%lx", &start_addr, &end_addr);
1174 ret =
sscanf(buf,
"0x%lx - 0x%lx", &start_addr,
1181 if (end_addr < start_addr)
1185 ret = adjust_memory(s, add, start_addr, end_addr);
1188 return ret ? ret :
count;
1190 static DEVICE_ATTR(available_resources_mem, 0600, show_mem_db, store_mem_db);
1192 static struct attribute *pccard_rsrc_attributes[] = {
1193 &dev_attr_available_resources_io.attr,
1194 &dev_attr_available_resources_mem.attr,
1199 .attrs = pccard_rsrc_attributes,
1224 .add_dev = &pccard_sysfs_add_rsrc,
1225 .remove_dev =
__devexit_p(&pccard_sysfs_remove_rsrc),
1228 static int __init nonstatic_sysfs_init(
void)
1233 static void __exit nonstatic_sysfs_exit(
void)