blob: 6be49249895a21bab3c06cd4bc658f347ed99124 [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;
Pierre Ossman4101c162007-05-19 13:39:01 +0200270
Kay Sieversd1b26862008-11-08 21:37:46 +0100271 dev_set_name(&card->dev, "%s:%04x", mmc_hostname(card->host), card->rca);
Pierre Ossman4101c162007-05-19 13:39:01 +0200272
Pierre Ossman109b5be2007-07-23 00:12:10 +0200273 switch (card->type) {
274 case MMC_TYPE_MMC:
275 type = "MMC";
276 break;
277 case MMC_TYPE_SD:
278 type = "SD";
Arindam Nath3a303512011-05-05 12:19:03 +0530279 if (mmc_card_blockaddr(card)) {
280 if (mmc_card_ext_capacity(card))
281 type = "SDXC";
282 else
283 type = "SDHC";
284 }
Pierre Ossman109b5be2007-07-23 00:12:10 +0200285 break;
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200286 case MMC_TYPE_SDIO:
287 type = "SDIO";
288 break;
Michal Miroslaw7310ece82010-08-10 18:01:40 -0700289 case MMC_TYPE_SD_COMBO:
290 type = "SD-combo";
291 if (mmc_card_blockaddr(card))
292 type = "SDHC-combo";
Michał Mirosław9bc21842011-04-09 08:16:47 +0200293 break;
Pierre Ossman109b5be2007-07-23 00:12:10 +0200294 default:
295 type = "?";
296 break;
297 }
298
David Brownellaf517152007-08-08 09:11:32 -0700299 if (mmc_host_is_spi(card->host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530300 pr_info("%s: new %s%s%s card on SPI\n",
David Brownellaf517152007-08-08 09:11:32 -0700301 mmc_hostname(card->host),
302 mmc_card_highspeed(card) ? "high speed " : "",
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +0300303 mmc_card_ddr_mode(card) ? "DDR " : "",
David Brownellaf517152007-08-08 09:11:32 -0700304 type);
305 } else {
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +0300306 printk(KERN_INFO "%s: new %s%s%s card at address %04x\n",
David Brownellaf517152007-08-08 09:11:32 -0700307 mmc_hostname(card->host),
Arindam Nath3a303512011-05-05 12:19:03 +0530308 mmc_sd_card_uhs(card) ? "ultra high speed " :
309 (mmc_card_highspeed(card) ? "high speed " : ""),
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +0300310 mmc_card_ddr_mode(card) ? "DDR " : "",
David Brownellaf517152007-08-08 09:11:32 -0700311 type, card->rca);
312 }
Pierre Ossman109b5be2007-07-23 00:12:10 +0200313
Haavard Skinnemoenf4b7f922008-07-24 14:18:58 +0200314#ifdef CONFIG_DEBUG_FS
315 mmc_add_card_debugfs(card);
316#endif
317
Viresh Kumar1a2727e2011-01-04 12:55:14 -0500318 ret = device_add(&card->dev);
319 if (ret)
320 return ret;
321
Pierre Ossman4101c162007-05-19 13:39:01 +0200322 mmc_card_set_present(card);
323
324 return 0;
325}
326
327/*
328 * Unregister a new MMC card with the driver model, and
329 * (eventually) free it.
330 */
331void mmc_remove_card(struct mmc_card *card)
332{
Haavard Skinnemoenf4b7f922008-07-24 14:18:58 +0200333#ifdef CONFIG_DEBUG_FS
334 mmc_remove_card_debugfs(card);
335#endif
336
Pierre Ossman4101c162007-05-19 13:39:01 +0200337 if (mmc_card_present(card)) {
David Brownellaf517152007-08-08 09:11:32 -0700338 if (mmc_host_is_spi(card->host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530339 pr_info("%s: SPI card removed\n",
David Brownellaf517152007-08-08 09:11:32 -0700340 mmc_hostname(card->host));
341 } else {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530342 pr_info("%s: card %04x removed\n",
David Brownellaf517152007-08-08 09:11:32 -0700343 mmc_hostname(card->host), card->rca);
344 }
Pierre Ossman4101c162007-05-19 13:39:01 +0200345 device_del(&card->dev);
346 }
347
348 put_device(&card->dev);
349}
350