blob: d56a3b6c2fb9db689e329fc5aa91b720eaa9788b [file] [log] [blame]
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001/*
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>
Paul Gortmaker3ef77af2011-07-10 12:42:00 -040016#include <linux/export.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Ohad Ben-Cohen80fd9332010-10-02 13:54:09 +020018#include <linux/pm_runtime.h>
Ulf Hanssonf48c7672014-09-29 13:58:47 +020019#include <linux/pm_domain.h>
Aaron Lueed222a2013-03-05 11:24:52 +080020#include <linux/acpi.h>
Pierre Ossmane29a7d72007-05-26 13:48:18 +020021
22#include <linux/mmc/card.h>
Ohad Ben-Cohened919b02010-11-19 09:29:09 +020023#include <linux/mmc/host.h>
Pierre Ossmane29a7d72007-05-26 13:48:18 +020024#include <linux/mmc/sdio_func.h>
Sascha Hauer25185f32014-06-30 11:07:25 +020025#include <linux/of.h>
Pierre Ossmane29a7d72007-05-26 13:48:18 +020026
Sascha Hauer25185f32014-06-30 11:07:25 +020027#include "core.h"
Nicolas Pitreb1538bc2007-06-16 02:06:47 -040028#include "sdio_cis.h"
Pierre Ossmane29a7d72007-05-26 13:48:18 +020029#include "sdio_bus.h"
30
Ulf Hansson433b7b12014-10-06 11:00:15 +020031#define to_sdio_driver(d) container_of(d, struct sdio_driver, drv)
32
Pierre Ossmanbcfe66e2007-06-17 11:42:21 +020033/* show configuration fields */
34#define sdio_config_attr(field, format_string) \
35static ssize_t \
36field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
37{ \
38 struct sdio_func *func; \
39 \
40 func = dev_to_sdio_func (dev); \
41 return sprintf (buf, format_string, func->field); \
Greg Kroah-Hartmanf24fc572013-10-06 23:55:43 -070042} \
43static DEVICE_ATTR_RO(field)
Pierre Ossmanbcfe66e2007-06-17 11:42:21 +020044
45sdio_config_attr(class, "0x%02x\n");
46sdio_config_attr(vendor, "0x%04x\n");
47sdio_config_attr(device, "0x%04x\n");
48
49static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
50{
51 struct sdio_func *func = dev_to_sdio_func (dev);
52
53 return sprintf(buf, "sdio:c%02Xv%04Xd%04X\n",
54 func->class, func->vendor, func->device);
55}
Greg Kroah-Hartmanf24fc572013-10-06 23:55:43 -070056static DEVICE_ATTR_RO(modalias);
Pierre Ossmanbcfe66e2007-06-17 11:42:21 +020057
Greg Kroah-Hartmanf24fc572013-10-06 23:55:43 -070058static struct attribute *sdio_dev_attrs[] = {
59 &dev_attr_class.attr,
60 &dev_attr_vendor.attr,
61 &dev_attr_device.attr,
62 &dev_attr_modalias.attr,
63 NULL,
Pierre Ossmanbcfe66e2007-06-17 11:42:21 +020064};
Greg Kroah-Hartmanf24fc572013-10-06 23:55:43 -070065ATTRIBUTE_GROUPS(sdio_dev);
Pierre Ossmanbcfe66e2007-06-17 11:42:21 +020066
Pierre Ossman3b38bea2007-06-16 15:54:55 +020067static const struct sdio_device_id *sdio_match_one(struct sdio_func *func,
68 const struct sdio_device_id *id)
69{
70 if (id->class != (__u8)SDIO_ANY_ID && id->class != func->class)
71 return NULL;
72 if (id->vendor != (__u16)SDIO_ANY_ID && id->vendor != func->vendor)
73 return NULL;
74 if (id->device != (__u16)SDIO_ANY_ID && id->device != func->device)
75 return NULL;
76 return id;
77}
78
79static const struct sdio_device_id *sdio_match_device(struct sdio_func *func,
80 struct sdio_driver *sdrv)
81{
82 const struct sdio_device_id *ids;
83
84 ids = sdrv->id_table;
85
86 if (ids) {
87 while (ids->class || ids->vendor || ids->device) {
88 if (sdio_match_one(func, ids))
89 return ids;
90 ids++;
91 }
92 }
93
94 return NULL;
95}
96
Pierre Ossmane29a7d72007-05-26 13:48:18 +020097static int sdio_bus_match(struct device *dev, struct device_driver *drv)
98{
Pierre Ossman3b38bea2007-06-16 15:54:55 +020099 struct sdio_func *func = dev_to_sdio_func(dev);
100 struct sdio_driver *sdrv = to_sdio_driver(drv);
101
102 if (sdio_match_device(func, sdrv))
103 return 1;
104
105 return 0;
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200106}
107
108static int
Al Viro7ac03262007-10-14 05:46:09 +0100109sdio_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200110{
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200111 struct sdio_func *func = dev_to_sdio_func(dev);
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200112
Al Viro7ac03262007-10-14 05:46:09 +0100113 if (add_uevent_var(env,
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200114 "SDIO_CLASS=%02X", func->class))
115 return -ENOMEM;
116
Al Viro7ac03262007-10-14 05:46:09 +0100117 if (add_uevent_var(env,
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200118 "SDIO_ID=%04X:%04X", func->vendor, func->device))
119 return -ENOMEM;
120
Al Viro7ac03262007-10-14 05:46:09 +0100121 if (add_uevent_var(env,
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200122 "MODALIAS=sdio:c%02Xv%04Xd%04X",
123 func->class, func->vendor, func->device))
124 return -ENOMEM;
125
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200126 return 0;
127}
128
129static int sdio_bus_probe(struct device *dev)
130{
Pierre Ossman3b38bea2007-06-16 15:54:55 +0200131 struct sdio_driver *drv = to_sdio_driver(dev->driver);
132 struct sdio_func *func = dev_to_sdio_func(dev);
133 const struct sdio_device_id *id;
David Vrabel9a08f822007-08-08 14:23:48 +0100134 int ret;
Pierre Ossman3b38bea2007-06-16 15:54:55 +0200135
136 id = sdio_match_device(func, drv);
137 if (!id)
138 return -ENODEV;
139
Ulf Hansson1ef48e32015-06-01 12:18:25 +0200140 ret = dev_pm_domain_attach(dev, false);
141 if (ret == -EPROBE_DEFER)
142 return ret;
143
Ohad Ben-Cohen40bba0c2010-10-02 13:54:11 +0200144 /* Unbound SDIO functions are always suspended.
145 * During probe, the function is set active and the usage count
146 * is incremented. If the driver supports runtime PM,
147 * it should call pm_runtime_put_noidle() in its probe routine and
148 * pm_runtime_get_noresume() in its remove routine.
149 */
Ohad Ben-Cohened919b02010-11-19 09:29:09 +0200150 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD) {
151 ret = pm_runtime_get_sync(dev);
152 if (ret < 0)
Li Fei3bffb802013-04-08 09:36:39 +0800153 goto disable_runtimepm;
Ohad Ben-Cohened919b02010-11-19 09:29:09 +0200154 }
Ohad Ben-Cohen40bba0c2010-10-02 13:54:11 +0200155
David Vrabel9a08f822007-08-08 14:23:48 +0100156 /* Set the default block size so the driver is sure it's something
157 * sensible. */
158 sdio_claim_host(func);
159 ret = sdio_set_block_size(func, 0);
160 sdio_release_host(func);
161 if (ret)
Ohad Ben-Cohen40bba0c2010-10-02 13:54:11 +0200162 goto disable_runtimepm;
David Vrabel9a08f822007-08-08 14:23:48 +0100163
Ohad Ben-Cohen40bba0c2010-10-02 13:54:11 +0200164 ret = drv->probe(func, id);
165 if (ret)
166 goto disable_runtimepm;
167
168 return 0;
169
170disable_runtimepm:
Ohad Ben-Cohened919b02010-11-19 09:29:09 +0200171 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
172 pm_runtime_put_noidle(dev);
Ulf Hansson1ef48e32015-06-01 12:18:25 +0200173 dev_pm_domain_detach(dev, false);
Ohad Ben-Cohen40bba0c2010-10-02 13:54:11 +0200174 return ret;
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200175}
176
177static int sdio_bus_remove(struct device *dev)
178{
Pierre Ossman3b38bea2007-06-16 15:54:55 +0200179 struct sdio_driver *drv = to_sdio_driver(dev->driver);
180 struct sdio_func *func = dev_to_sdio_func(dev);
Ohad Ben-Cohened919b02010-11-19 09:29:09 +0200181 int ret = 0;
Ohad Ben-Cohen40bba0c2010-10-02 13:54:11 +0200182
183 /* Make sure card is powered before invoking ->remove() */
Ohad Ben-Cohenecc024412011-07-17 16:38:21 +0100184 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
185 pm_runtime_get_sync(dev);
Pierre Ossman3b38bea2007-06-16 15:54:55 +0200186
187 drv->remove(func);
188
Nicolas Pitred1496c32007-06-30 16:29:41 +0200189 if (func->irq_handler) {
Joe Perches66061102014-09-12 14:56:56 -0700190 pr_warn("WARNING: driver %s did not remove its interrupt handler!\n",
191 drv->name);
Nicolas Pitred1496c32007-06-30 16:29:41 +0200192 sdio_claim_host(func);
193 sdio_release_irq(func);
194 sdio_release_host(func);
195 }
196
Ohad Ben-Cohen40bba0c2010-10-02 13:54:11 +0200197 /* First, undo the increment made directly above */
Ohad Ben-Cohened919b02010-11-19 09:29:09 +0200198 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
199 pm_runtime_put_noidle(dev);
Ohad Ben-Cohen40bba0c2010-10-02 13:54:11 +0200200
201 /* Then undo the runtime PM settings in sdio_bus_probe() */
Ohad Ben-Cohened919b02010-11-19 09:29:09 +0200202 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
Ohad Ben-Cohen297c7f22011-06-09 23:40:27 +0000203 pm_runtime_put_sync(dev);
Ohad Ben-Cohen40bba0c2010-10-02 13:54:11 +0200204
Ulf Hansson1ef48e32015-06-01 12:18:25 +0200205 dev_pm_domain_detach(dev, false);
206
Ohad Ben-Cohen40bba0c2010-10-02 13:54:11 +0200207 return ret;
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200208}
209
Ohad Ben-Cohen80fd9332010-10-02 13:54:09 +0200210static const struct dev_pm_ops sdio_bus_pm_ops = {
Ulf Hansson573185c2014-02-28 12:49:00 +0100211 SET_SYSTEM_SLEEP_PM_OPS(pm_generic_suspend, pm_generic_resume)
Ohad Ben-Cohen80fd9332010-10-02 13:54:09 +0200212 SET_RUNTIME_PM_OPS(
213 pm_generic_runtime_suspend,
214 pm_generic_runtime_resume,
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +0200215 NULL
Ohad Ben-Cohen80fd9332010-10-02 13:54:09 +0200216 )
217};
218
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200219static struct bus_type sdio_bus_type = {
220 .name = "sdio",
Greg Kroah-Hartmanf24fc572013-10-06 23:55:43 -0700221 .dev_groups = sdio_dev_groups,
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200222 .match = sdio_bus_match,
223 .uevent = sdio_bus_uevent,
224 .probe = sdio_bus_probe,
225 .remove = sdio_bus_remove,
Ulf Hanssond99903c2014-10-06 10:28:35 +0200226 .pm = &sdio_bus_pm_ops,
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200227};
228
229int sdio_register_bus(void)
230{
231 return bus_register(&sdio_bus_type);
232}
233
234void sdio_unregister_bus(void)
235{
236 bus_unregister(&sdio_bus_type);
237}
238
Pierre Ossmanf76c8512007-05-27 12:00:02 +0200239/**
240 * sdio_register_driver - register a function driver
241 * @drv: SDIO function driver
242 */
243int sdio_register_driver(struct sdio_driver *drv)
244{
245 drv->drv.name = drv->name;
246 drv->drv.bus = &sdio_bus_type;
247 return driver_register(&drv->drv);
248}
249EXPORT_SYMBOL_GPL(sdio_register_driver);
250
251/**
252 * sdio_unregister_driver - unregister a function driver
253 * @drv: SDIO function driver
254 */
255void sdio_unregister_driver(struct sdio_driver *drv)
256{
257 drv->drv.bus = &sdio_bus_type;
258 driver_unregister(&drv->drv);
259}
260EXPORT_SYMBOL_GPL(sdio_unregister_driver);
261
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200262static void sdio_release_func(struct device *dev)
263{
264 struct sdio_func *func = dev_to_sdio_func(dev);
265
Pierre Ossman1a632f82007-07-30 15:15:30 +0200266 sdio_free_func_cis(func);
Nicolas Pitreb1538bc2007-06-16 02:06:47 -0400267
Sachin Kamat4c42d6c2012-11-20 14:43:16 +0530268 kfree(func->info);
Heiner Kallweit727a1532017-03-29 20:54:37 +0200269 kfree(func->tmpbuf);
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200270 kfree(func);
271}
272
273/*
274 * Allocate and initialise a new SDIO function structure.
275 */
276struct sdio_func *sdio_alloc_func(struct mmc_card *card)
277{
278 struct sdio_func *func;
279
Mariusz Kozlowski9f2fcf92007-08-01 00:05:24 +0200280 func = kzalloc(sizeof(struct sdio_func), GFP_KERNEL);
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200281 if (!func)
282 return ERR_PTR(-ENOMEM);
283
Heiner Kallweit727a1532017-03-29 20:54:37 +0200284 /*
285 * allocate buffer separately to make sure it's properly aligned for
286 * DMA usage (incl. 64 bit DMA)
287 */
288 func->tmpbuf = kmalloc(4, GFP_KERNEL);
289 if (!func->tmpbuf) {
290 kfree(func);
291 return ERR_PTR(-ENOMEM);
292 }
293
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200294 func->card = card;
295
296 device_initialize(&func->dev);
297
298 func->dev.parent = &card->dev;
299 func->dev.bus = &sdio_bus_type;
300 func->dev.release = sdio_release_func;
301
302 return func;
303}
304
Aaron Lueed222a2013-03-05 11:24:52 +0800305#ifdef CONFIG_ACPI
306static void sdio_acpi_set_handle(struct sdio_func *func)
307{
308 struct mmc_host *host = func->card->host;
Dan Carpenter51d34602014-10-23 14:37:00 +0300309 u64 addr = ((u64)host->slotno << 16) | func->num;
Aaron Lueed222a2013-03-05 11:24:52 +0800310
Rafael J. Wysocki9c5ad362013-11-28 23:58:28 +0100311 acpi_preset_companion(&func->dev, ACPI_COMPANION(host->parent), addr);
Aaron Lueed222a2013-03-05 11:24:52 +0800312}
313#else
314static inline void sdio_acpi_set_handle(struct sdio_func *func) {}
315#endif
316
Sascha Hauer25185f32014-06-30 11:07:25 +0200317static void sdio_set_of_node(struct sdio_func *func)
318{
319 struct mmc_host *host = func->card->host;
320
321 func->dev.of_node = mmc_of_find_child_device(host, func->num);
322}
323
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200324/*
325 * Register a new SDIO function with the driver model.
326 */
327int sdio_add_func(struct sdio_func *func)
328{
329 int ret;
330
Kay Sieversd1b26862008-11-08 21:37:46 +0100331 dev_set_name(&func->dev, "%s:%d", mmc_card_id(func->card), func->num);
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200332
Sascha Hauer25185f32014-06-30 11:07:25 +0200333 sdio_set_of_node(func);
Aaron Lueed222a2013-03-05 11:24:52 +0800334 sdio_acpi_set_handle(func);
Fu, Zhonghuiec076cd2015-12-04 21:05:56 +0800335 device_enable_async_suspend(&func->dev);
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200336 ret = device_add(&func->dev);
Ulf Hansson1ef48e32015-06-01 12:18:25 +0200337 if (ret == 0)
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200338 sdio_func_set_present(func);
339
340 return ret;
341}
342
343/*
344 * Unregister a SDIO function with the driver model, and
345 * (eventually) free it.
Daniel Drake3d10a1b2009-12-17 15:27:17 -0800346 * This function can be called through error paths where sdio_add_func() was
347 * never executed (because a failure occurred at an earlier point).
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200348 */
349void sdio_remove_func(struct sdio_func *func)
350{
Daniel Drake3d10a1b2009-12-17 15:27:17 -0800351 if (!sdio_func_present(func))
352 return;
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200353
Daniel Drake3d10a1b2009-12-17 15:27:17 -0800354 device_del(&func->dev);
Sascha Hauer25185f32014-06-30 11:07:25 +0200355 of_node_put(func->dev.of_node);
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200356 put_device(&func->dev);
357}
358