Yama: do not modify global sysctl table entry
[linux/fpc-iii.git] / drivers / mmc / core / sdio_bus.c
blob60885316afbae7051390b0c86fcf5659173b9376
1 /*
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>
26 #include "sdio_cis.h"
27 #include "sdio_bus.h"
29 #define to_sdio_driver(d) container_of(d, struct sdio_driver, drv)
31 /* show configuration fields */
32 #define sdio_config_attr(field, format_string) \
33 static ssize_t \
34 field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
35 { \
36 struct sdio_func *func; \
38 func = dev_to_sdio_func (dev); \
39 return sprintf (buf, format_string, func->field); \
40 } \
41 static DEVICE_ATTR_RO(field)
43 sdio_config_attr(class, "0x%02x\n");
44 sdio_config_attr(vendor, "0x%04x\n");
45 sdio_config_attr(device, "0x%04x\n");
47 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
49 struct sdio_func *func = dev_to_sdio_func (dev);
51 return sprintf(buf, "sdio:c%02Xv%04Xd%04X\n",
52 func->class, func->vendor, func->device);
54 static DEVICE_ATTR_RO(modalias);
56 static struct attribute *sdio_dev_attrs[] = {
57 &dev_attr_class.attr,
58 &dev_attr_vendor.attr,
59 &dev_attr_device.attr,
60 &dev_attr_modalias.attr,
61 NULL,
63 ATTRIBUTE_GROUPS(sdio_dev);
65 static const struct sdio_device_id *sdio_match_one(struct sdio_func *func,
66 const struct sdio_device_id *id)
68 if (id->class != (__u8)SDIO_ANY_ID && id->class != func->class)
69 return NULL;
70 if (id->vendor != (__u16)SDIO_ANY_ID && id->vendor != func->vendor)
71 return NULL;
72 if (id->device != (__u16)SDIO_ANY_ID && id->device != func->device)
73 return NULL;
74 return id;
77 static const struct sdio_device_id *sdio_match_device(struct sdio_func *func,
78 struct sdio_driver *sdrv)
80 const struct sdio_device_id *ids;
82 ids = sdrv->id_table;
84 if (ids) {
85 while (ids->class || ids->vendor || ids->device) {
86 if (sdio_match_one(func, ids))
87 return ids;
88 ids++;
92 return NULL;
95 static int sdio_bus_match(struct device *dev, struct device_driver *drv)
97 struct sdio_func *func = dev_to_sdio_func(dev);
98 struct sdio_driver *sdrv = to_sdio_driver(drv);
100 if (sdio_match_device(func, sdrv))
101 return 1;
103 return 0;
106 static int
107 sdio_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
109 struct sdio_func *func = dev_to_sdio_func(dev);
111 if (add_uevent_var(env,
112 "SDIO_CLASS=%02X", func->class))
113 return -ENOMEM;
115 if (add_uevent_var(env,
116 "SDIO_ID=%04X:%04X", func->vendor, func->device))
117 return -ENOMEM;
119 if (add_uevent_var(env,
120 "MODALIAS=sdio:c%02Xv%04Xd%04X",
121 func->class, func->vendor, func->device))
122 return -ENOMEM;
124 return 0;
127 static int sdio_bus_probe(struct device *dev)
129 struct sdio_driver *drv = to_sdio_driver(dev->driver);
130 struct sdio_func *func = dev_to_sdio_func(dev);
131 const struct sdio_device_id *id;
132 int ret;
134 id = sdio_match_device(func, drv);
135 if (!id)
136 return -ENODEV;
138 /* Unbound SDIO functions are always suspended.
139 * During probe, the function is set active and the usage count
140 * is incremented. If the driver supports runtime PM,
141 * it should call pm_runtime_put_noidle() in its probe routine and
142 * pm_runtime_get_noresume() in its remove routine.
144 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD) {
145 ret = pm_runtime_get_sync(dev);
146 if (ret < 0)
147 goto disable_runtimepm;
150 /* Set the default block size so the driver is sure it's something
151 * sensible. */
152 sdio_claim_host(func);
153 ret = sdio_set_block_size(func, 0);
154 sdio_release_host(func);
155 if (ret)
156 goto disable_runtimepm;
158 ret = drv->probe(func, id);
159 if (ret)
160 goto disable_runtimepm;
162 return 0;
164 disable_runtimepm:
165 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
166 pm_runtime_put_noidle(dev);
167 return ret;
170 static int sdio_bus_remove(struct device *dev)
172 struct sdio_driver *drv = to_sdio_driver(dev->driver);
173 struct sdio_func *func = dev_to_sdio_func(dev);
174 int ret = 0;
176 /* Make sure card is powered before invoking ->remove() */
177 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
178 pm_runtime_get_sync(dev);
180 drv->remove(func);
182 if (func->irq_handler) {
183 pr_warn("WARNING: driver %s did not remove its interrupt handler!\n",
184 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);
198 return ret;
201 static const struct dev_pm_ops sdio_bus_pm_ops = {
202 SET_SYSTEM_SLEEP_PM_OPS(pm_generic_suspend, pm_generic_resume)
203 SET_RUNTIME_PM_OPS(
204 pm_generic_runtime_suspend,
205 pm_generic_runtime_resume,
206 NULL
210 static struct bus_type sdio_bus_type = {
211 .name = "sdio",
212 .dev_groups = sdio_dev_groups,
213 .match = sdio_bus_match,
214 .uevent = sdio_bus_uevent,
215 .probe = sdio_bus_probe,
216 .remove = sdio_bus_remove,
217 .pm = &sdio_bus_pm_ops,
220 int sdio_register_bus(void)
222 return bus_register(&sdio_bus_type);
225 void sdio_unregister_bus(void)
227 bus_unregister(&sdio_bus_type);
231 * sdio_register_driver - register a function driver
232 * @drv: SDIO function driver
234 int sdio_register_driver(struct sdio_driver *drv)
236 drv->drv.name = drv->name;
237 drv->drv.bus = &sdio_bus_type;
238 return driver_register(&drv->drv);
240 EXPORT_SYMBOL_GPL(sdio_register_driver);
243 * sdio_unregister_driver - unregister a function driver
244 * @drv: SDIO function driver
246 void sdio_unregister_driver(struct sdio_driver *drv)
248 drv->drv.bus = &sdio_bus_type;
249 driver_unregister(&drv->drv);
251 EXPORT_SYMBOL_GPL(sdio_unregister_driver);
253 static void sdio_release_func(struct device *dev)
255 struct sdio_func *func = dev_to_sdio_func(dev);
257 sdio_free_func_cis(func);
259 kfree(func->info);
261 kfree(func);
265 * Allocate and initialise a new SDIO function structure.
267 struct sdio_func *sdio_alloc_func(struct mmc_card *card)
269 struct sdio_func *func;
271 func = kzalloc(sizeof(struct sdio_func), GFP_KERNEL);
272 if (!func)
273 return ERR_PTR(-ENOMEM);
275 func->card = card;
277 device_initialize(&func->dev);
279 func->dev.parent = &card->dev;
280 func->dev.bus = &sdio_bus_type;
281 func->dev.release = sdio_release_func;
283 return func;
286 #ifdef CONFIG_ACPI
287 static void sdio_acpi_set_handle(struct sdio_func *func)
289 struct mmc_host *host = func->card->host;
290 u64 addr = ((u64)host->slotno << 16) | func->num;
292 acpi_preset_companion(&func->dev, ACPI_COMPANION(host->parent), addr);
294 #else
295 static inline void sdio_acpi_set_handle(struct sdio_func *func) {}
296 #endif
299 * Register a new SDIO function with the driver model.
301 int sdio_add_func(struct sdio_func *func)
303 int ret;
305 dev_set_name(&func->dev, "%s:%d", mmc_card_id(func->card), func->num);
307 sdio_acpi_set_handle(func);
308 ret = device_add(&func->dev);
309 if (ret == 0) {
310 sdio_func_set_present(func);
311 dev_pm_domain_attach(&func->dev, false);
314 return ret;
318 * Unregister a SDIO function with the driver model, and
319 * (eventually) free it.
320 * This function can be called through error paths where sdio_add_func() was
321 * never executed (because a failure occurred at an earlier point).
323 void sdio_remove_func(struct sdio_func *func)
325 if (!sdio_func_present(func))
326 return;
328 dev_pm_domain_detach(&func->dev, false);
329 device_del(&func->dev);
330 put_device(&func->dev);