Commit 3ea7f59a authored by Armin Wolf's avatar Armin Wolf Committed by Hans de Goede

platform/x86: wmi: Decouple legacy WMI notify handlers from wmi_block_list

Until now, legacy WMI notify handler functions where using the
wmi_block_list, which did no refcounting on the returned WMI device.
This meant that the WMI device could disappear at any moment,
potentially leading to various errors.
Fix this by using bus_find_device() which returns an actual
reference to the found WMI device.

Tested on a Dell Inspiron 3505 and a Acer Aspire E1-731.
Signed-off-by: default avatarArmin Wolf <W_Armin@gmx.de>
Reviewed-by: default avatarIlpo Järvinen <ilpo.jarvinen@linux.intel.com>
Reviewed-by: default avatarHans de Goede <hdegoede@redhat.com>
Link: https://lore.kernel.org/r/20240103192707.115512-4-W_Armin@gmx.deSigned-off-by: default avatarHans de Goede <hdegoede@redhat.com>
parent 3d8a29fe
...@@ -219,6 +219,17 @@ static int wmidev_match_guid(struct device *dev, const void *data) ...@@ -219,6 +219,17 @@ static int wmidev_match_guid(struct device *dev, const void *data)
return 0; return 0;
} }
static int wmidev_match_notify_id(struct device *dev, const void *data)
{
struct wmi_block *wblock = dev_to_wblock(dev);
const u32 *notify_id = data;
if (wblock->gblock.flags & ACPI_WMI_EVENT && wblock->gblock.notify_id == *notify_id)
return 1;
return 0;
}
static struct bus_type wmi_bus_type; static struct bus_type wmi_bus_type;
static struct wmi_device *wmi_find_device_by_guid(const char *guid_string) static struct wmi_device *wmi_find_device_by_guid(const char *guid_string)
...@@ -238,6 +249,17 @@ static struct wmi_device *wmi_find_device_by_guid(const char *guid_string) ...@@ -238,6 +249,17 @@ static struct wmi_device *wmi_find_device_by_guid(const char *guid_string)
return dev_to_wdev(dev); return dev_to_wdev(dev);
} }
static struct wmi_device *wmi_find_event_by_notify_id(const u32 notify_id)
{
struct device *dev;
dev = bus_find_device(&wmi_bus_type, NULL, &notify_id, wmidev_match_notify_id);
if (!dev)
return ERR_PTR(-ENODEV);
return to_wmi_device(dev);
}
static void wmi_device_put(struct wmi_device *wdev) static void wmi_device_put(struct wmi_device *wdev)
{ {
put_device(&wdev->dev); put_device(&wdev->dev);
...@@ -572,34 +594,30 @@ acpi_status wmi_install_notify_handler(const char *guid, ...@@ -572,34 +594,30 @@ acpi_status wmi_install_notify_handler(const char *guid,
wmi_notify_handler handler, wmi_notify_handler handler,
void *data) void *data)
{ {
struct wmi_block *block; struct wmi_block *wblock;
guid_t guid_input; struct wmi_device *wdev;
acpi_status status;
if (!guid || !handler)
return AE_BAD_PARAMETER;
if (guid_parse(guid, &guid_input))
return AE_BAD_PARAMETER;
list_for_each_entry(block, &wmi_block_list, list) {
acpi_status wmi_status;
if (guid_equal(&block->gblock.guid, &guid_input)) { wdev = wmi_find_device_by_guid(guid);
if (block->handler) if (IS_ERR(wdev))
return AE_ALREADY_ACQUIRED; return AE_ERROR;
block->handler = handler; wblock = container_of(wdev, struct wmi_block, dev);
block->handler_data = data; if (wblock->handler) {
status = AE_ALREADY_ACQUIRED;
} else {
wblock->handler = handler;
wblock->handler_data = data;
wmi_status = wmi_method_enable(block, true); if (ACPI_FAILURE(wmi_method_enable(wblock, true)))
if (ACPI_FAILURE(wmi_status)) dev_warn(&wblock->dev.dev, "Failed to enable device\n");
dev_warn(&block->dev.dev, "Failed to enable device\n");
return AE_OK; status = AE_OK;
}
} }
return AE_NOT_EXIST; wmi_device_put(wdev);
return status;
} }
EXPORT_SYMBOL_GPL(wmi_install_notify_handler); EXPORT_SYMBOL_GPL(wmi_install_notify_handler);
...@@ -613,34 +631,30 @@ EXPORT_SYMBOL_GPL(wmi_install_notify_handler); ...@@ -613,34 +631,30 @@ EXPORT_SYMBOL_GPL(wmi_install_notify_handler);
*/ */
acpi_status wmi_remove_notify_handler(const char *guid) acpi_status wmi_remove_notify_handler(const char *guid)
{ {
struct wmi_block *block; struct wmi_block *wblock;
guid_t guid_input; struct wmi_device *wdev;
acpi_status status;
if (!guid)
return AE_BAD_PARAMETER;
if (guid_parse(guid, &guid_input))
return AE_BAD_PARAMETER;
list_for_each_entry(block, &wmi_block_list, list) {
acpi_status wmi_status;
if (guid_equal(&block->gblock.guid, &guid_input)) { wdev = wmi_find_device_by_guid(guid);
if (!block->handler) if (IS_ERR(wdev))
return AE_NULL_ENTRY; return AE_ERROR;
wmi_status = wmi_method_enable(block, false); wblock = container_of(wdev, struct wmi_block, dev);
if (ACPI_FAILURE(wmi_status)) if (!wblock->handler) {
dev_warn(&block->dev.dev, "Failed to disable device\n"); status = AE_NULL_ENTRY;
} else {
if (ACPI_FAILURE(wmi_method_enable(wblock, false)))
dev_warn(&wblock->dev.dev, "Failed to disable device\n");
block->handler = NULL; wblock->handler = NULL;
block->handler_data = NULL; wblock->handler_data = NULL;
return AE_OK; status = AE_OK;
}
} }
return AE_NOT_EXIST; wmi_device_put(wdev);
return status;
} }
EXPORT_SYMBOL_GPL(wmi_remove_notify_handler); EXPORT_SYMBOL_GPL(wmi_remove_notify_handler);
...@@ -657,15 +671,19 @@ EXPORT_SYMBOL_GPL(wmi_remove_notify_handler); ...@@ -657,15 +671,19 @@ EXPORT_SYMBOL_GPL(wmi_remove_notify_handler);
acpi_status wmi_get_event_data(u32 event, struct acpi_buffer *out) acpi_status wmi_get_event_data(u32 event, struct acpi_buffer *out)
{ {
struct wmi_block *wblock; struct wmi_block *wblock;
struct wmi_device *wdev;
acpi_status status;
list_for_each_entry(wblock, &wmi_block_list, list) { wdev = wmi_find_event_by_notify_id(event);
struct guid_block *gblock = &wblock->gblock; if (IS_ERR(wdev))
return AE_NOT_FOUND;
if ((gblock->flags & ACPI_WMI_EVENT) && gblock->notify_id == event) wblock = container_of(wdev, struct wmi_block, dev);
return get_event_data(wblock, out); status = get_event_data(wblock, out);
}
return AE_NOT_FOUND; wmi_device_put(wdev);
return status;
} }
EXPORT_SYMBOL_GPL(wmi_get_event_data); EXPORT_SYMBOL_GPL(wmi_get_event_data);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment