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>
16 #include <linux/export.h>
17 #include <linux/slab.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/pm_domain.h>
20 #include <linux/acpi.h>
22 #include <linux/mmc/card.h>
23 #include <linux/mmc/host.h>
24 #include <linux/mmc/sdio_func.h>
29 /* show configuration fields */
30 #define sdio_config_attr(field, format_string) \
32 field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
34 struct sdio_func *func; \
36 func = dev_to_sdio_func (dev); \
37 return sprintf (buf, format_string, func->field); \
39 static DEVICE_ATTR_RO(field)
41 sdio_config_attr(class, "0x%02x\n");
42 sdio_config_attr(vendor
, "0x%04x\n");
43 sdio_config_attr(device
, "0x%04x\n");
45 static ssize_t
modalias_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
47 struct sdio_func
*func
= dev_to_sdio_func (dev
);
49 return sprintf(buf
, "sdio:c%02Xv%04Xd%04X\n",
50 func
->class, func
->vendor
, func
->device
);
52 static DEVICE_ATTR_RO(modalias
);
54 static struct attribute
*sdio_dev_attrs
[] = {
56 &dev_attr_vendor
.attr
,
57 &dev_attr_device
.attr
,
58 &dev_attr_modalias
.attr
,
61 ATTRIBUTE_GROUPS(sdio_dev
);
63 static const struct sdio_device_id
*sdio_match_one(struct sdio_func
*func
,
64 const struct sdio_device_id
*id
)
66 if (id
->class != (__u8
)SDIO_ANY_ID
&& id
->class != func
->class)
68 if (id
->vendor
!= (__u16
)SDIO_ANY_ID
&& id
->vendor
!= func
->vendor
)
70 if (id
->device
!= (__u16
)SDIO_ANY_ID
&& id
->device
!= func
->device
)
75 static const struct sdio_device_id
*sdio_match_device(struct sdio_func
*func
,
76 struct sdio_driver
*sdrv
)
78 const struct sdio_device_id
*ids
;
83 while (ids
->class || ids
->vendor
|| ids
->device
) {
84 if (sdio_match_one(func
, ids
))
93 static int sdio_bus_match(struct device
*dev
, struct device_driver
*drv
)
95 struct sdio_func
*func
= dev_to_sdio_func(dev
);
96 struct sdio_driver
*sdrv
= to_sdio_driver(drv
);
98 if (sdio_match_device(func
, sdrv
))
105 sdio_bus_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
107 struct sdio_func
*func
= dev_to_sdio_func(dev
);
109 if (add_uevent_var(env
,
110 "SDIO_CLASS=%02X", func
->class))
113 if (add_uevent_var(env
,
114 "SDIO_ID=%04X:%04X", func
->vendor
, func
->device
))
117 if (add_uevent_var(env
,
118 "MODALIAS=sdio:c%02Xv%04Xd%04X",
119 func
->class, func
->vendor
, func
->device
))
125 static int sdio_bus_probe(struct device
*dev
)
127 struct sdio_driver
*drv
= to_sdio_driver(dev
->driver
);
128 struct sdio_func
*func
= dev_to_sdio_func(dev
);
129 const struct sdio_device_id
*id
;
132 id
= sdio_match_device(func
, drv
);
136 /* Unbound SDIO functions are always suspended.
137 * During probe, the function is set active and the usage count
138 * is incremented. If the driver supports runtime PM,
139 * it should call pm_runtime_put_noidle() in its probe routine and
140 * pm_runtime_get_noresume() in its remove routine.
142 if (func
->card
->host
->caps
& MMC_CAP_POWER_OFF_CARD
) {
143 ret
= pm_runtime_get_sync(dev
);
145 goto disable_runtimepm
;
148 /* Set the default block size so the driver is sure it's something
150 sdio_claim_host(func
);
151 ret
= sdio_set_block_size(func
, 0);
152 sdio_release_host(func
);
154 goto disable_runtimepm
;
156 ret
= drv
->probe(func
, id
);
158 goto disable_runtimepm
;
163 if (func
->card
->host
->caps
& MMC_CAP_POWER_OFF_CARD
)
164 pm_runtime_put_noidle(dev
);
168 static int sdio_bus_remove(struct device
*dev
)
170 struct sdio_driver
*drv
= to_sdio_driver(dev
->driver
);
171 struct sdio_func
*func
= dev_to_sdio_func(dev
);
174 /* Make sure card is powered before invoking ->remove() */
175 if (func
->card
->host
->caps
& MMC_CAP_POWER_OFF_CARD
)
176 pm_runtime_get_sync(dev
);
180 if (func
->irq_handler
) {
181 pr_warn("WARNING: driver %s did not remove its interrupt handler!\n",
183 sdio_claim_host(func
);
184 sdio_release_irq(func
);
185 sdio_release_host(func
);
188 /* First, undo the increment made directly above */
189 if (func
->card
->host
->caps
& MMC_CAP_POWER_OFF_CARD
)
190 pm_runtime_put_noidle(dev
);
192 /* Then undo the runtime PM settings in sdio_bus_probe() */
193 if (func
->card
->host
->caps
& MMC_CAP_POWER_OFF_CARD
)
194 pm_runtime_put_sync(dev
);
201 static const struct dev_pm_ops sdio_bus_pm_ops
= {
202 SET_SYSTEM_SLEEP_PM_OPS(pm_generic_suspend
, pm_generic_resume
)
204 pm_generic_runtime_suspend
,
205 pm_generic_runtime_resume
,
210 #define SDIO_PM_OPS_PTR (&sdio_bus_pm_ops)
212 #else /* !CONFIG_PM */
214 #define SDIO_PM_OPS_PTR NULL
216 #endif /* !CONFIG_PM */
218 static struct bus_type sdio_bus_type
= {
220 .dev_groups
= sdio_dev_groups
,
221 .match
= sdio_bus_match
,
222 .uevent
= sdio_bus_uevent
,
223 .probe
= sdio_bus_probe
,
224 .remove
= sdio_bus_remove
,
225 .pm
= SDIO_PM_OPS_PTR
,
228 int sdio_register_bus(void)
230 return bus_register(&sdio_bus_type
);
233 void sdio_unregister_bus(void)
235 bus_unregister(&sdio_bus_type
);
239 * sdio_register_driver - register a function driver
240 * @drv: SDIO function driver
242 int sdio_register_driver(struct sdio_driver
*drv
)
244 drv
->drv
.name
= drv
->name
;
245 drv
->drv
.bus
= &sdio_bus_type
;
246 return driver_register(&drv
->drv
);
248 EXPORT_SYMBOL_GPL(sdio_register_driver
);
251 * sdio_unregister_driver - unregister a function driver
252 * @drv: SDIO function driver
254 void sdio_unregister_driver(struct sdio_driver
*drv
)
256 drv
->drv
.bus
= &sdio_bus_type
;
257 driver_unregister(&drv
->drv
);
259 EXPORT_SYMBOL_GPL(sdio_unregister_driver
);
261 static void sdio_release_func(struct device
*dev
)
263 struct sdio_func
*func
= dev_to_sdio_func(dev
);
265 sdio_free_func_cis(func
);
273 * Allocate and initialise a new SDIO function structure.
275 struct sdio_func
*sdio_alloc_func(struct mmc_card
*card
)
277 struct sdio_func
*func
;
279 func
= kzalloc(sizeof(struct sdio_func
), GFP_KERNEL
);
281 return ERR_PTR(-ENOMEM
);
285 device_initialize(&func
->dev
);
287 func
->dev
.parent
= &card
->dev
;
288 func
->dev
.bus
= &sdio_bus_type
;
289 func
->dev
.release
= sdio_release_func
;
295 static void sdio_acpi_set_handle(struct sdio_func
*func
)
297 struct mmc_host
*host
= func
->card
->host
;
298 u64 addr
= (host
->slotno
<< 16) | func
->num
;
300 acpi_preset_companion(&func
->dev
, ACPI_COMPANION(host
->parent
), addr
);
303 static inline void sdio_acpi_set_handle(struct sdio_func
*func
) {}
307 * Register a new SDIO function with the driver model.
309 int sdio_add_func(struct sdio_func
*func
)
313 dev_set_name(&func
->dev
, "%s:%d", mmc_card_id(func
->card
), func
->num
);
315 sdio_acpi_set_handle(func
);
316 ret
= device_add(&func
->dev
);
318 sdio_func_set_present(func
);
319 dev_pm_domain_attach(&func
->dev
, false);
326 * Unregister a SDIO function with the driver model, and
327 * (eventually) free it.
328 * This function can be called through error paths where sdio_add_func() was
329 * never executed (because a failure occurred at an earlier point).
331 void sdio_remove_func(struct sdio_func
*func
)
333 if (!sdio_func_present(func
))
336 dev_pm_domain_detach(&func
->dev
, false);
337 device_del(&func
->dev
);
338 put_device(&func
->dev
);