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>
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
);
34 return sprintf(buf
, "MMC\n");
36 return sprintf(buf
, "SD\n");
38 return sprintf(buf
, "SDIO\n");
39 case MMC_TYPE_SD_COMBO
:
40 return sprintf(buf
, "SDcombo\n");
46 static struct device_attribute mmc_dev_attrs
[] = {
47 __ATTR(type
, S_IRUGO
, mmc_type_show
, 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
56 static int mmc_bus_match(struct device
*dev
, struct device_driver
*drv
)
62 mmc_bus_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
64 struct mmc_card
*card
= mmc_dev_to_card(dev
);
78 case MMC_TYPE_SD_COMBO
:
86 retval
= add_uevent_var(env
, "MMC_TYPE=%s", type
);
91 retval
= add_uevent_var(env
, "MMC_NAME=%s", mmc_card_name(card
));
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");
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
);
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
);
128 if (dev
->driver
&& drv
->suspend
)
129 ret
= drv
->suspend(card
, state
);
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
);
139 if (dev
->driver
&& drv
->resume
)
140 ret
= drv
->resume(card
);
144 static struct bus_type mmc_bus_type
= {
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
);
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
);
210 return ERR_PTR(-ENOMEM
);
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
;
225 * Register a new MMC card with the driver model.
227 int mmc_add_card(struct mmc_card
*card
)
232 dev_set_name(&card
->dev
, "%s:%04x", mmc_hostname(card
->host
), card
->rca
);
234 switch (card
->type
) {
240 if (mmc_card_blockaddr(card
))
246 case MMC_TYPE_SD_COMBO
:
248 if (mmc_card_blockaddr(card
))
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 " : "",
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 " : "",
267 ret
= device_add(&card
->dev
);
271 #ifdef CONFIG_DEBUG_FS
272 mmc_add_card_debugfs(card
);
275 mmc_card_set_present(card
);
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
);
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
));
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
);