ia64/kvm: compilation fix. export account_system_vtime.
[pv_ops_mirror.git] / drivers / mmc / core / sdio_irq.c
blobc292e124107ac13ec36be751024ad3728cafaa40
1 /*
2 * linux/drivers/mmc/core/sdio_irq.c
4 * Author: Nicolas Pitre
5 * Created: June 18, 2007
6 * Copyright: MontaVista Software Inc.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or (at
11 * your option) any later version.
14 #include <linux/kernel.h>
15 #include <linux/sched.h>
16 #include <linux/kthread.h>
17 #include <linux/wait.h>
18 #include <linux/delay.h>
20 #include <linux/mmc/core.h>
21 #include <linux/mmc/host.h>
22 #include <linux/mmc/card.h>
23 #include <linux/mmc/sdio.h>
24 #include <linux/mmc/sdio_func.h>
26 #include "sdio_ops.h"
28 static int process_sdio_pending_irqs(struct mmc_card *card)
30 int i, ret, count;
31 unsigned char pending;
33 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_INTx, 0, &pending);
34 if (ret) {
35 printk(KERN_DEBUG "%s: error %d reading SDIO_CCCR_INTx\n",
36 mmc_card_id(card), ret);
37 return ret;
40 count = 0;
41 for (i = 1; i <= 7; i++) {
42 if (pending & (1 << i)) {
43 struct sdio_func *func = card->sdio_func[i - 1];
44 if (!func) {
45 printk(KERN_WARNING "%s: pending IRQ for "
46 "non-existant function\n",
47 mmc_card_id(card));
48 ret = -EINVAL;
49 } else if (func->irq_handler) {
50 func->irq_handler(func);
51 count++;
52 } else {
53 printk(KERN_WARNING "%s: pending IRQ with no handler\n",
54 sdio_func_id(func));
55 ret = -EINVAL;
60 if (count)
61 return count;
63 return ret;
66 static int sdio_irq_thread(void *_host)
68 struct mmc_host *host = _host;
69 struct sched_param param = { .sched_priority = 1 };
70 unsigned long period, idle_period;
71 int ret;
73 sched_setscheduler(current, SCHED_FIFO, &param);
76 * We want to allow for SDIO cards to work even on non SDIO
77 * aware hosts. One thing that non SDIO host cannot do is
78 * asynchronous notification of pending SDIO card interrupts
79 * hence we poll for them in that case.
81 idle_period = msecs_to_jiffies(10);
82 period = (host->caps & MMC_CAP_SDIO_IRQ) ?
83 MAX_SCHEDULE_TIMEOUT : idle_period;
85 pr_debug("%s: IRQ thread started (poll period = %lu jiffies)\n",
86 mmc_hostname(host), period);
88 do {
90 * We claim the host here on drivers behalf for a couple
91 * reasons:
93 * 1) it is already needed to retrieve the CCCR_INTx;
94 * 2) we want the driver(s) to clear the IRQ condition ASAP;
95 * 3) we need to control the abort condition locally.
97 * Just like traditional hard IRQ handlers, we expect SDIO
98 * IRQ handlers to be quick and to the point, so that the
99 * holding of the host lock does not cover too much work
100 * that doesn't require that lock to be held.
102 ret = __mmc_claim_host(host, &host->sdio_irq_thread_abort);
103 if (ret)
104 break;
105 ret = process_sdio_pending_irqs(host->card);
106 mmc_release_host(host);
109 * Give other threads a chance to run in the presence of
110 * errors. FIXME: determine if due to card removal and
111 * possibly exit this thread if so.
113 if (ret < 0)
114 ssleep(1);
117 * Adaptive polling frequency based on the assumption
118 * that an interrupt will be closely followed by more.
119 * This has a substantial benefit for network devices.
121 if (!(host->caps & MMC_CAP_SDIO_IRQ)) {
122 if (ret > 0)
123 period /= 2;
124 else {
125 period++;
126 if (period > idle_period)
127 period = idle_period;
131 set_current_state(TASK_INTERRUPTIBLE);
132 if (host->caps & MMC_CAP_SDIO_IRQ)
133 host->ops->enable_sdio_irq(host, 1);
134 if (!kthread_should_stop())
135 schedule_timeout(period);
136 set_current_state(TASK_RUNNING);
137 } while (!kthread_should_stop());
139 if (host->caps & MMC_CAP_SDIO_IRQ)
140 host->ops->enable_sdio_irq(host, 0);
142 pr_debug("%s: IRQ thread exiting with code %d\n",
143 mmc_hostname(host), ret);
145 return ret;
148 static int sdio_card_irq_get(struct mmc_card *card)
150 struct mmc_host *host = card->host;
152 WARN_ON(!host->claimed);
154 if (!host->sdio_irqs++) {
155 atomic_set(&host->sdio_irq_thread_abort, 0);
156 host->sdio_irq_thread =
157 kthread_run(sdio_irq_thread, host, "ksdiorqd");
158 if (IS_ERR(host->sdio_irq_thread)) {
159 int err = PTR_ERR(host->sdio_irq_thread);
160 host->sdio_irqs--;
161 return err;
165 return 0;
168 static int sdio_card_irq_put(struct mmc_card *card)
170 struct mmc_host *host = card->host;
172 WARN_ON(!host->claimed);
173 BUG_ON(host->sdio_irqs < 1);
175 if (!--host->sdio_irqs) {
176 atomic_set(&host->sdio_irq_thread_abort, 1);
177 kthread_stop(host->sdio_irq_thread);
180 return 0;
184 * sdio_claim_irq - claim the IRQ for a SDIO function
185 * @func: SDIO function
186 * @handler: IRQ handler callback
188 * Claim and activate the IRQ for the given SDIO function. The provided
189 * handler will be called when that IRQ is asserted. The host is always
190 * claimed already when the handler is called so the handler must not
191 * call sdio_claim_host() nor sdio_release_host().
193 int sdio_claim_irq(struct sdio_func *func, sdio_irq_handler_t *handler)
195 int ret;
196 unsigned char reg;
198 BUG_ON(!func);
199 BUG_ON(!func->card);
201 pr_debug("SDIO: Enabling IRQ for %s...\n", sdio_func_id(func));
203 if (func->irq_handler) {
204 pr_debug("SDIO: IRQ for %s already in use.\n", sdio_func_id(func));
205 return -EBUSY;
208 ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, &reg);
209 if (ret)
210 return ret;
212 reg |= 1 << func->num;
214 reg |= 1; /* Master interrupt enable */
216 ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, reg, NULL);
217 if (ret)
218 return ret;
220 func->irq_handler = handler;
221 ret = sdio_card_irq_get(func->card);
222 if (ret)
223 func->irq_handler = NULL;
225 return ret;
227 EXPORT_SYMBOL_GPL(sdio_claim_irq);
230 * sdio_release_irq - release the IRQ for a SDIO function
231 * @func: SDIO function
233 * Disable and release the IRQ for the given SDIO function.
235 int sdio_release_irq(struct sdio_func *func)
237 int ret;
238 unsigned char reg;
240 BUG_ON(!func);
241 BUG_ON(!func->card);
243 pr_debug("SDIO: Disabling IRQ for %s...\n", sdio_func_id(func));
245 if (func->irq_handler) {
246 func->irq_handler = NULL;
247 sdio_card_irq_put(func->card);
250 ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, &reg);
251 if (ret)
252 return ret;
254 reg &= ~(1 << func->num);
256 /* Disable master interrupt with the last function interrupt */
257 if (!(reg & 0xFE))
258 reg = 0;
260 ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, reg, NULL);
261 if (ret)
262 return ret;
264 return 0;
266 EXPORT_SYMBOL_GPL(sdio_release_irq);