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/export.h> 15 #include <linux/device.h> 16 #include <linux/err.h> 17 #include <linux/slab.h> 18 #include <linux/stat.h> 19 #include <linux/pm_runtime.h> 20 21 #include <linux/mmc/card.h> 22 #include <linux/mmc/host.h> 23 24 #include "core.h" 25 #include "sdio_cis.h" 26 #include "bus.h" 27 28 static ssize_t type_show(struct device *dev, 29 struct device_attribute *attr, char *buf) 30 { 31 struct mmc_card *card = mmc_dev_to_card(dev); 32 33 switch (card->type) { 34 case MMC_TYPE_MMC: 35 return sprintf(buf, "MMC\n"); 36 case MMC_TYPE_SD: 37 return sprintf(buf, "SD\n"); 38 case MMC_TYPE_SDIO: 39 return sprintf(buf, "SDIO\n"); 40 case MMC_TYPE_SD_COMBO: 41 return sprintf(buf, "SDcombo\n"); 42 default: 43 return -EFAULT; 44 } 45 } 46 static DEVICE_ATTR_RO(type); 47 48 static struct attribute *mmc_dev_attrs[] = { 49 &dev_attr_type.attr, 50 NULL, 51 }; 52 ATTRIBUTE_GROUPS(mmc_dev); 53 54 /* 55 * This currently matches any MMC driver to any MMC card - drivers 56 * themselves make the decision whether to drive this card in their 57 * probe method. 58 */ 59 static int mmc_bus_match(struct device *dev, struct device_driver *drv) 60 { 61 return 1; 62 } 63 64 static int 65 mmc_bus_uevent(struct device *dev, struct kobj_uevent_env *env) 66 { 67 struct mmc_card *card = mmc_dev_to_card(dev); 68 const char *type; 69 int retval = 0; 70 71 switch (card->type) { 72 case MMC_TYPE_MMC: 73 type = "MMC"; 74 break; 75 case MMC_TYPE_SD: 76 type = "SD"; 77 break; 78 case MMC_TYPE_SDIO: 79 type = "SDIO"; 80 break; 81 case MMC_TYPE_SD_COMBO: 82 type = "SDcombo"; 83 break; 84 default: 85 type = NULL; 86 } 87 88 if (type) { 89 retval = add_uevent_var(env, "MMC_TYPE=%s", type); 90 if (retval) 91 return retval; 92 } 93 94 retval = add_uevent_var(env, "MMC_NAME=%s", mmc_card_name(card)); 95 if (retval) 96 return retval; 97 98 /* 99 * Request the mmc_block device. Note: that this is a direct request 100 * for the module it carries no information as to what is inserted. 101 */ 102 retval = add_uevent_var(env, "MODALIAS=mmc:block"); 103 104 return retval; 105 } 106 107 static void mmc_bus_shutdown(struct device *dev) 108 { 109 struct mmc_card *card = mmc_dev_to_card(dev); 110 struct mmc_host *host = card->host; 111 int ret; 112 113 if (dev->driver && dev->driver->shutdown) 114 dev->driver->shutdown(dev); 115 116 if (host->bus_ops->shutdown) { 117 ret = host->bus_ops->shutdown(host); 118 if (ret) 119 pr_warn("%s: error %d during shutdown\n", 120 mmc_hostname(host), ret); 121 } 122 } 123 124 #ifdef CONFIG_PM_SLEEP 125 static int mmc_bus_suspend(struct device *dev) 126 { 127 struct mmc_card *card = mmc_dev_to_card(dev); 128 struct mmc_host *host = card->host; 129 int ret; 130 131 ret = pm_generic_suspend(dev); 132 if (ret) 133 return ret; 134 135 ret = host->bus_ops->suspend(host); 136 return ret; 137 } 138 139 static int mmc_bus_resume(struct device *dev) 140 { 141 struct mmc_card *card = mmc_dev_to_card(dev); 142 struct mmc_host *host = card->host; 143 int ret; 144 145 ret = host->bus_ops->resume(host); 146 if (ret) 147 pr_warn("%s: error %d during resume (card was removed?)\n", 148 mmc_hostname(host), ret); 149 150 ret = pm_generic_resume(dev); 151 return ret; 152 } 153 #endif 154 155 #ifdef CONFIG_PM 156 static int mmc_runtime_suspend(struct device *dev) 157 { 158 struct mmc_card *card = mmc_dev_to_card(dev); 159 struct mmc_host *host = card->host; 160 161 return host->bus_ops->runtime_suspend(host); 162 } 163 164 static int mmc_runtime_resume(struct device *dev) 165 { 166 struct mmc_card *card = mmc_dev_to_card(dev); 167 struct mmc_host *host = card->host; 168 169 return host->bus_ops->runtime_resume(host); 170 } 171 #endif /* !CONFIG_PM */ 172 173 static const struct dev_pm_ops mmc_bus_pm_ops = { 174 SET_RUNTIME_PM_OPS(mmc_runtime_suspend, mmc_runtime_resume, NULL) 175 SET_SYSTEM_SLEEP_PM_OPS(mmc_bus_suspend, mmc_bus_resume) 176 }; 177 178 static struct bus_type mmc_bus_type = { 179 .name = "mmc", 180 .dev_groups = mmc_dev_groups, 181 .match = mmc_bus_match, 182 .uevent = mmc_bus_uevent, 183 .shutdown = mmc_bus_shutdown, 184 .pm = &mmc_bus_pm_ops, 185 }; 186 187 int mmc_register_bus(void) 188 { 189 return bus_register(&mmc_bus_type); 190 } 191 192 void mmc_unregister_bus(void) 193 { 194 bus_unregister(&mmc_bus_type); 195 } 196 197 /** 198 * mmc_register_driver - register a media driver 199 * @drv: MMC media driver 200 */ 201 int mmc_register_driver(struct device_driver *drv) 202 { 203 drv->bus = &mmc_bus_type; 204 return driver_register(drv); 205 } 206 EXPORT_SYMBOL(mmc_register_driver); 207 208 /** 209 * mmc_unregister_driver - unregister a media driver 210 * @drv: MMC media driver 211 */ 212 void mmc_unregister_driver(struct device_driver *drv) 213 { 214 drv->bus = &mmc_bus_type; 215 driver_unregister(drv); 216 } 217 EXPORT_SYMBOL(mmc_unregister_driver); 218 219 static void mmc_release_card(struct device *dev) 220 { 221 struct mmc_card *card = mmc_dev_to_card(dev); 222 223 sdio_free_common_cis(card); 224 225 kfree(card->info); 226 227 kfree(card); 228 } 229 230 /* 231 * Allocate and initialise a new MMC card structure. 232 */ 233 struct mmc_card *mmc_alloc_card(struct mmc_host *host, struct device_type *type) 234 { 235 struct mmc_card *card; 236 237 card = kzalloc(sizeof(struct mmc_card), GFP_KERNEL); 238 if (!card) 239 return ERR_PTR(-ENOMEM); 240 241 card->host = host; 242 243 device_initialize(&card->dev); 244 245 card->dev.parent = mmc_classdev(host); 246 card->dev.bus = &mmc_bus_type; 247 card->dev.release = mmc_release_card; 248 card->dev.type = type; 249 250 return card; 251 } 252 253 /* 254 * Register a new MMC card with the driver model. 255 */ 256 int mmc_add_card(struct mmc_card *card) 257 { 258 int ret; 259 const char *type; 260 const char *uhs_bus_speed_mode = ""; 261 static const char *const uhs_speeds[] = { 262 [UHS_SDR12_BUS_SPEED] = "SDR12 ", 263 [UHS_SDR25_BUS_SPEED] = "SDR25 ", 264 [UHS_SDR50_BUS_SPEED] = "SDR50 ", 265 [UHS_SDR104_BUS_SPEED] = "SDR104 ", 266 [UHS_DDR50_BUS_SPEED] = "DDR50 ", 267 }; 268 269 270 dev_set_name(&card->dev, "%s:%04x", mmc_hostname(card->host), card->rca); 271 272 switch (card->type) { 273 case MMC_TYPE_MMC: 274 type = "MMC"; 275 break; 276 case MMC_TYPE_SD: 277 type = "SD"; 278 if (mmc_card_blockaddr(card)) { 279 if (mmc_card_ext_capacity(card)) 280 type = "SDXC"; 281 else 282 type = "SDHC"; 283 } 284 break; 285 case MMC_TYPE_SDIO: 286 type = "SDIO"; 287 break; 288 case MMC_TYPE_SD_COMBO: 289 type = "SD-combo"; 290 if (mmc_card_blockaddr(card)) 291 type = "SDHC-combo"; 292 break; 293 default: 294 type = "?"; 295 break; 296 } 297 298 if (mmc_card_uhs(card) && 299 (card->sd_bus_speed < ARRAY_SIZE(uhs_speeds))) 300 uhs_bus_speed_mode = uhs_speeds[card->sd_bus_speed]; 301 302 if (mmc_host_is_spi(card->host)) { 303 pr_info("%s: new %s%s%s card on SPI\n", 304 mmc_hostname(card->host), 305 mmc_card_hs(card) ? "high speed " : "", 306 mmc_card_ddr52(card) ? "DDR " : "", 307 type); 308 } else { 309 pr_info("%s: new %s%s%s%s%s card at address %04x\n", 310 mmc_hostname(card->host), 311 mmc_card_uhs(card) ? "ultra high speed " : 312 (mmc_card_hs(card) ? "high speed " : ""), 313 mmc_card_hs400(card) ? "HS400 " : 314 (mmc_card_hs200(card) ? "HS200 " : ""), 315 mmc_card_ddr52(card) ? "DDR " : "", 316 uhs_bus_speed_mode, type, card->rca); 317 } 318 319 #ifdef CONFIG_DEBUG_FS 320 mmc_add_card_debugfs(card); 321 #endif 322 mmc_init_context_info(card->host); 323 324 ret = device_add(&card->dev); 325 if (ret) 326 return ret; 327 328 mmc_card_set_present(card); 329 330 return 0; 331 } 332 333 /* 334 * Unregister a new MMC card with the driver model, and 335 * (eventually) free it. 336 */ 337 void mmc_remove_card(struct mmc_card *card) 338 { 339 #ifdef CONFIG_DEBUG_FS 340 mmc_remove_card_debugfs(card); 341 #endif 342 343 if (mmc_card_present(card)) { 344 if (mmc_host_is_spi(card->host)) { 345 pr_info("%s: SPI card removed\n", 346 mmc_hostname(card->host)); 347 } else { 348 pr_info("%s: card %04x removed\n", 349 mmc_hostname(card->host), card->rca); 350 } 351 device_del(&card->dev); 352 } 353 354 put_device(&card->dev); 355 } 356 357