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