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