blob: a1fb0dc1a71fbc6e19e4a9e091bfdfbde611d371 [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 Brodowski7feabb62010-07-29 18:35:47 +0200171 addr = (p_dev->config_base + 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
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200207int pcmcia_map_mem_page(struct pcmcia_device *p_dev, struct resource *res,
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 unsigned int w;
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100212 int ret;
Magnus Damm868575d2006-12-13 19:46:43 +0900213
Dominik Brodowski0ca724d2010-07-24 19:03:02 +0200214 w = ((res->flags & IORESOURCE_BITS & WIN_FLAGS_REQ) >> 2) - 1;
215 if (w >= MAX_WIN)
Dominik Brodowskiffb8da22008-08-03 11:18:00 +0200216 return -EINVAL;
Dominik Brodowskib5cb2592010-07-24 18:46:42 +0200217
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100218 mutex_lock(&s->ops_mutex);
Dominik Brodowski0ca724d2010-07-24 19:03:02 +0200219 s->win[w].card_start = offset;
220 ret = s->ops->set_mem_map(s, &s->win[w]);
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100221 if (ret)
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200222 dev_warn(&p_dev->dev, "failed to set_mem_map\n");
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100223 mutex_unlock(&s->ops_mutex);
224 return ret;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700225} /* pcmcia_map_mem_page */
226EXPORT_SYMBOL(pcmcia_map_mem_page);
227
228
Dominik Brodowskifb49fa52010-07-29 14:06:42 +0200229/**
230 * pcmcia_fixup_iowidth() - reduce io width to 8bit
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700231 *
Dominik Brodowskifb49fa52010-07-29 14:06:42 +0200232 * pcmcia_fixup_iowidth() allows a PCMCIA device driver to reduce the
233 * IO width to 8bit after having called pcmcia_request_configuration()
234 * previously.
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700235 */
Dominik Brodowskifb49fa52010-07-29 14:06:42 +0200236int pcmcia_fixup_iowidth(struct pcmcia_device *p_dev)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700237{
Dominik Brodowskifb49fa52010-07-29 14:06:42 +0200238 struct pcmcia_socket *s = p_dev->socket;
239 pccard_io_map io_off = { 0, 0, 0, 0, 1 };
240 pccard_io_map io_on;
241 int i, ret = 0;
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100242
243 mutex_lock(&s->ops_mutex);
Dominik Brodowskidbb22f02006-01-10 20:41:27 +0100244
Dominik Brodowskifb49fa52010-07-29 14:06:42 +0200245 dev_dbg(&p_dev->dev, "fixup iowidth to 8bit\n");
246
247 if (!(s->state & SOCKET_PRESENT) ||
248 !(p_dev->function_config->state & CONFIG_LOCKED)) {
249 dev_dbg(&p_dev->dev, "No card? Config not locked?\n");
Jiri Slaby4e06e242010-03-16 15:57:44 +0100250 ret = -EACCES;
251 goto unlock;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200252 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700253
Dominik Brodowskifb49fa52010-07-29 14:06:42 +0200254 io_on.speed = io_speed;
255 for (i = 0; i < MAX_IO_WIN; i++) {
256 if (!s->io[i].res)
257 continue;
258 io_off.map = i;
259 io_on.map = i;
260
261 io_on.flags = MAP_ACTIVE | IO_DATA_PATH_WIDTH_8;
262 io_on.start = s->io[i].res->start;
263 io_on.stop = s->io[i].res->end;
264
265 s->ops->set_io_map(s, &io_off);
266 mdelay(40);
267 s->ops->set_io_map(s, &io_on);
Dominik Brodowskid8b0a492008-08-03 10:56:13 +0200268 }
Jiri Slaby4e06e242010-03-16 15:57:44 +0100269unlock:
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100270 mutex_unlock(&s->ops_mutex);
Dominik Brodowski4bbed522006-01-15 11:18:12 +0100271
Jiri Slaby4e06e242010-03-16 15:57:44 +0100272 return ret;
Dominik Brodowskifb49fa52010-07-29 14:06:42 +0200273}
274EXPORT_SYMBOL(pcmcia_fixup_iowidth);
275
276
277/**
278 * pcmcia_fixup_vpp() - set Vpp to a new voltage level
279 *
280 * pcmcia_fixup_vpp() allows a PCMCIA device driver to set Vpp to
281 * a new voltage level between calls to pcmcia_request_configuration()
282 * and pcmcia_disable_device().
283 */
284int pcmcia_fixup_vpp(struct pcmcia_device *p_dev, unsigned char new_vpp)
285{
286 struct pcmcia_socket *s = p_dev->socket;
287 int ret = 0;
288
289 mutex_lock(&s->ops_mutex);
290
291 dev_dbg(&p_dev->dev, "fixup Vpp to %d\n", new_vpp);
292
293 if (!(s->state & SOCKET_PRESENT) ||
294 !(p_dev->function_config->state & CONFIG_LOCKED)) {
295 dev_dbg(&p_dev->dev, "No card? Config not locked?\n");
296 ret = -EACCES;
297 goto unlock;
298 }
299
300 s->socket.Vpp = new_vpp;
301 if (s->ops->set_socket(s, &s->socket)) {
302 dev_warn(&p_dev->dev, "Unable to set VPP\n");
303 ret = -EIO;
304 goto unlock;
305 }
Dominik Brodowskie8405f02010-07-29 15:50:55 +0200306 p_dev->vpp = new_vpp;
Dominik Brodowskifb49fa52010-07-29 14:06:42 +0200307
308unlock:
309 mutex_unlock(&s->ops_mutex);
310
311 return ret;
312}
313EXPORT_SYMBOL(pcmcia_fixup_vpp);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700314
315
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700316int pcmcia_release_configuration(struct pcmcia_device *p_dev)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700317{
318 pccard_io_map io = { 0, 0, 0, 0, 1 };
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700319 struct pcmcia_socket *s = p_dev->socket;
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100320 config_t *c;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700321 int i;
322
Dominik Brodowski9e86749c2010-01-16 00:26:33 +0100323 mutex_lock(&s->ops_mutex);
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100324 c = p_dev->function_config;
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100325 if (p_dev->_locked) {
326 p_dev->_locked = 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700327 if (--(s->lock_count) == 0) {
328 s->socket.flags = SS_OUTPUT_ENA; /* Is this correct? */
329 s->socket.Vpp = 0;
330 s->socket.io_irq = 0;
331 s->ops->set_socket(s, &s->socket);
332 }
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100333 }
334 if (c->state & CONFIG_LOCKED) {
335 c->state &= ~CONFIG_LOCKED;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700336 if (c->state & CONFIG_IO_REQ)
337 for (i = 0; i < MAX_IO_WIN; i++) {
Dominik Brodowskic7d00692006-01-15 08:04:43 +0100338 if (!s->io[i].res)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700339 continue;
340 s->io[i].Config--;
341 if (s->io[i].Config != 0)
342 continue;
343 io.map = i;
344 s->ops->set_io_map(s, &io);
345 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700346 }
Dominik Brodowski9e86749c2010-01-16 00:26:33 +0100347 mutex_unlock(&s->ops_mutex);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700348
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200349 return 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700350} /* pcmcia_release_configuration */
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700351
352
353/** pcmcia_release_io
354 *
355 * Release_io() releases the I/O ranges allocated by a client. This
356 * may be invoked some time after a card ejection has already dumped
357 * the actual socket configuration, so if the client is "stale", we
358 * don't bother checking the port ranges against the current socket
359 * values.
360 */
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200361static int pcmcia_release_io(struct pcmcia_device *p_dev)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700362{
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700363 struct pcmcia_socket *s = p_dev->socket;
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100364 int ret = -EINVAL;
365 config_t *c;
366
367 mutex_lock(&s->ops_mutex);
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100368 if (!p_dev->_io)
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100369 goto out;
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100370
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200371 c = p_dev->function_config;
372
373 release_io_space(s, &c->io[0]);
374
375 if (c->io[1].end)
376 release_io_space(s, &c->io[1]);
377
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100378 p_dev->_io = 0;
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100379 c->state &= ~CONFIG_IO_REQ;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700380
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100381out:
382 mutex_unlock(&s->ops_mutex);
383
384 return ret;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700385} /* pcmcia_release_io */
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700386
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200387/**
388 * pcmcia_release_window() - release reserved iomem for PCMCIA devices
389 *
390 * pcmcia_release_window() releases struct resource *res which was
391 * previously reserved by calling pcmcia_request_window().
392 */
Dominik Brodowski0ca724d2010-07-24 19:03:02 +0200393int pcmcia_release_window(struct pcmcia_device *p_dev, struct resource *res)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700394{
Magnus Damm0bdf9b32006-12-13 19:46:53 +0900395 struct pcmcia_socket *s = p_dev->socket;
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100396 pccard_mem_map *win;
Dominik Brodowski0ca724d2010-07-24 19:03:02 +0200397 unsigned int w;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700398
Dominik Brodowski0ca724d2010-07-24 19:03:02 +0200399 dev_dbg(&p_dev->dev, "releasing window %pR\n", res);
400
401 w = ((res->flags & IORESOURCE_BITS & WIN_FLAGS_REQ) >> 2) - 1;
402 if (w >= MAX_WIN)
Dominik Brodowskiffb8da22008-08-03 11:18:00 +0200403 return -EINVAL;
Magnus Damm0bdf9b32006-12-13 19:46:53 +0900404
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100405 mutex_lock(&s->ops_mutex);
Dominik Brodowski0ca724d2010-07-24 19:03:02 +0200406 win = &s->win[w];
Magnus Damm0bdf9b32006-12-13 19:46:53 +0900407
Dominik Brodowski0ca724d2010-07-24 19:03:02 +0200408 if (!(p_dev->_win & CLIENT_WIN_REQ(w))) {
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200409 dev_dbg(&p_dev->dev, "not releasing unknown window\n");
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100410 mutex_unlock(&s->ops_mutex);
Dominik Brodowskiffb8da22008-08-03 11:18:00 +0200411 return -EINVAL;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200412 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700413
414 /* Shut down memory window */
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100415 win->flags &= ~MAP_ACTIVE;
416 s->ops->set_mem_map(s, win);
Dominik Brodowski0ca724d2010-07-24 19:03:02 +0200417 s->state &= ~SOCKET_WIN_REQ(w);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700418
419 /* Release system memory */
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100420 if (win->res) {
Dominik Brodowskiad0c7be2010-07-25 13:10:22 +0200421 release_resource(res);
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100422 release_resource(win->res);
423 kfree(win->res);
424 win->res = NULL;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700425 }
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200426 res->start = res->end = 0;
427 res->flags = IORESOURCE_MEM;
Dominik Brodowski0ca724d2010-07-24 19:03:02 +0200428 p_dev->_win &= ~CLIENT_WIN_REQ(w);
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100429 mutex_unlock(&s->ops_mutex);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700430
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200431 return 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700432} /* pcmcia_release_window */
433EXPORT_SYMBOL(pcmcia_release_window);
434
435
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700436int pcmcia_request_configuration(struct pcmcia_device *p_dev,
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700437 config_req_t *req)
438{
439 int i;
440 u_int base;
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700441 struct pcmcia_socket *s = p_dev->socket;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700442 config_t *c;
443 pccard_io_map iomap;
Dominik Brodowskifc301102010-07-29 16:19:39 +0200444 unsigned char status = 0;
445 unsigned char ext_status = 0;
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200446 unsigned char option = 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700447
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700448 if (!(s->state & SOCKET_PRESENT))
Joe Perchesd598de02009-06-28 09:26:41 -0700449 return -ENODEV;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700450
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100451 mutex_lock(&s->ops_mutex);
Dominik Brodowskidbb22f02006-01-10 20:41:27 +0100452 c = p_dev->function_config;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200453 if (c->state & CONFIG_LOCKED) {
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100454 mutex_unlock(&s->ops_mutex);
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200455 dev_dbg(&p_dev->dev, "Configuration is locked\n");
Dominik Brodowski943f70f2008-08-03 11:23:42 +0200456 return -EACCES;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200457 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700458
459 /* Do power control. We don't allow changes in Vcc. */
Dominik Brodowskie8405f02010-07-29 15:50:55 +0200460 s->socket.Vpp = p_dev->vpp;
Dominik Brodowskid8b0a492008-08-03 10:56:13 +0200461 if (s->ops->set_socket(s, &s->socket)) {
Dominik Brodowski9e86749c2010-01-16 00:26:33 +0100462 mutex_unlock(&s->ops_mutex);
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200463 dev_printk(KERN_WARNING, &p_dev->dev,
Dominik Brodowskid8b0a492008-08-03 10:56:13 +0200464 "Unable to set socket state\n");
465 return -EINVAL;
466 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700467
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700468 /* Pick memory or I/O card, DMA mode, interrupt */
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700469 c->Attributes = req->Attributes;
Dominik Brodowski37979e12010-07-29 16:33:23 +0200470 if (p_dev->_io)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700471 s->socket.flags |= SS_IOCARD;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700472 if (req->Attributes & CONF_ENABLE_DMA)
473 s->socket.flags |= SS_DMA_MODE;
Dominik Brodowskifc301102010-07-29 16:19:39 +0200474 if (req->Attributes & CONF_ENABLE_SPKR) {
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700475 s->socket.flags |= SS_SPKR_ENA;
Dominik Brodowskifc301102010-07-29 16:19:39 +0200476 status = CCSR_AUDIO_ENA;
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200477 if (!(p_dev->config_regs & PRESENT_STATUS))
Dominik Brodowskifc301102010-07-29 16:19:39 +0200478 dev_warn(&p_dev->dev, "speaker requested, but "
479 "PRESENT_STATUS not set!\n");
480 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700481 if (req->Attributes & CONF_ENABLE_IRQ)
Dominik Brodowski6f840af2010-03-07 10:51:23 +0100482 s->socket.io_irq = s->pcmcia_irq;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700483 else
484 s->socket.io_irq = 0;
Dominik Brodowskifc301102010-07-29 16:19:39 +0200485 if (req->Attributes & CONF_ENABLE_ESR) {
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200486 p_dev->config_regs |= PRESENT_EXT_STATUS;
Dominik Brodowskifc301102010-07-29 16:19:39 +0200487 ext_status = ESR_REQ_ATTN_ENA;
488 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700489 s->ops->set_socket(s, &s->socket);
490 s->lock_count++;
491
492 /* Set up CIS configuration registers */
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200493 base = p_dev->config_base;
494 if (p_dev->config_regs & PRESENT_COPY) {
Dominik Brodowski1a4a0462010-07-29 15:54:54 +0200495 u16 tmp = 0;
496 dev_dbg(&p_dev->dev, "clearing CISREG_SCR\n");
497 pcmcia_write_cis_mem(s, 1, (base + CISREG_SCR)>>1, 1, &tmp);
498 }
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200499 if (p_dev->config_regs & PRESENT_PIN_REPLACE) {
Dominik Brodowski1a4a0462010-07-29 15:54:54 +0200500 u16 tmp = 0;
501 dev_dbg(&p_dev->dev, "clearing CISREG_PRR\n");
502 pcmcia_write_cis_mem(s, 1, (base + CISREG_PRR)>>1, 1, &tmp);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700503 }
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200504 if (p_dev->config_regs & PRESENT_OPTION) {
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700505 if (s->functions == 1) {
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200506 option = p_dev->config_index & COR_CONFIG_MASK;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700507 } else {
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200508 option = p_dev->config_index & COR_MFC_CONFIG_MASK;
509 option |= COR_FUNC_ENA|COR_IREQ_ENA;
510 if (p_dev->config_regs & PRESENT_IOBASE_0)
511 option |= COR_ADDR_DECODE;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700512 }
Dominik Brodowskia7debe72010-03-07 10:58:29 +0100513 if ((req->Attributes & CONF_ENABLE_IRQ) &&
514 !(req->Attributes & CONF_ENABLE_PULSE_IRQ))
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200515 option |= COR_LEVEL_REQ;
516 pcmcia_write_cis_mem(s, 1, (base + CISREG_COR)>>1, 1, &option);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700517 mdelay(40);
518 }
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200519 if (p_dev->config_regs & PRESENT_STATUS)
Dominik Brodowskifc301102010-07-29 16:19:39 +0200520 pcmcia_write_cis_mem(s, 1, (base + CISREG_CCSR)>>1, 1, &status);
521
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200522 if (p_dev->config_regs & PRESENT_EXT_STATUS)
Dominik Brodowskifc301102010-07-29 16:19:39 +0200523 pcmcia_write_cis_mem(s, 1, (base + CISREG_ESR)>>1, 1,
524 &ext_status);
525
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200526 if (p_dev->config_regs & PRESENT_IOBASE_0) {
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200527 u8 b = c->io[0].start & 0xff;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700528 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_0)>>1, 1, &b);
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200529 b = (c->io[0].start >> 8) & 0xff;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700530 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_1)>>1, 1, &b);
531 }
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200532 if (p_dev->config_regs & PRESENT_IOSIZE) {
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200533 u8 b = resource_size(&c->io[0]) + resource_size(&c->io[1]) - 1;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700534 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOSIZE)>>1, 1, &b);
535 }
536
537 /* Configure I/O windows */
538 if (c->state & CONFIG_IO_REQ) {
539 iomap.speed = io_speed;
540 for (i = 0; i < MAX_IO_WIN; i++)
Dominik Brodowskic7d00692006-01-15 08:04:43 +0100541 if (s->io[i].res) {
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700542 iomap.map = i;
543 iomap.flags = MAP_ACTIVE;
Dominik Brodowskic7d00692006-01-15 08:04:43 +0100544 switch (s->io[i].res->flags & IO_DATA_PATH_WIDTH) {
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700545 case IO_DATA_PATH_WIDTH_16:
546 iomap.flags |= MAP_16BIT; break;
547 case IO_DATA_PATH_WIDTH_AUTO:
548 iomap.flags |= MAP_AUTOSZ; break;
549 default:
550 break;
551 }
Dominik Brodowskic7d00692006-01-15 08:04:43 +0100552 iomap.start = s->io[i].res->start;
553 iomap.stop = s->io[i].res->end;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700554 s->ops->set_io_map(s, &iomap);
555 s->io[i].Config++;
556 }
557 }
558
559 c->state |= CONFIG_LOCKED;
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100560 p_dev->_locked = 1;
Dominik Brodowski059f6672010-03-30 18:07:50 +0200561 mutex_unlock(&s->ops_mutex);
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200562 return 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700563} /* pcmcia_request_configuration */
564EXPORT_SYMBOL(pcmcia_request_configuration);
565
566
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200567/**
568 * pcmcia_request_io() - attempt to reserve port ranges for PCMCIA devices
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700569 *
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200570 * pcmcia_request_io() attepts to reserve the IO port ranges specified in
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200571 * &struct pcmcia_device @p_dev->resource[0] and @p_dev->resource[1]. The
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200572 * "start" value is the requested start of the IO port resource; "end"
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200573 * reflects the number of ports requested. The number of IO lines requested
574 * is specified in &struct pcmcia_device @p_dev->io_lines.
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700575 */
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200576int pcmcia_request_io(struct pcmcia_device *p_dev)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700577{
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700578 struct pcmcia_socket *s = p_dev->socket;
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200579 config_t *c = p_dev->function_config;
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100580 int ret = -EINVAL;
581
582 mutex_lock(&s->ops_mutex);
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200583 dev_dbg(&p_dev->dev, "pcmcia_request_io: %pR , %pR",
584 &c->io[0], &c->io[1]);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700585
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200586 if (!(s->state & SOCKET_PRESENT)) {
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200587 dev_dbg(&p_dev->dev, "pcmcia_request_io: No card present\n");
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100588 goto out;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200589 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700590
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200591 if (c->state & CONFIG_LOCKED) {
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200592 dev_dbg(&p_dev->dev, "Configuration is locked\n");
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100593 goto out;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200594 }
Dominik Brodowskif9580952008-08-03 11:40:19 +0200595 if (c->state & CONFIG_IO_REQ) {
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200596 dev_dbg(&p_dev->dev, "IO already configured\n");
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100597 goto out;
Dominik Brodowskif9580952008-08-03 11:40:19 +0200598 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700599
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200600 ret = alloc_io_space(s, &c->io[0], p_dev->io_lines);
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200601 if (ret)
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100602 goto out;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700603
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200604 if (c->io[1].end) {
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200605 ret = alloc_io_space(s, &c->io[1], p_dev->io_lines);
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100606 if (ret) {
Dominik Brodowski7cdffc82010-09-18 10:19:13 +0200607 struct resource tmp = c->io[0];
608 /* release the previously allocated resource */
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200609 release_io_space(s, &c->io[0]);
Dominik Brodowski7cdffc82010-09-18 10:19:13 +0200610 /* but preserve the settings, for they worked... */
611 c->io[0].end = resource_size(&tmp);
612 c->io[0].start = tmp.start;
613 c->io[0].flags = tmp.flags;
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100614 goto out;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700615 }
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200616 } else
617 c->io[1].start = 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700618
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700619 c->state |= CONFIG_IO_REQ;
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100620 p_dev->_io = 1;
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100621
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200622 dev_dbg(&p_dev->dev, "pcmcia_request_io succeeded: %pR , %pR",
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200623 &c->io[0], &c->io[1]);
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100624out:
625 mutex_unlock(&s->ops_mutex);
626
627 return ret;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700628} /* pcmcia_request_io */
629EXPORT_SYMBOL(pcmcia_request_io);
630
631
Dominik Brodowskieb141202010-03-07 12:21:16 +0100632/**
633 * pcmcia_request_irq() - attempt to request a IRQ for a PCMCIA device
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700634 *
Dominik Brodowskieb141202010-03-07 12:21:16 +0100635 * pcmcia_request_irq() is a wrapper around request_irq which will allow
636 * the PCMCIA core to clean up the registration in pcmcia_disable_device().
637 * Drivers are free to use request_irq() directly, but then they need to
638 * call free_irq themselfves, too. Also, only IRQF_SHARED capable IRQ
639 * handlers are allowed.
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700640 */
Dominik Brodowskieb141202010-03-07 12:21:16 +0100641int __must_check pcmcia_request_irq(struct pcmcia_device *p_dev,
642 irq_handler_t handler)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700643{
Dominik Brodowskieb141202010-03-07 12:21:16 +0100644 int ret;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700645
Dominik Brodowskieb141202010-03-07 12:21:16 +0100646 if (!p_dev->irq)
647 return -EINVAL;
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100648
Dominik Brodowskieb141202010-03-07 12:21:16 +0100649 ret = request_irq(p_dev->irq, handler, IRQF_SHARED,
650 p_dev->devname, p_dev->priv);
651 if (!ret)
652 p_dev->_irq = 1;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700653
Dominik Brodowskieb141202010-03-07 12:21:16 +0100654 return ret;
655}
656EXPORT_SYMBOL(pcmcia_request_irq);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700657
Dominik Brodowskieb141202010-03-07 12:21:16 +0100658
659/**
660 * pcmcia_request_exclusive_irq() - attempt to request an exclusive IRQ first
661 *
662 * pcmcia_request_exclusive_irq() is a wrapper around request_irq which
663 * attempts first to request an exclusive IRQ. If it fails, it also accepts
664 * a shared IRQ, but prints out a warning. PCMCIA drivers should allow for
665 * IRQ sharing and either use request_irq directly (then they need to call
666 * free_irq themselves, too), or the pcmcia_request_irq() function.
667 */
668int __must_check
Dominik Brodowskib19a7272010-03-20 13:10:47 +0100669__pcmcia_request_exclusive_irq(struct pcmcia_device *p_dev,
670 irq_handler_t handler)
Dominik Brodowskieb141202010-03-07 12:21:16 +0100671{
672 int ret;
673
674 if (!p_dev->irq)
675 return -EINVAL;
676
677 ret = request_irq(p_dev->irq, handler, 0, p_dev->devname, p_dev->priv);
678 if (ret) {
679 ret = pcmcia_request_irq(p_dev, handler);
680 dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: "
681 "request for exclusive IRQ could not be fulfilled.\n");
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200682 dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: the driver "
683 "needs updating to supported shared IRQ lines.\n");
684 }
Dominik Brodowskieb141202010-03-07 12:21:16 +0100685 if (ret)
686 dev_printk(KERN_INFO, &p_dev->dev, "request_irq() failed\n");
687 else
688 p_dev->_irq = 1;
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200689
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100690 return ret;
Dominik Brodowskieb141202010-03-07 12:21:16 +0100691} /* pcmcia_request_exclusive_irq */
Dominik Brodowskib19a7272010-03-20 13:10:47 +0100692EXPORT_SYMBOL(__pcmcia_request_exclusive_irq);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700693
694
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200695#ifdef CONFIG_PCMCIA_PROBE
696
697/* mask of IRQs already reserved by other cards, we should avoid using them */
Dominik Brodowski127c03c2010-08-03 09:33:45 +0200698static u8 pcmcia_used_irq[32];
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200699
700static irqreturn_t test_action(int cpl, void *dev_id)
701{
702 return IRQ_NONE;
703}
704
705/**
706 * pcmcia_setup_isa_irq() - determine whether an ISA IRQ can be used
707 * @p_dev - the associated PCMCIA device
708 *
709 * locking note: must be called with ops_mutex locked.
710 */
711static int pcmcia_setup_isa_irq(struct pcmcia_device *p_dev, int type)
712{
713 struct pcmcia_socket *s = p_dev->socket;
714 unsigned int try, irq;
715 u32 mask = s->irq_mask;
716 int ret = -ENODEV;
717
718 for (try = 0; try < 64; try++) {
719 irq = try % 32;
720
Dominik Brodowski127c03c2010-08-03 09:33:45 +0200721 if (irq > NR_IRQS)
722 continue;
723
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200724 /* marked as available by driver, not blocked by userspace? */
725 if (!((mask >> irq) & 1))
726 continue;
727
728 /* avoid an IRQ which is already used by another PCMCIA card */
729 if ((try < 32) && pcmcia_used_irq[irq])
730 continue;
731
732 /* register the correct driver, if possible, to check whether
733 * registering a dummy handle works, i.e. if the IRQ isn't
734 * marked as used by the kernel resource management core */
735 ret = request_irq(irq, test_action, type, p_dev->devname,
736 p_dev);
737 if (!ret) {
738 free_irq(irq, p_dev);
Dominik Brodowskieb141202010-03-07 12:21:16 +0100739 p_dev->irq = s->pcmcia_irq = irq;
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200740 pcmcia_used_irq[irq]++;
741 break;
742 }
743 }
744
745 return ret;
746}
747
748void pcmcia_cleanup_irq(struct pcmcia_socket *s)
749{
Dominik Brodowski6f840af2010-03-07 10:51:23 +0100750 pcmcia_used_irq[s->pcmcia_irq]--;
751 s->pcmcia_irq = 0;
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200752}
753
754#else /* CONFIG_PCMCIA_PROBE */
755
756static int pcmcia_setup_isa_irq(struct pcmcia_device *p_dev, int type)
757{
758 return -EINVAL;
759}
760
761void pcmcia_cleanup_irq(struct pcmcia_socket *s)
762{
Dominik Brodowski6f840af2010-03-07 10:51:23 +0100763 s->pcmcia_irq = 0;
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200764 return;
765}
766
767#endif /* CONFIG_PCMCIA_PROBE */
768
769
770/**
771 * pcmcia_setup_irq() - determine IRQ to be used for device
772 * @p_dev - the associated PCMCIA device
773 *
774 * locking note: must be called with ops_mutex locked.
775 */
776int pcmcia_setup_irq(struct pcmcia_device *p_dev)
777{
778 struct pcmcia_socket *s = p_dev->socket;
779
Dominik Brodowskieb141202010-03-07 12:21:16 +0100780 if (p_dev->irq)
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200781 return 0;
782
783 /* already assigned? */
Dominik Brodowski6f840af2010-03-07 10:51:23 +0100784 if (s->pcmcia_irq) {
Dominik Brodowskieb141202010-03-07 12:21:16 +0100785 p_dev->irq = s->pcmcia_irq;
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200786 return 0;
787 }
788
789 /* prefer an exclusive ISA irq */
790 if (!pcmcia_setup_isa_irq(p_dev, 0))
791 return 0;
792
793 /* but accept a shared ISA irq */
794 if (!pcmcia_setup_isa_irq(p_dev, IRQF_SHARED))
795 return 0;
796
797 /* but use the PCI irq otherwise */
798 if (s->pci_irq) {
Dominik Brodowskieb141202010-03-07 12:21:16 +0100799 p_dev->irq = s->pcmcia_irq = s->pci_irq;
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200800 return 0;
801 }
802
803 return -EINVAL;
804}
805
806
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200807/**
808 * pcmcia_request_window() - attempt to reserve iomem for PCMCIA devices
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700809 *
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200810 * pcmcia_request_window() attepts to reserve an iomem ranges specified in
811 * struct resource *res pointing to one of the entries in
812 * struct pcmcia_device *p_dev->resource[2..5]. The "start" value is the
813 * requested start of the IO mem resource; "end" reflects the size
814 * requested.
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700815 */
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200816int pcmcia_request_window(struct pcmcia_device *p_dev, struct resource *res,
817 unsigned int speed)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700818{
Dominik Brodowski6838b032009-11-03 01:31:52 +0100819 struct pcmcia_socket *s = p_dev->socket;
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100820 pccard_mem_map *win;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700821 u_long align;
822 int w;
823
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200824 if (!(s->state & SOCKET_PRESENT)) {
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200825 dev_dbg(&p_dev->dev, "No card present\n");
Dominik Brodowski3939c1ef2008-08-03 11:10:56 +0200826 return -ENODEV;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200827 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700828
829 /* Window size defaults to smallest available */
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200830 if (res->end == 0)
831 res->end = s->map_size;
832 align = (s->features & SS_CAP_MEM_ALIGN) ? res->end : s->map_size;
833 if (res->end & (s->map_size-1)) {
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200834 dev_dbg(&p_dev->dev, "invalid map size\n");
Dominik Brodowski69ba4432008-08-03 12:10:53 +0200835 return -EINVAL;
836 }
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200837 if ((res->start && (s->features & SS_CAP_STATIC_MAP)) ||
838 (res->start & (align-1))) {
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200839 dev_dbg(&p_dev->dev, "invalid base address\n");
Dominik Brodowski69ba4432008-08-03 12:10:53 +0200840 return -EINVAL;
841 }
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200842 if (res->start)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700843 align = 0;
844
845 /* Allocate system memory window */
Dominik Brodowski0ca724d2010-07-24 19:03:02 +0200846 mutex_lock(&s->ops_mutex);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700847 for (w = 0; w < MAX_WIN; w++)
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100848 if (!(s->state & SOCKET_WIN_REQ(w)))
849 break;
Dominik Brodowskif9580952008-08-03 11:40:19 +0200850 if (w == MAX_WIN) {
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200851 dev_dbg(&p_dev->dev, "all windows are used already\n");
Dominik Brodowski0ca724d2010-07-24 19:03:02 +0200852 mutex_unlock(&s->ops_mutex);
Dominik Brodowskif9580952008-08-03 11:40:19 +0200853 return -EINVAL;
854 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700855
856 win = &s->win[w];
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700857
858 if (!(s->features & SS_CAP_STATIC_MAP)) {
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200859 win->res = pcmcia_find_mem_region(res->start, res->end, align,
Dominik Brodowski0ca724d2010-07-24 19:03:02 +0200860 0, s);
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100861 if (!win->res) {
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200862 dev_dbg(&p_dev->dev, "allocating mem region failed\n");
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100863 mutex_unlock(&s->ops_mutex);
Dominik Brodowskif9580952008-08-03 11:40:19 +0200864 return -EINVAL;
865 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700866 }
Dominik Brodowski6838b032009-11-03 01:31:52 +0100867 p_dev->_win |= CLIENT_WIN_REQ(w);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700868
869 /* Configure the socket controller */
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100870 win->map = w+1;
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200871 win->flags = res->flags & WIN_FLAGS_MAP;
872 win->speed = speed;
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100873 win->card_start = 0;
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100874
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100875 if (s->ops->set_mem_map(s, win) != 0) {
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200876 dev_dbg(&p_dev->dev, "failed to set memory mapping\n");
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100877 mutex_unlock(&s->ops_mutex);
Dominik Brodowski926c5402008-08-03 12:15:11 +0200878 return -EIO;
879 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700880 s->state |= SOCKET_WIN_REQ(w);
881
882 /* Return window handle */
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100883 if (s->features & SS_CAP_STATIC_MAP)
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200884 res->start = win->static_start;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100885 else
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200886 res->start = win->res->start;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100887
Dominik Brodowski0ca724d2010-07-24 19:03:02 +0200888 /* convert to new-style resources */
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200889 res->end += res->start - 1;
890 res->flags &= ~WIN_FLAGS_REQ;
891 res->flags |= (win->map << 2) | IORESOURCE_MEM;
Dominik Brodowskiad0c7be2010-07-25 13:10:22 +0200892 res->parent = win->res;
893 if (win->res)
894 request_resource(&iomem_resource, res);
895
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200896 dev_dbg(&p_dev->dev, "request_window results in %pR\n", res);
Dominik Brodowski0ca724d2010-07-24 19:03:02 +0200897
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100898 mutex_unlock(&s->ops_mutex);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700899
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200900 return 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700901} /* pcmcia_request_window */
902EXPORT_SYMBOL(pcmcia_request_window);
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100903
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100904void pcmcia_disable_device(struct pcmcia_device *p_dev)
905{
Dominik Brodowski0ca724d2010-07-24 19:03:02 +0200906 int i;
907 for (i = 0; i < MAX_WIN; i++) {
908 struct resource *res = p_dev->resource[MAX_IO_WIN + i];
909 if (res->flags & WIN_FLAGS_REQ)
910 pcmcia_release_window(p_dev, res);
911 }
912
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100913 pcmcia_release_configuration(p_dev);
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200914 pcmcia_release_io(p_dev);
Patrick McHardy418c5272010-07-20 15:21:42 -0700915 if (p_dev->_irq) {
Dominik Brodowskieb141202010-03-07 12:21:16 +0100916 free_irq(p_dev->irq, p_dev->priv);
Patrick McHardy418c5272010-07-20 15:21:42 -0700917 p_dev->_irq = 0;
918 }
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100919}
920EXPORT_SYMBOL(pcmcia_disable_device);