blob: e70bd6641ceead22cccf094e4344f5ef45415be6 [file] [log] [blame]
Pierre Ossman4101c162007-05-19 13:39:01 +02001/*
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/device.h>
15#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Pierre Ossman4101c162007-05-19 13:39:01 +020017
18#include <linux/mmc/card.h>
19#include <linux/mmc/host.h>
20
Pierre Ossman4101c162007-05-19 13:39:01 +020021#include "core.h"
Pierre Ossman1a632f82007-07-30 15:15:30 +020022#include "sdio_cis.h"
Pierre Ossman4101c162007-05-19 13:39:01 +020023#include "bus.h"
24
Pierre Ossman4101c162007-05-19 13:39:01 +020025#define to_mmc_driver(d) container_of(d, struct mmc_driver, drv)
26
27static ssize_t mmc_type_show(struct device *dev,
28 struct device_attribute *attr, char *buf)
29{
Andy Shevchenko265cdc92010-09-17 20:32:25 -040030 struct mmc_card *card = mmc_dev_to_card(dev);
Pierre Ossman4101c162007-05-19 13:39:01 +020031
32 switch (card->type) {
33 case MMC_TYPE_MMC:
34 return sprintf(buf, "MMC\n");
35 case MMC_TYPE_SD:
36 return sprintf(buf, "SD\n");
Pierre Ossman5c4e6f12007-05-21 20:23:20 +020037 case MMC_TYPE_SDIO:
38 return sprintf(buf, "SDIO\n");
Michal Miroslaw7310ece82010-08-10 18:01:40 -070039 case MMC_TYPE_SD_COMBO:
40 return sprintf(buf, "SDcombo\n");
Pierre Ossman4101c162007-05-19 13:39:01 +020041 default:
42 return -EFAULT;
43 }
44}
45
46static struct device_attribute mmc_dev_attrs[] = {
Pierre Ossman51ec92e2008-03-21 23:54:50 +010047 __ATTR(type, S_IRUGO, mmc_type_show, NULL),
Pierre Ossman4101c162007-05-19 13:39:01 +020048 __ATTR_NULL,
49};
50
51/*
52 * This currently matches any MMC driver to any MMC card - drivers
53 * themselves make the decision whether to drive this card in their
54 * probe method.
55 */
56static int mmc_bus_match(struct device *dev, struct device_driver *drv)
57{
58 return 1;
59}
60
61static int
Kay Sievers7eff2e72007-08-14 15:15:12 +020062mmc_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
Pierre Ossman4101c162007-05-19 13:39:01 +020063{
Andy Shevchenko265cdc92010-09-17 20:32:25 -040064 struct mmc_card *card = mmc_dev_to_card(dev);
Pierre Ossman9eb3a942007-06-17 11:18:46 +020065 const char *type;
Kay Sievers7eff2e72007-08-14 15:15:12 +020066 int retval = 0;
Pierre Ossman4101c162007-05-19 13:39:01 +020067
68 switch (card->type) {
69 case MMC_TYPE_MMC:
Pierre Ossman9eb3a942007-06-17 11:18:46 +020070 type = "MMC";
Pierre Ossman4101c162007-05-19 13:39:01 +020071 break;
72 case MMC_TYPE_SD:
Pierre Ossman9eb3a942007-06-17 11:18:46 +020073 type = "SD";
Pierre Ossman4101c162007-05-19 13:39:01 +020074 break;
Pierre Ossman5c4e6f12007-05-21 20:23:20 +020075 case MMC_TYPE_SDIO:
Pierre Ossman9eb3a942007-06-17 11:18:46 +020076 type = "SDIO";
Pierre Ossman5c4e6f12007-05-21 20:23:20 +020077 break;
Michal Miroslaw7310ece82010-08-10 18:01:40 -070078 case MMC_TYPE_SD_COMBO:
79 type = "SDcombo";
80 break;
Pierre Ossman9eb3a942007-06-17 11:18:46 +020081 default:
82 type = NULL;
Pierre Ossman4101c162007-05-19 13:39:01 +020083 }
84
Pierre Ossman9eb3a942007-06-17 11:18:46 +020085 if (type) {
Kay Sievers7eff2e72007-08-14 15:15:12 +020086 retval = add_uevent_var(env, "MMC_TYPE=%s", type);
87 if (retval)
88 return retval;
Pierre Ossman9eb3a942007-06-17 11:18:46 +020089 }
Pierre Ossman4101c162007-05-19 13:39:01 +020090
Kay Sievers7eff2e72007-08-14 15:15:12 +020091 retval = add_uevent_var(env, "MMC_NAME=%s", mmc_card_name(card));
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000092 if (retval)
93 return retval;
94
95 /*
96 * Request the mmc_block device. Note: that this is a direct request
97 * for the module it carries no information as to what is inserted.
98 */
99 retval = add_uevent_var(env, "MODALIAS=mmc:block");
Pierre Ossman4101c162007-05-19 13:39:01 +0200100
Kay Sievers7eff2e72007-08-14 15:15:12 +0200101 return retval;
Pierre Ossman4101c162007-05-19 13:39:01 +0200102}
103
104static int mmc_bus_probe(struct device *dev)
105{
106 struct mmc_driver *drv = to_mmc_driver(dev->driver);
Andy Shevchenko265cdc92010-09-17 20:32:25 -0400107 struct mmc_card *card = mmc_dev_to_card(dev);
Pierre Ossman4101c162007-05-19 13:39:01 +0200108
109 return drv->probe(card);
110}
111
112static int mmc_bus_remove(struct device *dev)
113{
114 struct mmc_driver *drv = to_mmc_driver(dev->driver);
Andy Shevchenko265cdc92010-09-17 20:32:25 -0400115 struct mmc_card *card = mmc_dev_to_card(dev);
Pierre Ossman4101c162007-05-19 13:39:01 +0200116
117 drv->remove(card);
118
119 return 0;
120}
121
122static int mmc_bus_suspend(struct device *dev, pm_message_t state)
123{
124 struct mmc_driver *drv = to_mmc_driver(dev->driver);
Andy Shevchenko265cdc92010-09-17 20:32:25 -0400125 struct mmc_card *card = mmc_dev_to_card(dev);
Pierre Ossman4101c162007-05-19 13:39:01 +0200126 int ret = 0;
127
128 if (dev->driver && drv->suspend)
129 ret = drv->suspend(card, state);
130 return ret;
131}
132
133static int mmc_bus_resume(struct device *dev)
134{
135 struct mmc_driver *drv = to_mmc_driver(dev->driver);
Andy Shevchenko265cdc92010-09-17 20:32:25 -0400136 struct mmc_card *card = mmc_dev_to_card(dev);
Pierre Ossman4101c162007-05-19 13:39:01 +0200137 int ret = 0;
138
139 if (dev->driver && drv->resume)
140 ret = drv->resume(card);
141 return ret;
142}
143
144static struct bus_type mmc_bus_type = {
145 .name = "mmc",
146 .dev_attrs = mmc_dev_attrs,
147 .match = mmc_bus_match,
148 .uevent = mmc_bus_uevent,
149 .probe = mmc_bus_probe,
150 .remove = mmc_bus_remove,
151 .suspend = mmc_bus_suspend,
152 .resume = mmc_bus_resume,
153};
154
155int mmc_register_bus(void)
156{
157 return bus_register(&mmc_bus_type);
158}
159
160void mmc_unregister_bus(void)
161{
162 bus_unregister(&mmc_bus_type);
163}
164
165/**
166 * mmc_register_driver - register a media driver
167 * @drv: MMC media driver
168 */
169int mmc_register_driver(struct mmc_driver *drv)
170{
171 drv->drv.bus = &mmc_bus_type;
172 return driver_register(&drv->drv);
173}
174
175EXPORT_SYMBOL(mmc_register_driver);
176
177/**
178 * mmc_unregister_driver - unregister a media driver
179 * @drv: MMC media driver
180 */
181void mmc_unregister_driver(struct mmc_driver *drv)
182{
183 drv->drv.bus = &mmc_bus_type;
184 driver_unregister(&drv->drv);
185}
186
187EXPORT_SYMBOL(mmc_unregister_driver);
188
189static void mmc_release_card(struct device *dev)
190{
Andy Shevchenko265cdc92010-09-17 20:32:25 -0400191 struct mmc_card *card = mmc_dev_to_card(dev);
Pierre Ossman4101c162007-05-19 13:39:01 +0200192
Pierre Ossman1a632f82007-07-30 15:15:30 +0200193 sdio_free_common_cis(card);
194
Pierre Ossman759bdc72007-09-19 18:42:16 +0200195 if (card->info)
196 kfree(card->info);
197
Pierre Ossman4101c162007-05-19 13:39:01 +0200198 kfree(card);
199}
200
201/*
202 * Allocate and initialise a new MMC card structure.
203 */
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100204struct mmc_card *mmc_alloc_card(struct mmc_host *host, struct device_type *type)
Pierre Ossman4101c162007-05-19 13:39:01 +0200205{
206 struct mmc_card *card;
207
Mariusz Kozlowski733cb1e2007-08-10 14:00:47 -0700208 card = kzalloc(sizeof(struct mmc_card), GFP_KERNEL);
Pierre Ossman4101c162007-05-19 13:39:01 +0200209 if (!card)
210 return ERR_PTR(-ENOMEM);
211
Pierre Ossman4101c162007-05-19 13:39:01 +0200212 card->host = host;
213
214 device_initialize(&card->dev);
215
216 card->dev.parent = mmc_classdev(host);
217 card->dev.bus = &mmc_bus_type;
218 card->dev.release = mmc_release_card;
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100219 card->dev.type = type;
Pierre Ossman4101c162007-05-19 13:39:01 +0200220
221 return card;
222}
223
224/*
225 * Register a new MMC card with the driver model.
226 */
227int mmc_add_card(struct mmc_card *card)
228{
229 int ret;
Pierre Ossman109b5be2007-07-23 00:12:10 +0200230 const char *type;
Pierre Ossman4101c162007-05-19 13:39:01 +0200231
Kay Sieversd1b26862008-11-08 21:37:46 +0100232 dev_set_name(&card->dev, "%s:%04x", mmc_hostname(card->host), card->rca);
Pierre Ossman4101c162007-05-19 13:39:01 +0200233
Pierre Ossman109b5be2007-07-23 00:12:10 +0200234 switch (card->type) {
235 case MMC_TYPE_MMC:
236 type = "MMC";
237 break;
238 case MMC_TYPE_SD:
239 type = "SD";
240 if (mmc_card_blockaddr(card))
241 type = "SDHC";
242 break;
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200243 case MMC_TYPE_SDIO:
244 type = "SDIO";
245 break;
Michal Miroslaw7310ece82010-08-10 18:01:40 -0700246 case MMC_TYPE_SD_COMBO:
247 type = "SD-combo";
248 if (mmc_card_blockaddr(card))
249 type = "SDHC-combo";
Pierre Ossman109b5be2007-07-23 00:12:10 +0200250 default:
251 type = "?";
252 break;
253 }
254
David Brownellaf517152007-08-08 09:11:32 -0700255 if (mmc_host_is_spi(card->host)) {
256 printk(KERN_INFO "%s: new %s%s card on SPI\n",
257 mmc_hostname(card->host),
258 mmc_card_highspeed(card) ? "high speed " : "",
259 type);
260 } else {
261 printk(KERN_INFO "%s: new %s%s card at address %04x\n",
262 mmc_hostname(card->host),
263 mmc_card_highspeed(card) ? "high speed " : "",
264 type, card->rca);
265 }
Pierre Ossman109b5be2007-07-23 00:12:10 +0200266
Pierre Ossman4101c162007-05-19 13:39:01 +0200267 ret = device_add(&card->dev);
268 if (ret)
269 return ret;
270
Haavard Skinnemoenf4b7f922008-07-24 14:18:58 +0200271#ifdef CONFIG_DEBUG_FS
272 mmc_add_card_debugfs(card);
273#endif
274
Pierre Ossman4101c162007-05-19 13:39:01 +0200275 mmc_card_set_present(card);
276
277 return 0;
278}
279
280/*
281 * Unregister a new MMC card with the driver model, and
282 * (eventually) free it.
283 */
284void mmc_remove_card(struct mmc_card *card)
285{
Haavard Skinnemoenf4b7f922008-07-24 14:18:58 +0200286#ifdef CONFIG_DEBUG_FS
287 mmc_remove_card_debugfs(card);
288#endif
289
Pierre Ossman4101c162007-05-19 13:39:01 +0200290 if (mmc_card_present(card)) {
David Brownellaf517152007-08-08 09:11:32 -0700291 if (mmc_host_is_spi(card->host)) {
292 printk(KERN_INFO "%s: SPI card removed\n",
293 mmc_hostname(card->host));
294 } else {
295 printk(KERN_INFO "%s: card %04x removed\n",
296 mmc_hostname(card->host), card->rca);
297 }
Pierre Ossman4101c162007-05-19 13:39:01 +0200298 device_del(&card->dev);
299 }
300
301 put_device(&card->dev);
302}
303