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