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