2 * linux/drivers/mmc/core/bus.c
4 * Copyright (C) 2003 Russell King, All Rights Reserved.
5 * Copyright (C) 2007 Pierre Ossman
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * MMC card bus driver model
14 #include <linux/export.h>
15 #include <linux/device.h>
16 #include <linux/err.h>
17 #include <linux/slab.h>
18 #include <linux/pm_runtime.h>
20 #include <linux/mmc/card.h>
21 #include <linux/mmc/host.h>
27 #define to_mmc_driver(d) container_of(d, struct mmc_driver, drv)
29 static ssize_t mmc_type_show(struct device *dev,
30 struct device_attribute *attr, char *buf)
32 struct mmc_card *card = mmc_dev_to_card(dev);
36 return sprintf(buf, "MMC\n");
38 return sprintf(buf, "SD\n");
40 return sprintf(buf, "SDIO\n");
41 case MMC_TYPE_SD_COMBO:
42 return sprintf(buf, "SDcombo\n");
48 static struct device_attribute mmc_dev_attrs[] = {
49 __ATTR(type, S_IRUGO, mmc_type_show, NULL),
54 * This currently matches any MMC driver to any MMC card - drivers
55 * themselves make the decision whether to drive this card in their
58 static int mmc_bus_match(struct device *dev, struct device_driver *drv)
64 mmc_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
66 struct mmc_card *card = mmc_dev_to_card(dev);
80 case MMC_TYPE_SD_COMBO:
88 retval = add_uevent_var(env, "MMC_TYPE=%s", type);
93 retval = add_uevent_var(env, "MMC_NAME=%s", mmc_card_name(card));
98 * Request the mmc_block device. Note: that this is a direct request
99 * for the module it carries no information as to what is inserted.
101 retval = add_uevent_var(env, "MODALIAS=mmc:block");
106 static int mmc_bus_probe(struct device *dev)
108 struct mmc_driver *drv = to_mmc_driver(dev->driver);
109 struct mmc_card *card = mmc_dev_to_card(dev);
111 return drv->probe(card);
114 static int mmc_bus_remove(struct device *dev)
116 struct mmc_driver *drv = to_mmc_driver(dev->driver);
117 struct mmc_card *card = mmc_dev_to_card(dev);
124 static int mmc_bus_suspend(struct device *dev, pm_message_t state)
126 struct mmc_driver *drv = to_mmc_driver(dev->driver);
127 struct mmc_card *card = mmc_dev_to_card(dev);
130 if (dev->driver && drv->suspend)
131 ret = drv->suspend(card, state);
135 static int mmc_bus_resume(struct device *dev)
137 struct mmc_driver *drv = to_mmc_driver(dev->driver);
138 struct mmc_card *card = mmc_dev_to_card(dev);
141 if (dev->driver && drv->resume)
142 ret = drv->resume(card);
146 #ifdef CONFIG_PM_RUNTIME
148 static int mmc_runtime_suspend(struct device *dev)
150 struct mmc_card *card = mmc_dev_to_card(dev);
152 return mmc_power_save_host(card->host);
155 static int mmc_runtime_resume(struct device *dev)
157 struct mmc_card *card = mmc_dev_to_card(dev);
159 return mmc_power_restore_host(card->host);
162 static int mmc_runtime_idle(struct device *dev)
164 return pm_runtime_suspend(dev);
167 static const struct dev_pm_ops mmc_bus_pm_ops = {
168 .runtime_suspend = mmc_runtime_suspend,
169 .runtime_resume = mmc_runtime_resume,
170 .runtime_idle = mmc_runtime_idle,
173 #define MMC_PM_OPS_PTR (&mmc_bus_pm_ops)
175 #else /* !CONFIG_PM_RUNTIME */
177 #define MMC_PM_OPS_PTR NULL
179 #endif /* !CONFIG_PM_RUNTIME */
181 static struct bus_type mmc_bus_type = {
183 .dev_attrs = mmc_dev_attrs,
184 .match = mmc_bus_match,
185 .uevent = mmc_bus_uevent,
186 .probe = mmc_bus_probe,
187 .remove = mmc_bus_remove,
188 .suspend = mmc_bus_suspend,
189 .resume = mmc_bus_resume,
190 .pm = MMC_PM_OPS_PTR,
193 int mmc_register_bus(void)
195 return bus_register(&mmc_bus_type);
198 void mmc_unregister_bus(void)
200 bus_unregister(&mmc_bus_type);
204 * mmc_register_driver - register a media driver
205 * @drv: MMC media driver
207 int mmc_register_driver(struct mmc_driver *drv)
209 drv->drv.bus = &mmc_bus_type;
210 return driver_register(&drv->drv);
213 EXPORT_SYMBOL(mmc_register_driver);
216 * mmc_unregister_driver - unregister a media driver
217 * @drv: MMC media driver
219 void mmc_unregister_driver(struct mmc_driver *drv)
221 drv->drv.bus = &mmc_bus_type;
222 driver_unregister(&drv->drv);
225 EXPORT_SYMBOL(mmc_unregister_driver);
227 static void mmc_release_card(struct device *dev)
229 struct mmc_card *card = mmc_dev_to_card(dev);
231 sdio_free_common_cis(card);
240 * Allocate and initialise a new MMC card structure.
242 struct mmc_card *mmc_alloc_card(struct mmc_host *host, struct device_type *type)
244 struct mmc_card *card;
246 card = kzalloc(sizeof(struct mmc_card), GFP_KERNEL);
248 return ERR_PTR(-ENOMEM);
252 device_initialize(&card->dev);
254 card->dev.parent = mmc_classdev(host);
255 card->dev.bus = &mmc_bus_type;
256 card->dev.release = mmc_release_card;
257 card->dev.type = type;
263 * Register a new MMC card with the driver model.
265 int mmc_add_card(struct mmc_card *card)
270 dev_set_name(&card->dev, "%s:%04x", mmc_hostname(card->host), card->rca);
272 switch (card->type) {
278 if (mmc_card_blockaddr(card)) {
279 if (mmc_card_ext_capacity(card))
288 case MMC_TYPE_SD_COMBO:
290 if (mmc_card_blockaddr(card))
298 if (mmc_host_is_spi(card->host)) {
299 pr_info("%s: new %s%s%s card on SPI\n",
300 mmc_hostname(card->host),
301 mmc_card_highspeed(card) ? "high speed " : "",
302 mmc_card_ddr_mode(card) ? "DDR " : "",
305 printk(KERN_INFO "%s: new %s%s%s card at address %04x\n",
306 mmc_hostname(card->host),
307 mmc_sd_card_uhs(card) ? "ultra high speed " :
308 (mmc_card_highspeed(card) ? "high speed " : ""),
309 mmc_card_ddr_mode(card) ? "DDR " : "",
313 #ifdef CONFIG_DEBUG_FS
314 mmc_add_card_debugfs(card);
317 ret = device_add(&card->dev);
321 mmc_card_set_present(card);
327 * Unregister a new MMC card with the driver model, and
328 * (eventually) free it.
330 void mmc_remove_card(struct mmc_card *card)
332 #ifdef CONFIG_DEBUG_FS
333 mmc_remove_card_debugfs(card);
336 if (mmc_card_present(card)) {
337 if (mmc_host_is_spi(card->host)) {
338 pr_info("%s: SPI card removed\n",
339 mmc_hostname(card->host));
341 pr_info("%s: card %04x removed\n",
342 mmc_hostname(card->host), card->rca);
344 device_del(&card->dev);
347 put_device(&card->dev);