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/bulkmem.h>
29 #include <pcmcia/cistpl.h>
30 #include <pcmcia/cisreg.h>
31 #include <pcmcia/ds.h>
33 #include "cs_internal.h"
34 #include "ds_internal.h"
37 /* Access speed for IO windows */
38 static int io_speed = 0;
39 module_param(io_speed, int, 0444);
42 #ifdef CONFIG_PCMCIA_PROBE
44 /* mask of IRQs already reserved by other cards, we should avoid using them */
45 static u8 pcmcia_used_irq[NR_IRQS];
50 extern int ds_pc_debug;
51 #define cs_socket_name(skt) ((skt)->dev.class_id)
53 #define ds_dbg(skt, lvl, fmt, arg...) do { \
54 if (ds_pc_debug >= lvl) \
55 printk(KERN_DEBUG "pcmcia_resource: %s: " fmt, \
56 cs_socket_name(skt) , ## arg); \
59 #define ds_dbg(lvl, fmt, arg...) do { } while (0)
66 * Special stuff for managing IO windows, because they are scarce
69 static int alloc_io_space(struct pcmcia_socket *s, u_int attr, ioaddr_t *base,
70 ioaddr_t num, u_int lines)
73 kio_addr_t try, align;
75 align = (*base) ? (lines ? 1<<lines : 0) : 1;
76 if (align && (align < num)) {
78 ds_dbg(s, 0, "odd IO request: num %#x align %#lx\n",
82 while (align && (align < num)) align <<= 1;
84 if (*base & ~(align-1)) {
85 ds_dbg(s, 0, "odd IO request: base %#x align %#lx\n",
89 if ((s->features & SS_CAP_STATIC_MAP) && s->io_offset) {
90 *base = s->io_offset | (*base & 0x0fff);
91 s->io[0].Attributes = attr;
94 /* Check for an already-allocated window that must conflict with
95 * what was asked for. It is a hack because it does not catch all
96 * potential conflicts, just the most obvious ones.
98 for (i = 0; i < MAX_IO_WIN; i++)
99 if ((s->io[i].NumPorts != 0) &&
100 ((s->io[i].BasePort & (align-1)) == *base))
102 for (i = 0; i < MAX_IO_WIN; i++) {
103 if (s->io[i].NumPorts == 0) {
104 s->io[i].res = pcmcia_find_io_region(*base, num, align, s);
106 s->io[i].Attributes = attr;
107 s->io[i].BasePort = *base = s->io[i].res->start;
108 s->io[i].NumPorts = s->io[i].InUse = num;
112 } else if (s->io[i].Attributes != attr)
114 /* Try to extend top of window */
115 try = s->io[i].BasePort + s->io[i].NumPorts;
116 if ((*base == 0) || (*base == try))
117 if (pcmcia_adjust_io_region(s->io[i].res, s->io[i].res->start,
118 s->io[i].res->end + num, s) == 0) {
120 s->io[i].NumPorts += num;
121 s->io[i].InUse += num;
124 /* Try to extend bottom of window */
125 try = s->io[i].BasePort - num;
126 if ((*base == 0) || (*base == try))
127 if (pcmcia_adjust_io_region(s->io[i].res, s->io[i].res->start - num,
128 s->io[i].res->end, s) == 0) {
129 s->io[i].BasePort = *base = try;
130 s->io[i].NumPorts += num;
131 s->io[i].InUse += num;
135 return (i == MAX_IO_WIN);
136 } /* alloc_io_space */
139 static void release_io_space(struct pcmcia_socket *s, ioaddr_t base,
144 for (i = 0; i < MAX_IO_WIN; i++) {
145 if ((s->io[i].BasePort <= base) &&
146 (s->io[i].BasePort+s->io[i].NumPorts >= base+num)) {
147 s->io[i].InUse -= num;
148 /* Free the window if no one else is using it */
149 if (s->io[i].InUse == 0) {
150 s->io[i].NumPorts = 0;
151 release_resource(s->io[i].res);
157 } /* release_io_space */
160 /** pccard_access_configuration_register
162 * Access_configuration_register() reads and writes configuration
163 * registers in attribute memory. Memory window 0 is reserved for
164 * this and the tuple reading services.
167 int pcmcia_access_configuration_register(struct pcmcia_device *p_dev,
170 struct pcmcia_socket *s;
175 if (!p_dev || !p_dev->function_config)
179 c = p_dev->function_config;
181 if (!(c->state & CONFIG_LOCKED))
182 return CS_CONFIGURATION_LOCKED;
184 addr = (c->ConfigBase + reg->Offset) >> 1;
186 switch (reg->Action) {
188 pcmcia_read_cis_mem(s, 1, addr, 1, &val);
193 pcmcia_write_cis_mem(s, 1, addr, 1, &val);
200 } /* pcmcia_access_configuration_register */
201 EXPORT_SYMBOL(pcmcia_access_configuration_register);
204 int pccard_get_configuration_info(struct pcmcia_socket *s,
205 struct pcmcia_device *p_dev,
206 config_info_t *config)
210 if (!(s->state & SOCKET_PRESENT))
213 config->Function = p_dev->func;
215 #ifdef CONFIG_CARDBUS
216 if (s->state & SOCKET_CARDBUS) {
217 memset(config, 0, sizeof(config_info_t));
218 config->Vcc = s->socket.Vcc;
219 config->Vpp1 = config->Vpp2 = s->socket.Vpp;
220 config->Option = s->cb_dev->subordinate->number;
221 if (s->state & SOCKET_CARDBUS_CONFIG) {
222 config->Attributes = CONF_VALID_CLIENT;
223 config->IntType = INT_CARDBUS;
224 config->AssignedIRQ = s->irq.AssignedIRQ;
225 if (config->AssignedIRQ)
226 config->Attributes |= CONF_ENABLE_IRQ;
227 config->BasePort1 = s->io[0].BasePort;
228 config->NumPorts1 = s->io[0].NumPorts;
234 c = (p_dev) ? p_dev->function_config : NULL;
236 if ((c == NULL) || !(c->state & CONFIG_LOCKED)) {
237 config->Attributes = 0;
238 config->Vcc = s->socket.Vcc;
239 config->Vpp1 = config->Vpp2 = s->socket.Vpp;
243 /* !!! This is a hack !!! */
244 memcpy(&config->Attributes, &c->Attributes, sizeof(config_t));
245 config->Attributes |= CONF_VALID_CLIENT;
246 config->CardValues = c->CardValues;
247 config->IRQAttributes = c->irq.Attributes;
248 config->AssignedIRQ = s->irq.AssignedIRQ;
249 config->BasePort1 = c->io.BasePort1;
250 config->NumPorts1 = c->io.NumPorts1;
251 config->Attributes1 = c->io.Attributes1;
252 config->BasePort2 = c->io.BasePort2;
253 config->NumPorts2 = c->io.NumPorts2;
254 config->Attributes2 = c->io.Attributes2;
255 config->IOAddrLines = c->io.IOAddrLines;
258 } /* pccard_get_configuration_info */
260 int pcmcia_get_configuration_info(struct pcmcia_device *p_dev,
261 config_info_t *config)
263 return pccard_get_configuration_info(p_dev->socket, p_dev,
266 EXPORT_SYMBOL(pcmcia_get_configuration_info);
269 /** pcmcia_get_window
271 int pcmcia_get_window(struct pcmcia_socket *s, window_handle_t *handle,
272 int idx, win_req_t *req)
277 if (!s || !(s->state & SOCKET_PRESENT))
279 for (w = idx; w < MAX_WIN; w++)
280 if (s->state & SOCKET_WIN_REQ(w))
283 return CS_NO_MORE_ITEMS;
285 req->Base = win->ctl.res->start;
286 req->Size = win->ctl.res->end - win->ctl.res->start + 1;
287 req->AccessSpeed = win->ctl.speed;
289 if (win->ctl.flags & MAP_ATTRIB)
290 req->Attributes |= WIN_MEMORY_TYPE_AM;
291 if (win->ctl.flags & MAP_ACTIVE)
292 req->Attributes |= WIN_ENABLE;
293 if (win->ctl.flags & MAP_16BIT)
294 req->Attributes |= WIN_DATA_WIDTH_16;
295 if (win->ctl.flags & MAP_USE_WAIT)
296 req->Attributes |= WIN_USE_WAIT;
299 } /* pcmcia_get_window */
300 EXPORT_SYMBOL(pcmcia_get_window);
303 /** pccard_get_status
305 * Get the current socket state bits. We don't support the latched
306 * SocketState yet: I haven't seen any point for it.
309 int pccard_get_status(struct pcmcia_socket *s, struct pcmcia_device *p_dev,
315 s->ops->get_status(s, &val);
316 status->CardState = status->SocketState = 0;
317 status->CardState |= (val & SS_DETECT) ? CS_EVENT_CARD_DETECT : 0;
318 status->CardState |= (val & SS_CARDBUS) ? CS_EVENT_CB_DETECT : 0;
319 status->CardState |= (val & SS_3VCARD) ? CS_EVENT_3VCARD : 0;
320 status->CardState |= (val & SS_XVCARD) ? CS_EVENT_XVCARD : 0;
321 if (s->state & SOCKET_SUSPEND)
322 status->CardState |= CS_EVENT_PM_SUSPEND;
323 if (!(s->state & SOCKET_PRESENT))
326 c = (p_dev) ? p_dev->function_config : NULL;
328 if ((c != NULL) && (c->state & CONFIG_LOCKED) &&
329 (c->IntType & (INT_MEMORY_AND_IO | INT_ZOOMED_VIDEO))) {
331 if (c->CardValues & PRESENT_PIN_REPLACE) {
332 pcmcia_read_cis_mem(s, 1, (c->ConfigBase+CISREG_PRR)>>1, 1, ®);
334 (reg & PRR_WP_STATUS) ? CS_EVENT_WRITE_PROTECT : 0;
336 (reg & PRR_READY_STATUS) ? CS_EVENT_READY_CHANGE : 0;
338 (reg & PRR_BVD2_STATUS) ? CS_EVENT_BATTERY_LOW : 0;
340 (reg & PRR_BVD1_STATUS) ? CS_EVENT_BATTERY_DEAD : 0;
342 /* No PRR? Then assume we're always ready */
343 status->CardState |= CS_EVENT_READY_CHANGE;
345 if (c->CardValues & PRESENT_EXT_STATUS) {
346 pcmcia_read_cis_mem(s, 1, (c->ConfigBase+CISREG_ESR)>>1, 1, ®);
348 (reg & ESR_REQ_ATTN) ? CS_EVENT_REQUEST_ATTENTION : 0;
353 (val & SS_WRPROT) ? CS_EVENT_WRITE_PROTECT : 0;
355 (val & SS_BATDEAD) ? CS_EVENT_BATTERY_DEAD : 0;
357 (val & SS_BATWARN) ? CS_EVENT_BATTERY_LOW : 0;
359 (val & SS_READY) ? CS_EVENT_READY_CHANGE : 0;
361 } /* pccard_get_status */
363 int pcmcia_get_status(struct pcmcia_device *p_dev, cs_status_t *status)
365 return pccard_get_status(p_dev->socket, p_dev, status);
367 EXPORT_SYMBOL(pcmcia_get_status);
371 /** pcmcia_get_mem_page
373 * Change the card address of an already open memory window.
375 int pcmcia_get_mem_page(window_handle_t win, memreq_t *req)
377 if ((win == NULL) || (win->magic != WINDOW_MAGIC))
378 return CS_BAD_HANDLE;
380 req->CardOffset = win->ctl.card_start;
382 } /* pcmcia_get_mem_page */
383 EXPORT_SYMBOL(pcmcia_get_mem_page);
386 int pcmcia_map_mem_page(window_handle_t win, memreq_t *req)
388 struct pcmcia_socket *s;
389 if ((win == NULL) || (win->magic != WINDOW_MAGIC))
390 return CS_BAD_HANDLE;
394 win->ctl.card_start = req->CardOffset;
395 if (s->ops->set_mem_map(s, &win->ctl) != 0)
396 return CS_BAD_OFFSET;
398 } /* pcmcia_map_mem_page */
399 EXPORT_SYMBOL(pcmcia_map_mem_page);
402 /** pcmcia_modify_configuration
404 * Modify a locked socket configuration
406 int pcmcia_modify_configuration(struct pcmcia_device *p_dev,
409 struct pcmcia_socket *s;
413 c = p_dev->function_config;
415 if (!(s->state & SOCKET_PRESENT))
417 if (!(c->state & CONFIG_LOCKED))
418 return CS_CONFIGURATION_LOCKED;
420 if (mod->Attributes & CONF_IRQ_CHANGE_VALID) {
421 if (mod->Attributes & CONF_ENABLE_IRQ) {
422 c->Attributes |= CONF_ENABLE_IRQ;
423 s->socket.io_irq = s->irq.AssignedIRQ;
425 c->Attributes &= ~CONF_ENABLE_IRQ;
426 s->socket.io_irq = 0;
428 s->ops->set_socket(s, &s->socket);
431 if (mod->Attributes & CONF_VCC_CHANGE_VALID)
434 /* We only allow changing Vpp1 and Vpp2 to the same value */
435 if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) &&
436 (mod->Attributes & CONF_VPP2_CHANGE_VALID)) {
437 if (mod->Vpp1 != mod->Vpp2)
439 s->socket.Vpp = mod->Vpp1;
440 if (s->ops->set_socket(s, &s->socket))
442 } else if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) ||
443 (mod->Attributes & CONF_VPP2_CHANGE_VALID))
447 } /* modify_configuration */
448 EXPORT_SYMBOL(pcmcia_modify_configuration);
451 int pcmcia_release_configuration(struct pcmcia_device *p_dev)
453 pccard_io_map io = { 0, 0, 0, 0, 1 };
454 struct pcmcia_socket *s = p_dev->socket;
457 if (!(p_dev->state & CLIENT_CONFIG_LOCKED))
458 return CS_BAD_HANDLE;
459 p_dev->state &= ~CLIENT_CONFIG_LOCKED;
461 if (!(p_dev->state & CLIENT_STALE)) {
462 config_t *c = p_dev->function_config;
463 if (--(s->lock_count) == 0) {
464 s->socket.flags = SS_OUTPUT_ENA; /* Is this correct? */
466 s->socket.io_irq = 0;
467 s->ops->set_socket(s, &s->socket);
469 if (c->state & CONFIG_IO_REQ)
470 for (i = 0; i < MAX_IO_WIN; i++) {
471 if (s->io[i].NumPorts == 0)
474 if (s->io[i].Config != 0)
477 s->ops->set_io_map(s, &io);
479 c->state &= ~CONFIG_LOCKED;
483 } /* pcmcia_release_configuration */
484 EXPORT_SYMBOL(pcmcia_release_configuration);
487 /** pcmcia_release_io
489 * Release_io() releases the I/O ranges allocated by a client. This
490 * may be invoked some time after a card ejection has already dumped
491 * the actual socket configuration, so if the client is "stale", we
492 * don't bother checking the port ranges against the current socket
495 int pcmcia_release_io(struct pcmcia_device *p_dev, io_req_t *req)
497 struct pcmcia_socket *s = p_dev->socket;
499 if (!(p_dev->state & CLIENT_IO_REQ))
500 return CS_BAD_HANDLE;
501 p_dev->state &= ~CLIENT_IO_REQ;
503 if (!(p_dev->state & CLIENT_STALE)) {
504 config_t *c = p_dev->function_config;
505 if (c->state & CONFIG_LOCKED)
506 return CS_CONFIGURATION_LOCKED;
507 if ((c->io.BasePort1 != req->BasePort1) ||
508 (c->io.NumPorts1 != req->NumPorts1) ||
509 (c->io.BasePort2 != req->BasePort2) ||
510 (c->io.NumPorts2 != req->NumPorts2))
512 c->state &= ~CONFIG_IO_REQ;
515 release_io_space(s, req->BasePort1, req->NumPorts1);
517 release_io_space(s, req->BasePort2, req->NumPorts2);
520 } /* pcmcia_release_io */
521 EXPORT_SYMBOL(pcmcia_release_io);
524 int pcmcia_release_irq(struct pcmcia_device *p_dev, irq_req_t *req)
526 struct pcmcia_socket *s = p_dev->socket;
527 if (!(p_dev->state & CLIENT_IRQ_REQ))
528 return CS_BAD_HANDLE;
529 p_dev->state &= ~CLIENT_IRQ_REQ;
531 if (!(p_dev->state & CLIENT_STALE)) {
532 config_t *c= p_dev->function_config;
533 if (c->state & CONFIG_LOCKED)
534 return CS_CONFIGURATION_LOCKED;
535 if (c->irq.Attributes != req->Attributes)
536 return CS_BAD_ATTRIBUTE;
537 if (s->irq.AssignedIRQ != req->AssignedIRQ)
539 if (--s->irq.Config == 0) {
540 c->state &= ~CONFIG_IRQ_REQ;
541 s->irq.AssignedIRQ = 0;
545 if (req->Attributes & IRQ_HANDLE_PRESENT) {
546 free_irq(req->AssignedIRQ, req->Instance);
549 #ifdef CONFIG_PCMCIA_PROBE
550 pcmcia_used_irq[req->AssignedIRQ]--;
554 } /* pcmcia_release_irq */
555 EXPORT_SYMBOL(pcmcia_release_irq);
558 int pcmcia_release_window(window_handle_t win)
560 struct pcmcia_socket *s;
562 if ((win == NULL) || (win->magic != WINDOW_MAGIC))
563 return CS_BAD_HANDLE;
565 if (!(win->handle->state & CLIENT_WIN_REQ(win->index)))
566 return CS_BAD_HANDLE;
568 /* Shut down memory window */
569 win->ctl.flags &= ~MAP_ACTIVE;
570 s->ops->set_mem_map(s, &win->ctl);
571 s->state &= ~SOCKET_WIN_REQ(win->index);
573 /* Release system memory */
575 release_resource(win->ctl.res);
579 win->handle->state &= ~CLIENT_WIN_REQ(win->index);
584 } /* pcmcia_release_window */
585 EXPORT_SYMBOL(pcmcia_release_window);
588 int pcmcia_request_configuration(struct pcmcia_device *p_dev,
593 struct pcmcia_socket *s = p_dev->socket;
597 if (!(s->state & SOCKET_PRESENT))
600 if (req->IntType & INT_CARDBUS)
601 return CS_UNSUPPORTED_MODE;
602 c = p_dev->function_config;
603 if (c->state & CONFIG_LOCKED)
604 return CS_CONFIGURATION_LOCKED;
606 /* Do power control. We don't allow changes in Vcc. */
607 if (s->socket.Vcc != req->Vcc)
609 if (req->Vpp1 != req->Vpp2)
611 s->socket.Vpp = req->Vpp1;
612 if (s->ops->set_socket(s, &s->socket))
615 /* Pick memory or I/O card, DMA mode, interrupt */
616 c->IntType = req->IntType;
617 c->Attributes = req->Attributes;
618 if (req->IntType & INT_MEMORY_AND_IO)
619 s->socket.flags |= SS_IOCARD;
620 if (req->IntType & INT_ZOOMED_VIDEO)
621 s->socket.flags |= SS_ZVCARD | SS_IOCARD;
622 if (req->Attributes & CONF_ENABLE_DMA)
623 s->socket.flags |= SS_DMA_MODE;
624 if (req->Attributes & CONF_ENABLE_SPKR)
625 s->socket.flags |= SS_SPKR_ENA;
626 if (req->Attributes & CONF_ENABLE_IRQ)
627 s->socket.io_irq = s->irq.AssignedIRQ;
629 s->socket.io_irq = 0;
630 s->ops->set_socket(s, &s->socket);
633 /* Set up CIS configuration registers */
634 base = c->ConfigBase = req->ConfigBase;
635 c->CardValues = req->Present;
636 if (req->Present & PRESENT_COPY) {
638 pcmcia_write_cis_mem(s, 1, (base + CISREG_SCR)>>1, 1, &c->Copy);
640 if (req->Present & PRESENT_OPTION) {
641 if (s->functions == 1) {
642 c->Option = req->ConfigIndex & COR_CONFIG_MASK;
644 c->Option = req->ConfigIndex & COR_MFC_CONFIG_MASK;
645 c->Option |= COR_FUNC_ENA|COR_IREQ_ENA;
646 if (req->Present & PRESENT_IOBASE_0)
647 c->Option |= COR_ADDR_DECODE;
649 if (c->state & CONFIG_IRQ_REQ)
650 if (!(c->irq.Attributes & IRQ_FORCED_PULSE))
651 c->Option |= COR_LEVEL_REQ;
652 pcmcia_write_cis_mem(s, 1, (base + CISREG_COR)>>1, 1, &c->Option);
655 if (req->Present & PRESENT_STATUS) {
656 c->Status = req->Status;
657 pcmcia_write_cis_mem(s, 1, (base + CISREG_CCSR)>>1, 1, &c->Status);
659 if (req->Present & PRESENT_PIN_REPLACE) {
661 pcmcia_write_cis_mem(s, 1, (base + CISREG_PRR)>>1, 1, &c->Pin);
663 if (req->Present & PRESENT_EXT_STATUS) {
664 c->ExtStatus = req->ExtStatus;
665 pcmcia_write_cis_mem(s, 1, (base + CISREG_ESR)>>1, 1, &c->ExtStatus);
667 if (req->Present & PRESENT_IOBASE_0) {
668 u_char b = c->io.BasePort1 & 0xff;
669 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_0)>>1, 1, &b);
670 b = (c->io.BasePort1 >> 8) & 0xff;
671 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_1)>>1, 1, &b);
673 if (req->Present & PRESENT_IOSIZE) {
674 u_char b = c->io.NumPorts1 + c->io.NumPorts2 - 1;
675 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOSIZE)>>1, 1, &b);
678 /* Configure I/O windows */
679 if (c->state & CONFIG_IO_REQ) {
680 iomap.speed = io_speed;
681 for (i = 0; i < MAX_IO_WIN; i++)
682 if (s->io[i].NumPorts != 0) {
684 iomap.flags = MAP_ACTIVE;
685 switch (s->io[i].Attributes & IO_DATA_PATH_WIDTH) {
686 case IO_DATA_PATH_WIDTH_16:
687 iomap.flags |= MAP_16BIT; break;
688 case IO_DATA_PATH_WIDTH_AUTO:
689 iomap.flags |= MAP_AUTOSZ; break;
693 iomap.start = s->io[i].BasePort;
694 iomap.stop = iomap.start + s->io[i].NumPorts - 1;
695 s->ops->set_io_map(s, &iomap);
700 c->state |= CONFIG_LOCKED;
701 p_dev->state |= CLIENT_CONFIG_LOCKED;
703 } /* pcmcia_request_configuration */
704 EXPORT_SYMBOL(pcmcia_request_configuration);
707 /** pcmcia_request_io
709 * Request_io() reserves ranges of port addresses for a socket.
710 * I have not implemented range sharing or alias addressing.
712 int pcmcia_request_io(struct pcmcia_device *p_dev, io_req_t *req)
714 struct pcmcia_socket *s = p_dev->socket;
717 if (!(s->state & SOCKET_PRESENT))
721 return CS_UNSUPPORTED_MODE;
722 c = p_dev->function_config;
723 if (c->state & CONFIG_LOCKED)
724 return CS_CONFIGURATION_LOCKED;
725 if (c->state & CONFIG_IO_REQ)
727 if (req->Attributes1 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS))
728 return CS_BAD_ATTRIBUTE;
729 if ((req->NumPorts2 > 0) &&
730 (req->Attributes2 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS)))
731 return CS_BAD_ATTRIBUTE;
733 if (alloc_io_space(s, req->Attributes1, &req->BasePort1,
734 req->NumPorts1, req->IOAddrLines))
737 if (req->NumPorts2) {
738 if (alloc_io_space(s, req->Attributes2, &req->BasePort2,
739 req->NumPorts2, req->IOAddrLines)) {
740 release_io_space(s, req->BasePort1, req->NumPorts1);
746 c->state |= CONFIG_IO_REQ;
747 p_dev->state |= CLIENT_IO_REQ;
749 } /* pcmcia_request_io */
750 EXPORT_SYMBOL(pcmcia_request_io);
753 /** pcmcia_request_irq
755 * Request_irq() reserves an irq for this client.
757 * Also, since Linux only reserves irq's when they are actually
758 * hooked, we don't guarantee that an irq will still be available
759 * when the configuration is locked. Now that I think about it,
760 * there might be a way to fix this using a dummy handler.
763 #ifdef CONFIG_PCMCIA_PROBE
764 static irqreturn_t test_action(int cpl, void *dev_id, struct pt_regs *regs)
770 int pcmcia_request_irq(struct pcmcia_device *p_dev, irq_req_t *req)
772 struct pcmcia_socket *s = p_dev->socket;
774 int ret = CS_IN_USE, irq = 0;
776 if (!(s->state & SOCKET_PRESENT))
778 c = p_dev->function_config;
779 if (c->state & CONFIG_LOCKED)
780 return CS_CONFIGURATION_LOCKED;
781 if (c->state & CONFIG_IRQ_REQ)
784 #ifdef CONFIG_PCMCIA_PROBE
785 if (s->irq.AssignedIRQ != 0) {
786 /* If the interrupt is already assigned, it must be the same */
787 irq = s->irq.AssignedIRQ;
790 u32 mask = s->irq_mask;
791 void *data = &p_dev->dev.driver; /* something unique to this device */
793 for (try = 0; try < 64; try++) {
796 /* marked as available by driver, and not blocked by userspace? */
797 if (!((mask >> irq) & 1))
800 /* avoid an IRQ which is already used by a PCMCIA card */
801 if ((try < 32) && pcmcia_used_irq[irq])
804 /* register the correct driver, if possible, of check whether
805 * registering a dummy handle works, i.e. if the IRQ isn't
806 * marked as used by the kernel resource management core */
807 ret = request_irq(irq,
808 (req->Attributes & IRQ_HANDLE_PRESENT) ? req->Handler : test_action,
809 ((req->Attributes & IRQ_TYPE_DYNAMIC_SHARING) ||
810 (s->functions > 1) ||
811 (irq == s->pci_irq)) ? SA_SHIRQ : 0,
813 (req->Attributes & IRQ_HANDLE_PRESENT) ? req->Instance : data);
815 if (!(req->Attributes & IRQ_HANDLE_PRESENT))
822 /* only assign PCI irq if no IRQ already assigned */
823 if (ret && !s->irq.AssignedIRQ) {
829 if (ret && req->Attributes & IRQ_HANDLE_PRESENT) {
830 if (request_irq(irq, req->Handler,
831 ((req->Attributes & IRQ_TYPE_DYNAMIC_SHARING) ||
832 (s->functions > 1) ||
833 (irq == s->pci_irq)) ? SA_SHIRQ : 0,
834 p_dev->devname, req->Instance))
838 c->irq.Attributes = req->Attributes;
839 s->irq.AssignedIRQ = req->AssignedIRQ = irq;
842 c->state |= CONFIG_IRQ_REQ;
843 p_dev->state |= CLIENT_IRQ_REQ;
845 #ifdef CONFIG_PCMCIA_PROBE
846 pcmcia_used_irq[irq]++;
850 } /* pcmcia_request_irq */
851 EXPORT_SYMBOL(pcmcia_request_irq);
854 /** pcmcia_request_window
856 * Request_window() establishes a mapping between card memory space
857 * and system memory space.
859 int pcmcia_request_window(struct pcmcia_device **p_dev, win_req_t *req, window_handle_t *wh)
861 struct pcmcia_socket *s = (*p_dev)->socket;
866 if (!(s->state & SOCKET_PRESENT))
868 if (req->Attributes & (WIN_PAGED | WIN_SHARED))
869 return CS_BAD_ATTRIBUTE;
871 /* Window size defaults to smallest available */
873 req->Size = s->map_size;
874 align = (((s->features & SS_CAP_MEM_ALIGN) ||
875 (req->Attributes & WIN_STRICT_ALIGN)) ?
876 req->Size : s->map_size);
877 if (req->Size & (s->map_size-1))
879 if ((req->Base && (s->features & SS_CAP_STATIC_MAP)) ||
880 (req->Base & (align-1)))
885 /* Allocate system memory window */
886 for (w = 0; w < MAX_WIN; w++)
887 if (!(s->state & SOCKET_WIN_REQ(w))) break;
889 return CS_OUT_OF_RESOURCE;
892 win->magic = WINDOW_MAGIC;
894 win->handle = *p_dev;
897 if (!(s->features & SS_CAP_STATIC_MAP)) {
898 win->ctl.res = pcmcia_find_mem_region(req->Base, req->Size, align,
899 (req->Attributes & WIN_MAP_BELOW_1MB), s);
903 (*p_dev)->state |= CLIENT_WIN_REQ(w);
905 /* Configure the socket controller */
908 win->ctl.speed = req->AccessSpeed;
909 if (req->Attributes & WIN_MEMORY_TYPE)
910 win->ctl.flags |= MAP_ATTRIB;
911 if (req->Attributes & WIN_ENABLE)
912 win->ctl.flags |= MAP_ACTIVE;
913 if (req->Attributes & WIN_DATA_WIDTH_16)
914 win->ctl.flags |= MAP_16BIT;
915 if (req->Attributes & WIN_USE_WAIT)
916 win->ctl.flags |= MAP_USE_WAIT;
917 win->ctl.card_start = 0;
918 if (s->ops->set_mem_map(s, &win->ctl) != 0)
920 s->state |= SOCKET_WIN_REQ(w);
922 /* Return window handle */
923 if (s->features & SS_CAP_STATIC_MAP) {
924 req->Base = win->ctl.static_start;
926 req->Base = win->ctl.res->start;
931 } /* pcmcia_request_window */
932 EXPORT_SYMBOL(pcmcia_request_window);