Alan Cox | 02b0384 | 2014-12-10 15:07:36 +0000 | [diff] [blame] | 1 | #include <linux/slab.h> |
| 2 | #include <linux/module.h> |
| 3 | #include <linux/kernel.h> |
Arnd Bergmann | d885d4f | 2015-01-13 15:11:55 +0100 | [diff] [blame^] | 4 | #include <linux/pci.h> |
Alan Cox | 02b0384 | 2014-12-10 15:07:36 +0000 | [diff] [blame] | 5 | |
| 6 | #include <pcmcia/ss.h> |
| 7 | #include <pcmcia/cistpl.h> |
| 8 | #include "cs_internal.h" |
| 9 | |
| 10 | |
| 11 | struct pcmcia_align_data { |
| 12 | unsigned long mask; |
| 13 | unsigned long offset; |
| 14 | }; |
| 15 | |
| 16 | static resource_size_t pcmcia_align(void *align_data, |
| 17 | const struct resource *res, |
| 18 | resource_size_t size, resource_size_t align) |
| 19 | { |
| 20 | struct pcmcia_align_data *data = align_data; |
| 21 | resource_size_t start; |
| 22 | |
| 23 | start = (res->start & ~data->mask) + data->offset; |
| 24 | if (start < res->start) |
| 25 | start += data->mask + 1; |
| 26 | return start; |
| 27 | } |
| 28 | |
| 29 | static struct resource *find_io_region(struct pcmcia_socket *s, |
| 30 | unsigned long base, int num, |
| 31 | unsigned long align) |
| 32 | { |
| 33 | struct resource *res = pcmcia_make_resource(0, num, IORESOURCE_IO, |
| 34 | dev_name(&s->dev)); |
| 35 | struct pcmcia_align_data data; |
| 36 | int ret; |
| 37 | |
| 38 | data.mask = align - 1; |
| 39 | data.offset = base & data.mask; |
| 40 | |
| 41 | ret = pci_bus_alloc_resource(s->cb_dev->bus, res, num, 1, |
| 42 | base, 0, pcmcia_align, &data); |
| 43 | if (ret != 0) { |
| 44 | kfree(res); |
| 45 | res = NULL; |
| 46 | } |
| 47 | return res; |
| 48 | } |
| 49 | |
| 50 | static int res_pci_find_io(struct pcmcia_socket *s, unsigned int attr, |
| 51 | unsigned int *base, unsigned int num, |
| 52 | unsigned int align, struct resource **parent) |
| 53 | { |
| 54 | int i, ret = 0; |
| 55 | |
| 56 | /* Check for an already-allocated window that must conflict with |
| 57 | * what was asked for. It is a hack because it does not catch all |
| 58 | * potential conflicts, just the most obvious ones. |
| 59 | */ |
| 60 | for (i = 0; i < MAX_IO_WIN; i++) { |
| 61 | if (!s->io[i].res) |
| 62 | continue; |
| 63 | |
| 64 | if (!*base) |
| 65 | continue; |
| 66 | |
| 67 | if ((s->io[i].res->start & (align-1)) == *base) |
| 68 | return -EBUSY; |
| 69 | } |
| 70 | |
| 71 | for (i = 0; i < MAX_IO_WIN; i++) { |
| 72 | struct resource *res = s->io[i].res; |
| 73 | unsigned int try; |
| 74 | |
| 75 | if (res && (res->flags & IORESOURCE_BITS) != |
| 76 | (attr & IORESOURCE_BITS)) |
| 77 | continue; |
| 78 | |
| 79 | if (!res) { |
| 80 | if (align == 0) |
| 81 | align = 0x10000; |
| 82 | |
| 83 | res = s->io[i].res = find_io_region(s, *base, num, |
| 84 | align); |
| 85 | if (!res) |
| 86 | return -EINVAL; |
| 87 | |
| 88 | *base = res->start; |
| 89 | s->io[i].res->flags = |
| 90 | ((res->flags & ~IORESOURCE_BITS) | |
| 91 | (attr & IORESOURCE_BITS)); |
| 92 | s->io[i].InUse = num; |
| 93 | *parent = res; |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | /* Try to extend top of window */ |
| 98 | try = res->end + 1; |
| 99 | if ((*base == 0) || (*base == try)) { |
| 100 | ret = adjust_resource(s->io[i].res, res->start, |
| 101 | resource_size(res) + num); |
| 102 | if (ret) |
| 103 | continue; |
| 104 | *base = try; |
| 105 | s->io[i].InUse += num; |
| 106 | *parent = res; |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | /* Try to extend bottom of window */ |
| 111 | try = res->start - num; |
| 112 | if ((*base == 0) || (*base == try)) { |
| 113 | ret = adjust_resource(s->io[i].res, |
| 114 | res->start - num, |
| 115 | resource_size(res) + num); |
| 116 | if (ret) |
| 117 | continue; |
| 118 | *base = try; |
| 119 | s->io[i].InUse += num; |
| 120 | *parent = res; |
| 121 | return 0; |
| 122 | } |
| 123 | } |
| 124 | return -EINVAL; |
| 125 | } |
| 126 | |
| 127 | static struct resource *res_pci_find_mem(u_long base, u_long num, |
| 128 | u_long align, int low, struct pcmcia_socket *s) |
| 129 | { |
| 130 | struct resource *res = pcmcia_make_resource(0, num, IORESOURCE_MEM, |
| 131 | dev_name(&s->dev)); |
| 132 | struct pcmcia_align_data data; |
| 133 | unsigned long min; |
| 134 | int ret; |
| 135 | |
| 136 | if (align < 0x20000) |
| 137 | align = 0x20000; |
| 138 | data.mask = align - 1; |
| 139 | data.offset = base & data.mask; |
| 140 | |
| 141 | min = 0; |
| 142 | if (!low) |
| 143 | min = 0x100000UL; |
| 144 | |
| 145 | ret = pci_bus_alloc_resource(s->cb_dev->bus, |
| 146 | res, num, 1, min, 0, |
| 147 | pcmcia_align, &data); |
| 148 | |
| 149 | if (ret != 0) { |
| 150 | kfree(res); |
| 151 | res = NULL; |
| 152 | } |
| 153 | return res; |
| 154 | } |
| 155 | |
| 156 | |
| 157 | static int res_pci_init(struct pcmcia_socket *s) |
| 158 | { |
| 159 | if (!s->cb_dev || (!s->features & SS_CAP_PAGE_REGS)) { |
| 160 | dev_err(&s->dev, "not supported by res_pci\n"); |
| 161 | return -EOPNOTSUPP; |
| 162 | } |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | struct pccard_resource_ops pccard_nonstatic_ops = { |
| 167 | .validate_mem = NULL, |
| 168 | .find_io = res_pci_find_io, |
| 169 | .find_mem = res_pci_find_mem, |
| 170 | .init = res_pci_init, |
| 171 | .exit = NULL, |
| 172 | }; |
| 173 | EXPORT_SYMBOL(pccard_nonstatic_ops); |