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