Skip to content

Commit

Permalink
pwm: Set class for exported channels in sysfs
Browse files Browse the repository at this point in the history
Summary:
   This patch sends a uevent (KOBJ_CHANGE) on the pwmchipN device,
everytime a pwmX channel has been exported/unexported via sysfs.

This
allows udev to implement rules on such events, like:

SUBSYSTEM=="pwm*", PROGRAM="/bin/sh -c '\
        chown -R root:gpio /sys/class/pwm && chmod -R 770 /sys/class/pwm;\
        chown -R root:gpio
/sys/devices/platform/soc/*.pwm/pwm/pwmchip* && chmod -R 770
/sys/devices/platform/soc/*.pwm/pwm/pwmchip*\
'"

basic testing:
$ udevadm monitor --environment &
$ echo 0 > /sys/class/pwm/pwmchip0/export
KERNEL[68.967366] change   /devices/platform/soc/a500d000.pwm/pwm/pwmchip0 (pwm)
ACTION=change
DEVPATH=/devices/platform/soc/a500d000.pwm/pwm/pwmchip0
SUBSYSTEM=pwm
UNEXPORT=pwm0
SEQNUM=2007

Signed-off-by: yaqiang.li <[email protected]>
  • Loading branch information
yaqiang.li committed Apr 3, 2023
1 parent b29dc51 commit e40256f
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions drivers/pwm/sysfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ static void pwm_export_release(struct device *child)
static int pwm_export_child(struct device *parent, struct pwm_device *pwm)
{
struct pwm_export *export;
char *pwm_prop[2];
int ret;

if (test_and_set_bit(PWMF_EXPORTED, &pwm->flags))
Expand Down Expand Up @@ -276,6 +277,11 @@ static int pwm_export_child(struct device *parent, struct pwm_device *pwm)
return ret;
}

pwm_prop[0] = kasprintf(GFP_KERNEL, "EXPORT=pwm%u", pwm->hwpwm);
pwm_prop[1] = NULL;
kobject_uevent_env(&parent->kobj, KOBJ_CHANGE, pwm_prop);
kfree(pwm_prop[0]);

return 0;
}

Expand All @@ -287,6 +293,7 @@ static int pwm_unexport_match(struct device *child, void *data)
static int pwm_unexport_child(struct device *parent, struct pwm_device *pwm)
{
struct device *child;
char *pwm_prop[2];

if (!test_and_clear_bit(PWMF_EXPORTED, &pwm->flags))
return -ENODEV;
Expand All @@ -295,6 +302,11 @@ static int pwm_unexport_child(struct device *parent, struct pwm_device *pwm)
if (!child)
return -ENODEV;

pwm_prop[0] = kasprintf(GFP_KERNEL, "UNEXPORT=pwm%u", pwm->hwpwm);
pwm_prop[1] = NULL;
kobject_uevent_env(&parent->kobj, KOBJ_CHANGE, pwm_prop);
kfree(pwm_prop[0]);

/* for device_find_child() */
put_device(child);
device_unregister(child);
Expand Down

0 comments on commit e40256f

Please sign in to comment.