1 /*
2 * linux/drivers/mmc/core/bus.c
3 *
4 * Copyright (C) 2003 Russell King, All Rights Reserved.
5 * Copyright (C) 2007 Pierre Ossman
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * MMC card bus driver model
12 */
13
14 #include <linux/export.h>
15 #include <linux/device.h>
16 #include <linux/err.h>
17 #include <linux/slab.h>
18 #include <linux/stat.h>
19 #include <linux/pm_runtime.h>
20
21 #include <linux/mmc/card.h>
22 #include <linux/mmc/host.h>
23
24 #include "core.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
mmc_type_show(struct device * dev,struct device_attribute * attr,char * buf)30 static ssize_t mmc_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
49 static struct device_attribute mmc_dev_attrs[] = {
50 __ATTR(type, S_IRUGO, mmc_type_show, NULL),
51 __ATTR_NULL,
52 };
53
54 /*
55 * This currently matches any MMC driver to any MMC card - drivers
56 * themselves make the decision whether to drive this card in their
57 * probe method.
58 */
mmc_bus_match(struct device * dev,struct device_driver * drv)59 static int mmc_bus_match(struct device *dev, struct device_driver *drv)
60 {
61 return 1;
62 }
63
64 static int
mmc_bus_uevent(struct device * dev,struct kobj_uevent_env * env)65 mmc_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
66 {
67 struct mmc_card *card = mmc_dev_to_card(dev);
68 const char *type;
69 int retval = 0;
70
71 switch (card->type) {
72 case MMC_TYPE_MMC:
73 type = "MMC";
74 break;
75 case MMC_TYPE_SD:
76 type = "SD";
77 break;
78 case MMC_TYPE_SDIO:
79 type = "SDIO";
80 break;
81 case MMC_TYPE_SD_COMBO:
82 type = "SDcombo";
83 break;
84 default:
85 type = NULL;
86 }
87
88 if (type) {
89 retval = add_uevent_var(env, "MMC_TYPE=%s", type);
90 if (retval)
91 return retval;
92 }
93
94 retval = add_uevent_var(env, "MMC_NAME=%s", mmc_card_name(card));
95 if (retval)
96 return retval;
97
98 /*
99 * Request the mmc_block device. Note: that this is a direct request
100 * for the module it carries no information as to what is inserted.
101 */
102 retval = add_uevent_var(env, "MODALIAS=mmc:block");
103
104 return retval;
105 }
106
mmc_bus_probe(struct device * dev)107 static int mmc_bus_probe(struct device *dev)
108 {
109 struct mmc_driver *drv = to_mmc_driver(dev->driver);
110 struct mmc_card *card = mmc_dev_to_card(dev);
111
112 return drv->probe(card);
113 }
114
mmc_bus_remove(struct device * dev)115 static int mmc_bus_remove(struct device *dev)
116 {
117 struct mmc_driver *drv = to_mmc_driver(dev->driver);
118 struct mmc_card *card = mmc_dev_to_card(dev);
119
120 drv->remove(card);
121
122 return 0;
123 }
124
mmc_bus_shutdown(struct device * dev)125 static void mmc_bus_shutdown(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 struct mmc_host *host = card->host;
130 int ret;
131
132 if (dev->driver && drv->shutdown)
133 drv->shutdown(card);
134
135 if (host->bus_ops->shutdown) {
136 ret = host->bus_ops->shutdown(host);
137 if (ret)
138 pr_warn("%s: error %d during shutdown\n",
139 mmc_hostname(host), ret);
140 }
141 }
142
143 #ifdef CONFIG_PM_SLEEP
mmc_bus_suspend(struct device * dev)144 static int mmc_bus_suspend(struct device *dev)
145 {
146 struct mmc_driver *drv = to_mmc_driver(dev->driver);
147 struct mmc_card *card = mmc_dev_to_card(dev);
148 int ret = 0;
149
150 if (dev->driver && drv->suspend)
151 ret = drv->suspend(card);
152 return ret;
153 }
154
mmc_bus_resume(struct device * dev)155 static int mmc_bus_resume(struct device *dev)
156 {
157 struct mmc_driver *drv = to_mmc_driver(dev->driver);
158 struct mmc_card *card = mmc_dev_to_card(dev);
159 int ret = 0;
160
161 if (dev->driver && drv->resume)
162 ret = drv->resume(card);
163 return ret;
164 }
165 #endif
166
167 #ifdef CONFIG_PM_RUNTIME
168
mmc_runtime_suspend(struct device * dev)169 static int mmc_runtime_suspend(struct device *dev)
170 {
171 struct mmc_card *card = mmc_dev_to_card(dev);
172
173 return mmc_power_save_host(card->host);
174 }
175
mmc_runtime_resume(struct device * dev)176 static int mmc_runtime_resume(struct device *dev)
177 {
178 struct mmc_card *card = mmc_dev_to_card(dev);
179
180 return mmc_power_restore_host(card->host);
181 }
182
mmc_runtime_idle(struct device * dev)183 static int mmc_runtime_idle(struct device *dev)
184 {
185 return pm_runtime_suspend(dev);
186 }
187
188 #endif /* !CONFIG_PM_RUNTIME */
189
190 static const struct dev_pm_ops mmc_bus_pm_ops = {
191 SET_RUNTIME_PM_OPS(mmc_runtime_suspend, mmc_runtime_resume,
192 mmc_runtime_idle)
193 SET_SYSTEM_SLEEP_PM_OPS(mmc_bus_suspend, mmc_bus_resume)
194 };
195
196 static struct bus_type mmc_bus_type = {
197 .name = "mmc",
198 .dev_attrs = mmc_dev_attrs,
199 .match = mmc_bus_match,
200 .uevent = mmc_bus_uevent,
201 .probe = mmc_bus_probe,
202 .remove = mmc_bus_remove,
203 .shutdown = mmc_bus_shutdown,
204 .pm = &mmc_bus_pm_ops,
205 };
206
mmc_register_bus(void)207 int mmc_register_bus(void)
208 {
209 return bus_register(&mmc_bus_type);
210 }
211
mmc_unregister_bus(void)212 void mmc_unregister_bus(void)
213 {
214 bus_unregister(&mmc_bus_type);
215 }
216
217 /**
218 * mmc_register_driver - register a media driver
219 * @drv: MMC media driver
220 */
mmc_register_driver(struct mmc_driver * drv)221 int mmc_register_driver(struct mmc_driver *drv)
222 {
223 drv->drv.bus = &mmc_bus_type;
224 return driver_register(&drv->drv);
225 }
226
227 EXPORT_SYMBOL(mmc_register_driver);
228
229 /**
230 * mmc_unregister_driver - unregister a media driver
231 * @drv: MMC media driver
232 */
mmc_unregister_driver(struct mmc_driver * drv)233 void mmc_unregister_driver(struct mmc_driver *drv)
234 {
235 drv->drv.bus = &mmc_bus_type;
236 driver_unregister(&drv->drv);
237 }
238
239 EXPORT_SYMBOL(mmc_unregister_driver);
240
mmc_release_card(struct device * dev)241 static void mmc_release_card(struct device *dev)
242 {
243 struct mmc_card *card = mmc_dev_to_card(dev);
244
245 sdio_free_common_cis(card);
246
247 kfree(card->info);
248
249 kfree(card);
250 }
251
252 /*
253 * Allocate and initialise a new MMC card structure.
254 */
mmc_alloc_card(struct mmc_host * host,struct device_type * type)255 struct mmc_card *mmc_alloc_card(struct mmc_host *host, struct device_type *type)
256 {
257 struct mmc_card *card;
258
259 card = kzalloc(sizeof(struct mmc_card), GFP_KERNEL);
260 if (!card)
261 return ERR_PTR(-ENOMEM);
262
263 card->host = host;
264
265 device_initialize(&card->dev);
266
267 card->dev.parent = mmc_classdev(host);
268 card->dev.bus = &mmc_bus_type;
269 card->dev.release = mmc_release_card;
270 card->dev.type = type;
271
272 return card;
273 }
274
275 /*
276 * Register a new MMC card with the driver model.
277 */
mmc_add_card(struct mmc_card * card)278 int mmc_add_card(struct mmc_card *card)
279 {
280 int ret;
281 const char *type;
282 const char *uhs_bus_speed_mode = "";
283 static const char *const uhs_speeds[] = {
284 [UHS_SDR12_BUS_SPEED] = "SDR12 ",
285 [UHS_SDR25_BUS_SPEED] = "SDR25 ",
286 [UHS_SDR50_BUS_SPEED] = "SDR50 ",
287 [UHS_SDR104_BUS_SPEED] = "SDR104 ",
288 [UHS_DDR50_BUS_SPEED] = "DDR50 ",
289 };
290
291
292 dev_set_name(&card->dev, "%s:%04x", mmc_hostname(card->host), card->rca);
293
294 switch (card->type) {
295 case MMC_TYPE_MMC:
296 type = "MMC";
297 break;
298 case MMC_TYPE_SD:
299 type = "SD";
300 if (mmc_card_blockaddr(card)) {
301 if (mmc_card_ext_capacity(card))
302 type = "SDXC";
303 else
304 type = "SDHC";
305 }
306 break;
307 case MMC_TYPE_SDIO:
308 type = "SDIO";
309 break;
310 case MMC_TYPE_SD_COMBO:
311 type = "SD-combo";
312 if (mmc_card_blockaddr(card))
313 type = "SDHC-combo";
314 break;
315 default:
316 type = "?";
317 break;
318 }
319
320 if (mmc_sd_card_uhs(card) &&
321 (card->sd_bus_speed < ARRAY_SIZE(uhs_speeds)))
322 uhs_bus_speed_mode = uhs_speeds[card->sd_bus_speed];
323
324 if (mmc_host_is_spi(card->host)) {
325 pr_info("%s: new %s%s%s card on SPI\n",
326 mmc_hostname(card->host),
327 mmc_card_highspeed(card) ? "high speed " : "",
328 mmc_card_ddr_mode(card) ? "DDR " : "",
329 type);
330 } else {
331 pr_info("%s: new %s%s%s%s%s card at address %04x\n",
332 mmc_hostname(card->host),
333 mmc_card_uhs(card) ? "ultra high speed " :
334 (mmc_card_highspeed(card) ? "high speed " : ""),
335 (mmc_card_hs200(card) ? "HS200 " : ""),
336 mmc_card_ddr_mode(card) ? "DDR " : "",
337 uhs_bus_speed_mode, type, card->rca);
338 }
339
340 #ifdef CONFIG_DEBUG_FS
341 mmc_add_card_debugfs(card);
342 #endif
343 mmc_init_context_info(card->host);
344
345 ret = device_add(&card->dev);
346 if (ret)
347 return ret;
348
349 mmc_card_set_present(card);
350
351 return 0;
352 }
353
354 /*
355 * Unregister a new MMC card with the driver model, and
356 * (eventually) free it.
357 */
mmc_remove_card(struct mmc_card * card)358 void mmc_remove_card(struct mmc_card *card)
359 {
360 #ifdef CONFIG_DEBUG_FS
361 mmc_remove_card_debugfs(card);
362 #endif
363
364 if (mmc_card_present(card)) {
365 if (mmc_host_is_spi(card->host)) {
366 pr_info("%s: SPI card removed\n",
367 mmc_hostname(card->host));
368 } else {
369 pr_info("%s: card %04x removed\n",
370 mmc_hostname(card->host), card->rca);
371 }
372 device_del(&card->dev);
373 }
374
375 put_device(&card->dev);
376 }
377
378