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