2019-01-22 01:05:50 +07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
|
|
|
/* Copyright (C) 2016 National Instruments Corp. */
|
2016-10-17 22:49:55 +07:00
|
|
|
#include <linux/leds.h>
|
|
|
|
#include <linux/phy.h>
|
2017-01-25 17:39:49 +07:00
|
|
|
#include <linux/phy_led_triggers.h>
|
2016-10-17 22:49:55 +07:00
|
|
|
#include <linux/netdevice.h>
|
|
|
|
|
|
|
|
static struct phy_led_trigger *phy_speed_to_led_trigger(struct phy_device *phy,
|
|
|
|
unsigned int speed)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < phy->phy_num_led_triggers; i++) {
|
|
|
|
if (phy->phy_led_triggers[i].speed == speed)
|
|
|
|
return &phy->phy_led_triggers[i];
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-11-02 06:48:45 +07:00
|
|
|
static void phy_led_trigger_no_link(struct phy_device *phy)
|
|
|
|
{
|
|
|
|
if (phy->last_triggered) {
|
|
|
|
led_trigger_event(&phy->last_triggered->trigger, LED_OFF);
|
2017-11-02 06:49:18 +07:00
|
|
|
led_trigger_event(&phy->led_link_trigger->trigger, LED_OFF);
|
2017-11-02 06:48:45 +07:00
|
|
|
phy->last_triggered = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-17 22:49:55 +07:00
|
|
|
void phy_led_trigger_change_speed(struct phy_device *phy)
|
|
|
|
{
|
|
|
|
struct phy_led_trigger *plt;
|
|
|
|
|
|
|
|
if (!phy->link)
|
2017-11-02 06:48:45 +07:00
|
|
|
return phy_led_trigger_no_link(phy);
|
2016-10-17 22:49:55 +07:00
|
|
|
|
|
|
|
if (phy->speed == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
plt = phy_speed_to_led_trigger(phy, phy->speed);
|
|
|
|
if (!plt) {
|
|
|
|
netdev_alert(phy->attached_dev,
|
|
|
|
"No phy led trigger registered for speed(%d)\n",
|
|
|
|
phy->speed);
|
2017-11-02 06:48:45 +07:00
|
|
|
return phy_led_trigger_no_link(phy);
|
2016-10-17 22:49:55 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (plt != phy->last_triggered) {
|
2017-11-02 06:49:18 +07:00
|
|
|
if (!phy->last_triggered)
|
|
|
|
led_trigger_event(&phy->led_link_trigger->trigger,
|
|
|
|
LED_FULL);
|
|
|
|
|
2016-10-17 22:49:55 +07:00
|
|
|
led_trigger_event(&phy->last_triggered->trigger, LED_OFF);
|
|
|
|
led_trigger_event(&plt->trigger, LED_FULL);
|
|
|
|
phy->last_triggered = plt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(phy_led_trigger_change_speed);
|
|
|
|
|
2017-11-02 06:49:18 +07:00
|
|
|
static void phy_led_trigger_format_name(struct phy_device *phy, char *buf,
|
2018-11-10 01:48:03 +07:00
|
|
|
size_t size, const char *suffix)
|
2017-11-02 06:49:18 +07:00
|
|
|
{
|
|
|
|
snprintf(buf, size, PHY_ID_FMT ":%s",
|
|
|
|
phy->mdio.bus->id, phy->mdio.addr, suffix);
|
|
|
|
}
|
|
|
|
|
2016-10-17 22:49:55 +07:00
|
|
|
static int phy_led_trigger_register(struct phy_device *phy,
|
|
|
|
struct phy_led_trigger *plt,
|
|
|
|
unsigned int speed)
|
|
|
|
{
|
|
|
|
plt->speed = speed;
|
2017-11-02 06:49:18 +07:00
|
|
|
phy_led_trigger_format_name(phy, plt->name, sizeof(plt->name),
|
2018-11-10 01:48:03 +07:00
|
|
|
phy_speed_to_str(speed));
|
2016-10-17 22:49:55 +07:00
|
|
|
plt->trigger.name = plt->name;
|
|
|
|
|
|
|
|
return led_trigger_register(&plt->trigger);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void phy_led_trigger_unregister(struct phy_led_trigger *plt)
|
|
|
|
{
|
|
|
|
led_trigger_unregister(&plt->trigger);
|
|
|
|
}
|
|
|
|
|
|
|
|
int phy_led_triggers_register(struct phy_device *phy)
|
|
|
|
{
|
|
|
|
int i, err;
|
|
|
|
unsigned int speeds[50];
|
|
|
|
|
|
|
|
phy->phy_num_led_triggers = phy_supported_speeds(phy, speeds,
|
|
|
|
ARRAY_SIZE(speeds));
|
|
|
|
if (!phy->phy_num_led_triggers)
|
|
|
|
return 0;
|
|
|
|
|
2017-11-02 06:49:18 +07:00
|
|
|
phy->led_link_trigger = devm_kzalloc(&phy->mdio.dev,
|
|
|
|
sizeof(*phy->led_link_trigger),
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (!phy->led_link_trigger) {
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto out_clear;
|
|
|
|
}
|
|
|
|
|
|
|
|
phy_led_trigger_format_name(phy, phy->led_link_trigger->name,
|
|
|
|
sizeof(phy->led_link_trigger->name),
|
|
|
|
"link");
|
|
|
|
phy->led_link_trigger->trigger.name = phy->led_link_trigger->name;
|
|
|
|
|
|
|
|
err = led_trigger_register(&phy->led_link_trigger->trigger);
|
|
|
|
if (err)
|
|
|
|
goto out_free_link;
|
|
|
|
|
treewide: devm_kzalloc() -> devm_kcalloc()
The devm_kzalloc() function has a 2-factor argument form, devm_kcalloc().
This patch replaces cases of:
devm_kzalloc(handle, a * b, gfp)
with:
devm_kcalloc(handle, a * b, gfp)
as well as handling cases of:
devm_kzalloc(handle, a * b * c, gfp)
with:
devm_kzalloc(handle, array3_size(a, b, c), gfp)
as it's slightly less ugly than:
devm_kcalloc(handle, array_size(a, b), c, gfp)
This does, however, attempt to ignore constant size factors like:
devm_kzalloc(handle, 4 * 1024, gfp)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
Some manual whitespace fixes were needed in this patch, as Coccinelle
really liked to write "=devm_kcalloc..." instead of "= devm_kcalloc...".
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
expression HANDLE;
type TYPE;
expression THING, E;
@@
(
devm_kzalloc(HANDLE,
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
devm_kzalloc(HANDLE,
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression HANDLE;
expression COUNT;
typedef u8;
typedef __u8;
@@
(
devm_kzalloc(HANDLE,
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(char) * COUNT
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
expression HANDLE;
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * (COUNT_ID)
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * COUNT_ID
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * (COUNT_CONST)
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * COUNT_CONST
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * (COUNT_ID)
+ COUNT_ID, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * COUNT_ID
+ COUNT_ID, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * (COUNT_CONST)
+ COUNT_CONST, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * COUNT_CONST
+ COUNT_CONST, sizeof(THING)
, ...)
)
// 2-factor product, only identifiers.
@@
expression HANDLE;
identifier SIZE, COUNT;
@@
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- SIZE * COUNT
+ COUNT, SIZE
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression HANDLE;
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
devm_kzalloc(HANDLE,
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression HANDLE;
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
expression HANDLE;
identifier STRIDE, SIZE, COUNT;
@@
(
devm_kzalloc(HANDLE,
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products,
// when they're not all constants...
@@
expression HANDLE;
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
devm_kzalloc(HANDLE, C1 * C2 * C3, ...)
|
devm_kzalloc(HANDLE,
- (E1) * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
|
devm_kzalloc(HANDLE,
- (E1) * (E2) * E3
+ array3_size(E1, E2, E3)
, ...)
|
devm_kzalloc(HANDLE,
- (E1) * (E2) * (E3)
+ array3_size(E1, E2, E3)
, ...)
|
devm_kzalloc(HANDLE,
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants,
// keeping sizeof() as the second factor argument.
@@
expression HANDLE;
expression THING, E1, E2;
type TYPE;
constant C1, C2, C3;
@@
(
devm_kzalloc(HANDLE, sizeof(THING) * C2, ...)
|
devm_kzalloc(HANDLE, sizeof(TYPE) * C2, ...)
|
devm_kzalloc(HANDLE, C1 * C2 * C3, ...)
|
devm_kzalloc(HANDLE, C1 * C2, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * (E2)
+ E2, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * E2
+ E2, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * (E2)
+ E2, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * E2
+ E2, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- (E1) * E2
+ E1, E2
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- (E1) * (E2)
+ E1, E2
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- E1 * E2
+ E1, E2
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-13 04:07:58 +07:00
|
|
|
phy->phy_led_triggers = devm_kcalloc(&phy->mdio.dev,
|
|
|
|
phy->phy_num_led_triggers,
|
|
|
|
sizeof(struct phy_led_trigger),
|
2016-10-17 22:49:55 +07:00
|
|
|
GFP_KERNEL);
|
2017-01-25 17:39:48 +07:00
|
|
|
if (!phy->phy_led_triggers) {
|
|
|
|
err = -ENOMEM;
|
2017-11-02 06:49:18 +07:00
|
|
|
goto out_unreg_link;
|
2017-01-25 17:39:48 +07:00
|
|
|
}
|
2016-10-17 22:49:55 +07:00
|
|
|
|
|
|
|
for (i = 0; i < phy->phy_num_led_triggers; i++) {
|
|
|
|
err = phy_led_trigger_register(phy, &phy->phy_led_triggers[i],
|
|
|
|
speeds[i]);
|
|
|
|
if (err)
|
|
|
|
goto out_unreg;
|
|
|
|
}
|
|
|
|
|
|
|
|
phy->last_triggered = NULL;
|
|
|
|
phy_led_trigger_change_speed(phy);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
out_unreg:
|
|
|
|
while (i--)
|
|
|
|
phy_led_trigger_unregister(&phy->phy_led_triggers[i]);
|
|
|
|
devm_kfree(&phy->mdio.dev, phy->phy_led_triggers);
|
2017-11-02 06:49:18 +07:00
|
|
|
out_unreg_link:
|
|
|
|
phy_led_trigger_unregister(phy->led_link_trigger);
|
|
|
|
out_free_link:
|
|
|
|
devm_kfree(&phy->mdio.dev, phy->led_link_trigger);
|
|
|
|
phy->led_link_trigger = NULL;
|
2017-01-25 17:39:48 +07:00
|
|
|
out_clear:
|
|
|
|
phy->phy_num_led_triggers = 0;
|
2016-10-17 22:49:55 +07:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(phy_led_triggers_register);
|
|
|
|
|
|
|
|
void phy_led_triggers_unregister(struct phy_device *phy)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < phy->phy_num_led_triggers; i++)
|
|
|
|
phy_led_trigger_unregister(&phy->phy_led_triggers[i]);
|
2017-11-02 06:49:18 +07:00
|
|
|
|
|
|
|
if (phy->led_link_trigger)
|
|
|
|
phy_led_trigger_unregister(phy->led_link_trigger);
|
2016-10-17 22:49:55 +07:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(phy_led_triggers_unregister);
|