Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / pcmcia / pcmcia_resource.c
CommitLineData
1a8d4663
DB
1/*
2 * PCMCIA 16-bit resource management functions
3 *
4 * The initial developer of the original code is David A. Hinds
5 * <dahinds@users.sourceforge.net>. Portions created by David A. Hinds
6 * are Copyright (C) 1999 David A. Hinds. All Rights Reserved.
7 *
8 * Copyright (C) 1999 David A. Hinds
9485ee14 9 * Copyright (C) 2004-2010 Dominik Brodowski
1a8d4663
DB
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
1a8d4663
DB
17#include <linux/module.h>
18#include <linux/kernel.h>
19#include <linux/interrupt.h>
20#include <linux/delay.h>
21#include <linux/pci.h>
22#include <linux/device.h>
91284224 23#include <linux/netdevice.h>
5a0e3ad6 24#include <linux/slab.h>
1a8d4663 25
6f0f38c4
DB
26#include <asm/irq.h>
27
1a8d4663 28#include <pcmcia/ss.h>
1a8d4663
DB
29#include <pcmcia/cistpl.h>
30#include <pcmcia/cisreg.h>
31#include <pcmcia/ds.h>
32
33#include "cs_internal.h"
1a8d4663
DB
34
35
1a8d4663 36/* Access speed for IO windows */
9fea84f4 37static int io_speed;
1a8d4663
DB
38module_param(io_speed, int, 0444);
39
40
a3ac9af5
DB
41int pcmcia_validate_mem(struct pcmcia_socket *s)
42{
43 if (s->resource_ops->validate_mem)
44 return s->resource_ops->validate_mem(s);
45 /* if there is no callback, we can assume that everything is OK */
46 return 0;
47}
48
49struct resource *pcmcia_find_mem_region(u_long base, u_long num, u_long align,
50 int low, struct pcmcia_socket *s)
51{
52 if (s->resource_ops->find_mem)
53 return s->resource_ops->find_mem(base, num, align, low, s);
54 return NULL;
55}
56
1a8d4663 57
9485ee14
DB
58/**
59 * release_io_space() - release IO ports allocated with alloc_io_space()
60 * @s: pcmcia socket
61 * @res: resource to release
62 *
63 */
ad0c7be2
DB
64static void release_io_space(struct pcmcia_socket *s, struct resource *res)
65{
66 resource_size_t num = resource_size(res);
67 int i;
68
69 dev_dbg(&s->dev, "release_io_space for %pR\n", res);
70
71 for (i = 0; i < MAX_IO_WIN; i++) {
72 if (!s->io[i].res)
73 continue;
74 if ((s->io[i].res->start <= res->start) &&
75 (s->io[i].res->end >= res->end)) {
76 s->io[i].InUse -= num;
77 if (res->parent)
78 release_resource(res);
79 res->start = res->end = 0;
80 res->flags = IORESOURCE_IO;
81 /* Free the window if no one else is using it */
82 if (s->io[i].InUse == 0) {
83 release_resource(s->io[i].res);
84 kfree(s->io[i].res);
85 s->io[i].res = NULL;
86 }
87 }
88 }
9485ee14
DB
89}
90
ad0c7be2 91
9485ee14
DB
92/**
93 * alloc_io_space() - allocate IO ports for use by a PCMCIA device
94 * @s: pcmcia socket
95 * @res: resource to allocate (begin: begin, end: size)
96 * @lines: number of IO lines decoded by the PCMCIA card
1a8d4663
DB
97 *
98 * Special stuff for managing IO windows, because they are scarce
99 */
2ce4905e
DB
100static int alloc_io_space(struct pcmcia_socket *s, struct resource *res,
101 unsigned int lines)
1a8d4663 102{
b19a7275 103 unsigned int align;
2ce4905e
DB
104 unsigned int base = res->start;
105 unsigned int num = res->end;
106 int ret;
107
108 res->flags |= IORESOURCE_IO;
1a8d4663 109
90abdc3b
DB
110 dev_dbg(&s->dev, "alloc_io_space request for %pR, %d lines\n",
111 res, lines);
2ce4905e
DB
112
113 align = base ? (lines ? 1<<lines : 0) : 1;
1a8d4663 114 if (align && (align < num)) {
2ce4905e
DB
115 if (base) {
116 dev_dbg(&s->dev, "odd IO request\n");
1a8d4663
DB
117 align = 0;
118 } else
9fea84f4
DB
119 while (align && (align < num))
120 align <<= 1;
1a8d4663 121 }
2ce4905e
DB
122 if (base & ~(align-1)) {
123 dev_dbg(&s->dev, "odd IO request\n");
1a8d4663
DB
124 align = 0;
125 }
b19a7275 126
ad0c7be2
DB
127 ret = s->resource_ops->find_io(s, res->flags, &base, num, align,
128 &res->parent);
2ce4905e 129 if (ret) {
ad0c7be2 130 dev_dbg(&s->dev, "alloc_io_space request failed (%d)\n", ret);
2ce4905e
DB
131 return -EINVAL;
132 }
133
134 res->start = base;
135 res->end = res->start + num - 1;
1a8d4663 136
ad0c7be2
DB
137 if (res->parent) {
138 ret = request_resource(res->parent, res);
139 if (ret) {
140 dev_warn(&s->dev,
141 "request_resource %pR failed: %d\n", res, ret);
142 res->parent = NULL;
143 release_io_space(s, res);
1a8d4663
DB
144 }
145 }
ad0c7be2
DB
146 dev_dbg(&s->dev, "alloc_io_space request result %d: %pR\n", ret, res);
147 return ret;
9485ee14 148}
1a8d4663
DB
149
150
1d5cc192
DB
151/**
152 * pcmcia_access_config() - read or write card configuration registers
1a8d4663 153 *
1d5cc192
DB
154 * pcmcia_access_config() reads and writes configuration registers in
155 * attribute memory. Memory window 0 is reserved for this and the tuple
156 * reading services. Drivers must use pcmcia_read_config_byte() or
157 * pcmcia_write_config_byte().
1a8d4663 158 */
1d5cc192
DB
159static int pcmcia_access_config(struct pcmcia_device *p_dev,
160 off_t where, u8 *val,
161 int (*accessf) (struct pcmcia_socket *s,
162 int attr, unsigned int addr,
163 unsigned int len, void *ptr))
1a8d4663 164{
855cdf13 165 struct pcmcia_socket *s;
1a8d4663
DB
166 config_t *c;
167 int addr;
059f667d 168 int ret = 0;
1a8d4663 169
855cdf13 170 s = p_dev->socket;
94a819f8
DB
171
172 mutex_lock(&s->ops_mutex);
855cdf13 173 c = p_dev->function_config;
1a8d4663 174
6d9a299f 175 if (!(c->state & CONFIG_LOCKED)) {
e9c54999 176 dev_dbg(&p_dev->dev, "Configuration isn't locked\n");
94a819f8 177 mutex_unlock(&s->ops_mutex);
943f70f1 178 return -EACCES;
6d9a299f 179 }
1a8d4663 180
7feabb64 181 addr = (p_dev->config_base + where) >> 1;
1d5cc192
DB
182
183 ret = accessf(s, 1, addr, 1, val);
184
059f667d 185 mutex_unlock(&s->ops_mutex);
1d5cc192 186
059f667d 187 return ret;
9485ee14 188}
1d5cc192
DB
189
190
191/**
192 * pcmcia_read_config_byte() - read a byte from a card configuration register
193 *
194 * pcmcia_read_config_byte() reads a byte from a configuration register in
195 * attribute memory.
196 */
197int pcmcia_read_config_byte(struct pcmcia_device *p_dev, off_t where, u8 *val)
198{
199 return pcmcia_access_config(p_dev, where, val, pcmcia_read_cis_mem);
200}
201EXPORT_SYMBOL(pcmcia_read_config_byte);
202
203
204/**
205 * pcmcia_write_config_byte() - write a byte to a card configuration register
206 *
207 * pcmcia_write_config_byte() writes a byte to a configuration register in
208 * attribute memory.
209 */
210int pcmcia_write_config_byte(struct pcmcia_device *p_dev, off_t where, u8 val)
211{
212 return pcmcia_access_config(p_dev, where, &val, pcmcia_write_cis_mem);
213}
214EXPORT_SYMBOL(pcmcia_write_config_byte);
3448139b 215
1a8d4663 216
9485ee14
DB
217/**
218 * pcmcia_map_mem_page() - modify iomem window to point to a different offset
219 * @p_dev: pcmcia device
220 * @res: iomem resource already enabled by pcmcia_request_window()
221 * @offset: card_offset to map
222 *
223 * pcmcia_map_mem_page() modifies what can be read and written by accessing
224 * an iomem range previously enabled by pcmcia_request_window(), by setting
225 * the card_offset value to @offset.
226 */
cdb13808 227int pcmcia_map_mem_page(struct pcmcia_device *p_dev, struct resource *res,
b5cb259e 228 unsigned int offset)
1a8d4663 229{
0bdf9b3d 230 struct pcmcia_socket *s = p_dev->socket;
0ca724d3 231 unsigned int w;
6b8e087b 232 int ret;
868575d1 233
0ca724d3
DB
234 w = ((res->flags & IORESOURCE_BITS & WIN_FLAGS_REQ) >> 2) - 1;
235 if (w >= MAX_WIN)
ffb8da20 236 return -EINVAL;
b5cb259e 237
6b8e087b 238 mutex_lock(&s->ops_mutex);
0ca724d3
DB
239 s->win[w].card_start = offset;
240 ret = s->ops->set_mem_map(s, &s->win[w]);
6b8e087b 241 if (ret)
eb838fe1 242 dev_warn(&p_dev->dev, "failed to set_mem_map\n");
6b8e087b
DB
243 mutex_unlock(&s->ops_mutex);
244 return ret;
9485ee14 245}
1a8d4663
DB
246EXPORT_SYMBOL(pcmcia_map_mem_page);
247
248
fb49fa53
DB
249/**
250 * pcmcia_fixup_iowidth() - reduce io width to 8bit
9485ee14 251 * @p_dev: pcmcia device
1a8d4663 252 *
fb49fa53 253 * pcmcia_fixup_iowidth() allows a PCMCIA device driver to reduce the
1ac71e5a 254 * IO width to 8bit after having called pcmcia_enable_device()
fb49fa53 255 * previously.
1a8d4663 256 */
fb49fa53 257int pcmcia_fixup_iowidth(struct pcmcia_device *p_dev)
1a8d4663 258{
fb49fa53
DB
259 struct pcmcia_socket *s = p_dev->socket;
260 pccard_io_map io_off = { 0, 0, 0, 0, 1 };
261 pccard_io_map io_on;
262 int i, ret = 0;
94a819f8
DB
263
264 mutex_lock(&s->ops_mutex);
dbb22f0d 265
fb49fa53
DB
266 dev_dbg(&p_dev->dev, "fixup iowidth to 8bit\n");
267
268 if (!(s->state & SOCKET_PRESENT) ||
269 !(p_dev->function_config->state & CONFIG_LOCKED)) {
270 dev_dbg(&p_dev->dev, "No card? Config not locked?\n");
4e06e240
JS
271 ret = -EACCES;
272 goto unlock;
6d9a299f 273 }
1a8d4663 274
fb49fa53
DB
275 io_on.speed = io_speed;
276 for (i = 0; i < MAX_IO_WIN; i++) {
277 if (!s->io[i].res)
278 continue;
279 io_off.map = i;
280 io_on.map = i;
281
282 io_on.flags = MAP_ACTIVE | IO_DATA_PATH_WIDTH_8;
283 io_on.start = s->io[i].res->start;
284 io_on.stop = s->io[i].res->end;
285
286 s->ops->set_io_map(s, &io_off);
287 mdelay(40);
288 s->ops->set_io_map(s, &io_on);
d8b0a49d 289 }
fb49fa53
DB
290unlock:
291 mutex_unlock(&s->ops_mutex);
1a8d4663 292
fb49fa53
DB
293 return ret;
294}
295EXPORT_SYMBOL(pcmcia_fixup_iowidth);
296
297
298/**
299 * pcmcia_fixup_vpp() - set Vpp to a new voltage level
9485ee14
DB
300 * @p_dev: pcmcia device
301 * @new_vpp: new Vpp voltage
fb49fa53
DB
302 *
303 * pcmcia_fixup_vpp() allows a PCMCIA device driver to set Vpp to
1ac71e5a 304 * a new voltage level between calls to pcmcia_enable_device()
fb49fa53
DB
305 * and pcmcia_disable_device().
306 */
307int pcmcia_fixup_vpp(struct pcmcia_device *p_dev, unsigned char new_vpp)
308{
309 struct pcmcia_socket *s = p_dev->socket;
310 int ret = 0;
311
312 mutex_lock(&s->ops_mutex);
313
314 dev_dbg(&p_dev->dev, "fixup Vpp to %d\n", new_vpp);
315
316 if (!(s->state & SOCKET_PRESENT) ||
317 !(p_dev->function_config->state & CONFIG_LOCKED)) {
318 dev_dbg(&p_dev->dev, "No card? Config not locked?\n");
319 ret = -EACCES;
4e06e240 320 goto unlock;
d8b0a49d 321 }
1a8d4663 322
fb49fa53
DB
323 s->socket.Vpp = new_vpp;
324 if (s->ops->set_socket(s, &s->socket)) {
325 dev_warn(&p_dev->dev, "Unable to set VPP\n");
326 ret = -EIO;
327 goto unlock;
4bbed523 328 }
e8405f0f 329 p_dev->vpp = new_vpp;
fb49fa53 330
4e06e240 331unlock:
94a819f8 332 mutex_unlock(&s->ops_mutex);
4bbed523 333
4e06e240 334 return ret;
fb49fa53
DB
335}
336EXPORT_SYMBOL(pcmcia_fixup_vpp);
1a8d4663
DB
337
338
9485ee14
DB
339/**
340 * pcmcia_release_configuration() - physically disable a PCMCIA device
341 * @p_dev: pcmcia device
342 *
343 * pcmcia_release_configuration() is the 1:1 counterpart to
344 * pcmcia_enable_device(): If a PCMCIA device is no longer used by any
345 * driver, the Vpp voltage is set to 0, IRQs will no longer be generated,
346 * and I/O ranges will be disabled. As pcmcia_release_io() and
347 * pcmcia_release_window() still need to be called, device drivers are
348 * expected to call pcmcia_disable_device() instead.
349 */
2bc5a9bd 350int pcmcia_release_configuration(struct pcmcia_device *p_dev)
1a8d4663
DB
351{
352 pccard_io_map io = { 0, 0, 0, 0, 1 };
2bc5a9bd 353 struct pcmcia_socket *s = p_dev->socket;
94a819f8 354 config_t *c;
1a8d4663
DB
355 int i;
356
9e86749c 357 mutex_lock(&s->ops_mutex);
94a819f8 358 c = p_dev->function_config;
e2d40963
DB
359 if (p_dev->_locked) {
360 p_dev->_locked = 0;
1a8d4663 361 if (--(s->lock_count) == 0) {
9485ee14 362 s->socket.flags = SS_OUTPUT_ENA; /* Is this correct? */
1a8d4663
DB
363 s->socket.Vpp = 0;
364 s->socket.io_irq = 0;
365 s->ops->set_socket(s, &s->socket);
366 }
5f2a71fc
DB
367 }
368 if (c->state & CONFIG_LOCKED) {
369 c->state &= ~CONFIG_LOCKED;
1a8d4663
DB
370 if (c->state & CONFIG_IO_REQ)
371 for (i = 0; i < MAX_IO_WIN; i++) {
c7d00693 372 if (!s->io[i].res)
1a8d4663
DB
373 continue;
374 s->io[i].Config--;
375 if (s->io[i].Config != 0)
376 continue;
377 io.map = i;
378 s->ops->set_io_map(s, &io);
379 }
1a8d4663 380 }
9e86749c 381 mutex_unlock(&s->ops_mutex);
1a8d4663 382
4c89e88b 383 return 0;
9485ee14 384}
1a8d4663
DB
385
386
9485ee14
DB
387/**
388 * pcmcia_release_io() - release I/O allocated by a PCMCIA device
389 * @p_dev: pcmcia device
1a8d4663 390 *
9485ee14
DB
391 * pcmcia_release_io() releases the I/O ranges allocated by a PCMCIA
392 * device. This may be invoked some time after a card ejection has
393 * already dumped the actual socket configuration, so if the client is
394 * "stale", we don't bother checking the port ranges against the
395 * current socket values.
1a8d4663 396 */
2ce4905e 397static int pcmcia_release_io(struct pcmcia_device *p_dev)
1a8d4663 398{
2bc5a9bd 399 struct pcmcia_socket *s = p_dev->socket;
94a819f8
DB
400 int ret = -EINVAL;
401 config_t *c;
402
403 mutex_lock(&s->ops_mutex);
9fea84f4 404 if (!p_dev->_io)
94a819f8 405 goto out;
5f2a71fc 406
2ce4905e 407 c = p_dev->function_config;
1a8d4663 408
2ce4905e 409 release_io_space(s, &c->io[0]);
5f2a71fc 410
2ce4905e
DB
411 if (c->io[1].end)
412 release_io_space(s, &c->io[1]);
1a8d4663 413
2ce4905e
DB
414 p_dev->_io = 0;
415 c->state &= ~CONFIG_IO_REQ;
1a8d4663 416
94a819f8
DB
417out:
418 mutex_unlock(&s->ops_mutex);
419
420 return ret;
1a8d4663 421} /* pcmcia_release_io */
1a8d4663 422
9485ee14 423
cdb13808
DB
424/**
425 * pcmcia_release_window() - release reserved iomem for PCMCIA devices
9485ee14
DB
426 * @p_dev: pcmcia device
427 * @res: iomem resource to release
cdb13808 428 *
9485ee14 429 * pcmcia_release_window() releases &struct resource *res which was
cdb13808
DB
430 * previously reserved by calling pcmcia_request_window().
431 */
0ca724d3 432int pcmcia_release_window(struct pcmcia_device *p_dev, struct resource *res)
1a8d4663 433{
0bdf9b3d 434 struct pcmcia_socket *s = p_dev->socket;
82f88e36 435 pccard_mem_map *win;
0ca724d3 436 unsigned int w;
1a8d4663 437
0ca724d3
DB
438 dev_dbg(&p_dev->dev, "releasing window %pR\n", res);
439
440 w = ((res->flags & IORESOURCE_BITS & WIN_FLAGS_REQ) >> 2) - 1;
441 if (w >= MAX_WIN)
ffb8da20 442 return -EINVAL;
0bdf9b3d 443
6b8e087b 444 mutex_lock(&s->ops_mutex);
0ca724d3 445 win = &s->win[w];
0bdf9b3d 446
0ca724d3 447 if (!(p_dev->_win & CLIENT_WIN_REQ(w))) {
eb838fe1 448 dev_dbg(&p_dev->dev, "not releasing unknown window\n");
6b8e087b 449 mutex_unlock(&s->ops_mutex);
ffb8da20 450 return -EINVAL;
6d9a299f 451 }
1a8d4663
DB
452
453 /* Shut down memory window */
82f88e36
DB
454 win->flags &= ~MAP_ACTIVE;
455 s->ops->set_mem_map(s, win);
0ca724d3 456 s->state &= ~SOCKET_WIN_REQ(w);
1a8d4663
DB
457
458 /* Release system memory */
82f88e36 459 if (win->res) {
ad0c7be2 460 release_resource(res);
82f88e36
DB
461 release_resource(win->res);
462 kfree(win->res);
463 win->res = NULL;
1a8d4663 464 }
cdb13808
DB
465 res->start = res->end = 0;
466 res->flags = IORESOURCE_MEM;
0ca724d3 467 p_dev->_win &= ~CLIENT_WIN_REQ(w);
6b8e087b 468 mutex_unlock(&s->ops_mutex);
1a8d4663 469
4c89e88b 470 return 0;
1a8d4663
DB
471} /* pcmcia_release_window */
472EXPORT_SYMBOL(pcmcia_release_window);
473
9485ee14 474
1ac71e5a
DB
475/**
476 * pcmcia_enable_device() - set up and activate a PCMCIA device
9485ee14 477 * @p_dev: the associated PCMCIA device
1ac71e5a 478 *
9485ee14
DB
479 * pcmcia_enable_device() physically enables a PCMCIA device. It parses
480 * the flags passed to in @flags and stored in @p_dev->flags and sets up
481 * the Vpp voltage, enables the speaker line, I/O ports and store proper
482 * values to configuration registers.
1ac71e5a
DB
483 */
484int pcmcia_enable_device(struct pcmcia_device *p_dev)
1a8d4663
DB
485{
486 int i;
1ac71e5a 487 unsigned int base;
2bc5a9bd 488 struct pcmcia_socket *s = p_dev->socket;
1a8d4663
DB
489 config_t *c;
490 pccard_io_map iomap;
fc301101
DB
491 unsigned char status = 0;
492 unsigned char ext_status = 0;
7feabb64 493 unsigned char option = 0;
1ac71e5a 494 unsigned int flags = p_dev->config_flags;
1a8d4663 495
1a8d4663 496 if (!(s->state & SOCKET_PRESENT))
d598de02 497 return -ENODEV;
1a8d4663 498
94a819f8 499 mutex_lock(&s->ops_mutex);
dbb22f0d 500 c = p_dev->function_config;
6d9a299f 501 if (c->state & CONFIG_LOCKED) {
94a819f8 502 mutex_unlock(&s->ops_mutex);
eb838fe1 503 dev_dbg(&p_dev->dev, "Configuration is locked\n");
943f70f1 504 return -EACCES;
6d9a299f 505 }
1a8d4663
DB
506
507 /* Do power control. We don't allow changes in Vcc. */
e8405f0f 508 s->socket.Vpp = p_dev->vpp;
d8b0a49d 509 if (s->ops->set_socket(s, &s->socket)) {
9e86749c 510 mutex_unlock(&s->ops_mutex);
eb838fe1 511 dev_printk(KERN_WARNING, &p_dev->dev,
d8b0a49d
DB
512 "Unable to set socket state\n");
513 return -EINVAL;
514 }
1a8d4663 515
1a8d4663 516 /* Pick memory or I/O card, DMA mode, interrupt */
ff10fca5
DB
517 if (p_dev->_io || flags & CONF_ENABLE_IRQ)
518 flags |= CONF_ENABLE_IOCARD;
519 if (flags & CONF_ENABLE_IOCARD)
1a8d4663 520 s->socket.flags |= SS_IOCARD;
33619f0d
DB
521 if (flags & CONF_ENABLE_ZVCARD)
522 s->socket.flags |= SS_ZVCARD | SS_IOCARD;
1ac71e5a 523 if (flags & CONF_ENABLE_SPKR) {
1a8d4663 524 s->socket.flags |= SS_SPKR_ENA;
fc301101 525 status = CCSR_AUDIO_ENA;
7feabb64 526 if (!(p_dev->config_regs & PRESENT_STATUS))
fc301101
DB
527 dev_warn(&p_dev->dev, "speaker requested, but "
528 "PRESENT_STATUS not set!\n");
529 }
1ac71e5a 530 if (flags & CONF_ENABLE_IRQ)
6f840afb 531 s->socket.io_irq = s->pcmcia_irq;
1a8d4663
DB
532 else
533 s->socket.io_irq = 0;
1ac71e5a 534 if (flags & CONF_ENABLE_ESR) {
7feabb64 535 p_dev->config_regs |= PRESENT_EXT_STATUS;
fc301101
DB
536 ext_status = ESR_REQ_ATTN_ENA;
537 }
1a8d4663
DB
538 s->ops->set_socket(s, &s->socket);
539 s->lock_count++;
540
1c4a77bf
DB
541 dev_dbg(&p_dev->dev,
542 "enable_device: V %d, flags %x, base %x, regs %x, idx %x\n",
543 p_dev->vpp, flags, p_dev->config_base, p_dev->config_regs,
544 p_dev->config_index);
545
1a8d4663 546 /* Set up CIS configuration registers */
7feabb64
DB
547 base = p_dev->config_base;
548 if (p_dev->config_regs & PRESENT_COPY) {
1a4a0460
DB
549 u16 tmp = 0;
550 dev_dbg(&p_dev->dev, "clearing CISREG_SCR\n");
551 pcmcia_write_cis_mem(s, 1, (base + CISREG_SCR)>>1, 1, &tmp);
552 }
7feabb64 553 if (p_dev->config_regs & PRESENT_PIN_REPLACE) {
1a4a0460
DB
554 u16 tmp = 0;
555 dev_dbg(&p_dev->dev, "clearing CISREG_PRR\n");
556 pcmcia_write_cis_mem(s, 1, (base + CISREG_PRR)>>1, 1, &tmp);
1a8d4663 557 }
7feabb64 558 if (p_dev->config_regs & PRESENT_OPTION) {
1a8d4663 559 if (s->functions == 1) {
7feabb64 560 option = p_dev->config_index & COR_CONFIG_MASK;
1a8d4663 561 } else {
7feabb64
DB
562 option = p_dev->config_index & COR_MFC_CONFIG_MASK;
563 option |= COR_FUNC_ENA|COR_IREQ_ENA;
564 if (p_dev->config_regs & PRESENT_IOBASE_0)
565 option |= COR_ADDR_DECODE;
1a8d4663 566 }
1ac71e5a
DB
567 if ((flags & CONF_ENABLE_IRQ) &&
568 !(flags & CONF_ENABLE_PULSE_IRQ))
7feabb64
DB
569 option |= COR_LEVEL_REQ;
570 pcmcia_write_cis_mem(s, 1, (base + CISREG_COR)>>1, 1, &option);
1a8d4663
DB
571 mdelay(40);
572 }
7feabb64 573 if (p_dev->config_regs & PRESENT_STATUS)
fc301101
DB
574 pcmcia_write_cis_mem(s, 1, (base + CISREG_CCSR)>>1, 1, &status);
575
7feabb64 576 if (p_dev->config_regs & PRESENT_EXT_STATUS)
fc301101
DB
577 pcmcia_write_cis_mem(s, 1, (base + CISREG_ESR)>>1, 1,
578 &ext_status);
579
7feabb64 580 if (p_dev->config_regs & PRESENT_IOBASE_0) {
2ce4905e 581 u8 b = c->io[0].start & 0xff;
1a8d4663 582 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_0)>>1, 1, &b);
2ce4905e 583 b = (c->io[0].start >> 8) & 0xff;
1a8d4663
DB
584 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_1)>>1, 1, &b);
585 }
7feabb64 586 if (p_dev->config_regs & PRESENT_IOSIZE) {
2ce4905e 587 u8 b = resource_size(&c->io[0]) + resource_size(&c->io[1]) - 1;
1a8d4663
DB
588 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOSIZE)>>1, 1, &b);
589 }
590
591 /* Configure I/O windows */
592 if (c->state & CONFIG_IO_REQ) {
593 iomap.speed = io_speed;
594 for (i = 0; i < MAX_IO_WIN; i++)
c7d00693 595 if (s->io[i].res) {
1a8d4663
DB
596 iomap.map = i;
597 iomap.flags = MAP_ACTIVE;
c7d00693 598 switch (s->io[i].res->flags & IO_DATA_PATH_WIDTH) {
1a8d4663
DB
599 case IO_DATA_PATH_WIDTH_16:
600 iomap.flags |= MAP_16BIT; break;
601 case IO_DATA_PATH_WIDTH_AUTO:
602 iomap.flags |= MAP_AUTOSZ; break;
603 default:
604 break;
605 }
c7d00693
DB
606 iomap.start = s->io[i].res->start;
607 iomap.stop = s->io[i].res->end;
1a8d4663
DB
608 s->ops->set_io_map(s, &iomap);
609 s->io[i].Config++;
610 }
611 }
612
613 c->state |= CONFIG_LOCKED;
e2d40963 614 p_dev->_locked = 1;
059f667d 615 mutex_unlock(&s->ops_mutex);
4c89e88b 616 return 0;
1ac71e5a
DB
617} /* pcmcia_enable_device */
618EXPORT_SYMBOL(pcmcia_enable_device);
1a8d4663
DB
619
620
2ce4905e
DB
621/**
622 * pcmcia_request_io() - attempt to reserve port ranges for PCMCIA devices
9485ee14 623 * @p_dev: the associated PCMCIA device
2ce4905e 624 *
9485ee14 625 * pcmcia_request_io() attempts to reserve the IO port ranges specified in
90abdc3b 626 * &struct pcmcia_device @p_dev->resource[0] and @p_dev->resource[1]. The
2ce4905e 627 * "start" value is the requested start of the IO port resource; "end"
90abdc3b
DB
628 * reflects the number of ports requested. The number of IO lines requested
629 * is specified in &struct pcmcia_device @p_dev->io_lines.
1a8d4663 630 */
90abdc3b 631int pcmcia_request_io(struct pcmcia_device *p_dev)
1a8d4663 632{
2bc5a9bd 633 struct pcmcia_socket *s = p_dev->socket;
90abdc3b 634 config_t *c = p_dev->function_config;
94a819f8
DB
635 int ret = -EINVAL;
636
637 mutex_lock(&s->ops_mutex);
eb838fe1
DB
638 dev_dbg(&p_dev->dev, "pcmcia_request_io: %pR , %pR",
639 &c->io[0], &c->io[1]);
1a8d4663 640
6d9a299f 641 if (!(s->state & SOCKET_PRESENT)) {
eb838fe1 642 dev_dbg(&p_dev->dev, "pcmcia_request_io: No card present\n");
94a819f8 643 goto out;
6d9a299f 644 }
1a8d4663 645
6d9a299f 646 if (c->state & CONFIG_LOCKED) {
eb838fe1 647 dev_dbg(&p_dev->dev, "Configuration is locked\n");
94a819f8 648 goto out;
6d9a299f 649 }
f958095e 650 if (c->state & CONFIG_IO_REQ) {
eb838fe1 651 dev_dbg(&p_dev->dev, "IO already configured\n");
94a819f8 652 goto out;
f958095e 653 }
1a8d4663 654
90abdc3b 655 ret = alloc_io_space(s, &c->io[0], p_dev->io_lines);
2ce4905e 656 if (ret)
94a819f8 657 goto out;
1a8d4663 658
2ce4905e 659 if (c->io[1].end) {
90abdc3b 660 ret = alloc_io_space(s, &c->io[1], p_dev->io_lines);
94a819f8 661 if (ret) {
7cdffc86
DB
662 struct resource tmp = c->io[0];
663 /* release the previously allocated resource */
2ce4905e 664 release_io_space(s, &c->io[0]);
7cdffc86
DB
665 /* but preserve the settings, for they worked... */
666 c->io[0].end = resource_size(&tmp);
667 c->io[0].start = tmp.start;
668 c->io[0].flags = tmp.flags;
94a819f8 669 goto out;
1a8d4663 670 }
2ce4905e
DB
671 } else
672 c->io[1].start = 0;
1a8d4663 673
1a8d4663 674 c->state |= CONFIG_IO_REQ;
e2d40963 675 p_dev->_io = 1;
94a819f8 676
eb838fe1 677 dev_dbg(&p_dev->dev, "pcmcia_request_io succeeded: %pR , %pR",
2ce4905e 678 &c->io[0], &c->io[1]);
94a819f8
DB
679out:
680 mutex_unlock(&s->ops_mutex);
681
682 return ret;
1a8d4663
DB
683} /* pcmcia_request_io */
684EXPORT_SYMBOL(pcmcia_request_io);
685
686
eb14120f
DB
687/**
688 * pcmcia_request_irq() - attempt to request a IRQ for a PCMCIA device
9485ee14
DB
689 * @p_dev: the associated PCMCIA device
690 * @handler: IRQ handler to register
1a8d4663 691 *
9485ee14 692 * pcmcia_request_irq() is a wrapper around request_irq() which allows
eb14120f
DB
693 * the PCMCIA core to clean up the registration in pcmcia_disable_device().
694 * Drivers are free to use request_irq() directly, but then they need to
9485ee14 695 * call free_irq() themselfves, too. Also, only %IRQF_SHARED capable IRQ
eb14120f 696 * handlers are allowed.
1a8d4663 697 */
eb14120f
DB
698int __must_check pcmcia_request_irq(struct pcmcia_device *p_dev,
699 irq_handler_t handler)
1a8d4663 700{
eb14120f 701 int ret;
1a8d4663 702
eb14120f
DB
703 if (!p_dev->irq)
704 return -EINVAL;
94a819f8 705
eb14120f
DB
706 ret = request_irq(p_dev->irq, handler, IRQF_SHARED,
707 p_dev->devname, p_dev->priv);
708 if (!ret)
709 p_dev->_irq = 1;
1a8d4663 710
eb14120f
DB
711 return ret;
712}
713EXPORT_SYMBOL(pcmcia_request_irq);
6f0f38c4 714
1a8d4663 715
eb14120f
DB
716/**
717 * pcmcia_request_exclusive_irq() - attempt to request an exclusive IRQ first
9485ee14
DB
718 * @p_dev: the associated PCMCIA device
719 * @handler: IRQ handler to register
eb14120f 720 *
9485ee14 721 * pcmcia_request_exclusive_irq() is a wrapper around request_irq() which
eb14120f
DB
722 * attempts first to request an exclusive IRQ. If it fails, it also accepts
723 * a shared IRQ, but prints out a warning. PCMCIA drivers should allow for
724 * IRQ sharing and either use request_irq directly (then they need to call
9485ee14 725 * free_irq() themselves, too), or the pcmcia_request_irq() function.
eb14120f
DB
726 */
727int __must_check
b19a7275
DB
728__pcmcia_request_exclusive_irq(struct pcmcia_device *p_dev,
729 irq_handler_t handler)
eb14120f
DB
730{
731 int ret;
1a8d4663 732
eb14120f
DB
733 if (!p_dev->irq)
734 return -EINVAL;
1a8d4663 735
eb14120f
DB
736 ret = request_irq(p_dev->irq, handler, 0, p_dev->devname, p_dev->priv);
737 if (ret) {
738 ret = pcmcia_request_irq(p_dev, handler);
739 dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: "
740 "request for exclusive IRQ could not be fulfilled.\n");
741 dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: the driver "
742 "needs updating to supported shared IRQ lines.\n");
743 }
744 if (ret)
745 dev_printk(KERN_INFO, &p_dev->dev, "request_irq() failed\n");
746 else
747 p_dev->_irq = 1;
1a8d4663 748
94a819f8 749 return ret;
eb14120f 750} /* pcmcia_request_exclusive_irq */
b19a7275 751EXPORT_SYMBOL(__pcmcia_request_exclusive_irq);
1a8d4663
DB
752
753
6f0f38c4
DB
754#ifdef CONFIG_PCMCIA_PROBE
755
756/* mask of IRQs already reserved by other cards, we should avoid using them */
127c03cd 757static u8 pcmcia_used_irq[32];
6f0f38c4
DB
758
759static irqreturn_t test_action(int cpl, void *dev_id)
760{
761 return IRQ_NONE;
762}
763
764/**
765 * pcmcia_setup_isa_irq() - determine whether an ISA IRQ can be used
766 * @p_dev - the associated PCMCIA device
767 *
768 * locking note: must be called with ops_mutex locked.
769 */
770static int pcmcia_setup_isa_irq(struct pcmcia_device *p_dev, int type)
771{
772 struct pcmcia_socket *s = p_dev->socket;
773 unsigned int try, irq;
774 u32 mask = s->irq_mask;
775 int ret = -ENODEV;
776
777 for (try = 0; try < 64; try++) {
778 irq = try % 32;
779
127c03cd
DB
780 if (irq > NR_IRQS)
781 continue;
782
6f0f38c4
DB
783 /* marked as available by driver, not blocked by userspace? */
784 if (!((mask >> irq) & 1))
785 continue;
786
787 /* avoid an IRQ which is already used by another PCMCIA card */
788 if ((try < 32) && pcmcia_used_irq[irq])
789 continue;
790
791 /* register the correct driver, if possible, to check whether
792 * registering a dummy handle works, i.e. if the IRQ isn't
793 * marked as used by the kernel resource management core */
794 ret = request_irq(irq, test_action, type, p_dev->devname,
795 p_dev);
796 if (!ret) {
797 free_irq(irq, p_dev);
eb14120f 798 p_dev->irq = s->pcmcia_irq = irq;
6f0f38c4
DB
799 pcmcia_used_irq[irq]++;
800 break;
801 }
802 }
803
804 return ret;
805}
806
807void pcmcia_cleanup_irq(struct pcmcia_socket *s)
808{
6f840afb
DB
809 pcmcia_used_irq[s->pcmcia_irq]--;
810 s->pcmcia_irq = 0;
6f0f38c4
DB
811}
812
813#else /* CONFIG_PCMCIA_PROBE */
814
815static int pcmcia_setup_isa_irq(struct pcmcia_device *p_dev, int type)
816{
817 return -EINVAL;
818}
819
820void pcmcia_cleanup_irq(struct pcmcia_socket *s)
821{
6f840afb 822 s->pcmcia_irq = 0;
6f0f38c4
DB
823 return;
824}
825
826#endif /* CONFIG_PCMCIA_PROBE */
827
828
829/**
830 * pcmcia_setup_irq() - determine IRQ to be used for device
831 * @p_dev - the associated PCMCIA device
832 *
833 * locking note: must be called with ops_mutex locked.
834 */
835int pcmcia_setup_irq(struct pcmcia_device *p_dev)
836{
837 struct pcmcia_socket *s = p_dev->socket;
838
eb14120f 839 if (p_dev->irq)
6f0f38c4
DB
840 return 0;
841
842 /* already assigned? */
6f840afb 843 if (s->pcmcia_irq) {
eb14120f 844 p_dev->irq = s->pcmcia_irq;
6f0f38c4
DB
845 return 0;
846 }
847
848 /* prefer an exclusive ISA irq */
849 if (!pcmcia_setup_isa_irq(p_dev, 0))
850 return 0;
851
852 /* but accept a shared ISA irq */
853 if (!pcmcia_setup_isa_irq(p_dev, IRQF_SHARED))
854 return 0;
855
856 /* but use the PCI irq otherwise */
857 if (s->pci_irq) {
eb14120f 858 p_dev->irq = s->pcmcia_irq = s->pci_irq;
6f0f38c4
DB
859 return 0;
860 }
861
862 return -EINVAL;
863}
864
865
cdb13808
DB
866/**
867 * pcmcia_request_window() - attempt to reserve iomem for PCMCIA devices
9485ee14
DB
868 * @p_dev: the associated PCMCIA device
869 * @res: &struct resource pointing to p_dev->resource[2..5]
870 * @speed: access speed
1a8d4663 871 *
cdb13808 872 * pcmcia_request_window() attepts to reserve an iomem ranges specified in
9485ee14
DB
873 * &struct resource @res pointing to one of the entries in
874 * &struct pcmcia_device @p_dev->resource[2..5]. The "start" value is the
cdb13808
DB
875 * requested start of the IO mem resource; "end" reflects the size
876 * requested.
1a8d4663 877 */
cdb13808
DB
878int pcmcia_request_window(struct pcmcia_device *p_dev, struct resource *res,
879 unsigned int speed)
1a8d4663 880{
6838b03f 881 struct pcmcia_socket *s = p_dev->socket;
82f88e36 882 pccard_mem_map *win;
1a8d4663
DB
883 u_long align;
884 int w;
885
1c4a77bf
DB
886 dev_dbg(&p_dev->dev, "request_window %pR %d\n", res, speed);
887
6d9a299f 888 if (!(s->state & SOCKET_PRESENT)) {
eb838fe1 889 dev_dbg(&p_dev->dev, "No card present\n");
3939c1ef 890 return -ENODEV;
6d9a299f 891 }
1a8d4663
DB
892
893 /* Window size defaults to smallest available */
cdb13808
DB
894 if (res->end == 0)
895 res->end = s->map_size;
896 align = (s->features & SS_CAP_MEM_ALIGN) ? res->end : s->map_size;
897 if (res->end & (s->map_size-1)) {
eb838fe1 898 dev_dbg(&p_dev->dev, "invalid map size\n");
69ba4433
DB
899 return -EINVAL;
900 }
cdb13808
DB
901 if ((res->start && (s->features & SS_CAP_STATIC_MAP)) ||
902 (res->start & (align-1))) {
eb838fe1 903 dev_dbg(&p_dev->dev, "invalid base address\n");
69ba4433
DB
904 return -EINVAL;
905 }
cdb13808 906 if (res->start)
1a8d4663
DB
907 align = 0;
908
909 /* Allocate system memory window */
0ca724d3 910 mutex_lock(&s->ops_mutex);
1a8d4663 911 for (w = 0; w < MAX_WIN; w++)
9fea84f4
DB
912 if (!(s->state & SOCKET_WIN_REQ(w)))
913 break;
f958095e 914 if (w == MAX_WIN) {
eb838fe1 915 dev_dbg(&p_dev->dev, "all windows are used already\n");
0ca724d3 916 mutex_unlock(&s->ops_mutex);
f958095e
DB
917 return -EINVAL;
918 }
1a8d4663
DB
919
920 win = &s->win[w];
1a8d4663
DB
921
922 if (!(s->features & SS_CAP_STATIC_MAP)) {
cdb13808 923 win->res = pcmcia_find_mem_region(res->start, res->end, align,
0ca724d3 924 0, s);
82f88e36 925 if (!win->res) {
eb838fe1 926 dev_dbg(&p_dev->dev, "allocating mem region failed\n");
6b8e087b 927 mutex_unlock(&s->ops_mutex);
f958095e
DB
928 return -EINVAL;
929 }
1a8d4663 930 }
6838b03f 931 p_dev->_win |= CLIENT_WIN_REQ(w);
1a8d4663
DB
932
933 /* Configure the socket controller */
82f88e36 934 win->map = w+1;
cdb13808
DB
935 win->flags = res->flags & WIN_FLAGS_MAP;
936 win->speed = speed;
82f88e36 937 win->card_start = 0;
6b8e087b 938
82f88e36 939 if (s->ops->set_mem_map(s, win) != 0) {
eb838fe1 940 dev_dbg(&p_dev->dev, "failed to set memory mapping\n");
6b8e087b 941 mutex_unlock(&s->ops_mutex);
926c5402
DB
942 return -EIO;
943 }
1a8d4663
DB
944 s->state |= SOCKET_WIN_REQ(w);
945
946 /* Return window handle */
9fea84f4 947 if (s->features & SS_CAP_STATIC_MAP)
cdb13808 948 res->start = win->static_start;
9fea84f4 949 else
cdb13808 950 res->start = win->res->start;
9fea84f4 951
0ca724d3 952 /* convert to new-style resources */
cdb13808
DB
953 res->end += res->start - 1;
954 res->flags &= ~WIN_FLAGS_REQ;
955 res->flags |= (win->map << 2) | IORESOURCE_MEM;
ad0c7be2
DB
956 res->parent = win->res;
957 if (win->res)
958 request_resource(&iomem_resource, res);
959
eb838fe1 960 dev_dbg(&p_dev->dev, "request_window results in %pR\n", res);
0ca724d3 961
6b8e087b 962 mutex_unlock(&s->ops_mutex);
1a8d4663 963
4c89e88b 964 return 0;
1a8d4663
DB
965} /* pcmcia_request_window */
966EXPORT_SYMBOL(pcmcia_request_window);
5f2a71fc 967
9485ee14
DB
968
969/**
970 * pcmcia_disable_device() - disable and clean up a PCMCIA device
971 * @p_dev: the associated PCMCIA device
972 *
973 * pcmcia_disable_device() is the driver-callable counterpart to
974 * pcmcia_enable_device(): If a PCMCIA device is no longer used,
975 * drivers are expected to clean up and disable the device by calling
976 * this function. Any I/O ranges (iomem and ioports) will be released,
977 * the Vpp voltage will be set to 0, and IRQs will no longer be
978 * generated -- at least if there is no other card function (of
979 * multifunction devices) being used.
980 */
9fea84f4
DB
981void pcmcia_disable_device(struct pcmcia_device *p_dev)
982{
0ca724d3 983 int i;
1c4a77bf
DB
984
985 dev_dbg(&p_dev->dev, "disabling device\n");
986
0ca724d3
DB
987 for (i = 0; i < MAX_WIN; i++) {
988 struct resource *res = p_dev->resource[MAX_IO_WIN + i];
989 if (res->flags & WIN_FLAGS_REQ)
990 pcmcia_release_window(p_dev, res);
991 }
992
5f2a71fc 993 pcmcia_release_configuration(p_dev);
2ce4905e 994 pcmcia_release_io(p_dev);
418c5278 995 if (p_dev->_irq) {
eb14120f 996 free_irq(p_dev->irq, p_dev->priv);
418c5278
PM
997 p_dev->_irq = 0;
998 }
5f2a71fc
DB
999}
1000EXPORT_SYMBOL(pcmcia_disable_device);