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>
24 #define dev_to_mmc_card(d) container_of(d, struct mmc_card, dev)
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
= dev_to_mmc_card(dev
);
34 return sprintf(buf
, "MMC\n");
36 return sprintf(buf
, "SD\n");
38 return sprintf(buf
, "SDIO\n");
44 static struct device_attribute mmc_dev_attrs
[] = {
45 __ATTR(type
, S_IRUGO
, mmc_type_show
, NULL
),
50 * This currently matches any MMC driver to any MMC card - drivers
51 * themselves make the decision whether to drive this card in their
54 static int mmc_bus_match(struct device
*dev
, struct device_driver
*drv
)
60 mmc_bus_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
62 struct mmc_card
*card
= dev_to_mmc_card(dev
);
81 retval
= add_uevent_var(env
, "MMC_TYPE=%s", type
);
86 retval
= add_uevent_var(env
, "MMC_NAME=%s", mmc_card_name(card
));
91 static int mmc_bus_probe(struct device
*dev
)
93 struct mmc_driver
*drv
= to_mmc_driver(dev
->driver
);
94 struct mmc_card
*card
= dev_to_mmc_card(dev
);
96 return drv
->probe(card
);
99 static int mmc_bus_remove(struct device
*dev
)
101 struct mmc_driver
*drv
= to_mmc_driver(dev
->driver
);
102 struct mmc_card
*card
= dev_to_mmc_card(dev
);
109 static int mmc_bus_suspend(struct device
*dev
, pm_message_t state
)
111 struct mmc_driver
*drv
= to_mmc_driver(dev
->driver
);
112 struct mmc_card
*card
= dev_to_mmc_card(dev
);
115 if (dev
->driver
&& drv
->suspend
)
116 ret
= drv
->suspend(card
, state
);
120 static int mmc_bus_resume(struct device
*dev
)
122 struct mmc_driver
*drv
= to_mmc_driver(dev
->driver
);
123 struct mmc_card
*card
= dev_to_mmc_card(dev
);
126 if (dev
->driver
&& drv
->resume
)
127 ret
= drv
->resume(card
);
131 static struct bus_type mmc_bus_type
= {
133 .dev_attrs
= mmc_dev_attrs
,
134 .match
= mmc_bus_match
,
135 .uevent
= mmc_bus_uevent
,
136 .probe
= mmc_bus_probe
,
137 .remove
= mmc_bus_remove
,
138 .suspend
= mmc_bus_suspend
,
139 .resume
= mmc_bus_resume
,
142 int mmc_register_bus(void)
144 return bus_register(&mmc_bus_type
);
147 void mmc_unregister_bus(void)
149 bus_unregister(&mmc_bus_type
);
153 * mmc_register_driver - register a media driver
154 * @drv: MMC media driver
156 int mmc_register_driver(struct mmc_driver
*drv
)
158 drv
->drv
.bus
= &mmc_bus_type
;
159 return driver_register(&drv
->drv
);
162 EXPORT_SYMBOL(mmc_register_driver
);
165 * mmc_unregister_driver - unregister a media driver
166 * @drv: MMC media driver
168 void mmc_unregister_driver(struct mmc_driver
*drv
)
170 drv
->drv
.bus
= &mmc_bus_type
;
171 driver_unregister(&drv
->drv
);
174 EXPORT_SYMBOL(mmc_unregister_driver
);
176 static void mmc_release_card(struct device
*dev
)
178 struct mmc_card
*card
= dev_to_mmc_card(dev
);
180 sdio_free_common_cis(card
);
189 * Allocate and initialise a new MMC card structure.
191 struct mmc_card
*mmc_alloc_card(struct mmc_host
*host
, struct device_type
*type
)
193 struct mmc_card
*card
;
195 card
= kzalloc(sizeof(struct mmc_card
), GFP_KERNEL
);
197 return ERR_PTR(-ENOMEM
);
201 device_initialize(&card
->dev
);
203 card
->dev
.parent
= mmc_classdev(host
);
204 card
->dev
.bus
= &mmc_bus_type
;
205 card
->dev
.release
= mmc_release_card
;
206 card
->dev
.type
= type
;
212 * Register a new MMC card with the driver model.
214 int mmc_add_card(struct mmc_card
*card
)
219 dev_set_name(&card
->dev
, "%s:%04x", mmc_hostname(card
->host
), card
->rca
);
221 switch (card
->type
) {
227 if (mmc_card_blockaddr(card
))
238 if (mmc_host_is_spi(card
->host
)) {
239 printk(KERN_INFO
"%s: new %s%s card on SPI\n",
240 mmc_hostname(card
->host
),
241 mmc_card_highspeed(card
) ? "high speed " : "",
244 printk(KERN_INFO
"%s: new %s%s card at address %04x\n",
245 mmc_hostname(card
->host
),
246 mmc_card_highspeed(card
) ? "high speed " : "",
250 ret
= device_add(&card
->dev
);
254 #ifdef CONFIG_DEBUG_FS
255 mmc_add_card_debugfs(card
);
258 mmc_card_set_present(card
);
264 * Unregister a new MMC card with the driver model, and
265 * (eventually) free it.
267 void mmc_remove_card(struct mmc_card
*card
)
269 #ifdef CONFIG_DEBUG_FS
270 mmc_remove_card_debugfs(card
);
273 if (mmc_card_present(card
)) {
274 if (mmc_host_is_spi(card
->host
)) {
275 printk(KERN_INFO
"%s: SPI card removed\n",
276 mmc_hostname(card
->host
));
278 printk(KERN_INFO
"%s: card %04x removed\n",
279 mmc_hostname(card
->host
), card
->rca
);
281 device_del(&card
->dev
);
284 put_device(&card
->dev
);