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