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