mmc: rename dev_to_mmc_card() to mmc_dev_to_card()
[linux-2.6/cjktty.git] / drivers / mmc / core / bus.c
blobe70bd6641ceead22cccf094e4344f5ef45415be6
1 /*
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/device.h>
15 #include <linux/err.h>
16 #include <linux/slab.h>
18 #include <linux/mmc/card.h>
19 #include <linux/mmc/host.h>
21 #include "core.h"
22 #include "sdio_cis.h"
23 #include "bus.h"
25 #define to_mmc_driver(d) container_of(d, struct mmc_driver, drv)
27 static ssize_t mmc_type_show(struct device *dev,
28 struct device_attribute *attr, char *buf)
30 struct mmc_card *card = mmc_dev_to_card(dev);
32 switch (card->type) {
33 case MMC_TYPE_MMC:
34 return sprintf(buf, "MMC\n");
35 case MMC_TYPE_SD:
36 return sprintf(buf, "SD\n");
37 case MMC_TYPE_SDIO:
38 return sprintf(buf, "SDIO\n");
39 case MMC_TYPE_SD_COMBO:
40 return sprintf(buf, "SDcombo\n");
41 default:
42 return -EFAULT;
46 static struct device_attribute mmc_dev_attrs[] = {
47 __ATTR(type, S_IRUGO, mmc_type_show, NULL),
48 __ATTR_NULL,
52 * This currently matches any MMC driver to any MMC card - drivers
53 * themselves make the decision whether to drive this card in their
54 * probe method.
56 static int mmc_bus_match(struct device *dev, struct device_driver *drv)
58 return 1;
61 static int
62 mmc_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
64 struct mmc_card *card = mmc_dev_to_card(dev);
65 const char *type;
66 int retval = 0;
68 switch (card->type) {
69 case MMC_TYPE_MMC:
70 type = "MMC";
71 break;
72 case MMC_TYPE_SD:
73 type = "SD";
74 break;
75 case MMC_TYPE_SDIO:
76 type = "SDIO";
77 break;
78 case MMC_TYPE_SD_COMBO:
79 type = "SDcombo";
80 break;
81 default:
82 type = NULL;
85 if (type) {
86 retval = add_uevent_var(env, "MMC_TYPE=%s", type);
87 if (retval)
88 return retval;
91 retval = add_uevent_var(env, "MMC_NAME=%s", mmc_card_name(card));
92 if (retval)
93 return retval;
96 * Request the mmc_block device. Note: that this is a direct request
97 * for the module it carries no information as to what is inserted.
99 retval = add_uevent_var(env, "MODALIAS=mmc:block");
101 return retval;
104 static int mmc_bus_probe(struct device *dev)
106 struct mmc_driver *drv = to_mmc_driver(dev->driver);
107 struct mmc_card *card = mmc_dev_to_card(dev);
109 return drv->probe(card);
112 static int mmc_bus_remove(struct device *dev)
114 struct mmc_driver *drv = to_mmc_driver(dev->driver);
115 struct mmc_card *card = mmc_dev_to_card(dev);
117 drv->remove(card);
119 return 0;
122 static int mmc_bus_suspend(struct device *dev, pm_message_t state)
124 struct mmc_driver *drv = to_mmc_driver(dev->driver);
125 struct mmc_card *card = mmc_dev_to_card(dev);
126 int ret = 0;
128 if (dev->driver && drv->suspend)
129 ret = drv->suspend(card, state);
130 return ret;
133 static int mmc_bus_resume(struct device *dev)
135 struct mmc_driver *drv = to_mmc_driver(dev->driver);
136 struct mmc_card *card = mmc_dev_to_card(dev);
137 int ret = 0;
139 if (dev->driver && drv->resume)
140 ret = drv->resume(card);
141 return ret;
144 static struct bus_type mmc_bus_type = {
145 .name = "mmc",
146 .dev_attrs = mmc_dev_attrs,
147 .match = mmc_bus_match,
148 .uevent = mmc_bus_uevent,
149 .probe = mmc_bus_probe,
150 .remove = mmc_bus_remove,
151 .suspend = mmc_bus_suspend,
152 .resume = mmc_bus_resume,
155 int mmc_register_bus(void)
157 return bus_register(&mmc_bus_type);
160 void mmc_unregister_bus(void)
162 bus_unregister(&mmc_bus_type);
166 * mmc_register_driver - register a media driver
167 * @drv: MMC media driver
169 int mmc_register_driver(struct mmc_driver *drv)
171 drv->drv.bus = &mmc_bus_type;
172 return driver_register(&drv->drv);
175 EXPORT_SYMBOL(mmc_register_driver);
178 * mmc_unregister_driver - unregister a media driver
179 * @drv: MMC media driver
181 void mmc_unregister_driver(struct mmc_driver *drv)
183 drv->drv.bus = &mmc_bus_type;
184 driver_unregister(&drv->drv);
187 EXPORT_SYMBOL(mmc_unregister_driver);
189 static void mmc_release_card(struct device *dev)
191 struct mmc_card *card = mmc_dev_to_card(dev);
193 sdio_free_common_cis(card);
195 if (card->info)
196 kfree(card->info);
198 kfree(card);
202 * Allocate and initialise a new MMC card structure.
204 struct mmc_card *mmc_alloc_card(struct mmc_host *host, struct device_type *type)
206 struct mmc_card *card;
208 card = kzalloc(sizeof(struct mmc_card), GFP_KERNEL);
209 if (!card)
210 return ERR_PTR(-ENOMEM);
212 card->host = host;
214 device_initialize(&card->dev);
216 card->dev.parent = mmc_classdev(host);
217 card->dev.bus = &mmc_bus_type;
218 card->dev.release = mmc_release_card;
219 card->dev.type = type;
221 return card;
225 * Register a new MMC card with the driver model.
227 int mmc_add_card(struct mmc_card *card)
229 int ret;
230 const char *type;
232 dev_set_name(&card->dev, "%s:%04x", mmc_hostname(card->host), card->rca);
234 switch (card->type) {
235 case MMC_TYPE_MMC:
236 type = "MMC";
237 break;
238 case MMC_TYPE_SD:
239 type = "SD";
240 if (mmc_card_blockaddr(card))
241 type = "SDHC";
242 break;
243 case MMC_TYPE_SDIO:
244 type = "SDIO";
245 break;
246 case MMC_TYPE_SD_COMBO:
247 type = "SD-combo";
248 if (mmc_card_blockaddr(card))
249 type = "SDHC-combo";
250 default:
251 type = "?";
252 break;
255 if (mmc_host_is_spi(card->host)) {
256 printk(KERN_INFO "%s: new %s%s card on SPI\n",
257 mmc_hostname(card->host),
258 mmc_card_highspeed(card) ? "high speed " : "",
259 type);
260 } else {
261 printk(KERN_INFO "%s: new %s%s card at address %04x\n",
262 mmc_hostname(card->host),
263 mmc_card_highspeed(card) ? "high speed " : "",
264 type, card->rca);
267 ret = device_add(&card->dev);
268 if (ret)
269 return ret;
271 #ifdef CONFIG_DEBUG_FS
272 mmc_add_card_debugfs(card);
273 #endif
275 mmc_card_set_present(card);
277 return 0;
281 * Unregister a new MMC card with the driver model, and
282 * (eventually) free it.
284 void mmc_remove_card(struct mmc_card *card)
286 #ifdef CONFIG_DEBUG_FS
287 mmc_remove_card_debugfs(card);
288 #endif
290 if (mmc_card_present(card)) {
291 if (mmc_host_is_spi(card->host)) {
292 printk(KERN_INFO "%s: SPI card removed\n",
293 mmc_hostname(card->host));
294 } else {
295 printk(KERN_INFO "%s: card %04x removed\n",
296 mmc_hostname(card->host), card->rca);
298 device_del(&card->dev);
301 put_device(&card->dev);