e5a33121de77b9dca1a3e369243f6211fe926092
[safe/jmp/linux-2.6] / drivers / pcmcia / rsrc_mgr.c
1 /*
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
15 #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
24 #ifdef CONFIG_PCMCIA_PROBE
25
26 static int adjust_irq(struct pcmcia_socket *s, adjust_t *adj)
27 {
28         int irq;
29         u32 mask;
30
31         irq = adj->resource.irq.IRQ;
32         if ((irq < 0) || (irq > 15))
33                 return CS_BAD_IRQ;
34
35         if (adj->Action != REMOVE_MANAGED_RESOURCE)
36                 return 0;
37
38         mask = 1 << irq;
39
40         if (!(s->irq_mask & mask))
41                 return 0;
42
43         s->irq_mask &= ~mask;
44
45         return 0;
46 }
47
48 #else
49
50 static inline int adjust_irq(struct pcmcia_socket *s, adjust_t *adj) {
51         return CS_SUCCESS;
52 }
53
54 #endif
55
56
57 int pcmcia_adjust_resource_info(adjust_t *adj)
58 {
59         struct pcmcia_socket *s;
60         int ret = CS_UNSUPPORTED_FUNCTION;
61         unsigned long flags;
62
63         down_read(&pcmcia_socket_list_rwsem);
64         list_for_each_entry(s, &pcmcia_socket_list, socket_list) {
65
66                 if (adj->Resource == RES_IRQ)
67                         ret = adjust_irq(s, adj);
68
69                 else if (s->resource_ops->adjust_resource) {
70
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);
77                                 continue;
78                         } else if (!(s->resource_setup_old))
79                                 s->resource_setup_old = 1;
80                         spin_unlock_irqrestore(&s->lock, flags);
81
82                         ret = s->resource_ops->adjust_resource(s, adj);
83                         if (!ret) {
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
87                                  * one... */
88                                 spin_lock_irqsave(&s->lock, flags);
89                                 s->resource_setup_done = 1;
90                                 spin_unlock_irqrestore(&s->lock, flags);
91                         }
92                 }
93         }
94         up_read(&pcmcia_socket_list_rwsem);
95
96         return (ret);
97 }
98 EXPORT_SYMBOL(pcmcia_adjust_resource_info);
99
100 int pcmcia_validate_mem(struct pcmcia_socket *s)
101 {
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 */
105         return 0;
106 }
107 EXPORT_SYMBOL(pcmcia_validate_mem);
108
109 int pcmcia_adjust_io_region(struct resource *res, unsigned long r_start,
110                      unsigned long r_end, struct pcmcia_socket *s)
111 {
112         if (s->resource_ops->adjust_io_region)
113                 return s->resource_ops->adjust_io_region(res, r_start, r_end, s);
114         return -ENOMEM;
115 }
116 EXPORT_SYMBOL(pcmcia_adjust_io_region);
117
118 struct resource *pcmcia_find_io_region(unsigned long base, int num,
119                    unsigned long align, struct pcmcia_socket *s)
120 {
121         if (s->resource_ops->find_io)
122                 return s->resource_ops->find_io(base, num, align, s);
123         return NULL;
124 }
125 EXPORT_SYMBOL(pcmcia_find_io_region);
126
127 struct resource *pcmcia_find_mem_region(u_long base, u_long num, u_long align,
128                                  int low, struct pcmcia_socket *s)
129 {
130         if (s->resource_ops->find_mem)
131                 return s->resource_ops->find_mem(base, num, align, low, s);
132         return NULL;
133 }
134 EXPORT_SYMBOL(pcmcia_find_mem_region);
135
136 void release_resource_db(struct pcmcia_socket *s)
137 {
138         if (s->resource_ops->exit)
139                 s->resource_ops->exit(s);
140 }
141
142
143 static int static_init(struct pcmcia_socket *s)
144 {
145         unsigned long flags;
146
147         /* the good thing about SS_CAP_STATIC_MAP sockets is
148          * that they don't need a resource database */
149
150         spin_lock_irqsave(&s->lock, flags);
151         s->resource_setup_done = 1;
152         spin_unlock_irqrestore(&s->lock, flags);
153
154         return 0;
155 }
156
157
158 struct pccard_resource_ops pccard_static_ops = {
159         .validate_mem = NULL,
160         .adjust_io_region = NULL,
161         .find_io = NULL,
162         .find_mem = NULL,
163         .adjust_resource = NULL,
164         .init = static_init,
165         .exit = NULL,
166 };
167 EXPORT_SYMBOL(pccard_static_ops);
168
169
170 #ifdef CONFIG_PCCARD_IODYN
171
172 static struct resource *
173 make_resource(unsigned long b, unsigned long n, int flags, char *name)
174 {
175         struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
176
177         if (res) {
178                 res->name = name;
179                 res->start = b;
180                 res->end = b + n - 1;
181                 res->flags = flags;
182         }
183         return res;
184 }
185
186 struct pcmcia_align_data {
187         unsigned long   mask;
188         unsigned long   offset;
189 };
190
191 static void pcmcia_align(void *align_data, struct resource *res,
192                         unsigned long size, unsigned long align)
193 {
194         struct pcmcia_align_data *data = align_data;
195         unsigned long start;
196
197         start = (res->start & ~data->mask) + data->offset;
198         if (start < res->start)
199                 start += data->mask + 1;
200         res->start = start;
201
202 #ifdef CONFIG_X86
203         if (res->flags & IORESOURCE_IO) {
204                 if (start & 0x300) {
205                         start = (start + 0x3ff) & ~0x3ff;
206                         res->start = start;
207                 }
208         }
209 #endif
210
211 #ifdef CONFIG_M68K
212         if (res->flags & IORESOURCE_IO) {
213                 if ((res->start + size - 1) >= 1024)
214                         res->start = res->end;
215         }
216 #endif
217 }
218
219
220 static int iodyn_adjust_io_region(struct resource *res, unsigned long r_start,
221                                       unsigned long r_end, struct pcmcia_socket *s)
222 {
223         return adjust_resource(res, r_start, r_end - r_start + 1);
224 }
225
226
227 static struct resource *iodyn_find_io_region(unsigned long base, int num,
228                 unsigned long align, struct pcmcia_socket *s)
229 {
230         struct resource *res = make_resource(0, num, IORESOURCE_IO,
231                                              s->dev.class_id);
232         struct pcmcia_align_data data;
233         unsigned long min = base;
234         int ret;
235
236         if (align == 0)
237                 align = 0x10000;
238
239         data.mask = align - 1;
240         data.offset = base & data.mask;
241
242 #ifdef CONFIG_PCI
243         if (s->cb_dev) {
244                 ret = pci_bus_alloc_resource(s->cb_dev->bus, res, num, 1,
245                                              min, 0, pcmcia_align, &data);
246         } else
247 #endif
248                 ret = allocate_resource(&ioport_resource, res, num, min, ~0UL,
249                                         1, pcmcia_align, &data);
250
251         if (ret != 0) {
252                 kfree(res);
253                 res = NULL;
254         }
255         return res;
256 }
257
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,
262         .find_mem = NULL,
263         .adjust_resource = NULL,
264         .init = static_init,
265         .exit = NULL,
266 };
267 EXPORT_SYMBOL(pccard_iodyn_ops);
268
269 #endif /* CONFIG_PCCARD_IODYN */