2 * linux/drivers/mmc/core/sdio_bus.c
4 * Copyright 2007 Pierre Ossman
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.
11 * SDIO function driver model
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>
22 #include <linux/mmc/card.h>
23 #include <linux/mmc/host.h>
24 #include <linux/mmc/sdio_func.h>
29 /* show configuration fields */
30 #define sdio_config_attr(field, format_string) \
32 field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
34 struct sdio_func *func; \
36 func = dev_to_sdio_func (dev); \
37 return sprintf (buf, format_string, func->field); \
39 static DEVICE_ATTR_RO(field)
41 sdio_config_attr(class, "0x%02x\n");
42 sdio_config_attr(vendor, "0x%04x\n");
43 sdio_config_attr(device, "0x%04x\n");
45 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
47 struct sdio_func *func = dev_to_sdio_func (dev);
49 return sprintf(buf, "sdio:c%02Xv%04Xd%04X\n",
50 func->class, func->vendor, func->device);
52 static DEVICE_ATTR_RO(modalias);
54 static struct attribute *sdio_dev_attrs[] = {
56 &dev_attr_vendor.attr,
57 &dev_attr_device.attr,
58 &dev_attr_modalias.attr,
61 ATTRIBUTE_GROUPS(sdio_dev);
63 static const struct sdio_device_id *sdio_match_one(struct sdio_func *func,
64 const struct sdio_device_id *id)
66 if (id->class != (__u8)SDIO_ANY_ID && id->class != func->class)
68 if (id->vendor != (__u16)SDIO_ANY_ID && id->vendor != func->vendor)
70 if (id->device != (__u16)SDIO_ANY_ID && id->device != func->device)
75 static const struct sdio_device_id *sdio_match_device(struct sdio_func *func,
76 struct sdio_driver *sdrv)
78 const struct sdio_device_id *ids;
83 while (ids->class || ids->vendor || ids->device) {
84 if (sdio_match_one(func, ids))
93 static int sdio_bus_match(struct device *dev, struct device_driver *drv)
95 struct sdio_func *func = dev_to_sdio_func(dev);
96 struct sdio_driver *sdrv = to_sdio_driver(drv);
98 if (sdio_match_device(func, sdrv))
105 sdio_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
107 struct sdio_func *func = dev_to_sdio_func(dev);
109 if (add_uevent_var(env,
110 "SDIO_CLASS=%02X", func->class))
113 if (add_uevent_var(env,
114 "SDIO_ID=%04X:%04X", func->vendor, func->device))
117 if (add_uevent_var(env,
118 "MODALIAS=sdio:c%02Xv%04Xd%04X",
119 func->class, func->vendor, func->device))
125 static int sdio_bus_probe(struct device *dev)
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;
132 id = sdio_match_device(func, drv);
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.
142 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD) {
143 ret = pm_runtime_get_sync(dev);
145 goto disable_runtimepm;
148 /* Set the default block size so the driver is sure it's something
150 sdio_claim_host(func);
151 ret = sdio_set_block_size(func, 0);
152 sdio_release_host(func);
154 goto disable_runtimepm;
156 ret = drv->probe(func, id);
158 goto disable_runtimepm;
163 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
164 pm_runtime_put_noidle(dev);
168 static int sdio_bus_remove(struct device *dev)
170 struct sdio_driver *drv = to_sdio_driver(dev->driver);
171 struct sdio_func *func = dev_to_sdio_func(dev);
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);
180 if (func->irq_handler) {
181 pr_warn("WARNING: driver %s did not remove its interrupt handler!\n",
183 sdio_claim_host(func);
184 sdio_release_irq(func);
185 sdio_release_host(func);
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);
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);
199 static const struct dev_pm_ops sdio_bus_pm_ops = {
200 SET_SYSTEM_SLEEP_PM_OPS(pm_generic_suspend, pm_generic_resume)
202 pm_generic_runtime_suspend,
203 pm_generic_runtime_resume,
208 static struct bus_type sdio_bus_type = {
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,
218 int sdio_register_bus(void)
220 return bus_register(&sdio_bus_type);
223 void sdio_unregister_bus(void)
225 bus_unregister(&sdio_bus_type);
229 * sdio_register_driver - register a function driver
230 * @drv: SDIO function driver
232 int sdio_register_driver(struct sdio_driver *drv)
234 drv->drv.name = drv->name;
235 drv->drv.bus = &sdio_bus_type;
236 return driver_register(&drv->drv);
238 EXPORT_SYMBOL_GPL(sdio_register_driver);
241 * sdio_unregister_driver - unregister a function driver
242 * @drv: SDIO function driver
244 void sdio_unregister_driver(struct sdio_driver *drv)
246 drv->drv.bus = &sdio_bus_type;
247 driver_unregister(&drv->drv);
249 EXPORT_SYMBOL_GPL(sdio_unregister_driver);
251 static void sdio_release_func(struct device *dev)
253 struct sdio_func *func = dev_to_sdio_func(dev);
255 sdio_free_func_cis(func);
263 * Allocate and initialise a new SDIO function structure.
265 struct sdio_func *sdio_alloc_func(struct mmc_card *card)
267 struct sdio_func *func;
269 func = kzalloc(sizeof(struct sdio_func), GFP_KERNEL);
271 return ERR_PTR(-ENOMEM);
275 device_initialize(&func->dev);
277 func->dev.parent = &card->dev;
278 func->dev.bus = &sdio_bus_type;
279 func->dev.release = sdio_release_func;
285 static void sdio_acpi_set_handle(struct sdio_func *func)
287 struct mmc_host *host = func->card->host;
288 u64 addr = (host->slotno << 16) | func->num;
290 acpi_preset_companion(&func->dev, ACPI_COMPANION(host->parent), addr);
293 static inline void sdio_acpi_set_handle(struct sdio_func *func) {}
297 * Register a new SDIO function with the driver model.
299 int sdio_add_func(struct sdio_func *func)
303 dev_set_name(&func->dev, "%s:%d", mmc_card_id(func->card), func->num);
305 sdio_acpi_set_handle(func);
306 ret = device_add(&func->dev);
308 sdio_func_set_present(func);
309 dev_pm_domain_attach(&func->dev, false);
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).
321 void sdio_remove_func(struct sdio_func *func)
323 if (!sdio_func_present(func))
326 dev_pm_domain_detach(&func->dev, false);
327 device_del(&func->dev);
328 put_device(&func->dev);