Commit | Line | Data |
---|---|---|
b93931a6 PO |
1 | /* |
2 | * linux/drivers/mmc/core/host.c | |
3 | * | |
4 | * Copyright (C) 2003 Russell King, All Rights Reserved. | |
ff3112f5 | 5 | * Copyright (C) 2007-2008 Pierre Ossman |
b93931a6 PO |
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 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> | |
af8350c7 | 18 | #include <linux/leds.h> |
5a0e3ad6 | 19 | #include <linux/slab.h> |
4c2ef25f | 20 | #include <linux/suspend.h> |
b93931a6 PO |
21 | |
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 | ||
29 | static 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 | ||
35 | static struct class mmc_host_class = { | |
36 | .name = "mmc_host", | |
37 | .dev_release = mmc_host_classdev_release, | |
38 | }; | |
39 | ||
40 | int mmc_register_host_class(void) | |
41 | { | |
42 | return class_register(&mmc_host_class); | |
43 | } | |
44 | ||
45 | void mmc_unregister_host_class(void) | |
46 | { | |
47 | class_unregister(&mmc_host_class); | |
48 | } | |
49 | ||
50 | static DEFINE_IDR(mmc_host_idr); | |
51 | static 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 | */ | |
60 | struct mmc_host *mmc_alloc_host(int extra, struct device *dev) | |
61 | { | |
ff3112f5 | 62 | int err; |
b93931a6 PO |
63 | struct mmc_host *host; |
64 | ||
ff3112f5 PO |
65 | if (!idr_pre_get(&mmc_host_idr, GFP_KERNEL)) |
66 | return NULL; | |
67 | ||
be760a9d | 68 | host = kzalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL); |
b93931a6 PO |
69 | if (!host) |
70 | return NULL; | |
71 | ||
ff3112f5 PO |
72 | 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 | ||
d1b26863 | 78 | dev_set_name(&host->class_dev, "mmc%d", host->index); |
ff3112f5 | 79 | |
b93931a6 PO |
80 | 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); | |
8ea926b2 | 88 | INIT_DELAYED_WORK_DEFERRABLE(&host->disable, mmc_host_deeper_disable); |
81ca03a0 | 89 | #ifdef CONFIG_PM |
4c2ef25f | 90 | host->pm_notify.notifier_call = mmc_pm_notify; |
81ca03a0 | 91 | #endif |
b93931a6 PO |
92 | |
93 | /* | |
94 | * By default, hosts do not support SGIO or large requests. | |
95 | * They have to set these according to their abilities. | |
96 | */ | |
97 | host->max_hw_segs = 1; | |
98 | host->max_phys_segs = 1; | |
99 | host->max_seg_size = PAGE_CACHE_SIZE; | |
100 | ||
101 | host->max_req_size = PAGE_CACHE_SIZE; | |
102 | host->max_blk_size = 512; | |
103 | host->max_blk_count = PAGE_CACHE_SIZE / 512; | |
104 | ||
105 | return host; | |
ff3112f5 PO |
106 | |
107 | free: | |
108 | kfree(host); | |
109 | return NULL; | |
b93931a6 PO |
110 | } |
111 | ||
112 | EXPORT_SYMBOL(mmc_alloc_host); | |
113 | ||
114 | /** | |
115 | * mmc_add_host - initialise host hardware | |
116 | * @host: mmc host | |
67a61c48 PO |
117 | * |
118 | * Register the host with the driver model. The host must be | |
119 | * prepared to start servicing requests before this function | |
120 | * completes. | |
b93931a6 PO |
121 | */ |
122 | int mmc_add_host(struct mmc_host *host) | |
123 | { | |
124 | int err; | |
125 | ||
17b759af NP |
126 | WARN_ON((host->caps & MMC_CAP_SDIO_IRQ) && |
127 | !host->ops->enable_sdio_irq); | |
128 | ||
d1b26863 | 129 | led_trigger_register_simple(dev_name(&host->class_dev), &host->led); |
af8350c7 | 130 | |
b93931a6 PO |
131 | err = device_add(&host->class_dev); |
132 | if (err) | |
133 | return err; | |
134 | ||
6edd8ee6 HS |
135 | #ifdef CONFIG_DEBUG_FS |
136 | mmc_add_host_debugfs(host); | |
137 | #endif | |
138 | ||
b93931a6 | 139 | mmc_start_host(host); |
4c2ef25f | 140 | register_pm_notifier(&host->pm_notify); |
b93931a6 PO |
141 | |
142 | return 0; | |
143 | } | |
144 | ||
145 | EXPORT_SYMBOL(mmc_add_host); | |
146 | ||
147 | /** | |
148 | * mmc_remove_host - remove host hardware | |
149 | * @host: mmc host | |
150 | * | |
151 | * Unregister and remove all cards associated with this host, | |
67a61c48 PO |
152 | * and power down the MMC bus. No new requests will be issued |
153 | * after this function has returned. | |
b93931a6 PO |
154 | */ |
155 | void mmc_remove_host(struct mmc_host *host) | |
156 | { | |
4c2ef25f | 157 | unregister_pm_notifier(&host->pm_notify); |
b93931a6 PO |
158 | mmc_stop_host(host); |
159 | ||
6edd8ee6 HS |
160 | #ifdef CONFIG_DEBUG_FS |
161 | mmc_remove_host_debugfs(host); | |
162 | #endif | |
163 | ||
b93931a6 PO |
164 | device_del(&host->class_dev); |
165 | ||
77f1fd6e | 166 | led_trigger_unregister_simple(host->led); |
b93931a6 PO |
167 | } |
168 | ||
169 | EXPORT_SYMBOL(mmc_remove_host); | |
170 | ||
171 | /** | |
172 | * mmc_free_host - free the host structure | |
173 | * @host: mmc host | |
174 | * | |
175 | * Free the host once all references to it have been dropped. | |
176 | */ | |
177 | void mmc_free_host(struct mmc_host *host) | |
178 | { | |
ff3112f5 PO |
179 | spin_lock(&mmc_host_lock); |
180 | idr_remove(&mmc_host_idr, host->index); | |
181 | spin_unlock(&mmc_host_lock); | |
182 | ||
b93931a6 PO |
183 | put_device(&host->class_dev); |
184 | } | |
185 | ||
186 | EXPORT_SYMBOL(mmc_free_host); | |
187 |