xref: /linux/drivers/mmc/core/bus.c (revision a4eb44a6435d6d8f9e642407a4a06f65eb90ca04)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  linux/drivers/mmc/core/bus.c
4  *
5  *  Copyright (C) 2003 Russell King, All Rights Reserved.
6  *  Copyright (C) 2007 Pierre Ossman
7  *
8  *  MMC card bus driver model
9  */
10 
11 #include <linux/export.h>
12 #include <linux/device.h>
13 #include <linux/err.h>
14 #include <linux/slab.h>
15 #include <linux/stat.h>
16 #include <linux/of.h>
17 #include <linux/pm_runtime.h>
18 
19 #include <linux/mmc/card.h>
20 #include <linux/mmc/host.h>
21 
22 #include "core.h"
23 #include "card.h"
24 #include "host.h"
25 #include "sdio_cis.h"
26 #include "bus.h"
27 
28 #define to_mmc_driver(d)	container_of(d, struct mmc_driver, drv)
29 
30 static ssize_t type_show(struct device *dev,
31 	struct device_attribute *attr, char *buf)
32 {
33 	struct mmc_card *card = mmc_dev_to_card(dev);
34 
35 	switch (card->type) {
36 	case MMC_TYPE_MMC:
37 		return sprintf(buf, "MMC\n");
38 	case MMC_TYPE_SD:
39 		return sprintf(buf, "SD\n");
40 	case MMC_TYPE_SDIO:
41 		return sprintf(buf, "SDIO\n");
42 	case MMC_TYPE_SD_COMBO:
43 		return sprintf(buf, "SDcombo\n");
44 	default:
45 		return -EFAULT;
46 	}
47 }
48 static DEVICE_ATTR_RO(type);
49 
50 static struct attribute *mmc_dev_attrs[] = {
51 	&dev_attr_type.attr,
52 	NULL,
53 };
54 ATTRIBUTE_GROUPS(mmc_dev);
55 
56 static int
57 mmc_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
58 {
59 	struct mmc_card *card = mmc_dev_to_card(dev);
60 	const char *type;
61 	unsigned int i;
62 	int retval = 0;
63 
64 	switch (card->type) {
65 	case MMC_TYPE_MMC:
66 		type = "MMC";
67 		break;
68 	case MMC_TYPE_SD:
69 		type = "SD";
70 		break;
71 	case MMC_TYPE_SDIO:
72 		type = "SDIO";
73 		break;
74 	case MMC_TYPE_SD_COMBO:
75 		type = "SDcombo";
76 		break;
77 	default:
78 		type = NULL;
79 	}
80 
81 	if (type) {
82 		retval = add_uevent_var(env, "MMC_TYPE=%s", type);
83 		if (retval)
84 			return retval;
85 	}
86 
87 	if (card->type == MMC_TYPE_SDIO || card->type == MMC_TYPE_SD_COMBO) {
88 		retval = add_uevent_var(env, "SDIO_ID=%04X:%04X",
89 					card->cis.vendor, card->cis.device);
90 		if (retval)
91 			return retval;
92 
93 		retval = add_uevent_var(env, "SDIO_REVISION=%u.%u",
94 					card->major_rev, card->minor_rev);
95 		if (retval)
96 			return retval;
97 
98 		for (i = 0; i < card->num_info; i++) {
99 			retval = add_uevent_var(env, "SDIO_INFO%u=%s", i+1, card->info[i]);
100 			if (retval)
101 				return retval;
102 		}
103 	}
104 
105 	/*
106 	 * SDIO (non-combo) cards are not handled by mmc_block driver and do not
107 	 * have accessible CID register which used by mmc_card_name() function.
108 	 */
109 	if (card->type == MMC_TYPE_SDIO)
110 		return 0;
111 
112 	retval = add_uevent_var(env, "MMC_NAME=%s", mmc_card_name(card));
113 	if (retval)
114 		return retval;
115 
116 	/*
117 	 * Request the mmc_block device.  Note: that this is a direct request
118 	 * for the module it carries no information as to what is inserted.
119 	 */
120 	retval = add_uevent_var(env, "MODALIAS=mmc:block");
121 
122 	return retval;
123 }
124 
125 static int mmc_bus_probe(struct device *dev)
126 {
127 	struct mmc_driver *drv = to_mmc_driver(dev->driver);
128 	struct mmc_card *card = mmc_dev_to_card(dev);
129 
130 	return drv->probe(card);
131 }
132 
133 static void mmc_bus_remove(struct device *dev)
134 {
135 	struct mmc_driver *drv = to_mmc_driver(dev->driver);
136 	struct mmc_card *card = mmc_dev_to_card(dev);
137 
138 	drv->remove(card);
139 }
140 
141 static void mmc_bus_shutdown(struct device *dev)
142 {
143 	struct mmc_driver *drv = to_mmc_driver(dev->driver);
144 	struct mmc_card *card = mmc_dev_to_card(dev);
145 	struct mmc_host *host = card->host;
146 	int ret;
147 
148 	if (dev->driver && drv->shutdown)
149 		drv->shutdown(card);
150 
151 	if (host->bus_ops->shutdown) {
152 		ret = host->bus_ops->shutdown(host);
153 		if (ret)
154 			pr_warn("%s: error %d during shutdown\n",
155 				mmc_hostname(host), ret);
156 	}
157 }
158 
159 #ifdef CONFIG_PM_SLEEP
160 static int mmc_bus_suspend(struct device *dev)
161 {
162 	struct mmc_card *card = mmc_dev_to_card(dev);
163 	struct mmc_host *host = card->host;
164 	int ret;
165 
166 	ret = pm_generic_suspend(dev);
167 	if (ret)
168 		return ret;
169 
170 	ret = host->bus_ops->suspend(host);
171 	if (ret)
172 		pm_generic_resume(dev);
173 
174 	return ret;
175 }
176 
177 static int mmc_bus_resume(struct device *dev)
178 {
179 	struct mmc_card *card = mmc_dev_to_card(dev);
180 	struct mmc_host *host = card->host;
181 	int ret;
182 
183 	ret = host->bus_ops->resume(host);
184 	if (ret)
185 		pr_warn("%s: error %d during resume (card was removed?)\n",
186 			mmc_hostname(host), ret);
187 
188 	ret = pm_generic_resume(dev);
189 	return ret;
190 }
191 #endif
192 
193 #ifdef CONFIG_PM
194 static int mmc_runtime_suspend(struct device *dev)
195 {
196 	struct mmc_card *card = mmc_dev_to_card(dev);
197 	struct mmc_host *host = card->host;
198 
199 	return host->bus_ops->runtime_suspend(host);
200 }
201 
202 static int mmc_runtime_resume(struct device *dev)
203 {
204 	struct mmc_card *card = mmc_dev_to_card(dev);
205 	struct mmc_host *host = card->host;
206 
207 	return host->bus_ops->runtime_resume(host);
208 }
209 #endif /* !CONFIG_PM */
210 
211 static const struct dev_pm_ops mmc_bus_pm_ops = {
212 	SET_RUNTIME_PM_OPS(mmc_runtime_suspend, mmc_runtime_resume, NULL)
213 	SET_SYSTEM_SLEEP_PM_OPS(mmc_bus_suspend, mmc_bus_resume)
214 };
215 
216 static struct bus_type mmc_bus_type = {
217 	.name		= "mmc",
218 	.dev_groups	= mmc_dev_groups,
219 	.uevent		= mmc_bus_uevent,
220 	.probe		= mmc_bus_probe,
221 	.remove		= mmc_bus_remove,
222 	.shutdown	= mmc_bus_shutdown,
223 	.pm		= &mmc_bus_pm_ops,
224 };
225 
226 int mmc_register_bus(void)
227 {
228 	return bus_register(&mmc_bus_type);
229 }
230 
231 void mmc_unregister_bus(void)
232 {
233 	bus_unregister(&mmc_bus_type);
234 }
235 
236 /**
237  *	mmc_register_driver - register a media driver
238  *	@drv: MMC media driver
239  */
240 int mmc_register_driver(struct mmc_driver *drv)
241 {
242 	drv->drv.bus = &mmc_bus_type;
243 	return driver_register(&drv->drv);
244 }
245 
246 EXPORT_SYMBOL(mmc_register_driver);
247 
248 /**
249  *	mmc_unregister_driver - unregister a media driver
250  *	@drv: MMC media driver
251  */
252 void mmc_unregister_driver(struct mmc_driver *drv)
253 {
254 	drv->drv.bus = &mmc_bus_type;
255 	driver_unregister(&drv->drv);
256 }
257 
258 EXPORT_SYMBOL(mmc_unregister_driver);
259 
260 static void mmc_release_card(struct device *dev)
261 {
262 	struct mmc_card *card = mmc_dev_to_card(dev);
263 
264 	sdio_free_common_cis(card);
265 
266 	kfree(card->info);
267 
268 	kfree(card);
269 }
270 
271 /*
272  * Allocate and initialise a new MMC card structure.
273  */
274 struct mmc_card *mmc_alloc_card(struct mmc_host *host, struct device_type *type)
275 {
276 	struct mmc_card *card;
277 
278 	card = kzalloc(sizeof(struct mmc_card), GFP_KERNEL);
279 	if (!card)
280 		return ERR_PTR(-ENOMEM);
281 
282 	card->host = host;
283 
284 	device_initialize(&card->dev);
285 
286 	card->dev.parent = mmc_classdev(host);
287 	card->dev.bus = &mmc_bus_type;
288 	card->dev.release = mmc_release_card;
289 	card->dev.type = type;
290 
291 	return card;
292 }
293 
294 /*
295  * Register a new MMC card with the driver model.
296  */
297 int mmc_add_card(struct mmc_card *card)
298 {
299 	int ret;
300 	const char *type;
301 	const char *uhs_bus_speed_mode = "";
302 	static const char *const uhs_speeds[] = {
303 		[UHS_SDR12_BUS_SPEED] = "SDR12 ",
304 		[UHS_SDR25_BUS_SPEED] = "SDR25 ",
305 		[UHS_SDR50_BUS_SPEED] = "SDR50 ",
306 		[UHS_SDR104_BUS_SPEED] = "SDR104 ",
307 		[UHS_DDR50_BUS_SPEED] = "DDR50 ",
308 	};
309 
310 
311 	dev_set_name(&card->dev, "%s:%04x", mmc_hostname(card->host), card->rca);
312 
313 	switch (card->type) {
314 	case MMC_TYPE_MMC:
315 		type = "MMC";
316 		break;
317 	case MMC_TYPE_SD:
318 		type = "SD";
319 		if (mmc_card_blockaddr(card)) {
320 			if (mmc_card_ext_capacity(card))
321 				type = "SDXC";
322 			else
323 				type = "SDHC";
324 		}
325 		break;
326 	case MMC_TYPE_SDIO:
327 		type = "SDIO";
328 		break;
329 	case MMC_TYPE_SD_COMBO:
330 		type = "SD-combo";
331 		if (mmc_card_blockaddr(card))
332 			type = "SDHC-combo";
333 		break;
334 	default:
335 		type = "?";
336 		break;
337 	}
338 
339 	if (mmc_card_uhs(card) &&
340 		(card->sd_bus_speed < ARRAY_SIZE(uhs_speeds)))
341 		uhs_bus_speed_mode = uhs_speeds[card->sd_bus_speed];
342 
343 	if (mmc_host_is_spi(card->host)) {
344 		pr_info("%s: new %s%s%s card on SPI\n",
345 			mmc_hostname(card->host),
346 			mmc_card_hs(card) ? "high speed " : "",
347 			mmc_card_ddr52(card) ? "DDR " : "",
348 			type);
349 	} else {
350 		pr_info("%s: new %s%s%s%s%s%s card at address %04x\n",
351 			mmc_hostname(card->host),
352 			mmc_card_uhs(card) ? "ultra high speed " :
353 			(mmc_card_hs(card) ? "high speed " : ""),
354 			mmc_card_hs400(card) ? "HS400 " :
355 			(mmc_card_hs200(card) ? "HS200 " : ""),
356 			mmc_card_hs400es(card) ? "Enhanced strobe " : "",
357 			mmc_card_ddr52(card) ? "DDR " : "",
358 			uhs_bus_speed_mode, type, card->rca);
359 	}
360 
361 #ifdef CONFIG_DEBUG_FS
362 	mmc_add_card_debugfs(card);
363 #endif
364 	card->dev.of_node = mmc_of_find_child_device(card->host, 0);
365 
366 	device_enable_async_suspend(&card->dev);
367 
368 	ret = device_add(&card->dev);
369 	if (ret)
370 		return ret;
371 
372 	mmc_card_set_present(card);
373 
374 	return 0;
375 }
376 
377 /*
378  * Unregister a new MMC card with the driver model, and
379  * (eventually) free it.
380  */
381 void mmc_remove_card(struct mmc_card *card)
382 {
383 	struct mmc_host *host = card->host;
384 
385 #ifdef CONFIG_DEBUG_FS
386 	mmc_remove_card_debugfs(card);
387 #endif
388 
389 	if (mmc_card_present(card)) {
390 		if (mmc_host_is_spi(card->host)) {
391 			pr_info("%s: SPI card removed\n",
392 				mmc_hostname(card->host));
393 		} else {
394 			pr_info("%s: card %04x removed\n",
395 				mmc_hostname(card->host), card->rca);
396 		}
397 		device_del(&card->dev);
398 		of_node_put(card->dev.of_node);
399 	}
400 
401 	if (host->cqe_enabled) {
402 		host->cqe_ops->cqe_disable(host);
403 		host->cqe_enabled = false;
404 	}
405 
406 	put_device(&card->dev);
407 }
408