0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/device.h>
0011 #include <linux/err.h>
0012 #include <linux/export.h>
0013 #include <linux/slab.h>
0014 #include <linux/pm_runtime.h>
0015 #include <linux/pm_domain.h>
0016 #include <linux/acpi.h>
0017 #include <linux/sysfs.h>
0018
0019 #include <linux/mmc/card.h>
0020 #include <linux/mmc/host.h>
0021 #include <linux/mmc/sdio_func.h>
0022 #include <linux/of.h>
0023
0024 #include "core.h"
0025 #include "card.h"
0026 #include "sdio_cis.h"
0027 #include "sdio_bus.h"
0028
0029 #define to_sdio_driver(d) container_of(d, struct sdio_driver, drv)
0030
0031
0032 #define sdio_config_attr(field, format_string, args...) \
0033 static ssize_t \
0034 field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
0035 { \
0036 struct sdio_func *func; \
0037 \
0038 func = dev_to_sdio_func (dev); \
0039 return sysfs_emit(buf, format_string, args); \
0040 } \
0041 static DEVICE_ATTR_RO(field)
0042
0043 sdio_config_attr(class, "0x%02x\n", func->class);
0044 sdio_config_attr(vendor, "0x%04x\n", func->vendor);
0045 sdio_config_attr(device, "0x%04x\n", func->device);
0046 sdio_config_attr(revision, "%u.%u\n", func->major_rev, func->minor_rev);
0047 sdio_config_attr(modalias, "sdio:c%02Xv%04Xd%04X\n", func->class, func->vendor, func->device);
0048
0049 #define sdio_info_attr(num) \
0050 static ssize_t info##num##_show(struct device *dev, struct device_attribute *attr, char *buf) \
0051 { \
0052 struct sdio_func *func = dev_to_sdio_func(dev); \
0053 \
0054 if (num > func->num_info) \
0055 return -ENODATA; \
0056 if (!func->info[num - 1][0]) \
0057 return 0; \
0058 return sysfs_emit(buf, "%s\n", func->info[num - 1]); \
0059 } \
0060 static DEVICE_ATTR_RO(info##num)
0061
0062 sdio_info_attr(1);
0063 sdio_info_attr(2);
0064 sdio_info_attr(3);
0065 sdio_info_attr(4);
0066
0067 static struct attribute *sdio_dev_attrs[] = {
0068 &dev_attr_class.attr,
0069 &dev_attr_vendor.attr,
0070 &dev_attr_device.attr,
0071 &dev_attr_revision.attr,
0072 &dev_attr_info1.attr,
0073 &dev_attr_info2.attr,
0074 &dev_attr_info3.attr,
0075 &dev_attr_info4.attr,
0076 &dev_attr_modalias.attr,
0077 NULL,
0078 };
0079 ATTRIBUTE_GROUPS(sdio_dev);
0080
0081 static const struct sdio_device_id *sdio_match_one(struct sdio_func *func,
0082 const struct sdio_device_id *id)
0083 {
0084 if (id->class != (__u8)SDIO_ANY_ID && id->class != func->class)
0085 return NULL;
0086 if (id->vendor != (__u16)SDIO_ANY_ID && id->vendor != func->vendor)
0087 return NULL;
0088 if (id->device != (__u16)SDIO_ANY_ID && id->device != func->device)
0089 return NULL;
0090 return id;
0091 }
0092
0093 static const struct sdio_device_id *sdio_match_device(struct sdio_func *func,
0094 struct sdio_driver *sdrv)
0095 {
0096 const struct sdio_device_id *ids;
0097
0098 ids = sdrv->id_table;
0099
0100 if (ids) {
0101 while (ids->class || ids->vendor || ids->device) {
0102 if (sdio_match_one(func, ids))
0103 return ids;
0104 ids++;
0105 }
0106 }
0107
0108 return NULL;
0109 }
0110
0111 static int sdio_bus_match(struct device *dev, struct device_driver *drv)
0112 {
0113 struct sdio_func *func = dev_to_sdio_func(dev);
0114 struct sdio_driver *sdrv = to_sdio_driver(drv);
0115
0116 if (sdio_match_device(func, sdrv))
0117 return 1;
0118
0119 return 0;
0120 }
0121
0122 static int
0123 sdio_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
0124 {
0125 struct sdio_func *func = dev_to_sdio_func(dev);
0126 unsigned int i;
0127
0128 if (add_uevent_var(env,
0129 "SDIO_CLASS=%02X", func->class))
0130 return -ENOMEM;
0131
0132 if (add_uevent_var(env,
0133 "SDIO_ID=%04X:%04X", func->vendor, func->device))
0134 return -ENOMEM;
0135
0136 if (add_uevent_var(env,
0137 "SDIO_REVISION=%u.%u", func->major_rev, func->minor_rev))
0138 return -ENOMEM;
0139
0140 for (i = 0; i < func->num_info; i++) {
0141 if (add_uevent_var(env, "SDIO_INFO%u=%s", i+1, func->info[i]))
0142 return -ENOMEM;
0143 }
0144
0145 if (add_uevent_var(env,
0146 "MODALIAS=sdio:c%02Xv%04Xd%04X",
0147 func->class, func->vendor, func->device))
0148 return -ENOMEM;
0149
0150 return 0;
0151 }
0152
0153 static int sdio_bus_probe(struct device *dev)
0154 {
0155 struct sdio_driver *drv = to_sdio_driver(dev->driver);
0156 struct sdio_func *func = dev_to_sdio_func(dev);
0157 const struct sdio_device_id *id;
0158 int ret;
0159
0160 id = sdio_match_device(func, drv);
0161 if (!id)
0162 return -ENODEV;
0163
0164 ret = dev_pm_domain_attach(dev, false);
0165 if (ret)
0166 return ret;
0167
0168 atomic_inc(&func->card->sdio_funcs_probed);
0169
0170
0171
0172
0173
0174
0175
0176 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD) {
0177 ret = pm_runtime_get_sync(dev);
0178 if (ret < 0)
0179 goto disable_runtimepm;
0180 }
0181
0182
0183
0184 sdio_claim_host(func);
0185 if (mmc_card_removed(func->card))
0186 ret = -ENOMEDIUM;
0187 else
0188 ret = sdio_set_block_size(func, 0);
0189 sdio_release_host(func);
0190 if (ret)
0191 goto disable_runtimepm;
0192
0193 ret = drv->probe(func, id);
0194 if (ret)
0195 goto disable_runtimepm;
0196
0197 return 0;
0198
0199 disable_runtimepm:
0200 atomic_dec(&func->card->sdio_funcs_probed);
0201 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
0202 pm_runtime_put_noidle(dev);
0203 dev_pm_domain_detach(dev, false);
0204 return ret;
0205 }
0206
0207 static void sdio_bus_remove(struct device *dev)
0208 {
0209 struct sdio_driver *drv = to_sdio_driver(dev->driver);
0210 struct sdio_func *func = dev_to_sdio_func(dev);
0211
0212
0213 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
0214 pm_runtime_get_sync(dev);
0215
0216 drv->remove(func);
0217 atomic_dec(&func->card->sdio_funcs_probed);
0218
0219 if (func->irq_handler) {
0220 pr_warn("WARNING: driver %s did not remove its interrupt handler!\n",
0221 drv->name);
0222 sdio_claim_host(func);
0223 sdio_release_irq(func);
0224 sdio_release_host(func);
0225 }
0226
0227
0228 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
0229 pm_runtime_put_noidle(dev);
0230
0231
0232 if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
0233 pm_runtime_put_sync(dev);
0234
0235 dev_pm_domain_detach(dev, false);
0236 }
0237
0238 static const struct dev_pm_ops sdio_bus_pm_ops = {
0239 SET_SYSTEM_SLEEP_PM_OPS(pm_generic_suspend, pm_generic_resume)
0240 SET_RUNTIME_PM_OPS(
0241 pm_generic_runtime_suspend,
0242 pm_generic_runtime_resume,
0243 NULL
0244 )
0245 };
0246
0247 static struct bus_type sdio_bus_type = {
0248 .name = "sdio",
0249 .dev_groups = sdio_dev_groups,
0250 .match = sdio_bus_match,
0251 .uevent = sdio_bus_uevent,
0252 .probe = sdio_bus_probe,
0253 .remove = sdio_bus_remove,
0254 .pm = &sdio_bus_pm_ops,
0255 };
0256
0257 int sdio_register_bus(void)
0258 {
0259 return bus_register(&sdio_bus_type);
0260 }
0261
0262 void sdio_unregister_bus(void)
0263 {
0264 bus_unregister(&sdio_bus_type);
0265 }
0266
0267
0268
0269
0270
0271 int sdio_register_driver(struct sdio_driver *drv)
0272 {
0273 drv->drv.name = drv->name;
0274 drv->drv.bus = &sdio_bus_type;
0275 return driver_register(&drv->drv);
0276 }
0277 EXPORT_SYMBOL_GPL(sdio_register_driver);
0278
0279
0280
0281
0282
0283 void sdio_unregister_driver(struct sdio_driver *drv)
0284 {
0285 drv->drv.bus = &sdio_bus_type;
0286 driver_unregister(&drv->drv);
0287 }
0288 EXPORT_SYMBOL_GPL(sdio_unregister_driver);
0289
0290 static void sdio_release_func(struct device *dev)
0291 {
0292 struct sdio_func *func = dev_to_sdio_func(dev);
0293
0294 sdio_free_func_cis(func);
0295
0296 kfree(func->info);
0297 kfree(func->tmpbuf);
0298 kfree(func);
0299 }
0300
0301
0302
0303
0304 struct sdio_func *sdio_alloc_func(struct mmc_card *card)
0305 {
0306 struct sdio_func *func;
0307
0308 func = kzalloc(sizeof(struct sdio_func), GFP_KERNEL);
0309 if (!func)
0310 return ERR_PTR(-ENOMEM);
0311
0312
0313
0314
0315
0316 func->tmpbuf = kmalloc(4, GFP_KERNEL);
0317 if (!func->tmpbuf) {
0318 kfree(func);
0319 return ERR_PTR(-ENOMEM);
0320 }
0321
0322 func->card = card;
0323
0324 device_initialize(&func->dev);
0325
0326 func->dev.parent = &card->dev;
0327 func->dev.bus = &sdio_bus_type;
0328 func->dev.release = sdio_release_func;
0329
0330 return func;
0331 }
0332
0333 #ifdef CONFIG_ACPI
0334 static void sdio_acpi_set_handle(struct sdio_func *func)
0335 {
0336 struct mmc_host *host = func->card->host;
0337 u64 addr = ((u64)host->slotno << 16) | func->num;
0338
0339 acpi_preset_companion(&func->dev, ACPI_COMPANION(host->parent), addr);
0340 }
0341 #else
0342 static inline void sdio_acpi_set_handle(struct sdio_func *func) {}
0343 #endif
0344
0345 static void sdio_set_of_node(struct sdio_func *func)
0346 {
0347 struct mmc_host *host = func->card->host;
0348
0349 func->dev.of_node = mmc_of_find_child_device(host, func->num);
0350 }
0351
0352
0353
0354
0355 int sdio_add_func(struct sdio_func *func)
0356 {
0357 int ret;
0358
0359 dev_set_name(&func->dev, "%s:%d", mmc_card_id(func->card), func->num);
0360
0361 sdio_set_of_node(func);
0362 sdio_acpi_set_handle(func);
0363 device_enable_async_suspend(&func->dev);
0364 ret = device_add(&func->dev);
0365 if (ret == 0)
0366 sdio_func_set_present(func);
0367
0368 return ret;
0369 }
0370
0371
0372
0373
0374
0375
0376
0377 void sdio_remove_func(struct sdio_func *func)
0378 {
0379 if (!sdio_func_present(func))
0380 return;
0381
0382 device_del(&func->dev);
0383 of_node_put(func->dev.of_node);
0384 put_device(&func->dev);
0385 }
0386