blob: 5e945e64ead7cb64aaf2b1043d53c0d745d66219 [file] [log] [blame]
Pierre Ossmanb93931a2007-05-19 14:06:24 +02001/*
2 * linux/drivers/mmc/core/host.c
3 *
4 * Copyright (C) 2003 Russell King, All Rights Reserved.
Pierre Ossmanff3112f2008-03-08 23:43:19 +01005 * Copyright (C) 2007-2008 Pierre Ossman
Pierre Ossmanb93931a2007-05-19 14:06:24 +02006 *
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 host class device management
12 */
13
14#include <linux/device.h>
15#include <linux/err.h>
16#include <linux/idr.h>
17#include <linux/pagemap.h>
Pierre Ossmanaf8350c2007-09-24 07:15:48 +020018#include <linux/leds.h>
Pierre Ossmanb93931a2007-05-19 14:06:24 +020019
20#include <linux/mmc/host.h>
21
22#include "core.h"
23#include "host.h"
24
25#define cls_dev_to_mmc_host(d) container_of(d, struct mmc_host, class_dev)
26
27static void mmc_host_classdev_release(struct device *dev)
28{
29 struct mmc_host *host = cls_dev_to_mmc_host(dev);
30 kfree(host);
31}
32
33static struct class mmc_host_class = {
34 .name = "mmc_host",
35 .dev_release = mmc_host_classdev_release,
36};
37
38int mmc_register_host_class(void)
39{
40 return class_register(&mmc_host_class);
41}
42
43void mmc_unregister_host_class(void)
44{
45 class_unregister(&mmc_host_class);
46}
47
48static DEFINE_IDR(mmc_host_idr);
49static DEFINE_SPINLOCK(mmc_host_lock);
50
51/**
52 * mmc_alloc_host - initialise the per-host structure.
53 * @extra: sizeof private data structure
54 * @dev: pointer to host device model structure
55 *
56 * Initialise the per-host structure.
57 */
58struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
59{
Pierre Ossmanff3112f2008-03-08 23:43:19 +010060 int err;
Pierre Ossmanb93931a2007-05-19 14:06:24 +020061 struct mmc_host *host;
62
Pierre Ossmanff3112f2008-03-08 23:43:19 +010063 if (!idr_pre_get(&mmc_host_idr, GFP_KERNEL))
64 return NULL;
65
Mariusz Kozlowskibe760a92007-08-10 14:00:50 -070066 host = kzalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL);
Pierre Ossmanb93931a2007-05-19 14:06:24 +020067 if (!host)
68 return NULL;
69
Pierre Ossmanff3112f2008-03-08 23:43:19 +010070 spin_lock(&mmc_host_lock);
71 err = idr_get_new(&mmc_host_idr, host, &host->index);
72 spin_unlock(&mmc_host_lock);
73 if (err)
74 goto free;
75
Kay Sieversd1b26862008-11-08 21:37:46 +010076 dev_set_name(&host->class_dev, "mmc%d", host->index);
Pierre Ossmanff3112f2008-03-08 23:43:19 +010077
Pierre Ossmanb93931a2007-05-19 14:06:24 +020078 host->parent = dev;
79 host->class_dev.parent = dev;
80 host->class_dev.class = &mmc_host_class;
81 device_initialize(&host->class_dev);
82
83 spin_lock_init(&host->lock);
84 init_waitqueue_head(&host->wq);
85 INIT_DELAYED_WORK(&host->detect, mmc_rescan);
86
87 /*
88 * By default, hosts do not support SGIO or large requests.
89 * They have to set these according to their abilities.
90 */
91 host->max_hw_segs = 1;
92 host->max_phys_segs = 1;
93 host->max_seg_size = PAGE_CACHE_SIZE;
94
95 host->max_req_size = PAGE_CACHE_SIZE;
96 host->max_blk_size = 512;
97 host->max_blk_count = PAGE_CACHE_SIZE / 512;
98
99 return host;
Pierre Ossmanff3112f2008-03-08 23:43:19 +0100100
101free:
102 kfree(host);
103 return NULL;
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200104}
105
106EXPORT_SYMBOL(mmc_alloc_host);
107
108/**
109 * mmc_add_host - initialise host hardware
110 * @host: mmc host
Pierre Ossman67a61c42007-07-11 20:22:11 +0200111 *
112 * Register the host with the driver model. The host must be
113 * prepared to start servicing requests before this function
114 * completes.
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200115 */
116int mmc_add_host(struct mmc_host *host)
117{
118 int err;
119
Nicolas Pitre17b759a2007-07-24 02:09:39 -0400120 WARN_ON((host->caps & MMC_CAP_SDIO_IRQ) &&
121 !host->ops->enable_sdio_irq);
122
Kay Sieversd1b26862008-11-08 21:37:46 +0100123 led_trigger_register_simple(dev_name(&host->class_dev), &host->led);
Pierre Ossmanaf8350c2007-09-24 07:15:48 +0200124
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200125 err = device_add(&host->class_dev);
126 if (err)
127 return err;
128
Haavard Skinnemoen6edd8ee2008-07-24 14:18:57 +0200129#ifdef CONFIG_DEBUG_FS
130 mmc_add_host_debugfs(host);
131#endif
132
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200133 mmc_start_host(host);
134
135 return 0;
136}
137
138EXPORT_SYMBOL(mmc_add_host);
139
140/**
141 * mmc_remove_host - remove host hardware
142 * @host: mmc host
143 *
144 * Unregister and remove all cards associated with this host,
Pierre Ossman67a61c42007-07-11 20:22:11 +0200145 * and power down the MMC bus. No new requests will be issued
146 * after this function has returned.
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200147 */
148void mmc_remove_host(struct mmc_host *host)
149{
150 mmc_stop_host(host);
151
Haavard Skinnemoen6edd8ee2008-07-24 14:18:57 +0200152#ifdef CONFIG_DEBUG_FS
153 mmc_remove_host_debugfs(host);
154#endif
155
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200156 device_del(&host->class_dev);
157
Pierre Ossman77f1fd62007-10-12 22:48:46 +0200158 led_trigger_unregister_simple(host->led);
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200159}
160
161EXPORT_SYMBOL(mmc_remove_host);
162
163/**
164 * mmc_free_host - free the host structure
165 * @host: mmc host
166 *
167 * Free the host once all references to it have been dropped.
168 */
169void mmc_free_host(struct mmc_host *host)
170{
Pierre Ossmanff3112f2008-03-08 23:43:19 +0100171 spin_lock(&mmc_host_lock);
172 idr_remove(&mmc_host_idr, host->index);
173 spin_unlock(&mmc_host_lock);
174
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200175 put_device(&host->class_dev);
176}
177
178EXPORT_SYMBOL(mmc_free_host);
179