Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[deliverable/linux.git] / drivers / mmc / core / sdio_bus.c
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>
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>
21
22 #include <linux/mmc/card.h>
23 #include <linux/mmc/host.h>
24 #include <linux/mmc/sdio_func.h>
25
26 #include "sdio_cis.h"
27 #include "sdio_bus.h"
28
29 /* show configuration fields */
30 #define sdio_config_attr(field, format_string) \
31 static ssize_t \
32 field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
33 { \
34 struct sdio_func *func; \
35 \
36 func = dev_to_sdio_func (dev); \
37 return sprintf (buf, format_string, func->field); \
38 } \
39 static DEVICE_ATTR_RO(field)
40
41 sdio_config_attr(class, "0x%02x\n");
42 sdio_config_attr(vendor, "0x%04x\n");
43 sdio_config_attr(device, "0x%04x\n");
44
45 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
46 {
47 struct sdio_func *func = dev_to_sdio_func (dev);
48
49 return sprintf(buf, "sdio:c%02Xv%04Xd%04X\n",
50 func->class, func->vendor, func->device);
51 }
52 static DEVICE_ATTR_RO(modalias);
53
54 static struct attribute *sdio_dev_attrs[] = {
55 &dev_attr_class.attr,
56 &dev_attr_vendor.attr,
57 &dev_attr_device.attr,
58 &dev_attr_modalias.attr,
59 NULL,
60 };
61 ATTRIBUTE_GROUPS(sdio_dev);
62
63 static const struct sdio_device_id *sdio_match_one(struct sdio_func *func,
64 const struct sdio_device_id *id)
65 {
66 if (id->class != (__u8)SDIO_ANY_ID && id->class != func->class)
67 return NULL;
68 if (id->vendor != (__u16)SDIO_ANY_ID && id->vendor != func->vendor)
69 return NULL;
70 if (id->device != (__u16)SDIO_ANY_ID && id->device != func->device)
71 return NULL;
72 return id;
73 }
74
75 static const struct sdio_device_id *sdio_match_device(struct sdio_func *func,
76 struct sdio_driver *sdrv)
77 {
78 const struct sdio_device_id *ids;
79
80 ids = sdrv->id_table;
81
82 if (ids) {
83 while (ids->class || ids->vendor || ids->device) {
84 if (sdio_match_one(func, ids))
85 return ids;
86 ids++;
87 }
88 }
89
90 return NULL;
91 }
92
93 static int sdio_bus_match(struct device *dev, struct device_driver *drv)
94 {
95 struct sdio_func *func = dev_to_sdio_func(dev);
96 struct sdio_driver *sdrv = to_sdio_driver(drv);
97
98 if (sdio_match_device(func, sdrv))
99 return 1;
100
101 return 0;
102 }
103
104 static int
105 sdio_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
106 {
107 struct sdio_func *func = dev_to_sdio_func(dev);
108
109 if (add_uevent_var(env,
110 "SDIO_CLASS=%02X", func->class))
111 return -ENOMEM;
112
113 if (add_uevent_var(env,
114 "SDIO_ID=%04X:%04X", func->vendor, func->device))
115 return -ENOMEM;
116
117 if (add_uevent_var(env,
118 "MODALIAS=sdio:c%02Xv%04Xd%04X",
119 func->class, func->vendor, func->device))
120 return -ENOMEM;
121
122 return 0;
123 }
124
125 static int sdio_bus_probe(struct device *dev)
126 {
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;
130 int ret;
131
132 id = sdio_match_device(func, drv);
133 if (!id)
134 return -ENODEV;
135
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.
141 */
142 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD) {
143 ret = pm_runtime_get_sync(dev);
144 if (ret < 0)
145 goto disable_runtimepm;
146 }
147
148 /* Set the default block size so the driver is sure it's something
149 * sensible. */
150 sdio_claim_host(func);
151 ret = sdio_set_block_size(func, 0);
152 sdio_release_host(func);
153 if (ret)
154 goto disable_runtimepm;
155
156 ret = drv->probe(func, id);
157 if (ret)
158 goto disable_runtimepm;
159
160 return 0;
161
162 disable_runtimepm:
163 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
164 pm_runtime_put_noidle(dev);
165 return ret;
166 }
167
168 static int sdio_bus_remove(struct device *dev)
169 {
170 struct sdio_driver *drv = to_sdio_driver(dev->driver);
171 struct sdio_func *func = dev_to_sdio_func(dev);
172 int ret = 0;
173
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);
177
178 drv->remove(func);
179
180 if (func->irq_handler) {
181 pr_warn("WARNING: driver %s did not remove its interrupt handler!\n",
182 drv->name);
183 sdio_claim_host(func);
184 sdio_release_irq(func);
185 sdio_release_host(func);
186 }
187
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);
191
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);
195
196 return ret;
197 }
198
199 #ifdef CONFIG_PM
200
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
207 )
208 };
209
210 #define SDIO_PM_OPS_PTR (&sdio_bus_pm_ops)
211
212 #else /* !CONFIG_PM */
213
214 #define SDIO_PM_OPS_PTR NULL
215
216 #endif /* !CONFIG_PM */
217
218 static struct bus_type sdio_bus_type = {
219 .name = "sdio",
220 .dev_groups = sdio_dev_groups,
221 .match = sdio_bus_match,
222 .uevent = sdio_bus_uevent,
223 .probe = sdio_bus_probe,
224 .remove = sdio_bus_remove,
225 .pm = SDIO_PM_OPS_PTR,
226 };
227
228 int sdio_register_bus(void)
229 {
230 return bus_register(&sdio_bus_type);
231 }
232
233 void sdio_unregister_bus(void)
234 {
235 bus_unregister(&sdio_bus_type);
236 }
237
238 /**
239 * sdio_register_driver - register a function driver
240 * @drv: SDIO function driver
241 */
242 int sdio_register_driver(struct sdio_driver *drv)
243 {
244 drv->drv.name = drv->name;
245 drv->drv.bus = &sdio_bus_type;
246 return driver_register(&drv->drv);
247 }
248 EXPORT_SYMBOL_GPL(sdio_register_driver);
249
250 /**
251 * sdio_unregister_driver - unregister a function driver
252 * @drv: SDIO function driver
253 */
254 void sdio_unregister_driver(struct sdio_driver *drv)
255 {
256 drv->drv.bus = &sdio_bus_type;
257 driver_unregister(&drv->drv);
258 }
259 EXPORT_SYMBOL_GPL(sdio_unregister_driver);
260
261 static void sdio_release_func(struct device *dev)
262 {
263 struct sdio_func *func = dev_to_sdio_func(dev);
264
265 sdio_free_func_cis(func);
266
267 kfree(func->info);
268
269 kfree(func);
270 }
271
272 /*
273 * Allocate and initialise a new SDIO function structure.
274 */
275 struct sdio_func *sdio_alloc_func(struct mmc_card *card)
276 {
277 struct sdio_func *func;
278
279 func = kzalloc(sizeof(struct sdio_func), GFP_KERNEL);
280 if (!func)
281 return ERR_PTR(-ENOMEM);
282
283 func->card = card;
284
285 device_initialize(&func->dev);
286
287 func->dev.parent = &card->dev;
288 func->dev.bus = &sdio_bus_type;
289 func->dev.release = sdio_release_func;
290
291 return func;
292 }
293
294 #ifdef CONFIG_ACPI
295 static void sdio_acpi_set_handle(struct sdio_func *func)
296 {
297 struct mmc_host *host = func->card->host;
298 u64 addr = (host->slotno << 16) | func->num;
299
300 acpi_preset_companion(&func->dev, ACPI_COMPANION(host->parent), addr);
301 }
302 #else
303 static inline void sdio_acpi_set_handle(struct sdio_func *func) {}
304 #endif
305
306 /*
307 * Register a new SDIO function with the driver model.
308 */
309 int sdio_add_func(struct sdio_func *func)
310 {
311 int ret;
312
313 dev_set_name(&func->dev, "%s:%d", mmc_card_id(func->card), func->num);
314
315 sdio_acpi_set_handle(func);
316 ret = device_add(&func->dev);
317 if (ret == 0) {
318 sdio_func_set_present(func);
319 dev_pm_domain_attach(&func->dev, false);
320 }
321
322 return ret;
323 }
324
325 /*
326 * Unregister a SDIO function with the driver model, and
327 * (eventually) free it.
328 * This function can be called through error paths where sdio_add_func() was
329 * never executed (because a failure occurred at an earlier point).
330 */
331 void sdio_remove_func(struct sdio_func *func)
332 {
333 if (!sdio_func_present(func))
334 return;
335
336 dev_pm_domain_detach(&func->dev, false);
337 device_del(&func->dev);
338 put_device(&func->dev);
339 }
340
This page took 0.039127 seconds and 5 git commands to generate.