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/config.h>
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/interrupt.h>
21 #include <linux/delay.h>
22 #include <linux/pci.h>
23 #include <linux/device.h>
25 #define IN_CARD_SERVICES
26 #include <pcmcia/version.h>
27 #include <pcmcia/cs_types.h>
28 #include <pcmcia/ss.h>
29 #include <pcmcia/cs.h>
30 #include <pcmcia/bulkmem.h>
31 #include <pcmcia/cistpl.h>
32 #include <pcmcia/cisreg.h>
33 #include <pcmcia/ds.h>
35 #include "cs_internal.h"
36 #include "ds_internal.h"
39 /* Access speed for IO windows */
40 static int io_speed = 0;
41 module_param(io_speed, int, 0444);
44 #ifdef CONFIG_PCMCIA_PROBE
45 /* mask of IRQs already reserved by other cards, we should avoid using them */
46 static u8 pcmcia_used_irq[NR_IRQS];
51 extern int ds_pc_debug;
52 #define cs_socket_name(skt) ((skt)->dev.class_id)
54 #define ds_dbg(skt, lvl, fmt, arg...) do { \
55 if (ds_pc_debug >= lvl) \
56 printk(KERN_DEBUG "pcmcia_resource: %s: " fmt, \
57 cs_socket_name(skt) , ## arg); \
60 #define ds_dbg(lvl, fmt, arg...) do { } while (0)
67 * Special stuff for managing IO windows, because they are scarce
70 static int alloc_io_space(struct pcmcia_socket *s, u_int attr, ioaddr_t *base,
71 ioaddr_t num, u_int lines)
74 kio_addr_t try, align;
76 align = (*base) ? (lines ? 1<<lines : 0) : 1;
77 if (align && (align < num)) {
79 ds_dbg(s, 0, "odd IO request: num %#x align %#lx\n",
83 while (align && (align < num)) align <<= 1;
85 if (*base & ~(align-1)) {
86 ds_dbg(s, 0, "odd IO request: base %#x align %#lx\n",
90 if ((s->features & SS_CAP_STATIC_MAP) && s->io_offset) {
91 *base = s->io_offset | (*base & 0x0fff);
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 pccard_access_configuration_register(struct pcmcia_socket *s,
168 unsigned int function,
175 if (!s || !s->config)
178 c = &s->config[function];
183 if (!(c->state & CONFIG_LOCKED))
184 return CS_CONFIGURATION_LOCKED;
186 addr = (c->ConfigBase + reg->Offset) >> 1;
188 switch (reg->Action) {
190 pcmcia_read_cis_mem(s, 1, addr, 1, &val);
195 pcmcia_write_cis_mem(s, 1, addr, 1, &val);
202 } /* pccard_access_configuration_register */
204 int pcmcia_access_configuration_register(client_handle_t handle,
207 struct pcmcia_socket *s;
208 if (CHECK_HANDLE(handle))
209 return CS_BAD_HANDLE;
211 return pccard_access_configuration_register(s, handle->Function, reg);
213 EXPORT_SYMBOL(pcmcia_access_configuration_register);
217 int pccard_get_configuration_info(struct pcmcia_socket *s,
218 unsigned int function,
219 config_info_t *config)
223 if (!(s->state & SOCKET_PRESENT))
226 config->Function = function;
228 #ifdef CONFIG_CARDBUS
229 if (s->state & SOCKET_CARDBUS) {
230 memset(config, 0, sizeof(config_info_t));
231 config->Vcc = s->socket.Vcc;
232 config->Vpp1 = config->Vpp2 = s->socket.Vpp;
233 config->Option = s->cb_dev->subordinate->number;
234 if (s->state & SOCKET_CARDBUS_CONFIG) {
235 config->Attributes = CONF_VALID_CLIENT;
236 config->IntType = INT_CARDBUS;
237 config->AssignedIRQ = s->irq.AssignedIRQ;
238 if (config->AssignedIRQ)
239 config->Attributes |= CONF_ENABLE_IRQ;
240 config->BasePort1 = s->io[0].BasePort;
241 config->NumPorts1 = s->io[0].NumPorts;
247 c = (s->config != NULL) ? &s->config[function] : NULL;
249 if ((c == NULL) || !(c->state & CONFIG_LOCKED)) {
250 config->Attributes = 0;
251 config->Vcc = s->socket.Vcc;
252 config->Vpp1 = config->Vpp2 = s->socket.Vpp;
256 /* !!! This is a hack !!! */
257 memcpy(&config->Attributes, &c->Attributes, sizeof(config_t));
258 config->Attributes |= CONF_VALID_CLIENT;
259 config->CardValues = c->CardValues;
260 config->IRQAttributes = c->irq.Attributes;
261 config->AssignedIRQ = s->irq.AssignedIRQ;
262 config->BasePort1 = c->io.BasePort1;
263 config->NumPorts1 = c->io.NumPorts1;
264 config->Attributes1 = c->io.Attributes1;
265 config->BasePort2 = c->io.BasePort2;
266 config->NumPorts2 = c->io.NumPorts2;
267 config->Attributes2 = c->io.Attributes2;
268 config->IOAddrLines = c->io.IOAddrLines;
271 } /* pccard_get_configuration_info */
273 int pcmcia_get_configuration_info(client_handle_t handle,
274 config_info_t *config)
276 struct pcmcia_socket *s;
278 if ((CHECK_HANDLE(handle)) || !config)
279 return CS_BAD_HANDLE;
282 return CS_BAD_HANDLE;
283 return pccard_get_configuration_info(s, handle->Function, config);
285 EXPORT_SYMBOL(pcmcia_get_configuration_info);
288 /** pcmcia_get_window
290 int pcmcia_get_window(struct pcmcia_socket *s, window_handle_t *handle,
291 int idx, win_req_t *req)
296 if (!s || !(s->state & SOCKET_PRESENT))
298 for (w = idx; w < MAX_WIN; w++)
299 if (s->state & SOCKET_WIN_REQ(w))
302 return CS_NO_MORE_ITEMS;
304 req->Base = win->ctl.res->start;
305 req->Size = win->ctl.res->end - win->ctl.res->start + 1;
306 req->AccessSpeed = win->ctl.speed;
308 if (win->ctl.flags & MAP_ATTRIB)
309 req->Attributes |= WIN_MEMORY_TYPE_AM;
310 if (win->ctl.flags & MAP_ACTIVE)
311 req->Attributes |= WIN_ENABLE;
312 if (win->ctl.flags & MAP_16BIT)
313 req->Attributes |= WIN_DATA_WIDTH_16;
314 if (win->ctl.flags & MAP_USE_WAIT)
315 req->Attributes |= WIN_USE_WAIT;
318 } /* pcmcia_get_window */
319 EXPORT_SYMBOL(pcmcia_get_window);
322 /** pccard_get_status
324 * Get the current socket state bits. We don't support the latched
325 * SocketState yet: I haven't seen any point for it.
328 int pccard_get_status(struct pcmcia_socket *s, unsigned int function,
334 s->ops->get_status(s, &val);
335 status->CardState = status->SocketState = 0;
336 status->CardState |= (val & SS_DETECT) ? CS_EVENT_CARD_DETECT : 0;
337 status->CardState |= (val & SS_CARDBUS) ? CS_EVENT_CB_DETECT : 0;
338 status->CardState |= (val & SS_3VCARD) ? CS_EVENT_3VCARD : 0;
339 status->CardState |= (val & SS_XVCARD) ? CS_EVENT_XVCARD : 0;
340 if (s->state & SOCKET_SUSPEND)
341 status->CardState |= CS_EVENT_PM_SUSPEND;
342 if (!(s->state & SOCKET_PRESENT))
345 c = (s->config != NULL) ? &s->config[function] : NULL;
346 if ((c != NULL) && (c->state & CONFIG_LOCKED) &&
347 (c->IntType & (INT_MEMORY_AND_IO | INT_ZOOMED_VIDEO))) {
349 if (c->Present & PRESENT_PIN_REPLACE) {
350 pcmcia_read_cis_mem(s, 1, (c->ConfigBase+CISREG_PRR)>>1, 1, ®);
352 (reg & PRR_WP_STATUS) ? CS_EVENT_WRITE_PROTECT : 0;
354 (reg & PRR_READY_STATUS) ? CS_EVENT_READY_CHANGE : 0;
356 (reg & PRR_BVD2_STATUS) ? CS_EVENT_BATTERY_LOW : 0;
358 (reg & PRR_BVD1_STATUS) ? CS_EVENT_BATTERY_DEAD : 0;
360 /* No PRR? Then assume we're always ready */
361 status->CardState |= CS_EVENT_READY_CHANGE;
363 if (c->Present & PRESENT_EXT_STATUS) {
364 pcmcia_read_cis_mem(s, 1, (c->ConfigBase+CISREG_ESR)>>1, 1, ®);
366 (reg & ESR_REQ_ATTN) ? CS_EVENT_REQUEST_ATTENTION : 0;
371 (val & SS_WRPROT) ? CS_EVENT_WRITE_PROTECT : 0;
373 (val & SS_BATDEAD) ? CS_EVENT_BATTERY_DEAD : 0;
375 (val & SS_BATWARN) ? CS_EVENT_BATTERY_LOW : 0;
377 (val & SS_READY) ? CS_EVENT_READY_CHANGE : 0;
379 } /* pccard_get_status */
381 int pcmcia_get_status(client_handle_t handle, cs_status_t *status)
383 struct pcmcia_socket *s;
384 if (CHECK_HANDLE(handle))
385 return CS_BAD_HANDLE;
387 return pccard_get_status(s, handle->Function, status);
389 EXPORT_SYMBOL(pcmcia_get_status);
393 /** pcmcia_get_mem_page
395 * Change the card address of an already open memory window.
397 int pcmcia_get_mem_page(window_handle_t win, memreq_t *req)
399 if ((win == NULL) || (win->magic != WINDOW_MAGIC))
400 return CS_BAD_HANDLE;
402 req->CardOffset = win->ctl.card_start;
404 } /* pcmcia_get_mem_page */
405 EXPORT_SYMBOL(pcmcia_get_mem_page);
408 int pcmcia_map_mem_page(window_handle_t win, memreq_t *req)
410 struct pcmcia_socket *s;
411 if ((win == NULL) || (win->magic != WINDOW_MAGIC))
412 return CS_BAD_HANDLE;
416 win->ctl.card_start = req->CardOffset;
417 if (s->ops->set_mem_map(s, &win->ctl) != 0)
418 return CS_BAD_OFFSET;
420 } /* pcmcia_map_mem_page */
421 EXPORT_SYMBOL(pcmcia_map_mem_page);
424 /** pcmcia_modify_configuration
426 * Modify a locked socket configuration
428 int pcmcia_modify_configuration(client_handle_t handle,
431 struct pcmcia_socket *s;
434 if (CHECK_HANDLE(handle))
435 return CS_BAD_HANDLE;
438 if (!(s->state & SOCKET_PRESENT))
440 if (!(c->state & CONFIG_LOCKED))
441 return CS_CONFIGURATION_LOCKED;
443 if (mod->Attributes & CONF_IRQ_CHANGE_VALID) {
444 if (mod->Attributes & CONF_ENABLE_IRQ) {
445 c->Attributes |= CONF_ENABLE_IRQ;
446 s->socket.io_irq = s->irq.AssignedIRQ;
448 c->Attributes &= ~CONF_ENABLE_IRQ;
449 s->socket.io_irq = 0;
451 s->ops->set_socket(s, &s->socket);
454 if (mod->Attributes & CONF_VCC_CHANGE_VALID)
457 /* We only allow changing Vpp1 and Vpp2 to the same value */
458 if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) &&
459 (mod->Attributes & CONF_VPP2_CHANGE_VALID)) {
460 if (mod->Vpp1 != mod->Vpp2)
462 c->Vpp1 = c->Vpp2 = s->socket.Vpp = mod->Vpp1;
463 if (s->ops->set_socket(s, &s->socket))
465 } else if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) ||
466 (mod->Attributes & CONF_VPP2_CHANGE_VALID))
470 } /* modify_configuration */
471 EXPORT_SYMBOL(pcmcia_modify_configuration);
474 int pcmcia_release_configuration(client_handle_t handle)
476 pccard_io_map io = { 0, 0, 0, 0, 1 };
477 struct pcmcia_socket *s;
480 if (CHECK_HANDLE(handle) ||
481 !(handle->state & CLIENT_CONFIG_LOCKED))
482 return CS_BAD_HANDLE;
483 handle->state &= ~CLIENT_CONFIG_LOCKED;
486 #ifdef CONFIG_CARDBUS
487 if (handle->state & CLIENT_CARDBUS)
491 if (!(handle->state & CLIENT_STALE)) {
492 config_t *c = CONFIG(handle);
493 if (--(s->lock_count) == 0) {
494 s->socket.flags = SS_OUTPUT_ENA; /* Is this correct? */
496 s->socket.io_irq = 0;
497 s->ops->set_socket(s, &s->socket);
499 if (c->state & CONFIG_IO_REQ)
500 for (i = 0; i < MAX_IO_WIN; i++) {
501 if (s->io[i].NumPorts == 0)
504 if (s->io[i].Config != 0)
507 s->ops->set_io_map(s, &io);
509 c->state &= ~CONFIG_LOCKED;
513 } /* pcmcia_release_configuration */
514 EXPORT_SYMBOL(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 int pcmcia_release_io(client_handle_t handle, io_req_t *req)
527 struct pcmcia_socket *s;
529 if (CHECK_HANDLE(handle) || !(handle->state & CLIENT_IO_REQ))
530 return CS_BAD_HANDLE;
531 handle->state &= ~CLIENT_IO_REQ;
534 #ifdef CONFIG_CARDBUS
535 if (handle->state & CLIENT_CARDBUS)
539 if (!(handle->state & CLIENT_STALE)) {
540 config_t *c = CONFIG(handle);
541 if (c->state & CONFIG_LOCKED)
542 return CS_CONFIGURATION_LOCKED;
543 if ((c->io.BasePort1 != req->BasePort1) ||
544 (c->io.NumPorts1 != req->NumPorts1) ||
545 (c->io.BasePort2 != req->BasePort2) ||
546 (c->io.NumPorts2 != req->NumPorts2))
548 c->state &= ~CONFIG_IO_REQ;
551 release_io_space(s, req->BasePort1, req->NumPorts1);
553 release_io_space(s, req->BasePort2, req->NumPorts2);
556 } /* pcmcia_release_io */
557 EXPORT_SYMBOL(pcmcia_release_io);
560 int pcmcia_release_irq(client_handle_t handle, irq_req_t *req)
562 struct pcmcia_socket *s;
563 if (CHECK_HANDLE(handle) || !(handle->state & CLIENT_IRQ_REQ))
564 return CS_BAD_HANDLE;
565 handle->state &= ~CLIENT_IRQ_REQ;
568 if (!(handle->state & CLIENT_STALE)) {
569 config_t *c = CONFIG(handle);
570 if (c->state & CONFIG_LOCKED)
571 return CS_CONFIGURATION_LOCKED;
572 if (c->irq.Attributes != req->Attributes)
573 return CS_BAD_ATTRIBUTE;
574 if (s->irq.AssignedIRQ != req->AssignedIRQ)
576 if (--s->irq.Config == 0) {
577 c->state &= ~CONFIG_IRQ_REQ;
578 s->irq.AssignedIRQ = 0;
582 if (req->Attributes & IRQ_HANDLE_PRESENT) {
583 free_irq(req->AssignedIRQ, req->Instance);
586 #ifdef CONFIG_PCMCIA_PROBE
587 pcmcia_used_irq[req->AssignedIRQ]--;
591 } /* pcmcia_release_irq */
592 EXPORT_SYMBOL(pcmcia_release_irq);
595 int pcmcia_release_window(window_handle_t win)
597 struct pcmcia_socket *s;
599 if ((win == NULL) || (win->magic != WINDOW_MAGIC))
600 return CS_BAD_HANDLE;
602 if (!(win->handle->state & CLIENT_WIN_REQ(win->index)))
603 return CS_BAD_HANDLE;
605 /* Shut down memory window */
606 win->ctl.flags &= ~MAP_ACTIVE;
607 s->ops->set_mem_map(s, &win->ctl);
608 s->state &= ~SOCKET_WIN_REQ(win->index);
610 /* Release system memory */
612 release_resource(win->ctl.res);
616 win->handle->state &= ~CLIENT_WIN_REQ(win->index);
621 } /* pcmcia_release_window */
622 EXPORT_SYMBOL(pcmcia_release_window);
625 int pcmcia_request_configuration(client_handle_t handle,
630 struct pcmcia_socket *s;
634 if (CHECK_HANDLE(handle))
635 return CS_BAD_HANDLE;
637 if (!(s->state & SOCKET_PRESENT))
640 #ifdef CONFIG_CARDBUS
641 if (handle->state & CLIENT_CARDBUS)
642 return CS_UNSUPPORTED_MODE;
645 if (req->IntType & INT_CARDBUS)
646 return CS_UNSUPPORTED_MODE;
648 if (c->state & CONFIG_LOCKED)
649 return CS_CONFIGURATION_LOCKED;
651 /* Do power control. We don't allow changes in Vcc. */
652 if (s->socket.Vcc != req->Vcc)
654 if (req->Vpp1 != req->Vpp2)
656 s->socket.Vpp = req->Vpp1;
657 if (s->ops->set_socket(s, &s->socket))
660 c->Vcc = req->Vcc; c->Vpp1 = c->Vpp2 = req->Vpp1;
662 /* Pick memory or I/O card, DMA mode, interrupt */
663 c->IntType = req->IntType;
664 c->Attributes = req->Attributes;
665 if (req->IntType & INT_MEMORY_AND_IO)
666 s->socket.flags |= SS_IOCARD;
667 if (req->IntType & INT_ZOOMED_VIDEO)
668 s->socket.flags |= SS_ZVCARD | SS_IOCARD;
669 if (req->Attributes & CONF_ENABLE_DMA)
670 s->socket.flags |= SS_DMA_MODE;
671 if (req->Attributes & CONF_ENABLE_SPKR)
672 s->socket.flags |= SS_SPKR_ENA;
673 if (req->Attributes & CONF_ENABLE_IRQ)
674 s->socket.io_irq = s->irq.AssignedIRQ;
676 s->socket.io_irq = 0;
677 s->ops->set_socket(s, &s->socket);
680 /* Set up CIS configuration registers */
681 base = c->ConfigBase = req->ConfigBase;
682 c->Present = c->CardValues = req->Present;
683 if (req->Present & PRESENT_COPY) {
685 pcmcia_write_cis_mem(s, 1, (base + CISREG_SCR)>>1, 1, &c->Copy);
687 if (req->Present & PRESENT_OPTION) {
688 if (s->functions == 1) {
689 c->Option = req->ConfigIndex & COR_CONFIG_MASK;
691 c->Option = req->ConfigIndex & COR_MFC_CONFIG_MASK;
692 c->Option |= COR_FUNC_ENA|COR_IREQ_ENA;
693 if (req->Present & PRESENT_IOBASE_0)
694 c->Option |= COR_ADDR_DECODE;
696 if (c->state & CONFIG_IRQ_REQ)
697 if (!(c->irq.Attributes & IRQ_FORCED_PULSE))
698 c->Option |= COR_LEVEL_REQ;
699 pcmcia_write_cis_mem(s, 1, (base + CISREG_COR)>>1, 1, &c->Option);
702 if (req->Present & PRESENT_STATUS) {
703 c->Status = req->Status;
704 pcmcia_write_cis_mem(s, 1, (base + CISREG_CCSR)>>1, 1, &c->Status);
706 if (req->Present & PRESENT_PIN_REPLACE) {
708 pcmcia_write_cis_mem(s, 1, (base + CISREG_PRR)>>1, 1, &c->Pin);
710 if (req->Present & PRESENT_EXT_STATUS) {
711 c->ExtStatus = req->ExtStatus;
712 pcmcia_write_cis_mem(s, 1, (base + CISREG_ESR)>>1, 1, &c->ExtStatus);
714 if (req->Present & PRESENT_IOBASE_0) {
715 u_char b = c->io.BasePort1 & 0xff;
716 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_0)>>1, 1, &b);
717 b = (c->io.BasePort1 >> 8) & 0xff;
718 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_1)>>1, 1, &b);
720 if (req->Present & PRESENT_IOSIZE) {
721 u_char b = c->io.NumPorts1 + c->io.NumPorts2 - 1;
722 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOSIZE)>>1, 1, &b);
725 /* Configure I/O windows */
726 if (c->state & CONFIG_IO_REQ) {
727 iomap.speed = io_speed;
728 for (i = 0; i < MAX_IO_WIN; i++)
729 if (s->io[i].NumPorts != 0) {
731 iomap.flags = MAP_ACTIVE;
732 switch (s->io[i].Attributes & IO_DATA_PATH_WIDTH) {
733 case IO_DATA_PATH_WIDTH_16:
734 iomap.flags |= MAP_16BIT; break;
735 case IO_DATA_PATH_WIDTH_AUTO:
736 iomap.flags |= MAP_AUTOSZ; break;
740 iomap.start = s->io[i].BasePort;
741 iomap.stop = iomap.start + s->io[i].NumPorts - 1;
742 s->ops->set_io_map(s, &iomap);
747 c->state |= CONFIG_LOCKED;
748 handle->state |= CLIENT_CONFIG_LOCKED;
750 } /* pcmcia_request_configuration */
751 EXPORT_SYMBOL(pcmcia_request_configuration);
754 /** pcmcia_request_io
756 * Request_io() reserves ranges of port addresses for a socket.
757 * I have not implemented range sharing or alias addressing.
759 int pcmcia_request_io(client_handle_t handle, io_req_t *req)
761 struct pcmcia_socket *s;
764 if (CHECK_HANDLE(handle))
765 return CS_BAD_HANDLE;
767 if (!(s->state & SOCKET_PRESENT))
770 if (handle->state & CLIENT_CARDBUS) {
771 #ifdef CONFIG_CARDBUS
772 handle->state |= CLIENT_IO_REQ;
775 return CS_UNSUPPORTED_FUNCTION;
780 return CS_UNSUPPORTED_MODE;
782 if (c->state & CONFIG_LOCKED)
783 return CS_CONFIGURATION_LOCKED;
784 if (c->state & CONFIG_IO_REQ)
786 if (req->Attributes1 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS))
787 return CS_BAD_ATTRIBUTE;
788 if ((req->NumPorts2 > 0) &&
789 (req->Attributes2 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS)))
790 return CS_BAD_ATTRIBUTE;
792 if (alloc_io_space(s, req->Attributes1, &req->BasePort1,
793 req->NumPorts1, req->IOAddrLines))
796 if (req->NumPorts2) {
797 if (alloc_io_space(s, req->Attributes2, &req->BasePort2,
798 req->NumPorts2, req->IOAddrLines)) {
799 release_io_space(s, req->BasePort1, req->NumPorts1);
805 c->state |= CONFIG_IO_REQ;
806 handle->state |= CLIENT_IO_REQ;
808 } /* pcmcia_request_io */
809 EXPORT_SYMBOL(pcmcia_request_io);
812 /** pcmcia_request_irq
814 * Request_irq() reserves an irq for this client.
816 * Also, since Linux only reserves irq's when they are actually
817 * hooked, we don't guarantee that an irq will still be available
818 * when the configuration is locked. Now that I think about it,
819 * there might be a way to fix this using a dummy handler.
822 #ifdef CONFIG_PCMCIA_PROBE
823 static irqreturn_t test_action(int cpl, void *dev_id, struct pt_regs *regs)
829 int pcmcia_request_irq(client_handle_t handle, irq_req_t *req)
831 struct pcmcia_socket *s;
833 int ret = CS_IN_USE, irq = 0;
834 struct pcmcia_device *p_dev = handle_to_pdev(handle);
836 if (CHECK_HANDLE(handle))
837 return CS_BAD_HANDLE;
839 if (!(s->state & SOCKET_PRESENT))
842 if (c->state & CONFIG_LOCKED)
843 return CS_CONFIGURATION_LOCKED;
844 if (c->state & CONFIG_IRQ_REQ)
847 #ifdef CONFIG_PCMCIA_PROBE
848 if (s->irq.AssignedIRQ != 0) {
849 /* If the interrupt is already assigned, it must be the same */
850 irq = s->irq.AssignedIRQ;
853 u32 mask = s->irq_mask;
856 for (try = 0; try < 64; try++) {
859 /* marked as available by driver, and not blocked by userspace? */
860 if (!((mask >> irq) & 1))
863 /* avoid an IRQ which is already used by a PCMCIA card */
864 if ((try < 32) && pcmcia_used_irq[irq])
867 /* register the correct driver, if possible, of check whether
868 * registering a dummy handle works, i.e. if the IRQ isn't
869 * marked as used by the kernel resource management core */
870 ret = request_irq(irq,
871 (req->Attributes & IRQ_HANDLE_PRESENT) ? req->Handler : test_action,
872 ((req->Attributes & IRQ_TYPE_DYNAMIC_SHARING) ||
873 (s->functions > 1) ||
874 (irq == s->pci_irq)) ? SA_SHIRQ : 0,
876 (req->Attributes & IRQ_HANDLE_PRESENT) ? req->Instance : data);
878 if (!(req->Attributes & IRQ_HANDLE_PRESENT))
891 if (ret && req->Attributes & IRQ_HANDLE_PRESENT) {
892 if (request_irq(irq, req->Handler,
893 ((req->Attributes & IRQ_TYPE_DYNAMIC_SHARING) ||
894 (s->functions > 1) ||
895 (irq == s->pci_irq)) ? SA_SHIRQ : 0,
896 p_dev->dev.bus_id, req->Instance))
900 c->irq.Attributes = req->Attributes;
901 s->irq.AssignedIRQ = req->AssignedIRQ = irq;
904 c->state |= CONFIG_IRQ_REQ;
905 handle->state |= CLIENT_IRQ_REQ;
907 #ifdef CONFIG_PCMCIA_PROBE
908 pcmcia_used_irq[irq]++;
912 } /* pcmcia_request_irq */
913 EXPORT_SYMBOL(pcmcia_request_irq);
916 /** pcmcia_request_window
918 * Request_window() establishes a mapping between card memory space
919 * and system memory space.
921 int pcmcia_request_window(client_handle_t *handle, win_req_t *req, window_handle_t *wh)
923 struct pcmcia_socket *s;
928 if (CHECK_HANDLE(*handle))
929 return CS_BAD_HANDLE;
930 s = (*handle)->Socket;
931 if (!(s->state & SOCKET_PRESENT))
933 if (req->Attributes & (WIN_PAGED | WIN_SHARED))
934 return CS_BAD_ATTRIBUTE;
936 /* Window size defaults to smallest available */
938 req->Size = s->map_size;
939 align = (((s->features & SS_CAP_MEM_ALIGN) ||
940 (req->Attributes & WIN_STRICT_ALIGN)) ?
941 req->Size : s->map_size);
942 if (req->Size & (s->map_size-1))
944 if ((req->Base && (s->features & SS_CAP_STATIC_MAP)) ||
945 (req->Base & (align-1)))
950 /* Allocate system memory window */
951 for (w = 0; w < MAX_WIN; w++)
952 if (!(s->state & SOCKET_WIN_REQ(w))) break;
954 return CS_OUT_OF_RESOURCE;
957 win->magic = WINDOW_MAGIC;
959 win->handle = *handle;
962 if (!(s->features & SS_CAP_STATIC_MAP)) {
963 win->ctl.res = pcmcia_find_mem_region(req->Base, req->Size, align,
964 (req->Attributes & WIN_MAP_BELOW_1MB), s);
968 (*handle)->state |= CLIENT_WIN_REQ(w);
970 /* Configure the socket controller */
973 win->ctl.speed = req->AccessSpeed;
974 if (req->Attributes & WIN_MEMORY_TYPE)
975 win->ctl.flags |= MAP_ATTRIB;
976 if (req->Attributes & WIN_ENABLE)
977 win->ctl.flags |= MAP_ACTIVE;
978 if (req->Attributes & WIN_DATA_WIDTH_16)
979 win->ctl.flags |= MAP_16BIT;
980 if (req->Attributes & WIN_USE_WAIT)
981 win->ctl.flags |= MAP_USE_WAIT;
982 win->ctl.card_start = 0;
983 if (s->ops->set_mem_map(s, &win->ctl) != 0)
985 s->state |= SOCKET_WIN_REQ(w);
987 /* Return window handle */
988 if (s->features & SS_CAP_STATIC_MAP) {
989 req->Base = win->ctl.static_start;
991 req->Base = win->ctl.res->start;
996 } /* pcmcia_request_window */
997 EXPORT_SYMBOL(pcmcia_request_window);