host.c 4.0 KB
Newer Older
1 2 3 4
/*
 *  linux/drivers/mmc/core/host.c
 *
 *  Copyright (C) 2003 Russell King, All Rights Reserved.
P
Pierre Ossman 已提交
5
 *  Copyright (C) 2007-2008 Pierre Ossman
6 7 8 9 10 11 12 13 14 15 16 17
 *
 * 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 host class device management
 */

#include <linux/device.h>
#include <linux/err.h>
#include <linux/idr.h>
#include <linux/pagemap.h>
P
Pierre Ossman 已提交
18
#include <linux/leds.h>
19
#include <linux/slab.h>
20
#include <linux/suspend.h>
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

#include <linux/mmc/host.h>

#include "core.h"
#include "host.h"

#define cls_dev_to_mmc_host(d)	container_of(d, struct mmc_host, class_dev)

static void mmc_host_classdev_release(struct device *dev)
{
	struct mmc_host *host = cls_dev_to_mmc_host(dev);
	kfree(host);
}

static struct class mmc_host_class = {
	.name		= "mmc_host",
	.dev_release	= mmc_host_classdev_release,
};

int mmc_register_host_class(void)
{
	return class_register(&mmc_host_class);
}

void mmc_unregister_host_class(void)
{
	class_unregister(&mmc_host_class);
}

static DEFINE_IDR(mmc_host_idr);
static DEFINE_SPINLOCK(mmc_host_lock);

/**
 *	mmc_alloc_host - initialise the per-host structure.
 *	@extra: sizeof private data structure
 *	@dev: pointer to host device model structure
 *
 *	Initialise the per-host structure.
 */
struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
{
P
Pierre Ossman 已提交
62
	int err;
63 64
	struct mmc_host *host;

P
Pierre Ossman 已提交
65 66 67
	if (!idr_pre_get(&mmc_host_idr, GFP_KERNEL))
		return NULL;

68
	host = kzalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL);
69 70 71
	if (!host)
		return NULL;

P
Pierre Ossman 已提交
72 73 74 75 76 77
	spin_lock(&mmc_host_lock);
	err = idr_get_new(&mmc_host_idr, host, &host->index);
	spin_unlock(&mmc_host_lock);
	if (err)
		goto free;

78
	dev_set_name(&host->class_dev, "mmc%d", host->index);
P
Pierre Ossman 已提交
79

80 81 82 83 84 85 86 87
	host->parent = dev;
	host->class_dev.parent = dev;
	host->class_dev.class = &mmc_host_class;
	device_initialize(&host->class_dev);

	spin_lock_init(&host->lock);
	init_waitqueue_head(&host->wq);
	INIT_DELAYED_WORK(&host->detect, mmc_rescan);
88
	INIT_DELAYED_WORK_DEFERRABLE(&host->disable, mmc_host_deeper_disable);
89
	host->pm_notify.notifier_call = mmc_pm_notify;
90 91 92 93 94 95 96 97 98 99 100 101 102 103

	/*
	 * By default, hosts do not support SGIO or large requests.
	 * They have to set these according to their abilities.
	 */
	host->max_hw_segs = 1;
	host->max_phys_segs = 1;
	host->max_seg_size = PAGE_CACHE_SIZE;

	host->max_req_size = PAGE_CACHE_SIZE;
	host->max_blk_size = 512;
	host->max_blk_count = PAGE_CACHE_SIZE / 512;

	return host;
P
Pierre Ossman 已提交
104 105 106 107

free:
	kfree(host);
	return NULL;
108 109 110 111 112 113 114
}

EXPORT_SYMBOL(mmc_alloc_host);

/**
 *	mmc_add_host - initialise host hardware
 *	@host: mmc host
P
Pierre Ossman 已提交
115 116 117 118
 *
 *	Register the host with the driver model. The host must be
 *	prepared to start servicing requests before this function
 *	completes.
119 120 121 122 123
 */
int mmc_add_host(struct mmc_host *host)
{
	int err;

124 125 126
	WARN_ON((host->caps & MMC_CAP_SDIO_IRQ) &&
		!host->ops->enable_sdio_irq);

127
	led_trigger_register_simple(dev_name(&host->class_dev), &host->led);
P
Pierre Ossman 已提交
128

129 130 131 132
	err = device_add(&host->class_dev);
	if (err)
		return err;

133 134 135 136
#ifdef CONFIG_DEBUG_FS
	mmc_add_host_debugfs(host);
#endif

137
	mmc_start_host(host);
138
	register_pm_notifier(&host->pm_notify);
139 140 141 142 143 144 145 146 147 148 149

	return 0;
}

EXPORT_SYMBOL(mmc_add_host);

/**
 *	mmc_remove_host - remove host hardware
 *	@host: mmc host
 *
 *	Unregister and remove all cards associated with this host,
P
Pierre Ossman 已提交
150 151
 *	and power down the MMC bus. No new requests will be issued
 *	after this function has returned.
152 153 154
 */
void mmc_remove_host(struct mmc_host *host)
{
155
	unregister_pm_notifier(&host->pm_notify);
156 157
	mmc_stop_host(host);

158 159 160 161
#ifdef CONFIG_DEBUG_FS
	mmc_remove_host_debugfs(host);
#endif

162 163
	device_del(&host->class_dev);

164
	led_trigger_unregister_simple(host->led);
165 166 167 168 169 170 171 172 173 174 175 176
}

EXPORT_SYMBOL(mmc_remove_host);

/**
 *	mmc_free_host - free the host structure
 *	@host: mmc host
 *
 *	Free the host once all references to it have been dropped.
 */
void mmc_free_host(struct mmc_host *host)
{
P
Pierre Ossman 已提交
177 178 179 180
	spin_lock(&mmc_host_lock);
	idr_remove(&mmc_host_idr, host->index);
	spin_unlock(&mmc_host_lock);

181 182 183 184 185
	put_device(&host->class_dev);
}

EXPORT_SYMBOL(mmc_free_host);