pcmcia: don't add extra DEBUG cflag
[linux-2.6.git] / drivers / pcmcia / pcmcia_resource.c
blob2c636058f493d73ef45720d39316274f27949019
1 /*
2 * PCMCIA 16-bit resource management functions
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.
8 * Copyright (C) 1999 David A. Hinds
9 * Copyright (C) 2004-2005 Dominik Brodowski
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.
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>
24 #include <pcmcia/cs_types.h>
25 #include <pcmcia/ss.h>
26 #include <pcmcia/cs.h>
27 #include <pcmcia/cistpl.h>
28 #include <pcmcia/cisreg.h>
29 #include <pcmcia/ds.h>
31 #include "cs_internal.h"
32 #include "ds_internal.h"
35 /* Access speed for IO windows */
36 static int io_speed = 0;
37 module_param(io_speed, int, 0444);
40 #ifdef CONFIG_PCMCIA_PROBE
41 #include <asm/irq.h>
42 /* mask of IRQs already reserved by other cards, we should avoid using them */
43 static u8 pcmcia_used_irq[NR_IRQS];
44 #endif
47 #ifdef CONFIG_PCMCIA_DEBUG
48 extern int ds_pc_debug;
50 #define ds_dbg(skt, lvl, fmt, arg...) do { \
51 if (ds_pc_debug >= lvl) \
52 dev_printk(KERN_DEBUG, &skt->dev, \
53 "pcmcia_resource: " fmt, \
54 ## arg); \
55 } while (0)
56 #else
57 #define ds_dbg(skt, lvl, fmt, arg...) do { } while (0)
58 #endif
62 /** alloc_io_space
64 * Special stuff for managing IO windows, because they are scarce
67 static int alloc_io_space(struct pcmcia_socket *s, u_int attr,
68 unsigned int *base, unsigned int num, u_int lines)
70 int i;
71 unsigned int try, align;
73 align = (*base) ? (lines ? 1<<lines : 0) : 1;
74 if (align && (align < num)) {
75 if (*base) {
76 ds_dbg(s, 0, "odd IO request: num %#x align %#x\n",
77 num, align);
78 align = 0;
79 } else
80 while (align && (align < num)) align <<= 1;
82 if (*base & ~(align-1)) {
83 ds_dbg(s, 0, "odd IO request: base %#x align %#x\n",
84 *base, align);
85 align = 0;
87 if ((s->features & SS_CAP_STATIC_MAP) && s->io_offset) {
88 *base = s->io_offset | (*base & 0x0fff);
89 return 0;
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.
95 for (i = 0; i < MAX_IO_WIN; i++)
96 if ((s->io[i].res) && *base &&
97 ((s->io[i].res->start & (align-1)) == *base))
98 return 1;
99 for (i = 0; i < MAX_IO_WIN; i++) {
100 if (!s->io[i].res) {
101 s->io[i].res = pcmcia_find_io_region(*base, num, align, s);
102 if (s->io[i].res) {
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;
106 break;
107 } else
108 return 1;
109 } else if ((s->io[i].res->flags & IORESOURCE_BITS) != (attr & IORESOURCE_BITS))
110 continue;
111 /* Try to extend top of window */
112 try = s->io[i].res->end + 1;
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;
117 s->io[i].InUse += num;
118 break;
120 /* Try to extend bottom of window */
121 try = s->io[i].res->start - num;
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) {
125 *base = try;
126 s->io[i].InUse += num;
127 break;
130 return (i == MAX_IO_WIN);
131 } /* alloc_io_space */
134 static void release_io_space(struct pcmcia_socket *s, unsigned int base,
135 unsigned int num)
137 int i;
139 for (i = 0; i < MAX_IO_WIN; i++) {
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)) {
144 s->io[i].InUse -= num;
145 /* Free the window if no one else is using it */
146 if (s->io[i].InUse == 0) {
147 release_resource(s->io[i].res);
148 kfree(s->io[i].res);
149 s->io[i].res = NULL;
153 } /* release_io_space */
156 /** pccard_access_configuration_register
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.
163 int pcmcia_access_configuration_register(struct pcmcia_device *p_dev,
164 conf_reg_t *reg)
166 struct pcmcia_socket *s;
167 config_t *c;
168 int addr;
169 u_char val;
171 if (!p_dev || !p_dev->function_config)
172 return CS_NO_CARD;
174 s = p_dev->socket;
175 c = p_dev->function_config;
177 if (!(c->state & CONFIG_LOCKED))
178 return CS_CONFIGURATION_LOCKED;
180 addr = (c->ConfigBase + reg->Offset) >> 1;
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;
195 return CS_SUCCESS;
196 } /* pcmcia_access_configuration_register */
197 EXPORT_SYMBOL(pcmcia_access_configuration_register);
200 int pccard_get_configuration_info(struct pcmcia_socket *s,
201 struct pcmcia_device *p_dev,
202 config_info_t *config)
204 config_t *c;
206 if (!(s->state & SOCKET_PRESENT))
207 return CS_NO_CARD;
210 #ifdef CONFIG_CARDBUS
211 if (s->state & SOCKET_CARDBUS) {
212 memset(config, 0, sizeof(config_info_t));
213 config->Vcc = s->socket.Vcc;
214 config->Vpp1 = config->Vpp2 = s->socket.Vpp;
215 config->Option = s->cb_dev->subordinate->number;
216 if (s->state & SOCKET_CARDBUS_CONFIG) {
217 config->Attributes = CONF_VALID_CLIENT;
218 config->IntType = INT_CARDBUS;
219 config->AssignedIRQ = s->irq.AssignedIRQ;
220 if (config->AssignedIRQ)
221 config->Attributes |= CONF_ENABLE_IRQ;
222 if (s->io[0].res) {
223 config->BasePort1 = s->io[0].res->start;
224 config->NumPorts1 = s->io[0].res->end - config->BasePort1 + 1;
227 return CS_SUCCESS;
229 #endif
231 if (p_dev) {
232 c = p_dev->function_config;
233 config->Function = p_dev->func;
234 } else {
235 c = NULL;
236 config->Function = 0;
239 if ((c == NULL) || !(c->state & CONFIG_LOCKED)) {
240 config->Attributes = 0;
241 config->Vcc = s->socket.Vcc;
242 config->Vpp1 = config->Vpp2 = s->socket.Vpp;
243 return CS_SUCCESS;
246 config->Attributes = c->Attributes | CONF_VALID_CLIENT;
247 config->Vcc = s->socket.Vcc;
248 config->Vpp1 = config->Vpp2 = s->socket.Vpp;
249 config->IntType = c->IntType;
250 config->ConfigBase = c->ConfigBase;
251 config->Status = c->Status;
252 config->Pin = c->Pin;
253 config->Copy = c->Copy;
254 config->Option = c->Option;
255 config->ExtStatus = c->ExtStatus;
256 config->Present = config->CardValues = c->CardValues;
257 config->IRQAttributes = c->irq.Attributes;
258 config->AssignedIRQ = s->irq.AssignedIRQ;
259 config->BasePort1 = c->io.BasePort1;
260 config->NumPorts1 = c->io.NumPorts1;
261 config->Attributes1 = c->io.Attributes1;
262 config->BasePort2 = c->io.BasePort2;
263 config->NumPorts2 = c->io.NumPorts2;
264 config->Attributes2 = c->io.Attributes2;
265 config->IOAddrLines = c->io.IOAddrLines;
267 return CS_SUCCESS;
268 } /* pccard_get_configuration_info */
270 int pcmcia_get_configuration_info(struct pcmcia_device *p_dev,
271 config_info_t *config)
273 return pccard_get_configuration_info(p_dev->socket, p_dev,
274 config);
276 EXPORT_SYMBOL(pcmcia_get_configuration_info);
279 /** pcmcia_get_window
281 int pcmcia_get_window(struct pcmcia_socket *s, window_handle_t *handle,
282 int idx, win_req_t *req)
284 window_t *win;
285 int w;
287 if (!s || !(s->state & SOCKET_PRESENT))
288 return CS_NO_CARD;
289 for (w = idx; w < MAX_WIN; w++)
290 if (s->state & SOCKET_WIN_REQ(w))
291 break;
292 if (w == MAX_WIN)
293 return CS_NO_MORE_ITEMS;
294 win = &s->win[w];
295 req->Base = win->ctl.res->start;
296 req->Size = win->ctl.res->end - win->ctl.res->start + 1;
297 req->AccessSpeed = win->ctl.speed;
298 req->Attributes = 0;
299 if (win->ctl.flags & MAP_ATTRIB)
300 req->Attributes |= WIN_MEMORY_TYPE_AM;
301 if (win->ctl.flags & MAP_ACTIVE)
302 req->Attributes |= WIN_ENABLE;
303 if (win->ctl.flags & MAP_16BIT)
304 req->Attributes |= WIN_DATA_WIDTH_16;
305 if (win->ctl.flags & MAP_USE_WAIT)
306 req->Attributes |= WIN_USE_WAIT;
307 *handle = win;
308 return CS_SUCCESS;
309 } /* pcmcia_get_window */
310 EXPORT_SYMBOL(pcmcia_get_window);
313 /** pcmcia_get_mem_page
315 * Change the card address of an already open memory window.
317 int pcmcia_get_mem_page(window_handle_t win, memreq_t *req)
319 if ((win == NULL) || (win->magic != WINDOW_MAGIC))
320 return CS_BAD_HANDLE;
321 req->Page = 0;
322 req->CardOffset = win->ctl.card_start;
323 return CS_SUCCESS;
324 } /* pcmcia_get_mem_page */
325 EXPORT_SYMBOL(pcmcia_get_mem_page);
328 int pcmcia_map_mem_page(window_handle_t win, memreq_t *req)
330 struct pcmcia_socket *s;
331 if ((win == NULL) || (win->magic != WINDOW_MAGIC))
332 return CS_BAD_HANDLE;
333 if (req->Page != 0)
334 return CS_BAD_PAGE;
335 s = win->sock;
336 win->ctl.card_start = req->CardOffset;
337 if (s->ops->set_mem_map(s, &win->ctl) != 0)
338 return CS_BAD_OFFSET;
339 return CS_SUCCESS;
340 } /* pcmcia_map_mem_page */
341 EXPORT_SYMBOL(pcmcia_map_mem_page);
344 /** pcmcia_modify_configuration
346 * Modify a locked socket configuration
348 int pcmcia_modify_configuration(struct pcmcia_device *p_dev,
349 modconf_t *mod)
351 struct pcmcia_socket *s;
352 config_t *c;
354 s = p_dev->socket;
355 c = p_dev->function_config;
357 if (!(s->state & SOCKET_PRESENT))
358 return CS_NO_CARD;
359 if (!(c->state & CONFIG_LOCKED))
360 return CS_CONFIGURATION_LOCKED;
362 if (mod->Attributes & CONF_IRQ_CHANGE_VALID) {
363 if (mod->Attributes & CONF_ENABLE_IRQ) {
364 c->Attributes |= CONF_ENABLE_IRQ;
365 s->socket.io_irq = s->irq.AssignedIRQ;
366 } else {
367 c->Attributes &= ~CONF_ENABLE_IRQ;
368 s->socket.io_irq = 0;
370 s->ops->set_socket(s, &s->socket);
373 if (mod->Attributes & CONF_VCC_CHANGE_VALID)
374 return CS_BAD_VCC;
376 /* We only allow changing Vpp1 and Vpp2 to the same value */
377 if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) &&
378 (mod->Attributes & CONF_VPP2_CHANGE_VALID)) {
379 if (mod->Vpp1 != mod->Vpp2)
380 return CS_BAD_VPP;
381 s->socket.Vpp = mod->Vpp1;
382 if (s->ops->set_socket(s, &s->socket))
383 return CS_BAD_VPP;
384 } else if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) ||
385 (mod->Attributes & CONF_VPP2_CHANGE_VALID))
386 return CS_BAD_VPP;
388 if (mod->Attributes & CONF_IO_CHANGE_WIDTH) {
389 pccard_io_map io_off = { 0, 0, 0, 0, 1 };
390 pccard_io_map io_on;
391 int i;
393 io_on.speed = io_speed;
394 for (i = 0; i < MAX_IO_WIN; i++) {
395 if (!s->io[i].res)
396 continue;
397 io_off.map = i;
398 io_on.map = i;
400 io_on.flags = MAP_ACTIVE | IO_DATA_PATH_WIDTH_8;
401 io_on.start = s->io[i].res->start;
402 io_on.stop = s->io[i].res->end;
404 s->ops->set_io_map(s, &io_off);
405 mdelay(40);
406 s->ops->set_io_map(s, &io_on);
410 return CS_SUCCESS;
411 } /* modify_configuration */
412 EXPORT_SYMBOL(pcmcia_modify_configuration);
415 int pcmcia_release_configuration(struct pcmcia_device *p_dev)
417 pccard_io_map io = { 0, 0, 0, 0, 1 };
418 struct pcmcia_socket *s = p_dev->socket;
419 config_t *c = p_dev->function_config;
420 int i;
422 if (p_dev->_locked) {
423 p_dev->_locked = 0;
424 if (--(s->lock_count) == 0) {
425 s->socket.flags = SS_OUTPUT_ENA; /* Is this correct? */
426 s->socket.Vpp = 0;
427 s->socket.io_irq = 0;
428 s->ops->set_socket(s, &s->socket);
431 if (c->state & CONFIG_LOCKED) {
432 c->state &= ~CONFIG_LOCKED;
433 if (c->state & CONFIG_IO_REQ)
434 for (i = 0; i < MAX_IO_WIN; i++) {
435 if (!s->io[i].res)
436 continue;
437 s->io[i].Config--;
438 if (s->io[i].Config != 0)
439 continue;
440 io.map = i;
441 s->ops->set_io_map(s, &io);
445 return CS_SUCCESS;
446 } /* pcmcia_release_configuration */
449 /** pcmcia_release_io
451 * Release_io() releases the I/O ranges allocated by a client. This
452 * may be invoked some time after a card ejection has already dumped
453 * the actual socket configuration, so if the client is "stale", we
454 * don't bother checking the port ranges against the current socket
455 * values.
457 static int pcmcia_release_io(struct pcmcia_device *p_dev, io_req_t *req)
459 struct pcmcia_socket *s = p_dev->socket;
460 config_t *c = p_dev->function_config;
462 if (!p_dev->_io )
463 return CS_BAD_HANDLE;
465 p_dev->_io = 0;
467 if ((c->io.BasePort1 != req->BasePort1) ||
468 (c->io.NumPorts1 != req->NumPorts1) ||
469 (c->io.BasePort2 != req->BasePort2) ||
470 (c->io.NumPorts2 != req->NumPorts2))
471 return CS_BAD_ARGS;
473 c->state &= ~CONFIG_IO_REQ;
475 release_io_space(s, req->BasePort1, req->NumPorts1);
476 if (req->NumPorts2)
477 release_io_space(s, req->BasePort2, req->NumPorts2);
479 return CS_SUCCESS;
480 } /* pcmcia_release_io */
483 static int pcmcia_release_irq(struct pcmcia_device *p_dev, irq_req_t *req)
485 struct pcmcia_socket *s = p_dev->socket;
486 config_t *c= p_dev->function_config;
488 if (!p_dev->_irq)
489 return CS_BAD_HANDLE;
490 p_dev->_irq = 0;
492 if (c->state & CONFIG_LOCKED)
493 return CS_CONFIGURATION_LOCKED;
494 if (c->irq.Attributes != req->Attributes)
495 return CS_BAD_ATTRIBUTE;
496 if (s->irq.AssignedIRQ != req->AssignedIRQ)
497 return CS_BAD_IRQ;
498 if (--s->irq.Config == 0) {
499 c->state &= ~CONFIG_IRQ_REQ;
500 s->irq.AssignedIRQ = 0;
503 if (req->Attributes & IRQ_HANDLE_PRESENT) {
504 free_irq(req->AssignedIRQ, req->Instance);
507 #ifdef CONFIG_PCMCIA_PROBE
508 pcmcia_used_irq[req->AssignedIRQ]--;
509 #endif
511 return CS_SUCCESS;
512 } /* pcmcia_release_irq */
515 int pcmcia_release_window(window_handle_t win)
517 struct pcmcia_socket *s;
519 if ((win == NULL) || (win->magic != WINDOW_MAGIC))
520 return CS_BAD_HANDLE;
521 s = win->sock;
522 if (!(win->handle->_win & CLIENT_WIN_REQ(win->index)))
523 return CS_BAD_HANDLE;
525 /* Shut down memory window */
526 win->ctl.flags &= ~MAP_ACTIVE;
527 s->ops->set_mem_map(s, &win->ctl);
528 s->state &= ~SOCKET_WIN_REQ(win->index);
530 /* Release system memory */
531 if (win->ctl.res) {
532 release_resource(win->ctl.res);
533 kfree(win->ctl.res);
534 win->ctl.res = NULL;
536 win->handle->_win &= ~CLIENT_WIN_REQ(win->index);
538 win->magic = 0;
540 return CS_SUCCESS;
541 } /* pcmcia_release_window */
542 EXPORT_SYMBOL(pcmcia_release_window);
545 int pcmcia_request_configuration(struct pcmcia_device *p_dev,
546 config_req_t *req)
548 int i;
549 u_int base;
550 struct pcmcia_socket *s = p_dev->socket;
551 config_t *c;
552 pccard_io_map iomap;
554 if (!(s->state & SOCKET_PRESENT))
555 return CS_NO_CARD;
557 if (req->IntType & INT_CARDBUS)
558 return CS_UNSUPPORTED_MODE;
559 c = p_dev->function_config;
560 if (c->state & CONFIG_LOCKED)
561 return CS_CONFIGURATION_LOCKED;
563 /* Do power control. We don't allow changes in Vcc. */
564 s->socket.Vpp = req->Vpp;
565 if (s->ops->set_socket(s, &s->socket))
566 return CS_BAD_VPP;
568 /* Pick memory or I/O card, DMA mode, interrupt */
569 c->IntType = req->IntType;
570 c->Attributes = req->Attributes;
571 if (req->IntType & INT_MEMORY_AND_IO)
572 s->socket.flags |= SS_IOCARD;
573 if (req->IntType & INT_ZOOMED_VIDEO)
574 s->socket.flags |= SS_ZVCARD | SS_IOCARD;
575 if (req->Attributes & CONF_ENABLE_DMA)
576 s->socket.flags |= SS_DMA_MODE;
577 if (req->Attributes & CONF_ENABLE_SPKR)
578 s->socket.flags |= SS_SPKR_ENA;
579 if (req->Attributes & CONF_ENABLE_IRQ)
580 s->socket.io_irq = s->irq.AssignedIRQ;
581 else
582 s->socket.io_irq = 0;
583 s->ops->set_socket(s, &s->socket);
584 s->lock_count++;
586 /* Set up CIS configuration registers */
587 base = c->ConfigBase = req->ConfigBase;
588 c->CardValues = req->Present;
589 if (req->Present & PRESENT_COPY) {
590 c->Copy = req->Copy;
591 pcmcia_write_cis_mem(s, 1, (base + CISREG_SCR)>>1, 1, &c->Copy);
593 if (req->Present & PRESENT_OPTION) {
594 if (s->functions == 1) {
595 c->Option = req->ConfigIndex & COR_CONFIG_MASK;
596 } else {
597 c->Option = req->ConfigIndex & COR_MFC_CONFIG_MASK;
598 c->Option |= COR_FUNC_ENA|COR_IREQ_ENA;
599 if (req->Present & PRESENT_IOBASE_0)
600 c->Option |= COR_ADDR_DECODE;
602 if (c->state & CONFIG_IRQ_REQ)
603 if (!(c->irq.Attributes & IRQ_FORCED_PULSE))
604 c->Option |= COR_LEVEL_REQ;
605 pcmcia_write_cis_mem(s, 1, (base + CISREG_COR)>>1, 1, &c->Option);
606 mdelay(40);
608 if (req->Present & PRESENT_STATUS) {
609 c->Status = req->Status;
610 pcmcia_write_cis_mem(s, 1, (base + CISREG_CCSR)>>1, 1, &c->Status);
612 if (req->Present & PRESENT_PIN_REPLACE) {
613 c->Pin = req->Pin;
614 pcmcia_write_cis_mem(s, 1, (base + CISREG_PRR)>>1, 1, &c->Pin);
616 if (req->Present & PRESENT_EXT_STATUS) {
617 c->ExtStatus = req->ExtStatus;
618 pcmcia_write_cis_mem(s, 1, (base + CISREG_ESR)>>1, 1, &c->ExtStatus);
620 if (req->Present & PRESENT_IOBASE_0) {
621 u_char b = c->io.BasePort1 & 0xff;
622 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_0)>>1, 1, &b);
623 b = (c->io.BasePort1 >> 8) & 0xff;
624 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_1)>>1, 1, &b);
626 if (req->Present & PRESENT_IOSIZE) {
627 u_char b = c->io.NumPorts1 + c->io.NumPorts2 - 1;
628 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOSIZE)>>1, 1, &b);
631 /* Configure I/O windows */
632 if (c->state & CONFIG_IO_REQ) {
633 iomap.speed = io_speed;
634 for (i = 0; i < MAX_IO_WIN; i++)
635 if (s->io[i].res) {
636 iomap.map = i;
637 iomap.flags = MAP_ACTIVE;
638 switch (s->io[i].res->flags & IO_DATA_PATH_WIDTH) {
639 case IO_DATA_PATH_WIDTH_16:
640 iomap.flags |= MAP_16BIT; break;
641 case IO_DATA_PATH_WIDTH_AUTO:
642 iomap.flags |= MAP_AUTOSZ; break;
643 default:
644 break;
646 iomap.start = s->io[i].res->start;
647 iomap.stop = s->io[i].res->end;
648 s->ops->set_io_map(s, &iomap);
649 s->io[i].Config++;
653 c->state |= CONFIG_LOCKED;
654 p_dev->_locked = 1;
655 return CS_SUCCESS;
656 } /* pcmcia_request_configuration */
657 EXPORT_SYMBOL(pcmcia_request_configuration);
660 /** pcmcia_request_io
662 * Request_io() reserves ranges of port addresses for a socket.
663 * I have not implemented range sharing or alias addressing.
665 int pcmcia_request_io(struct pcmcia_device *p_dev, io_req_t *req)
667 struct pcmcia_socket *s = p_dev->socket;
668 config_t *c;
670 if (!(s->state & SOCKET_PRESENT))
671 return CS_NO_CARD;
673 if (!req)
674 return CS_UNSUPPORTED_MODE;
675 c = p_dev->function_config;
676 if (c->state & CONFIG_LOCKED)
677 return CS_CONFIGURATION_LOCKED;
678 if (c->state & CONFIG_IO_REQ)
679 return CS_IN_USE;
680 if (req->Attributes1 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS))
681 return CS_BAD_ATTRIBUTE;
682 if ((req->NumPorts2 > 0) &&
683 (req->Attributes2 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS)))
684 return CS_BAD_ATTRIBUTE;
686 if (alloc_io_space(s, req->Attributes1, &req->BasePort1,
687 req->NumPorts1, req->IOAddrLines))
688 return CS_IN_USE;
690 if (req->NumPorts2) {
691 if (alloc_io_space(s, req->Attributes2, &req->BasePort2,
692 req->NumPorts2, req->IOAddrLines)) {
693 release_io_space(s, req->BasePort1, req->NumPorts1);
694 return CS_IN_USE;
698 c->io = *req;
699 c->state |= CONFIG_IO_REQ;
700 p_dev->_io = 1;
701 return CS_SUCCESS;
702 } /* pcmcia_request_io */
703 EXPORT_SYMBOL(pcmcia_request_io);
706 /** pcmcia_request_irq
708 * Request_irq() reserves an irq for this client.
710 * Also, since Linux only reserves irq's when they are actually
711 * hooked, we don't guarantee that an irq will still be available
712 * when the configuration is locked. Now that I think about it,
713 * there might be a way to fix this using a dummy handler.
716 #ifdef CONFIG_PCMCIA_PROBE
717 static irqreturn_t test_action(int cpl, void *dev_id)
719 return IRQ_NONE;
721 #endif
723 int pcmcia_request_irq(struct pcmcia_device *p_dev, irq_req_t *req)
725 struct pcmcia_socket *s = p_dev->socket;
726 config_t *c;
727 int ret = CS_IN_USE, irq = 0;
728 int type;
730 if (!(s->state & SOCKET_PRESENT))
731 return CS_NO_CARD;
732 c = p_dev->function_config;
733 if (c->state & CONFIG_LOCKED)
734 return CS_CONFIGURATION_LOCKED;
735 if (c->state & CONFIG_IRQ_REQ)
736 return CS_IN_USE;
738 /* Decide what type of interrupt we are registering */
739 type = 0;
740 if (s->functions > 1) /* All of this ought to be handled higher up */
741 type = IRQF_SHARED;
742 if (req->Attributes & IRQ_TYPE_DYNAMIC_SHARING)
743 type = IRQF_SHARED;
745 #ifdef CONFIG_PCMCIA_PROBE
747 #ifdef IRQ_NOAUTOEN
748 /* if the underlying IRQ infrastructure allows for it, only allocate
749 * the IRQ, but do not enable it
751 if (!(req->Attributes & IRQ_HANDLE_PRESENT))
752 type |= IRQ_NOAUTOEN;
753 #endif /* IRQ_NOAUTOEN */
755 if (s->irq.AssignedIRQ != 0) {
756 /* If the interrupt is already assigned, it must be the same */
757 irq = s->irq.AssignedIRQ;
758 } else {
759 int try;
760 u32 mask = s->irq_mask;
761 void *data = &p_dev->dev.driver; /* something unique to this device */
763 for (try = 0; try < 64; try++) {
764 irq = try % 32;
766 /* marked as available by driver, and not blocked by userspace? */
767 if (!((mask >> irq) & 1))
768 continue;
770 /* avoid an IRQ which is already used by a PCMCIA card */
771 if ((try < 32) && pcmcia_used_irq[irq])
772 continue;
774 /* register the correct driver, if possible, of check whether
775 * registering a dummy handle works, i.e. if the IRQ isn't
776 * marked as used by the kernel resource management core */
777 ret = request_irq(irq,
778 (req->Attributes & IRQ_HANDLE_PRESENT) ? req->Handler : test_action,
779 type,
780 p_dev->devname,
781 (req->Attributes & IRQ_HANDLE_PRESENT) ? req->Instance : data);
782 if (!ret) {
783 if (!(req->Attributes & IRQ_HANDLE_PRESENT))
784 free_irq(irq, data);
785 break;
789 #endif
790 /* only assign PCI irq if no IRQ already assigned */
791 if (ret && !s->irq.AssignedIRQ) {
792 if (!s->pci_irq)
793 return ret;
794 type = IRQF_SHARED;
795 irq = s->pci_irq;
798 if (ret && (req->Attributes & IRQ_HANDLE_PRESENT)) {
799 if (request_irq(irq, req->Handler, type, p_dev->devname, req->Instance))
800 return CS_IN_USE;
803 /* Make sure the fact the request type was overridden is passed back */
804 if (type == IRQF_SHARED && !(req->Attributes & IRQ_TYPE_DYNAMIC_SHARING)) {
805 req->Attributes |= IRQ_TYPE_DYNAMIC_SHARING;
806 dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: "
807 "request for exclusive IRQ could not be fulfilled.\n");
808 dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: the driver "
809 "needs updating to supported shared IRQ lines.\n");
811 c->irq.Attributes = req->Attributes;
812 s->irq.AssignedIRQ = req->AssignedIRQ = irq;
813 s->irq.Config++;
815 c->state |= CONFIG_IRQ_REQ;
816 p_dev->_irq = 1;
818 #ifdef CONFIG_PCMCIA_PROBE
819 pcmcia_used_irq[irq]++;
820 #endif
822 return CS_SUCCESS;
823 } /* pcmcia_request_irq */
824 EXPORT_SYMBOL(pcmcia_request_irq);
827 /** pcmcia_request_window
829 * Request_window() establishes a mapping between card memory space
830 * and system memory space.
832 int pcmcia_request_window(struct pcmcia_device **p_dev, win_req_t *req, window_handle_t *wh)
834 struct pcmcia_socket *s = (*p_dev)->socket;
835 window_t *win;
836 u_long align;
837 int w;
839 if (!(s->state & SOCKET_PRESENT))
840 return CS_NO_CARD;
841 if (req->Attributes & (WIN_PAGED | WIN_SHARED))
842 return CS_BAD_ATTRIBUTE;
844 /* Window size defaults to smallest available */
845 if (req->Size == 0)
846 req->Size = s->map_size;
847 align = (((s->features & SS_CAP_MEM_ALIGN) ||
848 (req->Attributes & WIN_STRICT_ALIGN)) ?
849 req->Size : s->map_size);
850 if (req->Size & (s->map_size-1))
851 return CS_BAD_SIZE;
852 if ((req->Base && (s->features & SS_CAP_STATIC_MAP)) ||
853 (req->Base & (align-1)))
854 return CS_BAD_BASE;
855 if (req->Base)
856 align = 0;
858 /* Allocate system memory window */
859 for (w = 0; w < MAX_WIN; w++)
860 if (!(s->state & SOCKET_WIN_REQ(w))) break;
861 if (w == MAX_WIN)
862 return CS_OUT_OF_RESOURCE;
864 win = &s->win[w];
865 win->magic = WINDOW_MAGIC;
866 win->index = w;
867 win->handle = *p_dev;
868 win->sock = s;
870 if (!(s->features & SS_CAP_STATIC_MAP)) {
871 win->ctl.res = pcmcia_find_mem_region(req->Base, req->Size, align,
872 (req->Attributes & WIN_MAP_BELOW_1MB), s);
873 if (!win->ctl.res)
874 return CS_IN_USE;
876 (*p_dev)->_win |= CLIENT_WIN_REQ(w);
878 /* Configure the socket controller */
879 win->ctl.map = w+1;
880 win->ctl.flags = 0;
881 win->ctl.speed = req->AccessSpeed;
882 if (req->Attributes & WIN_MEMORY_TYPE)
883 win->ctl.flags |= MAP_ATTRIB;
884 if (req->Attributes & WIN_ENABLE)
885 win->ctl.flags |= MAP_ACTIVE;
886 if (req->Attributes & WIN_DATA_WIDTH_16)
887 win->ctl.flags |= MAP_16BIT;
888 if (req->Attributes & WIN_USE_WAIT)
889 win->ctl.flags |= MAP_USE_WAIT;
890 win->ctl.card_start = 0;
891 if (s->ops->set_mem_map(s, &win->ctl) != 0)
892 return CS_BAD_ARGS;
893 s->state |= SOCKET_WIN_REQ(w);
895 /* Return window handle */
896 if (s->features & SS_CAP_STATIC_MAP) {
897 req->Base = win->ctl.static_start;
898 } else {
899 req->Base = win->ctl.res->start;
901 *wh = win;
903 return CS_SUCCESS;
904 } /* pcmcia_request_window */
905 EXPORT_SYMBOL(pcmcia_request_window);
907 void pcmcia_disable_device(struct pcmcia_device *p_dev) {
908 pcmcia_release_configuration(p_dev);
909 pcmcia_release_io(p_dev, &p_dev->io);
910 pcmcia_release_irq(p_dev, &p_dev->irq);
911 if (p_dev->win)
912 pcmcia_release_window(p_dev->win);
914 EXPORT_SYMBOL(pcmcia_disable_device);