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 "cs_internal.h"
24 #ifdef CONFIG_PCMCIA_PROBE
26 static int adjust_irq(struct pcmcia_socket *s, adjust_t *adj)
31 irq = adj->resource.irq.IRQ;
32 if ((irq < 0) || (irq > 15))
35 if (adj->Action != REMOVE_MANAGED_RESOURCE)
40 if (!(s->irq_mask & mask))
50 static inline int adjust_irq(struct pcmcia_socket *s, adjust_t *adj) {
57 int pcmcia_adjust_resource_info(adjust_t *adj)
59 struct pcmcia_socket *s;
60 int ret = CS_UNSUPPORTED_FUNCTION;
63 down_read(&pcmcia_socket_list_rwsem);
64 list_for_each_entry(s, &pcmcia_socket_list, socket_list) {
66 if (adj->Resource == RES_IRQ)
67 ret = adjust_irq(s, adj);
69 else if (s->resource_ops->adjust_resource) {
71 /* you can't use the old interface if the new
72 * one was used before */
73 spin_lock_irqsave(&s->lock, flags);
74 if ((s->resource_setup_new) &&
75 !(s->resource_setup_old)) {
76 spin_unlock_irqrestore(&s->lock, flags);
78 } else if (!(s->resource_setup_old))
79 s->resource_setup_old = 1;
80 spin_unlock_irqrestore(&s->lock, flags);
82 ret = s->resource_ops->adjust_resource(s, adj);
84 /* as there's no way we know this is the
85 * last call to adjust_resource_info, we
86 * always need to assume this is the latest
88 spin_lock_irqsave(&s->lock, flags);
89 s->resource_setup_done = 1;
90 spin_unlock_irqrestore(&s->lock, flags);
94 up_read(&pcmcia_socket_list_rwsem);
98 EXPORT_SYMBOL(pcmcia_adjust_resource_info);
100 int pcmcia_validate_mem(struct pcmcia_socket *s)
102 if (s->resource_ops->validate_mem)
103 return s->resource_ops->validate_mem(s);
104 /* if there is no callback, we can assume that everything is OK */
107 EXPORT_SYMBOL(pcmcia_validate_mem);
109 int pcmcia_adjust_io_region(struct resource *res, unsigned long r_start,
110 unsigned long r_end, struct pcmcia_socket *s)
112 if (s->resource_ops->adjust_io_region)
113 return s->resource_ops->adjust_io_region(res, r_start, r_end, s);
116 EXPORT_SYMBOL(pcmcia_adjust_io_region);
118 struct resource *pcmcia_find_io_region(unsigned long base, int num,
119 unsigned long align, struct pcmcia_socket *s)
121 if (s->resource_ops->find_io)
122 return s->resource_ops->find_io(base, num, align, s);
125 EXPORT_SYMBOL(pcmcia_find_io_region);
127 struct resource *pcmcia_find_mem_region(u_long base, u_long num, u_long align,
128 int low, struct pcmcia_socket *s)
130 if (s->resource_ops->find_mem)
131 return s->resource_ops->find_mem(base, num, align, low, s);
134 EXPORT_SYMBOL(pcmcia_find_mem_region);
136 void release_resource_db(struct pcmcia_socket *s)
138 if (s->resource_ops->exit)
139 s->resource_ops->exit(s);
143 static int static_init(struct pcmcia_socket *s)
147 /* the good thing about SS_CAP_STATIC_MAP sockets is
148 * that they don't need a resource database */
150 spin_lock_irqsave(&s->lock, flags);
151 s->resource_setup_done = 1;
152 spin_unlock_irqrestore(&s->lock, flags);
158 struct pccard_resource_ops pccard_static_ops = {
159 .validate_mem = NULL,
160 .adjust_io_region = NULL,
163 .adjust_resource = NULL,
167 EXPORT_SYMBOL(pccard_static_ops);
170 #ifdef CONFIG_PCCARD_IODYN
172 static struct resource *
173 make_resource(unsigned long b, unsigned long n, int flags, char *name)
175 struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
180 res->end = b + n - 1;
186 struct pcmcia_align_data {
188 unsigned long offset;
191 static void pcmcia_align(void *align_data, struct resource *res,
192 unsigned long size, unsigned long align)
194 struct pcmcia_align_data *data = align_data;
197 start = (res->start & ~data->mask) + data->offset;
198 if (start < res->start)
199 start += data->mask + 1;
203 if (res->flags & IORESOURCE_IO) {
205 start = (start + 0x3ff) & ~0x3ff;
212 if (res->flags & IORESOURCE_IO) {
213 if ((res->start + size - 1) >= 1024)
214 res->start = res->end;
220 static int iodyn_adjust_io_region(struct resource *res, unsigned long r_start,
221 unsigned long r_end, struct pcmcia_socket *s)
223 return adjust_resource(res, r_start, r_end - r_start + 1);
227 static struct resource *iodyn_find_io_region(unsigned long base, int num,
228 unsigned long align, struct pcmcia_socket *s)
230 struct resource *res = make_resource(0, num, IORESOURCE_IO,
232 struct pcmcia_align_data data;
233 unsigned long min = base;
239 data.mask = align - 1;
240 data.offset = base & data.mask;
244 ret = pci_bus_alloc_resource(s->cb_dev->bus, res, num, 1,
245 min, 0, pcmcia_align, &data);
248 ret = allocate_resource(&ioport_resource, res, num, min, ~0UL,
249 1, pcmcia_align, &data);
258 struct pccard_resource_ops pccard_iodyn_ops = {
259 .validate_mem = NULL,
260 .adjust_io_region = iodyn_adjust_io_region,
261 .find_io = iodyn_find_io_region,
263 .adjust_resource = NULL,
267 EXPORT_SYMBOL(pccard_iodyn_ops);
269 #endif /* CONFIG_PCCARD_IODYN */