blob: 528524a22c8091107d1ca34787413c616ed3bb37 [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
San Mehat2b6be3a2008-04-14 15:22:49 -070031#ifdef CONFIG_MMC_EMBEDDED_SDIO
32#include <linux/mmc/host.h>
33#endif
34
Ulf Hansson433b7b12014-10-06 11:00:15 +020035#define to_sdio_driver(d) container_of(d, struct sdio_driver, drv)
36
Pierre Ossmanbcfe66e2007-06-17 11:42:21 +020037/* show configuration fields */
38#define sdio_config_attr(field, format_string) \
39static ssize_t \
40field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
41{ \
42 struct sdio_func *func; \
43 \
44 func = dev_to_sdio_func (dev); \
45 return sprintf (buf, format_string, func->field); \
Greg Kroah-Hartmanf24fc572013-10-06 23:55:43 -070046} \
47static DEVICE_ATTR_RO(field)
Pierre Ossmanbcfe66e2007-06-17 11:42:21 +020048
49sdio_config_attr(class, "0x%02x\n");
50sdio_config_attr(vendor, "0x%04x\n");
51sdio_config_attr(device, "0x%04x\n");
52
53static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
54{
55 struct sdio_func *func = dev_to_sdio_func (dev);
56
57 return sprintf(buf, "sdio:c%02Xv%04Xd%04X\n",
58 func->class, func->vendor, func->device);
59}
Greg Kroah-Hartmanf24fc572013-10-06 23:55:43 -070060static DEVICE_ATTR_RO(modalias);
Pierre Ossmanbcfe66e2007-06-17 11:42:21 +020061
Greg Kroah-Hartmanf24fc572013-10-06 23:55:43 -070062static struct attribute *sdio_dev_attrs[] = {
63 &dev_attr_class.attr,
64 &dev_attr_vendor.attr,
65 &dev_attr_device.attr,
66 &dev_attr_modalias.attr,
67 NULL,
Pierre Ossmanbcfe66e2007-06-17 11:42:21 +020068};
Greg Kroah-Hartmanf24fc572013-10-06 23:55:43 -070069ATTRIBUTE_GROUPS(sdio_dev);
Pierre Ossmanbcfe66e2007-06-17 11:42:21 +020070
Pierre Ossman3b38bea2007-06-16 15:54:55 +020071static const struct sdio_device_id *sdio_match_one(struct sdio_func *func,
72 const struct sdio_device_id *id)
73{
74 if (id->class != (__u8)SDIO_ANY_ID && id->class != func->class)
75 return NULL;
76 if (id->vendor != (__u16)SDIO_ANY_ID && id->vendor != func->vendor)
77 return NULL;
78 if (id->device != (__u16)SDIO_ANY_ID && id->device != func->device)
79 return NULL;
80 return id;
81}
82
83static const struct sdio_device_id *sdio_match_device(struct sdio_func *func,
84 struct sdio_driver *sdrv)
85{
86 const struct sdio_device_id *ids;
87
88 ids = sdrv->id_table;
89
90 if (ids) {
91 while (ids->class || ids->vendor || ids->device) {
92 if (sdio_match_one(func, ids))
93 return ids;
94 ids++;
95 }
96 }
97
98 return NULL;
99}
100
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200101static int sdio_bus_match(struct device *dev, struct device_driver *drv)
102{
Pierre Ossman3b38bea2007-06-16 15:54:55 +0200103 struct sdio_func *func = dev_to_sdio_func(dev);
104 struct sdio_driver *sdrv = to_sdio_driver(drv);
105
106 if (sdio_match_device(func, sdrv))
107 return 1;
108
109 return 0;
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200110}
111
112static int
Al Viro7ac03262007-10-14 05:46:09 +0100113sdio_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200114{
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200115 struct sdio_func *func = dev_to_sdio_func(dev);
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200116
Al Viro7ac03262007-10-14 05:46:09 +0100117 if (add_uevent_var(env,
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200118 "SDIO_CLASS=%02X", func->class))
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 "SDIO_ID=%04X:%04X", func->vendor, func->device))
123 return -ENOMEM;
124
Al Viro7ac03262007-10-14 05:46:09 +0100125 if (add_uevent_var(env,
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200126 "MODALIAS=sdio:c%02Xv%04Xd%04X",
127 func->class, func->vendor, func->device))
128 return -ENOMEM;
129
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200130 return 0;
131}
132
133static int sdio_bus_probe(struct device *dev)
134{
Pierre Ossman3b38bea2007-06-16 15:54:55 +0200135 struct sdio_driver *drv = to_sdio_driver(dev->driver);
136 struct sdio_func *func = dev_to_sdio_func(dev);
137 const struct sdio_device_id *id;
David Vrabel9a08f822007-08-08 14:23:48 +0100138 int ret;
Pierre Ossman3b38bea2007-06-16 15:54:55 +0200139
140 id = sdio_match_device(func, drv);
141 if (!id)
142 return -ENODEV;
143
Ulf Hansson1ef48e32015-06-01 12:18:25 +0200144 ret = dev_pm_domain_attach(dev, false);
145 if (ret == -EPROBE_DEFER)
146 return ret;
147
Ohad Ben-Cohen40bba0c2010-10-02 13:54:11 +0200148 /* Unbound SDIO functions are always suspended.
149 * During probe, the function is set active and the usage count
150 * is incremented. If the driver supports runtime PM,
151 * it should call pm_runtime_put_noidle() in its probe routine and
152 * pm_runtime_get_noresume() in its remove routine.
153 */
Ohad Ben-Cohened919b02010-11-19 09:29:09 +0200154 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD) {
155 ret = pm_runtime_get_sync(dev);
156 if (ret < 0)
Li Fei3bffb802013-04-08 09:36:39 +0800157 goto disable_runtimepm;
Ohad Ben-Cohened919b02010-11-19 09:29:09 +0200158 }
Ohad Ben-Cohen40bba0c2010-10-02 13:54:11 +0200159
David Vrabel9a08f822007-08-08 14:23:48 +0100160 /* Set the default block size so the driver is sure it's something
161 * sensible. */
162 sdio_claim_host(func);
163 ret = sdio_set_block_size(func, 0);
164 sdio_release_host(func);
165 if (ret)
Ohad Ben-Cohen40bba0c2010-10-02 13:54:11 +0200166 goto disable_runtimepm;
David Vrabel9a08f822007-08-08 14:23:48 +0100167
Ohad Ben-Cohen40bba0c2010-10-02 13:54:11 +0200168 ret = drv->probe(func, id);
169 if (ret)
170 goto disable_runtimepm;
171
172 return 0;
173
174disable_runtimepm:
Ohad Ben-Cohened919b02010-11-19 09:29:09 +0200175 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
176 pm_runtime_put_noidle(dev);
Ulf Hansson1ef48e32015-06-01 12:18:25 +0200177 dev_pm_domain_detach(dev, false);
Ohad Ben-Cohen40bba0c2010-10-02 13:54:11 +0200178 return ret;
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200179}
180
181static int sdio_bus_remove(struct device *dev)
182{
Pierre Ossman3b38bea2007-06-16 15:54:55 +0200183 struct sdio_driver *drv = to_sdio_driver(dev->driver);
184 struct sdio_func *func = dev_to_sdio_func(dev);
Ohad Ben-Cohened919b02010-11-19 09:29:09 +0200185 int ret = 0;
Ohad Ben-Cohen40bba0c2010-10-02 13:54:11 +0200186
187 /* Make sure card is powered before invoking ->remove() */
Ohad Ben-Cohenecc024412011-07-17 16:38:21 +0100188 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
189 pm_runtime_get_sync(dev);
Pierre Ossman3b38bea2007-06-16 15:54:55 +0200190
191 drv->remove(func);
192
Nicolas Pitred1496c32007-06-30 16:29:41 +0200193 if (func->irq_handler) {
Joe Perches66061102014-09-12 14:56:56 -0700194 pr_warn("WARNING: driver %s did not remove its interrupt handler!\n",
195 drv->name);
Nicolas Pitred1496c32007-06-30 16:29:41 +0200196 sdio_claim_host(func);
197 sdio_release_irq(func);
198 sdio_release_host(func);
199 }
200
Ohad Ben-Cohen40bba0c2010-10-02 13:54:11 +0200201 /* First, undo the increment made directly above */
Ohad Ben-Cohened919b02010-11-19 09:29:09 +0200202 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
203 pm_runtime_put_noidle(dev);
Ohad Ben-Cohen40bba0c2010-10-02 13:54:11 +0200204
205 /* Then undo the runtime PM settings in sdio_bus_probe() */
Ohad Ben-Cohened919b02010-11-19 09:29:09 +0200206 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
Ohad Ben-Cohen297c7f22011-06-09 23:40:27 +0000207 pm_runtime_put_sync(dev);
Ohad Ben-Cohen40bba0c2010-10-02 13:54:11 +0200208
Ulf Hansson1ef48e32015-06-01 12:18:25 +0200209 dev_pm_domain_detach(dev, false);
210
Ohad Ben-Cohen40bba0c2010-10-02 13:54:11 +0200211 return ret;
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200212}
213
Ohad Ben-Cohen80fd9332010-10-02 13:54:09 +0200214static const struct dev_pm_ops sdio_bus_pm_ops = {
Ulf Hansson573185c2014-02-28 12:49:00 +0100215 SET_SYSTEM_SLEEP_PM_OPS(pm_generic_suspend, pm_generic_resume)
Ohad Ben-Cohen80fd9332010-10-02 13:54:09 +0200216 SET_RUNTIME_PM_OPS(
217 pm_generic_runtime_suspend,
218 pm_generic_runtime_resume,
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +0200219 NULL
Ohad Ben-Cohen80fd9332010-10-02 13:54:09 +0200220 )
221};
222
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200223static struct bus_type sdio_bus_type = {
224 .name = "sdio",
Greg Kroah-Hartmanf24fc572013-10-06 23:55:43 -0700225 .dev_groups = sdio_dev_groups,
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200226 .match = sdio_bus_match,
227 .uevent = sdio_bus_uevent,
228 .probe = sdio_bus_probe,
229 .remove = sdio_bus_remove,
Ulf Hanssond99903c2014-10-06 10:28:35 +0200230 .pm = &sdio_bus_pm_ops,
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200231};
232
233int sdio_register_bus(void)
234{
235 return bus_register(&sdio_bus_type);
236}
237
238void sdio_unregister_bus(void)
239{
240 bus_unregister(&sdio_bus_type);
241}
242
Pierre Ossmanf76c8512007-05-27 12:00:02 +0200243/**
244 * sdio_register_driver - register a function driver
245 * @drv: SDIO function driver
246 */
247int sdio_register_driver(struct sdio_driver *drv)
248{
249 drv->drv.name = drv->name;
250 drv->drv.bus = &sdio_bus_type;
251 return driver_register(&drv->drv);
252}
253EXPORT_SYMBOL_GPL(sdio_register_driver);
254
255/**
256 * sdio_unregister_driver - unregister a function driver
257 * @drv: SDIO function driver
258 */
259void sdio_unregister_driver(struct sdio_driver *drv)
260{
261 drv->drv.bus = &sdio_bus_type;
262 driver_unregister(&drv->drv);
263}
264EXPORT_SYMBOL_GPL(sdio_unregister_driver);
265
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200266static void sdio_release_func(struct device *dev)
267{
268 struct sdio_func *func = dev_to_sdio_func(dev);
269
San Mehat2b6be3a2008-04-14 15:22:49 -0700270#ifdef CONFIG_MMC_EMBEDDED_SDIO
271 /*
272 * If this device is embedded then we never allocated
273 * cis tables for this func
274 */
275 if (!func->card->host->embedded_sdio_data.funcs)
276#endif
277 sdio_free_func_cis(func);
Nicolas Pitreb1538bc2007-06-16 02:06:47 -0400278
Sachin Kamat4c42d6c2012-11-20 14:43:16 +0530279 kfree(func->info);
Heiner Kallweit727a1532017-03-29 20:54:37 +0200280 kfree(func->tmpbuf);
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200281 kfree(func);
282}
283
284/*
285 * Allocate and initialise a new SDIO function structure.
286 */
287struct sdio_func *sdio_alloc_func(struct mmc_card *card)
288{
289 struct sdio_func *func;
290
Mariusz Kozlowski9f2fcf92007-08-01 00:05:24 +0200291 func = kzalloc(sizeof(struct sdio_func), GFP_KERNEL);
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200292 if (!func)
293 return ERR_PTR(-ENOMEM);
294
Heiner Kallweit727a1532017-03-29 20:54:37 +0200295 /*
296 * allocate buffer separately to make sure it's properly aligned for
297 * DMA usage (incl. 64 bit DMA)
298 */
299 func->tmpbuf = kmalloc(4, GFP_KERNEL);
300 if (!func->tmpbuf) {
301 kfree(func);
302 return ERR_PTR(-ENOMEM);
303 }
304
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200305 func->card = card;
306
307 device_initialize(&func->dev);
308
309 func->dev.parent = &card->dev;
310 func->dev.bus = &sdio_bus_type;
311 func->dev.release = sdio_release_func;
312
313 return func;
314}
315
Aaron Lueed222a2013-03-05 11:24:52 +0800316#ifdef CONFIG_ACPI
317static void sdio_acpi_set_handle(struct sdio_func *func)
318{
319 struct mmc_host *host = func->card->host;
Dan Carpenter51d34602014-10-23 14:37:00 +0300320 u64 addr = ((u64)host->slotno << 16) | func->num;
Aaron Lueed222a2013-03-05 11:24:52 +0800321
Rafael J. Wysocki9c5ad362013-11-28 23:58:28 +0100322 acpi_preset_companion(&func->dev, ACPI_COMPANION(host->parent), addr);
Aaron Lueed222a2013-03-05 11:24:52 +0800323}
324#else
325static inline void sdio_acpi_set_handle(struct sdio_func *func) {}
326#endif
327
Sascha Hauer25185f32014-06-30 11:07:25 +0200328static void sdio_set_of_node(struct sdio_func *func)
329{
330 struct mmc_host *host = func->card->host;
331
332 func->dev.of_node = mmc_of_find_child_device(host, func->num);
333}
334
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200335/*
336 * Register a new SDIO function with the driver model.
337 */
338int sdio_add_func(struct sdio_func *func)
339{
340 int ret;
341
Kay Sieversd1b26862008-11-08 21:37:46 +0100342 dev_set_name(&func->dev, "%s:%d", mmc_card_id(func->card), func->num);
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200343
Sascha Hauer25185f32014-06-30 11:07:25 +0200344 sdio_set_of_node(func);
Aaron Lueed222a2013-03-05 11:24:52 +0800345 sdio_acpi_set_handle(func);
Fu, Zhonghuiec076cd2015-12-04 21:05:56 +0800346 device_enable_async_suspend(&func->dev);
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200347 ret = device_add(&func->dev);
Ulf Hansson1ef48e32015-06-01 12:18:25 +0200348 if (ret == 0)
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200349 sdio_func_set_present(func);
350
351 return ret;
352}
353
354/*
355 * Unregister a SDIO function with the driver model, and
356 * (eventually) free it.
Daniel Drake3d10a1b2009-12-17 15:27:17 -0800357 * This function can be called through error paths where sdio_add_func() was
358 * never executed (because a failure occurred at an earlier point).
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200359 */
360void sdio_remove_func(struct sdio_func *func)
361{
Daniel Drake3d10a1b2009-12-17 15:27:17 -0800362 if (!sdio_func_present(func))
363 return;
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200364
Daniel Drake3d10a1b2009-12-17 15:27:17 -0800365 device_del(&func->dev);
Sascha Hauer25185f32014-06-30 11:07:25 +0200366 of_node_put(func->dev.of_node);
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200367 put_device(&func->dev);
368}
369