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/acpi.h>
20
21 #include <linux/mmc/card.h>
22 #include <linux/mmc/host.h>
23 #include <linux/mmc/sdio_func.h>
24
25 #include "sdio_cis.h"
26 #include "sdio_bus.h"
27
28 #ifdef CONFIG_MMC_EMBEDDED_SDIO
29 #include <linux/mmc/host.h>
30 #endif
31
32 /* show configuration fields */
33 #define sdio_config_attr(field, format_string) \
34 static ssize_t \
35 field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
36 { \
37 struct sdio_func *func; \
38 \
39 func = dev_to_sdio_func (dev); \
40 return sprintf (buf, format_string, func->field); \
41 }
42
43 sdio_config_attr(class, "0x%02x\n");
44 sdio_config_attr(vendor, "0x%04x\n");
45 sdio_config_attr(device, "0x%04x\n");
46
modalias_show(struct device * dev,struct device_attribute * attr,char * buf)47 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
48 {
49 struct sdio_func *func = dev_to_sdio_func (dev);
50
51 return sprintf(buf, "sdio:c%02Xv%04Xd%04X\n",
52 func->class, func->vendor, func->device);
53 }
54
55 static struct device_attribute sdio_dev_attrs[] = {
56 __ATTR_RO(class),
57 __ATTR_RO(vendor),
58 __ATTR_RO(device),
59 __ATTR_RO(modalias),
60 __ATTR_NULL,
61 };
62
sdio_match_one(struct sdio_func * func,const struct sdio_device_id * id)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
sdio_match_device(struct sdio_func * func,struct sdio_driver * sdrv)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
sdio_bus_match(struct device * dev,struct device_driver * drv)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
sdio_bus_uevent(struct device * dev,struct kobj_uevent_env * env)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
sdio_bus_probe(struct device * dev)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
sdio_bus_remove(struct device * dev)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_warning("WARNING: driver %s did not remove "
182 "its interrupt handler!\n", 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 #ifdef CONFIG_PM_SLEEP
pm_no_operation(struct device * dev)202 static int pm_no_operation(struct device *dev)
203 {
204 /*
205 * Prevent the PM core from calling SDIO device drivers' suspend
206 * callback routines, which it is not supposed to do, by using this
207 * empty function as the bus type suspend callaback for SDIO.
208 */
209 return 0;
210 }
211 #endif
212
213 static const struct dev_pm_ops sdio_bus_pm_ops = {
214 SET_SYSTEM_SLEEP_PM_OPS(pm_no_operation, pm_no_operation)
215 SET_RUNTIME_PM_OPS(
216 pm_generic_runtime_suspend,
217 pm_generic_runtime_resume,
218 pm_generic_runtime_idle
219 )
220 };
221
222 #define SDIO_PM_OPS_PTR (&sdio_bus_pm_ops)
223
224 #else /* !CONFIG_PM */
225
226 #define SDIO_PM_OPS_PTR NULL
227
228 #endif /* !CONFIG_PM */
229
230 static struct bus_type sdio_bus_type = {
231 .name = "sdio",
232 .dev_attrs = sdio_dev_attrs,
233 .match = sdio_bus_match,
234 .uevent = sdio_bus_uevent,
235 .probe = sdio_bus_probe,
236 .remove = sdio_bus_remove,
237 .pm = SDIO_PM_OPS_PTR,
238 };
239
sdio_register_bus(void)240 int sdio_register_bus(void)
241 {
242 return bus_register(&sdio_bus_type);
243 }
244
sdio_unregister_bus(void)245 void sdio_unregister_bus(void)
246 {
247 bus_unregister(&sdio_bus_type);
248 }
249
250 /**
251 * sdio_register_driver - register a function driver
252 * @drv: SDIO function driver
253 */
sdio_register_driver(struct sdio_driver * drv)254 int sdio_register_driver(struct sdio_driver *drv)
255 {
256 drv->drv.name = drv->name;
257 drv->drv.bus = &sdio_bus_type;
258 return driver_register(&drv->drv);
259 }
260 EXPORT_SYMBOL_GPL(sdio_register_driver);
261
262 /**
263 * sdio_unregister_driver - unregister a function driver
264 * @drv: SDIO function driver
265 */
sdio_unregister_driver(struct sdio_driver * drv)266 void sdio_unregister_driver(struct sdio_driver *drv)
267 {
268 drv->drv.bus = &sdio_bus_type;
269 driver_unregister(&drv->drv);
270 }
271 EXPORT_SYMBOL_GPL(sdio_unregister_driver);
272
sdio_release_func(struct device * dev)273 static void sdio_release_func(struct device *dev)
274 {
275 struct sdio_func *func = dev_to_sdio_func(dev);
276
277 #ifdef CONFIG_MMC_EMBEDDED_SDIO
278 /*
279 * If this device is embedded then we never allocated
280 * cis tables for this func
281 */
282 if (!func->card->host->embedded_sdio_data.funcs)
283 #endif
284 sdio_free_func_cis(func);
285
286 kfree(func->info);
287
288 kfree(func);
289 }
290
291 /*
292 * Allocate and initialise a new SDIO function structure.
293 */
sdio_alloc_func(struct mmc_card * card)294 struct sdio_func *sdio_alloc_func(struct mmc_card *card)
295 {
296 struct sdio_func *func;
297
298 func = kzalloc(sizeof(struct sdio_func), GFP_KERNEL);
299 if (!func)
300 return ERR_PTR(-ENOMEM);
301
302 func->card = card;
303
304 device_initialize(&func->dev);
305
306 func->dev.parent = &card->dev;
307 func->dev.bus = &sdio_bus_type;
308 func->dev.release = sdio_release_func;
309
310 return func;
311 }
312
313 #ifdef CONFIG_ACPI
sdio_acpi_set_handle(struct sdio_func * func)314 static void sdio_acpi_set_handle(struct sdio_func *func)
315 {
316 struct mmc_host *host = func->card->host;
317 u64 addr = (host->slotno << 16) | func->num;
318
319 ACPI_HANDLE_SET(&func->dev,
320 acpi_get_child(ACPI_HANDLE(host->parent), addr));
321 }
322 #else
sdio_acpi_set_handle(struct sdio_func * func)323 static inline void sdio_acpi_set_handle(struct sdio_func *func) {}
324 #endif
325
326 /*
327 * Register a new SDIO function with the driver model.
328 */
sdio_add_func(struct sdio_func * func)329 int sdio_add_func(struct sdio_func *func)
330 {
331 int ret;
332
333 dev_set_name(&func->dev, "%s:%d", mmc_card_id(func->card), func->num);
334
335 sdio_acpi_set_handle(func);
336 ret = device_add(&func->dev);
337 if (ret == 0) {
338 sdio_func_set_present(func);
339 acpi_dev_pm_attach(&func->dev, false);
340 }
341
342 return ret;
343 }
344
345 /*
346 * Unregister a SDIO function with the driver model, and
347 * (eventually) free it.
348 * This function can be called through error paths where sdio_add_func() was
349 * never executed (because a failure occurred at an earlier point).
350 */
sdio_remove_func(struct sdio_func * func)351 void sdio_remove_func(struct sdio_func *func)
352 {
353 if (!sdio_func_present(func))
354 return;
355
356 acpi_dev_pm_detach(&func->dev, false);
357 device_del(&func->dev);
358 put_device(&func->dev);
359 }
360
361