blob: 704bf66f58733a036bf79bfc4ef585f29acb7626 [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
30static ssize_t mmc_type_show(struct device *dev,
31 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}
48
49static struct device_attribute mmc_dev_attrs[] = {
Pierre Ossman51ec92e2008-03-21 23:54:50 +010050 __ATTR(type, S_IRUGO, mmc_type_show, NULL),
Pierre Ossman4101c162007-05-19 13:39:01 +020051 __ATTR_NULL,
52};
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 */
59static int mmc_bus_match(struct device *dev, struct device_driver *drv)
60{
61 return 1;
62}
63
64static int
Kay Sievers7eff2e72007-08-14 15:15:12 +020065mmc_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
Pierre Ossman4101c162007-05-19 13:39:01 +020066{
Andy Shevchenko265cdc92010-09-17 20:32:25 -040067 struct mmc_card *card = mmc_dev_to_card(dev);
Pierre Ossman9eb3a942007-06-17 11:18:46 +020068 const char *type;
Kay Sievers7eff2e72007-08-14 15:15:12 +020069 int retval = 0;
Pierre Ossman4101c162007-05-19 13:39:01 +020070
71 switch (card->type) {
72 case MMC_TYPE_MMC:
Pierre Ossman9eb3a942007-06-17 11:18:46 +020073 type = "MMC";
Pierre Ossman4101c162007-05-19 13:39:01 +020074 break;
75 case MMC_TYPE_SD:
Pierre Ossman9eb3a942007-06-17 11:18:46 +020076 type = "SD";
Pierre Ossman4101c162007-05-19 13:39:01 +020077 break;
Pierre Ossman5c4e6f12007-05-21 20:23:20 +020078 case MMC_TYPE_SDIO:
Pierre Ossman9eb3a942007-06-17 11:18:46 +020079 type = "SDIO";
Pierre Ossman5c4e6f12007-05-21 20:23:20 +020080 break;
Michal Miroslaw7310ece82010-08-10 18:01:40 -070081 case MMC_TYPE_SD_COMBO:
82 type = "SDcombo";
83 break;
Pierre Ossman9eb3a942007-06-17 11:18:46 +020084 default:
85 type = NULL;
Pierre Ossman4101c162007-05-19 13:39:01 +020086 }
87
Pierre Ossman9eb3a942007-06-17 11:18:46 +020088 if (type) {
Kay Sievers7eff2e72007-08-14 15:15:12 +020089 retval = add_uevent_var(env, "MMC_TYPE=%s", type);
90 if (retval)
91 return retval;
Pierre Ossman9eb3a942007-06-17 11:18:46 +020092 }
Pierre Ossman4101c162007-05-19 13:39:01 +020093
Kay Sievers7eff2e72007-08-14 15:15:12 +020094 retval = add_uevent_var(env, "MMC_NAME=%s", mmc_card_name(card));
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000095 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");
Pierre Ossman4101c162007-05-19 13:39:01 +0200103
Kay Sievers7eff2e72007-08-14 15:15:12 +0200104 return retval;
Pierre Ossman4101c162007-05-19 13:39:01 +0200105}
106
107static int mmc_bus_probe(struct device *dev)
108{
109 struct mmc_driver *drv = to_mmc_driver(dev->driver);
Andy Shevchenko265cdc92010-09-17 20:32:25 -0400110 struct mmc_card *card = mmc_dev_to_card(dev);
Pierre Ossman4101c162007-05-19 13:39:01 +0200111
112 return drv->probe(card);
113}
114
115static int mmc_bus_remove(struct device *dev)
116{
117 struct mmc_driver *drv = to_mmc_driver(dev->driver);
Andy Shevchenko265cdc92010-09-17 20:32:25 -0400118 struct mmc_card *card = mmc_dev_to_card(dev);
Pierre Ossman4101c162007-05-19 13:39:01 +0200119
120 drv->remove(card);
121
122 return 0;
123}
124
Ulf Hansson76287742013-06-10 17:03:40 +0200125static void mmc_bus_shutdown(struct device *dev)
126{
127 struct mmc_driver *drv = to_mmc_driver(dev->driver);
128 struct mmc_card *card = mmc_dev_to_card(dev);
Ulf Hansson6b086bd2013-06-10 17:03:41 +0200129 struct mmc_host *host = card->host;
130 int ret;
Ulf Hansson76287742013-06-10 17:03:40 +0200131
Ulf Hansson203bb5a2013-07-02 12:53:01 +0200132 if (dev->driver && drv->shutdown)
133 drv->shutdown(card);
Ulf Hansson6b086bd2013-06-10 17:03:41 +0200134
135 if (host->bus_ops->shutdown) {
136 ret = host->bus_ops->shutdown(host);
137 if (ret)
138 pr_warn("%s: error %d during shutdown\n",
139 mmc_hostname(host), ret);
140 }
Ulf Hansson76287742013-06-10 17:03:40 +0200141}
142
Chuanxiao Dong0dd1bfe2012-04-23 15:18:02 +0800143#ifdef CONFIG_PM_SLEEP
Chuanxiao Dong32d317c2012-04-11 19:54:38 +0800144static int mmc_bus_suspend(struct device *dev)
Pierre Ossman4101c162007-05-19 13:39:01 +0200145{
146 struct mmc_driver *drv = to_mmc_driver(dev->driver);
Andy Shevchenko265cdc92010-09-17 20:32:25 -0400147 struct mmc_card *card = mmc_dev_to_card(dev);
Ulf Hansson986892c2013-06-10 17:03:39 +0200148 struct mmc_host *host = card->host;
149 int ret;
Pierre Ossman4101c162007-05-19 13:39:01 +0200150
Ulf Hansson986892c2013-06-10 17:03:39 +0200151 if (dev->driver && drv->suspend) {
Chuanxiao Dong32d317c2012-04-11 19:54:38 +0800152 ret = drv->suspend(card);
Ulf Hansson986892c2013-06-10 17:03:39 +0200153 if (ret)
154 return ret;
155 }
156
157 ret = host->bus_ops->suspend(host);
Pierre Ossman4101c162007-05-19 13:39:01 +0200158 return ret;
159}
160
161static int mmc_bus_resume(struct device *dev)
162{
163 struct mmc_driver *drv = to_mmc_driver(dev->driver);
Andy Shevchenko265cdc92010-09-17 20:32:25 -0400164 struct mmc_card *card = mmc_dev_to_card(dev);
Ulf Hansson986892c2013-06-10 17:03:39 +0200165 struct mmc_host *host = card->host;
166 int ret;
167
168 ret = host->bus_ops->resume(host);
169 if (ret)
170 pr_warn("%s: error %d during resume (card was removed?)\n",
171 mmc_hostname(host), ret);
Pierre Ossman4101c162007-05-19 13:39:01 +0200172
173 if (dev->driver && drv->resume)
174 ret = drv->resume(card);
Ulf Hansson986892c2013-06-10 17:03:39 +0200175
Pierre Ossman4101c162007-05-19 13:39:01 +0200176 return ret;
177}
Chuanxiao Dong0dd1bfe2012-04-23 15:18:02 +0800178#endif
Pierre Ossman4101c162007-05-19 13:39:01 +0200179
Ohad Ben-Cohen516d5cc2010-10-02 13:54:08 +0200180#ifdef CONFIG_PM_RUNTIME
181
182static int mmc_runtime_suspend(struct device *dev)
183{
184 struct mmc_card *card = mmc_dev_to_card(dev);
Ulf Hansson12d01d02013-05-02 14:02:37 +0200185 struct mmc_host *host = card->host;
186 int ret = 0;
Ohad Ben-Cohen516d5cc2010-10-02 13:54:08 +0200187
Ulf Hansson12d01d02013-05-02 14:02:37 +0200188 if (host->bus_ops->runtime_suspend)
189 ret = host->bus_ops->runtime_suspend(host);
190
191 return ret;
Ohad Ben-Cohen516d5cc2010-10-02 13:54:08 +0200192}
193
194static int mmc_runtime_resume(struct device *dev)
195{
196 struct mmc_card *card = mmc_dev_to_card(dev);
Ulf Hansson12d01d02013-05-02 14:02:37 +0200197 struct mmc_host *host = card->host;
198 int ret = 0;
Ohad Ben-Cohen516d5cc2010-10-02 13:54:08 +0200199
Ulf Hansson12d01d02013-05-02 14:02:37 +0200200 if (host->bus_ops->runtime_resume)
201 ret = host->bus_ops->runtime_resume(host);
202
203 return ret;
Ohad Ben-Cohen516d5cc2010-10-02 13:54:08 +0200204}
205
206static int mmc_runtime_idle(struct device *dev)
207{
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +0200208 return 0;
Ohad Ben-Cohen516d5cc2010-10-02 13:54:08 +0200209}
210
Ohad Ben-Cohen516d5cc2010-10-02 13:54:08 +0200211#endif /* !CONFIG_PM_RUNTIME */
212
Chuanxiao Dong32d317c2012-04-11 19:54:38 +0800213static const struct dev_pm_ops mmc_bus_pm_ops = {
214 SET_RUNTIME_PM_OPS(mmc_runtime_suspend, mmc_runtime_resume,
215 mmc_runtime_idle)
216 SET_SYSTEM_SLEEP_PM_OPS(mmc_bus_suspend, mmc_bus_resume)
217};
218
Pierre Ossman4101c162007-05-19 13:39:01 +0200219static struct bus_type mmc_bus_type = {
220 .name = "mmc",
221 .dev_attrs = mmc_dev_attrs,
222 .match = mmc_bus_match,
223 .uevent = mmc_bus_uevent,
224 .probe = mmc_bus_probe,
225 .remove = mmc_bus_remove,
Ulf Hansson76287742013-06-10 17:03:40 +0200226 .shutdown = mmc_bus_shutdown,
Chuanxiao Dong32d317c2012-04-11 19:54:38 +0800227 .pm = &mmc_bus_pm_ops,
Pierre Ossman4101c162007-05-19 13:39:01 +0200228};
229
230int mmc_register_bus(void)
231{
232 return bus_register(&mmc_bus_type);
233}
234
235void mmc_unregister_bus(void)
236{
237 bus_unregister(&mmc_bus_type);
238}
239
240/**
241 * mmc_register_driver - register a media driver
242 * @drv: MMC media driver
243 */
244int mmc_register_driver(struct mmc_driver *drv)
245{
246 drv->drv.bus = &mmc_bus_type;
247 return driver_register(&drv->drv);
248}
249
250EXPORT_SYMBOL(mmc_register_driver);
251
252/**
253 * mmc_unregister_driver - unregister a media driver
254 * @drv: MMC media driver
255 */
256void mmc_unregister_driver(struct mmc_driver *drv)
257{
258 drv->drv.bus = &mmc_bus_type;
259 driver_unregister(&drv->drv);
260}
261
262EXPORT_SYMBOL(mmc_unregister_driver);
263
264static void mmc_release_card(struct device *dev)
265{
Andy Shevchenko265cdc92010-09-17 20:32:25 -0400266 struct mmc_card *card = mmc_dev_to_card(dev);
Pierre Ossman4101c162007-05-19 13:39:01 +0200267
Pierre Ossman1a632f82007-07-30 15:15:30 +0200268 sdio_free_common_cis(card);
269
Sachin Kamat8db580c2012-11-20 14:43:17 +0530270 kfree(card->info);
Pierre Ossman759bdc72007-09-19 18:42:16 +0200271
Pierre Ossman4101c162007-05-19 13:39:01 +0200272 kfree(card);
273}
274
275/*
276 * Allocate and initialise a new MMC card structure.
277 */
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100278struct mmc_card *mmc_alloc_card(struct mmc_host *host, struct device_type *type)
Pierre Ossman4101c162007-05-19 13:39:01 +0200279{
280 struct mmc_card *card;
281
Mariusz Kozlowski733cb1e2007-08-10 14:00:47 -0700282 card = kzalloc(sizeof(struct mmc_card), GFP_KERNEL);
Pierre Ossman4101c162007-05-19 13:39:01 +0200283 if (!card)
284 return ERR_PTR(-ENOMEM);
285
Pierre Ossman4101c162007-05-19 13:39:01 +0200286 card->host = host;
287
288 device_initialize(&card->dev);
289
290 card->dev.parent = mmc_classdev(host);
291 card->dev.bus = &mmc_bus_type;
292 card->dev.release = mmc_release_card;
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100293 card->dev.type = type;
Pierre Ossman4101c162007-05-19 13:39:01 +0200294
295 return card;
296}
297
298/*
299 * Register a new MMC card with the driver model.
300 */
301int mmc_add_card(struct mmc_card *card)
302{
303 int ret;
Pierre Ossman109b5be2007-07-23 00:12:10 +0200304 const char *type;
Subhash Jadavani6500c8e2012-03-30 12:10:18 +0530305 const char *uhs_bus_speed_mode = "";
306 static const char *const uhs_speeds[] = {
307 [UHS_SDR12_BUS_SPEED] = "SDR12 ",
308 [UHS_SDR25_BUS_SPEED] = "SDR25 ",
309 [UHS_SDR50_BUS_SPEED] = "SDR50 ",
310 [UHS_SDR104_BUS_SPEED] = "SDR104 ",
311 [UHS_DDR50_BUS_SPEED] = "DDR50 ",
312 };
313
Pierre Ossman4101c162007-05-19 13:39:01 +0200314
Kay Sieversd1b26862008-11-08 21:37:46 +0100315 dev_set_name(&card->dev, "%s:%04x", mmc_hostname(card->host), card->rca);
Pierre Ossman4101c162007-05-19 13:39:01 +0200316
Pierre Ossman109b5be2007-07-23 00:12:10 +0200317 switch (card->type) {
318 case MMC_TYPE_MMC:
319 type = "MMC";
320 break;
321 case MMC_TYPE_SD:
322 type = "SD";
Arindam Nath3a303512011-05-05 12:19:03 +0530323 if (mmc_card_blockaddr(card)) {
324 if (mmc_card_ext_capacity(card))
325 type = "SDXC";
326 else
327 type = "SDHC";
328 }
Pierre Ossman109b5be2007-07-23 00:12:10 +0200329 break;
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200330 case MMC_TYPE_SDIO:
331 type = "SDIO";
332 break;
Michal Miroslaw7310ece82010-08-10 18:01:40 -0700333 case MMC_TYPE_SD_COMBO:
334 type = "SD-combo";
335 if (mmc_card_blockaddr(card))
336 type = "SDHC-combo";
Michał Mirosław9bc21842011-04-09 08:16:47 +0200337 break;
Pierre Ossman109b5be2007-07-23 00:12:10 +0200338 default:
339 type = "?";
340 break;
341 }
342
Subhash Jadavani6500c8e2012-03-30 12:10:18 +0530343 if (mmc_sd_card_uhs(card) &&
344 (card->sd_bus_speed < ARRAY_SIZE(uhs_speeds)))
345 uhs_bus_speed_mode = uhs_speeds[card->sd_bus_speed];
346
David Brownellaf517152007-08-08 09:11:32 -0700347 if (mmc_host_is_spi(card->host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530348 pr_info("%s: new %s%s%s card on SPI\n",
David Brownellaf517152007-08-08 09:11:32 -0700349 mmc_hostname(card->host),
350 mmc_card_highspeed(card) ? "high speed " : "",
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +0300351 mmc_card_ddr_mode(card) ? "DDR " : "",
David Brownellaf517152007-08-08 09:11:32 -0700352 type);
353 } else {
Subhash Jadavani6500c8e2012-03-30 12:10:18 +0530354 pr_info("%s: new %s%s%s%s%s card at address %04x\n",
David Brownellaf517152007-08-08 09:11:32 -0700355 mmc_hostname(card->host),
Philip Rakitya303c532011-11-14 19:14:38 -0800356 mmc_card_uhs(card) ? "ultra high speed " :
Arindam Nath3a303512011-05-05 12:19:03 +0530357 (mmc_card_highspeed(card) ? "high speed " : ""),
Girish K Sa4924c72012-01-11 14:04:52 -0500358 (mmc_card_hs200(card) ? "HS200 " : ""),
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +0300359 mmc_card_ddr_mode(card) ? "DDR " : "",
Subhash Jadavani6500c8e2012-03-30 12:10:18 +0530360 uhs_bus_speed_mode, type, card->rca);
David Brownellaf517152007-08-08 09:11:32 -0700361 }
Pierre Ossman109b5be2007-07-23 00:12:10 +0200362
Haavard Skinnemoenf4b7f922008-07-24 14:18:58 +0200363#ifdef CONFIG_DEBUG_FS
364 mmc_add_card_debugfs(card);
365#endif
Konstantin Dorfman2220eed2013-01-14 14:28:17 -0500366 mmc_init_context_info(card->host);
Haavard Skinnemoenf4b7f922008-07-24 14:18:58 +0200367
Viresh Kumar1a2727e2011-01-04 12:55:14 -0500368 ret = device_add(&card->dev);
369 if (ret)
370 return ret;
371
Pierre Ossman4101c162007-05-19 13:39:01 +0200372 mmc_card_set_present(card);
373
374 return 0;
375}
376
377/*
378 * Unregister a new MMC card with the driver model, and
379 * (eventually) free it.
380 */
381void mmc_remove_card(struct mmc_card *card)
382{
Haavard Skinnemoenf4b7f922008-07-24 14:18:58 +0200383#ifdef CONFIG_DEBUG_FS
384 mmc_remove_card_debugfs(card);
385#endif
386
Pierre Ossman4101c162007-05-19 13:39:01 +0200387 if (mmc_card_present(card)) {
David Brownellaf517152007-08-08 09:11:32 -0700388 if (mmc_host_is_spi(card->host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530389 pr_info("%s: SPI card removed\n",
David Brownellaf517152007-08-08 09:11:32 -0700390 mmc_hostname(card->host));
391 } else {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530392 pr_info("%s: card %04x removed\n",
David Brownellaf517152007-08-08 09:11:32 -0700393 mmc_hostname(card->host), card->rca);
394 }
Pierre Ossman4101c162007-05-19 13:39:01 +0200395 device_del(&card->dev);
396 }
397
398 put_device(&card->dev);
399}
400