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 "card.h"
29 #include "sdio_cis.h"
30 #include "sdio_bus.h"
31
32 #ifdef CONFIG_MMC_EMBEDDED_SDIO
33 #include <linux/mmc/host.h>
34 #endif
35
36 #define to_sdio_driver(d) container_of(d, struct sdio_driver, drv)
37
38 /* show configuration fields */
39 #define sdio_config_attr(field, format_string) \
40 static ssize_t \
41 field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
42 { \
43 struct sdio_func *func; \
44 \
45 func = dev_to_sdio_func (dev); \
46 return sprintf (buf, format_string, func->field); \
47 } \
48 static DEVICE_ATTR_RO(field)
49
50 sdio_config_attr(class, "0x%02x\n");
51 sdio_config_attr(vendor, "0x%04x\n");
52 sdio_config_attr(device, "0x%04x\n");
53
modalias_show(struct device * dev,struct device_attribute * attr,char * buf)54 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
55 {
56 struct sdio_func *func = dev_to_sdio_func (dev);
57
58 return sprintf(buf, "sdio:c%02Xv%04Xd%04X\n",
59 func->class, func->vendor, func->device);
60 }
61 static DEVICE_ATTR_RO(modalias);
62
63 static struct attribute *sdio_dev_attrs[] = {
64 &dev_attr_class.attr,
65 &dev_attr_vendor.attr,
66 &dev_attr_device.attr,
67 &dev_attr_modalias.attr,
68 NULL,
69 };
70 ATTRIBUTE_GROUPS(sdio_dev);
71
sdio_match_one(struct sdio_func * func,const struct sdio_device_id * id)72 static const struct sdio_device_id *sdio_match_one(struct sdio_func *func,
73 const struct sdio_device_id *id)
74 {
75 if (id->class != (__u8)SDIO_ANY_ID && id->class != func->class)
76 return NULL;
77 if (id->vendor != (__u16)SDIO_ANY_ID && id->vendor != func->vendor)
78 return NULL;
79 if (id->device != (__u16)SDIO_ANY_ID && id->device != func->device)
80 return NULL;
81 return id;
82 }
83
sdio_match_device(struct sdio_func * func,struct sdio_driver * sdrv)84 static const struct sdio_device_id *sdio_match_device(struct sdio_func *func,
85 struct sdio_driver *sdrv)
86 {
87 const struct sdio_device_id *ids;
88
89 ids = sdrv->id_table;
90
91 if (ids) {
92 while (ids->class || ids->vendor || ids->device) {
93 if (sdio_match_one(func, ids))
94 return ids;
95 ids++;
96 }
97 }
98
99 return NULL;
100 }
101
sdio_bus_match(struct device * dev,struct device_driver * drv)102 static int sdio_bus_match(struct device *dev, struct device_driver *drv)
103 {
104 struct sdio_func *func = dev_to_sdio_func(dev);
105 struct sdio_driver *sdrv = to_sdio_driver(drv);
106
107 if (sdio_match_device(func, sdrv))
108 return 1;
109
110 return 0;
111 }
112
113 static int
sdio_bus_uevent(struct device * dev,struct kobj_uevent_env * env)114 sdio_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
115 {
116 struct sdio_func *func = dev_to_sdio_func(dev);
117
118 if (add_uevent_var(env,
119 "SDIO_CLASS=%02X", func->class))
120 return -ENOMEM;
121
122 if (add_uevent_var(env,
123 "SDIO_ID=%04X:%04X", func->vendor, func->device))
124 return -ENOMEM;
125
126 if (add_uevent_var(env,
127 "MODALIAS=sdio:c%02Xv%04Xd%04X",
128 func->class, func->vendor, func->device))
129 return -ENOMEM;
130
131 return 0;
132 }
133
sdio_bus_probe(struct device * dev)134 static int sdio_bus_probe(struct device *dev)
135 {
136 struct sdio_driver *drv = to_sdio_driver(dev->driver);
137 struct sdio_func *func = dev_to_sdio_func(dev);
138 const struct sdio_device_id *id;
139 int ret;
140
141 id = sdio_match_device(func, drv);
142 if (!id)
143 return -ENODEV;
144
145 ret = dev_pm_domain_attach(dev, false);
146 if (ret == -EPROBE_DEFER)
147 return ret;
148
149 /* Unbound SDIO functions are always suspended.
150 * During probe, the function is set active and the usage count
151 * is incremented. If the driver supports runtime PM,
152 * it should call pm_runtime_put_noidle() in its probe routine and
153 * pm_runtime_get_noresume() in its remove routine.
154 */
155 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD) {
156 ret = pm_runtime_get_sync(dev);
157 if (ret < 0)
158 goto disable_runtimepm;
159 }
160
161 /* Set the default block size so the driver is sure it's something
162 * sensible. */
163 sdio_claim_host(func);
164 ret = sdio_set_block_size(func, 0);
165 sdio_release_host(func);
166 if (ret)
167 goto disable_runtimepm;
168
169 ret = drv->probe(func, id);
170 if (ret)
171 goto disable_runtimepm;
172
173 return 0;
174
175 disable_runtimepm:
176 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
177 pm_runtime_put_noidle(dev);
178 dev_pm_domain_detach(dev, false);
179 return ret;
180 }
181
sdio_bus_remove(struct device * dev)182 static int sdio_bus_remove(struct device *dev)
183 {
184 struct sdio_driver *drv = to_sdio_driver(dev->driver);
185 struct sdio_func *func = dev_to_sdio_func(dev);
186 int ret = 0;
187
188 /* Make sure card is powered before invoking ->remove() */
189 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
190 pm_runtime_get_sync(dev);
191
192 drv->remove(func);
193
194 if (func->irq_handler) {
195 pr_warn("WARNING: driver %s did not remove its interrupt handler!\n",
196 drv->name);
197 sdio_claim_host(func);
198 sdio_release_irq(func);
199 sdio_release_host(func);
200 }
201
202 /* First, undo the increment made directly above */
203 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
204 pm_runtime_put_noidle(dev);
205
206 /* Then undo the runtime PM settings in sdio_bus_probe() */
207 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
208 pm_runtime_put_sync(dev);
209
210 dev_pm_domain_detach(dev, false);
211
212 return ret;
213 }
214
215 static const struct dev_pm_ops sdio_bus_pm_ops = {
216 SET_SYSTEM_SLEEP_PM_OPS(pm_generic_suspend, pm_generic_resume)
217 SET_RUNTIME_PM_OPS(
218 pm_generic_runtime_suspend,
219 pm_generic_runtime_resume,
220 NULL
221 )
222 };
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_bus_pm_ops,
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 = ((u64)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 device_enable_async_suspend(&func->dev);
348 ret = device_add(&func->dev);
349 if (ret == 0)
350 sdio_func_set_present(func);
351
352 return ret;
353 }
354
355 /*
356 * Unregister a SDIO function with the driver model, and
357 * (eventually) free it.
358 * This function can be called through error paths where sdio_add_func() was
359 * never executed (because a failure occurred at an earlier point).
360 */
sdio_remove_func(struct sdio_func * func)361 void sdio_remove_func(struct sdio_func *func)
362 {
363 if (!sdio_func_present(func))
364 return;
365
366 device_del(&func->dev);
367 of_node_put(func->dev.of_node);
368 put_device(&func->dev);
369 }
370
371