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