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