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 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Dominik Brodowski | 9128422 | 2009-10-18 23:32:33 +0200 | [diff] [blame] | 22 | #include <pcmcia/cistpl.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include "cs_internal.h" |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | static int static_init(struct pcmcia_socket *s) |
| 26 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | /* the good thing about SS_CAP_STATIC_MAP sockets is |
| 28 | * that they don't need a resource database */ |
| 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | s->resource_setup_done = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
| 32 | return 0; |
| 33 | } |
| 34 | |
| 35 | |
| 36 | struct pccard_resource_ops pccard_static_ops = { |
| 37 | .validate_mem = NULL, |
| 38 | .adjust_io_region = NULL, |
| 39 | .find_io = NULL, |
| 40 | .find_mem = NULL, |
Dominik Brodowski | c502380 | 2008-06-19 19:02:52 +0200 | [diff] [blame] | 41 | .add_io = NULL, |
| 42 | .add_mem = NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | .init = static_init, |
| 44 | .exit = NULL, |
| 45 | }; |
| 46 | EXPORT_SYMBOL(pccard_static_ops); |
Dominik Brodowski | 3b27e94 | 2005-12-07 12:32:20 +0100 | [diff] [blame] | 47 | |
| 48 | |
| 49 | #ifdef CONFIG_PCCARD_IODYN |
| 50 | |
| 51 | static struct resource * |
| 52 | make_resource(unsigned long b, unsigned long n, int flags, char *name) |
| 53 | { |
| 54 | struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL); |
| 55 | |
| 56 | if (res) { |
| 57 | res->name = name; |
| 58 | res->start = b; |
| 59 | res->end = b + n - 1; |
| 60 | res->flags = flags; |
| 61 | } |
| 62 | return res; |
| 63 | } |
| 64 | |
| 65 | struct pcmcia_align_data { |
| 66 | unsigned long mask; |
| 67 | unsigned long offset; |
| 68 | }; |
| 69 | |
Dominik Brodowski | 3b7a17f | 2010-01-01 17:40:50 +0100 | [diff] [blame] | 70 | static resource_size_t pcmcia_align(void *align_data, |
| 71 | const struct resource *res, |
Dominik Brodowski | b26b2d4 | 2010-01-01 17:40:49 +0100 | [diff] [blame] | 72 | resource_size_t size, resource_size_t align) |
Dominik Brodowski | 3b27e94 | 2005-12-07 12:32:20 +0100 | [diff] [blame] | 73 | { |
| 74 | struct pcmcia_align_data *data = align_data; |
Dominik Brodowski | b26b2d4 | 2010-01-01 17:40:49 +0100 | [diff] [blame] | 75 | resource_size_t start; |
Dominik Brodowski | 3b27e94 | 2005-12-07 12:32:20 +0100 | [diff] [blame] | 76 | |
| 77 | start = (res->start & ~data->mask) + data->offset; |
| 78 | if (start < res->start) |
| 79 | start += data->mask + 1; |
Dominik Brodowski | 3b27e94 | 2005-12-07 12:32:20 +0100 | [diff] [blame] | 80 | |
| 81 | #ifdef CONFIG_X86 |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 82 | if (res->flags & IORESOURCE_IO) { |
Dominik Brodowski | 6e83ee0 | 2010-03-02 08:57:33 +0100 | [diff] [blame] | 83 | if (start & 0x300) |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 84 | start = (start + 0x3ff) & ~0x3ff; |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 85 | } |
Dominik Brodowski | 3b27e94 | 2005-12-07 12:32:20 +0100 | [diff] [blame] | 86 | #endif |
| 87 | |
| 88 | #ifdef CONFIG_M68K |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 89 | if (res->flags & IORESOURCE_IO) { |
Dominik Brodowski | 3b27e94 | 2005-12-07 12:32:20 +0100 | [diff] [blame] | 90 | if ((res->start + size - 1) >= 1024) |
Dominik Brodowski | b26b2d4 | 2010-01-01 17:40:49 +0100 | [diff] [blame] | 91 | start = res->end; |
Dominik Brodowski | 3b27e94 | 2005-12-07 12:32:20 +0100 | [diff] [blame] | 92 | } |
| 93 | #endif |
Dominik Brodowski | b26b2d4 | 2010-01-01 17:40:49 +0100 | [diff] [blame] | 94 | |
| 95 | return start; |
Dominik Brodowski | 3b27e94 | 2005-12-07 12:32:20 +0100 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | |
| 99 | static int iodyn_adjust_io_region(struct resource *res, unsigned long r_start, |
| 100 | unsigned long r_end, struct pcmcia_socket *s) |
| 101 | { |
| 102 | return adjust_resource(res, r_start, r_end - r_start + 1); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | static struct resource *iodyn_find_io_region(unsigned long base, int num, |
| 107 | unsigned long align, struct pcmcia_socket *s) |
| 108 | { |
| 109 | struct resource *res = make_resource(0, num, IORESOURCE_IO, |
Kay Sievers | f2fecec | 2009-03-24 16:38:22 -0700 | [diff] [blame] | 110 | dev_name(&s->dev)); |
Dominik Brodowski | 3b27e94 | 2005-12-07 12:32:20 +0100 | [diff] [blame] | 111 | struct pcmcia_align_data data; |
| 112 | unsigned long min = base; |
| 113 | int ret; |
| 114 | |
| 115 | if (align == 0) |
| 116 | align = 0x10000; |
| 117 | |
| 118 | data.mask = align - 1; |
| 119 | data.offset = base & data.mask; |
| 120 | |
| 121 | #ifdef CONFIG_PCI |
| 122 | if (s->cb_dev) { |
| 123 | ret = pci_bus_alloc_resource(s->cb_dev->bus, res, num, 1, |
| 124 | min, 0, pcmcia_align, &data); |
| 125 | } else |
| 126 | #endif |
| 127 | ret = allocate_resource(&ioport_resource, res, num, min, ~0UL, |
| 128 | 1, pcmcia_align, &data); |
| 129 | |
| 130 | if (ret != 0) { |
| 131 | kfree(res); |
| 132 | res = NULL; |
| 133 | } |
| 134 | return res; |
| 135 | } |
| 136 | |
| 137 | struct pccard_resource_ops pccard_iodyn_ops = { |
| 138 | .validate_mem = NULL, |
| 139 | .adjust_io_region = iodyn_adjust_io_region, |
| 140 | .find_io = iodyn_find_io_region, |
| 141 | .find_mem = NULL, |
Dominik Brodowski | c502380 | 2008-06-19 19:02:52 +0200 | [diff] [blame] | 142 | .add_io = NULL, |
| 143 | .add_mem = NULL, |
Dominik Brodowski | 3b27e94 | 2005-12-07 12:32:20 +0100 | [diff] [blame] | 144 | .init = static_init, |
| 145 | .exit = NULL, |
| 146 | }; |
| 147 | EXPORT_SYMBOL(pccard_iodyn_ops); |
| 148 | |
| 149 | #endif /* CONFIG_PCCARD_IODYN */ |