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