From mboxrd@z Thu Jan 1 00:00:00 1970 From: Joe Hershberger Date: Tue, 3 Mar 2015 20:41:00 -0600 Subject: [U-Boot] [PATCH v5 06/27] net: Refactor in preparation for driver model In-Reply-To: <1425436881-10323-1-git-send-email-joe.hershberger@ni.com> References: <1424822552-4366-1-git-send-email-joe.hershberger@ni.com> <1425436881-10323-1-git-send-email-joe.hershberger@ni.com> Message-ID: <1425436881-10323-7-git-send-email-joe.hershberger@ni.com> List-Id: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: u-boot@lists.denx.de Move some things around and organize things so that the driver model implementation will fit in more easily. Signed-off-by: Joe Hershberger Reviewed-by: Simon Glass --- Changes in v5: -Fixed warning from missing declaration Changes in v4: -Fix compile regression in !DM_ETH case Changes in v3: -Move the get_dev_by_* protos to also be !DM_ETH like the impl Changes in v2: None include/net.h | 68 +++++++++++++++++++++++++------------------------- net/eth.c | 79 ++++++++++++++++++++++++++++++++--------------------------- 2 files changed, 78 insertions(+), 69 deletions(-) diff --git a/include/net.h b/include/net.h index b82a29d..4cef00c 100644 --- a/include/net.h +++ b/include/net.h @@ -97,13 +97,9 @@ struct eth_device { void *priv; }; -int eth_initialize(bd_t *bis); /* Initialize network subsystem */ int eth_register(struct eth_device *dev);/* Register network device */ int eth_unregister(struct eth_device *dev);/* Remove network device */ -void eth_try_another(int first_restart); /* Change the device */ -void eth_set_current(void); /* set nterface to ethcur var */ -/* get the current device MAC */ extern struct eth_device *eth_current; static inline __attribute__((always_inline)) @@ -111,7 +107,10 @@ struct eth_device *eth_get_dev(void) { return eth_current; } +struct eth_device *eth_get_dev_by_name(const char *devname); +struct eth_device *eth_get_dev_by_index(int index); /* get dev @ index */ +/* get the current device MAC */ static inline unsigned char *eth_get_ethaddr(void) { if (eth_current) @@ -119,8 +118,37 @@ static inline unsigned char *eth_get_ethaddr(void) return NULL; } -struct eth_device *eth_get_dev_by_name(const char *devname); -struct eth_device *eth_get_dev_by_index(int index); /* get dev @ index */ +/* Set active state */ +static inline __attribute__((always_inline)) int eth_init_state_only(bd_t *bis) +{ + eth_get_dev()->state = ETH_STATE_ACTIVE; + + return 0; +} +/* Set passive state */ +static inline __attribute__((always_inline)) void eth_halt_state_only(void) +{ + eth_get_dev()->state = ETH_STATE_PASSIVE; +} + +/* + * Set the hardware address for an ethernet interface based on 'eth%daddr' + * environment variable (or just 'ethaddr' if eth_number is 0). + * Args: + * base_name - base name for device (normally "eth") + * eth_number - value of %d (0 for first device of this type) + * Returns: + * 0 is success, non-zero is error status from driver. + */ +int eth_write_hwaddr(struct eth_device *dev, const char *base_name, + int eth_number); + +int usb_eth_initialize(bd_t *bi); + +int eth_initialize(bd_t *bis); /* Initialize network subsystem */ +void eth_try_another(int first_restart); /* Change the device */ +void eth_set_current(void); /* set nterface to ethcur var */ + int eth_get_dev_index(void); /* get the device index */ void eth_parse_enetaddr(const char *addr, uchar *enetaddr); int eth_getenv_enetaddr(char *name, uchar *enetaddr); @@ -138,7 +166,6 @@ int eth_setenv_enetaddr(char *name, const uchar *enetaddr); int eth_getenv_enetaddr_by_index(const char *base_name, int index, uchar *enetaddr); -int usb_eth_initialize(bd_t *bi); int eth_init(bd_t *bis); /* Initialize the device */ int eth_send(void *packet, int length); /* Send a packet */ @@ -148,32 +175,7 @@ void (*push_packet)(void *packet, int length); #endif int eth_rx(void); /* Check for received packets */ void eth_halt(void); /* stop SCC */ -char *eth_get_name(void); /* get name of current device */ - -/* Set active state */ -static inline __attribute__((always_inline)) int eth_init_state_only(bd_t *bis) -{ - eth_get_dev()->state = ETH_STATE_ACTIVE; - - return 0; -} -/* Set passive state */ -static inline __attribute__((always_inline)) void eth_halt_state_only(void) -{ - eth_get_dev()->state = ETH_STATE_PASSIVE; -} - -/* - * Set the hardware address for an ethernet interface based on 'eth%daddr' - * environment variable (or just 'ethaddr' if eth_number is 0). - * Args: - * base_name - base name for device (normally "eth") - * eth_number - value of %d (0 for first device of this type) - * Returns: - * 0 is success, non-zero is error status from driver. - */ -int eth_write_hwaddr(struct eth_device *dev, const char *base_name, - int eth_number); +const char *eth_get_name(void); /* get name of current device */ #ifdef CONFIG_MCAST_TFTP int eth_mcast_join(IPaddr_t mcast_addr, u8 join); diff --git a/net/eth.c b/net/eth.c index 65e8c77..84919e0 100644 --- a/net/eth.c +++ b/net/eth.c @@ -55,6 +55,14 @@ static inline int eth_setenv_enetaddr_by_index(const char *base_name, int index, return eth_setenv_enetaddr(enetvar, enetaddr); } +static void eth_env_init(void) +{ + const char *s; + + s = getenv("bootfile"); + if (s != NULL) + copy_filename(BootFile, s, sizeof(BootFile)); +} static int eth_mac_skip(int index) { @@ -64,6 +72,8 @@ static int eth_mac_skip(int index) return ((skip_state = getenv(enetvar)) != NULL); } +static void eth_current_changed(void); + /* * CPU and board-specific Ethernet initializations. Aliased function * signals caller to move on @@ -87,6 +97,11 @@ static unsigned int eth_rcv_current, eth_rcv_last; static struct eth_device *eth_devices; struct eth_device *eth_current; +static void eth_set_current_to_next(void) +{ + eth_current = eth_current->next; +} + struct eth_device *eth_get_dev_by_name(const char *devname) { struct eth_device *dev, *target_dev; @@ -137,22 +152,6 @@ int eth_get_dev_index(void) return eth_current->index; } -static void eth_current_changed(void) -{ - char *act = getenv("ethact"); - /* update current ethernet name */ - if (eth_current) { - if (act == NULL || strcmp(act, eth_current->name) != 0) - setenv("ethact", eth_current->name); - } - /* - * remove the variable completely if there is no active - * interface - */ - else if (act != NULL) - setenv("ethact", NULL); -} - int eth_write_hwaddr(struct eth_device *dev, const char *base_name, int eth_number) { @@ -251,14 +250,6 @@ int eth_unregister(struct eth_device *dev) return 0; } -static void eth_env_init(bd_t *bis) -{ - const char *s; - - if ((s = getenv("bootfile")) != NULL) - copy_filename(BootFile, s, sizeof(BootFile)); -} - int eth_initialize(bd_t *bis) { int num_devices = 0; @@ -274,7 +265,7 @@ int eth_initialize(bd_t *bis) phy_init(); #endif - eth_env_init(bis); + eth_env_init(); /* * If board-specific initialization exists, call it. @@ -479,6 +470,22 @@ int eth_receive(void *packet, int length) } #endif /* CONFIG_API */ +static void eth_current_changed(void) +{ + char *act = getenv("ethact"); + /* update current ethernet name */ + if (eth_get_dev()) { + if (act == NULL || strcmp(act, eth_get_name()) != 0) + setenv("ethact", eth_get_name()); + } + /* + * remove the variable completely if there is no active + * interface + */ + else if (act != NULL) + setenv("ethact", NULL); +} + void eth_try_another(int first_restart) { static struct eth_device *first_failed; @@ -492,17 +499,17 @@ void eth_try_another(int first_restart) if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) return; - if (!eth_current) + if (!eth_get_dev()) return; if (first_restart) - first_failed = eth_current; + first_failed = eth_get_dev(); - eth_current = eth_current->next; + eth_set_current_to_next(); eth_current_changed(); - if (first_failed == eth_current) + if (first_failed == eth_get_dev()) NetRestartWrap = 1; } @@ -513,7 +520,7 @@ void eth_set_current(void) struct eth_device *old_current; int env_id; - if (!eth_current) /* XXX no current */ + if (!eth_get_dev()) /* XXX no current */ return; env_id = get_env_id(); @@ -522,18 +529,18 @@ void eth_set_current(void) env_changed_id = env_id; } if (act != NULL) { - old_current = eth_current; + old_current = eth_get_dev(); do { - if (strcmp(eth_current->name, act) == 0) + if (strcmp(eth_get_name(), act) == 0) return; - eth_current = eth_current->next; - } while (old_current != eth_current); + eth_set_current_to_next(); + } while (old_current != eth_get_dev()); } eth_current_changed(); } -char *eth_get_name(void) +const char *eth_get_name(void) { - return eth_current ? eth_current->name : "unknown"; + return eth_get_dev() ? eth_get_dev()->name : "unknown"; } -- 1.7.11.5