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 #include <linux/of.h>
26
27 #include "core.h"
28 #include "sdio_cis.h"
29 #include "sdio_bus.h"
30
31 #ifdef CONFIG_MMC_EMBEDDED_SDIO
32 #include <linux/mmc/host.h>
33 #endif
34
35 /* show configuration fields */
36 #define sdio_config_attr(field, format_string) \
37 static ssize_t \
38 field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
39 { \
40 struct sdio_func *func; \
41 \
42 func = dev_to_sdio_func (dev); \
43 return sprintf (buf, format_string, func->field); \
44 } \
45 static DEVICE_ATTR_RO(field)
46
47 sdio_config_attr(class, "0x%02x\n");
48 sdio_config_attr(vendor, "0x%04x\n");
49 sdio_config_attr(device, "0x%04x\n");
50
modalias_show(struct device * dev,struct device_attribute * attr,char * buf)51 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
52 {
53 struct sdio_func *func = dev_to_sdio_func (dev);
54
55 return sprintf(buf, "sdio:c%02Xv%04Xd%04X\n",
56 func->class, func->vendor, func->device);
57 }
58 static DEVICE_ATTR_RO(modalias);
59
60 static struct attribute *sdio_dev_attrs[] = {
61 &dev_attr_class.attr,
62 &dev_attr_vendor.attr,
63 &dev_attr_device.attr,
64 &dev_attr_modalias.attr,
65 NULL,
66 };
67 ATTRIBUTE_GROUPS(sdio_dev);
68
sdio_match_one(struct sdio_func * func,const struct sdio_device_id * id)69 static const struct sdio_device_id *sdio_match_one(struct sdio_func *func,
70 const struct sdio_device_id *id)
71 {
72 if (id->class != (__u8)SDIO_ANY_ID && id->class != func->class)
73 return NULL;
74 if (id->vendor != (__u16)SDIO_ANY_ID && id->vendor != func->vendor)
75 return NULL;
76 if (id->device != (__u16)SDIO_ANY_ID && id->device != func->device)
77 return NULL;
78 return id;
79 }
80
sdio_match_device(struct sdio_func * func,struct sdio_driver * sdrv)81 static const struct sdio_device_id *sdio_match_device(struct sdio_func *func,
82 struct sdio_driver *sdrv)
83 {
84 const struct sdio_device_id *ids;
85
86 ids = sdrv->id_table;
87
88 if (ids) {
89 while (ids->class || ids->vendor || ids->device) {
90 if (sdio_match_one(func, ids))
91 return ids;
92 ids++;
93 }
94 }
95
96 return NULL;
97 }
98
sdio_bus_match(struct device * dev,struct device_driver * drv)99 static int sdio_bus_match(struct device *dev, struct device_driver *drv)
100 {
101 struct sdio_func *func = dev_to_sdio_func(dev);
102 struct sdio_driver *sdrv = to_sdio_driver(drv);
103
104 if (sdio_match_device(func, sdrv))
105 return 1;
106
107 return 0;
108 }
109
110 static int
sdio_bus_uevent(struct device * dev,struct kobj_uevent_env * env)111 sdio_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
112 {
113 struct sdio_func *func = dev_to_sdio_func(dev);
114
115 if (add_uevent_var(env,
116 "SDIO_CLASS=%02X", func->class))
117 return -ENOMEM;
118
119 if (add_uevent_var(env,
120 "SDIO_ID=%04X:%04X", func->vendor, func->device))
121 return -ENOMEM;
122
123 if (add_uevent_var(env,
124 "MODALIAS=sdio:c%02Xv%04Xd%04X",
125 func->class, func->vendor, func->device))
126 return -ENOMEM;
127
128 return 0;
129 }
130
sdio_bus_probe(struct device * dev)131 static int sdio_bus_probe(struct device *dev)
132 {
133 struct sdio_driver *drv = to_sdio_driver(dev->driver);
134 struct sdio_func *func = dev_to_sdio_func(dev);
135 const struct sdio_device_id *id;
136 int ret;
137
138 id = sdio_match_device(func, drv);
139 if (!id)
140 return -ENODEV;
141
142 /* Unbound SDIO functions are always suspended.
143 * During probe, the function is set active and the usage count
144 * is incremented. If the driver supports runtime PM,
145 * it should call pm_runtime_put_noidle() in its probe routine and
146 * pm_runtime_get_noresume() in its remove routine.
147 */
148 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD) {
149 ret = pm_runtime_get_sync(dev);
150 if (ret < 0)
151 goto disable_runtimepm;
152 }
153
154 /* Set the default block size so the driver is sure it's something
155 * sensible. */
156 sdio_claim_host(func);
157 ret = sdio_set_block_size(func, 0);
158 sdio_release_host(func);
159 if (ret)
160 goto disable_runtimepm;
161
162 ret = drv->probe(func, id);
163 if (ret)
164 goto disable_runtimepm;
165
166 return 0;
167
168 disable_runtimepm:
169 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
170 pm_runtime_put_noidle(dev);
171 return ret;
172 }
173
sdio_bus_remove(struct device * dev)174 static int sdio_bus_remove(struct device *dev)
175 {
176 struct sdio_driver *drv = to_sdio_driver(dev->driver);
177 struct sdio_func *func = dev_to_sdio_func(dev);
178 int ret = 0;
179
180 /* Make sure card is powered before invoking ->remove() */
181 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
182 pm_runtime_get_sync(dev);
183
184 drv->remove(func);
185
186 if (func->irq_handler) {
187 pr_warn("WARNING: driver %s did not remove its interrupt handler!\n",
188 drv->name);
189 sdio_claim_host(func);
190 sdio_release_irq(func);
191 sdio_release_host(func);
192 }
193
194 /* First, undo the increment made directly above */
195 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
196 pm_runtime_put_noidle(dev);
197
198 /* Then undo the runtime PM settings in sdio_bus_probe() */
199 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
200 pm_runtime_put_sync(dev);
201
202 return ret;
203 }
204
205 #ifdef CONFIG_PM
206
207 static const struct dev_pm_ops sdio_bus_pm_ops = {
208 SET_SYSTEM_SLEEP_PM_OPS(pm_generic_suspend, pm_generic_resume)
209 SET_RUNTIME_PM_OPS(
210 pm_generic_runtime_suspend,
211 pm_generic_runtime_resume,
212 NULL
213 )
214 };
215
216 #define SDIO_PM_OPS_PTR (&sdio_bus_pm_ops)
217
218 #else /* !CONFIG_PM */
219
220 #define SDIO_PM_OPS_PTR NULL
221
222 #endif /* !CONFIG_PM */
223
224 static struct bus_type sdio_bus_type = {
225 .name = "sdio",
226 .dev_groups = sdio_dev_groups,
227 .match = sdio_bus_match,
228 .uevent = sdio_bus_uevent,
229 .probe = sdio_bus_probe,
230 .remove = sdio_bus_remove,
231 .pm = SDIO_PM_OPS_PTR,
232 };
233
sdio_register_bus(void)234 int sdio_register_bus(void)
235 {
236 return bus_register(&sdio_bus_type);
237 }
238
sdio_unregister_bus(void)239 void sdio_unregister_bus(void)
240 {
241 bus_unregister(&sdio_bus_type);
242 }
243
244 /**
245 * sdio_register_driver - register a function driver
246 * @drv: SDIO function driver
247 */
sdio_register_driver(struct sdio_driver * drv)248 int sdio_register_driver(struct sdio_driver *drv)
249 {
250 drv->drv.name = drv->name;
251 drv->drv.bus = &sdio_bus_type;
252 return driver_register(&drv->drv);
253 }
254 EXPORT_SYMBOL_GPL(sdio_register_driver);
255
256 /**
257 * sdio_unregister_driver - unregister a function driver
258 * @drv: SDIO function driver
259 */
sdio_unregister_driver(struct sdio_driver * drv)260 void sdio_unregister_driver(struct sdio_driver *drv)
261 {
262 drv->drv.bus = &sdio_bus_type;
263 driver_unregister(&drv->drv);
264 }
265 EXPORT_SYMBOL_GPL(sdio_unregister_driver);
266
sdio_release_func(struct device * dev)267 static void sdio_release_func(struct device *dev)
268 {
269 struct sdio_func *func = dev_to_sdio_func(dev);
270
271 #ifdef CONFIG_MMC_EMBEDDED_SDIO
272 /*
273 * If this device is embedded then we never allocated
274 * cis tables for this func
275 */
276 if (!func->card->host->embedded_sdio_data.funcs)
277 #endif
278 sdio_free_func_cis(func);
279
280 kfree(func->info);
281 kfree(func->tmpbuf);
282 kfree(func);
283 }
284
285 /*
286 * Allocate and initialise a new SDIO function structure.
287 */
sdio_alloc_func(struct mmc_card * card)288 struct sdio_func *sdio_alloc_func(struct mmc_card *card)
289 {
290 struct sdio_func *func;
291
292 func = kzalloc(sizeof(struct sdio_func), GFP_KERNEL);
293 if (!func)
294 return ERR_PTR(-ENOMEM);
295
296 /*
297 * allocate buffer separately to make sure it's properly aligned for
298 * DMA usage (incl. 64 bit DMA)
299 */
300 func->tmpbuf = kmalloc(4, GFP_KERNEL);
301 if (!func->tmpbuf) {
302 kfree(func);
303 return ERR_PTR(-ENOMEM);
304 }
305
306 func->card = card;
307
308 device_initialize(&func->dev);
309
310 func->dev.parent = &card->dev;
311 func->dev.bus = &sdio_bus_type;
312 func->dev.release = sdio_release_func;
313
314 return func;
315 }
316
317 #ifdef CONFIG_ACPI
sdio_acpi_set_handle(struct sdio_func * func)318 static void sdio_acpi_set_handle(struct sdio_func *func)
319 {
320 struct mmc_host *host = func->card->host;
321 u64 addr = (host->slotno << 16) | func->num;
322
323 acpi_preset_companion(&func->dev, ACPI_COMPANION(host->parent), addr);
324 }
325 #else
sdio_acpi_set_handle(struct sdio_func * func)326 static inline void sdio_acpi_set_handle(struct sdio_func *func) {}
327 #endif
328
sdio_set_of_node(struct sdio_func * func)329 static void sdio_set_of_node(struct sdio_func *func)
330 {
331 struct mmc_host *host = func->card->host;
332
333 func->dev.of_node = mmc_of_find_child_device(host, func->num);
334 }
335
336 /*
337 * Register a new SDIO function with the driver model.
338 */
sdio_add_func(struct sdio_func * func)339 int sdio_add_func(struct sdio_func *func)
340 {
341 int ret;
342
343 dev_set_name(&func->dev, "%s:%d", mmc_card_id(func->card), func->num);
344
345 sdio_set_of_node(func);
346 sdio_acpi_set_handle(func);
347 ret = device_add(&func->dev);
348 if (ret == 0) {
349 sdio_func_set_present(func);
350 dev_pm_domain_attach(&func->dev, false);
351 }
352
353 return ret;
354 }
355
356 /*
357 * Unregister a SDIO function with the driver model, and
358 * (eventually) free it.
359 * This function can be called through error paths where sdio_add_func() was
360 * never executed (because a failure occurred at an earlier point).
361 */
sdio_remove_func(struct sdio_func * func)362 void sdio_remove_func(struct sdio_func *func)
363 {
364 if (!sdio_func_present(func))
365 return;
366
367 dev_pm_domain_detach(&func->dev, false);
368 device_del(&func->dev);
369 of_node_put(func->dev.of_node);
370 put_device(&func->dev);
371 }
372
373