blob: 3f606068d552ee738afe82516fff71bca47fc79d [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
125static int mmc_bus_suspend(struct device *dev, pm_message_t state)
126{
127 struct mmc_driver *drv = to_mmc_driver(dev->driver);
Andy Shevchenko265cdc92010-09-17 20:32:25 -0400128 struct mmc_card *card = mmc_dev_to_card(dev);
Pierre Ossman4101c162007-05-19 13:39:01 +0200129 int ret = 0;
130
131 if (dev->driver && drv->suspend)
132 ret = drv->suspend(card, state);
133 return ret;
134}
135
136static int mmc_bus_resume(struct device *dev)
137{
138 struct mmc_driver *drv = to_mmc_driver(dev->driver);
Andy Shevchenko265cdc92010-09-17 20:32:25 -0400139 struct mmc_card *card = mmc_dev_to_card(dev);
Pierre Ossman4101c162007-05-19 13:39:01 +0200140 int ret = 0;
141
142 if (dev->driver && drv->resume)
143 ret = drv->resume(card);
144 return ret;
145}
146
Ohad Ben-Cohen516d5cc2010-10-02 13:54:08 +0200147#ifdef CONFIG_PM_RUNTIME
148
149static int mmc_runtime_suspend(struct device *dev)
150{
151 struct mmc_card *card = mmc_dev_to_card(dev);
152
153 return mmc_power_save_host(card->host);
154}
155
156static int mmc_runtime_resume(struct device *dev)
157{
158 struct mmc_card *card = mmc_dev_to_card(dev);
159
160 return mmc_power_restore_host(card->host);
161}
162
163static int mmc_runtime_idle(struct device *dev)
164{
165 return pm_runtime_suspend(dev);
166}
167
168static const struct dev_pm_ops mmc_bus_pm_ops = {
169 .runtime_suspend = mmc_runtime_suspend,
170 .runtime_resume = mmc_runtime_resume,
171 .runtime_idle = mmc_runtime_idle,
172};
173
174#define MMC_PM_OPS_PTR (&mmc_bus_pm_ops)
175
176#else /* !CONFIG_PM_RUNTIME */
177
178#define MMC_PM_OPS_PTR NULL
179
180#endif /* !CONFIG_PM_RUNTIME */
181
Pierre Ossman4101c162007-05-19 13:39:01 +0200182static struct bus_type mmc_bus_type = {
183 .name = "mmc",
184 .dev_attrs = mmc_dev_attrs,
185 .match = mmc_bus_match,
186 .uevent = mmc_bus_uevent,
187 .probe = mmc_bus_probe,
188 .remove = mmc_bus_remove,
189 .suspend = mmc_bus_suspend,
190 .resume = mmc_bus_resume,
Ohad Ben-Cohen516d5cc2010-10-02 13:54:08 +0200191 .pm = MMC_PM_OPS_PTR,
Pierre Ossman4101c162007-05-19 13:39:01 +0200192};
193
194int mmc_register_bus(void)
195{
196 return bus_register(&mmc_bus_type);
197}
198
199void mmc_unregister_bus(void)
200{
201 bus_unregister(&mmc_bus_type);
202}
203
204/**
205 * mmc_register_driver - register a media driver
206 * @drv: MMC media driver
207 */
208int mmc_register_driver(struct mmc_driver *drv)
209{
210 drv->drv.bus = &mmc_bus_type;
211 return driver_register(&drv->drv);
212}
213
214EXPORT_SYMBOL(mmc_register_driver);
215
216/**
217 * mmc_unregister_driver - unregister a media driver
218 * @drv: MMC media driver
219 */
220void mmc_unregister_driver(struct mmc_driver *drv)
221{
222 drv->drv.bus = &mmc_bus_type;
223 driver_unregister(&drv->drv);
224}
225
226EXPORT_SYMBOL(mmc_unregister_driver);
227
228static void mmc_release_card(struct device *dev)
229{
Andy Shevchenko265cdc92010-09-17 20:32:25 -0400230 struct mmc_card *card = mmc_dev_to_card(dev);
Pierre Ossman4101c162007-05-19 13:39:01 +0200231
Pierre Ossman1a632f82007-07-30 15:15:30 +0200232 sdio_free_common_cis(card);
233
Pierre Ossman759bdc72007-09-19 18:42:16 +0200234 if (card->info)
235 kfree(card->info);
236
Pierre Ossman4101c162007-05-19 13:39:01 +0200237 kfree(card);
238}
239
240/*
241 * Allocate and initialise a new MMC card structure.
242 */
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100243struct mmc_card *mmc_alloc_card(struct mmc_host *host, struct device_type *type)
Pierre Ossman4101c162007-05-19 13:39:01 +0200244{
245 struct mmc_card *card;
246
Mariusz Kozlowski733cb1e2007-08-10 14:00:47 -0700247 card = kzalloc(sizeof(struct mmc_card), GFP_KERNEL);
Pierre Ossman4101c162007-05-19 13:39:01 +0200248 if (!card)
249 return ERR_PTR(-ENOMEM);
250
Pierre Ossman4101c162007-05-19 13:39:01 +0200251 card->host = host;
252
253 device_initialize(&card->dev);
254
255 card->dev.parent = mmc_classdev(host);
256 card->dev.bus = &mmc_bus_type;
257 card->dev.release = mmc_release_card;
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100258 card->dev.type = type;
Pierre Ossman4101c162007-05-19 13:39:01 +0200259
260 return card;
261}
262
263/*
264 * Register a new MMC card with the driver model.
265 */
266int mmc_add_card(struct mmc_card *card)
267{
268 int ret;
Pierre Ossman109b5be2007-07-23 00:12:10 +0200269 const char *type;
Subhash Jadavani6500c8e2012-03-30 12:10:18 +0530270 const char *uhs_bus_speed_mode = "";
271 static const char *const uhs_speeds[] = {
272 [UHS_SDR12_BUS_SPEED] = "SDR12 ",
273 [UHS_SDR25_BUS_SPEED] = "SDR25 ",
274 [UHS_SDR50_BUS_SPEED] = "SDR50 ",
275 [UHS_SDR104_BUS_SPEED] = "SDR104 ",
276 [UHS_DDR50_BUS_SPEED] = "DDR50 ",
277 };
278
Pierre Ossman4101c162007-05-19 13:39:01 +0200279
Kay Sieversd1b26862008-11-08 21:37:46 +0100280 dev_set_name(&card->dev, "%s:%04x", mmc_hostname(card->host), card->rca);
Pierre Ossman4101c162007-05-19 13:39:01 +0200281
Pierre Ossman109b5be2007-07-23 00:12:10 +0200282 switch (card->type) {
283 case MMC_TYPE_MMC:
284 type = "MMC";
285 break;
286 case MMC_TYPE_SD:
287 type = "SD";
Arindam Nath3a303512011-05-05 12:19:03 +0530288 if (mmc_card_blockaddr(card)) {
289 if (mmc_card_ext_capacity(card))
290 type = "SDXC";
291 else
292 type = "SDHC";
293 }
Pierre Ossman109b5be2007-07-23 00:12:10 +0200294 break;
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200295 case MMC_TYPE_SDIO:
296 type = "SDIO";
297 break;
Michal Miroslaw7310ece82010-08-10 18:01:40 -0700298 case MMC_TYPE_SD_COMBO:
299 type = "SD-combo";
300 if (mmc_card_blockaddr(card))
301 type = "SDHC-combo";
Michał Mirosław9bc218482011-04-09 08:16:47 +0200302 break;
Pierre Ossman109b5be2007-07-23 00:12:10 +0200303 default:
304 type = "?";
305 break;
306 }
307
Subhash Jadavani6500c8e2012-03-30 12:10:18 +0530308 if (mmc_sd_card_uhs(card) &&
309 (card->sd_bus_speed < ARRAY_SIZE(uhs_speeds)))
310 uhs_bus_speed_mode = uhs_speeds[card->sd_bus_speed];
311
David Brownellaf517152007-08-08 09:11:32 -0700312 if (mmc_host_is_spi(card->host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530313 pr_info("%s: new %s%s%s card on SPI\n",
David Brownellaf517152007-08-08 09:11:32 -0700314 mmc_hostname(card->host),
315 mmc_card_highspeed(card) ? "high speed " : "",
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +0300316 mmc_card_ddr_mode(card) ? "DDR " : "",
David Brownellaf517152007-08-08 09:11:32 -0700317 type);
318 } else {
Subhash Jadavani6500c8e2012-03-30 12:10:18 +0530319 pr_info("%s: new %s%s%s%s%s card at address %04x\n",
David Brownellaf517152007-08-08 09:11:32 -0700320 mmc_hostname(card->host),
Philip Rakitya303c532011-11-14 19:14:38 -0800321 mmc_card_uhs(card) ? "ultra high speed " :
Arindam Nath3a303512011-05-05 12:19:03 +0530322 (mmc_card_highspeed(card) ? "high speed " : ""),
Girish K Sa4924c72012-01-11 14:04:52 -0500323 (mmc_card_hs200(card) ? "HS200 " : ""),
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +0300324 mmc_card_ddr_mode(card) ? "DDR " : "",
Subhash Jadavani6500c8e2012-03-30 12:10:18 +0530325 uhs_bus_speed_mode, type, card->rca);
David Brownellaf517152007-08-08 09:11:32 -0700326 }
Pierre Ossman109b5be2007-07-23 00:12:10 +0200327
Haavard Skinnemoenf4b7f922008-07-24 14:18:58 +0200328#ifdef CONFIG_DEBUG_FS
329 mmc_add_card_debugfs(card);
330#endif
331
Viresh Kumar1a2727e2011-01-04 12:55:14 -0500332 ret = device_add(&card->dev);
333 if (ret)
334 return ret;
335
Pierre Ossman4101c162007-05-19 13:39:01 +0200336 mmc_card_set_present(card);
337
338 return 0;
339}
340
341/*
342 * Unregister a new MMC card with the driver model, and
343 * (eventually) free it.
344 */
345void mmc_remove_card(struct mmc_card *card)
346{
Haavard Skinnemoenf4b7f922008-07-24 14:18:58 +0200347#ifdef CONFIG_DEBUG_FS
348 mmc_remove_card_debugfs(card);
349#endif
350
Pierre Ossman4101c162007-05-19 13:39:01 +0200351 if (mmc_card_present(card)) {
David Brownellaf517152007-08-08 09:11:32 -0700352 if (mmc_host_is_spi(card->host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530353 pr_info("%s: SPI card removed\n",
David Brownellaf517152007-08-08 09:11:32 -0700354 mmc_hostname(card->host));
355 } else {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530356 pr_info("%s: card %04x removed\n",
David Brownellaf517152007-08-08 09:11:32 -0700357 mmc_hostname(card->host), card->rca);
358 }
Pierre Ossman4101c162007-05-19 13:39:01 +0200359 device_del(&card->dev);
360 }
361
362 put_device(&card->dev);
363}
364