mmc: implement SDIO IO_RW_DIRECT operation
[linux-2.6-block.git] / drivers / mmc / core / core.c
CommitLineData
1da177e4 1/*
aaac1b47 2 * linux/drivers/mmc/core/core.c
1da177e4
LT
3 *
4 * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
5b4fd9ae 5 * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
b855885e 6 * Copyright (C) 2005-2007 Pierre Ossman, All Rights Reserved.
bce40a36 7 * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved.
1da177e4
LT
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
1da177e4
LT
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/interrupt.h>
16#include <linux/completion.h>
17#include <linux/device.h>
18#include <linux/delay.h>
19#include <linux/pagemap.h>
20#include <linux/err.h>
b57c43ad
PO
21#include <asm/scatterlist.h>
22#include <linux/scatterlist.h>
1da177e4
LT
23
24#include <linux/mmc/card.h>
25#include <linux/mmc/host.h>
da7fbe58
PO
26#include <linux/mmc/mmc.h>
27#include <linux/mmc/sd.h>
1da177e4 28
aaac1b47 29#include "core.h"
ffce2e7e
PO
30#include "bus.h"
31#include "host.h"
da7fbe58
PO
32
33#include "mmc_ops.h"
34#include "sd_ops.h"
5c4e6f13 35#include "sdio_ops.h"
1da177e4 36
7ea239d9
PO
37extern int mmc_attach_mmc(struct mmc_host *host, u32 ocr);
38extern int mmc_attach_sd(struct mmc_host *host, u32 ocr);
5c4e6f13 39extern int mmc_attach_sdio(struct mmc_host *host, u32 ocr);
1da177e4 40
ffce2e7e
PO
41static struct workqueue_struct *workqueue;
42
43/*
44 * Internal function. Schedule delayed work in the MMC work queue.
45 */
46static int mmc_schedule_delayed_work(struct delayed_work *work,
47 unsigned long delay)
48{
49 return queue_delayed_work(workqueue, work, delay);
50}
51
52/*
53 * Internal function. Flush all scheduled work from the MMC work queue.
54 */
55static void mmc_flush_scheduled_work(void)
56{
57 flush_workqueue(workqueue);
58}
59
1da177e4 60/**
fe10c6ab
RK
61 * mmc_request_done - finish processing an MMC request
62 * @host: MMC host which completed request
63 * @mrq: MMC request which request
1da177e4
LT
64 *
65 * MMC drivers should call this function when they have completed
fe10c6ab 66 * their processing of a request.
1da177e4
LT
67 */
68void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
69{
70 struct mmc_command *cmd = mrq->cmd;
920e70c5
RK
71 int err = cmd->error;
72
1da177e4 73 if (err && cmd->retries) {
e4d21708
PO
74 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
75 mmc_hostname(host), cmd->opcode, err);
76
1da177e4
LT
77 cmd->retries--;
78 cmd->error = 0;
79 host->ops->request(host, mrq);
e4d21708
PO
80 } else {
81 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
82 mmc_hostname(host), cmd->opcode, err,
83 cmd->resp[0], cmd->resp[1],
84 cmd->resp[2], cmd->resp[3]);
85
86 if (mrq->data) {
87 pr_debug("%s: %d bytes transferred: %d\n",
88 mmc_hostname(host),
89 mrq->data->bytes_xfered, mrq->data->error);
90 }
91
92 if (mrq->stop) {
93 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
94 mmc_hostname(host), mrq->stop->opcode,
95 mrq->stop->error,
96 mrq->stop->resp[0], mrq->stop->resp[1],
97 mrq->stop->resp[2], mrq->stop->resp[3]);
98 }
99
100 if (mrq->done)
101 mrq->done(mrq);
1da177e4
LT
102 }
103}
104
105EXPORT_SYMBOL(mmc_request_done);
106
39361851 107static void
1da177e4
LT
108mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
109{
976d9276
PO
110#ifdef CONFIG_MMC_DEBUG
111 unsigned int i, sz;
112#endif
113
920e70c5
RK
114 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
115 mmc_hostname(host), mrq->cmd->opcode,
116 mrq->cmd->arg, mrq->cmd->flags);
1da177e4 117
e4d21708
PO
118 if (mrq->data) {
119 pr_debug("%s: blksz %d blocks %d flags %08x "
120 "tsac %d ms nsac %d\n",
121 mmc_hostname(host), mrq->data->blksz,
122 mrq->data->blocks, mrq->data->flags,
123 mrq->data->timeout_ns / 10000000,
124 mrq->data->timeout_clks);
125 }
126
127 if (mrq->stop) {
128 pr_debug("%s: CMD%u arg %08x flags %08x\n",
129 mmc_hostname(host), mrq->stop->opcode,
130 mrq->stop->arg, mrq->stop->flags);
131 }
132
f22ee4ed 133 WARN_ON(!host->claimed);
1da177e4
LT
134
135 mrq->cmd->error = 0;
136 mrq->cmd->mrq = mrq;
137 if (mrq->data) {
fe4a3c7a 138 BUG_ON(mrq->data->blksz > host->max_blk_size);
55db890a
PO
139 BUG_ON(mrq->data->blocks > host->max_blk_count);
140 BUG_ON(mrq->data->blocks * mrq->data->blksz >
141 host->max_req_size);
fe4a3c7a 142
976d9276
PO
143#ifdef CONFIG_MMC_DEBUG
144 sz = 0;
145 for (i = 0;i < mrq->data->sg_len;i++)
146 sz += mrq->data->sg[i].length;
147 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
148#endif
149
1da177e4
LT
150 mrq->cmd->data = mrq->data;
151 mrq->data->error = 0;
152 mrq->data->mrq = mrq;
153 if (mrq->stop) {
154 mrq->data->stop = mrq->stop;
155 mrq->stop->error = 0;
156 mrq->stop->mrq = mrq;
157 }
158 }
159 host->ops->request(host, mrq);
160}
161
1da177e4
LT
162static void mmc_wait_done(struct mmc_request *mrq)
163{
164 complete(mrq->done_data);
165}
166
67a61c48
PO
167/**
168 * mmc_wait_for_req - start a request and wait for completion
169 * @host: MMC host to start command
170 * @mrq: MMC request to start
171 *
172 * Start a new MMC custom command request for a host, and wait
173 * for the command to complete. Does not attempt to parse the
174 * response.
175 */
176void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
1da177e4 177{
0afffc72 178 DECLARE_COMPLETION_ONSTACK(complete);
1da177e4
LT
179
180 mrq->done_data = &complete;
181 mrq->done = mmc_wait_done;
182
183 mmc_start_request(host, mrq);
184
185 wait_for_completion(&complete);
1da177e4
LT
186}
187
188EXPORT_SYMBOL(mmc_wait_for_req);
189
190/**
191 * mmc_wait_for_cmd - start a command and wait for completion
192 * @host: MMC host to start command
193 * @cmd: MMC command to start
194 * @retries: maximum number of retries
195 *
196 * Start a new MMC command for a host, and wait for the command
197 * to complete. Return any error that occurred while the command
198 * was executing. Do not attempt to parse the response.
199 */
200int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
201{
202 struct mmc_request mrq;
203
f22ee4ed 204 BUG_ON(!host->claimed);
1da177e4
LT
205
206 memset(&mrq, 0, sizeof(struct mmc_request));
207
208 memset(cmd->resp, 0, sizeof(cmd->resp));
209 cmd->retries = retries;
210
211 mrq.cmd = cmd;
212 cmd->data = NULL;
213
214 mmc_wait_for_req(host, &mrq);
215
216 return cmd->error;
217}
218
219EXPORT_SYMBOL(mmc_wait_for_cmd);
220
d773d725
RK
221/**
222 * mmc_set_data_timeout - set the timeout for a data command
223 * @data: data phase for command
224 * @card: the MMC card associated with the data transfer
67a61c48
PO
225 *
226 * Computes the data timeout parameters according to the
227 * correct algorithm given the card type.
d773d725 228 */
b146d26a 229void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
d773d725
RK
230{
231 unsigned int mult;
232
233 /*
234 * SD cards use a 100 multiplier rather than 10
235 */
236 mult = mmc_card_sd(card) ? 100 : 10;
237
238 /*
239 * Scale up the multiplier (and therefore the timeout) by
240 * the r2w factor for writes.
241 */
b146d26a 242 if (data->flags & MMC_DATA_WRITE)
d773d725
RK
243 mult <<= card->csd.r2w_factor;
244
245 data->timeout_ns = card->csd.tacc_ns * mult;
246 data->timeout_clks = card->csd.tacc_clks * mult;
247
248 /*
249 * SD cards also have an upper limit on the timeout.
250 */
251 if (mmc_card_sd(card)) {
252 unsigned int timeout_us, limit_us;
253
254 timeout_us = data->timeout_ns / 1000;
255 timeout_us += data->timeout_clks * 1000 /
256 (card->host->ios.clock / 1000);
257
b146d26a 258 if (data->flags & MMC_DATA_WRITE)
d773d725
RK
259 limit_us = 250000;
260 else
261 limit_us = 100000;
262
fba68bd2
PL
263 /*
264 * SDHC cards always use these fixed values.
265 */
266 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
d773d725
RK
267 data->timeout_ns = limit_us * 1000;
268 data->timeout_clks = 0;
269 }
270 }
271}
272EXPORT_SYMBOL(mmc_set_data_timeout);
273
1da177e4 274/**
67a61c48 275 * mmc_claim_host - exclusively claim a host
1da177e4 276 * @host: mmc host to claim
1da177e4 277 *
67a61c48 278 * Claim a host for a set of operations.
1da177e4 279 */
b855885e 280void mmc_claim_host(struct mmc_host *host)
1da177e4
LT
281{
282 DECLARE_WAITQUEUE(wait, current);
283 unsigned long flags;
1da177e4 284
cf795bfb
PO
285 might_sleep();
286
1da177e4
LT
287 add_wait_queue(&host->wq, &wait);
288 spin_lock_irqsave(&host->lock, flags);
289 while (1) {
290 set_current_state(TASK_UNINTERRUPTIBLE);
f22ee4ed 291 if (!host->claimed)
1da177e4
LT
292 break;
293 spin_unlock_irqrestore(&host->lock, flags);
294 schedule();
295 spin_lock_irqsave(&host->lock, flags);
296 }
297 set_current_state(TASK_RUNNING);
f22ee4ed 298 host->claimed = 1;
1da177e4
LT
299 spin_unlock_irqrestore(&host->lock, flags);
300 remove_wait_queue(&host->wq, &wait);
1da177e4
LT
301}
302
b855885e 303EXPORT_SYMBOL(mmc_claim_host);
1da177e4
LT
304
305/**
306 * mmc_release_host - release a host
307 * @host: mmc host to release
308 *
309 * Release a MMC host, allowing others to claim the host
310 * for their operations.
311 */
312void mmc_release_host(struct mmc_host *host)
313{
314 unsigned long flags;
315
f22ee4ed 316 BUG_ON(!host->claimed);
1da177e4
LT
317
318 spin_lock_irqsave(&host->lock, flags);
f22ee4ed 319 host->claimed = 0;
1da177e4
LT
320 spin_unlock_irqrestore(&host->lock, flags);
321
322 wake_up(&host->wq);
323}
324
325EXPORT_SYMBOL(mmc_release_host);
326
7ea239d9
PO
327/*
328 * Internal function that does the actual ios call to the host driver,
329 * optionally printing some debug output.
330 */
920e70c5
RK
331static inline void mmc_set_ios(struct mmc_host *host)
332{
333 struct mmc_ios *ios = &host->ios;
334
cd9277c0
PO
335 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
336 "width %u timing %u\n",
920e70c5
RK
337 mmc_hostname(host), ios->clock, ios->bus_mode,
338 ios->power_mode, ios->chip_select, ios->vdd,
cd9277c0 339 ios->bus_width, ios->timing);
fba68bd2 340
920e70c5
RK
341 host->ops->set_ios(host, ios);
342}
343
7ea239d9
PO
344/*
345 * Control chip select pin on a host.
346 */
da7fbe58 347void mmc_set_chip_select(struct mmc_host *host, int mode)
1da177e4 348{
da7fbe58
PO
349 host->ios.chip_select = mode;
350 mmc_set_ios(host);
1da177e4
LT
351}
352
7ea239d9
PO
353/*
354 * Sets the host clock to the highest possible frequency that
355 * is below "hz".
356 */
357void mmc_set_clock(struct mmc_host *host, unsigned int hz)
358{
359 WARN_ON(hz < host->f_min);
360
361 if (hz > host->f_max)
362 hz = host->f_max;
363
364 host->ios.clock = hz;
365 mmc_set_ios(host);
366}
367
368/*
369 * Change the bus mode (open drain/push-pull) of a host.
370 */
371void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
372{
373 host->ios.bus_mode = mode;
374 mmc_set_ios(host);
375}
376
377/*
378 * Change data bus width of a host.
379 */
380void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
381{
382 host->ios.bus_width = width;
383 mmc_set_ios(host);
384}
385
1da177e4
LT
386/*
387 * Mask off any voltages we don't support and select
388 * the lowest voltage
389 */
7ea239d9 390u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
1da177e4
LT
391{
392 int bit;
393
394 ocr &= host->ocr_avail;
395
396 bit = ffs(ocr);
397 if (bit) {
398 bit -= 1;
399
63ef731a 400 ocr &= 3 << bit;
1da177e4
LT
401
402 host->ios.vdd = bit;
920e70c5 403 mmc_set_ios(host);
1da177e4
LT
404 } else {
405 ocr = 0;
406 }
407
408 return ocr;
409}
410
b57c43ad 411/*
7ea239d9 412 * Select timing parameters for host.
b57c43ad 413 */
7ea239d9 414void mmc_set_timing(struct mmc_host *host, unsigned int timing)
b57c43ad 415{
7ea239d9
PO
416 host->ios.timing = timing;
417 mmc_set_ios(host);
b57c43ad
PO
418}
419
1da177e4 420/*
45f8245b
RK
421 * Apply power to the MMC stack. This is a two-stage process.
422 * First, we enable power to the card without the clock running.
423 * We then wait a bit for the power to stabilise. Finally,
424 * enable the bus drivers and clock to the card.
425 *
426 * We must _NOT_ enable the clock prior to power stablising.
427 *
428 * If a host does all the power sequencing itself, ignore the
429 * initial MMC_POWER_UP stage.
1da177e4
LT
430 */
431static void mmc_power_up(struct mmc_host *host)
432{
433 int bit = fls(host->ocr_avail) - 1;
434
435 host->ios.vdd = bit;
436 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
865e9f13 437 host->ios.chip_select = MMC_CS_DONTCARE;
1da177e4 438 host->ios.power_mode = MMC_POWER_UP;
f218278a 439 host->ios.bus_width = MMC_BUS_WIDTH_1;
cd9277c0 440 host->ios.timing = MMC_TIMING_LEGACY;
920e70c5 441 mmc_set_ios(host);
1da177e4
LT
442
443 mmc_delay(1);
444
445 host->ios.clock = host->f_min;
446 host->ios.power_mode = MMC_POWER_ON;
920e70c5 447 mmc_set_ios(host);
1da177e4
LT
448
449 mmc_delay(2);
450}
451
452static void mmc_power_off(struct mmc_host *host)
453{
454 host->ios.clock = 0;
455 host->ios.vdd = 0;
456 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
865e9f13 457 host->ios.chip_select = MMC_CS_DONTCARE;
1da177e4 458 host->ios.power_mode = MMC_POWER_OFF;
f218278a 459 host->ios.bus_width = MMC_BUS_WIDTH_1;
cd9277c0 460 host->ios.timing = MMC_TIMING_LEGACY;
920e70c5 461 mmc_set_ios(host);
1da177e4
LT
462}
463
39361851
AB
464/*
465 * Cleanup when the last reference to the bus operator is dropped.
466 */
467void __mmc_release_bus(struct mmc_host *host)
468{
469 BUG_ON(!host);
470 BUG_ON(host->bus_refs);
471 BUG_ON(!host->bus_dead);
472
473 host->bus_ops = NULL;
474}
475
476/*
477 * Increase reference count of bus operator
478 */
479static inline void mmc_bus_get(struct mmc_host *host)
480{
481 unsigned long flags;
482
483 spin_lock_irqsave(&host->lock, flags);
484 host->bus_refs++;
485 spin_unlock_irqrestore(&host->lock, flags);
486}
487
488/*
489 * Decrease reference count of bus operator and free it if
490 * it is the last reference.
491 */
492static inline void mmc_bus_put(struct mmc_host *host)
493{
494 unsigned long flags;
495
496 spin_lock_irqsave(&host->lock, flags);
497 host->bus_refs--;
498 if ((host->bus_refs == 0) && host->bus_ops)
499 __mmc_release_bus(host);
500 spin_unlock_irqrestore(&host->lock, flags);
501}
502
1da177e4 503/*
7ea239d9
PO
504 * Assign a mmc bus handler to a host. Only one bus handler may control a
505 * host at any given time.
1da177e4 506 */
7ea239d9 507void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
1da177e4 508{
7ea239d9 509 unsigned long flags;
e45a1bd2 510
7ea239d9
PO
511 BUG_ON(!host);
512 BUG_ON(!ops);
b855885e 513
7ea239d9 514 BUG_ON(!host->claimed);
bce40a36 515
7ea239d9 516 spin_lock_irqsave(&host->lock, flags);
bce40a36 517
7ea239d9
PO
518 BUG_ON(host->bus_ops);
519 BUG_ON(host->bus_refs);
b57c43ad 520
7ea239d9
PO
521 host->bus_ops = ops;
522 host->bus_refs = 1;
523 host->bus_dead = 0;
b57c43ad 524
7ea239d9 525 spin_unlock_irqrestore(&host->lock, flags);
b57c43ad
PO
526}
527
7ea239d9
PO
528/*
529 * Remove the current bus handler from a host. Assumes that there are
530 * no interesting cards left, so the bus is powered down.
531 */
532void mmc_detach_bus(struct mmc_host *host)
7ccd266e 533{
7ea239d9 534 unsigned long flags;
7ccd266e 535
7ea239d9 536 BUG_ON(!host);
7ccd266e 537
7ea239d9
PO
538 BUG_ON(!host->claimed);
539 BUG_ON(!host->bus_ops);
cd9277c0 540
7ea239d9 541 spin_lock_irqsave(&host->lock, flags);
7ccd266e 542
7ea239d9 543 host->bus_dead = 1;
7ccd266e 544
7ea239d9 545 spin_unlock_irqrestore(&host->lock, flags);
1da177e4 546
7ea239d9 547 mmc_power_off(host);
1da177e4 548
7ea239d9 549 mmc_bus_put(host);
1da177e4
LT
550}
551
1da177e4
LT
552/**
553 * mmc_detect_change - process change of state on a MMC socket
554 * @host: host which changed state.
8dc00335 555 * @delay: optional delay to wait before detection (jiffies)
1da177e4 556 *
67a61c48
PO
557 * MMC drivers should call this when they detect a card has been
558 * inserted or removed. The MMC layer will confirm that any
559 * present card is still functional, and initialize any newly
560 * inserted.
1da177e4 561 */
8dc00335 562void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1da177e4 563{
3b91e550 564#ifdef CONFIG_MMC_DEBUG
1efd48b3 565 unsigned long flags;
01f41ec7 566 spin_lock_irqsave(&host->lock, flags);
3b91e550 567 BUG_ON(host->removed);
01f41ec7 568 spin_unlock_irqrestore(&host->lock, flags);
3b91e550
PO
569#endif
570
c4028958 571 mmc_schedule_delayed_work(&host->detect, delay);
1da177e4
LT
572}
573
574EXPORT_SYMBOL(mmc_detect_change);
575
576
b93931a6 577void mmc_rescan(struct work_struct *work)
1da177e4 578{
c4028958
DH
579 struct mmc_host *host =
580 container_of(work, struct mmc_host, detect.work);
7ea239d9
PO
581 u32 ocr;
582 int err;
1da177e4 583
7ea239d9 584 mmc_bus_get(host);
b855885e 585
7ea239d9 586 if (host->bus_ops == NULL) {
1da177e4 587 /*
7ea239d9
PO
588 * Only we can add a new handler, so it's safe to
589 * release the lock here.
1da177e4 590 */
7ea239d9 591 mmc_bus_put(host);
1da177e4 592
7ea239d9 593 mmc_claim_host(host);
1da177e4 594
7ea239d9
PO
595 mmc_power_up(host);
596 mmc_go_idle(host);
1da177e4 597
7ea239d9 598 mmc_send_if_cond(host, host->ocr_avail);
1da177e4 599
5c4e6f13
PO
600 /*
601 * First we search for SDIO...
602 */
603 err = mmc_send_io_op_cond(host, 0, &ocr);
604 if (!err) {
605 if (mmc_attach_sdio(host, ocr))
606 mmc_power_off(host);
607 return;
608 }
609
610 /*
611 * ...then normal SD...
612 */
7ea239d9 613 err = mmc_send_app_op_cond(host, 0, &ocr);
17b0429d 614 if (!err) {
7ea239d9
PO
615 if (mmc_attach_sd(host, ocr))
616 mmc_power_off(host);
5c4e6f13
PO
617 return;
618 }
619
620 /*
621 * ...and finally MMC.
622 */
623 err = mmc_send_op_cond(host, 0, &ocr);
624 if (!err) {
625 if (mmc_attach_mmc(host, ocr))
7ea239d9 626 mmc_power_off(host);
5c4e6f13 627 return;
7ea239d9 628 }
5c4e6f13
PO
629
630 mmc_release_host(host);
631 mmc_power_off(host);
7ea239d9
PO
632 } else {
633 if (host->bus_ops->detect && !host->bus_dead)
634 host->bus_ops->detect(host);
635
636 mmc_bus_put(host);
637 }
1da177e4
LT
638}
639
b93931a6 640void mmc_start_host(struct mmc_host *host)
1da177e4 641{
b93931a6
PO
642 mmc_power_off(host);
643 mmc_detect_change(host, 0);
1da177e4
LT
644}
645
b93931a6 646void mmc_stop_host(struct mmc_host *host)
1da177e4 647{
3b91e550 648#ifdef CONFIG_MMC_DEBUG
1efd48b3
PO
649 unsigned long flags;
650 spin_lock_irqsave(&host->lock, flags);
3b91e550 651 host->removed = 1;
1efd48b3 652 spin_unlock_irqrestore(&host->lock, flags);
3b91e550
PO
653#endif
654
655 mmc_flush_scheduled_work();
656
7ea239d9
PO
657 mmc_bus_get(host);
658 if (host->bus_ops && !host->bus_dead) {
659 if (host->bus_ops->remove)
660 host->bus_ops->remove(host);
661
662 mmc_claim_host(host);
663 mmc_detach_bus(host);
664 mmc_release_host(host);
1da177e4 665 }
7ea239d9
PO
666 mmc_bus_put(host);
667
668 BUG_ON(host->card);
1da177e4
LT
669
670 mmc_power_off(host);
671}
672
1da177e4
LT
673#ifdef CONFIG_PM
674
675/**
676 * mmc_suspend_host - suspend a host
677 * @host: mmc host
678 * @state: suspend mode (PM_SUSPEND_xxx)
679 */
e5378ca8 680int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
1da177e4 681{
b5af25be
PO
682 mmc_flush_scheduled_work();
683
7ea239d9
PO
684 mmc_bus_get(host);
685 if (host->bus_ops && !host->bus_dead) {
6abaa0c9
PO
686 if (host->bus_ops->suspend)
687 host->bus_ops->suspend(host);
688 if (!host->bus_ops->resume) {
689 if (host->bus_ops->remove)
690 host->bus_ops->remove(host);
691
692 mmc_claim_host(host);
693 mmc_detach_bus(host);
694 mmc_release_host(host);
695 }
b5af25be 696 }
7ea239d9
PO
697 mmc_bus_put(host);
698
1da177e4 699 mmc_power_off(host);
1da177e4
LT
700
701 return 0;
702}
703
704EXPORT_SYMBOL(mmc_suspend_host);
705
706/**
707 * mmc_resume_host - resume a previously suspended host
708 * @host: mmc host
709 */
710int mmc_resume_host(struct mmc_host *host)
711{
6abaa0c9
PO
712 mmc_bus_get(host);
713 if (host->bus_ops && !host->bus_dead) {
714 mmc_power_up(host);
715 BUG_ON(!host->bus_ops->resume);
716 host->bus_ops->resume(host);
717 }
718 mmc_bus_put(host);
719
720 /*
721 * We add a slight delay here so that resume can progress
722 * in parallel.
723 */
724 mmc_detect_change(host, 1);
1da177e4
LT
725
726 return 0;
727}
728
729EXPORT_SYMBOL(mmc_resume_host);
730
731#endif
732
ffce2e7e
PO
733static int __init mmc_init(void)
734{
735 int ret;
736
737 workqueue = create_singlethread_workqueue("kmmcd");
738 if (!workqueue)
739 return -ENOMEM;
740
741 ret = mmc_register_bus();
742 if (ret == 0) {
743 ret = mmc_register_host_class();
744 if (ret)
745 mmc_unregister_bus();
746 }
747 return ret;
748}
749
750static void __exit mmc_exit(void)
751{
752 mmc_unregister_host_class();
753 mmc_unregister_bus();
754 destroy_workqueue(workqueue);
755}
756
757module_init(mmc_init);
758module_exit(mmc_exit);
759
1da177e4 760MODULE_LICENSE("GPL");