pcmcia: deprecate CS_SUCCESS
[linux-2.6-block.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>
23
1a8d4663
DB
24#include <pcmcia/cs_types.h>
25#include <pcmcia/ss.h>
26#include <pcmcia/cs.h>
1a8d4663
DB
27#include <pcmcia/cistpl.h>
28#include <pcmcia/cisreg.h>
29#include <pcmcia/ds.h>
30
31#include "cs_internal.h"
32#include "ds_internal.h"
33
34
1a8d4663
DB
35/* Access speed for IO windows */
36static int io_speed = 0;
37module_param(io_speed, int, 0444);
38
39
40#ifdef CONFIG_PCMCIA_PROBE
531e5ca6 41#include <asm/irq.h>
1a8d4663
DB
42/* mask of IRQs already reserved by other cards, we should avoid using them */
43static u8 pcmcia_used_irq[NR_IRQS];
44#endif
45
46
7d16b658 47#ifdef CONFIG_PCMCIA_DEBUG
1a8d4663 48extern int ds_pc_debug;
1a8d4663
DB
49
50#define ds_dbg(skt, lvl, fmt, arg...) do { \
51 if (ds_pc_debug >= lvl) \
ac449d6e
DB
52 dev_printk(KERN_DEBUG, &skt->dev, \
53 "pcmcia_resource: " fmt, \
54 ## arg); \
1a8d4663
DB
55} while (0)
56#else
ac449d6e 57#define ds_dbg(skt, lvl, fmt, arg...) do { } while (0)
1a8d4663
DB
58#endif
59
60
61
62/** alloc_io_space
63 *
64 * Special stuff for managing IO windows, because they are scarce
65 */
66
ecb8a847
OJ
67static int alloc_io_space(struct pcmcia_socket *s, u_int attr,
68 unsigned int *base, unsigned int num, u_int lines)
1a8d4663
DB
69{
70 int i;
ecb8a847 71 unsigned int try, align;
1a8d4663
DB
72
73 align = (*base) ? (lines ? 1<<lines : 0) : 1;
74 if (align && (align < num)) {
75 if (*base) {
ecb8a847 76 ds_dbg(s, 0, "odd IO request: num %#x align %#x\n",
1a8d4663
DB
77 num, align);
78 align = 0;
79 } else
80 while (align && (align < num)) align <<= 1;
81 }
82 if (*base & ~(align-1)) {
ecb8a847 83 ds_dbg(s, 0, "odd IO request: base %#x align %#x\n",
1a8d4663
DB
84 *base, align);
85 align = 0;
86 }
87 if ((s->features & SS_CAP_STATIC_MAP) && s->io_offset) {
88 *base = s->io_offset | (*base & 0x0fff);
89 return 0;
90 }
91 /* Check for an already-allocated window that must conflict with
92 * what was asked for. It is a hack because it does not catch all
93 * potential conflicts, just the most obvious ones.
94 */
95 for (i = 0; i < MAX_IO_WIN; i++)
4708b5fa 96 if ((s->io[i].res) && *base &&
c7d00693 97 ((s->io[i].res->start & (align-1)) == *base))
1a8d4663
DB
98 return 1;
99 for (i = 0; i < MAX_IO_WIN; i++) {
c7d00693 100 if (!s->io[i].res) {
1a8d4663
DB
101 s->io[i].res = pcmcia_find_io_region(*base, num, align, s);
102 if (s->io[i].res) {
c7d00693
DB
103 *base = s->io[i].res->start;
104 s->io[i].res->flags = (s->io[i].res->flags & ~IORESOURCE_BITS) | (attr & IORESOURCE_BITS);
105 s->io[i].InUse = num;
1a8d4663
DB
106 break;
107 } else
108 return 1;
c7d00693 109 } else if ((s->io[i].res->flags & IORESOURCE_BITS) != (attr & IORESOURCE_BITS))
1a8d4663
DB
110 continue;
111 /* Try to extend top of window */
c7d00693 112 try = s->io[i].res->end + 1;
1a8d4663
DB
113 if ((*base == 0) || (*base == try))
114 if (pcmcia_adjust_io_region(s->io[i].res, s->io[i].res->start,
115 s->io[i].res->end + num, s) == 0) {
116 *base = try;
1a8d4663
DB
117 s->io[i].InUse += num;
118 break;
119 }
120 /* Try to extend bottom of window */
c7d00693 121 try = s->io[i].res->start - num;
1a8d4663
DB
122 if ((*base == 0) || (*base == try))
123 if (pcmcia_adjust_io_region(s->io[i].res, s->io[i].res->start - num,
124 s->io[i].res->end, s) == 0) {
c7d00693 125 *base = try;
1a8d4663
DB
126 s->io[i].InUse += num;
127 break;
128 }
129 }
130 return (i == MAX_IO_WIN);
131} /* alloc_io_space */
132
133
ecb8a847
OJ
134static void release_io_space(struct pcmcia_socket *s, unsigned int base,
135 unsigned int num)
1a8d4663
DB
136{
137 int i;
138
139 for (i = 0; i < MAX_IO_WIN; i++) {
c7d00693
DB
140 if (!s->io[i].res)
141 continue;
142 if ((s->io[i].res->start <= base) &&
143 (s->io[i].res->end >= base+num-1)) {
1a8d4663
DB
144 s->io[i].InUse -= num;
145 /* Free the window if no one else is using it */
146 if (s->io[i].InUse == 0) {
1a8d4663
DB
147 release_resource(s->io[i].res);
148 kfree(s->io[i].res);
149 s->io[i].res = NULL;
150 }
151 }
152 }
153} /* release_io_space */
154
155
156/** pccard_access_configuration_register
157 *
158 * Access_configuration_register() reads and writes configuration
159 * registers in attribute memory. Memory window 0 is reserved for
160 * this and the tuple reading services.
161 */
162
855cdf13 163int pcmcia_access_configuration_register(struct pcmcia_device *p_dev,
1a8d4663
DB
164 conf_reg_t *reg)
165{
855cdf13 166 struct pcmcia_socket *s;
1a8d4663
DB
167 config_t *c;
168 int addr;
169 u_char val;
170
855cdf13 171 if (!p_dev || !p_dev->function_config)
1a8d4663
DB
172 return CS_NO_CARD;
173
855cdf13
DB
174 s = p_dev->socket;
175 c = p_dev->function_config;
1a8d4663
DB
176
177 if (!(c->state & CONFIG_LOCKED))
178 return CS_CONFIGURATION_LOCKED;
179
180 addr = (c->ConfigBase + reg->Offset) >> 1;
181
182 switch (reg->Action) {
183 case CS_READ:
184 pcmcia_read_cis_mem(s, 1, addr, 1, &val);
185 reg->Value = val;
186 break;
187 case CS_WRITE:
188 val = reg->Value;
189 pcmcia_write_cis_mem(s, 1, addr, 1, &val);
190 break;
191 default:
192 return CS_BAD_ARGS;
193 break;
194 }
4c89e88b 195 return 0;
855cdf13 196} /* pcmcia_access_configuration_register */
3448139b
DB
197EXPORT_SYMBOL(pcmcia_access_configuration_register);
198
1a8d4663 199
1a8d4663
DB
200/** pcmcia_get_window
201 */
202int pcmcia_get_window(struct pcmcia_socket *s, window_handle_t *handle,
203 int idx, win_req_t *req)
204{
205 window_t *win;
206 int w;
207
208 if (!s || !(s->state & SOCKET_PRESENT))
209 return CS_NO_CARD;
210 for (w = idx; w < MAX_WIN; w++)
211 if (s->state & SOCKET_WIN_REQ(w))
212 break;
213 if (w == MAX_WIN)
214 return CS_NO_MORE_ITEMS;
215 win = &s->win[w];
216 req->Base = win->ctl.res->start;
217 req->Size = win->ctl.res->end - win->ctl.res->start + 1;
218 req->AccessSpeed = win->ctl.speed;
219 req->Attributes = 0;
220 if (win->ctl.flags & MAP_ATTRIB)
221 req->Attributes |= WIN_MEMORY_TYPE_AM;
222 if (win->ctl.flags & MAP_ACTIVE)
223 req->Attributes |= WIN_ENABLE;
224 if (win->ctl.flags & MAP_16BIT)
225 req->Attributes |= WIN_DATA_WIDTH_16;
226 if (win->ctl.flags & MAP_USE_WAIT)
227 req->Attributes |= WIN_USE_WAIT;
228 *handle = win;
4c89e88b 229 return 0;
1a8d4663
DB
230} /* pcmcia_get_window */
231EXPORT_SYMBOL(pcmcia_get_window);
232
233
1a8d4663
DB
234/** pcmcia_get_mem_page
235 *
236 * Change the card address of an already open memory window.
237 */
238int pcmcia_get_mem_page(window_handle_t win, memreq_t *req)
239{
240 if ((win == NULL) || (win->magic != WINDOW_MAGIC))
241 return CS_BAD_HANDLE;
242 req->Page = 0;
243 req->CardOffset = win->ctl.card_start;
4c89e88b 244 return 0;
1a8d4663
DB
245} /* pcmcia_get_mem_page */
246EXPORT_SYMBOL(pcmcia_get_mem_page);
247
248
249int pcmcia_map_mem_page(window_handle_t win, memreq_t *req)
250{
251 struct pcmcia_socket *s;
252 if ((win == NULL) || (win->magic != WINDOW_MAGIC))
253 return CS_BAD_HANDLE;
254 if (req->Page != 0)
255 return CS_BAD_PAGE;
256 s = win->sock;
257 win->ctl.card_start = req->CardOffset;
258 if (s->ops->set_mem_map(s, &win->ctl) != 0)
259 return CS_BAD_OFFSET;
4c89e88b 260 return 0;
1a8d4663
DB
261} /* pcmcia_map_mem_page */
262EXPORT_SYMBOL(pcmcia_map_mem_page);
263
264
265/** pcmcia_modify_configuration
266 *
267 * Modify a locked socket configuration
268 */
2bc5a9bd 269int pcmcia_modify_configuration(struct pcmcia_device *p_dev,
1a8d4663
DB
270 modconf_t *mod)
271{
272 struct pcmcia_socket *s;
273 config_t *c;
274
2bc5a9bd 275 s = p_dev->socket;
dbb22f0d
DB
276 c = p_dev->function_config;
277
1a8d4663
DB
278 if (!(s->state & SOCKET_PRESENT))
279 return CS_NO_CARD;
280 if (!(c->state & CONFIG_LOCKED))
281 return CS_CONFIGURATION_LOCKED;
282
283 if (mod->Attributes & CONF_IRQ_CHANGE_VALID) {
284 if (mod->Attributes & CONF_ENABLE_IRQ) {
285 c->Attributes |= CONF_ENABLE_IRQ;
286 s->socket.io_irq = s->irq.AssignedIRQ;
287 } else {
288 c->Attributes &= ~CONF_ENABLE_IRQ;
289 s->socket.io_irq = 0;
290 }
291 s->ops->set_socket(s, &s->socket);
292 }
293
294 if (mod->Attributes & CONF_VCC_CHANGE_VALID)
295 return CS_BAD_VCC;
296
297 /* We only allow changing Vpp1 and Vpp2 to the same value */
298 if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) &&
299 (mod->Attributes & CONF_VPP2_CHANGE_VALID)) {
300 if (mod->Vpp1 != mod->Vpp2)
301 return CS_BAD_VPP;
71ed90d8 302 s->socket.Vpp = mod->Vpp1;
1a8d4663
DB
303 if (s->ops->set_socket(s, &s->socket))
304 return CS_BAD_VPP;
305 } else if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) ||
306 (mod->Attributes & CONF_VPP2_CHANGE_VALID))
307 return CS_BAD_VPP;
308
4bbed523
DB
309 if (mod->Attributes & CONF_IO_CHANGE_WIDTH) {
310 pccard_io_map io_off = { 0, 0, 0, 0, 1 };
311 pccard_io_map io_on;
312 int i;
313
314 io_on.speed = io_speed;
315 for (i = 0; i < MAX_IO_WIN; i++) {
316 if (!s->io[i].res)
317 continue;
318 io_off.map = i;
319 io_on.map = i;
320
321 io_on.flags = MAP_ACTIVE | IO_DATA_PATH_WIDTH_8;
322 io_on.start = s->io[i].res->start;
323 io_on.stop = s->io[i].res->end;
324
325 s->ops->set_io_map(s, &io_off);
326 mdelay(40);
327 s->ops->set_io_map(s, &io_on);
328 }
329 }
330
4c89e88b 331 return 0;
1a8d4663
DB
332} /* modify_configuration */
333EXPORT_SYMBOL(pcmcia_modify_configuration);
334
335
2bc5a9bd 336int pcmcia_release_configuration(struct pcmcia_device *p_dev)
1a8d4663
DB
337{
338 pccard_io_map io = { 0, 0, 0, 0, 1 };
2bc5a9bd 339 struct pcmcia_socket *s = p_dev->socket;
5f2a71fc 340 config_t *c = p_dev->function_config;
1a8d4663
DB
341 int i;
342
e2d40963
DB
343 if (p_dev->_locked) {
344 p_dev->_locked = 0;
1a8d4663
DB
345 if (--(s->lock_count) == 0) {
346 s->socket.flags = SS_OUTPUT_ENA; /* Is this correct? */
347 s->socket.Vpp = 0;
348 s->socket.io_irq = 0;
349 s->ops->set_socket(s, &s->socket);
350 }
5f2a71fc
DB
351 }
352 if (c->state & CONFIG_LOCKED) {
353 c->state &= ~CONFIG_LOCKED;
1a8d4663
DB
354 if (c->state & CONFIG_IO_REQ)
355 for (i = 0; i < MAX_IO_WIN; i++) {
c7d00693 356 if (!s->io[i].res)
1a8d4663
DB
357 continue;
358 s->io[i].Config--;
359 if (s->io[i].Config != 0)
360 continue;
361 io.map = i;
362 s->ops->set_io_map(s, &io);
363 }
1a8d4663
DB
364 }
365
4c89e88b 366 return 0;
1a8d4663 367} /* pcmcia_release_configuration */
1a8d4663
DB
368
369
370/** pcmcia_release_io
371 *
372 * Release_io() releases the I/O ranges allocated by a client. This
373 * may be invoked some time after a card ejection has already dumped
374 * the actual socket configuration, so if the client is "stale", we
375 * don't bother checking the port ranges against the current socket
376 * values.
377 */
b4c88400 378static int pcmcia_release_io(struct pcmcia_device *p_dev, io_req_t *req)
1a8d4663 379{
2bc5a9bd 380 struct pcmcia_socket *s = p_dev->socket;
5f2a71fc 381 config_t *c = p_dev->function_config;
1a8d4663 382
e2d40963 383 if (!p_dev->_io )
1a8d4663 384 return CS_BAD_HANDLE;
5f2a71fc 385
e2d40963 386 p_dev->_io = 0;
1a8d4663 387
5f2a71fc
DB
388 if ((c->io.BasePort1 != req->BasePort1) ||
389 (c->io.NumPorts1 != req->NumPorts1) ||
390 (c->io.BasePort2 != req->BasePort2) ||
391 (c->io.NumPorts2 != req->NumPorts2))
392 return CS_BAD_ARGS;
393
394 c->state &= ~CONFIG_IO_REQ;
1a8d4663
DB
395
396 release_io_space(s, req->BasePort1, req->NumPorts1);
397 if (req->NumPorts2)
398 release_io_space(s, req->BasePort2, req->NumPorts2);
399
4c89e88b 400 return 0;
1a8d4663 401} /* pcmcia_release_io */
1a8d4663
DB
402
403
b4c88400 404static int pcmcia_release_irq(struct pcmcia_device *p_dev, irq_req_t *req)
1a8d4663 405{
2bc5a9bd 406 struct pcmcia_socket *s = p_dev->socket;
5f2a71fc
DB
407 config_t *c= p_dev->function_config;
408
e2d40963 409 if (!p_dev->_irq)
1a8d4663 410 return CS_BAD_HANDLE;
e2d40963 411 p_dev->_irq = 0;
1a8d4663 412
5f2a71fc
DB
413 if (c->state & CONFIG_LOCKED)
414 return CS_CONFIGURATION_LOCKED;
415 if (c->irq.Attributes != req->Attributes)
416 return CS_BAD_ATTRIBUTE;
417 if (s->irq.AssignedIRQ != req->AssignedIRQ)
418 return CS_BAD_IRQ;
419 if (--s->irq.Config == 0) {
420 c->state &= ~CONFIG_IRQ_REQ;
421 s->irq.AssignedIRQ = 0;
1a8d4663
DB
422 }
423
424 if (req->Attributes & IRQ_HANDLE_PRESENT) {
425 free_irq(req->AssignedIRQ, req->Instance);
426 }
427
428#ifdef CONFIG_PCMCIA_PROBE
429 pcmcia_used_irq[req->AssignedIRQ]--;
430#endif
431
4c89e88b 432 return 0;
1a8d4663 433} /* pcmcia_release_irq */
1a8d4663
DB
434
435
436int pcmcia_release_window(window_handle_t win)
437{
438 struct pcmcia_socket *s;
439
440 if ((win == NULL) || (win->magic != WINDOW_MAGIC))
441 return CS_BAD_HANDLE;
442 s = win->sock;
e2d40963 443 if (!(win->handle->_win & CLIENT_WIN_REQ(win->index)))
1a8d4663
DB
444 return CS_BAD_HANDLE;
445
446 /* Shut down memory window */
447 win->ctl.flags &= ~MAP_ACTIVE;
448 s->ops->set_mem_map(s, &win->ctl);
449 s->state &= ~SOCKET_WIN_REQ(win->index);
450
451 /* Release system memory */
452 if (win->ctl.res) {
453 release_resource(win->ctl.res);
454 kfree(win->ctl.res);
455 win->ctl.res = NULL;
456 }
e2d40963 457 win->handle->_win &= ~CLIENT_WIN_REQ(win->index);
1a8d4663
DB
458
459 win->magic = 0;
460
4c89e88b 461 return 0;
1a8d4663
DB
462} /* pcmcia_release_window */
463EXPORT_SYMBOL(pcmcia_release_window);
464
465
2bc5a9bd 466int pcmcia_request_configuration(struct pcmcia_device *p_dev,
1a8d4663
DB
467 config_req_t *req)
468{
469 int i;
470 u_int base;
2bc5a9bd 471 struct pcmcia_socket *s = p_dev->socket;
1a8d4663
DB
472 config_t *c;
473 pccard_io_map iomap;
474
1a8d4663
DB
475 if (!(s->state & SOCKET_PRESENT))
476 return CS_NO_CARD;
477
1a8d4663
DB
478 if (req->IntType & INT_CARDBUS)
479 return CS_UNSUPPORTED_MODE;
dbb22f0d 480 c = p_dev->function_config;
1a8d4663
DB
481 if (c->state & CONFIG_LOCKED)
482 return CS_CONFIGURATION_LOCKED;
483
484 /* Do power control. We don't allow changes in Vcc. */
70294b46 485 s->socket.Vpp = req->Vpp;
1a8d4663
DB
486 if (s->ops->set_socket(s, &s->socket))
487 return CS_BAD_VPP;
488
1a8d4663
DB
489 /* Pick memory or I/O card, DMA mode, interrupt */
490 c->IntType = req->IntType;
491 c->Attributes = req->Attributes;
492 if (req->IntType & INT_MEMORY_AND_IO)
493 s->socket.flags |= SS_IOCARD;
494 if (req->IntType & INT_ZOOMED_VIDEO)
495 s->socket.flags |= SS_ZVCARD | SS_IOCARD;
496 if (req->Attributes & CONF_ENABLE_DMA)
497 s->socket.flags |= SS_DMA_MODE;
498 if (req->Attributes & CONF_ENABLE_SPKR)
499 s->socket.flags |= SS_SPKR_ENA;
500 if (req->Attributes & CONF_ENABLE_IRQ)
501 s->socket.io_irq = s->irq.AssignedIRQ;
502 else
503 s->socket.io_irq = 0;
504 s->ops->set_socket(s, &s->socket);
505 s->lock_count++;
506
507 /* Set up CIS configuration registers */
508 base = c->ConfigBase = req->ConfigBase;
1ae9c7d8 509 c->CardValues = req->Present;
1a8d4663
DB
510 if (req->Present & PRESENT_COPY) {
511 c->Copy = req->Copy;
512 pcmcia_write_cis_mem(s, 1, (base + CISREG_SCR)>>1, 1, &c->Copy);
513 }
514 if (req->Present & PRESENT_OPTION) {
515 if (s->functions == 1) {
516 c->Option = req->ConfigIndex & COR_CONFIG_MASK;
517 } else {
518 c->Option = req->ConfigIndex & COR_MFC_CONFIG_MASK;
519 c->Option |= COR_FUNC_ENA|COR_IREQ_ENA;
520 if (req->Present & PRESENT_IOBASE_0)
521 c->Option |= COR_ADDR_DECODE;
522 }
523 if (c->state & CONFIG_IRQ_REQ)
524 if (!(c->irq.Attributes & IRQ_FORCED_PULSE))
525 c->Option |= COR_LEVEL_REQ;
526 pcmcia_write_cis_mem(s, 1, (base + CISREG_COR)>>1, 1, &c->Option);
527 mdelay(40);
528 }
529 if (req->Present & PRESENT_STATUS) {
530 c->Status = req->Status;
531 pcmcia_write_cis_mem(s, 1, (base + CISREG_CCSR)>>1, 1, &c->Status);
532 }
533 if (req->Present & PRESENT_PIN_REPLACE) {
534 c->Pin = req->Pin;
535 pcmcia_write_cis_mem(s, 1, (base + CISREG_PRR)>>1, 1, &c->Pin);
536 }
537 if (req->Present & PRESENT_EXT_STATUS) {
538 c->ExtStatus = req->ExtStatus;
539 pcmcia_write_cis_mem(s, 1, (base + CISREG_ESR)>>1, 1, &c->ExtStatus);
540 }
541 if (req->Present & PRESENT_IOBASE_0) {
542 u_char b = c->io.BasePort1 & 0xff;
543 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_0)>>1, 1, &b);
544 b = (c->io.BasePort1 >> 8) & 0xff;
545 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_1)>>1, 1, &b);
546 }
547 if (req->Present & PRESENT_IOSIZE) {
548 u_char b = c->io.NumPorts1 + c->io.NumPorts2 - 1;
549 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOSIZE)>>1, 1, &b);
550 }
551
552 /* Configure I/O windows */
553 if (c->state & CONFIG_IO_REQ) {
554 iomap.speed = io_speed;
555 for (i = 0; i < MAX_IO_WIN; i++)
c7d00693 556 if (s->io[i].res) {
1a8d4663
DB
557 iomap.map = i;
558 iomap.flags = MAP_ACTIVE;
c7d00693 559 switch (s->io[i].res->flags & IO_DATA_PATH_WIDTH) {
1a8d4663
DB
560 case IO_DATA_PATH_WIDTH_16:
561 iomap.flags |= MAP_16BIT; break;
562 case IO_DATA_PATH_WIDTH_AUTO:
563 iomap.flags |= MAP_AUTOSZ; break;
564 default:
565 break;
566 }
c7d00693
DB
567 iomap.start = s->io[i].res->start;
568 iomap.stop = s->io[i].res->end;
1a8d4663
DB
569 s->ops->set_io_map(s, &iomap);
570 s->io[i].Config++;
571 }
572 }
573
574 c->state |= CONFIG_LOCKED;
e2d40963 575 p_dev->_locked = 1;
4c89e88b 576 return 0;
1a8d4663
DB
577} /* pcmcia_request_configuration */
578EXPORT_SYMBOL(pcmcia_request_configuration);
579
580
581/** pcmcia_request_io
582 *
583 * Request_io() reserves ranges of port addresses for a socket.
584 * I have not implemented range sharing or alias addressing.
585 */
2bc5a9bd 586int pcmcia_request_io(struct pcmcia_device *p_dev, io_req_t *req)
1a8d4663 587{
2bc5a9bd 588 struct pcmcia_socket *s = p_dev->socket;
1a8d4663
DB
589 config_t *c;
590
1a8d4663
DB
591 if (!(s->state & SOCKET_PRESENT))
592 return CS_NO_CARD;
593
1a8d4663
DB
594 if (!req)
595 return CS_UNSUPPORTED_MODE;
dbb22f0d 596 c = p_dev->function_config;
1a8d4663
DB
597 if (c->state & CONFIG_LOCKED)
598 return CS_CONFIGURATION_LOCKED;
599 if (c->state & CONFIG_IO_REQ)
600 return CS_IN_USE;
601 if (req->Attributes1 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS))
602 return CS_BAD_ATTRIBUTE;
603 if ((req->NumPorts2 > 0) &&
604 (req->Attributes2 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS)))
605 return CS_BAD_ATTRIBUTE;
606
607 if (alloc_io_space(s, req->Attributes1, &req->BasePort1,
608 req->NumPorts1, req->IOAddrLines))
609 return CS_IN_USE;
610
611 if (req->NumPorts2) {
612 if (alloc_io_space(s, req->Attributes2, &req->BasePort2,
613 req->NumPorts2, req->IOAddrLines)) {
614 release_io_space(s, req->BasePort1, req->NumPorts1);
615 return CS_IN_USE;
616 }
617 }
618
619 c->io = *req;
620 c->state |= CONFIG_IO_REQ;
e2d40963 621 p_dev->_io = 1;
4c89e88b 622 return 0;
1a8d4663
DB
623} /* pcmcia_request_io */
624EXPORT_SYMBOL(pcmcia_request_io);
625
626
627/** pcmcia_request_irq
628 *
629 * Request_irq() reserves an irq for this client.
630 *
631 * Also, since Linux only reserves irq's when they are actually
632 * hooked, we don't guarantee that an irq will still be available
633 * when the configuration is locked. Now that I think about it,
634 * there might be a way to fix this using a dummy handler.
635 */
636
637#ifdef CONFIG_PCMCIA_PROBE
7d12e780 638static irqreturn_t test_action(int cpl, void *dev_id)
1a8d4663
DB
639{
640 return IRQ_NONE;
641}
642#endif
643
2bc5a9bd 644int pcmcia_request_irq(struct pcmcia_device *p_dev, irq_req_t *req)
1a8d4663 645{
2bc5a9bd 646 struct pcmcia_socket *s = p_dev->socket;
1a8d4663
DB
647 config_t *c;
648 int ret = CS_IN_USE, irq = 0;
c533120b 649 int type;
1a8d4663 650
1a8d4663
DB
651 if (!(s->state & SOCKET_PRESENT))
652 return CS_NO_CARD;
dbb22f0d 653 c = p_dev->function_config;
1a8d4663
DB
654 if (c->state & CONFIG_LOCKED)
655 return CS_CONFIGURATION_LOCKED;
656 if (c->state & CONFIG_IRQ_REQ)
657 return CS_IN_USE;
658
c533120b
AC
659 /* Decide what type of interrupt we are registering */
660 type = 0;
661 if (s->functions > 1) /* All of this ought to be handled higher up */
dace1453 662 type = IRQF_SHARED;
c533120b 663 if (req->Attributes & IRQ_TYPE_DYNAMIC_SHARING)
dace1453 664 type = IRQF_SHARED;
c533120b 665
1a8d4663 666#ifdef CONFIG_PCMCIA_PROBE
635416ef
AC
667
668#ifdef IRQ_NOAUTOEN
669 /* if the underlying IRQ infrastructure allows for it, only allocate
670 * the IRQ, but do not enable it
671 */
672 if (!(req->Attributes & IRQ_HANDLE_PRESENT))
673 type |= IRQ_NOAUTOEN;
674#endif /* IRQ_NOAUTOEN */
675
1a8d4663
DB
676 if (s->irq.AssignedIRQ != 0) {
677 /* If the interrupt is already assigned, it must be the same */
678 irq = s->irq.AssignedIRQ;
679 } else {
680 int try;
681 u32 mask = s->irq_mask;
a1b274fb 682 void *data = &p_dev->dev.driver; /* something unique to this device */
1a8d4663
DB
683
684 for (try = 0; try < 64; try++) {
685 irq = try % 32;
686
687 /* marked as available by driver, and not blocked by userspace? */
688 if (!((mask >> irq) & 1))
689 continue;
690
691 /* avoid an IRQ which is already used by a PCMCIA card */
692 if ((try < 32) && pcmcia_used_irq[irq])
693 continue;
694
695 /* register the correct driver, if possible, of check whether
696 * registering a dummy handle works, i.e. if the IRQ isn't
697 * marked as used by the kernel resource management core */
698 ret = request_irq(irq,
699 (req->Attributes & IRQ_HANDLE_PRESENT) ? req->Handler : test_action,
c533120b 700 type,
bd65a685 701 p_dev->devname,
1a8d4663
DB
702 (req->Attributes & IRQ_HANDLE_PRESENT) ? req->Instance : data);
703 if (!ret) {
704 if (!(req->Attributes & IRQ_HANDLE_PRESENT))
705 free_irq(irq, data);
706 break;
707 }
708 }
709 }
710#endif
c181e0e0
DR
711 /* only assign PCI irq if no IRQ already assigned */
712 if (ret && !s->irq.AssignedIRQ) {
1a8d4663
DB
713 if (!s->pci_irq)
714 return ret;
dace1453 715 type = IRQF_SHARED;
1a8d4663
DB
716 irq = s->pci_irq;
717 }
718
c533120b
AC
719 if (ret && (req->Attributes & IRQ_HANDLE_PRESENT)) {
720 if (request_irq(irq, req->Handler, type, p_dev->devname, req->Instance))
1a8d4663
DB
721 return CS_IN_USE;
722 }
723
c533120b 724 /* Make sure the fact the request type was overridden is passed back */
dace1453 725 if (type == IRQF_SHARED && !(req->Attributes & IRQ_TYPE_DYNAMIC_SHARING)) {
c533120b 726 req->Attributes |= IRQ_TYPE_DYNAMIC_SHARING;
ac449d6e
DB
727 dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: "
728 "request for exclusive IRQ could not be fulfilled.\n");
729 dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: the driver "
730 "needs updating to supported shared IRQ lines.\n");
c533120b 731 }
1a8d4663
DB
732 c->irq.Attributes = req->Attributes;
733 s->irq.AssignedIRQ = req->AssignedIRQ = irq;
734 s->irq.Config++;
735
736 c->state |= CONFIG_IRQ_REQ;
e2d40963 737 p_dev->_irq = 1;
1a8d4663
DB
738
739#ifdef CONFIG_PCMCIA_PROBE
740 pcmcia_used_irq[irq]++;
741#endif
742
4c89e88b 743 return 0;
1a8d4663
DB
744} /* pcmcia_request_irq */
745EXPORT_SYMBOL(pcmcia_request_irq);
746
747
748/** pcmcia_request_window
749 *
750 * Request_window() establishes a mapping between card memory space
751 * and system memory space.
752 */
2bc5a9bd 753int pcmcia_request_window(struct pcmcia_device **p_dev, win_req_t *req, window_handle_t *wh)
1a8d4663 754{
2bc5a9bd 755 struct pcmcia_socket *s = (*p_dev)->socket;
1a8d4663
DB
756 window_t *win;
757 u_long align;
758 int w;
759
1a8d4663
DB
760 if (!(s->state & SOCKET_PRESENT))
761 return CS_NO_CARD;
762 if (req->Attributes & (WIN_PAGED | WIN_SHARED))
763 return CS_BAD_ATTRIBUTE;
764
765 /* Window size defaults to smallest available */
766 if (req->Size == 0)
767 req->Size = s->map_size;
768 align = (((s->features & SS_CAP_MEM_ALIGN) ||
769 (req->Attributes & WIN_STRICT_ALIGN)) ?
770 req->Size : s->map_size);
771 if (req->Size & (s->map_size-1))
772 return CS_BAD_SIZE;
773 if ((req->Base && (s->features & SS_CAP_STATIC_MAP)) ||
774 (req->Base & (align-1)))
775 return CS_BAD_BASE;
776 if (req->Base)
777 align = 0;
778
779 /* Allocate system memory window */
780 for (w = 0; w < MAX_WIN; w++)
781 if (!(s->state & SOCKET_WIN_REQ(w))) break;
782 if (w == MAX_WIN)
783 return CS_OUT_OF_RESOURCE;
784
785 win = &s->win[w];
786 win->magic = WINDOW_MAGIC;
787 win->index = w;
2bc5a9bd 788 win->handle = *p_dev;
1a8d4663
DB
789 win->sock = s;
790
791 if (!(s->features & SS_CAP_STATIC_MAP)) {
792 win->ctl.res = pcmcia_find_mem_region(req->Base, req->Size, align,
793 (req->Attributes & WIN_MAP_BELOW_1MB), s);
794 if (!win->ctl.res)
795 return CS_IN_USE;
796 }
e2d40963 797 (*p_dev)->_win |= CLIENT_WIN_REQ(w);
1a8d4663
DB
798
799 /* Configure the socket controller */
800 win->ctl.map = w+1;
801 win->ctl.flags = 0;
802 win->ctl.speed = req->AccessSpeed;
803 if (req->Attributes & WIN_MEMORY_TYPE)
804 win->ctl.flags |= MAP_ATTRIB;
805 if (req->Attributes & WIN_ENABLE)
806 win->ctl.flags |= MAP_ACTIVE;
807 if (req->Attributes & WIN_DATA_WIDTH_16)
808 win->ctl.flags |= MAP_16BIT;
809 if (req->Attributes & WIN_USE_WAIT)
810 win->ctl.flags |= MAP_USE_WAIT;
811 win->ctl.card_start = 0;
812 if (s->ops->set_mem_map(s, &win->ctl) != 0)
813 return CS_BAD_ARGS;
814 s->state |= SOCKET_WIN_REQ(w);
815
816 /* Return window handle */
817 if (s->features & SS_CAP_STATIC_MAP) {
818 req->Base = win->ctl.static_start;
819 } else {
820 req->Base = win->ctl.res->start;
821 }
822 *wh = win;
823
4c89e88b 824 return 0;
1a8d4663
DB
825} /* pcmcia_request_window */
826EXPORT_SYMBOL(pcmcia_request_window);
5f2a71fc
DB
827
828void pcmcia_disable_device(struct pcmcia_device *p_dev) {
5f2a71fc 829 pcmcia_release_configuration(p_dev);
fd238232
DB
830 pcmcia_release_io(p_dev, &p_dev->io);
831 pcmcia_release_irq(p_dev, &p_dev->irq);
c1ac0228 832 if (p_dev->win)
fd238232 833 pcmcia_release_window(p_dev->win);
5f2a71fc
DB
834}
835EXPORT_SYMBOL(pcmcia_disable_device);
a804b574
DB
836
837
838struct pcmcia_cfg_mem {
839 tuple_t tuple;
840 cisparse_t parse;
841 u8 buf[256];
8e2fc39d 842 cistpl_cftable_entry_t dflt;
a804b574
DB
843};
844
845/**
846 * pcmcia_loop_config() - loop over configuration options
847 * @p_dev: the struct pcmcia_device which we need to loop for.
848 * @conf_check: function to call for each configuration option.
849 * It gets passed the struct pcmcia_device, the CIS data
850 * describing the configuration option, and private data
851 * being passed to pcmcia_loop_config()
852 * @priv_data: private data to be passed to the conf_check function.
853 *
854 * pcmcia_loop_config() loops over all configuration options, and calls
855 * the driver-specific conf_check() for each one, checking whether
856 * it is a valid one.
857 */
858int pcmcia_loop_config(struct pcmcia_device *p_dev,
859 int (*conf_check) (struct pcmcia_device *p_dev,
860 cistpl_cftable_entry_t *cfg,
8e2fc39d 861 cistpl_cftable_entry_t *dflt,
ad913c11 862 unsigned int vcc,
a804b574
DB
863 void *priv_data),
864 void *priv_data)
865{
866 struct pcmcia_cfg_mem *cfg_mem;
8e2fc39d 867
a804b574
DB
868 tuple_t *tuple;
869 int ret = -ENODEV;
ad913c11 870 unsigned int vcc;
a804b574
DB
871
872 cfg_mem = kzalloc(sizeof(struct pcmcia_cfg_mem), GFP_KERNEL);
873 if (cfg_mem == NULL)
874 return -ENOMEM;
875
ad913c11
DB
876 /* get the current Vcc setting */
877 vcc = p_dev->socket->socket.Vcc;
878
a804b574
DB
879 tuple = &cfg_mem->tuple;
880 tuple->TupleData = cfg_mem->buf;
881 tuple->TupleDataMax = 255;
882 tuple->TupleOffset = 0;
883 tuple->DesiredTuple = CISTPL_CFTABLE_ENTRY;
884 tuple->Attributes = 0;
885
886 ret = pcmcia_get_first_tuple(p_dev, tuple);
887 while (!ret) {
498ac189
DB
888 cistpl_cftable_entry_t *cfg = &cfg_mem->parse.cftable_entry;
889
a804b574
DB
890 if (pcmcia_get_tuple_data(p_dev, tuple))
891 goto next_entry;
892
893 if (pcmcia_parse_tuple(p_dev, tuple, &cfg_mem->parse))
894 goto next_entry;
895
498ac189
DB
896 /* default values */
897 p_dev->conf.ConfigIndex = cfg->index;
8e2fc39d
DB
898 if (cfg->flags & CISTPL_CFTABLE_DEFAULT)
899 cfg_mem->dflt = *cfg;
498ac189 900
ad913c11 901 ret = conf_check(p_dev, cfg, &cfg_mem->dflt, vcc, priv_data);
a804b574
DB
902 if (!ret)
903 break;
904
905next_entry:
906 ret = pcmcia_get_next_tuple(p_dev, tuple);
907 }
908
909 return ret;
910}
911EXPORT_SYMBOL(pcmcia_loop_config);