blob: 975baaa8168b451c1a94f41173dd1f41028746a7 [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 Brodowski1a8d4662005-06-27 16:28:53 -070059/** alloc_io_space
60 *
61 * Special stuff for managing IO windows, because they are scarce
62 */
Dominik Brodowski2ce49052010-07-24 13:14:44 +020063static int alloc_io_space(struct pcmcia_socket *s, struct resource *res,
64 unsigned int lines)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070065{
Dominik Brodowskib19a7272010-03-20 13:10:47 +010066 unsigned int align;
Dominik Brodowski2ce49052010-07-24 13:14:44 +020067 unsigned int base = res->start;
68 unsigned int num = res->end;
69 int ret;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070070
Dominik Brodowski2ce49052010-07-24 13:14:44 +020071 res->flags |= IORESOURCE_IO;
72
Dominik Brodowski90abdc32010-07-24 17:23:51 +020073 dev_dbg(&s->dev, "alloc_io_space request for %pR, %d lines\n",
74 res, lines);
Dominik Brodowski2ce49052010-07-24 13:14:44 +020075
76 align = base ? (lines ? 1<<lines : 0) : 1;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070077 if (align && (align < num)) {
Dominik Brodowski2ce49052010-07-24 13:14:44 +020078 if (base) {
79 dev_dbg(&s->dev, "odd IO request\n");
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070080 align = 0;
81 } else
Dominik Brodowski9fea84f2009-12-07 22:11:45 +010082 while (align && (align < num))
83 align <<= 1;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070084 }
Dominik Brodowski2ce49052010-07-24 13:14:44 +020085 if (base & ~(align-1)) {
86 dev_dbg(&s->dev, "odd IO request\n");
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070087 align = 0;
88 }
Dominik Brodowskib19a7272010-03-20 13:10:47 +010089
Dominik Brodowski2ce49052010-07-24 13:14:44 +020090 ret = s->resource_ops->find_io(s, res->flags, &base, num, align);
91 if (ret) {
92 dev_dbg(&s->dev, "alloc_io_space request returned %d", ret);
93 return -EINVAL;
94 }
95
96 res->start = base;
97 res->end = res->start + num - 1;
98 dev_dbg(&s->dev, "alloc_io_space request returned %pR, %d\n", res, ret);
99 return 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700100} /* alloc_io_space */
101
102
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200103static void release_io_space(struct pcmcia_socket *s, struct resource *res)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700104{
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200105 resource_size_t num = resource_size(res);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700106 int i;
107
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200108 dev_dbg(&s->dev, "release_io_space for %pR\n", res);
109
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700110 for (i = 0; i < MAX_IO_WIN; i++) {
Dominik Brodowskic7d00692006-01-15 08:04:43 +0100111 if (!s->io[i].res)
112 continue;
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200113 if ((s->io[i].res->start <= res->start) &&
114 (s->io[i].res->end >= res->end)) {
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700115 s->io[i].InUse -= num;
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200116 res->start = res->end = 0;
117 res->flags = IORESOURCE_IO;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700118 /* Free the window if no one else is using it */
119 if (s->io[i].InUse == 0) {
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700120 release_resource(s->io[i].res);
121 kfree(s->io[i].res);
122 s->io[i].res = NULL;
123 }
124 }
125 }
126} /* release_io_space */
127
128
Dominik Brodowski1d5cc192010-07-24 12:23:21 +0200129/**
130 * pcmcia_access_config() - read or write card configuration registers
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700131 *
Dominik Brodowski1d5cc192010-07-24 12:23:21 +0200132 * pcmcia_access_config() reads and writes configuration registers in
133 * attribute memory. Memory window 0 is reserved for this and the tuple
134 * reading services. Drivers must use pcmcia_read_config_byte() or
135 * pcmcia_write_config_byte().
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700136 */
Dominik Brodowski1d5cc192010-07-24 12:23:21 +0200137static int pcmcia_access_config(struct pcmcia_device *p_dev,
138 off_t where, u8 *val,
139 int (*accessf) (struct pcmcia_socket *s,
140 int attr, unsigned int addr,
141 unsigned int len, void *ptr))
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700142{
Dominik Brodowski855cdf12006-01-10 20:48:59 +0100143 struct pcmcia_socket *s;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700144 config_t *c;
145 int addr;
Dominik Brodowski059f6672010-03-30 18:07:50 +0200146 int ret = 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700147
Dominik Brodowski855cdf12006-01-10 20:48:59 +0100148 s = p_dev->socket;
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100149
150 mutex_lock(&s->ops_mutex);
Dominik Brodowski855cdf12006-01-10 20:48:59 +0100151 c = p_dev->function_config;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700152
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200153 if (!(c->state & CONFIG_LOCKED)) {
154 dev_dbg(&s->dev, "Configuration isnt't locked\n");
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100155 mutex_unlock(&s->ops_mutex);
Dominik Brodowski943f70f2008-08-03 11:23:42 +0200156 return -EACCES;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200157 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700158
Dominik Brodowski1d5cc192010-07-24 12:23:21 +0200159 addr = (c->ConfigBase + where) >> 1;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700160
Dominik Brodowski1d5cc192010-07-24 12:23:21 +0200161 ret = accessf(s, 1, addr, 1, val);
162
Dominik Brodowski059f6672010-03-30 18:07:50 +0200163 mutex_unlock(&s->ops_mutex);
Dominik Brodowski1d5cc192010-07-24 12:23:21 +0200164
Dominik Brodowski059f6672010-03-30 18:07:50 +0200165 return ret;
Dominik Brodowski1d5cc192010-07-24 12:23:21 +0200166} /* pcmcia_access_config */
167
168
169/**
170 * pcmcia_read_config_byte() - read a byte from a card configuration register
171 *
172 * pcmcia_read_config_byte() reads a byte from a configuration register in
173 * attribute memory.
174 */
175int pcmcia_read_config_byte(struct pcmcia_device *p_dev, off_t where, u8 *val)
176{
177 return pcmcia_access_config(p_dev, where, val, pcmcia_read_cis_mem);
178}
179EXPORT_SYMBOL(pcmcia_read_config_byte);
180
181
182/**
183 * pcmcia_write_config_byte() - write a byte to a card configuration register
184 *
185 * pcmcia_write_config_byte() writes a byte to a configuration register in
186 * attribute memory.
187 */
188int pcmcia_write_config_byte(struct pcmcia_device *p_dev, off_t where, u8 val)
189{
190 return pcmcia_access_config(p_dev, where, &val, pcmcia_write_cis_mem);
191}
192EXPORT_SYMBOL(pcmcia_write_config_byte);
Dominik Brodowski34481392005-06-27 16:28:54 -0700193
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700194
Magnus Damm868575d2006-12-13 19:46:43 +0900195int pcmcia_map_mem_page(struct pcmcia_device *p_dev, window_handle_t wh,
Dominik Brodowskib5cb2592010-07-24 18:46:42 +0200196 unsigned int offset)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700197{
Magnus Damm0bdf9b32006-12-13 19:46:53 +0900198 struct pcmcia_socket *s = p_dev->socket;
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100199 int ret;
Magnus Damm868575d2006-12-13 19:46:43 +0900200
Magnus Damm0bdf9b32006-12-13 19:46:53 +0900201 wh--;
202 if (wh >= MAX_WIN)
Dominik Brodowskiffb8da22008-08-03 11:18:00 +0200203 return -EINVAL;
Dominik Brodowskib5cb2592010-07-24 18:46:42 +0200204
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100205 mutex_lock(&s->ops_mutex);
Dominik Brodowskib5cb2592010-07-24 18:46:42 +0200206 s->win[wh].card_start = offset;
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100207 ret = s->ops->set_mem_map(s, &s->win[wh]);
208 if (ret)
209 dev_warn(&s->dev, "failed to set_mem_map\n");
210 mutex_unlock(&s->ops_mutex);
211 return ret;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700212} /* pcmcia_map_mem_page */
213EXPORT_SYMBOL(pcmcia_map_mem_page);
214
215
216/** pcmcia_modify_configuration
217 *
218 * Modify a locked socket configuration
219 */
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700220int pcmcia_modify_configuration(struct pcmcia_device *p_dev,
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700221 modconf_t *mod)
222{
223 struct pcmcia_socket *s;
224 config_t *c;
Jiri Slaby4e06e242010-03-16 15:57:44 +0100225 int ret;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700226
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700227 s = p_dev->socket;
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100228
229 mutex_lock(&s->ops_mutex);
Dominik Brodowskidbb22f02006-01-10 20:41:27 +0100230 c = p_dev->function_config;
231
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200232 if (!(s->state & SOCKET_PRESENT)) {
233 dev_dbg(&s->dev, "No card present\n");
Jiri Slaby4e06e242010-03-16 15:57:44 +0100234 ret = -ENODEV;
235 goto unlock;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200236 }
237 if (!(c->state & CONFIG_LOCKED)) {
238 dev_dbg(&s->dev, "Configuration isnt't locked\n");
Jiri Slaby4e06e242010-03-16 15:57:44 +0100239 ret = -EACCES;
240 goto unlock;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200241 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700242
Dominik Brodowski0cb3c492010-03-06 20:42:35 +0100243 if (mod->Attributes & (CONF_IRQ_CHANGE_VALID | CONF_VCC_CHANGE_VALID)) {
244 dev_dbg(&s->dev,
245 "changing Vcc or IRQ is not allowed at this time\n");
Jiri Slaby4e06e242010-03-16 15:57:44 +0100246 ret = -EINVAL;
247 goto unlock;
Dominik Brodowskid8b0a492008-08-03 10:56:13 +0200248 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700249
250 /* We only allow changing Vpp1 and Vpp2 to the same value */
251 if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) &&
252 (mod->Attributes & CONF_VPP2_CHANGE_VALID)) {
Ilpo Järvinen60df3de82008-10-30 13:02:54 +0200253 if (mod->Vpp1 != mod->Vpp2) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200254 dev_dbg(&s->dev, "Vpp1 and Vpp2 must be the same\n");
Jiri Slaby4e06e242010-03-16 15:57:44 +0100255 ret = -EINVAL;
256 goto unlock;
Ilpo Järvinen60df3de82008-10-30 13:02:54 +0200257 }
Dominik Brodowski71ed90d2005-09-09 13:03:27 -0700258 s->socket.Vpp = mod->Vpp1;
Dominik Brodowskid8b0a492008-08-03 10:56:13 +0200259 if (s->ops->set_socket(s, &s->socket)) {
260 dev_printk(KERN_WARNING, &s->dev,
261 "Unable to set VPP\n");
Jiri Slaby4e06e242010-03-16 15:57:44 +0100262 ret = -EIO;
263 goto unlock;
Dominik Brodowskid8b0a492008-08-03 10:56:13 +0200264 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700265 } else if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) ||
Dominik Brodowskid8b0a492008-08-03 10:56:13 +0200266 (mod->Attributes & CONF_VPP2_CHANGE_VALID)) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200267 dev_dbg(&s->dev, "changing Vcc is not allowed at this time\n");
Jiri Slaby4e06e242010-03-16 15:57:44 +0100268 ret = -EINVAL;
269 goto unlock;
Dominik Brodowskid8b0a492008-08-03 10:56:13 +0200270 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700271
Dominik Brodowski4bbed522006-01-15 11:18:12 +0100272 if (mod->Attributes & CONF_IO_CHANGE_WIDTH) {
273 pccard_io_map io_off = { 0, 0, 0, 0, 1 };
274 pccard_io_map io_on;
275 int i;
276
277 io_on.speed = io_speed;
278 for (i = 0; i < MAX_IO_WIN; i++) {
279 if (!s->io[i].res)
280 continue;
281 io_off.map = i;
282 io_on.map = i;
283
284 io_on.flags = MAP_ACTIVE | IO_DATA_PATH_WIDTH_8;
285 io_on.start = s->io[i].res->start;
286 io_on.stop = s->io[i].res->end;
287
288 s->ops->set_io_map(s, &io_off);
289 mdelay(40);
290 s->ops->set_io_map(s, &io_on);
291 }
292 }
Jiri Slaby4e06e242010-03-16 15:57:44 +0100293 ret = 0;
294unlock:
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100295 mutex_unlock(&s->ops_mutex);
Dominik Brodowski4bbed522006-01-15 11:18:12 +0100296
Jiri Slaby4e06e242010-03-16 15:57:44 +0100297 return ret;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700298} /* modify_configuration */
299EXPORT_SYMBOL(pcmcia_modify_configuration);
300
301
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700302int pcmcia_release_configuration(struct pcmcia_device *p_dev)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700303{
304 pccard_io_map io = { 0, 0, 0, 0, 1 };
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700305 struct pcmcia_socket *s = p_dev->socket;
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100306 config_t *c;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700307 int i;
308
Dominik Brodowski9e86749c2010-01-16 00:26:33 +0100309 mutex_lock(&s->ops_mutex);
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100310 c = p_dev->function_config;
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100311 if (p_dev->_locked) {
312 p_dev->_locked = 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700313 if (--(s->lock_count) == 0) {
314 s->socket.flags = SS_OUTPUT_ENA; /* Is this correct? */
315 s->socket.Vpp = 0;
316 s->socket.io_irq = 0;
317 s->ops->set_socket(s, &s->socket);
318 }
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100319 }
320 if (c->state & CONFIG_LOCKED) {
321 c->state &= ~CONFIG_LOCKED;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700322 if (c->state & CONFIG_IO_REQ)
323 for (i = 0; i < MAX_IO_WIN; i++) {
Dominik Brodowskic7d00692006-01-15 08:04:43 +0100324 if (!s->io[i].res)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700325 continue;
326 s->io[i].Config--;
327 if (s->io[i].Config != 0)
328 continue;
329 io.map = i;
330 s->ops->set_io_map(s, &io);
331 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700332 }
Dominik Brodowski9e86749c2010-01-16 00:26:33 +0100333 mutex_unlock(&s->ops_mutex);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700334
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200335 return 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700336} /* pcmcia_release_configuration */
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700337
338
339/** pcmcia_release_io
340 *
341 * Release_io() releases the I/O ranges allocated by a client. This
342 * may be invoked some time after a card ejection has already dumped
343 * the actual socket configuration, so if the client is "stale", we
344 * don't bother checking the port ranges against the current socket
345 * values.
346 */
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200347static int pcmcia_release_io(struct pcmcia_device *p_dev)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700348{
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700349 struct pcmcia_socket *s = p_dev->socket;
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100350 int ret = -EINVAL;
351 config_t *c;
352
353 mutex_lock(&s->ops_mutex);
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100354 if (!p_dev->_io)
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100355 goto out;
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100356
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200357 c = p_dev->function_config;
358
359 release_io_space(s, &c->io[0]);
360
361 if (c->io[1].end)
362 release_io_space(s, &c->io[1]);
363
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100364 p_dev->_io = 0;
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100365 c->state &= ~CONFIG_IO_REQ;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700366
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100367out:
368 mutex_unlock(&s->ops_mutex);
369
370 return ret;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700371} /* pcmcia_release_io */
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700372
373
Magnus Dammf5560da2006-12-13 19:46:38 +0900374int pcmcia_release_window(struct pcmcia_device *p_dev, window_handle_t wh)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700375{
Magnus Damm0bdf9b32006-12-13 19:46:53 +0900376 struct pcmcia_socket *s = p_dev->socket;
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100377 pccard_mem_map *win;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700378
Magnus Damm0bdf9b32006-12-13 19:46:53 +0900379 wh--;
380 if (wh >= MAX_WIN)
Dominik Brodowskiffb8da22008-08-03 11:18:00 +0200381 return -EINVAL;
Magnus Damm0bdf9b32006-12-13 19:46:53 +0900382
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100383 mutex_lock(&s->ops_mutex);
Magnus Damm0bdf9b32006-12-13 19:46:53 +0900384 win = &s->win[wh];
385
386 if (!(p_dev->_win & CLIENT_WIN_REQ(wh))) {
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200387 dev_dbg(&s->dev, "not releasing unknown window\n");
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100388 mutex_unlock(&s->ops_mutex);
Dominik Brodowskiffb8da22008-08-03 11:18:00 +0200389 return -EINVAL;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200390 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700391
392 /* Shut down memory window */
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100393 win->flags &= ~MAP_ACTIVE;
394 s->ops->set_mem_map(s, win);
Magnus Damm0bdf9b32006-12-13 19:46:53 +0900395 s->state &= ~SOCKET_WIN_REQ(wh);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700396
397 /* Release system memory */
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100398 if (win->res) {
399 release_resource(win->res);
400 kfree(win->res);
401 win->res = NULL;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700402 }
Magnus Damm0bdf9b32006-12-13 19:46:53 +0900403 p_dev->_win &= ~CLIENT_WIN_REQ(wh);
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100404 mutex_unlock(&s->ops_mutex);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700405
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200406 return 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700407} /* pcmcia_release_window */
408EXPORT_SYMBOL(pcmcia_release_window);
409
410
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700411int pcmcia_request_configuration(struct pcmcia_device *p_dev,
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700412 config_req_t *req)
413{
414 int i;
415 u_int base;
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700416 struct pcmcia_socket *s = p_dev->socket;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700417 config_t *c;
418 pccard_io_map iomap;
419
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700420 if (!(s->state & SOCKET_PRESENT))
Joe Perchesd598de02009-06-28 09:26:41 -0700421 return -ENODEV;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700422
Dominik Brodowskide6405e2008-08-03 10:47:59 +0200423 if (req->IntType & INT_CARDBUS) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200424 dev_dbg(&s->dev, "IntType may not be INT_CARDBUS\n");
Dominik Brodowskide6405e2008-08-03 10:47:59 +0200425 return -EINVAL;
426 }
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100427
428 mutex_lock(&s->ops_mutex);
Dominik Brodowskidbb22f02006-01-10 20:41:27 +0100429 c = p_dev->function_config;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200430 if (c->state & CONFIG_LOCKED) {
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100431 mutex_unlock(&s->ops_mutex);
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200432 dev_dbg(&s->dev, "Configuration is locked\n");
Dominik Brodowski943f70f2008-08-03 11:23:42 +0200433 return -EACCES;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200434 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700435
436 /* Do power control. We don't allow changes in Vcc. */
Dominik Brodowski70294b42006-01-15 12:43:16 +0100437 s->socket.Vpp = req->Vpp;
Dominik Brodowskid8b0a492008-08-03 10:56:13 +0200438 if (s->ops->set_socket(s, &s->socket)) {
Dominik Brodowski9e86749c2010-01-16 00:26:33 +0100439 mutex_unlock(&s->ops_mutex);
Dominik Brodowskid8b0a492008-08-03 10:56:13 +0200440 dev_printk(KERN_WARNING, &s->dev,
441 "Unable to set socket state\n");
442 return -EINVAL;
443 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700444
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700445 /* Pick memory or I/O card, DMA mode, interrupt */
446 c->IntType = req->IntType;
447 c->Attributes = req->Attributes;
448 if (req->IntType & INT_MEMORY_AND_IO)
449 s->socket.flags |= SS_IOCARD;
450 if (req->IntType & INT_ZOOMED_VIDEO)
451 s->socket.flags |= SS_ZVCARD | SS_IOCARD;
452 if (req->Attributes & CONF_ENABLE_DMA)
453 s->socket.flags |= SS_DMA_MODE;
454 if (req->Attributes & CONF_ENABLE_SPKR)
455 s->socket.flags |= SS_SPKR_ENA;
456 if (req->Attributes & CONF_ENABLE_IRQ)
Dominik Brodowski6f840af2010-03-07 10:51:23 +0100457 s->socket.io_irq = s->pcmcia_irq;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700458 else
459 s->socket.io_irq = 0;
460 s->ops->set_socket(s, &s->socket);
461 s->lock_count++;
462
463 /* Set up CIS configuration registers */
464 base = c->ConfigBase = req->ConfigBase;
Dominik Brodowski1ae9c7d2006-01-10 20:40:40 +0100465 c->CardValues = req->Present;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700466 if (req->Present & PRESENT_COPY) {
467 c->Copy = req->Copy;
468 pcmcia_write_cis_mem(s, 1, (base + CISREG_SCR)>>1, 1, &c->Copy);
469 }
470 if (req->Present & PRESENT_OPTION) {
471 if (s->functions == 1) {
472 c->Option = req->ConfigIndex & COR_CONFIG_MASK;
473 } else {
474 c->Option = req->ConfigIndex & COR_MFC_CONFIG_MASK;
475 c->Option |= COR_FUNC_ENA|COR_IREQ_ENA;
476 if (req->Present & PRESENT_IOBASE_0)
477 c->Option |= COR_ADDR_DECODE;
478 }
Dominik Brodowskia7debe72010-03-07 10:58:29 +0100479 if ((req->Attributes & CONF_ENABLE_IRQ) &&
480 !(req->Attributes & CONF_ENABLE_PULSE_IRQ))
481 c->Option |= COR_LEVEL_REQ;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700482 pcmcia_write_cis_mem(s, 1, (base + CISREG_COR)>>1, 1, &c->Option);
483 mdelay(40);
484 }
485 if (req->Present & PRESENT_STATUS) {
486 c->Status = req->Status;
487 pcmcia_write_cis_mem(s, 1, (base + CISREG_CCSR)>>1, 1, &c->Status);
488 }
489 if (req->Present & PRESENT_PIN_REPLACE) {
490 c->Pin = req->Pin;
491 pcmcia_write_cis_mem(s, 1, (base + CISREG_PRR)>>1, 1, &c->Pin);
492 }
493 if (req->Present & PRESENT_EXT_STATUS) {
494 c->ExtStatus = req->ExtStatus;
495 pcmcia_write_cis_mem(s, 1, (base + CISREG_ESR)>>1, 1, &c->ExtStatus);
496 }
497 if (req->Present & PRESENT_IOBASE_0) {
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200498 u8 b = c->io[0].start & 0xff;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700499 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_0)>>1, 1, &b);
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200500 b = (c->io[0].start >> 8) & 0xff;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700501 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_1)>>1, 1, &b);
502 }
503 if (req->Present & PRESENT_IOSIZE) {
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200504 u8 b = resource_size(&c->io[0]) + resource_size(&c->io[1]) - 1;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700505 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOSIZE)>>1, 1, &b);
506 }
507
508 /* Configure I/O windows */
509 if (c->state & CONFIG_IO_REQ) {
510 iomap.speed = io_speed;
511 for (i = 0; i < MAX_IO_WIN; i++)
Dominik Brodowskic7d00692006-01-15 08:04:43 +0100512 if (s->io[i].res) {
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700513 iomap.map = i;
514 iomap.flags = MAP_ACTIVE;
Dominik Brodowskic7d00692006-01-15 08:04:43 +0100515 switch (s->io[i].res->flags & IO_DATA_PATH_WIDTH) {
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700516 case IO_DATA_PATH_WIDTH_16:
517 iomap.flags |= MAP_16BIT; break;
518 case IO_DATA_PATH_WIDTH_AUTO:
519 iomap.flags |= MAP_AUTOSZ; break;
520 default:
521 break;
522 }
Dominik Brodowskic7d00692006-01-15 08:04:43 +0100523 iomap.start = s->io[i].res->start;
524 iomap.stop = s->io[i].res->end;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700525 s->ops->set_io_map(s, &iomap);
526 s->io[i].Config++;
527 }
528 }
529
530 c->state |= CONFIG_LOCKED;
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100531 p_dev->_locked = 1;
Dominik Brodowski059f6672010-03-30 18:07:50 +0200532 mutex_unlock(&s->ops_mutex);
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200533 return 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700534} /* pcmcia_request_configuration */
535EXPORT_SYMBOL(pcmcia_request_configuration);
536
537
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200538/**
539 * pcmcia_request_io() - attempt to reserve port ranges for PCMCIA devices
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700540 *
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200541 * pcmcia_request_io() attepts to reserve the IO port ranges specified in
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200542 * &struct pcmcia_device @p_dev->resource[0] and @p_dev->resource[1]. The
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200543 * "start" value is the requested start of the IO port resource; "end"
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200544 * reflects the number of ports requested. The number of IO lines requested
545 * is specified in &struct pcmcia_device @p_dev->io_lines.
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700546 */
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200547int pcmcia_request_io(struct pcmcia_device *p_dev)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700548{
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700549 struct pcmcia_socket *s = p_dev->socket;
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200550 config_t *c = p_dev->function_config;
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100551 int ret = -EINVAL;
552
553 mutex_lock(&s->ops_mutex);
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200554 dev_dbg(&s->dev, "pcmcia_request_io: %pR , %pR", &c->io[0], &c->io[1]);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700555
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200556 if (!(s->state & SOCKET_PRESENT)) {
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200557 dev_dbg(&s->dev, "pcmcia_request_io: No card present\n");
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100558 goto out;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200559 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700560
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200561 if (c->state & CONFIG_LOCKED) {
562 dev_dbg(&s->dev, "Configuration is locked\n");
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100563 goto out;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200564 }
Dominik Brodowskif9580952008-08-03 11:40:19 +0200565 if (c->state & CONFIG_IO_REQ) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200566 dev_dbg(&s->dev, "IO already configured\n");
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100567 goto out;
Dominik Brodowskif9580952008-08-03 11:40:19 +0200568 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700569
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200570 ret = alloc_io_space(s, &c->io[0], p_dev->io_lines);
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200571 if (ret)
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100572 goto out;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700573
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200574 if (c->io[1].end) {
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200575 ret = alloc_io_space(s, &c->io[1], p_dev->io_lines);
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100576 if (ret) {
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200577 release_io_space(s, &c->io[0]);
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100578 goto out;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700579 }
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200580 } else
581 c->io[1].start = 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700582
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700583 c->state |= CONFIG_IO_REQ;
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100584 p_dev->_io = 1;
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100585
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200586 dev_dbg(&s->dev, "pcmcia_request_io succeeded: %pR , %pR",
587 &c->io[0], &c->io[1]);
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100588out:
589 mutex_unlock(&s->ops_mutex);
590
591 return ret;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700592} /* pcmcia_request_io */
593EXPORT_SYMBOL(pcmcia_request_io);
594
595
Dominik Brodowskieb141202010-03-07 12:21:16 +0100596/**
597 * pcmcia_request_irq() - attempt to request a IRQ for a PCMCIA device
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700598 *
Dominik Brodowskieb141202010-03-07 12:21:16 +0100599 * pcmcia_request_irq() is a wrapper around request_irq which will allow
600 * the PCMCIA core to clean up the registration in pcmcia_disable_device().
601 * Drivers are free to use request_irq() directly, but then they need to
602 * call free_irq themselfves, too. Also, only IRQF_SHARED capable IRQ
603 * handlers are allowed.
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700604 */
Dominik Brodowskieb141202010-03-07 12:21:16 +0100605int __must_check pcmcia_request_irq(struct pcmcia_device *p_dev,
606 irq_handler_t handler)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700607{
Dominik Brodowskieb141202010-03-07 12:21:16 +0100608 int ret;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700609
Dominik Brodowskieb141202010-03-07 12:21:16 +0100610 if (!p_dev->irq)
611 return -EINVAL;
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100612
Dominik Brodowskieb141202010-03-07 12:21:16 +0100613 ret = request_irq(p_dev->irq, handler, IRQF_SHARED,
614 p_dev->devname, p_dev->priv);
615 if (!ret)
616 p_dev->_irq = 1;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700617
Dominik Brodowskieb141202010-03-07 12:21:16 +0100618 return ret;
619}
620EXPORT_SYMBOL(pcmcia_request_irq);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700621
Dominik Brodowskieb141202010-03-07 12:21:16 +0100622
623/**
624 * pcmcia_request_exclusive_irq() - attempt to request an exclusive IRQ first
625 *
626 * pcmcia_request_exclusive_irq() is a wrapper around request_irq which
627 * attempts first to request an exclusive IRQ. If it fails, it also accepts
628 * a shared IRQ, but prints out a warning. PCMCIA drivers should allow for
629 * IRQ sharing and either use request_irq directly (then they need to call
630 * free_irq themselves, too), or the pcmcia_request_irq() function.
631 */
632int __must_check
Dominik Brodowskib19a7272010-03-20 13:10:47 +0100633__pcmcia_request_exclusive_irq(struct pcmcia_device *p_dev,
634 irq_handler_t handler)
Dominik Brodowskieb141202010-03-07 12:21:16 +0100635{
636 int ret;
637
638 if (!p_dev->irq)
639 return -EINVAL;
640
641 ret = request_irq(p_dev->irq, handler, 0, p_dev->devname, p_dev->priv);
642 if (ret) {
643 ret = pcmcia_request_irq(p_dev, handler);
644 dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: "
645 "request for exclusive IRQ could not be fulfilled.\n");
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200646 dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: the driver "
647 "needs updating to supported shared IRQ lines.\n");
648 }
Dominik Brodowskieb141202010-03-07 12:21:16 +0100649 if (ret)
650 dev_printk(KERN_INFO, &p_dev->dev, "request_irq() failed\n");
651 else
652 p_dev->_irq = 1;
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200653
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100654 return ret;
Dominik Brodowskieb141202010-03-07 12:21:16 +0100655} /* pcmcia_request_exclusive_irq */
Dominik Brodowskib19a7272010-03-20 13:10:47 +0100656EXPORT_SYMBOL(__pcmcia_request_exclusive_irq);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700657
658
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200659#ifdef CONFIG_PCMCIA_PROBE
660
661/* mask of IRQs already reserved by other cards, we should avoid using them */
662static u8 pcmcia_used_irq[NR_IRQS];
663
664static irqreturn_t test_action(int cpl, void *dev_id)
665{
666 return IRQ_NONE;
667}
668
669/**
670 * pcmcia_setup_isa_irq() - determine whether an ISA IRQ can be used
671 * @p_dev - the associated PCMCIA device
672 *
673 * locking note: must be called with ops_mutex locked.
674 */
675static int pcmcia_setup_isa_irq(struct pcmcia_device *p_dev, int type)
676{
677 struct pcmcia_socket *s = p_dev->socket;
678 unsigned int try, irq;
679 u32 mask = s->irq_mask;
680 int ret = -ENODEV;
681
682 for (try = 0; try < 64; try++) {
683 irq = try % 32;
684
685 /* marked as available by driver, not blocked by userspace? */
686 if (!((mask >> irq) & 1))
687 continue;
688
689 /* avoid an IRQ which is already used by another PCMCIA card */
690 if ((try < 32) && pcmcia_used_irq[irq])
691 continue;
692
693 /* register the correct driver, if possible, to check whether
694 * registering a dummy handle works, i.e. if the IRQ isn't
695 * marked as used by the kernel resource management core */
696 ret = request_irq(irq, test_action, type, p_dev->devname,
697 p_dev);
698 if (!ret) {
699 free_irq(irq, p_dev);
Dominik Brodowskieb141202010-03-07 12:21:16 +0100700 p_dev->irq = s->pcmcia_irq = irq;
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200701 pcmcia_used_irq[irq]++;
702 break;
703 }
704 }
705
706 return ret;
707}
708
709void pcmcia_cleanup_irq(struct pcmcia_socket *s)
710{
Dominik Brodowski6f840af2010-03-07 10:51:23 +0100711 pcmcia_used_irq[s->pcmcia_irq]--;
712 s->pcmcia_irq = 0;
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200713}
714
715#else /* CONFIG_PCMCIA_PROBE */
716
717static int pcmcia_setup_isa_irq(struct pcmcia_device *p_dev, int type)
718{
719 return -EINVAL;
720}
721
722void pcmcia_cleanup_irq(struct pcmcia_socket *s)
723{
Dominik Brodowski6f840af2010-03-07 10:51:23 +0100724 s->pcmcia_irq = 0;
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200725 return;
726}
727
728#endif /* CONFIG_PCMCIA_PROBE */
729
730
731/**
732 * pcmcia_setup_irq() - determine IRQ to be used for device
733 * @p_dev - the associated PCMCIA device
734 *
735 * locking note: must be called with ops_mutex locked.
736 */
737int pcmcia_setup_irq(struct pcmcia_device *p_dev)
738{
739 struct pcmcia_socket *s = p_dev->socket;
740
Dominik Brodowskieb141202010-03-07 12:21:16 +0100741 if (p_dev->irq)
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200742 return 0;
743
744 /* already assigned? */
Dominik Brodowski6f840af2010-03-07 10:51:23 +0100745 if (s->pcmcia_irq) {
Dominik Brodowskieb141202010-03-07 12:21:16 +0100746 p_dev->irq = s->pcmcia_irq;
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200747 return 0;
748 }
749
750 /* prefer an exclusive ISA irq */
751 if (!pcmcia_setup_isa_irq(p_dev, 0))
752 return 0;
753
754 /* but accept a shared ISA irq */
755 if (!pcmcia_setup_isa_irq(p_dev, IRQF_SHARED))
756 return 0;
757
758 /* but use the PCI irq otherwise */
759 if (s->pci_irq) {
Dominik Brodowskieb141202010-03-07 12:21:16 +0100760 p_dev->irq = s->pcmcia_irq = s->pci_irq;
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200761 return 0;
762 }
763
764 return -EINVAL;
765}
766
767
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700768/** pcmcia_request_window
769 *
770 * Request_window() establishes a mapping between card memory space
771 * and system memory space.
772 */
Dominik Brodowski6838b032009-11-03 01:31:52 +0100773int pcmcia_request_window(struct pcmcia_device *p_dev, win_req_t *req, window_handle_t *wh)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700774{
Dominik Brodowski6838b032009-11-03 01:31:52 +0100775 struct pcmcia_socket *s = p_dev->socket;
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100776 pccard_mem_map *win;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700777 u_long align;
778 int w;
779
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200780 if (!(s->state & SOCKET_PRESENT)) {
781 dev_dbg(&s->dev, "No card present\n");
Dominik Brodowski3939c1ef2008-08-03 11:10:56 +0200782 return -ENODEV;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200783 }
Dominik Brodowski610e2372008-08-03 11:58:53 +0200784 if (req->Attributes & (WIN_PAGED | WIN_SHARED)) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200785 dev_dbg(&s->dev, "bad attribute setting for iomem region\n");
Dominik Brodowski610e2372008-08-03 11:58:53 +0200786 return -EINVAL;
787 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700788
789 /* Window size defaults to smallest available */
790 if (req->Size == 0)
791 req->Size = s->map_size;
792 align = (((s->features & SS_CAP_MEM_ALIGN) ||
793 (req->Attributes & WIN_STRICT_ALIGN)) ?
794 req->Size : s->map_size);
Dominik Brodowski69ba4432008-08-03 12:10:53 +0200795 if (req->Size & (s->map_size-1)) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200796 dev_dbg(&s->dev, "invalid map size\n");
Dominik Brodowski69ba4432008-08-03 12:10:53 +0200797 return -EINVAL;
798 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700799 if ((req->Base && (s->features & SS_CAP_STATIC_MAP)) ||
Dominik Brodowski69ba4432008-08-03 12:10:53 +0200800 (req->Base & (align-1))) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200801 dev_dbg(&s->dev, "invalid base address\n");
Dominik Brodowski69ba4432008-08-03 12:10:53 +0200802 return -EINVAL;
803 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700804 if (req->Base)
805 align = 0;
806
807 /* Allocate system memory window */
808 for (w = 0; w < MAX_WIN; w++)
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100809 if (!(s->state & SOCKET_WIN_REQ(w)))
810 break;
Dominik Brodowskif9580952008-08-03 11:40:19 +0200811 if (w == MAX_WIN) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200812 dev_dbg(&s->dev, "all windows are used already\n");
Dominik Brodowskif9580952008-08-03 11:40:19 +0200813 return -EINVAL;
814 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700815
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100816 mutex_lock(&s->ops_mutex);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700817 win = &s->win[w];
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700818
819 if (!(s->features & SS_CAP_STATIC_MAP)) {
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100820 win->res = pcmcia_find_mem_region(req->Base, req->Size, align,
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700821 (req->Attributes & WIN_MAP_BELOW_1MB), s);
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100822 if (!win->res) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200823 dev_dbg(&s->dev, "allocating mem region failed\n");
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100824 mutex_unlock(&s->ops_mutex);
Dominik Brodowskif9580952008-08-03 11:40:19 +0200825 return -EINVAL;
826 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700827 }
Dominik Brodowski6838b032009-11-03 01:31:52 +0100828 p_dev->_win |= CLIENT_WIN_REQ(w);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700829
830 /* Configure the socket controller */
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100831 win->map = w+1;
832 win->flags = 0;
833 win->speed = req->AccessSpeed;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700834 if (req->Attributes & WIN_MEMORY_TYPE)
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100835 win->flags |= MAP_ATTRIB;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700836 if (req->Attributes & WIN_ENABLE)
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100837 win->flags |= MAP_ACTIVE;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700838 if (req->Attributes & WIN_DATA_WIDTH_16)
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100839 win->flags |= MAP_16BIT;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700840 if (req->Attributes & WIN_USE_WAIT)
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100841 win->flags |= MAP_USE_WAIT;
842 win->card_start = 0;
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100843
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100844 if (s->ops->set_mem_map(s, win) != 0) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200845 dev_dbg(&s->dev, "failed to set memory mapping\n");
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100846 mutex_unlock(&s->ops_mutex);
Dominik Brodowski926c5402008-08-03 12:15:11 +0200847 return -EIO;
848 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700849 s->state |= SOCKET_WIN_REQ(w);
850
851 /* Return window handle */
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100852 if (s->features & SS_CAP_STATIC_MAP)
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100853 req->Base = win->static_start;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100854 else
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100855 req->Base = win->res->start;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100856
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100857 mutex_unlock(&s->ops_mutex);
Magnus Damm0bdf9b32006-12-13 19:46:53 +0900858 *wh = w + 1;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700859
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200860 return 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700861} /* pcmcia_request_window */
862EXPORT_SYMBOL(pcmcia_request_window);
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100863
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100864void pcmcia_disable_device(struct pcmcia_device *p_dev)
865{
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100866 pcmcia_release_configuration(p_dev);
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200867 pcmcia_release_io(p_dev);
Patrick McHardy418c5272010-07-20 15:21:42 -0700868 if (p_dev->_irq) {
Dominik Brodowskieb141202010-03-07 12:21:16 +0100869 free_irq(p_dev->irq, p_dev->priv);
Patrick McHardy418c5272010-07-20 15:21:42 -0700870 p_dev->_irq = 0;
871 }
Dominik Brodowskic1ac0222008-06-14 18:51:06 +0200872 if (p_dev->win)
Magnus Dammf5560da2006-12-13 19:46:38 +0900873 pcmcia_release_window(p_dev, p_dev->win);
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100874}
875EXPORT_SYMBOL(pcmcia_disable_device);