This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / mmc / mmc_sysfs.c
1 /*
2  *  linux/drivers/mmc/mmc_sysfs.c
3  *
4  *  Copyright (C) 2003 Russell King, All Rights Reserved.
5  *
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.
9  *
10  *  MMC sysfs/driver model support.
11  */
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/device.h>
15
16 #include <linux/mmc/card.h>
17 #include <linux/mmc/host.h>
18
19 #include "mmc.h"
20
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
24 static void mmc_release_card(struct device *dev)
25 {
26         struct mmc_card *card = dev_to_mmc_card(dev);
27
28         kfree(card);
29 }
30
31 /*
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.
35  */
36 static int mmc_bus_match(struct device *dev, struct device_driver *drv)
37 {
38         struct mmc_card *card = dev_to_mmc_card(dev);
39         return !mmc_card_bad(card);
40 }
41
42 static int
43 mmc_bus_hotplug(struct device *dev, char **envp, int num_envp, char *buf,
44                 int buf_size)
45 {
46         struct mmc_card *card = dev_to_mmc_card(dev);
47         char ccc[13];
48         int i = 0;
49
50 #define add_env(fmt,val)                                                \
51         ({                                                              \
52                 int len, ret = -ENOMEM;                                 \
53                 if (i < num_envp) {                                     \
54                         envp[i++] = buf;                                \
55                         len = snprintf(buf, buf_size, fmt, val) + 1;    \
56                         buf_size -= len;                                \
57                         buf += len;                                     \
58                         if (buf_size >= 0)                              \
59                                 ret = 0;                                \
60                 }                                                       \
61                 ret;                                                    \
62         })
63
64         for (i = 0; i < 12; i++)
65                 ccc[i] = card->csd.cmdclass & (1 << i) ? '1' : '0';
66         ccc[12] = '\0';
67
68         i = 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);
73
74         return 0;
75 }
76
77 static int mmc_bus_suspend(struct device *dev, u32 state)
78 {
79         struct mmc_driver *drv = to_mmc_driver(dev->driver);
80         struct mmc_card *card = dev_to_mmc_card(dev);
81         int ret = 0;
82
83         if (dev->driver && drv->suspend)
84                 ret = drv->suspend(card, state);
85         return ret;
86 }
87
88 static int mmc_bus_resume(struct device *dev)
89 {
90         struct mmc_driver *drv = to_mmc_driver(dev->driver);
91         struct mmc_card *card = dev_to_mmc_card(dev);
92         int ret = 0;
93
94         if (dev->driver && drv->resume)
95                 ret = drv->resume(card);
96         return ret;
97 }
98
99 static struct bus_type mmc_bus_type = {
100         .name           = "mmc",
101         .match          = mmc_bus_match,
102         .hotplug        = mmc_bus_hotplug,
103         .suspend        = mmc_bus_suspend,
104         .resume         = mmc_bus_resume,
105 };
106
107
108 static int mmc_drv_probe(struct device *dev)
109 {
110         struct mmc_driver *drv = to_mmc_driver(dev->driver);
111         struct mmc_card *card = dev_to_mmc_card(dev);
112
113         return drv->probe(card);
114 }
115
116 static int mmc_drv_remove(struct device *dev)
117 {
118         struct mmc_driver *drv = to_mmc_driver(dev->driver);
119         struct mmc_card *card = dev_to_mmc_card(dev);
120
121         drv->remove(card);
122
123         return 0;
124 }
125
126
127 /**
128  *      mmc_register_driver - register a media driver
129  *      @drv: MMC media driver
130  */
131 int mmc_register_driver(struct mmc_driver *drv)
132 {
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);
137 }
138
139 EXPORT_SYMBOL(mmc_register_driver);
140
141 /**
142  *      mmc_unregister_driver - unregister a media driver
143  *      @drv: MMC media driver
144  */
145 void mmc_unregister_driver(struct mmc_driver *drv)
146 {
147         drv->drv.bus = &mmc_bus_type;
148         driver_unregister(&drv->drv);
149 }
150
151 EXPORT_SYMBOL(mmc_unregister_driver);
152
153
154 #define MMC_ATTR(name, fmt, args...)                                    \
155 static ssize_t mmc_dev_show_##name (struct device *dev, char *buf)      \
156 {                                                                       \
157         struct mmc_card *card = dev_to_mmc_card(dev);                   \
158         return sprintf(buf, fmt, args);                                 \
159 }                                                                       \
160 static DEVICE_ATTR(name, S_IRUGO, mmc_dev_show_##name, NULL)
161
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);
173
174 static struct device_attribute *mmc_dev_attributes[] = {
175         &dev_attr_cid,
176         &dev_attr_csd,
177         &dev_attr_date,
178         &dev_attr_fwrev,
179         &dev_attr_hwrev,
180         &dev_attr_manfid,
181         &dev_attr_name,
182         &dev_attr_oemid,
183         &dev_attr_serial,
184 };
185
186 /*
187  * Internal function.  Initialise a MMC card structure.
188  */
189 void mmc_init_card(struct mmc_card *card, struct mmc_host *host)
190 {
191         memset(card, 0, sizeof(struct mmc_card));
192         card->host = host;
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;
197 }
198
199 /*
200  * Internal function.  Register a new MMC card with the driver model.
201  */
202 int mmc_register_card(struct mmc_card *card)
203 {
204         int ret, i;
205
206         snprintf(card->dev.bus_id, sizeof(card->dev.bus_id),
207                  "%s:%04x", card->host->host_name, card->rca);
208
209         ret = device_add(&card->dev);
210         if (ret == 0)
211                 for (i = 0; i < ARRAY_SIZE(mmc_dev_attributes); i++)
212                         device_create_file(&card->dev, mmc_dev_attributes[i]);
213
214         return ret;
215 }
216
217 /*
218  * Internal function.  Unregister a new MMC card with the
219  * driver model, and (eventually) free it.
220  */
221 void mmc_remove_card(struct mmc_card *card)
222 {
223         if (mmc_card_present(card))
224                 device_del(&card->dev);
225
226         put_device(&card->dev);
227 }
228
229
230 static int __init mmc_init(void)
231 {
232         return bus_register(&mmc_bus_type);
233 }
234
235 static void __exit mmc_exit(void)
236 {
237         bus_unregister(&mmc_bus_type);
238 }
239
240 module_init(mmc_init);
241 module_exit(mmc_exit);