bus.c 7.0 KB
Newer Older
P
Pierre Ossman 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 *  linux/drivers/mmc/core/bus.c
 *
 *  Copyright (C) 2003 Russell King, All Rights Reserved.
 *  Copyright (C) 2007 Pierre Ossman
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 *  MMC card bus driver model
 */

14
#include <linux/export.h>
P
Pierre Ossman 已提交
15 16
#include <linux/device.h>
#include <linux/err.h>
17
#include <linux/slab.h>
O
Ohad Ben-Cohen 已提交
18
#include <linux/pm_runtime.h>
P
Pierre Ossman 已提交
19 20 21 22 23

#include <linux/mmc/card.h>
#include <linux/mmc/host.h>

#include "core.h"
24
#include "sdio_cis.h"
P
Pierre Ossman 已提交
25 26 27 28 29 30 31
#include "bus.h"

#define to_mmc_driver(d)	container_of(d, struct mmc_driver, drv)

static ssize_t mmc_type_show(struct device *dev,
	struct device_attribute *attr, char *buf)
{
32
	struct mmc_card *card = mmc_dev_to_card(dev);
P
Pierre Ossman 已提交
33 34 35 36 37 38

	switch (card->type) {
	case MMC_TYPE_MMC:
		return sprintf(buf, "MMC\n");
	case MMC_TYPE_SD:
		return sprintf(buf, "SD\n");
P
Pierre Ossman 已提交
39 40
	case MMC_TYPE_SDIO:
		return sprintf(buf, "SDIO\n");
41 42
	case MMC_TYPE_SD_COMBO:
		return sprintf(buf, "SDcombo\n");
P
Pierre Ossman 已提交
43 44 45 46 47 48
	default:
		return -EFAULT;
	}
}

static struct device_attribute mmc_dev_attrs[] = {
49
	__ATTR(type, S_IRUGO, mmc_type_show, NULL),
P
Pierre Ossman 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63
	__ATTR_NULL,
};

/*
 * This currently matches any MMC driver to any MMC card - drivers
 * themselves make the decision whether to drive this card in their
 * probe method.
 */
static int mmc_bus_match(struct device *dev, struct device_driver *drv)
{
	return 1;
}

static int
64
mmc_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
P
Pierre Ossman 已提交
65
{
66
	struct mmc_card *card = mmc_dev_to_card(dev);
67
	const char *type;
68
	int retval = 0;
P
Pierre Ossman 已提交
69 70 71

	switch (card->type) {
	case MMC_TYPE_MMC:
72
		type = "MMC";
P
Pierre Ossman 已提交
73 74
		break;
	case MMC_TYPE_SD:
75
		type = "SD";
P
Pierre Ossman 已提交
76
		break;
P
Pierre Ossman 已提交
77
	case MMC_TYPE_SDIO:
78
		type = "SDIO";
P
Pierre Ossman 已提交
79
		break;
80 81 82
	case MMC_TYPE_SD_COMBO:
		type = "SDcombo";
		break;
83 84
	default:
		type = NULL;
P
Pierre Ossman 已提交
85 86
	}

87
	if (type) {
88 89 90
		retval = add_uevent_var(env, "MMC_TYPE=%s", type);
		if (retval)
			return retval;
91
	}
P
Pierre Ossman 已提交
92

93
	retval = add_uevent_var(env, "MMC_NAME=%s", mmc_card_name(card));
94 95 96 97 98 99 100 101
	if (retval)
		return retval;

	/*
	 * Request the mmc_block device.  Note: that this is a direct request
	 * for the module it carries no information as to what is inserted.
	 */
	retval = add_uevent_var(env, "MODALIAS=mmc:block");
P
Pierre Ossman 已提交
102

103
	return retval;
P
Pierre Ossman 已提交
104 105 106 107 108
}

static int mmc_bus_probe(struct device *dev)
{
	struct mmc_driver *drv = to_mmc_driver(dev->driver);
109
	struct mmc_card *card = mmc_dev_to_card(dev);
P
Pierre Ossman 已提交
110 111 112 113 114 115 116

	return drv->probe(card);
}

static int mmc_bus_remove(struct device *dev)
{
	struct mmc_driver *drv = to_mmc_driver(dev->driver);
117
	struct mmc_card *card = mmc_dev_to_card(dev);
P
Pierre Ossman 已提交
118 119 120 121 122 123 124 125 126

	drv->remove(card);

	return 0;
}

static int mmc_bus_suspend(struct device *dev, pm_message_t state)
{
	struct mmc_driver *drv = to_mmc_driver(dev->driver);
127
	struct mmc_card *card = mmc_dev_to_card(dev);
P
Pierre Ossman 已提交
128 129 130 131 132 133 134 135 136 137
	int ret = 0;

	if (dev->driver && drv->suspend)
		ret = drv->suspend(card, state);
	return ret;
}

static int mmc_bus_resume(struct device *dev)
{
	struct mmc_driver *drv = to_mmc_driver(dev->driver);
138
	struct mmc_card *card = mmc_dev_to_card(dev);
P
Pierre Ossman 已提交
139 140 141 142 143 144 145
	int ret = 0;

	if (dev->driver && drv->resume)
		ret = drv->resume(card);
	return ret;
}

O
Ohad Ben-Cohen 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
#ifdef CONFIG_PM_RUNTIME

static int mmc_runtime_suspend(struct device *dev)
{
	struct mmc_card *card = mmc_dev_to_card(dev);

	return mmc_power_save_host(card->host);
}

static int mmc_runtime_resume(struct device *dev)
{
	struct mmc_card *card = mmc_dev_to_card(dev);

	return mmc_power_restore_host(card->host);
}

static int mmc_runtime_idle(struct device *dev)
{
	return pm_runtime_suspend(dev);
}

static const struct dev_pm_ops mmc_bus_pm_ops = {
	.runtime_suspend	= mmc_runtime_suspend,
	.runtime_resume		= mmc_runtime_resume,
	.runtime_idle		= mmc_runtime_idle,
};

#define MMC_PM_OPS_PTR	(&mmc_bus_pm_ops)

#else /* !CONFIG_PM_RUNTIME */

#define MMC_PM_OPS_PTR	NULL

#endif /* !CONFIG_PM_RUNTIME */

P
Pierre Ossman 已提交
181 182 183 184 185 186 187 188 189
static struct bus_type mmc_bus_type = {
	.name		= "mmc",
	.dev_attrs	= mmc_dev_attrs,
	.match		= mmc_bus_match,
	.uevent		= mmc_bus_uevent,
	.probe		= mmc_bus_probe,
	.remove		= mmc_bus_remove,
	.suspend	= mmc_bus_suspend,
	.resume		= mmc_bus_resume,
O
Ohad Ben-Cohen 已提交
190
	.pm		= MMC_PM_OPS_PTR,
P
Pierre Ossman 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
};

int mmc_register_bus(void)
{
	return bus_register(&mmc_bus_type);
}

void mmc_unregister_bus(void)
{
	bus_unregister(&mmc_bus_type);
}

/**
 *	mmc_register_driver - register a media driver
 *	@drv: MMC media driver
 */
int mmc_register_driver(struct mmc_driver *drv)
{
	drv->drv.bus = &mmc_bus_type;
	return driver_register(&drv->drv);
}

EXPORT_SYMBOL(mmc_register_driver);

/**
 *	mmc_unregister_driver - unregister a media driver
 *	@drv: MMC media driver
 */
void mmc_unregister_driver(struct mmc_driver *drv)
{
	drv->drv.bus = &mmc_bus_type;
	driver_unregister(&drv->drv);
}

EXPORT_SYMBOL(mmc_unregister_driver);

static void mmc_release_card(struct device *dev)
{
229
	struct mmc_card *card = mmc_dev_to_card(dev);
P
Pierre Ossman 已提交
230

231 232
	sdio_free_common_cis(card);

P
Pierre Ossman 已提交
233 234 235
	if (card->info)
		kfree(card->info);

P
Pierre Ossman 已提交
236 237 238 239 240 241
	kfree(card);
}

/*
 * Allocate and initialise a new MMC card structure.
 */
242
struct mmc_card *mmc_alloc_card(struct mmc_host *host, struct device_type *type)
P
Pierre Ossman 已提交
243 244 245
{
	struct mmc_card *card;

246
	card = kzalloc(sizeof(struct mmc_card), GFP_KERNEL);
P
Pierre Ossman 已提交
247 248 249 250 251 252 253 254 255 256
	if (!card)
		return ERR_PTR(-ENOMEM);

	card->host = host;

	device_initialize(&card->dev);

	card->dev.parent = mmc_classdev(host);
	card->dev.bus = &mmc_bus_type;
	card->dev.release = mmc_release_card;
257
	card->dev.type = type;
P
Pierre Ossman 已提交
258 259 260 261 262 263 264 265 266 267

	return card;
}

/*
 * Register a new MMC card with the driver model.
 */
int mmc_add_card(struct mmc_card *card)
{
	int ret;
268
	const char *type;
P
Pierre Ossman 已提交
269

270
	dev_set_name(&card->dev, "%s:%04x", mmc_hostname(card->host), card->rca);
P
Pierre Ossman 已提交
271

272 273 274 275 276 277
	switch (card->type) {
	case MMC_TYPE_MMC:
		type = "MMC";
		break;
	case MMC_TYPE_SD:
		type = "SD";
278 279 280 281 282 283
		if (mmc_card_blockaddr(card)) {
			if (mmc_card_ext_capacity(card))
				type = "SDXC";
			else
				type = "SDHC";
		}
284
		break;
P
Pierre Ossman 已提交
285 286 287
	case MMC_TYPE_SDIO:
		type = "SDIO";
		break;
288 289 290 291
	case MMC_TYPE_SD_COMBO:
		type = "SD-combo";
		if (mmc_card_blockaddr(card))
			type = "SDHC-combo";
292
		break;
293 294 295 296 297
	default:
		type = "?";
		break;
	}

D
David Brownell 已提交
298
	if (mmc_host_is_spi(card->host)) {
299
		pr_info("%s: new %s%s%s card on SPI\n",
D
David Brownell 已提交
300 301
			mmc_hostname(card->host),
			mmc_card_highspeed(card) ? "high speed " : "",
302
			mmc_card_ddr_mode(card) ? "DDR " : "",
D
David Brownell 已提交
303 304
			type);
	} else {
305
		printk(KERN_INFO "%s: new %s%s%s card at address %04x\n",
D
David Brownell 已提交
306
			mmc_hostname(card->host),
307 308
			mmc_sd_card_uhs(card) ? "ultra high speed " :
			(mmc_card_highspeed(card) ? "high speed " : ""),
309
			mmc_card_ddr_mode(card) ? "DDR " : "",
D
David Brownell 已提交
310 311
			type, card->rca);
	}
312

313 314 315 316
#ifdef CONFIG_DEBUG_FS
	mmc_add_card_debugfs(card);
#endif

317 318 319 320
	ret = device_add(&card->dev);
	if (ret)
		return ret;

P
Pierre Ossman 已提交
321 322 323 324 325 326 327 328 329 330 331
	mmc_card_set_present(card);

	return 0;
}

/*
 * Unregister a new MMC card with the driver model, and
 * (eventually) free it.
 */
void mmc_remove_card(struct mmc_card *card)
{
332 333 334 335
#ifdef CONFIG_DEBUG_FS
	mmc_remove_card_debugfs(card);
#endif

P
Pierre Ossman 已提交
336
	if (mmc_card_present(card)) {
D
David Brownell 已提交
337
		if (mmc_host_is_spi(card->host)) {
338
			pr_info("%s: SPI card removed\n",
D
David Brownell 已提交
339 340
				mmc_hostname(card->host));
		} else {
341
			pr_info("%s: card %04x removed\n",
D
David Brownell 已提交
342 343
				mmc_hostname(card->host), card->rca);
		}
P
Pierre Ossman 已提交
344 345 346 347 348 349
		device_del(&card->dev);
	}

	put_device(&card->dev);
}