Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/mmc/core/bus.c |
| 3 | * |
| 4 | * Copyright (C) 2003 Russell King, All Rights Reserved. |
| 5 | * Copyright (C) 2007 Pierre Ossman |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | * |
| 11 | * MMC card bus driver model |
| 12 | */ |
| 13 | |
| 14 | #include <linux/device.h> |
| 15 | #include <linux/err.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 16 | #include <linux/slab.h> |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 17 | |
| 18 | #include <linux/mmc/card.h> |
| 19 | #include <linux/mmc/host.h> |
| 20 | |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 21 | #include "core.h" |
Pierre Ossman | 1a632f8 | 2007-07-30 15:15:30 +0200 | [diff] [blame] | 22 | #include "sdio_cis.h" |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 23 | #include "bus.h" |
| 24 | |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 25 | #define to_mmc_driver(d) container_of(d, struct mmc_driver, drv) |
| 26 | |
| 27 | static ssize_t mmc_type_show(struct device *dev, |
| 28 | struct device_attribute *attr, char *buf) |
| 29 | { |
Andy Shevchenko | 265cdc9 | 2010-09-17 20:32:25 -0400 | [diff] [blame^] | 30 | struct mmc_card *card = mmc_dev_to_card(dev); |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 31 | |
| 32 | switch (card->type) { |
| 33 | case MMC_TYPE_MMC: |
| 34 | return sprintf(buf, "MMC\n"); |
| 35 | case MMC_TYPE_SD: |
| 36 | return sprintf(buf, "SD\n"); |
Pierre Ossman | 5c4e6f1 | 2007-05-21 20:23:20 +0200 | [diff] [blame] | 37 | case MMC_TYPE_SDIO: |
| 38 | return sprintf(buf, "SDIO\n"); |
Michal Miroslaw | 7310ece8 | 2010-08-10 18:01:40 -0700 | [diff] [blame] | 39 | case MMC_TYPE_SD_COMBO: |
| 40 | return sprintf(buf, "SDcombo\n"); |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 41 | default: |
| 42 | return -EFAULT; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | static struct device_attribute mmc_dev_attrs[] = { |
Pierre Ossman | 51ec92e | 2008-03-21 23:54:50 +0100 | [diff] [blame] | 47 | __ATTR(type, S_IRUGO, mmc_type_show, NULL), |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 48 | __ATTR_NULL, |
| 49 | }; |
| 50 | |
| 51 | /* |
| 52 | * This currently matches any MMC driver to any MMC card - drivers |
| 53 | * themselves make the decision whether to drive this card in their |
| 54 | * probe method. |
| 55 | */ |
| 56 | static int mmc_bus_match(struct device *dev, struct device_driver *drv) |
| 57 | { |
| 58 | return 1; |
| 59 | } |
| 60 | |
| 61 | static int |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 62 | mmc_bus_uevent(struct device *dev, struct kobj_uevent_env *env) |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 63 | { |
Andy Shevchenko | 265cdc9 | 2010-09-17 20:32:25 -0400 | [diff] [blame^] | 64 | struct mmc_card *card = mmc_dev_to_card(dev); |
Pierre Ossman | 9eb3a94 | 2007-06-17 11:18:46 +0200 | [diff] [blame] | 65 | const char *type; |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 66 | int retval = 0; |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 67 | |
| 68 | switch (card->type) { |
| 69 | case MMC_TYPE_MMC: |
Pierre Ossman | 9eb3a94 | 2007-06-17 11:18:46 +0200 | [diff] [blame] | 70 | type = "MMC"; |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 71 | break; |
| 72 | case MMC_TYPE_SD: |
Pierre Ossman | 9eb3a94 | 2007-06-17 11:18:46 +0200 | [diff] [blame] | 73 | type = "SD"; |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 74 | break; |
Pierre Ossman | 5c4e6f1 | 2007-05-21 20:23:20 +0200 | [diff] [blame] | 75 | case MMC_TYPE_SDIO: |
Pierre Ossman | 9eb3a94 | 2007-06-17 11:18:46 +0200 | [diff] [blame] | 76 | type = "SDIO"; |
Pierre Ossman | 5c4e6f1 | 2007-05-21 20:23:20 +0200 | [diff] [blame] | 77 | break; |
Michal Miroslaw | 7310ece8 | 2010-08-10 18:01:40 -0700 | [diff] [blame] | 78 | case MMC_TYPE_SD_COMBO: |
| 79 | type = "SDcombo"; |
| 80 | break; |
Pierre Ossman | 9eb3a94 | 2007-06-17 11:18:46 +0200 | [diff] [blame] | 81 | default: |
| 82 | type = NULL; |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 83 | } |
| 84 | |
Pierre Ossman | 9eb3a94 | 2007-06-17 11:18:46 +0200 | [diff] [blame] | 85 | if (type) { |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 86 | retval = add_uevent_var(env, "MMC_TYPE=%s", type); |
| 87 | if (retval) |
| 88 | return retval; |
Pierre Ossman | 9eb3a94 | 2007-06-17 11:18:46 +0200 | [diff] [blame] | 89 | } |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 90 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 91 | retval = add_uevent_var(env, "MMC_NAME=%s", mmc_card_name(card)); |
Andy Whitcroft | 6b0b628 | 2009-02-23 12:38:41 +0000 | [diff] [blame] | 92 | if (retval) |
| 93 | return retval; |
| 94 | |
| 95 | /* |
| 96 | * Request the mmc_block device. Note: that this is a direct request |
| 97 | * for the module it carries no information as to what is inserted. |
| 98 | */ |
| 99 | retval = add_uevent_var(env, "MODALIAS=mmc:block"); |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 100 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 101 | return retval; |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | static int mmc_bus_probe(struct device *dev) |
| 105 | { |
| 106 | struct mmc_driver *drv = to_mmc_driver(dev->driver); |
Andy Shevchenko | 265cdc9 | 2010-09-17 20:32:25 -0400 | [diff] [blame^] | 107 | struct mmc_card *card = mmc_dev_to_card(dev); |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 108 | |
| 109 | return drv->probe(card); |
| 110 | } |
| 111 | |
| 112 | static int mmc_bus_remove(struct device *dev) |
| 113 | { |
| 114 | struct mmc_driver *drv = to_mmc_driver(dev->driver); |
Andy Shevchenko | 265cdc9 | 2010-09-17 20:32:25 -0400 | [diff] [blame^] | 115 | struct mmc_card *card = mmc_dev_to_card(dev); |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 116 | |
| 117 | drv->remove(card); |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | static int mmc_bus_suspend(struct device *dev, pm_message_t state) |
| 123 | { |
| 124 | struct mmc_driver *drv = to_mmc_driver(dev->driver); |
Andy Shevchenko | 265cdc9 | 2010-09-17 20:32:25 -0400 | [diff] [blame^] | 125 | struct mmc_card *card = mmc_dev_to_card(dev); |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 126 | int ret = 0; |
| 127 | |
| 128 | if (dev->driver && drv->suspend) |
| 129 | ret = drv->suspend(card, state); |
| 130 | return ret; |
| 131 | } |
| 132 | |
| 133 | static int mmc_bus_resume(struct device *dev) |
| 134 | { |
| 135 | struct mmc_driver *drv = to_mmc_driver(dev->driver); |
Andy Shevchenko | 265cdc9 | 2010-09-17 20:32:25 -0400 | [diff] [blame^] | 136 | struct mmc_card *card = mmc_dev_to_card(dev); |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 137 | int ret = 0; |
| 138 | |
| 139 | if (dev->driver && drv->resume) |
| 140 | ret = drv->resume(card); |
| 141 | return ret; |
| 142 | } |
| 143 | |
| 144 | static struct bus_type mmc_bus_type = { |
| 145 | .name = "mmc", |
| 146 | .dev_attrs = mmc_dev_attrs, |
| 147 | .match = mmc_bus_match, |
| 148 | .uevent = mmc_bus_uevent, |
| 149 | .probe = mmc_bus_probe, |
| 150 | .remove = mmc_bus_remove, |
| 151 | .suspend = mmc_bus_suspend, |
| 152 | .resume = mmc_bus_resume, |
| 153 | }; |
| 154 | |
| 155 | int mmc_register_bus(void) |
| 156 | { |
| 157 | return bus_register(&mmc_bus_type); |
| 158 | } |
| 159 | |
| 160 | void mmc_unregister_bus(void) |
| 161 | { |
| 162 | bus_unregister(&mmc_bus_type); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * mmc_register_driver - register a media driver |
| 167 | * @drv: MMC media driver |
| 168 | */ |
| 169 | int mmc_register_driver(struct mmc_driver *drv) |
| 170 | { |
| 171 | drv->drv.bus = &mmc_bus_type; |
| 172 | return driver_register(&drv->drv); |
| 173 | } |
| 174 | |
| 175 | EXPORT_SYMBOL(mmc_register_driver); |
| 176 | |
| 177 | /** |
| 178 | * mmc_unregister_driver - unregister a media driver |
| 179 | * @drv: MMC media driver |
| 180 | */ |
| 181 | void mmc_unregister_driver(struct mmc_driver *drv) |
| 182 | { |
| 183 | drv->drv.bus = &mmc_bus_type; |
| 184 | driver_unregister(&drv->drv); |
| 185 | } |
| 186 | |
| 187 | EXPORT_SYMBOL(mmc_unregister_driver); |
| 188 | |
| 189 | static void mmc_release_card(struct device *dev) |
| 190 | { |
Andy Shevchenko | 265cdc9 | 2010-09-17 20:32:25 -0400 | [diff] [blame^] | 191 | struct mmc_card *card = mmc_dev_to_card(dev); |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 192 | |
Pierre Ossman | 1a632f8 | 2007-07-30 15:15:30 +0200 | [diff] [blame] | 193 | sdio_free_common_cis(card); |
| 194 | |
Pierre Ossman | 759bdc7 | 2007-09-19 18:42:16 +0200 | [diff] [blame] | 195 | if (card->info) |
| 196 | kfree(card->info); |
| 197 | |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 198 | kfree(card); |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | * Allocate and initialise a new MMC card structure. |
| 203 | */ |
Pierre Ossman | 51ec92e | 2008-03-21 23:54:50 +0100 | [diff] [blame] | 204 | struct mmc_card *mmc_alloc_card(struct mmc_host *host, struct device_type *type) |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 205 | { |
| 206 | struct mmc_card *card; |
| 207 | |
Mariusz Kozlowski | 733cb1e | 2007-08-10 14:00:47 -0700 | [diff] [blame] | 208 | card = kzalloc(sizeof(struct mmc_card), GFP_KERNEL); |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 209 | if (!card) |
| 210 | return ERR_PTR(-ENOMEM); |
| 211 | |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 212 | card->host = host; |
| 213 | |
| 214 | device_initialize(&card->dev); |
| 215 | |
| 216 | card->dev.parent = mmc_classdev(host); |
| 217 | card->dev.bus = &mmc_bus_type; |
| 218 | card->dev.release = mmc_release_card; |
Pierre Ossman | 51ec92e | 2008-03-21 23:54:50 +0100 | [diff] [blame] | 219 | card->dev.type = type; |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 220 | |
| 221 | return card; |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * Register a new MMC card with the driver model. |
| 226 | */ |
| 227 | int mmc_add_card(struct mmc_card *card) |
| 228 | { |
| 229 | int ret; |
Pierre Ossman | 109b5be | 2007-07-23 00:12:10 +0200 | [diff] [blame] | 230 | const char *type; |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 231 | |
Kay Sievers | d1b2686 | 2008-11-08 21:37:46 +0100 | [diff] [blame] | 232 | dev_set_name(&card->dev, "%s:%04x", mmc_hostname(card->host), card->rca); |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 233 | |
Pierre Ossman | 109b5be | 2007-07-23 00:12:10 +0200 | [diff] [blame] | 234 | switch (card->type) { |
| 235 | case MMC_TYPE_MMC: |
| 236 | type = "MMC"; |
| 237 | break; |
| 238 | case MMC_TYPE_SD: |
| 239 | type = "SD"; |
| 240 | if (mmc_card_blockaddr(card)) |
| 241 | type = "SDHC"; |
| 242 | break; |
Pierre Ossman | 5c4e6f1 | 2007-05-21 20:23:20 +0200 | [diff] [blame] | 243 | case MMC_TYPE_SDIO: |
| 244 | type = "SDIO"; |
| 245 | break; |
Michal Miroslaw | 7310ece8 | 2010-08-10 18:01:40 -0700 | [diff] [blame] | 246 | case MMC_TYPE_SD_COMBO: |
| 247 | type = "SD-combo"; |
| 248 | if (mmc_card_blockaddr(card)) |
| 249 | type = "SDHC-combo"; |
Pierre Ossman | 109b5be | 2007-07-23 00:12:10 +0200 | [diff] [blame] | 250 | default: |
| 251 | type = "?"; |
| 252 | break; |
| 253 | } |
| 254 | |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 255 | if (mmc_host_is_spi(card->host)) { |
| 256 | printk(KERN_INFO "%s: new %s%s card on SPI\n", |
| 257 | mmc_hostname(card->host), |
| 258 | mmc_card_highspeed(card) ? "high speed " : "", |
| 259 | type); |
| 260 | } else { |
| 261 | printk(KERN_INFO "%s: new %s%s card at address %04x\n", |
| 262 | mmc_hostname(card->host), |
| 263 | mmc_card_highspeed(card) ? "high speed " : "", |
| 264 | type, card->rca); |
| 265 | } |
Pierre Ossman | 109b5be | 2007-07-23 00:12:10 +0200 | [diff] [blame] | 266 | |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 267 | ret = device_add(&card->dev); |
| 268 | if (ret) |
| 269 | return ret; |
| 270 | |
Haavard Skinnemoen | f4b7f92 | 2008-07-24 14:18:58 +0200 | [diff] [blame] | 271 | #ifdef CONFIG_DEBUG_FS |
| 272 | mmc_add_card_debugfs(card); |
| 273 | #endif |
| 274 | |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 275 | mmc_card_set_present(card); |
| 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | /* |
| 281 | * Unregister a new MMC card with the driver model, and |
| 282 | * (eventually) free it. |
| 283 | */ |
| 284 | void mmc_remove_card(struct mmc_card *card) |
| 285 | { |
Haavard Skinnemoen | f4b7f92 | 2008-07-24 14:18:58 +0200 | [diff] [blame] | 286 | #ifdef CONFIG_DEBUG_FS |
| 287 | mmc_remove_card_debugfs(card); |
| 288 | #endif |
| 289 | |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 290 | if (mmc_card_present(card)) { |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 291 | if (mmc_host_is_spi(card->host)) { |
| 292 | printk(KERN_INFO "%s: SPI card removed\n", |
| 293 | mmc_hostname(card->host)); |
| 294 | } else { |
| 295 | printk(KERN_INFO "%s: card %04x removed\n", |
| 296 | mmc_hostname(card->host), card->rca); |
| 297 | } |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 298 | device_del(&card->dev); |
| 299 | } |
| 300 | |
| 301 | put_device(&card->dev); |
| 302 | } |
| 303 | |