blob: 6c0613d56b77e5d1e5b5670787e2a20391c790f8 [file] [log] [blame]
Pierre Ossman4101c162007-05-19 13:39:01 +02001/*
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
Paul Gortmaker3ef77af2011-07-10 12:42:00 -040014#include <linux/export.h>
Pierre Ossman4101c162007-05-19 13:39:01 +020015#include <linux/device.h>
16#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Paul Gortmaker0205a902011-07-15 12:01:27 -040018#include <linux/stat.h>
Ohad Ben-Cohen516d5cc2010-10-02 13:54:08 +020019#include <linux/pm_runtime.h>
Pierre Ossman4101c162007-05-19 13:39:01 +020020
21#include <linux/mmc/card.h>
22#include <linux/mmc/host.h>
23
Pierre Ossman4101c162007-05-19 13:39:01 +020024#include "core.h"
Pierre Ossman1a632f82007-07-30 15:15:30 +020025#include "sdio_cis.h"
Pierre Ossman4101c162007-05-19 13:39:01 +020026#include "bus.h"
27
Pierre Ossman4101c162007-05-19 13:39:01 +020028#define to_mmc_driver(d) container_of(d, struct mmc_driver, drv)
29
Greg Kroah-Hartmanf24fc572013-10-06 23:55:43 -070030static ssize_t type_show(struct device *dev,
Pierre Ossman4101c162007-05-19 13:39:01 +020031 struct device_attribute *attr, char *buf)
32{
Andy Shevchenko265cdc92010-09-17 20:32:25 -040033 struct mmc_card *card = mmc_dev_to_card(dev);
Pierre Ossman4101c162007-05-19 13:39:01 +020034
35 switch (card->type) {
36 case MMC_TYPE_MMC:
37 return sprintf(buf, "MMC\n");
38 case MMC_TYPE_SD:
39 return sprintf(buf, "SD\n");
Pierre Ossman5c4e6f12007-05-21 20:23:20 +020040 case MMC_TYPE_SDIO:
41 return sprintf(buf, "SDIO\n");
Michal Miroslaw7310ece82010-08-10 18:01:40 -070042 case MMC_TYPE_SD_COMBO:
43 return sprintf(buf, "SDcombo\n");
Pierre Ossman4101c162007-05-19 13:39:01 +020044 default:
45 return -EFAULT;
46 }
47}
Greg Kroah-Hartmanf24fc572013-10-06 23:55:43 -070048static DEVICE_ATTR_RO(type);
Pierre Ossman4101c162007-05-19 13:39:01 +020049
Greg Kroah-Hartmanf24fc572013-10-06 23:55:43 -070050static struct attribute *mmc_dev_attrs[] = {
51 &dev_attr_type.attr,
52 NULL,
Pierre Ossman4101c162007-05-19 13:39:01 +020053};
Greg Kroah-Hartmanf24fc572013-10-06 23:55:43 -070054ATTRIBUTE_GROUPS(mmc_dev);
Pierre Ossman4101c162007-05-19 13:39:01 +020055
56/*
57 * This currently matches any MMC driver to any MMC card - drivers
58 * themselves make the decision whether to drive this card in their
59 * probe method.
60 */
61static int mmc_bus_match(struct device *dev, struct device_driver *drv)
62{
63 return 1;
64}
65
66static int
Kay Sievers7eff2e72007-08-14 15:15:12 +020067mmc_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
Pierre Ossman4101c162007-05-19 13:39:01 +020068{
Andy Shevchenko265cdc92010-09-17 20:32:25 -040069 struct mmc_card *card = mmc_dev_to_card(dev);
Pierre Ossman9eb3a942007-06-17 11:18:46 +020070 const char *type;
Kay Sievers7eff2e72007-08-14 15:15:12 +020071 int retval = 0;
Pierre Ossman4101c162007-05-19 13:39:01 +020072
73 switch (card->type) {
74 case MMC_TYPE_MMC:
Pierre Ossman9eb3a942007-06-17 11:18:46 +020075 type = "MMC";
Pierre Ossman4101c162007-05-19 13:39:01 +020076 break;
77 case MMC_TYPE_SD:
Pierre Ossman9eb3a942007-06-17 11:18:46 +020078 type = "SD";
Pierre Ossman4101c162007-05-19 13:39:01 +020079 break;
Pierre Ossman5c4e6f12007-05-21 20:23:20 +020080 case MMC_TYPE_SDIO:
Pierre Ossman9eb3a942007-06-17 11:18:46 +020081 type = "SDIO";
Pierre Ossman5c4e6f12007-05-21 20:23:20 +020082 break;
Michal Miroslaw7310ece82010-08-10 18:01:40 -070083 case MMC_TYPE_SD_COMBO:
84 type = "SDcombo";
85 break;
Pierre Ossman9eb3a942007-06-17 11:18:46 +020086 default:
87 type = NULL;
Pierre Ossman4101c162007-05-19 13:39:01 +020088 }
89
Pierre Ossman9eb3a942007-06-17 11:18:46 +020090 if (type) {
Kay Sievers7eff2e72007-08-14 15:15:12 +020091 retval = add_uevent_var(env, "MMC_TYPE=%s", type);
92 if (retval)
93 return retval;
Pierre Ossman9eb3a942007-06-17 11:18:46 +020094 }
Pierre Ossman4101c162007-05-19 13:39:01 +020095
Kay Sievers7eff2e72007-08-14 15:15:12 +020096 retval = add_uevent_var(env, "MMC_NAME=%s", mmc_card_name(card));
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000097 if (retval)
98 return retval;
99
100 /*
101 * Request the mmc_block device. Note: that this is a direct request
102 * for the module it carries no information as to what is inserted.
103 */
104 retval = add_uevent_var(env, "MODALIAS=mmc:block");
Pierre Ossman4101c162007-05-19 13:39:01 +0200105
Kay Sievers7eff2e72007-08-14 15:15:12 +0200106 return retval;
Pierre Ossman4101c162007-05-19 13:39:01 +0200107}
108
109static int mmc_bus_probe(struct device *dev)
110{
111 struct mmc_driver *drv = to_mmc_driver(dev->driver);
Andy Shevchenko265cdc92010-09-17 20:32:25 -0400112 struct mmc_card *card = mmc_dev_to_card(dev);
Pierre Ossman4101c162007-05-19 13:39:01 +0200113
114 return drv->probe(card);
115}
116
117static int mmc_bus_remove(struct device *dev)
118{
119 struct mmc_driver *drv = to_mmc_driver(dev->driver);
Andy Shevchenko265cdc92010-09-17 20:32:25 -0400120 struct mmc_card *card = mmc_dev_to_card(dev);
Pierre Ossman4101c162007-05-19 13:39:01 +0200121
122 drv->remove(card);
123
124 return 0;
125}
126
Ulf Hansson76287742013-06-10 17:03:40 +0200127static void mmc_bus_shutdown(struct device *dev)
128{
129 struct mmc_driver *drv = to_mmc_driver(dev->driver);
130 struct mmc_card *card = mmc_dev_to_card(dev);
Ulf Hansson6b086bd2013-06-10 17:03:41 +0200131 struct mmc_host *host = card->host;
132 int ret;
Ulf Hansson76287742013-06-10 17:03:40 +0200133
Ulf Hansson203bb5a2013-07-02 12:53:01 +0200134 if (dev->driver && drv->shutdown)
135 drv->shutdown(card);
Ulf Hansson6b086bd2013-06-10 17:03:41 +0200136
137 if (host->bus_ops->shutdown) {
138 ret = host->bus_ops->shutdown(host);
139 if (ret)
140 pr_warn("%s: error %d during shutdown\n",
141 mmc_hostname(host), ret);
142 }
Ulf Hansson76287742013-06-10 17:03:40 +0200143}
144
Chuanxiao Dong0dd1bfe2012-04-23 15:18:02 +0800145#ifdef CONFIG_PM_SLEEP
Chuanxiao Dong32d317c2012-04-11 19:54:38 +0800146static int mmc_bus_suspend(struct device *dev)
Pierre Ossman4101c162007-05-19 13:39:01 +0200147{
148 struct mmc_driver *drv = to_mmc_driver(dev->driver);
Andy Shevchenko265cdc92010-09-17 20:32:25 -0400149 struct mmc_card *card = mmc_dev_to_card(dev);
Ulf Hansson986892c2013-06-10 17:03:39 +0200150 struct mmc_host *host = card->host;
151 int ret;
Pierre Ossman4101c162007-05-19 13:39:01 +0200152
Ulf Hansson986892c2013-06-10 17:03:39 +0200153 if (dev->driver && drv->suspend) {
Chuanxiao Dong32d317c2012-04-11 19:54:38 +0800154 ret = drv->suspend(card);
Ulf Hansson986892c2013-06-10 17:03:39 +0200155 if (ret)
156 return ret;
157 }
158
159 ret = host->bus_ops->suspend(host);
Pierre Ossman4101c162007-05-19 13:39:01 +0200160 return ret;
161}
162
163static int mmc_bus_resume(struct device *dev)
164{
165 struct mmc_driver *drv = to_mmc_driver(dev->driver);
Andy Shevchenko265cdc92010-09-17 20:32:25 -0400166 struct mmc_card *card = mmc_dev_to_card(dev);
Ulf Hansson986892c2013-06-10 17:03:39 +0200167 struct mmc_host *host = card->host;
168 int ret;
169
170 ret = host->bus_ops->resume(host);
171 if (ret)
172 pr_warn("%s: error %d during resume (card was removed?)\n",
173 mmc_hostname(host), ret);
Pierre Ossman4101c162007-05-19 13:39:01 +0200174
175 if (dev->driver && drv->resume)
176 ret = drv->resume(card);
Ulf Hansson986892c2013-06-10 17:03:39 +0200177
Pierre Ossman4101c162007-05-19 13:39:01 +0200178 return ret;
179}
Chuanxiao Dong0dd1bfe2012-04-23 15:18:02 +0800180#endif
Pierre Ossman4101c162007-05-19 13:39:01 +0200181
Rafael J. Wysocki162d6f92014-12-05 03:05:33 +0100182#ifdef CONFIG_PM
Ohad Ben-Cohen516d5cc2010-10-02 13:54:08 +0200183static int mmc_runtime_suspend(struct device *dev)
184{
185 struct mmc_card *card = mmc_dev_to_card(dev);
Ulf Hansson12d01d02013-05-02 14:02:37 +0200186 struct mmc_host *host = card->host;
Ohad Ben-Cohen516d5cc2010-10-02 13:54:08 +0200187
Ulf Hansson5601aaf2013-10-30 23:15:30 +0100188 return host->bus_ops->runtime_suspend(host);
Ohad Ben-Cohen516d5cc2010-10-02 13:54:08 +0200189}
190
191static int mmc_runtime_resume(struct device *dev)
192{
193 struct mmc_card *card = mmc_dev_to_card(dev);
Ulf Hansson12d01d02013-05-02 14:02:37 +0200194 struct mmc_host *host = card->host;
Ohad Ben-Cohen516d5cc2010-10-02 13:54:08 +0200195
Ulf Hansson5601aaf2013-10-30 23:15:30 +0100196 return host->bus_ops->runtime_resume(host);
Ohad Ben-Cohen516d5cc2010-10-02 13:54:08 +0200197}
Rafael J. Wysocki162d6f92014-12-05 03:05:33 +0100198#endif /* !CONFIG_PM */
Ohad Ben-Cohen516d5cc2010-10-02 13:54:08 +0200199
Chuanxiao Dong32d317c2012-04-11 19:54:38 +0800200static const struct dev_pm_ops mmc_bus_pm_ops = {
Ulf Hansson2e42da52014-05-28 11:44:05 +0200201 SET_RUNTIME_PM_OPS(mmc_runtime_suspend, mmc_runtime_resume, NULL)
Chuanxiao Dong32d317c2012-04-11 19:54:38 +0800202 SET_SYSTEM_SLEEP_PM_OPS(mmc_bus_suspend, mmc_bus_resume)
203};
204
Pierre Ossman4101c162007-05-19 13:39:01 +0200205static struct bus_type mmc_bus_type = {
206 .name = "mmc",
Greg Kroah-Hartmanf24fc572013-10-06 23:55:43 -0700207 .dev_groups = mmc_dev_groups,
Pierre Ossman4101c162007-05-19 13:39:01 +0200208 .match = mmc_bus_match,
209 .uevent = mmc_bus_uevent,
210 .probe = mmc_bus_probe,
211 .remove = mmc_bus_remove,
Ulf Hansson76287742013-06-10 17:03:40 +0200212 .shutdown = mmc_bus_shutdown,
Chuanxiao Dong32d317c2012-04-11 19:54:38 +0800213 .pm = &mmc_bus_pm_ops,
Pierre Ossman4101c162007-05-19 13:39:01 +0200214};
215
216int mmc_register_bus(void)
217{
218 return bus_register(&mmc_bus_type);
219}
220
221void mmc_unregister_bus(void)
222{
223 bus_unregister(&mmc_bus_type);
224}
225
226/**
227 * mmc_register_driver - register a media driver
228 * @drv: MMC media driver
229 */
230int mmc_register_driver(struct mmc_driver *drv)
231{
232 drv->drv.bus = &mmc_bus_type;
233 return driver_register(&drv->drv);
234}
235
236EXPORT_SYMBOL(mmc_register_driver);
237
238/**
239 * mmc_unregister_driver - unregister a media driver
240 * @drv: MMC media driver
241 */
242void mmc_unregister_driver(struct mmc_driver *drv)
243{
244 drv->drv.bus = &mmc_bus_type;
245 driver_unregister(&drv->drv);
246}
247
248EXPORT_SYMBOL(mmc_unregister_driver);
249
250static void mmc_release_card(struct device *dev)
251{
Andy Shevchenko265cdc92010-09-17 20:32:25 -0400252 struct mmc_card *card = mmc_dev_to_card(dev);
Pierre Ossman4101c162007-05-19 13:39:01 +0200253
Pierre Ossman1a632f82007-07-30 15:15:30 +0200254 sdio_free_common_cis(card);
255
Sachin Kamat8db580c2012-11-20 14:43:17 +0530256 kfree(card->info);
Pierre Ossman759bdc72007-09-19 18:42:16 +0200257
Pierre Ossman4101c162007-05-19 13:39:01 +0200258 kfree(card);
259}
260
261/*
262 * Allocate and initialise a new MMC card structure.
263 */
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100264struct mmc_card *mmc_alloc_card(struct mmc_host *host, struct device_type *type)
Pierre Ossman4101c162007-05-19 13:39:01 +0200265{
266 struct mmc_card *card;
267
Mariusz Kozlowski733cb1e2007-08-10 14:00:47 -0700268 card = kzalloc(sizeof(struct mmc_card), GFP_KERNEL);
Pierre Ossman4101c162007-05-19 13:39:01 +0200269 if (!card)
270 return ERR_PTR(-ENOMEM);
271
Pierre Ossman4101c162007-05-19 13:39:01 +0200272 card->host = host;
273
274 device_initialize(&card->dev);
275
276 card->dev.parent = mmc_classdev(host);
277 card->dev.bus = &mmc_bus_type;
278 card->dev.release = mmc_release_card;
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100279 card->dev.type = type;
Pierre Ossman4101c162007-05-19 13:39:01 +0200280
281 return card;
282}
283
284/*
285 * Register a new MMC card with the driver model.
286 */
287int mmc_add_card(struct mmc_card *card)
288{
289 int ret;
Pierre Ossman109b5be2007-07-23 00:12:10 +0200290 const char *type;
Subhash Jadavani6500c8e2012-03-30 12:10:18 +0530291 const char *uhs_bus_speed_mode = "";
292 static const char *const uhs_speeds[] = {
293 [UHS_SDR12_BUS_SPEED] = "SDR12 ",
294 [UHS_SDR25_BUS_SPEED] = "SDR25 ",
295 [UHS_SDR50_BUS_SPEED] = "SDR50 ",
296 [UHS_SDR104_BUS_SPEED] = "SDR104 ",
297 [UHS_DDR50_BUS_SPEED] = "DDR50 ",
298 };
299
Pierre Ossman4101c162007-05-19 13:39:01 +0200300
Kay Sieversd1b26862008-11-08 21:37:46 +0100301 dev_set_name(&card->dev, "%s:%04x", mmc_hostname(card->host), card->rca);
Pierre Ossman4101c162007-05-19 13:39:01 +0200302
Pierre Ossman109b5be2007-07-23 00:12:10 +0200303 switch (card->type) {
304 case MMC_TYPE_MMC:
305 type = "MMC";
306 break;
307 case MMC_TYPE_SD:
308 type = "SD";
Arindam Nath3a303512011-05-05 12:19:03 +0530309 if (mmc_card_blockaddr(card)) {
310 if (mmc_card_ext_capacity(card))
311 type = "SDXC";
312 else
313 type = "SDHC";
314 }
Pierre Ossman109b5be2007-07-23 00:12:10 +0200315 break;
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200316 case MMC_TYPE_SDIO:
317 type = "SDIO";
318 break;
Michal Miroslaw7310ece82010-08-10 18:01:40 -0700319 case MMC_TYPE_SD_COMBO:
320 type = "SD-combo";
321 if (mmc_card_blockaddr(card))
322 type = "SDHC-combo";
Michał Mirosław9bc21842011-04-09 08:16:47 +0200323 break;
Pierre Ossman109b5be2007-07-23 00:12:10 +0200324 default:
325 type = "?";
326 break;
327 }
328
Jackey Shen71ef1ea2013-05-17 17:17:43 +0800329 if (mmc_card_uhs(card) &&
Subhash Jadavani6500c8e2012-03-30 12:10:18 +0530330 (card->sd_bus_speed < ARRAY_SIZE(uhs_speeds)))
331 uhs_bus_speed_mode = uhs_speeds[card->sd_bus_speed];
332
David Brownellaf517152007-08-08 09:11:32 -0700333 if (mmc_host_is_spi(card->host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530334 pr_info("%s: new %s%s%s card on SPI\n",
David Brownellaf517152007-08-08 09:11:32 -0700335 mmc_hostname(card->host),
Seungwon Jeoncdc99172014-04-23 17:07:35 +0900336 mmc_card_hs(card) ? "high speed " : "",
337 mmc_card_ddr52(card) ? "DDR " : "",
David Brownellaf517152007-08-08 09:11:32 -0700338 type);
339 } else {
Subhash Jadavani6500c8e2012-03-30 12:10:18 +0530340 pr_info("%s: new %s%s%s%s%s card at address %04x\n",
David Brownellaf517152007-08-08 09:11:32 -0700341 mmc_hostname(card->host),
Philip Rakitya303c532011-11-14 19:14:38 -0800342 mmc_card_uhs(card) ? "ultra high speed " :
Seungwon Jeoncdc99172014-04-23 17:07:35 +0900343 (mmc_card_hs(card) ? "high speed " : ""),
Seungwon Jeon0a5b6432014-04-23 17:14:58 +0900344 mmc_card_hs400(card) ? "HS400 " :
Girish K Sa4924c72012-01-11 14:04:52 -0500345 (mmc_card_hs200(card) ? "HS200 " : ""),
Seungwon Jeoncdc99172014-04-23 17:07:35 +0900346 mmc_card_ddr52(card) ? "DDR " : "",
Subhash Jadavani6500c8e2012-03-30 12:10:18 +0530347 uhs_bus_speed_mode, type, card->rca);
David Brownellaf517152007-08-08 09:11:32 -0700348 }
Pierre Ossman109b5be2007-07-23 00:12:10 +0200349
Haavard Skinnemoenf4b7f922008-07-24 14:18:58 +0200350#ifdef CONFIG_DEBUG_FS
351 mmc_add_card_debugfs(card);
352#endif
Konstantin Dorfman2220eed2013-01-14 14:28:17 -0500353 mmc_init_context_info(card->host);
Haavard Skinnemoenf4b7f922008-07-24 14:18:58 +0200354
Viresh Kumar1a2727e2011-01-04 12:55:14 -0500355 ret = device_add(&card->dev);
356 if (ret)
357 return ret;
358
Pierre Ossman4101c162007-05-19 13:39:01 +0200359 mmc_card_set_present(card);
360
361 return 0;
362}
363
364/*
365 * Unregister a new MMC card with the driver model, and
366 * (eventually) free it.
367 */
368void mmc_remove_card(struct mmc_card *card)
369{
Haavard Skinnemoenf4b7f922008-07-24 14:18:58 +0200370#ifdef CONFIG_DEBUG_FS
371 mmc_remove_card_debugfs(card);
372#endif
373
Pierre Ossman4101c162007-05-19 13:39:01 +0200374 if (mmc_card_present(card)) {
David Brownellaf517152007-08-08 09:11:32 -0700375 if (mmc_host_is_spi(card->host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530376 pr_info("%s: SPI card removed\n",
David Brownellaf517152007-08-08 09:11:32 -0700377 mmc_hostname(card->host));
378 } else {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530379 pr_info("%s: card %04x removed\n",
David Brownellaf517152007-08-08 09:11:32 -0700380 mmc_hostname(card->host), card->rca);
381 }
Pierre Ossman4101c162007-05-19 13:39:01 +0200382 device_del(&card->dev);
383 }
384
385 put_device(&card->dev);
386}
387