hwmon: (applesmc) Simplify feature sysfs handling
authorHenrik Rydberg <rydberg@euromail.se>
Tue, 9 Nov 2010 15:15:09 +0000 (15:15 +0000)
committerGuenter Roeck <guenter.roeck@ericsson.com>
Sat, 8 Jan 2011 18:55:43 +0000 (10:55 -0800)
Given the dynamic node construction method, the setup of the
accelerometer, light sensor and keyboard backlight sysfs nodes
can be simplified. This patch does not contain any logic changes.

Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Signed-off-by: Guenter Roeck <guenter.roeck@ericsson.com>
drivers/hwmon/applesmc.c

index b0792361c52eb18abd89dd6fd33e3d674e52fd17..ec06b99892787157bd8f1b08dc1643884ddcd27d 100644 (file)
@@ -992,47 +992,27 @@ static struct led_classdev applesmc_backlight = {
        .brightness_set         = applesmc_brightness_set,
 };
 
-static DEVICE_ATTR(name, 0444, applesmc_name_show, NULL);
-
-static DEVICE_ATTR(position, 0444, applesmc_position_show, NULL);
-static DEVICE_ATTR(calibrate, 0644,
-                       applesmc_calibrate_show, applesmc_calibrate_store);
-
-static struct attribute *accelerometer_attributes[] = {
-       &dev_attr_position.attr,
-       &dev_attr_calibrate.attr,
-       NULL
+static struct applesmc_node_group info_group[] = {
+       { "name", applesmc_name_show },
+       { "key_count", applesmc_key_count_show },
+       { "key_at_index", applesmc_key_at_index_show, applesmc_key_at_index_store },
+       { "key_at_index_name", applesmc_key_at_index_name_show },
+       { "key_at_index_type", applesmc_key_at_index_type_show },
+       { "key_at_index_data_length", applesmc_key_at_index_data_length_show },
+       { "key_at_index_data", applesmc_key_at_index_read_show },
+       { }
 };
 
-static const struct attribute_group accelerometer_attributes_group =
-       { .attrs = accelerometer_attributes };
-
-static DEVICE_ATTR(light, 0444, applesmc_light_show, NULL);
-
-static DEVICE_ATTR(key_count, 0444, applesmc_key_count_show, NULL);
-static DEVICE_ATTR(key_at_index, 0644,
-               applesmc_key_at_index_show, applesmc_key_at_index_store);
-static DEVICE_ATTR(key_at_index_name, 0444,
-                                       applesmc_key_at_index_name_show, NULL);
-static DEVICE_ATTR(key_at_index_type, 0444,
-                                       applesmc_key_at_index_type_show, NULL);
-static DEVICE_ATTR(key_at_index_data_length, 0444,
-                               applesmc_key_at_index_data_length_show, NULL);
-static DEVICE_ATTR(key_at_index_data, 0444,
-                               applesmc_key_at_index_read_show, NULL);
-
-static struct attribute *key_enumeration_attributes[] = {
-       &dev_attr_key_count.attr,
-       &dev_attr_key_at_index.attr,
-       &dev_attr_key_at_index_name.attr,
-       &dev_attr_key_at_index_type.attr,
-       &dev_attr_key_at_index_data_length.attr,
-       &dev_attr_key_at_index_data.attr,
-       NULL
+static struct applesmc_node_group accelerometer_group[] = {
+       { "position", applesmc_position_show },
+       { "calibrate", applesmc_calibrate_show, applesmc_calibrate_store },
+       { }
 };
 
-static const struct attribute_group key_enumeration_group =
-       { .attrs = key_enumeration_attributes };
+static struct applesmc_node_group light_sensor_group[] = {
+       { "light", applesmc_light_show },
+       { }
+};
 
 static struct applesmc_node_group fan_group[] = {
        { "fan%d_label", applesmc_show_fan_position },
@@ -1115,8 +1095,10 @@ static int applesmc_create_accelerometer(void)
        struct input_dev *idev;
        int ret;
 
-       ret = sysfs_create_group(&pdev->dev.kobj,
-                                       &accelerometer_attributes_group);
+       if (!smcreg.has_accelerometer)
+               return 0;
+
+       ret = applesmc_create_nodes(accelerometer_group, 1);
        if (ret)
                goto out;
 
@@ -1153,7 +1135,7 @@ out_idev:
        input_free_polled_device(applesmc_idev);
 
 out_sysfs:
-       sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
+       applesmc_destroy_nodes(accelerometer_group);
 
 out:
        pr_warn("driver init failed (ret=%d)!\n", ret);
@@ -1163,10 +1145,45 @@ out:
 /* Release all ressources used by the accelerometer */
 static void applesmc_release_accelerometer(void)
 {
+       if (!smcreg.has_accelerometer)
+               return;
        input_unregister_polled_device(applesmc_idev);
        input_free_polled_device(applesmc_idev);
-       sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
+       applesmc_destroy_nodes(accelerometer_group);
 }
+
+static int applesmc_create_light_sensor(void)
+{
+       if (!smcreg.num_light_sensors)
+               return 0;
+       return applesmc_create_nodes(light_sensor_group, 1);
+}
+
+static void applesmc_release_light_sensor(void)
+{
+       if (!smcreg.num_light_sensors)
+               return;
+       applesmc_destroy_nodes(light_sensor_group);
+}
+
+static int applesmc_create_key_backlight(void)
+{
+       if (!smcreg.has_key_backlight)
+               return 0;
+       applesmc_led_wq = create_singlethread_workqueue("applesmc-led");
+       if (!applesmc_led_wq)
+               return -ENOMEM;
+       return led_classdev_register(&pdev->dev, &applesmc_backlight);
+}
+
+static void applesmc_release_key_backlight(void)
+{
+       if (!smcreg.has_key_backlight)
+               return;
+       led_classdev_unregister(&applesmc_backlight);
+       destroy_workqueue(applesmc_led_wq);
+}
+
 static int applesmc_dmi_match(const struct dmi_system_id *id)
 {
        return 1;
@@ -1234,15 +1251,10 @@ static int __init applesmc_init(void)
        if (ret)
                goto out_device;
 
-       ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_name.attr);
+       ret = applesmc_create_nodes(info_group, 1);
        if (ret)
                goto out_smcreg;
 
-       /* Create key enumeration sysfs files */
-       ret = sysfs_create_group(&pdev->dev.kobj, &key_enumeration_group);
-       if (ret)
-               goto out_name;
-
        ret = applesmc_create_nodes(fan_group, smcreg.fan_count);
        if (ret)
                goto out_info;
@@ -1251,32 +1263,17 @@ static int __init applesmc_init(void)
        if (ret)
                goto out_fans;
 
-       if (smcreg.has_accelerometer) {
-               ret = applesmc_create_accelerometer();
-               if (ret)
-                       goto out_temperature;
-       }
-
-       if (smcreg.num_light_sensors) {
-               /* Add light sensor file */
-               ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_light.attr);
-               if (ret)
-                       goto out_accelerometer;
-       }
+       ret = applesmc_create_accelerometer();
+       if (ret)
+               goto out_temperature;
 
-       if (smcreg.has_key_backlight) {
-               /* Create the workqueue */
-               applesmc_led_wq = create_singlethread_workqueue("applesmc-led");
-               if (!applesmc_led_wq) {
-                       ret = -ENOMEM;
-                       goto out_light_sysfs;
-               }
+       ret = applesmc_create_light_sensor();
+       if (ret)
+               goto out_accelerometer;
 
-               /* register as a led device */
-               ret = led_classdev_register(&pdev->dev, &applesmc_backlight);
-               if (ret < 0)
-                       goto out_light_wq;
-       }
+       ret = applesmc_create_key_backlight();
+       if (ret)
+               goto out_light_sysfs;
 
        hwmon_dev = hwmon_device_register(&pdev->dev);
        if (IS_ERR(hwmon_dev)) {
@@ -1289,25 +1286,17 @@ static int __init applesmc_init(void)
        return 0;
 
 out_light_ledclass:
-       if (smcreg.has_key_backlight)
-               led_classdev_unregister(&applesmc_backlight);
-out_light_wq:
-       if (smcreg.has_key_backlight)
-               destroy_workqueue(applesmc_led_wq);
+       applesmc_release_key_backlight();
 out_light_sysfs:
-       if (smcreg.num_light_sensors)
-               sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
+       applesmc_release_light_sensor();
 out_accelerometer:
-       if (smcreg.has_accelerometer)
-               applesmc_release_accelerometer();
+       applesmc_release_accelerometer();
 out_temperature:
        applesmc_destroy_nodes(temp_group);
 out_fans:
        applesmc_destroy_nodes(fan_group);
 out_info:
-       sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
-out_name:
-       sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
+       applesmc_destroy_nodes(info_group);
 out_smcreg:
        applesmc_destroy_smcreg();
 out_device:
@@ -1324,18 +1313,12 @@ out:
 static void __exit applesmc_exit(void)
 {
        hwmon_device_unregister(hwmon_dev);
-       if (smcreg.has_key_backlight) {
-               led_classdev_unregister(&applesmc_backlight);
-               destroy_workqueue(applesmc_led_wq);
-       }
-       if (smcreg.num_light_sensors)
-               sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
-       if (smcreg.has_accelerometer)
-               applesmc_release_accelerometer();
+       applesmc_release_key_backlight();
+       applesmc_release_light_sensor();
+       applesmc_release_accelerometer();
        applesmc_destroy_nodes(temp_group);
        applesmc_destroy_nodes(fan_group);
-       sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
-       sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
+       applesmc_destroy_nodes(info_group);
        applesmc_destroy_smcreg();
        platform_device_unregister(pdev);
        platform_driver_unregister(&applesmc_driver);