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)
24 static void mmc_release_card(struct device *dev)
26 struct mmc_card *card = dev_to_mmc_card(dev);
32 * This currently matches any MMC driver to any MMC card - drivers
33 * themselves make the decision whether to drive this card in their
34 * probe method. However, we force "bad" cards to fail.
36 static int mmc_bus_match(struct device *dev, struct device_driver *drv)
38 struct mmc_card *card = dev_to_mmc_card(dev);
39 return !mmc_card_bad(card);
43 mmc_bus_hotplug(struct device *dev, char **envp, int num_envp, char *buf,
46 struct mmc_card *card = dev_to_mmc_card(dev);
50 #define add_env(fmt,val) \
52 int len, ret = -ENOMEM; \
55 len = snprintf(buf, buf_size, fmt, val) + 1; \
64 for (i = 0; i < 12; i++)
65 ccc[i] = card->csd.cmdclass & (1 << i) ? '1' : '0';
69 add_env("MMC_CCC=%s", ccc);
70 add_env("MMC_MANFID=%06x", card->cid.manfid);
71 add_env("MMC_NAME=%s", mmc_card_name(card));
72 add_env("MMC_OEMID=%04x", card->cid.oemid);
77 static int mmc_bus_suspend(struct device *dev, u32 state)
79 struct mmc_driver *drv = to_mmc_driver(dev->driver);
80 struct mmc_card *card = dev_to_mmc_card(dev);
83 if (dev->driver && drv->suspend)
84 ret = drv->suspend(card, state);
88 static int mmc_bus_resume(struct device *dev)
90 struct mmc_driver *drv = to_mmc_driver(dev->driver);
91 struct mmc_card *card = dev_to_mmc_card(dev);
94 if (dev->driver && drv->resume)
95 ret = drv->resume(card);
99 static struct bus_type mmc_bus_type = {
101 .match = mmc_bus_match,
102 .hotplug = mmc_bus_hotplug,
103 .suspend = mmc_bus_suspend,
104 .resume = mmc_bus_resume,
108 static int mmc_drv_probe(struct device *dev)
110 struct mmc_driver *drv = to_mmc_driver(dev->driver);
111 struct mmc_card *card = dev_to_mmc_card(dev);
113 return drv->probe(card);
116 static int mmc_drv_remove(struct device *dev)
118 struct mmc_driver *drv = to_mmc_driver(dev->driver);
119 struct mmc_card *card = dev_to_mmc_card(dev);
128 * mmc_register_driver - register a media driver
129 * @drv: MMC media driver
131 int mmc_register_driver(struct mmc_driver *drv)
133 drv->drv.bus = &mmc_bus_type;
134 drv->drv.probe = mmc_drv_probe;
135 drv->drv.remove = mmc_drv_remove;
136 return driver_register(&drv->drv);
139 EXPORT_SYMBOL(mmc_register_driver);
142 * mmc_unregister_driver - unregister a media driver
143 * @drv: MMC media driver
145 void mmc_unregister_driver(struct mmc_driver *drv)
147 drv->drv.bus = &mmc_bus_type;
148 driver_unregister(&drv->drv);
151 EXPORT_SYMBOL(mmc_unregister_driver);
154 #define MMC_ATTR(name, fmt, args...) \
155 static ssize_t mmc_dev_show_##name (struct device *dev, char *buf) \
157 struct mmc_card *card = dev_to_mmc_card(dev); \
158 return sprintf(buf, fmt, args); \
160 static DEVICE_ATTR(name, S_IRUGO, mmc_dev_show_##name, NULL)
162 MMC_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
163 card->raw_cid[2], card->raw_cid[3]);
164 MMC_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
165 card->raw_csd[2], card->raw_csd[3]);
166 MMC_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
167 MMC_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
168 MMC_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
169 MMC_ATTR(manfid, "0x%06x\n", card->cid.manfid);
170 MMC_ATTR(name, "%s\n", card->cid.prod_name);
171 MMC_ATTR(oemid, "0x%04x\n", card->cid.oemid);
172 MMC_ATTR(serial, "0x%08x\n", card->cid.serial);
174 static struct device_attribute *mmc_dev_attributes[] = {
187 * Internal function. Initialise a MMC card structure.
189 void mmc_init_card(struct mmc_card *card, struct mmc_host *host)
191 memset(card, 0, sizeof(struct mmc_card));
193 device_initialize(&card->dev);
194 card->dev.parent = card->host->dev;
195 card->dev.bus = &mmc_bus_type;
196 card->dev.release = mmc_release_card;
200 * Internal function. Register a new MMC card with the driver model.
202 int mmc_register_card(struct mmc_card *card)
206 snprintf(card->dev.bus_id, sizeof(card->dev.bus_id),
207 "%s:%04x", card->host->host_name, card->rca);
209 ret = device_add(&card->dev);
211 for (i = 0; i < ARRAY_SIZE(mmc_dev_attributes); i++)
212 device_create_file(&card->dev, mmc_dev_attributes[i]);
218 * Internal function. Unregister a new MMC card with the
219 * driver model, and (eventually) free it.
221 void mmc_remove_card(struct mmc_card *card)
223 if (mmc_card_present(card))
224 device_del(&card->dev);
226 put_device(&card->dev);
230 static int __init mmc_init(void)
232 return bus_register(&mmc_bus_type);
235 static void __exit mmc_exit(void)
237 bus_unregister(&mmc_bus_type);
240 module_init(mmc_init);
241 module_exit(mmc_exit);