igb: remove unneeded declaration shadowing earlier one
[linux-2.6/libata-dev.git] / drivers / mmc / core / bus.c
blobb0c22cad9423f2878c8742f48801f422967c32df
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>
17 #include <linux/mmc/card.h>
18 #include <linux/mmc/host.h>
20 #include "sysfs.h"
21 #include "core.h"
22 #include "sdio_cis.h"
23 #include "bus.h"
25 #define dev_to_mmc_card(d) container_of(d, struct mmc_card, dev)
26 #define to_mmc_driver(d) container_of(d, struct mmc_driver, drv)
28 static ssize_t mmc_type_show(struct device *dev,
29 struct device_attribute *attr, char *buf)
31 struct mmc_card *card = dev_to_mmc_card(dev);
33 switch (card->type) {
34 case MMC_TYPE_MMC:
35 return sprintf(buf, "MMC\n");
36 case MMC_TYPE_SD:
37 return sprintf(buf, "SD\n");
38 case MMC_TYPE_SDIO:
39 return sprintf(buf, "SDIO\n");
40 default:
41 return -EFAULT;
45 static struct device_attribute mmc_dev_attrs[] = {
46 MMC_ATTR_RO(type),
47 __ATTR_NULL,
51 * This currently matches any MMC driver to any MMC card - drivers
52 * themselves make the decision whether to drive this card in their
53 * probe method.
55 static int mmc_bus_match(struct device *dev, struct device_driver *drv)
57 return 1;
60 static int
61 mmc_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
63 struct mmc_card *card = dev_to_mmc_card(dev);
64 const char *type;
65 int retval = 0;
67 switch (card->type) {
68 case MMC_TYPE_MMC:
69 type = "MMC";
70 break;
71 case MMC_TYPE_SD:
72 type = "SD";
73 break;
74 case MMC_TYPE_SDIO:
75 type = "SDIO";
76 break;
77 default:
78 type = NULL;
81 if (type) {
82 retval = add_uevent_var(env, "MMC_TYPE=%s", type);
83 if (retval)
84 return retval;
87 retval = add_uevent_var(env, "MMC_NAME=%s", mmc_card_name(card));
89 return retval;
92 static int mmc_bus_probe(struct device *dev)
94 struct mmc_driver *drv = to_mmc_driver(dev->driver);
95 struct mmc_card *card = dev_to_mmc_card(dev);
97 return drv->probe(card);
100 static int mmc_bus_remove(struct device *dev)
102 struct mmc_driver *drv = to_mmc_driver(dev->driver);
103 struct mmc_card *card = dev_to_mmc_card(dev);
105 drv->remove(card);
107 return 0;
110 static int mmc_bus_suspend(struct device *dev, pm_message_t state)
112 struct mmc_driver *drv = to_mmc_driver(dev->driver);
113 struct mmc_card *card = dev_to_mmc_card(dev);
114 int ret = 0;
116 if (dev->driver && drv->suspend)
117 ret = drv->suspend(card, state);
118 return ret;
121 static int mmc_bus_resume(struct device *dev)
123 struct mmc_driver *drv = to_mmc_driver(dev->driver);
124 struct mmc_card *card = dev_to_mmc_card(dev);
125 int ret = 0;
127 if (dev->driver && drv->resume)
128 ret = drv->resume(card);
129 return ret;
132 static struct bus_type mmc_bus_type = {
133 .name = "mmc",
134 .dev_attrs = mmc_dev_attrs,
135 .match = mmc_bus_match,
136 .uevent = mmc_bus_uevent,
137 .probe = mmc_bus_probe,
138 .remove = mmc_bus_remove,
139 .suspend = mmc_bus_suspend,
140 .resume = mmc_bus_resume,
143 int mmc_register_bus(void)
145 return bus_register(&mmc_bus_type);
148 void mmc_unregister_bus(void)
150 bus_unregister(&mmc_bus_type);
154 * mmc_register_driver - register a media driver
155 * @drv: MMC media driver
157 int mmc_register_driver(struct mmc_driver *drv)
159 drv->drv.bus = &mmc_bus_type;
160 return driver_register(&drv->drv);
163 EXPORT_SYMBOL(mmc_register_driver);
166 * mmc_unregister_driver - unregister a media driver
167 * @drv: MMC media driver
169 void mmc_unregister_driver(struct mmc_driver *drv)
171 drv->drv.bus = &mmc_bus_type;
172 driver_unregister(&drv->drv);
175 EXPORT_SYMBOL(mmc_unregister_driver);
177 static void mmc_release_card(struct device *dev)
179 struct mmc_card *card = dev_to_mmc_card(dev);
181 sdio_free_common_cis(card);
183 if (card->info)
184 kfree(card->info);
186 kfree(card);
190 * Allocate and initialise a new MMC card structure.
192 struct mmc_card *mmc_alloc_card(struct mmc_host *host)
194 struct mmc_card *card;
196 card = kzalloc(sizeof(struct mmc_card), GFP_KERNEL);
197 if (!card)
198 return ERR_PTR(-ENOMEM);
200 card->host = host;
202 device_initialize(&card->dev);
204 card->dev.parent = mmc_classdev(host);
205 card->dev.bus = &mmc_bus_type;
206 card->dev.release = mmc_release_card;
208 return card;
212 * Register a new MMC card with the driver model.
214 int mmc_add_card(struct mmc_card *card)
216 int ret;
217 const char *type;
219 snprintf(card->dev.bus_id, sizeof(card->dev.bus_id),
220 "%s:%04x", mmc_hostname(card->host), card->rca);
222 switch (card->type) {
223 case MMC_TYPE_MMC:
224 type = "MMC";
225 break;
226 case MMC_TYPE_SD:
227 type = "SD";
228 if (mmc_card_blockaddr(card))
229 type = "SDHC";
230 break;
231 case MMC_TYPE_SDIO:
232 type = "SDIO";
233 break;
234 default:
235 type = "?";
236 break;
239 if (mmc_host_is_spi(card->host)) {
240 printk(KERN_INFO "%s: new %s%s card on SPI\n",
241 mmc_hostname(card->host),
242 mmc_card_highspeed(card) ? "high speed " : "",
243 type);
244 } else {
245 printk(KERN_INFO "%s: new %s%s card at address %04x\n",
246 mmc_hostname(card->host),
247 mmc_card_highspeed(card) ? "high speed " : "",
248 type, card->rca);
251 card->dev.uevent_suppress = 1;
253 ret = device_add(&card->dev);
254 if (ret)
255 return ret;
257 if (card->host->bus_ops->sysfs_add) {
258 ret = card->host->bus_ops->sysfs_add(card->host, card);
259 if (ret) {
260 device_del(&card->dev);
261 return ret;
265 card->dev.uevent_suppress = 0;
267 kobject_uevent(&card->dev.kobj, KOBJ_ADD);
269 mmc_card_set_present(card);
271 return 0;
275 * Unregister a new MMC card with the driver model, and
276 * (eventually) free it.
278 void mmc_remove_card(struct mmc_card *card)
280 if (mmc_card_present(card)) {
281 if (mmc_host_is_spi(card->host)) {
282 printk(KERN_INFO "%s: SPI card removed\n",
283 mmc_hostname(card->host));
284 } else {
285 printk(KERN_INFO "%s: card %04x removed\n",
286 mmc_hostname(card->host), card->rca);
289 if (card->host->bus_ops->sysfs_remove)
290 card->host->bus_ops->sysfs_remove(card->host, card);
291 device_del(&card->dev);
294 put_device(&card->dev);