Merge git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi-misc-2.6
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / pcmcia / rsrc_mgr.c
blobf8401a0ef89b97de9d5cebc76b8867ddd097875d
1 /*
2 * rsrc_mgr.c -- Resource management routines and/or wrappers
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/kernel.h>
18 #include <pcmcia/cs_types.h>
19 #include <pcmcia/ss.h>
20 #include <pcmcia/cs.h>
21 #include <pcmcia/cistpl.h>
22 #include "cs_internal.h"
25 int pcmcia_validate_mem(struct pcmcia_socket *s)
27 if (s->resource_ops->validate_mem)
28 return s->resource_ops->validate_mem(s);
29 /* if there is no callback, we can assume that everything is OK */
30 return 0;
32 EXPORT_SYMBOL(pcmcia_validate_mem);
34 int pcmcia_adjust_io_region(struct resource *res, unsigned long r_start,
35 unsigned long r_end, struct pcmcia_socket *s)
37 if (s->resource_ops->adjust_io_region)
38 return s->resource_ops->adjust_io_region(res, r_start, r_end, s);
39 return -ENOMEM;
41 EXPORT_SYMBOL(pcmcia_adjust_io_region);
43 struct resource *pcmcia_find_io_region(unsigned long base, int num,
44 unsigned long align, struct pcmcia_socket *s)
46 if (s->resource_ops->find_io)
47 return s->resource_ops->find_io(base, num, align, s);
48 return NULL;
50 EXPORT_SYMBOL(pcmcia_find_io_region);
52 struct resource *pcmcia_find_mem_region(u_long base, u_long num, u_long align,
53 int low, struct pcmcia_socket *s)
55 if (s->resource_ops->find_mem)
56 return s->resource_ops->find_mem(base, num, align, low, s);
57 return NULL;
59 EXPORT_SYMBOL(pcmcia_find_mem_region);
61 void release_resource_db(struct pcmcia_socket *s)
63 if (s->resource_ops->exit)
64 s->resource_ops->exit(s);
68 static int static_init(struct pcmcia_socket *s)
70 unsigned long flags;
72 /* the good thing about SS_CAP_STATIC_MAP sockets is
73 * that they don't need a resource database */
75 spin_lock_irqsave(&s->lock, flags);
76 s->resource_setup_done = 1;
77 spin_unlock_irqrestore(&s->lock, flags);
79 return 0;
83 struct pccard_resource_ops pccard_static_ops = {
84 .validate_mem = NULL,
85 .adjust_io_region = NULL,
86 .find_io = NULL,
87 .find_mem = NULL,
88 .add_io = NULL,
89 .add_mem = NULL,
90 .init = static_init,
91 .exit = NULL,
93 EXPORT_SYMBOL(pccard_static_ops);
96 #ifdef CONFIG_PCCARD_IODYN
98 static struct resource *
99 make_resource(unsigned long b, unsigned long n, int flags, char *name)
101 struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
103 if (res) {
104 res->name = name;
105 res->start = b;
106 res->end = b + n - 1;
107 res->flags = flags;
109 return res;
112 struct pcmcia_align_data {
113 unsigned long mask;
114 unsigned long offset;
117 static resource_size_t pcmcia_align(void *align_data,
118 const struct resource *res,
119 resource_size_t size, resource_size_t align)
121 struct pcmcia_align_data *data = align_data;
122 resource_size_t start;
124 start = (res->start & ~data->mask) + data->offset;
125 if (start < res->start)
126 start += data->mask + 1;
128 #ifdef CONFIG_X86
129 if (res->flags & IORESOURCE_IO) {
130 if (start & 0x300) {
131 start = (start + 0x3ff) & ~0x3ff;
134 #endif
136 #ifdef CONFIG_M68K
137 if (res->flags & IORESOURCE_IO) {
138 if ((res->start + size - 1) >= 1024)
139 start = res->end;
141 #endif
143 return start;
147 static int iodyn_adjust_io_region(struct resource *res, unsigned long r_start,
148 unsigned long r_end, struct pcmcia_socket *s)
150 return adjust_resource(res, r_start, r_end - r_start + 1);
154 static struct resource *iodyn_find_io_region(unsigned long base, int num,
155 unsigned long align, struct pcmcia_socket *s)
157 struct resource *res = make_resource(0, num, IORESOURCE_IO,
158 dev_name(&s->dev));
159 struct pcmcia_align_data data;
160 unsigned long min = base;
161 int ret;
163 if (align == 0)
164 align = 0x10000;
166 data.mask = align - 1;
167 data.offset = base & data.mask;
169 #ifdef CONFIG_PCI
170 if (s->cb_dev) {
171 ret = pci_bus_alloc_resource(s->cb_dev->bus, res, num, 1,
172 min, 0, pcmcia_align, &data);
173 } else
174 #endif
175 ret = allocate_resource(&ioport_resource, res, num, min, ~0UL,
176 1, pcmcia_align, &data);
178 if (ret != 0) {
179 kfree(res);
180 res = NULL;
182 return res;
185 struct pccard_resource_ops pccard_iodyn_ops = {
186 .validate_mem = NULL,
187 .adjust_io_region = iodyn_adjust_io_region,
188 .find_io = iodyn_find_io_region,
189 .find_mem = NULL,
190 .add_io = NULL,
191 .add_mem = NULL,
192 .init = static_init,
193 .exit = NULL,
195 EXPORT_SYMBOL(pccard_iodyn_ops);
197 #endif /* CONFIG_PCCARD_IODYN */