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");
42 static struct device_attribute mmc_dev_attrs
[] = {
48 * This currently matches any MMC driver to any MMC card - drivers
49 * themselves make the decision whether to drive this card in their
52 static int mmc_bus_match(struct device
*dev
, struct device_driver
*drv
)
58 mmc_bus_uevent(struct device
*dev
, char **envp
, int num_envp
, char *buf
,
61 struct mmc_card
*card
= dev_to_mmc_card(dev
);
62 int retval
= 0, i
= 0, length
= 0;
64 #define add_env(fmt,val) do { \
65 retval = add_uevent_var(envp, num_envp, &i, \
66 buf, buf_size, &length, \
74 add_env("MMC_TYPE=%s", "MMC");
77 add_env("MMC_TYPE=%s", "SD");
81 add_env("MMC_NAME=%s", mmc_card_name(card
));
90 static int mmc_bus_probe(struct device
*dev
)
92 struct mmc_driver
*drv
= to_mmc_driver(dev
->driver
);
93 struct mmc_card
*card
= dev_to_mmc_card(dev
);
95 return drv
->probe(card
);
98 static int mmc_bus_remove(struct device
*dev
)
100 struct mmc_driver
*drv
= to_mmc_driver(dev
->driver
);
101 struct mmc_card
*card
= dev_to_mmc_card(dev
);
108 static int mmc_bus_suspend(struct device
*dev
, pm_message_t state
)
110 struct mmc_driver
*drv
= to_mmc_driver(dev
->driver
);
111 struct mmc_card
*card
= dev_to_mmc_card(dev
);
114 if (dev
->driver
&& drv
->suspend
)
115 ret
= drv
->suspend(card
, state
);
119 static int mmc_bus_resume(struct device
*dev
)
121 struct mmc_driver
*drv
= to_mmc_driver(dev
->driver
);
122 struct mmc_card
*card
= dev_to_mmc_card(dev
);
125 if (dev
->driver
&& drv
->resume
)
126 ret
= drv
->resume(card
);
130 static struct bus_type mmc_bus_type
= {
132 .dev_attrs
= mmc_dev_attrs
,
133 .match
= mmc_bus_match
,
134 .uevent
= mmc_bus_uevent
,
135 .probe
= mmc_bus_probe
,
136 .remove
= mmc_bus_remove
,
137 .suspend
= mmc_bus_suspend
,
138 .resume
= mmc_bus_resume
,
141 int mmc_register_bus(void)
143 return bus_register(&mmc_bus_type
);
146 void mmc_unregister_bus(void)
148 bus_unregister(&mmc_bus_type
);
152 * mmc_register_driver - register a media driver
153 * @drv: MMC media driver
155 int mmc_register_driver(struct mmc_driver
*drv
)
157 drv
->drv
.bus
= &mmc_bus_type
;
158 return driver_register(&drv
->drv
);
161 EXPORT_SYMBOL(mmc_register_driver
);
164 * mmc_unregister_driver - unregister a media driver
165 * @drv: MMC media driver
167 void mmc_unregister_driver(struct mmc_driver
*drv
)
169 drv
->drv
.bus
= &mmc_bus_type
;
170 driver_unregister(&drv
->drv
);
173 EXPORT_SYMBOL(mmc_unregister_driver
);
175 static void mmc_release_card(struct device
*dev
)
177 struct mmc_card
*card
= dev_to_mmc_card(dev
);
183 * Allocate and initialise a new MMC card structure.
185 struct mmc_card
*mmc_alloc_card(struct mmc_host
*host
)
187 struct mmc_card
*card
;
189 card
= kzalloc(sizeof(struct mmc_card
), GFP_KERNEL
);
191 return ERR_PTR(-ENOMEM
);
195 device_initialize(&card
->dev
);
197 card
->dev
.parent
= mmc_classdev(host
);
198 card
->dev
.bus
= &mmc_bus_type
;
199 card
->dev
.release
= mmc_release_card
;
205 * Register a new MMC card with the driver model.
207 int mmc_add_card(struct mmc_card
*card
)
212 snprintf(card
->dev
.bus_id
, sizeof(card
->dev
.bus_id
),
213 "%s:%04x", mmc_hostname(card
->host
), card
->rca
);
215 switch (card
->type
) {
221 if (mmc_card_blockaddr(card
))
229 printk(KERN_INFO
"%s: new %s%s card at address %04x\n",
230 mmc_hostname(card
->host
),
231 mmc_card_highspeed(card
) ? "high speed " : "",
234 card
->dev
.uevent_suppress
= 1;
236 ret
= device_add(&card
->dev
);
240 if (card
->host
->bus_ops
->sysfs_add
) {
241 ret
= card
->host
->bus_ops
->sysfs_add(card
->host
, card
);
243 device_del(&card
->dev
);
248 card
->dev
.uevent_suppress
= 0;
250 kobject_uevent(&card
->dev
.kobj
, KOBJ_ADD
);
252 mmc_card_set_present(card
);
258 * Unregister a new MMC card with the driver model, and
259 * (eventually) free it.
261 void mmc_remove_card(struct mmc_card
*card
)
263 if (mmc_card_present(card
)) {
264 printk(KERN_INFO
"%s: card %04x removed\n",
265 mmc_hostname(card
->host
), card
->rca
);
267 if (card
->host
->bus_ops
->sysfs_remove
)
268 card
->host
->bus_ops
->sysfs_remove(card
->host
, card
);
269 device_del(&card
->dev
);
272 put_device(&card
->dev
);