9443e8e9e693007f5c90389b29958c5e2d717f9d
[pandora-kernel.git] / drivers / mmc / core / bus.c
1 /*
2  *  linux/drivers/mmc/core/bus.c
3  *
4  *  Copyright (C) 2003 Russell King, All Rights Reserved.
5  *  Copyright (C) 2007 Pierre Ossman
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 card bus driver model
12  */
13
14 #include <linux/export.h>
15 #include <linux/device.h>
16 #include <linux/err.h>
17 #include <linux/slab.h>
18 #include <linux/pm_runtime.h>
19
20 #include <linux/mmc/card.h>
21 #include <linux/mmc/host.h>
22
23 #include "core.h"
24 #include "sdio_cis.h"
25 #include "bus.h"
26
27 #define to_mmc_driver(d)        container_of(d, struct mmc_driver, drv)
28
29 static ssize_t mmc_type_show(struct device *dev,
30         struct device_attribute *attr, char *buf)
31 {
32         struct mmc_card *card = mmc_dev_to_card(dev);
33
34         switch (card->type) {
35         case MMC_TYPE_MMC:
36                 return sprintf(buf, "MMC\n");
37         case MMC_TYPE_SD:
38                 return sprintf(buf, "SD\n");
39         case MMC_TYPE_SDIO:
40                 return sprintf(buf, "SDIO\n");
41         case MMC_TYPE_SD_COMBO:
42                 return sprintf(buf, "SDcombo\n");
43         default:
44                 return -EFAULT;
45         }
46 }
47
48 static struct device_attribute mmc_dev_attrs[] = {
49         __ATTR(type, S_IRUGO, mmc_type_show, NULL),
50         __ATTR_NULL,
51 };
52
53 /*
54  * This currently matches any MMC driver to any MMC card - drivers
55  * themselves make the decision whether to drive this card in their
56  * probe method.
57  */
58 static int mmc_bus_match(struct device *dev, struct device_driver *drv)
59 {
60         return 1;
61 }
62
63 static int
64 mmc_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
65 {
66         struct mmc_card *card = mmc_dev_to_card(dev);
67         const char *type;
68         int retval = 0;
69
70         switch (card->type) {
71         case MMC_TYPE_MMC:
72                 type = "MMC";
73                 break;
74         case MMC_TYPE_SD:
75                 type = "SD";
76                 break;
77         case MMC_TYPE_SDIO:
78                 type = "SDIO";
79                 break;
80         case MMC_TYPE_SD_COMBO:
81                 type = "SDcombo";
82                 break;
83         default:
84                 type = NULL;
85         }
86
87         if (type) {
88                 retval = add_uevent_var(env, "MMC_TYPE=%s", type);
89                 if (retval)
90                         return retval;
91         }
92
93         retval = add_uevent_var(env, "MMC_NAME=%s", mmc_card_name(card));
94         if (retval)
95                 return retval;
96
97         /*
98          * Request the mmc_block device.  Note: that this is a direct request
99          * for the module it carries no information as to what is inserted.
100          */
101         retval = add_uevent_var(env, "MODALIAS=mmc:block");
102
103         return retval;
104 }
105
106 static int mmc_bus_probe(struct device *dev)
107 {
108         struct mmc_driver *drv = to_mmc_driver(dev->driver);
109         struct mmc_card *card = mmc_dev_to_card(dev);
110
111         return drv->probe(card);
112 }
113
114 static int mmc_bus_remove(struct device *dev)
115 {
116         struct mmc_driver *drv = to_mmc_driver(dev->driver);
117         struct mmc_card *card = mmc_dev_to_card(dev);
118
119         drv->remove(card);
120
121         return 0;
122 }
123
124 static int mmc_bus_suspend(struct device *dev, pm_message_t state)
125 {
126         struct mmc_driver *drv = to_mmc_driver(dev->driver);
127         struct mmc_card *card = mmc_dev_to_card(dev);
128         int ret = 0;
129
130         if (dev->driver && drv->suspend)
131                 ret = drv->suspend(card, state);
132         return ret;
133 }
134
135 static int mmc_bus_resume(struct device *dev)
136 {
137         struct mmc_driver *drv = to_mmc_driver(dev->driver);
138         struct mmc_card *card = mmc_dev_to_card(dev);
139         int ret = 0;
140
141         if (dev->driver && drv->resume)
142                 ret = drv->resume(card);
143         return ret;
144 }
145
146 #ifdef CONFIG_PM_RUNTIME
147
148 static int mmc_runtime_suspend(struct device *dev)
149 {
150         struct mmc_card *card = mmc_dev_to_card(dev);
151
152         return mmc_power_save_host(card->host);
153 }
154
155 static int mmc_runtime_resume(struct device *dev)
156 {
157         struct mmc_card *card = mmc_dev_to_card(dev);
158
159         return mmc_power_restore_host(card->host);
160 }
161
162 static int mmc_runtime_idle(struct device *dev)
163 {
164         return pm_runtime_suspend(dev);
165 }
166
167 static const struct dev_pm_ops mmc_bus_pm_ops = {
168         .runtime_suspend        = mmc_runtime_suspend,
169         .runtime_resume         = mmc_runtime_resume,
170         .runtime_idle           = mmc_runtime_idle,
171 };
172
173 #define MMC_PM_OPS_PTR  (&mmc_bus_pm_ops)
174
175 #else /* !CONFIG_PM_RUNTIME */
176
177 #define MMC_PM_OPS_PTR  NULL
178
179 #endif /* !CONFIG_PM_RUNTIME */
180
181 static struct bus_type mmc_bus_type = {
182         .name           = "mmc",
183         .dev_attrs      = mmc_dev_attrs,
184         .match          = mmc_bus_match,
185         .uevent         = mmc_bus_uevent,
186         .probe          = mmc_bus_probe,
187         .remove         = mmc_bus_remove,
188         .suspend        = mmc_bus_suspend,
189         .resume         = mmc_bus_resume,
190         .pm             = MMC_PM_OPS_PTR,
191 };
192
193 int mmc_register_bus(void)
194 {
195         return bus_register(&mmc_bus_type);
196 }
197
198 void mmc_unregister_bus(void)
199 {
200         bus_unregister(&mmc_bus_type);
201 }
202
203 /**
204  *      mmc_register_driver - register a media driver
205  *      @drv: MMC media driver
206  */
207 int mmc_register_driver(struct mmc_driver *drv)
208 {
209         drv->drv.bus = &mmc_bus_type;
210         return driver_register(&drv->drv);
211 }
212
213 EXPORT_SYMBOL(mmc_register_driver);
214
215 /**
216  *      mmc_unregister_driver - unregister a media driver
217  *      @drv: MMC media driver
218  */
219 void mmc_unregister_driver(struct mmc_driver *drv)
220 {
221         drv->drv.bus = &mmc_bus_type;
222         driver_unregister(&drv->drv);
223 }
224
225 EXPORT_SYMBOL(mmc_unregister_driver);
226
227 static void mmc_release_card(struct device *dev)
228 {
229         struct mmc_card *card = mmc_dev_to_card(dev);
230
231         sdio_free_common_cis(card);
232
233         if (card->info)
234                 kfree(card->info);
235
236         kfree(card);
237 }
238
239 /*
240  * Allocate and initialise a new MMC card structure.
241  */
242 struct mmc_card *mmc_alloc_card(struct mmc_host *host, struct device_type *type)
243 {
244         struct mmc_card *card;
245
246         card = kzalloc(sizeof(struct mmc_card), GFP_KERNEL);
247         if (!card)
248                 return ERR_PTR(-ENOMEM);
249
250         card->host = host;
251
252         device_initialize(&card->dev);
253
254         card->dev.parent = mmc_classdev(host);
255         card->dev.bus = &mmc_bus_type;
256         card->dev.release = mmc_release_card;
257         card->dev.type = type;
258
259         return card;
260 }
261
262 /*
263  * Register a new MMC card with the driver model.
264  */
265 int mmc_add_card(struct mmc_card *card)
266 {
267         int ret;
268         const char *type;
269
270         dev_set_name(&card->dev, "%s:%04x", mmc_hostname(card->host), card->rca);
271
272         switch (card->type) {
273         case MMC_TYPE_MMC:
274                 type = "MMC";
275                 break;
276         case MMC_TYPE_SD:
277                 type = "SD";
278                 if (mmc_card_blockaddr(card)) {
279                         if (mmc_card_ext_capacity(card))
280                                 type = "SDXC";
281                         else
282                                 type = "SDHC";
283                 }
284                 break;
285         case MMC_TYPE_SDIO:
286                 type = "SDIO";
287                 break;
288         case MMC_TYPE_SD_COMBO:
289                 type = "SD-combo";
290                 if (mmc_card_blockaddr(card))
291                         type = "SDHC-combo";
292                 break;
293         default:
294                 type = "?";
295                 break;
296         }
297
298         if (mmc_host_is_spi(card->host)) {
299                 pr_info("%s: new %s%s%s card on SPI\n",
300                         mmc_hostname(card->host),
301                         mmc_card_highspeed(card) ? "high speed " : "",
302                         mmc_card_ddr_mode(card) ? "DDR " : "",
303                         type);
304         } else {
305                 printk(KERN_INFO "%s: new %s%s%s card at address %04x\n",
306                         mmc_hostname(card->host),
307                         mmc_sd_card_uhs(card) ? "ultra high speed " :
308                         (mmc_card_highspeed(card) ? "high speed " : ""),
309                         mmc_card_ddr_mode(card) ? "DDR " : "",
310                         type, card->rca);
311         }
312
313 #ifdef CONFIG_DEBUG_FS
314         mmc_add_card_debugfs(card);
315 #endif
316
317         ret = device_add(&card->dev);
318         if (ret)
319                 return ret;
320
321         mmc_card_set_present(card);
322
323         return 0;
324 }
325
326 /*
327  * Unregister a new MMC card with the driver model, and
328  * (eventually) free it.
329  */
330 void mmc_remove_card(struct mmc_card *card)
331 {
332 #ifdef CONFIG_DEBUG_FS
333         mmc_remove_card_debugfs(card);
334 #endif
335
336         if (mmc_card_present(card)) {
337                 if (mmc_host_is_spi(card->host)) {
338                         pr_info("%s: SPI card removed\n",
339                                 mmc_hostname(card->host));
340                 } else {
341                         pr_info("%s: card %04x removed\n",
342                                 mmc_hostname(card->host), card->rca);
343                 }
344                 device_del(&card->dev);
345         }
346
347         put_device(&card->dev);
348 }
349