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