Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * PCMCIA 16-bit resource management functions |
| 3 | * |
| 4 | * The initial developer of the original code is David A. Hinds |
| 5 | * <dahinds@users.sourceforge.net>. Portions created by David A. Hinds |
| 6 | * are Copyright (C) 1999 David A. Hinds. All Rights Reserved. |
| 7 | * |
| 8 | * Copyright (C) 1999 David A. Hinds |
| 9 | * Copyright (C) 2004-2005 Dominik Brodowski |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License version 2 as |
| 13 | * published by the Free Software Foundation. |
| 14 | * |
| 15 | */ |
| 16 | |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 17 | #include <linux/module.h> |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/delay.h> |
| 21 | #include <linux/pci.h> |
| 22 | #include <linux/device.h> |
Dominik Brodowski | 9128422 | 2009-10-18 23:32:33 +0200 | [diff] [blame] | 23 | #include <linux/netdevice.h> |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 24 | |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 25 | #include <pcmcia/cs_types.h> |
| 26 | #include <pcmcia/ss.h> |
| 27 | #include <pcmcia/cs.h> |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 28 | #include <pcmcia/cistpl.h> |
| 29 | #include <pcmcia/cisreg.h> |
| 30 | #include <pcmcia/ds.h> |
| 31 | |
| 32 | #include "cs_internal.h" |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 33 | |
| 34 | |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 35 | /* Access speed for IO windows */ |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 36 | static int io_speed; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 37 | module_param(io_speed, int, 0444); |
| 38 | |
| 39 | |
| 40 | #ifdef CONFIG_PCMCIA_PROBE |
Al Viro | 531e5ca | 2005-08-23 22:47:01 +0100 | [diff] [blame] | 41 | #include <asm/irq.h> |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 42 | /* mask of IRQs already reserved by other cards, we should avoid using them */ |
| 43 | static u8 pcmcia_used_irq[NR_IRQS]; |
| 44 | #endif |
| 45 | |
| 46 | |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 47 | /** alloc_io_space |
| 48 | * |
| 49 | * Special stuff for managing IO windows, because they are scarce |
| 50 | */ |
| 51 | |
Olof Johansson | ecb8a84 | 2008-02-04 22:27:34 -0800 | [diff] [blame] | 52 | static int alloc_io_space(struct pcmcia_socket *s, u_int attr, |
| 53 | unsigned int *base, unsigned int num, u_int lines) |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 54 | { |
| 55 | int i; |
Olof Johansson | ecb8a84 | 2008-02-04 22:27:34 -0800 | [diff] [blame] | 56 | unsigned int try, align; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 57 | |
| 58 | align = (*base) ? (lines ? 1<<lines : 0) : 1; |
| 59 | if (align && (align < num)) { |
| 60 | if (*base) { |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 61 | dev_dbg(&s->dev, "odd IO request: num %#x align %#x\n", |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 62 | num, align); |
| 63 | align = 0; |
| 64 | } else |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 65 | while (align && (align < num)) |
| 66 | align <<= 1; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 67 | } |
| 68 | if (*base & ~(align-1)) { |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 69 | dev_dbg(&s->dev, "odd IO request: base %#x align %#x\n", |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 70 | *base, align); |
| 71 | align = 0; |
| 72 | } |
| 73 | if ((s->features & SS_CAP_STATIC_MAP) && s->io_offset) { |
| 74 | *base = s->io_offset | (*base & 0x0fff); |
| 75 | return 0; |
| 76 | } |
| 77 | /* Check for an already-allocated window that must conflict with |
| 78 | * what was asked for. It is a hack because it does not catch all |
| 79 | * potential conflicts, just the most obvious ones. |
| 80 | */ |
| 81 | for (i = 0; i < MAX_IO_WIN; i++) |
Kaustav Majumdar | 4708b5f | 2006-10-20 14:44:09 -0700 | [diff] [blame] | 82 | if ((s->io[i].res) && *base && |
Dominik Brodowski | c7d0069 | 2006-01-15 08:04:43 +0100 | [diff] [blame] | 83 | ((s->io[i].res->start & (align-1)) == *base)) |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 84 | return 1; |
| 85 | for (i = 0; i < MAX_IO_WIN; i++) { |
Dominik Brodowski | c7d0069 | 2006-01-15 08:04:43 +0100 | [diff] [blame] | 86 | if (!s->io[i].res) { |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 87 | s->io[i].res = pcmcia_find_io_region(*base, num, align, s); |
| 88 | if (s->io[i].res) { |
Dominik Brodowski | c7d0069 | 2006-01-15 08:04:43 +0100 | [diff] [blame] | 89 | *base = s->io[i].res->start; |
| 90 | s->io[i].res->flags = (s->io[i].res->flags & ~IORESOURCE_BITS) | (attr & IORESOURCE_BITS); |
| 91 | s->io[i].InUse = num; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 92 | break; |
| 93 | } else |
| 94 | return 1; |
Dominik Brodowski | c7d0069 | 2006-01-15 08:04:43 +0100 | [diff] [blame] | 95 | } else if ((s->io[i].res->flags & IORESOURCE_BITS) != (attr & IORESOURCE_BITS)) |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 96 | continue; |
| 97 | /* Try to extend top of window */ |
Dominik Brodowski | c7d0069 | 2006-01-15 08:04:43 +0100 | [diff] [blame] | 98 | try = s->io[i].res->end + 1; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 99 | if ((*base == 0) || (*base == try)) |
| 100 | if (pcmcia_adjust_io_region(s->io[i].res, s->io[i].res->start, |
| 101 | s->io[i].res->end + num, s) == 0) { |
| 102 | *base = try; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 103 | s->io[i].InUse += num; |
| 104 | break; |
| 105 | } |
| 106 | /* Try to extend bottom of window */ |
Dominik Brodowski | c7d0069 | 2006-01-15 08:04:43 +0100 | [diff] [blame] | 107 | try = s->io[i].res->start - num; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 108 | if ((*base == 0) || (*base == try)) |
| 109 | if (pcmcia_adjust_io_region(s->io[i].res, s->io[i].res->start - num, |
| 110 | s->io[i].res->end, s) == 0) { |
Dominik Brodowski | c7d0069 | 2006-01-15 08:04:43 +0100 | [diff] [blame] | 111 | *base = try; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 112 | s->io[i].InUse += num; |
| 113 | break; |
| 114 | } |
| 115 | } |
| 116 | return (i == MAX_IO_WIN); |
| 117 | } /* alloc_io_space */ |
| 118 | |
| 119 | |
Olof Johansson | ecb8a84 | 2008-02-04 22:27:34 -0800 | [diff] [blame] | 120 | static void release_io_space(struct pcmcia_socket *s, unsigned int base, |
| 121 | unsigned int num) |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 122 | { |
| 123 | int i; |
| 124 | |
| 125 | for (i = 0; i < MAX_IO_WIN; i++) { |
Dominik Brodowski | c7d0069 | 2006-01-15 08:04:43 +0100 | [diff] [blame] | 126 | if (!s->io[i].res) |
| 127 | continue; |
| 128 | if ((s->io[i].res->start <= base) && |
| 129 | (s->io[i].res->end >= base+num-1)) { |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 130 | s->io[i].InUse -= num; |
| 131 | /* Free the window if no one else is using it */ |
| 132 | if (s->io[i].InUse == 0) { |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 133 | release_resource(s->io[i].res); |
| 134 | kfree(s->io[i].res); |
| 135 | s->io[i].res = NULL; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | } /* release_io_space */ |
| 140 | |
| 141 | |
| 142 | /** pccard_access_configuration_register |
| 143 | * |
| 144 | * Access_configuration_register() reads and writes configuration |
| 145 | * registers in attribute memory. Memory window 0 is reserved for |
| 146 | * this and the tuple reading services. |
| 147 | */ |
| 148 | |
Dominik Brodowski | 855cdf1 | 2006-01-10 20:48:59 +0100 | [diff] [blame] | 149 | int pcmcia_access_configuration_register(struct pcmcia_device *p_dev, |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 150 | conf_reg_t *reg) |
| 151 | { |
Dominik Brodowski | 855cdf1 | 2006-01-10 20:48:59 +0100 | [diff] [blame] | 152 | struct pcmcia_socket *s; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 153 | config_t *c; |
| 154 | int addr; |
| 155 | u_char val; |
| 156 | |
Dominik Brodowski | 855cdf1 | 2006-01-10 20:48:59 +0100 | [diff] [blame] | 157 | if (!p_dev || !p_dev->function_config) |
Dominik Brodowski | 3939c1ef | 2008-08-03 11:10:56 +0200 | [diff] [blame] | 158 | return -EINVAL; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 159 | |
Dominik Brodowski | 855cdf1 | 2006-01-10 20:48:59 +0100 | [diff] [blame] | 160 | s = p_dev->socket; |
| 161 | c = p_dev->function_config; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 162 | |
Dominik Brodowski | 6d9a299 | 2009-10-24 12:20:18 +0200 | [diff] [blame] | 163 | if (!(c->state & CONFIG_LOCKED)) { |
| 164 | dev_dbg(&s->dev, "Configuration isnt't locked\n"); |
Dominik Brodowski | 943f70f | 2008-08-03 11:23:42 +0200 | [diff] [blame] | 165 | return -EACCES; |
Dominik Brodowski | 6d9a299 | 2009-10-24 12:20:18 +0200 | [diff] [blame] | 166 | } |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 167 | |
| 168 | addr = (c->ConfigBase + reg->Offset) >> 1; |
| 169 | |
| 170 | switch (reg->Action) { |
| 171 | case CS_READ: |
| 172 | pcmcia_read_cis_mem(s, 1, addr, 1, &val); |
| 173 | reg->Value = val; |
| 174 | break; |
| 175 | case CS_WRITE: |
| 176 | val = reg->Value; |
| 177 | pcmcia_write_cis_mem(s, 1, addr, 1, &val); |
| 178 | break; |
| 179 | default: |
Dominik Brodowski | 6d9a299 | 2009-10-24 12:20:18 +0200 | [diff] [blame] | 180 | dev_dbg(&s->dev, "Invalid conf register request\n"); |
Dominik Brodowski | 926c540 | 2008-08-03 12:15:11 +0200 | [diff] [blame] | 181 | return -EINVAL; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 182 | break; |
| 183 | } |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 184 | return 0; |
Dominik Brodowski | 855cdf1 | 2006-01-10 20:48:59 +0100 | [diff] [blame] | 185 | } /* pcmcia_access_configuration_register */ |
Dominik Brodowski | 3448139 | 2005-06-27 16:28:54 -0700 | [diff] [blame] | 186 | EXPORT_SYMBOL(pcmcia_access_configuration_register); |
| 187 | |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 188 | |
Magnus Damm | 868575d | 2006-12-13 19:46:43 +0900 | [diff] [blame] | 189 | int pcmcia_map_mem_page(struct pcmcia_device *p_dev, window_handle_t wh, |
| 190 | memreq_t *req) |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 191 | { |
Magnus Damm | 0bdf9b3 | 2006-12-13 19:46:53 +0900 | [diff] [blame] | 192 | struct pcmcia_socket *s = p_dev->socket; |
Magnus Damm | 868575d | 2006-12-13 19:46:43 +0900 | [diff] [blame] | 193 | |
Magnus Damm | 0bdf9b3 | 2006-12-13 19:46:53 +0900 | [diff] [blame] | 194 | wh--; |
| 195 | if (wh >= MAX_WIN) |
Dominik Brodowski | ffb8da2 | 2008-08-03 11:18:00 +0200 | [diff] [blame] | 196 | return -EINVAL; |
Dominik Brodowski | 610e237 | 2008-08-03 11:58:53 +0200 | [diff] [blame] | 197 | if (req->Page != 0) { |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 198 | dev_dbg(&s->dev, "failure: requested page is zero\n"); |
Dominik Brodowski | 610e237 | 2008-08-03 11:58:53 +0200 | [diff] [blame] | 199 | return -EINVAL; |
| 200 | } |
Dominik Brodowski | 82f88e3 | 2009-11-03 01:16:12 +0100 | [diff] [blame] | 201 | s->win[wh].card_start = req->CardOffset; |
| 202 | if (s->ops->set_mem_map(s, &s->win[wh]) != 0) { |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 203 | dev_dbg(&s->dev, "failed to set_mem_map\n"); |
Dominik Brodowski | 69ba443 | 2008-08-03 12:10:53 +0200 | [diff] [blame] | 204 | return -EIO; |
| 205 | } |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 206 | return 0; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 207 | } /* pcmcia_map_mem_page */ |
| 208 | EXPORT_SYMBOL(pcmcia_map_mem_page); |
| 209 | |
| 210 | |
| 211 | /** pcmcia_modify_configuration |
| 212 | * |
| 213 | * Modify a locked socket configuration |
| 214 | */ |
Dominik Brodowski | 2bc5a9b | 2005-07-07 17:59:02 -0700 | [diff] [blame] | 215 | int pcmcia_modify_configuration(struct pcmcia_device *p_dev, |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 216 | modconf_t *mod) |
| 217 | { |
| 218 | struct pcmcia_socket *s; |
| 219 | config_t *c; |
| 220 | |
Dominik Brodowski | 2bc5a9b | 2005-07-07 17:59:02 -0700 | [diff] [blame] | 221 | s = p_dev->socket; |
Dominik Brodowski | dbb22f0 | 2006-01-10 20:41:27 +0100 | [diff] [blame] | 222 | c = p_dev->function_config; |
| 223 | |
Dominik Brodowski | 6d9a299 | 2009-10-24 12:20:18 +0200 | [diff] [blame] | 224 | if (!(s->state & SOCKET_PRESENT)) { |
| 225 | dev_dbg(&s->dev, "No card present\n"); |
Dominik Brodowski | 3939c1ef | 2008-08-03 11:10:56 +0200 | [diff] [blame] | 226 | return -ENODEV; |
Dominik Brodowski | 6d9a299 | 2009-10-24 12:20:18 +0200 | [diff] [blame] | 227 | } |
| 228 | if (!(c->state & CONFIG_LOCKED)) { |
| 229 | dev_dbg(&s->dev, "Configuration isnt't locked\n"); |
Dominik Brodowski | 943f70f | 2008-08-03 11:23:42 +0200 | [diff] [blame] | 230 | return -EACCES; |
Dominik Brodowski | 6d9a299 | 2009-10-24 12:20:18 +0200 | [diff] [blame] | 231 | } |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 232 | |
| 233 | if (mod->Attributes & CONF_IRQ_CHANGE_VALID) { |
| 234 | if (mod->Attributes & CONF_ENABLE_IRQ) { |
| 235 | c->Attributes |= CONF_ENABLE_IRQ; |
| 236 | s->socket.io_irq = s->irq.AssignedIRQ; |
| 237 | } else { |
| 238 | c->Attributes &= ~CONF_ENABLE_IRQ; |
| 239 | s->socket.io_irq = 0; |
| 240 | } |
| 241 | s->ops->set_socket(s, &s->socket); |
| 242 | } |
| 243 | |
Dominik Brodowski | d8b0a49 | 2008-08-03 10:56:13 +0200 | [diff] [blame] | 244 | if (mod->Attributes & CONF_VCC_CHANGE_VALID) { |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 245 | dev_dbg(&s->dev, "changing Vcc is not allowed at this time\n"); |
Dominik Brodowski | d8b0a49 | 2008-08-03 10:56:13 +0200 | [diff] [blame] | 246 | return -EINVAL; |
| 247 | } |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 248 | |
| 249 | /* We only allow changing Vpp1 and Vpp2 to the same value */ |
| 250 | if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) && |
| 251 | (mod->Attributes & CONF_VPP2_CHANGE_VALID)) { |
Ilpo Järvinen | 60df3de8 | 2008-10-30 13:02:54 +0200 | [diff] [blame] | 252 | if (mod->Vpp1 != mod->Vpp2) { |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 253 | dev_dbg(&s->dev, "Vpp1 and Vpp2 must be the same\n"); |
Dominik Brodowski | d8b0a49 | 2008-08-03 10:56:13 +0200 | [diff] [blame] | 254 | return -EINVAL; |
Ilpo Järvinen | 60df3de8 | 2008-10-30 13:02:54 +0200 | [diff] [blame] | 255 | } |
Dominik Brodowski | 71ed90d | 2005-09-09 13:03:27 -0700 | [diff] [blame] | 256 | s->socket.Vpp = mod->Vpp1; |
Dominik Brodowski | d8b0a49 | 2008-08-03 10:56:13 +0200 | [diff] [blame] | 257 | if (s->ops->set_socket(s, &s->socket)) { |
| 258 | dev_printk(KERN_WARNING, &s->dev, |
| 259 | "Unable to set VPP\n"); |
| 260 | return -EIO; |
| 261 | } |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 262 | } else if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) || |
Dominik Brodowski | d8b0a49 | 2008-08-03 10:56:13 +0200 | [diff] [blame] | 263 | (mod->Attributes & CONF_VPP2_CHANGE_VALID)) { |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 264 | dev_dbg(&s->dev, "changing Vcc is not allowed at this time\n"); |
Dominik Brodowski | d8b0a49 | 2008-08-03 10:56:13 +0200 | [diff] [blame] | 265 | return -EINVAL; |
| 266 | } |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 267 | |
Dominik Brodowski | 4bbed52 | 2006-01-15 11:18:12 +0100 | [diff] [blame] | 268 | if (mod->Attributes & CONF_IO_CHANGE_WIDTH) { |
| 269 | pccard_io_map io_off = { 0, 0, 0, 0, 1 }; |
| 270 | pccard_io_map io_on; |
| 271 | int i; |
| 272 | |
| 273 | io_on.speed = io_speed; |
| 274 | for (i = 0; i < MAX_IO_WIN; i++) { |
| 275 | if (!s->io[i].res) |
| 276 | continue; |
| 277 | io_off.map = i; |
| 278 | io_on.map = i; |
| 279 | |
| 280 | io_on.flags = MAP_ACTIVE | IO_DATA_PATH_WIDTH_8; |
| 281 | io_on.start = s->io[i].res->start; |
| 282 | io_on.stop = s->io[i].res->end; |
| 283 | |
| 284 | s->ops->set_io_map(s, &io_off); |
| 285 | mdelay(40); |
| 286 | s->ops->set_io_map(s, &io_on); |
| 287 | } |
| 288 | } |
| 289 | |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 290 | return 0; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 291 | } /* modify_configuration */ |
| 292 | EXPORT_SYMBOL(pcmcia_modify_configuration); |
| 293 | |
| 294 | |
Dominik Brodowski | 2bc5a9b | 2005-07-07 17:59:02 -0700 | [diff] [blame] | 295 | int pcmcia_release_configuration(struct pcmcia_device *p_dev) |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 296 | { |
| 297 | pccard_io_map io = { 0, 0, 0, 0, 1 }; |
Dominik Brodowski | 2bc5a9b | 2005-07-07 17:59:02 -0700 | [diff] [blame] | 298 | struct pcmcia_socket *s = p_dev->socket; |
Dominik Brodowski | 5f2a71f | 2006-01-15 09:32:39 +0100 | [diff] [blame] | 299 | config_t *c = p_dev->function_config; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 300 | int i; |
| 301 | |
Dominik Brodowski | e2d4096 | 2006-03-02 00:09:29 +0100 | [diff] [blame] | 302 | if (p_dev->_locked) { |
| 303 | p_dev->_locked = 0; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 304 | if (--(s->lock_count) == 0) { |
| 305 | s->socket.flags = SS_OUTPUT_ENA; /* Is this correct? */ |
| 306 | s->socket.Vpp = 0; |
| 307 | s->socket.io_irq = 0; |
| 308 | s->ops->set_socket(s, &s->socket); |
| 309 | } |
Dominik Brodowski | 5f2a71f | 2006-01-15 09:32:39 +0100 | [diff] [blame] | 310 | } |
| 311 | if (c->state & CONFIG_LOCKED) { |
| 312 | c->state &= ~CONFIG_LOCKED; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 313 | if (c->state & CONFIG_IO_REQ) |
| 314 | for (i = 0; i < MAX_IO_WIN; i++) { |
Dominik Brodowski | c7d0069 | 2006-01-15 08:04:43 +0100 | [diff] [blame] | 315 | if (!s->io[i].res) |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 316 | continue; |
| 317 | s->io[i].Config--; |
| 318 | if (s->io[i].Config != 0) |
| 319 | continue; |
| 320 | io.map = i; |
| 321 | s->ops->set_io_map(s, &io); |
| 322 | } |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 325 | return 0; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 326 | } /* pcmcia_release_configuration */ |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 327 | |
| 328 | |
| 329 | /** pcmcia_release_io |
| 330 | * |
| 331 | * Release_io() releases the I/O ranges allocated by a client. This |
| 332 | * may be invoked some time after a card ejection has already dumped |
| 333 | * the actual socket configuration, so if the client is "stale", we |
| 334 | * don't bother checking the port ranges against the current socket |
| 335 | * values. |
| 336 | */ |
Adrian Bunk | b4c8840 | 2006-01-18 23:53:13 -0800 | [diff] [blame] | 337 | static int pcmcia_release_io(struct pcmcia_device *p_dev, io_req_t *req) |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 338 | { |
Dominik Brodowski | 2bc5a9b | 2005-07-07 17:59:02 -0700 | [diff] [blame] | 339 | struct pcmcia_socket *s = p_dev->socket; |
Dominik Brodowski | 5f2a71f | 2006-01-15 09:32:39 +0100 | [diff] [blame] | 340 | config_t *c = p_dev->function_config; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 341 | |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 342 | if (!p_dev->_io) |
Dominik Brodowski | ffb8da2 | 2008-08-03 11:18:00 +0200 | [diff] [blame] | 343 | return -EINVAL; |
Dominik Brodowski | 5f2a71f | 2006-01-15 09:32:39 +0100 | [diff] [blame] | 344 | |
Dominik Brodowski | e2d4096 | 2006-03-02 00:09:29 +0100 | [diff] [blame] | 345 | p_dev->_io = 0; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 346 | |
Dominik Brodowski | 5f2a71f | 2006-01-15 09:32:39 +0100 | [diff] [blame] | 347 | if ((c->io.BasePort1 != req->BasePort1) || |
| 348 | (c->io.NumPorts1 != req->NumPorts1) || |
| 349 | (c->io.BasePort2 != req->BasePort2) || |
| 350 | (c->io.NumPorts2 != req->NumPorts2)) |
Dominik Brodowski | 926c540 | 2008-08-03 12:15:11 +0200 | [diff] [blame] | 351 | return -EINVAL; |
Dominik Brodowski | 5f2a71f | 2006-01-15 09:32:39 +0100 | [diff] [blame] | 352 | |
| 353 | c->state &= ~CONFIG_IO_REQ; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 354 | |
| 355 | release_io_space(s, req->BasePort1, req->NumPorts1); |
| 356 | if (req->NumPorts2) |
| 357 | release_io_space(s, req->BasePort2, req->NumPorts2); |
| 358 | |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 359 | return 0; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 360 | } /* pcmcia_release_io */ |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 361 | |
| 362 | |
Adrian Bunk | b4c8840 | 2006-01-18 23:53:13 -0800 | [diff] [blame] | 363 | static int pcmcia_release_irq(struct pcmcia_device *p_dev, irq_req_t *req) |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 364 | { |
Dominik Brodowski | 2bc5a9b | 2005-07-07 17:59:02 -0700 | [diff] [blame] | 365 | struct pcmcia_socket *s = p_dev->socket; |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 366 | config_t *c = p_dev->function_config; |
Dominik Brodowski | 5f2a71f | 2006-01-15 09:32:39 +0100 | [diff] [blame] | 367 | |
Dominik Brodowski | e2d4096 | 2006-03-02 00:09:29 +0100 | [diff] [blame] | 368 | if (!p_dev->_irq) |
Dominik Brodowski | ffb8da2 | 2008-08-03 11:18:00 +0200 | [diff] [blame] | 369 | return -EINVAL; |
Dominik Brodowski | e2d4096 | 2006-03-02 00:09:29 +0100 | [diff] [blame] | 370 | p_dev->_irq = 0; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 371 | |
Dominik Brodowski | 5f2a71f | 2006-01-15 09:32:39 +0100 | [diff] [blame] | 372 | if (c->state & CONFIG_LOCKED) |
Dominik Brodowski | 943f70f | 2008-08-03 11:23:42 +0200 | [diff] [blame] | 373 | return -EACCES; |
Dominik Brodowski | 610e237 | 2008-08-03 11:58:53 +0200 | [diff] [blame] | 374 | if (c->irq.Attributes != req->Attributes) { |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 375 | dev_dbg(&s->dev, "IRQ attributes must match assigned ones\n"); |
Dominik Brodowski | 610e237 | 2008-08-03 11:58:53 +0200 | [diff] [blame] | 376 | return -EINVAL; |
| 377 | } |
Dominik Brodowski | 69ba443 | 2008-08-03 12:10:53 +0200 | [diff] [blame] | 378 | if (s->irq.AssignedIRQ != req->AssignedIRQ) { |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 379 | dev_dbg(&s->dev, "IRQ must match assigned one\n"); |
Dominik Brodowski | 69ba443 | 2008-08-03 12:10:53 +0200 | [diff] [blame] | 380 | return -EINVAL; |
| 381 | } |
Dominik Brodowski | 5f2a71f | 2006-01-15 09:32:39 +0100 | [diff] [blame] | 382 | if (--s->irq.Config == 0) { |
| 383 | c->state &= ~CONFIG_IRQ_REQ; |
| 384 | s->irq.AssignedIRQ = 0; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 387 | if (req->Handler) |
Dominik Brodowski | 5fa9167 | 2009-11-08 17:24:46 +0100 | [diff] [blame] | 388 | free_irq(req->AssignedIRQ, p_dev->priv); |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 389 | |
| 390 | #ifdef CONFIG_PCMCIA_PROBE |
| 391 | pcmcia_used_irq[req->AssignedIRQ]--; |
| 392 | #endif |
| 393 | |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 394 | return 0; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 395 | } /* pcmcia_release_irq */ |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 396 | |
| 397 | |
Magnus Damm | f5560da | 2006-12-13 19:46:38 +0900 | [diff] [blame] | 398 | int pcmcia_release_window(struct pcmcia_device *p_dev, window_handle_t wh) |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 399 | { |
Magnus Damm | 0bdf9b3 | 2006-12-13 19:46:53 +0900 | [diff] [blame] | 400 | struct pcmcia_socket *s = p_dev->socket; |
Dominik Brodowski | 82f88e3 | 2009-11-03 01:16:12 +0100 | [diff] [blame] | 401 | pccard_mem_map *win; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 402 | |
Magnus Damm | 0bdf9b3 | 2006-12-13 19:46:53 +0900 | [diff] [blame] | 403 | wh--; |
| 404 | if (wh >= MAX_WIN) |
Dominik Brodowski | ffb8da2 | 2008-08-03 11:18:00 +0200 | [diff] [blame] | 405 | return -EINVAL; |
Magnus Damm | 0bdf9b3 | 2006-12-13 19:46:53 +0900 | [diff] [blame] | 406 | |
| 407 | win = &s->win[wh]; |
| 408 | |
| 409 | if (!(p_dev->_win & CLIENT_WIN_REQ(wh))) { |
Dominik Brodowski | 6d9a299 | 2009-10-24 12:20:18 +0200 | [diff] [blame] | 410 | dev_dbg(&s->dev, "not releasing unknown window\n"); |
Dominik Brodowski | ffb8da2 | 2008-08-03 11:18:00 +0200 | [diff] [blame] | 411 | return -EINVAL; |
Dominik Brodowski | 6d9a299 | 2009-10-24 12:20:18 +0200 | [diff] [blame] | 412 | } |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 413 | |
| 414 | /* Shut down memory window */ |
Dominik Brodowski | 82f88e3 | 2009-11-03 01:16:12 +0100 | [diff] [blame] | 415 | win->flags &= ~MAP_ACTIVE; |
| 416 | s->ops->set_mem_map(s, win); |
Magnus Damm | 0bdf9b3 | 2006-12-13 19:46:53 +0900 | [diff] [blame] | 417 | s->state &= ~SOCKET_WIN_REQ(wh); |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 418 | |
| 419 | /* Release system memory */ |
Dominik Brodowski | 82f88e3 | 2009-11-03 01:16:12 +0100 | [diff] [blame] | 420 | if (win->res) { |
| 421 | release_resource(win->res); |
| 422 | kfree(win->res); |
| 423 | win->res = NULL; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 424 | } |
Magnus Damm | 0bdf9b3 | 2006-12-13 19:46:53 +0900 | [diff] [blame] | 425 | p_dev->_win &= ~CLIENT_WIN_REQ(wh); |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 426 | |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 427 | return 0; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 428 | } /* pcmcia_release_window */ |
| 429 | EXPORT_SYMBOL(pcmcia_release_window); |
| 430 | |
| 431 | |
Dominik Brodowski | 2bc5a9b | 2005-07-07 17:59:02 -0700 | [diff] [blame] | 432 | int pcmcia_request_configuration(struct pcmcia_device *p_dev, |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 433 | config_req_t *req) |
| 434 | { |
| 435 | int i; |
| 436 | u_int base; |
Dominik Brodowski | 2bc5a9b | 2005-07-07 17:59:02 -0700 | [diff] [blame] | 437 | struct pcmcia_socket *s = p_dev->socket; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 438 | config_t *c; |
| 439 | pccard_io_map iomap; |
| 440 | |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 441 | if (!(s->state & SOCKET_PRESENT)) |
Joe Perches | d598de0 | 2009-06-28 09:26:41 -0700 | [diff] [blame] | 442 | return -ENODEV; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 443 | |
Dominik Brodowski | de6405e | 2008-08-03 10:47:59 +0200 | [diff] [blame] | 444 | if (req->IntType & INT_CARDBUS) { |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 445 | dev_dbg(&s->dev, "IntType may not be INT_CARDBUS\n"); |
Dominik Brodowski | de6405e | 2008-08-03 10:47:59 +0200 | [diff] [blame] | 446 | return -EINVAL; |
| 447 | } |
Dominik Brodowski | dbb22f0 | 2006-01-10 20:41:27 +0100 | [diff] [blame] | 448 | c = p_dev->function_config; |
Dominik Brodowski | 6d9a299 | 2009-10-24 12:20:18 +0200 | [diff] [blame] | 449 | if (c->state & CONFIG_LOCKED) { |
| 450 | dev_dbg(&s->dev, "Configuration is locked\n"); |
Dominik Brodowski | 943f70f | 2008-08-03 11:23:42 +0200 | [diff] [blame] | 451 | return -EACCES; |
Dominik Brodowski | 6d9a299 | 2009-10-24 12:20:18 +0200 | [diff] [blame] | 452 | } |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 453 | |
| 454 | /* Do power control. We don't allow changes in Vcc. */ |
Dominik Brodowski | 70294b4 | 2006-01-15 12:43:16 +0100 | [diff] [blame] | 455 | s->socket.Vpp = req->Vpp; |
Dominik Brodowski | d8b0a49 | 2008-08-03 10:56:13 +0200 | [diff] [blame] | 456 | if (s->ops->set_socket(s, &s->socket)) { |
| 457 | dev_printk(KERN_WARNING, &s->dev, |
| 458 | "Unable to set socket state\n"); |
| 459 | return -EINVAL; |
| 460 | } |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 461 | |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 462 | /* Pick memory or I/O card, DMA mode, interrupt */ |
| 463 | c->IntType = req->IntType; |
| 464 | c->Attributes = req->Attributes; |
| 465 | if (req->IntType & INT_MEMORY_AND_IO) |
| 466 | s->socket.flags |= SS_IOCARD; |
| 467 | if (req->IntType & INT_ZOOMED_VIDEO) |
| 468 | s->socket.flags |= SS_ZVCARD | SS_IOCARD; |
| 469 | if (req->Attributes & CONF_ENABLE_DMA) |
| 470 | s->socket.flags |= SS_DMA_MODE; |
| 471 | if (req->Attributes & CONF_ENABLE_SPKR) |
| 472 | s->socket.flags |= SS_SPKR_ENA; |
| 473 | if (req->Attributes & CONF_ENABLE_IRQ) |
| 474 | s->socket.io_irq = s->irq.AssignedIRQ; |
| 475 | else |
| 476 | s->socket.io_irq = 0; |
| 477 | s->ops->set_socket(s, &s->socket); |
| 478 | s->lock_count++; |
| 479 | |
| 480 | /* Set up CIS configuration registers */ |
| 481 | base = c->ConfigBase = req->ConfigBase; |
Dominik Brodowski | 1ae9c7d | 2006-01-10 20:40:40 +0100 | [diff] [blame] | 482 | c->CardValues = req->Present; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 483 | if (req->Present & PRESENT_COPY) { |
| 484 | c->Copy = req->Copy; |
| 485 | pcmcia_write_cis_mem(s, 1, (base + CISREG_SCR)>>1, 1, &c->Copy); |
| 486 | } |
| 487 | if (req->Present & PRESENT_OPTION) { |
| 488 | if (s->functions == 1) { |
| 489 | c->Option = req->ConfigIndex & COR_CONFIG_MASK; |
| 490 | } else { |
| 491 | c->Option = req->ConfigIndex & COR_MFC_CONFIG_MASK; |
| 492 | c->Option |= COR_FUNC_ENA|COR_IREQ_ENA; |
| 493 | if (req->Present & PRESENT_IOBASE_0) |
| 494 | c->Option |= COR_ADDR_DECODE; |
| 495 | } |
| 496 | if (c->state & CONFIG_IRQ_REQ) |
| 497 | if (!(c->irq.Attributes & IRQ_FORCED_PULSE)) |
| 498 | c->Option |= COR_LEVEL_REQ; |
| 499 | pcmcia_write_cis_mem(s, 1, (base + CISREG_COR)>>1, 1, &c->Option); |
| 500 | mdelay(40); |
| 501 | } |
| 502 | if (req->Present & PRESENT_STATUS) { |
| 503 | c->Status = req->Status; |
| 504 | pcmcia_write_cis_mem(s, 1, (base + CISREG_CCSR)>>1, 1, &c->Status); |
| 505 | } |
| 506 | if (req->Present & PRESENT_PIN_REPLACE) { |
| 507 | c->Pin = req->Pin; |
| 508 | pcmcia_write_cis_mem(s, 1, (base + CISREG_PRR)>>1, 1, &c->Pin); |
| 509 | } |
| 510 | if (req->Present & PRESENT_EXT_STATUS) { |
| 511 | c->ExtStatus = req->ExtStatus; |
| 512 | pcmcia_write_cis_mem(s, 1, (base + CISREG_ESR)>>1, 1, &c->ExtStatus); |
| 513 | } |
| 514 | if (req->Present & PRESENT_IOBASE_0) { |
| 515 | u_char b = c->io.BasePort1 & 0xff; |
| 516 | pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_0)>>1, 1, &b); |
| 517 | b = (c->io.BasePort1 >> 8) & 0xff; |
| 518 | pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_1)>>1, 1, &b); |
| 519 | } |
| 520 | if (req->Present & PRESENT_IOSIZE) { |
| 521 | u_char b = c->io.NumPorts1 + c->io.NumPorts2 - 1; |
| 522 | pcmcia_write_cis_mem(s, 1, (base + CISREG_IOSIZE)>>1, 1, &b); |
| 523 | } |
| 524 | |
| 525 | /* Configure I/O windows */ |
| 526 | if (c->state & CONFIG_IO_REQ) { |
| 527 | iomap.speed = io_speed; |
| 528 | for (i = 0; i < MAX_IO_WIN; i++) |
Dominik Brodowski | c7d0069 | 2006-01-15 08:04:43 +0100 | [diff] [blame] | 529 | if (s->io[i].res) { |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 530 | iomap.map = i; |
| 531 | iomap.flags = MAP_ACTIVE; |
Dominik Brodowski | c7d0069 | 2006-01-15 08:04:43 +0100 | [diff] [blame] | 532 | switch (s->io[i].res->flags & IO_DATA_PATH_WIDTH) { |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 533 | case IO_DATA_PATH_WIDTH_16: |
| 534 | iomap.flags |= MAP_16BIT; break; |
| 535 | case IO_DATA_PATH_WIDTH_AUTO: |
| 536 | iomap.flags |= MAP_AUTOSZ; break; |
| 537 | default: |
| 538 | break; |
| 539 | } |
Dominik Brodowski | c7d0069 | 2006-01-15 08:04:43 +0100 | [diff] [blame] | 540 | iomap.start = s->io[i].res->start; |
| 541 | iomap.stop = s->io[i].res->end; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 542 | s->ops->set_io_map(s, &iomap); |
| 543 | s->io[i].Config++; |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | c->state |= CONFIG_LOCKED; |
Dominik Brodowski | e2d4096 | 2006-03-02 00:09:29 +0100 | [diff] [blame] | 548 | p_dev->_locked = 1; |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 549 | return 0; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 550 | } /* pcmcia_request_configuration */ |
| 551 | EXPORT_SYMBOL(pcmcia_request_configuration); |
| 552 | |
| 553 | |
| 554 | /** pcmcia_request_io |
| 555 | * |
| 556 | * Request_io() reserves ranges of port addresses for a socket. |
| 557 | * I have not implemented range sharing or alias addressing. |
| 558 | */ |
Dominik Brodowski | 2bc5a9b | 2005-07-07 17:59:02 -0700 | [diff] [blame] | 559 | int pcmcia_request_io(struct pcmcia_device *p_dev, io_req_t *req) |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 560 | { |
Dominik Brodowski | 2bc5a9b | 2005-07-07 17:59:02 -0700 | [diff] [blame] | 561 | struct pcmcia_socket *s = p_dev->socket; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 562 | config_t *c; |
| 563 | |
Dominik Brodowski | 6d9a299 | 2009-10-24 12:20:18 +0200 | [diff] [blame] | 564 | if (!(s->state & SOCKET_PRESENT)) { |
| 565 | dev_dbg(&s->dev, "No card present\n"); |
Dominik Brodowski | 3939c1ef | 2008-08-03 11:10:56 +0200 | [diff] [blame] | 566 | return -ENODEV; |
Dominik Brodowski | 6d9a299 | 2009-10-24 12:20:18 +0200 | [diff] [blame] | 567 | } |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 568 | |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 569 | if (!req) |
Dominik Brodowski | de6405e | 2008-08-03 10:47:59 +0200 | [diff] [blame] | 570 | return -EINVAL; |
Dominik Brodowski | dbb22f0 | 2006-01-10 20:41:27 +0100 | [diff] [blame] | 571 | c = p_dev->function_config; |
Dominik Brodowski | 6d9a299 | 2009-10-24 12:20:18 +0200 | [diff] [blame] | 572 | if (c->state & CONFIG_LOCKED) { |
| 573 | dev_dbg(&s->dev, "Configuration is locked\n"); |
Dominik Brodowski | 943f70f | 2008-08-03 11:23:42 +0200 | [diff] [blame] | 574 | return -EACCES; |
Dominik Brodowski | 6d9a299 | 2009-10-24 12:20:18 +0200 | [diff] [blame] | 575 | } |
Dominik Brodowski | f958095 | 2008-08-03 11:40:19 +0200 | [diff] [blame] | 576 | if (c->state & CONFIG_IO_REQ) { |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 577 | dev_dbg(&s->dev, "IO already configured\n"); |
Dominik Brodowski | f958095 | 2008-08-03 11:40:19 +0200 | [diff] [blame] | 578 | return -EBUSY; |
| 579 | } |
Dominik Brodowski | 610e237 | 2008-08-03 11:58:53 +0200 | [diff] [blame] | 580 | if (req->Attributes1 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS)) { |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 581 | dev_dbg(&s->dev, "bad attribute setting for IO region 1\n"); |
Dominik Brodowski | 610e237 | 2008-08-03 11:58:53 +0200 | [diff] [blame] | 582 | return -EINVAL; |
| 583 | } |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 584 | if ((req->NumPorts2 > 0) && |
Dominik Brodowski | 610e237 | 2008-08-03 11:58:53 +0200 | [diff] [blame] | 585 | (req->Attributes2 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS))) { |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 586 | dev_dbg(&s->dev, "bad attribute setting for IO region 2\n"); |
Dominik Brodowski | 610e237 | 2008-08-03 11:58:53 +0200 | [diff] [blame] | 587 | return -EINVAL; |
| 588 | } |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 589 | |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 590 | dev_dbg(&s->dev, "trying to allocate resource 1\n"); |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 591 | if (alloc_io_space(s, req->Attributes1, &req->BasePort1, |
Dominik Brodowski | f958095 | 2008-08-03 11:40:19 +0200 | [diff] [blame] | 592 | req->NumPorts1, req->IOAddrLines)) { |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 593 | dev_dbg(&s->dev, "allocation of resource 1 failed\n"); |
Dominik Brodowski | f958095 | 2008-08-03 11:40:19 +0200 | [diff] [blame] | 594 | return -EBUSY; |
| 595 | } |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 596 | |
| 597 | if (req->NumPorts2) { |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 598 | dev_dbg(&s->dev, "trying to allocate resource 2\n"); |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 599 | if (alloc_io_space(s, req->Attributes2, &req->BasePort2, |
| 600 | req->NumPorts2, req->IOAddrLines)) { |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 601 | dev_dbg(&s->dev, "allocation of resource 2 failed\n"); |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 602 | release_io_space(s, req->BasePort1, req->NumPorts1); |
Dominik Brodowski | f958095 | 2008-08-03 11:40:19 +0200 | [diff] [blame] | 603 | return -EBUSY; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 604 | } |
| 605 | } |
| 606 | |
| 607 | c->io = *req; |
| 608 | c->state |= CONFIG_IO_REQ; |
Dominik Brodowski | e2d4096 | 2006-03-02 00:09:29 +0100 | [diff] [blame] | 609 | p_dev->_io = 1; |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 610 | return 0; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 611 | } /* pcmcia_request_io */ |
| 612 | EXPORT_SYMBOL(pcmcia_request_io); |
| 613 | |
| 614 | |
| 615 | /** pcmcia_request_irq |
| 616 | * |
| 617 | * Request_irq() reserves an irq for this client. |
| 618 | * |
| 619 | * Also, since Linux only reserves irq's when they are actually |
| 620 | * hooked, we don't guarantee that an irq will still be available |
| 621 | * when the configuration is locked. Now that I think about it, |
| 622 | * there might be a way to fix this using a dummy handler. |
| 623 | */ |
| 624 | |
| 625 | #ifdef CONFIG_PCMCIA_PROBE |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 626 | static irqreturn_t test_action(int cpl, void *dev_id) |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 627 | { |
| 628 | return IRQ_NONE; |
| 629 | } |
| 630 | #endif |
| 631 | |
Dominik Brodowski | 2bc5a9b | 2005-07-07 17:59:02 -0700 | [diff] [blame] | 632 | int pcmcia_request_irq(struct pcmcia_device *p_dev, irq_req_t *req) |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 633 | { |
Dominik Brodowski | 2bc5a9b | 2005-07-07 17:59:02 -0700 | [diff] [blame] | 634 | struct pcmcia_socket *s = p_dev->socket; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 635 | config_t *c; |
Dominik Brodowski | f958095 | 2008-08-03 11:40:19 +0200 | [diff] [blame] | 636 | int ret = -EINVAL, irq = 0; |
Alan Cox | c533120 | 2006-05-16 16:16:44 +0100 | [diff] [blame] | 637 | int type; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 638 | |
Dominik Brodowski | 6d9a299 | 2009-10-24 12:20:18 +0200 | [diff] [blame] | 639 | if (!(s->state & SOCKET_PRESENT)) { |
| 640 | dev_dbg(&s->dev, "No card present\n"); |
Dominik Brodowski | 3939c1ef | 2008-08-03 11:10:56 +0200 | [diff] [blame] | 641 | return -ENODEV; |
Dominik Brodowski | 6d9a299 | 2009-10-24 12:20:18 +0200 | [diff] [blame] | 642 | } |
Dominik Brodowski | dbb22f0 | 2006-01-10 20:41:27 +0100 | [diff] [blame] | 643 | c = p_dev->function_config; |
Dominik Brodowski | 6d9a299 | 2009-10-24 12:20:18 +0200 | [diff] [blame] | 644 | if (c->state & CONFIG_LOCKED) { |
| 645 | dev_dbg(&s->dev, "Configuration is locked\n"); |
Dominik Brodowski | 943f70f | 2008-08-03 11:23:42 +0200 | [diff] [blame] | 646 | return -EACCES; |
Dominik Brodowski | 6d9a299 | 2009-10-24 12:20:18 +0200 | [diff] [blame] | 647 | } |
Dominik Brodowski | f958095 | 2008-08-03 11:40:19 +0200 | [diff] [blame] | 648 | if (c->state & CONFIG_IRQ_REQ) { |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 649 | dev_dbg(&s->dev, "IRQ already configured\n"); |
Dominik Brodowski | f958095 | 2008-08-03 11:40:19 +0200 | [diff] [blame] | 650 | return -EBUSY; |
| 651 | } |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 652 | |
Alan Cox | c533120 | 2006-05-16 16:16:44 +0100 | [diff] [blame] | 653 | /* Decide what type of interrupt we are registering */ |
| 654 | type = 0; |
| 655 | if (s->functions > 1) /* All of this ought to be handled higher up */ |
Thomas Gleixner | dace145 | 2006-07-01 19:29:38 -0700 | [diff] [blame] | 656 | type = IRQF_SHARED; |
Alan Cox | 7bbfd39 | 2008-10-05 17:35:59 +0100 | [diff] [blame] | 657 | else if (req->Attributes & IRQ_TYPE_DYNAMIC_SHARING) |
Thomas Gleixner | dace145 | 2006-07-01 19:29:38 -0700 | [diff] [blame] | 658 | type = IRQF_SHARED; |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 659 | else |
| 660 | printk(KERN_WARNING "pcmcia: Driver needs updating to support IRQ sharing.\n"); |
Alan Cox | c533120 | 2006-05-16 16:16:44 +0100 | [diff] [blame] | 661 | |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 662 | #ifdef CONFIG_PCMCIA_PROBE |
Alan Cox | 635416e | 2008-06-16 14:35:15 +0200 | [diff] [blame] | 663 | |
| 664 | #ifdef IRQ_NOAUTOEN |
| 665 | /* if the underlying IRQ infrastructure allows for it, only allocate |
| 666 | * the IRQ, but do not enable it |
| 667 | */ |
Dominik Brodowski | 5fa9167 | 2009-11-08 17:24:46 +0100 | [diff] [blame] | 668 | if (!(req->Handler)) |
Alan Cox | 635416e | 2008-06-16 14:35:15 +0200 | [diff] [blame] | 669 | type |= IRQ_NOAUTOEN; |
| 670 | #endif /* IRQ_NOAUTOEN */ |
| 671 | |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 672 | if (s->irq.AssignedIRQ != 0) { |
| 673 | /* If the interrupt is already assigned, it must be the same */ |
| 674 | irq = s->irq.AssignedIRQ; |
| 675 | } else { |
| 676 | int try; |
| 677 | u32 mask = s->irq_mask; |
Dominik Brodowski | 5fa9167 | 2009-11-08 17:24:46 +0100 | [diff] [blame] | 678 | void *data = p_dev; /* something unique to this device */ |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 679 | |
| 680 | for (try = 0; try < 64; try++) { |
| 681 | irq = try % 32; |
| 682 | |
| 683 | /* marked as available by driver, and not blocked by userspace? */ |
| 684 | if (!((mask >> irq) & 1)) |
| 685 | continue; |
| 686 | |
| 687 | /* avoid an IRQ which is already used by a PCMCIA card */ |
| 688 | if ((try < 32) && pcmcia_used_irq[irq]) |
| 689 | continue; |
| 690 | |
| 691 | /* register the correct driver, if possible, of check whether |
| 692 | * registering a dummy handle works, i.e. if the IRQ isn't |
| 693 | * marked as used by the kernel resource management core */ |
| 694 | ret = request_irq(irq, |
Dominik Brodowski | 5fa9167 | 2009-11-08 17:24:46 +0100 | [diff] [blame] | 695 | (req->Handler) ? req->Handler : test_action, |
Alan Cox | c533120 | 2006-05-16 16:16:44 +0100 | [diff] [blame] | 696 | type, |
Brice Goglin | bd65a68 | 2005-09-09 13:03:29 -0700 | [diff] [blame] | 697 | p_dev->devname, |
Dominik Brodowski | 5fa9167 | 2009-11-08 17:24:46 +0100 | [diff] [blame] | 698 | (req->Handler) ? p_dev->priv : data); |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 699 | if (!ret) { |
Dominik Brodowski | 5fa9167 | 2009-11-08 17:24:46 +0100 | [diff] [blame] | 700 | if (!req->Handler) |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 701 | free_irq(irq, data); |
| 702 | break; |
| 703 | } |
| 704 | } |
| 705 | } |
| 706 | #endif |
Daniel Ritz | c181e0e | 2005-09-09 13:03:25 -0700 | [diff] [blame] | 707 | /* only assign PCI irq if no IRQ already assigned */ |
| 708 | if (ret && !s->irq.AssignedIRQ) { |
Dominik Brodowski | 6d9a299 | 2009-10-24 12:20:18 +0200 | [diff] [blame] | 709 | if (!s->pci_irq) { |
| 710 | dev_printk(KERN_INFO, &s->dev, "no IRQ found\n"); |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 711 | return ret; |
Dominik Brodowski | 6d9a299 | 2009-10-24 12:20:18 +0200 | [diff] [blame] | 712 | } |
Thomas Gleixner | dace145 | 2006-07-01 19:29:38 -0700 | [diff] [blame] | 713 | type = IRQF_SHARED; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 714 | irq = s->pci_irq; |
| 715 | } |
| 716 | |
Dominik Brodowski | 5fa9167 | 2009-11-08 17:24:46 +0100 | [diff] [blame] | 717 | if (ret && req->Handler) { |
Dominik Brodowski | f958095 | 2008-08-03 11:40:19 +0200 | [diff] [blame] | 718 | ret = request_irq(irq, req->Handler, type, |
Dominik Brodowski | 5fa9167 | 2009-11-08 17:24:46 +0100 | [diff] [blame] | 719 | p_dev->devname, p_dev->priv); |
Dominik Brodowski | 6d9a299 | 2009-10-24 12:20:18 +0200 | [diff] [blame] | 720 | if (ret) { |
| 721 | dev_printk(KERN_INFO, &s->dev, |
| 722 | "request_irq() failed\n"); |
Dominik Brodowski | f958095 | 2008-08-03 11:40:19 +0200 | [diff] [blame] | 723 | return ret; |
Dominik Brodowski | 6d9a299 | 2009-10-24 12:20:18 +0200 | [diff] [blame] | 724 | } |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 725 | } |
| 726 | |
Alan Cox | c533120 | 2006-05-16 16:16:44 +0100 | [diff] [blame] | 727 | /* Make sure the fact the request type was overridden is passed back */ |
Thomas Gleixner | dace145 | 2006-07-01 19:29:38 -0700 | [diff] [blame] | 728 | if (type == IRQF_SHARED && !(req->Attributes & IRQ_TYPE_DYNAMIC_SHARING)) { |
Alan Cox | c533120 | 2006-05-16 16:16:44 +0100 | [diff] [blame] | 729 | req->Attributes |= IRQ_TYPE_DYNAMIC_SHARING; |
Dominik Brodowski | ac449d6 | 2008-08-02 18:33:56 +0200 | [diff] [blame] | 730 | dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: " |
| 731 | "request for exclusive IRQ could not be fulfilled.\n"); |
| 732 | dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: the driver " |
| 733 | "needs updating to supported shared IRQ lines.\n"); |
Alan Cox | c533120 | 2006-05-16 16:16:44 +0100 | [diff] [blame] | 734 | } |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 735 | c->irq.Attributes = req->Attributes; |
| 736 | s->irq.AssignedIRQ = req->AssignedIRQ = irq; |
| 737 | s->irq.Config++; |
| 738 | |
| 739 | c->state |= CONFIG_IRQ_REQ; |
Dominik Brodowski | e2d4096 | 2006-03-02 00:09:29 +0100 | [diff] [blame] | 740 | p_dev->_irq = 1; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 741 | |
| 742 | #ifdef CONFIG_PCMCIA_PROBE |
| 743 | pcmcia_used_irq[irq]++; |
| 744 | #endif |
| 745 | |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 746 | return 0; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 747 | } /* pcmcia_request_irq */ |
| 748 | EXPORT_SYMBOL(pcmcia_request_irq); |
| 749 | |
| 750 | |
| 751 | /** pcmcia_request_window |
| 752 | * |
| 753 | * Request_window() establishes a mapping between card memory space |
| 754 | * and system memory space. |
| 755 | */ |
Dominik Brodowski | 6838b03 | 2009-11-03 01:31:52 +0100 | [diff] [blame] | 756 | int pcmcia_request_window(struct pcmcia_device *p_dev, win_req_t *req, window_handle_t *wh) |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 757 | { |
Dominik Brodowski | 6838b03 | 2009-11-03 01:31:52 +0100 | [diff] [blame] | 758 | struct pcmcia_socket *s = p_dev->socket; |
Dominik Brodowski | 82f88e3 | 2009-11-03 01:16:12 +0100 | [diff] [blame] | 759 | pccard_mem_map *win; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 760 | u_long align; |
| 761 | int w; |
| 762 | |
Dominik Brodowski | 6d9a299 | 2009-10-24 12:20:18 +0200 | [diff] [blame] | 763 | if (!(s->state & SOCKET_PRESENT)) { |
| 764 | dev_dbg(&s->dev, "No card present\n"); |
Dominik Brodowski | 3939c1ef | 2008-08-03 11:10:56 +0200 | [diff] [blame] | 765 | return -ENODEV; |
Dominik Brodowski | 6d9a299 | 2009-10-24 12:20:18 +0200 | [diff] [blame] | 766 | } |
Dominik Brodowski | 610e237 | 2008-08-03 11:58:53 +0200 | [diff] [blame] | 767 | if (req->Attributes & (WIN_PAGED | WIN_SHARED)) { |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 768 | dev_dbg(&s->dev, "bad attribute setting for iomem region\n"); |
Dominik Brodowski | 610e237 | 2008-08-03 11:58:53 +0200 | [diff] [blame] | 769 | return -EINVAL; |
| 770 | } |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 771 | |
| 772 | /* Window size defaults to smallest available */ |
| 773 | if (req->Size == 0) |
| 774 | req->Size = s->map_size; |
| 775 | align = (((s->features & SS_CAP_MEM_ALIGN) || |
| 776 | (req->Attributes & WIN_STRICT_ALIGN)) ? |
| 777 | req->Size : s->map_size); |
Dominik Brodowski | 69ba443 | 2008-08-03 12:10:53 +0200 | [diff] [blame] | 778 | if (req->Size & (s->map_size-1)) { |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 779 | dev_dbg(&s->dev, "invalid map size\n"); |
Dominik Brodowski | 69ba443 | 2008-08-03 12:10:53 +0200 | [diff] [blame] | 780 | return -EINVAL; |
| 781 | } |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 782 | if ((req->Base && (s->features & SS_CAP_STATIC_MAP)) || |
Dominik Brodowski | 69ba443 | 2008-08-03 12:10:53 +0200 | [diff] [blame] | 783 | (req->Base & (align-1))) { |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 784 | dev_dbg(&s->dev, "invalid base address\n"); |
Dominik Brodowski | 69ba443 | 2008-08-03 12:10:53 +0200 | [diff] [blame] | 785 | return -EINVAL; |
| 786 | } |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 787 | if (req->Base) |
| 788 | align = 0; |
| 789 | |
| 790 | /* Allocate system memory window */ |
| 791 | for (w = 0; w < MAX_WIN; w++) |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 792 | if (!(s->state & SOCKET_WIN_REQ(w))) |
| 793 | break; |
Dominik Brodowski | f958095 | 2008-08-03 11:40:19 +0200 | [diff] [blame] | 794 | if (w == MAX_WIN) { |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 795 | dev_dbg(&s->dev, "all windows are used already\n"); |
Dominik Brodowski | f958095 | 2008-08-03 11:40:19 +0200 | [diff] [blame] | 796 | return -EINVAL; |
| 797 | } |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 798 | |
| 799 | win = &s->win[w]; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 800 | |
| 801 | if (!(s->features & SS_CAP_STATIC_MAP)) { |
Dominik Brodowski | 82f88e3 | 2009-11-03 01:16:12 +0100 | [diff] [blame] | 802 | win->res = pcmcia_find_mem_region(req->Base, req->Size, align, |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 803 | (req->Attributes & WIN_MAP_BELOW_1MB), s); |
Dominik Brodowski | 82f88e3 | 2009-11-03 01:16:12 +0100 | [diff] [blame] | 804 | if (!win->res) { |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 805 | dev_dbg(&s->dev, "allocating mem region failed\n"); |
Dominik Brodowski | f958095 | 2008-08-03 11:40:19 +0200 | [diff] [blame] | 806 | return -EINVAL; |
| 807 | } |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 808 | } |
Dominik Brodowski | 6838b03 | 2009-11-03 01:31:52 +0100 | [diff] [blame] | 809 | p_dev->_win |= CLIENT_WIN_REQ(w); |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 810 | |
| 811 | /* Configure the socket controller */ |
Dominik Brodowski | 82f88e3 | 2009-11-03 01:16:12 +0100 | [diff] [blame] | 812 | win->map = w+1; |
| 813 | win->flags = 0; |
| 814 | win->speed = req->AccessSpeed; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 815 | if (req->Attributes & WIN_MEMORY_TYPE) |
Dominik Brodowski | 82f88e3 | 2009-11-03 01:16:12 +0100 | [diff] [blame] | 816 | win->flags |= MAP_ATTRIB; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 817 | if (req->Attributes & WIN_ENABLE) |
Dominik Brodowski | 82f88e3 | 2009-11-03 01:16:12 +0100 | [diff] [blame] | 818 | win->flags |= MAP_ACTIVE; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 819 | if (req->Attributes & WIN_DATA_WIDTH_16) |
Dominik Brodowski | 82f88e3 | 2009-11-03 01:16:12 +0100 | [diff] [blame] | 820 | win->flags |= MAP_16BIT; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 821 | if (req->Attributes & WIN_USE_WAIT) |
Dominik Brodowski | 82f88e3 | 2009-11-03 01:16:12 +0100 | [diff] [blame] | 822 | win->flags |= MAP_USE_WAIT; |
| 823 | win->card_start = 0; |
| 824 | if (s->ops->set_mem_map(s, win) != 0) { |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 825 | dev_dbg(&s->dev, "failed to set memory mapping\n"); |
Dominik Brodowski | 926c540 | 2008-08-03 12:15:11 +0200 | [diff] [blame] | 826 | return -EIO; |
| 827 | } |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 828 | s->state |= SOCKET_WIN_REQ(w); |
| 829 | |
| 830 | /* Return window handle */ |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 831 | if (s->features & SS_CAP_STATIC_MAP) |
Dominik Brodowski | 82f88e3 | 2009-11-03 01:16:12 +0100 | [diff] [blame] | 832 | req->Base = win->static_start; |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 833 | else |
Dominik Brodowski | 82f88e3 | 2009-11-03 01:16:12 +0100 | [diff] [blame] | 834 | req->Base = win->res->start; |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 835 | |
Magnus Damm | 0bdf9b3 | 2006-12-13 19:46:53 +0900 | [diff] [blame] | 836 | *wh = w + 1; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 837 | |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 838 | return 0; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 839 | } /* pcmcia_request_window */ |
| 840 | EXPORT_SYMBOL(pcmcia_request_window); |
Dominik Brodowski | 5f2a71f | 2006-01-15 09:32:39 +0100 | [diff] [blame] | 841 | |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 842 | void pcmcia_disable_device(struct pcmcia_device *p_dev) |
| 843 | { |
Dominik Brodowski | 5f2a71f | 2006-01-15 09:32:39 +0100 | [diff] [blame] | 844 | pcmcia_release_configuration(p_dev); |
Dominik Brodowski | fd23823 | 2006-03-05 10:45:09 +0100 | [diff] [blame] | 845 | pcmcia_release_io(p_dev, &p_dev->io); |
| 846 | pcmcia_release_irq(p_dev, &p_dev->irq); |
Dominik Brodowski | c1ac022 | 2008-06-14 18:51:06 +0200 | [diff] [blame] | 847 | if (p_dev->win) |
Magnus Damm | f5560da | 2006-12-13 19:46:38 +0900 | [diff] [blame] | 848 | pcmcia_release_window(p_dev, p_dev->win); |
Dominik Brodowski | 5f2a71f | 2006-01-15 09:32:39 +0100 | [diff] [blame] | 849 | } |
| 850 | EXPORT_SYMBOL(pcmcia_disable_device); |
Dominik Brodowski | a804b57 | 2008-07-29 08:38:30 +0200 | [diff] [blame] | 851 | |
| 852 | |
| 853 | struct pcmcia_cfg_mem { |
Dominik Brodowski | 9128422 | 2009-10-18 23:32:33 +0200 | [diff] [blame] | 854 | struct pcmcia_device *p_dev; |
| 855 | void *priv_data; |
| 856 | int (*conf_check) (struct pcmcia_device *p_dev, |
| 857 | cistpl_cftable_entry_t *cfg, |
| 858 | cistpl_cftable_entry_t *dflt, |
| 859 | unsigned int vcc, |
| 860 | void *priv_data); |
Dominik Brodowski | a804b57 | 2008-07-29 08:38:30 +0200 | [diff] [blame] | 861 | cisparse_t parse; |
Dominik Brodowski | 8e2fc39 | 2008-08-02 15:30:31 +0200 | [diff] [blame] | 862 | cistpl_cftable_entry_t dflt; |
Dominik Brodowski | a804b57 | 2008-07-29 08:38:30 +0200 | [diff] [blame] | 863 | }; |
| 864 | |
| 865 | /** |
Dominik Brodowski | 9128422 | 2009-10-18 23:32:33 +0200 | [diff] [blame] | 866 | * pcmcia_do_loop_config() - internal helper for pcmcia_loop_config() |
| 867 | * |
| 868 | * pcmcia_do_loop_config() is the internal callback for the call from |
| 869 | * pcmcia_loop_config() to pccard_loop_tuple(). Data is transferred |
| 870 | * by a struct pcmcia_cfg_mem. |
| 871 | */ |
| 872 | static int pcmcia_do_loop_config(tuple_t *tuple, cisparse_t *parse, void *priv) |
| 873 | { |
| 874 | cistpl_cftable_entry_t *cfg = &parse->cftable_entry; |
| 875 | struct pcmcia_cfg_mem *cfg_mem = priv; |
| 876 | |
| 877 | /* default values */ |
| 878 | cfg_mem->p_dev->conf.ConfigIndex = cfg->index; |
| 879 | if (cfg->flags & CISTPL_CFTABLE_DEFAULT) |
| 880 | cfg_mem->dflt = *cfg; |
| 881 | |
| 882 | return cfg_mem->conf_check(cfg_mem->p_dev, cfg, &cfg_mem->dflt, |
| 883 | cfg_mem->p_dev->socket->socket.Vcc, |
| 884 | cfg_mem->priv_data); |
| 885 | } |
| 886 | |
| 887 | /** |
Dominik Brodowski | a804b57 | 2008-07-29 08:38:30 +0200 | [diff] [blame] | 888 | * pcmcia_loop_config() - loop over configuration options |
| 889 | * @p_dev: the struct pcmcia_device which we need to loop for. |
| 890 | * @conf_check: function to call for each configuration option. |
| 891 | * It gets passed the struct pcmcia_device, the CIS data |
| 892 | * describing the configuration option, and private data |
| 893 | * being passed to pcmcia_loop_config() |
| 894 | * @priv_data: private data to be passed to the conf_check function. |
| 895 | * |
| 896 | * pcmcia_loop_config() loops over all configuration options, and calls |
| 897 | * the driver-specific conf_check() for each one, checking whether |
Wolfram Sang | 889c277 | 2009-07-29 14:25:37 +0200 | [diff] [blame] | 898 | * it is a valid one. Returns 0 on success or errorcode otherwise. |
Dominik Brodowski | a804b57 | 2008-07-29 08:38:30 +0200 | [diff] [blame] | 899 | */ |
| 900 | int pcmcia_loop_config(struct pcmcia_device *p_dev, |
| 901 | int (*conf_check) (struct pcmcia_device *p_dev, |
| 902 | cistpl_cftable_entry_t *cfg, |
Dominik Brodowski | 8e2fc39 | 2008-08-02 15:30:31 +0200 | [diff] [blame] | 903 | cistpl_cftable_entry_t *dflt, |
Dominik Brodowski | ad913c1 | 2008-08-02 16:12:00 +0200 | [diff] [blame] | 904 | unsigned int vcc, |
Dominik Brodowski | a804b57 | 2008-07-29 08:38:30 +0200 | [diff] [blame] | 905 | void *priv_data), |
| 906 | void *priv_data) |
| 907 | { |
| 908 | struct pcmcia_cfg_mem *cfg_mem; |
Wolfram Sang | 889c277 | 2009-07-29 14:25:37 +0200 | [diff] [blame] | 909 | int ret; |
Dominik Brodowski | a804b57 | 2008-07-29 08:38:30 +0200 | [diff] [blame] | 910 | |
| 911 | cfg_mem = kzalloc(sizeof(struct pcmcia_cfg_mem), GFP_KERNEL); |
| 912 | if (cfg_mem == NULL) |
| 913 | return -ENOMEM; |
| 914 | |
Dominik Brodowski | 9128422 | 2009-10-18 23:32:33 +0200 | [diff] [blame] | 915 | cfg_mem->p_dev = p_dev; |
| 916 | cfg_mem->conf_check = conf_check; |
| 917 | cfg_mem->priv_data = priv_data; |
Dominik Brodowski | ad913c1 | 2008-08-02 16:12:00 +0200 | [diff] [blame] | 918 | |
Dominik Brodowski | 9128422 | 2009-10-18 23:32:33 +0200 | [diff] [blame] | 919 | ret = pccard_loop_tuple(p_dev->socket, p_dev->func, |
| 920 | CISTPL_CFTABLE_ENTRY, &cfg_mem->parse, |
| 921 | cfg_mem, pcmcia_do_loop_config); |
Dominik Brodowski | a804b57 | 2008-07-29 08:38:30 +0200 | [diff] [blame] | 922 | |
Dominik Brodowski | 9128422 | 2009-10-18 23:32:33 +0200 | [diff] [blame] | 923 | kfree(cfg_mem); |
Dominik Brodowski | a804b57 | 2008-07-29 08:38:30 +0200 | [diff] [blame] | 924 | return ret; |
| 925 | } |
| 926 | EXPORT_SYMBOL(pcmcia_loop_config); |
Dominik Brodowski | 9128422 | 2009-10-18 23:32:33 +0200 | [diff] [blame] | 927 | |
| 928 | |
| 929 | struct pcmcia_loop_mem { |
| 930 | struct pcmcia_device *p_dev; |
| 931 | void *priv_data; |
| 932 | int (*loop_tuple) (struct pcmcia_device *p_dev, |
| 933 | tuple_t *tuple, |
| 934 | void *priv_data); |
| 935 | }; |
| 936 | |
| 937 | /** |
| 938 | * pcmcia_do_loop_tuple() - internal helper for pcmcia_loop_config() |
| 939 | * |
| 940 | * pcmcia_do_loop_tuple() is the internal callback for the call from |
| 941 | * pcmcia_loop_tuple() to pccard_loop_tuple(). Data is transferred |
| 942 | * by a struct pcmcia_cfg_mem. |
| 943 | */ |
| 944 | static int pcmcia_do_loop_tuple(tuple_t *tuple, cisparse_t *parse, void *priv) |
| 945 | { |
| 946 | struct pcmcia_loop_mem *loop = priv; |
| 947 | |
| 948 | return loop->loop_tuple(loop->p_dev, tuple, loop->priv_data); |
| 949 | }; |
| 950 | |
| 951 | /** |
| 952 | * pcmcia_loop_tuple() - loop over tuples in the CIS |
| 953 | * @p_dev: the struct pcmcia_device which we need to loop for. |
| 954 | * @code: which CIS code shall we look for? |
| 955 | * @priv_data: private data to be passed to the loop_tuple function. |
| 956 | * @loop_tuple: function to call for each CIS entry of type @function. IT |
| 957 | * gets passed the raw tuple and @priv_data. |
| 958 | * |
| 959 | * pcmcia_loop_tuple() loops over all CIS entries of type @function, and |
| 960 | * calls the @loop_tuple function for each entry. If the call to @loop_tuple |
| 961 | * returns 0, the loop exits. Returns 0 on success or errorcode otherwise. |
| 962 | */ |
| 963 | int pcmcia_loop_tuple(struct pcmcia_device *p_dev, cisdata_t code, |
| 964 | int (*loop_tuple) (struct pcmcia_device *p_dev, |
| 965 | tuple_t *tuple, |
| 966 | void *priv_data), |
| 967 | void *priv_data) |
| 968 | { |
| 969 | struct pcmcia_loop_mem loop = { |
| 970 | .p_dev = p_dev, |
| 971 | .loop_tuple = loop_tuple, |
| 972 | .priv_data = priv_data}; |
| 973 | |
| 974 | return pccard_loop_tuple(p_dev->socket, p_dev->func, code, NULL, |
| 975 | &loop, pcmcia_do_loop_tuple); |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 976 | } |
Dominik Brodowski | 9128422 | 2009-10-18 23:32:33 +0200 | [diff] [blame] | 977 | EXPORT_SYMBOL(pcmcia_loop_tuple); |
| 978 | |
| 979 | |
| 980 | struct pcmcia_loop_get { |
| 981 | size_t len; |
| 982 | cisdata_t **buf; |
| 983 | }; |
| 984 | |
| 985 | /** |
| 986 | * pcmcia_do_get_tuple() - internal helper for pcmcia_get_tuple() |
| 987 | * |
| 988 | * pcmcia_do_get_tuple() is the internal callback for the call from |
| 989 | * pcmcia_get_tuple() to pcmcia_loop_tuple(). As we're only interested in |
| 990 | * the first tuple, return 0 unconditionally. Create a memory buffer large |
| 991 | * enough to hold the content of the tuple, and fill it with the tuple data. |
| 992 | * The caller is responsible to free the buffer. |
| 993 | */ |
| 994 | static int pcmcia_do_get_tuple(struct pcmcia_device *p_dev, tuple_t *tuple, |
| 995 | void *priv) |
| 996 | { |
| 997 | struct pcmcia_loop_get *get = priv; |
| 998 | |
| 999 | *get->buf = kzalloc(tuple->TupleDataLen, GFP_KERNEL); |
| 1000 | if (*get->buf) { |
| 1001 | get->len = tuple->TupleDataLen; |
| 1002 | memcpy(*get->buf, tuple->TupleData, tuple->TupleDataLen); |
Dominik Brodowski | 6d9a299 | 2009-10-24 12:20:18 +0200 | [diff] [blame] | 1003 | } else |
| 1004 | dev_dbg(&p_dev->dev, "do_get_tuple: out of memory\n"); |
Dominik Brodowski | 9128422 | 2009-10-18 23:32:33 +0200 | [diff] [blame] | 1005 | return 0; |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 1006 | } |
Dominik Brodowski | 9128422 | 2009-10-18 23:32:33 +0200 | [diff] [blame] | 1007 | |
| 1008 | /** |
| 1009 | * pcmcia_get_tuple() - get first tuple from CIS |
| 1010 | * @p_dev: the struct pcmcia_device which we need to loop for. |
| 1011 | * @code: which CIS code shall we look for? |
| 1012 | * @buf: pointer to store the buffer to. |
| 1013 | * |
| 1014 | * pcmcia_get_tuple() gets the content of the first CIS entry of type @code. |
| 1015 | * It returns the buffer length (or zero). The caller is responsible to free |
| 1016 | * the buffer passed in @buf. |
| 1017 | */ |
| 1018 | size_t pcmcia_get_tuple(struct pcmcia_device *p_dev, cisdata_t code, |
| 1019 | unsigned char **buf) |
| 1020 | { |
| 1021 | struct pcmcia_loop_get get = { |
| 1022 | .len = 0, |
| 1023 | .buf = buf, |
| 1024 | }; |
| 1025 | |
| 1026 | *get.buf = NULL; |
| 1027 | pcmcia_loop_tuple(p_dev, code, pcmcia_do_get_tuple, &get); |
| 1028 | |
| 1029 | return get.len; |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 1030 | } |
Dominik Brodowski | 9128422 | 2009-10-18 23:32:33 +0200 | [diff] [blame] | 1031 | EXPORT_SYMBOL(pcmcia_get_tuple); |
| 1032 | |
| 1033 | |
| 1034 | /** |
| 1035 | * pcmcia_do_get_mac() - internal helper for pcmcia_get_mac_from_cis() |
| 1036 | * |
| 1037 | * pcmcia_do_get_mac() is the internal callback for the call from |
| 1038 | * pcmcia_get_mac_from_cis() to pcmcia_loop_tuple(). We check whether the |
| 1039 | * tuple contains a proper LAN_NODE_ID of length 6, and copy the data |
| 1040 | * to struct net_device->dev_addr[i]. |
| 1041 | */ |
| 1042 | static int pcmcia_do_get_mac(struct pcmcia_device *p_dev, tuple_t *tuple, |
| 1043 | void *priv) |
| 1044 | { |
| 1045 | struct net_device *dev = priv; |
| 1046 | int i; |
| 1047 | |
| 1048 | if (tuple->TupleData[0] != CISTPL_FUNCE_LAN_NODE_ID) |
| 1049 | return -EINVAL; |
| 1050 | if (tuple->TupleDataLen < ETH_ALEN + 2) { |
| 1051 | dev_warn(&p_dev->dev, "Invalid CIS tuple length for " |
| 1052 | "LAN_NODE_ID\n"); |
| 1053 | return -EINVAL; |
| 1054 | } |
| 1055 | |
| 1056 | if (tuple->TupleData[1] != ETH_ALEN) { |
| 1057 | dev_warn(&p_dev->dev, "Invalid header for LAN_NODE_ID\n"); |
| 1058 | return -EINVAL; |
| 1059 | } |
| 1060 | for (i = 0; i < 6; i++) |
| 1061 | dev->dev_addr[i] = tuple->TupleData[i+2]; |
| 1062 | return 0; |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 1063 | } |
Dominik Brodowski | 9128422 | 2009-10-18 23:32:33 +0200 | [diff] [blame] | 1064 | |
| 1065 | /** |
| 1066 | * pcmcia_get_mac_from_cis() - read out MAC address from CISTPL_FUNCE |
| 1067 | * @p_dev: the struct pcmcia_device for which we want the address. |
| 1068 | * @dev: a properly prepared struct net_device to store the info to. |
| 1069 | * |
| 1070 | * pcmcia_get_mac_from_cis() reads out the hardware MAC address from |
| 1071 | * CISTPL_FUNCE and stores it into struct net_device *dev->dev_addr which |
| 1072 | * must be set up properly by the driver (see examples!). |
| 1073 | */ |
| 1074 | int pcmcia_get_mac_from_cis(struct pcmcia_device *p_dev, struct net_device *dev) |
| 1075 | { |
| 1076 | return pcmcia_loop_tuple(p_dev, CISTPL_FUNCE, pcmcia_do_get_mac, dev); |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 1077 | } |
Dominik Brodowski | 9128422 | 2009-10-18 23:32:33 +0200 | [diff] [blame] | 1078 | EXPORT_SYMBOL(pcmcia_get_mac_from_cis); |
| 1079 | |