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