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/acpi.h>
21 #include <linux/mmc/card.h>
22 #include <linux/mmc/host.h>
23 #include <linux/mmc/sdio_func.h>
28 /* show configuration fields */
29 #define sdio_config_attr(field, format_string) \
31 field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
33 struct sdio_func *func; \
35 func = dev_to_sdio_func (dev); \
36 return sprintf (buf, format_string, func->field); \
39 sdio_config_attr(class, "0x%02x\n");
40 sdio_config_attr(vendor, "0x%04x\n");
41 sdio_config_attr(device, "0x%04x\n");
43 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
45 struct sdio_func *func = dev_to_sdio_func (dev);
47 return sprintf(buf, "sdio:c%02Xv%04Xd%04X\n",
48 func->class, func->vendor, func->device);
51 static struct device_attribute sdio_dev_attrs[] = {
59 static const struct sdio_device_id *sdio_match_one(struct sdio_func *func,
60 const struct sdio_device_id *id)
62 if (id->class != (__u8)SDIO_ANY_ID && id->class != func->class)
64 if (id->vendor != (__u16)SDIO_ANY_ID && id->vendor != func->vendor)
66 if (id->device != (__u16)SDIO_ANY_ID && id->device != func->device)
71 static const struct sdio_device_id *sdio_match_device(struct sdio_func *func,
72 struct sdio_driver *sdrv)
74 const struct sdio_device_id *ids;
79 while (ids->class || ids->vendor || ids->device) {
80 if (sdio_match_one(func, ids))
89 static int sdio_bus_match(struct device *dev, struct device_driver *drv)
91 struct sdio_func *func = dev_to_sdio_func(dev);
92 struct sdio_driver *sdrv = to_sdio_driver(drv);
94 if (sdio_match_device(func, sdrv))
101 sdio_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
103 struct sdio_func *func = dev_to_sdio_func(dev);
105 if (add_uevent_var(env,
106 "SDIO_CLASS=%02X", func->class))
109 if (add_uevent_var(env,
110 "SDIO_ID=%04X:%04X", func->vendor, func->device))
113 if (add_uevent_var(env,
114 "MODALIAS=sdio:c%02Xv%04Xd%04X",
115 func->class, func->vendor, func->device))
121 static int sdio_bus_probe(struct device *dev)
123 struct sdio_driver *drv = to_sdio_driver(dev->driver);
124 struct sdio_func *func = dev_to_sdio_func(dev);
125 const struct sdio_device_id *id;
128 id = sdio_match_device(func, drv);
132 /* Unbound SDIO functions are always suspended.
133 * During probe, the function is set active and the usage count
134 * is incremented. If the driver supports runtime PM,
135 * it should call pm_runtime_put_noidle() in its probe routine and
136 * pm_runtime_get_noresume() in its remove routine.
138 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD) {
139 ret = pm_runtime_get_sync(dev);
141 goto disable_runtimepm;
144 /* Set the default block size so the driver is sure it's something
146 sdio_claim_host(func);
147 ret = sdio_set_block_size(func, 0);
148 sdio_release_host(func);
150 goto disable_runtimepm;
152 ret = drv->probe(func, id);
154 goto disable_runtimepm;
159 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
160 pm_runtime_put_noidle(dev);
164 static int sdio_bus_remove(struct device *dev)
166 struct sdio_driver *drv = to_sdio_driver(dev->driver);
167 struct sdio_func *func = dev_to_sdio_func(dev);
170 /* Make sure card is powered before invoking ->remove() */
171 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
172 pm_runtime_get_sync(dev);
176 if (func->irq_handler) {
177 pr_warning("WARNING: driver %s did not remove "
178 "its interrupt handler!\n", drv->name);
179 sdio_claim_host(func);
180 sdio_release_irq(func);
181 sdio_release_host(func);
184 /* First, undo the increment made directly above */
185 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
186 pm_runtime_put_noidle(dev);
188 /* Then undo the runtime PM settings in sdio_bus_probe() */
189 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
190 pm_runtime_put_sync(dev);
197 #ifdef CONFIG_PM_SLEEP
198 static int pm_no_operation(struct device *dev)
201 * Prevent the PM core from calling SDIO device drivers' suspend
202 * callback routines, which it is not supposed to do, by using this
203 * empty function as the bus type suspend callaback for SDIO.
209 static const struct dev_pm_ops sdio_bus_pm_ops = {
210 SET_SYSTEM_SLEEP_PM_OPS(pm_no_operation, pm_no_operation)
212 pm_generic_runtime_suspend,
213 pm_generic_runtime_resume,
214 pm_generic_runtime_idle
218 #define SDIO_PM_OPS_PTR (&sdio_bus_pm_ops)
220 #else /* !CONFIG_PM */
222 #define SDIO_PM_OPS_PTR NULL
224 #endif /* !CONFIG_PM */
226 static struct bus_type sdio_bus_type = {
228 .dev_attrs = sdio_dev_attrs,
229 .match = sdio_bus_match,
230 .uevent = sdio_bus_uevent,
231 .probe = sdio_bus_probe,
232 .remove = sdio_bus_remove,
233 .pm = SDIO_PM_OPS_PTR,
236 int sdio_register_bus(void)
238 return bus_register(&sdio_bus_type);
241 void sdio_unregister_bus(void)
243 bus_unregister(&sdio_bus_type);
247 * sdio_register_driver - register a function driver
248 * @drv: SDIO function driver
250 int sdio_register_driver(struct sdio_driver *drv)
252 drv->drv.name = drv->name;
253 drv->drv.bus = &sdio_bus_type;
254 return driver_register(&drv->drv);
256 EXPORT_SYMBOL_GPL(sdio_register_driver);
259 * sdio_unregister_driver - unregister a function driver
260 * @drv: SDIO function driver
262 void sdio_unregister_driver(struct sdio_driver *drv)
264 drv->drv.bus = &sdio_bus_type;
265 driver_unregister(&drv->drv);
267 EXPORT_SYMBOL_GPL(sdio_unregister_driver);
269 static void sdio_release_func(struct device *dev)
271 struct sdio_func *func = dev_to_sdio_func(dev);
273 sdio_free_func_cis(func);
281 * Allocate and initialise a new SDIO function structure.
283 struct sdio_func *sdio_alloc_func(struct mmc_card *card)
285 struct sdio_func *func;
287 func = kzalloc(sizeof(struct sdio_func), GFP_KERNEL);
289 return ERR_PTR(-ENOMEM);
293 device_initialize(&func->dev);
295 func->dev.parent = &card->dev;
296 func->dev.bus = &sdio_bus_type;
297 func->dev.release = sdio_release_func;
303 static void sdio_acpi_set_handle(struct sdio_func *func)
305 struct mmc_host *host = func->card->host;
306 u64 addr = (host->slotno << 16) | func->num;
308 ACPI_HANDLE_SET(&func->dev,
309 acpi_get_child(ACPI_HANDLE(host->parent), addr));
312 static inline void sdio_acpi_set_handle(struct sdio_func *func) {}
316 * Register a new SDIO function with the driver model.
318 int sdio_add_func(struct sdio_func *func)
322 dev_set_name(&func->dev, "%s:%d", mmc_card_id(func->card), func->num);
324 sdio_acpi_set_handle(func);
325 ret = device_add(&func->dev);
327 sdio_func_set_present(func);
328 acpi_dev_pm_attach(&func->dev, false);
335 * Unregister a SDIO function with the driver model, and
336 * (eventually) free it.
337 * This function can be called through error paths where sdio_add_func() was
338 * never executed (because a failure occurred at an earlier point).
340 void sdio_remove_func(struct sdio_func *func)
342 if (!sdio_func_present(func))
345 acpi_dev_pm_detach(&func->dev, false);
346 device_del(&func->dev);
347 put_device(&func->dev);