Commit | Line | Data |
---|---|---|
4101c16a PO |
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/device.h> | |
15 | #include <linux/err.h> | |
16 | ||
17 | #include <linux/mmc/card.h> | |
18 | #include <linux/mmc/host.h> | |
19 | ||
20 | #include "sysfs.h" | |
21 | #include "core.h" | |
22 | #include "bus.h" | |
23 | ||
24 | #define dev_to_mmc_card(d) container_of(d, struct mmc_card, dev) | |
25 | #define to_mmc_driver(d) container_of(d, struct mmc_driver, drv) | |
26 | ||
27 | static ssize_t mmc_type_show(struct device *dev, | |
28 | struct device_attribute *attr, char *buf) | |
29 | { | |
30 | struct mmc_card *card = dev_to_mmc_card(dev); | |
31 | ||
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"); | |
5c4e6f13 PO |
37 | case MMC_TYPE_SDIO: |
38 | return sprintf(buf, "SDIO\n"); | |
4101c16a PO |
39 | default: |
40 | return -EFAULT; | |
41 | } | |
42 | } | |
43 | ||
44 | static struct device_attribute mmc_dev_attrs[] = { | |
45 | MMC_ATTR_RO(type), | |
46 | __ATTR_NULL, | |
47 | }; | |
48 | ||
49 | /* | |
50 | * This currently matches any MMC driver to any MMC card - drivers | |
51 | * themselves make the decision whether to drive this card in their | |
52 | * probe method. | |
53 | */ | |
54 | static int mmc_bus_match(struct device *dev, struct device_driver *drv) | |
55 | { | |
56 | return 1; | |
57 | } | |
58 | ||
59 | static int | |
60 | mmc_bus_uevent(struct device *dev, char **envp, int num_envp, char *buf, | |
61 | int buf_size) | |
62 | { | |
63 | struct mmc_card *card = dev_to_mmc_card(dev); | |
64 | int retval = 0, i = 0, length = 0; | |
65 | ||
66 | #define add_env(fmt,val) do { \ | |
67 | retval = add_uevent_var(envp, num_envp, &i, \ | |
68 | buf, buf_size, &length, \ | |
69 | fmt, val); \ | |
70 | if (retval) \ | |
71 | return retval; \ | |
72 | } while (0); | |
73 | ||
74 | switch (card->type) { | |
75 | case MMC_TYPE_MMC: | |
76 | add_env("MMC_TYPE=%s", "MMC"); | |
77 | break; | |
78 | case MMC_TYPE_SD: | |
79 | add_env("MMC_TYPE=%s", "SD"); | |
80 | break; | |
5c4e6f13 PO |
81 | case MMC_TYPE_SDIO: |
82 | add_env("MMC_TYPE=%s", "SDIO"); | |
83 | break; | |
4101c16a PO |
84 | } |
85 | ||
86 | add_env("MMC_NAME=%s", mmc_card_name(card)); | |
87 | ||
88 | #undef add_env | |
89 | ||
90 | envp[i] = NULL; | |
91 | ||
92 | return 0; | |
93 | } | |
94 | ||
95 | static int mmc_bus_probe(struct device *dev) | |
96 | { | |
97 | struct mmc_driver *drv = to_mmc_driver(dev->driver); | |
98 | struct mmc_card *card = dev_to_mmc_card(dev); | |
99 | ||
100 | return drv->probe(card); | |
101 | } | |
102 | ||
103 | static int mmc_bus_remove(struct device *dev) | |
104 | { | |
105 | struct mmc_driver *drv = to_mmc_driver(dev->driver); | |
106 | struct mmc_card *card = dev_to_mmc_card(dev); | |
107 | ||
108 | drv->remove(card); | |
109 | ||
110 | return 0; | |
111 | } | |
112 | ||
113 | static int mmc_bus_suspend(struct device *dev, pm_message_t state) | |
114 | { | |
115 | struct mmc_driver *drv = to_mmc_driver(dev->driver); | |
116 | struct mmc_card *card = dev_to_mmc_card(dev); | |
117 | int ret = 0; | |
118 | ||
119 | if (dev->driver && drv->suspend) | |
120 | ret = drv->suspend(card, state); | |
121 | return ret; | |
122 | } | |
123 | ||
124 | static int mmc_bus_resume(struct device *dev) | |
125 | { | |
126 | struct mmc_driver *drv = to_mmc_driver(dev->driver); | |
127 | struct mmc_card *card = dev_to_mmc_card(dev); | |
128 | int ret = 0; | |
129 | ||
130 | if (dev->driver && drv->resume) | |
131 | ret = drv->resume(card); | |
132 | return ret; | |
133 | } | |
134 | ||
135 | static struct bus_type mmc_bus_type = { | |
136 | .name = "mmc", | |
137 | .dev_attrs = mmc_dev_attrs, | |
138 | .match = mmc_bus_match, | |
139 | .uevent = mmc_bus_uevent, | |
140 | .probe = mmc_bus_probe, | |
141 | .remove = mmc_bus_remove, | |
142 | .suspend = mmc_bus_suspend, | |
143 | .resume = mmc_bus_resume, | |
144 | }; | |
145 | ||
146 | int mmc_register_bus(void) | |
147 | { | |
148 | return bus_register(&mmc_bus_type); | |
149 | } | |
150 | ||
151 | void mmc_unregister_bus(void) | |
152 | { | |
153 | bus_unregister(&mmc_bus_type); | |
154 | } | |
155 | ||
156 | /** | |
157 | * mmc_register_driver - register a media driver | |
158 | * @drv: MMC media driver | |
159 | */ | |
160 | int mmc_register_driver(struct mmc_driver *drv) | |
161 | { | |
162 | drv->drv.bus = &mmc_bus_type; | |
163 | return driver_register(&drv->drv); | |
164 | } | |
165 | ||
166 | EXPORT_SYMBOL(mmc_register_driver); | |
167 | ||
168 | /** | |
169 | * mmc_unregister_driver - unregister a media driver | |
170 | * @drv: MMC media driver | |
171 | */ | |
172 | void mmc_unregister_driver(struct mmc_driver *drv) | |
173 | { | |
174 | drv->drv.bus = &mmc_bus_type; | |
175 | driver_unregister(&drv->drv); | |
176 | } | |
177 | ||
178 | EXPORT_SYMBOL(mmc_unregister_driver); | |
179 | ||
180 | static void mmc_release_card(struct device *dev) | |
181 | { | |
182 | struct mmc_card *card = dev_to_mmc_card(dev); | |
183 | ||
184 | kfree(card); | |
185 | } | |
186 | ||
187 | /* | |
188 | * Allocate and initialise a new MMC card structure. | |
189 | */ | |
190 | struct mmc_card *mmc_alloc_card(struct mmc_host *host) | |
191 | { | |
192 | struct mmc_card *card; | |
193 | ||
733cb1e4 | 194 | card = kzalloc(sizeof(struct mmc_card), GFP_KERNEL); |
4101c16a PO |
195 | if (!card) |
196 | return ERR_PTR(-ENOMEM); | |
197 | ||
4101c16a PO |
198 | card->host = host; |
199 | ||
200 | device_initialize(&card->dev); | |
201 | ||
202 | card->dev.parent = mmc_classdev(host); | |
203 | card->dev.bus = &mmc_bus_type; | |
204 | card->dev.release = mmc_release_card; | |
205 | ||
206 | return card; | |
207 | } | |
208 | ||
209 | /* | |
210 | * Register a new MMC card with the driver model. | |
211 | */ | |
212 | int mmc_add_card(struct mmc_card *card) | |
213 | { | |
214 | int ret; | |
109b5bed | 215 | const char *type; |
4101c16a PO |
216 | |
217 | snprintf(card->dev.bus_id, sizeof(card->dev.bus_id), | |
218 | "%s:%04x", mmc_hostname(card->host), card->rca); | |
219 | ||
109b5bed PO |
220 | switch (card->type) { |
221 | case MMC_TYPE_MMC: | |
222 | type = "MMC"; | |
223 | break; | |
224 | case MMC_TYPE_SD: | |
225 | type = "SD"; | |
226 | if (mmc_card_blockaddr(card)) | |
227 | type = "SDHC"; | |
228 | break; | |
5c4e6f13 PO |
229 | case MMC_TYPE_SDIO: |
230 | type = "SDIO"; | |
231 | break; | |
109b5bed PO |
232 | default: |
233 | type = "?"; | |
234 | break; | |
235 | } | |
236 | ||
237 | printk(KERN_INFO "%s: new %s%s card at address %04x\n", | |
238 | mmc_hostname(card->host), | |
239 | mmc_card_highspeed(card) ? "high speed " : "", | |
240 | type, card->rca); | |
241 | ||
4101c16a PO |
242 | card->dev.uevent_suppress = 1; |
243 | ||
244 | ret = device_add(&card->dev); | |
245 | if (ret) | |
246 | return ret; | |
247 | ||
248 | if (card->host->bus_ops->sysfs_add) { | |
249 | ret = card->host->bus_ops->sysfs_add(card->host, card); | |
250 | if (ret) { | |
251 | device_del(&card->dev); | |
252 | return ret; | |
253 | } | |
254 | } | |
255 | ||
256 | card->dev.uevent_suppress = 0; | |
257 | ||
258 | kobject_uevent(&card->dev.kobj, KOBJ_ADD); | |
259 | ||
260 | mmc_card_set_present(card); | |
261 | ||
262 | return 0; | |
263 | } | |
264 | ||
265 | /* | |
266 | * Unregister a new MMC card with the driver model, and | |
267 | * (eventually) free it. | |
268 | */ | |
269 | void mmc_remove_card(struct mmc_card *card) | |
270 | { | |
271 | if (mmc_card_present(card)) { | |
109b5bed PO |
272 | printk(KERN_INFO "%s: card %04x removed\n", |
273 | mmc_hostname(card->host), card->rca); | |
274 | ||
4101c16a PO |
275 | if (card->host->bus_ops->sysfs_remove) |
276 | card->host->bus_ops->sysfs_remove(card->host, card); | |
277 | device_del(&card->dev); | |
278 | } | |
279 | ||
280 | put_device(&card->dev); | |
281 | } | |
282 |