pcmcia: remove useless indirection
[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 <pcmcia/cistpl.h>
22 #include "cs_internal.h"
23
24
25 int pcmcia_validate_mem(struct pcmcia_socket *s)
26 {
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;
31 }
32 EXPORT_SYMBOL(pcmcia_validate_mem);
33
34 int pcmcia_adjust_io_region(struct resource *res, unsigned long r_start,
35                      unsigned long r_end, struct pcmcia_socket *s)
36 {
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;
40 }
41 EXPORT_SYMBOL(pcmcia_adjust_io_region);
42
43 struct resource *pcmcia_find_io_region(unsigned long base, int num,
44                    unsigned long align, struct pcmcia_socket *s)
45 {
46         if (s->resource_ops->find_io)
47                 return s->resource_ops->find_io(base, num, align, s);
48         return NULL;
49 }
50 EXPORT_SYMBOL(pcmcia_find_io_region);
51
52 struct resource *pcmcia_find_mem_region(u_long base, u_long num, u_long align,
53                                  int low, struct pcmcia_socket *s)
54 {
55         if (s->resource_ops->find_mem)
56                 return s->resource_ops->find_mem(base, num, align, low, s);
57         return NULL;
58 }
59 EXPORT_SYMBOL(pcmcia_find_mem_region);
60
61
62 static int static_init(struct pcmcia_socket *s)
63 {
64         unsigned long flags;
65
66         /* the good thing about SS_CAP_STATIC_MAP sockets is
67          * that they don't need a resource database */
68
69         spin_lock_irqsave(&s->lock, flags);
70         s->resource_setup_done = 1;
71         spin_unlock_irqrestore(&s->lock, flags);
72
73         return 0;
74 }
75
76
77 struct pccard_resource_ops pccard_static_ops = {
78         .validate_mem = NULL,
79         .adjust_io_region = NULL,
80         .find_io = NULL,
81         .find_mem = NULL,
82         .add_io = NULL,
83         .add_mem = NULL,
84         .init = static_init,
85         .exit = NULL,
86 };
87 EXPORT_SYMBOL(pccard_static_ops);
88
89
90 #ifdef CONFIG_PCCARD_IODYN
91
92 static struct resource *
93 make_resource(unsigned long b, unsigned long n, int flags, char *name)
94 {
95         struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
96
97         if (res) {
98                 res->name = name;
99                 res->start = b;
100                 res->end = b + n - 1;
101                 res->flags = flags;
102         }
103         return res;
104 }
105
106 struct pcmcia_align_data {
107         unsigned long   mask;
108         unsigned long   offset;
109 };
110
111 static void pcmcia_align(void *align_data, struct resource *res,
112                         unsigned long size, unsigned long align)
113 {
114         struct pcmcia_align_data *data = align_data;
115         unsigned long start;
116
117         start = (res->start & ~data->mask) + data->offset;
118         if (start < res->start)
119                 start += data->mask + 1;
120         res->start = start;
121
122 #ifdef CONFIG_X86
123         if (res->flags & IORESOURCE_IO) {
124                 if (start & 0x300) {
125                         start = (start + 0x3ff) & ~0x3ff;
126                         res->start = start;
127                 }
128         }
129 #endif
130
131 #ifdef CONFIG_M68K
132         if (res->flags & IORESOURCE_IO) {
133                 if ((res->start + size - 1) >= 1024)
134                         res->start = res->end;
135         }
136 #endif
137 }
138
139
140 static int iodyn_adjust_io_region(struct resource *res, unsigned long r_start,
141                                       unsigned long r_end, struct pcmcia_socket *s)
142 {
143         return adjust_resource(res, r_start, r_end - r_start + 1);
144 }
145
146
147 static struct resource *iodyn_find_io_region(unsigned long base, int num,
148                 unsigned long align, struct pcmcia_socket *s)
149 {
150         struct resource *res = make_resource(0, num, IORESOURCE_IO,
151                                              dev_name(&s->dev));
152         struct pcmcia_align_data data;
153         unsigned long min = base;
154         int ret;
155
156         if (align == 0)
157                 align = 0x10000;
158
159         data.mask = align - 1;
160         data.offset = base & data.mask;
161
162 #ifdef CONFIG_PCI
163         if (s->cb_dev) {
164                 ret = pci_bus_alloc_resource(s->cb_dev->bus, res, num, 1,
165                                              min, 0, pcmcia_align, &data);
166         } else
167 #endif
168                 ret = allocate_resource(&ioport_resource, res, num, min, ~0UL,
169                                         1, pcmcia_align, &data);
170
171         if (ret != 0) {
172                 kfree(res);
173                 res = NULL;
174         }
175         return res;
176 }
177
178 struct pccard_resource_ops pccard_iodyn_ops = {
179         .validate_mem = NULL,
180         .adjust_io_region = iodyn_adjust_io_region,
181         .find_io = iodyn_find_io_region,
182         .find_mem = NULL,
183         .add_io = NULL,
184         .add_mem = NULL,
185         .init = static_init,
186         .exit = NULL,
187 };
188 EXPORT_SYMBOL(pccard_iodyn_ops);
189
190 #endif /* CONFIG_PCCARD_IODYN */