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