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/cs_types.h>
27 #include <pcmcia/ss.h>
28 #include <pcmcia/cs.h>
29 #include <pcmcia/bulkmem.h>
30 #include <pcmcia/cistpl.h>
31 #include <pcmcia/cisreg.h>
32 #include <pcmcia/ds.h>
34 #include "cs_internal.h"
35 #include "ds_internal.h"
38 /* Access speed for IO windows */
39 static int io_speed
= 0;
40 module_param(io_speed
, int, 0444);
43 #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 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(struct pcmcia_device
*p_dev
,
207 return pccard_access_configuration_register(p_dev
->socket
,
210 EXPORT_SYMBOL(pcmcia_access_configuration_register
);
214 int pccard_get_configuration_info(struct pcmcia_socket
*s
,
215 unsigned int function
,
216 config_info_t
*config
)
220 if (!(s
->state
& SOCKET_PRESENT
))
223 config
->Function
= function
;
225 #ifdef CONFIG_CARDBUS
226 if (s
->state
& SOCKET_CARDBUS
) {
227 memset(config
, 0, sizeof(config_info_t
));
228 config
->Vcc
= s
->socket
.Vcc
;
229 config
->Vpp1
= config
->Vpp2
= s
->socket
.Vpp
;
230 config
->Option
= s
->cb_dev
->subordinate
->number
;
231 if (s
->state
& SOCKET_CARDBUS_CONFIG
) {
232 config
->Attributes
= CONF_VALID_CLIENT
;
233 config
->IntType
= INT_CARDBUS
;
234 config
->AssignedIRQ
= s
->irq
.AssignedIRQ
;
235 if (config
->AssignedIRQ
)
236 config
->Attributes
|= CONF_ENABLE_IRQ
;
237 config
->BasePort1
= s
->io
[0].BasePort
;
238 config
->NumPorts1
= s
->io
[0].NumPorts
;
244 c
= (s
->config
!= NULL
) ? &s
->config
[function
] : NULL
;
246 if ((c
== NULL
) || !(c
->state
& CONFIG_LOCKED
)) {
247 config
->Attributes
= 0;
248 config
->Vcc
= s
->socket
.Vcc
;
249 config
->Vpp1
= config
->Vpp2
= s
->socket
.Vpp
;
253 /* !!! This is a hack !!! */
254 memcpy(&config
->Attributes
, &c
->Attributes
, sizeof(config_t
));
255 config
->Attributes
|= CONF_VALID_CLIENT
;
256 config
->CardValues
= c
->CardValues
;
257 config
->IRQAttributes
= c
->irq
.Attributes
;
258 config
->AssignedIRQ
= s
->irq
.AssignedIRQ
;
259 config
->BasePort1
= c
->io
.BasePort1
;
260 config
->NumPorts1
= c
->io
.NumPorts1
;
261 config
->Attributes1
= c
->io
.Attributes1
;
262 config
->BasePort2
= c
->io
.BasePort2
;
263 config
->NumPorts2
= c
->io
.NumPorts2
;
264 config
->Attributes2
= c
->io
.Attributes2
;
265 config
->IOAddrLines
= c
->io
.IOAddrLines
;
268 } /* pccard_get_configuration_info */
270 int pcmcia_get_configuration_info(struct pcmcia_device
*p_dev
,
271 config_info_t
*config
)
273 return pccard_get_configuration_info(p_dev
->socket
, p_dev
->func
,
276 EXPORT_SYMBOL(pcmcia_get_configuration_info
);
279 /** pcmcia_get_window
281 int pcmcia_get_window(struct pcmcia_socket
*s
, window_handle_t
*handle
,
282 int idx
, win_req_t
*req
)
287 if (!s
|| !(s
->state
& SOCKET_PRESENT
))
289 for (w
= idx
; w
< MAX_WIN
; w
++)
290 if (s
->state
& SOCKET_WIN_REQ(w
))
293 return CS_NO_MORE_ITEMS
;
295 req
->Base
= win
->ctl
.res
->start
;
296 req
->Size
= win
->ctl
.res
->end
- win
->ctl
.res
->start
+ 1;
297 req
->AccessSpeed
= win
->ctl
.speed
;
299 if (win
->ctl
.flags
& MAP_ATTRIB
)
300 req
->Attributes
|= WIN_MEMORY_TYPE_AM
;
301 if (win
->ctl
.flags
& MAP_ACTIVE
)
302 req
->Attributes
|= WIN_ENABLE
;
303 if (win
->ctl
.flags
& MAP_16BIT
)
304 req
->Attributes
|= WIN_DATA_WIDTH_16
;
305 if (win
->ctl
.flags
& MAP_USE_WAIT
)
306 req
->Attributes
|= WIN_USE_WAIT
;
309 } /* pcmcia_get_window */
310 EXPORT_SYMBOL(pcmcia_get_window
);
313 /** pccard_get_status
315 * Get the current socket state bits. We don't support the latched
316 * SocketState yet: I haven't seen any point for it.
319 int pccard_get_status(struct pcmcia_socket
*s
, unsigned int function
,
325 s
->ops
->get_status(s
, &val
);
326 status
->CardState
= status
->SocketState
= 0;
327 status
->CardState
|= (val
& SS_DETECT
) ? CS_EVENT_CARD_DETECT
: 0;
328 status
->CardState
|= (val
& SS_CARDBUS
) ? CS_EVENT_CB_DETECT
: 0;
329 status
->CardState
|= (val
& SS_3VCARD
) ? CS_EVENT_3VCARD
: 0;
330 status
->CardState
|= (val
& SS_XVCARD
) ? CS_EVENT_XVCARD
: 0;
331 if (s
->state
& SOCKET_SUSPEND
)
332 status
->CardState
|= CS_EVENT_PM_SUSPEND
;
333 if (!(s
->state
& SOCKET_PRESENT
))
336 c
= (s
->config
!= NULL
) ? &s
->config
[function
] : NULL
;
337 if ((c
!= NULL
) && (c
->state
& CONFIG_LOCKED
) &&
338 (c
->IntType
& (INT_MEMORY_AND_IO
| INT_ZOOMED_VIDEO
))) {
340 if (c
->Present
& PRESENT_PIN_REPLACE
) {
341 pcmcia_read_cis_mem(s
, 1, (c
->ConfigBase
+CISREG_PRR
)>>1, 1, ®
);
343 (reg
& PRR_WP_STATUS
) ? CS_EVENT_WRITE_PROTECT
: 0;
345 (reg
& PRR_READY_STATUS
) ? CS_EVENT_READY_CHANGE
: 0;
347 (reg
& PRR_BVD2_STATUS
) ? CS_EVENT_BATTERY_LOW
: 0;
349 (reg
& PRR_BVD1_STATUS
) ? CS_EVENT_BATTERY_DEAD
: 0;
351 /* No PRR? Then assume we're always ready */
352 status
->CardState
|= CS_EVENT_READY_CHANGE
;
354 if (c
->Present
& PRESENT_EXT_STATUS
) {
355 pcmcia_read_cis_mem(s
, 1, (c
->ConfigBase
+CISREG_ESR
)>>1, 1, ®
);
357 (reg
& ESR_REQ_ATTN
) ? CS_EVENT_REQUEST_ATTENTION
: 0;
362 (val
& SS_WRPROT
) ? CS_EVENT_WRITE_PROTECT
: 0;
364 (val
& SS_BATDEAD
) ? CS_EVENT_BATTERY_DEAD
: 0;
366 (val
& SS_BATWARN
) ? CS_EVENT_BATTERY_LOW
: 0;
368 (val
& SS_READY
) ? CS_EVENT_READY_CHANGE
: 0;
370 } /* pccard_get_status */
372 int pcmcia_get_status(client_handle_t handle
, cs_status_t
*status
)
374 struct pcmcia_socket
*s
;
376 return pccard_get_status(s
, handle
->func
, 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
;
425 if (!(s
->state
& SOCKET_PRESENT
))
427 if (!(c
->state
& CONFIG_LOCKED
))
428 return CS_CONFIGURATION_LOCKED
;
430 if (mod
->Attributes
& CONF_IRQ_CHANGE_VALID
) {
431 if (mod
->Attributes
& CONF_ENABLE_IRQ
) {
432 c
->Attributes
|= CONF_ENABLE_IRQ
;
433 s
->socket
.io_irq
= s
->irq
.AssignedIRQ
;
435 c
->Attributes
&= ~CONF_ENABLE_IRQ
;
436 s
->socket
.io_irq
= 0;
438 s
->ops
->set_socket(s
, &s
->socket
);
441 if (mod
->Attributes
& CONF_VCC_CHANGE_VALID
)
444 /* We only allow changing Vpp1 and Vpp2 to the same value */
445 if ((mod
->Attributes
& CONF_VPP1_CHANGE_VALID
) &&
446 (mod
->Attributes
& CONF_VPP2_CHANGE_VALID
)) {
447 if (mod
->Vpp1
!= mod
->Vpp2
)
449 c
->Vpp1
= c
->Vpp2
= s
->socket
.Vpp
= mod
->Vpp1
;
450 if (s
->ops
->set_socket(s
, &s
->socket
))
452 } else if ((mod
->Attributes
& CONF_VPP1_CHANGE_VALID
) ||
453 (mod
->Attributes
& CONF_VPP2_CHANGE_VALID
))
457 } /* modify_configuration */
458 EXPORT_SYMBOL(pcmcia_modify_configuration
);
461 int pcmcia_release_configuration(struct pcmcia_device
*p_dev
)
463 pccard_io_map io
= { 0, 0, 0, 0, 1 };
464 struct pcmcia_socket
*s
= p_dev
->socket
;
467 if (!(p_dev
->state
& CLIENT_CONFIG_LOCKED
))
468 return CS_BAD_HANDLE
;
469 p_dev
->state
&= ~CLIENT_CONFIG_LOCKED
;
471 if (!(p_dev
->state
& CLIENT_STALE
)) {
472 config_t
*c
= CONFIG(p_dev
);
473 if (--(s
->lock_count
) == 0) {
474 s
->socket
.flags
= SS_OUTPUT_ENA
; /* Is this correct? */
476 s
->socket
.io_irq
= 0;
477 s
->ops
->set_socket(s
, &s
->socket
);
479 if (c
->state
& CONFIG_IO_REQ
)
480 for (i
= 0; i
< MAX_IO_WIN
; i
++) {
481 if (s
->io
[i
].NumPorts
== 0)
484 if (s
->io
[i
].Config
!= 0)
487 s
->ops
->set_io_map(s
, &io
);
489 c
->state
&= ~CONFIG_LOCKED
;
493 } /* pcmcia_release_configuration */
494 EXPORT_SYMBOL(pcmcia_release_configuration
);
497 /** pcmcia_release_io
499 * Release_io() releases the I/O ranges allocated by a client. This
500 * may be invoked some time after a card ejection has already dumped
501 * the actual socket configuration, so if the client is "stale", we
502 * don't bother checking the port ranges against the current socket
505 int pcmcia_release_io(struct pcmcia_device
*p_dev
, io_req_t
*req
)
507 struct pcmcia_socket
*s
= p_dev
->socket
;
509 if (!(p_dev
->state
& CLIENT_IO_REQ
))
510 return CS_BAD_HANDLE
;
511 p_dev
->state
&= ~CLIENT_IO_REQ
;
513 if (!(p_dev
->state
& CLIENT_STALE
)) {
514 config_t
*c
= CONFIG(p_dev
);
515 if (c
->state
& CONFIG_LOCKED
)
516 return CS_CONFIGURATION_LOCKED
;
517 if ((c
->io
.BasePort1
!= req
->BasePort1
) ||
518 (c
->io
.NumPorts1
!= req
->NumPorts1
) ||
519 (c
->io
.BasePort2
!= req
->BasePort2
) ||
520 (c
->io
.NumPorts2
!= req
->NumPorts2
))
522 c
->state
&= ~CONFIG_IO_REQ
;
525 release_io_space(s
, req
->BasePort1
, req
->NumPorts1
);
527 release_io_space(s
, req
->BasePort2
, req
->NumPorts2
);
530 } /* pcmcia_release_io */
531 EXPORT_SYMBOL(pcmcia_release_io
);
534 int pcmcia_release_irq(struct pcmcia_device
*p_dev
, irq_req_t
*req
)
536 struct pcmcia_socket
*s
= p_dev
->socket
;
537 if (!(p_dev
->state
& CLIENT_IRQ_REQ
))
538 return CS_BAD_HANDLE
;
539 p_dev
->state
&= ~CLIENT_IRQ_REQ
;
541 if (!(p_dev
->state
& CLIENT_STALE
)) {
542 config_t
*c
= CONFIG(p_dev
);
543 if (c
->state
& CONFIG_LOCKED
)
544 return CS_CONFIGURATION_LOCKED
;
545 if (c
->irq
.Attributes
!= req
->Attributes
)
546 return CS_BAD_ATTRIBUTE
;
547 if (s
->irq
.AssignedIRQ
!= req
->AssignedIRQ
)
549 if (--s
->irq
.Config
== 0) {
550 c
->state
&= ~CONFIG_IRQ_REQ
;
551 s
->irq
.AssignedIRQ
= 0;
555 if (req
->Attributes
& IRQ_HANDLE_PRESENT
) {
556 free_irq(req
->AssignedIRQ
, req
->Instance
);
559 #ifdef CONFIG_PCMCIA_PROBE
560 pcmcia_used_irq
[req
->AssignedIRQ
]--;
564 } /* pcmcia_release_irq */
565 EXPORT_SYMBOL(pcmcia_release_irq
);
568 int pcmcia_release_window(window_handle_t win
)
570 struct pcmcia_socket
*s
;
572 if ((win
== NULL
) || (win
->magic
!= WINDOW_MAGIC
))
573 return CS_BAD_HANDLE
;
575 if (!(win
->handle
->state
& CLIENT_WIN_REQ(win
->index
)))
576 return CS_BAD_HANDLE
;
578 /* Shut down memory window */
579 win
->ctl
.flags
&= ~MAP_ACTIVE
;
580 s
->ops
->set_mem_map(s
, &win
->ctl
);
581 s
->state
&= ~SOCKET_WIN_REQ(win
->index
);
583 /* Release system memory */
585 release_resource(win
->ctl
.res
);
589 win
->handle
->state
&= ~CLIENT_WIN_REQ(win
->index
);
594 } /* pcmcia_release_window */
595 EXPORT_SYMBOL(pcmcia_release_window
);
598 int pcmcia_request_configuration(struct pcmcia_device
*p_dev
,
603 struct pcmcia_socket
*s
= p_dev
->socket
;
607 if (!(s
->state
& SOCKET_PRESENT
))
610 if (req
->IntType
& INT_CARDBUS
)
611 return CS_UNSUPPORTED_MODE
;
613 if (c
->state
& CONFIG_LOCKED
)
614 return CS_CONFIGURATION_LOCKED
;
616 /* Do power control. We don't allow changes in Vcc. */
617 if (s
->socket
.Vcc
!= req
->Vcc
)
619 if (req
->Vpp1
!= req
->Vpp2
)
621 s
->socket
.Vpp
= req
->Vpp1
;
622 if (s
->ops
->set_socket(s
, &s
->socket
))
625 c
->Vcc
= req
->Vcc
; c
->Vpp1
= c
->Vpp2
= req
->Vpp1
;
627 /* Pick memory or I/O card, DMA mode, interrupt */
628 c
->IntType
= req
->IntType
;
629 c
->Attributes
= req
->Attributes
;
630 if (req
->IntType
& INT_MEMORY_AND_IO
)
631 s
->socket
.flags
|= SS_IOCARD
;
632 if (req
->IntType
& INT_ZOOMED_VIDEO
)
633 s
->socket
.flags
|= SS_ZVCARD
| SS_IOCARD
;
634 if (req
->Attributes
& CONF_ENABLE_DMA
)
635 s
->socket
.flags
|= SS_DMA_MODE
;
636 if (req
->Attributes
& CONF_ENABLE_SPKR
)
637 s
->socket
.flags
|= SS_SPKR_ENA
;
638 if (req
->Attributes
& CONF_ENABLE_IRQ
)
639 s
->socket
.io_irq
= s
->irq
.AssignedIRQ
;
641 s
->socket
.io_irq
= 0;
642 s
->ops
->set_socket(s
, &s
->socket
);
645 /* Set up CIS configuration registers */
646 base
= c
->ConfigBase
= req
->ConfigBase
;
647 c
->Present
= c
->CardValues
= req
->Present
;
648 if (req
->Present
& PRESENT_COPY
) {
650 pcmcia_write_cis_mem(s
, 1, (base
+ CISREG_SCR
)>>1, 1, &c
->Copy
);
652 if (req
->Present
& PRESENT_OPTION
) {
653 if (s
->functions
== 1) {
654 c
->Option
= req
->ConfigIndex
& COR_CONFIG_MASK
;
656 c
->Option
= req
->ConfigIndex
& COR_MFC_CONFIG_MASK
;
657 c
->Option
|= COR_FUNC_ENA
|COR_IREQ_ENA
;
658 if (req
->Present
& PRESENT_IOBASE_0
)
659 c
->Option
|= COR_ADDR_DECODE
;
661 if (c
->state
& CONFIG_IRQ_REQ
)
662 if (!(c
->irq
.Attributes
& IRQ_FORCED_PULSE
))
663 c
->Option
|= COR_LEVEL_REQ
;
664 pcmcia_write_cis_mem(s
, 1, (base
+ CISREG_COR
)>>1, 1, &c
->Option
);
667 if (req
->Present
& PRESENT_STATUS
) {
668 c
->Status
= req
->Status
;
669 pcmcia_write_cis_mem(s
, 1, (base
+ CISREG_CCSR
)>>1, 1, &c
->Status
);
671 if (req
->Present
& PRESENT_PIN_REPLACE
) {
673 pcmcia_write_cis_mem(s
, 1, (base
+ CISREG_PRR
)>>1, 1, &c
->Pin
);
675 if (req
->Present
& PRESENT_EXT_STATUS
) {
676 c
->ExtStatus
= req
->ExtStatus
;
677 pcmcia_write_cis_mem(s
, 1, (base
+ CISREG_ESR
)>>1, 1, &c
->ExtStatus
);
679 if (req
->Present
& PRESENT_IOBASE_0
) {
680 u_char b
= c
->io
.BasePort1
& 0xff;
681 pcmcia_write_cis_mem(s
, 1, (base
+ CISREG_IOBASE_0
)>>1, 1, &b
);
682 b
= (c
->io
.BasePort1
>> 8) & 0xff;
683 pcmcia_write_cis_mem(s
, 1, (base
+ CISREG_IOBASE_1
)>>1, 1, &b
);
685 if (req
->Present
& PRESENT_IOSIZE
) {
686 u_char b
= c
->io
.NumPorts1
+ c
->io
.NumPorts2
- 1;
687 pcmcia_write_cis_mem(s
, 1, (base
+ CISREG_IOSIZE
)>>1, 1, &b
);
690 /* Configure I/O windows */
691 if (c
->state
& CONFIG_IO_REQ
) {
692 iomap
.speed
= io_speed
;
693 for (i
= 0; i
< MAX_IO_WIN
; i
++)
694 if (s
->io
[i
].NumPorts
!= 0) {
696 iomap
.flags
= MAP_ACTIVE
;
697 switch (s
->io
[i
].Attributes
& IO_DATA_PATH_WIDTH
) {
698 case IO_DATA_PATH_WIDTH_16
:
699 iomap
.flags
|= MAP_16BIT
; break;
700 case IO_DATA_PATH_WIDTH_AUTO
:
701 iomap
.flags
|= MAP_AUTOSZ
; break;
705 iomap
.start
= s
->io
[i
].BasePort
;
706 iomap
.stop
= iomap
.start
+ s
->io
[i
].NumPorts
- 1;
707 s
->ops
->set_io_map(s
, &iomap
);
712 c
->state
|= CONFIG_LOCKED
;
713 p_dev
->state
|= CLIENT_CONFIG_LOCKED
;
715 } /* pcmcia_request_configuration */
716 EXPORT_SYMBOL(pcmcia_request_configuration
);
719 /** pcmcia_request_io
721 * Request_io() reserves ranges of port addresses for a socket.
722 * I have not implemented range sharing or alias addressing.
724 int pcmcia_request_io(struct pcmcia_device
*p_dev
, io_req_t
*req
)
726 struct pcmcia_socket
*s
= p_dev
->socket
;
729 if (!(s
->state
& SOCKET_PRESENT
))
733 return CS_UNSUPPORTED_MODE
;
735 if (c
->state
& CONFIG_LOCKED
)
736 return CS_CONFIGURATION_LOCKED
;
737 if (c
->state
& CONFIG_IO_REQ
)
739 if (req
->Attributes1
& (IO_SHARED
| IO_FORCE_ALIAS_ACCESS
))
740 return CS_BAD_ATTRIBUTE
;
741 if ((req
->NumPorts2
> 0) &&
742 (req
->Attributes2
& (IO_SHARED
| IO_FORCE_ALIAS_ACCESS
)))
743 return CS_BAD_ATTRIBUTE
;
745 if (alloc_io_space(s
, req
->Attributes1
, &req
->BasePort1
,
746 req
->NumPorts1
, req
->IOAddrLines
))
749 if (req
->NumPorts2
) {
750 if (alloc_io_space(s
, req
->Attributes2
, &req
->BasePort2
,
751 req
->NumPorts2
, req
->IOAddrLines
)) {
752 release_io_space(s
, req
->BasePort1
, req
->NumPorts1
);
758 c
->state
|= CONFIG_IO_REQ
;
759 p_dev
->state
|= CLIENT_IO_REQ
;
761 } /* pcmcia_request_io */
762 EXPORT_SYMBOL(pcmcia_request_io
);
765 /** pcmcia_request_irq
767 * Request_irq() reserves an irq for this client.
769 * Also, since Linux only reserves irq's when they are actually
770 * hooked, we don't guarantee that an irq will still be available
771 * when the configuration is locked. Now that I think about it,
772 * there might be a way to fix this using a dummy handler.
775 #ifdef CONFIG_PCMCIA_PROBE
776 static irqreturn_t
test_action(int cpl
, void *dev_id
, struct pt_regs
*regs
)
782 int pcmcia_request_irq(struct pcmcia_device
*p_dev
, irq_req_t
*req
)
784 struct pcmcia_socket
*s
= p_dev
->socket
;
786 int ret
= CS_IN_USE
, irq
= 0;
788 if (!(s
->state
& SOCKET_PRESENT
))
791 if (c
->state
& CONFIG_LOCKED
)
792 return CS_CONFIGURATION_LOCKED
;
793 if (c
->state
& CONFIG_IRQ_REQ
)
796 #ifdef CONFIG_PCMCIA_PROBE
797 if (s
->irq
.AssignedIRQ
!= 0) {
798 /* If the interrupt is already assigned, it must be the same */
799 irq
= s
->irq
.AssignedIRQ
;
802 u32 mask
= s
->irq_mask
;
803 void *data
= &p_dev
->dev
.driver
; /* something unique to this device */
805 for (try = 0; try < 64; try++) {
808 /* marked as available by driver, and not blocked by userspace? */
809 if (!((mask
>> irq
) & 1))
812 /* avoid an IRQ which is already used by a PCMCIA card */
813 if ((try < 32) && pcmcia_used_irq
[irq
])
816 /* register the correct driver, if possible, of check whether
817 * registering a dummy handle works, i.e. if the IRQ isn't
818 * marked as used by the kernel resource management core */
819 ret
= request_irq(irq
,
820 (req
->Attributes
& IRQ_HANDLE_PRESENT
) ? req
->Handler
: test_action
,
821 ((req
->Attributes
& IRQ_TYPE_DYNAMIC_SHARING
) ||
822 (s
->functions
> 1) ||
823 (irq
== s
->pci_irq
)) ? SA_SHIRQ
: 0,
825 (req
->Attributes
& IRQ_HANDLE_PRESENT
) ? req
->Instance
: data
);
827 if (!(req
->Attributes
& IRQ_HANDLE_PRESENT
))
840 if (ret
&& req
->Attributes
& IRQ_HANDLE_PRESENT
) {
841 if (request_irq(irq
, req
->Handler
,
842 ((req
->Attributes
& IRQ_TYPE_DYNAMIC_SHARING
) ||
843 (s
->functions
> 1) ||
844 (irq
== s
->pci_irq
)) ? SA_SHIRQ
: 0,
845 p_dev
->dev
.bus_id
, req
->Instance
))
849 c
->irq
.Attributes
= req
->Attributes
;
850 s
->irq
.AssignedIRQ
= req
->AssignedIRQ
= irq
;
853 c
->state
|= CONFIG_IRQ_REQ
;
854 p_dev
->state
|= CLIENT_IRQ_REQ
;
856 #ifdef CONFIG_PCMCIA_PROBE
857 pcmcia_used_irq
[irq
]++;
861 } /* pcmcia_request_irq */
862 EXPORT_SYMBOL(pcmcia_request_irq
);
865 /** pcmcia_request_window
867 * Request_window() establishes a mapping between card memory space
868 * and system memory space.
870 int pcmcia_request_window(struct pcmcia_device
**p_dev
, win_req_t
*req
, window_handle_t
*wh
)
872 struct pcmcia_socket
*s
= (*p_dev
)->socket
;
877 if (!(s
->state
& SOCKET_PRESENT
))
879 if (req
->Attributes
& (WIN_PAGED
| WIN_SHARED
))
880 return CS_BAD_ATTRIBUTE
;
882 /* Window size defaults to smallest available */
884 req
->Size
= s
->map_size
;
885 align
= (((s
->features
& SS_CAP_MEM_ALIGN
) ||
886 (req
->Attributes
& WIN_STRICT_ALIGN
)) ?
887 req
->Size
: s
->map_size
);
888 if (req
->Size
& (s
->map_size
-1))
890 if ((req
->Base
&& (s
->features
& SS_CAP_STATIC_MAP
)) ||
891 (req
->Base
& (align
-1)))
896 /* Allocate system memory window */
897 for (w
= 0; w
< MAX_WIN
; w
++)
898 if (!(s
->state
& SOCKET_WIN_REQ(w
))) break;
900 return CS_OUT_OF_RESOURCE
;
903 win
->magic
= WINDOW_MAGIC
;
905 win
->handle
= *p_dev
;
908 if (!(s
->features
& SS_CAP_STATIC_MAP
)) {
909 win
->ctl
.res
= pcmcia_find_mem_region(req
->Base
, req
->Size
, align
,
910 (req
->Attributes
& WIN_MAP_BELOW_1MB
), s
);
914 (*p_dev
)->state
|= CLIENT_WIN_REQ(w
);
916 /* Configure the socket controller */
919 win
->ctl
.speed
= req
->AccessSpeed
;
920 if (req
->Attributes
& WIN_MEMORY_TYPE
)
921 win
->ctl
.flags
|= MAP_ATTRIB
;
922 if (req
->Attributes
& WIN_ENABLE
)
923 win
->ctl
.flags
|= MAP_ACTIVE
;
924 if (req
->Attributes
& WIN_DATA_WIDTH_16
)
925 win
->ctl
.flags
|= MAP_16BIT
;
926 if (req
->Attributes
& WIN_USE_WAIT
)
927 win
->ctl
.flags
|= MAP_USE_WAIT
;
928 win
->ctl
.card_start
= 0;
929 if (s
->ops
->set_mem_map(s
, &win
->ctl
) != 0)
931 s
->state
|= SOCKET_WIN_REQ(w
);
933 /* Return window handle */
934 if (s
->features
& SS_CAP_STATIC_MAP
) {
935 req
->Base
= win
->ctl
.static_start
;
937 req
->Base
= win
->ctl
.res
->start
;
942 } /* pcmcia_request_window */
943 EXPORT_SYMBOL(pcmcia_request_window
);