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