MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / mmc.old / mmc_sysfs.c
blob5c2b8ec7e2b59f27cead6fb71087c842d78ac2ae
1 /*
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>
15 #include <linux/idr.h>
17 #include <linux/mmc/card.h>
18 #include <linux/mmc/host.h>
20 #include "mmc.h"
22 #define dev_to_mmc_card(d) container_of(d, struct mmc_card, dev)
23 #define to_mmc_driver(d) container_of(d, struct mmc_driver, drv)
24 #define cls_dev_to_mmc_host(d) container_of(d, struct mmc_host, class_dev)
26 #define MMC_ATTR(name, fmt, args...) \
27 static ssize_t mmc_##name##_show (struct device *dev, struct device_attribute *attr, char *buf) \
28 { \
29 struct mmc_card *card = dev_to_mmc_card(dev); \
30 return sprintf(buf, fmt, args); \
33 MMC_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
34 card->raw_cid[2], card->raw_cid[3]);
35 MMC_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
36 card->raw_csd[2], card->raw_csd[3]);
37 MMC_ATTR(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]);
38 MMC_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
39 MMC_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
40 MMC_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
41 MMC_ATTR(manfid, "0x%06x\n", card->cid.manfid);
42 MMC_ATTR(name, "%s\n", card->cid.prod_name);
43 MMC_ATTR(oemid, "0x%04x\n", card->cid.oemid);
44 MMC_ATTR(serial, "0x%08x\n", card->cid.serial);
46 #define MMC_ATTR_RO(name) __ATTR(name, S_IRUGO, mmc_##name##_show, NULL)
48 static struct device_attribute mmc_dev_attrs[] = {
49 MMC_ATTR_RO(cid),
50 MMC_ATTR_RO(csd),
51 MMC_ATTR_RO(date),
52 MMC_ATTR_RO(fwrev),
53 MMC_ATTR_RO(hwrev),
54 MMC_ATTR_RO(manfid),
55 MMC_ATTR_RO(name),
56 MMC_ATTR_RO(oemid),
57 MMC_ATTR_RO(serial),
58 __ATTR_NULL
61 static struct device_attribute mmc_dev_attr_scr = MMC_ATTR_RO(scr);
64 static void mmc_release_card(struct device *dev)
66 struct mmc_card *card = dev_to_mmc_card(dev);
68 kfree(card);
72 * This currently matches any MMC driver to any MMC card - drivers
73 * themselves make the decision whether to drive this card in their
74 * probe method. However, we force "bad" cards to fail.
76 static int mmc_bus_match(struct device *dev, struct device_driver *drv)
78 struct mmc_card *card = dev_to_mmc_card(dev);
79 return !mmc_card_bad(card);
82 static int
83 mmc_bus_hotplug(struct device *dev, char **envp, int num_envp, char *buf,
84 int buf_size)
86 struct mmc_card *card = dev_to_mmc_card(dev);
87 char ccc[13];
88 int i = 0;
90 #define add_env(fmt,val) \
91 ({ \
92 int len, ret = -ENOMEM; \
93 if (i < num_envp) { \
94 envp[i++] = buf; \
95 len = snprintf(buf, buf_size, fmt, val) + 1; \
96 buf_size -= len; \
97 buf += len; \
98 if (buf_size >= 0) \
99 ret = 0; \
101 ret; \
104 for (i = 0; i < 12; i++)
105 ccc[i] = card->csd.cmdclass & (1 << i) ? '1' : '0';
106 ccc[12] = '\0';
108 i = 0;
109 add_env("MMC_CCC=%s", ccc);
110 add_env("MMC_MANFID=%06x", card->cid.manfid);
111 add_env("MMC_NAME=%s", mmc_card_name(card));
112 add_env("MMC_OEMID=%04x", card->cid.oemid);
114 return 0;
117 static int mmc_bus_suspend(struct device *dev, pm_message_t state)
119 struct mmc_driver *drv = to_mmc_driver(dev->driver);
120 struct mmc_card *card = dev_to_mmc_card(dev);
121 int ret = 0;
123 if (dev->driver && drv->suspend)
124 ret = drv->suspend(card, state);
125 return ret;
128 static int mmc_bus_resume(struct device *dev)
130 struct mmc_driver *drv = to_mmc_driver(dev->driver);
131 struct mmc_card *card = dev_to_mmc_card(dev);
132 int ret = 0;
134 if (dev->driver && drv->resume)
135 ret = drv->resume(card);
136 return ret;
139 static struct bus_type mmc_bus_type = {
140 .name = "mmc",
141 .dev_attrs = mmc_dev_attrs,
142 .match = mmc_bus_match,
143 .hotplug = mmc_bus_hotplug,
144 .suspend = mmc_bus_suspend,
145 .resume = mmc_bus_resume,
149 static int mmc_drv_probe(struct device *dev)
151 struct mmc_driver *drv = to_mmc_driver(dev->driver);
152 struct mmc_card *card = dev_to_mmc_card(dev);
154 return drv->probe(card);
157 static int mmc_drv_remove(struct device *dev)
159 struct mmc_driver *drv = to_mmc_driver(dev->driver);
160 struct mmc_card *card = dev_to_mmc_card(dev);
162 drv->remove(card);
164 return 0;
169 * mmc_register_driver - register a media driver
170 * @drv: MMC media driver
172 int mmc_register_driver(struct mmc_driver *drv)
174 drv->drv.bus = &mmc_bus_type;
175 drv->drv.probe = mmc_drv_probe;
176 drv->drv.remove = mmc_drv_remove;
177 return driver_register(&drv->drv);
180 EXPORT_SYMBOL(mmc_register_driver);
183 * mmc_unregister_driver - unregister a media driver
184 * @drv: MMC media driver
186 void mmc_unregister_driver(struct mmc_driver *drv)
188 drv->drv.bus = &mmc_bus_type;
189 driver_unregister(&drv->drv);
192 EXPORT_SYMBOL(mmc_unregister_driver);
196 * Internal function. Initialise a MMC card structure.
198 void mmc_init_card(struct mmc_card *card, struct mmc_host *host)
200 memset(card, 0, sizeof(struct mmc_card));
201 card->host = host;
202 device_initialize(&card->dev);
203 card->dev.parent = card->host->dev;
204 card->dev.bus = &mmc_bus_type;
205 card->dev.release = mmc_release_card;
209 * Internal function. Register a new MMC card with the driver model.
211 int mmc_register_card(struct mmc_card *card)
213 int ret;
215 snprintf(card->dev.bus_id, sizeof(card->dev.bus_id),
216 "%s:%04x", mmc_hostname(card->host), card->rca);
218 ret = device_add(&card->dev);
219 if (ret == 0) {
220 if (mmc_card_sd(card)) {
221 ret = device_create_file(&card->dev, &mmc_dev_attr_scr);
222 if (ret)
223 device_del(&card->dev);
226 return ret;
230 * Internal function. Unregister a new MMC card with the
231 * driver model, and (eventually) free it.
233 void mmc_remove_card(struct mmc_card *card)
235 if (mmc_card_present(card)) {
236 if (mmc_card_sd(card))
237 device_remove_file(&card->dev, &mmc_dev_attr_scr);
239 device_del(&card->dev);
242 put_device(&card->dev);
246 static void mmc_host_classdev_release(struct class_device *dev)
248 struct mmc_host *host = cls_dev_to_mmc_host(dev);
249 kfree(host);
252 static struct class mmc_host_class = {
253 .name = "mmc_host",
254 .release = mmc_host_classdev_release,
257 static DEFINE_IDR(mmc_host_idr);
258 #if 0 // mask by Victor Yu. 11-28-2005
259 static DEFINE_SPINLOCK(mmc_host_lock);
260 #else
261 static spinlock_t mmc_host_lock=SPIN_LOCK_UNLOCKED;
262 #endif
265 * Internal function. Allocate a new MMC host.
267 struct mmc_host *mmc_alloc_host_sysfs(int extra, struct device *dev)
269 struct mmc_host *host;
271 host = kmalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL);
272 if (host) {
273 memset(host, 0, sizeof(struct mmc_host) + extra);
275 host->dev = dev;
276 host->class_dev.dev = host->dev;
277 host->class_dev.class = &mmc_host_class;
278 class_device_initialize(&host->class_dev);
281 return host;
285 * Internal function. Register a new MMC host with the MMC class.
287 int mmc_add_host_sysfs(struct mmc_host *host)
289 int err;
291 if (!idr_pre_get(&mmc_host_idr, GFP_KERNEL))
292 return -ENOMEM;
294 spin_lock(&mmc_host_lock);
295 err = idr_get_new(&mmc_host_idr, host, &host->index);
296 spin_unlock(&mmc_host_lock);
297 if (err)
298 return err;
300 snprintf(host->class_dev.class_id, BUS_ID_SIZE,
301 "mmc%d", host->index);
303 return class_device_add(&host->class_dev);
307 * Internal function. Unregister a MMC host with the MMC class.
309 void mmc_remove_host_sysfs(struct mmc_host *host)
311 class_device_del(&host->class_dev);
313 spin_lock(&mmc_host_lock);
314 idr_remove(&mmc_host_idr, host->index);
315 spin_unlock(&mmc_host_lock);
319 * Internal function. Free a MMC host.
321 void mmc_free_host_sysfs(struct mmc_host *host)
323 class_device_put(&host->class_dev);
327 static int __init mmc_init(void)
329 int ret = bus_register(&mmc_bus_type);
330 if (ret == 0) {
331 ret = class_register(&mmc_host_class);
332 if (ret)
333 bus_unregister(&mmc_bus_type);
335 return ret;
338 static void __exit mmc_exit(void)
340 class_unregister(&mmc_host_class);
341 bus_unregister(&mmc_bus_type);
344 module_init(mmc_init);
345 module_exit(mmc_exit);