From 5700bb63522c2af9276f25a15448b61f19d72841 Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Tue, 27 Jul 2010 18:35:08 -0400 Subject: miiphy: constify device name The driver name does not need to be writable, so constify it. Signed-off-by: Mike Frysinger Signed-off-by: Ben Warren --- common/cmd_mii.c | 2 +- common/miiphyutil.c | 41 +++++++++++++++++++++-------------------- 2 files changed, 22 insertions(+), 21 deletions(-) (limited to 'common') diff --git a/common/cmd_mii.c b/common/cmd_mii.c index bb941862db6..3ea493ba492 100644 --- a/common/cmd_mii.c +++ b/common/cmd_mii.c @@ -299,7 +299,7 @@ int do_mii (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[]) unsigned char addr, reg; unsigned short data; int rcode = 0; - char *devname; + const char *devname; if (argc < 2) return cmd_usage(cmdtp); diff --git a/common/miiphyutil.c b/common/miiphyutil.c index 4b186dd28c0..a299b129a24 100644 --- a/common/miiphyutil.c +++ b/common/miiphyutil.c @@ -46,10 +46,10 @@ struct mii_dev { struct list_head link; - char *name; - int (*read) (char *devname, unsigned char addr, + const char *name; + int (*read) (const char *devname, unsigned char addr, unsigned char reg, unsigned short *value); - int (*write) (char *devname, unsigned char addr, + int (*write) (const char *devname, unsigned char addr, unsigned char reg, unsigned short value); }; @@ -60,7 +60,7 @@ static struct mii_dev *current_mii; * * Initialize global data. Need to be called before any other miiphy routine. */ -void miiphy_init () +void miiphy_init(void) { INIT_LIST_HEAD (&mii_devs); current_mii = NULL; @@ -70,16 +70,17 @@ void miiphy_init () * * Register read and write MII access routines for the device . */ -void miiphy_register (char *name, - int (*read) (char *devname, unsigned char addr, +void miiphy_register(const char *name, + int (*read) (const char *devname, unsigned char addr, unsigned char reg, unsigned short *value), - int (*write) (char *devname, unsigned char addr, + int (*write) (const char *devname, unsigned char addr, unsigned char reg, unsigned short value)) { struct list_head *entry; struct mii_dev *new_dev; struct mii_dev *miidev; unsigned int name_len; + char *new_name; /* check if we have unique name */ list_for_each (entry, &mii_devs) { @@ -107,9 +108,9 @@ void miiphy_register (char *name, INIT_LIST_HEAD (&new_dev->link); new_dev->read = read; new_dev->write = write; - new_dev->name = (char *)(new_dev + 1); - strncpy (new_dev->name, name, name_len); - new_dev->name[name_len] = '\0'; + new_dev->name = new_name = (char *)(new_dev + 1); + strncpy (new_name, name, name_len); + new_name[name_len] = '\0'; debug ("miiphy_register: added '%s', read=0x%08lx, write=0x%08lx\n", new_dev->name, new_dev->read, new_dev->write); @@ -121,7 +122,7 @@ void miiphy_register (char *name, current_mii = new_dev; } -int miiphy_set_current_dev (char *devname) +int miiphy_set_current_dev(const char *devname) { struct list_head *entry; struct mii_dev *dev; @@ -139,7 +140,7 @@ int miiphy_set_current_dev (char *devname) return 1; } -char *miiphy_get_current_dev () +const char *miiphy_get_current_dev(void) { if (current_mii) return current_mii->name; @@ -155,7 +156,7 @@ char *miiphy_get_current_dev () * Returns: * 0 on success */ -int miiphy_read (char *devname, unsigned char addr, unsigned char reg, +int miiphy_read(const char *devname, unsigned char addr, unsigned char reg, unsigned short *value) { struct list_head *entry; @@ -192,7 +193,7 @@ int miiphy_read (char *devname, unsigned char addr, unsigned char reg, * Returns: * 0 on success */ -int miiphy_write (char *devname, unsigned char addr, unsigned char reg, +int miiphy_write(const char *devname, unsigned char addr, unsigned char reg, unsigned short value) { struct list_head *entry; @@ -252,7 +253,7 @@ void miiphy_listdev (void) * Returns: * 0 on success */ -int miiphy_info (char *devname, unsigned char addr, unsigned int *oui, +int miiphy_info(const char *devname, unsigned char addr, unsigned int *oui, unsigned char *model, unsigned char *rev) { unsigned int reg = 0; @@ -290,7 +291,7 @@ int miiphy_info (char *devname, unsigned char addr, unsigned int *oui, * Returns: * 0 on success */ -int miiphy_reset (char *devname, unsigned char addr) +int miiphy_reset(const char *devname, unsigned char addr) { unsigned short reg; int timeout = 500; @@ -332,7 +333,7 @@ int miiphy_reset (char *devname, unsigned char addr) * * Determine the ethernet speed (10/100/1000). Return 10 on error. */ -int miiphy_speed (char *devname, unsigned char addr) +int miiphy_speed(const char *devname, unsigned char addr) { u16 bmcr, anlpar; @@ -386,7 +387,7 @@ miiphy_read_failed: * * Determine full/half duplex. Return half on error. */ -int miiphy_duplex (char *devname, unsigned char addr) +int miiphy_duplex(const char *devname, unsigned char addr) { u16 bmcr, anlpar; @@ -446,7 +447,7 @@ miiphy_read_failed: * Return 1 if PHY supports 1000BASE-X, 0 if PHY supports 10BASE-T/100BASE-TX/ * 1000BASE-T, or on error. */ -int miiphy_is_1000base_x (char *devname, unsigned char addr) +int miiphy_is_1000base_x(const char *devname, unsigned char addr) { #if defined(CONFIG_PHY_GIGE) u16 exsr; @@ -467,7 +468,7 @@ int miiphy_is_1000base_x (char *devname, unsigned char addr) * * Determine link status */ -int miiphy_link (char *devname, unsigned char addr) +int miiphy_link(const char *devname, unsigned char addr) { unsigned short reg; -- cgit v1.2.3 From 0daac97801fa5989d3740487aa824fbab9ff0503 Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Tue, 27 Jul 2010 18:35:09 -0400 Subject: miiphy: unify device list lookup Rather than have every func re-implement the list walking code, do it one local function. This shrinks the resulting object code a little while making the source much more manageable. Signed-off-by: Mike Frysinger Signed-off-by: Ben Warren --- common/miiphyutil.c | 100 +++++++++++++++++++++------------------------------- 1 file changed, 40 insertions(+), 60 deletions(-) (limited to 'common') diff --git a/common/miiphyutil.c b/common/miiphyutil.c index a299b129a24..e055c08fbd6 100644 --- a/common/miiphyutil.c +++ b/common/miiphyutil.c @@ -56,6 +56,30 @@ struct mii_dev { static struct list_head mii_devs; static struct mii_dev *current_mii; +/* + * Lookup the mii_dev struct by the registered device name. + */ +static struct mii_dev *miiphy_get_dev_by_name(const char *devname, int quiet) +{ + struct list_head *entry; + struct mii_dev *dev; + + if (!devname) { + printf("NULL device name!\n"); + return NULL; + } + + list_for_each(entry, &mii_devs) { + dev = list_entry(entry, struct mii_dev, link); + if (strcmp(dev->name, devname) == 0) + return dev; + } + + if (!quiet) + printf("No such device: %s\n", devname); + return NULL; +} + /***************************************************************************** * * Initialize global data. Need to be called before any other miiphy routine. @@ -76,20 +100,15 @@ void miiphy_register(const char *name, int (*write) (const char *devname, unsigned char addr, unsigned char reg, unsigned short value)) { - struct list_head *entry; struct mii_dev *new_dev; - struct mii_dev *miidev; unsigned int name_len; char *new_name; /* check if we have unique name */ - list_for_each (entry, &mii_devs) { - miidev = list_entry (entry, struct mii_dev, link); - if (strcmp (miidev->name, name) == 0) { - printf ("miiphy_register: non unique device name " - "'%s'\n", name); - return; - } + new_dev = miiphy_get_dev_by_name(name, 1); + if (new_dev) { + printf("miiphy_register: non unique device name '%s'\n", name); + return; } /* allocate memory */ @@ -124,19 +143,14 @@ void miiphy_register(const char *name, int miiphy_set_current_dev(const char *devname) { - struct list_head *entry; struct mii_dev *dev; - list_for_each (entry, &mii_devs) { - dev = list_entry (entry, struct mii_dev, link); - - if (strcmp (devname, dev->name) == 0) { - current_mii = dev; - return 0; - } + dev = miiphy_get_dev_by_name(devname, 0); + if (dev) { + current_mii = dev; + return 0; } - printf ("No such device: %s\n", devname); return 1; } @@ -159,30 +173,13 @@ const char *miiphy_get_current_dev(void) int miiphy_read(const char *devname, unsigned char addr, unsigned char reg, unsigned short *value) { - struct list_head *entry; struct mii_dev *dev; - int found_dev = 0; - int read_ret = 0; - - if (!devname) { - printf ("NULL device name!\n"); - return 1; - } - - list_for_each (entry, &mii_devs) { - dev = list_entry (entry, struct mii_dev, link); - - if (strcmp (devname, dev->name) == 0) { - found_dev = 1; - read_ret = dev->read (devname, addr, reg, value); - break; - } - } - if (found_dev == 0) - printf ("No such device: %s\n", devname); + dev = miiphy_get_dev_by_name(devname, 0); + if (dev) + return dev->read(devname, addr, reg, value); - return ((found_dev) ? read_ret : 1); + return 1; } /***************************************************************************** @@ -196,30 +193,13 @@ int miiphy_read(const char *devname, unsigned char addr, unsigned char reg, int miiphy_write(const char *devname, unsigned char addr, unsigned char reg, unsigned short value) { - struct list_head *entry; struct mii_dev *dev; - int found_dev = 0; - int write_ret = 0; - - if (!devname) { - printf ("NULL device name!\n"); - return 1; - } - list_for_each (entry, &mii_devs) { - dev = list_entry (entry, struct mii_dev, link); + dev = miiphy_get_dev_by_name(devname, 0); + if (dev) + return dev->write(devname, addr, reg, value); - if (strcmp (devname, dev->name) == 0) { - found_dev = 1; - write_ret = dev->write (devname, addr, reg, value); - break; - } - } - - if (found_dev == 0) - printf ("No such device: %s\n", devname); - - return ((found_dev) ? write_ret : 1); + return 1; } /***************************************************************************** -- cgit v1.2.3 From ede16ea32da7a37f892bf63dcb43c16118ba39d6 Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Tue, 27 Jul 2010 18:35:10 -0400 Subject: miiphy: leverage current_mii cache more For code that uses miiphy_{read,write}, every call invokes a full look up of the mii list. There is already a "current_mii" cache that is used by some code, but have the miiphy_{read,write} function use it as well. This does increase the code size slightly, but I think it's worth it. Signed-off-by: Mike Frysinger Signed-off-by: Ben Warren --- common/miiphyutil.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'common') diff --git a/common/miiphyutil.c b/common/miiphyutil.c index e055c08fbd6..9cf845f605d 100644 --- a/common/miiphyutil.c +++ b/common/miiphyutil.c @@ -162,6 +162,20 @@ const char *miiphy_get_current_dev(void) return NULL; } +static struct mii_dev *miiphy_get_active_dev(const char *devname) +{ + /* If the current mii is the one we want, return it */ + if (current_mii) + if (strcmp(current_mii->name, devname) == 0) + return current_mii; + + /* Otherwise, set the active one to the one we want */ + if (miiphy_set_current_dev(devname)) + return NULL; + else + return current_mii; +} + /***************************************************************************** * * Read to variable from the PHY attached to device , @@ -175,7 +189,7 @@ int miiphy_read(const char *devname, unsigned char addr, unsigned char reg, { struct mii_dev *dev; - dev = miiphy_get_dev_by_name(devname, 0); + dev = miiphy_get_active_dev(devname); if (dev) return dev->read(devname, addr, reg, value); @@ -195,7 +209,7 @@ int miiphy_write(const char *devname, unsigned char addr, unsigned char reg, { struct mii_dev *dev; - dev = miiphy_get_dev_by_name(devname, 0); + dev = miiphy_get_active_dev(devname); if (dev) return dev->write(devname, addr, reg, value); -- cgit v1.2.3