Merge commit 'v3.0-rc5' into android-3.0
[firefly-linux-kernel-4.4.55.git] / drivers / mmc / core / sdio_bus.c
1 /*
2  *  linux/drivers/mmc/core/sdio_bus.c
3  *
4  *  Copyright 2007 Pierre Ossman
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or (at
9  * your option) any later version.
10  *
11  * SDIO function driver model
12  */
13
14 #include <linux/device.h>
15 #include <linux/err.h>
16 #include <linux/slab.h>
17 #include <linux/pm_runtime.h>
18
19 #include <linux/mmc/card.h>
20 #include <linux/mmc/host.h>
21 #include <linux/mmc/sdio_func.h>
22
23 #include "sdio_cis.h"
24 #include "sdio_bus.h"
25
26 #ifdef CONFIG_MMC_EMBEDDED_SDIO
27 #include <linux/mmc/host.h>
28 #endif
29
30 /* show configuration fields */
31 #define sdio_config_attr(field, format_string)                          \
32 static ssize_t                                                          \
33 field##_show(struct device *dev, struct device_attribute *attr, char *buf)                              \
34 {                                                                       \
35         struct sdio_func *func;                                         \
36                                                                         \
37         func = dev_to_sdio_func (dev);                                  \
38         return sprintf (buf, format_string, func->field);               \
39 }
40
41 sdio_config_attr(class, "0x%02x\n");
42 sdio_config_attr(vendor, "0x%04x\n");
43 sdio_config_attr(device, "0x%04x\n");
44
45 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
46 {
47         struct sdio_func *func = dev_to_sdio_func (dev);
48
49         return sprintf(buf, "sdio:c%02Xv%04Xd%04X\n",
50                         func->class, func->vendor, func->device);
51 }
52
53 static struct device_attribute sdio_dev_attrs[] = {
54         __ATTR_RO(class),
55         __ATTR_RO(vendor),
56         __ATTR_RO(device),
57         __ATTR_RO(modalias),
58         __ATTR_NULL,
59 };
60
61 static const struct sdio_device_id *sdio_match_one(struct sdio_func *func,
62         const struct sdio_device_id *id)
63 {
64         if (id->class != (__u8)SDIO_ANY_ID && id->class != func->class)
65                 return NULL;
66         if (id->vendor != (__u16)SDIO_ANY_ID && id->vendor != func->vendor)
67                 return NULL;
68         if (id->device != (__u16)SDIO_ANY_ID && id->device != func->device)
69                 return NULL;
70         return id;
71 }
72
73 static const struct sdio_device_id *sdio_match_device(struct sdio_func *func,
74         struct sdio_driver *sdrv)
75 {
76         const struct sdio_device_id *ids;
77
78         ids = sdrv->id_table;
79
80         if (ids) {
81                 while (ids->class || ids->vendor || ids->device) {
82                         if (sdio_match_one(func, ids))
83                                 return ids;
84                         ids++;
85                 }
86         }
87
88         return NULL;
89 }
90
91 static int sdio_bus_match(struct device *dev, struct device_driver *drv)
92 {
93         struct sdio_func *func = dev_to_sdio_func(dev);
94         struct sdio_driver *sdrv = to_sdio_driver(drv);
95
96         if (sdio_match_device(func, sdrv))
97                 return 1;
98
99         return 0;
100 }
101
102 static int
103 sdio_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
104 {
105         struct sdio_func *func = dev_to_sdio_func(dev);
106
107         if (add_uevent_var(env,
108                         "SDIO_CLASS=%02X", func->class))
109                 return -ENOMEM;
110
111         if (add_uevent_var(env, 
112                         "SDIO_ID=%04X:%04X", func->vendor, func->device))
113                 return -ENOMEM;
114
115         if (add_uevent_var(env,
116                         "MODALIAS=sdio:c%02Xv%04Xd%04X",
117                         func->class, func->vendor, func->device))
118                 return -ENOMEM;
119
120         return 0;
121 }
122
123 static int sdio_bus_probe(struct device *dev)
124 {
125         struct sdio_driver *drv = to_sdio_driver(dev->driver);
126         struct sdio_func *func = dev_to_sdio_func(dev);
127         const struct sdio_device_id *id;
128         int ret;
129
130         id = sdio_match_device(func, drv);
131         if (!id)
132                 return -ENODEV;
133
134         /* Unbound SDIO functions are always suspended.
135          * During probe, the function is set active and the usage count
136          * is incremented.  If the driver supports runtime PM,
137          * it should call pm_runtime_put_noidle() in its probe routine and
138          * pm_runtime_get_noresume() in its remove routine.
139          */
140         if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD) {
141                 ret = pm_runtime_get_sync(dev);
142                 if (ret < 0)
143                         goto out;
144         }
145
146         /* Set the default block size so the driver is sure it's something
147          * sensible. */
148         sdio_claim_host(func);
149         ret = sdio_set_block_size(func, 0);
150         sdio_release_host(func);
151         if (ret)
152                 goto disable_runtimepm;
153
154         ret = drv->probe(func, id);
155         if (ret)
156                 goto disable_runtimepm;
157
158         return 0;
159
160 disable_runtimepm:
161         if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
162                 pm_runtime_put_noidle(dev);
163 out:
164         return ret;
165 }
166
167 static int sdio_bus_remove(struct device *dev)
168 {
169         struct sdio_driver *drv = to_sdio_driver(dev->driver);
170         struct sdio_func *func = dev_to_sdio_func(dev);
171         int ret = 0;
172
173         /* Make sure card is powered before invoking ->remove() */
174         if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD) {
175                 ret = pm_runtime_get_sync(dev);
176                 if (ret < 0)
177                         goto out;
178         }
179
180         drv->remove(func);
181
182         if (func->irq_handler) {
183                 printk(KERN_WARNING "WARNING: driver %s did not remove "
184                         "its interrupt handler!\n", drv->name);
185                 sdio_claim_host(func);
186                 sdio_release_irq(func);
187                 sdio_release_host(func);
188         }
189
190         /* First, undo the increment made directly above */
191         if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
192                 pm_runtime_put_noidle(dev);
193
194         /* Then undo the runtime PM settings in sdio_bus_probe() */
195         if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
196                 pm_runtime_put_sync(dev);
197
198 out:
199         return ret;
200 }
201
202 #ifdef CONFIG_PM_RUNTIME
203
204 static const struct dev_pm_ops sdio_bus_pm_ops = {
205         SET_RUNTIME_PM_OPS(
206                 pm_generic_runtime_suspend,
207                 pm_generic_runtime_resume,
208                 pm_generic_runtime_idle
209         )
210 };
211
212 #define SDIO_PM_OPS_PTR (&sdio_bus_pm_ops)
213
214 #else /* !CONFIG_PM_RUNTIME */
215
216 #define SDIO_PM_OPS_PTR NULL
217
218 #endif /* !CONFIG_PM_RUNTIME */
219
220 static struct bus_type sdio_bus_type = {
221         .name           = "sdio",
222         .dev_attrs      = sdio_dev_attrs,
223         .match          = sdio_bus_match,
224         .uevent         = sdio_bus_uevent,
225         .probe          = sdio_bus_probe,
226         .remove         = sdio_bus_remove,
227         .pm             = SDIO_PM_OPS_PTR,
228 };
229
230 int sdio_register_bus(void)
231 {
232         return bus_register(&sdio_bus_type);
233 }
234
235 void sdio_unregister_bus(void)
236 {
237         bus_unregister(&sdio_bus_type);
238 }
239
240 /**
241  *      sdio_register_driver - register a function driver
242  *      @drv: SDIO function driver
243  */
244 int sdio_register_driver(struct sdio_driver *drv)
245 {
246         drv->drv.name = drv->name;
247         drv->drv.bus = &sdio_bus_type;
248         return driver_register(&drv->drv);
249 }
250 EXPORT_SYMBOL_GPL(sdio_register_driver);
251
252 /**
253  *      sdio_unregister_driver - unregister a function driver
254  *      @drv: SDIO function driver
255  */
256 void sdio_unregister_driver(struct sdio_driver *drv)
257 {
258         drv->drv.bus = &sdio_bus_type;
259         driver_unregister(&drv->drv);
260 }
261 EXPORT_SYMBOL_GPL(sdio_unregister_driver);
262
263 static void sdio_release_func(struct device *dev)
264 {
265         struct sdio_func *func = dev_to_sdio_func(dev);
266
267 #ifdef CONFIG_MMC_EMBEDDED_SDIO
268         /*
269          * If this device is embedded then we never allocated
270          * cis tables for this func
271          */
272         if (!func->card->host->embedded_sdio_data.funcs)
273 #endif
274                 sdio_free_func_cis(func);
275
276         if (func->info)
277                 kfree(func->info);
278
279         kfree(func);
280 }
281
282 /*
283  * Allocate and initialise a new SDIO function structure.
284  */
285 struct sdio_func *sdio_alloc_func(struct mmc_card *card)
286 {
287         struct sdio_func *func;
288
289         func = kzalloc(sizeof(struct sdio_func), GFP_KERNEL);
290         if (!func)
291                 return ERR_PTR(-ENOMEM);
292
293         func->card = card;
294
295         device_initialize(&func->dev);
296
297         func->dev.parent = &card->dev;
298         func->dev.bus = &sdio_bus_type;
299         func->dev.release = sdio_release_func;
300
301         return func;
302 }
303
304 /*
305  * Register a new SDIO function with the driver model.
306  */
307 int sdio_add_func(struct sdio_func *func)
308 {
309         int ret;
310
311         dev_set_name(&func->dev, "%s:%d", mmc_card_id(func->card), func->num);
312
313         ret = device_add(&func->dev);
314         if (ret == 0)
315                 sdio_func_set_present(func);
316
317         return ret;
318 }
319
320 /*
321  * Unregister a SDIO function with the driver model, and
322  * (eventually) free it.
323  * This function can be called through error paths where sdio_add_func() was
324  * never executed (because a failure occurred at an earlier point).
325  */
326 void sdio_remove_func(struct sdio_func *func)
327 {
328         if (!sdio_func_present(func))
329                 return;
330
331         device_del(&func->dev);
332         put_device(&func->dev);
333 }
334