blob: ad8949810fc5cbeb79de505265ed57fb1aeba6b9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Russell Kingdce77372005-08-19 09:42:52 +010015#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17#include <linux/mmc/card.h>
18#include <linux/mmc/host.h>
19
20#include "mmc.h"
21
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)
Russell King00b137c2005-08-19 09:41:24 +010024#define cls_dev_to_mmc_host(d) container_of(d, struct mmc_host, class_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#define MMC_ATTR(name, fmt, args...) \
Yani Ioannoue404e272005-05-17 06:42:58 -040027static ssize_t mmc_##name##_show (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070028{ \
29 struct mmc_card *card = dev_to_mmc_card(dev); \
30 return sprintf(buf, fmt, args); \
31}
32
33MMC_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
34 card->raw_cid[2], card->raw_cid[3]);
35MMC_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
36 card->raw_csd[2], card->raw_csd[3]);
37MMC_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
38MMC_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
39MMC_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
40MMC_ATTR(manfid, "0x%06x\n", card->cid.manfid);
41MMC_ATTR(name, "%s\n", card->cid.prod_name);
42MMC_ATTR(oemid, "0x%04x\n", card->cid.oemid);
43MMC_ATTR(serial, "0x%08x\n", card->cid.serial);
44
45#define MMC_ATTR_RO(name) __ATTR(name, S_IRUGO, mmc_##name##_show, NULL)
46
47static struct device_attribute mmc_dev_attrs[] = {
48 MMC_ATTR_RO(cid),
49 MMC_ATTR_RO(csd),
50 MMC_ATTR_RO(date),
51 MMC_ATTR_RO(fwrev),
52 MMC_ATTR_RO(hwrev),
53 MMC_ATTR_RO(manfid),
54 MMC_ATTR_RO(name),
55 MMC_ATTR_RO(oemid),
56 MMC_ATTR_RO(serial),
57 __ATTR_NULL
58};
59
60
61static void mmc_release_card(struct device *dev)
62{
63 struct mmc_card *card = dev_to_mmc_card(dev);
64
65 kfree(card);
66}
67
68/*
69 * This currently matches any MMC driver to any MMC card - drivers
70 * themselves make the decision whether to drive this card in their
71 * probe method. However, we force "bad" cards to fail.
72 */
73static int mmc_bus_match(struct device *dev, struct device_driver *drv)
74{
75 struct mmc_card *card = dev_to_mmc_card(dev);
76 return !mmc_card_bad(card);
77}
78
79static int
80mmc_bus_hotplug(struct device *dev, char **envp, int num_envp, char *buf,
81 int buf_size)
82{
83 struct mmc_card *card = dev_to_mmc_card(dev);
84 char ccc[13];
85 int i = 0;
86
87#define add_env(fmt,val) \
88 ({ \
89 int len, ret = -ENOMEM; \
90 if (i < num_envp) { \
91 envp[i++] = buf; \
92 len = snprintf(buf, buf_size, fmt, val) + 1; \
93 buf_size -= len; \
94 buf += len; \
95 if (buf_size >= 0) \
96 ret = 0; \
97 } \
98 ret; \
99 })
100
101 for (i = 0; i < 12; i++)
102 ccc[i] = card->csd.cmdclass & (1 << i) ? '1' : '0';
103 ccc[12] = '\0';
104
105 i = 0;
106 add_env("MMC_CCC=%s", ccc);
107 add_env("MMC_MANFID=%06x", card->cid.manfid);
108 add_env("MMC_NAME=%s", mmc_card_name(card));
109 add_env("MMC_OEMID=%04x", card->cid.oemid);
110
111 return 0;
112}
113
114static int mmc_bus_suspend(struct device *dev, pm_message_t state)
115{
116 struct mmc_driver *drv = to_mmc_driver(dev->driver);
117 struct mmc_card *card = dev_to_mmc_card(dev);
118 int ret = 0;
119
120 if (dev->driver && drv->suspend)
121 ret = drv->suspend(card, state);
122 return ret;
123}
124
125static int mmc_bus_resume(struct device *dev)
126{
127 struct mmc_driver *drv = to_mmc_driver(dev->driver);
128 struct mmc_card *card = dev_to_mmc_card(dev);
129 int ret = 0;
130
131 if (dev->driver && drv->resume)
132 ret = drv->resume(card);
133 return ret;
134}
135
136static struct bus_type mmc_bus_type = {
137 .name = "mmc",
138 .dev_attrs = mmc_dev_attrs,
139 .match = mmc_bus_match,
140 .hotplug = mmc_bus_hotplug,
141 .suspend = mmc_bus_suspend,
142 .resume = mmc_bus_resume,
143};
144
145
146static int mmc_drv_probe(struct device *dev)
147{
148 struct mmc_driver *drv = to_mmc_driver(dev->driver);
149 struct mmc_card *card = dev_to_mmc_card(dev);
150
151 return drv->probe(card);
152}
153
154static int mmc_drv_remove(struct device *dev)
155{
156 struct mmc_driver *drv = to_mmc_driver(dev->driver);
157 struct mmc_card *card = dev_to_mmc_card(dev);
158
159 drv->remove(card);
160
161 return 0;
162}
163
164
165/**
166 * mmc_register_driver - register a media driver
167 * @drv: MMC media driver
168 */
169int mmc_register_driver(struct mmc_driver *drv)
170{
171 drv->drv.bus = &mmc_bus_type;
172 drv->drv.probe = mmc_drv_probe;
173 drv->drv.remove = mmc_drv_remove;
174 return driver_register(&drv->drv);
175}
176
177EXPORT_SYMBOL(mmc_register_driver);
178
179/**
180 * mmc_unregister_driver - unregister a media driver
181 * @drv: MMC media driver
182 */
183void mmc_unregister_driver(struct mmc_driver *drv)
184{
185 drv->drv.bus = &mmc_bus_type;
186 driver_unregister(&drv->drv);
187}
188
189EXPORT_SYMBOL(mmc_unregister_driver);
190
191
192/*
193 * Internal function. Initialise a MMC card structure.
194 */
195void mmc_init_card(struct mmc_card *card, struct mmc_host *host)
196{
197 memset(card, 0, sizeof(struct mmc_card));
198 card->host = host;
199 device_initialize(&card->dev);
200 card->dev.parent = card->host->dev;
201 card->dev.bus = &mmc_bus_type;
202 card->dev.release = mmc_release_card;
203}
204
205/*
206 * Internal function. Register a new MMC card with the driver model.
207 */
208int mmc_register_card(struct mmc_card *card)
209{
210 snprintf(card->dev.bus_id, sizeof(card->dev.bus_id),
Russell Kingd366b642005-08-19 09:40:08 +0100211 "%s:%04x", mmc_hostname(card->host), card->rca);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 return device_add(&card->dev);
214}
215
216/*
217 * Internal function. Unregister a new MMC card with the
218 * driver model, and (eventually) free it.
219 */
220void mmc_remove_card(struct mmc_card *card)
221{
222 if (mmc_card_present(card))
223 device_del(&card->dev);
224
225 put_device(&card->dev);
226}
227
228
Russell King00b137c2005-08-19 09:41:24 +0100229static void mmc_host_classdev_release(struct class_device *dev)
230{
231 struct mmc_host *host = cls_dev_to_mmc_host(dev);
232 kfree(host);
233}
234
235static struct class mmc_host_class = {
236 .name = "mmc_host",
237 .release = mmc_host_classdev_release,
238};
239
Russell Kingdce77372005-08-19 09:42:52 +0100240static DEFINE_IDR(mmc_host_idr);
241static DEFINE_SPINLOCK(mmc_host_lock);
242
Russell King00b137c2005-08-19 09:41:24 +0100243/*
244 * Internal function. Allocate a new MMC host.
245 */
246struct mmc_host *mmc_alloc_host_sysfs(int extra, struct device *dev)
247{
248 struct mmc_host *host;
249
250 host = kmalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL);
251 if (host) {
252 memset(host, 0, sizeof(struct mmc_host) + extra);
253
254 host->dev = dev;
255 host->class_dev.dev = host->dev;
256 host->class_dev.class = &mmc_host_class;
257 class_device_initialize(&host->class_dev);
258 }
259
260 return host;
261}
262
263/*
264 * Internal function. Register a new MMC host with the MMC class.
265 */
266int mmc_add_host_sysfs(struct mmc_host *host)
267{
Russell Kingdce77372005-08-19 09:42:52 +0100268 int err;
269
270 if (!idr_pre_get(&mmc_host_idr, GFP_KERNEL))
271 return -ENOMEM;
272
273 spin_lock(&mmc_host_lock);
274 err = idr_get_new(&mmc_host_idr, host, &host->index);
275 spin_unlock(&mmc_host_lock);
276 if (err)
277 return err;
Russell King00b137c2005-08-19 09:41:24 +0100278
Russell King1ad434d2005-08-19 09:42:21 +0100279 snprintf(host->class_dev.class_id, BUS_ID_SIZE,
Russell Kingdce77372005-08-19 09:42:52 +0100280 "mmc%d", host->index);
Russell King00b137c2005-08-19 09:41:24 +0100281
Russell King00b137c2005-08-19 09:41:24 +0100282 return class_device_add(&host->class_dev);
283}
284
285/*
286 * Internal function. Unregister a MMC host with the MMC class.
287 */
288void mmc_remove_host_sysfs(struct mmc_host *host)
289{
290 class_device_del(&host->class_dev);
Russell Kingdce77372005-08-19 09:42:52 +0100291
292 spin_lock(&mmc_host_lock);
293 idr_remove(&mmc_host_idr, host->index);
294 spin_unlock(&mmc_host_lock);
Russell King00b137c2005-08-19 09:41:24 +0100295}
296
297/*
298 * Internal function. Free a MMC host.
299 */
300void mmc_free_host_sysfs(struct mmc_host *host)
301{
302 class_device_put(&host->class_dev);
303}
304
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306static int __init mmc_init(void)
307{
Russell King00b137c2005-08-19 09:41:24 +0100308 int ret = bus_register(&mmc_bus_type);
309 if (ret == 0) {
310 ret = class_register(&mmc_host_class);
311 if (ret)
312 bus_unregister(&mmc_bus_type);
313 }
314 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
317static void __exit mmc_exit(void)
318{
Russell King00b137c2005-08-19 09:41:24 +0100319 class_unregister(&mmc_host_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 bus_unregister(&mmc_bus_type);
321}
322
323module_init(mmc_init);
324module_exit(mmc_exit);