Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 16 | #include <linux/slab.h> |
Ohad Ben-Cohen | 80fd933 | 2010-10-02 13:54:09 +0200 | [diff] [blame] | 17 | #include <linux/pm_runtime.h> |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 18 | |
| 19 | #include <linux/mmc/card.h> |
| 20 | #include <linux/mmc/sdio_func.h> |
| 21 | |
Nicolas Pitre | b1538bc | 2007-06-16 02:06:47 -0400 | [diff] [blame] | 22 | #include "sdio_cis.h" |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 23 | #include "sdio_bus.h" |
| 24 | |
Pierre Ossman | bcfe66e | 2007-06-17 11:42:21 +0200 | [diff] [blame] | 25 | /* show configuration fields */ |
| 26 | #define sdio_config_attr(field, format_string) \ |
| 27 | static ssize_t \ |
| 28 | field##_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 | |
| 36 | sdio_config_attr(class, "0x%02x\n"); |
| 37 | sdio_config_attr(vendor, "0x%04x\n"); |
| 38 | sdio_config_attr(device, "0x%04x\n"); |
| 39 | |
| 40 | static 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 Bunk | 22bfc97 | 2007-07-29 16:58:09 +0200 | [diff] [blame] | 48 | static struct device_attribute sdio_dev_attrs[] = { |
Pierre Ossman | bcfe66e | 2007-06-17 11:42:21 +0200 | [diff] [blame] | 49 | __ATTR_RO(class), |
| 50 | __ATTR_RO(vendor), |
| 51 | __ATTR_RO(device), |
| 52 | __ATTR_RO(modalias), |
| 53 | __ATTR_NULL, |
| 54 | }; |
| 55 | |
Pierre Ossman | 3b38bea | 2007-06-16 15:54:55 +0200 | [diff] [blame] | 56 | static 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 | |
| 68 | static 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 Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 86 | static int sdio_bus_match(struct device *dev, struct device_driver *drv) |
| 87 | { |
Pierre Ossman | 3b38bea | 2007-06-16 15:54:55 +0200 | [diff] [blame] | 88 | 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 Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | static int |
Al Viro | 7ac0326 | 2007-10-14 05:46:09 +0100 | [diff] [blame] | 98 | sdio_bus_uevent(struct device *dev, struct kobj_uevent_env *env) |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 99 | { |
Pierre Ossman | d59b66c | 2007-06-17 11:34:23 +0200 | [diff] [blame] | 100 | struct sdio_func *func = dev_to_sdio_func(dev); |
Pierre Ossman | d59b66c | 2007-06-17 11:34:23 +0200 | [diff] [blame] | 101 | |
Al Viro | 7ac0326 | 2007-10-14 05:46:09 +0100 | [diff] [blame] | 102 | if (add_uevent_var(env, |
Pierre Ossman | d59b66c | 2007-06-17 11:34:23 +0200 | [diff] [blame] | 103 | "SDIO_CLASS=%02X", func->class)) |
| 104 | return -ENOMEM; |
| 105 | |
Al Viro | 7ac0326 | 2007-10-14 05:46:09 +0100 | [diff] [blame] | 106 | if (add_uevent_var(env, |
Pierre Ossman | d59b66c | 2007-06-17 11:34:23 +0200 | [diff] [blame] | 107 | "SDIO_ID=%04X:%04X", func->vendor, func->device)) |
| 108 | return -ENOMEM; |
| 109 | |
Al Viro | 7ac0326 | 2007-10-14 05:46:09 +0100 | [diff] [blame] | 110 | if (add_uevent_var(env, |
Pierre Ossman | d59b66c | 2007-06-17 11:34:23 +0200 | [diff] [blame] | 111 | "MODALIAS=sdio:c%02Xv%04Xd%04X", |
| 112 | func->class, func->vendor, func->device)) |
| 113 | return -ENOMEM; |
| 114 | |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | static int sdio_bus_probe(struct device *dev) |
| 119 | { |
Pierre Ossman | 3b38bea | 2007-06-16 15:54:55 +0200 | [diff] [blame] | 120 | 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 Vrabel | 9a08f82 | 2007-08-08 14:23:48 +0100 | [diff] [blame] | 123 | int ret; |
Pierre Ossman | 3b38bea | 2007-06-16 15:54:55 +0200 | [diff] [blame] | 124 | |
| 125 | id = sdio_match_device(func, drv); |
| 126 | if (!id) |
| 127 | return -ENODEV; |
| 128 | |
Ohad Ben-Cohen | 40bba0c | 2010-10-02 13:54:11 +0200 | [diff] [blame] | 129 | /* 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 Vrabel | 9a08f82 | 2007-08-08 14:23:48 +0100 | [diff] [blame] | 139 | /* 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-Cohen | 40bba0c | 2010-10-02 13:54:11 +0200 | [diff] [blame] | 145 | goto disable_runtimepm; |
David Vrabel | 9a08f82 | 2007-08-08 14:23:48 +0100 | [diff] [blame] | 146 | |
Ohad Ben-Cohen | 40bba0c | 2010-10-02 13:54:11 +0200 | [diff] [blame] | 147 | ret = drv->probe(func, id); |
| 148 | if (ret) |
| 149 | goto disable_runtimepm; |
| 150 | |
| 151 | return 0; |
| 152 | |
| 153 | disable_runtimepm: |
| 154 | pm_runtime_put_noidle(dev); |
| 155 | out: |
| 156 | return ret; |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | static int sdio_bus_remove(struct device *dev) |
| 160 | { |
Pierre Ossman | 3b38bea | 2007-06-16 15:54:55 +0200 | [diff] [blame] | 161 | struct sdio_driver *drv = to_sdio_driver(dev->driver); |
| 162 | struct sdio_func *func = dev_to_sdio_func(dev); |
Ohad Ben-Cohen | 40bba0c | 2010-10-02 13:54:11 +0200 | [diff] [blame] | 163 | 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 Ossman | 3b38bea | 2007-06-16 15:54:55 +0200 | [diff] [blame] | 169 | |
| 170 | drv->remove(func); |
| 171 | |
Nicolas Pitre | d1496c3 | 2007-06-30 16:29:41 +0200 | [diff] [blame] | 172 | 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-Cohen | 40bba0c | 2010-10-02 13:54:11 +0200 | [diff] [blame] | 180 | /* 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 | |
| 186 | out: |
| 187 | return ret; |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 188 | } |
| 189 | |
Ohad Ben-Cohen | 80fd933 | 2010-10-02 13:54:09 +0200 | [diff] [blame] | 190 | #ifdef CONFIG_PM_RUNTIME |
| 191 | |
Ohad Ben-Cohen | ed2a978 | 2010-10-02 13:54:13 +0200 | [diff] [blame] | 192 | static int sdio_bus_pm_prepare(struct device *dev) |
| 193 | { |
| 194 | /* |
| 195 | * Resume an SDIO device which was suspended at run time at this |
| 196 | * point, in order to allow standard SDIO suspend/resume paths |
| 197 | * to keep working as usual. |
| 198 | * |
| 199 | * Ultimately, the SDIO driver itself will decide (in its |
| 200 | * suspend handler, or lack thereof) whether the card should be |
| 201 | * removed or kept, and if kept, at what power state. |
| 202 | * |
| 203 | * At this point, PM core have increased our use count, so it's |
| 204 | * safe to directly resume the device. After system is resumed |
| 205 | * again, PM core will drop back its runtime PM use count, and if |
| 206 | * needed device will be suspended again. |
| 207 | * |
| 208 | * The end result is guaranteed to be a power state that is |
| 209 | * coherent with the device's runtime PM use count. |
| 210 | * |
| 211 | * The return value of pm_runtime_resume is deliberately unchecked |
| 212 | * since there is little point in failing system suspend if a |
| 213 | * device can't be resumed. |
| 214 | */ |
| 215 | pm_runtime_resume(dev); |
| 216 | |
| 217 | return 0; |
| 218 | } |
| 219 | |
Ohad Ben-Cohen | 80fd933 | 2010-10-02 13:54:09 +0200 | [diff] [blame] | 220 | static const struct dev_pm_ops sdio_bus_pm_ops = { |
| 221 | SET_RUNTIME_PM_OPS( |
| 222 | pm_generic_runtime_suspend, |
| 223 | pm_generic_runtime_resume, |
| 224 | pm_generic_runtime_idle |
| 225 | ) |
Ohad Ben-Cohen | ed2a978 | 2010-10-02 13:54:13 +0200 | [diff] [blame] | 226 | .prepare = sdio_bus_pm_prepare, |
Ohad Ben-Cohen | 80fd933 | 2010-10-02 13:54:09 +0200 | [diff] [blame] | 227 | }; |
| 228 | |
| 229 | #define SDIO_PM_OPS_PTR (&sdio_bus_pm_ops) |
| 230 | |
| 231 | #else /* !CONFIG_PM_RUNTIME */ |
| 232 | |
| 233 | #define SDIO_PM_OPS_PTR NULL |
| 234 | |
| 235 | #endif /* !CONFIG_PM_RUNTIME */ |
| 236 | |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 237 | static struct bus_type sdio_bus_type = { |
| 238 | .name = "sdio", |
Pierre Ossman | bcfe66e | 2007-06-17 11:42:21 +0200 | [diff] [blame] | 239 | .dev_attrs = sdio_dev_attrs, |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 240 | .match = sdio_bus_match, |
| 241 | .uevent = sdio_bus_uevent, |
| 242 | .probe = sdio_bus_probe, |
| 243 | .remove = sdio_bus_remove, |
Ohad Ben-Cohen | 80fd933 | 2010-10-02 13:54:09 +0200 | [diff] [blame] | 244 | .pm = SDIO_PM_OPS_PTR, |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 245 | }; |
| 246 | |
| 247 | int sdio_register_bus(void) |
| 248 | { |
| 249 | return bus_register(&sdio_bus_type); |
| 250 | } |
| 251 | |
| 252 | void sdio_unregister_bus(void) |
| 253 | { |
| 254 | bus_unregister(&sdio_bus_type); |
| 255 | } |
| 256 | |
Pierre Ossman | f76c851 | 2007-05-27 12:00:02 +0200 | [diff] [blame] | 257 | /** |
| 258 | * sdio_register_driver - register a function driver |
| 259 | * @drv: SDIO function driver |
| 260 | */ |
| 261 | int sdio_register_driver(struct sdio_driver *drv) |
| 262 | { |
| 263 | drv->drv.name = drv->name; |
| 264 | drv->drv.bus = &sdio_bus_type; |
| 265 | return driver_register(&drv->drv); |
| 266 | } |
| 267 | EXPORT_SYMBOL_GPL(sdio_register_driver); |
| 268 | |
| 269 | /** |
| 270 | * sdio_unregister_driver - unregister a function driver |
| 271 | * @drv: SDIO function driver |
| 272 | */ |
| 273 | void sdio_unregister_driver(struct sdio_driver *drv) |
| 274 | { |
| 275 | drv->drv.bus = &sdio_bus_type; |
| 276 | driver_unregister(&drv->drv); |
| 277 | } |
| 278 | EXPORT_SYMBOL_GPL(sdio_unregister_driver); |
| 279 | |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 280 | static void sdio_release_func(struct device *dev) |
| 281 | { |
| 282 | struct sdio_func *func = dev_to_sdio_func(dev); |
| 283 | |
Pierre Ossman | 1a632f8 | 2007-07-30 15:15:30 +0200 | [diff] [blame] | 284 | sdio_free_func_cis(func); |
Nicolas Pitre | b1538bc | 2007-06-16 02:06:47 -0400 | [diff] [blame] | 285 | |
Pierre Ossman | 759bdc7 | 2007-09-19 18:42:16 +0200 | [diff] [blame] | 286 | if (func->info) |
| 287 | kfree(func->info); |
| 288 | |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 289 | kfree(func); |
| 290 | } |
| 291 | |
| 292 | /* |
| 293 | * Allocate and initialise a new SDIO function structure. |
| 294 | */ |
| 295 | struct sdio_func *sdio_alloc_func(struct mmc_card *card) |
| 296 | { |
| 297 | struct sdio_func *func; |
| 298 | |
Mariusz Kozlowski | 9f2fcf9 | 2007-08-01 00:05:24 +0200 | [diff] [blame] | 299 | func = kzalloc(sizeof(struct sdio_func), GFP_KERNEL); |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 300 | if (!func) |
| 301 | return ERR_PTR(-ENOMEM); |
| 302 | |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 303 | func->card = card; |
| 304 | |
| 305 | device_initialize(&func->dev); |
| 306 | |
| 307 | func->dev.parent = &card->dev; |
| 308 | func->dev.bus = &sdio_bus_type; |
| 309 | func->dev.release = sdio_release_func; |
| 310 | |
| 311 | return func; |
| 312 | } |
| 313 | |
| 314 | /* |
| 315 | * Register a new SDIO function with the driver model. |
| 316 | */ |
| 317 | int sdio_add_func(struct sdio_func *func) |
| 318 | { |
| 319 | int ret; |
| 320 | |
Kay Sievers | d1b2686 | 2008-11-08 21:37:46 +0100 | [diff] [blame] | 321 | dev_set_name(&func->dev, "%s:%d", mmc_card_id(func->card), func->num); |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 322 | |
| 323 | ret = device_add(&func->dev); |
| 324 | if (ret == 0) |
| 325 | sdio_func_set_present(func); |
| 326 | |
| 327 | return ret; |
| 328 | } |
| 329 | |
| 330 | /* |
| 331 | * Unregister a SDIO function with the driver model, and |
| 332 | * (eventually) free it. |
Daniel Drake | 3d10a1b | 2009-12-17 15:27:17 -0800 | [diff] [blame] | 333 | * This function can be called through error paths where sdio_add_func() was |
| 334 | * never executed (because a failure occurred at an earlier point). |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 335 | */ |
| 336 | void sdio_remove_func(struct sdio_func *func) |
| 337 | { |
Daniel Drake | 3d10a1b | 2009-12-17 15:27:17 -0800 | [diff] [blame] | 338 | if (!sdio_func_present(func)) |
| 339 | return; |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 340 | |
Daniel Drake | 3d10a1b | 2009-12-17 15:27:17 -0800 | [diff] [blame] | 341 | device_del(&func->dev); |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 342 | put_device(&func->dev); |
| 343 | } |
| 344 | |