Re: [RFC PATCH v3 6/8] leds: trigger: add hardware-phy-activity trigger
From: Andrew Lunn
Date: Tue Nov 09 2021 - 16:18:02 EST
> +#define DEFINE_OFFLOAD_TRIGGER(trigger_name, trigger) \
> + static ssize_t trigger_name##_show(struct device *dev, \
> + struct device_attribute *attr, char *buf) \
> + { \
> + struct led_classdev *led_cdev = led_trigger_get_led(dev); \
> + int val; \
> + val = led_cdev->hw_control_configure(led_cdev, trigger, BLINK_MODE_READ); \
> + return sprintf(buf, "%d\n", val ? 1 : 0); \
> + } \
> + static ssize_t trigger_name##_store(struct device *dev, \
> + struct device_attribute *attr, \
> + const char *buf, size_t size) \
> + { \
> + struct led_classdev *led_cdev = led_trigger_get_led(dev); \
> + unsigned long state; \
> + int cmd, ret; \
> + ret = kstrtoul(buf, 0, &state); \
> + if (ret) \
> + return ret; \
> + cmd = !!state ? BLINK_MODE_ENABLE : BLINK_MODE_DISABLE; \
> + /* Update the configuration with every change */ \
> + led_cdev->hw_control_configure(led_cdev, trigger, cmd); \
> + return size; \
> + } \
> + DEVICE_ATTR_RW(trigger_name)
These are pretty big macro magic functions. And there is little actual
macro in them. So make them simple functions which call helpers
static ssize_t trigger_name##_show(struct device *dev, \
struct device_attribute *attr, char *buf) \
{ \
return trigger_generic_store(dev, attr, buf, size, trigger); \
} \
static ssize_t trigger_name##_store(struct device *dev, \
struct device_attribute *attr, \
const char *buf, size_t size) \
{ \
return trigger_generic_store(dev, attr, buf, size, trigger); \
} \
> +/* The attrs will be placed dynamically based on the supported triggers */
> +static struct attribute *phy_activity_attrs[PHY_ACTIVITY_MAX_TRIGGERS + 1];
> +
> +static int offload_phy_activity_activate(struct led_classdev *led_cdev)
> +{
> + u32 checked_list = 0;
> + int i, trigger, ret;
> +
> + /* Scan the supported offload triggers and expose them in sysfs if supported */
> + for (trigger = 0, i = 0; trigger < PHY_ACTIVITY_MAX_TRIGGERS; trigger++) {
> + if (!(checked_list & BLINK_TX) &&
> + led_trigger_blink_mode_is_supported(led_cdev, BLINK_TX)) {
> + phy_activity_attrs[i++] = &dev_attr_blink_tx.attr;
> + checked_list |= BLINK_TX;
> + }
Please re-write this using tables, rather than all this repeated code.
Andrew