2 * linux/drivers/mmc/mmc_sysfs.c
4 * Copyright (C) 2003 Russell King, All Rights Reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * MMC sysfs/driver model support.
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/device.h>
16 #include <linux/mmc/card.h>
17 #include <linux/mmc/host.h>
21 #define dev_to_mmc_card(d) container_of(d, struct mmc_card, dev)
22 #define to_mmc_driver(d) container_of(d, struct mmc_driver, drv)
23 #define cls_dev_to_mmc_host(d) container_of(d, struct mmc_host, class_dev)
25 #define MMC_ATTR(name, fmt, args...) \
26 static ssize_t mmc_##name##_show (struct device *dev, struct device_attribute *attr, char *buf) \
28 struct mmc_card *card = dev_to_mmc_card(dev); \
29 return sprintf(buf, fmt, args); \
32 MMC_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
33 card->raw_cid[2], card->raw_cid[3]);
34 MMC_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
35 card->raw_csd[2], card->raw_csd[3]);
36 MMC_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
37 MMC_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
38 MMC_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
39 MMC_ATTR(manfid, "0x%06x\n", card->cid.manfid);
40 MMC_ATTR(name, "%s\n", card->cid.prod_name);
41 MMC_ATTR(oemid, "0x%04x\n", card->cid.oemid);
42 MMC_ATTR(serial, "0x%08x\n", card->cid.serial);
44 #define MMC_ATTR_RO(name) __ATTR(name, S_IRUGO, mmc_##name##_show, NULL)
46 static struct device_attribute mmc_dev_attrs[] = {
60 static void mmc_release_card(struct device *dev)
62 struct mmc_card *card = dev_to_mmc_card(dev);
68 * This currently matches any MMC driver to any MMC card - drivers
69 * themselves make the decision whether to drive this card in their
70 * probe method. However, we force "bad" cards to fail.
72 static int mmc_bus_match(struct device *dev, struct device_driver *drv)
74 struct mmc_card *card = dev_to_mmc_card(dev);
75 return !mmc_card_bad(card);
79 mmc_bus_hotplug(struct device *dev, char **envp, int num_envp, char *buf,
82 struct mmc_card *card = dev_to_mmc_card(dev);
86 #define add_env(fmt,val) \
88 int len, ret = -ENOMEM; \
91 len = snprintf(buf, buf_size, fmt, val) + 1; \
100 for (i = 0; i < 12; i++)
101 ccc[i] = card->csd.cmdclass & (1 << i) ? '1' : '0';
105 add_env("MMC_CCC=%s", ccc);
106 add_env("MMC_MANFID=%06x", card->cid.manfid);
107 add_env("MMC_NAME=%s", mmc_card_name(card));
108 add_env("MMC_OEMID=%04x", card->cid.oemid);
113 static int mmc_bus_suspend(struct device *dev, pm_message_t state)
115 struct mmc_driver *drv = to_mmc_driver(dev->driver);
116 struct mmc_card *card = dev_to_mmc_card(dev);
119 if (dev->driver && drv->suspend)
120 ret = drv->suspend(card, state);
124 static int mmc_bus_resume(struct device *dev)
126 struct mmc_driver *drv = to_mmc_driver(dev->driver);
127 struct mmc_card *card = dev_to_mmc_card(dev);
130 if (dev->driver && drv->resume)
131 ret = drv->resume(card);
135 static struct bus_type mmc_bus_type = {
137 .dev_attrs = mmc_dev_attrs,
138 .match = mmc_bus_match,
139 .hotplug = mmc_bus_hotplug,
140 .suspend = mmc_bus_suspend,
141 .resume = mmc_bus_resume,
145 static int mmc_drv_probe(struct device *dev)
147 struct mmc_driver *drv = to_mmc_driver(dev->driver);
148 struct mmc_card *card = dev_to_mmc_card(dev);
150 return drv->probe(card);
153 static int mmc_drv_remove(struct device *dev)
155 struct mmc_driver *drv = to_mmc_driver(dev->driver);
156 struct mmc_card *card = dev_to_mmc_card(dev);
165 * mmc_register_driver - register a media driver
166 * @drv: MMC media driver
168 int mmc_register_driver(struct mmc_driver *drv)
170 drv->drv.bus = &mmc_bus_type;
171 drv->drv.probe = mmc_drv_probe;
172 drv->drv.remove = mmc_drv_remove;
173 return driver_register(&drv->drv);
176 EXPORT_SYMBOL(mmc_register_driver);
179 * mmc_unregister_driver - unregister a media driver
180 * @drv: MMC media driver
182 void mmc_unregister_driver(struct mmc_driver *drv)
184 drv->drv.bus = &mmc_bus_type;
185 driver_unregister(&drv->drv);
188 EXPORT_SYMBOL(mmc_unregister_driver);
192 * Internal function. Initialise a MMC card structure.
194 void mmc_init_card(struct mmc_card *card, struct mmc_host *host)
196 memset(card, 0, sizeof(struct mmc_card));
198 device_initialize(&card->dev);
199 card->dev.parent = card->host->dev;
200 card->dev.bus = &mmc_bus_type;
201 card->dev.release = mmc_release_card;
205 * Internal function. Register a new MMC card with the driver model.
207 int mmc_register_card(struct mmc_card *card)
209 snprintf(card->dev.bus_id, sizeof(card->dev.bus_id),
210 "%s:%04x", mmc_hostname(card->host), card->rca);
212 return device_add(&card->dev);
216 * Internal function. Unregister a new MMC card with the
217 * driver model, and (eventually) free it.
219 void mmc_remove_card(struct mmc_card *card)
221 if (mmc_card_present(card))
222 device_del(&card->dev);
224 put_device(&card->dev);
228 static void mmc_host_classdev_release(struct class_device *dev)
230 struct mmc_host *host = cls_dev_to_mmc_host(dev);
234 static struct class mmc_host_class = {
236 .release = mmc_host_classdev_release,
240 * Internal function. Allocate a new MMC host.
242 struct mmc_host *mmc_alloc_host_sysfs(int extra, struct device *dev)
244 struct mmc_host *host;
246 host = kmalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL);
248 memset(host, 0, sizeof(struct mmc_host) + extra);
251 host->class_dev.dev = host->dev;
252 host->class_dev.class = &mmc_host_class;
253 class_device_initialize(&host->class_dev);
260 * Internal function. Register a new MMC host with the MMC class.
262 int mmc_add_host_sysfs(struct mmc_host *host)
264 static unsigned int host_num;
266 snprintf(host->class_dev.class_id, BUS_ID_SIZE,
267 "mmc%d", host_num++);
269 return class_device_add(&host->class_dev);
273 * Internal function. Unregister a MMC host with the MMC class.
275 void mmc_remove_host_sysfs(struct mmc_host *host)
277 class_device_del(&host->class_dev);
281 * Internal function. Free a MMC host.
283 void mmc_free_host_sysfs(struct mmc_host *host)
285 class_device_put(&host->class_dev);
289 static int __init mmc_init(void)
291 int ret = bus_register(&mmc_bus_type);
293 ret = class_register(&mmc_host_class);
295 bus_unregister(&mmc_bus_type);
300 static void __exit mmc_exit(void)
302 class_unregister(&mmc_host_class);
303 bus_unregister(&mmc_bus_type);
306 module_init(mmc_init);
307 module_exit(mmc_exit);