blob: 256a968774a0f5e0833ec42493f2e70ea8c87230 [file] [log] [blame]
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Ohad Ben-Cohen80fd9332010-10-02 13:54:09 +020017#include <linux/pm_runtime.h>
Pierre Ossmane29a7d72007-05-26 13:48:18 +020018
19#include <linux/mmc/card.h>
20#include <linux/mmc/sdio_func.h>
21
Nicolas Pitreb1538bc2007-06-16 02:06:47 -040022#include "sdio_cis.h"
Pierre Ossmane29a7d72007-05-26 13:48:18 +020023#include "sdio_bus.h"
24
Pierre Ossmanbcfe66e2007-06-17 11:42:21 +020025/* show configuration fields */
26#define sdio_config_attr(field, format_string) \
27static ssize_t \
28field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
29{ \
30 struct sdio_func *func; \
31 \
32 func = dev_to_sdio_func (dev); \
33 return sprintf (buf, format_string, func->field); \
34}
35
36sdio_config_attr(class, "0x%02x\n");
37sdio_config_attr(vendor, "0x%04x\n");
38sdio_config_attr(device, "0x%04x\n");
39
40static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
41{
42 struct sdio_func *func = dev_to_sdio_func (dev);
43
44 return sprintf(buf, "sdio:c%02Xv%04Xd%04X\n",
45 func->class, func->vendor, func->device);
46}
47
Adrian Bunk22bfc972007-07-29 16:58:09 +020048static struct device_attribute sdio_dev_attrs[] = {
Pierre Ossmanbcfe66e2007-06-17 11:42:21 +020049 __ATTR_RO(class),
50 __ATTR_RO(vendor),
51 __ATTR_RO(device),
52 __ATTR_RO(modalias),
53 __ATTR_NULL,
54};
55
Pierre Ossman3b38bea2007-06-16 15:54:55 +020056static const struct sdio_device_id *sdio_match_one(struct sdio_func *func,
57 const struct sdio_device_id *id)
58{
59 if (id->class != (__u8)SDIO_ANY_ID && id->class != func->class)
60 return NULL;
61 if (id->vendor != (__u16)SDIO_ANY_ID && id->vendor != func->vendor)
62 return NULL;
63 if (id->device != (__u16)SDIO_ANY_ID && id->device != func->device)
64 return NULL;
65 return id;
66}
67
68static const struct sdio_device_id *sdio_match_device(struct sdio_func *func,
69 struct sdio_driver *sdrv)
70{
71 const struct sdio_device_id *ids;
72
73 ids = sdrv->id_table;
74
75 if (ids) {
76 while (ids->class || ids->vendor || ids->device) {
77 if (sdio_match_one(func, ids))
78 return ids;
79 ids++;
80 }
81 }
82
83 return NULL;
84}
85
Pierre Ossmane29a7d72007-05-26 13:48:18 +020086static int sdio_bus_match(struct device *dev, struct device_driver *drv)
87{
Pierre Ossman3b38bea2007-06-16 15:54:55 +020088 struct sdio_func *func = dev_to_sdio_func(dev);
89 struct sdio_driver *sdrv = to_sdio_driver(drv);
90
91 if (sdio_match_device(func, sdrv))
92 return 1;
93
94 return 0;
Pierre Ossmane29a7d72007-05-26 13:48:18 +020095}
96
97static int
Al Viro7ac03262007-10-14 05:46:09 +010098sdio_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
Pierre Ossmane29a7d72007-05-26 13:48:18 +020099{
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200100 struct sdio_func *func = dev_to_sdio_func(dev);
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200101
Al Viro7ac03262007-10-14 05:46:09 +0100102 if (add_uevent_var(env,
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200103 "SDIO_CLASS=%02X", func->class))
104 return -ENOMEM;
105
Al Viro7ac03262007-10-14 05:46:09 +0100106 if (add_uevent_var(env,
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200107 "SDIO_ID=%04X:%04X", func->vendor, func->device))
108 return -ENOMEM;
109
Al Viro7ac03262007-10-14 05:46:09 +0100110 if (add_uevent_var(env,
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200111 "MODALIAS=sdio:c%02Xv%04Xd%04X",
112 func->class, func->vendor, func->device))
113 return -ENOMEM;
114
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200115 return 0;
116}
117
118static int sdio_bus_probe(struct device *dev)
119{
Pierre Ossman3b38bea2007-06-16 15:54:55 +0200120 struct sdio_driver *drv = to_sdio_driver(dev->driver);
121 struct sdio_func *func = dev_to_sdio_func(dev);
122 const struct sdio_device_id *id;
David Vrabel9a08f822007-08-08 14:23:48 +0100123 int ret;
Pierre Ossman3b38bea2007-06-16 15:54:55 +0200124
125 id = sdio_match_device(func, drv);
126 if (!id)
127 return -ENODEV;
128
David Vrabel9a08f822007-08-08 14:23:48 +0100129 /* Set the default block size so the driver is sure it's something
130 * sensible. */
131 sdio_claim_host(func);
132 ret = sdio_set_block_size(func, 0);
133 sdio_release_host(func);
134 if (ret)
135 return ret;
136
Pierre Ossman3b38bea2007-06-16 15:54:55 +0200137 return drv->probe(func, id);
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200138}
139
140static int sdio_bus_remove(struct device *dev)
141{
Pierre Ossman3b38bea2007-06-16 15:54:55 +0200142 struct sdio_driver *drv = to_sdio_driver(dev->driver);
143 struct sdio_func *func = dev_to_sdio_func(dev);
144
145 drv->remove(func);
146
Nicolas Pitred1496c32007-06-30 16:29:41 +0200147 if (func->irq_handler) {
148 printk(KERN_WARNING "WARNING: driver %s did not remove "
149 "its interrupt handler!\n", drv->name);
150 sdio_claim_host(func);
151 sdio_release_irq(func);
152 sdio_release_host(func);
153 }
154
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200155 return 0;
156}
157
Ohad Ben-Cohen80fd9332010-10-02 13:54:09 +0200158#ifdef CONFIG_PM_RUNTIME
159
160static const struct dev_pm_ops sdio_bus_pm_ops = {
161 SET_RUNTIME_PM_OPS(
162 pm_generic_runtime_suspend,
163 pm_generic_runtime_resume,
164 pm_generic_runtime_idle
165 )
166};
167
168#define SDIO_PM_OPS_PTR (&sdio_bus_pm_ops)
169
170#else /* !CONFIG_PM_RUNTIME */
171
172#define SDIO_PM_OPS_PTR NULL
173
174#endif /* !CONFIG_PM_RUNTIME */
175
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200176static struct bus_type sdio_bus_type = {
177 .name = "sdio",
Pierre Ossmanbcfe66e2007-06-17 11:42:21 +0200178 .dev_attrs = sdio_dev_attrs,
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200179 .match = sdio_bus_match,
180 .uevent = sdio_bus_uevent,
181 .probe = sdio_bus_probe,
182 .remove = sdio_bus_remove,
Ohad Ben-Cohen80fd9332010-10-02 13:54:09 +0200183 .pm = SDIO_PM_OPS_PTR,
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200184};
185
186int sdio_register_bus(void)
187{
188 return bus_register(&sdio_bus_type);
189}
190
191void sdio_unregister_bus(void)
192{
193 bus_unregister(&sdio_bus_type);
194}
195
Pierre Ossmanf76c8512007-05-27 12:00:02 +0200196/**
197 * sdio_register_driver - register a function driver
198 * @drv: SDIO function driver
199 */
200int sdio_register_driver(struct sdio_driver *drv)
201{
202 drv->drv.name = drv->name;
203 drv->drv.bus = &sdio_bus_type;
204 return driver_register(&drv->drv);
205}
206EXPORT_SYMBOL_GPL(sdio_register_driver);
207
208/**
209 * sdio_unregister_driver - unregister a function driver
210 * @drv: SDIO function driver
211 */
212void sdio_unregister_driver(struct sdio_driver *drv)
213{
214 drv->drv.bus = &sdio_bus_type;
215 driver_unregister(&drv->drv);
216}
217EXPORT_SYMBOL_GPL(sdio_unregister_driver);
218
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200219static void sdio_release_func(struct device *dev)
220{
221 struct sdio_func *func = dev_to_sdio_func(dev);
222
Pierre Ossman1a632f82007-07-30 15:15:30 +0200223 sdio_free_func_cis(func);
Nicolas Pitreb1538bc2007-06-16 02:06:47 -0400224
Pierre Ossman759bdc72007-09-19 18:42:16 +0200225 if (func->info)
226 kfree(func->info);
227
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200228 kfree(func);
229}
230
231/*
232 * Allocate and initialise a new SDIO function structure.
233 */
234struct sdio_func *sdio_alloc_func(struct mmc_card *card)
235{
236 struct sdio_func *func;
237
Mariusz Kozlowski9f2fcf92007-08-01 00:05:24 +0200238 func = kzalloc(sizeof(struct sdio_func), GFP_KERNEL);
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200239 if (!func)
240 return ERR_PTR(-ENOMEM);
241
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200242 func->card = card;
243
244 device_initialize(&func->dev);
245
246 func->dev.parent = &card->dev;
247 func->dev.bus = &sdio_bus_type;
248 func->dev.release = sdio_release_func;
249
250 return func;
251}
252
253/*
254 * Register a new SDIO function with the driver model.
255 */
256int sdio_add_func(struct sdio_func *func)
257{
258 int ret;
259
Kay Sieversd1b26862008-11-08 21:37:46 +0100260 dev_set_name(&func->dev, "%s:%d", mmc_card_id(func->card), func->num);
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200261
262 ret = device_add(&func->dev);
263 if (ret == 0)
264 sdio_func_set_present(func);
265
266 return ret;
267}
268
269/*
270 * Unregister a SDIO function with the driver model, and
271 * (eventually) free it.
Daniel Drake3d10a1b2009-12-17 15:27:17 -0800272 * This function can be called through error paths where sdio_add_func() was
273 * never executed (because a failure occurred at an earlier point).
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200274 */
275void sdio_remove_func(struct sdio_func *func)
276{
Daniel Drake3d10a1b2009-12-17 15:27:17 -0800277 if (!sdio_func_present(func))
278 return;
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200279
Daniel Drake3d10a1b2009-12-17 15:27:17 -0800280 device_del(&func->dev);
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200281 put_device(&func->dev);
282}
283