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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Dominik Brodowski | 9128422 | 2009-10-18 23:32:33 +0200 | [diff] [blame^] | 21 | #include <pcmcia/cistpl.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include "cs_internal.h" |
| 23 | |
| 24 | |
Dominik Brodowski | de75914 | 2005-09-28 19:41:56 +0200 | [diff] [blame] | 25 | int pcmcia_validate_mem(struct pcmcia_socket *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | { |
| 27 | if (s->resource_ops->validate_mem) |
Dominik Brodowski | de75914 | 2005-09-28 19:41:56 +0200 | [diff] [blame] | 28 | return s->resource_ops->validate_mem(s); |
| 29 | /* if there is no callback, we can assume that everything is OK */ |
| 30 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | } |
| 32 | EXPORT_SYMBOL(pcmcia_validate_mem); |
| 33 | |
Dominik Brodowski | e6ea0b9 | 2005-06-27 16:28:52 -0700 | [diff] [blame] | 34 | int pcmcia_adjust_io_region(struct resource *res, unsigned long r_start, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 41 | EXPORT_SYMBOL(pcmcia_adjust_io_region); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Dominik Brodowski | e6ea0b9 | 2005-06-27 16:28:52 -0700 | [diff] [blame] | 43 | struct resource *pcmcia_find_io_region(unsigned long base, int num, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 50 | EXPORT_SYMBOL(pcmcia_find_io_region); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Dominik Brodowski | e6ea0b9 | 2005-06-27 16:28:52 -0700 | [diff] [blame] | 52 | 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] | 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 | } |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 59 | EXPORT_SYMBOL(pcmcia_find_mem_region); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
| 61 | void release_resource_db(struct pcmcia_socket *s) |
| 62 | { |
| 63 | if (s->resource_ops->exit) |
| 64 | s->resource_ops->exit(s); |
| 65 | } |
| 66 | |
| 67 | |
| 68 | static int static_init(struct pcmcia_socket *s) |
| 69 | { |
| 70 | unsigned long flags; |
| 71 | |
| 72 | /* the good thing about SS_CAP_STATIC_MAP sockets is |
| 73 | * that they don't need a resource database */ |
| 74 | |
| 75 | spin_lock_irqsave(&s->lock, flags); |
| 76 | s->resource_setup_done = 1; |
| 77 | spin_unlock_irqrestore(&s->lock, flags); |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | |
| 83 | struct pccard_resource_ops pccard_static_ops = { |
| 84 | .validate_mem = NULL, |
| 85 | .adjust_io_region = NULL, |
| 86 | .find_io = NULL, |
| 87 | .find_mem = NULL, |
Dominik Brodowski | c502380 | 2008-06-19 19:02:52 +0200 | [diff] [blame] | 88 | .add_io = NULL, |
| 89 | .add_mem = NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | .init = static_init, |
| 91 | .exit = NULL, |
| 92 | }; |
| 93 | EXPORT_SYMBOL(pccard_static_ops); |
Dominik Brodowski | 3b27e94 | 2005-12-07 12:32:20 +0100 | [diff] [blame] | 94 | |
| 95 | |
| 96 | #ifdef CONFIG_PCCARD_IODYN |
| 97 | |
| 98 | static struct resource * |
| 99 | make_resource(unsigned long b, unsigned long n, int flags, char *name) |
| 100 | { |
| 101 | struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL); |
| 102 | |
| 103 | if (res) { |
| 104 | res->name = name; |
| 105 | res->start = b; |
| 106 | res->end = b + n - 1; |
| 107 | res->flags = flags; |
| 108 | } |
| 109 | return res; |
| 110 | } |
| 111 | |
| 112 | struct pcmcia_align_data { |
| 113 | unsigned long mask; |
| 114 | unsigned long offset; |
| 115 | }; |
| 116 | |
| 117 | static void pcmcia_align(void *align_data, struct resource *res, |
| 118 | unsigned long size, unsigned long align) |
| 119 | { |
| 120 | struct pcmcia_align_data *data = align_data; |
| 121 | unsigned long start; |
| 122 | |
| 123 | start = (res->start & ~data->mask) + data->offset; |
| 124 | if (start < res->start) |
| 125 | start += data->mask + 1; |
| 126 | res->start = start; |
| 127 | |
| 128 | #ifdef CONFIG_X86 |
| 129 | if (res->flags & IORESOURCE_IO) { |
| 130 | if (start & 0x300) { |
| 131 | start = (start + 0x3ff) & ~0x3ff; |
| 132 | res->start = start; |
| 133 | } |
| 134 | } |
| 135 | #endif |
| 136 | |
| 137 | #ifdef CONFIG_M68K |
| 138 | if (res->flags & IORESOURCE_IO) { |
| 139 | if ((res->start + size - 1) >= 1024) |
| 140 | res->start = res->end; |
| 141 | } |
| 142 | #endif |
| 143 | } |
| 144 | |
| 145 | |
| 146 | static int iodyn_adjust_io_region(struct resource *res, unsigned long r_start, |
| 147 | unsigned long r_end, struct pcmcia_socket *s) |
| 148 | { |
| 149 | return adjust_resource(res, r_start, r_end - r_start + 1); |
| 150 | } |
| 151 | |
| 152 | |
| 153 | static struct resource *iodyn_find_io_region(unsigned long base, int num, |
| 154 | unsigned long align, struct pcmcia_socket *s) |
| 155 | { |
| 156 | struct resource *res = make_resource(0, num, IORESOURCE_IO, |
Kay Sievers | f2fecec | 2009-03-24 16:38:22 -0700 | [diff] [blame] | 157 | dev_name(&s->dev)); |
Dominik Brodowski | 3b27e94 | 2005-12-07 12:32:20 +0100 | [diff] [blame] | 158 | struct pcmcia_align_data data; |
| 159 | unsigned long min = base; |
| 160 | int ret; |
| 161 | |
| 162 | if (align == 0) |
| 163 | align = 0x10000; |
| 164 | |
| 165 | data.mask = align - 1; |
| 166 | data.offset = base & data.mask; |
| 167 | |
| 168 | #ifdef CONFIG_PCI |
| 169 | if (s->cb_dev) { |
| 170 | ret = pci_bus_alloc_resource(s->cb_dev->bus, res, num, 1, |
| 171 | min, 0, pcmcia_align, &data); |
| 172 | } else |
| 173 | #endif |
| 174 | ret = allocate_resource(&ioport_resource, res, num, min, ~0UL, |
| 175 | 1, pcmcia_align, &data); |
| 176 | |
| 177 | if (ret != 0) { |
| 178 | kfree(res); |
| 179 | res = NULL; |
| 180 | } |
| 181 | return res; |
| 182 | } |
| 183 | |
| 184 | struct pccard_resource_ops pccard_iodyn_ops = { |
| 185 | .validate_mem = NULL, |
| 186 | .adjust_io_region = iodyn_adjust_io_region, |
| 187 | .find_io = iodyn_find_io_region, |
| 188 | .find_mem = NULL, |
Dominik Brodowski | c502380 | 2008-06-19 19:02:52 +0200 | [diff] [blame] | 189 | .add_io = NULL, |
| 190 | .add_mem = NULL, |
Dominik Brodowski | 3b27e94 | 2005-12-07 12:32:20 +0100 | [diff] [blame] | 191 | .init = static_init, |
| 192 | .exit = NULL, |
| 193 | }; |
| 194 | EXPORT_SYMBOL(pccard_iodyn_ops); |
| 195 | |
| 196 | #endif /* CONFIG_PCCARD_IODYN */ |