[ARM] pxa: update defconfig for Verdex Pro
[linux-2.6/verdex.git] / drivers / mmc / core / sdio_bus.c
blobd37464e296a507deb743182c3764714f83cdda9c
1 /*
2 * linux/drivers/mmc/core/sdio_bus.c
4 * Copyright 2007 Pierre Ossman
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.
11 * SDIO function driver model
14 #include <linux/device.h>
15 #include <linux/err.h>
17 #include <linux/mmc/card.h>
18 #include <linux/mmc/sdio_func.h>
20 #include "sdio_cis.h"
21 #include "sdio_bus.h"
23 /* show configuration fields */
24 #define sdio_config_attr(field, format_string) \
25 static ssize_t \
26 field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
27 { \
28 struct sdio_func *func; \
30 func = dev_to_sdio_func (dev); \
31 return sprintf (buf, format_string, func->field); \
34 sdio_config_attr(class, "0x%02x\n");
35 sdio_config_attr(vendor, "0x%04x\n");
36 sdio_config_attr(device, "0x%04x\n");
38 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
40 struct sdio_func *func = dev_to_sdio_func (dev);
42 return sprintf(buf, "sdio:c%02Xv%04Xd%04X\n",
43 func->class, func->vendor, func->device);
46 static 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,
54 static const struct sdio_device_id *sdio_match_one(struct sdio_func *func,
55 const struct sdio_device_id *id)
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;
66 static const struct sdio_device_id *sdio_match_device(struct sdio_func *func,
67 struct sdio_driver *sdrv)
69 const struct sdio_device_id *ids;
71 ids = sdrv->id_table;
73 if (ids) {
74 while (ids->class || ids->vendor || ids->device) {
75 if (sdio_match_one(func, ids))
76 return ids;
77 ids++;
81 return NULL;
84 static int sdio_bus_match(struct device *dev, struct device_driver *drv)
86 struct sdio_func *func = dev_to_sdio_func(dev);
87 struct sdio_driver *sdrv = to_sdio_driver(drv);
89 if (sdio_match_device(func, sdrv))
90 return 1;
92 return 0;
95 static int
96 sdio_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
98 struct sdio_func *func = dev_to_sdio_func(dev);
100 if (add_uevent_var(env,
101 "SDIO_CLASS=%02X", func->class))
102 return -ENOMEM;
104 if (add_uevent_var(env,
105 "SDIO_ID=%04X:%04X", func->vendor, func->device))
106 return -ENOMEM;
108 if (add_uevent_var(env,
109 "MODALIAS=sdio:c%02Xv%04Xd%04X",
110 func->class, func->vendor, func->device))
111 return -ENOMEM;
113 return 0;
116 static int sdio_bus_probe(struct device *dev)
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;
123 id = sdio_match_device(func, drv);
124 if (!id)
125 return -ENODEV;
127 /* Set the default block size so the driver is sure it's something
128 * sensible. */
129 sdio_claim_host(func);
130 ret = sdio_set_block_size(func, 0);
131 sdio_release_host(func);
132 if (ret)
133 return ret;
135 return drv->probe(func, id);
138 static int sdio_bus_remove(struct device *dev)
140 struct sdio_driver *drv = to_sdio_driver(dev->driver);
141 struct sdio_func *func = dev_to_sdio_func(dev);
143 drv->remove(func);
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);
153 return 0;
156 static 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,
165 int sdio_register_bus(void)
167 return bus_register(&sdio_bus_type);
170 void sdio_unregister_bus(void)
172 bus_unregister(&sdio_bus_type);
176 * sdio_register_driver - register a function driver
177 * @drv: SDIO function driver
179 int sdio_register_driver(struct sdio_driver *drv)
181 drv->drv.name = drv->name;
182 drv->drv.bus = &sdio_bus_type;
183 return driver_register(&drv->drv);
185 EXPORT_SYMBOL_GPL(sdio_register_driver);
188 * sdio_unregister_driver - unregister a function driver
189 * @drv: SDIO function driver
191 void sdio_unregister_driver(struct sdio_driver *drv)
193 drv->drv.bus = &sdio_bus_type;
194 driver_unregister(&drv->drv);
196 EXPORT_SYMBOL_GPL(sdio_unregister_driver);
198 static void sdio_release_func(struct device *dev)
200 struct sdio_func *func = dev_to_sdio_func(dev);
202 sdio_free_func_cis(func);
204 if (func->info)
205 kfree(func->info);
207 kfree(func);
211 * Allocate and initialise a new SDIO function structure.
213 struct sdio_func *sdio_alloc_func(struct mmc_card *card)
215 struct sdio_func *func;
217 func = kzalloc(sizeof(struct sdio_func), GFP_KERNEL);
218 if (!func)
219 return ERR_PTR(-ENOMEM);
221 func->card = card;
223 device_initialize(&func->dev);
225 func->dev.parent = &card->dev;
226 func->dev.bus = &sdio_bus_type;
227 func->dev.release = sdio_release_func;
229 return func;
233 * Register a new SDIO function with the driver model.
235 int sdio_add_func(struct sdio_func *func)
237 int ret;
239 dev_set_name(&func->dev, "%s:%d", mmc_card_id(func->card), func->num);
241 ret = device_add(&func->dev);
242 if (ret == 0)
243 sdio_func_set_present(func);
245 return ret;
249 * Unregister a SDIO function with the driver model, and
250 * (eventually) free it.
252 void sdio_remove_func(struct sdio_func *func)
254 if (sdio_func_present(func))
255 device_del(&func->dev);
257 put_device(&func->dev);