1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <linux/device.h>
15#include <linux/err.h>
16
17#include <linux/mmc/card.h>
18#include <linux/mmc/sdio_func.h>
19
20#include "sdio_cis.h"
21#include "sdio_bus.h"
22
23
24#define sdio_config_attr(field, format_string) \
25static ssize_t \
26field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
27{ \
28 struct sdio_func *func; \
29 \
30 func = dev_to_sdio_func (dev); \
31 return sprintf (buf, format_string, func->field); \
32}
33
34sdio_config_attr(class, "0x%02x\n");
35sdio_config_attr(vendor, "0x%04x\n");
36sdio_config_attr(device, "0x%04x\n");
37
38static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
39{
40 struct sdio_func *func = dev_to_sdio_func (dev);
41
42 return sprintf(buf, "sdio:c%02Xv%04Xd%04X\n",
43 func->class, func->vendor, func->device);
44}
45
46static struct device_attribute sdio_dev_attrs[] = {
47 __ATTR_RO(class),
48 __ATTR_RO(vendor),
49 __ATTR_RO(device),
50 __ATTR_RO(modalias),
51 __ATTR_NULL,
52};
53
54static const struct sdio_device_id *sdio_match_one(struct sdio_func *func,
55 const struct sdio_device_id *id)
56{
57 if (id->class != (__u8)SDIO_ANY_ID && id->class != func->class)
58 return NULL;
59 if (id->vendor != (__u16)SDIO_ANY_ID && id->vendor != func->vendor)
60 return NULL;
61 if (id->device != (__u16)SDIO_ANY_ID && id->device != func->device)
62 return NULL;
63 return id;
64}
65
66static const struct sdio_device_id *sdio_match_device(struct sdio_func *func,
67 struct sdio_driver *sdrv)
68{
69 const struct sdio_device_id *ids;
70
71 ids = sdrv->id_table;
72
73 if (ids) {
74 while (ids->class || ids->vendor || ids->device) {
75 if (sdio_match_one(func, ids))
76 return ids;
77 ids++;
78 }
79 }
80
81 return NULL;
82}
83
84static int sdio_bus_match(struct device *dev, struct device_driver *drv)
85{
86 struct sdio_func *func = dev_to_sdio_func(dev);
87 struct sdio_driver *sdrv = to_sdio_driver(drv);
88
89 if (sdio_match_device(func, sdrv))
90 return 1;
91
92 return 0;
93}
94
95static int
96sdio_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
97{
98 struct sdio_func *func = dev_to_sdio_func(dev);
99
100 if (add_uevent_var(env,
101 "SDIO_CLASS=%02X", func->class))
102 return -ENOMEM;
103
104 if (add_uevent_var(env,
105 "SDIO_ID=%04X:%04X", func->vendor, func->device))
106 return -ENOMEM;
107
108 if (add_uevent_var(env,
109 "MODALIAS=sdio:c%02Xv%04Xd%04X",
110 func->class, func->vendor, func->device))
111 return -ENOMEM;
112
113 return 0;
114}
115
116static int sdio_bus_probe(struct device *dev)
117{
118 struct sdio_driver *drv = to_sdio_driver(dev->driver);
119 struct sdio_func *func = dev_to_sdio_func(dev);
120 const struct sdio_device_id *id;
121 int ret;
122
123 id = sdio_match_device(func, drv);
124 if (!id)
125 return -ENODEV;
126
127
128
129 sdio_claim_host(func);
130 ret = sdio_set_block_size(func, 0);
131 sdio_release_host(func);
132 if (ret)
133 return ret;
134
135 return drv->probe(func, id);
136}
137
138static int sdio_bus_remove(struct device *dev)
139{
140 struct sdio_driver *drv = to_sdio_driver(dev->driver);
141 struct sdio_func *func = dev_to_sdio_func(dev);
142
143 drv->remove(func);
144
145 if (func->irq_handler) {
146 printk(KERN_WARNING "WARNING: driver %s did not remove "
147 "its interrupt handler!\n", drv->name);
148 sdio_claim_host(func);
149 sdio_release_irq(func);
150 sdio_release_host(func);
151 }
152
153 return 0;
154}
155
156static struct bus_type sdio_bus_type = {
157 .name = "sdio",
158 .dev_attrs = sdio_dev_attrs,
159 .match = sdio_bus_match,
160 .uevent = sdio_bus_uevent,
161 .probe = sdio_bus_probe,
162 .remove = sdio_bus_remove,
163};
164
165int sdio_register_bus(void)
166{
167 return bus_register(&sdio_bus_type);
168}
169
170void sdio_unregister_bus(void)
171{
172 bus_unregister(&sdio_bus_type);
173}
174
175
176
177
178
179int sdio_register_driver(struct sdio_driver *drv)
180{
181 drv->drv.name = drv->name;
182 drv->drv.bus = &sdio_bus_type;
183 return driver_register(&drv->drv);
184}
185EXPORT_SYMBOL_GPL(sdio_register_driver);
186
187
188
189
190
191void sdio_unregister_driver(struct sdio_driver *drv)
192{
193 drv->drv.bus = &sdio_bus_type;
194 driver_unregister(&drv->drv);
195}
196EXPORT_SYMBOL_GPL(sdio_unregister_driver);
197
198static void sdio_release_func(struct device *dev)
199{
200 struct sdio_func *func = dev_to_sdio_func(dev);
201
202 sdio_free_func_cis(func);
203
204 if (func->info)
205 kfree(func->info);
206
207 kfree(func);
208}
209
210
211
212
213struct sdio_func *sdio_alloc_func(struct mmc_card *card)
214{
215 struct sdio_func *func;
216
217 func = kzalloc(sizeof(struct sdio_func), GFP_KERNEL);
218 if (!func)
219 return ERR_PTR(-ENOMEM);
220
221 func->card = card;
222
223 device_initialize(&func->dev);
224
225 func->dev.parent = &card->dev;
226 func->dev.bus = &sdio_bus_type;
227 func->dev.release = sdio_release_func;
228
229 return func;
230}
231
232
233
234
235int sdio_add_func(struct sdio_func *func)
236{
237 int ret;
238
239 dev_set_name(&func->dev, "%s:%d", mmc_card_id(func->card), func->num);
240
241 ret = device_add(&func->dev);
242 if (ret == 0)
243 sdio_func_set_present(func);
244
245 return ret;
246}
247
248
249
250
251
252
253
254void sdio_remove_func(struct sdio_func *func)
255{
256 if (!sdio_func_present(func))
257 return;
258
259 device_del(&func->dev);
260 put_device(&func->dev);
261}
262
263