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>
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) \
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(date
, "%02d/%04d\n", card
->cid
.month
, card
->cid
.year
);
38 MMC_ATTR(fwrev
, "0x%x\n", card
->cid
.fwrev
);
39 MMC_ATTR(hwrev
, "0x%x\n", card
->cid
.hwrev
);
40 MMC_ATTR(manfid
, "0x%06x\n", card
->cid
.manfid
);
41 MMC_ATTR(name
, "%s\n", card
->cid
.prod_name
);
42 MMC_ATTR(oemid
, "0x%04x\n", card
->cid
.oemid
);
43 MMC_ATTR(serial
, "0x%08x\n", card
->cid
.serial
);
45 #define MMC_ATTR_RO(name) __ATTR(name, S_IRUGO, mmc_##name##_show, NULL)
47 static struct device_attribute mmc_dev_attrs
[] = {
61 static void mmc_release_card(struct device
*dev
)
63 struct mmc_card
*card
= dev_to_mmc_card(dev
);
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.
73 static int mmc_bus_match(struct device
*dev
, struct device_driver
*drv
)
75 struct mmc_card
*card
= dev_to_mmc_card(dev
);
76 return !mmc_card_bad(card
);
80 mmc_bus_hotplug(struct device
*dev
, char **envp
, int num_envp
, char *buf
,
83 struct mmc_card
*card
= dev_to_mmc_card(dev
);
87 #define add_env(fmt,val) \
89 int len, ret = -ENOMEM; \
92 len = snprintf(buf, buf_size, fmt, val) + 1; \
101 for (i
= 0; i
< 12; i
++)
102 ccc
[i
] = card
->csd
.cmdclass
& (1 << i
) ? '1' : '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
);
114 static int mmc_bus_suspend(struct device
*dev
, pm_message_t state
)
116 struct mmc_driver
*drv
= to_mmc_driver(dev
->driver
);
117 struct mmc_card
*card
= dev_to_mmc_card(dev
);
120 if (dev
->driver
&& drv
->suspend
)
121 ret
= drv
->suspend(card
, state
);
125 static int mmc_bus_resume(struct device
*dev
)
127 struct mmc_driver
*drv
= to_mmc_driver(dev
->driver
);
128 struct mmc_card
*card
= dev_to_mmc_card(dev
);
131 if (dev
->driver
&& drv
->resume
)
132 ret
= drv
->resume(card
);
136 static struct bus_type mmc_bus_type
= {
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
,
146 static int mmc_drv_probe(struct device
*dev
)
148 struct mmc_driver
*drv
= to_mmc_driver(dev
->driver
);
149 struct mmc_card
*card
= dev_to_mmc_card(dev
);
151 return drv
->probe(card
);
154 static int mmc_drv_remove(struct device
*dev
)
156 struct mmc_driver
*drv
= to_mmc_driver(dev
->driver
);
157 struct mmc_card
*card
= dev_to_mmc_card(dev
);
166 * mmc_register_driver - register a media driver
167 * @drv: MMC media driver
169 int mmc_register_driver(struct mmc_driver
*drv
)
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
);
177 EXPORT_SYMBOL(mmc_register_driver
);
180 * mmc_unregister_driver - unregister a media driver
181 * @drv: MMC media driver
183 void mmc_unregister_driver(struct mmc_driver
*drv
)
185 drv
->drv
.bus
= &mmc_bus_type
;
186 driver_unregister(&drv
->drv
);
189 EXPORT_SYMBOL(mmc_unregister_driver
);
193 * Internal function. Initialise a MMC card structure.
195 void mmc_init_card(struct mmc_card
*card
, struct mmc_host
*host
)
197 memset(card
, 0, sizeof(struct mmc_card
));
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
;
206 * Internal function. Register a new MMC card with the driver model.
208 int mmc_register_card(struct mmc_card
*card
)
210 snprintf(card
->dev
.bus_id
, sizeof(card
->dev
.bus_id
),
211 "%s:%04x", mmc_hostname(card
->host
), card
->rca
);
213 return device_add(&card
->dev
);
217 * Internal function. Unregister a new MMC card with the
218 * driver model, and (eventually) free it.
220 void mmc_remove_card(struct mmc_card
*card
)
222 if (mmc_card_present(card
))
223 device_del(&card
->dev
);
225 put_device(&card
->dev
);
229 static void mmc_host_classdev_release(struct class_device
*dev
)
231 struct mmc_host
*host
= cls_dev_to_mmc_host(dev
);
235 static struct class mmc_host_class
= {
237 .release
= mmc_host_classdev_release
,
240 static DEFINE_IDR(mmc_host_idr
);
241 static DEFINE_SPINLOCK(mmc_host_lock
);
244 * Internal function. Allocate a new MMC host.
246 struct mmc_host
*mmc_alloc_host_sysfs(int extra
, struct device
*dev
)
248 struct mmc_host
*host
;
250 host
= kmalloc(sizeof(struct mmc_host
) + extra
, GFP_KERNEL
);
252 memset(host
, 0, sizeof(struct mmc_host
) + extra
);
255 host
->class_dev
.dev
= host
->dev
;
256 host
->class_dev
.class = &mmc_host_class
;
257 class_device_initialize(&host
->class_dev
);
264 * Internal function. Register a new MMC host with the MMC class.
266 int mmc_add_host_sysfs(struct mmc_host
*host
)
270 if (!idr_pre_get(&mmc_host_idr
, GFP_KERNEL
))
273 spin_lock(&mmc_host_lock
);
274 err
= idr_get_new(&mmc_host_idr
, host
, &host
->index
);
275 spin_unlock(&mmc_host_lock
);
279 snprintf(host
->class_dev
.class_id
, BUS_ID_SIZE
,
280 "mmc%d", host
->index
);
282 return class_device_add(&host
->class_dev
);
286 * Internal function. Unregister a MMC host with the MMC class.
288 void mmc_remove_host_sysfs(struct mmc_host
*host
)
290 class_device_del(&host
->class_dev
);
292 spin_lock(&mmc_host_lock
);
293 idr_remove(&mmc_host_idr
, host
->index
);
294 spin_unlock(&mmc_host_lock
);
298 * Internal function. Free a MMC host.
300 void mmc_free_host_sysfs(struct mmc_host
*host
)
302 class_device_put(&host
->class_dev
);
306 static int __init
mmc_init(void)
308 int ret
= bus_register(&mmc_bus_type
);
310 ret
= class_register(&mmc_host_class
);
312 bus_unregister(&mmc_bus_type
);
317 static void __exit
mmc_exit(void)
319 class_unregister(&mmc_host_class
);
320 bus_unregister(&mmc_bus_type
);
323 module_init(mmc_init
);
324 module_exit(mmc_exit
);