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