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/slab.h>
17 #include <linux/pm_runtime.h>
19 #include <linux/mmc/card.h>
20 #include <linux/mmc/host.h>
21 #include <linux/mmc/sdio_func.h>
26 #ifdef CONFIG_MMC_EMBEDDED_SDIO
27 #include <linux/mmc/host.h>
30 /* show configuration fields */
31 #define sdio_config_attr(field, format_string) \
33 field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
35 struct sdio_func *func; \
37 func = dev_to_sdio_func (dev); \
38 return sprintf (buf, format_string, func->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);
53 static struct device_attribute sdio_dev_attrs[] = {
61 static const struct sdio_device_id *sdio_match_one(struct sdio_func *func,
62 const struct sdio_device_id *id)
64 if (id->class != (__u8)SDIO_ANY_ID && id->class != func->class)
66 if (id->vendor != (__u16)SDIO_ANY_ID && id->vendor != func->vendor)
68 if (id->device != (__u16)SDIO_ANY_ID && id->device != func->device)
73 static const struct sdio_device_id *sdio_match_device(struct sdio_func *func,
74 struct sdio_driver *sdrv)
76 const struct sdio_device_id *ids;
81 while (ids->class || ids->vendor || ids->device) {
82 if (sdio_match_one(func, ids))
91 static int sdio_bus_match(struct device *dev, struct device_driver *drv)
93 struct sdio_func *func = dev_to_sdio_func(dev);
94 struct sdio_driver *sdrv = to_sdio_driver(drv);
96 if (sdio_match_device(func, sdrv))
103 sdio_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
105 struct sdio_func *func = dev_to_sdio_func(dev);
107 if (add_uevent_var(env,
108 "SDIO_CLASS=%02X", func->class))
111 if (add_uevent_var(env,
112 "SDIO_ID=%04X:%04X", func->vendor, func->device))
115 if (add_uevent_var(env,
116 "MODALIAS=sdio:c%02Xv%04Xd%04X",
117 func->class, func->vendor, func->device))
123 static int sdio_bus_probe(struct device *dev)
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;
130 id = sdio_match_device(func, drv);
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.
140 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD) {
141 ret = pm_runtime_get_sync(dev);
146 /* Set the default block size so the driver is sure it's something
148 sdio_claim_host(func);
149 ret = sdio_set_block_size(func, 0);
150 sdio_release_host(func);
152 goto disable_runtimepm;
154 ret = drv->probe(func, id);
156 goto disable_runtimepm;
161 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
162 pm_runtime_put_noidle(dev);
167 static int sdio_bus_remove(struct device *dev)
169 struct sdio_driver *drv = to_sdio_driver(dev->driver);
170 struct sdio_func *func = dev_to_sdio_func(dev);
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);
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);
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);
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);
202 #ifdef CONFIG_PM_RUNTIME
204 static const struct dev_pm_ops sdio_bus_pm_ops = {
206 pm_generic_runtime_suspend,
207 pm_generic_runtime_resume,
208 pm_generic_runtime_idle
212 #define SDIO_PM_OPS_PTR (&sdio_bus_pm_ops)
214 #else /* !CONFIG_PM_RUNTIME */
216 #define SDIO_PM_OPS_PTR NULL
218 #endif /* !CONFIG_PM_RUNTIME */
220 static struct bus_type sdio_bus_type = {
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,
230 int sdio_register_bus(void)
232 return bus_register(&sdio_bus_type);
235 void sdio_unregister_bus(void)
237 bus_unregister(&sdio_bus_type);
241 * sdio_register_driver - register a function driver
242 * @drv: SDIO function driver
244 int sdio_register_driver(struct sdio_driver *drv)
246 drv->drv.name = drv->name;
247 drv->drv.bus = &sdio_bus_type;
248 return driver_register(&drv->drv);
250 EXPORT_SYMBOL_GPL(sdio_register_driver);
253 * sdio_unregister_driver - unregister a function driver
254 * @drv: SDIO function driver
256 void sdio_unregister_driver(struct sdio_driver *drv)
258 drv->drv.bus = &sdio_bus_type;
259 driver_unregister(&drv->drv);
261 EXPORT_SYMBOL_GPL(sdio_unregister_driver);
263 static void sdio_release_func(struct device *dev)
265 struct sdio_func *func = dev_to_sdio_func(dev);
267 #ifdef CONFIG_MMC_EMBEDDED_SDIO
269 * If this device is embedded then we never allocated
270 * cis tables for this func
272 if (!func->card->host->embedded_sdio_data.funcs)
274 sdio_free_func_cis(func);
283 * Allocate and initialise a new SDIO function structure.
285 struct sdio_func *sdio_alloc_func(struct mmc_card *card)
287 struct sdio_func *func;
289 func = kzalloc(sizeof(struct sdio_func), GFP_KERNEL);
291 return ERR_PTR(-ENOMEM);
295 device_initialize(&func->dev);
297 func->dev.parent = &card->dev;
298 func->dev.bus = &sdio_bus_type;
299 func->dev.release = sdio_release_func;
305 * Register a new SDIO function with the driver model.
307 int sdio_add_func(struct sdio_func *func)
311 dev_set_name(&func->dev, "%s:%d", mmc_card_id(func->card), func->num);
313 ret = device_add(&func->dev);
315 sdio_func_set_present(func);
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).
326 void sdio_remove_func(struct sdio_func *func)
328 if (!sdio_func_present(func))
331 device_del(&func->dev);
332 put_device(&func->dev);