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