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].res
->flags
= (s
->io
[0].res
->flags
& ~IORESOURCE_BITS
) | (attr
& IORESOURCE_BITS
);
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
++)
100 ((s
->io
[i
].res
->start
& (align
-1)) == *base
))
102 for (i
= 0; i
< MAX_IO_WIN
; i
++) {
104 s
->io
[i
].res
= pcmcia_find_io_region(*base
, num
, align
, s
);
106 *base
= s
->io
[i
].res
->start
;
107 s
->io
[i
].res
->flags
= (s
->io
[i
].res
->flags
& ~IORESOURCE_BITS
) | (attr
& IORESOURCE_BITS
);
108 s
->io
[i
].InUse
= num
;
112 } else if ((s
->io
[i
].res
->flags
& IORESOURCE_BITS
) != (attr
& IORESOURCE_BITS
))
114 /* Try to extend top of window */
115 try = s
->io
[i
].res
->end
+ 1;
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
].InUse
+= num
;
123 /* Try to extend bottom of window */
124 try = s
->io
[i
].res
->start
- num
;
125 if ((*base
== 0) || (*base
== try))
126 if (pcmcia_adjust_io_region(s
->io
[i
].res
, s
->io
[i
].res
->start
- num
,
127 s
->io
[i
].res
->end
, s
) == 0) {
129 s
->io
[i
].InUse
+= num
;
133 return (i
== MAX_IO_WIN
);
134 } /* alloc_io_space */
137 static void release_io_space(struct pcmcia_socket
*s
, ioaddr_t base
,
142 for (i
= 0; i
< MAX_IO_WIN
; i
++) {
145 if ((s
->io
[i
].res
->start
<= base
) &&
146 (s
->io
[i
].res
->end
>= base
+num
-1)) {
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 release_resource(s
->io
[i
].res
);
156 } /* release_io_space */
159 /** pccard_access_configuration_register
161 * Access_configuration_register() reads and writes configuration
162 * registers in attribute memory. Memory window 0 is reserved for
163 * this and the tuple reading services.
166 int pcmcia_access_configuration_register(struct pcmcia_device
*p_dev
,
169 struct pcmcia_socket
*s
;
174 if (!p_dev
|| !p_dev
->function_config
)
178 c
= p_dev
->function_config
;
180 if (!(c
->state
& CONFIG_LOCKED
))
181 return CS_CONFIGURATION_LOCKED
;
183 addr
= (c
->ConfigBase
+ reg
->Offset
) >> 1;
185 switch (reg
->Action
) {
187 pcmcia_read_cis_mem(s
, 1, addr
, 1, &val
);
192 pcmcia_write_cis_mem(s
, 1, addr
, 1, &val
);
199 } /* pcmcia_access_configuration_register */
200 EXPORT_SYMBOL(pcmcia_access_configuration_register
);
203 int pccard_get_configuration_info(struct pcmcia_socket
*s
,
204 struct pcmcia_device
*p_dev
,
205 config_info_t
*config
)
209 if (!(s
->state
& SOCKET_PRESENT
))
212 config
->Function
= p_dev
->func
;
214 #ifdef CONFIG_CARDBUS
215 if (s
->state
& SOCKET_CARDBUS
) {
216 memset(config
, 0, sizeof(config_info_t
));
217 config
->Vcc
= s
->socket
.Vcc
;
218 config
->Vpp1
= config
->Vpp2
= s
->socket
.Vpp
;
219 config
->Option
= s
->cb_dev
->subordinate
->number
;
220 if (s
->state
& SOCKET_CARDBUS_CONFIG
) {
221 config
->Attributes
= CONF_VALID_CLIENT
;
222 config
->IntType
= INT_CARDBUS
;
223 config
->AssignedIRQ
= s
->irq
.AssignedIRQ
;
224 if (config
->AssignedIRQ
)
225 config
->Attributes
|= CONF_ENABLE_IRQ
;
226 config
->BasePort1
= s
->io
[0].res
->start
;
227 config
->NumPorts1
= s
->io
[0].res
->end
- config
->BasePort1
+ 1;
233 c
= (p_dev
) ? p_dev
->function_config
: NULL
;
235 if ((c
== NULL
) || !(c
->state
& CONFIG_LOCKED
)) {
236 config
->Attributes
= 0;
237 config
->Vcc
= s
->socket
.Vcc
;
238 config
->Vpp1
= config
->Vpp2
= s
->socket
.Vpp
;
242 /* !!! This is a hack !!! */
243 memcpy(&config
->Attributes
, &c
->Attributes
, sizeof(config_t
));
244 config
->Attributes
|= CONF_VALID_CLIENT
;
245 config
->CardValues
= c
->CardValues
;
246 config
->IRQAttributes
= c
->irq
.Attributes
;
247 config
->AssignedIRQ
= s
->irq
.AssignedIRQ
;
248 config
->BasePort1
= c
->io
.BasePort1
;
249 config
->NumPorts1
= c
->io
.NumPorts1
;
250 config
->Attributes1
= c
->io
.Attributes1
;
251 config
->BasePort2
= c
->io
.BasePort2
;
252 config
->NumPorts2
= c
->io
.NumPorts2
;
253 config
->Attributes2
= c
->io
.Attributes2
;
254 config
->IOAddrLines
= c
->io
.IOAddrLines
;
257 } /* pccard_get_configuration_info */
259 int pcmcia_get_configuration_info(struct pcmcia_device
*p_dev
,
260 config_info_t
*config
)
262 return pccard_get_configuration_info(p_dev
->socket
, p_dev
,
265 EXPORT_SYMBOL(pcmcia_get_configuration_info
);
268 /** pcmcia_get_window
270 int pcmcia_get_window(struct pcmcia_socket
*s
, window_handle_t
*handle
,
271 int idx
, win_req_t
*req
)
276 if (!s
|| !(s
->state
& SOCKET_PRESENT
))
278 for (w
= idx
; w
< MAX_WIN
; w
++)
279 if (s
->state
& SOCKET_WIN_REQ(w
))
282 return CS_NO_MORE_ITEMS
;
284 req
->Base
= win
->ctl
.res
->start
;
285 req
->Size
= win
->ctl
.res
->end
- win
->ctl
.res
->start
+ 1;
286 req
->AccessSpeed
= win
->ctl
.speed
;
288 if (win
->ctl
.flags
& MAP_ATTRIB
)
289 req
->Attributes
|= WIN_MEMORY_TYPE_AM
;
290 if (win
->ctl
.flags
& MAP_ACTIVE
)
291 req
->Attributes
|= WIN_ENABLE
;
292 if (win
->ctl
.flags
& MAP_16BIT
)
293 req
->Attributes
|= WIN_DATA_WIDTH_16
;
294 if (win
->ctl
.flags
& MAP_USE_WAIT
)
295 req
->Attributes
|= WIN_USE_WAIT
;
298 } /* pcmcia_get_window */
299 EXPORT_SYMBOL(pcmcia_get_window
);
302 /** pccard_get_status
304 * Get the current socket state bits. We don't support the latched
305 * SocketState yet: I haven't seen any point for it.
308 int pccard_get_status(struct pcmcia_socket
*s
, struct pcmcia_device
*p_dev
,
314 s
->ops
->get_status(s
, &val
);
315 status
->CardState
= status
->SocketState
= 0;
316 status
->CardState
|= (val
& SS_DETECT
) ? CS_EVENT_CARD_DETECT
: 0;
317 status
->CardState
|= (val
& SS_CARDBUS
) ? CS_EVENT_CB_DETECT
: 0;
318 status
->CardState
|= (val
& SS_3VCARD
) ? CS_EVENT_3VCARD
: 0;
319 status
->CardState
|= (val
& SS_XVCARD
) ? CS_EVENT_XVCARD
: 0;
320 if (s
->state
& SOCKET_SUSPEND
)
321 status
->CardState
|= CS_EVENT_PM_SUSPEND
;
322 if (!(s
->state
& SOCKET_PRESENT
))
325 c
= (p_dev
) ? p_dev
->function_config
: NULL
;
327 if ((c
!= NULL
) && (c
->state
& CONFIG_LOCKED
) &&
328 (c
->IntType
& (INT_MEMORY_AND_IO
| INT_ZOOMED_VIDEO
))) {
330 if (c
->CardValues
& PRESENT_PIN_REPLACE
) {
331 pcmcia_read_cis_mem(s
, 1, (c
->ConfigBase
+CISREG_PRR
)>>1, 1, ®
);
333 (reg
& PRR_WP_STATUS
) ? CS_EVENT_WRITE_PROTECT
: 0;
335 (reg
& PRR_READY_STATUS
) ? CS_EVENT_READY_CHANGE
: 0;
337 (reg
& PRR_BVD2_STATUS
) ? CS_EVENT_BATTERY_LOW
: 0;
339 (reg
& PRR_BVD1_STATUS
) ? CS_EVENT_BATTERY_DEAD
: 0;
341 /* No PRR? Then assume we're always ready */
342 status
->CardState
|= CS_EVENT_READY_CHANGE
;
344 if (c
->CardValues
& PRESENT_EXT_STATUS
) {
345 pcmcia_read_cis_mem(s
, 1, (c
->ConfigBase
+CISREG_ESR
)>>1, 1, ®
);
347 (reg
& ESR_REQ_ATTN
) ? CS_EVENT_REQUEST_ATTENTION
: 0;
352 (val
& SS_WRPROT
) ? CS_EVENT_WRITE_PROTECT
: 0;
354 (val
& SS_BATDEAD
) ? CS_EVENT_BATTERY_DEAD
: 0;
356 (val
& SS_BATWARN
) ? CS_EVENT_BATTERY_LOW
: 0;
358 (val
& SS_READY
) ? CS_EVENT_READY_CHANGE
: 0;
360 } /* pccard_get_status */
362 int pcmcia_get_status(struct pcmcia_device
*p_dev
, cs_status_t
*status
)
364 return pccard_get_status(p_dev
->socket
, p_dev
, status
);
366 EXPORT_SYMBOL(pcmcia_get_status
);
370 /** pcmcia_get_mem_page
372 * Change the card address of an already open memory window.
374 int pcmcia_get_mem_page(window_handle_t win
, memreq_t
*req
)
376 if ((win
== NULL
) || (win
->magic
!= WINDOW_MAGIC
))
377 return CS_BAD_HANDLE
;
379 req
->CardOffset
= win
->ctl
.card_start
;
381 } /* pcmcia_get_mem_page */
382 EXPORT_SYMBOL(pcmcia_get_mem_page
);
385 int pcmcia_map_mem_page(window_handle_t win
, memreq_t
*req
)
387 struct pcmcia_socket
*s
;
388 if ((win
== NULL
) || (win
->magic
!= WINDOW_MAGIC
))
389 return CS_BAD_HANDLE
;
393 win
->ctl
.card_start
= req
->CardOffset
;
394 if (s
->ops
->set_mem_map(s
, &win
->ctl
) != 0)
395 return CS_BAD_OFFSET
;
397 } /* pcmcia_map_mem_page */
398 EXPORT_SYMBOL(pcmcia_map_mem_page
);
401 /** pcmcia_modify_configuration
403 * Modify a locked socket configuration
405 int pcmcia_modify_configuration(struct pcmcia_device
*p_dev
,
408 struct pcmcia_socket
*s
;
412 c
= p_dev
->function_config
;
414 if (!(s
->state
& SOCKET_PRESENT
))
416 if (!(c
->state
& CONFIG_LOCKED
))
417 return CS_CONFIGURATION_LOCKED
;
419 if (mod
->Attributes
& CONF_IRQ_CHANGE_VALID
) {
420 if (mod
->Attributes
& CONF_ENABLE_IRQ
) {
421 c
->Attributes
|= CONF_ENABLE_IRQ
;
422 s
->socket
.io_irq
= s
->irq
.AssignedIRQ
;
424 c
->Attributes
&= ~CONF_ENABLE_IRQ
;
425 s
->socket
.io_irq
= 0;
427 s
->ops
->set_socket(s
, &s
->socket
);
430 if (mod
->Attributes
& CONF_VCC_CHANGE_VALID
)
433 /* We only allow changing Vpp1 and Vpp2 to the same value */
434 if ((mod
->Attributes
& CONF_VPP1_CHANGE_VALID
) &&
435 (mod
->Attributes
& CONF_VPP2_CHANGE_VALID
)) {
436 if (mod
->Vpp1
!= mod
->Vpp2
)
438 s
->socket
.Vpp
= mod
->Vpp1
;
439 if (s
->ops
->set_socket(s
, &s
->socket
))
441 } else if ((mod
->Attributes
& CONF_VPP1_CHANGE_VALID
) ||
442 (mod
->Attributes
& CONF_VPP2_CHANGE_VALID
))
445 if (mod
->Attributes
& CONF_IO_CHANGE_WIDTH
) {
446 pccard_io_map io_off
= { 0, 0, 0, 0, 1 };
450 io_on
.speed
= io_speed
;
451 for (i
= 0; i
< MAX_IO_WIN
; i
++) {
457 io_on
.flags
= MAP_ACTIVE
| IO_DATA_PATH_WIDTH_8
;
458 io_on
.start
= s
->io
[i
].res
->start
;
459 io_on
.stop
= s
->io
[i
].res
->end
;
461 s
->ops
->set_io_map(s
, &io_off
);
463 s
->ops
->set_io_map(s
, &io_on
);
468 } /* modify_configuration */
469 EXPORT_SYMBOL(pcmcia_modify_configuration
);
472 int pcmcia_release_configuration(struct pcmcia_device
*p_dev
)
474 pccard_io_map io
= { 0, 0, 0, 0, 1 };
475 struct pcmcia_socket
*s
= p_dev
->socket
;
476 config_t
*c
= p_dev
->function_config
;
479 if (p_dev
->_locked
) {
481 if (--(s
->lock_count
) == 0) {
482 s
->socket
.flags
= SS_OUTPUT_ENA
; /* Is this correct? */
484 s
->socket
.io_irq
= 0;
485 s
->ops
->set_socket(s
, &s
->socket
);
488 if (c
->state
& CONFIG_LOCKED
) {
489 c
->state
&= ~CONFIG_LOCKED
;
490 if (c
->state
& CONFIG_IO_REQ
)
491 for (i
= 0; i
< MAX_IO_WIN
; i
++) {
495 if (s
->io
[i
].Config
!= 0)
498 s
->ops
->set_io_map(s
, &io
);
503 } /* pcmcia_release_configuration */
506 /** pcmcia_release_io
508 * Release_io() releases the I/O ranges allocated by a client. This
509 * may be invoked some time after a card ejection has already dumped
510 * the actual socket configuration, so if the client is "stale", we
511 * don't bother checking the port ranges against the current socket
514 static int pcmcia_release_io(struct pcmcia_device
*p_dev
, io_req_t
*req
)
516 struct pcmcia_socket
*s
= p_dev
->socket
;
517 config_t
*c
= p_dev
->function_config
;
520 return CS_BAD_HANDLE
;
524 if ((c
->io
.BasePort1
!= req
->BasePort1
) ||
525 (c
->io
.NumPorts1
!= req
->NumPorts1
) ||
526 (c
->io
.BasePort2
!= req
->BasePort2
) ||
527 (c
->io
.NumPorts2
!= req
->NumPorts2
))
530 c
->state
&= ~CONFIG_IO_REQ
;
532 release_io_space(s
, req
->BasePort1
, req
->NumPorts1
);
534 release_io_space(s
, req
->BasePort2
, req
->NumPorts2
);
537 } /* pcmcia_release_io */
540 static int pcmcia_release_irq(struct pcmcia_device
*p_dev
, irq_req_t
*req
)
542 struct pcmcia_socket
*s
= p_dev
->socket
;
543 config_t
*c
= p_dev
->function_config
;
546 return CS_BAD_HANDLE
;
549 if (c
->state
& CONFIG_LOCKED
)
550 return CS_CONFIGURATION_LOCKED
;
551 if (c
->irq
.Attributes
!= req
->Attributes
)
552 return CS_BAD_ATTRIBUTE
;
553 if (s
->irq
.AssignedIRQ
!= req
->AssignedIRQ
)
555 if (--s
->irq
.Config
== 0) {
556 c
->state
&= ~CONFIG_IRQ_REQ
;
557 s
->irq
.AssignedIRQ
= 0;
560 if (req
->Attributes
& IRQ_HANDLE_PRESENT
) {
561 free_irq(req
->AssignedIRQ
, req
->Instance
);
564 #ifdef CONFIG_PCMCIA_PROBE
565 pcmcia_used_irq
[req
->AssignedIRQ
]--;
569 } /* pcmcia_release_irq */
572 int pcmcia_release_window(window_handle_t win
)
574 struct pcmcia_socket
*s
;
576 if ((win
== NULL
) || (win
->magic
!= WINDOW_MAGIC
))
577 return CS_BAD_HANDLE
;
579 if (!(win
->handle
->_win
& CLIENT_WIN_REQ(win
->index
)))
580 return CS_BAD_HANDLE
;
582 /* Shut down memory window */
583 win
->ctl
.flags
&= ~MAP_ACTIVE
;
584 s
->ops
->set_mem_map(s
, &win
->ctl
);
585 s
->state
&= ~SOCKET_WIN_REQ(win
->index
);
587 /* Release system memory */
589 release_resource(win
->ctl
.res
);
593 win
->handle
->_win
&= ~CLIENT_WIN_REQ(win
->index
);
598 } /* pcmcia_release_window */
599 EXPORT_SYMBOL(pcmcia_release_window
);
602 int pcmcia_request_configuration(struct pcmcia_device
*p_dev
,
607 struct pcmcia_socket
*s
= p_dev
->socket
;
611 if (!(s
->state
& SOCKET_PRESENT
))
614 if (req
->IntType
& INT_CARDBUS
)
615 return CS_UNSUPPORTED_MODE
;
616 c
= p_dev
->function_config
;
617 if (c
->state
& CONFIG_LOCKED
)
618 return CS_CONFIGURATION_LOCKED
;
620 /* Do power control. We don't allow changes in Vcc. */
621 s
->socket
.Vpp
= req
->Vpp
;
622 if (s
->ops
->set_socket(s
, &s
->socket
))
625 /* Pick memory or I/O card, DMA mode, interrupt */
626 c
->IntType
= req
->IntType
;
627 c
->Attributes
= req
->Attributes
;
628 if (req
->IntType
& INT_MEMORY_AND_IO
)
629 s
->socket
.flags
|= SS_IOCARD
;
630 if (req
->IntType
& INT_ZOOMED_VIDEO
)
631 s
->socket
.flags
|= SS_ZVCARD
| SS_IOCARD
;
632 if (req
->Attributes
& CONF_ENABLE_DMA
)
633 s
->socket
.flags
|= SS_DMA_MODE
;
634 if (req
->Attributes
& CONF_ENABLE_SPKR
)
635 s
->socket
.flags
|= SS_SPKR_ENA
;
636 if (req
->Attributes
& CONF_ENABLE_IRQ
)
637 s
->socket
.io_irq
= s
->irq
.AssignedIRQ
;
639 s
->socket
.io_irq
= 0;
640 s
->ops
->set_socket(s
, &s
->socket
);
643 /* Set up CIS configuration registers */
644 base
= c
->ConfigBase
= req
->ConfigBase
;
645 c
->CardValues
= req
->Present
;
646 if (req
->Present
& PRESENT_COPY
) {
648 pcmcia_write_cis_mem(s
, 1, (base
+ CISREG_SCR
)>>1, 1, &c
->Copy
);
650 if (req
->Present
& PRESENT_OPTION
) {
651 if (s
->functions
== 1) {
652 c
->Option
= req
->ConfigIndex
& COR_CONFIG_MASK
;
654 c
->Option
= req
->ConfigIndex
& COR_MFC_CONFIG_MASK
;
655 c
->Option
|= COR_FUNC_ENA
|COR_IREQ_ENA
;
656 if (req
->Present
& PRESENT_IOBASE_0
)
657 c
->Option
|= COR_ADDR_DECODE
;
659 if (c
->state
& CONFIG_IRQ_REQ
)
660 if (!(c
->irq
.Attributes
& IRQ_FORCED_PULSE
))
661 c
->Option
|= COR_LEVEL_REQ
;
662 pcmcia_write_cis_mem(s
, 1, (base
+ CISREG_COR
)>>1, 1, &c
->Option
);
665 if (req
->Present
& PRESENT_STATUS
) {
666 c
->Status
= req
->Status
;
667 pcmcia_write_cis_mem(s
, 1, (base
+ CISREG_CCSR
)>>1, 1, &c
->Status
);
669 if (req
->Present
& PRESENT_PIN_REPLACE
) {
671 pcmcia_write_cis_mem(s
, 1, (base
+ CISREG_PRR
)>>1, 1, &c
->Pin
);
673 if (req
->Present
& PRESENT_EXT_STATUS
) {
674 c
->ExtStatus
= req
->ExtStatus
;
675 pcmcia_write_cis_mem(s
, 1, (base
+ CISREG_ESR
)>>1, 1, &c
->ExtStatus
);
677 if (req
->Present
& PRESENT_IOBASE_0
) {
678 u_char b
= c
->io
.BasePort1
& 0xff;
679 pcmcia_write_cis_mem(s
, 1, (base
+ CISREG_IOBASE_0
)>>1, 1, &b
);
680 b
= (c
->io
.BasePort1
>> 8) & 0xff;
681 pcmcia_write_cis_mem(s
, 1, (base
+ CISREG_IOBASE_1
)>>1, 1, &b
);
683 if (req
->Present
& PRESENT_IOSIZE
) {
684 u_char b
= c
->io
.NumPorts1
+ c
->io
.NumPorts2
- 1;
685 pcmcia_write_cis_mem(s
, 1, (base
+ CISREG_IOSIZE
)>>1, 1, &b
);
688 /* Configure I/O windows */
689 if (c
->state
& CONFIG_IO_REQ
) {
690 iomap
.speed
= io_speed
;
691 for (i
= 0; i
< MAX_IO_WIN
; i
++)
694 iomap
.flags
= MAP_ACTIVE
;
695 switch (s
->io
[i
].res
->flags
& IO_DATA_PATH_WIDTH
) {
696 case IO_DATA_PATH_WIDTH_16
:
697 iomap
.flags
|= MAP_16BIT
; break;
698 case IO_DATA_PATH_WIDTH_AUTO
:
699 iomap
.flags
|= MAP_AUTOSZ
; break;
703 iomap
.start
= s
->io
[i
].res
->start
;
704 iomap
.stop
= s
->io
[i
].res
->end
;
705 s
->ops
->set_io_map(s
, &iomap
);
710 c
->state
|= CONFIG_LOCKED
;
713 } /* pcmcia_request_configuration */
714 EXPORT_SYMBOL(pcmcia_request_configuration
);
717 /** pcmcia_request_io
719 * Request_io() reserves ranges of port addresses for a socket.
720 * I have not implemented range sharing or alias addressing.
722 int pcmcia_request_io(struct pcmcia_device
*p_dev
, io_req_t
*req
)
724 struct pcmcia_socket
*s
= p_dev
->socket
;
727 if (!(s
->state
& SOCKET_PRESENT
))
731 return CS_UNSUPPORTED_MODE
;
732 c
= p_dev
->function_config
;
733 if (c
->state
& CONFIG_LOCKED
)
734 return CS_CONFIGURATION_LOCKED
;
735 if (c
->state
& CONFIG_IO_REQ
)
737 if (req
->Attributes1
& (IO_SHARED
| IO_FORCE_ALIAS_ACCESS
))
738 return CS_BAD_ATTRIBUTE
;
739 if ((req
->NumPorts2
> 0) &&
740 (req
->Attributes2
& (IO_SHARED
| IO_FORCE_ALIAS_ACCESS
)))
741 return CS_BAD_ATTRIBUTE
;
743 if (alloc_io_space(s
, req
->Attributes1
, &req
->BasePort1
,
744 req
->NumPorts1
, req
->IOAddrLines
))
747 if (req
->NumPorts2
) {
748 if (alloc_io_space(s
, req
->Attributes2
, &req
->BasePort2
,
749 req
->NumPorts2
, req
->IOAddrLines
)) {
750 release_io_space(s
, req
->BasePort1
, req
->NumPorts1
);
756 c
->state
|= CONFIG_IO_REQ
;
759 } /* pcmcia_request_io */
760 EXPORT_SYMBOL(pcmcia_request_io
);
763 /** pcmcia_request_irq
765 * Request_irq() reserves an irq for this client.
767 * Also, since Linux only reserves irq's when they are actually
768 * hooked, we don't guarantee that an irq will still be available
769 * when the configuration is locked. Now that I think about it,
770 * there might be a way to fix this using a dummy handler.
773 #ifdef CONFIG_PCMCIA_PROBE
774 static irqreturn_t
test_action(int cpl
, void *dev_id
, struct pt_regs
*regs
)
780 int pcmcia_request_irq(struct pcmcia_device
*p_dev
, irq_req_t
*req
)
782 struct pcmcia_socket
*s
= p_dev
->socket
;
784 int ret
= CS_IN_USE
, irq
= 0;
786 if (!(s
->state
& SOCKET_PRESENT
))
788 c
= p_dev
->function_config
;
789 if (c
->state
& CONFIG_LOCKED
)
790 return CS_CONFIGURATION_LOCKED
;
791 if (c
->state
& CONFIG_IRQ_REQ
)
794 #ifdef CONFIG_PCMCIA_PROBE
795 if (s
->irq
.AssignedIRQ
!= 0) {
796 /* If the interrupt is already assigned, it must be the same */
797 irq
= s
->irq
.AssignedIRQ
;
800 u32 mask
= s
->irq_mask
;
801 void *data
= &p_dev
->dev
.driver
; /* something unique to this device */
803 for (try = 0; try < 64; try++) {
806 /* marked as available by driver, and not blocked by userspace? */
807 if (!((mask
>> irq
) & 1))
810 /* avoid an IRQ which is already used by a PCMCIA card */
811 if ((try < 32) && pcmcia_used_irq
[irq
])
814 /* register the correct driver, if possible, of check whether
815 * registering a dummy handle works, i.e. if the IRQ isn't
816 * marked as used by the kernel resource management core */
817 ret
= request_irq(irq
,
818 (req
->Attributes
& IRQ_HANDLE_PRESENT
) ? req
->Handler
: test_action
,
819 ((req
->Attributes
& IRQ_TYPE_DYNAMIC_SHARING
) ||
820 (s
->functions
> 1) ||
821 (irq
== s
->pci_irq
)) ? SA_SHIRQ
: 0,
823 (req
->Attributes
& IRQ_HANDLE_PRESENT
) ? req
->Instance
: data
);
825 if (!(req
->Attributes
& IRQ_HANDLE_PRESENT
))
832 /* only assign PCI irq if no IRQ already assigned */
833 if (ret
&& !s
->irq
.AssignedIRQ
) {
839 if (ret
&& req
->Attributes
& IRQ_HANDLE_PRESENT
) {
840 if (request_irq(irq
, req
->Handler
,
841 ((req
->Attributes
& IRQ_TYPE_DYNAMIC_SHARING
) ||
842 (s
->functions
> 1) ||
843 (irq
== s
->pci_irq
)) ? SA_SHIRQ
: 0,
844 p_dev
->devname
, req
->Instance
))
848 c
->irq
.Attributes
= req
->Attributes
;
849 s
->irq
.AssignedIRQ
= req
->AssignedIRQ
= irq
;
852 c
->state
|= CONFIG_IRQ_REQ
;
855 #ifdef CONFIG_PCMCIA_PROBE
856 pcmcia_used_irq
[irq
]++;
860 } /* pcmcia_request_irq */
861 EXPORT_SYMBOL(pcmcia_request_irq
);
864 /** pcmcia_request_window
866 * Request_window() establishes a mapping between card memory space
867 * and system memory space.
869 int pcmcia_request_window(struct pcmcia_device
**p_dev
, win_req_t
*req
, window_handle_t
*wh
)
871 struct pcmcia_socket
*s
= (*p_dev
)->socket
;
876 if (!(s
->state
& SOCKET_PRESENT
))
878 if (req
->Attributes
& (WIN_PAGED
| WIN_SHARED
))
879 return CS_BAD_ATTRIBUTE
;
881 /* Window size defaults to smallest available */
883 req
->Size
= s
->map_size
;
884 align
= (((s
->features
& SS_CAP_MEM_ALIGN
) ||
885 (req
->Attributes
& WIN_STRICT_ALIGN
)) ?
886 req
->Size
: s
->map_size
);
887 if (req
->Size
& (s
->map_size
-1))
889 if ((req
->Base
&& (s
->features
& SS_CAP_STATIC_MAP
)) ||
890 (req
->Base
& (align
-1)))
895 /* Allocate system memory window */
896 for (w
= 0; w
< MAX_WIN
; w
++)
897 if (!(s
->state
& SOCKET_WIN_REQ(w
))) break;
899 return CS_OUT_OF_RESOURCE
;
902 win
->magic
= WINDOW_MAGIC
;
904 win
->handle
= *p_dev
;
907 if (!(s
->features
& SS_CAP_STATIC_MAP
)) {
908 win
->ctl
.res
= pcmcia_find_mem_region(req
->Base
, req
->Size
, align
,
909 (req
->Attributes
& WIN_MAP_BELOW_1MB
), s
);
913 (*p_dev
)->_win
|= CLIENT_WIN_REQ(w
);
915 /* Configure the socket controller */
918 win
->ctl
.speed
= req
->AccessSpeed
;
919 if (req
->Attributes
& WIN_MEMORY_TYPE
)
920 win
->ctl
.flags
|= MAP_ATTRIB
;
921 if (req
->Attributes
& WIN_ENABLE
)
922 win
->ctl
.flags
|= MAP_ACTIVE
;
923 if (req
->Attributes
& WIN_DATA_WIDTH_16
)
924 win
->ctl
.flags
|= MAP_16BIT
;
925 if (req
->Attributes
& WIN_USE_WAIT
)
926 win
->ctl
.flags
|= MAP_USE_WAIT
;
927 win
->ctl
.card_start
= 0;
928 if (s
->ops
->set_mem_map(s
, &win
->ctl
) != 0)
930 s
->state
|= SOCKET_WIN_REQ(w
);
932 /* Return window handle */
933 if (s
->features
& SS_CAP_STATIC_MAP
) {
934 req
->Base
= win
->ctl
.static_start
;
936 req
->Base
= win
->ctl
.res
->start
;
941 } /* pcmcia_request_window */
942 EXPORT_SYMBOL(pcmcia_request_window
);
944 void pcmcia_disable_device(struct pcmcia_device
*p_dev
) {
945 pcmcia_release_configuration(p_dev
);
946 pcmcia_release_io(p_dev
, &p_dev
->io
);
947 pcmcia_release_irq(p_dev
, &p_dev
->irq
);
949 pcmcia_release_window(p_dev
->win
);
951 p_dev
->dev_node
= NULL
;
953 EXPORT_SYMBOL(pcmcia_disable_device
);