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