blob: e219c97a02a416e2439f6640093ac29de4e5d371 [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
Chuanxiao Dong0dd1bfe2012-04-23 15:18:02 +0800125#ifdef CONFIG_PM_SLEEP
Chuanxiao Dong32d317c2012-04-11 19:54:38 +0800126static int mmc_bus_suspend(struct device *dev)
Pierre Ossman4101c162007-05-19 13:39:01 +0200127{
128 struct mmc_driver *drv = to_mmc_driver(dev->driver);
Andy Shevchenko265cdc92010-09-17 20:32:25 -0400129 struct mmc_card *card = mmc_dev_to_card(dev);
Pierre Ossman4101c162007-05-19 13:39:01 +0200130 int ret = 0;
131
132 if (dev->driver && drv->suspend)
Chuanxiao Dong32d317c2012-04-11 19:54:38 +0800133 ret = drv->suspend(card);
Pierre Ossman4101c162007-05-19 13:39:01 +0200134 return ret;
135}
136
137static int mmc_bus_resume(struct device *dev)
138{
139 struct mmc_driver *drv = to_mmc_driver(dev->driver);
Andy Shevchenko265cdc92010-09-17 20:32:25 -0400140 struct mmc_card *card = mmc_dev_to_card(dev);
Pierre Ossman4101c162007-05-19 13:39:01 +0200141 int ret = 0;
142
143 if (dev->driver && drv->resume)
144 ret = drv->resume(card);
145 return ret;
146}
Chuanxiao Dong0dd1bfe2012-04-23 15:18:02 +0800147#endif
Pierre Ossman4101c162007-05-19 13:39:01 +0200148
Ohad Ben-Cohen516d5cc2010-10-02 13:54:08 +0200149#ifdef CONFIG_PM_RUNTIME
150
151static int mmc_runtime_suspend(struct device *dev)
152{
153 struct mmc_card *card = mmc_dev_to_card(dev);
154
155 return mmc_power_save_host(card->host);
156}
157
158static int mmc_runtime_resume(struct device *dev)
159{
160 struct mmc_card *card = mmc_dev_to_card(dev);
161
162 return mmc_power_restore_host(card->host);
163}
164
165static int mmc_runtime_idle(struct device *dev)
166{
167 return pm_runtime_suspend(dev);
168}
169
Ohad Ben-Cohen516d5cc2010-10-02 13:54:08 +0200170#endif /* !CONFIG_PM_RUNTIME */
171
Chuanxiao Dong32d317c2012-04-11 19:54:38 +0800172static const struct dev_pm_ops mmc_bus_pm_ops = {
173 SET_RUNTIME_PM_OPS(mmc_runtime_suspend, mmc_runtime_resume,
174 mmc_runtime_idle)
175 SET_SYSTEM_SLEEP_PM_OPS(mmc_bus_suspend, mmc_bus_resume)
176};
177
Pierre Ossman4101c162007-05-19 13:39:01 +0200178static struct bus_type mmc_bus_type = {
179 .name = "mmc",
180 .dev_attrs = mmc_dev_attrs,
181 .match = mmc_bus_match,
182 .uevent = mmc_bus_uevent,
183 .probe = mmc_bus_probe,
184 .remove = mmc_bus_remove,
Chuanxiao Dong32d317c2012-04-11 19:54:38 +0800185 .pm = &mmc_bus_pm_ops,
Pierre Ossman4101c162007-05-19 13:39:01 +0200186};
187
188int mmc_register_bus(void)
189{
190 return bus_register(&mmc_bus_type);
191}
192
193void mmc_unregister_bus(void)
194{
195 bus_unregister(&mmc_bus_type);
196}
197
198/**
199 * mmc_register_driver - register a media driver
200 * @drv: MMC media driver
201 */
202int mmc_register_driver(struct mmc_driver *drv)
203{
204 drv->drv.bus = &mmc_bus_type;
205 return driver_register(&drv->drv);
206}
207
208EXPORT_SYMBOL(mmc_register_driver);
209
210/**
211 * mmc_unregister_driver - unregister a media driver
212 * @drv: MMC media driver
213 */
214void mmc_unregister_driver(struct mmc_driver *drv)
215{
216 drv->drv.bus = &mmc_bus_type;
217 driver_unregister(&drv->drv);
218}
219
220EXPORT_SYMBOL(mmc_unregister_driver);
221
222static void mmc_release_card(struct device *dev)
223{
Andy Shevchenko265cdc92010-09-17 20:32:25 -0400224 struct mmc_card *card = mmc_dev_to_card(dev);
Pierre Ossman4101c162007-05-19 13:39:01 +0200225
Pierre Ossman1a632f82007-07-30 15:15:30 +0200226 sdio_free_common_cis(card);
227
Sachin Kamat8db580c2012-11-20 14:43:17 +0530228 kfree(card->info);
Pierre Ossman759bdc72007-09-19 18:42:16 +0200229
Pierre Ossman4101c162007-05-19 13:39:01 +0200230 kfree(card);
231}
232
233/*
234 * Allocate and initialise a new MMC card structure.
235 */
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100236struct mmc_card *mmc_alloc_card(struct mmc_host *host, struct device_type *type)
Pierre Ossman4101c162007-05-19 13:39:01 +0200237{
238 struct mmc_card *card;
239
Mariusz Kozlowski733cb1e2007-08-10 14:00:47 -0700240 card = kzalloc(sizeof(struct mmc_card), GFP_KERNEL);
Pierre Ossman4101c162007-05-19 13:39:01 +0200241 if (!card)
242 return ERR_PTR(-ENOMEM);
243
Pierre Ossman4101c162007-05-19 13:39:01 +0200244 card->host = host;
245
246 device_initialize(&card->dev);
247
248 card->dev.parent = mmc_classdev(host);
249 card->dev.bus = &mmc_bus_type;
250 card->dev.release = mmc_release_card;
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100251 card->dev.type = type;
Pierre Ossman4101c162007-05-19 13:39:01 +0200252
253 return card;
254}
255
256/*
257 * Register a new MMC card with the driver model.
258 */
259int mmc_add_card(struct mmc_card *card)
260{
261 int ret;
Pierre Ossman109b5be2007-07-23 00:12:10 +0200262 const char *type;
Subhash Jadavani6500c8e2012-03-30 12:10:18 +0530263 const char *uhs_bus_speed_mode = "";
264 static const char *const uhs_speeds[] = {
265 [UHS_SDR12_BUS_SPEED] = "SDR12 ",
266 [UHS_SDR25_BUS_SPEED] = "SDR25 ",
267 [UHS_SDR50_BUS_SPEED] = "SDR50 ",
268 [UHS_SDR104_BUS_SPEED] = "SDR104 ",
269 [UHS_DDR50_BUS_SPEED] = "DDR50 ",
270 };
271
Pierre Ossman4101c162007-05-19 13:39:01 +0200272
Kay Sieversd1b26862008-11-08 21:37:46 +0100273 dev_set_name(&card->dev, "%s:%04x", mmc_hostname(card->host), card->rca);
Pierre Ossman4101c162007-05-19 13:39:01 +0200274
Pierre Ossman109b5be2007-07-23 00:12:10 +0200275 switch (card->type) {
276 case MMC_TYPE_MMC:
277 type = "MMC";
278 break;
279 case MMC_TYPE_SD:
280 type = "SD";
Arindam Nath3a303512011-05-05 12:19:03 +0530281 if (mmc_card_blockaddr(card)) {
282 if (mmc_card_ext_capacity(card))
283 type = "SDXC";
284 else
285 type = "SDHC";
286 }
Pierre Ossman109b5be2007-07-23 00:12:10 +0200287 break;
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200288 case MMC_TYPE_SDIO:
289 type = "SDIO";
290 break;
Michal Miroslaw7310ece82010-08-10 18:01:40 -0700291 case MMC_TYPE_SD_COMBO:
292 type = "SD-combo";
293 if (mmc_card_blockaddr(card))
294 type = "SDHC-combo";
Michał Mirosław9bc21842011-04-09 08:16:47 +0200295 break;
Pierre Ossman109b5be2007-07-23 00:12:10 +0200296 default:
297 type = "?";
298 break;
299 }
300
Subhash Jadavani6500c8e2012-03-30 12:10:18 +0530301 if (mmc_sd_card_uhs(card) &&
302 (card->sd_bus_speed < ARRAY_SIZE(uhs_speeds)))
303 uhs_bus_speed_mode = uhs_speeds[card->sd_bus_speed];
304
David Brownellaf517152007-08-08 09:11:32 -0700305 if (mmc_host_is_spi(card->host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530306 pr_info("%s: new %s%s%s card on SPI\n",
David Brownellaf517152007-08-08 09:11:32 -0700307 mmc_hostname(card->host),
308 mmc_card_highspeed(card) ? "high speed " : "",
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +0300309 mmc_card_ddr_mode(card) ? "DDR " : "",
David Brownellaf517152007-08-08 09:11:32 -0700310 type);
311 } else {
Subhash Jadavani6500c8e2012-03-30 12:10:18 +0530312 pr_info("%s: new %s%s%s%s%s card at address %04x\n",
David Brownellaf517152007-08-08 09:11:32 -0700313 mmc_hostname(card->host),
Philip Rakitya303c532011-11-14 19:14:38 -0800314 mmc_card_uhs(card) ? "ultra high speed " :
Arindam Nath3a303512011-05-05 12:19:03 +0530315 (mmc_card_highspeed(card) ? "high speed " : ""),
Girish K Sa4924c72012-01-11 14:04:52 -0500316 (mmc_card_hs200(card) ? "HS200 " : ""),
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +0300317 mmc_card_ddr_mode(card) ? "DDR " : "",
Subhash Jadavani6500c8e2012-03-30 12:10:18 +0530318 uhs_bus_speed_mode, type, card->rca);
David Brownellaf517152007-08-08 09:11:32 -0700319 }
Pierre Ossman109b5be2007-07-23 00:12:10 +0200320
Haavard Skinnemoenf4b7f922008-07-24 14:18:58 +0200321#ifdef CONFIG_DEBUG_FS
322 mmc_add_card_debugfs(card);
323#endif
Konstantin Dorfman2220eed2013-01-14 14:28:17 -0500324 mmc_init_context_info(card->host);
Haavard Skinnemoenf4b7f922008-07-24 14:18:58 +0200325
Viresh Kumar1a2727e2011-01-04 12:55:14 -0500326 ret = device_add(&card->dev);
327 if (ret)
328 return ret;
329
Pierre Ossman4101c162007-05-19 13:39:01 +0200330 mmc_card_set_present(card);
331
332 return 0;
333}
334
335/*
336 * Unregister a new MMC card with the driver model, and
337 * (eventually) free it.
338 */
339void mmc_remove_card(struct mmc_card *card)
340{
Haavard Skinnemoenf4b7f922008-07-24 14:18:58 +0200341#ifdef CONFIG_DEBUG_FS
342 mmc_remove_card_debugfs(card);
343#endif
344
Pierre Ossman4101c162007-05-19 13:39:01 +0200345 if (mmc_card_present(card)) {
David Brownellaf517152007-08-08 09:11:32 -0700346 if (mmc_host_is_spi(card->host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530347 pr_info("%s: SPI card removed\n",
David Brownellaf517152007-08-08 09:11:32 -0700348 mmc_hostname(card->host));
349 } else {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530350 pr_info("%s: card %04x removed\n",
David Brownellaf517152007-08-08 09:11:32 -0700351 mmc_hostname(card->host), card->rca);
352 }
Pierre Ossman4101c162007-05-19 13:39:01 +0200353 device_del(&card->dev);
354 }
355
356 put_device(&card->dev);
357}
358