Skip to contentSkip to conversation

platform/x86: classmate-laptop: Bind to platform devices instead of ACPI ones

PATCH In review Rafael J. Wysocki 3 participants 13 messages May 11 – May 19

Subsystems (3)
lore
Share & tools
lore

Rafael J. Wysocki

author maintainer
#0 lore raw

v2 cover platform/x86: classmate-laptop: Bind to platform devices instead of ACPI ones

Hi All,

This is an update of

https://lore.kernel.org/linux-acpi/[email protected]/

sent mostly because I have realized that using devm_kzalloc() in the first patch simplifies things going forward, but also it adds ACPI companion checks against NULL at probe time that were missing previously.

This series is part of a larger effort to switch over all drivers using the struct acpi_driver interface to the more common struct platform_driver interface and eliminate the former. The background is explained in Documentation/driver-api/acpi/acpi-drivers.rst and in the changelog of the patch that introduced the above document:

https://lore.kernel.org/all/[email protected]/

The bottom line is that the kernel would be better off without struct acpi_driver and so it is better to get rid of it.

This series carries out driver conversion of 5 subdrivers in the platform x86 classmate-laptop driver.

The first 5 patches in the series are preliminary.

Patch [01/10] converts two subdrivers in classmate-laptop to using devm_kzalloc() for allocating memory which helps to avoid memory leaks during driver removal.

Patch [02/10] modifies 2 subdrivers in classmate-laptop so that their probe rollback code ordering is the same as the removal one.

Patches [03-04/10] change the type of the first argument in 2 helper functions in classmate-laptop and rename them.

Patch [05/10] updates classmate-laptop subdrivers to install their own ACPI notify handlers instead of using the .notify() callback in struct acpi_driver.

The remaining patches convert the classmate-laptop subdrivers to platform drivers, one at a time.

Patch [06/10] converts the accel-v4 subdriver in classmate-laptop to using struct platform_driver for device binding.

Patch [07/10] converts the accel subdriver in classmate-laptop to using struct platform_driver for device binding.

Patch [08/10] converts the tablet subdriver in classmate-laptop to using struct platform_driver for device binding.

Patch [09/10] converts the ipml subdriver in classmate-laptop to using struct platform_driver for device binding.

Patch [10/10] converts the keys subdriver in classmate-laptop to using struct platform_driver for device binding.

Thanks!

author maintainer
#1 lore raw

PATCH v2 1/10 platform/x86: classmate-laptop: Address memory leaks on driver removal

From: "Rafael J. Wysocki" <[email protected]>

Switch over cmpc_accel_add_v4() and cmpc_accel_add() to using devm_kzalloc() for allocating the accel object which will cause it to be freed automatically on device removal, so it won't be leaked any more.

This also simplifies the rollback paths in these functions somewhat.

Fixes 529aa8cb0a59 ("classmate-laptop: add support for Classmate PC ACPI devices")

---

v1 -> v2:
* Use devm_kzalloc() for allocating memory at probe time instead of adding kfree() calls to remove callbacks.

---
 drivers/platform/x86/classmate-laptop.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
drivers/platform/x86/classmate-laptop.c +4 -8
index e6eed3d65580..a5fe34211afb 100644
@@ -400,7 +400,7 @@ static int cmpc_accel_add_v4(struct acpi_device *acpi)
 	struct input_dev *inputdev;
 	struct cmpc_accel *accel;
 
-	accel = kmalloc_obj(*accel);
+	accel = devm_kzalloc(&acpi->dev, sizeof(*accel), GFP_KERNEL);
 	if (!accel)
 		return -ENOMEM;
 
@@ -411,7 +411,7 @@ static int cmpc_accel_add_v4(struct acpi_device *acpi)
 
 	error = device_create_file(&acpi->dev, &cmpc_accel_sensitivity_attr_v4);
 	if (error)
-		goto failed_sensitivity;
+		return error;
 
 	accel->g_select = CMPC_ACCEL_G_SELECT_DEFAULT;
 	cmpc_accel_set_g_select_v4(acpi->handle, accel->g_select);
@@ -434,8 +434,6 @@ static int cmpc_accel_add_v4(struct acpi_device *acpi)
 	device_remove_file(&acpi->dev, &cmpc_accel_g_select_attr_v4);
 failed_g_select:
 	device_remove_file(&acpi->dev, &cmpc_accel_sensitivity_attr_v4);
-failed_sensitivity:
-	kfree(accel);
 	return error;
 }
 
@@ -650,7 +648,7 @@ static int cmpc_accel_add(struct acpi_device *acpi)
 	struct input_dev *inputdev;
 	struct cmpc_accel *accel;
 
-	accel = kmalloc_obj(*accel);
+	accel = devm_kzalloc(&acpi->dev, sizeof(*accel), GFP_KERNEL);
 	if (!accel)
 		return -ENOMEM;
 
@@ -659,7 +657,7 @@ static int cmpc_accel_add(struct acpi_device *acpi)
 
 	error = device_create_file(&acpi->dev, &cmpc_accel_sensitivity_attr);
 	if (error)
-		goto failed_file;
+		return error;
 
 	error = cmpc_add_acpi_notify_device(acpi, "cmpc_accel",
 					    cmpc_accel_idev_init);
@@ -673,8 +671,6 @@ static int cmpc_accel_add(struct acpi_device *acpi)
 
 failed_input:
 	device_remove_file(&acpi->dev, &cmpc_accel_sensitivity_attr);
-failed_file:
-	kfree(accel);
 	return error;
 }
 
author maintainer
#2 lore raw

PATCH v2 2/10 platform/x86: classmate-laptop: Unify probe rollback and remove code

In reply to Rafael J. Wysocki

From: "Rafael J. Wysocki" <[email protected]>

To facilitate subsequent modifications, change code ordering in cmpc_accel_add_v4(), cmpc_accel_add(), and cmpc_accel_remove_v4() so that the ordering of the probe rollback code is the same as the ordering of the corresponding removal code.

No intentional functional impact.

---

v1 -> v2: Rebase

---
 drivers/platform/x86/classmate-laptop.c | 40 +++++++++++++------------
 1 file changed, 21 insertions(+), 19 deletions(-)
drivers/platform/x86/classmate-laptop.c +21 -19
index a5fe34211afb..d1407a28c1de 100644
@@ -406,12 +406,19 @@ static int cmpc_accel_add_v4(struct acpi_device *acpi)
 
 	accel->inputdev_state = CMPC_ACCEL_DEV_STATE_CLOSED;
 
+	error = cmpc_add_acpi_notify_device(acpi, "cmpc_accel_v4",
+					    cmpc_accel_idev_init_v4);
+	if (error)
+		return error;
+
+	inputdev = dev_get_drvdata(&acpi->dev);
+
 	accel->sensitivity = CMPC_ACCEL_SENSITIVITY_DEFAULT;
 	cmpc_accel_set_sensitivity_v4(acpi->handle, accel->sensitivity);
 
 	error = device_create_file(&acpi->dev, &cmpc_accel_sensitivity_attr_v4);
 	if (error)
-		return error;
+		goto failed_sensitivity;
 
 	accel->g_select = CMPC_ACCEL_G_SELECT_DEFAULT;
 	cmpc_accel_set_g_select_v4(acpi->handle, accel->g_select);
@@ -420,27 +427,21 @@ static int cmpc_accel_add_v4(struct acpi_device *acpi)
 	if (error)
 		goto failed_g_select;
 
-	error = cmpc_add_acpi_notify_device(acpi, "cmpc_accel_v4",
-					    cmpc_accel_idev_init_v4);
-	if (error)
-		goto failed_input;
-
-	inputdev = dev_get_drvdata(&acpi->dev);
 	dev_set_drvdata(&inputdev->dev, accel);
 
 	return 0;
 
-failed_input:
-	device_remove_file(&acpi->dev, &cmpc_accel_g_select_attr_v4);
 failed_g_select:
 	device_remove_file(&acpi->dev, &cmpc_accel_sensitivity_attr_v4);
+failed_sensitivity:
+	cmpc_remove_acpi_notify_device(acpi);
 	return error;
 }
 
 static void cmpc_accel_remove_v4(struct acpi_device *acpi)
 {
-	device_remove_file(&acpi->dev, &cmpc_accel_sensitivity_attr_v4);
 	device_remove_file(&acpi->dev, &cmpc_accel_g_select_attr_v4);
+	device_remove_file(&acpi->dev, &cmpc_accel_sensitivity_attr_v4);
 	cmpc_remove_acpi_notify_device(acpi);
 }
 
@@ -652,25 +653,26 @@ static int cmpc_accel_add(struct acpi_device *acpi)
 	if (!accel)
 		return -ENOMEM;
 
+	error = cmpc_add_acpi_notify_device(acpi, "cmpc_accel",
+					    cmpc_accel_idev_init);
+	if (error)
+		return error;
+
+	inputdev = dev_get_drvdata(&acpi->dev);
+
 	accel->sensitivity = CMPC_ACCEL_SENSITIVITY_DEFAULT;
 	cmpc_accel_set_sensitivity(acpi->handle, accel->sensitivity);
 
 	error = device_create_file(&acpi->dev, &cmpc_accel_sensitivity_attr);
 	if (error)
-		return error;
-
-	error = cmpc_add_acpi_notify_device(acpi, "cmpc_accel",
-					    cmpc_accel_idev_init);
-	if (error)
-		goto failed_input;
+		goto failed_file;
 
-	inputdev = dev_get_drvdata(&acpi->dev);
 	dev_set_drvdata(&inputdev->dev, accel);
 
 	return 0;
 
-failed_input:
-	device_remove_file(&acpi->dev, &cmpc_accel_sensitivity_attr);
+failed_file:
+	cmpc_remove_acpi_notify_device(acpi);
 	return error;
 }
 
Signature · 1 line
2.51.0
author maintainer
#3 lore raw

PATCH v2 3/10 platform/x86: classmate-laptop: Pass struct device pointer to helpers

In reply to Rafael J. Wysocki

From: "Rafael J. Wysocki" <[email protected]>

To facilitate subsequent conversion of the driver to using struct platform_driver instead of struct acpi_driver, modify two helper functions in it, cmpc_add_acpi_notify_device() and cmpc_remove_acpi_notify_device(), to take a struct device pointer argument instead of a struct acpi_device pointer argument and update their callers accordingly.

While at it, change the return type of cmpc_remove_acpi_notify_device() to void because its return value is never checked.

No intentional functional impact.

---

v1 -> v2: Rebase

---
 drivers/platform/x86/classmate-laptop.c | 32 ++++++++++++-------------
 1 file changed, 15 insertions(+), 17 deletions(-)
drivers/platform/x86/classmate-laptop.c +15 -17
index d1407a28c1de..6521d42f8204 100644
@@ -38,7 +38,7 @@ struct cmpc_accel {
 
 typedef void (*input_device_init)(struct input_dev *dev);
 
-static int cmpc_add_acpi_notify_device(struct acpi_device *acpi, char *name,
+static int cmpc_add_acpi_notify_device(struct device *dev, char *name,
 				       input_device_init idev_init)
 {
 	struct input_dev *inputdev;
@@ -48,22 +48,20 @@ static int cmpc_add_acpi_notify_device(struct acpi_device *acpi, char *name,
 	if (!inputdev)
 		return -ENOMEM;
 	inputdev->name = name;
-	inputdev->dev.parent = &acpi->dev;
+	inputdev->dev.parent = dev;
 	idev_init(inputdev);
 	error = input_register_device(inputdev);
 	if (error) {
 		input_free_device(inputdev);
 		return error;
 	}
-	dev_set_drvdata(&acpi->dev, inputdev);
+	dev_set_drvdata(dev, inputdev);
 	return 0;
 }
 
-static int cmpc_remove_acpi_notify_device(struct acpi_device *acpi)
+static void cmpc_remove_acpi_notify_device(struct device *dev)
 {
-	struct input_dev *inputdev = dev_get_drvdata(&acpi->dev);
-	input_unregister_device(inputdev);
-	return 0;
+	input_unregister_device(dev_get_drvdata(dev));
 }
 
 /*
@@ -406,7 +404,7 @@ static int cmpc_accel_add_v4(struct acpi_device *acpi)
 
 	accel->inputdev_state = CMPC_ACCEL_DEV_STATE_CLOSED;
 
-	error = cmpc_add_acpi_notify_device(acpi, "cmpc_accel_v4",
+	error = cmpc_add_acpi_notify_device(&acpi->dev, "cmpc_accel_v4",
 					    cmpc_accel_idev_init_v4);
 	if (error)
 		return error;
@@ -434,7 +432,7 @@ static int cmpc_accel_add_v4(struct acpi_device *acpi)
 failed_g_select:
 	device_remove_file(&acpi->dev, &cmpc_accel_sensitivity_attr_v4);
 failed_sensitivity:
-	cmpc_remove_acpi_notify_device(acpi);
+	cmpc_remove_acpi_notify_device(&acpi->dev);
 	return error;
 }
 
@@ -442,7 +440,7 @@ static void cmpc_accel_remove_v4(struct acpi_device *acpi)
 {
 	device_remove_file(&acpi->dev, &cmpc_accel_g_select_attr_v4);
 	device_remove_file(&acpi->dev, &cmpc_accel_sensitivity_attr_v4);
-	cmpc_remove_acpi_notify_device(acpi);
+	cmpc_remove_acpi_notify_device(&acpi->dev);
 }
 
 static SIMPLE_DEV_PM_OPS(cmpc_accel_pm, cmpc_accel_suspend_v4,
@@ -653,7 +651,7 @@ static int cmpc_accel_add(struct acpi_device *acpi)
 	if (!accel)
 		return -ENOMEM;
 
-	error = cmpc_add_acpi_notify_device(acpi, "cmpc_accel",
+	error = cmpc_add_acpi_notify_device(&acpi->dev, "cmpc_accel",
 					    cmpc_accel_idev_init);
 	if (error)
 		return error;
@@ -672,14 +670,14 @@ static int cmpc_accel_add(struct acpi_device *acpi)
 	return 0;
 
 failed_file:
-	cmpc_remove_acpi_notify_device(acpi);
+	cmpc_remove_acpi_notify_device(&acpi->dev);
 	return error;
 }
 
 static void cmpc_accel_remove(struct acpi_device *acpi)
 {
 	device_remove_file(&acpi->dev, &cmpc_accel_sensitivity_attr);
-	cmpc_remove_acpi_notify_device(acpi);
+	cmpc_remove_acpi_notify_device(&acpi->dev);
 }
 
 static const struct acpi_device_id cmpc_accel_device_ids[] = {
@@ -750,13 +748,13 @@ static void cmpc_tablet_idev_init(struct input_dev *inputdev)
 
 static int cmpc_tablet_add(struct acpi_device *acpi)
 {
-	return cmpc_add_acpi_notify_device(acpi, "cmpc_tablet",
+	return cmpc_add_acpi_notify_device(&acpi->dev, "cmpc_tablet",
 					   cmpc_tablet_idev_init);
 }
 
 static void cmpc_tablet_remove(struct acpi_device *acpi)
 {
-	cmpc_remove_acpi_notify_device(acpi);
+	cmpc_remove_acpi_notify_device(&acpi->dev);
 }
 
 #ifdef CONFIG_PM_SLEEP
@@ -1074,13 +1072,13 @@ static void cmpc_keys_idev_init(struct input_dev *inputdev)
 
 static int cmpc_keys_add(struct acpi_device *acpi)
 {
-	return cmpc_add_acpi_notify_device(acpi, "cmpc_keys",
+	return cmpc_add_acpi_notify_device(&acpi->dev, "cmpc_keys",
 					   cmpc_keys_idev_init);
 }
 
 static void cmpc_keys_remove(struct acpi_device *acpi)
 {
-	cmpc_remove_acpi_notify_device(acpi);
+	cmpc_remove_acpi_notify_device(&acpi->dev);
 }
 
 static const struct acpi_device_id cmpc_keys_device_ids[] = {
Signature · 1 line
2.51.0
author maintainer
#4 lore raw

PATCH v2 4/10 platform/x86: classmate-laptop: Rename two helper functions

In reply to Rafael J. Wysocki

From: "Rafael J. Wysocki" <[email protected]>

Since cmpc_add_acpi_notify_device() and cmpc_remove_acpi_notify_device() have been modified to take a plain struct device pointer as the first argument, then have nothing to do with ACPI in principle, so rename them to cmpc_add_notify_device() and cmpc_remove_notify_device(), respectively, and consolidate white space around the call sites of the former.

No intentional functional impact.

---

v1 -> v2: Rebase

---
 drivers/platform/x86/classmate-laptop.c | 30 +++++++++++--------------
 1 file changed, 13 insertions(+), 17 deletions(-)
drivers/platform/x86/classmate-laptop.c +13 -17
index 6521d42f8204..bc229771e7ba 100644
@@ -38,8 +38,8 @@ struct cmpc_accel {
 
 typedef void (*input_device_init)(struct input_dev *dev);
 
-static int cmpc_add_acpi_notify_device(struct device *dev, char *name,
-				       input_device_init idev_init)
+static int cmpc_add_notify_device(struct device *dev, char *name,
+				  input_device_init idev_init)
 {
 	struct input_dev *inputdev;
 	int error;
@@ -59,7 +59,7 @@ static int cmpc_add_acpi_notify_device(struct device *dev, char *name,
 	return 0;
 }
 
-static void cmpc_remove_acpi_notify_device(struct device *dev)
+static void cmpc_remove_notify_device(struct device *dev)
 {
 	input_unregister_device(dev_get_drvdata(dev));
 }
@@ -404,8 +404,7 @@ static int cmpc_accel_add_v4(struct acpi_device *acpi)
 
 	accel->inputdev_state = CMPC_ACCEL_DEV_STATE_CLOSED;
 
-	error = cmpc_add_acpi_notify_device(&acpi->dev, "cmpc_accel_v4",
-					    cmpc_accel_idev_init_v4);
+	error = cmpc_add_notify_device(&acpi->dev, "cmpc_accel_v4", cmpc_accel_idev_init_v4);
 	if (error)
 		return error;
 
@@ -432,7 +431,7 @@ static int cmpc_accel_add_v4(struct acpi_device *acpi)
 failed_g_select:
 	device_remove_file(&acpi->dev, &cmpc_accel_sensitivity_attr_v4);
 failed_sensitivity:
-	cmpc_remove_acpi_notify_device(&acpi->dev);
+	cmpc_remove_notify_device(&acpi->dev);
 	return error;
 }
 
@@ -440,7 +439,7 @@ static void cmpc_accel_remove_v4(struct acpi_device *acpi)
 {
 	device_remove_file(&acpi->dev, &cmpc_accel_g_select_attr_v4);
 	device_remove_file(&acpi->dev, &cmpc_accel_sensitivity_attr_v4);
-	cmpc_remove_acpi_notify_device(&acpi->dev);
+	cmpc_remove_notify_device(&acpi->dev);
 }
 
 static SIMPLE_DEV_PM_OPS(cmpc_accel_pm, cmpc_accel_suspend_v4,
@@ -651,8 +650,7 @@ static int cmpc_accel_add(struct acpi_device *acpi)
 	if (!accel)
 		return -ENOMEM;
 
-	error = cmpc_add_acpi_notify_device(&acpi->dev, "cmpc_accel",
-					    cmpc_accel_idev_init);
+	error = cmpc_add_notify_device(&acpi->dev, "cmpc_accel", cmpc_accel_idev_init);
 	if (error)
 		return error;
 
@@ -670,14 +668,14 @@ static int cmpc_accel_add(struct acpi_device *acpi)
 	return 0;
 
 failed_file:
-	cmpc_remove_acpi_notify_device(&acpi->dev);
+	cmpc_remove_notify_device(&acpi->dev);
 	return error;
 }
 
 static void cmpc_accel_remove(struct acpi_device *acpi)
 {
 	device_remove_file(&acpi->dev, &cmpc_accel_sensitivity_attr);
-	cmpc_remove_acpi_notify_device(&acpi->dev);
+	cmpc_remove_notify_device(&acpi->dev);
 }
 
 static const struct acpi_device_id cmpc_accel_device_ids[] = {
@@ -748,13 +746,12 @@ static void cmpc_tablet_idev_init(struct input_dev *inputdev)
 
 static int cmpc_tablet_add(struct acpi_device *acpi)
 {
-	return cmpc_add_acpi_notify_device(&acpi->dev, "cmpc_tablet",
-					   cmpc_tablet_idev_init);
+	return cmpc_add_notify_device(&acpi->dev, "cmpc_tablet", cmpc_tablet_idev_init);
 }
 
 static void cmpc_tablet_remove(struct acpi_device *acpi)
 {
-	cmpc_remove_acpi_notify_device(&acpi->dev);
+	cmpc_remove_notify_device(&acpi->dev);
 }
 
 #ifdef CONFIG_PM_SLEEP
@@ -1072,13 +1069,12 @@ static void cmpc_keys_idev_init(struct input_dev *inputdev)
 
 static int cmpc_keys_add(struct acpi_device *acpi)
 {
-	return cmpc_add_acpi_notify_device(&acpi->dev, "cmpc_keys",
-					   cmpc_keys_idev_init);
+	return cmpc_add_notify_device(&acpi->dev, "cmpc_keys", cmpc_keys_idev_init);
 }
 
 static void cmpc_keys_remove(struct acpi_device *acpi)
 {
-	cmpc_remove_acpi_notify_device(&acpi->dev);
+	cmpc_remove_notify_device(&acpi->dev);
 }
 
 static const struct acpi_device_id cmpc_keys_device_ids[] = {
Signature · 1 line
2.51.0
author maintainer
#5 lore raw

PATCH v2 5/10 platform/x86: classmate-laptop: Register ACPI notify handlers directly

In reply to Rafael J. Wysocki

From: "Rafael J. Wysocki" <[email protected]>

To facilitate subsequent conversion of the driver to using struct platform_driver instead of struct acpi_driver, make it install its ACPI notify handlers directly instead of using struct acpi_driver .notify() callbacks.

No intentional functional impact.

---

v1 -> v2: Rebase

---
 drivers/platform/x86/classmate-laptop.c | 66 +++++++++++++++++++++----
 1 file changed, 56 insertions(+), 10 deletions(-)
drivers/platform/x86/classmate-laptop.c +56 -10
index bc229771e7ba..8507483b23f9 100644
@@ -177,8 +177,10 @@ static acpi_status cmpc_get_accel_v4(acpi_handle handle,
 	return status;
 }
 
-static void cmpc_accel_handler_v4(struct acpi_device *dev, u32 event)
+static void cmpc_accel_handler_v4(acpi_handle handle, u32 event, void *data)
 {
+	struct acpi_device *dev = data;
+
 	if (event == 0x81) {
 		int16_t x, y, z;
 		acpi_status status;
@@ -424,10 +426,17 @@ static int cmpc_accel_add_v4(struct acpi_device *acpi)
 	if (error)
 		goto failed_g_select;
 
+	error = acpi_dev_install_notify_handler(acpi, ACPI_DEVICE_NOTIFY,
+						cmpc_accel_handler_v4, acpi);
+	if (error)
+		goto failed_notify_handler;
+
 	dev_set_drvdata(&inputdev->dev, accel);
 
 	return 0;
 
+failed_notify_handler:
+	device_remove_file(&acpi->dev, &cmpc_accel_g_select_attr_v4);
 failed_g_select:
 	device_remove_file(&acpi->dev, &cmpc_accel_sensitivity_attr_v4);
 failed_sensitivity:
@@ -437,6 +446,8 @@ static int cmpc_accel_add_v4(struct acpi_device *acpi)
 
 static void cmpc_accel_remove_v4(struct acpi_device *acpi)
 {
+	acpi_dev_remove_notify_handler(acpi, ACPI_DEVICE_NOTIFY,
+				       cmpc_accel_handler_v4);
 	device_remove_file(&acpi->dev, &cmpc_accel_g_select_attr_v4);
 	device_remove_file(&acpi->dev, &cmpc_accel_sensitivity_attr_v4);
 	cmpc_remove_notify_device(&acpi->dev);
@@ -457,7 +468,6 @@ static struct acpi_driver cmpc_accel_acpi_driver_v4 = {
 	.ops = {
 		.add = cmpc_accel_add_v4,
 		.remove = cmpc_accel_remove_v4,
-		.notify = cmpc_accel_handler_v4,
 	},
 	.drv.pm = &cmpc_accel_pm,
 };
@@ -539,8 +549,10 @@ static acpi_status cmpc_get_accel(acpi_handle handle,
 	return status;
 }
 
-static void cmpc_accel_handler(struct acpi_device *dev, u32 event)
+static void cmpc_accel_handler(acpi_handle handle, u32 event, void *data)
 {
+	struct acpi_device *dev = data;
+
 	if (event == 0x81) {
 		unsigned char x, y, z;
 		acpi_status status;
@@ -663,10 +675,17 @@ static int cmpc_accel_add(struct acpi_device *acpi)
 	if (error)
 		goto failed_file;
 
+	error = acpi_dev_install_notify_handler(acpi, ACPI_DEVICE_NOTIFY,
+						cmpc_accel_handler, acpi);
+	if (error)
+		goto failed_notify_handler;
+
 	dev_set_drvdata(&inputdev->dev, accel);
 
 	return 0;
 
+failed_notify_handler:
+	device_remove_file(&acpi->dev, &cmpc_accel_sensitivity_attr);
 failed_file:
 	cmpc_remove_notify_device(&acpi->dev);
 	return error;
@@ -674,6 +693,8 @@ static int cmpc_accel_add(struct acpi_device *acpi)
 
 static void cmpc_accel_remove(struct acpi_device *acpi)
 {
+	acpi_dev_remove_notify_handler(acpi, ACPI_DEVICE_NOTIFY,
+				       cmpc_accel_handler);
 	device_remove_file(&acpi->dev, &cmpc_accel_sensitivity_attr);
 	cmpc_remove_notify_device(&acpi->dev);
 }
@@ -690,7 +711,6 @@ static struct acpi_driver cmpc_accel_acpi_driver = {
 	.ops = {
 		.add = cmpc_accel_add,
 		.remove = cmpc_accel_remove,
-		.notify = cmpc_accel_handler,
 	}
 };
 
@@ -716,8 +736,9 @@ static acpi_status cmpc_get_tablet(acpi_handle handle,
 	return status;
 }
 
-static void cmpc_tablet_handler(struct acpi_device *dev, u32 event)
+static void cmpc_tablet_handler(acpi_handle handle, u32 event, void *data)
 {
+	struct acpi_device *dev = data;
 	unsigned long long val = 0;
 	struct input_dev *inputdev = dev_get_drvdata(&dev->dev);
 
@@ -746,11 +767,24 @@ static void cmpc_tablet_idev_init(struct input_dev *inputdev)
 
 static int cmpc_tablet_add(struct acpi_device *acpi)
 {
-	return cmpc_add_notify_device(&acpi->dev, "cmpc_tablet", cmpc_tablet_idev_init);
+	int error;
+
+	error = cmpc_add_notify_device(&acpi->dev, "cmpc_tablet", cmpc_tablet_idev_init);
+	if (error)
+		return error;
+
+	error = acpi_dev_install_notify_handler(acpi, ACPI_DEVICE_NOTIFY,
+						cmpc_tablet_handler, acpi);
+	if (error)
+		cmpc_remove_notify_device(&acpi->dev);
+
+	return error;
 }
 
 static void cmpc_tablet_remove(struct acpi_device *acpi)
 {
+	acpi_dev_remove_notify_handler(acpi, ACPI_DEVICE_NOTIFY,
+				       cmpc_tablet_handler);
 	cmpc_remove_notify_device(&acpi->dev);
 }
 
@@ -782,7 +816,6 @@ static struct acpi_driver cmpc_tablet_acpi_driver = {
 	.ops = {
 		.add = cmpc_tablet_add,
 		.remove = cmpc_tablet_remove,
-		.notify = cmpc_tablet_handler,
 	},
 	.drv.pm = &cmpc_tablet_pm,
 };
@@ -1046,8 +1079,9 @@ static int cmpc_keys_codes[] = {
 	KEY_MAX
 };
 
-static void cmpc_keys_handler(struct acpi_device *dev, u32 event)
+static void cmpc_keys_handler(acpi_handle handle, u32 event, void *data)
 {
+	struct acpi_device *dev = data;
 	struct input_dev *inputdev;
 	int code = KEY_MAX;
 
@@ -1069,11 +1103,24 @@ static void cmpc_keys_idev_init(struct input_dev *inputdev)
 
 static int cmpc_keys_add(struct acpi_device *acpi)
 {
-	return cmpc_add_notify_device(&acpi->dev, "cmpc_keys", cmpc_keys_idev_init);
+	int error;
+
+	error = cmpc_add_notify_device(&acpi->dev, "cmpc_keys", cmpc_keys_idev_init);
+	if (error)
+		return error;
+
+	error = acpi_dev_install_notify_handler(acpi, ACPI_DEVICE_NOTIFY,
+						cmpc_keys_handler, acpi);
+	if (error)
+		cmpc_remove_notify_device(&acpi->dev);
+
+	return error;
 }
 
 static void cmpc_keys_remove(struct acpi_device *acpi)
 {
+	acpi_dev_remove_notify_handler(acpi, ACPI_DEVICE_NOTIFY,
+				       cmpc_keys_handler);
 	cmpc_remove_notify_device(&acpi->dev);
 }
 
@@ -1089,7 +1136,6 @@ static struct acpi_driver cmpc_keys_acpi_driver = {
 	.ops = {
 		.add = cmpc_keys_add,
 		.remove = cmpc_keys_remove,
-		.notify = cmpc_keys_handler,
 	}
 };
 
Signature · 1 line
2.51.0
author maintainer
#6 lore raw

PATCH v2 6/10 platform/x86: classmate-laptop: Convert v4 accel driver to a platform one

In reply to Rafael J. Wysocki

From: "Rafael J. Wysocki" <[email protected]>

In all cases in which a struct acpi_driver is used for binding a driver to an ACPI device object, a corresponding platform device is created by the ACPI core and that device is regarded as a proper representation of underlying hardware. Accordingly, a struct platform_driver should be used by driver code to bind to that device. There are multiple reasons why drivers should not bind directly to ACPI device objects [1].

Overall, it is better to bind drivers to platform devices than to their ACPI companions, so convert cmpc_accel_acpi_driver_v4 in the Classmate laptop driver from an ACPI driver to a platform one.

After this change, the input device registered by the driver will appear under the platform device used for driver binding, but the sysfs attributes added by the driver under the ACPI companion of that device will stay there in case there are utilities in user space expecting them to be present there.

While this is not expected to alter functionality, it changes sysfs layout and so it will be visible to user space.

---

v1 -> v2:
* Rebase
* Check the ACPI companion against NULL at probe time

---
 drivers/platform/x86/classmate-laptop.c | 77 ++++++++++++++-----------
 1 file changed, 42 insertions(+), 35 deletions(-)
drivers/platform/x86/classmate-laptop.c +42 -35
index 8507483b23f9..454ba2823b51 100644
@@ -13,6 +13,7 @@
 #include <linux/input.h>
 #include <linux/rfkill.h>
 #include <linux/sysfs.h>
+#include <linux/platform_device.h>
 
 struct cmpc_accel {
 	int sensitivity;
@@ -179,15 +180,15 @@ static acpi_status cmpc_get_accel_v4(acpi_handle handle,
 
 static void cmpc_accel_handler_v4(acpi_handle handle, u32 event, void *data)
 {
-	struct acpi_device *dev = data;
+	struct device *dev = data;
 
 	if (event == 0x81) {
 		int16_t x, y, z;
 		acpi_status status;
 
-		status = cmpc_get_accel_v4(dev->handle, &x, &y, &z);
+		status = cmpc_get_accel_v4(ACPI_HANDLE(dev), &x, &y, &z);
 		if (ACPI_SUCCESS(status)) {
-			struct input_dev *inputdev = dev_get_drvdata(&dev->dev);
+			struct input_dev *inputdev = dev_get_drvdata(dev);
 
 			input_report_abs(inputdev, ABS_X, x);
 			input_report_abs(inputdev, ABS_Y, y);
@@ -317,18 +318,17 @@ static struct device_attribute cmpc_accel_g_select_attr_v4 = {
 
 static int cmpc_accel_open_v4(struct input_dev *input)
 {
-	struct acpi_device *acpi;
+	acpi_handle handle = ACPI_HANDLE(input->dev.parent);
 	struct cmpc_accel *accel;
 
-	acpi = to_acpi_device(input->dev.parent);
 	accel = dev_get_drvdata(&input->dev);
 	if (!accel)
 		return -ENXIO;
 
-	cmpc_accel_set_sensitivity_v4(acpi->handle, accel->sensitivity);
-	cmpc_accel_set_g_select_v4(acpi->handle, accel->g_select);
+	cmpc_accel_set_sensitivity_v4(handle, accel->sensitivity);
+	cmpc_accel_set_g_select_v4(handle, accel->g_select);
 
-	if (ACPI_SUCCESS(cmpc_start_accel_v4(acpi->handle))) {
+	if (ACPI_SUCCESS(cmpc_start_accel_v4(handle))) {
 		accel->inputdev_state = CMPC_ACCEL_DEV_STATE_OPEN;
 		return 0;
 	}
@@ -337,13 +337,11 @@ static int cmpc_accel_open_v4(struct input_dev *input)
 
 static void cmpc_accel_close_v4(struct input_dev *input)
 {
-	struct acpi_device *acpi;
 	struct cmpc_accel *accel;
 
-	acpi = to_acpi_device(input->dev.parent);
 	accel = dev_get_drvdata(&input->dev);
 
-	cmpc_stop_accel_v4(acpi->handle);
+	cmpc_stop_accel_v4(ACPI_HANDLE(input->dev.parent));
 	accel->inputdev_state = CMPC_ACCEL_DEV_STATE_CLOSED;
 }
 
@@ -367,7 +365,7 @@ static int cmpc_accel_suspend_v4(struct device *dev)
 	accel = dev_get_drvdata(&inputdev->dev);
 
 	if (accel->inputdev_state == CMPC_ACCEL_DEV_STATE_OPEN)
-		return cmpc_stop_accel_v4(to_acpi_device(dev)->handle);
+		return cmpc_stop_accel_v4(ACPI_HANDLE(dev));
 
 	return 0;
 }
@@ -381,12 +379,12 @@ static int cmpc_accel_resume_v4(struct device *dev)
 	accel = dev_get_drvdata(&inputdev->dev);
 
 	if (accel->inputdev_state == CMPC_ACCEL_DEV_STATE_OPEN) {
-		cmpc_accel_set_sensitivity_v4(to_acpi_device(dev)->handle,
-					      accel->sensitivity);
-		cmpc_accel_set_g_select_v4(to_acpi_device(dev)->handle,
-					   accel->g_select);
+		acpi_handle handle = ACPI_HANDLE(dev);
+
+		cmpc_accel_set_sensitivity_v4(handle, accel->sensitivity);
+		cmpc_accel_set_g_select_v4(handle, accel->g_select);
 
-		if (ACPI_FAILURE(cmpc_start_accel_v4(to_acpi_device(dev)->handle)))
+		if (ACPI_FAILURE(cmpc_start_accel_v4(handle)))
 			return -EIO;
 	}
 
@@ -394,23 +392,29 @@ static int cmpc_accel_resume_v4(struct device *dev)
 }
 #endif
 
-static int cmpc_accel_add_v4(struct acpi_device *acpi)
+static int cmpc_accel_probe_v4(struct platform_device *pdev)
 {
 	int error;
 	struct input_dev *inputdev;
 	struct cmpc_accel *accel;
+	struct acpi_device *acpi;
 
-	accel = devm_kzalloc(&acpi->dev, sizeof(*accel), GFP_KERNEL);
+	acpi = ACPI_COMPANION(&pdev->dev);
+	if (!acpi)
+		return -ENODEV;
+
+	accel = devm_kzalloc(&pdev->dev, sizeof(*accel), GFP_KERNEL);
 	if (!accel)
 		return -ENOMEM;
 
 	accel->inputdev_state = CMPC_ACCEL_DEV_STATE_CLOSED;
 
-	error = cmpc_add_notify_device(&acpi->dev, "cmpc_accel_v4", cmpc_accel_idev_init_v4);
+	error = cmpc_add_notify_device(&pdev->dev, "cmpc_accel_v4", cmpc_accel_idev_init_v4);
 	if (error)
 		return error;
 
-	inputdev = dev_get_drvdata(&acpi->dev);
+	inputdev = dev_get_drvdata(&pdev->dev);
+	dev_set_drvdata(&acpi->dev, inputdev);
 
 	accel->sensitivity = CMPC_ACCEL_SENSITIVITY_DEFAULT;
 	cmpc_accel_set_sensitivity_v4(acpi->handle, accel->sensitivity);
@@ -427,7 +431,7 @@ static int cmpc_accel_add_v4(struct acpi_device *acpi)
 		goto failed_g_select;
 
 	error = acpi_dev_install_notify_handler(acpi, ACPI_DEVICE_NOTIFY,
-						cmpc_accel_handler_v4, acpi);
+						cmpc_accel_handler_v4, &pdev->dev);
 	if (error)
 		goto failed_notify_handler;
 
@@ -440,17 +444,21 @@ static int cmpc_accel_add_v4(struct acpi_device *acpi)
 failed_g_select:
 	device_remove_file(&acpi->dev, &cmpc_accel_sensitivity_attr_v4);
 failed_sensitivity:
-	cmpc_remove_notify_device(&acpi->dev);
+	dev_set_drvdata(&acpi->dev, NULL);
+	cmpc_remove_notify_device(&pdev->dev);
 	return error;
 }
 
-static void cmpc_accel_remove_v4(struct acpi_device *acpi)
+static void cmpc_accel_remove_v4(struct platform_device *pdev)
 {
+	struct acpi_device *acpi = ACPI_COMPANION(&pdev->dev);
+
 	acpi_dev_remove_notify_handler(acpi, ACPI_DEVICE_NOTIFY,
 				       cmpc_accel_handler_v4);
 	device_remove_file(&acpi->dev, &cmpc_accel_g_select_attr_v4);
 	device_remove_file(&acpi->dev, &cmpc_accel_sensitivity_attr_v4);
-	cmpc_remove_notify_device(&acpi->dev);
+	dev_set_drvdata(&acpi->dev, NULL);
+	cmpc_remove_notify_device(&pdev->dev);
 }
 
 static SIMPLE_DEV_PM_OPS(cmpc_accel_pm, cmpc_accel_suspend_v4,
@@ -461,15 +469,14 @@ static const struct acpi_device_id cmpc_accel_device_ids_v4[] = {
 	{"", 0}
 };
 
-static struct acpi_driver cmpc_accel_acpi_driver_v4 = {
-	.name = "cmpc_accel_v4",
-	.class = "cmpc_accel_v4",
-	.ids = cmpc_accel_device_ids_v4,
-	.ops = {
-		.add = cmpc_accel_add_v4,
-		.remove = cmpc_accel_remove_v4,
+static struct platform_driver cmpc_accel_acpi_driver_v4 = {
+	.probe = cmpc_accel_probe_v4,
+	.remove = cmpc_accel_remove_v4,
+	.driver = {
+		.name = "cmpc_accel_v4",
+		.acpi_match_table = cmpc_accel_device_ids_v4,
+		.pm = &cmpc_accel_pm,
 	},
-	.drv.pm = &cmpc_accel_pm,
 };
 
 
@@ -1164,7 +1171,7 @@ static int cmpc_init(void)
 	if (r)
 		goto failed_accel;
 
-	r = acpi_bus_register_driver(&cmpc_accel_acpi_driver_v4);
+	r = platform_driver_register(&cmpc_accel_acpi_driver_v4);
 	if (r)
 		goto failed_accel_v4;
 
@@ -1188,7 +1195,7 @@ static int cmpc_init(void)
 
 static void cmpc_exit(void)
 {
-	acpi_bus_unregister_driver(&cmpc_accel_acpi_driver_v4);
+	platform_driver_unregister(&cmpc_accel_acpi_driver_v4);
 	acpi_bus_unregister_driver(&cmpc_accel_acpi_driver);
 	acpi_bus_unregister_driver(&cmpc_tablet_acpi_driver);
 	acpi_bus_unregister_driver(&cmpc_ipml_acpi_driver);
Signature · 1 line
2.51.0
author maintainer
#7 lore raw

PATCH v2 7/10 platform/x86: classmate-laptop: Convert accel driver to a platform one

In reply to Rafael J. Wysocki

From: "Rafael J. Wysocki" <[email protected]>

In all cases in which a struct acpi_driver is used for binding a driver to an ACPI device object, a corresponding platform device is created by the ACPI core and that device is regarded as a proper representation of underlying hardware. Accordingly, a struct platform_driver should be used by driver code to bind to that device. There are multiple reasons why drivers should not bind directly to ACPI device objects [1].

Overall, it is better to bind drivers to platform devices than to their ACPI companions, so convert cmpc_accel_acpi_driver in the Classmate laptop driver from an ACPI driver to a platform one.

After this change, the input device registered by the driver will appear under the platform device used for driver binding, but the sysfs attribute added by the driver under the ACPI companion of that device will stay there in case there are utilities in user space expecting it to be present there.

While this is not expected to alter functionality, it changes sysfs layout and so it will be visible to user space.

---

v1 -> v2:
* Rebase
* Check the ACPI companion against NULL at probe time

---
 drivers/platform/x86/classmate-laptop.c | 63 +++++++++++++------------
 1 file changed, 33 insertions(+), 30 deletions(-)
drivers/platform/x86/classmate-laptop.c +33 -30
index 454ba2823b51..0d5ad89d0f24 100644
@@ -558,15 +558,15 @@ static acpi_status cmpc_get_accel(acpi_handle handle,
 
 static void cmpc_accel_handler(acpi_handle handle, u32 event, void *data)
 {
-	struct acpi_device *dev = data;
+	struct device *dev = data;
 
 	if (event == 0x81) {
 		unsigned char x, y, z;
 		acpi_status status;
 
-		status = cmpc_get_accel(dev->handle, &x, &y, &z);
+		status = cmpc_get_accel(ACPI_HANDLE(dev), &x, &y, &z);
 		if (ACPI_SUCCESS(status)) {
-			struct input_dev *inputdev = dev_get_drvdata(&dev->dev);
+			struct input_dev *inputdev = dev_get_drvdata(dev);
 
 			input_report_abs(inputdev, ABS_X, x);
 			input_report_abs(inputdev, ABS_Y, y);
@@ -633,20 +633,14 @@ static struct device_attribute cmpc_accel_sensitivity_attr = {
 
 static int cmpc_accel_open(struct input_dev *input)
 {
-	struct acpi_device *acpi;
-
-	acpi = to_acpi_device(input->dev.parent);
-	if (ACPI_SUCCESS(cmpc_start_accel(acpi->handle)))
+	if (ACPI_SUCCESS(cmpc_start_accel(ACPI_HANDLE(input->dev.parent))))
 		return 0;
 	return -EIO;
 }
 
 static void cmpc_accel_close(struct input_dev *input)
 {
-	struct acpi_device *acpi;
-
-	acpi = to_acpi_device(input->dev.parent);
-	cmpc_stop_accel(acpi->handle);
+	cmpc_stop_accel(ACPI_HANDLE(input->dev.parent));
 }
 
 static void cmpc_accel_idev_init(struct input_dev *inputdev)
@@ -659,21 +653,27 @@ static void cmpc_accel_idev_init(struct input_dev *inputdev)
 	inputdev->close = cmpc_accel_close;
 }
 
-static int cmpc_accel_add(struct acpi_device *acpi)
+static int cmpc_accel_probe(struct platform_device *pdev)
 {
 	int error;
 	struct input_dev *inputdev;
 	struct cmpc_accel *accel;
+	struct acpi_device *acpi;
 
-	accel = devm_kzalloc(&acpi->dev, sizeof(*accel), GFP_KERNEL);
+	acpi = ACPI_COMPANION(&pdev->dev);
+	if (!acpi)
+		return -ENODEV;
+
+	accel = devm_kzalloc(&pdev->dev, sizeof(*accel), GFP_KERNEL);
 	if (!accel)
 		return -ENOMEM;
 
-	error = cmpc_add_notify_device(&acpi->dev, "cmpc_accel", cmpc_accel_idev_init);
+	error = cmpc_add_notify_device(&pdev->dev, "cmpc_accel", cmpc_accel_idev_init);
 	if (error)
 		return error;
 
-	inputdev = dev_get_drvdata(&acpi->dev);
+	inputdev = dev_get_drvdata(&pdev->dev);
+	dev_set_drvdata(&acpi->dev, inputdev);
 
 	accel->sensitivity = CMPC_ACCEL_SENSITIVITY_DEFAULT;
 	cmpc_accel_set_sensitivity(acpi->handle, accel->sensitivity);
@@ -683,7 +683,7 @@ static int cmpc_accel_add(struct acpi_device *acpi)
 		goto failed_file;
 
 	error = acpi_dev_install_notify_handler(acpi, ACPI_DEVICE_NOTIFY,
-						cmpc_accel_handler, acpi);
+						cmpc_accel_handler, &pdev->dev);
 	if (error)
 		goto failed_notify_handler;
 
@@ -694,16 +694,20 @@ static int cmpc_accel_add(struct acpi_device *acpi)
 failed_notify_handler:
 	device_remove_file(&acpi->dev, &cmpc_accel_sensitivity_attr);
 failed_file:
-	cmpc_remove_notify_device(&acpi->dev);
+	dev_set_drvdata(&acpi->dev, NULL);
+	cmpc_remove_notify_device(&pdev->dev);
 	return error;
 }
 
-static void cmpc_accel_remove(struct acpi_device *acpi)
+static void cmpc_accel_remove(struct platform_device *pdev)
 {
+	struct acpi_device *acpi = ACPI_COMPANION(&pdev->dev);
+
 	acpi_dev_remove_notify_handler(acpi, ACPI_DEVICE_NOTIFY,
 				       cmpc_accel_handler);
 	device_remove_file(&acpi->dev, &cmpc_accel_sensitivity_attr);
-	cmpc_remove_notify_device(&acpi->dev);
+	dev_set_drvdata(&acpi->dev, NULL);
+	cmpc_remove_notify_device(&pdev->dev);
 }
 
 static const struct acpi_device_id cmpc_accel_device_ids[] = {
@@ -711,14 +715,13 @@ static const struct acpi_device_id cmpc_accel_device_ids[] = {
 	{"", 0}
 };
 
-static struct acpi_driver cmpc_accel_acpi_driver = {
-	.name = "cmpc_accel",
-	.class = "cmpc_accel",
-	.ids = cmpc_accel_device_ids,
-	.ops = {
-		.add = cmpc_accel_add,
-		.remove = cmpc_accel_remove,
-	}
+static struct platform_driver cmpc_accel_acpi_driver = {
+	.probe = cmpc_accel_probe,
+	.remove = cmpc_accel_remove,
+	.driver = {
+		.name = "cmpc_accel",
+		.acpi_match_table = cmpc_accel_device_ids,
+	},
 };
 
 
@@ -1167,7 +1170,7 @@ static int cmpc_init(void)
 	if (r)
 		goto failed_tablet;
 
-	r = acpi_bus_register_driver(&cmpc_accel_acpi_driver);
+	r = platform_driver_register(&cmpc_accel_acpi_driver);
 	if (r)
 		goto failed_accel;
 
@@ -1178,7 +1181,7 @@ static int cmpc_init(void)
 	return r;
 
 failed_accel_v4:
-	acpi_bus_unregister_driver(&cmpc_accel_acpi_driver);
+	platform_driver_unregister(&cmpc_accel_acpi_driver);
 
 failed_accel:
 	acpi_bus_unregister_driver(&cmpc_tablet_acpi_driver);
@@ -1196,7 +1199,7 @@ static int cmpc_init(void)
 static void cmpc_exit(void)
 {
 	platform_driver_unregister(&cmpc_accel_acpi_driver_v4);
-	acpi_bus_unregister_driver(&cmpc_accel_acpi_driver);
+	platform_driver_unregister(&cmpc_accel_acpi_driver);
 	acpi_bus_unregister_driver(&cmpc_tablet_acpi_driver);
 	acpi_bus_unregister_driver(&cmpc_ipml_acpi_driver);
 	acpi_bus_unregister_driver(&cmpc_keys_acpi_driver);
Signature · 1 line
2.51.0
author maintainer
#8 lore raw

PATCH v2 8/10 platform/x86: classmate-laptop: Convert tablet driver to a platform one

In reply to Rafael J. Wysocki

From: "Rafael J. Wysocki" <[email protected]>

In all cases in which a struct acpi_driver is used for binding a driver to an ACPI device object, a corresponding platform device is created by the ACPI core and that device is regarded as a proper representation of underlying hardware. Accordingly, a struct platform_driver should be used by driver code to bind to that device. There are multiple reasons why drivers should not bind directly to ACPI device objects [1].

Overall, it is better to bind drivers to platform devices than to their ACPI companions, so convert cmpc_tablet_acpi_driver in the Classmate laptop driver from an ACPI driver to a platform one.

After this change, the input device registered by the driver will appear under the platform device used for driver binding.

While this is not expected to alter functionality, it changes sysfs layout and so it will be visible to user space.

---

v1 -> v2:
* Rebase
* Check the ACPI companion against NULL at probe time

---
 drivers/platform/x86/classmate-laptop.c | 55 +++++++++++++------------
 1 file changed, 29 insertions(+), 26 deletions(-)
drivers/platform/x86/classmate-laptop.c +29 -26
index 0d5ad89d0f24..464ab852b492 100644
@@ -748,12 +748,12 @@ static acpi_status cmpc_get_tablet(acpi_handle handle,
 
 static void cmpc_tablet_handler(acpi_handle handle, u32 event, void *data)
 {
-	struct acpi_device *dev = data;
+	struct device *dev = data;
 	unsigned long long val = 0;
-	struct input_dev *inputdev = dev_get_drvdata(&dev->dev);
+	struct input_dev *inputdev = dev_get_drvdata(dev);
 
 	if (event == 0x81) {
-		if (ACPI_SUCCESS(cmpc_get_tablet(dev->handle, &val))) {
+		if (ACPI_SUCCESS(cmpc_get_tablet(ACPI_HANDLE(dev), &val))) {
 			input_report_switch(inputdev, SW_TABLET_MODE, !val);
 			input_sync(inputdev);
 		}
@@ -762,40 +762,44 @@ static void cmpc_tablet_handler(acpi_handle handle, u32 event, void *data)
 
 static void cmpc_tablet_idev_init(struct input_dev *inputdev)
 {
+	acpi_handle handle = ACPI_HANDLE(inputdev->dev.parent);
 	unsigned long long val = 0;
-	struct acpi_device *acpi;
 
 	set_bit(EV_SW, inputdev->evbit);
 	set_bit(SW_TABLET_MODE, inputdev->swbit);
 
-	acpi = to_acpi_device(inputdev->dev.parent);
-	if (ACPI_SUCCESS(cmpc_get_tablet(acpi->handle, &val))) {
+	if (ACPI_SUCCESS(cmpc_get_tablet(handle, &val))) {
 		input_report_switch(inputdev, SW_TABLET_MODE, !val);
 		input_sync(inputdev);
 	}
 }
 
-static int cmpc_tablet_add(struct acpi_device *acpi)
+static int cmpc_tablet_probe(struct platform_device *pdev)
 {
+	struct acpi_device *acpi;
 	int error;
 
-	error = cmpc_add_notify_device(&acpi->dev, "cmpc_tablet", cmpc_tablet_idev_init);
+	acpi = ACPI_COMPANION(&pdev->dev);
+	if (!acpi)
+		return -ENODEV;
+
+	error = cmpc_add_notify_device(&pdev->dev, "cmpc_tablet", cmpc_tablet_idev_init);
 	if (error)
 		return error;
 
 	error = acpi_dev_install_notify_handler(acpi, ACPI_DEVICE_NOTIFY,
-						cmpc_tablet_handler, acpi);
+						cmpc_tablet_handler, &pdev->dev);
 	if (error)
-		cmpc_remove_notify_device(&acpi->dev);
+		cmpc_remove_notify_device(&pdev->dev);
 
 	return error;
 }
 
-static void cmpc_tablet_remove(struct acpi_device *acpi)
+static void cmpc_tablet_remove(struct platform_device *pdev)
 {
-	acpi_dev_remove_notify_handler(acpi, ACPI_DEVICE_NOTIFY,
-				       cmpc_tablet_handler);
-	cmpc_remove_notify_device(&acpi->dev);
+	acpi_dev_remove_notify_handler(ACPI_COMPANION(&pdev->dev),
+				       ACPI_DEVICE_NOTIFY, cmpc_tablet_handler);
+	cmpc_remove_notify_device(&pdev->dev);
 }
 
 #ifdef CONFIG_PM_SLEEP
@@ -804,7 +808,7 @@ static int cmpc_tablet_resume(struct device *dev)
 	struct input_dev *inputdev = dev_get_drvdata(dev);
 
 	unsigned long long val = 0;
-	if (ACPI_SUCCESS(cmpc_get_tablet(to_acpi_device(dev)->handle, &val))) {
+	if (ACPI_SUCCESS(cmpc_get_tablet(ACPI_HANDLE(dev), &val))) {
 		input_report_switch(inputdev, SW_TABLET_MODE, !val);
 		input_sync(inputdev);
 	}
@@ -819,15 +823,14 @@ static const struct acpi_device_id cmpc_tablet_device_ids[] = {
 	{"", 0}
 };
 
-static struct acpi_driver cmpc_tablet_acpi_driver = {
-	.name = "cmpc_tablet",
-	.class = "cmpc_tablet",
-	.ids = cmpc_tablet_device_ids,
-	.ops = {
-		.add = cmpc_tablet_add,
-		.remove = cmpc_tablet_remove,
+static struct platform_driver cmpc_tablet_acpi_driver = {
+	.probe = cmpc_tablet_probe,
+	.remove = cmpc_tablet_remove,
+	.driver = {
+		.name = "cmpc_tablet",
+		.acpi_match_table = cmpc_tablet_device_ids,
+		.pm = &cmpc_tablet_pm,
 	},
-	.drv.pm = &cmpc_tablet_pm,
 };
 
 
@@ -1166,7 +1169,7 @@ static int cmpc_init(void)
 	if (r)
 		goto failed_bl;
 
-	r = acpi_bus_register_driver(&cmpc_tablet_acpi_driver);
+	r = platform_driver_register(&cmpc_tablet_acpi_driver);
 	if (r)
 		goto failed_tablet;
 
@@ -1184,7 +1187,7 @@ static int cmpc_init(void)
 	platform_driver_unregister(&cmpc_accel_acpi_driver);
 
 failed_accel:
-	acpi_bus_unregister_driver(&cmpc_tablet_acpi_driver);
+	platform_driver_unregister(&cmpc_tablet_acpi_driver);
 
 failed_tablet:
 	acpi_bus_unregister_driver(&cmpc_ipml_acpi_driver);
@@ -1200,7 +1203,7 @@ static void cmpc_exit(void)
 {
 	platform_driver_unregister(&cmpc_accel_acpi_driver_v4);
 	platform_driver_unregister(&cmpc_accel_acpi_driver);
-	acpi_bus_unregister_driver(&cmpc_tablet_acpi_driver);
+	platform_driver_unregister(&cmpc_tablet_acpi_driver);
 	acpi_bus_unregister_driver(&cmpc_ipml_acpi_driver);
 	acpi_bus_unregister_driver(&cmpc_keys_acpi_driver);
 }
Signature · 1 line
2.51.0
author maintainer
#9 lore raw

PATCH v2 9/10 platform/x86: classmate-laptop: Convert ipml driver to a platform one

In reply to Rafael J. Wysocki

From: "Rafael J. Wysocki" <[email protected]>

In all cases in which a struct acpi_driver is used for binding a driver to an ACPI device object, a corresponding platform device is created by the ACPI core and that device is regarded as a proper representation of underlying hardware. Accordingly, a struct platform_driver should be used by driver code to bind to that device. There are multiple reasons why drivers should not bind directly to ACPI device objects [1].

Overall, it is better to bind drivers to platform devices than to their ACPI companions, so convert cmpc_ipml_acpi_driver in the Classmate laptop driver from an ACPI driver to a platform one.

After this change, the backlight and rfkill devices registered by the driver will appear under the platform device used for driver binding.

While this is not expected to alter functionality, it changes sysfs layout and so it will be visible to user space.

---

v1 -> v2:
* Rebase
* Check the ACPI handle against NULL at probe time

---
 drivers/platform/x86/classmate-laptop.c | 42 ++++++++++++++-----------
 1 file changed, 23 insertions(+), 19 deletions(-)
drivers/platform/x86/classmate-laptop.c +23 -19
index 464ab852b492..08ff4549e956 100644
@@ -997,11 +997,16 @@ struct ipml200_dev {
 	struct rfkill *rf;
 };
 
-static int cmpc_ipml_add(struct acpi_device *acpi)
+static int cmpc_ipml_probe(struct platform_device *pdev)
 {
 	int retval;
 	struct ipml200_dev *ipml;
 	struct backlight_properties props;
+	acpi_handle handle;
+
+	handle = ACPI_HANDLE(&pdev->dev);
+	if (!handle)
+		return -ENODEV;
 
 	ipml = kmalloc_obj(*ipml);
 	if (ipml == NULL)
@@ -1010,16 +1015,16 @@ static int cmpc_ipml_add(struct acpi_device *acpi)
 	memset(&props, 0, sizeof(struct backlight_properties));
 	props.type = BACKLIGHT_PLATFORM;
 	props.max_brightness = 7;
-	ipml->bd = backlight_device_register("cmpc_bl", &acpi->dev,
-					     acpi->handle, &cmpc_bl_ops,
+	ipml->bd = backlight_device_register("cmpc_bl", &pdev->dev,
+					     handle, &cmpc_bl_ops,
 					     &props);
 	if (IS_ERR(ipml->bd)) {
 		retval = PTR_ERR(ipml->bd);
 		goto out_bd;
 	}
 
-	ipml->rf = rfkill_alloc("cmpc_rfkill", &acpi->dev, RFKILL_TYPE_WLAN,
-				&cmpc_rfkill_ops, acpi->handle);
+	ipml->rf = rfkill_alloc("cmpc_rfkill", &pdev->dev, RFKILL_TYPE_WLAN,
+				&cmpc_rfkill_ops, handle);
 	/*
 	 * If RFKILL is disabled, rfkill_alloc will return ERR_PTR(-ENODEV).
 	 * This is OK, however, since all other uses of the device will not
@@ -1033,7 +1038,7 @@ static int cmpc_ipml_add(struct acpi_device *acpi)
 		}
 	}
 
-	dev_set_drvdata(&acpi->dev, ipml);
+	platform_set_drvdata(pdev, ipml);
 	return 0;
 
 out_bd:
@@ -1041,11 +1046,11 @@ static int cmpc_ipml_add(struct acpi_device *acpi)
 	return retval;
 }
 
-static void cmpc_ipml_remove(struct acpi_device *acpi)
+static void cmpc_ipml_remove(struct platform_device *pdev)
 {
 	struct ipml200_dev *ipml;
 
-	ipml = dev_get_drvdata(&acpi->dev);
+	ipml = platform_get_drvdata(pdev);
 
 	backlight_device_unregister(ipml->bd);
 
@@ -1062,14 +1067,13 @@ static const struct acpi_device_id cmpc_ipml_device_ids[] = {
 	{"", 0}
 };
 
-static struct acpi_driver cmpc_ipml_acpi_driver = {
-	.name = "cmpc",
-	.class = "cmpc",
-	.ids = cmpc_ipml_device_ids,
-	.ops = {
-		.add = cmpc_ipml_add,
-		.remove = cmpc_ipml_remove
-	}
+static struct platform_driver cmpc_ipml_acpi_driver = {
+	.probe = cmpc_ipml_probe,
+	.remove = cmpc_ipml_remove,
+	.driver = {
+		.name = "cmpc",
+		.acpi_match_table = cmpc_ipml_device_ids,
+	},
 };
 
 
@@ -1165,7 +1169,7 @@ static int cmpc_init(void)
 	if (r)
 		goto failed_keys;
 
-	r = acpi_bus_register_driver(&cmpc_ipml_acpi_driver);
+	r = platform_driver_register(&cmpc_ipml_acpi_driver);
 	if (r)
 		goto failed_bl;
 
@@ -1190,7 +1194,7 @@ static int cmpc_init(void)
 	platform_driver_unregister(&cmpc_tablet_acpi_driver);
 
 failed_tablet:
-	acpi_bus_unregister_driver(&cmpc_ipml_acpi_driver);
+	platform_driver_unregister(&cmpc_ipml_acpi_driver);
 
 failed_bl:
 	acpi_bus_unregister_driver(&cmpc_keys_acpi_driver);
@@ -1204,7 +1208,7 @@ static void cmpc_exit(void)
 	platform_driver_unregister(&cmpc_accel_acpi_driver_v4);
 	platform_driver_unregister(&cmpc_accel_acpi_driver);
 	platform_driver_unregister(&cmpc_tablet_acpi_driver);
-	acpi_bus_unregister_driver(&cmpc_ipml_acpi_driver);
+	platform_driver_unregister(&cmpc_ipml_acpi_driver);
 	acpi_bus_unregister_driver(&cmpc_keys_acpi_driver);
 }
 
author maintainer
#10 lore raw

PATCH v2 10/10 platform/x86: classmate-laptop: Convert keys driver to a platform one

In reply to Rafael J. Wysocki

From: "Rafael J. Wysocki" <[email protected]>

In all cases in which a struct acpi_driver is used for binding a driver to an ACPI device object, a corresponding platform device is created by the ACPI core and that device is regarded as a proper representation of underlying hardware. Accordingly, a struct platform_driver should be used by driver code to bind to that device. There are multiple reasons why drivers should not bind directly to ACPI device objects [1].

Overall, it is better to bind drivers to platform devices than to their ACPI companions, so convert cmpc_keys_acpi_driver in the Classmate laptop driver from an ACPI driver to a platform one.

After this change, the input device registered by the driver will appear under the platform device used for driver binding.

While this is not expected to alter functionality, it changes sysfs layout and so it will be visible to user space.

---

v1 -> v2:
* Rebase
* Check the ACPI companion against NULL at probe time

---
 drivers/platform/x86/classmate-laptop.c | 46 ++++++++++++++-----------
 1 file changed, 25 insertions(+), 21 deletions(-)
drivers/platform/x86/classmate-laptop.c +25 -21
index 08ff4549e956..05890815795d 100644
@@ -1098,13 +1098,13 @@ static int cmpc_keys_codes[] = {
 
 static void cmpc_keys_handler(acpi_handle handle, u32 event, void *data)
 {
-	struct acpi_device *dev = data;
+	struct device *dev = data;
 	struct input_dev *inputdev;
 	int code = KEY_MAX;
 
 	if ((event & 0x0F) < ARRAY_SIZE(cmpc_keys_codes))
 		code = cmpc_keys_codes[event & 0x0F];
-	inputdev = dev_get_drvdata(&dev->dev);
+	inputdev = dev_get_drvdata(dev);
 	input_report_key(inputdev, code, !(event & 0x10));
 	input_sync(inputdev);
 }
@@ -1118,27 +1118,32 @@ static void cmpc_keys_idev_init(struct input_dev *inputdev)
 		set_bit(cmpc_keys_codes[i], inputdev->keybit);
 }
 
-static int cmpc_keys_add(struct acpi_device *acpi)
+static int cmpc_keys_probe(struct platform_device *pdev)
 {
+	struct acpi_device *acpi;
 	int error;
 
-	error = cmpc_add_notify_device(&acpi->dev, "cmpc_keys", cmpc_keys_idev_init);
+	acpi = ACPI_COMPANION(&pdev->dev);
+	if (!acpi)
+		return -ENODEV;
+
+	error = cmpc_add_notify_device(&pdev->dev, "cmpc_keys", cmpc_keys_idev_init);
 	if (error)
 		return error;
 
 	error = acpi_dev_install_notify_handler(acpi, ACPI_DEVICE_NOTIFY,
-						cmpc_keys_handler, acpi);
+						cmpc_keys_handler, &pdev->dev);
 	if (error)
-		cmpc_remove_notify_device(&acpi->dev);
+		cmpc_remove_notify_device(&pdev->dev);
 
 	return error;
 }
 
-static void cmpc_keys_remove(struct acpi_device *acpi)
+static void cmpc_keys_remove(struct platform_device *pdev)
 {
-	acpi_dev_remove_notify_handler(acpi, ACPI_DEVICE_NOTIFY,
-				       cmpc_keys_handler);
-	cmpc_remove_notify_device(&acpi->dev);
+	acpi_dev_remove_notify_handler(ACPI_COMPANION(&pdev->dev),
+				       ACPI_DEVICE_NOTIFY, cmpc_keys_handler);
+	cmpc_remove_notify_device(&pdev->dev);
 }
 
 static const struct acpi_device_id cmpc_keys_device_ids[] = {
@@ -1146,14 +1151,13 @@ static const struct acpi_device_id cmpc_keys_device_ids[] = {
 	{"", 0}
 };
 
-static struct acpi_driver cmpc_keys_acpi_driver = {
-	.name = "cmpc_keys",
-	.class = "cmpc_keys",
-	.ids = cmpc_keys_device_ids,
-	.ops = {
-		.add = cmpc_keys_add,
-		.remove = cmpc_keys_remove,
-	}
+static struct platform_driver cmpc_keys_acpi_driver = {
+	.probe = cmpc_keys_probe,
+	.remove = cmpc_keys_remove,
+	.driver = {
+		.name = "cmpc_keys",
+		.acpi_match_table = cmpc_keys_device_ids,
+	},
 };
 
 
@@ -1165,7 +1169,7 @@ static int cmpc_init(void)
 {
 	int r;
 
-	r = acpi_bus_register_driver(&cmpc_keys_acpi_driver);
+	r = platform_driver_register(&cmpc_keys_acpi_driver);
 	if (r)
 		goto failed_keys;
 
@@ -1197,7 +1201,7 @@ static int cmpc_init(void)
 	platform_driver_unregister(&cmpc_ipml_acpi_driver);
 
 failed_bl:
-	acpi_bus_unregister_driver(&cmpc_keys_acpi_driver);
+	platform_driver_unregister(&cmpc_keys_acpi_driver);
 
 failed_keys:
 	return r;
@@ -1209,7 +1213,7 @@ static void cmpc_exit(void)
 	platform_driver_unregister(&cmpc_accel_acpi_driver);
 	platform_driver_unregister(&cmpc_tablet_acpi_driver);
 	platform_driver_unregister(&cmpc_ipml_acpi_driver);
-	acpi_bus_unregister_driver(&cmpc_keys_acpi_driver);
+	platform_driver_unregister(&cmpc_keys_acpi_driver);
 }
 
 module_init(cmpc_init);
Signature · 1 line
2.51.0
Acked-by on the v2 series
#11 lore raw
In reply to Rafael J. Wysocki
Quoted 51 lines · Rafael J. Wysocki

Hi All,

This is an update of

https://lore.kernel.org/linux-acpi/[email protected]/

sent mostly because I have realized that using devm_kzalloc() in the first patch simplifies things going forward, but also it adds ACPI companion checks against NULL at probe time that were missing previously.

This series is part of a larger effort to switch over all drivers using the struct acpi_driver interface to the more common struct platform_driver interface and eliminate the former. The background is explained in Documentation/driver-api/acpi/acpi-drivers.rst and in the changelog of the patch that introduced the above document:

https://lore.kernel.org/all/[email protected]/

The bottom line is that the kernel would be better off without struct acpi_driver and so it is better to get rid of it.

This series carries out driver conversion of 5 subdrivers in the platform x86 classmate-laptop driver.

The first 5 patches in the series are preliminary.

Patch [01/10] converts two subdrivers in classmate-laptop to using devm_kzalloc() for allocating memory which helps to avoid memory leaks during driver removal.

Patch [02/10] modifies 2 subdrivers in classmate-laptop so that their probe rollback code ordering is the same as the removal one.

Patches [03-04/10] change the type of the first argument in 2 helper functions in classmate-laptop and rename them.

Patch [05/10] updates classmate-laptop subdrivers to install their own ACPI notify handlers instead of using the .notify() callback in struct acpi_driver.

The remaining patches convert the classmate-laptop subdrivers to platform drivers, one at a time.

Patch [06/10] converts the accel-v4 subdriver in classmate-laptop to using struct platform_driver for device binding.

Patch [07/10] converts the accel subdriver in classmate-laptop to using struct platform_driver for device binding.

Patch [08/10] converts the tablet subdriver in classmate-laptop to using struct platform_driver for device binding.

Quoted 11 lines

Patch [09/10] converts the ipml subdriver in classmate-laptop to using struct platform_driver for device binding.

Patch [10/10] converts the keys subdriver in classmate-laptop to using struct platform_driver for device binding.

Thanks!

For the whole series:

maintainer
#12 lore raw
In reply to Rafael J. Wysocki
Quoted 9 lines · Rafael J. Wysocki

This is an update of

https://lore.kernel.org/linux-acpi/[email protected]/

sent mostly because I have realized that using devm_kzalloc() in the first patch simplifies things going forward, but also it adds ACPI companion checks against NULL at probe time that were missing previously.

[...]

Thank you for your contribution, it has been applied to my local review-ilpo-next branch. Note it will show up in the public platform-drivers-x86/review-ilpo-next branch only once I've pushed my local branch there, which might take a while.

The list of commits applied:
[01/10] platform/x86: classmate-laptop: Address memory leaks on driver removal

        commit: 4727a7b46fb264ee85f5a5b858ebde97b1a424d8
[02/10] platform/x86: classmate-laptop: Unify probe rollback and remove code
        commit: 5a45e962ee10d756fc945057000122ac258f4c2b
[03/10] platform/x86: classmate-laptop: Pass struct device pointer to helpers
        commit: 9daf8ebbca4ab6ff0bb114d8b3dfa579c63ddfec
[04/10] platform/x86: classmate-laptop: Rename two helper functions
        commit: 491e41d697a4ef602b05aadc03f4a881b48cbdf8
[05/10] platform/x86: classmate-laptop: Register ACPI notify handlers directly
        commit: 8dbe106718efbcb72d175cc472ccae885fd6471e
[06/10] platform/x86: classmate-laptop: Convert v4 accel driver to a platform one
        commit: 2f677df5ceda1cbb8704ffad35d5fd2479e4a667
[07/10] platform/x86: classmate-laptop: Convert accel driver to a platform one
        commit: 01feb0f446c1fbdf1daee2d95b0206fa1a7c3548
[08/10] platform/x86: classmate-laptop: Convert tablet driver to a platform one
        commit: 966c5bfa52d89b8701963f8587d7eb9cbe1ae5fd
[09/10] platform/x86: classmate-laptop: Convert ipml driver to a platform one
        commit: b207f8c36ea1fceb9b924af3b956f42dafdfa657
[10/10] platform/x86: classmate-laptop: Convert keys driver to a platform one
        commit: d1f01dfa7f1fda58a95846dc2c5ce50a71e18580

--
i.