Skip to content

Commit 1ac63e3

Browse files
ffainellidavem330
authored andcommitted
net: phy: phylink: Allow setting a custom link state callback
phylink_get_fixed_state() currently consults an optional "link_gpio" GPIO descriptor, expand this mechanism to allow specifying a custom callback. This is necessary to support out of band link notifcation (e.g: from an interrupt within a MMIO register). Signed-off-by: Florian Fainelli <f.fainelli@gmail.com> Reviewed-by: Andrew Lunn <andrew@lunn.ch> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent d38b4af commit 1ac63e3

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

drivers/net/phy/phylink.c

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ struct phylink {
5454
/* The link configuration settings */
5555
struct phylink_link_state link_config;
5656
struct gpio_desc *link_gpio;
57+
void (*get_fixed_state)(struct net_device *dev,
58+
struct phylink_link_state *s);
5759

5860
struct mutex state_mutex;
5961
struct phylink_link_state phy_state;
@@ -350,12 +352,14 @@ static int phylink_get_mac_state(struct phylink *pl, struct phylink_link_state *
350352
}
351353

352354
/* The fixed state is... fixed except for the link state,
353-
* which may be determined by a GPIO.
355+
* which may be determined by a GPIO or a callback.
354356
*/
355357
static void phylink_get_fixed_state(struct phylink *pl, struct phylink_link_state *state)
356358
{
357359
*state = pl->link_config;
358-
if (pl->link_gpio)
360+
if (pl->get_fixed_state)
361+
pl->get_fixed_state(pl->netdev, state);
362+
else if (pl->link_gpio)
359363
state->link = !!gpiod_get_value(pl->link_gpio);
360364
}
361365

@@ -814,6 +818,32 @@ void phylink_disconnect_phy(struct phylink *pl)
814818
}
815819
EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
816820

821+
/**
822+
* phylink_fixed_state_cb() - allow setting a fixed link callback
823+
* @pl: a pointer to a &struct phylink returned from phylink_create()
824+
* @cb: callback to execute to determine the fixed link state.
825+
*
826+
* The MAC driver should call this driver when the state of its link
827+
* can be determined through e.g: an out of band MMIO register.
828+
*/
829+
int phylink_fixed_state_cb(struct phylink *pl,
830+
void (*cb)(struct net_device *dev,
831+
struct phylink_link_state *state))
832+
{
833+
/* It does not make sense to let the link be overriden unless we use
834+
* MLO_AN_FIXED
835+
*/
836+
if (pl->link_an_mode != MLO_AN_FIXED)
837+
return -EINVAL;
838+
839+
mutex_lock(&pl->state_mutex);
840+
pl->get_fixed_state = cb;
841+
mutex_unlock(&pl->state_mutex);
842+
843+
return 0;
844+
}
845+
EXPORT_SYMBOL_GPL(phylink_fixed_state_cb);
846+
817847
/**
818848
* phylink_mac_change() - notify phylink of a change in MAC state
819849
* @pl: a pointer to a &struct phylink returned from phylink_create()

include/linux/phylink.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,9 @@ void phylink_destroy(struct phylink *);
190190
int phylink_connect_phy(struct phylink *, struct phy_device *);
191191
int phylink_of_phy_connect(struct phylink *, struct device_node *, u32 flags);
192192
void phylink_disconnect_phy(struct phylink *);
193+
int phylink_fixed_state_cb(struct phylink *,
194+
void (*cb)(struct net_device *dev,
195+
struct phylink_link_state *));
193196

194197
void phylink_mac_change(struct phylink *, bool up);
195198

0 commit comments

Comments
 (0)