blob: 36374834fcff0599342fa16f454c3ba4e19017b2 [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
Ohad Ben-Cohen40bba0c2010-10-02 13:54:11 +0200129 /* Unbound SDIO functions are always suspended.
130 * During probe, the function is set active and the usage count
131 * is incremented. If the driver supports runtime PM,
132 * it should call pm_runtime_put_noidle() in its probe routine and
133 * pm_runtime_get_noresume() in its remove routine.
134 */
135 ret = pm_runtime_get_sync(dev);
136 if (ret < 0)
137 goto out;
138
David Vrabel9a08f822007-08-08 14:23:48 +0100139 /* Set the default block size so the driver is sure it's something
140 * sensible. */
141 sdio_claim_host(func);
142 ret = sdio_set_block_size(func, 0);
143 sdio_release_host(func);
144 if (ret)
Ohad Ben-Cohen40bba0c2010-10-02 13:54:11 +0200145 goto disable_runtimepm;
David Vrabel9a08f822007-08-08 14:23:48 +0100146
Ohad Ben-Cohen40bba0c2010-10-02 13:54:11 +0200147 ret = drv->probe(func, id);
148 if (ret)
149 goto disable_runtimepm;
150
151 return 0;
152
153disable_runtimepm:
154 pm_runtime_put_noidle(dev);
155out:
156 return ret;
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200157}
158
159static int sdio_bus_remove(struct device *dev)
160{
Pierre Ossman3b38bea2007-06-16 15:54:55 +0200161 struct sdio_driver *drv = to_sdio_driver(dev->driver);
162 struct sdio_func *func = dev_to_sdio_func(dev);
Ohad Ben-Cohen40bba0c2010-10-02 13:54:11 +0200163 int ret;
164
165 /* Make sure card is powered before invoking ->remove() */
166 ret = pm_runtime_get_sync(dev);
167 if (ret < 0)
168 goto out;
Pierre Ossman3b38bea2007-06-16 15:54:55 +0200169
170 drv->remove(func);
171
Nicolas Pitred1496c32007-06-30 16:29:41 +0200172 if (func->irq_handler) {
173 printk(KERN_WARNING "WARNING: driver %s did not remove "
174 "its interrupt handler!\n", drv->name);
175 sdio_claim_host(func);
176 sdio_release_irq(func);
177 sdio_release_host(func);
178 }
179
Ohad Ben-Cohen40bba0c2010-10-02 13:54:11 +0200180 /* First, undo the increment made directly above */
181 pm_runtime_put_noidle(dev);
182
183 /* Then undo the runtime PM settings in sdio_bus_probe() */
184 pm_runtime_put_noidle(dev);
185
186out:
187 return ret;
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200188}
189
Ohad Ben-Cohen80fd9332010-10-02 13:54:09 +0200190#ifdef CONFIG_PM_RUNTIME
191
192static const struct dev_pm_ops sdio_bus_pm_ops = {
193 SET_RUNTIME_PM_OPS(
194 pm_generic_runtime_suspend,
195 pm_generic_runtime_resume,
196 pm_generic_runtime_idle
197 )
198};
199
200#define SDIO_PM_OPS_PTR (&sdio_bus_pm_ops)
201
202#else /* !CONFIG_PM_RUNTIME */
203
204#define SDIO_PM_OPS_PTR NULL
205
206#endif /* !CONFIG_PM_RUNTIME */
207
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200208static struct bus_type sdio_bus_type = {
209 .name = "sdio",
Pierre Ossmanbcfe66e2007-06-17 11:42:21 +0200210 .dev_attrs = sdio_dev_attrs,
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200211 .match = sdio_bus_match,
212 .uevent = sdio_bus_uevent,
213 .probe = sdio_bus_probe,
214 .remove = sdio_bus_remove,
Ohad Ben-Cohen80fd9332010-10-02 13:54:09 +0200215 .pm = SDIO_PM_OPS_PTR,
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200216};
217
218int sdio_register_bus(void)
219{
220 return bus_register(&sdio_bus_type);
221}
222
223void sdio_unregister_bus(void)
224{
225 bus_unregister(&sdio_bus_type);
226}
227
Pierre Ossmanf76c8512007-05-27 12:00:02 +0200228/**
229 * sdio_register_driver - register a function driver
230 * @drv: SDIO function driver
231 */
232int sdio_register_driver(struct sdio_driver *drv)
233{
234 drv->drv.name = drv->name;
235 drv->drv.bus = &sdio_bus_type;
236 return driver_register(&drv->drv);
237}
238EXPORT_SYMBOL_GPL(sdio_register_driver);
239
240/**
241 * sdio_unregister_driver - unregister a function driver
242 * @drv: SDIO function driver
243 */
244void sdio_unregister_driver(struct sdio_driver *drv)
245{
246 drv->drv.bus = &sdio_bus_type;
247 driver_unregister(&drv->drv);
248}
249EXPORT_SYMBOL_GPL(sdio_unregister_driver);
250
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200251static void sdio_release_func(struct device *dev)
252{
253 struct sdio_func *func = dev_to_sdio_func(dev);
254
Pierre Ossman1a632f82007-07-30 15:15:30 +0200255 sdio_free_func_cis(func);
Nicolas Pitreb1538bc2007-06-16 02:06:47 -0400256
Pierre Ossman759bdc72007-09-19 18:42:16 +0200257 if (func->info)
258 kfree(func->info);
259
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200260 kfree(func);
261}
262
263/*
264 * Allocate and initialise a new SDIO function structure.
265 */
266struct sdio_func *sdio_alloc_func(struct mmc_card *card)
267{
268 struct sdio_func *func;
269
Mariusz Kozlowski9f2fcf92007-08-01 00:05:24 +0200270 func = kzalloc(sizeof(struct sdio_func), GFP_KERNEL);
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200271 if (!func)
272 return ERR_PTR(-ENOMEM);
273
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200274 func->card = card;
275
276 device_initialize(&func->dev);
277
278 func->dev.parent = &card->dev;
279 func->dev.bus = &sdio_bus_type;
280 func->dev.release = sdio_release_func;
281
282 return func;
283}
284
285/*
286 * Register a new SDIO function with the driver model.
287 */
288int sdio_add_func(struct sdio_func *func)
289{
290 int ret;
291
Kay Sieversd1b26862008-11-08 21:37:46 +0100292 dev_set_name(&func->dev, "%s:%d", mmc_card_id(func->card), func->num);
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200293
294 ret = device_add(&func->dev);
295 if (ret == 0)
296 sdio_func_set_present(func);
297
298 return ret;
299}
300
301/*
302 * Unregister a SDIO function with the driver model, and
303 * (eventually) free it.
Daniel Drake3d10a1b2009-12-17 15:27:17 -0800304 * This function can be called through error paths where sdio_add_func() was
305 * never executed (because a failure occurred at an earlier point).
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200306 */
307void sdio_remove_func(struct sdio_func *func)
308{
Daniel Drake3d10a1b2009-12-17 15:27:17 -0800309 if (!sdio_func_present(func))
310 return;
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200311
Daniel Drake3d10a1b2009-12-17 15:27:17 -0800312 device_del(&func->dev);
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200313 put_device(&func->dev);
314}
315