blob: 0efe631e50cab2ddda5e6a98022e5b8e49f46705 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Maxim Levitsky4c2ef252010-08-10 18:01:41 -070020#include <linux/suspend.h>
Pierre Ossmanb93931a2007-05-19 14:06:24 +020021
22#include <linux/mmc/host.h>
23
24#include "core.h"
25#include "host.h"
26
27#define cls_dev_to_mmc_host(d) container_of(d, struct mmc_host, class_dev)
28
29static void mmc_host_classdev_release(struct device *dev)
30{
31 struct mmc_host *host = cls_dev_to_mmc_host(dev);
32 kfree(host);
33}
34
35static struct class mmc_host_class = {
36 .name = "mmc_host",
37 .dev_release = mmc_host_classdev_release,
38};
39
40int mmc_register_host_class(void)
41{
42 return class_register(&mmc_host_class);
43}
44
45void mmc_unregister_host_class(void)
46{
47 class_unregister(&mmc_host_class);
48}
49
50static DEFINE_IDR(mmc_host_idr);
51static DEFINE_SPINLOCK(mmc_host_lock);
52
53/**
54 * mmc_alloc_host - initialise the per-host structure.
55 * @extra: sizeof private data structure
56 * @dev: pointer to host device model structure
57 *
58 * Initialise the per-host structure.
59 */
60struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
61{
Pierre Ossmanff3112f2008-03-08 23:43:19 +010062 int err;
Pierre Ossmanb93931a2007-05-19 14:06:24 +020063 struct mmc_host *host;
64
Pierre Ossmanff3112f2008-03-08 23:43:19 +010065 if (!idr_pre_get(&mmc_host_idr, GFP_KERNEL))
66 return NULL;
67
Mariusz Kozlowskibe760a92007-08-10 14:00:50 -070068 host = kzalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL);
Pierre Ossmanb93931a2007-05-19 14:06:24 +020069 if (!host)
70 return NULL;
71
Pierre Ossmanff3112f2008-03-08 23:43:19 +010072 spin_lock(&mmc_host_lock);
73 err = idr_get_new(&mmc_host_idr, host, &host->index);
74 spin_unlock(&mmc_host_lock);
75 if (err)
76 goto free;
77
Kay Sieversd1b26862008-11-08 21:37:46 +010078 dev_set_name(&host->class_dev, "mmc%d", host->index);
Pierre Ossmanff3112f2008-03-08 23:43:19 +010079
Pierre Ossmanb93931a2007-05-19 14:06:24 +020080 host->parent = dev;
81 host->class_dev.parent = dev;
82 host->class_dev.class = &mmc_host_class;
83 device_initialize(&host->class_dev);
84
85 spin_lock_init(&host->lock);
86 init_waitqueue_head(&host->wq);
87 INIT_DELAYED_WORK(&host->detect, mmc_rescan);
Adrian Hunter8ea926b2009-09-22 16:44:29 -070088 INIT_DELAYED_WORK_DEFERRABLE(&host->disable, mmc_host_deeper_disable);
Maxim Levitsky4c2ef252010-08-10 18:01:41 -070089 host->pm_notify.notifier_call = mmc_pm_notify;
Pierre Ossmanb93931a2007-05-19 14:06:24 +020090
91 /*
92 * By default, hosts do not support SGIO or large requests.
93 * They have to set these according to their abilities.
94 */
95 host->max_hw_segs = 1;
96 host->max_phys_segs = 1;
97 host->max_seg_size = PAGE_CACHE_SIZE;
98
99 host->max_req_size = PAGE_CACHE_SIZE;
100 host->max_blk_size = 512;
101 host->max_blk_count = PAGE_CACHE_SIZE / 512;
102
103 return host;
Pierre Ossmanff3112f2008-03-08 23:43:19 +0100104
105free:
106 kfree(host);
107 return NULL;
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200108}
109
110EXPORT_SYMBOL(mmc_alloc_host);
111
112/**
113 * mmc_add_host - initialise host hardware
114 * @host: mmc host
Pierre Ossman67a61c42007-07-11 20:22:11 +0200115 *
116 * Register the host with the driver model. The host must be
117 * prepared to start servicing requests before this function
118 * completes.
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200119 */
120int mmc_add_host(struct mmc_host *host)
121{
122 int err;
123
Nicolas Pitre17b759a2007-07-24 02:09:39 -0400124 WARN_ON((host->caps & MMC_CAP_SDIO_IRQ) &&
125 !host->ops->enable_sdio_irq);
126
Kay Sieversd1b26862008-11-08 21:37:46 +0100127 led_trigger_register_simple(dev_name(&host->class_dev), &host->led);
Pierre Ossmanaf8350c2007-09-24 07:15:48 +0200128
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200129 err = device_add(&host->class_dev);
130 if (err)
131 return err;
132
Haavard Skinnemoen6edd8ee2008-07-24 14:18:57 +0200133#ifdef CONFIG_DEBUG_FS
134 mmc_add_host_debugfs(host);
135#endif
136
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200137 mmc_start_host(host);
Maxim Levitsky4c2ef252010-08-10 18:01:41 -0700138 register_pm_notifier(&host->pm_notify);
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200139
140 return 0;
141}
142
143EXPORT_SYMBOL(mmc_add_host);
144
145/**
146 * mmc_remove_host - remove host hardware
147 * @host: mmc host
148 *
149 * Unregister and remove all cards associated with this host,
Pierre Ossman67a61c42007-07-11 20:22:11 +0200150 * and power down the MMC bus. No new requests will be issued
151 * after this function has returned.
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200152 */
153void mmc_remove_host(struct mmc_host *host)
154{
Maxim Levitsky4c2ef252010-08-10 18:01:41 -0700155 unregister_pm_notifier(&host->pm_notify);
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200156 mmc_stop_host(host);
157
Haavard Skinnemoen6edd8ee2008-07-24 14:18:57 +0200158#ifdef CONFIG_DEBUG_FS
159 mmc_remove_host_debugfs(host);
160#endif
161
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200162 device_del(&host->class_dev);
163
Pierre Ossman77f1fd62007-10-12 22:48:46 +0200164 led_trigger_unregister_simple(host->led);
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200165}
166
167EXPORT_SYMBOL(mmc_remove_host);
168
169/**
170 * mmc_free_host - free the host structure
171 * @host: mmc host
172 *
173 * Free the host once all references to it have been dropped.
174 */
175void mmc_free_host(struct mmc_host *host)
176{
Pierre Ossmanff3112f2008-03-08 23:43:19 +0100177 spin_lock(&mmc_host_lock);
178 idr_remove(&mmc_host_idr, host->index);
179 spin_unlock(&mmc_host_lock);
180
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200181 put_device(&host->class_dev);
182}
183
184EXPORT_SYMBOL(mmc_free_host);
185