mmc: whip bus uevent handler into shape
[linux-2.6/verdex.git] / drivers / mmc / core / bus.c
blob1cc11714916f99c79e5fef1919f216319bfd0f02
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, char **envp, int num_envp, char *buf,
62 int buf_size)
64 struct mmc_card *card = dev_to_mmc_card(dev);
65 const char *type;
66 int i = 0, length = 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 default:
79 type = NULL;
82 if (type) {
83 if (add_uevent_var(envp, num_envp, &i,
84 buf, buf_size, &length,
85 "MMC_TYPE=%s", type))
86 return -ENOMEM;
89 if (add_uevent_var(envp, num_envp, &i,
90 buf, buf_size, &length,
91 "MMC_NAME=%s", mmc_card_name(card)))
92 return -ENOMEM;
94 envp[i] = NULL;
96 return 0;
99 static int mmc_bus_probe(struct device *dev)
101 struct mmc_driver *drv = to_mmc_driver(dev->driver);
102 struct mmc_card *card = dev_to_mmc_card(dev);
104 return drv->probe(card);
107 static int mmc_bus_remove(struct device *dev)
109 struct mmc_driver *drv = to_mmc_driver(dev->driver);
110 struct mmc_card *card = dev_to_mmc_card(dev);
112 drv->remove(card);
114 return 0;
117 static int mmc_bus_suspend(struct device *dev, pm_message_t state)
119 struct mmc_driver *drv = to_mmc_driver(dev->driver);
120 struct mmc_card *card = dev_to_mmc_card(dev);
121 int ret = 0;
123 if (dev->driver && drv->suspend)
124 ret = drv->suspend(card, state);
125 return ret;
128 static int mmc_bus_resume(struct device *dev)
130 struct mmc_driver *drv = to_mmc_driver(dev->driver);
131 struct mmc_card *card = dev_to_mmc_card(dev);
132 int ret = 0;
134 if (dev->driver && drv->resume)
135 ret = drv->resume(card);
136 return ret;
139 static struct bus_type mmc_bus_type = {
140 .name = "mmc",
141 .dev_attrs = mmc_dev_attrs,
142 .match = mmc_bus_match,
143 .uevent = mmc_bus_uevent,
144 .probe = mmc_bus_probe,
145 .remove = mmc_bus_remove,
146 .suspend = mmc_bus_suspend,
147 .resume = mmc_bus_resume,
150 int mmc_register_bus(void)
152 return bus_register(&mmc_bus_type);
155 void mmc_unregister_bus(void)
157 bus_unregister(&mmc_bus_type);
161 * mmc_register_driver - register a media driver
162 * @drv: MMC media driver
164 int mmc_register_driver(struct mmc_driver *drv)
166 drv->drv.bus = &mmc_bus_type;
167 return driver_register(&drv->drv);
170 EXPORT_SYMBOL(mmc_register_driver);
173 * mmc_unregister_driver - unregister a media driver
174 * @drv: MMC media driver
176 void mmc_unregister_driver(struct mmc_driver *drv)
178 drv->drv.bus = &mmc_bus_type;
179 driver_unregister(&drv->drv);
182 EXPORT_SYMBOL(mmc_unregister_driver);
184 static void mmc_release_card(struct device *dev)
186 struct mmc_card *card = dev_to_mmc_card(dev);
188 sdio_free_common_cis(card);
190 kfree(card);
194 * Allocate and initialise a new MMC card structure.
196 struct mmc_card *mmc_alloc_card(struct mmc_host *host)
198 struct mmc_card *card;
200 card = kzalloc(sizeof(struct mmc_card), GFP_KERNEL);
201 if (!card)
202 return ERR_PTR(-ENOMEM);
204 card->host = host;
206 device_initialize(&card->dev);
208 card->dev.parent = mmc_classdev(host);
209 card->dev.bus = &mmc_bus_type;
210 card->dev.release = mmc_release_card;
212 return card;
216 * Register a new MMC card with the driver model.
218 int mmc_add_card(struct mmc_card *card)
220 int ret;
221 const char *type;
223 snprintf(card->dev.bus_id, sizeof(card->dev.bus_id),
224 "%s:%04x", mmc_hostname(card->host), card->rca);
226 switch (card->type) {
227 case MMC_TYPE_MMC:
228 type = "MMC";
229 break;
230 case MMC_TYPE_SD:
231 type = "SD";
232 if (mmc_card_blockaddr(card))
233 type = "SDHC";
234 break;
235 case MMC_TYPE_SDIO:
236 type = "SDIO";
237 break;
238 default:
239 type = "?";
240 break;
243 printk(KERN_INFO "%s: new %s%s card at address %04x\n",
244 mmc_hostname(card->host),
245 mmc_card_highspeed(card) ? "high speed " : "",
246 type, card->rca);
248 card->dev.uevent_suppress = 1;
250 ret = device_add(&card->dev);
251 if (ret)
252 return ret;
254 if (card->host->bus_ops->sysfs_add) {
255 ret = card->host->bus_ops->sysfs_add(card->host, card);
256 if (ret) {
257 device_del(&card->dev);
258 return ret;
262 card->dev.uevent_suppress = 0;
264 kobject_uevent(&card->dev.kobj, KOBJ_ADD);
266 mmc_card_set_present(card);
268 return 0;
272 * Unregister a new MMC card with the driver model, and
273 * (eventually) free it.
275 void mmc_remove_card(struct mmc_card *card)
277 if (mmc_card_present(card)) {
278 printk(KERN_INFO "%s: card %04x removed\n",
279 mmc_hostname(card->host), card->rca);
281 if (card->host->bus_ops->sysfs_remove)
282 card->host->bus_ops->sysfs_remove(card->host, card);
283 device_del(&card->dev);
286 put_device(&card->dev);