blob: c0e2afc79e3e4555877562498d30035aa65815ae [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * rsrc_mgr.c -- Resource management routines and/or wrappers
3 *
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.
7 *
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.
11 *
12 * (C) 1999 David A. Hinds
13 */
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/module.h>
16#include <linux/kernel.h>
17
18#include <pcmcia/cs_types.h>
19#include <pcmcia/ss.h>
20#include <pcmcia/cs.h>
21#include "cs_internal.h"
22
23
Dominik Brodowskide759142005-09-28 19:41:56 +020024int pcmcia_validate_mem(struct pcmcia_socket *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
26 if (s->resource_ops->validate_mem)
Dominik Brodowskide759142005-09-28 19:41:56 +020027 return s->resource_ops->validate_mem(s);
28 /* if there is no callback, we can assume that everything is OK */
29 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030}
31EXPORT_SYMBOL(pcmcia_validate_mem);
32
Dominik Brodowskie6ea0b9e2005-06-27 16:28:52 -070033int pcmcia_adjust_io_region(struct resource *res, unsigned long r_start,
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 unsigned long r_end, struct pcmcia_socket *s)
35{
36 if (s->resource_ops->adjust_io_region)
37 return s->resource_ops->adjust_io_region(res, r_start, r_end, s);
38 return -ENOMEM;
39}
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070040EXPORT_SYMBOL(pcmcia_adjust_io_region);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Dominik Brodowskie6ea0b9e2005-06-27 16:28:52 -070042struct resource *pcmcia_find_io_region(unsigned long base, int num,
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 unsigned long align, struct pcmcia_socket *s)
44{
45 if (s->resource_ops->find_io)
46 return s->resource_ops->find_io(base, num, align, s);
47 return NULL;
48}
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070049EXPORT_SYMBOL(pcmcia_find_io_region);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Dominik Brodowskie6ea0b9e2005-06-27 16:28:52 -070051struct resource *pcmcia_find_mem_region(u_long base, u_long num, u_long align,
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 int low, struct pcmcia_socket *s)
53{
54 if (s->resource_ops->find_mem)
55 return s->resource_ops->find_mem(base, num, align, low, s);
56 return NULL;
57}
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070058EXPORT_SYMBOL(pcmcia_find_mem_region);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60void release_resource_db(struct pcmcia_socket *s)
61{
62 if (s->resource_ops->exit)
63 s->resource_ops->exit(s);
64}
65
66
67static int static_init(struct pcmcia_socket *s)
68{
69 unsigned long flags;
70
71 /* the good thing about SS_CAP_STATIC_MAP sockets is
72 * that they don't need a resource database */
73
74 spin_lock_irqsave(&s->lock, flags);
75 s->resource_setup_done = 1;
76 spin_unlock_irqrestore(&s->lock, flags);
77
78 return 0;
79}
80
81
82struct pccard_resource_ops pccard_static_ops = {
83 .validate_mem = NULL,
84 .adjust_io_region = NULL,
85 .find_io = NULL,
86 .find_mem = NULL,
Dominik Brodowskic5023802008-06-19 19:02:52 +020087 .add_io = NULL,
88 .add_mem = NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 .init = static_init,
90 .exit = NULL,
91};
92EXPORT_SYMBOL(pccard_static_ops);
Dominik Brodowski3b27e942005-12-07 12:32:20 +010093
94
95#ifdef CONFIG_PCCARD_IODYN
96
97static struct resource *
98make_resource(unsigned long b, unsigned long n, int flags, char *name)
99{
100 struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
101
102 if (res) {
103 res->name = name;
104 res->start = b;
105 res->end = b + n - 1;
106 res->flags = flags;
107 }
108 return res;
109}
110
111struct pcmcia_align_data {
112 unsigned long mask;
113 unsigned long offset;
114};
115
116static void pcmcia_align(void *align_data, struct resource *res,
117 unsigned long size, unsigned long align)
118{
119 struct pcmcia_align_data *data = align_data;
120 unsigned long start;
121
122 start = (res->start & ~data->mask) + data->offset;
123 if (start < res->start)
124 start += data->mask + 1;
125 res->start = start;
126
127#ifdef CONFIG_X86
128 if (res->flags & IORESOURCE_IO) {
129 if (start & 0x300) {
130 start = (start + 0x3ff) & ~0x3ff;
131 res->start = start;
132 }
133 }
134#endif
135
136#ifdef CONFIG_M68K
137 if (res->flags & IORESOURCE_IO) {
138 if ((res->start + size - 1) >= 1024)
139 res->start = res->end;
140 }
141#endif
142}
143
144
145static int iodyn_adjust_io_region(struct resource *res, unsigned long r_start,
146 unsigned long r_end, struct pcmcia_socket *s)
147{
148 return adjust_resource(res, r_start, r_end - r_start + 1);
149}
150
151
152static struct resource *iodyn_find_io_region(unsigned long base, int num,
153 unsigned long align, struct pcmcia_socket *s)
154{
155 struct resource *res = make_resource(0, num, IORESOURCE_IO,
Manuel Laussdfe461a2007-02-21 14:47:20 +0100156 s->dev.bus_id);
Dominik Brodowski3b27e942005-12-07 12:32:20 +0100157 struct pcmcia_align_data data;
158 unsigned long min = base;
159 int ret;
160
161 if (align == 0)
162 align = 0x10000;
163
164 data.mask = align - 1;
165 data.offset = base & data.mask;
166
167#ifdef CONFIG_PCI
168 if (s->cb_dev) {
169 ret = pci_bus_alloc_resource(s->cb_dev->bus, res, num, 1,
170 min, 0, pcmcia_align, &data);
171 } else
172#endif
173 ret = allocate_resource(&ioport_resource, res, num, min, ~0UL,
174 1, pcmcia_align, &data);
175
176 if (ret != 0) {
177 kfree(res);
178 res = NULL;
179 }
180 return res;
181}
182
183struct pccard_resource_ops pccard_iodyn_ops = {
184 .validate_mem = NULL,
185 .adjust_io_region = iodyn_adjust_io_region,
186 .find_io = iodyn_find_io_region,
187 .find_mem = NULL,
Dominik Brodowskic5023802008-06-19 19:02:52 +0200188 .add_io = NULL,
189 .add_mem = NULL,
Dominik Brodowski3b27e942005-12-07 12:32:20 +0100190 .init = static_init,
191 .exit = NULL,
192};
193EXPORT_SYMBOL(pccard_iodyn_ops);
194
195#endif /* CONFIG_PCCARD_IODYN */