From 7f4c9bc29ba72c5e3d0f9eaa91cabac9cd3342c9 Mon Sep 17 00:00:00 2001 From: Bumwoo Lee Date: Thu, 2 Mar 2023 18:01:40 +0900 Subject: extcon: Remove redundant null checking for class create_extcon_class() is already Null checking. Signed-off-by: Bumwoo Lee Acked-by: MyungJoo Ham --- drivers/extcon/extcon.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) (limited to 'drivers/extcon/extcon.c') diff --git a/drivers/extcon/extcon.c b/drivers/extcon/extcon.c index d43ba8e7260d..2fa033711a55 100644 --- a/drivers/extcon/extcon.c +++ b/drivers/extcon/extcon.c @@ -1012,12 +1012,13 @@ ATTRIBUTE_GROUPS(extcon); static int create_extcon_class(void) { - if (!extcon_class) { - extcon_class = class_create("extcon"); - if (IS_ERR(extcon_class)) - return PTR_ERR(extcon_class); - extcon_class->dev_groups = extcon_groups; - } + if (extcon_class) + return 0; + + extcon_class = class_create("extcon"); + if (IS_ERR(extcon_class)) + return PTR_ERR(extcon_class); + extcon_class->dev_groups = extcon_groups; return 0; } @@ -1088,11 +1089,9 @@ int extcon_dev_register(struct extcon_dev *edev) int ret, index = 0; static atomic_t edev_no = ATOMIC_INIT(-1); - if (!extcon_class) { - ret = create_extcon_class(); - if (ret < 0) - return ret; - } + ret = create_extcon_class(); + if (ret < 0) + return ret; if (!edev || !edev->supported_cable) return -EINVAL; -- cgit v1.2.3 From 3d9138e5bdcf9f0277904441cbf4cb1407ed8603 Mon Sep 17 00:00:00 2001 From: Bumwoo Lee Date: Mon, 20 Mar 2023 12:19:37 +0900 Subject: extcon: Add extcon_alloc_cables to simplify extcon register function The cable allocation part is functionalized from extcon_dev_register. Signed-off-by: Bumwoo Lee Acked-by: MyungJoo Ham --- drivers/extcon/extcon.c | 111 ++++++++++++++++++++++++++++-------------------- 1 file changed, 65 insertions(+), 46 deletions(-) (limited to 'drivers/extcon/extcon.c') diff --git a/drivers/extcon/extcon.c b/drivers/extcon/extcon.c index 2fa033711a55..72591af2fec2 100644 --- a/drivers/extcon/extcon.c +++ b/drivers/extcon/extcon.c @@ -1070,6 +1070,66 @@ void extcon_dev_free(struct extcon_dev *edev) } EXPORT_SYMBOL_GPL(extcon_dev_free); +/** + * extcon_alloc_cables() - alloc the cables for extcon device + * @edev: extcon device which has cables + * + * Returns 0 if success or error number if fail. + */ +static int extcon_alloc_cables(struct extcon_dev *edev) +{ + int index; + char *str; + struct extcon_cable *cable; + + if (!edev) + return -EINVAL; + + if (!edev->max_supported) + return 0; + + edev->cables = kcalloc(edev->max_supported, + sizeof(struct extcon_cable), + GFP_KERNEL); + if (!edev->cables) + return -ENOMEM; + + for (index = 0; index < edev->max_supported; index++) { + cable = &edev->cables[index]; + + str = kasprintf(GFP_KERNEL, "cable.%d", index); + if (!str) { + for (index--; index >= 0; index--) { + cable = &edev->cables[index]; + kfree(cable->attr_g.name); + } + + kfree(edev->cables); + return -ENOMEM; + } + + cable->edev = edev; + cable->cable_index = index; + cable->attrs[0] = &cable->attr_name.attr; + cable->attrs[1] = &cable->attr_state.attr; + cable->attrs[2] = NULL; + cable->attr_g.name = str; + cable->attr_g.attrs = cable->attrs; + + sysfs_attr_init(&cable->attr_name.attr); + cable->attr_name.attr.name = "name"; + cable->attr_name.attr.mode = 0444; + cable->attr_name.show = cable_name_show; + + sysfs_attr_init(&cable->attr_state.attr); + cable->attr_state.attr.name = "state"; + cable->attr_state.attr.mode = 0444; + cable->attr_state.show = cable_state_show; + } + + return 0; +} + /** * extcon_dev_register() - Register an new extcon device * @edev: the extcon device to be registered @@ -1117,50 +1177,9 @@ int extcon_dev_register(struct extcon_dev *edev) dev_set_name(&edev->dev, "extcon%lu", (unsigned long)atomic_inc_return(&edev_no)); - if (edev->max_supported) { - char *str; - struct extcon_cable *cable; - - edev->cables = kcalloc(edev->max_supported, - sizeof(struct extcon_cable), - GFP_KERNEL); - if (!edev->cables) { - ret = -ENOMEM; - goto err_sysfs_alloc; - } - for (index = 0; index < edev->max_supported; index++) { - cable = &edev->cables[index]; - - str = kasprintf(GFP_KERNEL, "cable.%d", index); - if (!str) { - for (index--; index >= 0; index--) { - cable = &edev->cables[index]; - kfree(cable->attr_g.name); - } - ret = -ENOMEM; - - goto err_alloc_cables; - } - - cable->edev = edev; - cable->cable_index = index; - cable->attrs[0] = &cable->attr_name.attr; - cable->attrs[1] = &cable->attr_state.attr; - cable->attrs[2] = NULL; - cable->attr_g.name = str; - cable->attr_g.attrs = cable->attrs; - - sysfs_attr_init(&cable->attr_name.attr); - cable->attr_name.attr.name = "name"; - cable->attr_name.attr.mode = 0444; - cable->attr_name.show = cable_name_show; - - sysfs_attr_init(&cable->attr_state.attr); - cable->attr_state.attr.name = "state"; - cable->attr_state.attr.mode = 0444; - cable->attr_state.show = cable_state_show; - } - } + ret = extcon_alloc_cables(edev); + if (ret < 0) + goto err_alloc_cables; if (edev->max_supported && edev->mutually_exclusive) { char *name; @@ -1279,10 +1298,10 @@ err_alloc_groups: err_muex: for (index = 0; index < edev->max_supported; index++) kfree(edev->cables[index].attr_g.name); -err_alloc_cables: if (edev->max_supported) kfree(edev->cables); -err_sysfs_alloc: +err_alloc_cables: + return ret; } EXPORT_SYMBOL_GPL(extcon_dev_register); -- cgit v1.2.3 From 3e70a014abcdf7e795ee7f446dde4160dfe354a4 Mon Sep 17 00:00:00 2001 From: Bumwoo Lee Date: Mon, 20 Mar 2023 12:19:38 +0900 Subject: extcon: Add extcon_alloc_muex to simplify extcon register function The mutual exclusive part is functionalized from extcon_dev_register. Signed-off-by: Bumwoo Lee Acked-by: MyungJoo Ham --- drivers/extcon/extcon.c | 109 +++++++++++++++++++++++++++--------------------- 1 file changed, 61 insertions(+), 48 deletions(-) (limited to 'drivers/extcon/extcon.c') diff --git a/drivers/extcon/extcon.c b/drivers/extcon/extcon.c index 72591af2fec2..3188b0e8c6d9 100644 --- a/drivers/extcon/extcon.c +++ b/drivers/extcon/extcon.c @@ -1130,6 +1130,63 @@ static int extcon_alloc_cables(struct extcon_dev *edev) return 0; } +/** + * extcon_alloc_muex() - alloc the mutual exclusive for extcon device + * @edev: extcon device + * + * Returns 0 if success or error number if fail. + */ +static int extcon_alloc_muex(struct extcon_dev *edev) +{ + char *name; + int index; + + if (!edev) + return -EINVAL; + + if (!(edev->max_supported && edev->mutually_exclusive)) + return 0; + + /* Count the size of mutually_exclusive array */ + for (index = 0; edev->mutually_exclusive[index]; index++) + ; + + edev->attrs_muex = kcalloc(index + 1, + sizeof(struct attribute *), + GFP_KERNEL); + if (!edev->attrs_muex) + return -ENOMEM; + + edev->d_attrs_muex = kcalloc(index, + sizeof(struct device_attribute), + GFP_KERNEL); + if (!edev->d_attrs_muex) { + kfree(edev->attrs_muex); + return -ENOMEM; + } + + for (index = 0; edev->mutually_exclusive[index]; index++) { + name = kasprintf(GFP_KERNEL, "0x%x", + edev->mutually_exclusive[index]); + if (!name) { + for (index--; index >= 0; index--) + kfree(edev->d_attrs_muex[index].attr.name); + + kfree(edev->d_attrs_muex); + kfree(edev->attrs_muex); + return -ENOMEM; + } + sysfs_attr_init(&edev->d_attrs_muex[index].attr); + edev->d_attrs_muex[index].attr.name = name; + edev->d_attrs_muex[index].attr.mode = 0000; + edev->attrs_muex[index] = &edev->d_attrs_muex[index].attr; + } + edev->attr_g_muex.name = muex_name; + edev->attr_g_muex.attrs = edev->attrs_muex; + + return 0; +} + /** * extcon_dev_register() - Register an new extcon device * @edev: the extcon device to be registered @@ -1181,53 +1238,9 @@ int extcon_dev_register(struct extcon_dev *edev) if (ret < 0) goto err_alloc_cables; - if (edev->max_supported && edev->mutually_exclusive) { - char *name; - - /* Count the size of mutually_exclusive array */ - for (index = 0; edev->mutually_exclusive[index]; index++) - ; - - edev->attrs_muex = kcalloc(index + 1, - sizeof(struct attribute *), - GFP_KERNEL); - if (!edev->attrs_muex) { - ret = -ENOMEM; - goto err_muex; - } - - edev->d_attrs_muex = kcalloc(index, - sizeof(struct device_attribute), - GFP_KERNEL); - if (!edev->d_attrs_muex) { - ret = -ENOMEM; - kfree(edev->attrs_muex); - goto err_muex; - } - - for (index = 0; edev->mutually_exclusive[index]; index++) { - name = kasprintf(GFP_KERNEL, "0x%x", - edev->mutually_exclusive[index]); - if (!name) { - for (index--; index >= 0; index--) { - kfree(edev->d_attrs_muex[index].attr. - name); - } - kfree(edev->d_attrs_muex); - kfree(edev->attrs_muex); - ret = -ENOMEM; - goto err_muex; - } - sysfs_attr_init(&edev->d_attrs_muex[index].attr); - edev->d_attrs_muex[index].attr.name = name; - edev->d_attrs_muex[index].attr.mode = 0000; - edev->attrs_muex[index] = &edev->d_attrs_muex[index] - .attr; - } - edev->attr_g_muex.name = muex_name; - edev->attr_g_muex.attrs = edev->attrs_muex; - - } + ret = extcon_alloc_muex(edev); + if (ret < 0) + goto err_alloc_muex; if (edev->max_supported) { edev->extcon_dev_type.groups = @@ -1295,7 +1308,7 @@ err_alloc_groups: kfree(edev->d_attrs_muex); kfree(edev->attrs_muex); } -err_muex: +err_alloc_muex: for (index = 0; index < edev->max_supported; index++) kfree(edev->cables[index].attr_g.name); if (edev->max_supported) -- cgit v1.2.3 From 04151575c507e7ea3aa6dee73ff9970d699641d3 Mon Sep 17 00:00:00 2001 From: Bumwoo Lee Date: Mon, 20 Mar 2023 12:19:39 +0900 Subject: extcon: Add extcon_alloc_groups to simplify extcon register function The alloc groups is functionalized from extcon_dev_register. Signed-off-by: Bumwoo Lee Acked-by: MyungJoo Ham --- drivers/extcon/extcon.c | 61 +++++++++++++++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 22 deletions(-) (limited to 'drivers/extcon/extcon.c') diff --git a/drivers/extcon/extcon.c b/drivers/extcon/extcon.c index 3188b0e8c6d9..3997b39680b7 100644 --- a/drivers/extcon/extcon.c +++ b/drivers/extcon/extcon.c @@ -1187,6 +1187,42 @@ static int extcon_alloc_muex(struct extcon_dev *edev) return 0; } +/** + * extcon_alloc_groups() - alloc the groups for extcon device + * @edev: extcon device + * + * Returns 0 if success or error number if fail. + */ +static int extcon_alloc_groups(struct extcon_dev *edev) +{ + int index; + + if (!edev) + return -EINVAL; + + if (!edev->max_supported) + return 0; + + edev->extcon_dev_type.groups = kcalloc(edev->max_supported + 2, + sizeof(struct attribute_group *), + GFP_KERNEL); + if (!edev->extcon_dev_type.groups) + return -ENOMEM; + + edev->extcon_dev_type.name = dev_name(&edev->dev); + edev->extcon_dev_type.release = dummy_sysfs_dev_release; + + for (index = 0; index < edev->max_supported; index++) + edev->extcon_dev_type.groups[index] = &edev->cables[index].attr_g; + + if (edev->mutually_exclusive) + edev->extcon_dev_type.groups[index] = &edev->attr_g_muex; + + edev->dev.type = &edev->extcon_dev_type; + + return 0; +} + /** * extcon_dev_register() - Register an new extcon device * @edev: the extcon device to be registered @@ -1242,28 +1278,9 @@ int extcon_dev_register(struct extcon_dev *edev) if (ret < 0) goto err_alloc_muex; - if (edev->max_supported) { - edev->extcon_dev_type.groups = - kcalloc(edev->max_supported + 2, - sizeof(struct attribute_group *), - GFP_KERNEL); - if (!edev->extcon_dev_type.groups) { - ret = -ENOMEM; - goto err_alloc_groups; - } - - edev->extcon_dev_type.name = dev_name(&edev->dev); - edev->extcon_dev_type.release = dummy_sysfs_dev_release; - - for (index = 0; index < edev->max_supported; index++) - edev->extcon_dev_type.groups[index] = - &edev->cables[index].attr_g; - if (edev->mutually_exclusive) - edev->extcon_dev_type.groups[index] = - &edev->attr_g_muex; - - edev->dev.type = &edev->extcon_dev_type; - } + ret = extcon_alloc_groups(edev); + if (ret < 0) + goto err_alloc_groups; spin_lock_init(&edev->lock); if (edev->max_supported) { -- cgit v1.2.3 From 7e77e0b7a9f4cdf91cb0950749b40c840ea63efc Mon Sep 17 00:00:00 2001 From: Andy Shevchenko Date: Wed, 22 Mar 2023 16:39:52 +0200 Subject: extcon: Fix kernel doc of property fields to avoid warnings Kernel documentation has to be synchronized with a code, otherwise the validator is not happy: Function parameter or member 'usb_propval' not described in 'extcon_cable' Function parameter or member 'chg_propval' not described in 'extcon_cable' Function parameter or member 'jack_propval' not described in 'extcon_cable' Function parameter or member 'disp_propval' not described in 'extcon_cable' Describe the fields added in the past. Fixes: 067c1652e7a7 ("extcon: Add the support for extcon property according to extcon type") Signed-off-by: Andy Shevchenko Signed-off-by: Chanwoo Choi --- drivers/extcon/extcon.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'drivers/extcon/extcon.c') diff --git a/drivers/extcon/extcon.c b/drivers/extcon/extcon.c index 3997b39680b7..1bc2639704c2 100644 --- a/drivers/extcon/extcon.c +++ b/drivers/extcon/extcon.c @@ -206,6 +206,10 @@ static const struct __extcon_info { * @attr_name: "name" sysfs entry * @attr_state: "state" sysfs entry * @attrs: the array pointing to attr_name and attr_state for attr_g + * @usb_propval: the array of USB connector properties + * @chg_propval: the array of charger connector properties + * @jack_propval: the array of jack connector properties + * @disp_propval: the array of display connector properties */ struct extcon_cable { struct extcon_dev *edev; -- cgit v1.2.3 From 73346b9965ebda2feb7fef8629e9b28baee820e3 Mon Sep 17 00:00:00 2001 From: Andy Shevchenko Date: Wed, 22 Mar 2023 16:39:53 +0200 Subject: extcon: Fix kernel doc of property capability fields to avoid warnings Kernel documentation has to be synchronized with a code, otherwise the validator is not happy: Function parameter or member 'usb_bits' not described in 'extcon_cable' Function parameter or member 'chg_bits' not described in 'extcon_cable' Function parameter or member 'jack_bits' not described in 'extcon_cable' Function parameter or member 'disp_bits' not described in 'extcon_cable' Describe the fields added in the past. Fixes: ceaa98f442cf ("extcon: Add the support for the capability of each property") Signed-off-by: Andy Shevchenko Signed-off-by: Chanwoo Choi --- drivers/extcon/extcon.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'drivers/extcon/extcon.c') diff --git a/drivers/extcon/extcon.c b/drivers/extcon/extcon.c index 1bc2639704c2..79006ab5334b 100644 --- a/drivers/extcon/extcon.c +++ b/drivers/extcon/extcon.c @@ -210,6 +210,10 @@ static const struct __extcon_info { * @chg_propval: the array of charger connector properties * @jack_propval: the array of jack connector properties * @disp_propval: the array of display connector properties + * @usb_bits: the bit array of the USB connector property capabilities + * @chg_bits: the bit array of the charger connector property capabilities + * @jack_bits: the bit array of the jack connector property capabilities + * @disp_bits: the bit array of the display connector property capabilities */ struct extcon_cable { struct extcon_dev *edev; -- cgit v1.2.3 From 6e4e8670c03b4fcac54bca8b62e5465182caeb26 Mon Sep 17 00:00:00 2001 From: Andy Shevchenko Date: Wed, 22 Mar 2023 16:39:54 +0200 Subject: extcon: Use DECLARE_BITMAP() to declare bit arrays Bit arrays has a specific type helper for the declaration. Use it instead of homegronw equivalent. Signed-off-by: Andy Shevchenko Signed-off-by: Chanwoo Choi --- drivers/extcon/extcon.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'drivers/extcon/extcon.c') diff --git a/drivers/extcon/extcon.c b/drivers/extcon/extcon.c index 79006ab5334b..70e9755ba2bc 100644 --- a/drivers/extcon/extcon.c +++ b/drivers/extcon/extcon.c @@ -230,10 +230,10 @@ struct extcon_cable { union extcon_property_value jack_propval[EXTCON_PROP_JACK_CNT]; union extcon_property_value disp_propval[EXTCON_PROP_DISP_CNT]; - unsigned long usb_bits[BITS_TO_LONGS(EXTCON_PROP_USB_CNT)]; - unsigned long chg_bits[BITS_TO_LONGS(EXTCON_PROP_CHG_CNT)]; - unsigned long jack_bits[BITS_TO_LONGS(EXTCON_PROP_JACK_CNT)]; - unsigned long disp_bits[BITS_TO_LONGS(EXTCON_PROP_DISP_CNT)]; + DECLARE_BITMAP(usb_bits, EXTCON_PROP_USB_CNT); + DECLARE_BITMAP(chg_bits, EXTCON_PROP_CHG_CNT); + DECLARE_BITMAP(jack_bits, EXTCON_PROP_JACK_CNT); + DECLARE_BITMAP(disp_bits, EXTCON_PROP_DISP_CNT); }; static struct class *extcon_class; -- cgit v1.2.3 From 6ee0a22e8694074877550bf4284cc4085f86dd4f Mon Sep 17 00:00:00 2001 From: Andy Shevchenko Date: Wed, 22 Mar 2023 16:39:55 +0200 Subject: extcon: Use sysfs_emit() to instead of sprintf() Follow the advice of the Documentation/filesystems/sysfs.rst that show() should only use sysfs_emit() or sysfs_emit_at() when formatting the value to be returned to user space. Signed-off-by: Andy Shevchenko Signed-off-by: Chanwoo Choi --- drivers/extcon/extcon.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'drivers/extcon/extcon.c') diff --git a/drivers/extcon/extcon.c b/drivers/extcon/extcon.c index 70e9755ba2bc..ac84f4aafc69 100644 --- a/drivers/extcon/extcon.c +++ b/drivers/extcon/extcon.c @@ -370,12 +370,12 @@ static ssize_t state_show(struct device *dev, struct device_attribute *attr, struct extcon_dev *edev = dev_get_drvdata(dev); if (edev->max_supported == 0) - return sprintf(buf, "%u\n", edev->state); + return sysfs_emit(buf, "%u\n", edev->state); for (i = 0; i < edev->max_supported; i++) { - count += sprintf(buf + count, "%s=%d\n", - extcon_info[edev->supported_cable[i]].name, - !!(edev->state & BIT(i))); + count += sysfs_emit_at(buf, count, "%s=%d\n", + extcon_info[edev->supported_cable[i]].name, + !!(edev->state & BIT(i))); } return count; @@ -387,7 +387,7 @@ static ssize_t name_show(struct device *dev, struct device_attribute *attr, { struct extcon_dev *edev = dev_get_drvdata(dev); - return sprintf(buf, "%s\n", edev->name); + return sysfs_emit(buf, "%s\n", edev->name); } static DEVICE_ATTR_RO(name); @@ -398,8 +398,8 @@ static ssize_t cable_name_show(struct device *dev, attr_name); int i = cable->cable_index; - return sprintf(buf, "%s\n", - extcon_info[cable->edev->supported_cable[i]].name); + return sysfs_emit(buf, "%s\n", + extcon_info[cable->edev->supported_cable[i]].name); } static ssize_t cable_state_show(struct device *dev, @@ -410,8 +410,8 @@ static ssize_t cable_state_show(struct device *dev, int i = cable->cable_index; - return sprintf(buf, "%d\n", - extcon_get_state(cable->edev, cable->edev->supported_cable[i])); + return sysfs_emit(buf, "%d\n", + extcon_get_state(cable->edev, cable->edev->supported_cable[i])); } /** -- cgit v1.2.3 From 0146f56b91a8ad287e7c94ea340b95a7040d29cf Mon Sep 17 00:00:00 2001 From: Andy Shevchenko Date: Wed, 22 Mar 2023 16:40:00 +0200 Subject: extcon: Use device_match_of_node() helper Instead of open coding, use device_match_of_node() helper. Signed-off-by: Andy Shevchenko Signed-off-by: Chanwoo Choi --- drivers/extcon/extcon.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'drivers/extcon/extcon.c') diff --git a/drivers/extcon/extcon.c b/drivers/extcon/extcon.c index ac84f4aafc69..588c552b9525 100644 --- a/drivers/extcon/extcon.c +++ b/drivers/extcon/extcon.c @@ -1405,7 +1405,7 @@ struct extcon_dev *extcon_find_edev_by_node(struct device_node *node) mutex_lock(&extcon_dev_list_lock); list_for_each_entry(edev, &extcon_dev_list, entry) - if (edev->dev.parent && edev->dev.parent->of_node == node) + if (edev->dev.parent && device_match_of_node(edev->dev.parent, node)) goto out; edev = ERR_PTR(-EPROBE_DEFER); out: -- cgit v1.2.3 From 9b4aea51cbcaefacaac655392f360bb3929ab63d Mon Sep 17 00:00:00 2001 From: Andy Shevchenko Date: Wed, 22 Mar 2023 16:40:01 +0200 Subject: extcon: Use dev_of_node(dev) instead of dev->of_node The dev_of_node function should be preferred. In the result we may drop unneeded NULL check of the pointer to the device object. Signed-off-by: Andy Shevchenko Signed-off-by: Chanwoo Choi --- drivers/extcon/extcon.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) (limited to 'drivers/extcon/extcon.c') diff --git a/drivers/extcon/extcon.c b/drivers/extcon/extcon.c index 588c552b9525..89b3946b3123 100644 --- a/drivers/extcon/extcon.c +++ b/drivers/extcon/extcon.c @@ -1423,21 +1423,17 @@ out: */ struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index) { - struct device_node *node; + struct device_node *node, *np = dev_of_node(dev); struct extcon_dev *edev; - if (!dev) - return ERR_PTR(-EINVAL); - - if (!dev->of_node) { + if (!np) { dev_dbg(dev, "device does not have a device node entry\n"); return ERR_PTR(-EINVAL); } - node = of_parse_phandle(dev->of_node, "extcon", index); + node = of_parse_phandle(np, "extcon", index); if (!node) { - dev_dbg(dev, "failed to get phandle in %pOF node\n", - dev->of_node); + dev_dbg(dev, "failed to get phandle in %pOF node\n", np); return ERR_PTR(-ENODEV); } -- cgit v1.2.3 From 566825a31f65da111270abac35662502706e7c8a Mon Sep 17 00:00:00 2001 From: Andy Shevchenko Date: Wed, 22 Mar 2023 16:40:02 +0200 Subject: extcon: Remove dup device name in the message and unneeded error check The device name is already printed with dev_err(), no need to repeat. The device pointer itself is not supposed to be an error point, drop that check. Signed-off-by: Andy Shevchenko Signed-off-by: Chanwoo Choi --- drivers/extcon/extcon.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'drivers/extcon/extcon.c') diff --git a/drivers/extcon/extcon.c b/drivers/extcon/extcon.c index 89b3946b3123..47819c5144d5 100644 --- a/drivers/extcon/extcon.c +++ b/drivers/extcon/extcon.c @@ -1362,9 +1362,8 @@ void extcon_dev_unregister(struct extcon_dev *edev) list_del(&edev->entry); mutex_unlock(&extcon_dev_list_lock); - if (IS_ERR_OR_NULL(get_device(&edev->dev))) { - dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n", - dev_name(&edev->dev)); + if (!get_device(&edev->dev)) { + dev_err(&edev->dev, "Failed to unregister extcon_dev\n"); return; } -- cgit v1.2.3 From 7bba9e81a6fbf00daa4063c41da6b250d339f43b Mon Sep 17 00:00:00 2001 From: Andy Shevchenko Date: Tue, 11 Apr 2023 14:48:22 +0300 Subject: extcon: Use unique number for the extcon device ID The use of atomic variable is still racy when we do not control which device has been unregistered and there is a (theoretical) possibility of the overflow that may cause a duplicate extcon device ID number to be allocated next time a device is registered. Replace above mentioned approach by using IDA framework. Signed-off-by: Andy Shevchenko Signed-off-by: Chanwoo Choi --- drivers/extcon/extcon.c | 16 +++++++++++++--- drivers/extcon/extcon.h | 2 ++ 2 files changed, 15 insertions(+), 3 deletions(-) (limited to 'drivers/extcon/extcon.c') diff --git a/drivers/extcon/extcon.c b/drivers/extcon/extcon.c index 47819c5144d5..5da1cc60582a 100644 --- a/drivers/extcon/extcon.c +++ b/drivers/extcon/extcon.c @@ -16,6 +16,7 @@ #include #include +#include #include #include #include @@ -238,6 +239,7 @@ struct extcon_cable { static struct class *extcon_class; +static DEFINE_IDA(extcon_dev_ids); static LIST_HEAD(extcon_dev_list); static DEFINE_MUTEX(extcon_dev_list_lock); @@ -1248,7 +1250,6 @@ static int extcon_alloc_groups(struct extcon_dev *edev) int extcon_dev_register(struct extcon_dev *edev) { int ret, index = 0; - static atomic_t edev_no = ATOMIC_INIT(-1); ret = create_extcon_class(); if (ret < 0) @@ -1275,8 +1276,14 @@ int extcon_dev_register(struct extcon_dev *edev) "extcon device name is null\n"); return -EINVAL; } - dev_set_name(&edev->dev, "extcon%lu", - (unsigned long)atomic_inc_return(&edev_no)); + + ret = ida_alloc(&extcon_dev_ids, GFP_KERNEL); + if (ret < 0) + return ret; + + edev->id = ret; + + dev_set_name(&edev->dev, "extcon%d", edev->id); ret = extcon_alloc_cables(edev); if (ret < 0) @@ -1339,6 +1346,7 @@ err_alloc_muex: if (edev->max_supported) kfree(edev->cables); err_alloc_cables: + ida_free(&extcon_dev_ids, edev->id); return ret; } @@ -1367,6 +1375,8 @@ void extcon_dev_unregister(struct extcon_dev *edev) return; } + ida_free(&extcon_dev_ids, edev->id); + device_unregister(&edev->dev); if (edev->mutually_exclusive && edev->max_supported) { diff --git a/drivers/extcon/extcon.h b/drivers/extcon/extcon.h index 15616446140d..946182687786 100644 --- a/drivers/extcon/extcon.h +++ b/drivers/extcon/extcon.h @@ -20,6 +20,7 @@ * {0x3, 0x6, 0x5, 0}. If it is {0xFFFFFFFF, 0}, there * can be no simultaneous connections. * @dev: Device of this extcon. + * @id: Unique device ID of this extcon. * @state: Attach/detach state of this extcon. Do not provide at * register-time. * @nh_all: Notifier for the state change events for all supported @@ -46,6 +47,7 @@ struct extcon_dev { /* Internal data. Please do not set. */ struct device dev; + unsigned int id; struct raw_notifier_head nh_all; struct raw_notifier_head *nh; struct list_head entry; -- cgit v1.2.3 From ef753fb4e86607afc6228d8632705122bc67f29a Mon Sep 17 00:00:00 2001 From: Andy Shevchenko Date: Tue, 11 Apr 2023 14:48:23 +0300 Subject: extcon: Use sizeof(*pointer) instead of sizeof(type) It is preferred to use sizeof(*pointer) instead of sizeof(type). The type of the variable can change and one needs not change the former (unlike the latter). No functional change intended. Signed-off-by: Andy Shevchenko Signed-off-by: Chanwoo Choi --- drivers/extcon/extcon.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) (limited to 'drivers/extcon/extcon.c') diff --git a/drivers/extcon/extcon.c b/drivers/extcon/extcon.c index 5da1cc60582a..76dc41e8f250 100644 --- a/drivers/extcon/extcon.c +++ b/drivers/extcon/extcon.c @@ -1098,8 +1098,7 @@ static int extcon_alloc_cables(struct extcon_dev *edev) if (!edev->max_supported) return 0; - edev->cables = kcalloc(edev->max_supported, - sizeof(struct extcon_cable), + edev->cables = kcalloc(edev->max_supported, sizeof(*edev->cables), GFP_KERNEL); if (!edev->cables) return -ENOMEM; @@ -1161,14 +1160,12 @@ static int extcon_alloc_muex(struct extcon_dev *edev) for (index = 0; edev->mutually_exclusive[index]; index++) ; - edev->attrs_muex = kcalloc(index + 1, - sizeof(struct attribute *), + edev->attrs_muex = kcalloc(index + 1, sizeof(*edev->attrs_muex), GFP_KERNEL); if (!edev->attrs_muex) return -ENOMEM; - edev->d_attrs_muex = kcalloc(index, - sizeof(struct device_attribute), + edev->d_attrs_muex = kcalloc(index, sizeof(*edev->d_attrs_muex), GFP_KERNEL); if (!edev->d_attrs_muex) { kfree(edev->attrs_muex); @@ -1214,8 +1211,8 @@ static int extcon_alloc_groups(struct extcon_dev *edev) return 0; edev->extcon_dev_type.groups = kcalloc(edev->max_supported + 2, - sizeof(struct attribute_group *), - GFP_KERNEL); + sizeof(*edev->extcon_dev_type.groups), + GFP_KERNEL); if (!edev->extcon_dev_type.groups) return -ENOMEM; -- cgit v1.2.3 From 93e60cd5e00e63f29e896453e10c7ede4cd8e882 Mon Sep 17 00:00:00 2001 From: Andy Shevchenko Date: Tue, 11 Apr 2023 14:48:24 +0300 Subject: extcon: Drop unneeded assignments MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In one case the assignment is duplicative, in the other, it's better to move it into the loop — the user of it. Signed-off-by: Andy Shevchenko Acked-by: Chanwoo Choi Signed-off-by: Chanwoo Choi --- drivers/extcon/extcon.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'drivers/extcon/extcon.c') diff --git a/drivers/extcon/extcon.c b/drivers/extcon/extcon.c index 76dc41e8f250..6f7a60d2ed91 100644 --- a/drivers/extcon/extcon.c +++ b/drivers/extcon/extcon.c @@ -245,7 +245,7 @@ static DEFINE_MUTEX(extcon_dev_list_lock); static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state) { - int i = 0; + int i; if (!edev->mutually_exclusive) return 0; @@ -1246,7 +1246,7 @@ static int extcon_alloc_groups(struct extcon_dev *edev) */ int extcon_dev_register(struct extcon_dev *edev) { - int ret, index = 0; + int ret, index; ret = create_extcon_class(); if (ret < 0) @@ -1255,7 +1255,7 @@ int extcon_dev_register(struct extcon_dev *edev) if (!edev || !edev->supported_cable) return -EINVAL; - for (; edev->supported_cable[index] != EXTCON_NONE; index++); + for (index = 0; edev->supported_cable[index] != EXTCON_NONE; index++); edev->max_supported = index; if (index > SUPPORTED_CABLE_MAX) { -- cgit v1.2.3