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
;
52 #define ds_dbg(skt, lvl, fmt, arg...) do { \
53 if (ds_pc_debug >= lvl) \
54 printk(KERN_DEBUG "pcmcia_resource: %s: " fmt, \
55 cs_socket_name(skt) , ## arg); \
58 #define ds_dbg(lvl, fmt, arg...) do { } while (0)
65 * Special stuff for managing IO windows, because they are scarce
68 static int alloc_io_space(struct pcmcia_socket
*s
, u_int attr
,
69 unsigned int *base
, unsigned int num
, u_int lines
)
72 unsigned int try, align
;
74 align
= (*base
) ? (lines
? 1<<lines
: 0) : 1;
75 if (align
&& (align
< num
)) {
77 ds_dbg(s
, 0, "odd IO request: num %#x align %#x\n",
81 while (align
&& (align
< num
)) align
<<= 1;
83 if (*base
& ~(align
-1)) {
84 ds_dbg(s
, 0, "odd IO request: base %#x align %#x\n",
88 if ((s
->features
& SS_CAP_STATIC_MAP
) && s
->io_offset
) {
89 *base
= s
->io_offset
| (*base
& 0x0fff);
92 /* Check for an already-allocated window that must conflict with
93 * what was asked for. It is a hack because it does not catch all
94 * potential conflicts, just the most obvious ones.
96 for (i
= 0; i
< MAX_IO_WIN
; i
++)
97 if ((s
->io
[i
].res
) && *base
&&
98 ((s
->io
[i
].res
->start
& (align
-1)) == *base
))
100 for (i
= 0; i
< MAX_IO_WIN
; i
++) {
102 s
->io
[i
].res
= pcmcia_find_io_region(*base
, num
, align
, s
);
104 *base
= s
->io
[i
].res
->start
;
105 s
->io
[i
].res
->flags
= (s
->io
[i
].res
->flags
& ~IORESOURCE_BITS
) | (attr
& IORESOURCE_BITS
);
106 s
->io
[i
].InUse
= num
;
110 } else if ((s
->io
[i
].res
->flags
& IORESOURCE_BITS
) != (attr
& IORESOURCE_BITS
))
112 /* Try to extend top of window */
113 try = s
->io
[i
].res
->end
+ 1;
114 if ((*base
== 0) || (*base
== try))
115 if (pcmcia_adjust_io_region(s
->io
[i
].res
, s
->io
[i
].res
->start
,
116 s
->io
[i
].res
->end
+ num
, s
) == 0) {
118 s
->io
[i
].InUse
+= num
;
121 /* Try to extend bottom of window */
122 try = s
->io
[i
].res
->start
- num
;
123 if ((*base
== 0) || (*base
== try))
124 if (pcmcia_adjust_io_region(s
->io
[i
].res
, s
->io
[i
].res
->start
- num
,
125 s
->io
[i
].res
->end
, s
) == 0) {
127 s
->io
[i
].InUse
+= num
;
131 return (i
== MAX_IO_WIN
);
132 } /* alloc_io_space */
135 static void release_io_space(struct pcmcia_socket
*s
, unsigned int base
,
140 for (i
= 0; i
< MAX_IO_WIN
; i
++) {
143 if ((s
->io
[i
].res
->start
<= base
) &&
144 (s
->io
[i
].res
->end
>= base
+num
-1)) {
145 s
->io
[i
].InUse
-= num
;
146 /* Free the window if no one else is using it */
147 if (s
->io
[i
].InUse
== 0) {
148 release_resource(s
->io
[i
].res
);
154 } /* release_io_space */
157 /** pccard_access_configuration_register
159 * Access_configuration_register() reads and writes configuration
160 * registers in attribute memory. Memory window 0 is reserved for
161 * this and the tuple reading services.
164 int pcmcia_access_configuration_register(struct pcmcia_device
*p_dev
,
167 struct pcmcia_socket
*s
;
172 if (!p_dev
|| !p_dev
->function_config
)
176 c
= p_dev
->function_config
;
178 if (!(c
->state
& CONFIG_LOCKED
))
179 return CS_CONFIGURATION_LOCKED
;
181 addr
= (c
->ConfigBase
+ reg
->Offset
) >> 1;
183 switch (reg
->Action
) {
185 pcmcia_read_cis_mem(s
, 1, addr
, 1, &val
);
190 pcmcia_write_cis_mem(s
, 1, addr
, 1, &val
);
197 } /* pcmcia_access_configuration_register */
198 EXPORT_SYMBOL(pcmcia_access_configuration_register
);
201 int pccard_get_configuration_info(struct pcmcia_socket
*s
,
202 struct pcmcia_device
*p_dev
,
203 config_info_t
*config
)
207 if (!(s
->state
& SOCKET_PRESENT
))
211 #ifdef CONFIG_CARDBUS
212 if (s
->state
& SOCKET_CARDBUS
) {
213 memset(config
, 0, sizeof(config_info_t
));
214 config
->Vcc
= s
->socket
.Vcc
;
215 config
->Vpp1
= config
->Vpp2
= s
->socket
.Vpp
;
216 config
->Option
= s
->cb_dev
->subordinate
->number
;
217 if (s
->state
& SOCKET_CARDBUS_CONFIG
) {
218 config
->Attributes
= CONF_VALID_CLIENT
;
219 config
->IntType
= INT_CARDBUS
;
220 config
->AssignedIRQ
= s
->irq
.AssignedIRQ
;
221 if (config
->AssignedIRQ
)
222 config
->Attributes
|= CONF_ENABLE_IRQ
;
224 config
->BasePort1
= s
->io
[0].res
->start
;
225 config
->NumPorts1
= s
->io
[0].res
->end
- config
->BasePort1
+ 1;
233 c
= p_dev
->function_config
;
234 config
->Function
= p_dev
->func
;
237 config
->Function
= 0;
240 if ((c
== NULL
) || !(c
->state
& CONFIG_LOCKED
)) {
241 config
->Attributes
= 0;
242 config
->Vcc
= s
->socket
.Vcc
;
243 config
->Vpp1
= config
->Vpp2
= s
->socket
.Vpp
;
247 config
->Attributes
= c
->Attributes
| CONF_VALID_CLIENT
;
248 config
->Vcc
= s
->socket
.Vcc
;
249 config
->Vpp1
= config
->Vpp2
= s
->socket
.Vpp
;
250 config
->IntType
= c
->IntType
;
251 config
->ConfigBase
= c
->ConfigBase
;
252 config
->Status
= c
->Status
;
253 config
->Pin
= c
->Pin
;
254 config
->Copy
= c
->Copy
;
255 config
->Option
= c
->Option
;
256 config
->ExtStatus
= c
->ExtStatus
;
257 config
->Present
= config
->CardValues
= c
->CardValues
;
258 config
->IRQAttributes
= c
->irq
.Attributes
;
259 config
->AssignedIRQ
= s
->irq
.AssignedIRQ
;
260 config
->BasePort1
= c
->io
.BasePort1
;
261 config
->NumPorts1
= c
->io
.NumPorts1
;
262 config
->Attributes1
= c
->io
.Attributes1
;
263 config
->BasePort2
= c
->io
.BasePort2
;
264 config
->NumPorts2
= c
->io
.NumPorts2
;
265 config
->Attributes2
= c
->io
.Attributes2
;
266 config
->IOAddrLines
= c
->io
.IOAddrLines
;
269 } /* pccard_get_configuration_info */
271 int pcmcia_get_configuration_info(struct pcmcia_device
*p_dev
,
272 config_info_t
*config
)
274 return pccard_get_configuration_info(p_dev
->socket
, p_dev
,
277 EXPORT_SYMBOL(pcmcia_get_configuration_info
);
280 /** pcmcia_get_window
282 int pcmcia_get_window(struct pcmcia_socket
*s
, window_handle_t
*handle
,
283 int idx
, win_req_t
*req
)
288 if (!s
|| !(s
->state
& SOCKET_PRESENT
))
290 for (w
= idx
; w
< MAX_WIN
; w
++)
291 if (s
->state
& SOCKET_WIN_REQ(w
))
294 return CS_NO_MORE_ITEMS
;
296 req
->Base
= win
->ctl
.res
->start
;
297 req
->Size
= win
->ctl
.res
->end
- win
->ctl
.res
->start
+ 1;
298 req
->AccessSpeed
= win
->ctl
.speed
;
300 if (win
->ctl
.flags
& MAP_ATTRIB
)
301 req
->Attributes
|= WIN_MEMORY_TYPE_AM
;
302 if (win
->ctl
.flags
& MAP_ACTIVE
)
303 req
->Attributes
|= WIN_ENABLE
;
304 if (win
->ctl
.flags
& MAP_16BIT
)
305 req
->Attributes
|= WIN_DATA_WIDTH_16
;
306 if (win
->ctl
.flags
& MAP_USE_WAIT
)
307 req
->Attributes
|= WIN_USE_WAIT
;
310 } /* pcmcia_get_window */
311 EXPORT_SYMBOL(pcmcia_get_window
);
314 /** pccard_get_status
316 * Get the current socket state bits. We don't support the latched
317 * SocketState yet: I haven't seen any point for it.
320 int pccard_get_status(struct pcmcia_socket
*s
, struct pcmcia_device
*p_dev
,
326 s
->ops
->get_status(s
, &val
);
327 status
->CardState
= status
->SocketState
= 0;
328 status
->CardState
|= (val
& SS_DETECT
) ? CS_EVENT_CARD_DETECT
: 0;
329 status
->CardState
|= (val
& SS_CARDBUS
) ? CS_EVENT_CB_DETECT
: 0;
330 status
->CardState
|= (val
& SS_3VCARD
) ? CS_EVENT_3VCARD
: 0;
331 status
->CardState
|= (val
& SS_XVCARD
) ? CS_EVENT_XVCARD
: 0;
332 if (s
->state
& SOCKET_SUSPEND
)
333 status
->CardState
|= CS_EVENT_PM_SUSPEND
;
334 if (!(s
->state
& SOCKET_PRESENT
))
337 c
= (p_dev
) ? p_dev
->function_config
: NULL
;
339 if ((c
!= NULL
) && (c
->state
& CONFIG_LOCKED
) &&
340 (c
->IntType
& (INT_MEMORY_AND_IO
| INT_ZOOMED_VIDEO
))) {
342 if (c
->CardValues
& PRESENT_PIN_REPLACE
) {
343 pcmcia_read_cis_mem(s
, 1, (c
->ConfigBase
+CISREG_PRR
)>>1, 1, ®
);
345 (reg
& PRR_WP_STATUS
) ? CS_EVENT_WRITE_PROTECT
: 0;
347 (reg
& PRR_READY_STATUS
) ? CS_EVENT_READY_CHANGE
: 0;
349 (reg
& PRR_BVD2_STATUS
) ? CS_EVENT_BATTERY_LOW
: 0;
351 (reg
& PRR_BVD1_STATUS
) ? CS_EVENT_BATTERY_DEAD
: 0;
353 /* No PRR? Then assume we're always ready */
354 status
->CardState
|= CS_EVENT_READY_CHANGE
;
356 if (c
->CardValues
& PRESENT_EXT_STATUS
) {
357 pcmcia_read_cis_mem(s
, 1, (c
->ConfigBase
+CISREG_ESR
)>>1, 1, ®
);
359 (reg
& ESR_REQ_ATTN
) ? CS_EVENT_REQUEST_ATTENTION
: 0;
364 (val
& SS_WRPROT
) ? CS_EVENT_WRITE_PROTECT
: 0;
366 (val
& SS_BATDEAD
) ? CS_EVENT_BATTERY_DEAD
: 0;
368 (val
& SS_BATWARN
) ? CS_EVENT_BATTERY_LOW
: 0;
370 (val
& SS_READY
) ? CS_EVENT_READY_CHANGE
: 0;
372 } /* pccard_get_status */
374 int pcmcia_get_status(struct pcmcia_device
*p_dev
, cs_status_t
*status
)
376 return pccard_get_status(p_dev
->socket
, p_dev
, status
);
378 EXPORT_SYMBOL(pcmcia_get_status
);
382 /** pcmcia_get_mem_page
384 * Change the card address of an already open memory window.
386 int pcmcia_get_mem_page(window_handle_t win
, memreq_t
*req
)
388 if ((win
== NULL
) || (win
->magic
!= WINDOW_MAGIC
))
389 return CS_BAD_HANDLE
;
391 req
->CardOffset
= win
->ctl
.card_start
;
393 } /* pcmcia_get_mem_page */
394 EXPORT_SYMBOL(pcmcia_get_mem_page
);
397 int pcmcia_map_mem_page(window_handle_t win
, memreq_t
*req
)
399 struct pcmcia_socket
*s
;
400 if ((win
== NULL
) || (win
->magic
!= WINDOW_MAGIC
))
401 return CS_BAD_HANDLE
;
405 win
->ctl
.card_start
= req
->CardOffset
;
406 if (s
->ops
->set_mem_map(s
, &win
->ctl
) != 0)
407 return CS_BAD_OFFSET
;
409 } /* pcmcia_map_mem_page */
410 EXPORT_SYMBOL(pcmcia_map_mem_page
);
413 /** pcmcia_modify_configuration
415 * Modify a locked socket configuration
417 int pcmcia_modify_configuration(struct pcmcia_device
*p_dev
,
420 struct pcmcia_socket
*s
;
424 c
= p_dev
->function_config
;
426 if (!(s
->state
& SOCKET_PRESENT
))
428 if (!(c
->state
& CONFIG_LOCKED
))
429 return CS_CONFIGURATION_LOCKED
;
431 if (mod
->Attributes
& CONF_IRQ_CHANGE_VALID
) {
432 if (mod
->Attributes
& CONF_ENABLE_IRQ
) {
433 c
->Attributes
|= CONF_ENABLE_IRQ
;
434 s
->socket
.io_irq
= s
->irq
.AssignedIRQ
;
436 c
->Attributes
&= ~CONF_ENABLE_IRQ
;
437 s
->socket
.io_irq
= 0;
439 s
->ops
->set_socket(s
, &s
->socket
);
442 if (mod
->Attributes
& CONF_VCC_CHANGE_VALID
)
445 /* We only allow changing Vpp1 and Vpp2 to the same value */
446 if ((mod
->Attributes
& CONF_VPP1_CHANGE_VALID
) &&
447 (mod
->Attributes
& CONF_VPP2_CHANGE_VALID
)) {
448 if (mod
->Vpp1
!= mod
->Vpp2
)
450 s
->socket
.Vpp
= mod
->Vpp1
;
451 if (s
->ops
->set_socket(s
, &s
->socket
))
453 } else if ((mod
->Attributes
& CONF_VPP1_CHANGE_VALID
) ||
454 (mod
->Attributes
& CONF_VPP2_CHANGE_VALID
))
457 if (mod
->Attributes
& CONF_IO_CHANGE_WIDTH
) {
458 pccard_io_map io_off
= { 0, 0, 0, 0, 1 };
462 io_on
.speed
= io_speed
;
463 for (i
= 0; i
< MAX_IO_WIN
; i
++) {
469 io_on
.flags
= MAP_ACTIVE
| IO_DATA_PATH_WIDTH_8
;
470 io_on
.start
= s
->io
[i
].res
->start
;
471 io_on
.stop
= s
->io
[i
].res
->end
;
473 s
->ops
->set_io_map(s
, &io_off
);
475 s
->ops
->set_io_map(s
, &io_on
);
480 } /* modify_configuration */
481 EXPORT_SYMBOL(pcmcia_modify_configuration
);
484 int pcmcia_release_configuration(struct pcmcia_device
*p_dev
)
486 pccard_io_map io
= { 0, 0, 0, 0, 1 };
487 struct pcmcia_socket
*s
= p_dev
->socket
;
488 config_t
*c
= p_dev
->function_config
;
491 if (p_dev
->_locked
) {
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
);
500 if (c
->state
& CONFIG_LOCKED
) {
501 c
->state
&= ~CONFIG_LOCKED
;
502 if (c
->state
& CONFIG_IO_REQ
)
503 for (i
= 0; i
< MAX_IO_WIN
; i
++) {
507 if (s
->io
[i
].Config
!= 0)
510 s
->ops
->set_io_map(s
, &io
);
515 } /* pcmcia_release_configuration */
518 /** pcmcia_release_io
520 * Release_io() releases the I/O ranges allocated by a client. This
521 * may be invoked some time after a card ejection has already dumped
522 * the actual socket configuration, so if the client is "stale", we
523 * don't bother checking the port ranges against the current socket
526 static int pcmcia_release_io(struct pcmcia_device
*p_dev
, io_req_t
*req
)
528 struct pcmcia_socket
*s
= p_dev
->socket
;
529 config_t
*c
= p_dev
->function_config
;
532 return CS_BAD_HANDLE
;
536 if ((c
->io
.BasePort1
!= req
->BasePort1
) ||
537 (c
->io
.NumPorts1
!= req
->NumPorts1
) ||
538 (c
->io
.BasePort2
!= req
->BasePort2
) ||
539 (c
->io
.NumPorts2
!= req
->NumPorts2
))
542 c
->state
&= ~CONFIG_IO_REQ
;
544 release_io_space(s
, req
->BasePort1
, req
->NumPorts1
);
546 release_io_space(s
, req
->BasePort2
, req
->NumPorts2
);
549 } /* pcmcia_release_io */
552 static int pcmcia_release_irq(struct pcmcia_device
*p_dev
, irq_req_t
*req
)
554 struct pcmcia_socket
*s
= p_dev
->socket
;
555 config_t
*c
= p_dev
->function_config
;
558 return CS_BAD_HANDLE
;
561 if (c
->state
& CONFIG_LOCKED
)
562 return CS_CONFIGURATION_LOCKED
;
563 if (c
->irq
.Attributes
!= req
->Attributes
)
564 return CS_BAD_ATTRIBUTE
;
565 if (s
->irq
.AssignedIRQ
!= req
->AssignedIRQ
)
567 if (--s
->irq
.Config
== 0) {
568 c
->state
&= ~CONFIG_IRQ_REQ
;
569 s
->irq
.AssignedIRQ
= 0;
572 if (req
->Attributes
& IRQ_HANDLE_PRESENT
) {
573 free_irq(req
->AssignedIRQ
, req
->Instance
);
576 #ifdef CONFIG_PCMCIA_PROBE
577 pcmcia_used_irq
[req
->AssignedIRQ
]--;
581 } /* pcmcia_release_irq */
584 int pcmcia_release_window(window_handle_t win
)
586 struct pcmcia_socket
*s
;
588 if ((win
== NULL
) || (win
->magic
!= WINDOW_MAGIC
))
589 return CS_BAD_HANDLE
;
591 if (!(win
->handle
->_win
& CLIENT_WIN_REQ(win
->index
)))
592 return CS_BAD_HANDLE
;
594 /* Shut down memory window */
595 win
->ctl
.flags
&= ~MAP_ACTIVE
;
596 s
->ops
->set_mem_map(s
, &win
->ctl
);
597 s
->state
&= ~SOCKET_WIN_REQ(win
->index
);
599 /* Release system memory */
601 release_resource(win
->ctl
.res
);
605 win
->handle
->_win
&= ~CLIENT_WIN_REQ(win
->index
);
610 } /* pcmcia_release_window */
611 EXPORT_SYMBOL(pcmcia_release_window
);
614 int pcmcia_request_configuration(struct pcmcia_device
*p_dev
,
619 struct pcmcia_socket
*s
= p_dev
->socket
;
623 if (!(s
->state
& SOCKET_PRESENT
))
626 if (req
->IntType
& INT_CARDBUS
)
627 return CS_UNSUPPORTED_MODE
;
628 c
= p_dev
->function_config
;
629 if (c
->state
& CONFIG_LOCKED
)
630 return CS_CONFIGURATION_LOCKED
;
632 /* Do power control. We don't allow changes in Vcc. */
633 s
->socket
.Vpp
= req
->Vpp
;
634 if (s
->ops
->set_socket(s
, &s
->socket
))
637 /* Pick memory or I/O card, DMA mode, interrupt */
638 c
->IntType
= req
->IntType
;
639 c
->Attributes
= req
->Attributes
;
640 if (req
->IntType
& INT_MEMORY_AND_IO
)
641 s
->socket
.flags
|= SS_IOCARD
;
642 if (req
->IntType
& INT_ZOOMED_VIDEO
)
643 s
->socket
.flags
|= SS_ZVCARD
| SS_IOCARD
;
644 if (req
->Attributes
& CONF_ENABLE_DMA
)
645 s
->socket
.flags
|= SS_DMA_MODE
;
646 if (req
->Attributes
& CONF_ENABLE_SPKR
)
647 s
->socket
.flags
|= SS_SPKR_ENA
;
648 if (req
->Attributes
& CONF_ENABLE_IRQ
)
649 s
->socket
.io_irq
= s
->irq
.AssignedIRQ
;
651 s
->socket
.io_irq
= 0;
652 s
->ops
->set_socket(s
, &s
->socket
);
655 /* Set up CIS configuration registers */
656 base
= c
->ConfigBase
= req
->ConfigBase
;
657 c
->CardValues
= req
->Present
;
658 if (req
->Present
& PRESENT_COPY
) {
660 pcmcia_write_cis_mem(s
, 1, (base
+ CISREG_SCR
)>>1, 1, &c
->Copy
);
662 if (req
->Present
& PRESENT_OPTION
) {
663 if (s
->functions
== 1) {
664 c
->Option
= req
->ConfigIndex
& COR_CONFIG_MASK
;
666 c
->Option
= req
->ConfigIndex
& COR_MFC_CONFIG_MASK
;
667 c
->Option
|= COR_FUNC_ENA
|COR_IREQ_ENA
;
668 if (req
->Present
& PRESENT_IOBASE_0
)
669 c
->Option
|= COR_ADDR_DECODE
;
671 if (c
->state
& CONFIG_IRQ_REQ
)
672 if (!(c
->irq
.Attributes
& IRQ_FORCED_PULSE
))
673 c
->Option
|= COR_LEVEL_REQ
;
674 pcmcia_write_cis_mem(s
, 1, (base
+ CISREG_COR
)>>1, 1, &c
->Option
);
677 if (req
->Present
& PRESENT_STATUS
) {
678 c
->Status
= req
->Status
;
679 pcmcia_write_cis_mem(s
, 1, (base
+ CISREG_CCSR
)>>1, 1, &c
->Status
);
681 if (req
->Present
& PRESENT_PIN_REPLACE
) {
683 pcmcia_write_cis_mem(s
, 1, (base
+ CISREG_PRR
)>>1, 1, &c
->Pin
);
685 if (req
->Present
& PRESENT_EXT_STATUS
) {
686 c
->ExtStatus
= req
->ExtStatus
;
687 pcmcia_write_cis_mem(s
, 1, (base
+ CISREG_ESR
)>>1, 1, &c
->ExtStatus
);
689 if (req
->Present
& PRESENT_IOBASE_0
) {
690 u_char b
= c
->io
.BasePort1
& 0xff;
691 pcmcia_write_cis_mem(s
, 1, (base
+ CISREG_IOBASE_0
)>>1, 1, &b
);
692 b
= (c
->io
.BasePort1
>> 8) & 0xff;
693 pcmcia_write_cis_mem(s
, 1, (base
+ CISREG_IOBASE_1
)>>1, 1, &b
);
695 if (req
->Present
& PRESENT_IOSIZE
) {
696 u_char b
= c
->io
.NumPorts1
+ c
->io
.NumPorts2
- 1;
697 pcmcia_write_cis_mem(s
, 1, (base
+ CISREG_IOSIZE
)>>1, 1, &b
);
700 /* Configure I/O windows */
701 if (c
->state
& CONFIG_IO_REQ
) {
702 iomap
.speed
= io_speed
;
703 for (i
= 0; i
< MAX_IO_WIN
; i
++)
706 iomap
.flags
= MAP_ACTIVE
;
707 switch (s
->io
[i
].res
->flags
& IO_DATA_PATH_WIDTH
) {
708 case IO_DATA_PATH_WIDTH_16
:
709 iomap
.flags
|= MAP_16BIT
; break;
710 case IO_DATA_PATH_WIDTH_AUTO
:
711 iomap
.flags
|= MAP_AUTOSZ
; break;
715 iomap
.start
= s
->io
[i
].res
->start
;
716 iomap
.stop
= s
->io
[i
].res
->end
;
717 s
->ops
->set_io_map(s
, &iomap
);
722 c
->state
|= CONFIG_LOCKED
;
725 } /* pcmcia_request_configuration */
726 EXPORT_SYMBOL(pcmcia_request_configuration
);
729 /** pcmcia_request_io
731 * Request_io() reserves ranges of port addresses for a socket.
732 * I have not implemented range sharing or alias addressing.
734 int pcmcia_request_io(struct pcmcia_device
*p_dev
, io_req_t
*req
)
736 struct pcmcia_socket
*s
= p_dev
->socket
;
739 if (!(s
->state
& SOCKET_PRESENT
))
743 return CS_UNSUPPORTED_MODE
;
744 c
= p_dev
->function_config
;
745 if (c
->state
& CONFIG_LOCKED
)
746 return CS_CONFIGURATION_LOCKED
;
747 if (c
->state
& CONFIG_IO_REQ
)
749 if (req
->Attributes1
& (IO_SHARED
| IO_FORCE_ALIAS_ACCESS
))
750 return CS_BAD_ATTRIBUTE
;
751 if ((req
->NumPorts2
> 0) &&
752 (req
->Attributes2
& (IO_SHARED
| IO_FORCE_ALIAS_ACCESS
)))
753 return CS_BAD_ATTRIBUTE
;
755 if (alloc_io_space(s
, req
->Attributes1
, &req
->BasePort1
,
756 req
->NumPorts1
, req
->IOAddrLines
))
759 if (req
->NumPorts2
) {
760 if (alloc_io_space(s
, req
->Attributes2
, &req
->BasePort2
,
761 req
->NumPorts2
, req
->IOAddrLines
)) {
762 release_io_space(s
, req
->BasePort1
, req
->NumPorts1
);
768 c
->state
|= CONFIG_IO_REQ
;
771 } /* pcmcia_request_io */
772 EXPORT_SYMBOL(pcmcia_request_io
);
775 /** pcmcia_request_irq
777 * Request_irq() reserves an irq for this client.
779 * Also, since Linux only reserves irq's when they are actually
780 * hooked, we don't guarantee that an irq will still be available
781 * when the configuration is locked. Now that I think about it,
782 * there might be a way to fix this using a dummy handler.
785 #ifdef CONFIG_PCMCIA_PROBE
786 static irqreturn_t
test_action(int cpl
, void *dev_id
)
792 int pcmcia_request_irq(struct pcmcia_device
*p_dev
, irq_req_t
*req
)
794 struct pcmcia_socket
*s
= p_dev
->socket
;
796 int ret
= CS_IN_USE
, irq
= 0;
799 if (!(s
->state
& SOCKET_PRESENT
))
801 c
= p_dev
->function_config
;
802 if (c
->state
& CONFIG_LOCKED
)
803 return CS_CONFIGURATION_LOCKED
;
804 if (c
->state
& CONFIG_IRQ_REQ
)
807 /* Decide what type of interrupt we are registering */
809 if (s
->functions
> 1) /* All of this ought to be handled higher up */
811 if (req
->Attributes
& IRQ_TYPE_DYNAMIC_SHARING
)
814 #ifdef CONFIG_PCMCIA_PROBE
815 if (s
->irq
.AssignedIRQ
!= 0) {
816 /* If the interrupt is already assigned, it must be the same */
817 irq
= s
->irq
.AssignedIRQ
;
820 u32 mask
= s
->irq_mask
;
821 void *data
= &p_dev
->dev
.driver
; /* something unique to this device */
823 for (try = 0; try < 64; try++) {
826 /* marked as available by driver, and not blocked by userspace? */
827 if (!((mask
>> irq
) & 1))
830 /* avoid an IRQ which is already used by a PCMCIA card */
831 if ((try < 32) && pcmcia_used_irq
[irq
])
834 /* register the correct driver, if possible, of check whether
835 * registering a dummy handle works, i.e. if the IRQ isn't
836 * marked as used by the kernel resource management core */
837 ret
= request_irq(irq
,
838 (req
->Attributes
& IRQ_HANDLE_PRESENT
) ? req
->Handler
: test_action
,
841 (req
->Attributes
& IRQ_HANDLE_PRESENT
) ? req
->Instance
: data
);
843 if (!(req
->Attributes
& IRQ_HANDLE_PRESENT
))
850 /* only assign PCI irq if no IRQ already assigned */
851 if (ret
&& !s
->irq
.AssignedIRQ
) {
858 if (ret
&& (req
->Attributes
& IRQ_HANDLE_PRESENT
)) {
859 if (request_irq(irq
, req
->Handler
, type
, p_dev
->devname
, req
->Instance
))
863 /* Make sure the fact the request type was overridden is passed back */
864 if (type
== IRQF_SHARED
&& !(req
->Attributes
& IRQ_TYPE_DYNAMIC_SHARING
)) {
865 req
->Attributes
|= IRQ_TYPE_DYNAMIC_SHARING
;
866 printk(KERN_WARNING
"pcmcia: request for exclusive IRQ could not be fulfilled.\n");
867 printk(KERN_WARNING
"pcmcia: the driver needs updating to supported shared IRQ lines.\n");
869 c
->irq
.Attributes
= req
->Attributes
;
870 s
->irq
.AssignedIRQ
= req
->AssignedIRQ
= irq
;
873 c
->state
|= CONFIG_IRQ_REQ
;
876 #ifdef CONFIG_PCMCIA_PROBE
877 pcmcia_used_irq
[irq
]++;
881 } /* pcmcia_request_irq */
882 EXPORT_SYMBOL(pcmcia_request_irq
);
885 /** pcmcia_request_window
887 * Request_window() establishes a mapping between card memory space
888 * and system memory space.
890 int pcmcia_request_window(struct pcmcia_device
**p_dev
, win_req_t
*req
, window_handle_t
*wh
)
892 struct pcmcia_socket
*s
= (*p_dev
)->socket
;
897 if (!(s
->state
& SOCKET_PRESENT
))
899 if (req
->Attributes
& (WIN_PAGED
| WIN_SHARED
))
900 return CS_BAD_ATTRIBUTE
;
902 /* Window size defaults to smallest available */
904 req
->Size
= s
->map_size
;
905 align
= (((s
->features
& SS_CAP_MEM_ALIGN
) ||
906 (req
->Attributes
& WIN_STRICT_ALIGN
)) ?
907 req
->Size
: s
->map_size
);
908 if (req
->Size
& (s
->map_size
-1))
910 if ((req
->Base
&& (s
->features
& SS_CAP_STATIC_MAP
)) ||
911 (req
->Base
& (align
-1)))
916 /* Allocate system memory window */
917 for (w
= 0; w
< MAX_WIN
; w
++)
918 if (!(s
->state
& SOCKET_WIN_REQ(w
))) break;
920 return CS_OUT_OF_RESOURCE
;
923 win
->magic
= WINDOW_MAGIC
;
925 win
->handle
= *p_dev
;
928 if (!(s
->features
& SS_CAP_STATIC_MAP
)) {
929 win
->ctl
.res
= pcmcia_find_mem_region(req
->Base
, req
->Size
, align
,
930 (req
->Attributes
& WIN_MAP_BELOW_1MB
), s
);
934 (*p_dev
)->_win
|= CLIENT_WIN_REQ(w
);
936 /* Configure the socket controller */
939 win
->ctl
.speed
= req
->AccessSpeed
;
940 if (req
->Attributes
& WIN_MEMORY_TYPE
)
941 win
->ctl
.flags
|= MAP_ATTRIB
;
942 if (req
->Attributes
& WIN_ENABLE
)
943 win
->ctl
.flags
|= MAP_ACTIVE
;
944 if (req
->Attributes
& WIN_DATA_WIDTH_16
)
945 win
->ctl
.flags
|= MAP_16BIT
;
946 if (req
->Attributes
& WIN_USE_WAIT
)
947 win
->ctl
.flags
|= MAP_USE_WAIT
;
948 win
->ctl
.card_start
= 0;
949 if (s
->ops
->set_mem_map(s
, &win
->ctl
) != 0)
951 s
->state
|= SOCKET_WIN_REQ(w
);
953 /* Return window handle */
954 if (s
->features
& SS_CAP_STATIC_MAP
) {
955 req
->Base
= win
->ctl
.static_start
;
957 req
->Base
= win
->ctl
.res
->start
;
962 } /* pcmcia_request_window */
963 EXPORT_SYMBOL(pcmcia_request_window
);
965 void pcmcia_disable_device(struct pcmcia_device
*p_dev
) {
966 pcmcia_release_configuration(p_dev
);
967 pcmcia_release_io(p_dev
, &p_dev
->io
);
968 pcmcia_release_irq(p_dev
, &p_dev
->irq
);
970 pcmcia_release_window(p_dev
->win
);
972 EXPORT_SYMBOL(pcmcia_disable_device
);