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