blob: a5c176598d9543d30e084a6e36daa72fbd65d80a [file] [log] [blame]
Dominik Brodowski1a8d4662005-06-27 16:28:53 -07001/*
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 Brodowski1a8d4662005-06-27 16:28:53 -070017#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 Brodowski91284222009-10-18 23:32:33 +020023#include <linux/netdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070025
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +020026#include <asm/irq.h>
27
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070028#include <pcmcia/ss.h>
29#include <pcmcia/cs.h>
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070030#include <pcmcia/cistpl.h>
31#include <pcmcia/cisreg.h>
32#include <pcmcia/ds.h>
33
34#include "cs_internal.h"
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070035
36
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070037/* Access speed for IO windows */
Dominik Brodowski9fea84f2009-12-07 22:11:45 +010038static int io_speed;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070039module_param(io_speed, int, 0444);
40
41
Dominik Brodowskia3ac9af2010-01-06 14:03:03 +010042int pcmcia_validate_mem(struct pcmcia_socket *s)
43{
44 if (s->resource_ops->validate_mem)
45 return s->resource_ops->validate_mem(s);
46 /* if there is no callback, we can assume that everything is OK */
47 return 0;
48}
49
50struct resource *pcmcia_find_mem_region(u_long base, u_long num, u_long align,
51 int low, struct pcmcia_socket *s)
52{
53 if (s->resource_ops->find_mem)
54 return s->resource_ops->find_mem(base, num, align, low, s);
55 return NULL;
56}
57
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070058
Dominik Brodowskiad0c7be2010-07-25 13:10:22 +020059static void release_io_space(struct pcmcia_socket *s, struct resource *res)
60{
61 resource_size_t num = resource_size(res);
62 int i;
63
64 dev_dbg(&s->dev, "release_io_space for %pR\n", res);
65
66 for (i = 0; i < MAX_IO_WIN; i++) {
67 if (!s->io[i].res)
68 continue;
69 if ((s->io[i].res->start <= res->start) &&
70 (s->io[i].res->end >= res->end)) {
71 s->io[i].InUse -= num;
72 if (res->parent)
73 release_resource(res);
74 res->start = res->end = 0;
75 res->flags = IORESOURCE_IO;
76 /* Free the window if no one else is using it */
77 if (s->io[i].InUse == 0) {
78 release_resource(s->io[i].res);
79 kfree(s->io[i].res);
80 s->io[i].res = NULL;
81 }
82 }
83 }
84} /* release_io_space */
85
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070086/** alloc_io_space
87 *
88 * Special stuff for managing IO windows, because they are scarce
89 */
Dominik Brodowski2ce49052010-07-24 13:14:44 +020090static int alloc_io_space(struct pcmcia_socket *s, struct resource *res,
91 unsigned int lines)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070092{
Dominik Brodowskib19a7272010-03-20 13:10:47 +010093 unsigned int align;
Dominik Brodowski2ce49052010-07-24 13:14:44 +020094 unsigned int base = res->start;
95 unsigned int num = res->end;
96 int ret;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070097
Dominik Brodowski2ce49052010-07-24 13:14:44 +020098 res->flags |= IORESOURCE_IO;
99
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200100 dev_dbg(&s->dev, "alloc_io_space request for %pR, %d lines\n",
101 res, lines);
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200102
103 align = base ? (lines ? 1<<lines : 0) : 1;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700104 if (align && (align < num)) {
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200105 if (base) {
106 dev_dbg(&s->dev, "odd IO request\n");
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700107 align = 0;
108 } else
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100109 while (align && (align < num))
110 align <<= 1;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700111 }
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200112 if (base & ~(align-1)) {
113 dev_dbg(&s->dev, "odd IO request\n");
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700114 align = 0;
115 }
Dominik Brodowskib19a7272010-03-20 13:10:47 +0100116
Dominik Brodowskiad0c7be2010-07-25 13:10:22 +0200117 ret = s->resource_ops->find_io(s, res->flags, &base, num, align,
118 &res->parent);
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200119 if (ret) {
Dominik Brodowskiad0c7be2010-07-25 13:10:22 +0200120 dev_dbg(&s->dev, "alloc_io_space request failed (%d)\n", ret);
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200121 return -EINVAL;
122 }
123
124 res->start = base;
125 res->end = res->start + num - 1;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700126
Dominik Brodowskiad0c7be2010-07-25 13:10:22 +0200127 if (res->parent) {
128 ret = request_resource(res->parent, res);
129 if (ret) {
130 dev_warn(&s->dev,
131 "request_resource %pR failed: %d\n", res, ret);
132 res->parent = NULL;
133 release_io_space(s, res);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700134 }
135 }
Dominik Brodowskiad0c7be2010-07-25 13:10:22 +0200136 dev_dbg(&s->dev, "alloc_io_space request result %d: %pR\n", ret, res);
137 return ret;
138} /* alloc_io_space */
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700139
140
Dominik Brodowski1d5cc192010-07-24 12:23:21 +0200141/**
142 * pcmcia_access_config() - read or write card configuration registers
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700143 *
Dominik Brodowski1d5cc192010-07-24 12:23:21 +0200144 * pcmcia_access_config() reads and writes configuration registers in
145 * attribute memory. Memory window 0 is reserved for this and the tuple
146 * reading services. Drivers must use pcmcia_read_config_byte() or
147 * pcmcia_write_config_byte().
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700148 */
Dominik Brodowski1d5cc192010-07-24 12:23:21 +0200149static int pcmcia_access_config(struct pcmcia_device *p_dev,
150 off_t where, u8 *val,
151 int (*accessf) (struct pcmcia_socket *s,
152 int attr, unsigned int addr,
153 unsigned int len, void *ptr))
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700154{
Dominik Brodowski855cdf12006-01-10 20:48:59 +0100155 struct pcmcia_socket *s;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700156 config_t *c;
157 int addr;
Dominik Brodowski059f6672010-03-30 18:07:50 +0200158 int ret = 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700159
Dominik Brodowski855cdf12006-01-10 20:48:59 +0100160 s = p_dev->socket;
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100161
162 mutex_lock(&s->ops_mutex);
Dominik Brodowski855cdf12006-01-10 20:48:59 +0100163 c = p_dev->function_config;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700164
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200165 if (!(c->state & CONFIG_LOCKED)) {
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200166 dev_dbg(&p_dev->dev, "Configuration isnt't locked\n");
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100167 mutex_unlock(&s->ops_mutex);
Dominik Brodowski943f70f2008-08-03 11:23:42 +0200168 return -EACCES;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200169 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700170
Dominik Brodowski1d5cc192010-07-24 12:23:21 +0200171 addr = (c->ConfigBase + where) >> 1;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700172
Dominik Brodowski1d5cc192010-07-24 12:23:21 +0200173 ret = accessf(s, 1, addr, 1, val);
174
Dominik Brodowski059f6672010-03-30 18:07:50 +0200175 mutex_unlock(&s->ops_mutex);
Dominik Brodowski1d5cc192010-07-24 12:23:21 +0200176
Dominik Brodowski059f6672010-03-30 18:07:50 +0200177 return ret;
Dominik Brodowski1d5cc192010-07-24 12:23:21 +0200178} /* pcmcia_access_config */
179
180
181/**
182 * pcmcia_read_config_byte() - read a byte from a card configuration register
183 *
184 * pcmcia_read_config_byte() reads a byte from a configuration register in
185 * attribute memory.
186 */
187int pcmcia_read_config_byte(struct pcmcia_device *p_dev, off_t where, u8 *val)
188{
189 return pcmcia_access_config(p_dev, where, val, pcmcia_read_cis_mem);
190}
191EXPORT_SYMBOL(pcmcia_read_config_byte);
192
193
194/**
195 * pcmcia_write_config_byte() - write a byte to a card configuration register
196 *
197 * pcmcia_write_config_byte() writes a byte to a configuration register in
198 * attribute memory.
199 */
200int pcmcia_write_config_byte(struct pcmcia_device *p_dev, off_t where, u8 val)
201{
202 return pcmcia_access_config(p_dev, where, &val, pcmcia_write_cis_mem);
203}
204EXPORT_SYMBOL(pcmcia_write_config_byte);
Dominik Brodowski34481392005-06-27 16:28:54 -0700205
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700206
Magnus Damm868575d2006-12-13 19:46:43 +0900207int pcmcia_map_mem_page(struct pcmcia_device *p_dev, window_handle_t wh,
Dominik Brodowskib5cb2592010-07-24 18:46:42 +0200208 unsigned int offset)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700209{
Magnus Damm0bdf9b32006-12-13 19:46:53 +0900210 struct pcmcia_socket *s = p_dev->socket;
Dominik Brodowski0ca724d2010-07-24 19:03:02 +0200211 struct resource *res = wh;
212 unsigned int w;
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100213 int ret;
Magnus Damm868575d2006-12-13 19:46:43 +0900214
Dominik Brodowski0ca724d2010-07-24 19:03:02 +0200215 w = ((res->flags & IORESOURCE_BITS & WIN_FLAGS_REQ) >> 2) - 1;
216 if (w >= MAX_WIN)
Dominik Brodowskiffb8da22008-08-03 11:18:00 +0200217 return -EINVAL;
Dominik Brodowskib5cb2592010-07-24 18:46:42 +0200218
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100219 mutex_lock(&s->ops_mutex);
Dominik Brodowski0ca724d2010-07-24 19:03:02 +0200220 s->win[w].card_start = offset;
221 ret = s->ops->set_mem_map(s, &s->win[w]);
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100222 if (ret)
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200223 dev_warn(&p_dev->dev, "failed to set_mem_map\n");
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100224 mutex_unlock(&s->ops_mutex);
225 return ret;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700226} /* pcmcia_map_mem_page */
227EXPORT_SYMBOL(pcmcia_map_mem_page);
228
229
230/** pcmcia_modify_configuration
231 *
232 * Modify a locked socket configuration
233 */
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700234int pcmcia_modify_configuration(struct pcmcia_device *p_dev,
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700235 modconf_t *mod)
236{
237 struct pcmcia_socket *s;
238 config_t *c;
Jiri Slaby4e06e242010-03-16 15:57:44 +0100239 int ret;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700240
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700241 s = p_dev->socket;
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100242
243 mutex_lock(&s->ops_mutex);
Dominik Brodowskidbb22f02006-01-10 20:41:27 +0100244 c = p_dev->function_config;
245
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200246 if (!(s->state & SOCKET_PRESENT)) {
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200247 dev_dbg(&p_dev->dev, "No card present\n");
Jiri Slaby4e06e242010-03-16 15:57:44 +0100248 ret = -ENODEV;
249 goto unlock;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200250 }
251 if (!(c->state & CONFIG_LOCKED)) {
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200252 dev_dbg(&p_dev->dev, "Configuration isnt't locked\n");
Jiri Slaby4e06e242010-03-16 15:57:44 +0100253 ret = -EACCES;
254 goto unlock;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200255 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700256
Dominik Brodowski0cb3c492010-03-06 20:42:35 +0100257 if (mod->Attributes & (CONF_IRQ_CHANGE_VALID | CONF_VCC_CHANGE_VALID)) {
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200258 dev_dbg(&p_dev->dev,
Dominik Brodowski0cb3c492010-03-06 20:42:35 +0100259 "changing Vcc or IRQ is not allowed at this time\n");
Jiri Slaby4e06e242010-03-16 15:57:44 +0100260 ret = -EINVAL;
261 goto unlock;
Dominik Brodowskid8b0a492008-08-03 10:56:13 +0200262 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700263
264 /* We only allow changing Vpp1 and Vpp2 to the same value */
265 if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) &&
266 (mod->Attributes & CONF_VPP2_CHANGE_VALID)) {
Ilpo Järvinen60df3de82008-10-30 13:02:54 +0200267 if (mod->Vpp1 != mod->Vpp2) {
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200268 dev_dbg(&p_dev->dev,
269 "Vpp1 and Vpp2 must be the same\n");
Jiri Slaby4e06e242010-03-16 15:57:44 +0100270 ret = -EINVAL;
271 goto unlock;
Ilpo Järvinen60df3de82008-10-30 13:02:54 +0200272 }
Dominik Brodowski71ed90d2005-09-09 13:03:27 -0700273 s->socket.Vpp = mod->Vpp1;
Dominik Brodowskid8b0a492008-08-03 10:56:13 +0200274 if (s->ops->set_socket(s, &s->socket)) {
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200275 dev_printk(KERN_WARNING, &p_dev->dev,
Dominik Brodowskid8b0a492008-08-03 10:56:13 +0200276 "Unable to set VPP\n");
Jiri Slaby4e06e242010-03-16 15:57:44 +0100277 ret = -EIO;
278 goto unlock;
Dominik Brodowskid8b0a492008-08-03 10:56:13 +0200279 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700280 } else if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) ||
Dominik Brodowskid8b0a492008-08-03 10:56:13 +0200281 (mod->Attributes & CONF_VPP2_CHANGE_VALID)) {
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200282 dev_dbg(&p_dev->dev,
283 "changing Vcc is not allowed at this time\n");
Jiri Slaby4e06e242010-03-16 15:57:44 +0100284 ret = -EINVAL;
285 goto unlock;
Dominik Brodowskid8b0a492008-08-03 10:56:13 +0200286 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700287
Dominik Brodowski4bbed522006-01-15 11:18:12 +0100288 if (mod->Attributes & CONF_IO_CHANGE_WIDTH) {
289 pccard_io_map io_off = { 0, 0, 0, 0, 1 };
290 pccard_io_map io_on;
291 int i;
292
293 io_on.speed = io_speed;
294 for (i = 0; i < MAX_IO_WIN; i++) {
295 if (!s->io[i].res)
296 continue;
297 io_off.map = i;
298 io_on.map = i;
299
300 io_on.flags = MAP_ACTIVE | IO_DATA_PATH_WIDTH_8;
301 io_on.start = s->io[i].res->start;
302 io_on.stop = s->io[i].res->end;
303
304 s->ops->set_io_map(s, &io_off);
305 mdelay(40);
306 s->ops->set_io_map(s, &io_on);
307 }
308 }
Jiri Slaby4e06e242010-03-16 15:57:44 +0100309 ret = 0;
310unlock:
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100311 mutex_unlock(&s->ops_mutex);
Dominik Brodowski4bbed522006-01-15 11:18:12 +0100312
Jiri Slaby4e06e242010-03-16 15:57:44 +0100313 return ret;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700314} /* modify_configuration */
315EXPORT_SYMBOL(pcmcia_modify_configuration);
316
317
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700318int pcmcia_release_configuration(struct pcmcia_device *p_dev)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700319{
320 pccard_io_map io = { 0, 0, 0, 0, 1 };
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700321 struct pcmcia_socket *s = p_dev->socket;
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100322 config_t *c;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700323 int i;
324
Dominik Brodowski9e86749c2010-01-16 00:26:33 +0100325 mutex_lock(&s->ops_mutex);
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100326 c = p_dev->function_config;
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100327 if (p_dev->_locked) {
328 p_dev->_locked = 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700329 if (--(s->lock_count) == 0) {
330 s->socket.flags = SS_OUTPUT_ENA; /* Is this correct? */
331 s->socket.Vpp = 0;
332 s->socket.io_irq = 0;
333 s->ops->set_socket(s, &s->socket);
334 }
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100335 }
336 if (c->state & CONFIG_LOCKED) {
337 c->state &= ~CONFIG_LOCKED;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700338 if (c->state & CONFIG_IO_REQ)
339 for (i = 0; i < MAX_IO_WIN; i++) {
Dominik Brodowskic7d00692006-01-15 08:04:43 +0100340 if (!s->io[i].res)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700341 continue;
342 s->io[i].Config--;
343 if (s->io[i].Config != 0)
344 continue;
345 io.map = i;
346 s->ops->set_io_map(s, &io);
347 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700348 }
Dominik Brodowski9e86749c2010-01-16 00:26:33 +0100349 mutex_unlock(&s->ops_mutex);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700350
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200351 return 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700352} /* pcmcia_release_configuration */
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700353
354
355/** pcmcia_release_io
356 *
357 * Release_io() releases the I/O ranges allocated by a client. This
358 * may be invoked some time after a card ejection has already dumped
359 * the actual socket configuration, so if the client is "stale", we
360 * don't bother checking the port ranges against the current socket
361 * values.
362 */
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200363static int pcmcia_release_io(struct pcmcia_device *p_dev)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700364{
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700365 struct pcmcia_socket *s = p_dev->socket;
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100366 int ret = -EINVAL;
367 config_t *c;
368
369 mutex_lock(&s->ops_mutex);
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100370 if (!p_dev->_io)
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100371 goto out;
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100372
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200373 c = p_dev->function_config;
374
375 release_io_space(s, &c->io[0]);
376
377 if (c->io[1].end)
378 release_io_space(s, &c->io[1]);
379
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100380 p_dev->_io = 0;
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100381 c->state &= ~CONFIG_IO_REQ;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700382
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100383out:
384 mutex_unlock(&s->ops_mutex);
385
386 return ret;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700387} /* pcmcia_release_io */
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700388
389
Dominik Brodowski0ca724d2010-07-24 19:03:02 +0200390int pcmcia_release_window(struct pcmcia_device *p_dev, struct resource *res)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700391{
Magnus Damm0bdf9b32006-12-13 19:46:53 +0900392 struct pcmcia_socket *s = p_dev->socket;
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100393 pccard_mem_map *win;
Dominik Brodowski0ca724d2010-07-24 19:03:02 +0200394 unsigned int w;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700395
Dominik Brodowski0ca724d2010-07-24 19:03:02 +0200396 dev_dbg(&p_dev->dev, "releasing window %pR\n", res);
397
398 w = ((res->flags & IORESOURCE_BITS & WIN_FLAGS_REQ) >> 2) - 1;
399 if (w >= MAX_WIN)
Dominik Brodowskiffb8da22008-08-03 11:18:00 +0200400 return -EINVAL;
Magnus Damm0bdf9b32006-12-13 19:46:53 +0900401
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100402 mutex_lock(&s->ops_mutex);
Dominik Brodowski0ca724d2010-07-24 19:03:02 +0200403 win = &s->win[w];
Magnus Damm0bdf9b32006-12-13 19:46:53 +0900404
Dominik Brodowski0ca724d2010-07-24 19:03:02 +0200405 if (!(p_dev->_win & CLIENT_WIN_REQ(w))) {
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200406 dev_dbg(&p_dev->dev, "not releasing unknown window\n");
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100407 mutex_unlock(&s->ops_mutex);
Dominik Brodowskiffb8da22008-08-03 11:18:00 +0200408 return -EINVAL;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200409 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700410
411 /* Shut down memory window */
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100412 win->flags &= ~MAP_ACTIVE;
413 s->ops->set_mem_map(s, win);
Dominik Brodowski0ca724d2010-07-24 19:03:02 +0200414 s->state &= ~SOCKET_WIN_REQ(w);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700415
416 /* Release system memory */
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100417 if (win->res) {
Dominik Brodowskiad0c7be2010-07-25 13:10:22 +0200418 release_resource(res);
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100419 release_resource(win->res);
420 kfree(win->res);
421 win->res = NULL;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700422 }
Dominik Brodowski0ca724d2010-07-24 19:03:02 +0200423 p_dev->_win &= ~CLIENT_WIN_REQ(w);
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100424 mutex_unlock(&s->ops_mutex);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700425
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200426 return 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700427} /* pcmcia_release_window */
428EXPORT_SYMBOL(pcmcia_release_window);
429
430
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700431int pcmcia_request_configuration(struct pcmcia_device *p_dev,
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700432 config_req_t *req)
433{
434 int i;
435 u_int base;
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700436 struct pcmcia_socket *s = p_dev->socket;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700437 config_t *c;
438 pccard_io_map iomap;
439
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700440 if (!(s->state & SOCKET_PRESENT))
Joe Perchesd598de02009-06-28 09:26:41 -0700441 return -ENODEV;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700442
Dominik Brodowskide6405e2008-08-03 10:47:59 +0200443 if (req->IntType & INT_CARDBUS) {
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200444 dev_dbg(&p_dev->dev, "IntType may not be INT_CARDBUS\n");
Dominik Brodowskide6405e2008-08-03 10:47:59 +0200445 return -EINVAL;
446 }
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100447
448 mutex_lock(&s->ops_mutex);
Dominik Brodowskidbb22f02006-01-10 20:41:27 +0100449 c = p_dev->function_config;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200450 if (c->state & CONFIG_LOCKED) {
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100451 mutex_unlock(&s->ops_mutex);
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200452 dev_dbg(&p_dev->dev, "Configuration is locked\n");
Dominik Brodowski943f70f2008-08-03 11:23:42 +0200453 return -EACCES;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200454 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700455
456 /* Do power control. We don't allow changes in Vcc. */
Dominik Brodowski70294b42006-01-15 12:43:16 +0100457 s->socket.Vpp = req->Vpp;
Dominik Brodowskid8b0a492008-08-03 10:56:13 +0200458 if (s->ops->set_socket(s, &s->socket)) {
Dominik Brodowski9e86749c2010-01-16 00:26:33 +0100459 mutex_unlock(&s->ops_mutex);
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200460 dev_printk(KERN_WARNING, &p_dev->dev,
Dominik Brodowskid8b0a492008-08-03 10:56:13 +0200461 "Unable to set socket state\n");
462 return -EINVAL;
463 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700464
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700465 /* Pick memory or I/O card, DMA mode, interrupt */
466 c->IntType = req->IntType;
467 c->Attributes = req->Attributes;
468 if (req->IntType & INT_MEMORY_AND_IO)
469 s->socket.flags |= SS_IOCARD;
470 if (req->IntType & INT_ZOOMED_VIDEO)
471 s->socket.flags |= SS_ZVCARD | SS_IOCARD;
472 if (req->Attributes & CONF_ENABLE_DMA)
473 s->socket.flags |= SS_DMA_MODE;
474 if (req->Attributes & CONF_ENABLE_SPKR)
475 s->socket.flags |= SS_SPKR_ENA;
476 if (req->Attributes & CONF_ENABLE_IRQ)
Dominik Brodowski6f840af2010-03-07 10:51:23 +0100477 s->socket.io_irq = s->pcmcia_irq;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700478 else
479 s->socket.io_irq = 0;
480 s->ops->set_socket(s, &s->socket);
481 s->lock_count++;
482
483 /* Set up CIS configuration registers */
484 base = c->ConfigBase = req->ConfigBase;
Dominik Brodowski1ae9c7d2006-01-10 20:40:40 +0100485 c->CardValues = req->Present;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700486 if (req->Present & PRESENT_COPY) {
487 c->Copy = req->Copy;
488 pcmcia_write_cis_mem(s, 1, (base + CISREG_SCR)>>1, 1, &c->Copy);
489 }
490 if (req->Present & PRESENT_OPTION) {
491 if (s->functions == 1) {
492 c->Option = req->ConfigIndex & COR_CONFIG_MASK;
493 } else {
494 c->Option = req->ConfigIndex & COR_MFC_CONFIG_MASK;
495 c->Option |= COR_FUNC_ENA|COR_IREQ_ENA;
496 if (req->Present & PRESENT_IOBASE_0)
497 c->Option |= COR_ADDR_DECODE;
498 }
Dominik Brodowskia7debe72010-03-07 10:58:29 +0100499 if ((req->Attributes & CONF_ENABLE_IRQ) &&
500 !(req->Attributes & CONF_ENABLE_PULSE_IRQ))
501 c->Option |= COR_LEVEL_REQ;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700502 pcmcia_write_cis_mem(s, 1, (base + CISREG_COR)>>1, 1, &c->Option);
503 mdelay(40);
504 }
505 if (req->Present & PRESENT_STATUS) {
506 c->Status = req->Status;
507 pcmcia_write_cis_mem(s, 1, (base + CISREG_CCSR)>>1, 1, &c->Status);
508 }
509 if (req->Present & PRESENT_PIN_REPLACE) {
510 c->Pin = req->Pin;
511 pcmcia_write_cis_mem(s, 1, (base + CISREG_PRR)>>1, 1, &c->Pin);
512 }
513 if (req->Present & PRESENT_EXT_STATUS) {
514 c->ExtStatus = req->ExtStatus;
515 pcmcia_write_cis_mem(s, 1, (base + CISREG_ESR)>>1, 1, &c->ExtStatus);
516 }
517 if (req->Present & PRESENT_IOBASE_0) {
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200518 u8 b = c->io[0].start & 0xff;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700519 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_0)>>1, 1, &b);
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200520 b = (c->io[0].start >> 8) & 0xff;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700521 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_1)>>1, 1, &b);
522 }
523 if (req->Present & PRESENT_IOSIZE) {
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200524 u8 b = resource_size(&c->io[0]) + resource_size(&c->io[1]) - 1;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700525 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOSIZE)>>1, 1, &b);
526 }
527
528 /* Configure I/O windows */
529 if (c->state & CONFIG_IO_REQ) {
530 iomap.speed = io_speed;
531 for (i = 0; i < MAX_IO_WIN; i++)
Dominik Brodowskic7d00692006-01-15 08:04:43 +0100532 if (s->io[i].res) {
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700533 iomap.map = i;
534 iomap.flags = MAP_ACTIVE;
Dominik Brodowskic7d00692006-01-15 08:04:43 +0100535 switch (s->io[i].res->flags & IO_DATA_PATH_WIDTH) {
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700536 case IO_DATA_PATH_WIDTH_16:
537 iomap.flags |= MAP_16BIT; break;
538 case IO_DATA_PATH_WIDTH_AUTO:
539 iomap.flags |= MAP_AUTOSZ; break;
540 default:
541 break;
542 }
Dominik Brodowskic7d00692006-01-15 08:04:43 +0100543 iomap.start = s->io[i].res->start;
544 iomap.stop = s->io[i].res->end;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700545 s->ops->set_io_map(s, &iomap);
546 s->io[i].Config++;
547 }
548 }
549
550 c->state |= CONFIG_LOCKED;
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100551 p_dev->_locked = 1;
Dominik Brodowski059f6672010-03-30 18:07:50 +0200552 mutex_unlock(&s->ops_mutex);
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200553 return 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700554} /* pcmcia_request_configuration */
555EXPORT_SYMBOL(pcmcia_request_configuration);
556
557
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200558/**
559 * pcmcia_request_io() - attempt to reserve port ranges for PCMCIA devices
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700560 *
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200561 * pcmcia_request_io() attepts to reserve the IO port ranges specified in
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200562 * &struct pcmcia_device @p_dev->resource[0] and @p_dev->resource[1]. The
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200563 * "start" value is the requested start of the IO port resource; "end"
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200564 * reflects the number of ports requested. The number of IO lines requested
565 * is specified in &struct pcmcia_device @p_dev->io_lines.
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700566 */
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200567int pcmcia_request_io(struct pcmcia_device *p_dev)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700568{
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700569 struct pcmcia_socket *s = p_dev->socket;
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200570 config_t *c = p_dev->function_config;
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100571 int ret = -EINVAL;
572
573 mutex_lock(&s->ops_mutex);
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200574 dev_dbg(&p_dev->dev, "pcmcia_request_io: %pR , %pR",
575 &c->io[0], &c->io[1]);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700576
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200577 if (!(s->state & SOCKET_PRESENT)) {
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200578 dev_dbg(&p_dev->dev, "pcmcia_request_io: No card present\n");
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100579 goto out;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200580 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700581
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200582 if (c->state & CONFIG_LOCKED) {
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200583 dev_dbg(&p_dev->dev, "Configuration is locked\n");
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100584 goto out;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200585 }
Dominik Brodowskif9580952008-08-03 11:40:19 +0200586 if (c->state & CONFIG_IO_REQ) {
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200587 dev_dbg(&p_dev->dev, "IO already configured\n");
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100588 goto out;
Dominik Brodowskif9580952008-08-03 11:40:19 +0200589 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700590
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200591 ret = alloc_io_space(s, &c->io[0], p_dev->io_lines);
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200592 if (ret)
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100593 goto out;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700594
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200595 if (c->io[1].end) {
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200596 ret = alloc_io_space(s, &c->io[1], p_dev->io_lines);
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100597 if (ret) {
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200598 release_io_space(s, &c->io[0]);
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100599 goto out;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700600 }
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200601 } else
602 c->io[1].start = 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700603
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700604 c->state |= CONFIG_IO_REQ;
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100605 p_dev->_io = 1;
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100606
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200607 dev_dbg(&p_dev->dev, "pcmcia_request_io succeeded: %pR , %pR",
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200608 &c->io[0], &c->io[1]);
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100609out:
610 mutex_unlock(&s->ops_mutex);
611
612 return ret;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700613} /* pcmcia_request_io */
614EXPORT_SYMBOL(pcmcia_request_io);
615
616
Dominik Brodowskieb141202010-03-07 12:21:16 +0100617/**
618 * pcmcia_request_irq() - attempt to request a IRQ for a PCMCIA device
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700619 *
Dominik Brodowskieb141202010-03-07 12:21:16 +0100620 * pcmcia_request_irq() is a wrapper around request_irq which will allow
621 * the PCMCIA core to clean up the registration in pcmcia_disable_device().
622 * Drivers are free to use request_irq() directly, but then they need to
623 * call free_irq themselfves, too. Also, only IRQF_SHARED capable IRQ
624 * handlers are allowed.
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700625 */
Dominik Brodowskieb141202010-03-07 12:21:16 +0100626int __must_check pcmcia_request_irq(struct pcmcia_device *p_dev,
627 irq_handler_t handler)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700628{
Dominik Brodowskieb141202010-03-07 12:21:16 +0100629 int ret;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700630
Dominik Brodowskieb141202010-03-07 12:21:16 +0100631 if (!p_dev->irq)
632 return -EINVAL;
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100633
Dominik Brodowskieb141202010-03-07 12:21:16 +0100634 ret = request_irq(p_dev->irq, handler, IRQF_SHARED,
635 p_dev->devname, p_dev->priv);
636 if (!ret)
637 p_dev->_irq = 1;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700638
Dominik Brodowskieb141202010-03-07 12:21:16 +0100639 return ret;
640}
641EXPORT_SYMBOL(pcmcia_request_irq);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700642
Dominik Brodowskieb141202010-03-07 12:21:16 +0100643
644/**
645 * pcmcia_request_exclusive_irq() - attempt to request an exclusive IRQ first
646 *
647 * pcmcia_request_exclusive_irq() is a wrapper around request_irq which
648 * attempts first to request an exclusive IRQ. If it fails, it also accepts
649 * a shared IRQ, but prints out a warning. PCMCIA drivers should allow for
650 * IRQ sharing and either use request_irq directly (then they need to call
651 * free_irq themselves, too), or the pcmcia_request_irq() function.
652 */
653int __must_check
Dominik Brodowskib19a7272010-03-20 13:10:47 +0100654__pcmcia_request_exclusive_irq(struct pcmcia_device *p_dev,
655 irq_handler_t handler)
Dominik Brodowskieb141202010-03-07 12:21:16 +0100656{
657 int ret;
658
659 if (!p_dev->irq)
660 return -EINVAL;
661
662 ret = request_irq(p_dev->irq, handler, 0, p_dev->devname, p_dev->priv);
663 if (ret) {
664 ret = pcmcia_request_irq(p_dev, handler);
665 dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: "
666 "request for exclusive IRQ could not be fulfilled.\n");
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200667 dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: the driver "
668 "needs updating to supported shared IRQ lines.\n");
669 }
Dominik Brodowskieb141202010-03-07 12:21:16 +0100670 if (ret)
671 dev_printk(KERN_INFO, &p_dev->dev, "request_irq() failed\n");
672 else
673 p_dev->_irq = 1;
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200674
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100675 return ret;
Dominik Brodowskieb141202010-03-07 12:21:16 +0100676} /* pcmcia_request_exclusive_irq */
Dominik Brodowskib19a7272010-03-20 13:10:47 +0100677EXPORT_SYMBOL(__pcmcia_request_exclusive_irq);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700678
679
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200680#ifdef CONFIG_PCMCIA_PROBE
681
682/* mask of IRQs already reserved by other cards, we should avoid using them */
Dominik Brodowski127c03c2010-08-03 09:33:45 +0200683static u8 pcmcia_used_irq[32];
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200684
685static irqreturn_t test_action(int cpl, void *dev_id)
686{
687 return IRQ_NONE;
688}
689
690/**
691 * pcmcia_setup_isa_irq() - determine whether an ISA IRQ can be used
692 * @p_dev - the associated PCMCIA device
693 *
694 * locking note: must be called with ops_mutex locked.
695 */
696static int pcmcia_setup_isa_irq(struct pcmcia_device *p_dev, int type)
697{
698 struct pcmcia_socket *s = p_dev->socket;
699 unsigned int try, irq;
700 u32 mask = s->irq_mask;
701 int ret = -ENODEV;
702
703 for (try = 0; try < 64; try++) {
704 irq = try % 32;
705
Dominik Brodowski127c03c2010-08-03 09:33:45 +0200706 if (irq > NR_IRQS)
707 continue;
708
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200709 /* marked as available by driver, not blocked by userspace? */
710 if (!((mask >> irq) & 1))
711 continue;
712
713 /* avoid an IRQ which is already used by another PCMCIA card */
714 if ((try < 32) && pcmcia_used_irq[irq])
715 continue;
716
717 /* register the correct driver, if possible, to check whether
718 * registering a dummy handle works, i.e. if the IRQ isn't
719 * marked as used by the kernel resource management core */
720 ret = request_irq(irq, test_action, type, p_dev->devname,
721 p_dev);
722 if (!ret) {
723 free_irq(irq, p_dev);
Dominik Brodowskieb141202010-03-07 12:21:16 +0100724 p_dev->irq = s->pcmcia_irq = irq;
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200725 pcmcia_used_irq[irq]++;
726 break;
727 }
728 }
729
730 return ret;
731}
732
733void pcmcia_cleanup_irq(struct pcmcia_socket *s)
734{
Dominik Brodowski6f840af2010-03-07 10:51:23 +0100735 pcmcia_used_irq[s->pcmcia_irq]--;
736 s->pcmcia_irq = 0;
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200737}
738
739#else /* CONFIG_PCMCIA_PROBE */
740
741static int pcmcia_setup_isa_irq(struct pcmcia_device *p_dev, int type)
742{
743 return -EINVAL;
744}
745
746void pcmcia_cleanup_irq(struct pcmcia_socket *s)
747{
Dominik Brodowski6f840af2010-03-07 10:51:23 +0100748 s->pcmcia_irq = 0;
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200749 return;
750}
751
752#endif /* CONFIG_PCMCIA_PROBE */
753
754
755/**
756 * pcmcia_setup_irq() - determine IRQ to be used for device
757 * @p_dev - the associated PCMCIA device
758 *
759 * locking note: must be called with ops_mutex locked.
760 */
761int pcmcia_setup_irq(struct pcmcia_device *p_dev)
762{
763 struct pcmcia_socket *s = p_dev->socket;
764
Dominik Brodowskieb141202010-03-07 12:21:16 +0100765 if (p_dev->irq)
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200766 return 0;
767
768 /* already assigned? */
Dominik Brodowski6f840af2010-03-07 10:51:23 +0100769 if (s->pcmcia_irq) {
Dominik Brodowskieb141202010-03-07 12:21:16 +0100770 p_dev->irq = s->pcmcia_irq;
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200771 return 0;
772 }
773
774 /* prefer an exclusive ISA irq */
775 if (!pcmcia_setup_isa_irq(p_dev, 0))
776 return 0;
777
778 /* but accept a shared ISA irq */
779 if (!pcmcia_setup_isa_irq(p_dev, IRQF_SHARED))
780 return 0;
781
782 /* but use the PCI irq otherwise */
783 if (s->pci_irq) {
Dominik Brodowskieb141202010-03-07 12:21:16 +0100784 p_dev->irq = s->pcmcia_irq = s->pci_irq;
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200785 return 0;
786 }
787
788 return -EINVAL;
789}
790
791
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700792/** pcmcia_request_window
793 *
794 * Request_window() establishes a mapping between card memory space
795 * and system memory space.
796 */
Dominik Brodowski6838b032009-11-03 01:31:52 +0100797int pcmcia_request_window(struct pcmcia_device *p_dev, win_req_t *req, window_handle_t *wh)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700798{
Dominik Brodowski6838b032009-11-03 01:31:52 +0100799 struct pcmcia_socket *s = p_dev->socket;
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100800 pccard_mem_map *win;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700801 u_long align;
Dominik Brodowski0ca724d2010-07-24 19:03:02 +0200802 struct resource *res;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700803 int w;
804
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200805 if (!(s->state & SOCKET_PRESENT)) {
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200806 dev_dbg(&p_dev->dev, "No card present\n");
Dominik Brodowski3939c1ef2008-08-03 11:10:56 +0200807 return -ENODEV;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200808 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700809
810 /* Window size defaults to smallest available */
811 if (req->Size == 0)
812 req->Size = s->map_size;
Dominik Brodowski0ca724d2010-07-24 19:03:02 +0200813 align = (s->features & SS_CAP_MEM_ALIGN) ? req->Size : s->map_size;
Dominik Brodowski69ba4432008-08-03 12:10:53 +0200814 if (req->Size & (s->map_size-1)) {
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200815 dev_dbg(&p_dev->dev, "invalid map size\n");
Dominik Brodowski69ba4432008-08-03 12:10:53 +0200816 return -EINVAL;
817 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700818 if ((req->Base && (s->features & SS_CAP_STATIC_MAP)) ||
Dominik Brodowski69ba4432008-08-03 12:10:53 +0200819 (req->Base & (align-1))) {
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200820 dev_dbg(&p_dev->dev, "invalid base address\n");
Dominik Brodowski69ba4432008-08-03 12:10:53 +0200821 return -EINVAL;
822 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700823 if (req->Base)
824 align = 0;
825
826 /* Allocate system memory window */
Dominik Brodowski0ca724d2010-07-24 19:03:02 +0200827 mutex_lock(&s->ops_mutex);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700828 for (w = 0; w < MAX_WIN; w++)
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100829 if (!(s->state & SOCKET_WIN_REQ(w)))
830 break;
Dominik Brodowskif9580952008-08-03 11:40:19 +0200831 if (w == MAX_WIN) {
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200832 dev_dbg(&p_dev->dev, "all windows are used already\n");
Dominik Brodowski0ca724d2010-07-24 19:03:02 +0200833 mutex_unlock(&s->ops_mutex);
Dominik Brodowskif9580952008-08-03 11:40:19 +0200834 return -EINVAL;
835 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700836
837 win = &s->win[w];
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700838
839 if (!(s->features & SS_CAP_STATIC_MAP)) {
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100840 win->res = pcmcia_find_mem_region(req->Base, req->Size, align,
Dominik Brodowski0ca724d2010-07-24 19:03:02 +0200841 0, s);
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100842 if (!win->res) {
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200843 dev_dbg(&p_dev->dev, "allocating mem region failed\n");
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100844 mutex_unlock(&s->ops_mutex);
Dominik Brodowskif9580952008-08-03 11:40:19 +0200845 return -EINVAL;
846 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700847 }
Dominik Brodowski6838b032009-11-03 01:31:52 +0100848 p_dev->_win |= CLIENT_WIN_REQ(w);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700849
850 /* Configure the socket controller */
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100851 win->map = w+1;
Dominik Brodowski0ca724d2010-07-24 19:03:02 +0200852 win->flags = req->Attributes;
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100853 win->speed = req->AccessSpeed;
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100854 win->card_start = 0;
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100855
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100856 if (s->ops->set_mem_map(s, win) != 0) {
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200857 dev_dbg(&p_dev->dev, "failed to set memory mapping\n");
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100858 mutex_unlock(&s->ops_mutex);
Dominik Brodowski926c5402008-08-03 12:15:11 +0200859 return -EIO;
860 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700861 s->state |= SOCKET_WIN_REQ(w);
862
863 /* Return window handle */
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100864 if (s->features & SS_CAP_STATIC_MAP)
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100865 req->Base = win->static_start;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100866 else
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100867 req->Base = win->res->start;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100868
Dominik Brodowski0ca724d2010-07-24 19:03:02 +0200869 /* convert to new-style resources */
870 res = p_dev->resource[w + MAX_IO_WIN];
871 res->start = req->Base;
872 res->end = req->Base + req->Size - 1;
873 res->flags &= ~IORESOURCE_BITS;
874 res->flags |= (req->Attributes & WIN_FLAGS_MAP) | (win->map << 2);
Dominik Brodowskiad0c7be2010-07-25 13:10:22 +0200875 res->flags |= IORESOURCE_MEM;
876 res->parent = win->res;
877 if (win->res)
878 request_resource(&iomem_resource, res);
879
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200880 dev_dbg(&p_dev->dev, "request_window results in %pR\n", res);
Dominik Brodowski0ca724d2010-07-24 19:03:02 +0200881
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100882 mutex_unlock(&s->ops_mutex);
Dominik Brodowski0ca724d2010-07-24 19:03:02 +0200883 *wh = res;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700884
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200885 return 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700886} /* pcmcia_request_window */
887EXPORT_SYMBOL(pcmcia_request_window);
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100888
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100889void pcmcia_disable_device(struct pcmcia_device *p_dev)
890{
Dominik Brodowski0ca724d2010-07-24 19:03:02 +0200891 int i;
892 for (i = 0; i < MAX_WIN; i++) {
893 struct resource *res = p_dev->resource[MAX_IO_WIN + i];
894 if (res->flags & WIN_FLAGS_REQ)
895 pcmcia_release_window(p_dev, res);
896 }
897
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100898 pcmcia_release_configuration(p_dev);
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200899 pcmcia_release_io(p_dev);
Patrick McHardy418c5272010-07-20 15:21:42 -0700900 if (p_dev->_irq) {
Dominik Brodowskieb141202010-03-07 12:21:16 +0100901 free_irq(p_dev->irq, p_dev->priv);
Patrick McHardy418c5272010-07-20 15:21:42 -0700902 p_dev->_irq = 0;
903 }
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100904}
905EXPORT_SYMBOL(pcmcia_disable_device);