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 #define IN_CARD_SERVICES
25 #include <pcmcia/cs_types.h>
26 #include <pcmcia/ss.h>
27 #include <pcmcia/cs.h>
28 #include <pcmcia/cistpl.h>
29 #include <pcmcia/cisreg.h>
30 #include <pcmcia/ds.h>
32 #include "cs_internal.h"
33 #include "ds_internal.h"
36 /* Access speed for IO windows */
37 static int io_speed = 0;
38 module_param(io_speed, int, 0444);
41 #ifdef CONFIG_PCMCIA_PROBE
43 /* mask of IRQs already reserved by other cards, we should avoid using them */
44 static u8 pcmcia_used_irq[NR_IRQS];
49 extern int ds_pc_debug;
51 #define ds_dbg(skt, lvl, fmt, arg...) do { \
52 if (ds_pc_debug >= lvl) \
53 printk(KERN_DEBUG "pcmcia_resource: %s: " fmt, \
54 cs_socket_name(skt) , ## arg); \
57 #define ds_dbg(lvl, fmt, arg...) do { } while (0)
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)
71 unsigned int try, align;
73 align = (*base) ? (lines ? 1<<lines : 0) : 1;
74 if (align && (align < num)) {
76 ds_dbg(s, 0, "odd IO request: num %#x align %#x\n",
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",
87 if ((s->features & SS_CAP_STATIC_MAP) && s->io_offset) {
88 *base = s->io_offset | (*base & 0x0fff);
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))
99 for (i = 0; i < MAX_IO_WIN; i++) {
101 s->io[i].res = pcmcia_find_io_region(*base, num, align, s);
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;
109 } else if ((s->io[i].res->flags & IORESOURCE_BITS) != (attr & IORESOURCE_BITS))
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) {
117 s->io[i].InUse += num;
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) {
126 s->io[i].InUse += num;
130 return (i == MAX_IO_WIN);
131 } /* alloc_io_space */
134 static void release_io_space(struct pcmcia_socket *s, unsigned int base,
139 for (i = 0; i < MAX_IO_WIN; i++) {
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);
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,
166 struct pcmcia_socket *s;
171 if (!p_dev || !p_dev->function_config)
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) {
184 pcmcia_read_cis_mem(s, 1, addr, 1, &val);
189 pcmcia_write_cis_mem(s, 1, addr, 1, &val);
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)
206 if (!(s->state & SOCKET_PRESENT))
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;
223 config->BasePort1 = s->io[0].res->start;
224 config->NumPorts1 = s->io[0].res->end - config->BasePort1 + 1;
232 c = p_dev->function_config;
233 config->Function = p_dev->func;
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;
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;
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,
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)
287 if (!s || !(s->state & SOCKET_PRESENT))
289 for (w = idx; w < MAX_WIN; w++)
290 if (s->state & SOCKET_WIN_REQ(w))
293 return CS_NO_MORE_ITEMS;
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;
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;
309 } /* pcmcia_get_window */
310 EXPORT_SYMBOL(pcmcia_get_window);
313 /** pccard_get_status
315 * Get the current socket state bits. We don't support the latched
316 * SocketState yet: I haven't seen any point for it.
319 int pccard_get_status(struct pcmcia_socket *s, struct pcmcia_device *p_dev,
325 s->ops->get_status(s, &val);
326 status->CardState = status->SocketState = 0;
327 status->CardState |= (val & SS_DETECT) ? CS_EVENT_CARD_DETECT : 0;
328 status->CardState |= (val & SS_CARDBUS) ? CS_EVENT_CB_DETECT : 0;
329 status->CardState |= (val & SS_3VCARD) ? CS_EVENT_3VCARD : 0;
330 status->CardState |= (val & SS_XVCARD) ? CS_EVENT_XVCARD : 0;
331 if (s->state & SOCKET_SUSPEND)
332 status->CardState |= CS_EVENT_PM_SUSPEND;
333 if (!(s->state & SOCKET_PRESENT))
336 c = (p_dev) ? p_dev->function_config : NULL;
338 if ((c != NULL) && (c->state & CONFIG_LOCKED) &&
339 (c->IntType & (INT_MEMORY_AND_IO | INT_ZOOMED_VIDEO))) {
341 if (c->CardValues & PRESENT_PIN_REPLACE) {
342 pcmcia_read_cis_mem(s, 1, (c->ConfigBase+CISREG_PRR)>>1, 1, ®);
344 (reg & PRR_WP_STATUS) ? CS_EVENT_WRITE_PROTECT : 0;
346 (reg & PRR_READY_STATUS) ? CS_EVENT_READY_CHANGE : 0;
348 (reg & PRR_BVD2_STATUS) ? CS_EVENT_BATTERY_LOW : 0;
350 (reg & PRR_BVD1_STATUS) ? CS_EVENT_BATTERY_DEAD : 0;
352 /* No PRR? Then assume we're always ready */
353 status->CardState |= CS_EVENT_READY_CHANGE;
355 if (c->CardValues & PRESENT_EXT_STATUS) {
356 pcmcia_read_cis_mem(s, 1, (c->ConfigBase+CISREG_ESR)>>1, 1, ®);
358 (reg & ESR_REQ_ATTN) ? CS_EVENT_REQUEST_ATTENTION : 0;
363 (val & SS_WRPROT) ? CS_EVENT_WRITE_PROTECT : 0;
365 (val & SS_BATDEAD) ? CS_EVENT_BATTERY_DEAD : 0;
367 (val & SS_BATWARN) ? CS_EVENT_BATTERY_LOW : 0;
369 (val & SS_READY) ? CS_EVENT_READY_CHANGE : 0;
371 } /* pccard_get_status */
373 int pcmcia_get_status(struct pcmcia_device *p_dev, cs_status_t *status)
375 return pccard_get_status(p_dev->socket, p_dev, status);
377 EXPORT_SYMBOL(pcmcia_get_status);
381 /** pcmcia_get_mem_page
383 * Change the card address of an already open memory window.
385 int pcmcia_get_mem_page(window_handle_t win, memreq_t *req)
387 if ((win == NULL) || (win->magic != WINDOW_MAGIC))
388 return CS_BAD_HANDLE;
390 req->CardOffset = win->ctl.card_start;
392 } /* pcmcia_get_mem_page */
393 EXPORT_SYMBOL(pcmcia_get_mem_page);
396 int pcmcia_map_mem_page(window_handle_t win, memreq_t *req)
398 struct pcmcia_socket *s;
399 if ((win == NULL) || (win->magic != WINDOW_MAGIC))
400 return CS_BAD_HANDLE;
404 win->ctl.card_start = req->CardOffset;
405 if (s->ops->set_mem_map(s, &win->ctl) != 0)
406 return CS_BAD_OFFSET;
408 } /* pcmcia_map_mem_page */
409 EXPORT_SYMBOL(pcmcia_map_mem_page);
412 /** pcmcia_modify_configuration
414 * Modify a locked socket configuration
416 int pcmcia_modify_configuration(struct pcmcia_device *p_dev,
419 struct pcmcia_socket *s;
423 c = p_dev->function_config;
425 if (!(s->state & SOCKET_PRESENT))
427 if (!(c->state & CONFIG_LOCKED))
428 return CS_CONFIGURATION_LOCKED;
430 if (mod->Attributes & CONF_IRQ_CHANGE_VALID) {
431 if (mod->Attributes & CONF_ENABLE_IRQ) {
432 c->Attributes |= CONF_ENABLE_IRQ;
433 s->socket.io_irq = s->irq.AssignedIRQ;
435 c->Attributes &= ~CONF_ENABLE_IRQ;
436 s->socket.io_irq = 0;
438 s->ops->set_socket(s, &s->socket);
441 if (mod->Attributes & CONF_VCC_CHANGE_VALID)
444 /* We only allow changing Vpp1 and Vpp2 to the same value */
445 if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) &&
446 (mod->Attributes & CONF_VPP2_CHANGE_VALID)) {
447 if (mod->Vpp1 != mod->Vpp2)
449 s->socket.Vpp = mod->Vpp1;
450 if (s->ops->set_socket(s, &s->socket))
452 } else if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) ||
453 (mod->Attributes & CONF_VPP2_CHANGE_VALID))
456 if (mod->Attributes & CONF_IO_CHANGE_WIDTH) {
457 pccard_io_map io_off = { 0, 0, 0, 0, 1 };
461 io_on.speed = io_speed;
462 for (i = 0; i < MAX_IO_WIN; i++) {
468 io_on.flags = MAP_ACTIVE | IO_DATA_PATH_WIDTH_8;
469 io_on.start = s->io[i].res->start;
470 io_on.stop = s->io[i].res->end;
472 s->ops->set_io_map(s, &io_off);
474 s->ops->set_io_map(s, &io_on);
479 } /* modify_configuration */
480 EXPORT_SYMBOL(pcmcia_modify_configuration);
483 int pcmcia_release_configuration(struct pcmcia_device *p_dev)
485 pccard_io_map io = { 0, 0, 0, 0, 1 };
486 struct pcmcia_socket *s = p_dev->socket;
487 config_t *c = p_dev->function_config;
490 if (p_dev->_locked) {
492 if (--(s->lock_count) == 0) {
493 s->socket.flags = SS_OUTPUT_ENA; /* Is this correct? */
495 s->socket.io_irq = 0;
496 s->ops->set_socket(s, &s->socket);
499 if (c->state & CONFIG_LOCKED) {
500 c->state &= ~CONFIG_LOCKED;
501 if (c->state & CONFIG_IO_REQ)
502 for (i = 0; i < MAX_IO_WIN; i++) {
506 if (s->io[i].Config != 0)
509 s->ops->set_io_map(s, &io);
514 } /* pcmcia_release_configuration */
517 /** pcmcia_release_io
519 * Release_io() releases the I/O ranges allocated by a client. This
520 * may be invoked some time after a card ejection has already dumped
521 * the actual socket configuration, so if the client is "stale", we
522 * don't bother checking the port ranges against the current socket
525 static int pcmcia_release_io(struct pcmcia_device *p_dev, io_req_t *req)
527 struct pcmcia_socket *s = p_dev->socket;
528 config_t *c = p_dev->function_config;
531 return CS_BAD_HANDLE;
535 if ((c->io.BasePort1 != req->BasePort1) ||
536 (c->io.NumPorts1 != req->NumPorts1) ||
537 (c->io.BasePort2 != req->BasePort2) ||
538 (c->io.NumPorts2 != req->NumPorts2))
541 c->state &= ~CONFIG_IO_REQ;
543 release_io_space(s, req->BasePort1, req->NumPorts1);
545 release_io_space(s, req->BasePort2, req->NumPorts2);
548 } /* pcmcia_release_io */
551 static int pcmcia_release_irq(struct pcmcia_device *p_dev, irq_req_t *req)
553 struct pcmcia_socket *s = p_dev->socket;
554 config_t *c= p_dev->function_config;
557 return CS_BAD_HANDLE;
560 if (c->state & CONFIG_LOCKED)
561 return CS_CONFIGURATION_LOCKED;
562 if (c->irq.Attributes != req->Attributes)
563 return CS_BAD_ATTRIBUTE;
564 if (s->irq.AssignedIRQ != req->AssignedIRQ)
566 if (--s->irq.Config == 0) {
567 c->state &= ~CONFIG_IRQ_REQ;
568 s->irq.AssignedIRQ = 0;
571 if (req->Attributes & IRQ_HANDLE_PRESENT) {
572 free_irq(req->AssignedIRQ, req->Instance);
575 #ifdef CONFIG_PCMCIA_PROBE
576 pcmcia_used_irq[req->AssignedIRQ]--;
580 } /* pcmcia_release_irq */
583 int pcmcia_release_window(window_handle_t win)
585 struct pcmcia_socket *s;
587 if ((win == NULL) || (win->magic != WINDOW_MAGIC))
588 return CS_BAD_HANDLE;
590 if (!(win->handle->_win & CLIENT_WIN_REQ(win->index)))
591 return CS_BAD_HANDLE;
593 /* Shut down memory window */
594 win->ctl.flags &= ~MAP_ACTIVE;
595 s->ops->set_mem_map(s, &win->ctl);
596 s->state &= ~SOCKET_WIN_REQ(win->index);
598 /* Release system memory */
600 release_resource(win->ctl.res);
604 win->handle->_win &= ~CLIENT_WIN_REQ(win->index);
609 } /* pcmcia_release_window */
610 EXPORT_SYMBOL(pcmcia_release_window);
613 int pcmcia_request_configuration(struct pcmcia_device *p_dev,
618 struct pcmcia_socket *s = p_dev->socket;
622 if (!(s->state & SOCKET_PRESENT))
625 if (req->IntType & INT_CARDBUS)
626 return CS_UNSUPPORTED_MODE;
627 c = p_dev->function_config;
628 if (c->state & CONFIG_LOCKED)
629 return CS_CONFIGURATION_LOCKED;
631 /* Do power control. We don't allow changes in Vcc. */
632 s->socket.Vpp = req->Vpp;
633 if (s->ops->set_socket(s, &s->socket))
636 /* Pick memory or I/O card, DMA mode, interrupt */
637 c->IntType = req->IntType;
638 c->Attributes = req->Attributes;
639 if (req->IntType & INT_MEMORY_AND_IO)
640 s->socket.flags |= SS_IOCARD;
641 if (req->IntType & INT_ZOOMED_VIDEO)
642 s->socket.flags |= SS_ZVCARD | SS_IOCARD;
643 if (req->Attributes & CONF_ENABLE_DMA)
644 s->socket.flags |= SS_DMA_MODE;
645 if (req->Attributes & CONF_ENABLE_SPKR)
646 s->socket.flags |= SS_SPKR_ENA;
647 if (req->Attributes & CONF_ENABLE_IRQ)
648 s->socket.io_irq = s->irq.AssignedIRQ;
650 s->socket.io_irq = 0;
651 s->ops->set_socket(s, &s->socket);
654 /* Set up CIS configuration registers */
655 base = c->ConfigBase = req->ConfigBase;
656 c->CardValues = req->Present;
657 if (req->Present & PRESENT_COPY) {
659 pcmcia_write_cis_mem(s, 1, (base + CISREG_SCR)>>1, 1, &c->Copy);
661 if (req->Present & PRESENT_OPTION) {
662 if (s->functions == 1) {
663 c->Option = req->ConfigIndex & COR_CONFIG_MASK;
665 c->Option = req->ConfigIndex & COR_MFC_CONFIG_MASK;
666 c->Option |= COR_FUNC_ENA|COR_IREQ_ENA;
667 if (req->Present & PRESENT_IOBASE_0)
668 c->Option |= COR_ADDR_DECODE;
670 if (c->state & CONFIG_IRQ_REQ)
671 if (!(c->irq.Attributes & IRQ_FORCED_PULSE))
672 c->Option |= COR_LEVEL_REQ;
673 pcmcia_write_cis_mem(s, 1, (base + CISREG_COR)>>1, 1, &c->Option);
676 if (req->Present & PRESENT_STATUS) {
677 c->Status = req->Status;
678 pcmcia_write_cis_mem(s, 1, (base + CISREG_CCSR)>>1, 1, &c->Status);
680 if (req->Present & PRESENT_PIN_REPLACE) {
682 pcmcia_write_cis_mem(s, 1, (base + CISREG_PRR)>>1, 1, &c->Pin);
684 if (req->Present & PRESENT_EXT_STATUS) {
685 c->ExtStatus = req->ExtStatus;
686 pcmcia_write_cis_mem(s, 1, (base + CISREG_ESR)>>1, 1, &c->ExtStatus);
688 if (req->Present & PRESENT_IOBASE_0) {
689 u_char b = c->io.BasePort1 & 0xff;
690 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_0)>>1, 1, &b);
691 b = (c->io.BasePort1 >> 8) & 0xff;
692 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_1)>>1, 1, &b);
694 if (req->Present & PRESENT_IOSIZE) {
695 u_char b = c->io.NumPorts1 + c->io.NumPorts2 - 1;
696 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOSIZE)>>1, 1, &b);
699 /* Configure I/O windows */
700 if (c->state & CONFIG_IO_REQ) {
701 iomap.speed = io_speed;
702 for (i = 0; i < MAX_IO_WIN; i++)
705 iomap.flags = MAP_ACTIVE;
706 switch (s->io[i].res->flags & IO_DATA_PATH_WIDTH) {
707 case IO_DATA_PATH_WIDTH_16:
708 iomap.flags |= MAP_16BIT; break;
709 case IO_DATA_PATH_WIDTH_AUTO:
710 iomap.flags |= MAP_AUTOSZ; break;
714 iomap.start = s->io[i].res->start;
715 iomap.stop = s->io[i].res->end;
716 s->ops->set_io_map(s, &iomap);
721 c->state |= CONFIG_LOCKED;
724 } /* pcmcia_request_configuration */
725 EXPORT_SYMBOL(pcmcia_request_configuration);
728 /** pcmcia_request_io
730 * Request_io() reserves ranges of port addresses for a socket.
731 * I have not implemented range sharing or alias addressing.
733 int pcmcia_request_io(struct pcmcia_device *p_dev, io_req_t *req)
735 struct pcmcia_socket *s = p_dev->socket;
738 if (!(s->state & SOCKET_PRESENT))
742 return CS_UNSUPPORTED_MODE;
743 c = p_dev->function_config;
744 if (c->state & CONFIG_LOCKED)
745 return CS_CONFIGURATION_LOCKED;
746 if (c->state & CONFIG_IO_REQ)
748 if (req->Attributes1 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS))
749 return CS_BAD_ATTRIBUTE;
750 if ((req->NumPorts2 > 0) &&
751 (req->Attributes2 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS)))
752 return CS_BAD_ATTRIBUTE;
754 if (alloc_io_space(s, req->Attributes1, &req->BasePort1,
755 req->NumPorts1, req->IOAddrLines))
758 if (req->NumPorts2) {
759 if (alloc_io_space(s, req->Attributes2, &req->BasePort2,
760 req->NumPorts2, req->IOAddrLines)) {
761 release_io_space(s, req->BasePort1, req->NumPorts1);
767 c->state |= CONFIG_IO_REQ;
770 } /* pcmcia_request_io */
771 EXPORT_SYMBOL(pcmcia_request_io);
774 /** pcmcia_request_irq
776 * Request_irq() reserves an irq for this client.
778 * Also, since Linux only reserves irq's when they are actually
779 * hooked, we don't guarantee that an irq will still be available
780 * when the configuration is locked. Now that I think about it,
781 * there might be a way to fix this using a dummy handler.
784 #ifdef CONFIG_PCMCIA_PROBE
785 static irqreturn_t test_action(int cpl, void *dev_id)
791 int pcmcia_request_irq(struct pcmcia_device *p_dev, irq_req_t *req)
793 struct pcmcia_socket *s = p_dev->socket;
795 int ret = CS_IN_USE, irq = 0;
798 if (!(s->state & SOCKET_PRESENT))
800 c = p_dev->function_config;
801 if (c->state & CONFIG_LOCKED)
802 return CS_CONFIGURATION_LOCKED;
803 if (c->state & CONFIG_IRQ_REQ)
806 /* Decide what type of interrupt we are registering */
808 if (s->functions > 1) /* All of this ought to be handled higher up */
810 if (req->Attributes & IRQ_TYPE_DYNAMIC_SHARING)
813 #ifdef CONFIG_PCMCIA_PROBE
816 /* if the underlying IRQ infrastructure allows for it, only allocate
817 * the IRQ, but do not enable it
819 if (!(req->Attributes & IRQ_HANDLE_PRESENT))
820 type |= IRQ_NOAUTOEN;
821 #endif /* IRQ_NOAUTOEN */
823 if (s->irq.AssignedIRQ != 0) {
824 /* If the interrupt is already assigned, it must be the same */
825 irq = s->irq.AssignedIRQ;
828 u32 mask = s->irq_mask;
829 void *data = &p_dev->dev.driver; /* something unique to this device */
831 for (try = 0; try < 64; try++) {
834 /* marked as available by driver, and not blocked by userspace? */
835 if (!((mask >> irq) & 1))
838 /* avoid an IRQ which is already used by a PCMCIA card */
839 if ((try < 32) && pcmcia_used_irq[irq])
842 /* register the correct driver, if possible, of check whether
843 * registering a dummy handle works, i.e. if the IRQ isn't
844 * marked as used by the kernel resource management core */
845 ret = request_irq(irq,
846 (req->Attributes & IRQ_HANDLE_PRESENT) ? req->Handler : test_action,
849 (req->Attributes & IRQ_HANDLE_PRESENT) ? req->Instance : data);
851 if (!(req->Attributes & IRQ_HANDLE_PRESENT))
858 /* only assign PCI irq if no IRQ already assigned */
859 if (ret && !s->irq.AssignedIRQ) {
866 if (ret && (req->Attributes & IRQ_HANDLE_PRESENT)) {
867 if (request_irq(irq, req->Handler, type, p_dev->devname, req->Instance))
871 /* Make sure the fact the request type was overridden is passed back */
872 if (type == IRQF_SHARED && !(req->Attributes & IRQ_TYPE_DYNAMIC_SHARING)) {
873 req->Attributes |= IRQ_TYPE_DYNAMIC_SHARING;
874 printk(KERN_WARNING "pcmcia: request for exclusive IRQ could not be fulfilled.\n");
875 printk(KERN_WARNING "pcmcia: the driver needs updating to supported shared IRQ lines.\n");
877 c->irq.Attributes = req->Attributes;
878 s->irq.AssignedIRQ = req->AssignedIRQ = irq;
881 c->state |= CONFIG_IRQ_REQ;
884 #ifdef CONFIG_PCMCIA_PROBE
885 pcmcia_used_irq[irq]++;
889 } /* pcmcia_request_irq */
890 EXPORT_SYMBOL(pcmcia_request_irq);
893 /** pcmcia_request_window
895 * Request_window() establishes a mapping between card memory space
896 * and system memory space.
898 int pcmcia_request_window(struct pcmcia_device **p_dev, win_req_t *req, window_handle_t *wh)
900 struct pcmcia_socket *s = (*p_dev)->socket;
905 if (!(s->state & SOCKET_PRESENT))
907 if (req->Attributes & (WIN_PAGED | WIN_SHARED))
908 return CS_BAD_ATTRIBUTE;
910 /* Window size defaults to smallest available */
912 req->Size = s->map_size;
913 align = (((s->features & SS_CAP_MEM_ALIGN) ||
914 (req->Attributes & WIN_STRICT_ALIGN)) ?
915 req->Size : s->map_size);
916 if (req->Size & (s->map_size-1))
918 if ((req->Base && (s->features & SS_CAP_STATIC_MAP)) ||
919 (req->Base & (align-1)))
924 /* Allocate system memory window */
925 for (w = 0; w < MAX_WIN; w++)
926 if (!(s->state & SOCKET_WIN_REQ(w))) break;
928 return CS_OUT_OF_RESOURCE;
931 win->magic = WINDOW_MAGIC;
933 win->handle = *p_dev;
936 if (!(s->features & SS_CAP_STATIC_MAP)) {
937 win->ctl.res = pcmcia_find_mem_region(req->Base, req->Size, align,
938 (req->Attributes & WIN_MAP_BELOW_1MB), s);
942 (*p_dev)->_win |= CLIENT_WIN_REQ(w);
944 /* Configure the socket controller */
947 win->ctl.speed = req->AccessSpeed;
948 if (req->Attributes & WIN_MEMORY_TYPE)
949 win->ctl.flags |= MAP_ATTRIB;
950 if (req->Attributes & WIN_ENABLE)
951 win->ctl.flags |= MAP_ACTIVE;
952 if (req->Attributes & WIN_DATA_WIDTH_16)
953 win->ctl.flags |= MAP_16BIT;
954 if (req->Attributes & WIN_USE_WAIT)
955 win->ctl.flags |= MAP_USE_WAIT;
956 win->ctl.card_start = 0;
957 if (s->ops->set_mem_map(s, &win->ctl) != 0)
959 s->state |= SOCKET_WIN_REQ(w);
961 /* Return window handle */
962 if (s->features & SS_CAP_STATIC_MAP) {
963 req->Base = win->ctl.static_start;
965 req->Base = win->ctl.res->start;
970 } /* pcmcia_request_window */
971 EXPORT_SYMBOL(pcmcia_request_window);
973 void pcmcia_disable_device(struct pcmcia_device *p_dev) {
974 pcmcia_release_configuration(p_dev);
975 pcmcia_release_io(p_dev, &p_dev->io);
976 pcmcia_release_irq(p_dev, &p_dev->irq);
978 pcmcia_release_window(p_dev->win);
980 EXPORT_SYMBOL(pcmcia_disable_device);