]> err.no Git - linux-2.6/blob - drivers/mmc/mmc_sysfs.c
[MMC] Use class device name for mmc host name
[linux-2.6] / 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 #define cls_dev_to_mmc_host(d)  container_of(d, struct mmc_host, class_dev)
24
25 #define MMC_ATTR(name, fmt, args...)                                    \
26 static ssize_t mmc_##name##_show (struct device *dev, struct device_attribute *attr, char *buf) \
27 {                                                                       \
28         struct mmc_card *card = dev_to_mmc_card(dev);                   \
29         return sprintf(buf, fmt, args);                                 \
30 }
31
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);
43
44 #define MMC_ATTR_RO(name) __ATTR(name, S_IRUGO, mmc_##name##_show, NULL)
45
46 static struct device_attribute mmc_dev_attrs[] = {
47         MMC_ATTR_RO(cid),
48         MMC_ATTR_RO(csd),
49         MMC_ATTR_RO(date),
50         MMC_ATTR_RO(fwrev),
51         MMC_ATTR_RO(hwrev),
52         MMC_ATTR_RO(manfid),
53         MMC_ATTR_RO(name),
54         MMC_ATTR_RO(oemid),
55         MMC_ATTR_RO(serial),
56         __ATTR_NULL
57 };
58
59
60 static void mmc_release_card(struct device *dev)
61 {
62         struct mmc_card *card = dev_to_mmc_card(dev);
63
64         kfree(card);
65 }
66
67 /*
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.
71  */
72 static int mmc_bus_match(struct device *dev, struct device_driver *drv)
73 {
74         struct mmc_card *card = dev_to_mmc_card(dev);
75         return !mmc_card_bad(card);
76 }
77
78 static int
79 mmc_bus_hotplug(struct device *dev, char **envp, int num_envp, char *buf,
80                 int buf_size)
81 {
82         struct mmc_card *card = dev_to_mmc_card(dev);
83         char ccc[13];
84         int i = 0;
85
86 #define add_env(fmt,val)                                                \
87         ({                                                              \
88                 int len, ret = -ENOMEM;                                 \
89                 if (i < num_envp) {                                     \
90                         envp[i++] = buf;                                \
91                         len = snprintf(buf, buf_size, fmt, val) + 1;    \
92                         buf_size -= len;                                \
93                         buf += len;                                     \
94                         if (buf_size >= 0)                              \
95                                 ret = 0;                                \
96                 }                                                       \
97                 ret;                                                    \
98         })
99
100         for (i = 0; i < 12; i++)
101                 ccc[i] = card->csd.cmdclass & (1 << i) ? '1' : '0';
102         ccc[12] = '\0';
103
104         i = 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);
109
110         return 0;
111 }
112
113 static int mmc_bus_suspend(struct device *dev, pm_message_t state)
114 {
115         struct mmc_driver *drv = to_mmc_driver(dev->driver);
116         struct mmc_card *card = dev_to_mmc_card(dev);
117         int ret = 0;
118
119         if (dev->driver && drv->suspend)
120                 ret = drv->suspend(card, state);
121         return ret;
122 }
123
124 static int mmc_bus_resume(struct device *dev)
125 {
126         struct mmc_driver *drv = to_mmc_driver(dev->driver);
127         struct mmc_card *card = dev_to_mmc_card(dev);
128         int ret = 0;
129
130         if (dev->driver && drv->resume)
131                 ret = drv->resume(card);
132         return ret;
133 }
134
135 static struct bus_type mmc_bus_type = {
136         .name           = "mmc",
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,
142 };
143
144
145 static int mmc_drv_probe(struct device *dev)
146 {
147         struct mmc_driver *drv = to_mmc_driver(dev->driver);
148         struct mmc_card *card = dev_to_mmc_card(dev);
149
150         return drv->probe(card);
151 }
152
153 static int mmc_drv_remove(struct device *dev)
154 {
155         struct mmc_driver *drv = to_mmc_driver(dev->driver);
156         struct mmc_card *card = dev_to_mmc_card(dev);
157
158         drv->remove(card);
159
160         return 0;
161 }
162
163
164 /**
165  *      mmc_register_driver - register a media driver
166  *      @drv: MMC media driver
167  */
168 int mmc_register_driver(struct mmc_driver *drv)
169 {
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);
174 }
175
176 EXPORT_SYMBOL(mmc_register_driver);
177
178 /**
179  *      mmc_unregister_driver - unregister a media driver
180  *      @drv: MMC media driver
181  */
182 void mmc_unregister_driver(struct mmc_driver *drv)
183 {
184         drv->drv.bus = &mmc_bus_type;
185         driver_unregister(&drv->drv);
186 }
187
188 EXPORT_SYMBOL(mmc_unregister_driver);
189
190
191 /*
192  * Internal function.  Initialise a MMC card structure.
193  */
194 void mmc_init_card(struct mmc_card *card, struct mmc_host *host)
195 {
196         memset(card, 0, sizeof(struct mmc_card));
197         card->host = host;
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;
202 }
203
204 /*
205  * Internal function.  Register a new MMC card with the driver model.
206  */
207 int mmc_register_card(struct mmc_card *card)
208 {
209         snprintf(card->dev.bus_id, sizeof(card->dev.bus_id),
210                  "%s:%04x", mmc_hostname(card->host), card->rca);
211
212         return device_add(&card->dev);
213 }
214
215 /*
216  * Internal function.  Unregister a new MMC card with the
217  * driver model, and (eventually) free it.
218  */
219 void mmc_remove_card(struct mmc_card *card)
220 {
221         if (mmc_card_present(card))
222                 device_del(&card->dev);
223
224         put_device(&card->dev);
225 }
226
227
228 static void mmc_host_classdev_release(struct class_device *dev)
229 {
230         struct mmc_host *host = cls_dev_to_mmc_host(dev);
231         kfree(host);
232 }
233
234 static struct class mmc_host_class = {
235         .name           = "mmc_host",
236         .release        = mmc_host_classdev_release,
237 };
238
239 /*
240  * Internal function. Allocate a new MMC host.
241  */
242 struct mmc_host *mmc_alloc_host_sysfs(int extra, struct device *dev)
243 {
244         struct mmc_host *host;
245
246         host = kmalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL);
247         if (host) {
248                 memset(host, 0, sizeof(struct mmc_host) + extra);
249
250                 host->dev = dev;
251                 host->class_dev.dev = host->dev;
252                 host->class_dev.class = &mmc_host_class;
253                 class_device_initialize(&host->class_dev);
254         }
255
256         return host;
257 }
258
259 /*
260  * Internal function. Register a new MMC host with the MMC class.
261  */
262 int mmc_add_host_sysfs(struct mmc_host *host)
263 {
264         static unsigned int host_num;
265
266         snprintf(host->class_dev.class_id, BUS_ID_SIZE,
267                  "mmc%d", host_num++);
268
269         return class_device_add(&host->class_dev);
270 }
271
272 /*
273  * Internal function. Unregister a MMC host with the MMC class.
274  */
275 void mmc_remove_host_sysfs(struct mmc_host *host)
276 {
277         class_device_del(&host->class_dev);
278 }
279
280 /*
281  * Internal function. Free a MMC host.
282  */
283 void mmc_free_host_sysfs(struct mmc_host *host)
284 {
285         class_device_put(&host->class_dev);
286 }
287
288
289 static int __init mmc_init(void)
290 {
291         int ret = bus_register(&mmc_bus_type);
292         if (ret == 0) {
293                 ret = class_register(&mmc_host_class);
294                 if (ret)
295                         bus_unregister(&mmc_bus_type);
296         }
297         return ret;
298 }
299
300 static void __exit mmc_exit(void)
301 {
302         class_unregister(&mmc_host_class);
303         bus_unregister(&mmc_bus_type);
304 }
305
306 module_init(mmc_init);
307 module_exit(mmc_exit);