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-2010 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>
23 #include <linux/netdevice.h>
24 #include <linux/slab.h>
28 #include <pcmcia/ss.h>
29 #include <pcmcia/cistpl.h>
30 #include <pcmcia/cisreg.h>
31 #include <pcmcia/ds.h>
33 #include "cs_internal.h"
36 /* Access speed for IO windows */
38 module_param(io_speed
, int, 0444);
41 int pcmcia_validate_mem(struct pcmcia_socket
*s
)
43 if (s
->resource_ops
->validate_mem
)
44 return s
->resource_ops
->validate_mem(s
);
45 /* if there is no callback, we can assume that everything is OK */
49 struct resource
*pcmcia_find_mem_region(u_long base
, u_long num
, u_long align
,
50 int low
, struct pcmcia_socket
*s
)
52 if (s
->resource_ops
->find_mem
)
53 return s
->resource_ops
->find_mem(base
, num
, align
, low
, s
);
59 * release_io_space() - release IO ports allocated with alloc_io_space()
61 * @res: resource to release
64 static void release_io_space(struct pcmcia_socket
*s
, struct resource
*res
)
66 resource_size_t num
= resource_size(res
);
69 dev_dbg(&s
->dev
, "release_io_space for %pR\n", res
);
71 for (i
= 0; i
< MAX_IO_WIN
; i
++) {
74 if ((s
->io
[i
].res
->start
<= res
->start
) &&
75 (s
->io
[i
].res
->end
>= res
->end
)) {
76 s
->io
[i
].InUse
-= num
;
78 release_resource(res
);
79 res
->start
= res
->end
= 0;
80 res
->flags
= IORESOURCE_IO
;
81 /* Free the window if no one else is using it */
82 if (s
->io
[i
].InUse
== 0) {
83 release_resource(s
->io
[i
].res
);
93 * alloc_io_space() - allocate IO ports for use by a PCMCIA device
95 * @res: resource to allocate (begin: begin, end: size)
96 * @lines: number of IO lines decoded by the PCMCIA card
98 * Special stuff for managing IO windows, because they are scarce
100 static int alloc_io_space(struct pcmcia_socket
*s
, struct resource
*res
,
104 unsigned int base
= res
->start
;
105 unsigned int num
= res
->end
;
108 res
->flags
|= IORESOURCE_IO
;
110 dev_dbg(&s
->dev
, "alloc_io_space request for %pR, %d lines\n",
113 align
= base
? (lines
? 1<<lines
: 0) : 1;
114 if (align
&& (align
< num
)) {
116 dev_dbg(&s
->dev
, "odd IO request\n");
119 while (align
&& (align
< num
))
122 if (base
& ~(align
-1)) {
123 dev_dbg(&s
->dev
, "odd IO request\n");
127 ret
= s
->resource_ops
->find_io(s
, res
->flags
, &base
, num
, align
,
130 dev_dbg(&s
->dev
, "alloc_io_space request failed (%d)\n", ret
);
135 res
->end
= res
->start
+ num
- 1;
138 ret
= request_resource(res
->parent
, res
);
141 "request_resource %pR failed: %d\n", res
, ret
);
143 release_io_space(s
, res
);
146 dev_dbg(&s
->dev
, "alloc_io_space request result %d: %pR\n", ret
, res
);
152 * pcmcia_access_config() - read or write card configuration registers
154 * pcmcia_access_config() reads and writes configuration registers in
155 * attribute memory. Memory window 0 is reserved for this and the tuple
156 * reading services. Drivers must use pcmcia_read_config_byte() or
157 * pcmcia_write_config_byte().
159 static int pcmcia_access_config(struct pcmcia_device
*p_dev
,
160 off_t where
, u8
*val
,
161 int (*accessf
) (struct pcmcia_socket
*s
,
162 int attr
, unsigned int addr
,
163 unsigned int len
, void *ptr
))
165 struct pcmcia_socket
*s
;
172 mutex_lock(&s
->ops_mutex
);
173 c
= p_dev
->function_config
;
175 if (!(c
->state
& CONFIG_LOCKED
)) {
176 dev_dbg(&p_dev
->dev
, "Configuration isnt't locked\n");
177 mutex_unlock(&s
->ops_mutex
);
181 addr
= (p_dev
->config_base
+ where
) >> 1;
183 ret
= accessf(s
, 1, addr
, 1, val
);
185 mutex_unlock(&s
->ops_mutex
);
192 * pcmcia_read_config_byte() - read a byte from a card configuration register
194 * pcmcia_read_config_byte() reads a byte from a configuration register in
197 int pcmcia_read_config_byte(struct pcmcia_device
*p_dev
, off_t where
, u8
*val
)
199 return pcmcia_access_config(p_dev
, where
, val
, pcmcia_read_cis_mem
);
201 EXPORT_SYMBOL(pcmcia_read_config_byte
);
205 * pcmcia_write_config_byte() - write a byte to a card configuration register
207 * pcmcia_write_config_byte() writes a byte to a configuration register in
210 int pcmcia_write_config_byte(struct pcmcia_device
*p_dev
, off_t where
, u8 val
)
212 return pcmcia_access_config(p_dev
, where
, &val
, pcmcia_write_cis_mem
);
214 EXPORT_SYMBOL(pcmcia_write_config_byte
);
218 * pcmcia_map_mem_page() - modify iomem window to point to a different offset
219 * @p_dev: pcmcia device
220 * @res: iomem resource already enabled by pcmcia_request_window()
221 * @offset: card_offset to map
223 * pcmcia_map_mem_page() modifies what can be read and written by accessing
224 * an iomem range previously enabled by pcmcia_request_window(), by setting
225 * the card_offset value to @offset.
227 int pcmcia_map_mem_page(struct pcmcia_device
*p_dev
, struct resource
*res
,
230 struct pcmcia_socket
*s
= p_dev
->socket
;
234 w
= ((res
->flags
& IORESOURCE_BITS
& WIN_FLAGS_REQ
) >> 2) - 1;
238 mutex_lock(&s
->ops_mutex
);
239 s
->win
[w
].card_start
= offset
;
240 ret
= s
->ops
->set_mem_map(s
, &s
->win
[w
]);
242 dev_warn(&p_dev
->dev
, "failed to set_mem_map\n");
243 mutex_unlock(&s
->ops_mutex
);
246 EXPORT_SYMBOL(pcmcia_map_mem_page
);
250 * pcmcia_fixup_iowidth() - reduce io width to 8bit
251 * @p_dev: pcmcia device
253 * pcmcia_fixup_iowidth() allows a PCMCIA device driver to reduce the
254 * IO width to 8bit after having called pcmcia_enable_device()
257 int pcmcia_fixup_iowidth(struct pcmcia_device
*p_dev
)
259 struct pcmcia_socket
*s
= p_dev
->socket
;
260 pccard_io_map io_off
= { 0, 0, 0, 0, 1 };
264 mutex_lock(&s
->ops_mutex
);
266 dev_dbg(&p_dev
->dev
, "fixup iowidth to 8bit\n");
268 if (!(s
->state
& SOCKET_PRESENT
) ||
269 !(p_dev
->function_config
->state
& CONFIG_LOCKED
)) {
270 dev_dbg(&p_dev
->dev
, "No card? Config not locked?\n");
275 io_on
.speed
= io_speed
;
276 for (i
= 0; i
< MAX_IO_WIN
; i
++) {
282 io_on
.flags
= MAP_ACTIVE
| IO_DATA_PATH_WIDTH_8
;
283 io_on
.start
= s
->io
[i
].res
->start
;
284 io_on
.stop
= s
->io
[i
].res
->end
;
286 s
->ops
->set_io_map(s
, &io_off
);
288 s
->ops
->set_io_map(s
, &io_on
);
291 mutex_unlock(&s
->ops_mutex
);
295 EXPORT_SYMBOL(pcmcia_fixup_iowidth
);
299 * pcmcia_fixup_vpp() - set Vpp to a new voltage level
300 * @p_dev: pcmcia device
301 * @new_vpp: new Vpp voltage
303 * pcmcia_fixup_vpp() allows a PCMCIA device driver to set Vpp to
304 * a new voltage level between calls to pcmcia_enable_device()
305 * and pcmcia_disable_device().
307 int pcmcia_fixup_vpp(struct pcmcia_device
*p_dev
, unsigned char new_vpp
)
309 struct pcmcia_socket
*s
= p_dev
->socket
;
312 mutex_lock(&s
->ops_mutex
);
314 dev_dbg(&p_dev
->dev
, "fixup Vpp to %d\n", new_vpp
);
316 if (!(s
->state
& SOCKET_PRESENT
) ||
317 !(p_dev
->function_config
->state
& CONFIG_LOCKED
)) {
318 dev_dbg(&p_dev
->dev
, "No card? Config not locked?\n");
323 s
->socket
.Vpp
= new_vpp
;
324 if (s
->ops
->set_socket(s
, &s
->socket
)) {
325 dev_warn(&p_dev
->dev
, "Unable to set VPP\n");
329 p_dev
->vpp
= new_vpp
;
332 mutex_unlock(&s
->ops_mutex
);
336 EXPORT_SYMBOL(pcmcia_fixup_vpp
);
340 * pcmcia_release_configuration() - physically disable a PCMCIA device
341 * @p_dev: pcmcia device
343 * pcmcia_release_configuration() is the 1:1 counterpart to
344 * pcmcia_enable_device(): If a PCMCIA device is no longer used by any
345 * driver, the Vpp voltage is set to 0, IRQs will no longer be generated,
346 * and I/O ranges will be disabled. As pcmcia_release_io() and
347 * pcmcia_release_window() still need to be called, device drivers are
348 * expected to call pcmcia_disable_device() instead.
350 int pcmcia_release_configuration(struct pcmcia_device
*p_dev
)
352 pccard_io_map io
= { 0, 0, 0, 0, 1 };
353 struct pcmcia_socket
*s
= p_dev
->socket
;
357 mutex_lock(&s
->ops_mutex
);
358 c
= p_dev
->function_config
;
359 if (p_dev
->_locked
) {
361 if (--(s
->lock_count
) == 0) {
362 s
->socket
.flags
= SS_OUTPUT_ENA
; /* Is this correct? */
364 s
->socket
.io_irq
= 0;
365 s
->ops
->set_socket(s
, &s
->socket
);
368 if (c
->state
& CONFIG_LOCKED
) {
369 c
->state
&= ~CONFIG_LOCKED
;
370 if (c
->state
& CONFIG_IO_REQ
)
371 for (i
= 0; i
< MAX_IO_WIN
; i
++) {
375 if (s
->io
[i
].Config
!= 0)
378 s
->ops
->set_io_map(s
, &io
);
381 mutex_unlock(&s
->ops_mutex
);
388 * pcmcia_release_io() - release I/O allocated by a PCMCIA device
389 * @p_dev: pcmcia device
391 * pcmcia_release_io() releases the I/O ranges allocated by a PCMCIA
392 * device. This may be invoked some time after a card ejection has
393 * already dumped the actual socket configuration, so if the client is
394 * "stale", we don't bother checking the port ranges against the
395 * current socket values.
397 static int pcmcia_release_io(struct pcmcia_device
*p_dev
)
399 struct pcmcia_socket
*s
= p_dev
->socket
;
403 mutex_lock(&s
->ops_mutex
);
407 c
= p_dev
->function_config
;
409 release_io_space(s
, &c
->io
[0]);
412 release_io_space(s
, &c
->io
[1]);
415 c
->state
&= ~CONFIG_IO_REQ
;
418 mutex_unlock(&s
->ops_mutex
);
421 } /* pcmcia_release_io */
425 * pcmcia_release_window() - release reserved iomem for PCMCIA devices
426 * @p_dev: pcmcia device
427 * @res: iomem resource to release
429 * pcmcia_release_window() releases &struct resource *res which was
430 * previously reserved by calling pcmcia_request_window().
432 int pcmcia_release_window(struct pcmcia_device
*p_dev
, struct resource
*res
)
434 struct pcmcia_socket
*s
= p_dev
->socket
;
438 dev_dbg(&p_dev
->dev
, "releasing window %pR\n", res
);
440 w
= ((res
->flags
& IORESOURCE_BITS
& WIN_FLAGS_REQ
) >> 2) - 1;
444 mutex_lock(&s
->ops_mutex
);
447 if (!(p_dev
->_win
& CLIENT_WIN_REQ(w
))) {
448 dev_dbg(&p_dev
->dev
, "not releasing unknown window\n");
449 mutex_unlock(&s
->ops_mutex
);
453 /* Shut down memory window */
454 win
->flags
&= ~MAP_ACTIVE
;
455 s
->ops
->set_mem_map(s
, win
);
456 s
->state
&= ~SOCKET_WIN_REQ(w
);
458 /* Release system memory */
460 release_resource(res
);
461 release_resource(win
->res
);
465 res
->start
= res
->end
= 0;
466 res
->flags
= IORESOURCE_MEM
;
467 p_dev
->_win
&= ~CLIENT_WIN_REQ(w
);
468 mutex_unlock(&s
->ops_mutex
);
471 } /* pcmcia_release_window */
472 EXPORT_SYMBOL(pcmcia_release_window
);
476 * pcmcia_enable_device() - set up and activate a PCMCIA device
477 * @p_dev: the associated PCMCIA device
479 * pcmcia_enable_device() physically enables a PCMCIA device. It parses
480 * the flags passed to in @flags and stored in @p_dev->flags and sets up
481 * the Vpp voltage, enables the speaker line, I/O ports and store proper
482 * values to configuration registers.
484 int pcmcia_enable_device(struct pcmcia_device
*p_dev
)
488 struct pcmcia_socket
*s
= p_dev
->socket
;
491 unsigned char status
= 0;
492 unsigned char ext_status
= 0;
493 unsigned char option
= 0;
494 unsigned int flags
= p_dev
->config_flags
;
496 if (!(s
->state
& SOCKET_PRESENT
))
499 mutex_lock(&s
->ops_mutex
);
500 c
= p_dev
->function_config
;
501 if (c
->state
& CONFIG_LOCKED
) {
502 mutex_unlock(&s
->ops_mutex
);
503 dev_dbg(&p_dev
->dev
, "Configuration is locked\n");
507 /* Do power control. We don't allow changes in Vcc. */
508 s
->socket
.Vpp
= p_dev
->vpp
;
509 if (s
->ops
->set_socket(s
, &s
->socket
)) {
510 mutex_unlock(&s
->ops_mutex
);
511 dev_printk(KERN_WARNING
, &p_dev
->dev
,
512 "Unable to set socket state\n");
516 /* Pick memory or I/O card, DMA mode, interrupt */
517 if (p_dev
->_io
|| flags
& CONF_ENABLE_IRQ
)
518 flags
|= CONF_ENABLE_IOCARD
;
519 if (flags
& CONF_ENABLE_IOCARD
)
520 s
->socket
.flags
|= SS_IOCARD
;
521 if (flags
& CONF_ENABLE_ZVCARD
)
522 s
->socket
.flags
|= SS_ZVCARD
| SS_IOCARD
;
523 if (flags
& CONF_ENABLE_SPKR
) {
524 s
->socket
.flags
|= SS_SPKR_ENA
;
525 status
= CCSR_AUDIO_ENA
;
526 if (!(p_dev
->config_regs
& PRESENT_STATUS
))
527 dev_warn(&p_dev
->dev
, "speaker requested, but "
528 "PRESENT_STATUS not set!\n");
530 if (flags
& CONF_ENABLE_IRQ
)
531 s
->socket
.io_irq
= s
->pcmcia_irq
;
533 s
->socket
.io_irq
= 0;
534 if (flags
& CONF_ENABLE_ESR
) {
535 p_dev
->config_regs
|= PRESENT_EXT_STATUS
;
536 ext_status
= ESR_REQ_ATTN_ENA
;
538 s
->ops
->set_socket(s
, &s
->socket
);
542 "enable_device: V %d, flags %x, base %x, regs %x, idx %x\n",
543 p_dev
->vpp
, flags
, p_dev
->config_base
, p_dev
->config_regs
,
544 p_dev
->config_index
);
546 /* Set up CIS configuration registers */
547 base
= p_dev
->config_base
;
548 if (p_dev
->config_regs
& PRESENT_COPY
) {
550 dev_dbg(&p_dev
->dev
, "clearing CISREG_SCR\n");
551 pcmcia_write_cis_mem(s
, 1, (base
+ CISREG_SCR
)>>1, 1, &tmp
);
553 if (p_dev
->config_regs
& PRESENT_PIN_REPLACE
) {
555 dev_dbg(&p_dev
->dev
, "clearing CISREG_PRR\n");
556 pcmcia_write_cis_mem(s
, 1, (base
+ CISREG_PRR
)>>1, 1, &tmp
);
558 if (p_dev
->config_regs
& PRESENT_OPTION
) {
559 if (s
->functions
== 1) {
560 option
= p_dev
->config_index
& COR_CONFIG_MASK
;
562 option
= p_dev
->config_index
& COR_MFC_CONFIG_MASK
;
563 option
|= COR_FUNC_ENA
|COR_IREQ_ENA
;
564 if (p_dev
->config_regs
& PRESENT_IOBASE_0
)
565 option
|= COR_ADDR_DECODE
;
567 if ((flags
& CONF_ENABLE_IRQ
) &&
568 !(flags
& CONF_ENABLE_PULSE_IRQ
))
569 option
|= COR_LEVEL_REQ
;
570 pcmcia_write_cis_mem(s
, 1, (base
+ CISREG_COR
)>>1, 1, &option
);
573 if (p_dev
->config_regs
& PRESENT_STATUS
)
574 pcmcia_write_cis_mem(s
, 1, (base
+ CISREG_CCSR
)>>1, 1, &status
);
576 if (p_dev
->config_regs
& PRESENT_EXT_STATUS
)
577 pcmcia_write_cis_mem(s
, 1, (base
+ CISREG_ESR
)>>1, 1,
580 if (p_dev
->config_regs
& PRESENT_IOBASE_0
) {
581 u8 b
= c
->io
[0].start
& 0xff;
582 pcmcia_write_cis_mem(s
, 1, (base
+ CISREG_IOBASE_0
)>>1, 1, &b
);
583 b
= (c
->io
[0].start
>> 8) & 0xff;
584 pcmcia_write_cis_mem(s
, 1, (base
+ CISREG_IOBASE_1
)>>1, 1, &b
);
586 if (p_dev
->config_regs
& PRESENT_IOSIZE
) {
587 u8 b
= resource_size(&c
->io
[0]) + resource_size(&c
->io
[1]) - 1;
588 pcmcia_write_cis_mem(s
, 1, (base
+ CISREG_IOSIZE
)>>1, 1, &b
);
591 /* Configure I/O windows */
592 if (c
->state
& CONFIG_IO_REQ
) {
593 iomap
.speed
= io_speed
;
594 for (i
= 0; i
< MAX_IO_WIN
; i
++)
597 iomap
.flags
= MAP_ACTIVE
;
598 switch (s
->io
[i
].res
->flags
& IO_DATA_PATH_WIDTH
) {
599 case IO_DATA_PATH_WIDTH_16
:
600 iomap
.flags
|= MAP_16BIT
; break;
601 case IO_DATA_PATH_WIDTH_AUTO
:
602 iomap
.flags
|= MAP_AUTOSZ
; break;
606 iomap
.start
= s
->io
[i
].res
->start
;
607 iomap
.stop
= s
->io
[i
].res
->end
;
608 s
->ops
->set_io_map(s
, &iomap
);
613 c
->state
|= CONFIG_LOCKED
;
615 mutex_unlock(&s
->ops_mutex
);
617 } /* pcmcia_enable_device */
618 EXPORT_SYMBOL(pcmcia_enable_device
);
622 * pcmcia_request_io() - attempt to reserve port ranges for PCMCIA devices
623 * @p_dev: the associated PCMCIA device
625 * pcmcia_request_io() attempts to reserve the IO port ranges specified in
626 * &struct pcmcia_device @p_dev->resource[0] and @p_dev->resource[1]. The
627 * "start" value is the requested start of the IO port resource; "end"
628 * reflects the number of ports requested. The number of IO lines requested
629 * is specified in &struct pcmcia_device @p_dev->io_lines.
631 int pcmcia_request_io(struct pcmcia_device
*p_dev
)
633 struct pcmcia_socket
*s
= p_dev
->socket
;
634 config_t
*c
= p_dev
->function_config
;
637 mutex_lock(&s
->ops_mutex
);
638 dev_dbg(&p_dev
->dev
, "pcmcia_request_io: %pR , %pR",
639 &c
->io
[0], &c
->io
[1]);
641 if (!(s
->state
& SOCKET_PRESENT
)) {
642 dev_dbg(&p_dev
->dev
, "pcmcia_request_io: No card present\n");
646 if (c
->state
& CONFIG_LOCKED
) {
647 dev_dbg(&p_dev
->dev
, "Configuration is locked\n");
650 if (c
->state
& CONFIG_IO_REQ
) {
651 dev_dbg(&p_dev
->dev
, "IO already configured\n");
655 ret
= alloc_io_space(s
, &c
->io
[0], p_dev
->io_lines
);
660 ret
= alloc_io_space(s
, &c
->io
[1], p_dev
->io_lines
);
662 struct resource tmp
= c
->io
[0];
663 /* release the previously allocated resource */
664 release_io_space(s
, &c
->io
[0]);
665 /* but preserve the settings, for they worked... */
666 c
->io
[0].end
= resource_size(&tmp
);
667 c
->io
[0].start
= tmp
.start
;
668 c
->io
[0].flags
= tmp
.flags
;
674 c
->state
|= CONFIG_IO_REQ
;
677 dev_dbg(&p_dev
->dev
, "pcmcia_request_io succeeded: %pR , %pR",
678 &c
->io
[0], &c
->io
[1]);
680 mutex_unlock(&s
->ops_mutex
);
683 } /* pcmcia_request_io */
684 EXPORT_SYMBOL(pcmcia_request_io
);
688 * pcmcia_request_irq() - attempt to request a IRQ for a PCMCIA device
689 * @p_dev: the associated PCMCIA device
690 * @handler: IRQ handler to register
692 * pcmcia_request_irq() is a wrapper around request_irq() which allows
693 * the PCMCIA core to clean up the registration in pcmcia_disable_device().
694 * Drivers are free to use request_irq() directly, but then they need to
695 * call free_irq() themselfves, too. Also, only %IRQF_SHARED capable IRQ
696 * handlers are allowed.
698 int __must_check
pcmcia_request_irq(struct pcmcia_device
*p_dev
,
699 irq_handler_t handler
)
706 ret
= request_irq(p_dev
->irq
, handler
, IRQF_SHARED
,
707 p_dev
->devname
, p_dev
->priv
);
713 EXPORT_SYMBOL(pcmcia_request_irq
);
717 * pcmcia_request_exclusive_irq() - attempt to request an exclusive IRQ first
718 * @p_dev: the associated PCMCIA device
719 * @handler: IRQ handler to register
721 * pcmcia_request_exclusive_irq() is a wrapper around request_irq() which
722 * attempts first to request an exclusive IRQ. If it fails, it also accepts
723 * a shared IRQ, but prints out a warning. PCMCIA drivers should allow for
724 * IRQ sharing and either use request_irq directly (then they need to call
725 * free_irq() themselves, too), or the pcmcia_request_irq() function.
728 __pcmcia_request_exclusive_irq(struct pcmcia_device
*p_dev
,
729 irq_handler_t handler
)
736 ret
= request_irq(p_dev
->irq
, handler
, 0, p_dev
->devname
, p_dev
->priv
);
738 ret
= pcmcia_request_irq(p_dev
, handler
);
739 dev_printk(KERN_WARNING
, &p_dev
->dev
, "pcmcia: "
740 "request for exclusive IRQ could not be fulfilled.\n");
741 dev_printk(KERN_WARNING
, &p_dev
->dev
, "pcmcia: the driver "
742 "needs updating to supported shared IRQ lines.\n");
745 dev_printk(KERN_INFO
, &p_dev
->dev
, "request_irq() failed\n");
750 } /* pcmcia_request_exclusive_irq */
751 EXPORT_SYMBOL(__pcmcia_request_exclusive_irq
);
754 #ifdef CONFIG_PCMCIA_PROBE
756 /* mask of IRQs already reserved by other cards, we should avoid using them */
757 static u8 pcmcia_used_irq
[32];
759 static irqreturn_t
test_action(int cpl
, void *dev_id
)
765 * pcmcia_setup_isa_irq() - determine whether an ISA IRQ can be used
766 * @p_dev - the associated PCMCIA device
768 * locking note: must be called with ops_mutex locked.
770 static int pcmcia_setup_isa_irq(struct pcmcia_device
*p_dev
, int type
)
772 struct pcmcia_socket
*s
= p_dev
->socket
;
773 unsigned int try, irq
;
774 u32 mask
= s
->irq_mask
;
777 for (try = 0; try < 64; try++) {
783 /* marked as available by driver, not blocked by userspace? */
784 if (!((mask
>> irq
) & 1))
787 /* avoid an IRQ which is already used by another PCMCIA card */
788 if ((try < 32) && pcmcia_used_irq
[irq
])
791 /* register the correct driver, if possible, to check whether
792 * registering a dummy handle works, i.e. if the IRQ isn't
793 * marked as used by the kernel resource management core */
794 ret
= request_irq(irq
, test_action
, type
, p_dev
->devname
,
797 free_irq(irq
, p_dev
);
798 p_dev
->irq
= s
->pcmcia_irq
= irq
;
799 pcmcia_used_irq
[irq
]++;
807 void pcmcia_cleanup_irq(struct pcmcia_socket
*s
)
809 pcmcia_used_irq
[s
->pcmcia_irq
]--;
813 #else /* CONFIG_PCMCIA_PROBE */
815 static int pcmcia_setup_isa_irq(struct pcmcia_device
*p_dev
, int type
)
820 void pcmcia_cleanup_irq(struct pcmcia_socket
*s
)
826 #endif /* CONFIG_PCMCIA_PROBE */
830 * pcmcia_setup_irq() - determine IRQ to be used for device
831 * @p_dev - the associated PCMCIA device
833 * locking note: must be called with ops_mutex locked.
835 int pcmcia_setup_irq(struct pcmcia_device
*p_dev
)
837 struct pcmcia_socket
*s
= p_dev
->socket
;
842 /* already assigned? */
844 p_dev
->irq
= s
->pcmcia_irq
;
848 /* prefer an exclusive ISA irq */
849 if (!pcmcia_setup_isa_irq(p_dev
, 0))
852 /* but accept a shared ISA irq */
853 if (!pcmcia_setup_isa_irq(p_dev
, IRQF_SHARED
))
856 /* but use the PCI irq otherwise */
858 p_dev
->irq
= s
->pcmcia_irq
= s
->pci_irq
;
867 * pcmcia_request_window() - attempt to reserve iomem for PCMCIA devices
868 * @p_dev: the associated PCMCIA device
869 * @res: &struct resource pointing to p_dev->resource[2..5]
870 * @speed: access speed
872 * pcmcia_request_window() attepts to reserve an iomem ranges specified in
873 * &struct resource @res pointing to one of the entries in
874 * &struct pcmcia_device @p_dev->resource[2..5]. The "start" value is the
875 * requested start of the IO mem resource; "end" reflects the size
878 int pcmcia_request_window(struct pcmcia_device
*p_dev
, struct resource
*res
,
881 struct pcmcia_socket
*s
= p_dev
->socket
;
886 dev_dbg(&p_dev
->dev
, "request_window %pR %d\n", res
, speed
);
888 if (!(s
->state
& SOCKET_PRESENT
)) {
889 dev_dbg(&p_dev
->dev
, "No card present\n");
893 /* Window size defaults to smallest available */
895 res
->end
= s
->map_size
;
896 align
= (s
->features
& SS_CAP_MEM_ALIGN
) ? res
->end
: s
->map_size
;
897 if (res
->end
& (s
->map_size
-1)) {
898 dev_dbg(&p_dev
->dev
, "invalid map size\n");
901 if ((res
->start
&& (s
->features
& SS_CAP_STATIC_MAP
)) ||
902 (res
->start
& (align
-1))) {
903 dev_dbg(&p_dev
->dev
, "invalid base address\n");
909 /* Allocate system memory window */
910 mutex_lock(&s
->ops_mutex
);
911 for (w
= 0; w
< MAX_WIN
; w
++)
912 if (!(s
->state
& SOCKET_WIN_REQ(w
)))
915 dev_dbg(&p_dev
->dev
, "all windows are used already\n");
916 mutex_unlock(&s
->ops_mutex
);
922 if (!(s
->features
& SS_CAP_STATIC_MAP
)) {
923 win
->res
= pcmcia_find_mem_region(res
->start
, res
->end
, align
,
926 dev_dbg(&p_dev
->dev
, "allocating mem region failed\n");
927 mutex_unlock(&s
->ops_mutex
);
931 p_dev
->_win
|= CLIENT_WIN_REQ(w
);
933 /* Configure the socket controller */
935 win
->flags
= res
->flags
& WIN_FLAGS_MAP
;
939 if (s
->ops
->set_mem_map(s
, win
) != 0) {
940 dev_dbg(&p_dev
->dev
, "failed to set memory mapping\n");
941 mutex_unlock(&s
->ops_mutex
);
944 s
->state
|= SOCKET_WIN_REQ(w
);
946 /* Return window handle */
947 if (s
->features
& SS_CAP_STATIC_MAP
)
948 res
->start
= win
->static_start
;
950 res
->start
= win
->res
->start
;
952 /* convert to new-style resources */
953 res
->end
+= res
->start
- 1;
954 res
->flags
&= ~WIN_FLAGS_REQ
;
955 res
->flags
|= (win
->map
<< 2) | IORESOURCE_MEM
;
956 res
->parent
= win
->res
;
958 request_resource(&iomem_resource
, res
);
960 dev_dbg(&p_dev
->dev
, "request_window results in %pR\n", res
);
962 mutex_unlock(&s
->ops_mutex
);
965 } /* pcmcia_request_window */
966 EXPORT_SYMBOL(pcmcia_request_window
);
970 * pcmcia_disable_device() - disable and clean up a PCMCIA device
971 * @p_dev: the associated PCMCIA device
973 * pcmcia_disable_device() is the driver-callable counterpart to
974 * pcmcia_enable_device(): If a PCMCIA device is no longer used,
975 * drivers are expected to clean up and disable the device by calling
976 * this function. Any I/O ranges (iomem and ioports) will be released,
977 * the Vpp voltage will be set to 0, and IRQs will no longer be
978 * generated -- at least if there is no other card function (of
979 * multifunction devices) being used.
981 void pcmcia_disable_device(struct pcmcia_device
*p_dev
)
985 dev_dbg(&p_dev
->dev
, "disabling device\n");
987 for (i
= 0; i
< MAX_WIN
; i
++) {
988 struct resource
*res
= p_dev
->resource
[MAX_IO_WIN
+ i
];
989 if (res
->flags
& WIN_FLAGS_REQ
)
990 pcmcia_release_window(p_dev
, res
);
993 pcmcia_release_configuration(p_dev
);
994 pcmcia_release_io(p_dev
);
996 free_irq(p_dev
->irq
, p_dev
->priv
);
1000 EXPORT_SYMBOL(pcmcia_disable_device
);