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