1 /*
2 * linux/drivers/mmc/core/sdio_bus.c
3 *
4 * Copyright 2007 Pierre Ossman
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
10 *
11 * SDIO function 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/sdio_func.h>
19
20 #include "sdio_cis.h"
21 #include "sdio_bus.h"
22
23 #ifdef CONFIG_MMC_EMBEDDED_SDIO
24 #include <linux/mmc/host.h>
25 #endif
26
27 #define dev_to_sdio_func(d) container_of(d, struct sdio_func, dev)
28 #define to_sdio_driver(d) container_of(d, struct sdio_driver, drv)
29
30 /* show configuration fields */
31 #define sdio_config_attr(field, format_string) \
32 static ssize_t \
33 field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
34 { \
35 struct sdio_func *func; \
36 \
37 func = dev_to_sdio_func (dev); \
38 return sprintf (buf, format_string, func->field); \
39 }
40
41 sdio_config_attr(class, "0x%02x\n");
42 sdio_config_attr(vendor, "0x%04x\n");
43 sdio_config_attr(device, "0x%04x\n");
44
modalias_show(struct device * dev,struct device_attribute * attr,char * buf)45 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
46 {
47 struct sdio_func *func = dev_to_sdio_func (dev);
48
49 return sprintf(buf, "sdio:c%02Xv%04Xd%04X\n",
50 func->class, func->vendor, func->device);
51 }
52
53 static struct device_attribute sdio_dev_attrs[] = {
54 __ATTR_RO(class),
55 __ATTR_RO(vendor),
56 __ATTR_RO(device),
57 __ATTR_RO(modalias),
58 __ATTR_NULL,
59 };
60
sdio_match_one(struct sdio_func * func,const struct sdio_device_id * id)61 static const struct sdio_device_id *sdio_match_one(struct sdio_func *func,
62 const struct sdio_device_id *id)
63 {
64 if (id->class != (__u8)SDIO_ANY_ID && id->class != func->class)
65 return NULL;
66 if (id->vendor != (__u16)SDIO_ANY_ID && id->vendor != func->vendor)
67 return NULL;
68 if (id->device != (__u16)SDIO_ANY_ID && id->device != func->device)
69 return NULL;
70 return id;
71 }
72
sdio_match_device(struct sdio_func * func,struct sdio_driver * sdrv)73 static const struct sdio_device_id *sdio_match_device(struct sdio_func *func,
74 struct sdio_driver *sdrv)
75 {
76 const struct sdio_device_id *ids;
77
78 ids = sdrv->id_table;
79
80 if (ids) {
81 while (ids->class || ids->vendor || ids->device) {
82 if (sdio_match_one(func, ids))
83 return ids;
84 ids++;
85 }
86 }
87
88 return NULL;
89 }
90
sdio_bus_match(struct device * dev,struct device_driver * drv)91 static int sdio_bus_match(struct device *dev, struct device_driver *drv)
92 {
93 struct sdio_func *func = dev_to_sdio_func(dev);
94 struct sdio_driver *sdrv = to_sdio_driver(drv);
95
96 if (sdio_match_device(func, sdrv))
97 return 1;
98
99 return 0;
100 }
101
102 static int
sdio_bus_uevent(struct device * dev,struct kobj_uevent_env * env)103 sdio_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
104 {
105 struct sdio_func *func = dev_to_sdio_func(dev);
106
107 if (add_uevent_var(env,
108 "SDIO_CLASS=%02X", func->class))
109 return -ENOMEM;
110
111 if (add_uevent_var(env,
112 "SDIO_ID=%04X:%04X", func->vendor, func->device))
113 return -ENOMEM;
114
115 if (add_uevent_var(env,
116 "MODALIAS=sdio:c%02Xv%04Xd%04X",
117 func->class, func->vendor, func->device))
118 return -ENOMEM;
119
120 return 0;
121 }
122
sdio_bus_probe(struct device * dev)123 static int sdio_bus_probe(struct device *dev)
124 {
125 struct sdio_driver *drv = to_sdio_driver(dev->driver);
126 struct sdio_func *func = dev_to_sdio_func(dev);
127 const struct sdio_device_id *id;
128 int ret;
129
130 id = sdio_match_device(func, drv);
131 if (!id)
132 return -ENODEV;
133
134 /* Set the default block size so the driver is sure it's something
135 * sensible. */
136 sdio_claim_host(func);
137 ret = sdio_set_block_size(func, 0);
138 sdio_release_host(func);
139 if (ret)
140 return ret;
141
142 return drv->probe(func, id);
143 }
144
sdio_bus_remove(struct device * dev)145 static int sdio_bus_remove(struct device *dev)
146 {
147 struct sdio_driver *drv = to_sdio_driver(dev->driver);
148 struct sdio_func *func = dev_to_sdio_func(dev);
149
150 drv->remove(func);
151
152 if (func->irq_handler) {
153 printk(KERN_WARNING "WARNING: driver %s did not remove "
154 "its interrupt handler!\n", drv->name);
155 sdio_claim_host(func);
156 sdio_release_irq(func);
157 sdio_release_host(func);
158 }
159
160 return 0;
161 }
162
163 static struct bus_type sdio_bus_type = {
164 .name = "sdio",
165 .dev_attrs = sdio_dev_attrs,
166 .match = sdio_bus_match,
167 .uevent = sdio_bus_uevent,
168 .probe = sdio_bus_probe,
169 .remove = sdio_bus_remove,
170 };
171
sdio_register_bus(void)172 int sdio_register_bus(void)
173 {
174 return bus_register(&sdio_bus_type);
175 }
176
sdio_unregister_bus(void)177 void sdio_unregister_bus(void)
178 {
179 bus_unregister(&sdio_bus_type);
180 }
181
182 /**
183 * sdio_register_driver - register a function driver
184 * @drv: SDIO function driver
185 */
sdio_register_driver(struct sdio_driver * drv)186 int sdio_register_driver(struct sdio_driver *drv)
187 {
188 drv->drv.name = drv->name;
189 drv->drv.bus = &sdio_bus_type;
190 return driver_register(&drv->drv);
191 }
192 EXPORT_SYMBOL_GPL(sdio_register_driver);
193
194 /**
195 * sdio_unregister_driver - unregister a function driver
196 * @drv: SDIO function driver
197 */
sdio_unregister_driver(struct sdio_driver * drv)198 void sdio_unregister_driver(struct sdio_driver *drv)
199 {
200 drv->drv.bus = &sdio_bus_type;
201 driver_unregister(&drv->drv);
202 }
203 EXPORT_SYMBOL_GPL(sdio_unregister_driver);
204
sdio_release_func(struct device * dev)205 static void sdio_release_func(struct device *dev)
206 {
207 struct sdio_func *func = dev_to_sdio_func(dev);
208
209 #ifdef CONFIG_MMC_EMBEDDED_SDIO
210 /*
211 * If this device is embedded then we never allocated
212 * cis tables for this func
213 */
214 if (!func->card->host->embedded_sdio_data.funcs)
215 #endif
216 sdio_free_func_cis(func);
217
218 if (func->info)
219 kfree(func->info);
220
221 kfree(func);
222 }
223
224 /*
225 * Allocate and initialise a new SDIO function structure.
226 */
sdio_alloc_func(struct mmc_card * card)227 struct sdio_func *sdio_alloc_func(struct mmc_card *card)
228 {
229 struct sdio_func *func;
230
231 func = kzalloc(sizeof(struct sdio_func), GFP_KERNEL);
232 if (!func)
233 return ERR_PTR(-ENOMEM);
234
235 func->card = card;
236
237 device_initialize(&func->dev);
238
239 func->dev.parent = &card->dev;
240 func->dev.bus = &sdio_bus_type;
241 func->dev.release = sdio_release_func;
242
243 return func;
244 }
245
246 /*
247 * Register a new SDIO function with the driver model.
248 */
sdio_add_func(struct sdio_func * func)249 int sdio_add_func(struct sdio_func *func)
250 {
251 int ret;
252
253 dev_set_name(&func->dev, "%s:%d", mmc_card_id(func->card), func->num);
254
255 ret = device_add(&func->dev);
256 if (ret == 0)
257 sdio_func_set_present(func);
258
259 return ret;
260 }
261
262 /*
263 * Unregister a SDIO function with the driver model, and
264 * (eventually) free it.
265 */
sdio_remove_func(struct sdio_func * func)266 void sdio_remove_func(struct sdio_func *func)
267 {
268 if (sdio_func_present(func))
269 device_del(&func->dev);
270
271 put_device(&func->dev);
272 }
273
274