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